All language subtitles for Harvard CS50 (2023) – Full Computer Science University Course - YouTube - English

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 Download
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) Download
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:00:00,240 --> 00:00:05,319 this is a Harvard University course 2 00:00:02,360 --> 00:00:07,360 taught by Dr David Meen and we are proud 3 00:00:05,319 --> 00:00:10,080 to bring it to the freeo camp YouTube 4 00:00:07,360 --> 00:00:12,240 channel throughout a series of lectures 5 00:00:10,080 --> 00:00:14,599 Dr men will teach you how to think 6 00:00:12,240 --> 00:00:16,720 algorithmically and solve problems 7 00:00:14,599 --> 00:00:19,199 efficiently and make sure to check the 8 00:00:16,720 --> 00:00:22,160 description for a lot of extra resources 9 00:00:19,199 --> 00:00:22,160 that go along with the 10 00:00:23,910 --> 00:00:31,960 [Music] 11 00:00:28,960 --> 00:00:31,960 course 12 00:00:32,409 --> 00:00:42,549 [Music] 13 00:00:47,050 --> 00:00:50,120 [Music] 14 00:00:56,390 --> 00:01:03,439 [Music] 15 00:01:06,330 --> 00:01:19,689 [Music] 16 00:01:25,890 --> 00:01:38,680 [Music] 17 00:01:34,479 --> 00:01:40,399 all right this is cs50 Harvard 18 00:01:38,680 --> 00:01:42,119 University's introduction to the 19 00:01:40,399 --> 00:01:44,040 intellectual Enterprises of computer 20 00:01:42,119 --> 00:01:45,640 science and the Arts of programming my 21 00:01:44,040 --> 00:01:49,159 name is David man and I actually took 22 00:01:45,640 --> 00:01:50,759 this course myself back in 1996 I was a 23 00:01:49,159 --> 00:01:52,360 sophomore at the time I was actually 24 00:01:50,759 --> 00:01:54,439 concentrating in government because a 25 00:01:52,360 --> 00:01:57,280 year prior as a first year I'd come into 26 00:01:54,439 --> 00:01:59,439 Harvard thinking that I liked high stre 27 00:01:57,280 --> 00:02:01,159 and constitutional law and sort of 28 00:01:59,439 --> 00:02:03,200 similar classes in high school and so 29 00:02:01,159 --> 00:02:05,479 when I got here I rather gravitated 30 00:02:03,200 --> 00:02:07,520 toward that which was familiar I figured 31 00:02:05,479 --> 00:02:09,239 if I liked and if I were good at that 32 00:02:07,520 --> 00:02:10,920 particular subject in high school then 33 00:02:09,239 --> 00:02:13,200 that's presumably who I'm supposed to be 34 00:02:10,920 --> 00:02:15,640 here but it wasn't until sophomore year 35 00:02:13,200 --> 00:02:17,480 that I got up the nerve to step foot in 36 00:02:15,640 --> 00:02:19,400 the cs50 classroom and even then it was 37 00:02:17,480 --> 00:02:21,959 only out of curiosity like I had no 38 00:02:19,400 --> 00:02:24,120 intention of studying computer science 39 00:02:21,959 --> 00:02:26,200 of even taking cs50 when I got to campus 40 00:02:24,120 --> 00:02:28,599 but people were talking about it and 41 00:02:26,200 --> 00:02:30,879 there was a lot of like beware and it 42 00:02:28,599 --> 00:02:32,400 was it perhaps for the initiated only 43 00:02:30,879 --> 00:02:35,000 and I didn't really know ultimately what 44 00:02:32,400 --> 00:02:37,640 computer science was but for me the sort 45 00:02:35,000 --> 00:02:38,840 of light bulb went off and I found that 46 00:02:37,640 --> 00:02:40,480 contrary to what I had seen in high 47 00:02:38,840 --> 00:02:42,120 school where I saw friends of mine like 48 00:02:40,480 --> 00:02:44,159 programming away in the computer lab 49 00:02:42,120 --> 00:02:46,000 heads down sort of anti-socially just 50 00:02:44,159 --> 00:02:48,120 doing whatever it was they were doing it 51 00:02:46,000 --> 00:02:49,760 really wasn't that once I got to this 52 00:02:48,120 --> 00:02:51,560 particular class in this particular 53 00:02:49,760 --> 00:02:53,080 place it was much more about problem 54 00:02:51,560 --> 00:02:55,080 solving more generally and just learning 55 00:02:53,080 --> 00:02:56,280 how to express yourself in code in 56 00:02:55,080 --> 00:02:57,760 different languages so that you can 57 00:02:56,280 --> 00:02:59,959 actually solve problems of interest to 58 00:02:57,760 --> 00:03:01,400 you even if you have no intention of 59 00:02:59,959 --> 00:03:03,080 being a computer scientist or an 60 00:03:01,400 --> 00:03:04,959 engineer but just want to be able to 61 00:03:03,080 --> 00:03:07,280 solve problems analyze data do 62 00:03:04,959 --> 00:03:09,200 interesting things in the Arts Humanity 63 00:03:07,280 --> 00:03:11,680 social sciences physical sciences or 64 00:03:09,200 --> 00:03:13,280 really any other field and indeed this 65 00:03:11,680 --> 00:03:14,959 particular path led me to computer 66 00:03:13,280 --> 00:03:17,480 science but the hope for cs50 more 67 00:03:14,959 --> 00:03:19,599 generally is that indeed you just find 68 00:03:17,480 --> 00:03:21,080 your way to applying principles that 69 00:03:19,599 --> 00:03:23,360 you'll learn over the coming months to 70 00:03:21,080 --> 00:03:25,640 whatever field is of interest to you 71 00:03:23,360 --> 00:03:27,920 with that said it was definitely a lot 72 00:03:25,640 --> 00:03:30,319 of work and not without its frustrations 73 00:03:27,920 --> 00:03:31,879 for me but there was no better feel 74 00:03:30,319 --> 00:03:33,080 then like banging your head proverbial 75 00:03:31,879 --> 00:03:35,239 against the wall for some number of 76 00:03:33,080 --> 00:03:37,879 hours even days trying to fix a bug a 77 00:03:35,239 --> 00:03:40,200 mistake in your code and then oh my God 78 00:03:37,879 --> 00:03:42,120 the rush of emotion of accomplishment of 79 00:03:40,200 --> 00:03:43,720 pride of exhaustion when you finally 80 00:03:42,120 --> 00:03:45,720 solve some problem that's really been 81 00:03:43,720 --> 00:03:47,760 weighing on you it's just so incredibly 82 00:03:45,720 --> 00:03:49,480 gratifying but also empowering because 83 00:03:47,760 --> 00:03:52,040 unlike a lot of fields the computer 84 00:03:49,480 --> 00:03:54,040 science was built by humans themselves 85 00:03:52,040 --> 00:03:56,319 and so if a human built this surely you 86 00:03:54,040 --> 00:03:58,040 another human can understand it as well 87 00:03:56,319 --> 00:03:59,519 and so even though there's going to be 88 00:03:58,040 --> 00:04:01,400 some distractions along the way you're 89 00:03:59,519 --> 00:04:02,840 going going to see what looks incredibly 90 00:04:01,400 --> 00:04:05,480 cryptic if you've never programmed 91 00:04:02,840 --> 00:04:07,040 before over time and with practice 92 00:04:05,480 --> 00:04:08,680 everything just starts to make more 93 00:04:07,040 --> 00:04:10,280 sense and with time and with practice 94 00:04:08,680 --> 00:04:12,400 you just get better at this particular 95 00:04:10,280 --> 00:04:14,400 field and indeed really the key to 96 00:04:12,400 --> 00:04:16,400 success in programming in general is 97 00:04:14,400 --> 00:04:18,440 just to allow yourself enough time and 98 00:04:16,400 --> 00:04:20,120 so at least thankfully I quickly got 99 00:04:18,440 --> 00:04:21,560 into the habit of starting early in the 100 00:04:20,120 --> 00:04:23,160 week for instance when writing actual 101 00:04:21,560 --> 00:04:24,440 code why because you're going to like 102 00:04:23,160 --> 00:04:25,919 run up against a wall you're not going 103 00:04:24,440 --> 00:04:27,520 to see some bug something's not going to 104 00:04:25,919 --> 00:04:29,160 jump out at you and that's fine that's 105 00:04:27,520 --> 00:04:30,800 when you sort of call it a day take a 106 00:04:29,160 --> 00:04:32,240 break move move on to something else and 107 00:04:30,800 --> 00:04:34,280 then just come back to it and that's 108 00:04:32,240 --> 00:04:35,600 what keeps programming fund for me even 109 00:04:34,280 --> 00:04:37,479 all of these years later whether it's 110 00:04:35,600 --> 00:04:39,800 teaching or actually applying it but 111 00:04:37,479 --> 00:04:41,400 there's uh down the road a history of uh 112 00:04:39,800 --> 00:04:43,000 an MIT hack and it looked a little 113 00:04:41,400 --> 00:04:44,680 something like this in yester year and 114 00:04:43,000 --> 00:04:46,720 there was a little sign the MIT students 115 00:04:44,680 --> 00:04:48,680 when they made this hack uh on the wall 116 00:04:46,720 --> 00:04:51,080 that says getting an education from MIT 117 00:04:48,680 --> 00:04:52,680 is like drinking from a fire hose which 118 00:04:51,080 --> 00:04:54,360 indeed they have connected to what 119 00:04:52,680 --> 00:04:55,840 should have been otherwise just a water 120 00:04:54,360 --> 00:04:57,560 fountain and that's going to be what it 121 00:04:55,840 --> 00:04:59,520 feels like sometimes not just in 122 00:04:57,560 --> 00:05:01,639 computer science per se but just in UNAM 123 00:04:59,520 --> 00:05:04,280 familiar field if you're not from stem 124 00:05:01,639 --> 00:05:06,280 if you're not from CS that's fine but so 125 00:05:04,280 --> 00:05:08,800 much of it ultimately is going to be 126 00:05:06,280 --> 00:05:10,840 absorbed by you and going to be uh 127 00:05:08,800 --> 00:05:12,400 within your grasp by term Zen so just 128 00:05:10,840 --> 00:05:14,400 keep in mind that's that's very much the 129 00:05:12,400 --> 00:05:16,800 intent but you'll be amazed what you're 130 00:05:14,400 --> 00:05:19,479 able to create to accomplish uh just 131 00:05:16,800 --> 00:05:21,280 three or so months hence indeed 2third 132 00:05:19,479 --> 00:05:24,080 of you contrary to what you might think 133 00:05:21,280 --> 00:05:25,639 or assume have never taken a CS class 134 00:05:24,080 --> 00:05:26,800 before so it's absolutely not the case 135 00:05:25,639 --> 00:05:28,560 that the person to the left and to the 136 00:05:26,800 --> 00:05:30,639 right surely must know more than you 137 00:05:28,560 --> 00:05:32,120 indeed it's quite the opposite and as 138 00:05:30,639 --> 00:05:33,800 you'll see in the coming weeks as you 139 00:05:32,120 --> 00:05:35,639 write your own code and solve your own 140 00:05:33,800 --> 00:05:37,120 problems what ultimately matters in this 141 00:05:35,639 --> 00:05:38,759 course is not so much where you end up 142 00:05:37,120 --> 00:05:41,360 relative to your classmates but where 143 00:05:38,759 --> 00:05:43,120 you end up relative to yourself when you 144 00:05:41,360 --> 00:05:44,880 began and it really is all about that 145 00:05:43,120 --> 00:05:47,400 Delta whether you've programmed or not 146 00:05:44,880 --> 00:05:49,560 just getting something out of a class 147 00:05:47,400 --> 00:05:51,000 like this and and if it does take time 148 00:05:49,560 --> 00:05:52,479 and if you do feel those frustrations 149 00:05:51,000 --> 00:05:54,120 but you simultaneously eventually feel 150 00:05:52,479 --> 00:05:55,880 that sense of accomplishment that just 151 00:05:54,120 --> 00:05:58,080 means it's all working and indeed 152 00:05:55,880 --> 00:06:00,880 hopefully all the more worthwhile and 153 00:05:58,080 --> 00:06:02,160 gratifying ultimately as a result so 154 00:06:00,880 --> 00:06:04,560 what are we going to do in the coming 155 00:06:02,160 --> 00:06:06,160 week so here we are in week zero we'll 156 00:06:04,560 --> 00:06:07,840 soon see why computers and computer 157 00:06:06,160 --> 00:06:10,039 scientists start counting if you will 158 00:06:07,840 --> 00:06:11,759 from zero but week zero is which is one 159 00:06:10,039 --> 00:06:13,199 in which we explore computational 160 00:06:11,759 --> 00:06:14,759 thinking sort of thinking like a 161 00:06:13,199 --> 00:06:16,759 computer and starting to clean up your 162 00:06:14,759 --> 00:06:18,400 thought processes getting you to think 163 00:06:16,759 --> 00:06:19,840 to solve problems more methodically and 164 00:06:18,400 --> 00:06:21,440 then ultimately Translating that into 165 00:06:19,840 --> 00:06:23,720 code and some of you might recognize 166 00:06:21,440 --> 00:06:25,599 this environment here AKA scratch 167 00:06:23,720 --> 00:06:27,440 coincidentally also from MIT you might 168 00:06:25,599 --> 00:06:29,000 have used it in in grade school we'll 169 00:06:27,440 --> 00:06:30,800 use it today and a little bit this 170 00:06:29,000 --> 00:06:33,080 weekend in the course's first homework 171 00:06:30,800 --> 00:06:34,639 assignment or problem set but not so 172 00:06:33,080 --> 00:06:36,160 much to kind of play around in a way 173 00:06:34,639 --> 00:06:38,479 that you might have if you did use it in 174 00:06:36,160 --> 00:06:39,960 yester year but to explore ideas of 175 00:06:38,479 --> 00:06:42,080 computer science and programming that 176 00:06:39,960 --> 00:06:44,199 we're going to use and reuse every week 177 00:06:42,080 --> 00:06:46,120 Hereafter as well uh thereafter we're 178 00:06:44,199 --> 00:06:48,039 going to transition just next week to 179 00:06:46,120 --> 00:06:49,400 week one so to speak whereby we'll 180 00:06:48,039 --> 00:06:51,680 introduce you to a more traditional 181 00:06:49,400 --> 00:06:53,800 language a lower level language an older 182 00:06:51,680 --> 00:06:55,400 language called C and in C you're going 183 00:06:53,800 --> 00:06:57,000 to use your keyboard not so much your 184 00:06:55,400 --> 00:06:58,400 mouse and pointing and clicking but 185 00:06:57,000 --> 00:06:59,720 you're going to write code that soon is 186 00:06:58,400 --> 00:07:01,560 going to look a little something like 187 00:06:59,720 --> 00:07:03,000 this and if you programmed before can 188 00:07:01,560 --> 00:07:04,479 probably glean what this is going to do 189 00:07:03,000 --> 00:07:07,000 if you've never programmed before which 190 00:07:04,479 --> 00:07:08,879 is the case for most of you this too 191 00:07:07,000 --> 00:07:10,800 will soon make sense but this is the 192 00:07:08,879 --> 00:07:12,840 most canonical program that most any 193 00:07:10,800 --> 00:07:15,039 programmer ever writes called hello 194 00:07:12,840 --> 00:07:17,120 world and indeed that and all of the 195 00:07:15,039 --> 00:07:19,319 surrounding syntax above and below just 196 00:07:17,120 --> 00:07:20,960 that sentence hello world will soon make 197 00:07:19,319 --> 00:07:23,120 all the more sense you'll learn how to 198 00:07:20,960 --> 00:07:24,520 use industry standard tools so to speak 199 00:07:23,120 --> 00:07:26,520 pictured here something called Visual 200 00:07:24,520 --> 00:07:28,319 Studio code or vs code you'll use a 201 00:07:26,520 --> 00:07:29,599 cloud-based version of it initially so 202 00:07:28,319 --> 00:07:31,199 you don't have to suffer with any 203 00:07:29,599 --> 00:07:33,479 technical difficulties or headaches like 204 00:07:31,199 --> 00:07:34,960 that it'll just work right off the bat 205 00:07:33,479 --> 00:07:37,360 but we'll use that tool and others 206 00:07:34,960 --> 00:07:39,400 ultimately to then explore ideas in 207 00:07:37,360 --> 00:07:41,039 computer science principles that you can 208 00:07:39,400 --> 00:07:42,919 apply and we'll take a look underneath 209 00:07:41,039 --> 00:07:45,800 the hood so to speak of your computer at 210 00:07:42,919 --> 00:07:47,199 your memory or Ram Random Access Memory 211 00:07:45,800 --> 00:07:49,000 where all of the data is ultimately 212 00:07:47,199 --> 00:07:51,360 going to be stored we'll also take a 213 00:07:49,000 --> 00:07:53,120 look thereafter at bugs a bug is a 214 00:07:51,360 --> 00:07:55,360 mistake in a program here is an actual 215 00:07:53,120 --> 00:07:57,479 bug in an actual computer in yester year 216 00:07:55,360 --> 00:07:59,400 but we'll teach you how to debug 217 00:07:57,479 --> 00:08:01,479 programs find your own mistakes find 218 00:07:59,400 --> 00:08:04,280 others mistakes and improve that code as 219 00:08:01,479 --> 00:08:05,879 well we'll transition then to algorithm 220 00:08:04,280 --> 00:08:07,720 step-by-step instructions for solving 221 00:08:05,879 --> 00:08:10,199 some problems which we'll touch on today 222 00:08:07,720 --> 00:08:11,520 too and if you picture here this is 223 00:08:10,199 --> 00:08:12,879 actually a pretty representative 224 00:08:11,520 --> 00:08:14,159 problems odds are you haven't had to 225 00:08:12,879 --> 00:08:16,400 deal with something like this but it's 226 00:08:14,159 --> 00:08:18,560 representative sorting for instance if 227 00:08:16,400 --> 00:08:20,039 you think of each of these small bars as 228 00:08:18,560 --> 00:08:22,120 being a small number each of the bigger 229 00:08:20,039 --> 00:08:24,080 bars as being a bigger number you might 230 00:08:22,120 --> 00:08:26,479 wonder well how is you could do as a 231 00:08:24,080 --> 00:08:28,360 human sort all of these bars like get 232 00:08:26,479 --> 00:08:30,560 all the short bars over here all the big 233 00:08:28,360 --> 00:08:31,840 bars over there well odds are if you're 234 00:08:30,560 --> 00:08:33,240 like me you would probably kind of 235 00:08:31,840 --> 00:08:35,039 eyeball it and if you could physically 236 00:08:33,240 --> 00:08:36,599 interact you might just start grabbing 237 00:08:35,039 --> 00:08:38,240 the smallest elements first put them 238 00:08:36,599 --> 00:08:40,440 over on the left maybe grab the biggest 239 00:08:38,240 --> 00:08:42,039 elements put them over on the right but 240 00:08:40,440 --> 00:08:43,800 what's your algorithm there like how 241 00:08:42,039 --> 00:08:45,600 would you teach someone younger than you 242 00:08:43,800 --> 00:08:47,600 who's never done that before how to do 243 00:08:45,600 --> 00:08:49,720 it how would you compel your Mac or PC 244 00:08:47,600 --> 00:08:51,000 or phone to do something like that you 245 00:08:49,720 --> 00:08:52,720 can't just kind of wave your hand and 246 00:08:51,000 --> 00:08:54,240 say oh you know figure it out move 247 00:08:52,720 --> 00:08:55,680 things around you have to express 248 00:08:54,240 --> 00:08:57,880 yourself more methodically so we'll 249 00:08:55,680 --> 00:08:59,160 translate even ideas like this into code 250 00:08:57,880 --> 00:09:01,000 to and that's what the Googles and 251 00:08:59,160 --> 00:09:02,800 others of the world are doing constantly 252 00:09:01,000 --> 00:09:05,240 as they sort and organize the world's 253 00:09:02,800 --> 00:09:06,760 information we'll use metaphors along 254 00:09:05,240 --> 00:09:08,440 the way if it helps we'll talk about 255 00:09:06,760 --> 00:09:10,720 your computer's memory is kind of being 256 00:09:08,440 --> 00:09:12,880 like a postal address like every mailbox 257 00:09:10,720 --> 00:09:14,800 in the world has some form of postal 258 00:09:12,880 --> 00:09:16,200 address Street city state country and 259 00:09:14,800 --> 00:09:18,079 the like and it turns out that's how 260 00:09:16,200 --> 00:09:19,720 your Mac your PC and your phone also 261 00:09:18,079 --> 00:09:21,399 work you've got a whole bunch of memory 262 00:09:19,720 --> 00:09:22,800 like the picture before but you can 263 00:09:21,399 --> 00:09:24,680 think of it really as individual 264 00:09:22,800 --> 00:09:26,560 mailboxes and you can put anything you 265 00:09:24,680 --> 00:09:29,360 want in those mailboxes and you can go 266 00:09:26,560 --> 00:09:30,959 to a mailbox to grab information that 267 00:09:29,360 --> 00:09:32,519 from it so at the end of the day that's 268 00:09:30,959 --> 00:09:34,480 really all your computer's doing with 269 00:09:32,519 --> 00:09:36,800 information it's just kind of organizing 270 00:09:34,480 --> 00:09:38,640 it not into mailboxes per se but a term 271 00:09:36,800 --> 00:09:40,720 you probably know called bites for 272 00:09:38,640 --> 00:09:43,600 instance instead we'll talk about 273 00:09:40,720 --> 00:09:45,480 problems that arise even nowadays in 274 00:09:43,600 --> 00:09:48,000 fact most of you are familiar with your 275 00:09:45,480 --> 00:09:50,240 Mac PC even phone like spontaneously 276 00:09:48,000 --> 00:09:52,079 rebooting sometimes crashing the little 277 00:09:50,240 --> 00:09:53,839 Annoying spinning beach ball or 278 00:09:52,079 --> 00:09:55,880 Hourglass Icon that Happ like what is 279 00:09:53,839 --> 00:09:57,760 with that well those are just bugs in 280 00:09:55,880 --> 00:09:59,720 programs that humans at Apple and Google 281 00:09:57,760 --> 00:10:01,279 and Microsoft and others they screwed up 282 00:09:59,720 --> 00:10:03,079 and they wrote buggy code and your 283 00:10:01,279 --> 00:10:05,480 computer when it encounters those 284 00:10:03,079 --> 00:10:07,440 mistakes doesn't know what to do and so 285 00:10:05,480 --> 00:10:09,680 nine times out of 10 so to speak it just 286 00:10:07,440 --> 00:10:10,959 crashes or freezes or the like but that 287 00:10:09,680 --> 00:10:12,480 kind of stuff will make more sense so 288 00:10:10,959 --> 00:10:14,480 even the real world will make sense and 289 00:10:12,480 --> 00:10:16,519 pictured here or some lower level terms 290 00:10:14,480 --> 00:10:17,959 will eventually get to Mid semester but 291 00:10:16,519 --> 00:10:20,000 generally speaking when something is 292 00:10:17,959 --> 00:10:21,560 going this way as per this arrow and 293 00:10:20,000 --> 00:10:23,519 something is going this way as per this 294 00:10:21,560 --> 00:10:25,200 Arrow like that does not end well and 295 00:10:23,519 --> 00:10:27,000 that often is what happens when your 296 00:10:25,200 --> 00:10:28,760 computer crashes someone's using memory 297 00:10:27,000 --> 00:10:30,120 up here but someone else is using memory 298 00:10:28,760 --> 00:10:32,279 down here here and then they're not 299 00:10:30,120 --> 00:10:34,240 really talking left hand and right hand 300 00:10:32,279 --> 00:10:36,079 so that is just a high level overview of 301 00:10:34,240 --> 00:10:38,079 some of the problems we'll encounter but 302 00:10:36,079 --> 00:10:39,320 we'll focus to on data ultimately so 303 00:10:38,079 --> 00:10:41,680 picture here is something fairly 304 00:10:39,320 --> 00:10:43,079 technical called a hash table it's an 305 00:10:41,680 --> 00:10:45,240 amalgam of something we're going to soon 306 00:10:43,079 --> 00:10:46,800 call an array and also something we call 307 00:10:45,240 --> 00:10:49,120 a linked list and these are just fancy 308 00:10:46,800 --> 00:10:51,880 terms for describing how you can 309 00:10:49,120 --> 00:10:54,200 organize information even more flexibly 310 00:10:51,880 --> 00:10:56,320 than just putting individual ma values 311 00:10:54,200 --> 00:10:58,639 in mailboxes like how could you build 312 00:10:56,320 --> 00:11:00,279 structures like actual data structures 313 00:10:58,639 --> 00:11:01,959 so to speak speak two-dimensional 314 00:11:00,279 --> 00:11:04,000 structures at that and so what you're 315 00:11:01,959 --> 00:11:05,360 seeing here is a glimpse uh as some of 316 00:11:04,000 --> 00:11:07,160 you might have recognized of some Harry 317 00:11:05,360 --> 00:11:09,360 Potter Universe names but they're 318 00:11:07,160 --> 00:11:10,839 organized somewhat alphabetically and 319 00:11:09,360 --> 00:11:12,440 notice that anytime there's multiple 320 00:11:10,839 --> 00:11:14,800 people with a name that starts with H 321 00:11:12,440 --> 00:11:16,920 like hermion Harry and Hagrid well they 322 00:11:14,800 --> 00:11:18,480 can't all fit in that mailbox if each of 323 00:11:16,920 --> 00:11:20,079 these squares along the left is that 324 00:11:18,480 --> 00:11:21,959 same mailbox so you have to kind of 325 00:11:20,079 --> 00:11:23,920 chain them together well you'll learn 326 00:11:21,959 --> 00:11:26,120 how to do that in code so that even if 327 00:11:23,920 --> 00:11:27,560 you get more data than you expect if 328 00:11:26,120 --> 00:11:29,040 your business is booming and you're some 329 00:11:27,560 --> 00:11:31,120 web-based business how do you keep 330 00:11:29,040 --> 00:11:34,120 adding and adding information to your 331 00:11:31,120 --> 00:11:35,880 software to actually keep up with it but 332 00:11:34,120 --> 00:11:37,720 this again is what codee's going to soon 333 00:11:35,880 --> 00:11:39,800 look like as soon as next week in week 334 00:11:37,720 --> 00:11:42,519 one this here being C but we'll 335 00:11:39,800 --> 00:11:45,240 transition in a few weeks to a more uh 336 00:11:42,519 --> 00:11:47,079 modern higher level language so to speak 337 00:11:45,240 --> 00:11:48,800 called python indeed the course Very 338 00:11:47,079 --> 00:11:50,920 deliberately back in my day and now this 339 00:11:48,800 --> 00:11:52,680 introduces you first to C which funny 340 00:11:50,920 --> 00:11:55,040 enough most many people don't tend to 341 00:11:52,680 --> 00:11:57,800 program and certainly every day I you 342 00:11:55,040 --> 00:11:59,519 see generally September October November 343 00:11:57,800 --> 00:12:02,079 December when teaching C is 50 itself 344 00:11:59,519 --> 00:12:03,839 but it's everywhere nonetheless in fact 345 00:12:02,079 --> 00:12:06,160 even today's other languages with which 346 00:12:03,839 --> 00:12:09,600 you might be familiar like Python and 347 00:12:06,160 --> 00:12:11,320 Java and uh yet others still you see 348 00:12:09,600 --> 00:12:13,600 this same primitive language underneath 349 00:12:11,320 --> 00:12:15,040 the hood because it's so darn fast and 350 00:12:13,600 --> 00:12:17,199 as you'll learn over the coming weeks it 351 00:12:15,040 --> 00:12:18,680 really gives you access to and an 352 00:12:17,199 --> 00:12:20,320 understanding of what's going on 353 00:12:18,680 --> 00:12:22,000 conceptually down here so that 354 00:12:20,320 --> 00:12:23,800 thereafter after cs50 when you're 355 00:12:22,000 --> 00:12:26,000 writing code you can sort of think at a 356 00:12:23,800 --> 00:12:28,079 very high level what's actually going on 357 00:12:26,000 --> 00:12:30,240 so in fact in just a few weeks what 358 00:12:28,079 --> 00:12:32,360 looks like this C is going to look 359 00:12:30,240 --> 00:12:33,800 instead like this in Python and you'll 360 00:12:32,360 --> 00:12:35,839 better understand what's going on 361 00:12:33,800 --> 00:12:37,519 underneath the hood and odds are after 362 00:12:35,839 --> 00:12:40,240 this class you'll reach for a language 363 00:12:37,519 --> 00:12:41,680 like python more frequently than C but 364 00:12:40,240 --> 00:12:44,680 you're going to benefit from that bottom 365 00:12:41,680 --> 00:12:46,279 up understanding thereof thereafter and 366 00:12:44,680 --> 00:12:48,360 toward terms end we'll introduce you to 367 00:12:46,279 --> 00:12:50,120 a few other ideas like where do you put 368 00:12:48,360 --> 00:12:52,000 large amounts of data in things called 369 00:12:50,120 --> 00:12:53,880 databases not things like spreadsheets 370 00:12:52,000 --> 00:12:55,079 like here but actual databases where 371 00:12:53,880 --> 00:12:56,839 using those same kinds of data 372 00:12:55,079 --> 00:12:58,959 structures you lay things out in an 373 00:12:56,839 --> 00:13:00,399 interesting way in memory thereafter 374 00:12:58,959 --> 00:13:02,279 will transition to a very familiar 375 00:13:00,399 --> 00:13:04,199 environment that you and I use every day 376 00:13:02,279 --> 00:13:06,360 the web like the web has become rather 377 00:13:04,199 --> 00:13:08,600 the user interface or UI that we use 378 00:13:06,360 --> 00:13:10,800 everywhere on the uh laptops desktops 379 00:13:08,600 --> 00:13:13,199 and even mobile devices nowadays well 380 00:13:10,800 --> 00:13:14,800 pictured here is a language called HTML 381 00:13:13,199 --> 00:13:16,160 it's not a programming language it's a 382 00:13:14,800 --> 00:13:18,079 markup language and some of you might 383 00:13:16,160 --> 00:13:20,240 have made homepages or portfolios in the 384 00:13:18,079 --> 00:13:22,600 past but you'll understand what's going 385 00:13:20,240 --> 00:13:24,399 on here but more powerfully you'll 386 00:13:22,600 --> 00:13:26,320 understand how the computer sees that 387 00:13:24,399 --> 00:13:29,120 same kind of code builds up a 388 00:13:26,320 --> 00:13:31,240 hierarchical family tree type structure 389 00:13:29,120 --> 00:13:33,360 in memory and then you can manipulate 390 00:13:31,240 --> 00:13:35,199 that tree with code to actually add more 391 00:13:33,360 --> 00:13:37,519 and more information chat messages 392 00:13:35,199 --> 00:13:40,000 anything on the screen that you like and 393 00:13:37,519 --> 00:13:41,519 finally we'll tie all of this together 394 00:13:40,000 --> 00:13:44,040 by introducing what are called 395 00:13:41,519 --> 00:13:46,240 Frameworks and libraries third-party 396 00:13:44,040 --> 00:13:48,440 code that makes it a lot easier to solve 397 00:13:46,240 --> 00:13:50,560 problems of interest to you and so in 398 00:13:48,440 --> 00:13:53,839 particular here this is the very first 399 00:13:50,560 --> 00:13:56,079 web app that I myself made back in like 400 00:13:53,839 --> 00:13:58,199 1997 I was part of the first year 401 00:13:56,079 --> 00:14:00,079 intramural sports program not as an 402 00:13:58,199 --> 00:14:01,680 athlete but as the programmer and I was 403 00:14:00,079 --> 00:14:03,839 teaching myself how to build web 404 00:14:01,680 --> 00:14:05,399 applications I only knew C and maybe a 405 00:14:03,839 --> 00:14:07,279 little bit of something else at the time 406 00:14:05,399 --> 00:14:09,440 but this became for Harvard at least the 407 00:14:07,279 --> 00:14:11,320 very first website for the first year uh 408 00:14:09,440 --> 00:14:13,120 intramural sports program and it wasn't 409 00:14:11,320 --> 00:14:15,079 just a static website with links and 410 00:14:13,120 --> 00:14:16,920 images and the like it was interactive 411 00:14:15,079 --> 00:14:18,800 you could register for sports we could 412 00:14:16,920 --> 00:14:20,240 input exactly who is in a tournament 413 00:14:18,800 --> 00:14:22,519 bracket or the leg and it could actually 414 00:14:20,240 --> 00:14:24,440 automatically keep track of this data so 415 00:14:22,519 --> 00:14:26,320 there two after just three months of a 416 00:14:24,440 --> 00:14:28,120 class like this you'll go from writing 417 00:14:26,320 --> 00:14:30,320 quite simply this week in next hello 418 00:14:28,120 --> 00:14:32,800 world to building things like this for 419 00:14:30,320 --> 00:14:34,920 whether it's web mobile uh or other 420 00:14:32,800 --> 00:14:36,440 platforms as well if you so choose but 421 00:14:34,920 --> 00:14:37,759 we'll get you off of the courses 422 00:14:36,440 --> 00:14:39,639 infrastructure by the end of the term 423 00:14:37,759 --> 00:14:41,320 you won't be using any toy environments 424 00:14:39,639 --> 00:14:43,519 along the way will Empower you 425 00:14:41,320 --> 00:14:45,720 ultimately to write code after cs50 426 00:14:43,519 --> 00:14:48,199 especially if this is the only CS class 427 00:14:45,720 --> 00:14:49,639 you ever take on your own Mac or PC 428 00:14:48,199 --> 00:14:51,480 using the same software but not the 429 00:14:49,639 --> 00:14:53,759 cloud-based version thereof but all of 430 00:14:51,480 --> 00:14:56,279 this software is itself free and can be 431 00:14:53,759 --> 00:14:58,839 used by you uh powerfully after the 432 00:14:56,279 --> 00:15:02,360 course's own end but along the way as 433 00:14:58,839 --> 00:15:04,120 you may know uh is there this tradition 434 00:15:02,360 --> 00:15:05,959 within the class particularly in healthy 435 00:15:04,120 --> 00:15:07,440 times of a number of events that really 436 00:15:05,959 --> 00:15:09,440 brings people get together not just 437 00:15:07,440 --> 00:15:12,079 collaboratively academically but to just 438 00:15:09,440 --> 00:15:13,720 solve problems and generally uh engage 439 00:15:12,079 --> 00:15:16,040 with each other as well coming up first 440 00:15:13,720 --> 00:15:18,160 cs50 puzzle day which is meant to be not 441 00:15:16,040 --> 00:15:19,959 jigsaw puzzles but logic puzzles that 442 00:15:18,160 --> 00:15:21,759 require no prior experience with 443 00:15:19,959 --> 00:15:23,800 computer science or programming but it's 444 00:15:21,759 --> 00:15:25,800 just an opportunity to sort of quietly 445 00:15:23,800 --> 00:15:27,639 work on a packet of puzzles with some 446 00:15:25,800 --> 00:15:30,000 number of friends for for prizes and 447 00:15:27,639 --> 00:15:32,519 more later the semester once you tackle 448 00:15:30,000 --> 00:15:33,800 your final projects the Capstone of the 449 00:15:32,519 --> 00:15:35,759 course where we don't give you a 450 00:15:33,800 --> 00:15:37,360 homework to write you yourself come up 451 00:15:35,759 --> 00:15:39,800 with something to build we'll get 452 00:15:37,360 --> 00:15:42,440 together generally around 7 a uh 700 453 00:15:39,800 --> 00:15:44,560 p.m. in the evening uh wrap up around 7 454 00:15:42,440 --> 00:15:46,319 a.m. if you so choose and it's an 455 00:15:44,560 --> 00:15:48,079 evening a 12-hour opportunity to 456 00:15:46,319 --> 00:15:50,319 collaborate with classmates on your very 457 00:15:48,079 --> 00:15:52,759 own final project in a large space on 458 00:15:50,319 --> 00:15:55,000 campus uh that ends if you're awake with 459 00:15:52,759 --> 00:15:56,880 us at 5:00 a.m. uh we can hop on some 460 00:15:55,000 --> 00:16:00,040 cs50 shuttles and go down the road for 461 00:15:56,880 --> 00:16:03,600 some pancakes at IHOP around 6 uh of 462 00:16:00,040 --> 00:16:05,240 course uh of course this is 6 7 a.m. at 463 00:16:03,600 --> 00:16:06,839 that point but it's an opportunity 464 00:16:05,240 --> 00:16:09,079 finally to lead into What's called the 465 00:16:06,839 --> 00:16:11,480 cs50 fair which is an end of semester 466 00:16:09,079 --> 00:16:12,880 celebration an exhibition of everything 467 00:16:11,480 --> 00:16:14,600 that you'll accomplish over the coming 468 00:16:12,880 --> 00:16:16,319 months and in fact pictured here are 469 00:16:14,600 --> 00:16:18,680 some of your predecessors and healthy 470 00:16:16,319 --> 00:16:20,199 times um the cs50 fair allows you to 471 00:16:18,680 --> 00:16:22,240 come with your laptop or phone and 472 00:16:20,199 --> 00:16:24,360 exhibit a students faculty and staff 473 00:16:22,240 --> 00:16:25,920 across campus put together something in 474 00:16:24,360 --> 00:16:28,000 person and on video that people can 475 00:16:25,920 --> 00:16:29,639 Delight in seeing as you exhibit what it 476 00:16:28,000 --> 00:16:31,480 is you create in what you learned over 477 00:16:29,639 --> 00:16:33,800 the course of the several weeks and 478 00:16:31,480 --> 00:16:36,160 ultimately a chance to just share and 479 00:16:33,800 --> 00:16:38,399 Inspire others as well um and you'll all 480 00:16:36,160 --> 00:16:42,120 walk home ultimately with your own I to 481 00:16:38,399 --> 00:16:44,120 cs50 t-shirt saying as much as well so 482 00:16:42,120 --> 00:16:46,440 with that highlevel overview of the 483 00:16:44,120 --> 00:16:48,920 course I propose that we begin to take a 484 00:16:46,440 --> 00:16:50,519 look at what computer science itself is 485 00:16:48,920 --> 00:16:52,240 and what it is we're going to be doing 486 00:16:50,519 --> 00:16:54,600 over the next several weeks at this 487 00:16:52,240 --> 00:16:56,560 lower level than two so what is computer 488 00:16:54,600 --> 00:16:58,680 science right if you're maybe like me or 489 00:16:56,560 --> 00:17:00,600 new people like my friends and High 490 00:16:58,680 --> 00:17:02,319 School you probably assume that it means 491 00:17:00,600 --> 00:17:04,360 programming and that's absolutely a big 492 00:17:02,319 --> 00:17:06,000 part of it for a lot of people because 493 00:17:04,360 --> 00:17:07,600 with code you can write you can express 494 00:17:06,000 --> 00:17:09,640 ideas and solve actual problems 495 00:17:07,600 --> 00:17:11,760 especially involving data but computer 496 00:17:09,640 --> 00:17:13,360 science itself is really the study of 497 00:17:11,760 --> 00:17:15,360 information if you will how do you 498 00:17:13,360 --> 00:17:16,679 represent it and how do you actually 499 00:17:15,360 --> 00:17:18,839 process it and in that sense 500 00:17:16,679 --> 00:17:21,319 computational thinking is just the 501 00:17:18,839 --> 00:17:23,640 application of ideas from computer 502 00:17:21,319 --> 00:17:25,600 science a course like this to problems 503 00:17:23,640 --> 00:17:27,520 of interest to you again in the Arts 504 00:17:25,600 --> 00:17:30,120 Humanities Sciences social sciences 505 00:17:27,520 --> 00:17:32,360 whatever the domain interest is to you 506 00:17:30,120 --> 00:17:34,280 so with that if computer science is all 507 00:17:32,360 --> 00:17:36,000 about information and with it the 508 00:17:34,280 --> 00:17:37,679 solving of problems well what does it 509 00:17:36,000 --> 00:17:39,720 actually mean to solve a problem let's 510 00:17:37,679 --> 00:17:42,000 see if we can't propose a model into 511 00:17:39,720 --> 00:17:43,760 which all of the Lessons Learned will 512 00:17:42,000 --> 00:17:46,919 ultimately follow and I'd propose that 513 00:17:43,760 --> 00:17:48,320 this is problem solving you've got some 514 00:17:46,919 --> 00:17:50,039 input which is like the problem you want 515 00:17:48,320 --> 00:17:51,760 to solve the goal is to solve it so 516 00:17:50,039 --> 00:17:53,720 that's the so-called output and then 517 00:17:51,760 --> 00:17:56,080 somewhere in here the proverbial black 518 00:17:53,720 --> 00:17:57,960 box is some kind of Secret Sauce that 519 00:17:56,080 --> 00:17:59,760 gets the work done and in the coming 520 00:17:57,960 --> 00:18:01,000 months have to decide well how are we 521 00:17:59,760 --> 00:18:03,400 going to represent these inputs and 522 00:18:01,000 --> 00:18:05,559 outputs and really how do we code up how 523 00:18:03,400 --> 00:18:07,360 do we write solutions for what it is 524 00:18:05,559 --> 00:18:09,840 that's solving the problem of interest 525 00:18:07,360 --> 00:18:11,520 to us so when it comes to representation 526 00:18:09,840 --> 00:18:13,080 of information like there's a lot of 527 00:18:11,520 --> 00:18:14,760 ways we can do this and for instance if 528 00:18:13,080 --> 00:18:16,760 the problem at hand quite simply is to 529 00:18:14,760 --> 00:18:18,559 like take attendance at the beginning of 530 00:18:16,760 --> 00:18:20,520 class on the first day of school well 531 00:18:18,559 --> 00:18:23,400 how could we go about doing this well we 532 00:18:20,520 --> 00:18:24,919 could actually use a system called unary 533 00:18:23,400 --> 00:18:29,159 well what is that well that's a fancy 534 00:18:24,919 --> 00:18:31,799 way of saying 1 2 3 4 5 maybe 6 7 8 9 10 535 00:18:29,159 --> 00:18:34,200 and I can use my digits pun intended on 536 00:18:31,799 --> 00:18:35,919 my fingers to actually count everyone up 537 00:18:34,200 --> 00:18:38,200 and eventually you need toes and whatnot 538 00:18:35,919 --> 00:18:40,280 if you have to count so high but unary 539 00:18:38,200 --> 00:18:42,360 is a very simple system of using a 540 00:18:40,280 --> 00:18:44,039 single symbol a human finger in this 541 00:18:42,360 --> 00:18:45,280 case to just solve some problem like 542 00:18:44,039 --> 00:18:46,960 counting the number of people in the 543 00:18:45,280 --> 00:18:48,320 room let's make this slightly more 544 00:18:46,960 --> 00:18:50,400 technical for a moment a little more 545 00:18:48,320 --> 00:18:52,200 mathy that's just called base one where 546 00:18:50,400 --> 00:18:54,559 the base under which you're operating 547 00:18:52,200 --> 00:18:56,120 has one digit in it like literally a 548 00:18:54,559 --> 00:18:58,039 human finger and maybe multiple such 549 00:18:56,120 --> 00:19:00,120 fingers if you need to count higher but 550 00:18:58,039 --> 00:19:02,600 of course of course all most of you if 551 00:19:00,120 --> 00:19:04,919 not all of you generally vaguely know 552 00:19:02,600 --> 00:19:07,039 that computers use something other than 553 00:19:04,919 --> 00:19:08,919 unary and even you and I probably don't 554 00:19:07,039 --> 00:19:10,679 use this that often they use what 555 00:19:08,919 --> 00:19:14,400 language or alphabet 556 00:19:10,679 --> 00:19:16,400 instead yeah so binary so binary is 557 00:19:14,400 --> 00:19:19,280 indeed the system that computers somehow 558 00:19:16,400 --> 00:19:21,640 use so in this case by implying two and 559 00:19:19,280 --> 00:19:23,400 so computers have two digits it turns 560 00:19:21,640 --> 00:19:25,640 out at their dispos one in fact if 561 00:19:23,400 --> 00:19:27,159 you've ever heard the technical term bit 562 00:19:25,640 --> 00:19:29,440 which is like a smaller version of a 563 00:19:27,159 --> 00:19:32,320 bite more on that soon well a binary 564 00:19:29,440 --> 00:19:33,320 digit is the origin of that term bit 565 00:19:32,320 --> 00:19:34,720 because if you get rid of some of the 566 00:19:33,320 --> 00:19:37,320 letters and you're left from binary 567 00:19:34,720 --> 00:19:39,799 digit with just bit thus is a bit a bit 568 00:19:37,320 --> 00:19:41,120 is just a zero and one it's two more 569 00:19:39,799 --> 00:19:43,080 digits than you might have on your own 570 00:19:41,120 --> 00:19:44,720 finger and of course it's fewer though 571 00:19:43,080 --> 00:19:47,159 than you and I have you and I typically 572 00:19:44,720 --> 00:19:48,919 use as humans the decimal system deck 573 00:19:47,159 --> 00:19:51,679 meaning 10 because you and I generally 574 00:19:48,919 --> 00:19:54,000 use zero through nine so on the one hand 575 00:19:51,679 --> 00:19:56,679 another pun intended you've got unary 576 00:19:54,000 --> 00:19:58,840 computers use binary we humans generally 577 00:19:56,679 --> 00:20:00,640 think and talk in terms of De 578 00:19:58,840 --> 00:20:02,320 but at the end of the day these are 579 00:20:00,640 --> 00:20:04,200 fundamentally going to be the same thing 580 00:20:02,320 --> 00:20:05,600 which is to say that it's all pretty 581 00:20:04,200 --> 00:20:07,760 accessible to us even if you're not a 582 00:20:05,600 --> 00:20:10,840 computer person I dare say you're about 583 00:20:07,760 --> 00:20:13,520 to be so what is a bit well a bit then 584 00:20:10,840 --> 00:20:15,799 is a zero or a one that is a so-called 585 00:20:13,520 --> 00:20:17,720 binary digit but how do computers only 586 00:20:15,799 --> 00:20:19,280 speak in binary how do they solve 587 00:20:17,720 --> 00:20:21,120 problems represent information using 588 00:20:19,280 --> 00:20:23,760 only binary well at the end of the day 589 00:20:21,120 --> 00:20:26,520 if they want to represent zero and one 590 00:20:23,760 --> 00:20:28,360 we need to do so physically somehow and 591 00:20:26,520 --> 00:20:31,280 I dare say that maybe the simplest way 592 00:20:28,360 --> 00:20:32,840 to think about a bit a zero or one is 593 00:20:31,280 --> 00:20:34,799 like a light bulb and so by human 594 00:20:32,840 --> 00:20:37,200 convention let's just assume that if you 595 00:20:34,799 --> 00:20:38,480 are a computer be it a laptop desktop 596 00:20:37,200 --> 00:20:41,440 phone or the like and you want to 597 00:20:38,480 --> 00:20:43,679 represent the number zero you know what 598 00:20:41,440 --> 00:20:46,200 you just keep a light switch off you 599 00:20:43,679 --> 00:20:47,320 keep a light bulb off if by contrast 600 00:20:46,200 --> 00:20:49,200 you're that same computer and you want 601 00:20:47,320 --> 00:20:50,679 to represent the number one you take 602 00:20:49,200 --> 00:20:53,000 that same switch that same light bulb 603 00:20:50,679 --> 00:20:54,520 and just turn it on so a light bulb 604 00:20:53,000 --> 00:20:57,480 that's on represents a one and a light 605 00:20:54,520 --> 00:20:58,960 bulb that's off represents a zero so why 606 00:20:57,480 --> 00:21:00,000 is this relevant into computers well at 607 00:20:58,960 --> 00:21:02,080 the end of the day you and I are 608 00:21:00,000 --> 00:21:03,640 charging our laptops or phones at night 609 00:21:02,080 --> 00:21:05,159 so there's some like physical resource 610 00:21:03,640 --> 00:21:07,760 being replenished there whether you're 611 00:21:05,159 --> 00:21:09,840 on battery or some power cord and so a 612 00:21:07,760 --> 00:21:12,039 inside of a computer are just thousands 613 00:21:09,840 --> 00:21:13,159 millions of tiny little switches 614 00:21:12,039 --> 00:21:14,679 nowadays you can think of them 615 00:21:13,159 --> 00:21:16,600 metaphorically as light bulbs but they 616 00:21:14,679 --> 00:21:18,880 don't yet it's actually shine light but 617 00:21:16,600 --> 00:21:20,279 there are tiny tiny little switches and 618 00:21:18,880 --> 00:21:22,760 those switches if you've ever heard the 619 00:21:20,279 --> 00:21:24,600 term are just called transistors so like 620 00:21:22,760 --> 00:21:26,080 computers have millions of transistors 621 00:21:24,600 --> 00:21:28,159 that can either be flipped on to 622 00:21:26,080 --> 00:21:30,799 represent ones or flipped off to 623 00:21:28,159 --> 00:21:33,720 represent zeros and from that very 624 00:21:30,799 --> 00:21:37,080 simple mechanism electricity is there or 625 00:21:33,720 --> 00:21:39,279 it's not a one or a zero computers can 626 00:21:37,080 --> 00:21:41,799 actually count obviously from zero to 627 00:21:39,279 --> 00:21:44,360 one but it turns out even higher if they 628 00:21:41,799 --> 00:21:45,919 use a little more electricity as well so 629 00:21:44,360 --> 00:21:47,960 how might I do this well let me go ahead 630 00:21:45,919 --> 00:21:49,720 and propose that I just grab one of our 631 00:21:47,960 --> 00:21:51,200 own light bulbs here on stage this one's 632 00:21:49,720 --> 00:21:53,120 off so for instance If This Were 633 00:21:51,200 --> 00:21:54,880 miniaturized inside of your Mac PC or 634 00:21:53,120 --> 00:21:56,200 phone this would be a transistor and 635 00:21:54,880 --> 00:21:58,080 indeed here's the little switch on the 636 00:21:56,200 --> 00:21:59,559 bottom and if your computer wants to 637 00:21:58,080 --> 00:22:01,760 represent a zero it just leaves the 638 00:21:59,559 --> 00:22:03,720 switch off and the light is not shining 639 00:22:01,760 --> 00:22:05,320 if you want to represent a one well now 640 00:22:03,720 --> 00:22:07,000 I've counted as high as one because the 641 00:22:05,320 --> 00:22:08,559 switch is now on I've grabbed a little 642 00:22:07,000 --> 00:22:10,640 electricity I'm holding on to it inside 643 00:22:08,559 --> 00:22:12,080 of the computer and so now I see that 644 00:22:10,640 --> 00:22:13,720 this is a one all right but 645 00:22:12,080 --> 00:22:16,679 unfortunately with just one switch one 646 00:22:13,720 --> 00:22:19,279 light bulb I can only count from zero to 647 00:22:16,679 --> 00:22:20,320 one how do I count out higher might you 648 00:22:19,279 --> 00:22:23,559 think 649 00:22:20,320 --> 00:22:25,039 intuitively say again yeah so more light 650 00:22:23,559 --> 00:22:27,679 bulbs so let me do this let me just put 651 00:22:25,039 --> 00:22:29,559 grab something to put these on so I can 652 00:22:27,679 --> 00:22:31,880 use a few of them at a time and let me 653 00:22:29,559 --> 00:22:33,919 propose that here instead of having just 654 00:22:31,880 --> 00:22:35,799 one light bulb let me give myself maybe 655 00:22:33,919 --> 00:22:37,840 three in total so all of them are 656 00:22:35,799 --> 00:22:39,520 initially off and if you think of this 657 00:22:37,840 --> 00:22:40,799 in miniature form in your mind's eye 658 00:22:39,520 --> 00:22:43,120 this is like a computer with three 659 00:22:40,799 --> 00:22:45,919 transistors three switches representing 660 00:22:43,120 --> 00:22:48,919 now the number you and I know as zero 661 00:22:45,919 --> 00:22:50,440 why they're just all off so how does a 662 00:22:48,919 --> 00:22:53,080 computer go about representing the 663 00:22:50,440 --> 00:22:55,159 number one well it turns on one of these 664 00:22:53,080 --> 00:22:57,400 light bulbs and how does the computer 665 00:22:55,159 --> 00:22:59,679 represent the number two well you might 666 00:22:57,400 --> 00:23:01,720 think if I by may you just turn on a 667 00:22:59,679 --> 00:23:03,360 second light bul and if you might think 668 00:23:01,720 --> 00:23:05,559 how does a computer represent three you 669 00:23:03,360 --> 00:23:08,159 just turn on the third light bulb and so 670 00:23:05,559 --> 00:23:09,960 as such with three bits a computer would 671 00:23:08,159 --> 00:23:12,960 seem to be able to count from zero on up 672 00:23:09,960 --> 00:23:15,520 to 1 two three but it turns out if I'm a 673 00:23:12,960 --> 00:23:17,679 little smarter here I can actually count 674 00:23:15,520 --> 00:23:19,039 higher than that why well I'm just 675 00:23:17,679 --> 00:23:20,360 considering like the combination of 676 00:23:19,039 --> 00:23:22,039 bulbs being on here what if I do 677 00:23:20,360 --> 00:23:24,279 something like this this is still zero I 678 00:23:22,039 --> 00:23:26,000 will claim but what if I propose now 679 00:23:24,279 --> 00:23:31,320 that this will be how a computer 680 00:23:26,000 --> 00:23:33,679 represents one on on off off this though 681 00:23:31,320 --> 00:23:35,440 will be how the computer represents two 682 00:23:33,679 --> 00:23:37,559 notice I didn't turn on the same two I'm 683 00:23:35,440 --> 00:23:39,159 just turning on the one in the middle 684 00:23:37,559 --> 00:23:42,320 this I now claim will be how a computer 685 00:23:39,159 --> 00:23:44,600 represents three this is going to be in 686 00:23:42,320 --> 00:23:48,039 just a second how a computer represents 687 00:23:44,600 --> 00:23:50,080 the number we know as four and yet I'm 688 00:23:48,039 --> 00:23:51,320 still only using three bulbs this is 689 00:23:50,080 --> 00:23:54,279 going to be the number the computer 690 00:23:51,320 --> 00:23:56,679 represents as five this is going to be 691 00:23:54,279 --> 00:23:59,080 how the computer represents the number 692 00:23:56,679 --> 00:24:00,520 six and then lastly it turns out with 693 00:23:59,080 --> 00:24:02,679 three light bulbs if you're smart about 694 00:24:00,520 --> 00:24:05,000 it you can count it seems as high as 695 00:24:02,679 --> 00:24:07,360 seven now even if you kind of lost track 696 00:24:05,000 --> 00:24:08,880 of like what I was turning on and Y I 697 00:24:07,360 --> 00:24:10,880 claimed there were eight different 698 00:24:08,880 --> 00:24:12,520 patterns from all of them off to all of 699 00:24:10,880 --> 00:24:14,080 them on but notice that I started to 700 00:24:12,520 --> 00:24:16,880 permute them I took into account which 701 00:24:14,080 --> 00:24:18,919 ones were on and which ones were off why 702 00:24:16,880 --> 00:24:21,200 though do these represent the numbers we 703 00:24:18,919 --> 00:24:23,400 know as 0 through 7even well let me go 704 00:24:21,200 --> 00:24:24,880 ahead and maybe let's let's do this 705 00:24:23,400 --> 00:24:27,080 instead of just considering there to be 706 00:24:24,880 --> 00:24:29,960 light bulbs let's assign some special 707 00:24:27,080 --> 00:24:31,799 significance to each of them based on 708 00:24:29,960 --> 00:24:35,720 where it is and maybe for this could we 709 00:24:31,799 --> 00:24:37,840 get uh maybe three volunteers three 710 00:24:35,720 --> 00:24:39,200 volunteer okay yeah who's you're being 711 00:24:37,840 --> 00:24:40,679 volunteered okay come on up if you want 712 00:24:39,200 --> 00:24:42,559 to go over to the stage there yeah you 713 00:24:40,679 --> 00:24:44,440 want to come on up as well and over here 714 00:24:42,559 --> 00:24:46,679 as well so there's some stairs on either 715 00:24:44,440 --> 00:24:49,679 end maybe a round of applause for our 716 00:24:46,679 --> 00:24:49,679 first volunteers of 717 00:24:54,080 --> 00:24:58,000 term all right so you want to be you are 718 00:24:56,159 --> 00:25:00,000 number one and if you want want to go 719 00:24:58,000 --> 00:25:02,200 ahead and stand roughly right here how 720 00:25:00,000 --> 00:25:03,760 about you want to be number two okay 721 00:25:02,200 --> 00:25:06,480 come on over and right uh to the right 722 00:25:03,760 --> 00:25:07,960 of here and you'll be number four it 723 00:25:06,480 --> 00:25:10,399 turns out and if you want to come over 724 00:25:07,960 --> 00:25:12,360 here on this end let's give you all a 725 00:25:10,399 --> 00:25:14,120 moment to uh introduce yourselves uh 726 00:25:12,360 --> 00:25:18,440 briefly to your classmates if you'd like 727 00:25:14,120 --> 00:25:22,159 hi I'm Ellie I'm a senior nice to meet 728 00:25:18,440 --> 00:25:25,240 I'm rahana and I'm a first year welcome 729 00:25:22,159 --> 00:25:27,039 hi I'm Joseph and I am a first year 730 00:25:25,240 --> 00:25:30,440 welcome all right so so glad to have all 731 00:25:27,039 --> 00:25:30,440 three of you up here thank 732 00:25:30,799 --> 00:25:36,520 you let me propose now that we'd like 733 00:25:34,520 --> 00:25:38,760 you you three to represent how about the 734 00:25:36,520 --> 00:25:41,279 number zero and I claim now that if each 735 00:25:38,760 --> 00:25:43,080 of you now represents a switch you have 736 00:25:41,279 --> 00:25:44,679 sort of fancier light bulbs now one is a 737 00:25:43,080 --> 00:25:46,240 one one is a two one is a four but each 738 00:25:44,679 --> 00:25:48,440 of you still just has a switch on the 739 00:25:46,240 --> 00:25:50,039 bottom in fact of your plastic devices I 740 00:25:48,440 --> 00:25:51,919 claim these three volunteers are 741 00:25:50,039 --> 00:25:53,559 representing the number zero let me ask 742 00:25:51,919 --> 00:25:57,240 you all now how might you represent the 743 00:25:53,559 --> 00:25:59,919 number one how should you cooperate here 744 00:25:57,240 --> 00:26:01,279 okay so so we would have on off off 745 00:25:59,919 --> 00:26:02,480 which I think is matches what I did with 746 00:26:01,279 --> 00:26:03,960 my three light bulbs as well if you want 747 00:26:02,480 --> 00:26:06,960 to go ahead and turn yours off how might 748 00:26:03,960 --> 00:26:11,039 you three represent the number 749 00:26:06,960 --> 00:26:14,080 two okay so off on off now from right to 750 00:26:11,039 --> 00:26:16,720 left how would you3 represent the number 751 00:26:14,080 --> 00:26:18,559 three ah so that's why my two light 752 00:26:16,720 --> 00:26:21,440 bulbs went on at the end how would you 753 00:26:18,559 --> 00:26:21,440 three represent the number 754 00:26:22,679 --> 00:26:28,760 four perfect number 755 00:26:25,080 --> 00:26:30,480 five number six 756 00:26:28,760 --> 00:26:32,600 and number 757 00:26:30,480 --> 00:26:35,039 seven all right and give us one more how 758 00:26:32,600 --> 00:26:39,000 would you represent 759 00:26:35,039 --> 00:26:41,520 eight okay you can't uh how about then 760 00:26:39,000 --> 00:26:43,960 one more volunteer one more volunteer 761 00:26:41,520 --> 00:26:43,960 okay come on 762 00:26:45,320 --> 00:26:49,760 up all right what's your name my name is 763 00:26:48,399 --> 00:26:51,360 Moen if you want to say it there my name 764 00:26:49,760 --> 00:26:53,159 is Moen all right Moen you're GNA be 765 00:26:51,360 --> 00:26:54,960 number eight and if now you all actually 766 00:26:53,159 --> 00:26:57,399 let's make this uh how would you 767 00:26:54,960 --> 00:27:00,200 represent number eight all collectively 768 00:26:57,399 --> 00:27:02,640 as four bits or four 769 00:27:00,200 --> 00:27:04,840 switches okay eight and now lastly give 770 00:27:02,640 --> 00:27:04,840 me 771 00:27:06,320 --> 00:27:12,279 15 everyone's awkwardly doing arithmetic 772 00:27:08,880 --> 00:27:12,279 in their head oh using 773 00:27:12,960 --> 00:27:19,240 unar hey yeah is 774 00:27:15,279 --> 00:27:20,520 everyone yes okay Round of Applause okay 775 00:27:19,240 --> 00:27:24,200 thank you 776 00:27:20,520 --> 00:27:26,159 all if you want to leave your numbers 777 00:27:24,200 --> 00:27:28,399 over here we have a 650 stress ball for 778 00:27:26,159 --> 00:27:29,720 you but thank you for volunteer you can 779 00:27:28,399 --> 00:27:31,600 turn those numbers off and leave them 780 00:27:29,720 --> 00:27:34,760 over here so thank 781 00:27:31,600 --> 00:27:40,200 you so how do we go about how do we go 782 00:27:34,760 --> 00:27:43,519 from there to uh creating these patterns 783 00:27:40,200 --> 00:27:46,039 well even though we still had three bits 784 00:27:43,519 --> 00:27:48,679 initially and three switches later four 785 00:27:46,039 --> 00:27:50,919 bits and four switches ultimately we 786 00:27:48,679 --> 00:27:53,120 still used it used the same approach 787 00:27:50,919 --> 00:27:55,200 fundamentally to actually representing 788 00:27:53,120 --> 00:27:57,000 information and now why were they those 789 00:27:55,200 --> 00:27:58,440 patterns and why did I very deliberately 790 00:27:57,000 --> 00:28:01,279 have our volum volunteers line up in 791 00:27:58,440 --> 00:28:04,240 that way well I wanted them using base 2 792 00:28:01,279 --> 00:28:06,279 AKA binary but with binary there come 793 00:28:04,240 --> 00:28:08,320 certain rules and even if you're not 794 00:28:06,279 --> 00:28:10,519 familiar with binary beyond that it 795 00:28:08,320 --> 00:28:12,320 exists and relates somehow to computers 796 00:28:10,519 --> 00:28:14,320 it's actually pretty much identical to 797 00:28:12,320 --> 00:28:17,240 the system you and I use every day known 798 00:28:14,320 --> 00:28:19,200 as base 10 AKA decimal so let's consider 799 00:28:17,240 --> 00:28:20,559 if you will by rewinding to primary 800 00:28:19,200 --> 00:28:22,279 school for just a moment like how 801 00:28:20,559 --> 00:28:24,120 decimal works and you'll see that even 802 00:28:22,279 --> 00:28:25,640 if you're not a computer person you 803 00:28:24,120 --> 00:28:28,200 actually are you just have to tweak your 804 00:28:25,640 --> 00:28:29,679 mental model ever so slightly so here is 805 00:28:28,200 --> 00:28:32,799 the number that you're probably viewing 806 00:28:29,679 --> 00:28:35,440 as 123 but why is that well it's not 807 00:28:32,799 --> 00:28:37,640 really 123 these are just uh this is 808 00:28:35,440 --> 00:28:41,039 just a pattern of three symbols on the 809 00:28:37,640 --> 00:28:42,600 screen 1 2 three and your mind is sort 810 00:28:41,039 --> 00:28:45,159 of rapidly assigning mathematical 811 00:28:42,600 --> 00:28:46,760 meaning to them 123 but why is that well 812 00:28:45,159 --> 00:28:47,880 if you're like me you probably learned 813 00:28:46,760 --> 00:28:49,480 back in the day when you have a 814 00:28:47,880 --> 00:28:51,919 three-digit number like this the 815 00:28:49,480 --> 00:28:53,760 rightmost number is in the ones place 816 00:28:51,919 --> 00:28:55,519 the middle digit is the 10 place the 817 00:28:53,760 --> 00:28:57,159 leftmost digit is in the hundreds place 818 00:28:55,519 --> 00:28:58,840 and why is that relevant well if you 819 00:28:57,159 --> 00:29:01,480 then quickly do some mental math as you 820 00:28:58,840 --> 00:29:05,039 and I just do uh instantly nowadays that 821 00:29:01,480 --> 00:29:08,559 just means 100 * 1 plus 10 * 2 plus 1 * 822 00:29:05,039 --> 00:29:10,600 3 of course 100 + 20 + 3 gives us the 823 00:29:08,559 --> 00:29:13,679 number you and I know as 824 00:29:10,600 --> 00:29:16,080 123 but beyond that how do we get to 825 00:29:13,679 --> 00:29:18,080 just two digits instead of as many as 826 00:29:16,080 --> 00:29:20,559 nine in the decimal system well let's 827 00:29:18,080 --> 00:29:22,480 generalize this in the decimal system 828 00:29:20,559 --> 00:29:24,600 you and I know if we've got three digits 829 00:29:22,480 --> 00:29:26,519 represented by these hashes here yes 830 00:29:24,600 --> 00:29:28,360 it's the ones place T Place hundreds 831 00:29:26,519 --> 00:29:30,840 place and if we keep going thousands 10 832 00:29:28,360 --> 00:29:32,760 thousands and so forth but why is that 833 00:29:30,840 --> 00:29:34,880 well base terminology is now a little 834 00:29:32,760 --> 00:29:37,559 more gerine that's technically the 10 to 835 00:29:34,880 --> 00:29:40,200 the zero column the 10 to the 1 10 to 836 00:29:37,559 --> 00:29:43,760 the two so these are powers of 10 where 837 00:29:40,200 --> 00:29:45,360 10 is your base computers just simplify 838 00:29:43,760 --> 00:29:46,880 things a little bit because computers at 839 00:29:45,360 --> 00:29:48,919 the end of the day only have access to 840 00:29:46,880 --> 00:29:50,720 electricity on or off they don't have 841 00:29:48,919 --> 00:29:53,120 access to like 10 different types of 842 00:29:50,720 --> 00:29:55,480 electricity just two on or off if you 843 00:29:53,120 --> 00:29:56,880 will well they just use a different base 844 00:29:55,480 --> 00:29:59,519 and the rightmost digit would be in the 845 00:29:56,880 --> 00:30:01,679 so-called 2 to the zero then the middle 846 00:29:59,519 --> 00:30:04,039 digit is 2 to the one the leftmost is 2 847 00:30:01,679 --> 00:30:06,519 to the 2 AKA one's Place two's Place 848 00:30:04,039 --> 00:30:10,559 Four's place and as we kept going 8 and 849 00:30:06,519 --> 00:30:12,279 if we keep going 16 32 64 128 and so 850 00:30:10,559 --> 00:30:15,640 forth but the idea is fundamentally the 851 00:30:12,279 --> 00:30:17,159 same so why is this how the computer 852 00:30:15,640 --> 00:30:21,000 represents the number you and I know is 853 00:30:17,159 --> 00:30:23,320 zero well off off off from right to left 854 00:30:21,000 --> 00:30:27,480 or in this case left to right is just 855 00:30:23,320 --> 00:30:32,399 zero why CU that's 4 * 0 + 2 * 0 + 1 856 00:30:27,480 --> 00:30:35,279 time 0 is of course 0o this is why 0 01 857 00:30:32,399 --> 00:30:38,519 represents one this is why 0 1 0 858 00:30:35,279 --> 00:30:42,120 represents two and three and four and 859 00:30:38,519 --> 00:30:44,480 five and six and seven on up and why did 860 00:30:42,120 --> 00:30:46,799 we need a fourth bit to represent eight 861 00:30:44,480 --> 00:30:49,320 well we kind of needed to carry the one 862 00:30:46,799 --> 00:30:51,039 so to speak using our human familiar uh 863 00:30:49,320 --> 00:30:53,840 familiar human terminology but for that 864 00:30:51,039 --> 00:30:55,519 we need a fourth bit another transistor 865 00:30:53,840 --> 00:30:57,639 and this now represents the number eight 866 00:30:55,519 --> 00:31:00,519 and that's why we ended with on from 867 00:30:57,639 --> 00:31:02,919 left to right off off off so I keep 868 00:31:00,519 --> 00:31:05,679 saying on and off or the light bulb is 869 00:31:02,919 --> 00:31:08,320 on or off but really I just mean one or 870 00:31:05,679 --> 00:31:10,080 zero and so computers and we humans 871 00:31:08,320 --> 00:31:11,960 think of things digitally as just being 872 00:31:10,080 --> 00:31:13,440 zeros and ones but mechanically you can 873 00:31:11,960 --> 00:31:15,600 think of it indeed as these light bulbs 874 00:31:13,440 --> 00:31:17,279 now a bit not very useful even three 875 00:31:15,600 --> 00:31:19,679 bits four bits not that useful you can 876 00:31:17,279 --> 00:31:22,080 count to seven or 15 generally speaking 877 00:31:19,679 --> 00:31:23,960 bites are a more useful unit of measure 878 00:31:22,080 --> 00:31:27,279 and anyone familiar how many bits is in 879 00:31:23,960 --> 00:31:29,720 a bite yeah so eight bits are in a bite 880 00:31:27,279 --> 00:31:31,120 can think of it as an octet equivalently 881 00:31:29,720 --> 00:31:32,600 uh in some context there are nuances 882 00:31:31,120 --> 00:31:34,519 there but think of a bite as just being 883 00:31:32,600 --> 00:31:36,360 eight bits and that's just a more useful 884 00:31:34,519 --> 00:31:37,919 measure so what does this mean in real 885 00:31:36,360 --> 00:31:41,679 terms well if you've ever downloaded 886 00:31:37,919 --> 00:31:43,919 like a music file or a photograph or a a 887 00:31:41,679 --> 00:31:45,440 video those are measured in bytes 888 00:31:43,919 --> 00:31:47,559 probably not small numbers of bytes 889 00:31:45,440 --> 00:31:49,679 probably kilobytes for thousands of 890 00:31:47,559 --> 00:31:52,039 bytes megabytes for millions of bytes 891 00:31:49,679 --> 00:31:54,120 gigabytes for billions of bytes 892 00:31:52,039 --> 00:31:57,760 especially for video that just means you 893 00:31:54,120 --> 00:31:59,480 have a lot of patterns of 8 Bits some 894 00:31:57,760 --> 00:32:02,039 combination of zeros and ones on your 895 00:31:59,480 --> 00:32:04,679 computer's hard drive here then with a 896 00:32:02,039 --> 00:32:06,360 bite of bits eight bits is how a 897 00:32:04,679 --> 00:32:08,320 computer would typically represent the 898 00:32:06,360 --> 00:32:11,080 number zero and if that same computer 899 00:32:08,320 --> 00:32:13,600 uses all eight of its bits it's full 900 00:32:11,080 --> 00:32:15,000 bite to rep uh to change it to one 901 00:32:13,600 --> 00:32:17,000 anyone who's quick with math or have 902 00:32:15,000 --> 00:32:21,399 seen this before how high can a computer 903 00:32:17,000 --> 00:32:22,799 count with eight bits or one yeah 255 904 00:32:21,399 --> 00:32:24,480 why is that well we're not going to turn 905 00:32:22,799 --> 00:32:25,679 this into a constant math exercise 906 00:32:24,480 --> 00:32:28,039 indeed after today we're not really 907 00:32:25,679 --> 00:32:30,159 going to think about or talk about bits 908 00:32:28,039 --> 00:32:36,200 at this low level but this is the ones 909 00:32:30,159 --> 00:32:37,880 place 2os 4S 8 16 32 64 128 and if I do 910 00:32:36,200 --> 00:32:39,320 all of that math from left to right that 911 00:32:37,880 --> 00:32:41,919 indeed gives me 912 00:32:39,320 --> 00:32:43,440 255 it ignores how we might represent 913 00:32:41,919 --> 00:32:45,799 negative numbers but perhaps more on 914 00:32:43,440 --> 00:32:47,880 those some other day but computers of 915 00:32:45,799 --> 00:32:50,159 course do so much more than numbers and 916 00:32:47,880 --> 00:32:52,320 math and all this lowlevel stuff we send 917 00:32:50,159 --> 00:32:54,559 text messages right documents emails and 918 00:32:52,320 --> 00:32:57,399 the like so how might a computer 919 00:32:54,559 --> 00:32:59,399 represent something like the letter A I 920 00:32:57,399 --> 00:33:01,399 claim at the end of the day your Mac 921 00:32:59,399 --> 00:33:03,840 your PC your phone just has lots of 922 00:33:01,399 --> 00:33:07,440 transistors lots of switches that it can 923 00:33:03,840 --> 00:33:09,559 use in units of eight in units of bytes 924 00:33:07,440 --> 00:33:11,360 how though if it's already using those 925 00:33:09,559 --> 00:33:14,679 patterns of zeros and ones apparently to 926 00:33:11,360 --> 00:33:16,760 represent numbers from zero on up how do 927 00:33:14,679 --> 00:33:20,760 you go about representing letters of the 928 00:33:16,760 --> 00:33:22,639 alphabet might you think yeah okay so we 929 00:33:20,760 --> 00:33:24,360 could assign a number to every letter 930 00:33:22,639 --> 00:33:27,559 okay so let me just conjecture well 931 00:33:24,360 --> 00:33:29,840 let's just call a0 for Simplicity B1 1 932 00:33:27,559 --> 00:33:33,559 C2 and now let me play Devil's Advocate 933 00:33:29,840 --> 00:33:36,080 okay how do I now represent zero or one 934 00:33:33,559 --> 00:33:37,559 or two well we've maybe created a 935 00:33:36,080 --> 00:33:39,880 problem for oursel if now we have to 936 00:33:37,559 --> 00:33:41,000 steal some numbers to represent letters 937 00:33:39,880 --> 00:33:42,240 we kind of have to pick a lane but 938 00:33:41,000 --> 00:33:44,519 there's a solution to that too that 939 00:33:42,240 --> 00:33:46,919 we'll see and it turns out the world is 940 00:33:44,519 --> 00:33:48,639 not quite as simple as a being zero a 941 00:33:46,919 --> 00:33:50,399 typically is represented by computers 942 00:33:48,639 --> 00:33:54,279 everywhere phones everywhere with the 943 00:33:50,399 --> 00:33:56,279 number 65 the decimal number 65 using 8 944 00:33:54,279 --> 00:33:57,919 Bits if we turn some of the zeros to 945 00:33:56,279 --> 00:34:00,559 ones let me just stipulate you can 946 00:33:57,919 --> 00:34:02,679 represent the letter A Using eight bits 947 00:34:00,559 --> 00:34:04,840 by turning certain ones on and certain 948 00:34:02,679 --> 00:34:07,519 ones off but we will try not to focus on 949 00:34:04,840 --> 00:34:09,919 the that binary level too much so if a 950 00:34:07,519 --> 00:34:13,800 is 65 it turns out that b is going to be 951 00:34:09,919 --> 00:34:15,159 66 and C is going to be 67 and so forth 952 00:34:13,800 --> 00:34:17,280 and so where does that get us well it 953 00:34:15,159 --> 00:34:21,440 turns out there's a whole system that 954 00:34:17,280 --> 00:34:23,000 Maps uh numbers to letters and here uh 955 00:34:21,440 --> 00:34:24,639 as I alluded to verbally a moment ago is 956 00:34:23,000 --> 00:34:27,159 the pattern of zeros and ones via which 957 00:34:24,639 --> 00:34:29,679 you'd represent 65 and just quick check 958 00:34:27,159 --> 00:34:32,760 here we won't constantly do math uh 959 00:34:29,679 --> 00:34:38,280 one's place that's easy 2os Four's 8 16 960 00:34:32,760 --> 00:34:41,839 32 64's place so 64 + 1 gives us 961 00:34:38,280 --> 00:34:43,520 65 so once I do that how do I get to all 962 00:34:41,839 --> 00:34:46,040 of the others well it turns out a bunch 963 00:34:43,520 --> 00:34:48,280 of Americans years ago came up with this 964 00:34:46,040 --> 00:34:50,320 asky the American Standard code for 965 00:34:48,280 --> 00:34:52,159 information interchange now what does 966 00:34:50,320 --> 00:34:54,000 that mean well it's just an acronym 967 00:34:52,159 --> 00:34:56,879 describing what really you proposed a 968 00:34:54,000 --> 00:34:59,760 mapping between numbers and letters not 969 00:34:56,879 --> 00:35:03,280 not quite as simple as 012 starts at 65 970 00:34:59,760 --> 00:35:05,359 66 67 for capital letters but here are 971 00:35:03,280 --> 00:35:07,079 most of the letters in use today at 972 00:35:05,359 --> 00:35:09,359 least with this system so this is just a 973 00:35:07,079 --> 00:35:11,720 big chart from online and you'll see it 974 00:35:09,359 --> 00:35:16,400 in the middle of this chart here here's 975 00:35:11,720 --> 00:35:20,119 my 65a here's my 66 b c and let's see 72 976 00:35:16,400 --> 00:35:21,920 is H 73 is I and so forth so there's a 977 00:35:20,119 --> 00:35:23,560 mapping at least for English between all 978 00:35:21,920 --> 00:35:25,560 of these numbers and all of these 979 00:35:23,560 --> 00:35:28,440 letters and if we focus here those are 980 00:35:25,560 --> 00:35:30,839 the beginning of our up case alphabet so 981 00:35:28,440 --> 00:35:34,000 suppose then that today tomorrow you 982 00:35:30,839 --> 00:35:35,720 receive a text message from someone and 983 00:35:34,000 --> 00:35:37,480 underneath the hood now that you're a 984 00:35:35,720 --> 00:35:39,680 computer person you figure out a way to 985 00:35:37,480 --> 00:35:41,320 see what pattern of zeros and ones was 986 00:35:39,680 --> 00:35:43,079 sent in this case it's wireless as 987 00:35:41,320 --> 00:35:44,680 opposed to wired but it's still some 988 00:35:43,079 --> 00:35:46,240 pattern of zeros and ones and your phone 989 00:35:44,680 --> 00:35:48,280 is turning some switches of its own on 990 00:35:46,240 --> 00:35:50,920 and off to represent that message from a 991 00:35:48,280 --> 00:35:53,400 friend suppose that the three patterns 992 00:35:50,920 --> 00:35:55,240 you received were these three btes from 993 00:35:53,400 --> 00:35:57,319 left to right spelling out a 994 00:35:55,240 --> 00:35:59,560 three-letter word well if if we do out 995 00:35:57,319 --> 00:36:01,560 the math one's Place two's place and so 996 00:35:59,560 --> 00:36:03,720 forth I'll spoil it for you suppose that 997 00:36:01,560 --> 00:36:06,960 you received a text message that doesn't 998 00:36:03,720 --> 00:36:09,680 literally say 72 73 33 but you've 999 00:36:06,960 --> 00:36:11,680 received a pattern of 8 plus 8 plus 8 24 1000 00:36:09,680 --> 00:36:15,680 bits that if you do out the math 1001 00:36:11,680 --> 00:36:17,520 represent the decimal number 72 73 33 1002 00:36:15,680 --> 00:36:19,359 anyone recall what message you might 1003 00:36:17,520 --> 00:36:20,960 have received from the green and white 1004 00:36:19,359 --> 00:36:25,440 charts 1005 00:36:20,960 --> 00:36:29,440 yeah hi but yes hi is the message but 1006 00:36:25,440 --> 00:36:30,640 7273 gives H and I what's 33 any any 1007 00:36:29,440 --> 00:36:33,839 guesses to 1008 00:36:30,640 --> 00:36:35,240 33 yeah over here yeah so it's an 1009 00:36:33,839 --> 00:36:36,480 exclamation point how would you know 1010 00:36:35,240 --> 00:36:38,920 that well you really do need some kind 1011 00:36:36,480 --> 00:36:40,240 of cheat sheet AKA aski in this case and 1012 00:36:38,920 --> 00:36:41,720 if we look elsewhere let me highlight 1013 00:36:40,240 --> 00:36:44,280 the left of the chart you can see that 1014 00:36:41,720 --> 00:36:46,480 next to 33 in decimal is indeed the 1015 00:36:44,280 --> 00:36:48,640 exclamation point so back in the day a 1016 00:36:46,480 --> 00:36:50,760 bunch of humans got in a room decided 1017 00:36:48,640 --> 00:36:52,800 that hey when we start building PCS and 1018 00:36:50,760 --> 00:36:55,640 later Macs and phones we all just have 1019 00:36:52,800 --> 00:36:58,200 to agree on this form of representation 1020 00:36:55,640 --> 00:36:59,920 of letters of the the English alphabet 1021 00:36:58,200 --> 00:37:02,359 in this case we just need to agree on 1022 00:36:59,920 --> 00:37:05,520 this mapping but somewhat curiously 1023 00:37:02,359 --> 00:37:07,480 notice this it turns out that once you 1024 00:37:05,520 --> 00:37:12,200 paint yourself into this corner and 1025 00:37:07,480 --> 00:37:15,119 start using 65 for a 66 for B well how 1026 00:37:12,200 --> 00:37:16,960 do you represent 65 the number and 66 1027 00:37:15,119 --> 00:37:19,200 the number if you want to do math or use 1028 00:37:16,960 --> 00:37:22,680 Excel or something like that does anyone 1029 00:37:19,200 --> 00:37:27,319 kind of see the solution perhaps how do 1030 00:37:22,680 --> 00:37:29,480 you represent the number one in asy yeah 1031 00:37:27,319 --> 00:37:32,520 in the 1032 00:37:29,480 --> 00:37:34,079 middle yeah so this is getting a little 1033 00:37:32,520 --> 00:37:35,440 maybe Inception or something but you 1034 00:37:34,079 --> 00:37:37,000 could represent numbers with other 1035 00:37:35,440 --> 00:37:38,800 numbers and so if you want to represent 1036 00:37:37,000 --> 00:37:40,760 the number you and I know as one like 1037 00:37:38,800 --> 00:37:42,560 when you type it on your keyboard turns 1038 00:37:40,760 --> 00:37:45,240 out the computer stores that as the 1039 00:37:42,560 --> 00:37:47,319 decimal number 49 if you hit two on your 1040 00:37:45,240 --> 00:37:49,520 keyboard the computer is not storing two 1041 00:37:47,319 --> 00:37:53,319 per se it's storing the decimal number 1042 00:37:49,520 --> 00:37:55,079 50 now thankfully the the uh the uh the 1043 00:37:53,319 --> 00:37:56,720 the Paradox kind of stops there we just 1044 00:37:55,079 --> 00:37:58,480 have a mapping now of numbers to to 1045 00:37:56,720 --> 00:37:59,400 numbers but really at the end of the day 1046 00:37:58,480 --> 00:38:00,760 and you're going to learn this when we 1047 00:37:59,400 --> 00:38:03,160 start writing code in that other 1048 00:38:00,760 --> 00:38:04,960 language C next week it's just context 1049 00:38:03,160 --> 00:38:06,920 dependent at the end of the day inside 1050 00:38:04,960 --> 00:38:08,960 of your Mac PC and phone there's just 1051 00:38:06,920 --> 00:38:11,000 all of these permutations of bits all of 1052 00:38:08,960 --> 00:38:12,720 these patterns of zeros and ones and 1053 00:38:11,000 --> 00:38:14,720 generally speaking when you open up a 1054 00:38:12,720 --> 00:38:16,640 text message that you've received from 1055 00:38:14,720 --> 00:38:17,960 someone it's zeros and ones but 1056 00:38:16,640 --> 00:38:19,680 obviously if it's a text message the 1057 00:38:17,960 --> 00:38:21,680 whole point of text messages is to send 1058 00:38:19,680 --> 00:38:23,280 text and so those patterns of zeros and 1059 00:38:21,680 --> 00:38:25,680 ones by default will typically be 1060 00:38:23,280 --> 00:38:27,000 interpreted as letters of the alphabet 1061 00:38:25,680 --> 00:38:28,800 so you won't see zeros and ones you 1062 00:38:27,000 --> 00:38:30,280 won't see decimal numbers you'll see the 1063 00:38:28,800 --> 00:38:33,040 English message that your friend 1064 00:38:30,280 --> 00:38:35,599 intended by contrast if you open up 1065 00:38:33,040 --> 00:38:37,760 something like Excel that same pattern 1066 00:38:35,599 --> 00:38:41,800 of zeros and ones might indeed work out 1067 00:38:37,760 --> 00:38:43,760 to be 6 uh 72 73 33 you might see cells 1068 00:38:41,800 --> 00:38:45,640 in your spreadsheet with literally those 1069 00:38:43,760 --> 00:38:46,800 three numbers why because spreadsheets 1070 00:38:45,640 --> 00:38:49,480 are all about numbers and number 1071 00:38:46,800 --> 00:38:52,079 crunching and math uh in many cases if 1072 00:38:49,480 --> 00:38:53,640 by contrast you open up Photoshop and 1073 00:38:52,079 --> 00:38:56,480 try to look at that same pattern of 1074 00:38:53,640 --> 00:38:59,079 zeros and ones it's not going to be 727 1075 00:38:56,480 --> 00:39:00,800 73 33 it's not going to be zeros and 1076 00:38:59,079 --> 00:39:02,960 ones it's not going to be high it's 1077 00:39:00,800 --> 00:39:04,720 going to be some color of the rainbow 1078 00:39:02,960 --> 00:39:06,560 you're going to use those patterns of 1079 00:39:04,720 --> 00:39:09,040 zeros and ones it turns out too to 1080 00:39:06,560 --> 00:39:12,000 represent colors and indeed so long as 1081 00:39:09,040 --> 00:39:14,000 you and I just agree as humans long have 1082 00:39:12,000 --> 00:39:15,480 what these patterns are going to be all 1083 00:39:14,000 --> 00:39:17,680 of our systems many of our systems 1084 00:39:15,480 --> 00:39:19,520 nowadays are indeed interoperable but 1085 00:39:17,680 --> 00:39:22,440 I'm being very biased here and indeed 1086 00:39:19,520 --> 00:39:25,119 the a in ASI is very American Centric 1087 00:39:22,440 --> 00:39:27,160 what do you not see in this chart if you 1088 00:39:25,119 --> 00:39:29,000 speak any other language in English odds 1089 00:39:27,160 --> 00:39:31,400 are you're not seeing characters you 1090 00:39:29,000 --> 00:39:33,480 know and love and need every day to type 1091 00:39:31,400 --> 00:39:35,280 or send messages well there's a huge 1092 00:39:33,480 --> 00:39:37,079 character set that's not supported here 1093 00:39:35,280 --> 00:39:38,520 whether it's accented characters and a 1094 00:39:37,079 --> 00:39:40,280 lot of Asian alphabets you have many 1095 00:39:38,520 --> 00:39:42,440 more symbols than can fit even on this 1096 00:39:40,280 --> 00:39:44,000 screen here and so humans kind of 1097 00:39:42,440 --> 00:39:46,280 painted themselves into a corner early 1098 00:39:44,000 --> 00:39:48,400 on or really Americans did but on a 1099 00:39:46,280 --> 00:39:50,440 typical keyboard us English keyboard 1100 00:39:48,400 --> 00:39:53,040 yeah you have A's and B's and C's 1101 00:39:50,440 --> 00:39:55,400 uppercase and lowercase but you also 1102 00:39:53,040 --> 00:39:57,599 have accented characters here and 1103 00:39:55,400 --> 00:39:59,400 nowadays not sure if this is maybe 1104 00:39:57,599 --> 00:40:01,880 necessary but nowadays you have other 1105 00:39:59,400 --> 00:40:03,160 characters on your keyboard like these 1106 00:40:01,880 --> 00:40:04,839 and these are kind of a playful 1107 00:40:03,160 --> 00:40:07,040 incarnation of what's actually a 1108 00:40:04,839 --> 00:40:08,880 technical solution to this problem if I 1109 00:40:07,040 --> 00:40:11,079 claim for the moment that ASI 1110 00:40:08,880 --> 00:40:12,520 historically used seven bits to 1111 00:40:11,079 --> 00:40:14,119 represent letters and let's just round 1112 00:40:12,520 --> 00:40:17,160 that up to a bite eight bits to 1113 00:40:14,119 --> 00:40:19,480 represent letters aski can represent as 1114 00:40:17,160 --> 00:40:22,640 many as 255 or really 1115 00:40:19,480 --> 00:40:25,079 256 total characters why 256 well if you 1116 00:40:22,640 --> 00:40:26,680 have them all zero that's zero and the 1117 00:40:25,079 --> 00:40:29,319 highest number I claimed a moment AG go 1118 00:40:26,680 --> 00:40:31,200 was 255 so that's 256 total 1119 00:40:29,319 --> 00:40:33,160 possibilities that's not many letters 1120 00:40:31,200 --> 00:40:35,359 it's fine for English but not a lot of 1121 00:40:33,160 --> 00:40:37,359 human languages so what might the 1122 00:40:35,359 --> 00:40:40,480 intuitive solution be if you want to 1123 00:40:37,359 --> 00:40:43,079 represent accented characters um Asian 1124 00:40:40,480 --> 00:40:45,359 characters Emoji even like these which 1125 00:40:43,079 --> 00:40:48,520 are just keys on a keyboard 1126 00:40:45,359 --> 00:40:50,400 nowadays what's the intuitive solution 1127 00:40:48,520 --> 00:40:52,720 if a bite's too few 1128 00:40:50,400 --> 00:40:54,599 yeah yeah so add another digit just like 1129 00:40:52,720 --> 00:40:56,280 we had a fourth volunteer come on up to 1130 00:40:54,599 --> 00:40:57,839 give us a fourth bid let's just throw 1131 00:40:56,280 --> 00:40:59,560 throw Hardware at the problem and use a 1132 00:40:57,839 --> 00:41:01,640 few more bits so maybe instead of one 1133 00:40:59,560 --> 00:41:03,560 bite let's use two or heck let's use 1134 00:41:01,640 --> 00:41:04,880 three or four bytes even though it's 1135 00:41:03,560 --> 00:41:08,560 getting a little expensive we're going 1136 00:41:04,880 --> 00:41:10,040 from 8 to 16 to 24 or 32 bits that's how 1137 00:41:08,560 --> 00:41:11,839 computers do things these days and 1138 00:41:10,040 --> 00:41:13,200 thankfully we have so much memory inside 1139 00:41:11,839 --> 00:41:15,319 of our computers and phones we can 1140 00:41:13,200 --> 00:41:17,680 certainly spare a few to represent these 1141 00:41:15,319 --> 00:41:20,800 things and the solution then to aski is 1142 00:41:17,680 --> 00:41:24,280 what we'll call Unicode so Unicode is 1143 00:41:20,800 --> 00:41:26,520 also just a mapping of letters to num of 1144 00:41:24,280 --> 00:41:27,680 numbers to letters but in many many 1145 00:41:26,520 --> 00:41:30,240 different languages and indeed the 1146 00:41:27,680 --> 00:41:32,720 Unicode Consortium is a bunch of people 1147 00:41:30,240 --> 00:41:35,319 from all different companies and and uh 1148 00:41:32,720 --> 00:41:37,680 and a lot of different uh companies and 1149 00:41:35,319 --> 00:41:40,680 countries and cultures whose mission as 1150 00:41:37,680 --> 00:41:43,800 an organization is to capture digitally 1151 00:41:40,680 --> 00:41:45,800 all forms of human language in this case 1152 00:41:43,800 --> 00:41:47,520 and to ensure that especially smaller 1153 00:41:45,800 --> 00:41:49,000 demographics of humans speaking lesser 1154 00:41:47,520 --> 00:41:51,240 known languages are nonetheless 1155 00:41:49,000 --> 00:41:53,640 represented and preserve digitally using 1156 00:41:51,240 --> 00:41:55,880 some mapping of these zeros and ones it 1157 00:41:53,640 --> 00:41:58,440 turns out though if you start using 32 1158 00:41:55,880 --> 00:42:00,960 bits as many as 32 bits to represent 1159 00:41:58,440 --> 00:42:03,280 characters on a keyboard that's 4 1160 00:42:00,960 --> 00:42:05,319 billion possible permutations of zeros 1161 00:42:03,280 --> 00:42:06,880 and ones that's way more than we need 1162 00:42:05,319 --> 00:42:08,240 for most human languages so there's a 1163 00:42:06,880 --> 00:42:10,119 little bit of room in there for some of 1164 00:42:08,240 --> 00:42:12,319 those more playful things like those 1165 00:42:10,119 --> 00:42:14,040 Emoji so for instance suppose you got a 1166 00:42:12,319 --> 00:42:15,040 text message with this pattern of zeros 1167 00:42:14,040 --> 00:42:17,680 and 1168 00:42:15,040 --> 00:42:19,040 ones or if we do out the math suppose 1169 00:42:17,680 --> 00:42:21,920 you receive a text message that if you 1170 00:42:19,040 --> 00:42:25,760 do out the math in decimal is 4 B 36 1171 00:42:21,920 --> 00:42:27,119 m991 106 anyone know what emoji you're 1172 00:42:25,760 --> 00:42:30,359 looking 1173 00:42:27,119 --> 00:42:33,000 at this would be weird if you do 1174 00:42:30,359 --> 00:42:34,520 but what is this Well turns out that as 1175 00:42:33,000 --> 00:42:37,200 of this past year this is the most 1176 00:42:34,520 --> 00:42:40,640 popular Emoji to be sent uh by many 1177 00:42:37,200 --> 00:42:42,440 measures face with tears of joy so that 1178 00:42:40,640 --> 00:42:44,119 is the pattern that a bunch of humans in 1179 00:42:42,440 --> 00:42:46,760 the Unicode Consortium decided would 1180 00:42:44,119 --> 00:42:48,200 represent this but you'll notice some 1181 00:42:46,760 --> 00:42:50,480 many of you might have iPhones some of 1182 00:42:48,200 --> 00:42:52,040 you might have Android devices too and 1183 00:42:50,480 --> 00:42:53,520 sometimes these don't actually look 1184 00:42:52,040 --> 00:42:55,839 quite the same this happens to be the 1185 00:42:53,520 --> 00:42:59,480 current version of face with tears of 1186 00:42:55,839 --> 00:43:01,440 joy on iOS on Android it tends to look a 1187 00:42:59,480 --> 00:43:04,000 little something more like this and 1188 00:43:01,440 --> 00:43:05,480 here's kind of a curiosity even though 1189 00:43:04,000 --> 00:43:07,720 you and I look at these things and they 1190 00:43:05,480 --> 00:43:09,680 look like images they're not images 1191 00:43:07,720 --> 00:43:12,839 they're characters at least as we've 1192 00:43:09,680 --> 00:43:15,440 defined them now in Unicode and a IOS 1193 00:43:12,839 --> 00:43:18,240 and Android and Windows and Facebook and 1194 00:43:15,440 --> 00:43:19,880 other companies and uh apps nowadays 1195 00:43:18,240 --> 00:43:21,720 really just have different fonts if you 1196 00:43:19,880 --> 00:43:23,800 will so just like fonts with English and 1197 00:43:21,720 --> 00:43:26,839 other languages can give you different F 1198 00:43:23,800 --> 00:43:28,480 characters with seraps or not emoji are 1199 00:43:26,839 --> 00:43:30,240 themselves yes drawings that someone 1200 00:43:28,480 --> 00:43:32,119 made but they're really just a font and 1201 00:43:30,240 --> 00:43:34,119 so that same pattern of zeros and ones 1202 00:43:32,119 --> 00:43:36,079 might just render slightly differently 1203 00:43:34,119 --> 00:43:37,920 on someone's phone or another if you've 1204 00:43:36,079 --> 00:43:39,960 ever gotten like an icon on your phone 1205 00:43:37,920 --> 00:43:41,599 that's broken and you've been sent an 1206 00:43:39,960 --> 00:43:43,680 emoji but it's like a square or 1207 00:43:41,599 --> 00:43:45,200 something arbitrary and not sensical 1208 00:43:43,680 --> 00:43:46,720 sensible it might just mean that you 1209 00:43:45,200 --> 00:43:49,079 have not updated to the latest version 1210 00:43:46,720 --> 00:43:51,040 of iOS or Android which just updates the 1211 00:43:49,079 --> 00:43:53,200 font of supported Emoji because those 1212 00:43:51,040 --> 00:43:55,680 Folks at Unicode pretty much every year 1213 00:43:53,200 --> 00:43:57,920 nowadays are adding more and more Emoji 1214 00:43:55,680 --> 00:43:59,359 to that that particular character said 1215 00:43:57,920 --> 00:44:01,079 now I went down the rabbit hole figuring 1216 00:43:59,359 --> 00:44:03,400 out the other day just which are the 1217 00:44:01,079 --> 00:44:05,319 most popular Emoji these days on Twitter 1218 00:44:03,400 --> 00:44:08,960 specifically this past year the most 1219 00:44:05,319 --> 00:44:11,200 popular Emoji by contrast uh was loudly 1220 00:44:08,960 --> 00:44:13,920 crying face I don't know if that says 1221 00:44:11,200 --> 00:44:15,480 more about 20121 or about Twitter but 1222 00:44:13,920 --> 00:44:18,200 you'll see different Trends certainly in 1223 00:44:15,480 --> 00:44:20,440 how these are used but even humans 1224 00:44:18,200 --> 00:44:22,480 themselves didn't necessarily think two 1225 00:44:20,440 --> 00:44:24,880 steps ahead and now a lot of the Emoji 1226 00:44:22,480 --> 00:44:26,280 are the sort of default yellow color but 1227 00:44:24,880 --> 00:44:27,640 there's a lot of emoji that aren't sort 1228 00:44:26,280 --> 00:44:29,240 of these cartoon characters but they're 1229 00:44:27,640 --> 00:44:31,920 meant to represent humans in various 1230 00:44:29,240 --> 00:44:33,559 professions or gestures or the like and 1231 00:44:31,920 --> 00:44:36,000 nowadays too you've probably noticed on 1232 00:44:33,559 --> 00:44:37,839 your phone and Macs and PCs there are 1233 00:44:36,000 --> 00:44:40,599 different skin tones that you can assign 1234 00:44:37,839 --> 00:44:42,520 to certain emojis if it's supported by 1235 00:44:40,599 --> 00:44:44,440 the company and by Unicode you can 1236 00:44:42,520 --> 00:44:45,680 actually like touch and hold on a 1237 00:44:44,440 --> 00:44:47,200 certain emoji and then you can choose 1238 00:44:45,680 --> 00:44:48,880 the appropriate skin tone to represent 1239 00:44:47,200 --> 00:44:50,920 yourself or someone else and that then 1240 00:44:48,880 --> 00:44:53,119 modifies the display well let's just 1241 00:44:50,920 --> 00:44:54,960 think for a moment here how did Apple 1242 00:44:53,119 --> 00:44:58,040 and Google and Microsoft and others go 1243 00:44:54,960 --> 00:45:00,040 about implementing support for emoji 1244 00:44:58,040 --> 00:45:03,400 with different skin tones how could you 1245 00:45:00,040 --> 00:45:06,160 do this if you want to represent some 1246 00:45:03,400 --> 00:45:07,800 smiling Emoji but in five in this case 1247 00:45:06,160 --> 00:45:09,800 different skin tones you could come up 1248 00:45:07,800 --> 00:45:12,160 with what five different patterns that 1249 00:45:09,800 --> 00:45:14,240 are identical structurally except for 1250 00:45:12,160 --> 00:45:16,440 the skin tone used in places in that 1251 00:45:14,240 --> 00:45:18,280 image but that's a little inefficient 1252 00:45:16,440 --> 00:45:20,000 right to kind of just do copy paste 1253 00:45:18,280 --> 00:45:22,000 paste paste paste and like change the 1254 00:45:20,000 --> 00:45:24,079 color in Photoshop if you will that's 1255 00:45:22,000 --> 00:45:27,319 going to use more bits more information 1256 00:45:24,079 --> 00:45:28,839 than you might need to how else if you 1257 00:45:27,319 --> 00:45:30,440 now start to think a little bit more 1258 00:45:28,839 --> 00:45:31,760 like a computer scientist if at the end 1259 00:45:30,440 --> 00:45:34,200 of the day all you have are zeros and 1260 00:45:31,760 --> 00:45:35,839 ones how else could you implement skin 1261 00:45:34,200 --> 00:45:38,400 tones might you think 1262 00:45:35,839 --> 00:45:39,720 yeah okay so RGB and we'll come to that 1263 00:45:38,400 --> 00:45:42,240 in just a moment that stands for red 1264 00:45:39,720 --> 00:45:44,720 green blue that's one way in this case 1265 00:45:42,240 --> 00:45:47,359 though I'm seeking an alternative to 1266 00:45:44,720 --> 00:45:48,880 just using five different patterns of 1267 00:45:47,359 --> 00:45:51,000 zeros and ones to represent the same 1268 00:45:48,880 --> 00:45:52,800 Emoji but different skin tones so not 1269 00:45:51,000 --> 00:45:56,160 quite RGB 1270 00:45:52,800 --> 00:45:58,079 yeah okay so store one copy of the Emoji 1271 00:45:56,160 --> 00:46:00,200 and then store different variants of the 1272 00:45:58,079 --> 00:46:01,720 color that you want to assign to that 1273 00:46:00,200 --> 00:46:04,359 emoji yeah so this is actually an 1274 00:46:01,720 --> 00:46:06,800 example of do you want to 1275 00:46:04,359 --> 00:46:08,000 elaborate okay so you can use a loop to 1276 00:46:06,800 --> 00:46:09,640 actually output these things more on 1277 00:46:08,000 --> 00:46:11,440 that in a moment let me go down this 1278 00:46:09,640 --> 00:46:13,520 this road for just a moment this would 1279 00:46:11,440 --> 00:46:16,319 be in some sense sort of a a better 1280 00:46:13,520 --> 00:46:18,520 design if you will uh but why yeah 1281 00:46:16,319 --> 00:46:20,040 filter okay so filter if we think sort 1282 00:46:18,520 --> 00:46:21,559 of in the Instagram sense you can sort 1283 00:46:20,040 --> 00:46:24,520 of change the color of something and 1284 00:46:21,559 --> 00:46:26,920 that could be related here too could it 1285 00:46:24,520 --> 00:46:28,359 be oh interesting so maybe it could be 1286 00:46:26,920 --> 00:46:29,480 just a completely different font and you 1287 00:46:28,359 --> 00:46:31,240 have five different fonts that are 1288 00:46:29,480 --> 00:46:32,839 almost identical except for the various 1289 00:46:31,240 --> 00:46:34,960 interpretations of skin tone for those 1290 00:46:32,839 --> 00:46:37,079 same Emoji let me spoil I think if we go 1291 00:46:34,960 --> 00:46:39,680 down this one particular Road the way 1292 00:46:37,079 --> 00:46:42,920 the uh the Unicode folks decided to do 1293 00:46:39,680 --> 00:46:45,240 this some years ago were the first uh 1294 00:46:42,920 --> 00:46:47,119 bite or bites that you receive via text 1295 00:46:45,240 --> 00:46:48,680 or email just represent like the 1296 00:46:47,119 --> 00:46:50,520 structure of the Emoji the default 1297 00:46:48,680 --> 00:46:52,520 Yellow Version thereof but if it's 1298 00:46:50,520 --> 00:46:54,280 immediately followed by a certain 1299 00:46:52,520 --> 00:46:56,599 pattern of bits that these humans 1300 00:46:54,280 --> 00:46:59,160 standardize to represent each of these 1301 00:46:56,599 --> 00:47:01,960 different shades of skin tone then the 1302 00:46:59,160 --> 00:47:03,960 phone the Mac the PC will change that 1303 00:47:01,960 --> 00:47:06,280 default color yellow in most cases to 1304 00:47:03,960 --> 00:47:08,640 whatever the more apt human tone is so 1305 00:47:06,280 --> 00:47:10,960 you just use twice as many bits but you 1306 00:47:08,640 --> 00:47:13,119 don't use five times as many bits so 1307 00:47:10,960 --> 00:47:15,839 what do I mean you don't have uh five 1308 00:47:13,119 --> 00:47:19,119 completely distinct patterns per se uh 1309 00:47:15,839 --> 00:47:21,720 for each of these possible variants you 1310 00:47:19,119 --> 00:47:25,480 have a representation of just the Emoji 1311 00:47:21,720 --> 00:47:27,400 itself structurally and then uh reusable 1312 00:47:25,480 --> 00:47:30,440 pattern patterns for those five skin 1313 00:47:27,400 --> 00:47:32,000 tones un um unfortunately that wasn't 1314 00:47:30,440 --> 00:47:33,800 quite versatile enough for other 1315 00:47:32,000 --> 00:47:35,400 features that were in the pipeline and 1316 00:47:33,800 --> 00:47:38,240 nowadays too and there's a double 1317 00:47:35,400 --> 00:47:40,880 meaning now to representation emojis had 1318 00:47:38,240 --> 00:47:42,280 tended to focus on certain professions 1319 00:47:40,880 --> 00:47:43,599 and early on too were certain 1320 00:47:42,280 --> 00:47:45,800 professions associated with certain 1321 00:47:43,599 --> 00:47:47,760 genders and vice versa and you couldn't 1322 00:47:45,800 --> 00:47:49,040 necessarily be one gender or another in 1323 00:47:47,760 --> 00:47:50,359 a certain profession or another there 1324 00:47:49,040 --> 00:47:51,920 were these combinatorics that just 1325 00:47:50,359 --> 00:47:53,880 weren't possible but nowadays as you 1326 00:47:51,920 --> 00:47:55,760 might have seen you can have couples in 1327 00:47:53,880 --> 00:47:57,480 love for instance that actually look a a 1328 00:47:55,760 --> 00:47:58,920 little more like three emojis but just 1329 00:47:57,480 --> 00:48:01,000 kind of combined into one and indeed 1330 00:47:58,920 --> 00:48:02,960 this is just one key press on your phone 1331 00:48:01,000 --> 00:48:05,440 and you can combine different emoji on 1332 00:48:02,960 --> 00:48:07,359 the left and in the right with the Emoji 1333 00:48:05,440 --> 00:48:09,200 in the middle and so it turns out how 1334 00:48:07,359 --> 00:48:12,800 computers nowadays represent these 1335 00:48:09,200 --> 00:48:14,680 patterns are one set of bits for the 1336 00:48:12,800 --> 00:48:16,440 character on the left one set of bits 1337 00:48:14,680 --> 00:48:18,359 for a character on the right one set of 1338 00:48:16,440 --> 00:48:20,960 bits for whatever Emoji you want in the 1339 00:48:18,359 --> 00:48:23,680 middle and then you assemble more 1340 00:48:20,960 --> 00:48:25,680 complicated compositions of emoji by 1341 00:48:23,680 --> 00:48:27,920 just reusing those same patterns and 1342 00:48:25,680 --> 00:48:29,440 bits and bits Emoji doesn't the Unicode 1343 00:48:27,920 --> 00:48:32,079 folks don't have to come up with a whole 1344 00:48:29,440 --> 00:48:34,520 new representation for some very 1345 00:48:32,079 --> 00:48:37,440 specific Incarnation they can create one 1346 00:48:34,520 --> 00:48:38,599 for person for male for female for other 1347 00:48:37,440 --> 00:48:41,160 uh characters that you might want to 1348 00:48:38,599 --> 00:48:43,319 display and reuse those same patterns of 1349 00:48:41,160 --> 00:48:44,640 zeros and ones and so here you see sort 1350 00:48:43,319 --> 00:48:46,200 of the imperfection of or lack of 1351 00:48:44,640 --> 00:48:47,720 foresight of humans for building a 1352 00:48:46,200 --> 00:48:49,799 system early on that was entirely 1353 00:48:47,720 --> 00:48:51,640 American Centric no characters Emoji or 1354 00:48:49,799 --> 00:48:53,400 the like that's evolved too and so 1355 00:48:51,640 --> 00:48:56,160 that's an important detail in Computing 1356 00:48:53,400 --> 00:48:57,640 nowadays um it too is evolved in and the 1357 00:48:56,160 --> 00:49:00,240 languages you're about to learn in the 1358 00:48:57,640 --> 00:49:01,839 coming days those twoo are evolving as 1359 00:49:00,240 --> 00:49:03,920 well and new features are getting added 1360 00:49:01,839 --> 00:49:05,359 and even programming languages have 1361 00:49:03,920 --> 00:49:06,799 version numbers you might have a 1362 00:49:05,359 --> 00:49:08,839 different version of an app on your 1363 00:49:06,799 --> 00:49:11,200 phone programming languages too have 1364 00:49:08,839 --> 00:49:13,000 different versions as well questions 1365 00:49:11,200 --> 00:49:15,880 then thus far and how information is 1366 00:49:13,000 --> 00:49:18,880 represented using asy or 1367 00:49:15,880 --> 00:49:21,559 Unicode or anything in between yeah so 1368 00:49:18,880 --> 00:49:26,240 you can use a string of a good question 1369 00:49:21,559 --> 00:49:28,319 so to recap why can't you just um well 1370 00:49:26,240 --> 00:49:30,440 let me summarize that as why can't you 1371 00:49:28,319 --> 00:49:32,000 similarly use different patterns to 1372 00:49:30,440 --> 00:49:33,599 change the context of what these 1373 00:49:32,000 --> 00:49:36,119 patterns of bits represent whether it's 1374 00:49:33,599 --> 00:49:37,680 a number or a letter or a graphic in 1375 00:49:36,119 --> 00:49:39,319 actuality that's kind of what's 1376 00:49:37,680 --> 00:49:41,240 Happening underneath the hood it's not 1377 00:49:39,319 --> 00:49:42,799 standardized in quite the same way but 1378 00:49:41,240 --> 00:49:44,400 starting next week when we transition 1379 00:49:42,799 --> 00:49:46,359 from scratch to C you'll learn about 1380 00:49:44,400 --> 00:49:48,200 types data types where the onus 1381 00:49:46,359 --> 00:49:50,720 initially is going to be on you the 1382 00:49:48,200 --> 00:49:51,960 programmer to tell the program whether 1383 00:49:50,720 --> 00:49:54,720 or not this pattern of bit should be 1384 00:49:51,960 --> 00:49:57,400 interpreted as a number or as a letter 1385 00:49:54,720 --> 00:49:58,599 or as a color or something else nowadays 1386 00:49:57,400 --> 00:50:00,640 though in toward the end of the semester 1387 00:49:58,599 --> 00:50:02,760 you'll use languages like python where 1388 00:50:00,640 --> 00:50:04,760 the computer just figures it out for you 1389 00:50:02,760 --> 00:50:07,119 by context which makes it even easier 1390 00:50:04,760 --> 00:50:10,680 and faster to program as well other 1391 00:50:07,119 --> 00:50:12,880 questions on Unicode asky or the 1392 00:50:10,680 --> 00:50:14,599 like all right well how about just a few 1393 00:50:12,880 --> 00:50:16,520 other forms of information RGB was 1394 00:50:14,599 --> 00:50:18,720 called out earlier red green blue How do 1395 00:50:16,520 --> 00:50:21,160 images get represented in computers well 1396 00:50:18,720 --> 00:50:22,920 in fact it's typically an assembly of 1397 00:50:21,160 --> 00:50:24,400 some amount of red some amount of green 1398 00:50:22,920 --> 00:50:25,880 some amount of blue but there are other 1399 00:50:24,400 --> 00:50:28,000 representations if you're a graphic 1400 00:50:25,880 --> 00:50:29,920 designer you might know them but RGB is 1401 00:50:28,000 --> 00:50:31,760 still pretty common what does this mean 1402 00:50:29,920 --> 00:50:34,440 this means to represent every dot on 1403 00:50:31,760 --> 00:50:37,359 your phone or every dot on your TV or 1404 00:50:34,440 --> 00:50:38,760 your laptop or desktop there is a number 1405 00:50:37,359 --> 00:50:40,440 representing how much red that dot 1406 00:50:38,760 --> 00:50:42,079 should show a number representing how 1407 00:50:40,440 --> 00:50:44,920 much green and a number representing how 1408 00:50:42,079 --> 00:50:47,520 much blue it should show red green blue 1409 00:50:44,920 --> 00:50:49,799 respectively so for instance if a DOT on 1410 00:50:47,520 --> 00:50:52,400 your screen were using these three 1411 00:50:49,799 --> 00:50:56,119 numbers these three values or bytes 72 1412 00:50:52,400 --> 00:50:58,680 73 33 in a text message email that would 1413 00:50:56,119 --> 00:51:01,119 be interpreted as I claimed high but in 1414 00:50:58,680 --> 00:51:02,920 Photoshop or in some graphical program 1415 00:51:01,119 --> 00:51:05,400 that same pattern would be repres would 1416 00:51:02,920 --> 00:51:07,480 be interpreted as let's call it a medium 1417 00:51:05,400 --> 00:51:09,440 amount of red a medium amount of green 1418 00:51:07,480 --> 00:51:11,400 and a little bit of blue and why medium 1419 00:51:09,440 --> 00:51:13,400 and little turns out that each of these 1420 00:51:11,400 --> 00:51:15,240 are bites the smallest value you can 1421 00:51:13,400 --> 00:51:16,680 have in a bite we said is zero the 1422 00:51:15,240 --> 00:51:19,000 largest value you can have in a bite is 1423 00:51:16,680 --> 00:51:20,680 255 so I'm just kind of spitballing here 1424 00:51:19,000 --> 00:51:23,760 this is like medium medium and a low 1425 00:51:20,680 --> 00:51:25,960 amount of red green blue uh specifically 1426 00:51:23,760 --> 00:51:28,000 those three colors um like uh 1427 00:51:25,960 --> 00:51:30,760 wavelengths of light are combined in 1428 00:51:28,000 --> 00:51:32,720 such a way that you would have this dot 1429 00:51:30,760 --> 00:51:35,839 on the screen a sort of murky shade of 1430 00:51:32,720 --> 00:51:38,040 yellow or brown that is how a computer 1431 00:51:35,839 --> 00:51:40,200 would store precisely that color and in 1432 00:51:38,040 --> 00:51:42,520 fact we've seen this color when you type 1433 00:51:40,200 --> 00:51:44,000 in face with tears of joy generally on 1434 00:51:42,520 --> 00:51:47,079 your screen it looks like this typically 1435 00:51:44,000 --> 00:51:49,440 much smaller but let's zoom in or let's 1436 00:51:47,079 --> 00:51:52,160 zoom in a little more what do you 1437 00:51:49,440 --> 00:51:54,280 starting to see if you know the term so 1438 00:51:52,160 --> 00:51:56,160 pixels it's getting very pixelated a 1439 00:51:54,280 --> 00:51:57,720 pixel is just a DOT on the screen and if 1440 00:51:56,160 --> 00:51:59,760 you really zoom in on it you can 1441 00:51:57,720 --> 00:52:02,799 literally see all of the dots that 1442 00:51:59,760 --> 00:52:04,680 compose in emoji in this case on iOS in 1443 00:52:02,799 --> 00:52:06,160 the font that Apple's using to represent 1444 00:52:04,680 --> 00:52:08,280 this particular pattern of zeros and 1445 00:52:06,160 --> 00:52:09,960 ones so one of those yellow dots and 1446 00:52:08,280 --> 00:52:12,200 there's many of them all that kind of 1447 00:52:09,960 --> 00:52:15,200 blend together here each dot on the 1448 00:52:12,200 --> 00:52:17,200 screen I claim is three bytes how much 1449 00:52:15,200 --> 00:52:18,599 red green blue for this dot how much red 1450 00:52:17,200 --> 00:52:19,960 green blue for this dot how much red 1451 00:52:18,599 --> 00:52:21,319 green blue for this Dot and you'll 1452 00:52:19,960 --> 00:52:23,280 notice too that when it gets to be sort 1453 00:52:21,319 --> 00:52:26,359 of brownish here the dots really stand 1454 00:52:23,280 --> 00:52:29,119 out the three values the three bytes AKA 1455 00:52:26,359 --> 00:52:31,119 24 bits are just slightly different and 1456 00:52:29,119 --> 00:52:33,680 so underneath the hood this is why 1457 00:52:31,119 --> 00:52:35,880 images photographs that you take or GIFs 1458 00:52:33,680 --> 00:52:38,119 that you download get so darn big 1459 00:52:35,880 --> 00:52:40,920 potentially because you have a number 1460 00:52:38,119 --> 00:52:42,799 representing every dot on the screen 1461 00:52:40,920 --> 00:52:44,839 well if this I claim is indeed how 1462 00:52:42,799 --> 00:52:47,200 images are typically represented using 1463 00:52:44,839 --> 00:52:49,319 pattern of bits that are assigned to 1464 00:52:47,200 --> 00:52:52,319 some amount of red green or blue how do 1465 00:52:49,319 --> 00:52:54,240 you get video what is a video if at the 1466 00:52:52,319 --> 00:52:55,760 end of the day all we have are zeros and 1467 00:52:54,240 --> 00:52:58,880 ones 1468 00:52:55,760 --> 00:53:01,359 what's a video perhaps yeah oh let's go 1469 00:52:58,880 --> 00:53:03,599 here weighing back yeah pixel's really 1470 00:53:01,359 --> 00:53:04,920 changing values over time and do you 1471 00:53:03,599 --> 00:53:07,400 want to confirm or deny the hand that 1472 00:53:04,920 --> 00:53:09,599 went up here yeah or equivalently a 1473 00:53:07,400 --> 00:53:11,200 sequence of images that over time are 1474 00:53:09,599 --> 00:53:13,079 changing on the screen so both of those 1475 00:53:11,200 --> 00:53:14,680 are valid interpretations and you know 1476 00:53:13,079 --> 00:53:16,319 just for fun if you uh grew up with 1477 00:53:14,680 --> 00:53:18,400 these sort of picture books you might 1478 00:53:16,319 --> 00:53:20,590 remember a little something like this if 1479 00:53:18,400 --> 00:53:24,160 we could dim the 1480 00:53:20,590 --> 00:53:25,680 [Music] 1481 00:53:24,160 --> 00:53:40,640 lights 1482 00:53:25,680 --> 00:53:42,520 [Music] 1483 00:53:40,640 --> 00:53:44,520 so that's sort of the old school analog 1484 00:53:42,520 --> 00:53:47,040 way to implement a video in the sense 1485 00:53:44,520 --> 00:53:49,240 that um that artist wrote out like 1486 00:53:47,040 --> 00:53:51,440 hundreds of pieces of paper with almost 1487 00:53:49,240 --> 00:53:53,839 identical images but where the ink from 1488 00:53:51,440 --> 00:53:55,520 their pencil or pen was slightly moving 1489 00:53:53,839 --> 00:53:57,040 and if you digitize that such that each 1490 00:53:55,520 --> 00:53:58,839 of those Strokes are represented with 1491 00:53:57,040 --> 00:54:00,680 dots instead that's really what you're 1492 00:53:58,839 --> 00:54:02,760 seeing as a sequence of all these images 1493 00:54:00,680 --> 00:54:04,400 flying across the screen and if we dive 1494 00:54:02,760 --> 00:54:06,280 into the the real world if you've ever 1495 00:54:04,400 --> 00:54:09,359 watched a film a Hollywood movie is 1496 00:54:06,280 --> 00:54:12,200 typically 24 FPS frames per second that 1497 00:54:09,359 --> 00:54:14,400 really means you're seeing 24 images per 1498 00:54:12,200 --> 00:54:16,720 second or on TV or in soap operas it's 1499 00:54:14,400 --> 00:54:18,240 often 30 frames per second that makes 1500 00:54:16,720 --> 00:54:21,000 things look a little more smooth so it's 1501 00:54:18,240 --> 00:54:22,920 not actual motion picture if you will it 1502 00:54:21,000 --> 00:54:25,160 sequences of pictures and your brain and 1503 00:54:22,920 --> 00:54:26,440 mind are kind of interpolating that oh 1504 00:54:25,160 --> 00:54:28,359 this is smooth movement even though 1505 00:54:26,440 --> 00:54:30,359 we're just seeing a lot of pictures 1506 00:54:28,359 --> 00:54:31,720 really fast now that gets really big and 1507 00:54:30,359 --> 00:54:33,640 we'll talk later in the semester how you 1508 00:54:31,720 --> 00:54:35,440 can compress information so that you're 1509 00:54:33,640 --> 00:54:36,720 not using way more bits than you 1510 00:54:35,440 --> 00:54:38,440 actually need to and there's fancy 1511 00:54:36,720 --> 00:54:40,000 algorithms that folks have developed but 1512 00:54:38,440 --> 00:54:42,599 at the end of the day that's really all 1513 00:54:40,000 --> 00:54:44,240 a video might be is a sequence of images 1514 00:54:42,599 --> 00:54:45,920 conversely if you want to represent the 1515 00:54:44,240 --> 00:54:48,240 music that accompanies that or something 1516 00:54:45,920 --> 00:54:50,760 else if any of you play an instrument 1517 00:54:48,240 --> 00:54:53,160 and can read sheet music How Could You 1518 00:54:50,760 --> 00:54:56,319 digitize this like how could you 1519 00:54:53,160 --> 00:54:58,240 represent musical notes in a computer 1520 00:54:56,319 --> 00:54:59,920 you and I hear them when we play files 1521 00:54:58,240 --> 00:55:01,160 but what's really going on underneath 1522 00:54:59,920 --> 00:55:05,839 the 1523 00:55:01,160 --> 00:55:09,319 hood any any musicians piano players 1524 00:55:05,839 --> 00:55:11,640 anyone yeah Val okay so Hertz value so 1525 00:55:09,319 --> 00:55:12,960 some frequency so sound is some 1526 00:55:11,640 --> 00:55:14,440 frequency and it's kind of hitting your 1527 00:55:12,960 --> 00:55:16,440 eardrum and that's what makes it sound 1528 00:55:14,440 --> 00:55:17,960 low or high or somewhere in between so 1529 00:55:16,440 --> 00:55:19,960 maybe we could assign just like there's 1530 00:55:17,960 --> 00:55:21,680 letters a through G here maybe we could 1531 00:55:19,960 --> 00:55:23,119 assign specific frequency values which 1532 00:55:21,680 --> 00:55:24,440 are just going to be numbers measured in 1533 00:55:23,119 --> 00:55:26,039 something called Hertz something per 1534 00:55:24,440 --> 00:55:27,559 second and maybe we could have a few 1535 00:55:26,039 --> 00:55:30,079 other numbers for each of these notes 1536 00:55:27,559 --> 00:55:32,480 not just the note or the frequency maybe 1537 00:55:30,079 --> 00:55:34,520 we could represent the loudness of it 1538 00:55:32,480 --> 00:55:36,240 like how hard or how softly a human 1539 00:55:34,520 --> 00:55:38,440 might equivalently press it maybe a 1540 00:55:36,240 --> 00:55:40,480 third number like duration like how long 1541 00:55:38,440 --> 00:55:42,480 is their finger on the keyboard so you 1542 00:55:40,480 --> 00:55:43,799 could imagine quantizing something like 1543 00:55:42,480 --> 00:55:45,799 music that in the real world is 1544 00:55:43,799 --> 00:55:48,200 perfectly continuous as something more 1545 00:55:45,799 --> 00:55:50,440 discret by representing each note over 1546 00:55:48,200 --> 00:55:51,599 time as just some sequence of values and 1547 00:55:50,440 --> 00:55:55,319 there's so many different ways to do 1548 00:55:51,599 --> 00:55:57,359 this midi if you've heard uh MP3s AAC I 1549 00:55:55,319 --> 00:55:59,319 mean almost all the file extensions you 1550 00:55:57,359 --> 00:56:01,599 see on your Mac or PC if you see them at 1551 00:55:59,319 --> 00:56:03,440 all ultimately just mean there's a 1552 00:56:01,599 --> 00:56:07,400 different form of representation for in 1553 00:56:03,440 --> 00:56:09,480 this case something like sound so let me 1554 00:56:07,400 --> 00:56:11,039 just stipulate there are these and many 1555 00:56:09,480 --> 00:56:13,000 more ways to represent inputs and 1556 00:56:11,039 --> 00:56:14,520 outputs and thankfully humans have 1557 00:56:13,000 --> 00:56:16,640 standardized a lot of this they don't 1558 00:56:14,520 --> 00:56:18,720 always dis agree and this is why we have 1559 00:56:16,640 --> 00:56:20,280 different file formats for Apple numbers 1560 00:56:18,720 --> 00:56:21,440 and Microsoft Excel and Google 1561 00:56:20,280 --> 00:56:23,280 spreadsheets and sort of stupid 1562 00:56:21,440 --> 00:56:24,440 incompatibilities like that but 1563 00:56:23,280 --> 00:56:26,240 generally speaking humans have 1564 00:56:24,440 --> 00:56:28,799 standardized how we represent the inputs 1565 00:56:26,240 --> 00:56:31,000 and outputs to and from problems but 1566 00:56:28,799 --> 00:56:33,839 let's now focus on this black box so to 1567 00:56:31,000 --> 00:56:35,720 speak in the middle this abstraction so 1568 00:56:33,839 --> 00:56:37,079 abstraction is technically a term that 1569 00:56:35,720 --> 00:56:38,440 you'll see all over the place in 1570 00:56:37,079 --> 00:56:40,839 computer science and really problem 1571 00:56:38,440 --> 00:56:43,200 solving that just refers to the 1572 00:56:40,839 --> 00:56:44,839 simplification of something so that you 1573 00:56:43,200 --> 00:56:46,839 don't focus on the lower level 1574 00:56:44,839 --> 00:56:49,160 implementation details you really just 1575 00:56:46,839 --> 00:56:52,240 focus on the high level goals or the 1576 00:56:49,160 --> 00:56:53,599 process itself uh Therefore your car if 1577 00:56:52,240 --> 00:56:55,799 youve uh if you have a license and have 1578 00:56:53,599 --> 00:56:57,079 driven or have been in a car a car so 1579 00:56:55,799 --> 00:56:58,839 far as you're concerned is probably an 1580 00:56:57,079 --> 00:57:00,760 abstraction most of us if you're like me 1581 00:56:58,839 --> 00:57:02,440 probably don't really know or care how 1582 00:57:00,760 --> 00:57:04,520 the engine works and all the parts that 1583 00:57:02,440 --> 00:57:06,200 are moving to you it's just a way of 1584 00:57:04,520 --> 00:57:07,880 getting from point A to point B it's an 1585 00:57:06,200 --> 00:57:10,119 abstraction but someone hopefully the 1586 00:57:07,880 --> 00:57:12,359 mechanic does know those lower level 1587 00:57:10,119 --> 00:57:13,720 implementation details if you had to 1588 00:57:12,359 --> 00:57:15,680 understand how a car works every time 1589 00:57:13,720 --> 00:57:17,039 you want to go to school or to the store 1590 00:57:15,680 --> 00:57:18,720 it's probably going to be a pretty slow 1591 00:57:17,039 --> 00:57:20,760 process you just want to think and 1592 00:57:18,720 --> 00:57:22,200 operate at this higher level of 1593 00:57:20,760 --> 00:57:24,000 abstraction and we're going to do this 1594 00:57:22,200 --> 00:57:25,319 all the time when writing code and 1595 00:57:24,000 --> 00:57:27,559 solving problems 1596 00:57:25,319 --> 00:57:29,799 so what then is in this black box this 1597 00:57:27,559 --> 00:57:31,160 abstraction at the moment well generally 1598 00:57:29,799 --> 00:57:33,160 it's what a computer scientist would 1599 00:57:31,160 --> 00:57:35,160 call an algorithm stepbystep 1600 00:57:33,160 --> 00:57:37,079 instructions for solving some problem 1601 00:57:35,160 --> 00:57:39,319 now let's consider the implementation 1602 00:57:37,079 --> 00:57:41,720 details that is to say how you might 1603 00:57:39,319 --> 00:57:43,920 solve certain problems and let's take a 1604 00:57:41,720 --> 00:57:46,240 a sort of old school example but in 1605 00:57:43,920 --> 00:57:48,440 modern form this icon if you have an 1606 00:57:46,240 --> 00:57:49,799 iPhone is of course for your contacts 1607 00:57:48,440 --> 00:57:51,640 application and if you've got a whole 1608 00:57:49,799 --> 00:57:53,280 bunch of family members or friends or 1609 00:57:51,640 --> 00:57:54,920 colleagues in your phone book you have 1610 00:57:53,280 --> 00:57:56,680 some kind of contacts pictured here here 1611 00:57:54,920 --> 00:57:58,520 and it's alphabetized typically by first 1612 00:57:56,680 --> 00:57:59,920 name and last name and odds are you and 1613 00:57:58,520 --> 00:58:02,119 I are in the habit if they're not 1614 00:57:59,920 --> 00:58:04,000 already a favorite of like clicking on 1615 00:58:02,119 --> 00:58:05,200 search and then using autocomplete and 1616 00:58:04,000 --> 00:58:07,039 what happens when you start typing 1617 00:58:05,200 --> 00:58:09,559 autocomplete well if you type in the 1618 00:58:07,039 --> 00:58:11,599 letter H you'll see only presumably 1619 00:58:09,559 --> 00:58:14,240 Hagrid Harry Hermione and so forth if 1620 00:58:11,599 --> 00:58:16,079 you type in ha that shows you only 1621 00:58:14,240 --> 00:58:18,960 Hagrid and Harry and it all happens 1622 00:58:16,079 --> 00:58:21,480 super fast so how is that happening well 1623 00:58:18,960 --> 00:58:24,039 typically you could just start at the 1624 00:58:21,480 --> 00:58:26,280 top and look to the bottom searching for 1625 00:58:24,039 --> 00:58:27,880 all the hes or all of the ha but for 1626 00:58:26,280 --> 00:58:29,640 larger data sets that's going to get 1627 00:58:27,880 --> 00:58:31,760 slow for the Googles of the world that's 1628 00:58:29,640 --> 00:58:33,680 going to get really slow and even on our 1629 00:58:31,760 --> 00:58:36,119 phones when you have hundreds thousands 1630 00:58:33,680 --> 00:58:38,119 of contacts eventually even that kind of 1631 00:58:36,119 --> 00:58:40,039 approach that algorithm step by step but 1632 00:58:38,119 --> 00:58:41,680 it might be slow so how might we go 1633 00:58:40,039 --> 00:58:44,559 about searching for someone in a phone 1634 00:58:41,680 --> 00:58:46,400 book like this uh like say uh John 1635 00:58:44,559 --> 00:58:48,839 Harvard well here's an old school 1636 00:58:46,400 --> 00:58:50,039 incarnation of this and uh odds are you 1637 00:58:48,839 --> 00:58:51,960 might not have had occasion to even 1638 00:58:50,039 --> 00:58:53,760 physically use this thing nowadays and 1639 00:58:51,960 --> 00:58:55,400 in fact this is a bit of a white lie cuz 1640 00:58:53,760 --> 00:58:57,680 this is the Yellow Pages which means 1641 00:58:55,400 --> 00:58:59,599 this is a book of companies not people 1642 00:58:57,680 --> 00:59:01,559 uh but for this is all you can find and 1643 00:58:59,599 --> 00:59:03,720 at that it's even hard to find this but 1644 00:59:01,559 --> 00:59:05,599 this is the same thing in analog form 1645 00:59:03,720 --> 00:59:07,440 physical form so if I wanted to search 1646 00:59:05,599 --> 00:59:10,039 for someone like John Harvard how could 1647 00:59:07,440 --> 00:59:12,520 I do that well I could start on page one 1648 00:59:10,039 --> 00:59:16,799 and I could start searching for page two 1649 00:59:12,520 --> 00:59:18,160 page three page four page five little 1650 00:59:16,799 --> 00:59:19,440 hard to do physically especially since 1651 00:59:18,160 --> 00:59:22,319 no one's used this phone book in a lot 1652 00:59:19,440 --> 00:59:26,000 of years but uh is this algorithm 1653 00:59:22,319 --> 00:59:28,079 correct Turning Page by Page 1654 00:59:26,000 --> 00:59:30,960 very inelegantly is this correct will I 1655 00:59:28,079 --> 00:59:32,440 find John Harvard if if he's in here all 1656 00:59:30,960 --> 00:59:34,280 right so yes I mean this is a little 1657 00:59:32,440 --> 00:59:35,760 stupidly tedious because if there's like 1658 00:59:34,280 --> 00:59:37,960 a thousand Pages he might be a few 1659 00:59:35,760 --> 00:59:39,520 hundred pages into this but it's correct 1660 00:59:37,960 --> 00:59:41,160 at some point I will find him and if 1661 00:59:39,520 --> 00:59:42,760 he's on the page I'll be able to call 1662 00:59:41,160 --> 00:59:44,319 why because presumably the names are 1663 00:59:42,760 --> 00:59:46,039 alphabetized in here and there's no like 1664 00:59:44,319 --> 00:59:47,559 cheat sheet on the edge so I have to 1665 00:59:46,039 --> 00:59:48,799 search for John Harvard from left to 1666 00:59:47,559 --> 00:59:50,480 right searching for H if it's 1667 00:59:48,799 --> 00:59:51,880 alphabetized by last name well what 1668 00:59:50,480 --> 00:59:53,520 would be marginally better well how 1669 00:59:51,880 --> 00:59:55,079 about two pages at a time it's hard to 1670 00:59:53,520 --> 00:59:57,280 do with a 20-year-old old phone book 1671 00:59:55,079 --> 01:00:02,200 where the pages are kind of uh grown 1672 00:59:57,280 --> 01:00:04,880 together but 2 4 6 8 10 12 this 1673 01:00:02,200 --> 01:00:06,319 algorithm is this correct all right so 1674 01:00:04,880 --> 01:00:08,559 no 1675 01:00:06,319 --> 01:00:11,359 why yeah so I'm skipping every other 1676 01:00:08,559 --> 01:00:13,960 page so if I don't consider that and I 1677 01:00:11,359 --> 01:00:16,599 find myself in like the I section or the 1678 01:00:13,960 --> 01:00:18,280 J section well I might accidentally 1679 01:00:16,599 --> 01:00:20,520 conclude nope I haven't found John 1680 01:00:18,280 --> 01:00:21,720 Harvard yet just because I skipped them 1681 01:00:20,520 --> 01:00:24,160 because it was sandwiched between two 1682 01:00:21,720 --> 01:00:26,160 pages now I can fix this I think if I do 1683 01:00:24,160 --> 01:00:28,079 hit the I section well let me just 1684 01:00:26,160 --> 01:00:30,000 double back one page just in case he was 1685 01:00:28,079 --> 01:00:32,359 in that last page so it's recoverable 1686 01:00:30,000 --> 01:00:34,039 but it's almost twice as fast minus that 1687 01:00:32,359 --> 01:00:35,599 that hiccup there but what most of us 1688 01:00:34,039 --> 01:00:37,599 would do and what your phones are doing 1689 01:00:35,599 --> 01:00:39,200 albeit digitally is they open up roughly 1690 01:00:37,599 --> 01:00:41,559 to the middle of the phone book and they 1691 01:00:39,200 --> 01:00:43,440 look down and they say oh I'm in roughly 1692 01:00:41,559 --> 01:00:45,119 the M section so I'm roughly halfway 1693 01:00:43,440 --> 01:00:46,640 through this thousand page phone book 1694 01:00:45,119 --> 01:00:50,000 but what do I now know about John 1695 01:00:46,640 --> 01:00:52,440 Harvard where is he to my left or to my 1696 01:00:50,000 --> 01:00:54,520 right all right so alphabetically he's 1697 01:00:52,440 --> 01:00:57,440 presumably to my left and so here I get 1698 01:00:54,520 --> 01:01:00,000 can both uh met uh metaphorically and 1699 01:00:57,440 --> 01:01:02,359 physically tear the problem in 1700 01:01:00,000 --> 01:01:04,240 half you don't need to be impressed it's 1701 01:01:02,359 --> 01:01:06,599 really easy down the the spine that way 1702 01:01:04,240 --> 01:01:09,119 but uh I know that John Harvard is to 1703 01:01:06,599 --> 01:01:11,720 the left here but now I can throw 1704 01:01:09,119 --> 01:01:14,440 unnecessarily dramatically half and page 1705 01:01:11,720 --> 01:01:16,000 one out of the way and what do I now 1706 01:01:14,440 --> 01:01:17,839 know I've gone from a thousand pages to 1707 01:01:16,000 --> 01:01:19,319 like 500 I can kind of repeat roughly 1708 01:01:17,839 --> 01:01:20,799 the same algorithm go to the half of 1709 01:01:19,319 --> 01:01:23,319 this and so this time I went back a 1710 01:01:20,799 --> 01:01:25,200 little too far I'm in now the um e 1711 01:01:23,319 --> 01:01:27,359 section so what do I know is John 1712 01:01:25,200 --> 01:01:29,799 Harvard to my left or to my 1713 01:01:27,359 --> 01:01:32,319 right to my right so I can again tear 1714 01:01:29,799 --> 01:01:34,559 the problem in half throw this half away 1715 01:01:32,319 --> 01:01:36,520 and now I'm really flying I'm toing it 1716 01:01:34,559 --> 01:01:39,680 verbally slowly but that went from a 1717 01:01:36,520 --> 01:01:42,440 th000 pages to 500 to now 250 and now I 1718 01:01:39,680 --> 01:01:45,200 can do it again 125 i' do it again 1719 01:01:42,440 --> 01:01:46,920 roughly like 67 and keep doing it again 1720 01:01:45,200 --> 01:01:50,119 and again and again until I get left 1721 01:01:46,920 --> 01:01:52,400 with hopefully just one single page or 1722 01:01:50,119 --> 01:01:55,079 in this case an ad for ironically a 1723 01:01:52,400 --> 01:01:56,720 mechanic okay so 1724 01:01:55,079 --> 01:01:58,760 what is the implication for our 1725 01:01:56,720 --> 01:02:00,920 performance well let's just do this sort 1726 01:01:58,760 --> 01:02:03,240 of in the abstract if you will if that 1727 01:02:00,920 --> 01:02:05,200 first algorithm were to be plotted just 1728 01:02:03,240 --> 01:02:07,559 quickly on a chart without even numbers 1729 01:02:05,200 --> 01:02:09,480 here's my x-axis size of problem on the 1730 01:02:07,559 --> 01:02:11,680 x-axis so the bigger the problem the 1731 01:02:09,480 --> 01:02:13,559 farther out that way time to solve the 1732 01:02:11,680 --> 01:02:15,839 problem the tire you go up on the y- 1733 01:02:13,559 --> 01:02:17,839 axis the uh more time you're taking to 1734 01:02:15,839 --> 01:02:19,839 solve it how would we draw the running 1735 01:02:17,839 --> 01:02:21,079 time The amount of time taken to run 1736 01:02:19,839 --> 01:02:23,160 that first algorithm well it's going to 1737 01:02:21,079 --> 01:02:24,760 be a straight line why cuz if you add 1738 01:02:23,160 --> 01:02:26,440 one more page next year because more 1739 01:02:24,760 --> 01:02:28,680 people move to Cambridge you're going to 1740 01:02:26,440 --> 01:02:30,880 add one more page turn potentially so 1741 01:02:28,680 --> 01:02:32,279 one more second one more unit of time so 1742 01:02:30,880 --> 01:02:34,640 it's a straight line and we'll abstract 1743 01:02:32,279 --> 01:02:36,559 it away as n if there's n pages in the 1744 01:02:34,640 --> 01:02:38,720 phone book the slope of this line is 1745 01:02:36,559 --> 01:02:41,160 essentially n the second algorithm 1746 01:02:38,720 --> 01:02:43,119 wherein I was doing two pages at a time 1747 01:02:41,160 --> 01:02:44,559 was twice as fast but it's still a 1748 01:02:43,119 --> 01:02:46,680 straight line and in fact let me just 1749 01:02:44,559 --> 01:02:49,200 draw some dotted lines here if the phone 1750 01:02:46,680 --> 01:02:50,920 book is this big with my first algorithm 1751 01:02:49,200 --> 01:02:53,119 it might take this many step this many 1752 01:02:50,920 --> 01:02:54,839 units of time this many steps this many 1753 01:02:53,119 --> 01:02:57,079 page turns but with that that second 1754 01:02:54,839 --> 01:02:59,240 algorithm notice that the intersection 1755 01:02:57,079 --> 01:03:01,760 is with much lower on the yellow line 1756 01:02:59,240 --> 01:03:03,559 than on the red so n/2 means there's 1757 01:03:01,760 --> 01:03:05,720 half as many pages here if n is the 1758 01:03:03,559 --> 01:03:07,839 number of pages so indeed that algorithm 1759 01:03:05,720 --> 01:03:09,440 the second one is twice as fast minus 1760 01:03:07,839 --> 01:03:11,599 the little hiccup that I have to double 1761 01:03:09,440 --> 01:03:14,039 back one page but that's not a big deal 1762 01:03:11,599 --> 01:03:15,279 if I'm still doing things twice as fast 1763 01:03:14,039 --> 01:03:16,839 but the third algorithm looks 1764 01:03:15,279 --> 01:03:19,000 fundamentally different it looks like 1765 01:03:16,839 --> 01:03:20,799 this logarithms if you recall from high 1766 01:03:19,000 --> 01:03:23,319 school or prior if you don't that's fine 1767 01:03:20,799 --> 01:03:25,319 too it's just a fundamentally different 1768 01:03:23,319 --> 01:03:26,960 function a different shape and notice 1769 01:03:25,319 --> 01:03:29,799 that the green line is going up and up 1770 01:03:26,960 --> 01:03:32,160 and up but a much slower rate of 1771 01:03:29,799 --> 01:03:33,960 increase which means crazy things are 1772 01:03:32,160 --> 01:03:35,559 possible if two towns in Massachusetts 1773 01:03:33,960 --> 01:03:38,400 like Cambridge and Austin across the 1774 01:03:35,559 --> 01:03:39,839 river merge next year for instance in 1775 01:03:38,400 --> 01:03:42,559 terms of their phone book their phone 1776 01:03:39,839 --> 01:03:44,039 book just got twice as big for the first 1777 01:03:42,559 --> 01:03:46,039 algorithm that's going to take me twice 1778 01:03:44,039 --> 01:03:48,000 as many steps to go through the second 1779 01:03:46,039 --> 01:03:50,200 algorithm almost Twi it's going to take 1780 01:03:48,000 --> 01:03:52,160 me 50% more steps to go through two at a 1781 01:03:50,200 --> 01:03:53,640 time but the third algorithm that I 1782 01:03:52,160 --> 01:03:55,720 ended with tearing things again and 1783 01:03:53,640 --> 01:03:57,599 again divid and conquering if you will 1784 01:03:55,720 --> 01:04:00,119 in half and in half and in half how many 1785 01:03:57,599 --> 01:04:01,960 more steps will my third algorithm take 1786 01:04:00,119 --> 01:04:03,920 if Cambridge and Austin merge into a 1787 01:04:01,960 --> 01:04:06,920 phone book that's twice as 1788 01:04:03,920 --> 01:04:08,480 big just one more step right no big deal 1789 01:04:06,920 --> 01:04:09,880 you just take a really big bite out of 1790 01:04:08,480 --> 01:04:11,920 the problem once you decide if John 1791 01:04:09,880 --> 01:04:14,520 Harvard is to the left or to the right 1792 01:04:11,920 --> 01:04:16,559 and so you've made much faster progress 1793 01:04:14,520 --> 01:04:18,720 and so this in essence is what your 1794 01:04:16,559 --> 01:04:20,520 computer your phone is probably doing 1795 01:04:18,720 --> 01:04:22,720 underneath the hood when searching for 1796 01:04:20,520 --> 01:04:24,920 Harry or Hermione or Hagrid or anyone 1797 01:04:22,720 --> 01:04:26,559 else because it's that much faster 1798 01:04:24,920 --> 01:04:28,119 especially when you have large data if 1799 01:04:26,559 --> 01:04:29,440 you don't have that many contacts 1800 01:04:28,119 --> 01:04:32,279 probably doesn't matter if you search 1801 01:04:29,440 --> 01:04:33,680 from top to bottom or more uh more in 1802 01:04:32,279 --> 01:04:35,400 the form of this divide and conquer 1803 01:04:33,680 --> 01:04:37,079 algorithm but if you're the Googles of 1804 01:04:35,400 --> 01:04:38,880 the world or you're analyzing large data 1805 01:04:37,079 --> 01:04:41,000 sets indeed this is going to add up 1806 01:04:38,880 --> 01:04:42,799 quite quickly so where do we go with 1807 01:04:41,000 --> 01:04:44,359 this well we're going to introduce next 1808 01:04:42,799 --> 01:04:46,720 something called pseudo code how can I 1809 01:04:44,359 --> 01:04:49,000 translate what I did verbally there sort 1810 01:04:46,720 --> 01:04:50,599 of intuitively to actual code well this 1811 01:04:49,000 --> 01:04:52,440 won't be scratch this won't be C or 1812 01:04:50,599 --> 01:04:54,440 python just yet it's just going to be an 1813 01:04:52,440 --> 01:04:55,960 english-like syntax and this is how many 1814 01:04:54,440 --> 01:04:57,920 programmers would start solving a 1815 01:04:55,960 --> 01:05:00,079 problem they don't start typing out code 1816 01:04:57,920 --> 01:05:01,599 in C or python or the like they use 1817 01:05:00,079 --> 01:05:03,440 English or whatever their human language 1818 01:05:01,599 --> 01:05:05,640 is to jot down an outline for their 1819 01:05:03,440 --> 01:05:07,799 ideas my first step really was picking 1820 01:05:05,640 --> 01:05:09,760 up the phone book my second step was 1821 01:05:07,799 --> 01:05:12,160 opening to the middle of the phone book 1822 01:05:09,760 --> 01:05:13,960 my third step was somewhat different 1823 01:05:12,160 --> 01:05:16,200 look at the page because why my fourth 1824 01:05:13,960 --> 01:05:19,920 step was if person I'm looking for is on 1825 01:05:16,200 --> 01:05:21,880 the page I then do what never happened 1826 01:05:19,920 --> 01:05:25,079 in my example but I call the person so 1827 01:05:21,880 --> 01:05:27,119 I'm done else if the person is earlier 1828 01:05:25,079 --> 01:05:29,760 in the book alphabetically as John 1829 01:05:27,119 --> 01:05:32,359 Harvard was in the case of my H then I 1830 01:05:29,760 --> 01:05:34,760 should search to the middle of the left 1831 01:05:32,359 --> 01:05:37,680 of the phone book and then I should go 1832 01:05:34,760 --> 01:05:39,359 back to step three step three is look at 1833 01:05:37,680 --> 01:05:41,559 the page thereby repeating the same 1834 01:05:39,359 --> 01:05:43,400 process again and again step nine though 1835 01:05:41,559 --> 01:05:45,119 might be else if the person is later in 1836 01:05:43,400 --> 01:05:47,000 the book then let's go ahead and open to 1837 01:05:45,119 --> 01:05:50,359 the middle of the right half of the book 1838 01:05:47,000 --> 01:05:52,079 and then go back to line three else 1839 01:05:50,359 --> 01:05:54,839 there's a fourth scenario we should 1840 01:05:52,079 --> 01:05:56,799 probably consider lest my search process 1841 01:05:54,839 --> 01:05:59,680 freeze or crash or give me one of those 1842 01:05:56,799 --> 01:06:01,960 spinning beach balls with a bug 1843 01:05:59,680 --> 01:06:03,200 yeah yeah what if John Harvard isn't in 1844 01:06:01,960 --> 01:06:04,920 the phone book I'd prefer that my 1845 01:06:03,200 --> 01:06:06,440 algorithm my phone not just reboot or 1846 01:06:04,920 --> 01:06:09,200 freeze I should handle that with some 1847 01:06:06,440 --> 01:06:10,720 kind of catchall else so to speak let's 1848 01:06:09,200 --> 01:06:13,880 just quit the program so there's 1849 01:06:10,720 --> 01:06:16,039 welldefined behavior for every possible 1850 01:06:13,880 --> 01:06:17,599 scenario of the four now let's call out 1851 01:06:16,039 --> 01:06:19,400 a few of these Salient terms it turns 1852 01:06:17,599 --> 01:06:20,960 out if I highlight in yellow here 1853 01:06:19,400 --> 01:06:22,480 there's a pattern to what I've been 1854 01:06:20,960 --> 01:06:24,160 doing here these are all of my English 1855 01:06:22,480 --> 01:06:26,440 verbs and we're in a moment we're going 1856 01:06:24,160 --> 01:06:28,799 to start calling those verbs functions 1857 01:06:26,440 --> 01:06:30,720 when you program or write code and you 1858 01:06:28,799 --> 01:06:32,880 want the program or the computer to do 1859 01:06:30,720 --> 01:06:34,559 something for you some action or verb 1860 01:06:32,880 --> 01:06:36,400 we're going to refer to those actions or 1861 01:06:34,559 --> 01:06:38,680 verbs as these things called functions 1862 01:06:36,400 --> 01:06:40,799 like those here by contrast I've just 1863 01:06:38,680 --> 01:06:42,839 highlighted instead my if my El if my 1864 01:06:40,799 --> 01:06:44,160 Els if and Els this is going to 1865 01:06:42,839 --> 01:06:46,640 represent what we're going to start 1866 01:06:44,160 --> 01:06:48,279 calling a conditional a proverbial fork 1867 01:06:46,640 --> 01:06:50,160 in the road where you can either go this 1868 01:06:48,279 --> 01:06:52,359 way or that way do this thing or this 1869 01:06:50,160 --> 01:06:54,559 other thing and you're going to decide 1870 01:06:52,359 --> 01:06:56,440 which of those things to do based on 1871 01:06:54,559 --> 01:06:59,240 what I've now highlighted here which are 1872 01:06:56,440 --> 01:07:01,359 going to be called Boolean Expressions 1873 01:06:59,240 --> 01:07:03,720 bull referring to a mathematician last 1874 01:07:01,359 --> 01:07:06,920 named bull a Boolean expression is just 1875 01:07:03,720 --> 01:07:09,079 a question with a yes no a true false a 1876 01:07:06,920 --> 01:07:10,920 one or a zero answer if you will and it 1877 01:07:09,079 --> 01:07:12,880 governs whether you do this thing or 1878 01:07:10,920 --> 01:07:15,359 this thing or this thing or that the 1879 01:07:12,880 --> 01:07:17,559 indentation in this case is important 1880 01:07:15,359 --> 01:07:20,000 the fact that I've indented line five 1881 01:07:17,559 --> 01:07:21,839 implies by convention in programming 1882 01:07:20,000 --> 01:07:24,799 that I should only do line five if the 1883 01:07:21,839 --> 01:07:26,720 answer to line four is a yes or true and 1884 01:07:24,799 --> 01:07:29,319 same for these other indented lines as 1885 01:07:26,720 --> 01:07:31,359 well and the last characteristic here is 1886 01:07:29,319 --> 01:07:33,559 this here uh someone called this out 1887 01:07:31,359 --> 01:07:35,799 earlier in fact these lines eight and 11 1888 01:07:33,559 --> 01:07:38,720 are now highlighted and represent 1889 01:07:35,799 --> 01:07:41,039 what what might we call these in code if 1890 01:07:38,720 --> 01:07:43,200 you've done that yeah so these are Loops 1891 01:07:41,039 --> 01:07:45,960 some kind of cycles that result in my 1892 01:07:43,200 --> 01:07:48,119 doing the same thing again and again but 1893 01:07:45,960 --> 01:07:50,039 there's a key detail with this algorithm 1894 01:07:48,119 --> 01:07:53,000 in pseudo code even though it's telling 1895 01:07:50,039 --> 01:07:56,319 me to go back to line three why is this 1896 01:07:53,000 --> 01:07:58,119 algorithm event going to stop why do I 1897 01:07:56,319 --> 01:08:00,440 not constantly keep looking for John 1898 01:07:58,119 --> 01:08:03,079 Harvard Forever by nature of these Loops 1899 01:08:00,440 --> 01:08:05,559 telling me to keep going back to line 1900 01:08:03,079 --> 01:08:08,440 three good eventually he'll be on the 1901 01:08:05,559 --> 01:08:10,960 page or or to your point earlier he 1902 01:08:08,440 --> 01:08:12,960 won't be at all and we're out of pages 1903 01:08:10,960 --> 01:08:14,400 and so we just quit and that's the key 1904 01:08:12,960 --> 01:08:16,000 about going to the left half or the 1905 01:08:14,400 --> 01:08:17,279 right half it doesn't matter if you do 1906 01:08:16,000 --> 01:08:18,799 the same thing again and again you're 1907 01:08:17,279 --> 01:08:20,880 not going to get stuck in a so-called 1908 01:08:18,799 --> 01:08:23,159 infinite Loop so long as you keep 1909 01:08:20,880 --> 01:08:24,799 dividing the problem and shrinking it 1910 01:08:23,159 --> 01:08:26,120 into something small smaller smaller 1911 01:08:24,799 --> 01:08:28,159 eventually there's going to be no 1912 01:08:26,120 --> 01:08:29,640 problem left to solve so even if you 1913 01:08:28,159 --> 01:08:31,520 don't think of yourself as a computer 1914 01:08:29,640 --> 01:08:33,199 person even if you've never written code 1915 01:08:31,520 --> 01:08:35,400 what you'll find in the coming days is 1916 01:08:33,199 --> 01:08:37,759 that these ideas that we've just kind of 1917 01:08:35,400 --> 01:08:39,679 harnessed from real life are at your 1918 01:08:37,759 --> 01:08:41,319 fingertips already and a lot of the 1919 01:08:39,679 --> 01:08:43,359 process of learning to code is yes it's 1920 01:08:41,319 --> 01:08:45,080 going to be a bumpy Ro uh some bumps in 1921 01:08:43,359 --> 01:08:47,120 the road because you can't quite see the 1922 01:08:45,080 --> 01:08:49,120 new syntax in a familiar way but you'll 1923 01:08:47,120 --> 01:08:50,359 find that the ideas in fact are going to 1924 01:08:49,120 --> 01:08:52,400 be more familiar than you might 1925 01:08:50,359 --> 01:08:54,319 otherwise think and so we'll see in a 1926 01:08:52,400 --> 01:08:56,279 bit and we'll take a break in a moment 1927 01:08:54,319 --> 01:08:58,080 uh to take a breather that you will see 1928 01:08:56,279 --> 01:09:00,199 these same ideas in a moment in the 1929 01:08:58,080 --> 01:09:01,759 context of scratch an actual programming 1930 01:09:00,199 --> 01:09:03,799 language via which we drag and drop 1931 01:09:01,759 --> 01:09:05,319 puzzle pieces to make actual code work 1932 01:09:03,799 --> 01:09:06,600 we'll see some variant of these ideas 1933 01:09:05,319 --> 01:09:08,480 things called arguments and return 1934 01:09:06,600 --> 01:09:11,679 values and variables but we'll 1935 01:09:08,480 --> 01:09:14,319 ultimately convert it into this somehow 1936 01:09:11,679 --> 01:09:16,799 anyone want to wager what this program 1937 01:09:14,319 --> 01:09:19,239 will do if fed to your Mac or PC or 1938 01:09:16,799 --> 01:09:21,120 phone here's just a massive pattern of 1939 01:09:19,239 --> 01:09:23,400 zeros and 1940 01:09:21,120 --> 01:09:25,640 ones it will indeed say rather 1941 01:09:23,400 --> 01:09:28,880 disappoint ly apparently just hello 1942 01:09:25,640 --> 01:09:32,319 world and indeed baked into all of these 1943 01:09:28,880 --> 01:09:34,799 zeros and ones are not just the h l l o 1944 01:09:32,319 --> 01:09:36,719 but also the verbs the action of 1945 01:09:34,799 --> 01:09:38,640 printing something to the screen and 1946 01:09:36,719 --> 01:09:40,120 there's other stuff too so that the 1947 01:09:38,640 --> 01:09:41,400 program knows how to start and how to 1948 01:09:40,120 --> 01:09:43,080 stop a lot of stuff that we won't have 1949 01:09:41,400 --> 01:09:44,960 to worry about that whoever designed the 1950 01:09:43,080 --> 01:09:46,920 computer or the language did but at the 1951 01:09:44,960 --> 01:09:48,920 end of the day you're never going to be 1952 01:09:46,920 --> 01:09:50,839 writing these zeros and ones yourselves 1953 01:09:48,920 --> 01:09:53,239 though our ancestors Once Upon a Time 1954 01:09:50,839 --> 01:09:56,280 did in some form we'll be using a much 1955 01:09:53,239 --> 01:09:58,679 higher l level language like this in C 1956 01:09:56,280 --> 01:10:00,960 or better yet in just a moment like in 1957 01:09:58,679 --> 01:10:03,520 scratch like this and indeed this is why 1958 01:10:00,960 --> 01:10:05,600 today we focus with focus on and begin 1959 01:10:03,520 --> 01:10:07,280 with scratch this graphical programming 1960 01:10:05,600 --> 01:10:09,280 language so we have a way of expressing 1961 01:10:07,280 --> 01:10:11,960 ourselves with functions conditionals 1962 01:10:09,280 --> 01:10:13,800 loops and more but in a way that doesn't 1963 01:10:11,960 --> 01:10:15,400 have stupid parentheses and curly braces 1964 01:10:13,800 --> 01:10:17,199 and all these visual distractions in the 1965 01:10:15,400 --> 01:10:19,280 way and we'll translate that thereafter 1966 01:10:17,199 --> 01:10:20,679 to this lower level language but for now 1967 01:10:19,280 --> 01:10:21,880 that was a lot that was definitely a 1968 01:10:20,679 --> 01:10:23,719 fire hose let's go ahead and take a 1969 01:10:21,880 --> 01:10:25,480 10-minute break feel free to get up or 1970 01:10:23,719 --> 01:10:27,040 stay here and we'll resume in a bit with 1971 01:10:25,480 --> 01:10:30,040 some actual 1972 01:10:27,040 --> 01:10:31,679 code uh so this then is scratch a 1973 01:10:30,040 --> 01:10:33,480 graphical programming language from a 1974 01:10:31,679 --> 01:10:34,800 friends down the road at mit's media lab 1975 01:10:33,480 --> 01:10:37,040 that indeed some of you might have used 1976 01:10:34,800 --> 01:10:38,840 in grade score the like for playing and 1977 01:10:37,040 --> 01:10:41,199 writing code and the like but you maybe 1978 01:10:38,840 --> 01:10:42,960 didn't necessarily think about how some 1979 01:10:41,199 --> 01:10:44,600 of these Primitives ultimately worked 1980 01:10:42,960 --> 01:10:46,120 and in fact everything you've done if 1981 01:10:44,600 --> 01:10:47,880 you've used scratch before and 1982 01:10:46,120 --> 01:10:50,080 everything you'll see today is going to 1983 01:10:47,880 --> 01:10:51,360 apply to all of the weeks to come as we 1984 01:10:50,080 --> 01:10:52,719 explore these things called functions 1985 01:10:51,360 --> 01:10:55,120 and loops and conditionals Boolean 1986 01:10:52,719 --> 01:10:56,920 expressions and more with scratch 1987 01:10:55,120 --> 01:10:58,760 because it's so graphical and animated 1988 01:10:56,920 --> 01:11:00,840 can create can you create animations 1989 01:10:58,760 --> 01:11:03,239 like this one interactive art and 1990 01:11:00,840 --> 01:11:05,239 software more generally but you'll do so 1991 01:11:03,239 --> 01:11:07,040 by dragging and dropping puzzle pieces 1992 01:11:05,239 --> 01:11:08,640 that only lock together if it makes 1993 01:11:07,040 --> 01:11:10,280 logical sense to do so and what you 1994 01:11:08,640 --> 01:11:12,679 won't have to deal with in this first 1995 01:11:10,280 --> 01:11:14,719 week of class is Curly braces 1996 01:11:12,679 --> 01:11:16,239 parentheses all of the weird symbology 1997 01:11:14,719 --> 01:11:18,320 that you might recall seeing when we 1998 01:11:16,239 --> 01:11:20,440 just wanted to say hello world now this 1999 01:11:18,320 --> 01:11:22,960 particular program um riging men was 2000 01:11:20,440 --> 01:11:24,560 written by a former cs50 teaching fellow 2001 01:11:22,960 --> 01:11:26,640 Andrew bar who's actually now the 2002 01:11:24,560 --> 01:11:28,679 general manager of the Cleveland Browns 2003 01:11:26,640 --> 01:11:30,440 the American football team and so these 2004 01:11:28,679 --> 01:11:32,320 are just some of the programs that some 2005 01:11:30,440 --> 01:11:34,440 of your predecessors in the class have 2006 01:11:32,320 --> 01:11:36,360 created and you'll see in the remainder 2007 01:11:34,440 --> 01:11:37,679 of class here a couple of others as well 2008 01:11:36,360 --> 01:11:40,800 and more in the course's first 2009 01:11:37,679 --> 01:11:42,760 assignment namely problem set zero so 2010 01:11:40,800 --> 01:11:44,560 how do we get there well first a quick 2011 01:11:42,760 --> 01:11:47,040 tour of what it is we're going to do 2012 01:11:44,560 --> 01:11:48,639 this in scratch is perhaps the simplest 2013 01:11:47,040 --> 01:11:50,360 program you can write and even if you've 2014 01:11:48,639 --> 01:11:52,280 never seen scratch or any programming 2015 01:11:50,360 --> 01:11:54,440 language before can probably guess that 2016 01:11:52,280 --> 01:11:56,320 this just says on the screen somehow 2017 01:11:54,440 --> 01:11:58,360 hello world but what you don't have to 2018 01:11:56,320 --> 01:11:59,760 do is type esoteric commands and weird 2019 01:11:58,360 --> 01:12:01,679 syntax those curly braces and 2020 01:11:59,760 --> 01:12:03,800 parentheses I keep alluding to you just 2021 01:12:01,679 --> 01:12:05,239 drag this yellow puzzle piece you drag 2022 01:12:03,800 --> 01:12:07,159 this purple puzzle piece let them 2023 01:12:05,239 --> 01:12:09,840 magnetically lock together so to speak 2024 01:12:07,159 --> 01:12:11,440 click a button and boom with those same 2025 01:12:09,840 --> 01:12:12,960 building blocks and several others can 2026 01:12:11,440 --> 01:12:14,960 you make what exactly the sorts of 2027 01:12:12,960 --> 01:12:17,199 things that Andrew brought to life as 2028 01:12:14,960 --> 01:12:20,800 well so here's what we're about to see 2029 01:12:17,199 --> 01:12:22,840 at scratch.mit.edu is a cloud-based 2030 01:12:20,800 --> 01:12:24,639 programming environment on MIT servers 2031 01:12:22,840 --> 01:12:26,520 you can also download it offline on your 2032 01:12:24,639 --> 01:12:29,000 own Mac or PC and it gives you an 2033 01:12:26,520 --> 01:12:31,560 interface like this on the left hand 2034 01:12:29,000 --> 01:12:34,199 side of the screen you'll see a blocks 2035 01:12:31,560 --> 01:12:36,000 pette these puzzle pieces AKA blocks 2036 01:12:34,199 --> 01:12:37,800 come in different colors which rather 2037 01:12:36,000 --> 01:12:39,760 categorize them so pictured here for 2038 01:12:37,800 --> 01:12:42,120 instance in blue are a whole bunch of 2039 01:12:39,760 --> 01:12:43,800 motion related blocks so Andrew used a 2040 01:12:42,120 --> 01:12:46,120 whole bunch of those to have the singer 2041 01:12:43,800 --> 01:12:48,120 and the men moving around on the screen 2042 01:12:46,120 --> 01:12:50,000 um in synchronicity with the song that 2043 01:12:48,120 --> 01:12:52,360 was playing in the background meanwhile 2044 01:12:50,000 --> 01:12:54,360 in the middle of this interface is going 2045 01:12:52,360 --> 01:12:56,520 to be the code area and this is where 2046 01:12:54,360 --> 01:12:58,199 Andrew and sunu will drag and drop some 2047 01:12:56,520 --> 01:13:00,520 of those puzzle pieces and other colors 2048 01:12:58,199 --> 01:13:03,120 as well and lock them together to get 2049 01:13:00,520 --> 01:13:05,320 your character soon to be invented to do 2050 01:13:03,120 --> 01:13:06,840 something on the screen indeed at the 2051 01:13:05,320 --> 01:13:09,760 bottom right here will you see 2052 01:13:06,840 --> 01:13:11,440 ultimately a Sprite area where a Sprite 2053 01:13:09,760 --> 01:13:13,000 is a technical term for like a character 2054 01:13:11,440 --> 01:13:14,920 in a video game or a programming 2055 01:13:13,000 --> 01:13:18,120 environment like this by default 2056 01:13:14,920 --> 01:13:19,719 historically scratch uh is the cat the 2057 01:13:18,120 --> 01:13:21,239 mascot if you will for this programming 2058 01:13:19,719 --> 01:13:23,560 environment and so here we see by 2059 01:13:21,239 --> 01:13:25,239 default just one Sprite selected because 2060 01:13:23,560 --> 01:13:27,560 on the top right of the screen is the 2061 01:13:25,239 --> 01:13:29,159 stage for that Sprite and you can click 2062 01:13:27,560 --> 01:13:31,120 in Click and zoom in to make it full 2063 01:13:29,159 --> 01:13:33,679 screen but this is the world in which 2064 01:13:31,120 --> 01:13:35,639 Scratch by default the cat will live but 2065 01:13:33,679 --> 01:13:37,639 you can change scratch's costume so that 2066 01:13:35,639 --> 01:13:39,639 it looks like a singer or a man falling 2067 01:13:37,639 --> 01:13:41,239 from the sky or the like or anything 2068 01:13:39,639 --> 01:13:42,600 else either creating the art yourself or 2069 01:13:41,239 --> 01:13:45,560 importing some of the things that come 2070 01:13:42,600 --> 01:13:48,080 with it or elsewhere online so what is 2071 01:13:45,560 --> 01:13:49,679 this world that scratch rather lives in 2072 01:13:48,080 --> 01:13:51,239 well generally speaking we won't have to 2073 01:13:49,679 --> 01:13:53,000 care too much about numbers because 2074 01:13:51,239 --> 01:13:55,440 we'll be able to ask questions like 2075 01:13:53,000 --> 01:13:57,840 intera active ones like is scratch the 2076 01:13:55,440 --> 01:13:59,199 cat or any character otherwise touching 2077 01:13:57,840 --> 01:14:01,960 the edge of the screen touching 2078 01:13:59,199 --> 01:14:02,760 something else but scratch does exist in 2079 01:14:01,960 --> 01:14:04,880 this 2080 01:14:02,760 --> 01:14:07,000 two-dimensional uh coordinate system 2081 01:14:04,880 --> 01:14:08,800 world so when the cat or any character 2082 01:14:07,000 --> 01:14:12,159 is dead center in the middle that would 2083 01:14:08,800 --> 01:14:15,760 be XY location 0 comma 0 if you will 2084 01:14:12,159 --> 01:14:17,600 meanwhile over here is 240 pixels or 2085 01:14:15,760 --> 01:14:20,480 dots all the way to the right so this 2086 01:14:17,600 --> 01:14:22,040 would be 240 comma 0 where Y is z 2087 01:14:20,480 --> 01:14:23,920 because it's right on that midline so 2088 01:14:22,040 --> 01:14:26,040 it's neither up or below 2089 01:14:23,920 --> 01:14:29,960 over here to the left of course would be 2090 01:14:26,040 --> 01:14:32,000 240 and 0 above the cat would be xal 0 2091 01:14:29,960 --> 01:14:34,400 cuz it's right on that vertical midline 2092 01:14:32,000 --> 01:14:36,800 and 180 and then down here as you might 2093 01:14:34,400 --> 01:14:38,199 guess would be 0 comma netive 180 2094 01:14:36,800 --> 01:14:40,159 generally speaking we don't have to care 2095 01:14:38,199 --> 01:14:41,840 about those precise pixel coordinates 2096 01:14:40,159 --> 01:14:43,400 but it's helpful ultimately if you do 2097 01:14:41,840 --> 01:14:45,520 want the cat to move up down left or 2098 01:14:43,400 --> 01:14:47,639 right having some sense of direction 2099 01:14:45,520 --> 01:14:49,600 according to the x- axis and y AIS as 2100 01:14:47,639 --> 01:14:52,040 well can help you express your ideas 2101 01:14:49,600 --> 01:14:54,639 ultimately so what might some of those 2102 01:14:52,040 --> 01:14:57,480 ideas be well let's do this I'm going to 2103 01:14:54,639 --> 01:15:01,520 go ahead and create on 2104 01:14:57,480 --> 01:15:03,360 scratch.mit.edu just an empty screen 2105 01:15:01,520 --> 01:15:05,320 like this one here and so this is the 2106 01:15:03,360 --> 01:15:07,800 exact same interface but now I'm in my 2107 01:15:05,320 --> 01:15:09,239 browser uh full screen so that I can 2108 01:15:07,800 --> 01:15:10,960 start writing some code and let's get 2109 01:15:09,239 --> 01:15:12,600 that cat to say something actually on 2110 01:15:10,960 --> 01:15:14,520 the screen now this takes a little bit 2111 01:15:12,600 --> 01:15:16,400 of practice but honestly just by 2112 01:15:14,520 --> 01:15:18,600 scrolling through these puzzle pieces 2113 01:15:16,400 --> 01:15:20,440 can you quickly get a sense of what's 2114 01:15:18,600 --> 01:15:22,159 possible not just categorically but 2115 01:15:20,440 --> 01:15:23,639 specifically and I'll jump around 2116 01:15:22,159 --> 01:15:25,920 because I've done this of course but I'm 2117 01:15:23,639 --> 01:15:27,520 going to go to events in yellow first 2118 01:15:25,920 --> 01:15:29,560 and I'm going to drag and drop this 2119 01:15:27,520 --> 01:15:31,159 first block called when green flag 2120 01:15:29,560 --> 01:15:33,000 clicked and I've zoomed in there just to 2121 01:15:31,159 --> 01:15:35,239 make it a little more legible and notice 2122 01:15:33,000 --> 01:15:38,120 that the shape of this green flag just 2123 01:15:35,239 --> 01:15:40,760 so happens to mirror this green flag 2124 01:15:38,120 --> 01:15:42,560 here at top next to this red stop sign 2125 01:15:40,760 --> 01:15:44,360 of sorts and the green flag is going to 2126 01:15:42,560 --> 01:15:47,040 mean go and the red stop sign's going to 2127 01:15:44,360 --> 01:15:48,480 mean stop to start or stop our program 2128 01:15:47,040 --> 01:15:49,920 next week you're going to be writing a 2129 01:15:48,480 --> 01:15:51,520 textual command at your keyboard to do 2130 01:15:49,920 --> 01:15:53,600 the exact same idea but for now it's a 2131 01:15:51,520 --> 01:15:55,600 button so when green flag clicked what 2132 01:15:53,600 --> 01:15:57,239 do I want scratch to do well how about 2133 01:15:55,600 --> 01:15:59,080 we have scratch just initially say 2134 01:15:57,239 --> 01:16:01,480 something like Hello World which indeed 2135 01:15:59,080 --> 01:16:03,639 historically is the first program that 2136 01:16:01,480 --> 01:16:05,280 most any programmer might write so 2137 01:16:03,639 --> 01:16:07,280 anything related to what the cat looks 2138 01:16:05,280 --> 01:16:09,320 like it's doing is actually going to be 2139 01:16:07,280 --> 01:16:12,280 under looks here in purple so I'm going 2140 01:16:09,320 --> 01:16:14,000 to drag over say hello and you'll notice 2141 01:16:12,280 --> 01:16:16,960 something curious and different about 2142 01:16:14,000 --> 01:16:18,880 this purple block it says of course say 2143 01:16:16,960 --> 01:16:21,440 in purple but then there's this white 2144 01:16:18,880 --> 01:16:23,199 oval and some text that by default is 2145 01:16:21,440 --> 01:16:25,239 hello cuz MIT just decided that by 2146 01:16:23,199 --> 01:16:27,199 default the placeholder will be hello 2147 01:16:25,239 --> 01:16:30,639 but anytime you see this white oval it's 2148 01:16:27,199 --> 01:16:33,920 an opportunity to provide an input into 2149 01:16:30,639 --> 01:16:35,400 the function called say and so here I'm 2150 01:16:33,920 --> 01:16:36,880 borrowing terminology from before 2151 01:16:35,400 --> 01:16:39,360 problem solving again is all about 2152 01:16:36,880 --> 01:16:41,280 inputs producing outputs and in between 2153 01:16:39,360 --> 01:16:42,239 there is some algorithm in a moment 2154 01:16:41,280 --> 01:16:45,080 we're going to start referring to 2155 01:16:42,239 --> 01:16:46,679 algorithms quite frequently as functions 2156 01:16:45,080 --> 01:16:48,800 why because it's the implementation of 2157 01:16:46,679 --> 01:16:51,480 some algorithm so let me override the 2158 01:16:48,800 --> 01:16:53,880 default with hello comma World I'll zoom 2159 01:16:51,480 --> 01:16:55,600 out and now if I go to the top right of 2160 01:16:53,880 --> 01:16:58,239 the screen and click the green flag 2161 01:16:55,600 --> 01:17:00,760 we'll see hopefully my very first 2162 01:16:58,239 --> 01:17:02,600 program in code now it wasn't a huge 2163 01:17:00,760 --> 01:17:04,239 lift right it only was a matter of 2164 01:17:02,600 --> 01:17:06,440 dragging and dropping puzzle pieces but 2165 01:17:04,239 --> 01:17:08,679 what has now happened well it turns out 2166 01:17:06,440 --> 01:17:11,280 that two things have happened when I the 2167 01:17:08,679 --> 01:17:12,400 human clicked on that green flag I 2168 01:17:11,280 --> 01:17:14,719 triggered what we're going to start 2169 01:17:12,400 --> 01:17:16,440 calling now an event an event is 2170 01:17:14,719 --> 01:17:17,880 generally something graphical or 2171 01:17:16,440 --> 01:17:19,719 interactive that just happens in a 2172 01:17:17,880 --> 01:17:22,000 computer program you and I trigger 2173 01:17:19,719 --> 01:17:24,920 events on our phones all day long 2174 01:17:22,000 --> 01:17:26,920 whenever you tap or drag or long press 2175 01:17:24,920 --> 01:17:28,800 or pinch or any of those gestures in 2176 01:17:26,920 --> 01:17:30,800 Vogue nowadays on phones you are 2177 01:17:28,800 --> 01:17:32,560 triggering events and people at Apple 2178 01:17:30,800 --> 01:17:35,199 and Google and elsewhere have written 2179 01:17:32,560 --> 01:17:37,400 code that listen for those events and do 2180 01:17:35,199 --> 01:17:39,760 something when that event happens that's 2181 01:17:37,400 --> 01:17:41,440 what I just did when green flag is 2182 01:17:39,760 --> 01:17:44,159 clicked I want something to happen 2183 01:17:41,440 --> 01:17:46,760 namely I want this purple function this 2184 01:17:44,159 --> 01:17:48,440 verb this action called say to do 2185 01:17:46,760 --> 01:17:50,120 something what do I want it to do I want 2186 01:17:48,440 --> 01:17:51,679 it to say what this input is and I'm 2187 01:17:50,120 --> 01:17:54,719 going to introduce another vocabulary 2188 01:17:51,679 --> 01:17:56,880 term the white ovals here are yes inputs 2189 01:17:54,719 --> 01:17:59,080 very generically but in the programmer's 2190 01:17:56,880 --> 01:18:00,679 terminology they're called arguments 2191 01:17:59,080 --> 01:18:02,400 otherwise known as parameters and that 2192 01:18:00,679 --> 01:18:04,800 just means an input to a function that 2193 01:18:02,400 --> 01:18:06,800 modifies Its Behavior in some way when I 2194 01:18:04,800 --> 01:18:08,239 click stop that's just another event and 2195 01:18:06,800 --> 01:18:09,560 that one is just built into scratch 2196 01:18:08,239 --> 01:18:11,760 scratch knows that when you click the 2197 01:18:09,560 --> 01:18:13,159 green stop sign uh everything should 2198 01:18:11,760 --> 01:18:15,920 just stop automatically I don't have to 2199 01:18:13,159 --> 01:18:17,840 write code to support that feature so 2200 01:18:15,920 --> 01:18:20,560 that's all fine and good hello world but 2201 01:18:17,840 --> 01:18:22,280 if I keep doing stop and start and stop 2202 01:18:20,560 --> 01:18:24,159 and start it's going to do the same 2203 01:18:22,280 --> 01:18:25,760 thing again and again and it's really 2204 01:18:24,159 --> 01:18:27,480 not that interesting at the end of the 2205 01:18:25,760 --> 01:18:28,840 day maybe gratifying once but it'd be 2206 01:18:27,480 --> 01:18:31,679 nice if this were a little more 2207 01:18:28,840 --> 01:18:33,199 interactive so it turns out that we can 2208 01:18:31,679 --> 01:18:35,920 do that too but we need a different 2209 01:18:33,199 --> 01:18:38,080 mental model instead so in this case 2210 01:18:35,920 --> 01:18:40,280 here when we think about this function 2211 01:18:38,080 --> 01:18:41,880 say in this input hello world this 2212 01:18:40,280 --> 01:18:44,000 actually Maps pretty cleanly to this 2213 01:18:41,880 --> 01:18:46,400 model earlier that I propose is problem 2214 01:18:44,000 --> 01:18:48,920 solving is computer science if you will 2215 01:18:46,400 --> 01:18:51,440 the input to the current problem is 2216 01:18:48,920 --> 01:18:54,880 going to be in white here hello world 2217 01:18:51,440 --> 01:18:56,880 the algorithm is the say algorithm now I 2218 01:18:54,880 --> 01:18:58,239 don't know how MIT got it to print out 2219 01:18:56,880 --> 01:19:00,239 the little pretty speech bubble on the 2220 01:18:58,239 --> 01:19:02,360 screen but they wrote those underlying 2221 01:19:00,239 --> 01:19:04,760 low-level implementation details and 2222 01:19:02,360 --> 01:19:06,960 they gave me and you a purple function 2223 01:19:04,760 --> 01:19:08,440 called say that just does that for you 2224 01:19:06,960 --> 01:19:10,840 you and I don't have to reinvent that 2225 01:19:08,440 --> 01:19:12,840 wheel the output of SE is another 2226 01:19:10,840 --> 01:19:14,880 technical term now called a side effect 2227 01:19:12,840 --> 01:19:16,600 a side effect is usually something 2228 01:19:14,880 --> 01:19:18,639 visual that happens like as a side 2229 01:19:16,600 --> 01:19:20,320 effect of you calling a function and so 2230 01:19:18,639 --> 01:19:22,320 the side effect here is that the cat has 2231 01:19:20,320 --> 01:19:24,560 this speech bubble magically appear 2232 01:19:22,320 --> 01:19:27,080 inside of which is hello world so we 2233 01:19:24,560 --> 01:19:28,920 have an input we have an output we have 2234 01:19:27,080 --> 01:19:30,400 an algorithm but now we're talking about 2235 01:19:28,920 --> 01:19:33,040 these ideas in the context of 2236 01:19:30,400 --> 01:19:35,960 programming so now the input is an 2237 01:19:33,040 --> 01:19:38,280 argument the algorithm is a function and 2238 01:19:35,960 --> 01:19:39,719 the output in this case is a side effect 2239 01:19:38,280 --> 01:19:41,880 terminology that you'll just hear more 2240 01:19:39,719 --> 01:19:43,320 and more and it'll eventually sink in 2241 01:19:41,880 --> 01:19:45,920 but not to worry if the terminology 2242 01:19:43,320 --> 01:19:48,679 doesn't come naturally early on so what 2243 01:19:45,920 --> 01:19:50,440 more might I do with this let me go back 2244 01:19:48,679 --> 01:19:52,000 to scratch here and make this maybe 2245 01:19:50,440 --> 01:19:53,360 perhaps more interactive and actually 2246 01:19:52,000 --> 01:19:55,080 get the cat to say something a little 2247 01:19:53,360 --> 01:19:57,199 more dynamically so instead of hello 2248 01:19:55,080 --> 01:20:00,080 world why don't I get it to say hello to 2249 01:19:57,199 --> 01:20:03,760 me or to you or anyone else so let me do 2250 01:20:00,080 --> 01:20:05,560 this let me go under say uh let me get 2251 01:20:03,760 --> 01:20:07,520 rid of this first and you'll notice this 2252 01:20:05,560 --> 01:20:09,600 neat trick as soon as you start dragging 2253 01:20:07,520 --> 01:20:11,320 on a block if it gets close to it it 2254 01:20:09,600 --> 01:20:13,120 kind of goes gray and it can be 2255 01:20:11,320 --> 01:20:15,239 magnetically snapped together you don't 2256 01:20:13,120 --> 01:20:16,719 have to do it very precisely conversely 2257 01:20:15,239 --> 01:20:19,199 if I want to get rid of a puzzle piece I 2258 01:20:16,719 --> 01:20:20,960 just drag it anywhere on the left let go 2259 01:20:19,199 --> 01:20:22,840 and that deletes it or you can right 2260 01:20:20,960 --> 01:20:24,560 click or control click in a little menu 2261 01:20:22,840 --> 01:20:26,840 will let you delete it as well well let 2262 01:20:24,560 --> 01:20:28,040 me do this instead under sensing which I 2263 01:20:26,840 --> 01:20:29,880 know is there because I've done this 2264 01:20:28,040 --> 01:20:32,360 before are a whole bunch of things 2265 01:20:29,880 --> 01:20:35,159 related to sensing whereby the cat can 2266 01:20:32,360 --> 01:20:37,199 kind of feel out its World in some sense 2267 01:20:35,159 --> 01:20:38,960 it can do things like ask this question 2268 01:20:37,199 --> 01:20:41,120 am I touching the mouse pointer like the 2269 01:20:38,960 --> 01:20:42,520 user's cursor am I touching a specific 2270 01:20:41,120 --> 01:20:44,520 color that you can override to be 2271 01:20:42,520 --> 01:20:46,960 anything you want is the distance to the 2272 01:20:44,520 --> 01:20:49,040 mouse pointer some specific value but 2273 01:20:46,960 --> 01:20:50,880 for now I'm going to focus this on this 2274 01:20:49,040 --> 01:20:53,159 this blue puzzle piece that asks a 2275 01:20:50,880 --> 01:20:54,760 question which itself is this white oval 2276 01:20:53,159 --> 01:20:56,760 that I can apparently change and then 2277 01:20:54,760 --> 01:20:58,800 it's going to wait for a response but 2278 01:20:56,760 --> 01:21:00,239 this puzzle piece is a little different 2279 01:20:58,800 --> 01:21:01,840 it's a little special it comes with a 2280 01:21:00,239 --> 01:21:04,239 freebie it comes with what we're going 2281 01:21:01,840 --> 01:21:05,960 to call technically a return value so 2282 01:21:04,239 --> 01:21:08,920 some functions don't just do something 2283 01:21:05,960 --> 01:21:10,920 on the screen they hand you back so to 2284 01:21:08,920 --> 01:21:12,600 speak a value that you can do anything 2285 01:21:10,920 --> 01:21:14,440 that you want with nothing happens 2286 01:21:12,600 --> 01:21:16,560 immediately unless you do something with 2287 01:21:14,440 --> 01:21:19,440 that so-called return value so let me go 2288 01:21:16,560 --> 01:21:20,560 ahead and drag this thing over here ask 2289 01:21:19,440 --> 01:21:22,159 what's your name and I'll use the 2290 01:21:20,560 --> 01:21:23,639 default question that seems a reasonable 2291 01:21:22,159 --> 01:21:25,440 place to start I'm not going to override 2292 01:21:23,639 --> 01:21:28,679 that default and now let me go ahead and 2293 01:21:25,440 --> 01:21:31,960 zoom out let me go back to looks let me 2294 01:21:28,679 --> 01:21:33,639 go to say and let me just form the 2295 01:21:31,960 --> 01:21:37,199 English sentence I want so let me zoom 2296 01:21:33,639 --> 01:21:39,880 in here and type in hello maybe comma 2297 01:21:37,199 --> 01:21:41,760 space I could do David but that's that's 2298 01:21:39,880 --> 01:21:43,800 obviously not right because I'm asking 2299 01:21:41,760 --> 01:21:45,120 for a name and then I'm like in advance 2300 01:21:43,800 --> 01:21:47,679 hardcoding my name that's not what I 2301 01:21:45,120 --> 01:21:50,639 want I just want hello comma and now let 2302 01:21:47,679 --> 01:21:52,280 me zoom out and grab one more say block 2303 01:21:50,639 --> 01:21:54,159 Let Me Maybe say here okay I don't want 2304 01:21:52,280 --> 01:21:56,000 to say hello hello I I don't want to 2305 01:21:54,159 --> 01:21:57,880 just type in my own name CU again then 2306 01:21:56,000 --> 01:22:00,360 what's the point of asking the user for 2307 01:21:57,880 --> 01:22:03,000 their name but notice this if I go back 2308 01:22:00,360 --> 01:22:06,600 to the sensing block this is where that 2309 01:22:03,000 --> 01:22:08,679 oval that's blue called answer is useful 2310 01:22:06,600 --> 01:22:10,239 this will be the so-called return value 2311 01:22:08,679 --> 01:22:12,280 of that function so I'm just going to go 2312 01:22:10,239 --> 01:22:14,080 ahead and do this and drag and drop even 2313 01:22:12,280 --> 01:22:16,040 though it's not the right size it is the 2314 01:22:14,080 --> 01:22:18,080 right shape and so scratch will be smart 2315 01:22:16,040 --> 01:22:20,520 about it and grow to fill that puzzle 2316 01:22:18,080 --> 01:22:22,639 piece for you let me zoom out now and 2317 01:22:20,520 --> 01:22:24,040 now let me click the green flag you'll 2318 01:22:22,639 --> 01:22:25,600 see that scratch is indeed prompting me 2319 01:22:24,040 --> 01:22:27,639 with the speech bubble what's your name 2320 01:22:25,600 --> 01:22:28,840 notice the little text box below the cat 2321 01:22:27,639 --> 01:22:31,199 is asking what's your name so I'm going 2322 01:22:28,840 --> 01:22:33,960 to type in da a v d and hit enter or I 2323 01:22:31,199 --> 01:22:36,639 can click the blue check 2324 01:22:33,960 --> 01:22:38,280 enter okay it's a little weird I wanted 2325 01:22:36,639 --> 01:22:40,760 him to say hello not just my name so let 2326 01:22:38,280 --> 01:22:43,600 me stop let me start it again all right 2327 01:22:40,760 --> 01:22:48,159 hello what's your name da a v 2328 01:22:43,600 --> 01:22:50,360 enter huh kind of rude uh why is there 2329 01:22:48,159 --> 01:22:53,320 this bug like I wanted to say hello 2330 01:22:50,360 --> 01:22:57,520 David not just David and and yet twice 2331 01:22:53,320 --> 01:22:59,960 it has failed to do so uh 2332 01:22:57,520 --> 01:23:02,199 yeah yeah the computer is processing my 2333 01:22:59,960 --> 01:23:03,679 directions my actions really quickly and 2334 01:23:02,199 --> 01:23:05,320 so it actually is doing it it's just you 2335 01:23:03,679 --> 01:23:07,760 and I in the room are just way too slow 2336 01:23:05,320 --> 01:23:10,080 to notice that it said hello David it 2337 01:23:07,760 --> 01:23:11,520 just seems to have just said David so 2338 01:23:10,080 --> 01:23:12,960 all right how can I fix this well here's 2339 01:23:11,520 --> 01:23:14,639 where you start to poke around and think 2340 01:23:12,960 --> 01:23:16,320 about how you might solve this let me go 2341 01:23:14,639 --> 01:23:19,199 back under looks maybe there's a smarter 2342 01:23:16,320 --> 01:23:20,760 way to do this maybe I could do okay I 2343 01:23:19,199 --> 01:23:22,600 could do this how about instead of just 2344 01:23:20,760 --> 01:23:24,320 say hello there's apparently another 2345 01:23:22,600 --> 01:23:26,520 puzzle piece where I can time it so I 2346 01:23:24,320 --> 01:23:29,320 can maybe slow things down a little bit 2347 01:23:26,520 --> 01:23:32,800 so let me do this let me throw away all 2348 01:23:29,320 --> 01:23:35,040 of this let me drag a say hello for two 2349 01:23:32,800 --> 01:23:37,000 seconds let me drag another say hello 2350 01:23:35,040 --> 01:23:39,800 for two seconds let me change the first 2351 01:23:37,000 --> 01:23:42,840 one to indeed hello comma and then let 2352 01:23:39,800 --> 01:23:44,360 me go back to sensing let me grab that 2353 01:23:42,840 --> 01:23:45,840 same answer because I threw it away a 2354 01:23:44,360 --> 01:23:47,159 second ago and I'll just change it I 2355 01:23:45,840 --> 01:23:49,840 don't even have to delete hello I can 2356 01:23:47,159 --> 01:23:51,239 just overwrite it like this so now I 2357 01:23:49,840 --> 01:23:54,159 think we'll kind of pump the brakes and 2358 01:23:51,239 --> 01:23:57,280 see things more slowly let me stop let 2359 01:23:54,159 --> 01:24:01,520 me start da ID enter 2360 01:23:57,280 --> 01:24:02,880 hello David okay so it's better like it 2361 01:24:01,520 --> 01:24:05,320 seems to be working I think your 2362 01:24:02,880 --> 01:24:06,679 hypothesis was right just looks kind of 2363 01:24:05,320 --> 01:24:07,600 stupid right like the fact that it's 2364 01:24:06,679 --> 01:24:10,120 saying 2365 01:24:07,600 --> 01:24:11,679 hello David like we can do better and 2366 01:24:10,120 --> 01:24:13,880 like literally every piece of software 2367 01:24:11,679 --> 01:24:16,199 on your phone or Mac or PC is better 2368 01:24:13,880 --> 01:24:17,840 than that it adds words together in the 2369 01:24:16,199 --> 01:24:19,440 user interfaces you and I are familiar 2370 01:24:17,840 --> 01:24:21,800 with so let's go a little more fishing 2371 01:24:19,440 --> 01:24:23,560 here let me throw away these let me go 2372 01:24:21,800 --> 01:24:27,800 back to look and just get the simpler 2373 01:24:23,560 --> 01:24:29,679 say I want this to say hello comma name 2374 01:24:27,800 --> 01:24:31,360 where name comes from that answer return 2375 01:24:29,679 --> 01:24:32,920 value so how can I do this well let me 2376 01:24:31,360 --> 01:24:34,639 go under operations which we haven't 2377 01:24:32,920 --> 01:24:36,440 been before there's a lot of stuff in 2378 01:24:34,639 --> 01:24:38,400 here some of it's mathematically related 2379 01:24:36,440 --> 01:24:40,040 adding subtracting and so forth you can 2380 01:24:38,400 --> 01:24:42,040 Generate random numbers which might be 2381 01:24:40,040 --> 01:24:44,280 useful and if I keep scrolling down 2382 01:24:42,040 --> 01:24:45,880 there's this join apple and banana but 2383 01:24:44,280 --> 01:24:48,520 that's just placeholder text you can 2384 01:24:45,880 --> 01:24:50,159 join one piece of text with another 2385 01:24:48,520 --> 01:24:52,040 piece of text by default apple and 2386 01:24:50,159 --> 01:24:54,800 banana but let's change it to hello and 2387 01:24:52,040 --> 01:24:56,840 my name name so this to wrong size but 2388 01:24:54,800 --> 01:24:59,600 right shape so let me let it snap into 2389 01:24:56,840 --> 01:25:02,639 place let me go ahead now and do hello 2390 01:24:59,600 --> 01:25:05,520 comma and now I think I just want to go 2391 01:25:02,639 --> 01:25:08,119 grab that answer return value let me 2392 01:25:05,520 --> 01:25:10,639 drag the same oval as before clobber 2393 01:25:08,119 --> 01:25:14,119 that is overwrite banana so now I'm kind 2394 01:25:10,639 --> 01:25:16,920 of composing functions the output of one 2395 01:25:14,119 --> 01:25:19,199 function join is going to be the input 2396 01:25:16,920 --> 01:25:20,239 of another function say so let's see 2397 01:25:19,199 --> 01:25:22,040 what happens now that they're kind of 2398 01:25:20,239 --> 01:25:24,239 stacked on top of each other or nested 2399 01:25:22,040 --> 01:25:28,119 so to speak click the Green Arrow a 2400 01:25:24,239 --> 01:25:29,159 green flag DAV ID enter hello David all 2401 01:25:28,119 --> 01:25:31,880 right that was pretty fast let's just 2402 01:25:29,159 --> 01:25:35,360 just do it once more stop start here we 2403 01:25:31,880 --> 01:25:37,000 go daavid enter okay right it's not the 2404 01:25:35,360 --> 01:25:38,880 most exciting program in the world but 2405 01:25:37,000 --> 01:25:39,880 it's more correct it's better design 2406 01:25:38,880 --> 01:25:41,960 just because that's what you would kind 2407 01:25:39,880 --> 01:25:43,600 of expect the software to do and not be 2408 01:25:41,960 --> 01:25:45,600 some kind of lame user interface that's 2409 01:25:43,600 --> 01:25:46,920 just inserting random delays to just 2410 01:25:45,600 --> 01:25:48,920 make it kind of work like that's a 2411 01:25:46,920 --> 01:25:50,119 workaround a hack if you will but 2412 01:25:48,920 --> 01:25:51,800 there's some cool things you can do with 2413 01:25:50,119 --> 01:25:53,800 scratch and we won't really go down the 2414 01:25:51,800 --> 01:25:55,400 rabbit hole of all of the fun and 2415 01:25:53,800 --> 01:25:57,119 familyfriendly features that it has but 2416 01:25:55,400 --> 01:25:59,400 there is one that's kind of cool here 2417 01:25:57,119 --> 01:26:01,280 let me go into the extensions button at 2418 01:25:59,400 --> 01:26:03,320 the bottom left of my screen and this 2419 01:26:01,280 --> 01:26:04,880 one's kind of cool let me go to text to 2420 01:26:03,320 --> 01:26:06,280 speech and you'll notice that this one 2421 01:26:04,880 --> 01:26:09,480 requires internet because it's 2422 01:26:06,280 --> 01:26:12,239 cloud-based but this just gave me some 2423 01:26:09,480 --> 01:26:13,840 new puzzle pieces in a new category text 2424 01:26:12,239 --> 01:26:16,040 to speech and these green ones do 2425 01:26:13,840 --> 01:26:18,239 exactly what they say so let me do this 2426 01:26:16,040 --> 01:26:19,480 let me zoom out again let me keep the 2427 01:26:18,239 --> 01:26:21,280 join block and I'm just going to 2428 01:26:19,480 --> 01:26:22,719 temporarily toss it over here it's not 2429 01:26:21,280 --> 01:26:24,880 going to delete itself cuz I didn't drag 2430 01:26:22,719 --> 01:26:27,320 it over to the other side but I'm going 2431 01:26:24,880 --> 01:26:29,719 to get rid of the say block in purple 2432 01:26:27,320 --> 01:26:32,119 I'm going to do the speak block here in 2433 01:26:29,719 --> 01:26:33,440 green and let it snap into place and 2434 01:26:32,119 --> 01:26:36,360 then I'm going to drag and drop this 2435 01:26:33,440 --> 01:26:38,719 onto the input to speak and now perhaps 2436 01:26:36,360 --> 01:26:42,159 a little more adorably let's try this 2437 01:26:38,719 --> 01:26:44,239 green flag what's your name d ID enter 2438 01:26:42,159 --> 01:26:47,639 and hello 2439 01:26:44,239 --> 01:26:49,320 David okay it's a little it's a little 2440 01:26:47,639 --> 01:26:50,719 robotic but at least now it has 2441 01:26:49,320 --> 01:26:52,679 synthesized speech and I've kind of got 2442 01:26:50,719 --> 01:26:54,880 my own like Siri or Google assistant or 2443 01:26:52,679 --> 01:26:56,840 Alexa thing going on here now where it's 2444 01:26:54,880 --> 01:26:59,119 now recognized whatever text it is and 2445 01:26:56,840 --> 01:27:00,760 it's played it well let's make this an 2446 01:26:59,119 --> 01:27:02,440 actual cat that doesn't talk in that 2447 01:27:00,760 --> 01:27:04,400 weird human voice let me go ahead and 2448 01:27:02,440 --> 01:27:06,280 get rid of most of this stuff and let's 2449 01:27:04,400 --> 01:27:08,600 get the cat to actually meow like a cat 2450 01:27:06,280 --> 01:27:10,960 tends to and let me go under the sounds 2451 01:27:08,600 --> 01:27:12,800 block now MIT gives you a few sounds for 2452 01:27:10,960 --> 01:27:14,320 free because it's designed around a cap 2453 01:27:12,800 --> 01:27:17,639 by default and I'm going to go ahead and 2454 01:27:14,320 --> 01:27:19,800 grab this one play sound meow until done 2455 01:27:17,639 --> 01:27:21,119 and now and we saw I heard a teaser for 2456 01:27:19,800 --> 01:27:23,600 this earlier in the 2457 01:27:21,119 --> 01:27:25,239 crowd 2458 01:27:23,600 --> 01:27:26,960 it's a little piercing admittedly we can 2459 01:27:25,239 --> 01:27:28,320 lower the volume a little bit there but 2460 01:27:26,960 --> 01:27:30,560 notice if I want the cat to meow a 2461 01:27:28,320 --> 01:27:33,960 second time I'll just click it 2462 01:27:30,560 --> 01:27:37,080 again okay and over there too I 2463 01:27:33,960 --> 01:27:40,040 hear okay all right so it's kind of cute 2464 01:27:37,080 --> 01:27:43,040 now right so it's just meow okay yes 2465 01:27:40,040 --> 01:27:45,000 echo echo so it's meowing now every time 2466 01:27:43,040 --> 01:27:47,400 I hit the green flag now that's great 2467 01:27:45,000 --> 01:27:49,600 but even a kid is probably going to like 2468 01:27:47,400 --> 01:27:51,159 would prefer that it just meow perhaps 2469 01:27:49,600 --> 01:27:53,440 like again and again without having to 2470 01:27:51,159 --> 01:27:54,800 keep hitting the button so well how 2471 01:27:53,440 --> 01:27:57,280 might we do this all right well if I 2472 01:27:54,800 --> 01:27:59,760 want it to meow multiple times why don't 2473 01:27:57,280 --> 01:28:02,199 I just like grab it another time and 2474 01:27:59,760 --> 01:28:03,719 another time alternatively you can right 2475 01:28:02,199 --> 01:28:05,280 click or control click a puzzle piece 2476 01:28:03,719 --> 01:28:08,600 and just duplicate it from a little menu 2477 01:28:05,280 --> 01:28:08,600 that drops down so here we go three 2478 01:28:09,360 --> 01:28:13,960 meows all right that's not really a 2479 01:28:11,400 --> 01:28:17,159 happy cat it sounds maybe hungry so can 2480 01:28:13,960 --> 01:28:19,600 we slow that down well maybe in fact if 2481 01:28:17,159 --> 01:28:21,800 I poke around let me go under control 2482 01:28:19,600 --> 01:28:23,760 looks like there's a weight block wait 1 2483 01:28:21,800 --> 01:28:25,480 second by default and notice scratch 2484 01:28:23,760 --> 01:28:27,679 will be pretty accommodating if you just 2485 01:28:25,480 --> 01:28:29,719 hover in between blocks it will grow to 2486 01:28:27,679 --> 01:28:31,600 fill that too so I could change it to 2487 01:28:29,719 --> 01:28:33,600 one or two or anything seconds I'll just 2488 01:28:31,600 --> 01:28:36,800 leave it at the default for now one and 2489 01:28:33,600 --> 01:28:36,800 now I'll go ahead and do 2490 01:28:39,040 --> 01:28:44,280 this okay so cuter and less hungry and 2491 01:28:41,960 --> 01:28:46,440 just more friendly but this isn't the 2492 01:28:44,280 --> 01:28:48,400 best design it is correct and let's use 2493 01:28:46,440 --> 01:28:50,239 that as a term of art correct means the 2494 01:28:48,400 --> 01:28:52,520 code does what you want it to do I want 2495 01:28:50,239 --> 01:28:55,000 the cat to meow three times slow slly 2496 01:28:52,520 --> 01:28:57,199 and it did so iager this is correct but 2497 01:28:55,000 --> 01:28:59,199 it's not the best design and this is 2498 01:28:57,199 --> 01:29:01,119 where things get more subjective right 2499 01:28:59,199 --> 01:29:03,239 like you could write accurate sentences 2500 01:29:01,119 --> 01:29:05,280 in an essay for an English class but 2501 01:29:03,239 --> 01:29:06,840 otherwise just it's just completely a 2502 01:29:05,280 --> 01:29:08,000 mess like your arguments here and there 2503 01:29:06,840 --> 01:29:11,119 and you don't say anything wrong but you 2504 01:29:08,000 --> 01:29:13,040 don't say it well in the context of code 2505 01:29:11,119 --> 01:29:14,920 we can do better than this and copy 2506 01:29:13,040 --> 01:29:17,400 paste or repeating yourself again and 2507 01:29:14,920 --> 01:29:19,080 again tends to be bad practice why 2508 01:29:17,400 --> 01:29:21,520 suppose that you want to change the 2509 01:29:19,080 --> 01:29:23,520 weight to two seconds instead of one 2510 01:29:21,520 --> 01:29:25,600 it's aditt not a big deal F I click 2511 01:29:23,520 --> 01:29:27,280 there I change it to two I click there I 2512 01:29:25,600 --> 01:29:29,280 change it to two but what if you Ma five 2513 01:29:27,280 --> 01:29:31,239 times 10 times now I have to change the 2514 01:29:29,280 --> 01:29:32,800 weight like in five 10 different places 2515 01:29:31,239 --> 01:29:34,679 like that's just stupid it's taking 2516 01:29:32,800 --> 01:29:36,480 unnecessary human time and you're going 2517 01:29:34,679 --> 01:29:37,719 to screw up eventually especially if 2518 01:29:36,480 --> 01:29:39,159 your program is getting longer you're 2519 01:29:37,719 --> 01:29:40,440 going to miss one of the inputs you're 2520 01:29:39,159 --> 01:29:42,480 going to leave the number wrong and 2521 01:29:40,440 --> 01:29:43,800 you're that's a bug so just based on 2522 01:29:42,480 --> 01:29:45,760 what you've seen already or if you've 2523 01:29:43,800 --> 01:29:47,480 programmed before which a few of you 2524 01:29:45,760 --> 01:29:50,440 have what's the term of art here that 2525 01:29:47,480 --> 01:29:53,320 will solve this how can we design this 2526 01:29:50,440 --> 01:29:55,679 better I heard it here here yeah so a 2527 01:29:53,320 --> 01:29:57,679 loop a loop some kind of cycle that says 2528 01:29:55,679 --> 01:29:59,600 do that again do that again not 2529 01:29:57,679 --> 01:30:01,719 infinitely many times necessarily but 2530 01:29:59,600 --> 01:30:03,520 some finite number well you can perhaps 2531 01:30:01,719 --> 01:30:06,320 see a spoiler on the screen under the 2532 01:30:03,520 --> 01:30:07,960 same uh orange control category is a 2533 01:30:06,320 --> 01:30:09,920 repeat block and by default it's 2534 01:30:07,960 --> 01:30:11,560 proposing 10 but we can change that so 2535 01:30:09,920 --> 01:30:14,480 let me do this I'm going to throw away 2536 01:30:11,560 --> 01:30:16,480 most of this copy paste as redundant I'm 2537 01:30:14,480 --> 01:30:18,119 going to detach this temporarily just to 2538 01:30:16,480 --> 01:30:20,719 make room for something else and I'm 2539 01:30:18,119 --> 01:30:22,159 going to drag a repeat block over here 2540 01:30:20,719 --> 01:30:23,320 and let that snap into place and I'm 2541 01:30:22,159 --> 01:30:25,360 going to change it for now just to be 2542 01:30:23,320 --> 01:30:26,840 three for consistency and this is the 2543 01:30:25,360 --> 01:30:29,159 correct shape even though it's too small 2544 01:30:26,840 --> 01:30:33,960 but scratch will accommodate that for us 2545 01:30:29,159 --> 01:30:35,800 and now same uh same output but arguably 2546 01:30:33,960 --> 01:30:37,400 better designed why because if I want to 2547 01:30:35,800 --> 01:30:39,560 change the number of meows I change it 2548 01:30:37,400 --> 01:30:41,400 in one place no copy paste messiness if 2549 01:30:39,560 --> 01:30:42,639 I want to change the waiting one place I 2550 01:30:41,400 --> 01:30:44,320 don't have to change it in multiple 2551 01:30:42,639 --> 01:30:46,639 places and not screw up so let me hit 2552 01:30:44,320 --> 01:30:46,639 the green 2553 01:30:49,320 --> 01:30:54,520 flag all right so nice now it would have 2554 01:30:52,199 --> 01:30:56,480 been nice if MIT had just given us a 2555 01:30:54,520 --> 01:30:58,440 meow block that just automates all of 2556 01:30:56,480 --> 01:31:00,280 this for us let me wager they gave us 2557 01:30:58,440 --> 01:31:03,440 the low-level implementation details 2558 01:31:00,280 --> 01:31:05,080 they gave us the play sound meow but I 2559 01:31:03,440 --> 01:31:06,960 had to implement like a decent number of 2560 01:31:05,080 --> 01:31:08,280 blocks just to get a cat to meow again 2561 01:31:06,960 --> 01:31:11,400 and again I feel like we should have 2562 01:31:08,280 --> 01:31:12,679 gotten that for free from MIT well they 2563 01:31:11,400 --> 01:31:15,159 don't have to be the only ones that 2564 01:31:12,679 --> 01:31:17,480 invent blocks for us to use you can 2565 01:31:15,159 --> 01:31:19,760 write your own functions your own verbs 2566 01:31:17,480 --> 01:31:22,639 or actions so how can we do this let's 2567 01:31:19,760 --> 01:31:24,880 make our own puzzle piece called now 2568 01:31:22,639 --> 01:31:27,000 that uses this code but creates it in 2569 01:31:24,880 --> 01:31:29,000 such a way that it's reusable elsewhere 2570 01:31:27,000 --> 01:31:30,760 so let me do this under my blocks in 2571 01:31:29,000 --> 01:31:33,159 pink here I'm going to go ahead and 2572 01:31:30,760 --> 01:31:34,639 click literally make a block now here's 2573 01:31:33,159 --> 01:31:37,239 an interface via which I can give the 2574 01:31:34,639 --> 01:31:38,400 block a name Meo W will be the name of 2575 01:31:37,239 --> 01:31:40,679 this block and I'm just going to go 2576 01:31:38,400 --> 01:31:42,760 ahead and quickly click okay that just 2577 01:31:40,679 --> 01:31:45,000 gives me a very generic pink puzzle 2578 01:31:42,760 --> 01:31:47,159 piece that starts with the word Define 2579 01:31:45,000 --> 01:31:49,920 because scratch is asking me to Define 2580 01:31:47,159 --> 01:31:51,800 that is implement or create this new 2581 01:31:49,920 --> 01:31:53,880 puzzle piece for me well what does it 2582 01:31:51,800 --> 01:31:56,119 mean to meow I'm going to claim that it 2583 01:31:53,880 --> 01:31:58,679 means to do these two steps to play the 2584 01:31:56,119 --> 01:32:01,000 sound meow and then just wait for one 2585 01:31:58,679 --> 01:32:03,360 second but what's powerful about this 2586 01:32:01,000 --> 01:32:06,400 idea is look at this up top now that 2587 01:32:03,360 --> 01:32:08,320 I've made a block it exists in scratch D 2588 01:32:06,400 --> 01:32:10,400 MIT didn't need to create this for me I 2589 01:32:08,320 --> 01:32:12,639 created it for myself and even you if we 2590 01:32:10,400 --> 01:32:15,960 end up sharing code so I can now drag 2591 01:32:12,639 --> 01:32:18,400 meow up in here and what's nice about 2592 01:32:15,960 --> 01:32:20,800 meow is that itself is yes a function 2593 01:32:18,400 --> 01:32:23,560 but it's also an abstraction like never 2594 01:32:20,800 --> 01:32:26,040 again do I or even you need to worry or 2595 01:32:23,560 --> 01:32:27,800 care about what it means to meow or 2596 01:32:26,040 --> 01:32:29,600 implement it I can sort of drag it out 2597 01:32:27,800 --> 01:32:31,320 of the way I didn't delete it drag it 2598 01:32:29,600 --> 01:32:33,320 out of the way out of sight out of mind 2599 01:32:31,320 --> 01:32:34,800 why because my code is now even better 2600 01:32:33,320 --> 01:32:36,800 designed in some sense because it's more 2601 01:32:34,800 --> 01:32:39,400 readable what is it doing when the green 2602 01:32:36,800 --> 01:32:41,159 flag is clicked repeat three times meow 2603 01:32:39,400 --> 01:32:42,679 it just says what it means and so it's a 2604 01:32:41,159 --> 01:32:44,119 lot easier to read it and it's a lot 2605 01:32:42,679 --> 01:32:46,719 easier to think about it especially if 2606 01:32:44,119 --> 01:32:49,199 you're using Meow in other uh projects 2607 01:32:46,719 --> 01:32:52,600 too now let me go ahead and right hit 2608 01:32:49,199 --> 01:32:54,719 click play same thing so it's not really 2609 01:32:52,600 --> 01:32:57,199 fundamentally any different but I can 2610 01:32:54,719 --> 01:32:59,360 make this custom puzzle piece this own 2611 01:32:57,199 --> 01:33:01,880 function of M meow even more powerful 2612 01:32:59,360 --> 01:33:03,400 let me kind of rewind a bit and go to my 2613 01:33:01,880 --> 01:33:05,520 meow puzzle piece and I am going to 2614 01:33:03,400 --> 01:33:07,199 control click or right click on my pink 2615 01:33:05,520 --> 01:33:09,679 puzzle piece and I'm going to edit it so 2616 01:33:07,199 --> 01:33:11,800 I kind of regret making meow so simple 2617 01:33:09,679 --> 01:33:15,080 wouldn't it be nice if meow took an 2618 01:33:11,800 --> 01:33:16,760 input AKA an argument that tells meow 2619 01:33:15,080 --> 01:33:19,080 how many times to meow then I can get 2620 01:33:16,760 --> 01:33:21,199 rid of that Loop and just tell meow how 2621 01:33:19,080 --> 01:33:22,840 many meows I actually want so I'm going 2622 01:33:21,199 --> 01:33:25,719 to click on another button here called 2623 01:33:22,840 --> 01:33:27,239 literally add an input and it's going to 2624 01:33:25,719 --> 01:33:28,840 have placeholder here so I'm just going 2625 01:33:27,239 --> 01:33:31,080 to put a placeholder there I keep using 2626 01:33:28,840 --> 01:33:32,880 n for number which is a go-to in 2627 01:33:31,080 --> 01:33:34,280 computer scientist terms um and I'm 2628 01:33:32,880 --> 01:33:35,440 going to add some descriptive text just 2629 01:33:34,280 --> 01:33:37,080 so that it's a little more 2630 01:33:35,440 --> 01:33:40,040 self-explanatory I'm just going to say 2631 01:33:37,080 --> 01:33:41,719 meow n times but there's only one oval 2632 01:33:40,040 --> 01:33:43,840 times is just going to be explanatory 2633 01:33:41,719 --> 01:33:47,119 text and now notice what has happened 2634 01:33:43,840 --> 01:33:49,639 now my puzzle piece takes an input AKA 2635 01:33:47,119 --> 01:33:51,119 an argument that will tell that function 2636 01:33:49,639 --> 01:33:52,000 to meow some number of times but it's 2637 01:33:51,119 --> 01:33:54,520 not just going to going to work 2638 01:33:52,000 --> 01:33:57,400 magically I need to implement that lower 2639 01:33:54,520 --> 01:33:59,719 level detail so let me zoom out I have 2640 01:33:57,400 --> 01:34:00,960 to remind myself what this function was 2641 01:33:59,719 --> 01:34:02,239 so I'm going to drag it higher up just 2642 01:34:00,960 --> 01:34:04,520 so they're on the screen at the same 2643 01:34:02,239 --> 01:34:06,520 time I'm going to go ahead now and 2644 01:34:04,520 --> 01:34:08,280 temporarily move this over here I'm 2645 01:34:06,520 --> 01:34:10,119 going to temporarily detach this over 2646 01:34:08,280 --> 01:34:13,639 here why because what I think I want to 2647 01:34:10,119 --> 01:34:17,199 do is move my Loop into the function 2648 01:34:13,639 --> 01:34:19,320 itself move the play and the weight into 2649 01:34:17,199 --> 01:34:22,400 the loop but I don't want to hardcode 2650 01:34:19,320 --> 01:34:24,880 three notice that n here is its own oval 2651 01:34:22,400 --> 01:34:27,600 I can drag a copy of N and just let it 2652 01:34:24,880 --> 01:34:30,320 go there so now I have a new version of 2653 01:34:27,600 --> 01:34:32,920 meow that takes an argument in that 2654 01:34:30,320 --> 01:34:35,080 tells meow how many times to meow and 2655 01:34:32,920 --> 01:34:36,520 now let me again drag this out of sight 2656 01:34:35,080 --> 01:34:38,119 out of mind because who cares how I 2657 01:34:36,520 --> 01:34:40,960 implemented it once it's implemented 2658 01:34:38,119 --> 01:34:43,080 it's sort of done now my program is even 2659 01:34:40,960 --> 01:34:45,040 better designed in some sense why 2660 01:34:43,080 --> 01:34:46,960 because now it really just says what it 2661 01:34:45,040 --> 01:34:49,040 means there's no Loop there's no repeat 2662 01:34:46,960 --> 01:34:51,760 no like implementation details when 2663 01:34:49,040 --> 01:34:54,400 green flag clicked meow three times and 2664 01:34:51,760 --> 01:34:56,159 so functions indeed let you implement 2665 01:34:54,400 --> 01:34:58,199 algorithms like they're just code that 2666 01:34:56,159 --> 01:35:00,280 do something for you but they're also 2667 01:34:58,199 --> 01:35:02,119 themselves abstractions why because once 2668 01:35:00,280 --> 01:35:04,119 a function exists it has a name and you 2669 01:35:02,119 --> 01:35:06,639 can think about it in that term and you 2670 01:35:04,119 --> 01:35:09,639 can use it by its name you don't have to 2671 01:35:06,639 --> 01:35:11,400 care or remember how the function itself 2672 01:35:09,639 --> 01:35:13,440 was built whether it's by you or even 2673 01:35:11,400 --> 01:35:16,280 MIT so again here I'll click the green 2674 01:35:13,440 --> 01:35:16,280 flag it's the same 2675 01:35:18,440 --> 01:35:23,719 thing so still correct but better and 2676 01:35:21,600 --> 01:35:25,560 better designed and so anytime here and 2677 01:35:23,719 --> 01:35:27,400 out with scratch or soon C and 2678 01:35:25,560 --> 01:35:29,639 eventually python when you find yourself 2679 01:35:27,400 --> 01:35:31,960 doing anything resembling copy paste or 2680 01:35:29,639 --> 01:35:33,360 again and again grabbing the same code 2681 01:35:31,960 --> 01:35:35,960 probably an opportunity to say wait a 2682 01:35:33,360 --> 01:35:37,760 minute let me refactor this so to speak 2683 01:35:35,960 --> 01:35:39,480 that is rip out the code that seems to 2684 01:35:37,760 --> 01:35:41,040 be repeated again and again and put it 2685 01:35:39,480 --> 01:35:44,080 in its own function so you can give it a 2686 01:35:41,040 --> 01:35:47,239 descriptive name and use and reuse it 2687 01:35:44,080 --> 01:35:49,440 any questions just yet on now saying or 2688 01:35:47,239 --> 01:35:52,010 these Loops or these functions that 2689 01:35:49,440 --> 01:35:54,840 we're using yeah 2690 01:35:52,010 --> 01:35:56,280 [Music] 2691 01:35:54,840 --> 01:35:58,400 how did I make it so it meows three 2692 01:35:56,280 --> 01:36:00,360 times so I originally only had a puzzle 2693 01:35:58,400 --> 01:36:02,920 piece called meow and I decided to 2694 01:36:00,360 --> 01:36:04,719 improve it so I held down control and I 2695 01:36:02,920 --> 01:36:06,840 right clicked or control clicked on the 2696 01:36:04,719 --> 01:36:08,400 pink puzzle piece at top left and I 2697 01:36:06,840 --> 01:36:10,840 clicked edit and that brought back the 2698 01:36:08,400 --> 01:36:13,239 original interface that lets me add some 2699 01:36:10,840 --> 01:36:15,800 arguments to the puzzle piece itself and 2700 01:36:13,239 --> 01:36:17,920 I clicked add an input on the left here 2701 01:36:15,800 --> 01:36:20,159 and then I clicked on add a label over 2702 01:36:17,920 --> 01:36:22,480 here so that just lets you customize it 2703 01:36:20,159 --> 01:36:24,040 even further all right so we've done 2704 01:36:22,480 --> 01:36:25,639 this let's add one of those other 2705 01:36:24,040 --> 01:36:28,000 Primitives too to do something 2706 01:36:25,639 --> 01:36:30,400 optionally so how about we make the cat 2707 01:36:28,000 --> 01:36:32,400 meow only if it's being petted by a 2708 01:36:30,400 --> 01:36:34,480 human as by moving the mouse to hover 2709 01:36:32,400 --> 01:36:36,600 over the cat like a human would pet a 2710 01:36:34,480 --> 01:36:39,840 cat well let me go ahead and throw uh 2711 01:36:36,600 --> 01:36:41,639 away the meowing uh for now and let me 2712 01:36:39,840 --> 01:36:42,920 simplify it by just using a sound I'm 2713 01:36:41,639 --> 01:36:46,199 going to go ahead and do this I'm going 2714 01:36:42,920 --> 01:36:48,400 to go ahead and have a control block 2715 01:36:46,199 --> 01:36:50,800 that says if because I want to implement 2716 01:36:48,400 --> 01:36:51,920 the idea of if the cursor is touching 2717 01:36:50,800 --> 01:36:53,960 the Cal 2718 01:36:51,920 --> 01:36:55,480 then play sound meow or I could use my 2719 01:36:53,960 --> 01:36:57,679 same pink puzzle piece but I'm going to 2720 01:36:55,480 --> 01:37:00,280 throw that away and focus only now on 2721 01:36:57,679 --> 01:37:02,440 the sounds and I'm going to do this uh 2722 01:37:00,280 --> 01:37:03,960 if uh touching Mouse pointer so I need 2723 01:37:02,440 --> 01:37:06,280 to sense something about the world and 2724 01:37:03,960 --> 01:37:08,520 we saw this earlier so if touching Mouse 2725 01:37:06,280 --> 01:37:10,360 pointer so notice this shape here way 2726 01:37:08,520 --> 01:37:13,639 too big but it is the right shape so if 2727 01:37:10,360 --> 01:37:15,840 I hover just right it'll snap into place 2728 01:37:13,639 --> 01:37:19,080 and this now in blue is my Boolean 2729 01:37:15,840 --> 01:37:21,800 expression a yes no question true false 2730 01:37:19,080 --> 01:37:25,159 uh if is a conditional and what do I 2731 01:37:21,800 --> 01:37:27,040 want to do well if the Cat is being uh 2732 01:37:25,159 --> 01:37:29,400 is touching the mouse pointer I want to 2733 01:37:27,040 --> 01:37:31,320 go ahead and play sound meow until done 2734 01:37:29,400 --> 01:37:34,400 so let's do this I'm going to hit green 2735 01:37:31,320 --> 01:37:35,800 flag click now nothing's happened yet 2736 01:37:34,400 --> 01:37:37,119 because it's a conditional right it's 2737 01:37:35,800 --> 01:37:39,040 only supposed to do something if I'm 2738 01:37:37,119 --> 01:37:42,800 touching the cat let me move the cursor 2739 01:37:39,040 --> 01:37:48,000 over to the cat and and and wait for 2740 01:37:42,800 --> 01:37:50,560 it h another bug why is the cat not 2741 01:37:48,000 --> 01:37:53,520 meowing even though I very explicitly 2742 01:37:50,560 --> 01:37:55,719 said if touch in Mouse pointer meow yeah 2743 01:37:53,520 --> 01:37:55,719 in the 2744 01:37:56,239 --> 01:38:00,280 middle yeah this is again my computers 2745 01:37:58,320 --> 01:38:02,119 are so darn fast like yours I click the 2746 01:38:00,280 --> 01:38:03,639 green flag it asked the question am I 2747 01:38:02,119 --> 01:38:05,480 touching the mouse pointer well no cuz 2748 01:38:03,639 --> 01:38:07,639 my cursor was up there not touching the 2749 01:38:05,480 --> 01:38:10,800 cat it's too late the cat's out of the 2750 01:38:07,639 --> 01:38:12,960 bag and so we have to instead solve this 2751 01:38:10,800 --> 01:38:15,480 some other mean by some other means how 2752 01:38:12,960 --> 01:38:18,520 can we fix this how do we fix that sort 2753 01:38:15,480 --> 01:38:20,400 of race yeah yeah so why don't we just 2754 01:38:18,520 --> 01:38:22,480 keep asking the question until I 2755 01:38:20,400 --> 01:38:24,119 eventually am I'm not actually petting 2756 01:38:22,480 --> 01:38:26,520 the cat so let me detach this 2757 01:38:24,119 --> 01:38:28,560 temporarily let me go under control let 2758 01:38:26,520 --> 01:38:29,960 me go under instead of repeat some 2759 01:38:28,560 --> 01:38:31,800 finite number of times let's just do it 2760 01:38:29,960 --> 01:38:33,800 forever so sometimes Loops that do work 2761 01:38:31,800 --> 01:38:36,080 forever are a good thing like the clock 2762 01:38:33,800 --> 01:38:37,520 on your phone that's in a loop forever 2763 01:38:36,080 --> 01:38:39,480 because you want it to always tell time 2764 01:38:37,520 --> 01:38:41,440 and not stop at the end of the day so 2765 01:38:39,480 --> 01:38:43,679 sometimes you do want code to Loop 2766 01:38:41,440 --> 01:38:45,639 forever as in this case so let me go 2767 01:38:43,679 --> 01:38:47,679 ahead and drag and drop it there let me 2768 01:38:45,639 --> 01:38:49,000 again click the green flag nothing's 2769 01:38:47,679 --> 01:38:51,800 happening yet but notice the program's 2770 01:38:49,000 --> 01:38:56,320 still running and so if if I move my 2771 01:38:51,800 --> 01:38:58,480 cursor move my cursor move my cursor 2772 01:38:56,320 --> 01:39:01,320 and okay so maybe we could add some 2773 01:38:58,480 --> 01:39:04,080 waiting but the cat does not want to be 2774 01:39:01,320 --> 01:39:05,360 pet in this case but it's indeed 2775 01:39:04,080 --> 01:39:07,280 conditional so there we have an 2776 01:39:05,360 --> 01:39:09,360 incarnation in scratch of doing 2777 01:39:07,280 --> 01:39:11,159 something conditionally now we can make 2778 01:39:09,360 --> 01:39:13,679 this really cool really fast if you will 2779 01:39:11,159 --> 01:39:16,639 let me stop this version let me go ahead 2780 01:39:13,679 --> 01:39:18,400 and do this uh let me go ahead and throw 2781 01:39:16,639 --> 01:39:20,560 all of this away let me go into my 2782 01:39:18,400 --> 01:39:22,800 little uh extensions Bucket over here 2783 01:39:20,560 --> 01:39:24,400 and let me do video sensing since most 2784 01:39:22,800 --> 01:39:27,000 uh laptops or phones these days have 2785 01:39:24,400 --> 01:39:30,280 cameras and there indeed I am with 2786 01:39:27,000 --> 01:39:32,679 Sanders behind me and let me do this um 2787 01:39:30,280 --> 01:39:35,199 when video motion and let me get out of 2788 01:39:32,679 --> 01:39:37,639 the way when video motion is greater 2789 01:39:35,199 --> 01:39:39,639 than some value so 10 is the default 2790 01:39:37,639 --> 01:39:41,400 this is just a number that measures how 2791 01:39:39,639 --> 01:39:43,320 much motion there is or isn't so small 2792 01:39:41,400 --> 01:39:45,320 number is like no motion big number is 2793 01:39:43,320 --> 01:39:47,840 lots of motion so I'm going to choose 50 2794 01:39:45,320 --> 01:39:49,880 somewhat arbitrarily here so 50 this is 2795 01:39:47,840 --> 01:39:53,159 not normal to program off to the side 2796 01:39:49,880 --> 01:39:56,800 but I'm now going to say this when video 2797 01:39:53,159 --> 01:40:00,000 motion is 50 go ahead and play sound 2798 01:39:56,800 --> 01:40:01,960 meow like this so the cat is still in 2799 01:40:00,000 --> 01:40:05,719 that world I'm going to stop the program 2800 01:40:01,960 --> 01:40:06,520 and rerun it so here we go green flag 2801 01:40:05,719 --> 01:40:08,719 and 2802 01:40:06,520 --> 01:40:10,719 now here come all right this is a little 2803 01:40:08,719 --> 01:40:13,719 creepy the way I'm petting the cat 2804 01:40:10,719 --> 01:40:13,719 but 2805 01:40:15,960 --> 01:40:21,840 and ah 2806 01:40:19,159 --> 01:40:25,199 okay there we go okay so 50 was too big 2807 01:40:21,840 --> 01:40:25,199 of a number I have to pet the cat 2808 01:40:26,440 --> 01:40:31,560 faster whereas this if I don't 2809 01:40:31,880 --> 01:40:37,159 know yeah 2810 01:40:34,239 --> 01:40:38,440 so okay so you can make things even more 2811 01:40:37,159 --> 01:40:39,840 interactive in this way by just 2812 01:40:38,440 --> 01:40:41,560 assembling different puzzle pieces and 2813 01:40:39,840 --> 01:40:42,760 honestly there are so many different 2814 01:40:41,560 --> 01:40:44,480 puzzle pieces in here we're not going to 2815 01:40:42,760 --> 01:40:46,719 even scratch the surface of a lot of 2816 01:40:44,480 --> 01:40:48,360 them but they generally just do what 2817 01:40:46,719 --> 01:40:51,239 they say and indeed when you see on the 2818 01:40:48,360 --> 01:40:52,800 screen here um this pallet of puzzle 2819 01:40:51,239 --> 01:40:54,119 pieces really a lot of programming 2820 01:40:52,800 --> 01:40:55,960 especially early on when learning a 2821 01:40:54,119 --> 01:40:58,000 language is just trying different things 2822 01:40:55,960 --> 01:40:59,920 and try and fail and if it doesn't work 2823 01:40:58,000 --> 01:41:01,520 quite look for doesn't work quite right 2824 01:40:59,920 --> 01:41:03,800 look for an alternative solution there 2825 01:41:01,520 --> 01:41:06,000 too as even I just had to do a moment 2826 01:41:03,800 --> 01:41:07,719 ago well let's go ahead and use actually 2827 01:41:06,000 --> 01:41:09,360 how about another example of something a 2828 01:41:07,719 --> 01:41:11,280 predecessor of yours made let me go 2829 01:41:09,360 --> 01:41:14,639 ahead and grab a program I opened in 2830 01:41:11,280 --> 01:41:17,280 advance here uh called wacka um might we 2831 01:41:14,639 --> 01:41:22,280 get a brave volunteer to come up who is 2832 01:41:17,280 --> 01:41:24,320 willing to whack a mole with their head 2833 01:41:22,280 --> 01:41:26,800 virtually maybe okay let's see how about 2834 01:41:24,320 --> 01:41:30,159 in way back you want to come on down all 2835 01:41:26,800 --> 01:41:33,080 right come on down and in just a sure 2836 01:41:30,159 --> 01:41:33,080 Round of Applause for our 2837 01:41:35,400 --> 01:41:38,560 volunteer all 2838 01:41:38,679 --> 01:41:42,880 right so here we 2839 01:41:43,239 --> 01:41:48,679 have come on down there what's your name 2840 01:41:47,199 --> 01:41:51,520 I'm Josh oh actually say it into the 2841 01:41:48,679 --> 01:41:54,280 microphone hi I'm Josh 2842 01:41:51,520 --> 01:41:57,360 okay nice welcome Josh come on 2843 01:41:54,280 --> 01:41:58,520 over all right so same idea here I'll 2844 01:41:57,360 --> 01:41:59,960 take the mic back you can you'll have to 2845 01:41:58,520 --> 01:42:01,040 stand in front of the camera in just a 2846 01:41:59,960 --> 01:42:03,440 moment you're going to have to position 2847 01:42:01,040 --> 01:42:06,159 your head in a box that your classmate 2848 01:42:03,440 --> 01:42:06,159 from yester year 2849 01:42:06,280 --> 01:42:11,440 created and we'll start with beginner 2850 01:42:09,080 --> 01:42:14,240 okay so line your head up in the Box in 2851 01:42:11,440 --> 01:42:14,850 a moment all right all 2852 01:42:14,240 --> 01:42:17,949 [Music] 2853 01:42:14,850 --> 01:42:17,949 [Applause] 2854 01:42:19,760 --> 01:42:22,760 right 2855 01:42:26,030 --> 01:42:32,080 [Music] 2856 01:42:28,679 --> 01:42:32,080 nice 12 2857 01:42:35,960 --> 01:42:41,840 seconds 5 Seconds notice the score is up 2858 01:42:39,040 --> 01:42:44,280 to 18 already pretty 2859 01:42:41,840 --> 01:42:47,000 good all right a round of applause for 2860 01:42:44,280 --> 01:42:47,000 Josh if you 2861 01:42:49,760 --> 01:42:54,159 can so notice how using some fairly 2862 01:42:52,560 --> 01:42:56,360 simple Primitives things do get 2863 01:42:54,159 --> 01:42:57,599 interesting pretty fast and how was that 2864 01:42:56,360 --> 01:42:59,440 implemented well there were probably at 2865 01:42:57,599 --> 01:43:01,080 least four Sprites so you're not re 2866 01:42:59,440 --> 01:43:02,760 confined to just one cat you can create 2867 01:43:01,080 --> 01:43:04,040 more and more Sprites change what they 2868 01:43:02,760 --> 01:43:06,000 look like so they actually look like a 2869 01:43:04,040 --> 01:43:07,880 mole in this case there's probably some 2870 01:43:06,000 --> 01:43:10,199 conditionals in there Some Loops for 30 2871 01:43:07,880 --> 01:43:12,400 seconds that's checking if Josh's head's 2872 01:43:10,199 --> 01:43:14,280 movement is exceeding some value over 2873 01:43:12,400 --> 01:43:16,320 this way or over this way then increment 2874 01:43:14,280 --> 01:43:18,000 something called a variable we'll see 2875 01:43:16,320 --> 01:43:20,239 those two just like in algebra you might 2876 01:43:18,000 --> 01:43:22,320 have X and Y and Z storing values like 2877 01:43:20,239 --> 01:43:24,639 numbers so can computer programs have 2878 01:43:22,320 --> 01:43:27,280 variables called X or Y or Z or more 2879 01:43:24,639 --> 01:43:29,199 descriptively called score as in this 2880 01:43:27,280 --> 01:43:31,239 case at top right or another variable 2881 01:43:29,199 --> 01:43:32,880 called countdown typically one word in 2882 01:43:31,239 --> 01:43:34,280 code but in this case two words that 2883 01:43:32,880 --> 01:43:36,119 just store some value so there's 2884 01:43:34,280 --> 01:43:38,199 probably some math going on in there 2885 01:43:36,119 --> 01:43:40,000 whereby the author of this program just 2886 01:43:38,199 --> 01:43:42,280 is incrementing that is adding one in 2887 01:43:40,000 --> 01:43:44,199 one every time it detected that uh a 2888 01:43:42,280 --> 01:43:46,760 mole had been whacked in this case with 2889 01:43:44,199 --> 01:43:48,920 movement so back in the day I myself 2890 01:43:46,760 --> 01:43:50,280 actually implemented my very first 2891 01:43:48,920 --> 01:43:52,760 program in scratch when I was at a 2892 01:43:50,280 --> 01:43:54,679 graduate student actually at MIT um cost 2893 01:43:52,760 --> 01:43:56,639 registered at MIT taking a class from 2894 01:43:54,679 --> 01:43:58,080 mit's media lab specifically the 2895 01:43:56,639 --> 01:44:00,040 lifelong kindergarten Group which is the 2896 01:43:58,080 --> 01:44:03,119 group that created scratch itself and 2897 01:44:00,040 --> 01:44:05,880 the program I wrote all those years ago 2898 01:44:03,119 --> 01:44:07,760 and still rather cling to is a little 2899 01:44:05,880 --> 01:44:10,080 something here called Oscar time that I 2900 01:44:07,760 --> 01:44:13,360 thought I'd play just a quick excerpt of 2901 01:44:10,080 --> 01:44:15,880 my myself here so in this case consider 2902 01:44:13,360 --> 01:44:17,760 as the music starts playing how this 2903 01:44:15,880 --> 01:44:19,719 program which is much more sophisticated 2904 01:44:17,760 --> 01:44:21,800 certainly than the earliest say hello 2905 01:44:19,719 --> 01:44:23,840 examples we just did might also be 2906 01:44:21,800 --> 01:44:26,760 implemented let me go ahead now and 2907 01:44:23,840 --> 01:44:26,760 click the green 2908 01:44:30,719 --> 01:44:35,360 flag so some trash is moving presumably 2909 01:44:33,520 --> 01:44:38,800 in some kind of loop from the 2910 01:44:35,360 --> 01:44:39,800 Top If I'm touching the mouse cursor it 2911 01:44:38,800 --> 01:44:43,239 follows 2912 01:44:39,800 --> 01:44:45,639 me if I hover over the trash can it 2913 01:44:43,239 --> 01:44:48,920 responds if I let 2914 01:44:45,639 --> 01:44:50,760 go in some kind of loop Oscar pops out 2915 01:44:48,920 --> 01:44:53,880 creates a variable with the current 2916 01:44:50,760 --> 01:44:53,880 score and it happens 2917 01:45:04,320 --> 01:45:08,440 again pretty easy at first 2918 01:45:11,280 --> 01:45:14,960 but I don't need to keep playing this up 2919 01:45:13,360 --> 01:45:16,840 on stage in front of everyone so my 2920 01:45:14,960 --> 01:45:18,960 score is already now up to some six or 2921 01:45:16,840 --> 01:45:20,920 so but in a moment two you'll see that 2922 01:45:18,960 --> 01:45:24,599 it's going to escalate so I'm taking 2923 01:45:20,920 --> 01:45:24,599 into account some time apparently so 2924 01:45:26,239 --> 01:45:30,080 now so more and more Sprites are 2925 01:45:28,520 --> 01:45:31,320 suddenly appearing and notice that each 2926 01:45:30,080 --> 01:45:32,800 time they're appearing from a different 2927 01:45:31,320 --> 01:45:35,320 part of the screen that's an illusion 2928 01:45:32,800 --> 01:45:37,719 perhaps do that pick a random number 2929 01:45:35,320 --> 01:45:39,560 between X and Y so you can actually pick 2930 01:45:37,719 --> 01:45:41,280 some range of values to have the game 2931 01:45:39,560 --> 01:45:42,679 constantly changing and indeed I'm going 2932 01:45:41,280 --> 01:45:44,880 to go ahead and click stop since i' 2933 01:45:42,679 --> 01:45:46,440 spent like 8 hours plus years ago making 2934 01:45:44,880 --> 01:45:48,400 this and I can never listen to the song 2935 01:45:46,440 --> 01:45:51,280 again not that I should be anyway at 2936 01:45:48,400 --> 01:45:52,719 this point in my life but this song is 2937 01:45:51,280 --> 01:45:55,119 uh synchronized in with a lot of the 2938 01:45:52,719 --> 01:45:56,440 actions that's happening and ultimately 2939 01:45:55,119 --> 01:45:57,880 there's just a lot of building blocks 2940 01:45:56,440 --> 01:46:00,119 but I didn't sit down and Implement 2941 01:45:57,880 --> 01:46:01,840 Oscar time as I called it all at once I 2942 01:46:00,119 --> 01:46:03,599 really did take baby steps so to speak 2943 01:46:01,840 --> 01:46:06,440 and I figured out well how could I 2944 01:46:03,599 --> 01:46:08,840 decompose this Vision I had at the time 2945 01:46:06,440 --> 01:46:10,520 to create this game ultimately and how 2946 01:46:08,840 --> 01:46:12,080 do I bite off maybe the easiest Parts 2947 01:46:10,520 --> 01:46:14,040 first and honestly the first thing I did 2948 01:46:12,080 --> 01:46:15,800 was I found this image and I just like 2949 01:46:14,040 --> 01:46:18,080 dragged and dropped it into scratch okay 2950 01:46:15,800 --> 01:46:19,679 done like lamp post is installed it 2951 01:46:18,080 --> 01:46:21,560 doesn't do anything it's not interactive 2952 01:46:19,679 --> 01:46:23,400 but I at least set the stage so to speak 2953 01:46:21,560 --> 01:46:25,280 for the program then what else might I 2954 01:46:23,400 --> 01:46:28,040 have done well let me do this let me go 2955 01:46:25,280 --> 01:46:32,040 ahead and open up uh in another editor 2956 01:46:28,040 --> 01:46:34,920 here a early incarnation of Oscar Time 2957 01:46:32,040 --> 01:46:37,520 by doing this let me go into Oscar time 2958 01:46:34,920 --> 01:46:39,840 here let me full screen this and here 2959 01:46:37,520 --> 01:46:42,400 you have let me hide the trash for just 2960 01:46:39,840 --> 01:46:44,679 a moment is what I might call the second 2961 01:46:42,400 --> 01:46:46,560 uh version of my program wherein at the 2962 01:46:44,679 --> 01:46:47,920 top right of the stage here I had the 2963 01:46:46,560 --> 01:46:49,639 lamp post which I just dragged and 2964 01:46:47,920 --> 01:46:51,400 dropped and got going but then I added 2965 01:46:49,639 --> 01:46:52,599 an actual Sprite and it has to be a 2966 01:46:51,400 --> 01:46:54,760 Sprite if you wanted to do things 2967 01:46:52,599 --> 01:46:57,040 interactively the lamp post not a Sprite 2968 01:46:54,760 --> 01:46:59,199 it's just an image a costume if you will 2969 01:46:57,040 --> 01:47:00,800 for the whole stage itself a backdrop 2970 01:46:59,199 --> 01:47:02,719 but this thing is indeed a Sprite 2971 01:47:00,800 --> 01:47:04,880 because it needs to respond to code and 2972 01:47:02,719 --> 01:47:07,360 events like dragging and dropping so 2973 01:47:04,880 --> 01:47:10,239 what might I have done early on with 2974 01:47:07,360 --> 01:47:11,840 that code well maybe the first version 2975 01:47:10,239 --> 01:47:13,920 would have been something like this 2976 01:47:11,840 --> 01:47:16,280 where by my very first version of Oscar 2977 01:47:13,920 --> 01:47:17,679 time might have said something like oh 2978 01:47:16,280 --> 01:47:20,119 this how about let me control the 2979 01:47:17,679 --> 01:47:22,199 program as before or rather events when 2980 01:47:20,119 --> 01:47:25,040 the green flag is clicked what do I want 2981 01:47:22,199 --> 01:47:27,960 to do well I want to go ahead and 2982 01:47:25,040 --> 01:47:30,520 forever do something like this uh 2983 01:47:27,960 --> 01:47:32,119 forever so I want the lid to open up if 2984 01:47:30,520 --> 01:47:33,760 I touch it so if the cursor gets near 2985 01:47:32,119 --> 01:47:35,840 the lid I want the lid to open up and 2986 01:47:33,760 --> 01:47:38,280 then if I move away I want it to close 2987 01:47:35,840 --> 01:47:39,719 so how can I do that I want an if but I 2988 01:47:38,280 --> 01:47:41,800 just don't want one question I really 2989 01:47:39,719 --> 01:47:44,119 want two a fork in the road that goes 2990 01:47:41,800 --> 01:47:46,320 left or right so to speak and let me 2991 01:47:44,119 --> 01:47:49,280 grab this puzzle piece here as I did 2992 01:47:46,320 --> 01:47:50,920 long ago so notice it grows to fill 2993 01:47:49,280 --> 01:47:53,159 what's the question I want to ask well 2994 01:47:50,920 --> 01:47:55,760 under sensing I'm going to go ahead here 2995 01:47:53,159 --> 01:47:58,400 and say if this trash can is touching 2996 01:47:55,760 --> 01:47:59,960 the mouse pointer what do I want to do 2997 01:47:58,400 --> 01:48:01,199 well I want to change what the trash can 2998 01:47:59,960 --> 01:48:03,040 looks like and this part I did in 2999 01:48:01,199 --> 01:48:04,599 advance of class if you go up here to 3000 01:48:03,040 --> 01:48:06,840 costumes this is where all the graphical 3001 01:48:04,599 --> 01:48:09,400 stuff happens and you'll see that I 3002 01:48:06,840 --> 01:48:11,639 imported a whole bunch of different 3003 01:48:09,400 --> 01:48:13,800 costumes that effectively much like a 3004 01:48:11,639 --> 01:48:15,920 video when you play them quickly creates 3005 01:48:13,800 --> 01:48:17,800 the illusion of movement some animation 3006 01:48:15,920 --> 01:48:19,840 but it's really just dot dot dot dot dot 3007 01:48:17,800 --> 01:48:21,679 different images showing on the screen 3008 01:48:19,840 --> 01:48:25,199 well some of these costumes are called 3009 01:48:21,679 --> 01:48:27,199 like Oscar 1 Oscar 2 Oscar 1 is closed 3010 01:48:25,199 --> 01:48:29,400 Oscar 2 is open so let's just deal with 3011 01:48:27,199 --> 01:48:32,520 those first so if I'm touching the mouse 3012 01:48:29,400 --> 01:48:33,840 pointer let me go under how about looks 3013 01:48:32,520 --> 01:48:36,320 and we didn't use this before but 3014 01:48:33,840 --> 01:48:37,920 there's this block switch costume to 3015 01:48:36,320 --> 01:48:39,920 something else I'm going to drag and 3016 01:48:37,920 --> 01:48:41,840 drop this inside of the if and notice 3017 01:48:39,920 --> 01:48:45,320 it's a little bit indented I'm going to 3018 01:48:41,840 --> 01:48:47,320 change it not to Oscar 8 but Oscar 2 3019 01:48:45,320 --> 01:48:49,040 otherwise if not touching the mouse 3020 01:48:47,320 --> 01:48:50,960 pointer this is the other direction in 3021 01:48:49,040 --> 01:48:53,440 the fork and the road let's go ahead and 3022 01:48:50,960 --> 01:48:56,159 switch the costume back to what I 3023 01:48:53,440 --> 01:48:58,360 described as Oscar 1 so let me run this 3024 01:48:56,159 --> 01:49:00,639 program and not much of interest is 3025 01:48:58,360 --> 01:49:04,239 happening yet but notice if I move the 3026 01:49:00,639 --> 01:49:05,920 cursor up down but how is that working 3027 01:49:04,239 --> 01:49:07,639 it's just changing the costume that's 3028 01:49:05,920 --> 01:49:09,119 being overlaid on the Sprite so it looks 3029 01:49:07,639 --> 01:49:10,560 like interactivity but you're really 3030 01:49:09,119 --> 01:49:12,040 just changing the Aesthetics and we 3031 01:49:10,560 --> 01:49:14,000 humans are just kind of you know 3032 01:49:12,040 --> 01:49:15,599 assuming oh it's opening up well no it's 3033 01:49:14,000 --> 01:49:17,280 just changing a costume so here's the 3034 01:49:15,599 --> 01:49:19,280 difference the high level abstraction 3035 01:49:17,280 --> 01:49:20,679 trash can opening the lower level 3036 01:49:19,280 --> 01:49:22,800 implementation detail 3037 01:49:20,679 --> 01:49:24,599 costume changing creating that illusion 3038 01:49:22,800 --> 01:49:26,280 and if I wanted to look prettier I could 3039 01:49:24,599 --> 01:49:28,440 just have many other costumes and go 3040 01:49:26,280 --> 01:49:31,199 boom boom boom boom boom to create more 3041 01:49:28,440 --> 01:49:33,679 frames per second if you will so I need 3042 01:49:31,199 --> 01:49:35,440 to do um one other thing maybe if I 3043 01:49:33,679 --> 01:49:37,599 accidentally leave the trash can open 3044 01:49:35,440 --> 01:49:39,840 let me make one change here let me make 3045 01:49:37,599 --> 01:49:41,360 sure that the very first thing I do when 3046 01:49:39,840 --> 01:49:43,920 the green flag is clicked is always 3047 01:49:41,360 --> 01:49:45,719 start with the trash can closed because 3048 01:49:43,920 --> 01:49:47,119 otherwise you might accidentally leave 3049 01:49:45,719 --> 01:49:49,639 it open so this gets me into some 3050 01:49:47,119 --> 01:49:52,599 default state so now it's always closed 3051 01:49:49,639 --> 01:49:54,400 until I manually hover over it instead 3052 01:49:52,599 --> 01:49:56,199 well what might I have done next well if 3053 01:49:54,400 --> 01:49:58,560 I wanted to introduce something like the 3054 01:49:56,199 --> 01:50:00,639 trash I need a second Sprite and here in 3055 01:49:58,560 --> 01:50:03,000 advance I grabb the image already let me 3056 01:50:00,639 --> 01:50:05,840 pretend that this never happened let me 3057 01:50:03,000 --> 01:50:07,840 drag this away here and now I have 3058 01:50:05,840 --> 01:50:09,639 nothing in my code area for this piece 3059 01:50:07,840 --> 01:50:10,960 of trash but it is a second Sprite and 3060 01:50:09,639 --> 01:50:13,080 all I did was I clicked on the little 3061 01:50:10,960 --> 01:50:15,679 cat plus icon here created a second 3062 01:50:13,080 --> 01:50:17,440 Sprite I named it trash I added an 3063 01:50:15,679 --> 01:50:19,599 costume for it sort of the aesthetic 3064 01:50:17,440 --> 01:50:22,000 stuff I did in advance but here I'll do 3065 01:50:19,599 --> 01:50:24,239 now the code how do I want to do this 3066 01:50:22,000 --> 01:50:26,880 well how about when the green flag is 3067 01:50:24,239 --> 01:50:29,760 clicked for the trash can I want the 3068 01:50:26,880 --> 01:50:31,639 trash can in parallel to do or I want 3069 01:50:29,760 --> 01:50:33,760 the trash the piece of trash to do its 3070 01:50:31,639 --> 01:50:37,080 own thing so what I wanted to do is 3071 01:50:33,760 --> 01:50:38,760 maybe let's do motion how about and 3072 01:50:37,080 --> 01:50:40,440 let's go to a specific coordinate now 3073 01:50:38,760 --> 01:50:43,760 there's a lot of options here there's 3074 01:50:40,440 --> 01:50:46,400 turning go to a random position go to x 3075 01:50:43,760 --> 01:50:47,520 comma y Glide more elegantly there's a 3076 01:50:46,400 --> 01:50:49,360 lot of different ways to implement 3077 01:50:47,520 --> 01:50:50,840 movement I just wanted to go to a very 3078 01:50:49,360 --> 01:50:53,719 specific location first so I'm just 3079 01:50:50,840 --> 01:50:57,679 going to go to x comma y first and I'm 3080 01:50:53,719 --> 01:50:59,159 going to say x how about will be um uh 3081 01:50:57,679 --> 01:51:01,560 let's not hardcode this let's just have 3082 01:50:59,159 --> 01:51:04,199 it be well let's do it at zero initially 3083 01:51:01,560 --> 01:51:06,000 and then 240 so whoops let's do 0 comma 3084 01:51:04,199 --> 01:51:07,800 240 so that this piece of trash always 3085 01:51:06,000 --> 01:51:09,040 starts at the top middle of the screen 3086 01:51:07,800 --> 01:51:11,599 if you think back to that coordinate 3087 01:51:09,040 --> 01:51:13,719 system 0 0 is in the middle 240 is 3088 01:51:11,599 --> 01:51:15,920 straight above it all right now after I 3089 01:51:13,719 --> 01:51:18,560 do that what do I want to do well how 3090 01:51:15,920 --> 01:51:21,000 about I control this thing by forever 3091 01:51:18,560 --> 01:51:22,400 falling now how do I make the trash move 3092 01:51:21,000 --> 01:51:24,800 we haven't seen this puzzle piece yet 3093 01:51:22,400 --> 01:51:26,960 but under motion the very first thing is 3094 01:51:24,800 --> 01:51:28,520 called move some number of steps by 3095 01:51:26,960 --> 01:51:30,920 default it's 10 but we'll do it more 3096 01:51:28,520 --> 01:51:34,159 simply let me go ahead and 3097 01:51:30,920 --> 01:51:36,800 move uh oh sorry move is going to move 3098 01:51:34,159 --> 01:51:39,119 it uh in whatever Direction it's facing 3099 01:51:36,800 --> 01:51:40,400 I only want it to move down so here even 3100 01:51:39,119 --> 01:51:42,079 I'm getting confused as to how many 3101 01:51:40,400 --> 01:51:44,679 different ways there are to do things 3102 01:51:42,079 --> 01:51:48,119 what I think I want to do is this let me 3103 01:51:44,679 --> 01:51:49,560 only change my y AIS as follows so 3104 01:51:48,119 --> 01:51:52,280 here's another puzzle piece called 3105 01:51:49,560 --> 01:51:55,000 change y so again Y is the vertical so 3106 01:51:52,280 --> 01:51:58,000 let me just change y by one pixel 3107 01:51:55,000 --> 01:52:00,560 downward at a time so Nega one one pixel 3108 01:51:58,000 --> 01:52:02,639 at a time so it's kind of slow and I 3109 01:52:00,560 --> 01:52:05,119 think now I think that's it let me hit 3110 01:52:02,639 --> 01:52:06,280 stop notice that my trash can is still 3111 01:52:05,119 --> 01:52:08,239 going to be interactive I haven't 3112 01:52:06,280 --> 01:52:10,599 changed or deleted that code I've just 3113 01:52:08,239 --> 01:52:13,599 added now code for my trash piece of 3114 01:52:10,599 --> 01:52:16,719 trash if I click the green flag notice 3115 01:52:13,599 --> 01:52:18,679 that after I enable it let me start that 3116 01:52:16,719 --> 01:52:20,520 again I had it hidden for before class 3117 01:52:18,679 --> 01:52:23,360 but let me enable it now green flag 3118 01:52:20,520 --> 01:52:26,560 notice it starts dead center at xal 0 y 3119 01:52:23,360 --> 01:52:28,599 = 240 and it's dropping one pixel at a 3120 01:52:26,560 --> 01:52:30,639 time if that seems a little boring we 3121 01:52:28,599 --> 01:52:32,320 can change it to negative 10 pixels at a 3122 01:52:30,639 --> 01:52:34,079 time and boom it's done so that's how 3123 01:52:32,320 --> 01:52:35,280 you might change the speed of a program 3124 01:52:34,079 --> 01:52:37,440 but I'm going to leave it more simply as 3125 01:52:35,280 --> 01:52:39,400 negative 1 and honestly it would be nice 3126 01:52:37,440 --> 01:52:40,679 if it doesn't always start from the top 3127 01:52:39,400 --> 01:52:42,159 otherwise this game is not going to be 3128 01:52:40,679 --> 01:52:43,639 very interactive I'm literally going to 3129 01:52:42,159 --> 01:52:46,239 be grabbing the trash from the same 3130 01:52:43,639 --> 01:52:49,199 place every time so why don't I instead 3131 01:52:46,239 --> 01:52:51,239 stop this let me go under operators and 3132 01:52:49,199 --> 01:52:53,280 let's pick our random number so let me 3133 01:52:51,239 --> 01:52:55,280 change the hardcoded the manually 3134 01:52:53,280 --> 01:52:57,560 inputed zero and let's make X be 3135 01:52:55,280 --> 01:53:00,679 somewhere between zero so in the middle 3136 01:52:57,560 --> 01:53:04,000 and all the way over to uh what was it 3137 01:53:00,679 --> 01:53:06,880 one oh I got my numbers wrong 240 and my 3138 01:53:04,000 --> 01:53:10,079 y will be 180 sorry I got my X and my my 3139 01:53:06,880 --> 01:53:11,719 y confused so let me play this again and 3140 01:53:10,079 --> 01:53:13,199 now we have a game that's more like 3141 01:53:11,719 --> 01:53:14,960 games you might have played growing up 3142 01:53:13,199 --> 01:53:16,960 or even now like there's some Randomness 3143 01:53:14,960 --> 01:53:18,560 to it so the CPU so to speak is doing 3144 01:53:16,960 --> 01:53:20,239 something more interesting let me run it 3145 01:53:18,560 --> 01:53:21,840 again now it's a little to the left let 3146 01:53:20,239 --> 01:53:23,719 me run it again now it's a little more 3147 01:53:21,840 --> 01:53:25,320 to the left again now it's back to the 3148 01:53:23,719 --> 01:53:26,520 right so Randomness just makes games 3149 01:53:25,320 --> 01:53:28,119 more interesting and this is why when 3150 01:53:26,520 --> 01:53:29,560 you play any video game if different 3151 01:53:28,119 --> 01:53:31,360 things are happening there's probably 3152 01:53:29,560 --> 01:53:33,360 just some Randomness and it's quantized 3153 01:53:31,360 --> 01:53:35,920 is just a simple number now I think I 3154 01:53:33,360 --> 01:53:38,880 just need one final flourish here if I 3155 01:53:35,920 --> 01:53:42,679 may let me go ahead and add this how 3156 01:53:38,880 --> 01:53:44,840 about uh events or rather yes events 3157 01:53:42,679 --> 01:53:47,040 when green flag is clicked I can do 3158 01:53:44,840 --> 01:53:48,599 multiple things within the same Sprite 3159 01:53:47,040 --> 01:53:51,920 they don't all have to be attached to 3160 01:53:48,599 --> 01:53:55,239 the same one let me go ahead and forever 3161 01:53:51,920 --> 01:54:00,119 go ahead and do something else how about 3162 01:53:55,239 --> 01:54:03,920 whenever the trash is how about touching 3163 01:54:00,119 --> 01:54:07,239 the trash can so forever if let's see I 3164 01:54:03,920 --> 01:54:09,599 need a sensing block so how about is 3165 01:54:07,239 --> 01:54:13,159 touching uh not the mouse pointer this 3166 01:54:09,599 --> 01:54:15,719 time but touching Oscar himself there 3167 01:54:13,159 --> 01:54:18,199 now let's see what happens all right so 3168 01:54:15,719 --> 01:54:21,239 let's go ahead and click the green flag 3169 01:54:18,199 --> 01:54:24,159 now I go down over here and let 3170 01:54:21,239 --> 01:54:26,800 go okay that's I kind of want it to go 3171 01:54:24,159 --> 01:54:28,719 into the trash can how do I make it go 3172 01:54:26,800 --> 01:54:31,320 into the trash can how can we take this 3173 01:54:28,719 --> 01:54:33,480 high level idea put trash into the trash 3174 01:54:31,320 --> 01:54:37,400 can and make it seem to 3175 01:54:33,480 --> 01:54:39,520 disappear logically what could we do 3176 01:54:37,400 --> 01:54:41,520 yeah okay so when it touches it let's 3177 01:54:39,520 --> 01:54:42,639 have it disappear so I could hide it or 3178 01:54:41,520 --> 01:54:44,840 honestly if the game's going to be 3179 01:54:42,639 --> 01:54:46,679 ongoing like it was letting me Mo drop 3180 01:54:44,840 --> 01:54:48,760 more and more trash let me just have it 3181 01:54:46,679 --> 01:54:51,239 go ahead and pick a new random location 3182 01:54:48,760 --> 01:54:52,800 so let me do this let me go ahead and 3183 01:54:51,239 --> 01:54:54,560 copy this puzzle piece up here and 3184 01:54:52,800 --> 01:54:56,840 duplicate and I don't want the whole 3185 01:54:54,560 --> 01:54:59,360 thing sorry let me get rid of this let 3186 01:54:56,840 --> 01:55:01,800 me just do this let me go back to some 3187 01:54:59,360 --> 01:55:03,440 random location at the top so now notice 3188 01:55:01,800 --> 01:55:06,639 what happens if I click and drag on it 3189 01:55:03,440 --> 01:55:08,400 here it goes and I let go it looks like 3190 01:55:06,639 --> 01:55:10,800 it's going into the trash can because it 3191 01:55:08,400 --> 01:55:12,040 snaps back up to some random location 3192 01:55:10,800 --> 01:55:14,040 now the only thing I'm not doing really 3193 01:55:12,040 --> 01:55:16,199 is keeping track of any kind of score 3194 01:55:14,040 --> 01:55:17,719 and it turns out if I full screen this 3195 01:55:16,199 --> 01:55:19,880 it's not going to be draggable by 3196 01:55:17,719 --> 01:55:21,360 default so just as a corner case so to 3197 01:55:19,880 --> 01:55:25,040 speak something that you might trip over 3198 01:55:21,360 --> 01:55:28,719 otherwise let me go ahead and under uh 3199 01:55:25,040 --> 01:55:30,639 let's see uh sensing it turns out I also 3200 01:55:28,719 --> 01:55:33,040 need this for the piece of trash there's 3201 01:55:30,639 --> 01:55:35,199 this way of setting and scratch a puzzle 3202 01:55:33,040 --> 01:55:37,159 a Sprite to be draggable or not 3203 01:55:35,199 --> 01:55:39,000 draggable I need to explicitly make it 3204 01:55:37,159 --> 01:55:40,920 draggable so that when I do full screen 3205 01:55:39,000 --> 01:55:43,119 this thing now it Still Remains 3206 01:55:40,920 --> 01:55:45,199 draggable and someone like myself can 3207 01:55:43,119 --> 01:55:46,760 play it again and again well how about 3208 01:55:45,199 --> 01:55:49,079 we supplement this with one final 3209 01:55:46,760 --> 01:55:52,480 flourish why don't we keep track now of 3210 01:55:49,079 --> 01:55:54,880 the user score so how about when the 3211 01:55:52,480 --> 01:55:56,599 user actually drags the piece of trash 3212 01:55:54,880 --> 01:55:58,440 to the trash can let me go under 3213 01:55:56,599 --> 01:56:00,280 variables here where in advance I've 3214 01:55:58,440 --> 01:56:02,679 already made myself a variable called 3215 01:56:00,280 --> 01:56:04,440 score I could have called it X or Y or Z 3216 01:56:02,679 --> 01:56:06,040 or ABC but that's not very descriptive 3217 01:56:04,440 --> 01:56:08,040 in programming you typically give things 3218 01:56:06,040 --> 01:56:10,560 a more descriptive English or some other 3219 01:56:08,040 --> 01:56:13,119 language name so I called this one score 3220 01:56:10,560 --> 01:56:15,760 so how do I want to do this in my score 3221 01:56:13,119 --> 01:56:18,639 well let me go ahead and initially set 3222 01:56:15,760 --> 01:56:20,040 this game score to zero at the very top 3223 01:56:18,639 --> 01:56:22,159 of one of these Scripts 3224 01:56:20,040 --> 01:56:26,320 or one of these programs up here and 3225 01:56:22,159 --> 01:56:28,880 then anytime my cursor my piece of trash 3226 01:56:26,320 --> 01:56:33,239 is touching Oscar let's not just jump to 3227 01:56:28,880 --> 01:56:35,880 the top let's change the score by one up 3228 01:56:33,239 --> 01:56:37,800 here so now notice if touching Oscar 3229 01:56:35,880 --> 01:56:39,639 change the score that is add one to the 3230 01:56:37,800 --> 01:56:43,119 score and then pick a new random 3231 01:56:39,639 --> 01:56:45,239 location and now green flag let's do 3232 01:56:43,119 --> 01:56:48,119 this slowly here it goes it's the trash 3233 01:56:45,239 --> 01:56:49,960 can opens I let go and now notice at the 3234 01:56:48,119 --> 01:56:52,480 top left of my program notice the score 3235 01:56:49,960 --> 01:56:55,119 is now two notice the score if I do this 3236 01:56:52,480 --> 01:56:57,199 again is about to become three and so 3237 01:56:55,119 --> 01:56:59,599 here we have building blocks literally 3238 01:56:57,199 --> 01:57:01,119 of making this program better and better 3239 01:56:59,599 --> 01:57:02,920 and better and so indeed that's how you 3240 01:57:01,119 --> 01:57:05,119 generally approach solving any large 3241 01:57:02,920 --> 01:57:07,079 program uh any problem with code be it 3242 01:57:05,119 --> 01:57:09,040 in scratch or C or python or some other 3243 01:57:07,079 --> 01:57:10,320 you take this Vision you might have or 3244 01:57:09,040 --> 01:57:12,280 some Vision you've been assigned in a 3245 01:57:10,320 --> 01:57:13,920 homework assignment and try to break it 3246 01:57:12,280 --> 01:57:16,159 down into these constituent parts and 3247 01:57:13,920 --> 01:57:17,679 just pluck off the easy ones first put 3248 01:57:16,159 --> 01:57:19,480 the lamp post there first and at least 3249 01:57:17,679 --> 01:57:20,760 feel like you're making some progress 3250 01:57:19,480 --> 01:57:22,800 then pluck off something like the trash 3251 01:57:20,760 --> 01:57:24,119 can and just make it do a little thing 3252 01:57:22,800 --> 01:57:25,840 and it doesn't have to be in some same 3253 01:57:24,119 --> 01:57:28,119 order here I could have done this in a 3254 01:57:25,840 --> 01:57:30,239 million different ways but figure out 3255 01:57:28,119 --> 01:57:31,679 what the small pieces are that 3256 01:57:30,239 --> 01:57:33,920 ultimately like a few of the problems 3257 01:57:31,679 --> 01:57:37,560 we've solved today assemble into a 3258 01:57:33,920 --> 01:57:39,320 greater solution there too uh so that 3259 01:57:37,560 --> 01:57:41,440 you have now a mental model for these 3260 01:57:39,320 --> 01:57:44,000 types of blocks and others let's return 3261 01:57:41,440 --> 01:57:46,560 for a moment to this we saw a moment ago 3262 01:57:44,000 --> 01:57:48,840 that when I started saying hello David 3263 01:57:46,560 --> 01:57:51,320 and nesting those puzzle pieces we had a 3264 01:57:48,840 --> 01:57:53,360 whole different Paradigm Al together my 3265 01:57:51,320 --> 01:57:55,440 input for that second version of hello 3266 01:57:53,360 --> 01:57:58,639 world was to now pass in for instance 3267 01:57:55,440 --> 01:58:01,360 what's your name into my function called 3268 01:57:58,639 --> 01:58:03,880 ask that gave me not a side effect but 3269 01:58:01,360 --> 01:58:06,360 what I called again a return value 3270 01:58:03,880 --> 01:58:09,159 called answer by default in scratch and 3271 01:58:06,360 --> 01:58:13,239 now notice and recall when I had that 3272 01:58:09,159 --> 01:58:15,119 same output become the input to my next 3273 01:58:13,239 --> 01:58:18,599 block it looked a little something like 3274 01:58:15,119 --> 01:58:20,560 this say so how does this type of block 3275 01:58:18,599 --> 01:58:22,599 and this nesting the stacking of blocks 3276 01:58:20,560 --> 01:58:24,560 fit into the same mental model well same 3277 01:58:22,599 --> 01:58:28,159 idea my input for that part of the story 3278 01:58:24,560 --> 01:58:30,920 is now taking in not one input but two 3279 01:58:28,159 --> 01:58:33,079 two arguments hello and the answer from 3280 01:58:30,920 --> 01:58:35,440 before the function in this case is that 3281 01:58:33,079 --> 01:58:38,159 new block called join the output thereof 3282 01:58:35,440 --> 01:58:42,000 is hello David which itself became if we 3283 01:58:38,159 --> 01:58:44,560 sort of animate this the input to my 3284 01:58:42,000 --> 01:58:47,079 final function which indeed was still 3285 01:58:44,560 --> 01:58:49,360 say and this is only to say no pun 3286 01:58:47,079 --> 01:58:51,040 intended that almost everything that 3287 01:58:49,360 --> 01:58:53,639 that you do with these puzzle pieces be 3288 01:58:51,040 --> 01:58:55,199 in the context of Oscar time or the mole 3289 01:58:53,639 --> 01:58:57,119 whacking or even just something simple 3290 01:58:55,199 --> 01:59:00,199 like hello world will ultimately fit 3291 01:58:57,119 --> 01:59:02,360 into that relatively simple mental model 3292 01:59:00,199 --> 01:59:04,560 there now I thought we'd End by taking a 3293 01:59:02,360 --> 01:59:06,520 look at just a couple final examples 3294 01:59:04,560 --> 01:59:08,679 these ones two made by some of your 3295 01:59:06,520 --> 01:59:11,000 predecessors and for this I thought we 3296 01:59:08,679 --> 01:59:13,520 would not write code together but read 3297 01:59:11,000 --> 01:59:16,239 it instead and so allow me to open up 3298 01:59:13,520 --> 01:59:19,320 one other example here that will show us 3299 01:59:16,239 --> 01:59:21,800 a few different versions of a program 3300 01:59:19,320 --> 01:59:24,159 that a predecessor made give me just a 3301 01:59:21,800 --> 01:59:26,400 moment here and we'll see how we might 3302 01:59:24,159 --> 01:59:28,639 build up to something even more 3303 01:59:26,400 --> 01:59:30,960 interactive and in just a moment we'll 3304 01:59:28,639 --> 01:59:34,119 see something they called Ivy's uh 3305 01:59:30,960 --> 01:59:36,880 hardest game focused here on these 3306 01:59:34,119 --> 01:59:39,280 particular mechanics so here is version 3307 01:59:36,880 --> 01:59:40,800 zero so to speak of this program where 3308 01:59:39,280 --> 01:59:42,360 in the goal is to create a game where 3309 01:59:40,800 --> 01:59:44,000 you have to like get out of some kind of 3310 01:59:42,360 --> 01:59:46,280 Maze and you have to get out in this 3311 01:59:44,000 --> 01:59:47,840 case the Harvard Crest from this maze 3312 01:59:46,280 --> 01:59:49,599 let me go ahead and just hit play on 3313 01:59:47,840 --> 01:59:51,040 this green flag so you can see what what 3314 01:59:49,599 --> 01:59:53,719 the first building block for this 3315 01:59:51,040 --> 01:59:55,920 program might have been notice that my 3316 01:59:53,719 --> 01:59:58,679 hand here is actually on the arrow keys 3317 01:59:55,920 --> 02:00:01,639 of my keyboard and it seems that by 3318 01:59:58,679 --> 02:00:04,119 moving up down left or right this little 3319 02:00:01,639 --> 02:00:06,119 Crest on the screen responds in exactly 3320 02:00:04,119 --> 02:00:07,480 that way now let's hypothesize for just 3321 02:00:06,119 --> 02:00:09,639 a moment even though we've not done 3322 02:00:07,480 --> 02:00:12,040 anything quite like this before how 3323 02:00:09,639 --> 02:00:15,079 might this code be implemented how do 3324 02:00:12,040 --> 02:00:17,719 you get a Sprite be it a cat or a Crest 3325 02:00:15,079 --> 02:00:19,599 to respond to keys on a keyboard might 3326 02:00:17,719 --> 02:00:21,440 you think intuitively yeah there could 3327 02:00:19,599 --> 02:00:22,960 be something that's sensing what key 3328 02:00:21,440 --> 02:00:24,159 you're pressing on yeah there could be 3329 02:00:22,960 --> 02:00:25,599 something sensing what key you're 3330 02:00:24,159 --> 02:00:27,800 pressing on and if you do it again in a 3331 02:00:25,599 --> 02:00:29,400 forever Loop you'll just constantly be 3332 02:00:27,800 --> 02:00:31,159 listening for keystrokes and this is how 3333 02:00:29,400 --> 02:00:33,040 like every piece of software nowadays 3334 02:00:31,159 --> 02:00:34,840 works it's constantly waiting for your 3335 02:00:33,040 --> 02:00:36,840 phone to be tapped or something to be 3336 02:00:34,840 --> 02:00:39,400 typed on the screen so let me go ahead 3337 02:00:36,840 --> 02:00:41,159 and look inside of this existing program 3338 02:00:39,400 --> 02:00:43,239 here and there's more going on but we'll 3339 02:00:41,159 --> 02:00:45,960 take a quick glance what's actually 3340 02:00:43,239 --> 02:00:48,639 going on well up here at top left notice 3341 02:00:45,960 --> 02:00:50,000 we just have go to x equals 0 and Y 3342 02:00:48,639 --> 02:00:51,639 equals 0 that means put the Harvard 3343 02:00:50,000 --> 02:00:54,480 Crest dead center in the middle of the 3344 02:00:51,639 --> 02:00:56,360 stage then we have forever two functions 3345 02:00:54,480 --> 02:00:59,079 that we made in advance as custom 3346 02:00:56,360 --> 02:01:01,159 functions uh listen for keyboard feel 3347 02:00:59,079 --> 02:01:03,040 for walls so it's doing two things at 3348 02:01:01,159 --> 02:01:05,079 once it's forever listening for the 3349 02:01:03,040 --> 02:01:06,480 keyboard up down left right and feeling 3350 02:01:05,079 --> 02:01:08,480 for the walls in the sense that if I get 3351 02:01:06,480 --> 02:01:10,360 too far to the left I don't want it to 3352 02:01:08,480 --> 02:01:11,639 keep moving past that black wall and if 3353 02:01:10,360 --> 02:01:13,520 it moves too far to the right I don't 3354 02:01:11,639 --> 02:01:14,920 want it to blow through that wall either 3355 02:01:13,520 --> 02:01:16,840 so it's going to do two things 3356 02:01:14,920 --> 02:01:19,159 constantly listening for keyboard and 3357 02:01:16,840 --> 02:01:20,880 feeling for walls so to speak and how 3358 02:01:19,159 --> 02:01:23,239 are those implemented well this one's a 3359 02:01:20,880 --> 02:01:26,800 bit long but on the left here is listen 3360 02:01:23,239 --> 02:01:29,360 for keyboard so this pink puzzle piece 3361 02:01:26,800 --> 02:01:31,719 listen for keyboard first checks if the 3362 02:01:29,360 --> 02:01:33,679 key up arrow is pressed question mark 3363 02:01:31,719 --> 02:01:35,800 Boolean expression in a conditional 3364 02:01:33,679 --> 02:01:38,719 change y by one that means move it up 3365 02:01:35,800 --> 02:01:41,159 one else if the key down arrow is 3366 02:01:38,719 --> 02:01:43,199 pressed then change y by negative one 3367 02:01:41,159 --> 02:01:44,800 and similar for left Arrow similar for 3368 02:01:43,199 --> 02:01:47,719 right arrow and even though there's not 3369 02:01:44,800 --> 02:01:49,639 a loop in this pink function there is 3370 02:01:47,719 --> 02:01:51,679 where I'm using it so it's constantly 3371 02:01:49,639 --> 02:01:53,639 being asked again and again how about 3372 02:01:51,679 --> 02:01:56,400 feeling for walls well over here to the 3373 02:01:53,639 --> 02:01:59,239 right it's a little cut off but here you 3374 02:01:56,400 --> 02:02:01,679 have if touching left wall change X by 3375 02:01:59,239 --> 02:02:02,880 one so if you hit the wall it's too late 3376 02:02:01,679 --> 02:02:05,119 you're kind of blowing through it 3377 02:02:02,880 --> 02:02:06,920 already so I want to move it wh one 3378 02:02:05,119 --> 02:02:08,719 pixel so it's no longer touching that 3379 02:02:06,920 --> 02:02:10,520 wall similarly if it's touching the 3380 02:02:08,719 --> 02:02:12,239 right wall I want to back it up one 3381 02:02:10,520 --> 02:02:13,880 pixel so it's no longer touching that 3382 02:02:12,239 --> 02:02:16,040 wall so it's kind of like bouncing off 3383 02:02:13,880 --> 02:02:17,960 ever so slightly so that it doesn't slip 3384 02:02:16,040 --> 02:02:20,520 through that actual wall and what are 3385 02:02:17,960 --> 02:02:23,199 those walls well noce down here it's 3386 02:02:20,520 --> 02:02:24,840 just a simple Sprite with a black line 3387 02:02:23,199 --> 02:02:26,599 that I've oriented hor vertically 3388 02:02:24,840 --> 02:02:28,360 instead of horizontally and that's just 3389 02:02:26,599 --> 02:02:30,840 so that I can ask questions of these 3390 02:02:28,360 --> 02:02:32,719 other two Sprites now that gives me that 3391 02:02:30,840 --> 02:02:34,520 form of interactivity what more can I 3392 02:02:32,719 --> 02:02:37,119 now do well what if we make things a 3393 02:02:34,520 --> 02:02:39,320 little more interactive here let me go 3394 02:02:37,119 --> 02:02:41,840 ahead and see inside version one our 3395 02:02:39,320 --> 02:02:44,560 second and let me propose what's going 3396 02:02:41,840 --> 02:02:47,159 to happen here well how might we add a 3397 02:02:44,560 --> 02:02:49,079 little something like Yale into the mix 3398 02:02:47,159 --> 02:02:51,159 well what's Yale going to do when I I 3399 02:02:49,079 --> 02:02:53,320 hit the green flag now based on this 3400 02:02:51,159 --> 02:02:56,840 code any 3401 02:02:53,320 --> 02:02:59,960 hunches here is the code for my Yale 3402 02:02:56,840 --> 02:03:02,040 Sprite yeah yeah it's kind of got to be 3403 02:02:59,960 --> 02:03:03,440 an adversary by blocking my path 3404 02:03:02,040 --> 02:03:05,840 theoretically if I keep writing more 3405 02:03:03,440 --> 02:03:08,199 code so why it to goes to the middle of 3406 02:03:05,840 --> 02:03:09,960 the screen it points in Direction 90 de 3407 02:03:08,199 --> 02:03:12,040 so similarly there's a whole degree 3408 02:03:09,960 --> 02:03:14,480 system as well and it forever asks this 3409 02:03:12,040 --> 02:03:16,840 if touching the left wall or notice the 3410 02:03:14,480 --> 02:03:19,239 green block touching the right wall then 3411 02:03:16,840 --> 02:03:20,520 just turn around 180° and in if you 3412 02:03:19,239 --> 02:03:22,599 think this through logically that just 3413 02:03:20,520 --> 02:03:24,679 means you're bouncing this way and this 3414 02:03:22,599 --> 02:03:27,880 way by just flipping yourself around 3415 02:03:24,679 --> 02:03:29,560 180° for just this Yale Sprite so if I 3416 02:03:27,880 --> 02:03:32,119 go ahead and zoom in on this and click 3417 02:03:29,560 --> 02:03:34,719 the green flag I can still move up and 3418 02:03:32,119 --> 02:03:36,880 down but Yale is just kind of doing this 3419 02:03:34,719 --> 02:03:38,920 all day long back and forth and back and 3420 02:03:36,880 --> 02:03:41,000 forth forever nothing bad happens if I 3421 02:03:38,920 --> 02:03:42,960 try to go through it but we could add 3422 02:03:41,000 --> 02:03:45,000 that certainly to the mix in fact let's 3423 02:03:42,960 --> 02:03:47,119 add one final feature before we play 3424 02:03:45,000 --> 02:03:49,719 this particular game and let me go ahead 3425 02:03:47,119 --> 02:03:53,119 and open up the final version of these 3426 02:03:49,719 --> 02:03:54,400 building blocks that adds MIT to the mix 3427 02:03:53,119 --> 02:03:57,320 so here's 3428 02:03:54,400 --> 02:03:58,760 MIT someone want to explain what this 3429 02:03:57,320 --> 02:04:00,639 code does and this is what we're doing 3430 02:03:58,760 --> 02:04:02,559 this itself is a skill reading someone 3431 02:04:00,639 --> 02:04:04,639 else's code and understanding it is half 3432 02:04:02,559 --> 02:04:08,000 of the part of programming besides 3433 02:04:04,639 --> 02:04:09,559 writing yeah yeah it's chasing down the 3434 02:04:08,000 --> 02:04:11,360 Harvard logo outline so this is 3435 02:04:09,559 --> 02:04:13,800 apparently the name of the costume that 3436 02:04:11,360 --> 02:04:15,719 this student made Harvard logo outline 3437 02:04:13,800 --> 02:04:17,440 outline and apparently it goes to a 3438 02:04:15,719 --> 02:04:19,880 random position first but then it 3439 02:04:17,440 --> 02:04:22,159 forever points to Har so no matter where 3440 02:04:19,880 --> 02:04:23,400 I'm moving it up down left or right MIT 3441 02:04:22,159 --> 02:04:25,079 is being a little more strategic than 3442 02:04:23,400 --> 02:04:26,880 Yale bouncing back and forth like this 3443 02:04:25,079 --> 02:04:29,079 so let's go ahead and play this one in 3444 02:04:26,880 --> 02:04:32,040 full screen and here we have a green 3445 02:04:29,079 --> 02:04:36,280 flag so if I move up 3446 02:04:32,040 --> 02:04:38,320 MIT rather strategically is following me 3447 02:04:36,280 --> 02:04:40,800 no matter where I go all right so still 3448 02:04:38,320 --> 02:04:42,280 nothing bad happens but and now it's 3449 02:04:40,800 --> 02:04:44,320 struggling right it's going up down up 3450 02:04:42,280 --> 02:04:46,040 down it's trying to uh follow me even 3451 02:04:44,320 --> 02:04:48,480 though I'm not moving so we need some 3452 02:04:46,040 --> 02:04:51,280 final flourishes and so I think for this 3453 02:04:48,480 --> 02:04:53,559 we need perhaps one final volunteer 3454 02:04:51,280 --> 02:04:55,440 after this cake awaits for everyone 3455 02:04:53,559 --> 02:04:57,000 outside as is an end of first lecture 3456 02:04:55,440 --> 02:05:00,400 cs50 tradition would you like to come up 3457 02:04:57,000 --> 02:05:00,400 and be our our vinyl of 3458 02:05:06,800 --> 02:05:11,599 volunteer all right and so this will be 3459 02:05:09,360 --> 02:05:13,639 the actual version bit written by one of 3460 02:05:11,599 --> 02:05:15,880 your predecessors that I'll full screen 3461 02:05:13,639 --> 02:05:17,679 here it's going to stitch together all 3462 02:05:15,880 --> 02:05:20,079 of these same Primitives in more but add 3463 02:05:17,679 --> 02:05:22,000 the notion of score and lives so that 3464 02:05:20,079 --> 02:05:24,440 there's actually a goal which in this 3465 02:05:22,000 --> 02:05:26,360 case is to move the Harvard Crest to 3466 02:05:24,440 --> 02:05:27,960 constantly pursue the character on the 3467 02:05:26,360 --> 02:05:29,400 right hand side so that your Sprite 3468 02:05:27,960 --> 02:05:31,559 touches that one would you like to 3469 02:05:29,400 --> 02:05:33,559 introduce yourself uh hi my name is 3470 02:05:31,559 --> 02:05:35,079 Muhammad all right wonderful welcome 3471 02:05:33,559 --> 02:05:37,119 aboard and here we come with some 3472 02:05:35,079 --> 02:05:38,639 instructions and final flourish if we 3473 02:05:37,119 --> 02:05:41,239 want to keep the lights up but perhaps 3474 02:05:38,639 --> 02:05:41,239 increase the 3475 02:05:47,679 --> 02:06:08,250 music 3476 02:05:49,610 --> 02:06:08,250 [Music] 3477 02:06:17,599 --> 02:06:20,599 e 3478 02:06:22,120 --> 02:06:25,189 [Music] 3479 02:06:32,030 --> 02:06:45,399 [Music] 3480 02:06:47,599 --> 02:06:50,599 you 3481 02:06:51,320 --> 02:07:03,520 [Music] 3482 02:07:00,159 --> 02:07:05,760 all right this is cs50 and this is week 3483 02:07:03,520 --> 02:07:07,760 one wherein we continue programming but 3484 02:07:05,760 --> 02:07:10,000 we do it in a different language because 3485 02:07:07,760 --> 02:07:11,960 recall last time we focused on this 3486 02:07:10,000 --> 02:07:13,920 graphical language called scratch but we 3487 02:07:11,960 --> 02:07:15,280 use scratch uh not only because it's 3488 02:07:13,920 --> 02:07:17,440 sort of fun and accessible but because 3489 02:07:15,280 --> 02:07:19,520 it allows us to explore a lot of these 3490 02:07:17,440 --> 02:07:21,760 Concepts here Nam functions and 3491 02:07:19,520 --> 02:07:24,679 conditionals Boolean Expressions Loops 3492 02:07:21,760 --> 02:07:27,040 variables and more and so indeed even if 3493 02:07:24,679 --> 02:07:29,559 today's syntax as we transition to this 3494 02:07:27,040 --> 02:07:31,360 new language called C feels a little bit 3495 02:07:29,559 --> 02:07:33,520 cryptic maybe a little intimidating at 3496 02:07:31,360 --> 02:07:35,440 first and you don't quite see all of the 3497 02:07:33,520 --> 02:07:37,400 meaning of the symbols beyond the syntax 3498 02:07:35,440 --> 02:07:39,760 itself realized that the ideas are 3499 02:07:37,400 --> 02:07:42,639 ultimately going to be the same in fact 3500 02:07:39,760 --> 02:07:44,639 as we transition from what was last week 3501 02:07:42,639 --> 02:07:46,920 a hello world program that looked a 3502 02:07:44,639 --> 02:07:48,119 little something like this this week of 3503 02:07:46,920 --> 02:07:49,679 course it's going to now look a little 3504 02:07:48,119 --> 02:07:51,679 more cryptic it's going to look a little 3505 02:07:49,679 --> 02:07:53,559 something like this and now even if you 3506 02:07:51,679 --> 02:07:55,480 can't quite distinguish what all of the 3507 02:07:53,559 --> 02:07:56,719 various symbols mean in this code turns 3508 02:07:55,480 --> 02:07:58,440 out that at the end of the day it's 3509 02:07:56,719 --> 02:08:00,360 indeed going to do what you expect it's 3510 02:07:58,440 --> 02:08:02,280 just going to say hello world on the 3511 02:08:00,360 --> 02:08:04,199 screen just like we did in scratch so 3512 02:08:02,280 --> 02:08:06,880 let's start to apply some terminology to 3513 02:08:04,199 --> 02:08:08,239 these to these uh tokens first so what 3514 02:08:06,880 --> 02:08:09,480 we're about to see what we're about to 3515 02:08:08,239 --> 02:08:11,679 write henceforth we're going to start 3516 02:08:09,480 --> 02:08:13,239 calling source code code that you the 3517 02:08:11,679 --> 02:08:14,880 human programmer write is just 3518 02:08:13,239 --> 02:08:16,440 henceforth called source code doesn't 3519 02:08:14,880 --> 02:08:18,000 matter if it's scratch doesn't matter if 3520 02:08:16,440 --> 02:08:19,040 it's C doesn't matter if it's python 3521 02:08:18,000 --> 02:08:20,480 before 3522 02:08:19,040 --> 02:08:21,840 source code is the general term for 3523 02:08:20,480 --> 02:08:24,239 really what you and I as human 3524 02:08:21,840 --> 02:08:27,760 programmers will ultimately write of 3525 02:08:24,239 --> 02:08:29,599 course computers don't understand source 3526 02:08:27,760 --> 02:08:31,480 code it turns out computers don't 3527 02:08:29,599 --> 02:08:33,760 understand scratch and puzzle pieces per 3528 02:08:31,480 --> 02:08:35,159 se or C code like we're about to see 3529 02:08:33,760 --> 02:08:38,079 they only understand this which we 3530 02:08:35,159 --> 02:08:40,239 called what last week yeah so this is 3531 02:08:38,079 --> 02:08:43,079 binary zeros and ones but really it's 3532 02:08:40,239 --> 02:08:45,520 just information represented in binary 3533 02:08:43,079 --> 02:08:47,159 and in fact the technical term now for 3534 02:08:45,520 --> 02:08:48,400 patterns of zeros and ones that a 3535 02:08:47,159 --> 02:08:50,719 computer not only understands 3536 02:08:48,400 --> 02:08:53,360 understands how to interpret as letters 3537 02:08:50,719 --> 02:08:55,559 or numbers or colors or images or more 3538 02:08:53,360 --> 02:08:57,719 but knows how to execute as well 3539 02:08:55,559 --> 02:08:59,559 henceforth is going to be called machine 3540 02:08:57,719 --> 02:09:00,920 code to contrast it with source code so 3541 02:08:59,559 --> 02:09:02,920 whereas you and I the humans write 3542 02:09:00,920 --> 02:09:05,480 source code it's the computer that 3543 02:09:02,920 --> 02:09:06,719 ultimately only understands machine code 3544 02:09:05,480 --> 02:09:08,599 and even though we won't get into the 3545 02:09:06,719 --> 02:09:11,800 details of exactly what pattern of 3546 02:09:08,599 --> 02:09:14,159 symbols means what you'll see that in 3547 02:09:11,800 --> 02:09:15,360 this kind of pattern of zeros and ones 3548 02:09:14,159 --> 02:09:16,679 there's going to be numbers there's 3549 02:09:15,360 --> 02:09:18,400 going to be letters but there's also 3550 02:09:16,679 --> 02:09:19,760 going to be instructions because indeed 3551 02:09:18,400 --> 02:09:21,480 computers are really good at doing 3552 02:09:19,760 --> 02:09:23,679 things addition subtraction moving 3553 02:09:21,480 --> 02:09:26,119 things in and out of memory and suffice 3554 02:09:23,679 --> 02:09:27,719 it to say that the Macs the PCS the 3555 02:09:26,119 --> 02:09:30,000 other computers of the world have just 3556 02:09:27,719 --> 02:09:32,159 decided as a society what certain 3557 02:09:30,000 --> 02:09:34,360 patterns of zeros and ones mean when it 3558 02:09:32,159 --> 02:09:36,800 comes to operations as well so not just 3559 02:09:34,360 --> 02:09:37,960 data but instructions but those patterns 3560 02:09:36,800 --> 02:09:39,239 are not something we're going to focus 3561 02:09:37,960 --> 02:09:41,480 on in a class like this we're going to 3562 02:09:39,239 --> 02:09:43,559 focus on the higher level software side 3563 02:09:41,480 --> 02:09:46,040 of things simply assuming that we need 3564 02:09:43,559 --> 02:09:48,800 to somehow output machine code so it 3565 02:09:46,040 --> 02:09:50,360 turns out then that this problem we have 3566 02:09:48,800 --> 02:09:52,079 to solve getting from source code to 3567 02:09:50,360 --> 02:09:54,840 machine code actually fits into the same 3568 02:09:52,079 --> 02:09:57,079 Paradigm as last time but the input in 3569 02:09:54,840 --> 02:09:58,880 this case is going to be source code on 3570 02:09:57,079 --> 02:10:00,679 the one hand like that's what you and I 3571 02:09:58,880 --> 02:10:02,840 ideally will write so that we don't have 3572 02:10:00,679 --> 02:10:04,840 to write zeros and ones but we need to 3573 02:10:02,840 --> 02:10:06,800 somehow output machine code because 3574 02:10:04,840 --> 02:10:08,679 that's what your Macs PCS phones are 3575 02:10:06,800 --> 02:10:10,320 actually going to understand well it 3576 02:10:08,679 --> 02:10:13,119 turns out there's special programs in 3577 02:10:10,320 --> 02:10:15,199 life whose purpose is to do exactly this 3578 02:10:13,119 --> 02:10:17,199 conversion convert the source code you 3579 02:10:15,199 --> 02:10:19,400 and I write to the machine code that our 3580 02:10:17,199 --> 02:10:21,840 phones and computers understand and that 3581 02:10:19,400 --> 02:10:24,360 type of program is going to be called a 3582 02:10:21,840 --> 02:10:26,079 compiler so indeed today we'll introduce 3583 02:10:24,360 --> 02:10:28,079 you to another piece of software and 3584 02:10:26,079 --> 02:10:30,280 these come in many forms we'll use a 3585 02:10:28,079 --> 02:10:33,960 popular one here that allows you to 3586 02:10:30,280 --> 02:10:36,360 convert source code in C to machine code 3587 02:10:33,960 --> 02:10:37,719 in uh zeros and ones now you didn't have 3588 02:10:36,360 --> 02:10:39,239 to do this with scratch in the world of 3589 02:10:37,719 --> 02:10:41,679 scratch it was as simple as clicking the 3590 02:10:39,239 --> 02:10:43,639 green flag because essentially MIT did 3591 02:10:41,679 --> 02:10:45,040 all of the heavy lifting there figuring 3592 02:10:43,639 --> 02:10:47,000 out how to convert these graphical 3593 02:10:45,040 --> 02:10:48,960 puzzle pieces to the underlying machine 3594 02:10:47,000 --> 02:10:50,719 code but now starting today as we begin 3595 02:10:48,960 --> 02:10:52,880 to study programming and computer 3596 02:10:50,719 --> 02:10:54,719 science proper now that power moves to 3597 02:10:52,880 --> 02:10:57,360 you and it's up to you now to do that 3598 02:10:54,719 --> 02:10:59,040 kind of conversion but thankfully the 3599 02:10:57,360 --> 02:11:01,040 fact that these compilers exist means 3600 02:10:59,040 --> 02:11:03,159 that you and I don't have to program in 3601 02:11:01,040 --> 02:11:05,599 machine code like our ancestors Once 3602 02:11:03,159 --> 02:11:07,199 Upon a Time did be it virtually or with 3603 02:11:05,599 --> 02:11:08,880 physical Punch Cards like pieces of 3604 02:11:07,199 --> 02:11:12,679 paper with holes in them you and I get 3605 02:11:08,880 --> 02:11:14,239 to focus uh on our keyboard as such but 3606 02:11:12,679 --> 02:11:16,599 it's not just going to be a matter today 3607 02:11:14,239 --> 02:11:18,760 of like writing code it's going to be a 3608 02:11:16,599 --> 02:11:20,360 matter ultimately today onward of good 3609 02:11:18,760 --> 02:11:21,280 code as well and this is the kind of 3610 02:11:20,360 --> 02:11:22,760 thing that you don't just learn 3611 02:11:21,280 --> 02:11:24,719 overnight it takes time it takes 3612 02:11:22,760 --> 02:11:26,440 practice just like writing an essay in 3613 02:11:24,719 --> 02:11:28,400 any subject might take time and practice 3614 02:11:26,440 --> 02:11:30,800 and iteration over time but in a 3615 02:11:28,400 --> 02:11:33,800 programming class like cs50 we're going 3616 02:11:30,800 --> 02:11:36,079 to Aspire to evaluate the quality of 3617 02:11:33,800 --> 02:11:37,800 code along these three axes generally is 3618 02:11:36,079 --> 02:11:39,520 it correct first and foremost like does 3619 02:11:37,800 --> 02:11:40,760 the code do what it's supposed to do 3620 02:11:39,520 --> 02:11:42,000 after all if it doesn't well what was 3621 02:11:40,760 --> 02:11:43,520 the point of writing it in the first 3622 02:11:42,000 --> 02:11:45,400 place so it sort of goes without saying 3623 02:11:43,520 --> 02:11:47,520 that you want code you write to be 3624 02:11:45,400 --> 02:11:49,239 correct and it's obviously not always 3625 02:11:47,520 --> 02:11:51,639 again anytime you're Mac or PC or phone 3626 02:11:49,239 --> 02:11:54,119 has crashed some human somewhere wrote 3627 02:11:51,639 --> 02:11:55,679 buggy that is code with mistakes but C 3628 02:11:54,119 --> 02:11:57,480 correctness is going to be the first and 3629 02:11:55,679 --> 02:11:59,599 foremost goal but then there's a more 3630 02:11:57,480 --> 02:12:01,480 subjective goal we'll see in time a 3631 02:11:59,599 --> 02:12:03,639 matter of design and we saw a little bit 3632 02:12:01,480 --> 02:12:05,800 of this last week when I proposed that 3633 02:12:03,639 --> 02:12:07,800 we could design even scratch programs 3634 02:12:05,800 --> 02:12:09,480 better maybe by using Loops instead of 3635 02:12:07,800 --> 02:12:11,960 just by copying and pasting the same 3636 02:12:09,480 --> 02:12:14,800 blocks again and again so design is more 3637 02:12:11,960 --> 02:12:16,440 subjective it's more of a a learned art 3638 02:12:14,800 --> 02:12:18,440 whereby two people might ultimately 3639 02:12:16,440 --> 02:12:19,920 disagree as to which version of a 3640 02:12:18,440 --> 02:12:21,400 program is better designed but we'll 3641 02:12:19,920 --> 02:12:22,960 give you building blocks and principles 3642 02:12:21,400 --> 02:12:24,719 over the coming weeks so that you can 3643 02:12:22,960 --> 02:12:27,400 have a better sense for yourself if your 3644 02:12:24,719 --> 02:12:28,880 own code is well designed and why is 3645 02:12:27,400 --> 02:12:30,960 that valuable well the better design 3646 02:12:28,880 --> 02:12:32,840 your code is often the faster it's going 3647 02:12:30,960 --> 02:12:34,480 to run the more maintainable it's going 3648 02:12:32,840 --> 02:12:36,280 to be by you or colleagues if you're 3649 02:12:34,480 --> 02:12:37,800 working with others in the real world so 3650 02:12:36,280 --> 02:12:39,520 good design is a good thing it helps you 3651 02:12:37,800 --> 02:12:41,880 communicate your ideas just like an 3652 02:12:39,520 --> 02:12:44,159 typical English essay and then lastly 3653 02:12:41,880 --> 02:12:45,559 we'll talk this week onward about style 3654 02:12:44,159 --> 02:12:47,520 and this is really just the Aesthetics 3655 02:12:45,559 --> 02:12:50,119 of your code it turns out that computers 3656 02:12:47,520 --> 02:12:53,840 often don't care how sloppy your actual 3657 02:12:50,119 --> 02:12:55,440 code is um where uh in the world of code 3658 02:12:53,840 --> 02:12:57,400 it turns out that you don't really need 3659 02:12:55,440 --> 02:12:58,840 to indent things in a beautiful way you 3660 02:12:57,400 --> 02:13:00,880 don't need to paginate things like you 3661 02:12:58,840 --> 02:13:03,079 might in an essay the computer generally 3662 02:13:00,880 --> 02:13:05,400 does not care but the human does the 3663 02:13:03,079 --> 02:13:06,840 teaching assistant does you will care 3664 02:13:05,400 --> 02:13:08,520 the next day when you're just trying to 3665 02:13:06,840 --> 02:13:10,679 understand what your code does so we'll 3666 02:13:08,520 --> 02:13:12,559 focus lastly on Style the Aesthetics of 3667 02:13:10,679 --> 02:13:14,320 the code that you're writing so where 3668 02:13:12,559 --> 02:13:16,559 are we going to write code where are we 3669 02:13:14,320 --> 02:13:18,760 going to compile code so for this class 3670 02:13:16,559 --> 02:13:20,400 not only with C but the other languages 3671 02:13:18,760 --> 02:13:23,360 we use later in the term we're going to 3672 02:13:20,400 --> 02:13:25,800 use a free text editor that is program 3673 02:13:23,360 --> 02:13:27,639 called Visual Studio code AKA vs code 3674 02:13:25,800 --> 02:13:30,840 it's super popular nowadays not just for 3675 02:13:27,639 --> 02:13:32,920 C but for C++ and Python and Java and 3676 02:13:30,840 --> 02:13:34,920 any number of other languages it's a 3677 02:13:32,920 --> 02:13:37,320 text editor in the sense that it lets 3678 02:13:34,920 --> 02:13:39,559 you edit text and that all that's all 3679 02:13:37,320 --> 02:13:41,599 code is going to be now strictly 3680 02:13:39,559 --> 02:13:42,840 speaking you could write code on paper 3681 02:13:41,599 --> 02:13:43,960 pencil in fact in high school if you 3682 02:13:42,840 --> 02:13:45,719 took a class you might have done that 3683 02:13:43,960 --> 02:13:47,520 one or more times as sort of an in-class 3684 02:13:45,719 --> 02:13:49,199 exercise you can't run it on paper of 3685 02:13:47,520 --> 02:13:50,800 course but you could write it certainly 3686 02:13:49,199 --> 02:13:53,440 you could use something like Microsoft 3687 02:13:50,800 --> 02:13:55,239 Word or notepad.exe or text edit on the 3688 02:13:53,440 --> 02:13:57,559 Mac but none of those programs are 3689 02:13:55,239 --> 02:13:59,320 really designed to format the code in 3690 02:13:57,559 --> 02:14:01,520 the best way for you nor are they 3691 02:13:59,320 --> 02:14:04,119 designed to let you compile and run the 3692 02:14:01,520 --> 02:14:06,159 code so VSS code is going to be a tool 3693 02:14:04,119 --> 02:14:08,360 via which you can do all that and more 3694 02:14:06,159 --> 02:14:10,480 write the code compile the code run the 3695 02:14:08,360 --> 02:14:12,239 code so that you all don't have to 3696 02:14:10,480 --> 02:14:14,040 wrestle with stupid technical support 3697 02:14:12,239 --> 02:14:15,760 headaches at the beginning of the course 3698 02:14:14,040 --> 02:14:18,280 by installing this software and that on 3699 02:14:15,760 --> 02:14:21,599 your Macs or PCS we'll use a cloud based 3700 02:14:18,280 --> 02:14:23,119 version of VSS code at code. cs50. and 3701 02:14:21,599 --> 02:14:24,800 that's going to be the exact same tool 3702 02:14:23,119 --> 02:14:27,280 and the goal then is by the end of the 3703 02:14:24,800 --> 02:14:28,960 semester to sort of uh migrate you off 3704 02:14:27,280 --> 02:14:31,000 of that cloud-based environment to your 3705 02:14:28,960 --> 02:14:33,119 own Mac and PC so that even if cs50 is 3706 02:14:31,000 --> 02:14:35,800 the only CS class you ever take you're 3707 02:14:33,119 --> 02:14:37,320 100% equipped to continue writing code 3708 02:14:35,800 --> 02:14:40,119 after the class using not something 3709 02:14:37,320 --> 02:14:42,440 that's even cs50 specific but a de facto 3710 02:14:40,119 --> 02:14:44,520 industry standard at least for some time 3711 02:14:42,440 --> 02:14:46,159 so what's this program VSS code going to 3712 02:14:44,520 --> 02:14:48,079 look like be it on your Mac PC or 3713 02:14:46,159 --> 02:14:49,639 initially in your browser and it's going 3714 02:14:48,079 --> 02:14:50,719 to look a little something like this and 3715 02:14:49,639 --> 02:14:52,719 there's going to be several different 3716 02:14:50,719 --> 02:14:54,840 regions to the screen and picture here 3717 02:14:52,719 --> 02:14:56,559 is that very same code I keep proposing 3718 02:14:54,840 --> 02:14:58,280 is the simplest program you can write in 3719 02:14:56,559 --> 02:15:00,639 C and what are these different regions 3720 02:14:58,280 --> 02:15:03,639 of the screen well there's essentially 3721 02:15:00,639 --> 02:15:06,199 these four here so first highlighted up 3722 02:15:03,639 --> 02:15:07,360 top is going to be one or more tabs 3723 02:15:06,199 --> 02:15:09,199 where you're going to actually write 3724 02:15:07,360 --> 02:15:10,840 code so much like in Google Docs or 3725 02:15:09,199 --> 02:15:13,400 Microsoft Word you can have tabs open 3726 02:15:10,840 --> 02:15:15,119 with files similarly in VSS code or 3727 02:15:13,400 --> 02:15:17,000 really any programming environment do 3728 02:15:15,119 --> 02:15:18,520 generally nowadays have tabs of some 3729 02:15:17,000 --> 02:15:20,239 sort and this is going to be a tab 3730 02:15:18,520 --> 02:15:22,239 containing a file it seems called 3731 02:15:20,239 --> 02:15:24,760 hello.c and that's going to be the very 3732 02:15:22,239 --> 02:15:26,199 first file we write in just a moment uh 3733 02:15:24,760 --> 02:15:27,960 down here though is going to be an 3734 02:15:26,199 --> 02:15:30,079 interface that many of you might not 3735 02:15:27,960 --> 02:15:32,440 know this is what's called a terminal 3736 02:15:30,079 --> 02:15:34,520 window and a terminal window provides 3737 02:15:32,440 --> 02:15:37,679 what's generally called a commandline 3738 02:15:34,520 --> 02:15:40,360 interface or CLI and this is in contrast 3739 02:15:37,679 --> 02:15:42,840 with a graphical user interface or guey 3740 02:15:40,360 --> 02:15:44,960 now you and I every day are using guies 3741 02:15:42,840 --> 02:15:47,199 on our phones on our PCS and a guy is 3742 02:15:44,960 --> 02:15:48,920 literally graphical so menus and buttons 3743 02:15:47,199 --> 02:15:51,320 and icons and you generally use your 3744 02:15:48,920 --> 02:15:53,040 finger or a trackpad or a mouse or 3745 02:15:51,320 --> 02:15:54,840 something like that to interact with it 3746 02:15:53,040 --> 02:15:56,800 but it turns out that many programmers 3747 02:15:54,840 --> 02:15:59,960 dare say most programmers at least over 3748 02:15:56,800 --> 02:16:01,559 time come to prefer not a guey but a CLI 3749 02:15:59,960 --> 02:16:04,480 a command line interface where you 3750 02:16:01,559 --> 02:16:07,880 actually do everything somewhat uh 3751 02:16:04,480 --> 02:16:09,280 somewhat arcely via keyboard alone why 3752 02:16:07,880 --> 02:16:11,280 well it turns out there's just more 3753 02:16:09,280 --> 02:16:12,920 features built in to most computers if 3754 02:16:11,280 --> 02:16:14,639 you can access them with a keyboard 3755 02:16:12,920 --> 02:16:16,199 turns out you can most of us can type 3756 02:16:14,639 --> 02:16:18,239 faster than you can point and click and 3757 02:16:16,199 --> 02:16:20,159 so that ends up being an efficient gain 3758 02:16:18,239 --> 02:16:22,280 over time so in time will you get 3759 02:16:20,159 --> 02:16:24,840 comfortable using this terminal window 3760 02:16:22,280 --> 02:16:27,079 to do things like compile your code or 3761 02:16:24,840 --> 02:16:29,040 make your program as well as run it so 3762 02:16:27,079 --> 02:16:30,960 you won't be in the habit initially of 3763 02:16:29,040 --> 02:16:32,960 just double clicking icons like we do in 3764 02:16:30,960 --> 02:16:35,160 our typical real world you'll do it sort 3765 02:16:32,960 --> 02:16:36,960 of the programmer's way but it's not to 3766 02:16:35,160 --> 02:16:39,280 the exclusion of adding icons and 3767 02:16:36,960 --> 02:16:40,960 clickability and more on the left hand 3768 02:16:39,280 --> 02:16:43,719 side of vs code there's going to be a 3769 02:16:40,960 --> 02:16:45,519 somewhat familiar file explorer some 3770 02:16:43,719 --> 02:16:47,319 kind of hierarchical tree like on your 3771 02:16:45,519 --> 02:16:49,519 Mac or PC where you can see all of the 3772 02:16:47,319 --> 02:16:51,280 files in your account pictured here for 3773 02:16:49,519 --> 02:16:53,519 instance is just hello.c which I'll 3774 02:16:51,280 --> 02:16:55,080 create myself in a moment and then far 3775 02:16:53,519 --> 02:16:56,840 away on the left is the so-called 3776 02:16:55,080 --> 02:16:58,479 activity bar and this is where you just 3777 02:16:56,840 --> 02:17:00,399 get a lot of traditional menus and 3778 02:16:58,479 --> 02:17:03,559 buttons so VSS code itself gives you 3779 02:17:00,399 --> 02:17:06,280 both a guey and a CLI but it's within 3780 02:17:03,559 --> 02:17:07,519 the CLI the terminal window the bottom 3781 02:17:06,280 --> 02:17:08,880 region of the screen that we're actually 3782 02:17:07,519 --> 02:17:10,359 going to type most of our commands and 3783 02:17:08,880 --> 02:17:12,120 in general in class I'm going to hide 3784 02:17:10,359 --> 02:17:15,240 all of the graphical stuff that's just 3785 02:17:12,120 --> 02:17:16,920 not of all that uh that much interest so 3786 02:17:15,240 --> 02:17:19,639 with that said let me actually change 3787 02:17:16,920 --> 02:17:21,399 over to a live version of vs code and 3788 02:17:19,639 --> 02:17:23,399 I've indeed HD in the activity bar I've 3789 02:17:21,399 --> 02:17:25,120 indeed hid in the file explorer so what 3790 02:17:23,399 --> 02:17:27,200 I have here for visibility sake is a 3791 02:17:25,120 --> 02:17:29,399 really big area for writing code and a 3792 02:17:27,200 --> 02:17:30,880 really big terminal window at the bottom 3793 02:17:29,399 --> 02:17:32,280 you'll see in the terminal window 3794 02:17:30,880 --> 02:17:34,240 there's a dollar sign and this doesn't 3795 02:17:32,280 --> 02:17:36,920 mean any form of currency this is just 3796 02:17:34,240 --> 02:17:38,639 the standard symbol that represents type 3797 02:17:36,920 --> 02:17:40,319 commands here so the fact that there's 3798 02:17:38,639 --> 02:17:41,280 just a dollar sign in a cursor means 3799 02:17:40,319 --> 02:17:43,120 eventually that's where I'm going to 3800 02:17:41,280 --> 02:17:46,399 type commands but first i'm going to 3801 02:17:43,120 --> 02:17:49,479 actually create some code so how might I 3802 02:17:46,399 --> 02:17:51,160 program using vs code be it on my Mac PC 3803 02:17:49,479 --> 02:17:54,120 or in this cloud-based environment that 3804 02:17:51,160 --> 02:17:55,960 you'll get set up for problem set one go 3805 02:17:54,120 --> 02:17:57,920 about writing my first file well perhaps 3806 02:17:55,960 --> 02:18:00,120 the easiest way is this literally run 3807 02:17:57,920 --> 02:18:02,359 the command code and then the name of 3808 02:18:00,120 --> 02:18:04,800 the file you want to create notice that 3809 02:18:02,359 --> 02:18:06,519 I deliberately end the file with C in 3810 02:18:04,800 --> 02:18:08,120 lowercase notice that I've deliberately 3811 02:18:06,519 --> 02:18:10,080 lowercased the whole file name and these 3812 02:18:08,120 --> 02:18:12,080 are just conventions you could use a 3813 02:18:10,080 --> 02:18:14,319 capital H you kind of could use a 3814 02:18:12,080 --> 02:18:15,920 Capital C but just don't do that follow 3815 02:18:14,319 --> 02:18:17,800 best practices so that it's consistent 3816 02:18:15,920 --> 02:18:20,359 with what most everyone else would do 3817 02:18:17,800 --> 02:18:22,240 when I hit enter I just get an empty tab 3818 02:18:20,359 --> 02:18:24,040 just like the screenshot a moment ago 3819 02:18:22,240 --> 02:18:26,800 and it's in this tab where I can now 3820 02:18:24,040 --> 02:18:28,319 write my very first program in C 3821 02:18:26,800 --> 02:18:29,840 unfortunately it's not quite as user 3822 02:18:28,319 --> 02:18:31,920 friendly as scratch where you drag and 3823 02:18:29,840 --> 02:18:33,599 drop a couple of puzzle pieces and boom 3824 02:18:31,920 --> 02:18:35,439 it's done so I'm going to do this for 3825 02:18:33,599 --> 02:18:37,840 memory but this too will become familiar 3826 02:18:35,439 --> 02:18:40,599 to you over time I'm going to include 3827 02:18:37,840 --> 02:18:44,000 something called standard io. I'm going 3828 02:18:40,599 --> 02:18:45,840 to type int main void and parentheses on 3829 02:18:44,000 --> 02:18:48,080 a new line I'm going to insert some 3830 02:18:45,840 --> 02:18:50,479 curly braces as we'll call them and then 3831 02:18:48,080 --> 02:18:53,120 I'm going to type print F and then some 3832 02:18:50,479 --> 02:18:55,800 parentheses and then in quotes hello 3833 02:18:53,120 --> 02:18:58,479 comma world then a backslash then a 3834 02:18:55,800 --> 02:19:00,960 lowercase n then a close quote and then 3835 02:18:58,479 --> 02:19:03,280 a semicolon at the very end of the line 3836 02:19:00,960 --> 02:19:05,800 so all I've done is recreate just from 3837 02:19:03,280 --> 02:19:07,760 memory that very first program in a 3838 02:19:05,800 --> 02:19:09,599 little bit we'll make clear what most of 3839 02:19:07,760 --> 02:19:11,080 this does but for now let's just 3840 02:19:09,599 --> 02:19:12,639 actually run this thing and just like I 3841 02:19:11,080 --> 02:19:15,160 click the green flag last week for the 3842 02:19:12,639 --> 02:19:18,399 first time let's actually compile and 3843 02:19:15,160 --> 02:19:20,559 run this program if it were your Mac PC 3844 02:19:18,399 --> 02:19:22,200 and Google or Microsoft or someone else 3845 02:19:20,559 --> 02:19:23,559 had made the software at this point in 3846 02:19:22,200 --> 02:19:25,479 the story we'd be double clicking an 3847 02:19:23,559 --> 02:19:27,359 icon but we can't do that yet this is 3848 02:19:25,479 --> 02:19:29,519 still source code so I'm going to click 3849 02:19:27,359 --> 02:19:31,519 back down in my terminal window notice I 3850 02:19:29,519 --> 02:19:33,359 have a second dollar sign below the 3851 02:19:31,519 --> 02:19:35,960 first which just means it's ready for a 3852 02:19:33,359 --> 02:19:38,880 second command and now the command via 3853 02:19:35,960 --> 02:19:41,080 which to make this an actual program to 3854 02:19:38,880 --> 02:19:43,800 compile it from source code to machine 3855 02:19:41,080 --> 02:19:45,359 code is going to be quite simply make 3856 02:19:43,800 --> 02:19:48,160 and then the name of the program I want 3857 02:19:45,359 --> 02:19:50,280 to make slight subtlety I'm omitting 3858 02:19:48,160 --> 02:19:52,280 deliberately. C because the program I 3859 02:19:50,280 --> 02:19:54,560 want to make I just want to call hello 3860 02:19:52,280 --> 02:19:58,120 so don't write make hello. C just write 3861 02:19:54,560 --> 02:20:00,120 make hello and this program make is 3862 02:19:58,120 --> 02:20:02,160 essentially our compiler technically 3863 02:20:00,120 --> 02:20:04,399 speaking it's a program that automates 3864 02:20:02,160 --> 02:20:06,040 the compilation of my program for me but 3865 02:20:04,399 --> 02:20:08,520 it is going to see that I've typed the 3866 02:20:06,040 --> 02:20:10,479 word hello it's going to automatically 3867 02:20:08,520 --> 02:20:13,359 look now for a file on the hard drive 3868 02:20:10,479 --> 02:20:16,120 called hello.c and convert it from 3869 02:20:13,359 --> 02:20:18,560 source code in C to machine code in 3870 02:20:16,120 --> 02:20:20,760 zeros and ones so if I didn't make any 3871 02:20:18,560 --> 02:20:23,880 typos 3872 02:20:20,760 --> 02:20:26,160 enter nothing seems to happen and that's 3873 02:20:23,880 --> 02:20:28,200 a good thing almost always if nothing 3874 02:20:26,160 --> 02:20:29,600 gets outputed on the screen like you did 3875 02:20:28,200 --> 02:20:30,800 good like you didn't make any mistakes 3876 02:20:29,600 --> 02:20:32,439 you didn't get yelled at there's no 3877 02:20:30,800 --> 02:20:34,760 error messages so this is actually a 3878 02:20:32,439 --> 02:20:36,399 good thing how do I now run this program 3879 02:20:34,760 --> 02:20:38,040 well notice I've got a third dollar sign 3880 02:20:36,399 --> 02:20:39,720 which just means I'm ready for a third 3881 02:20:38,040 --> 02:20:42,520 command and now I'm going to go ahead 3882 02:20:39,720 --> 02:20:44,319 and run dot slash hello and this is 3883 02:20:42,520 --> 02:20:46,359 admittedly a little weird that you have 3884 02:20:44,319 --> 02:20:48,439 to do dot slash but for now just take on 3885 02:20:46,359 --> 02:20:51,319 faith that this is how you run a program 3886 02:20:48,439 --> 02:20:52,760 called hello in your current folder in 3887 02:20:51,319 --> 02:20:54,720 your current directory in this 3888 02:20:52,760 --> 02:20:57,080 cloud-based environment all right 3889 02:20:54,720 --> 02:21:01,040 crossing my fingers again hitting enter 3890 02:20:57,080 --> 02:21:04,120 and voila my very first program in see 3891 02:21:01,040 --> 02:21:07,080 hello world and now let me go ahead and 3892 02:21:04,120 --> 02:21:08,680 reveal the file explorer that I proposed 3893 02:21:07,080 --> 02:21:10,560 exist earlier I'm just going to use a 3894 02:21:08,680 --> 02:21:11,840 keyboard shortcut to reveal that and 3895 02:21:10,560 --> 02:21:13,240 generally I keep it close because I 3896 02:21:11,840 --> 02:21:15,240 don't really need to know constantly 3897 02:21:13,240 --> 02:21:16,960 what files are in my account but you'll 3898 02:21:15,240 --> 02:21:19,280 see now in the file explorer similar 3899 02:21:16,960 --> 02:21:21,960 INSP to a Mac or PC but graphically a 3900 02:21:19,280 --> 02:21:23,720 little different here's my file hello.c 3901 02:21:21,960 --> 02:21:26,120 it's highlighted because I have that tab 3902 02:21:23,720 --> 02:21:28,760 open but now there's a second file here 3903 02:21:26,120 --> 02:21:30,840 called just hello that's the name of my 3904 02:21:28,760 --> 02:21:32,319 program so if you were on a Mac or PC 3905 02:21:30,840 --> 02:21:34,040 you would ideally double click that 3906 02:21:32,319 --> 02:21:35,640 thing you can't do that in a command 3907 02:21:34,040 --> 02:21:37,200 line environment you have to run it down 3908 02:21:35,640 --> 02:21:39,479 here but that's all we've done we've 3909 02:21:37,200 --> 02:21:42,840 created a file called hello.c and then 3910 02:21:39,479 --> 02:21:45,319 my compiler made the program from that 3911 02:21:42,840 --> 02:21:48,080 let me pause here and see if there's any 3912 02:21:45,319 --> 02:21:50,359 questions because it's a lot of magical 3913 02:21:48,080 --> 02:21:52,880 phrases 3914 02:21:50,359 --> 02:21:55,120 yeah yeah so if you're currently 3915 02:21:52,880 --> 02:21:56,520 following along uh playing along at home 3916 02:21:55,120 --> 02:21:58,040 and you're getting some kind of error 3917 02:21:56,520 --> 02:21:59,880 message part of today will be for me to 3918 02:21:58,040 --> 02:22:02,160 deliberately induce some of those error 3919 02:21:59,880 --> 02:22:03,840 messages for now let me just propose 3920 02:22:02,160 --> 02:22:06,120 that if you literally did what I did you 3921 02:22:03,840 --> 02:22:09,319 must have made a typo somewhere and 3922 02:22:06,120 --> 02:22:12,280 notice that it's indeed standard IO 3923 02:22:09,319 --> 02:22:15,280 stdio.h uh maybe you type 3924 02:22:12,280 --> 02:22:17,359 studio. okay super common mistake if I 3925 02:22:15,280 --> 02:22:20,840 don't if I could call you out um like it 3926 02:22:17,359 --> 02:22:22,200 is not studio. it is standard io. so 3927 02:22:20,840 --> 02:22:23,680 common but this is exactly 3928 02:22:22,200 --> 02:22:24,960 representative of like the kind of 3929 02:22:23,680 --> 02:22:26,800 stupid headaches you're going to run 3930 02:22:24,960 --> 02:22:28,800 into this week probably for a few weeks 3931 02:22:26,800 --> 02:22:30,359 probably honestly for a few years but 3932 02:22:28,800 --> 02:22:32,160 you start to see past these sort of 3933 02:22:30,359 --> 02:22:33,640 stupid mistakes over time and it just 3934 02:22:32,160 --> 02:22:35,439 gets easier and easier because the 3935 02:22:33,640 --> 02:22:36,760 computer is going to be so regimented 3936 02:22:35,439 --> 02:22:38,800 like you it will only do what you tell 3937 02:22:36,760 --> 02:22:41,359 it to do and if you say because it's 3938 02:22:38,800 --> 02:22:42,680 verbally sounds like studio. it's not 3939 02:22:41,359 --> 02:22:44,000 going to know what the file is so 3940 02:22:42,680 --> 02:22:46,120 actually thank you for tripping over 3941 02:22:44,000 --> 02:22:49,560 that so early that's super common to 3942 02:22:46,120 --> 02:22:51,920 happen yeah have two hello so I why do I 3943 02:22:49,560 --> 02:22:53,920 have two hello files so why do I have 3944 02:22:51,920 --> 02:22:56,040 two hello files one is the one I created 3945 02:22:53,920 --> 02:22:58,280 as the human called hello.c and it's 3946 02:22:56,040 --> 02:23:01,840 pictured right here but then when I ran 3947 02:22:58,280 --> 02:23:04,120 make hello that process compiled my 3948 02:23:01,840 --> 02:23:06,439 source code into machine code so this 3949 02:23:04,120 --> 02:23:08,040 second file just called hello is the 3950 02:23:06,439 --> 02:23:11,399 file that contains all of those zeros 3951 02:23:08,040 --> 02:23:15,560 and ones that the server actually 3952 02:23:11,399 --> 02:23:15,560 understands all right so yeah 3953 02:23:15,760 --> 02:23:19,520 question if you try clicking on the 3954 02:23:17,880 --> 02:23:21,680 Hello file you'll see in this 3955 02:23:19,520 --> 02:23:23,240 environment of vs code quote unquote the 3956 02:23:21,680 --> 02:23:25,680 file is not displayed in the editor 3957 02:23:23,240 --> 02:23:27,640 because it is either binary AKA zeros 3958 02:23:25,680 --> 02:23:29,240 and ones or uses an unsupported text 3959 02:23:27,640 --> 02:23:32,120 encoding in this case it's binary it's 3960 02:23:29,240 --> 02:23:34,359 zeros and ones now you could use 3961 02:23:32,120 --> 02:23:35,920 software to see those zeros and ones it 3962 02:23:34,359 --> 02:23:38,200 won't be intellectually enlightening to 3963 02:23:35,920 --> 02:23:39,680 most any human so VSS code just takes 3964 02:23:38,200 --> 02:23:41,160 the choice of not showing it to you at 3965 02:23:39,680 --> 02:23:43,040 all so that would be a common mistake 3966 02:23:41,160 --> 02:23:44,640 too clicking on a file you don't intend 3967 02:23:43,040 --> 02:23:47,399 but the source code is indeed going to 3968 02:23:44,640 --> 02:23:48,960 be editable by us all right all right so 3969 02:23:47,399 --> 02:23:50,880 I've written this program it seems to 3970 02:23:48,960 --> 02:23:52,359 sort of magically work at least with 3971 02:23:50,880 --> 02:23:54,560 some effort if you get every single 3972 02:23:52,359 --> 02:23:56,279 keystroke right well what is it that's 3973 02:23:54,560 --> 02:23:58,680 going on and how is this working well 3974 02:23:56,279 --> 02:24:01,160 first of all notice that even without my 3975 02:23:58,680 --> 02:24:03,279 highlighting things or choosing buttons 3976 02:24:01,160 --> 02:24:05,160 from menus notice that it's already kind 3977 02:24:03,279 --> 02:24:06,640 of colorcoded and yet I wasn't 3978 02:24:05,160 --> 02:24:08,399 highlighting along the way in sort of 3979 02:24:06,640 --> 02:24:10,560 Google doc style changing the color 3980 02:24:08,399 --> 02:24:12,600 certainly well it turns out what VSS 3981 02:24:10,560 --> 02:24:14,800 code and most programming environments 3982 02:24:12,600 --> 02:24:17,200 nowadays do for you automatically is 3983 02:24:14,800 --> 02:24:19,319 syntax highlighting so syntax 3984 02:24:17,200 --> 02:24:21,600 highlighting is just this feature of 3985 02:24:19,319 --> 02:24:23,640 typical text editors nowadays that 3986 02:24:21,600 --> 02:24:25,760 analyzes the code that you've typed and 3987 02:24:23,640 --> 02:24:27,640 when it notices certain types of 3988 02:24:25,760 --> 02:24:30,000 keystrokes things that represent 3989 02:24:27,640 --> 02:24:31,680 functions or conditionals or Loops or 3990 02:24:30,000 --> 02:24:33,680 variables a lot of the vocab from last 3991 02:24:31,680 --> 02:24:35,840 week it just highlights it ever so 3992 02:24:33,680 --> 02:24:38,000 differently for you so main for instance 3993 02:24:35,840 --> 02:24:40,600 which we'll soon see is in purple here 3994 02:24:38,000 --> 02:24:42,920 int and void and include are in red 3995 02:24:40,600 --> 02:24:45,160 hello world is in blue my parentheses 3996 02:24:42,920 --> 02:24:47,600 are in green this will totally vary by 3997 02:24:45,160 --> 02:24:49,520 programmer too in fact if you do want to 3998 02:24:47,600 --> 02:24:51,160 change these colors for problem set one 3999 02:24:49,520 --> 02:24:53,240 for your own environment you can poke 4000 02:24:51,160 --> 02:24:55,120 around vs code settings via the gear 4001 02:24:53,240 --> 02:24:57,000 icon you can change a different a change 4002 02:24:55,120 --> 02:24:58,600 to a different color theme syntax 4003 02:24:57,000 --> 02:25:00,600 highlighting isn't some specific color 4004 02:24:58,600 --> 02:25:02,680 scheme like it is in scratch it just 4005 02:25:00,600 --> 02:25:03,960 generally is to each human their own 4006 02:25:02,680 --> 02:25:06,120 preference but that's all that's 4007 02:25:03,960 --> 02:25:07,920 happening here is this notion of syntax 4008 02:25:06,120 --> 02:25:10,439 highlighting at the moment well what 4009 02:25:07,920 --> 02:25:12,560 more is going on in this code before I 4010 02:25:10,439 --> 02:25:14,200 run it but rather write it well it looks 4011 02:25:12,560 --> 02:25:15,920 a little something like this if I take 4012 02:25:14,200 --> 02:25:17,479 away all of the colors and then just for 4013 02:25:15,920 --> 02:25:19,520 discussion sake let me ahead and color 4014 02:25:17,479 --> 02:25:21,600 it a little more like scratch recall 4015 02:25:19,520 --> 02:25:23,880 that our very first scratch program that 4016 02:25:21,600 --> 02:25:26,680 just said hello world on the screen had 4017 02:25:23,880 --> 02:25:29,160 a when green flag clicked icon uh puzzle 4018 02:25:26,680 --> 02:25:32,319 piece roughly an orange and then a 4019 02:25:29,160 --> 02:25:34,640 purple uh sa block beneath it so whereas 4020 02:25:32,319 --> 02:25:37,040 this is the C version if we rewind to 4021 02:25:34,640 --> 02:25:38,760 last week this was the same program in 4022 02:25:37,040 --> 02:25:40,479 scratch but what's happening now is 4023 02:25:38,760 --> 02:25:42,720 exactly the same so if you think back to 4024 02:25:40,479 --> 02:25:45,439 last week and you've got some function 4025 02:25:42,720 --> 02:25:47,200 like the say function in purple that 4026 02:25:45,439 --> 02:25:49,080 might take one or more arguments like 4027 02:25:47,200 --> 02:25:51,560 inputs that influences what it says on 4028 02:25:49,080 --> 02:25:53,279 the screen and then functions recall can 4029 02:25:51,560 --> 02:25:55,000 sometimes have side effects right like 4030 02:25:53,279 --> 02:25:57,560 the speech bubble appears on the screen 4031 02:25:55,000 --> 02:25:59,319 so last week when we used the sa block 4032 02:25:57,560 --> 02:26:01,880 and we passed in an argument of hello 4033 02:25:59,319 --> 02:26:04,080 world at left we got this visual side 4034 02:26:01,880 --> 02:26:05,720 effect on the screen that says now hello 4035 02:26:04,080 --> 02:26:07,760 world in the speech bubble and that's 4036 02:26:05,720 --> 02:26:10,279 exactly what just happened in VSS code 4037 02:26:07,760 --> 02:26:11,960 but much much more textually and let's 4038 02:26:10,279 --> 02:26:14,479 look a little closer now at the code 4039 02:26:11,960 --> 02:26:16,279 itself let me wave my hand at the 4040 02:26:14,479 --> 02:26:18,920 equivalent of the when green flag 4041 02:26:16,279 --> 02:26:22,000 clicked part of my code and let's focus 4042 02:26:18,920 --> 02:26:26,240 only on the say block in scratch and the 4043 02:26:22,000 --> 02:26:28,200 corresponding function in C so if I step 4044 02:26:26,240 --> 02:26:30,479 through this and I wanted to convert 4045 02:26:28,200 --> 02:26:33,279 what we did last week with the say block 4046 02:26:30,479 --> 02:26:34,560 to C I would first use the print 4047 02:26:33,279 --> 02:26:36,560 function although that's actually a bit 4048 02:26:34,560 --> 02:26:38,880 of a white lie it's actually the print F 4049 02:26:36,560 --> 02:26:40,279 function print f means formatted and 4050 02:26:38,880 --> 02:26:42,920 it's just a function that allows you to 4051 02:26:40,279 --> 02:26:45,240 format text on the screen There Is No Sa 4052 02:26:42,920 --> 02:26:47,080 function in C there's a print F function 4053 02:26:45,240 --> 02:26:49,200 what MIT did down the road years ago go 4054 02:26:47,080 --> 02:26:51,160 was they took what existed historically 4055 02:26:49,200 --> 02:26:52,800 as print F and they simplified it for a 4056 02:26:51,160 --> 02:26:55,680 broader audience by just calling it 4057 02:26:52,800 --> 02:26:58,439 essentially say instead but notice that 4058 02:26:55,680 --> 02:27:00,960 now if I want to convert the scratch 4059 02:26:58,439 --> 02:27:03,640 code at left to C code at right it's 4060 02:27:00,960 --> 02:27:05,800 sort of the same shape so MIT 4061 02:27:03,640 --> 02:27:08,040 deliberately used this white oval if 4062 02:27:05,800 --> 02:27:10,120 only because it kind of conjures these 4063 02:27:08,040 --> 02:27:12,120 uh this idea of having parentheses too 4064 02:27:10,120 --> 02:27:14,359 so on the right if I want to pass an 4065 02:27:12,120 --> 02:27:16,720 argument or an input to the print F 4066 02:27:14,359 --> 02:27:19,279 function I use an open parenthesis and a 4067 02:27:16,720 --> 02:27:21,240 CL parenthesis in those parentheses I 4068 02:27:19,279 --> 02:27:23,399 then type whatever it is I want to print 4069 02:27:21,240 --> 02:27:25,359 on the screen in this case hello comma 4070 02:27:23,399 --> 02:27:26,960 world but notice I've deliberately left 4071 02:27:25,359 --> 02:27:29,680 some room because you need some extra 4072 02:27:26,960 --> 02:27:32,720 keystrokes in the world of C anytime you 4073 02:27:29,680 --> 02:27:35,080 type out some text otherwise known as a 4074 02:27:32,720 --> 02:27:37,600 string of text to use computer science 4075 02:27:35,080 --> 02:27:39,359 jargon you need to quote it in this case 4076 02:27:37,600 --> 02:27:41,399 with double quotes double quote at the 4077 02:27:39,359 --> 02:27:43,120 left double quote at the right and 4078 02:27:41,399 --> 02:27:45,120 notice to I'm going to include some 4079 02:27:43,120 --> 02:27:47,760 slightly cryptic symbol here too back 4080 02:27:45,120 --> 02:27:49,880 sln which I also typed and said verbally 4081 02:27:47,760 --> 02:27:52,760 earlier and then one last nuisance at 4082 02:27:49,880 --> 02:27:54,720 the end of this which is a semicolon so 4083 02:27:52,760 --> 02:27:57,000 suffice it to say this is why we start 4084 02:27:54,720 --> 02:27:59,479 with scratch this drag and drop you're 4085 02:27:57,000 --> 02:28:01,359 good to go in a language like C print F 4086 02:27:59,479 --> 02:28:03,080 parenthesis double quotes the text you 4087 02:28:01,359 --> 02:28:04,960 want backs slash and semicolon at the 4088 02:28:03,080 --> 02:28:07,000 end there's just so much syntactic 4089 02:28:04,960 --> 02:28:08,680 overhead but at the end of the day it's 4090 02:28:07,000 --> 02:28:10,680 just a function and you'll get used to 4091 02:28:08,680 --> 02:28:12,840 these sort of nuisances like the 4092 02:28:10,680 --> 02:28:15,960 parentheses the quotes the semicolon and 4093 02:28:12,840 --> 02:28:17,960 the like but things can very easily go 4094 02:28:15,960 --> 02:28:19,680 wrong and it's very easy to make 4095 02:28:17,960 --> 02:28:21,359 mistakes even with lines of code like 4096 02:28:19,680 --> 02:28:23,720 this so let me do this let me go back to 4097 02:28:21,359 --> 02:28:26,680 vs code where I have the exact same code 4098 02:28:23,720 --> 02:28:28,439 notice that on line five is exactly that 4099 02:28:26,680 --> 02:28:30,600 line of code so this is the equivalent 4100 02:28:28,439 --> 02:28:32,680 of the sa block and let's consider what 4101 02:28:30,600 --> 02:28:34,399 mistakes I may make early on or even now 4102 02:28:32,680 --> 02:28:37,160 20 years later after learning this that 4103 02:28:34,399 --> 02:28:39,960 are quite common um in general suppose I 4104 02:28:37,160 --> 02:28:41,960 forget the semicolon there so easy to do 4105 02:28:39,960 --> 02:28:43,760 you will do this eventually let's see 4106 02:28:41,960 --> 02:28:45,680 what happens now when I go back to my 4107 02:28:43,760 --> 02:28:47,760 terminal window and try to compile my 4108 02:28:45,680 --> 02:28:49,760 code again just to keep things tidy I'm 4109 02:28:47,760 --> 02:28:51,479 going to clear my screen but that's just 4110 02:28:49,760 --> 02:28:53,319 for lecture sake so that we can focus 4111 02:28:51,479 --> 02:28:55,840 only on the most recent command but I'm 4112 02:28:53,319 --> 02:28:59,040 going to go ahead now and rerun make 4113 02:28:55,840 --> 02:29:01,200 Hello this will ensure that my program 4114 02:28:59,040 --> 02:29:03,240 is recompiled and this is a manual 4115 02:29:01,200 --> 02:29:05,240 process I changed my code the zeros and 4116 02:29:03,240 --> 02:29:07,279 ones on the hard drive have not changed 4117 02:29:05,240 --> 02:29:09,240 I need to recompile it to Output the 4118 02:29:07,279 --> 02:29:10,840 latest machine code so here we go I'm 4119 02:29:09,240 --> 02:29:12,920 going to hit enter crossing my fingers 4120 02:29:10,840 --> 02:29:14,920 as before but again I remove the 4121 02:29:12,920 --> 02:29:17,279 semicolon by 4122 02:29:14,920 --> 02:29:19,479 accident oh my God there's like more 4123 02:29:17,279 --> 02:29:21,080 lines of Errors now than there are of 4124 02:29:19,479 --> 02:29:23,359 actual code and this too takes them 4125 02:29:21,080 --> 02:29:25,319 getting used to um the programs we're 4126 02:29:23,359 --> 02:29:27,040 using were not necessarily written with 4127 02:29:25,319 --> 02:29:28,720 the least comfortable audience in mind 4128 02:29:27,040 --> 02:29:31,000 but really professional programmers back 4129 02:29:28,720 --> 02:29:32,560 in the day but through practice and 4130 02:29:31,000 --> 02:29:34,319 through experience and through mistakes 4131 02:29:32,560 --> 02:29:36,560 you'll start to notice patterns here too 4132 02:29:34,319 --> 02:29:38,920 so here's what I typed make hello after 4133 02:29:36,560 --> 02:29:42,640 the dollar sign prompt now I get yelled 4134 02:29:38,920 --> 02:29:45,319 at with as follows hello.c colon 4135 02:29:42,640 --> 02:29:47,960 529 well what's that referring to I've 4136 02:29:45,319 --> 02:29:50,560 screwed up somewhere on line five on the 4137 02:29:47,960 --> 02:29:51,960 29th character on that line generally 4138 02:29:50,560 --> 02:29:53,319 the specific character is not that 4139 02:29:51,960 --> 02:29:55,680 useful unless you actually want to count 4140 02:29:53,319 --> 02:29:57,560 it out but line five is a good clue why 4141 02:29:55,680 --> 02:29:59,359 it means I screwed up somewhere on line 4142 02:29:57,560 --> 02:30:02,279 five here all right well what is the 4143 02:29:59,359 --> 02:30:03,800 error expected a semicolon after 4144 02:30:02,279 --> 02:30:05,720 expression and this error is actually 4145 02:30:03,800 --> 02:30:06,880 pretty obvious now that I see it and I 4146 02:30:05,720 --> 02:30:09,080 realize oh wait a minute all right 4147 02:30:06,880 --> 02:30:12,279 here's my line of code here in sort of 4148 02:30:09,080 --> 02:30:14,600 asky art so to speak textual uh text 4149 02:30:12,279 --> 02:30:16,240 representing Graphics it wants me to put 4150 02:30:14,600 --> 02:30:18,840 in green here this semicolon at the end 4151 02:30:16,240 --> 02:30:21,279 of that line one error generated built 4152 02:30:18,840 --> 02:30:23,439 in so some esoteric stuff there but my 4153 02:30:21,279 --> 02:30:26,120 program did not compile when you see an 4154 02:30:23,439 --> 02:30:28,439 error like this it means it did not work 4155 02:30:26,120 --> 02:30:31,439 so what's the fix well obviously the fix 4156 02:30:28,439 --> 02:30:34,279 is to go back up here put the semicolon 4157 02:30:31,439 --> 02:30:36,359 there and now if I recompile my code 4158 02:30:34,279 --> 02:30:37,880 with make hello I won't clear my screen 4159 02:30:36,359 --> 02:30:40,640 just yet just to show you the difference 4160 02:30:37,880 --> 02:30:42,600 now it just worked so we're back in 4161 02:30:40,640 --> 02:30:44,800 business as before all right let me 4162 02:30:42,600 --> 02:30:46,650 pause here though and ask if there's any 4163 02:30:44,800 --> 02:30:48,840 questions about what I just did 4164 02:30:46,650 --> 02:30:50,200 [Music] 4165 02:30:48,840 --> 02:30:53,240 these error messages will become 4166 02:30:50,200 --> 02:30:53,240 frequent initially 4167 02:30:55,160 --> 02:30:59,560 yeah really good question do you need a 4168 02:30:57,240 --> 02:31:01,319 semicolon after every line or just some 4169 02:30:59,560 --> 02:31:02,600 it turns out just some uh this is 4170 02:31:01,319 --> 02:31:04,240 something you'll learn through practice 4171 02:31:02,600 --> 02:31:06,800 through demonstrations and examples 4172 02:31:04,240 --> 02:31:10,080 today generally you put a semicolon 4173 02:31:06,800 --> 02:31:11,640 after a statement so to speak and this 4174 02:31:10,080 --> 02:31:13,319 is the technical term for this line of 4175 02:31:11,640 --> 02:31:14,800 code it's a statement and think of it is 4176 02:31:13,319 --> 02:31:16,760 it's kind of the code equivalent of like 4177 02:31:14,800 --> 02:31:19,040 an English sentence so the semi colon 4178 02:31:16,760 --> 02:31:20,520 and code is sort of like a period in 4179 02:31:19,040 --> 02:31:22,279 English when you're done with that 4180 02:31:20,520 --> 02:31:24,399 particular thought you don't need 4181 02:31:22,279 --> 02:31:26,200 semicolons for now anywhere else and 4182 02:31:24,399 --> 02:31:27,840 we'll see examples of where else you put 4183 02:31:26,200 --> 02:31:30,000 them but it usually is at the end of a 4184 02:31:27,840 --> 02:31:32,680 line of code that hasn't that isn't 4185 02:31:30,000 --> 02:31:35,160 purely syntactic like uh curly braces 4186 02:31:32,680 --> 02:31:39,750 instead other questions on the mistake I 4187 02:31:35,160 --> 02:31:40,840 just fixed and created for myself 4188 02:31:39,750 --> 02:31:44,160 [Music] 4189 02:31:40,840 --> 02:31:46,600 yeah uh correct so line five is where 4190 02:31:44,160 --> 02:31:48,560 the error is most likely character 29 4191 02:31:46,600 --> 02:31:50,319 means it's sort of 29 characters that 4192 02:31:48,560 --> 02:31:52,000 way and then it's actually in this case 4193 02:31:50,319 --> 02:31:53,960 giving me a suggestion the compiler 4194 02:31:52,000 --> 02:31:55,760 won't always know how to advise me 4195 02:31:53,960 --> 02:31:58,279 especially if I've made a real mess of 4196 02:31:55,760 --> 02:32:02,040 my code but often it will do its best to 4197 02:31:58,279 --> 02:32:02,040 give you the answer like this 4198 02:32:02,880 --> 02:32:09,080 yeah ah so how come I first typed code 4199 02:32:06,640 --> 02:32:11,120 space hello.c and now I'm typing make 4200 02:32:09,080 --> 02:32:14,160 hello two different processes so when I 4201 02:32:11,120 --> 02:32:15,840 typed code space hello.c that was 4202 02:32:14,160 --> 02:32:17,840 because I wanted to open vs code and 4203 02:32:15,840 --> 02:32:20,439 create a new new file called hello.c 4204 02:32:17,840 --> 02:32:22,800 it's like going to file new in in a a 4205 02:32:20,439 --> 02:32:25,279 Mac or PC thereafter though once the 4206 02:32:22,800 --> 02:32:26,880 file exists and is actually open here 4207 02:32:25,279 --> 02:32:29,319 and it does autosave you don't need to 4208 02:32:26,880 --> 02:32:31,800 hit command s or control s all the time 4209 02:32:29,319 --> 02:32:33,640 I can now compile it with make hello 4210 02:32:31,800 --> 02:32:36,479 again and again so theoretically I 4211 02:32:33,640 --> 02:32:38,319 should never need to type code space 4212 02:32:36,479 --> 02:32:40,840 hello.c again unless I want to create a 4213 02:32:38,319 --> 02:32:43,040 brand new file called the same thing all 4214 02:32:40,840 --> 02:32:44,800 right so what about this other piece of 4215 02:32:43,040 --> 02:32:46,920 syntax here let me clear my terminal 4216 02:32:44,800 --> 02:32:48,439 window here you can also hit control L 4217 02:32:46,920 --> 02:32:50,960 just to throw everything away just to 4218 02:32:48,439 --> 02:32:53,040 clean it up aesthetically suppose that I 4219 02:32:50,960 --> 02:32:55,520 omit whatever this sequence of symbols 4220 02:32:53,040 --> 02:32:58,240 is back sln since I'm not really sure at 4221 02:32:55,520 --> 02:32:59,920 first glance why that's even there does 4222 02:32:58,240 --> 02:33:01,880 anyone want to conjecture especially if 4223 02:32:59,920 --> 02:33:04,800 you've never programmed before what 4224 02:33:01,880 --> 02:33:07,520 might happen now if I recompile and 4225 02:33:04,800 --> 02:33:09,680 rerun this version of the program I left 4226 02:33:07,520 --> 02:33:10,800 the semicolon but I took away the backs 4227 02:33:09,680 --> 02:33:13,960 slash 4228 02:33:10,800 --> 02:33:17,120 n any 4229 02:33:13,960 --> 02:33:19,160 instincts all right well yeah 4230 02:33:17,120 --> 02:33:22,120 will the next dollar sign appear 4231 02:33:19,160 --> 02:33:24,479 straight after your it will the next 4232 02:33:22,120 --> 02:33:27,000 dollar sign will appear right after my 4233 02:33:24,479 --> 02:33:30,880 hello world but what makes you think 4234 02:33:27,000 --> 02:33:32,720 that back creates a line exactly back 4235 02:33:30,880 --> 02:33:35,399 sln is actually a special sequence of 4236 02:33:32,720 --> 02:33:37,319 symbols that creates a new line and so 4237 02:33:35,399 --> 02:33:40,600 to your point if I recompile this 4238 02:33:37,319 --> 02:33:42,600 program make hello enter no syntax error 4239 02:33:40,600 --> 02:33:44,600 so it did compile this time so you don't 4240 02:33:42,600 --> 02:33:46,800 need the backs slash in you do need the 4241 02:33:44,600 --> 02:33:49,399 semicolon but if you don't have the back 4242 02:33:46,800 --> 02:33:52,120 sln watch what happens when I do/ hello 4243 02:33:49,399 --> 02:33:53,920 this time now indeed I see hello comma 4244 02:33:52,120 --> 02:33:55,920 world and then a weird dollar sign and 4245 02:33:53,920 --> 02:33:57,880 this is still a prompt I can still type 4246 02:33:55,920 --> 02:33:59,680 commands at it like clear and everything 4247 02:33:57,880 --> 02:34:02,279 gets cleaned up but it just looks kind 4248 02:33:59,680 --> 02:34:04,319 of stupid if I run it again here with 4249 02:34:02,279 --> 02:34:06,040 hello you know it's just not very user 4250 02:34:04,319 --> 02:34:07,279 friendly it is convention that when 4251 02:34:06,040 --> 02:34:09,160 you're done running your program you 4252 02:34:07,279 --> 02:34:11,160 should ideally clean things up move the 4253 02:34:09,160 --> 02:34:13,080 cursor to the next line for the user and 4254 02:34:11,160 --> 02:34:15,720 so the backs slash n is simply the 4255 02:34:13,080 --> 02:34:16,600 special symbol otherwise known as an 4256 02:34:15,720 --> 02:34:20,319 escape 4257 02:34:16,600 --> 02:34:21,960 sequence that c knows means move the 4258 02:34:20,319 --> 02:34:24,160 cursor to the next line in other 4259 02:34:21,960 --> 02:34:28,120 languages python among them uses the 4260 02:34:24,160 --> 02:34:32,399 same symbology as well now if I go back 4261 02:34:28,120 --> 02:34:34,359 to the code here and for instance I try 4262 02:34:32,399 --> 02:34:36,080 to do this differently like suppose I 4263 02:34:34,359 --> 02:34:37,680 don't put the backs slash N I just hit 4264 02:34:36,080 --> 02:34:39,880 enter like a normal person would in 4265 02:34:37,680 --> 02:34:42,120 Google Docs or Microsoft Word let me go 4266 02:34:39,880 --> 02:34:43,680 ahead and try compiling this program and 4267 02:34:42,120 --> 02:34:45,080 this you would hope would work right you 4268 02:34:43,680 --> 02:34:47,120 would hope this would print out hello 4269 02:34:45,080 --> 02:34:49,399 world and then a blank line because I 4270 02:34:47,120 --> 02:34:51,319 move the cursor to the next line but no 4271 02:34:49,399 --> 02:34:54,160 if I run make hello now and try to 4272 02:34:51,319 --> 02:34:56,359 compile that c does not like this now I 4273 02:34:54,160 --> 02:34:58,840 get a different error still on line five 4274 02:34:56,359 --> 02:35:01,600 this time starting at character 12 uh 4275 02:34:58,840 --> 02:35:03,439 error missing terminating double quote 4276 02:35:01,600 --> 02:35:04,880 character and then some other esoteric 4277 02:35:03,439 --> 02:35:07,000 stuff and then this does not sound good 4278 02:35:04,880 --> 02:35:08,960 fatal error this time too many errors 4279 02:35:07,000 --> 02:35:12,160 admitted stopping now so I really 4280 02:35:08,960 --> 02:35:13,600 screwed up here so why can't I do this 4281 02:35:12,160 --> 02:35:15,439 just because like the humans who 4282 02:35:13,600 --> 02:35:17,560 designed C decided that if you have a 4283 02:35:15,439 --> 02:35:20,479 string of text it must stay on the same 4284 02:35:17,560 --> 02:35:22,840 line it can get really long it can soft 4285 02:35:20,479 --> 02:35:24,479 wrap that is without you hitting enter 4286 02:35:22,840 --> 02:35:26,800 but you can't hit enter to create a new 4287 02:35:24,479 --> 02:35:29,160 line if you deliberately want a new line 4288 02:35:26,800 --> 02:35:31,319 you have to indeed use this back slash 4289 02:35:29,160 --> 02:35:33,800 and Escape character so let me go ahead 4290 02:35:31,319 --> 02:35:35,680 and do this let me put it back let me go 4291 02:35:33,800 --> 02:35:37,359 back to my terminal window I'll clear 4292 02:35:35,680 --> 02:35:39,439 the screen again let me go ahead now and 4293 02:35:37,359 --> 02:35:41,920 do make Hello to recompile to that 4294 02:35:39,439 --> 02:35:44,960 version do/ hello and voila we're back 4295 02:35:41,920 --> 02:35:46,479 in business with uh hello all right so 4296 02:35:44,960 --> 02:35:48,080 now let's tease apart some some other 4297 02:35:46,479 --> 02:35:50,439 aspects of this code because there's a 4298 02:35:48,080 --> 02:35:52,600 lot going on just to get us to say hello 4299 02:35:50,439 --> 02:35:54,680 world on the screen for today we're 4300 02:35:52,600 --> 02:35:56,920 largely going to ignore this int main 4301 02:35:54,680 --> 02:35:58,640 void and these curly braces here we'll 4302 02:35:56,920 --> 02:36:00,840 come back to that before long as to why 4303 02:35:58,640 --> 02:36:03,160 it's there but for now just think of int 4304 02:36:00,840 --> 02:36:04,800 main void and these curly braces here as 4305 02:36:03,160 --> 02:36:06,520 really being the C equivalent of when 4306 02:36:04,800 --> 02:36:08,040 green flag clicked like why you just 4307 02:36:06,520 --> 02:36:10,160 need it there that's how you get your 4308 02:36:08,040 --> 02:36:11,960 program going and Maine is indeed going 4309 02:36:10,160 --> 02:36:14,120 to be some special function but more on 4310 02:36:11,960 --> 02:36:16,479 that another time but why do I have this 4311 02:36:14,120 --> 02:36:19,880 line of code here the cor spelling is 4312 02:36:16,479 --> 02:36:22,960 indeed standard io. 4313 02:36:19,880 --> 02:36:24,479 stdio.h and they're angled brackets this 4314 02:36:22,960 --> 02:36:27,080 time so that's a little new there's a 4315 02:36:24,479 --> 02:36:28,520 hash and then an include keyword you 4316 02:36:27,080 --> 02:36:29,680 know if you don't know what something is 4317 02:36:28,520 --> 02:36:31,120 you know there's not really that much 4318 02:36:29,680 --> 02:36:32,920 harm in just getting rid of it and see 4319 02:36:31,120 --> 02:36:34,760 what happens so let me delete that line 4320 02:36:32,920 --> 02:36:36,520 let me go back to my terminal window 4321 02:36:34,760 --> 02:36:38,000 clear the screen and then run make hello 4322 02:36:36,520 --> 02:36:40,399 again and let's try compiling this 4323 02:36:38,000 --> 02:36:42,040 program now without that first line why 4324 02:36:40,399 --> 02:36:44,200 I don't understand it so let's see what 4325 02:36:42,040 --> 02:36:47,399 happens all right here's yet another 4326 02:36:44,200 --> 02:36:50,120 error but let's see how hello. C line 4327 02:36:47,399 --> 02:36:52,680 five character 5 so it's pretty early on 4328 02:36:50,120 --> 02:36:54,920 error implicitly declaring Library 4329 02:36:52,680 --> 02:36:57,720 function printf with type int and then 4330 02:36:54,920 --> 02:37:00,160 dot dot dot so implicitly declaring 4331 02:36:57,720 --> 02:37:02,080 Library function print F so this is very 4332 02:37:00,160 --> 02:37:03,960 cryptic sounding you'll get better at 4333 02:37:02,080 --> 02:37:07,040 understanding phrases like these but 4334 02:37:03,960 --> 02:37:08,399 apparently I do need the include line 4335 02:37:07,040 --> 02:37:13,240 for standard 4336 02:37:08,399 --> 02:37:16,120 i.h but why based on this symptom what 4337 02:37:13,240 --> 02:37:18,399 might Your Instinct be for what that 4338 02:37:16,120 --> 02:37:21,319 first line of code is doing for us in 4339 02:37:18,399 --> 02:37:23,479 the first place why intuitively must it 4340 02:37:21,319 --> 02:37:25,880 be 4341 02:37:23,479 --> 02:37:27,399 there exactly it's like importing a 4342 02:37:25,880 --> 02:37:29,160 library so that you can do things like 4343 02:37:27,399 --> 02:37:30,640 print things out on the screen now in 4344 02:37:29,160 --> 02:37:32,279 scratch you didn't have to do this for 4345 02:37:30,640 --> 02:37:34,720 most of the puzzle pieces but you might 4346 02:37:32,279 --> 02:37:36,720 recall that partway in through week zero 4347 02:37:34,720 --> 02:37:38,520 I went to the extensions button at the 4348 02:37:36,720 --> 02:37:40,520 bottom left of the scratch screen and I 4349 02:37:38,520 --> 02:37:43,040 imported some extra puzzle pieces for 4350 02:37:40,520 --> 02:37:45,399 text to speech that gave us the sort of 4351 02:37:43,040 --> 02:37:47,160 creepy uh humanized voice that actually 4352 02:37:45,399 --> 02:37:49,680 came out of the cat's mouth well that 4353 02:37:47,160 --> 02:37:51,920 was like adding a library code that 4354 02:37:49,680 --> 02:37:54,279 someone else wrote in that case it was a 4355 02:37:51,920 --> 02:37:56,920 third party then but I gave myself 4356 02:37:54,279 --> 02:37:58,840 access to it same here turns out that 4357 02:37:56,920 --> 02:38:02,240 you don't really get printf 4358 02:37:58,840 --> 02:38:05,240 automatically in C you have to include a 4359 02:38:02,240 --> 02:38:07,520 so-called header file that declares that 4360 02:38:05,240 --> 02:38:09,200 function to exist now the reason for 4361 02:38:07,520 --> 02:38:10,640 this historically is just efficiency 4362 02:38:09,200 --> 02:38:13,640 back in the day when computers were 4363 02:38:10,640 --> 02:38:14,880 really slower and resource constrained 4364 02:38:13,640 --> 02:38:16,600 you don't want to just give yourself 4365 02:38:14,880 --> 02:38:18,920 access to the entire kitchen sync of 4366 02:38:16,600 --> 02:38:20,880 functionality you only want to include 4367 02:38:18,920 --> 02:38:24,040 only the functions you actually care 4368 02:38:20,880 --> 02:38:25,760 about nowadays it's sort of a sort of a 4369 02:38:24,040 --> 02:38:27,120 a copy paste step because you almost 4370 02:38:25,760 --> 02:38:28,279 always want to print something out on 4371 02:38:27,120 --> 02:38:30,080 the screen at least when writing 4372 02:38:28,279 --> 02:38:32,479 programs like these but these so-called 4373 02:38:30,080 --> 02:38:34,240 header files contain enough information 4374 02:38:32,479 --> 02:38:37,319 about all of the functions in What's 4375 02:38:34,240 --> 02:38:39,840 called the standard IO library and 4376 02:38:37,319 --> 02:38:41,359 standard IO just means standard input in 4377 02:38:39,840 --> 02:38:43,760 output and that's appropriate right 4378 02:38:41,359 --> 02:38:45,279 because printing is pretty basic output 4379 02:38:43,760 --> 02:38:46,920 turns out there's other functions for 4380 02:38:45,279 --> 02:38:48,720 getting input from the human's keyboard 4381 02:38:46,920 --> 02:38:49,960 more on that in a bit but anytime you 4382 02:38:48,720 --> 02:38:52,279 want to print something on the screen 4383 02:38:49,960 --> 02:38:54,600 and see you indeed need to include this 4384 02:38:52,279 --> 02:38:56,439 header file at the top of your code and 4385 02:38:54,600 --> 02:38:59,840 that's going to essentially inform the 4386 02:38:56,439 --> 02:39:01,840 compiler hey compiler I want to use 4387 02:38:59,840 --> 02:39:04,840 functionality from the standard IO 4388 02:39:01,840 --> 02:39:06,800 Library including printf in this case 4389 02:39:04,840 --> 02:39:08,560 and if you omit the header file by 4390 02:39:06,800 --> 02:39:10,600 accident it's just not going to work 4391 02:39:08,560 --> 02:39:13,479 because it doesn't know what print f is 4392 02:39:10,600 --> 02:39:16,040 it's sort of some unrecognized symbol in 4393 02:39:13,479 --> 02:39:18,479 that case all right questions then about 4394 02:39:16,040 --> 02:39:21,279 this line of 4395 02:39:18,479 --> 02:39:23,279 code this line of code here or what 4396 02:39:21,279 --> 02:39:25,800 these header files 4397 02:39:23,279 --> 02:39:27,720 are all right you might wonder well how 4398 02:39:25,800 --> 02:39:29,720 do you know what functions exist how do 4399 02:39:27,720 --> 02:39:31,560 you know what files you might indeed 4400 02:39:29,720 --> 02:39:33,760 want to include well it turns out that c 4401 02:39:31,560 --> 02:39:36,439 is a many-year old language and it has 4402 02:39:33,760 --> 02:39:38,439 ample documentation a caveat is that its 4403 02:39:36,439 --> 02:39:40,840 documentation isn't necessarily all that 4404 02:39:38,439 --> 02:39:43,240 userfriendly but what we have for the 4405 02:39:40,840 --> 02:39:46,120 course is a simplified version of the 4406 02:39:43,240 --> 02:39:49,840 official documentation for C at this URL 4407 02:39:46,120 --> 02:39:51,359 here manual. cs50. so in the world of c 4408 02:39:49,840 --> 02:39:53,200 and other languages too there are what 4409 02:39:51,359 --> 02:39:55,680 are called manual pages and these are 4410 02:39:53,200 --> 02:39:57,600 just like text-based documentation that 4411 02:39:55,680 --> 02:39:58,960 honestly is typically written in a voice 4412 02:39:57,600 --> 02:40:00,520 that you kind of have to be an 4413 02:39:58,960 --> 02:40:02,359 experienced programmer to understand 4414 02:40:00,520 --> 02:40:04,399 some of it so what we've done it this 4415 02:40:02,359 --> 02:40:06,040 version of the same documentation is 4416 02:40:04,399 --> 02:40:08,120 we've imported all of the original 4417 02:40:06,040 --> 02:40:10,319 official documentation but we've added 4418 02:40:08,120 --> 02:40:12,080 sort of less comfortable translations in 4419 02:40:10,319 --> 02:40:13,640 English for a lot of the functionality 4420 02:40:12,080 --> 02:40:15,240 that you might use in class just to help 4421 02:40:13,640 --> 02:40:17,319 onboard you so at the end of the day you 4422 02:40:15,240 --> 02:40:19,120 don't need this documentation long term 4423 02:40:17,319 --> 02:40:21,479 but just to get started we'll translate 4424 02:40:19,120 --> 02:40:23,359 it into terminology uh that you might 4425 02:40:21,479 --> 02:40:25,200 appreciate from a teaching assistant for 4426 02:40:23,359 --> 02:40:27,000 instance as opposed to the original 4427 02:40:25,200 --> 02:40:28,399 author of these documents and so for 4428 02:40:27,000 --> 02:40:31,680 instance if you were interested in 4429 02:40:28,399 --> 02:40:34,960 reading up on what functions exist in 4430 02:40:31,680 --> 02:40:37,040 the standard io. um header file well you 4431 02:40:34,960 --> 02:40:39,680 could go to a URL like this or you could 4432 02:40:37,040 --> 02:40:40,840 search for it at manual. cs50. that 4433 02:40:39,680 --> 02:40:43,200 would show you a list of all of the 4434 02:40:40,840 --> 02:40:45,080 available functions in that library and 4435 02:40:43,200 --> 02:40:46,520 print F indeed would be one of them and 4436 02:40:45,080 --> 02:40:48,120 then you could click further on that 4437 02:40:46,520 --> 02:40:49,080 reaching a URL like this that's just 4438 02:40:48,120 --> 02:40:50,680 going to give you all of the 4439 02:40:49,080 --> 02:40:51,920 documentation for how to use print def 4440 02:40:50,680 --> 02:40:54,080 it turns out you can do even more than 4441 02:40:51,920 --> 02:40:55,479 it uh than just printing out hello world 4442 02:40:54,080 --> 02:40:57,960 and we'll scratch the surface of that 4443 02:40:55,479 --> 02:40:59,840 today but it turns out that the 4444 02:40:57,960 --> 02:41:01,640 documentation will always be your 4445 02:40:59,840 --> 02:41:03,680 authoritative Source ultimately for 4446 02:41:01,640 --> 02:41:07,920 questions like what can I do and how can 4447 02:41:03,680 --> 02:41:10,560 I do it meanwhile it turns out that cs50 4448 02:41:07,920 --> 02:41:13,200 has its own library in A Accessible via 4449 02:41:10,560 --> 02:41:16,120 header file called cs50.h it turns out 4450 02:41:13,200 --> 02:41:17,760 in C that output is actually pretty easy 4451 02:41:16,120 --> 02:41:19,720 relatively speaking once you get used to 4452 02:41:17,760 --> 02:41:22,120 all the curly braces parentheses quote 4453 02:41:19,720 --> 02:41:23,920 marks and the like but input is a little 4454 02:41:22,120 --> 02:41:26,479 more difficult and if you have 4455 02:41:23,920 --> 02:41:28,640 programmed before input's not that hard 4456 02:41:26,479 --> 02:41:31,279 to do in Python it's not that hard to do 4457 02:41:28,640 --> 02:41:32,960 in Java it's more difficult to do in C 4458 02:41:31,279 --> 02:41:34,439 and we'll see why in a couple of weeks 4459 02:41:32,960 --> 02:41:36,640 but for the first couple of weeks of the 4460 02:41:34,439 --> 02:41:39,040 class we actually provide you with some 4461 02:41:36,640 --> 02:41:41,040 training wheels of sorts whereby we have 4462 02:41:39,040 --> 02:41:43,920 a number of functions that are declared 4463 02:41:41,040 --> 02:41:45,880 in this file cs50.h it lives its 4464 02:41:43,920 --> 02:41:47,800 documentation at a URL like this this 4465 02:41:45,880 --> 02:41:49,760 and in a moment we'll use a few of these 4466 02:41:47,800 --> 02:41:52,479 you'll see that cs50 provides you with 4467 02:41:49,760 --> 02:41:54,240 some functions like get Char forget a 4468 02:41:52,479 --> 02:41:56,880 single character from the user's 4469 02:41:54,240 --> 02:41:59,040 keyboard uh get int to get an integer 4470 02:41:56,880 --> 02:42:00,600 from the user's keyboard uh get string 4471 02:41:59,040 --> 02:42:02,920 to get a sequence of text from the 4472 02:42:00,600 --> 02:42:05,560 user's keyboard and a bunch of others as 4473 02:42:02,920 --> 02:42:08,040 well so let's actually use some of these 4474 02:42:05,560 --> 02:42:09,760 functions how about by revisiting really 4475 02:42:08,040 --> 02:42:12,720 the second program we wrote in scratch 4476 02:42:09,760 --> 02:42:14,520 last time which adds some input to the 4477 02:42:12,720 --> 02:42:16,200 output so first version of scratch was 4478 02:42:14,520 --> 02:42:17,920 just hello world said the same thing 4479 02:42:16,200 --> 02:42:20,000 every time you click the green flag 4480 02:42:17,920 --> 02:42:22,640 version two recall though did this it 4481 02:42:20,000 --> 02:42:25,160 asked the user what's your name and then 4482 02:42:22,640 --> 02:42:27,640 that somehow gave it back a a return 4483 02:42:25,160 --> 02:42:29,800 variable return value we called it and 4484 02:42:27,640 --> 02:42:31,960 we then joined hello in that name to say 4485 02:42:29,800 --> 02:42:34,680 something a little more interesting on 4486 02:42:31,960 --> 02:42:36,600 the screen so what did that model look 4487 02:42:34,680 --> 02:42:38,399 like same thing as before we've got a 4488 02:42:36,600 --> 02:42:40,279 function in the Middle where function is 4489 02:42:38,399 --> 02:42:42,240 like the code implementation of our 4490 02:42:40,279 --> 02:42:44,640 algorithm that takes in one or more 4491 02:42:42,240 --> 02:42:47,080 arguments like what is it you want to uh 4492 02:42:44,640 --> 02:42:48,800 say on the the screen ultimately and 4493 02:42:47,080 --> 02:42:50,319 return value in this case is going to be 4494 02:42:48,800 --> 02:42:52,520 actually a value that comes back so in 4495 02:42:50,319 --> 02:42:54,680 the case of getting input we can 4496 02:42:52,520 --> 02:42:57,080 consider this ask block again like last 4497 02:42:54,680 --> 02:42:59,279 week the input to it is whatever words 4498 02:42:57,080 --> 02:43:01,680 of English you want to ask the user and 4499 02:42:59,279 --> 02:43:04,160 then it returns a value and this was 4500 02:43:01,680 --> 02:43:05,560 called by default in mit's World answer 4501 02:43:04,160 --> 02:43:07,399 that we'll see and see you can call 4502 02:43:05,560 --> 02:43:09,520 these return values anything you want 4503 02:43:07,399 --> 02:43:10,920 ultimately in variables but this is 4504 02:43:09,520 --> 02:43:12,560 different from a side effect a side 4505 02:43:10,920 --> 02:43:13,760 effect is just something visual often 4506 02:43:12,560 --> 02:43:16,279 that happens on the screen like the 4507 02:43:13,760 --> 02:43:18,200 speech bubble or hello world a return 4508 02:43:16,279 --> 02:43:19,880 value is actually a value you get back 4509 02:43:18,200 --> 02:43:22,319 from a function that you can use or 4510 02:43:19,880 --> 02:43:25,080 reuse so how do we convert this scratch 4511 02:43:22,319 --> 02:43:26,760 block from last week to C code this week 4512 02:43:25,080 --> 02:43:28,680 well if you want to ask the user for 4513 02:43:26,760 --> 02:43:31,359 something like their name you can do 4514 02:43:28,680 --> 02:43:34,040 this you use a cs50 function called get 4515 02:43:31,359 --> 02:43:36,000 string and you use the parentheses to 4516 02:43:34,040 --> 02:43:38,359 represent here comes the inputs there 4517 02:43:36,000 --> 02:43:40,279 too you can then put the sentence you 4518 02:43:38,359 --> 02:43:42,000 want to ask the user quote unquote 4519 02:43:40,279 --> 02:43:44,359 what's your name but you do indeed need 4520 02:43:42,000 --> 02:43:46,760 the quotes literally in C so I'll go 4521 02:43:44,359 --> 02:43:49,040 ahead and add those as well well subtle 4522 02:43:46,760 --> 02:43:51,439 but I've deliberately included a space 4523 02:43:49,040 --> 02:43:53,439 after the question mark but before the 4524 02:43:51,439 --> 02:43:55,680 double quote just so that the cursor 4525 02:43:53,439 --> 02:43:57,000 moves one step over because in this case 4526 02:43:55,680 --> 02:43:58,840 we're not going to get a special speech 4527 02:43:57,000 --> 02:44:00,439 box like we did in scratch it's just 4528 02:43:58,840 --> 02:44:01,760 going to leave the cursor where it is so 4529 02:44:00,439 --> 02:44:04,040 we'll see that aesthetically this just 4530 02:44:01,760 --> 02:44:06,680 moves the blinking cursor one space 4531 02:44:04,040 --> 02:44:08,720 after the sentence on the screen all 4532 02:44:06,680 --> 02:44:11,080 right but the catch is with scratch we 4533 02:44:08,720 --> 02:44:12,760 just automatically got back the answer 4534 02:44:11,080 --> 02:44:14,920 from the user in a special variable 4535 02:44:12,760 --> 02:44:18,359 called answer in C you're going to have 4536 02:44:14,920 --> 02:44:20,680 to be a little more specific in C if you 4537 02:44:18,359 --> 02:44:23,439 want to get back a return value from a 4538 02:44:20,680 --> 02:44:25,760 function like get string you have to use 4539 02:44:23,439 --> 02:44:27,680 an equal sign and then the name of a 4540 02:44:25,760 --> 02:44:29,200 variable on the left the choice of 4541 02:44:27,680 --> 02:44:31,200 variables is up to you I could have 4542 02:44:29,200 --> 02:44:33,200 called this anything X YZ I'm going to 4543 02:44:31,200 --> 02:44:35,640 more descriptively call it answer for 4544 02:44:33,200 --> 02:44:37,960 parody with what MIT did with scratch 4545 02:44:35,640 --> 02:44:41,479 but notice that this doesn't represent 4546 02:44:37,960 --> 02:44:43,920 equality per se this is assignment in 4547 02:44:41,479 --> 02:44:45,960 this case so in C when you use a single 4548 02:44:43,920 --> 02:44:48,399 equal sign that means means copy the 4549 02:44:45,960 --> 02:44:50,760 value on the right over to the value on 4550 02:44:48,399 --> 02:44:53,000 the left from right to left so what does 4551 02:44:50,760 --> 02:44:54,680 this do for us well if get string is a 4552 02:44:53,000 --> 02:44:56,359 function that prompts the user with 4553 02:44:54,680 --> 02:44:59,359 quote unquote what's your name and it 4554 02:44:56,359 --> 02:45:01,399 has I claim a return value that means it 4555 02:44:59,359 --> 02:45:03,840 kind of hands me back some value but 4556 02:45:01,399 --> 02:45:05,560 it's up to me in C to do something with 4557 02:45:03,840 --> 02:45:07,399 that value so if I want to copy that 4558 02:45:05,560 --> 02:45:10,200 value into a variable that I can use and 4559 02:45:07,399 --> 02:45:12,000 reuse I use an equal sign and I invent 4560 02:45:10,200 --> 02:45:14,520 on the left hand side of that equal sign 4561 02:45:12,000 --> 02:45:16,600 any variable name I want there's certain 4562 02:45:14,520 --> 02:45:18,359 rules certain conventions but generally 4563 02:45:16,600 --> 02:45:21,880 if you use a single word with all 4564 02:45:18,359 --> 02:45:23,640 lowercase you're in good shape but C is 4565 02:45:21,880 --> 02:45:25,160 a little more pedantic than that and 4566 02:45:23,640 --> 02:45:26,880 those of you who have have programmed 4567 02:45:25,160 --> 02:45:28,359 before might not be used to this for 4568 02:45:26,880 --> 02:45:30,600 instance in Python which is a world 4569 02:45:28,359 --> 02:45:34,000 we'll get to in a few weeks you also 4570 02:45:30,600 --> 02:45:37,439 have to tell see what type of value 4571 02:45:34,000 --> 02:45:40,720 you're storing so if I do want a string 4572 02:45:37,439 --> 02:45:42,439 of text from the user so not an integer 4573 02:45:40,720 --> 02:45:44,439 not a single character I want a whole 4574 02:45:42,439 --> 02:45:46,680 string of text like a phrase a sentence 4575 02:45:44,439 --> 02:45:50,880 a name in this this case I have to tell 4576 02:45:46,680 --> 02:45:52,680 C that this variable is of type string 4577 02:45:50,880 --> 02:45:54,160 so it's a little wordy but you get used 4578 02:45:52,680 --> 02:45:56,279 to it and you just have to be precise 4579 02:45:54,160 --> 02:45:58,399 you're informing the computer what type 4580 02:45:56,279 --> 02:46:00,279 of value is going in this 4581 02:45:58,399 --> 02:46:03,200 variable all right it's so close to 4582 02:46:00,279 --> 02:46:04,920 being correct but I have omitted 4583 02:46:03,200 --> 02:46:08,880 something that's annoyingly important 4584 02:46:04,920 --> 02:46:11,160 still what's missing still yeah so 4585 02:46:08,880 --> 02:46:13,840 semicolon this is a statement this is 4586 02:46:11,160 --> 02:46:16,080 like a a a full thought if you will in 4587 02:46:13,840 --> 02:46:18,760 code I do need to end It ultimately with 4588 02:46:16,080 --> 02:46:20,359 the semicolon at the end there all right 4589 02:46:18,760 --> 02:46:22,600 so this was more of a mouthful but let's 4590 02:46:20,359 --> 02:46:25,040 try using this in now my code let me go 4591 02:46:22,600 --> 02:46:27,800 back to vs code where I have version 4592 02:46:25,040 --> 02:46:30,359 zero of my code here let me go ahead and 4593 02:46:27,800 --> 02:46:34,479 include one other file at the top of 4594 02:46:30,359 --> 02:46:36,520 hello.c namely include cs50.h so that I 4595 02:46:34,479 --> 02:46:39,040 have access now to get string and 4596 02:46:36,520 --> 02:46:42,000 anything else I might want now let me go 4597 02:46:39,040 --> 02:46:43,920 ahead and add a line of cod here inside 4598 02:46:42,000 --> 02:46:47,319 of these curly braces and let me go 4599 02:46:43,920 --> 02:46:50,040 ahead and do this string answer equals 4600 02:46:47,319 --> 02:46:52,120 get string quote unquote what's your 4601 02:46:50,040 --> 02:46:53,920 name question mark I'm going to add an 4602 02:46:52,120 --> 02:46:56,160 extra space before the double quote I'm 4603 02:46:53,920 --> 02:46:58,960 going to indeed end my thought with a 4604 02:46:56,160 --> 02:47:00,840 semicolon and now let me deliberately 4605 02:46:58,960 --> 02:47:03,600 make a mistake just to make a point here 4606 02:47:00,840 --> 02:47:07,680 let me now try changing hello world to 4607 02:47:03,600 --> 02:47:08,960 hello comma answer all right now perhaps 4608 02:47:07,680 --> 02:47:10,800 even though this is some new lines of 4609 02:47:08,960 --> 02:47:12,240 code you can see where I've aired 4610 02:47:10,800 --> 02:47:15,240 already but let me try making this 4611 02:47:12,240 --> 02:47:16,880 program now so far so good so no error 4612 02:47:15,240 --> 02:47:19,920 messages so that's a good thing let me 4613 02:47:16,880 --> 02:47:21,960 go ahead and run /hello and you'll see 4614 02:47:19,920 --> 02:47:23,840 the prompt what's your name question 4615 02:47:21,960 --> 02:47:25,160 mark and notice the cursor indeed one 4616 02:47:23,840 --> 02:47:26,479 space to the right just because I 4617 02:47:25,160 --> 02:47:28,479 thought it would look prettier to put a 4618 02:47:26,479 --> 02:47:30,240 little Blank Space there as opposed to 4619 02:47:28,479 --> 02:47:32,479 leaving it right after the question mark 4620 02:47:30,240 --> 02:47:34,359 let me type my name but even if you've 4621 02:47:32,479 --> 02:47:35,800 never programmed before I have screwed 4622 02:47:34,359 --> 02:47:38,120 up here what are we going to see on the 4623 02:47:35,800 --> 02:47:41,439 screen when I hit 4624 02:47:38,120 --> 02:47:42,800 enter yeah hello answer most likely why 4625 02:47:41,439 --> 02:47:44,680 cuz the computer is going to take me 4626 02:47:42,800 --> 02:47:47,000 literally and if I say quote unquote 4627 02:47:44,680 --> 02:47:48,479 hello answer that is the string of text 4628 02:47:47,000 --> 02:47:50,359 followed by a new line that's going to 4629 02:47:48,479 --> 02:47:52,760 be outputed to the screen so we need 4630 02:47:50,359 --> 02:47:55,920 some way of actually plugging answer 4631 02:47:52,760 --> 02:47:57,240 into this line of code it's not quite as 4632 02:47:55,920 --> 02:47:59,279 simple as scratch where you could just 4633 02:47:57,240 --> 02:48:01,120 grab like a second say block and drag 4634 02:47:59,279 --> 02:48:02,760 and drop the variable there we actually 4635 02:48:01,120 --> 02:48:05,040 need a new syntax and it's going to look 4636 02:48:02,760 --> 02:48:07,399 weird at first but it is everywhere in 4637 02:48:05,040 --> 02:48:09,640 software nowadays especially in the 4638 02:48:07,399 --> 02:48:12,520 world of c and certain other languages 4639 02:48:09,640 --> 02:48:15,080 so let me go ahead and propose that I 4640 02:48:12,520 --> 02:48:16,920 solve it as follows well back when we 4641 02:48:15,080 --> 02:48:19,160 did this in scratch remember that the 4642 02:48:16,920 --> 02:48:21,040 most elegant solution was this here we 4643 02:48:19,160 --> 02:48:23,479 used the say block still which is going 4644 02:48:21,040 --> 02:48:25,880 to be analogous to print F today but I 4645 02:48:23,479 --> 02:48:28,640 use the join puzzle piece in scratch to 4646 02:48:25,880 --> 02:48:30,960 combine hello comma space and then the 4647 02:48:28,640 --> 02:48:33,160 name of the human so how do we translate 4648 02:48:30,960 --> 02:48:35,120 this code to C well it's going to look a 4649 02:48:33,160 --> 02:48:37,560 little different now I'm going to start 4650 02:48:35,120 --> 02:48:40,439 with print f with some parth C's and a 4651 02:48:37,560 --> 02:48:42,120 semicolon representing the say block but 4652 02:48:40,439 --> 02:48:43,800 how do I now do this joining this is 4653 02:48:42,120 --> 02:48:45,800 where the puzzle pieces don't quite 4654 02:48:43,800 --> 02:48:48,680 translate perfectly this would be the 4655 02:48:45,800 --> 02:48:50,720 way to do this you put hello comma and 4656 02:48:48,680 --> 02:48:53,479 then a placeholder so this is what's 4657 02:48:50,720 --> 02:48:56,120 known as a format code in C specifically 4658 02:48:53,479 --> 02:48:58,520 for printf and it just means this is a 4659 02:48:56,120 --> 02:49:01,040 placeholder for a string again a string 4660 02:48:58,520 --> 02:49:03,720 is just text so this means hey computer 4661 02:49:01,040 --> 02:49:05,680 print out literally hello comma space 4662 02:49:03,720 --> 02:49:08,960 and then not literally percent s percent 4663 02:49:05,680 --> 02:49:11,160 s is uh treated specially to mean plug 4664 02:49:08,960 --> 02:49:12,920 in some value here all right so what 4665 02:49:11,160 --> 02:49:15,200 else do I still need well this is still 4666 02:49:12,920 --> 02:49:18,200 some text so I'm still going going to 4667 02:49:15,200 --> 02:49:20,880 surround the whole thing with double 4668 02:49:18,200 --> 02:49:22,800 quotes I'm still going to include my 4669 02:49:20,880 --> 02:49:25,000 back sln just to keep things tidy and 4670 02:49:22,800 --> 02:49:28,880 move the cursor to the next line so the 4671 02:49:25,000 --> 02:49:32,120 last step here in C is to somehow join 4672 02:49:28,880 --> 02:49:35,120 the answer with that word hello and the 4673 02:49:32,120 --> 02:49:37,080 way you do this is with printf passing 4674 02:49:35,120 --> 02:49:39,160 it not one argument which is what I keep 4675 02:49:37,080 --> 02:49:41,359 doing I keep passing it one string of 4676 02:49:39,160 --> 02:49:44,279 text quote unquote I'm going to now add 4677 02:49:41,359 --> 02:49:46,760 a comma and then the name of the value 4678 02:49:44,279 --> 02:49:50,080 that I want want printf to go back and 4679 02:49:46,760 --> 02:49:52,479 plug into that percent s and printf is 4680 02:49:50,080 --> 02:49:54,720 just smart about this if you have 1% s 4681 02:49:52,479 --> 02:49:56,800 and one additional argument after a 4682 02:49:54,720 --> 02:49:59,640 comma it just does from right to left it 4683 02:49:56,800 --> 02:50:02,560 plugs it in if you have two percent S's 4684 02:49:59,640 --> 02:50:03,840 and two variables after the comma that's 4685 02:50:02,560 --> 02:50:05,520 okay too if you separate them with 4686 02:50:03,840 --> 02:50:07,760 commas it'll plug the first into the 4687 02:50:05,520 --> 02:50:09,399 first percent s and the second variable 4688 02:50:07,760 --> 02:50:11,479 into the second percent s so it's just 4689 02:50:09,399 --> 02:50:14,000 left to right order of operations it's 4690 02:50:11,479 --> 02:50:17,399 not as pretty or as uh simple as this 4691 02:50:14,000 --> 02:50:18,840 but this is how it's done in C all right 4692 02:50:17,399 --> 02:50:21,000 let me pause because this is a lot of 4693 02:50:18,840 --> 02:50:22,420 symbology any questions on this 4694 02:50:21,000 --> 02:50:26,120 technique here 4695 02:50:22,420 --> 02:50:28,960 [Music] 4696 02:50:26,120 --> 02:50:32,279 yeah yeah really good question why did I 4697 02:50:28,960 --> 02:50:34,399 exclude the backslash n a moment ago 4698 02:50:32,279 --> 02:50:36,520 really just my sense of Aesthetics if 4699 02:50:34,399 --> 02:50:38,439 you will No Good Reason beyond that so 4700 02:50:36,520 --> 02:50:39,920 if I look back at my code you quite 4701 02:50:38,439 --> 02:50:42,040 rightly noticed that I didn't have a 4702 02:50:39,920 --> 02:50:44,040 backslash in there that's just because 4703 02:50:42,040 --> 02:50:46,040 for whatever sense of style that I have 4704 02:50:44,040 --> 02:50:48,359 I wanted the name to be typed right 4705 02:50:46,040 --> 02:50:50,200 after the question I totally could have 4706 02:50:48,359 --> 02:50:52,040 added a back sln there instead of a 4707 02:50:50,200 --> 02:50:54,399 space that would have just allowed me to 4708 02:50:52,040 --> 02:50:56,319 type down here totally fine just wanted 4709 02:50:54,399 --> 02:50:57,720 to show you something different good 4710 02:50:56,319 --> 02:51:01,160 good catch 4711 02:50:57,720 --> 02:51:04,040 yeah can I show an example with 2% s's 4712 02:51:01,160 --> 02:51:05,520 surely so let me uh in vs code do this 4713 02:51:04,040 --> 02:51:08,040 let me clear my terminal window to clean 4714 02:51:05,520 --> 02:51:09,760 things up and let me do this instead of 4715 02:51:08,040 --> 02:51:13,239 calling the variable answer all over the 4716 02:51:09,760 --> 02:51:15,560 place let me call it uh first and I'll 4717 02:51:13,239 --> 02:51:18,239 ask two questions what's your first name 4718 02:51:15,560 --> 02:51:20,479 and now let me do string last equals get 4719 02:51:18,239 --> 02:51:22,720 string quot whoops capitalization 4720 02:51:20,479 --> 02:51:25,680 matters so let me fix my capital S there 4721 02:51:22,720 --> 02:51:29,920 quote unquote what's your last name 4722 02:51:25,680 --> 02:51:32,920 question mark semicolon and now we'll 4723 02:51:29,920 --> 02:51:35,279 plug in one percent s and a second 4724 02:51:32,920 --> 02:51:39,479 percent s and now I'm going to plug in 4725 02:51:35,279 --> 02:51:40,800 first first and last last coincidentally 4726 02:51:39,479 --> 02:51:44,120 and now I'm going to go back to the 4727 02:51:40,800 --> 02:51:45,160 terminal window make hello crossing my 4728 02:51:44,120 --> 02:51:48,359 fingers 4729 02:51:45,160 --> 02:51:51,479 all good/ hello here's my first question 4730 02:51:48,359 --> 02:51:54,080 David here's my second question me and 4731 02:51:51,479 --> 02:51:55,960 again hello David Ma so it just inserts 4732 02:51:54,080 --> 02:51:57,640 them left to right all I was doing for 4733 02:51:55,960 --> 02:51:59,880 parody with scratch though and let me go 4734 02:51:57,640 --> 02:52:03,399 ahead and undo this again I'll go back 4735 02:51:59,880 --> 02:52:04,760 to answer like this I'll go back to just 4736 02:52:03,399 --> 02:52:06,279 asking for the person's name I'm going 4737 02:52:04,760 --> 02:52:08,520 to delete mention of last I'm going to 4738 02:52:06,279 --> 02:52:10,880 delete mention of the second perc s and 4739 02:52:08,520 --> 02:52:14,520 now if I recompile this simpler 4740 02:52:10,880 --> 02:52:17,120 version I did screw up didn't intend it 4741 02:52:14,520 --> 02:52:17,120 what did I do 4742 02:52:17,640 --> 02:52:22,800 wrong yeah so just newbie mistakes so I 4743 02:52:20,640 --> 02:52:24,760 changed my variable back to answer just 4744 02:52:22,800 --> 02:52:27,279 to be consistent with week zero but I 4745 02:52:24,760 --> 02:52:29,479 didn't change it here so I have an use 4746 02:52:27,279 --> 02:52:31,560 of Undeclared identifier first it's 4747 02:52:29,479 --> 02:52:34,040 Undeclared in the sense that I declared 4748 02:52:31,560 --> 02:52:36,399 answer align prior I didn't declare 4749 02:52:34,040 --> 02:52:38,319 first so indeed intuitively I want to 4750 02:52:36,399 --> 02:52:42,239 just change that to that let me now do 4751 02:52:38,319 --> 02:52:44,479 make hello again /hello type in just my 4752 02:52:42,239 --> 02:52:47,680 first name this time and there it is 4753 02:52:44,479 --> 02:52:50,920 hello David questions on this then 4754 02:52:47,680 --> 02:52:53,600 syntax with print F 4755 02:52:50,920 --> 02:52:56,520 yeah uh the placeholder I'll zoom in is 4756 02:52:53,600 --> 02:53:01,600 just a single percent and then an S so 4757 02:52:56,520 --> 02:53:03,399 inside of my string here is percent s 4758 02:53:01,600 --> 02:53:05,520 and then I have a comma outside the 4759 02:53:03,399 --> 02:53:09,000 quotes and then the name of the variable 4760 02:53:05,520 --> 02:53:11,920 whose value I want to plug in for that 4761 02:53:09,000 --> 02:53:15,040 percent s and now notice there's 4762 02:53:11,920 --> 02:53:17,720 technically two commas in side of these 4763 02:53:15,040 --> 02:53:20,760 parentheses on line seven and yet I 4764 02:53:17,720 --> 02:53:23,439 claim that printf at the moment is only 4765 02:53:20,760 --> 02:53:27,359 taking in two 4766 02:53:23,439 --> 02:53:29,239 arguments why is there then two commas 4767 02:53:27,359 --> 02:53:30,160 but only two arguments if there were two 4768 02:53:29,239 --> 02:53:33,439 commas you would think there would be 4769 02:53:30,160 --> 02:53:33,439 three arguments 4770 02:53:35,840 --> 02:53:39,960 right exactly the comma in between the 4771 02:53:38,279 --> 02:53:42,279 quotes is just an English thing it's 4772 02:53:39,960 --> 02:53:43,880 separating the hello from the name so 4773 02:53:42,279 --> 02:53:45,600 that's why indeed it's not only in 4774 02:53:43,880 --> 02:53:48,120 quotes that that's also why programs 4775 02:53:45,600 --> 02:53:49,920 like VSS code tend to syntax highlight 4776 02:53:48,120 --> 02:53:52,200 it a little differently just so that it 4777 02:53:49,920 --> 02:53:53,439 sort of jumps out as different to you 4778 02:53:52,200 --> 02:53:55,640 even though in this case it's a little 4779 02:53:53,439 --> 02:53:57,800 subtle a light blue versus white but 4780 02:53:55,640 --> 02:54:01,880 indeed it's trying its best other 4781 02:53:57,800 --> 02:54:01,880 questions now on this placeholder 4782 02:54:08,120 --> 02:54:12,479 yeah ah good question if I wanted to add 4783 02:54:10,399 --> 02:54:14,359 an exclamation point after the name 4784 02:54:12,479 --> 02:54:15,560 would I have to add another placeholder 4785 02:54:14,359 --> 02:54:17,760 and so forth I could actually do that 4786 02:54:15,560 --> 02:54:19,479 much more simply I can just put the 4787 02:54:17,760 --> 02:54:21,239 exclamation point right after the 4788 02:54:19,479 --> 02:54:24,080 percent sign I don't need an additional 4789 02:54:21,239 --> 02:54:27,080 placeholder per se if I zoom out now and 4790 02:54:24,080 --> 02:54:29,040 run make hello again/ hello and type in 4791 02:54:27,080 --> 02:54:31,479 just my name no exclamation point now 4792 02:54:29,040 --> 02:54:33,720 you'll see more excitedly hello comma 4793 02:54:31,479 --> 02:54:37,000 David So print f is smart it will figure 4794 02:54:33,720 --> 02:54:40,000 out where the percent s is and then go 4795 02:54:37,000 --> 02:54:41,720 and replace it now let me propose that a 4796 02:54:40,000 --> 02:54:43,160 common thing in programming is that as 4797 02:54:41,720 --> 02:54:44,880 soon as we make a decision as to how to 4798 02:54:43,160 --> 02:54:46,680 design something we often paint 4799 02:54:44,880 --> 02:54:50,040 ourselves into a corner and sort of 4800 02:54:46,680 --> 02:54:53,040 regret a decision can anyone think of a 4801 02:54:50,040 --> 02:54:54,200 problem that arises from using percent S 4802 02:54:53,040 --> 02:54:58,359 as a 4803 02:54:54,200 --> 02:54:59,880 placeholder in this string to print F 4804 02:54:58,359 --> 02:55:03,040 what could go wrong if we're using 4805 02:54:59,880 --> 02:55:03,040 percent in this special 4806 02:55:03,080 --> 02:55:07,800 way yeah if you literally want to say 4807 02:55:05,560 --> 02:55:09,279 for whatever weird reason percent s on 4808 02:55:07,800 --> 02:55:10,880 the screen or honestly even just a 4809 02:55:09,279 --> 02:55:14,040 single percent it turns out that a 4810 02:55:10,880 --> 02:55:17,200 percent sign is treated specially inside 4811 02:55:14,040 --> 02:55:19,239 of of print F strings so what's the 4812 02:55:17,200 --> 02:55:21,040 solution here there's different uh 4813 02:55:19,239 --> 02:55:25,359 patterns of solutions to problems like 4814 02:55:21,040 --> 02:55:28,359 these but suppose you wanted to say uh I 4815 02:55:25,359 --> 02:55:30,560 got 100% for instance let me let me go 4816 02:55:28,359 --> 02:55:34,040 ahead and change this completely so I 4817 02:55:30,560 --> 02:55:36,600 got 100% on your test or whatever all 4818 02:55:34,040 --> 02:55:39,319 right let me go ahead and run make hello 4819 02:55:36,600 --> 02:55:40,760 enter all right so invalid conversion 4820 02:55:39,319 --> 02:55:42,160 specifier I mean I have no idea what 4821 02:55:40,760 --> 02:55:44,520 this means but it's underlining the 4822 02:55:42,160 --> 02:55:46,760 percent sign is problematic well it 4823 02:55:44,520 --> 02:55:48,359 turns out that humans years ago decided 4824 02:55:46,760 --> 02:55:52,080 all right damn it we already Ed percent 4825 02:55:48,359 --> 02:55:56,160 well 2% signs will mean 1% literally so 4826 02:55:52,080 --> 02:55:58,120 now if I rerun make hello aha hello I 4827 02:55:56,160 --> 02:55:59,399 got 100% so there's going to be things 4828 02:55:58,120 --> 02:56:00,520 like that honestly that you have to ask 4829 02:55:59,399 --> 02:56:01,960 someone you have to Google you have to 4830 02:56:00,520 --> 02:56:03,640 look it up in the documentation but 4831 02:56:01,960 --> 02:56:05,200 there's always a solution to those kinds 4832 02:56:03,640 --> 02:56:08,399 of problems and thankfully they don't 4833 02:56:05,200 --> 02:56:10,090 come up all that often yeah oh just 4834 02:56:08,399 --> 02:56:13,120 poting other questions 4835 02:56:10,090 --> 02:56:16,439 [Music] 4836 02:56:13,120 --> 02:56:18,760 yeah if you have multiple variables it 4837 02:56:16,439 --> 02:56:20,760 is in the Left Right order so print F 4838 02:56:18,760 --> 02:56:22,399 will analyze the first string of text 4839 02:56:20,760 --> 02:56:24,720 that you pass in between quotes and 4840 02:56:22,399 --> 02:56:26,200 whatever the first percent is the next 4841 02:56:24,720 --> 02:56:28,080 the first variable that's passed in 4842 02:56:26,200 --> 02:56:29,479 after a comma gets plugged in there and 4843 02:56:28,080 --> 02:56:31,080 then the second gets plugged into the 4844 02:56:29,479 --> 02:56:33,000 second third and to the third and so 4845 02:56:31,080 --> 02:56:36,000 forth so it's just based on left to 4846 02:56:33,000 --> 02:56:36,000 right 4847 02:56:38,720 --> 02:56:43,279 yeah it's just a placeholder it's called 4848 02:56:41,040 --> 02:56:45,840 a format code and it just means 4849 02:56:43,279 --> 02:56:47,600 colloquially plug in some value here and 4850 02:56:45,840 --> 02:56:50,200 printf the like the humans who wrote 4851 02:56:47,600 --> 02:56:52,200 printf decades ago decided to treat 4852 02:56:50,200 --> 02:56:54,279 percent s special why just because they 4853 02:56:52,200 --> 02:56:55,560 needed some placeholder they decided 4854 02:56:54,279 --> 02:56:57,359 that eh no one's ever going to really 4855 02:56:55,560 --> 02:56:59,800 want to type percent s and if they do 4856 02:56:57,359 --> 02:57:01,600 they can just do percent percent s so 4857 02:56:59,800 --> 02:57:03,880 they decided to implement printf in such 4858 02:57:01,600 --> 02:57:05,920 a way that they have code that look 4859 02:57:03,880 --> 02:57:08,239 analyzes whatever text comes in looks 4860 02:57:05,920 --> 02:57:10,760 for percent s and then somehow plugs in 4861 02:57:08,239 --> 02:57:14,610 the subsequent values into that 4862 02:57:10,760 --> 02:57:17,319 placeholder and just this H question 4863 02:57:14,610 --> 02:57:19,520 [Music] 4864 02:57:17,319 --> 02:57:21,640 sorry ah so what if you wanted to do 4865 02:57:19,520 --> 02:57:24,640 single characters like initials like DM 4866 02:57:21,640 --> 02:57:26,600 or djm for first middle last absolutely 4867 02:57:24,640 --> 02:57:28,439 and that too is a perfect segue from the 4868 02:57:26,600 --> 02:57:31,680 two of you to what in general are going 4869 02:57:28,439 --> 02:57:34,600 to be called data types in C so it turns 4870 02:57:31,680 --> 02:57:36,319 out in C there's not only strings as 4871 02:57:34,600 --> 02:57:37,399 text and we'll see in more detail in 4872 02:57:36,319 --> 02:57:39,439 over the next couple of weeks what a 4873 02:57:37,399 --> 02:57:41,080 string really is underneath the hood but 4874 02:57:39,439 --> 02:57:42,800 strings of text are not the only thing 4875 02:57:41,080 --> 02:57:45,560 that programs can output they can indeed 4876 02:57:42,800 --> 02:57:47,960 output single characters as for initials 4877 02:57:45,560 --> 02:57:49,760 they can output integers as well turns 4878 02:57:47,960 --> 02:57:52,120 out that printf has different format 4879 02:57:49,760 --> 02:57:53,960 codes for all sorts of different data 4880 02:57:52,120 --> 02:57:55,520 types and just some of the data types 4881 02:57:53,960 --> 02:57:57,200 we'll see in the coming weeks will be 4882 02:57:55,520 --> 02:57:59,960 this list here which you'll notice it 4883 02:57:57,200 --> 02:58:01,800 almost perfectly lines up with the cs50 4884 02:57:59,960 --> 02:58:04,439 functions that I rattled off earlier 4885 02:58:01,800 --> 02:58:06,359 like get Char get int get string the 4886 02:58:04,439 --> 02:58:07,760 reason we called those functions that is 4887 02:58:06,359 --> 02:58:10,080 because each of them is designed to 4888 02:58:07,760 --> 02:58:11,840 return to you a different type of value 4889 02:58:10,080 --> 02:58:14,479 we've used get string already in this 4890 02:58:11,840 --> 02:58:16,640 example here we'll soon see get in and 4891 02:58:14,479 --> 02:58:19,319 we'll see opportunities to use others 4892 02:58:16,640 --> 02:58:21,800 but these indeed are the menu of 4893 02:58:19,319 --> 02:58:23,800 available data types plus others dot dot 4894 02:58:21,800 --> 02:58:26,279 dot that you can use when writing a 4895 02:58:23,800 --> 02:58:28,520 program in C the onus therefore is on 4896 02:58:26,279 --> 02:58:30,319 you to decide in advance do I want to 4897 02:58:28,520 --> 02:58:32,279 store an INT in this variable or a 4898 02:58:30,319 --> 02:58:35,160 string or heck when writing fancier code 4899 02:58:32,279 --> 02:58:36,920 an image or a sound or a video even 4900 02:58:35,160 --> 02:58:38,840 those can all be different data types 4901 02:58:36,920 --> 02:58:41,359 dot dot dot but for now we'll focus 4902 02:58:38,840 --> 02:58:43,160 really on just these Primitives that was 4903 02:58:41,359 --> 02:58:45,000 a lot let's go ahead and take a 5minute 4904 02:58:43,160 --> 02:58:46,439 break here no cookies yet but in 5 4905 02:58:45,000 --> 02:58:47,840 minutes we'll come back dive into more 4906 02:58:46,439 --> 02:58:50,120 detail and our second break today we'll 4907 02:58:47,840 --> 02:58:50,120 have 4908 02:58:51,399 --> 02:58:55,880 cookies all right we are back and so if 4909 02:58:54,239 --> 02:58:57,319 you have been playing along at home but 4910 02:58:55,880 --> 02:58:58,840 hitting some bumps in the road that's 4911 02:58:57,319 --> 02:59:01,319 totally normal and indeed the goals of 4912 02:58:58,840 --> 02:59:03,319 lecture generally will be to give you a 4913 02:59:01,319 --> 02:59:04,800 sense uh conceptually of where we'll be 4914 02:59:03,319 --> 02:59:06,359 going during the course of the week but 4915 02:59:04,800 --> 02:59:07,680 it's indeed through the Hands-On labs 4916 02:59:06,359 --> 02:59:09,960 and problem sets that you'll really have 4917 02:59:07,680 --> 02:59:11,840 an opportunity at your own pace to work 4918 02:59:09,960 --> 02:59:14,040 through uh some of those same bumps in 4919 02:59:11,840 --> 02:59:15,920 the road but for today let me give you a 4920 02:59:14,040 --> 02:59:18,120 few more building blocks and these two 4921 02:59:15,920 --> 02:59:19,760 will translate from scratch initially 4922 02:59:18,120 --> 02:59:22,560 namely like conditionals like how now 4923 02:59:19,760 --> 02:59:25,479 and see after knowing now how we can use 4924 02:59:22,560 --> 02:59:27,760 functions at least uh get string and 4925 02:59:25,479 --> 02:59:30,319 print F and we can use variables like 4926 02:59:27,760 --> 02:59:31,520 the string I created earlier how can I 4927 02:59:30,319 --> 02:59:33,840 now add to the mix things like 4928 02:59:31,520 --> 02:59:35,800 decisionmaking and conditionals at that 4929 02:59:33,840 --> 02:59:37,920 well with conditionals in scratch we had 4930 02:59:35,800 --> 02:59:40,000 this kind of syntax on the left here in 4931 02:59:37,920 --> 02:59:43,000 scratch is how you might Express if two 4932 02:59:40,000 --> 02:59:45,880 variables X and Y have this relationship 4933 02:59:43,000 --> 02:59:47,960 if x is less than y then say on the 4934 02:59:45,880 --> 02:59:49,640 screen X is less than y well let me 4935 02:59:47,960 --> 02:59:52,239 translate that to the right now in C 4936 02:59:49,640 --> 02:59:54,160 code so in C the corresponding code is 4937 02:59:52,239 --> 02:59:56,640 going to look like this assuming X and Y 4938 02:59:54,160 --> 02:59:58,640 already exist more on that later and 4939 02:59:56,640 --> 03:00:01,160 notice a pattern we're going to see 4940 02:59:58,640 --> 03:00:04,479 again and again there is going to be 4941 03:00:01,160 --> 03:00:06,000 parentheses around the X and less than y 4942 03:00:04,479 --> 03:00:07,960 so parentheses around the Boolean 4943 03:00:06,000 --> 03:00:10,359 expression recall the Boolean expression 4944 03:00:07,960 --> 03:00:11,960 is the true false the yes no answer that 4945 03:00:10,359 --> 03:00:13,840 a question that you're trying to ask in 4946 03:00:11,960 --> 03:00:15,800 order to decide whether or not to do 4947 03:00:13,840 --> 03:00:19,080 something so you use parentheses there 4948 03:00:15,800 --> 03:00:21,080 so similar in functions where we Ed 4949 03:00:19,080 --> 03:00:23,200 parentheses for printf and parentheses 4950 03:00:21,080 --> 03:00:25,920 for get string and this is just a weird 4951 03:00:23,200 --> 03:00:28,439 inconsistency stylistically when using 4952 03:00:25,920 --> 03:00:30,439 the keyword if you should as a matter of 4953 03:00:28,439 --> 03:00:32,880 better best practice put a space after 4954 03:00:30,439 --> 03:00:36,080 the word if when using a function like 4955 03:00:32,880 --> 03:00:37,760 print F or get string you shouldn't both 4956 03:00:36,080 --> 03:00:39,319 will work but you'll find that these are 4957 03:00:37,760 --> 03:00:41,800 conventions stylistically that most 4958 03:00:39,319 --> 03:00:44,239 people adhere to so space when using an 4959 03:00:41,800 --> 03:00:46,880 if here all right now inside of the 4960 03:00:44,239 --> 03:00:48,359 curly braces is where the actual code 4961 03:00:46,880 --> 03:00:50,040 goes that you want to execute 4962 03:00:48,359 --> 03:00:53,040 conditionally so if you want to print 4963 03:00:50,040 --> 03:00:55,520 out X is less than y only if x is 4964 03:00:53,040 --> 03:00:57,200 actually less than y in C you use this 4965 03:00:55,520 --> 03:00:58,760 open curly brace which up until now 4966 03:00:57,200 --> 03:01:00,960 you've probably rarely used on your 4967 03:00:58,760 --> 03:01:02,800 keyboard and the Clos curly brace down 4968 03:01:00,960 --> 03:01:05,279 here and those are kind of hugging if 4969 03:01:02,800 --> 03:01:07,520 you will the one or more lines of code 4970 03:01:05,279 --> 03:01:09,720 underneath the if very similar in spirit 4971 03:01:07,520 --> 03:01:11,680 to how the orange block here kind of 4972 03:01:09,720 --> 03:01:13,800 hugs the purple puzzle piece here so 4973 03:01:11,680 --> 03:01:15,439 there's no Graphics in C it's all text 4974 03:01:13,800 --> 03:01:18,760 so you can think of those curly braces 4975 03:01:15,439 --> 03:01:21,760 as really representing the same idea as 4976 03:01:18,760 --> 03:01:24,920 a side note if you only have one line of 4977 03:01:21,760 --> 03:01:26,880 code inside of the if condition if you 4978 03:01:24,920 --> 03:01:28,800 will you strictly speaking don't need 4979 03:01:26,880 --> 03:01:30,960 the curly braces but as a matter of good 4980 03:01:28,800 --> 03:01:33,279 style do include them it will make more 4981 03:01:30,960 --> 03:01:35,399 obvious what your intent is how about in 4982 03:01:33,279 --> 03:01:37,800 scratch if you wanted to express this 4983 03:01:35,399 --> 03:01:40,279 two ways in the road that you might go 4984 03:01:37,800 --> 03:01:42,720 left or right so to speak well if x is 4985 03:01:40,279 --> 03:01:45,279 less than y I want to say x is less than 4986 03:01:42,720 --> 03:01:48,000 y else I want want to say the opposite X 4987 03:01:45,279 --> 03:01:49,640 is not less than y in this case so I'm 4988 03:01:48,000 --> 03:01:52,560 making a decision based on that Boolean 4989 03:01:49,640 --> 03:01:54,920 expression in C it's almost the same but 4990 03:01:52,560 --> 03:01:57,040 you're adding to the mix the keyword Els 4991 03:01:54,920 --> 03:01:59,080 so MIT borrowed for scratch the same 4992 03:01:57,040 --> 03:02:01,560 keyword there and a second pair of curly 4993 03:01:59,080 --> 03:02:03,520 braces open and close respectively and 4994 03:02:01,560 --> 03:02:06,319 you might guess now what goes inside of 4995 03:02:03,520 --> 03:02:09,760 those well you print out X's less than y 4996 03:02:06,319 --> 03:02:11,359 or you print out X's not less than y all 4997 03:02:09,760 --> 03:02:12,920 right what if there's a three-way fork 4998 03:02:11,359 --> 03:02:15,319 in the road in scratch this actually 4999 03:02:12,920 --> 03:02:17,560 gets a little unwieldy graphically if 5000 03:02:15,319 --> 03:02:20,479 you will but notice that in scratch this 5001 03:02:17,560 --> 03:02:24,479 is how we could express if x is less 5002 03:02:20,479 --> 03:02:27,479 than y say x is less than y else if x is 5003 03:02:24,479 --> 03:02:31,439 greater than y say x is greater than y 5004 03:02:27,479 --> 03:02:35,840 else if x equals y then say x is equal 5005 03:02:31,439 --> 03:02:37,920 to Y now minor inconsistency here just a 5006 03:02:35,840 --> 03:02:41,120 little bit ago I claimed in C that a 5007 03:02:37,920 --> 03:02:43,680 equal sign represents what 5008 03:02:41,120 --> 03:02:45,800 operation assignment from right to left 5009 03:02:43,680 --> 03:02:46,960 left in so far as scratch is really 5010 03:02:45,800 --> 03:02:48,560 meant for kids and they didn't really 5011 03:02:46,960 --> 03:02:51,600 want to get into the weeds of this kind 5012 03:02:48,560 --> 03:02:53,680 of uh semantic equal sign in scratch 5013 03:02:51,600 --> 03:02:55,640 means equality however we're going to 5014 03:02:53,680 --> 03:02:57,840 need to fix this in C in just a moment 5015 03:02:55,640 --> 03:02:59,840 in c equal sign means assignment right 5016 03:02:57,840 --> 03:03:01,359 to left in scratch it literally means 5017 03:02:59,840 --> 03:03:03,760 what you would expect all right let's 5018 03:03:01,359 --> 03:03:06,640 translate this code then to C on the 5019 03:03:03,760 --> 03:03:09,200 right this code would correspond really 5020 03:03:06,640 --> 03:03:11,239 to this and you can perhaps see somewhat 5021 03:03:09,200 --> 03:03:13,080 goofy what the solution was not unlike 5022 03:03:11,239 --> 03:03:14,760 the percent percent solution earlier 5023 03:03:13,080 --> 03:03:17,880 when hum painted themselves into one 5024 03:03:14,760 --> 03:03:20,279 other corner you say if you say else if 5025 03:03:17,880 --> 03:03:22,720 and you say else if and how did we 5026 03:03:20,279 --> 03:03:24,880 resolve the use of a single equal sign 5027 03:03:22,720 --> 03:03:26,479 already in C when you want to repr when 5028 03:03:24,880 --> 03:03:28,120 you want to express equality is the 5029 03:03:26,479 --> 03:03:30,319 thing on the left equal to the thing on 5030 03:03:28,120 --> 03:03:32,200 the right you literally use two equal 5031 03:03:30,319 --> 03:03:35,479 signs right next to each other no space 5032 03:03:32,200 --> 03:03:37,279 in between them but now this code would 5033 03:03:35,479 --> 03:03:38,439 be correct on both the left and the 5034 03:03:37,279 --> 03:03:41,080 right whether you're doing this in 5035 03:03:38,439 --> 03:03:43,319 scratch or C respectively but now we can 5036 03:03:41,080 --> 03:03:46,160 kind of nitpick our code specifically 5037 03:03:43,319 --> 03:03:49,479 the the design thereof logically can 5038 03:03:46,160 --> 03:03:51,359 anyone critique the design of this code 5039 03:03:49,479 --> 03:03:55,239 either in scratch or C like I feel like 5040 03:03:51,359 --> 03:03:55,239 we could do better how about in 5041 03:03:59,040 --> 03:04:04,880 back perfect logically it's got to be 5042 03:04:02,239 --> 03:04:08,000 the case that X is less than y or X is 5043 03:04:04,880 --> 03:04:10,239 greater than y or by conclusion it's got 5044 03:04:08,000 --> 03:04:12,000 to be equal to Y so why are you wasting 5045 03:04:10,239 --> 03:04:13,840 my time or the computer's time asking a 5046 03:04:12,000 --> 03:04:16,279 third question you don't need to ask 5047 03:04:13,840 --> 03:04:18,479 this final else if because logically as 5048 03:04:16,279 --> 03:04:20,239 you knowe it should go without saying so 5049 03:04:18,479 --> 03:04:22,960 it's a minor tweak like you're doing 5050 03:04:20,239 --> 03:04:25,080 extra work potentially in cases where x 5051 03:04:22,960 --> 03:04:26,520 equals y so we can just refine that and 5052 03:04:25,080 --> 03:04:29,200 just like in scratch you could just use 5053 03:04:26,520 --> 03:04:32,080 an else block similarly in C could we 5054 03:04:29,200 --> 03:04:34,040 simplify this code to just an else a 5055 03:04:32,080 --> 03:04:35,920 sort of catchall logically that just 5056 03:04:34,040 --> 03:04:38,040 handles the reality that of course 5057 03:04:35,920 --> 03:04:40,000 that's going to be the final situation 5058 03:04:38,040 --> 03:04:42,239 instead all right so we have this 5059 03:04:40,000 --> 03:04:44,160 ability now to express conditionals with 5060 03:04:42,239 --> 03:04:45,840 Boolean expressions let's actually do 5061 03:04:44,160 --> 03:04:48,720 something with this next here so let me 5062 03:04:45,840 --> 03:04:50,200 go back to vs code I've closed hello.c 5063 03:04:48,720 --> 03:04:52,319 and I want to create a second file for 5064 03:04:50,200 --> 03:04:54,960 the sake of some demos now recall that 5065 03:04:52,319 --> 03:04:56,720 you can create new files by typing code 5066 03:04:54,960 --> 03:04:59,080 space and then the name of the file you 5067 03:04:56,720 --> 03:05:01,439 want to create uh for instance I might 5068 03:04:59,080 --> 03:05:02,720 do compare. c I want to write a program 5069 03:05:01,439 --> 03:05:05,000 that's going to start comparing some 5070 03:05:02,720 --> 03:05:06,840 values for demonstration sake but before 5071 03:05:05,000 --> 03:05:09,359 I do that let me just show you by 5072 03:05:06,840 --> 03:05:11,720 opening the file explorer at right this 5073 03:05:09,359 --> 03:05:13,800 is similar in spirit to a Mac or PC like 5074 03:05:11,720 --> 03:05:15,399 you can go up here and click on an icon 5075 03:05:13,800 --> 03:05:17,720 and you can click on the plus icon and 5076 03:05:15,399 --> 03:05:19,680 you'll get a blue box and I can type in 5077 03:05:17,720 --> 03:05:21,960 compare. C and I can just manually 5078 03:05:19,680 --> 03:05:24,920 create it that way notice that opens the 5079 03:05:21,960 --> 03:05:26,800 tab even without my having typed code so 5080 03:05:24,920 --> 03:05:28,520 again on the left you have a guey a 5081 03:05:26,800 --> 03:05:30,560 graphical user interface albeit a 5082 03:05:28,520 --> 03:05:31,920 simplistic one on the right and at the 5083 03:05:30,560 --> 03:05:34,000 bottom here you have a command line 5084 03:05:31,920 --> 03:05:35,840 interface but they're one and the same 5085 03:05:34,000 --> 03:05:38,760 what's nice though is that if I close 5086 03:05:35,840 --> 03:05:41,319 this file accidentally intentionally 5087 03:05:38,760 --> 03:05:43,319 whatnot I can reopen it without creating 5088 03:05:41,319 --> 03:05:46,040 a new one by just running that same 5089 03:05:43,319 --> 03:05:47,880 command code space compare. c so code is 5090 03:05:46,040 --> 03:05:50,399 a vs code thing it's just a userfriendly 5091 03:05:47,880 --> 03:05:52,680 shortcut but it's just creating a file 5092 03:05:50,399 --> 03:05:54,040 or opening an existing file like that 5093 03:05:52,680 --> 03:05:55,600 I'm going to hide the file explorer just 5094 03:05:54,040 --> 03:05:57,160 to make more room for code here and 5095 03:05:55,600 --> 03:05:59,920 let's go ahead and do this let's write a 5096 03:05:57,160 --> 03:06:01,720 program that compares two values that 5097 03:05:59,920 --> 03:06:03,960 the human inputs but not strings this 5098 03:06:01,720 --> 03:06:05,720 time let's use some actual integers all 5099 03:06:03,960 --> 03:06:08,840 right I'm going to go ahead and include 5100 03:06:05,720 --> 03:06:11,080 the cs-50 librar header file at top 5101 03:06:08,840 --> 03:06:14,000 cs50.h I'm going to also include 5102 03:06:11,080 --> 03:06:16,600 standard io. why one gives me 5103 03:06:14,000 --> 03:06:18,520 userfriendly input via get string get 5104 03:06:16,600 --> 03:06:20,760 int and so forth one gives me user 5105 03:06:18,520 --> 03:06:23,120 friendly output via printf in the case 5106 03:06:20,760 --> 03:06:25,160 of standard i.h now I'm just going to 5107 03:06:23,120 --> 03:06:26,800 kind of blindly type this line of code 5108 03:06:25,160 --> 03:06:29,040 which we'll come back to in future weeks 5109 03:06:26,800 --> 03:06:31,760 but for now that's analogous to the when 5110 03:06:29,040 --> 03:06:33,600 green flag clicked code in scratch and 5111 03:06:31,760 --> 03:06:36,200 now let's go ahead and do this let me go 5112 03:06:33,600 --> 03:06:38,760 ahead and get an INT from the user and 5113 03:06:36,200 --> 03:06:40,040 ask the user what's X question mark I'm 5114 03:06:38,760 --> 03:06:41,720 not going to bother with a new line I 5115 03:06:40,040 --> 03:06:44,200 want to keep it all in one line just for 5116 03:06:41,720 --> 03:06:46,600 Aesthetics sake but when I get back an 5117 03:06:44,200 --> 03:06:49,200 INT just like I get back a string I get 5118 03:06:46,600 --> 03:06:52,880 back a return value so if I want to 5119 03:06:49,200 --> 03:06:54,760 store the result of get int somewhere I 5120 03:06:52,880 --> 03:06:56,399 had better put it in a variable and I 5121 03:06:54,760 --> 03:06:58,920 can call the variable anything I want 5122 03:06:56,399 --> 03:07:01,479 previously I used answer or first or 5123 03:06:58,920 --> 03:07:04,120 last now I'm going to use x but there's 5124 03:07:01,479 --> 03:07:05,239 still two things left to do here 5125 03:07:04,120 --> 03:07:06,800 logically even though we haven't 5126 03:07:05,239 --> 03:07:08,520 technically done this yet what do I 5127 03:07:06,800 --> 03:07:11,640 still need to 5128 03:07:08,520 --> 03:07:14,800 do so I need the semicolon at the 5129 03:07:11,640 --> 03:07:17,080 end and the in at the beginning you the 5130 03:07:14,800 --> 03:07:18,319 programmer starting today kind of need 5131 03:07:17,080 --> 03:07:19,800 to decide what you're going to be 5132 03:07:18,319 --> 03:07:21,800 storing in your variables and you just 5133 03:07:19,800 --> 03:07:23,800 need to tell the computer that so that 5134 03:07:21,800 --> 03:07:26,040 it knows now as a teaser for languages 5135 03:07:23,800 --> 03:07:27,640 like python more modern languages turns 5136 03:07:26,040 --> 03:07:29,160 out humans realize well gee this is 5137 03:07:27,640 --> 03:07:30,520 stupid like why can't the computer just 5138 03:07:29,160 --> 03:07:32,720 figure out that I'm putting an INT there 5139 03:07:30,520 --> 03:07:34,680 why do I have to tell it proactively so 5140 03:07:32,720 --> 03:07:36,359 in some languages nowadays like python 5141 03:07:34,680 --> 03:07:37,920 we'll get rid of some of this syntax 5142 03:07:36,359 --> 03:07:39,880 we'll get rid of the semicolons but for 5143 03:07:37,920 --> 03:07:41,840 now we're looking at really the origins 5144 03:07:39,880 --> 03:07:44,000 of how this all worked all right so I've 5145 03:07:41,840 --> 03:07:45,600 done this one line ending with semicolon 5146 03:07:44,000 --> 03:07:48,040 let me do one other and let me get a 5147 03:07:45,600 --> 03:07:50,840 second int asking the user what's why 5148 03:07:48,040 --> 03:07:52,640 question mark so almost identical but 5149 03:07:50,840 --> 03:07:54,640 different responses from the user 5150 03:07:52,640 --> 03:07:58,520 hopefully and let me just ask simply if 5151 03:07:54,640 --> 03:08:00,640 x is less than y in parentheses then 5152 03:07:58,520 --> 03:08:04,239 some curly braces let me go ahead and 5153 03:08:00,640 --> 03:08:07,520 print out quote unquote X is less than y 5154 03:08:04,239 --> 03:08:10,279 back sln and now just as a side note I I 5155 03:08:07,520 --> 03:08:12,080 seem to be typing kind of fast some of 5156 03:08:10,279 --> 03:08:13,640 that is because vs code is helping me 5157 03:08:12,080 --> 03:08:16,920 let me go back to this 5158 03:08:13,640 --> 03:08:18,560 first line with the if hit enter and now 5159 03:08:16,920 --> 03:08:21,399 I'm only on my keyboard going to type 5160 03:08:18,560 --> 03:08:23,120 the open curly brace this is a feature 5161 03:08:21,399 --> 03:08:25,279 of many text editors nowadays it 5162 03:08:23,120 --> 03:08:26,880 finishes part of your thought why just 5163 03:08:25,279 --> 03:08:28,439 to save yourself a keystroke to make 5164 03:08:26,880 --> 03:08:30,319 sure you don't accidentally forget the 5165 03:08:28,439 --> 03:08:31,479 closing one so you'll notice sometimes 5166 03:08:30,319 --> 03:08:33,800 that things are happening that you 5167 03:08:31,479 --> 03:08:35,479 didn't type it's just vs code or future 5168 03:08:33,800 --> 03:08:37,439 programs you use trying to be helpful 5169 03:08:35,479 --> 03:08:41,399 for you I'll go ahead and manually type 5170 03:08:37,439 --> 03:08:44,160 out now printf uh X is less than y back 5171 03:08:41,399 --> 03:08:47,399 slend close quote semicolon so let me go 5172 03:08:44,160 --> 03:08:50,439 ahead now and try to run this and we'll 5173 03:08:47,399 --> 03:08:52,319 see let's see so make not hello but make 5174 03:08:50,439 --> 03:08:56,439 compare because this file is called 5175 03:08:52,319 --> 03:08:58,120 compare. C hitting enter okay no output 5176 03:08:56,439 --> 03:09:00,920 is good because it means I haven't 5177 03:08:58,120 --> 03:09:04,120 messed up let me do dot SL compare 5178 03:09:00,920 --> 03:09:07,560 instead of/ hello enter what's X how 5179 03:09:04,120 --> 03:09:10,279 about one what's Y how about two x is 5180 03:09:07,560 --> 03:09:11,800 less than y well let's try it again and 5181 03:09:10,279 --> 03:09:13,760 here I'll save you some keystrokes too 5182 03:09:11,800 --> 03:09:16,160 let me clear my screen instead of 5183 03:09:13,760 --> 03:09:17,960 constantly typing dot slash this and Dot 5184 03:09:16,160 --> 03:09:20,239 slash that you can also use your 5185 03:09:17,960 --> 03:09:22,960 keyboard's arrow keys in vs code to 5186 03:09:20,239 --> 03:09:24,640 scroll back through time so if I hit up 5187 03:09:22,960 --> 03:09:26,640 once there's the last command I wrote If 5188 03:09:24,640 --> 03:09:28,479 I write uh do it up twice there's the 5189 03:09:26,640 --> 03:09:29,720 second to last command I wrote so 5190 03:09:28,479 --> 03:09:30,880 sometimes if you see me doing things 5191 03:09:29,720 --> 03:09:32,520 fast it's just because I'm kind of 5192 03:09:30,880 --> 03:09:34,160 cheating and going through my history 5193 03:09:32,520 --> 03:09:35,120 like that all right let me go ahead 5194 03:09:34,160 --> 03:09:38,840 though and 5195 03:09:35,120 --> 03:09:42,399 rerun compare enter let's reverse it 5196 03:09:38,840 --> 03:09:43,760 this time two for x one for y and now of 5197 03:09:42,399 --> 03:09:45,319 course there's no no output all right 5198 03:09:43,760 --> 03:09:46,720 well that's logically to be expected 5199 03:09:45,319 --> 03:09:49,399 because we didn't have an else here so 5200 03:09:46,720 --> 03:09:51,479 let's add that else now let's open my 5201 03:09:49,399 --> 03:09:54,560 curly braces letting vs code do one of 5202 03:09:51,479 --> 03:09:59,200 them for me print F quote unquote X is 5203 03:09:54,560 --> 03:10:02,279 not less than y back sln semicolon let 5204 03:09:59,200 --> 03:10:05,960 me go ahead and try this again/ compare 5205 03:10:02,279 --> 03:10:08,680 enter again two for x one for y and we 5206 03:10:05,960 --> 03:10:11,920 should see 5207 03:10:08,680 --> 03:10:15,800 huh what did I do wrong why am I not 5208 03:10:11,920 --> 03:10:15,800 seeing any else output 5209 03:10:16,439 --> 03:10:19,840 yeah exactly you got to get into the 5210 03:10:18,600 --> 03:10:21,720 Habit after you change your code of 5211 03:10:19,840 --> 03:10:24,239 recompiling it otherwise the zeros and 5212 03:10:21,720 --> 03:10:26,160 ones in the server are the old ones 5213 03:10:24,239 --> 03:10:28,239 until you manually compile so let's fix 5214 03:10:26,160 --> 03:10:32,399 this make compare enter no error 5215 03:10:28,239 --> 03:10:34,920 messages that's good do/ compare 2 1 and 5216 03:10:32,399 --> 03:10:37,520 now I get back the output so X is not 5217 03:10:34,920 --> 03:10:40,120 less than y how about if I go and add in 5218 03:10:37,520 --> 03:10:42,399 the third condition well we can do this 5219 03:10:40,120 --> 03:10:44,520 either efficiently or inefficiently let 5220 03:10:42,399 --> 03:10:47,319 me go ahead head and refine this so else 5221 03:10:44,520 --> 03:10:51,359 if x is greater than y let's literally 5222 03:10:47,319 --> 03:10:54,800 say x is greater than y and now I could 5223 03:10:51,359 --> 03:10:56,120 do X else if x equals equals y but I 5224 03:10:54,800 --> 03:10:58,279 think we already claim that that's 5225 03:10:56,120 --> 03:11:00,279 unnecessarily inefficient so let's just 5226 03:10:58,279 --> 03:11:04,359 have our catchall and here I'm going to 5227 03:11:00,279 --> 03:11:07,720 say quote unquote X is equal 2 y back 5228 03:11:04,359 --> 03:11:09,720 sln close quote there so I think now 5229 03:11:07,720 --> 03:11:11,880 with this code we've handled all three 5230 03:11:09,720 --> 03:11:15,960 scenarios let me go ahead and recompile 5231 03:11:11,880 --> 03:11:19,640 it properly compare do/ compare and now 5232 03:11:15,960 --> 03:11:23,560 1 and 2 x is less than y let me run it 5233 03:11:19,640 --> 03:11:27,880 again 2 and One X is greater than y and 5234 03:11:23,560 --> 03:11:29,399 lastly one and one and X is equal to Y 5235 03:11:27,880 --> 03:11:31,600 so for the most part our code's getting 5236 03:11:29,399 --> 03:11:33,080 longer we're up to like 21 lines of code 5237 03:11:31,600 --> 03:11:34,880 though some of them are just single 5238 03:11:33,080 --> 03:11:36,479 characters on the screen almost 5239 03:11:34,880 --> 03:11:39,920 everything else is the same I'm using 5240 03:11:36,479 --> 03:11:43,160 the cs50 libraries header file for my 5241 03:11:39,920 --> 03:11:45,239 get int function standard i.h for my 5242 03:11:43,160 --> 03:11:47,840 print F function and the rest of this is 5243 03:11:45,239 --> 03:11:50,239 just now new syntax for conditionals as 5244 03:11:47,840 --> 03:11:52,319 well questions then on this C 5245 03:11:50,239 --> 03:11:53,920 implementation of just some basic 5246 03:11:52,319 --> 03:11:57,520 comparisons like 5247 03:11:53,920 --> 03:12:00,239 this any questions 5248 03:11:57,520 --> 03:12:02,359 [Music] 5249 03:12:00,239 --> 03:12:03,920 yeah good question do the opening 5250 03:12:02,359 --> 03:12:06,600 brackets need to be on a separate line 5251 03:12:03,920 --> 03:12:08,080 in cs50 yes what you'll see is that as 5252 03:12:06,600 --> 03:12:10,600 part of the submission process We 5253 03:12:08,080 --> 03:12:12,880 compare your code against a style guide 5254 03:12:10,600 --> 03:12:14,680 which is the norm in Industry a company 5255 03:12:12,880 --> 03:12:16,239 would have its own sense of style and 5256 03:12:14,680 --> 03:12:17,720 how its code should look and there's 5257 03:12:16,239 --> 03:12:19,880 generally automated tools within a 5258 03:12:17,720 --> 03:12:22,319 company that help uh give feedback on 5259 03:12:19,880 --> 03:12:24,520 the code or stylize it as such there are 5260 03:12:22,319 --> 03:12:27,279 alternative Styles than what we use in 5261 03:12:24,520 --> 03:12:29,120 the class we deliberately keep and ask 5262 03:12:27,279 --> 03:12:31,960 that you keep the curly braces on their 5263 03:12:29,120 --> 03:12:34,080 own line if only because it rather 5264 03:12:31,960 --> 03:12:36,040 resembles like the hugging nature of 5265 03:12:34,080 --> 03:12:38,239 scratches blocks and just makes clear 5266 03:12:36,040 --> 03:12:40,760 that they're balanced open and closed 5267 03:12:38,239 --> 03:12:42,840 however another common Paradigm in some 5268 03:12:40,760 --> 03:12:45,600 languages and with some programmers is 5269 03:12:42,840 --> 03:12:48,520 to do something like this uh on each of 5270 03:12:45,600 --> 03:12:50,840 them so you have the opening curly brace 5271 03:12:48,520 --> 03:12:52,520 on the same line as here we do not 5272 03:12:50,840 --> 03:12:54,840 recommend this this is invogue in the 5273 03:12:52,520 --> 03:12:56,439 JavaScript world and some others um but 5274 03:12:54,840 --> 03:12:58,439 ultimately in the real world it's up to 5275 03:12:56,439 --> 03:12:59,680 each individual programmer Andor the 5276 03:12:58,439 --> 03:13:01,880 company they're working for if 5277 03:12:59,680 --> 03:13:04,800 applicable to decide on those 5278 03:13:01,880 --> 03:13:05,920 things all right so beyond then these 5279 03:13:04,800 --> 03:13:07,880 conditionals what if we want to do 5280 03:13:05,920 --> 03:13:09,200 something that's maybe pretty common so 5281 03:13:07,880 --> 03:13:11,279 almost every piece of software or 5282 03:13:09,200 --> 03:13:13,080 website nowadays that you use has you 5283 03:13:11,279 --> 03:13:15,560 like agree to some terms and conditions 5284 03:13:13,080 --> 03:13:17,680 by typing like yes or no or just y for 5285 03:13:15,560 --> 03:13:19,880 yes and N for no so how could we 5286 03:13:17,680 --> 03:13:21,520 Implement some kind of Agreement System 5287 03:13:19,880 --> 03:13:24,600 well let me do this let me create a new 5288 03:13:21,520 --> 03:13:26,600 program a third one called agree. C so 5289 03:13:24,600 --> 03:13:28,439 I'm going to write code agree. C just to 5290 03:13:26,600 --> 03:13:31,040 give myself a new tab I'm going to start 5291 03:13:28,439 --> 03:13:34,239 as always now include cs50.h let's 5292 03:13:31,040 --> 03:13:36,920 include standard i.h and then let me do 5293 03:13:34,239 --> 03:13:38,560 my int main void which again for today's 5294 03:13:36,920 --> 03:13:41,000 purposes we'll take it face value is 5295 03:13:38,560 --> 03:13:43,720 just copy paste and if I just want to 5296 03:13:41,000 --> 03:13:46,880 get y or n for instance instead of yes 5297 03:13:43,720 --> 03:13:49,319 or no we can just use a simpler variable 5298 03:13:46,880 --> 03:13:51,840 here how about just a Char a character A 5299 03:13:49,319 --> 03:13:54,520 Single Character so I can use get Char 5300 03:13:51,840 --> 03:13:58,080 to ask the user for instance do you 5301 03:13:54,520 --> 03:13:59,680 agree question mark but before as before 5302 03:13:58,080 --> 03:14:01,040 I need to store this somewhere so I 5303 03:13:59,680 --> 03:14:03,080 don't want a string because it's a 5304 03:14:01,040 --> 03:14:06,000 single Char I don't want an INT I just 5305 03:14:03,080 --> 03:14:07,080 want a Char and it's literally CH h a r 5306 03:14:06,000 --> 03:14:09,080 and then I could call this thing 5307 03:14:07,080 --> 03:14:11,359 anything I want it's conventional if you 5308 03:14:09,080 --> 03:14:13,640 have a simple program with just a single 5309 03:14:11,359 --> 03:14:15,359 variable and it's of type Char call it C 5310 03:14:13,640 --> 03:14:17,239 if it's an INT call it I if it's a 5311 03:14:15,359 --> 03:14:19,600 string call it s for now I'm just going 5312 03:14:17,239 --> 03:14:23,560 to keep it simple and call it C and now 5313 03:14:19,600 --> 03:14:28,200 I'm going to ask a question so if C 5314 03:14:23,560 --> 03:14:32,479 equals equals how about quote unquote 5315 03:14:28,200 --> 03:14:34,800 y then let me go ahead and print out uh 5316 03:14:32,479 --> 03:14:38,040 agreed back sln as though they agreed to 5317 03:14:34,800 --> 03:14:40,680 my terms and conditions uh otherwise 5318 03:14:38,040 --> 03:14:43,120 let's see else if the character equals 5319 03:14:40,680 --> 03:14:45,680 equals quote unquote n 5320 03:14:43,120 --> 03:14:49,040 then let me go ahead and print out uh 5321 03:14:45,680 --> 03:14:52,399 say not agreed as though they didn't 5322 03:14:49,040 --> 03:14:54,479 quote unquote and let's leave it at that 5323 03:14:52,399 --> 03:14:58,479 I think here initially now you'll notice 5324 03:14:54,479 --> 03:15:00,120 one curiosity one inconsistency perhaps 5325 03:14:58,479 --> 03:15:01,399 does anyone want to call it out though 5326 03:15:00,120 --> 03:15:03,399 it's somewhat 5327 03:15:01,399 --> 03:15:05,840 subtle I've done something ever so 5328 03:15:03,399 --> 03:15:10,080 slightly differently without explaining 5329 03:15:05,840 --> 03:15:12,479 it yet do you see it single single yeah 5330 03:15:10,080 --> 03:15:14,840 so I've suddenly used single quotation 5331 03:15:12,479 --> 03:15:16,760 marks for my single characters and 5332 03:15:14,840 --> 03:15:18,880 double quotes for my actual strings of 5333 03:15:16,760 --> 03:15:20,760 text this is a necessity in C when 5334 03:15:18,880 --> 03:15:23,120 you're dealing with strings like strings 5335 03:15:20,760 --> 03:15:24,920 of text like someone's name a sentence a 5336 03:15:23,120 --> 03:15:27,560 paragraph anything really more than one 5337 03:15:24,920 --> 03:15:29,760 character you typically use uh double 5338 03:15:27,560 --> 03:15:32,520 quotes and indeed you must when dealing 5339 03:15:29,760 --> 03:15:35,640 with deliberately single characters like 5340 03:15:32,520 --> 03:15:37,800 I am here for y or n you must use single 5341 03:15:35,640 --> 03:15:39,319 quotes instead why because that makes 5342 03:15:37,800 --> 03:15:41,600 sure that the computer knows that it's 5343 03:15:39,319 --> 03:15:43,319 indeed a Char and not a string so double 5344 03:15:41,600 --> 03:15:46,000 quotes are for Strings single quotes are 5345 03:15:43,319 --> 03:15:48,399 for chars so with that said let me go 5346 03:15:46,000 --> 03:15:51,560 ahead and zoom out let me go ahead in my 5347 03:15:48,399 --> 03:15:53,239 terminal window run make agree enter 5348 03:15:51,560 --> 03:15:57,080 seems to work okay so let me go ahead 5349 03:15:53,239 --> 03:16:01,080 and do/ agree uh let me go ahead now and 5350 03:15:57,080 --> 03:16:04,520 type in y here we go 5351 03:16:01,080 --> 03:16:08,319 enter huh uh let me try that again rerun 5352 03:16:04,520 --> 03:16:11,160 do/ agree how about no 5353 03:16:08,319 --> 03:16:14,239 enter why is it not behaving as I would 5354 03:16:11,160 --> 03:16:16,439 have expected uh because you the capital 5355 03:16:14,239 --> 03:16:17,960 Y yeah I kind of cheated there and I hit 5356 03:16:16,439 --> 03:16:19,279 the caps lock key just as I started 5357 03:16:17,960 --> 03:16:21,399 typing in input why because I 5358 03:16:19,279 --> 03:16:23,239 deliberately wanted to type in uppercase 5359 03:16:21,399 --> 03:16:25,040 instead of lowercase which is kind of 5360 03:16:23,239 --> 03:16:26,920 reasonable right it's a little obnoxious 5361 03:16:25,040 --> 03:16:28,399 if you force the user to toggle their 5362 03:16:26,920 --> 03:16:30,000 caps lock key on or off when you just 5363 03:16:28,399 --> 03:16:32,760 need a simple answer that's not the best 5364 03:16:30,000 --> 03:16:34,720 user experience or ux but it would work 5365 03:16:32,760 --> 03:16:38,840 if I cooperated let me run this again 5366 03:16:34,720 --> 03:16:41,920 without caps lock on y lowercase for yes 5367 03:16:38,840 --> 03:16:43,479 ah that worked and uh lowercase for no 5368 03:16:41,920 --> 03:16:45,760 that work worked but how could I get it 5369 03:16:43,479 --> 03:16:47,199 to work for both well how about this let 5370 03:16:45,760 --> 03:16:50,880 me go ahead and just add two 5371 03:16:47,199 --> 03:16:54,239 possibilities so else if C equals equals 5372 03:16:50,880 --> 03:16:58,640 quote unquote capital Y then also do 5373 03:16:54,239 --> 03:17:02,600 printf agreed back sln and down here 5374 03:16:58,640 --> 03:17:05,680 else if C equals equals uh single quote 5375 03:17:02,600 --> 03:17:08,880 uh capital N then go ahead and print out 5376 03:17:05,680 --> 03:17:11,479 again not agreed okay this I will claim 5377 03:17:08,880 --> 03:17:14,199 now is correct and I'll do make agree 5378 03:17:11,479 --> 03:17:17,239 real fast do slash agree and I'll use 5379 03:17:14,199 --> 03:17:19,920 Capital it now works I'll use uh Capital 5380 03:17:17,239 --> 03:17:22,080 it again works but this is perhaps not 5381 03:17:19,920 --> 03:17:23,720 the best design let me hide the terminal 5382 03:17:22,080 --> 03:17:25,279 window and pull this up on the screen 5383 03:17:23,720 --> 03:17:28,000 all at 5384 03:17:25,279 --> 03:17:31,399 once why might this arguably not be the 5385 03:17:28,000 --> 03:17:31,399 best design even though it's 5386 03:17:31,760 --> 03:17:36,640 correct there's another term of art we 5387 03:17:33,920 --> 03:17:38,000 can toss here like something smells kind 5388 03:17:36,640 --> 03:17:39,600 of funky about this code this is an 5389 03:17:38,000 --> 03:17:41,800 actual term of art like there's code 5390 03:17:39,600 --> 03:17:44,860 smell here like something smells a 5391 03:17:41,800 --> 03:17:47,929 little off why what do you 5392 03:17:44,860 --> 03:17:47,929 [Music] 5393 03:17:50,800 --> 03:17:55,399 [Music] 5394 03:17:53,120 --> 03:17:57,239 think yeah there's the same output again 5395 03:17:55,399 --> 03:17:58,439 and again I mean I manually typed it but 5396 03:17:57,239 --> 03:18:00,279 honestly I might as well have just 5397 03:17:58,439 --> 03:18:02,479 copied and pasted most of my original 5398 03:18:00,279 --> 03:18:06,239 code to do it again and again for the C 5399 03:18:02,479 --> 03:18:09,359 two capital letters so if line 10 and 14 5400 03:18:06,239 --> 03:18:11,120 are the same and line 18 and 22 are the 5401 03:18:09,359 --> 03:18:13,880 same and then the rest of these if and 5402 03:18:11,120 --> 03:18:15,239 Els ifs are almost the same like like 5403 03:18:13,880 --> 03:18:17,120 there's some code smell there like it's 5404 03:18:15,239 --> 03:18:18,800 not well-designed why because if I want 5405 03:18:17,120 --> 03:18:20,279 to change things now just like last week 5406 03:18:18,800 --> 03:18:22,680 in scratch I might have to change my 5407 03:18:20,279 --> 03:18:24,760 code in multiple places or copy paste is 5408 03:18:22,680 --> 03:18:27,120 never a good thing and go God forbid I 5409 03:18:24,760 --> 03:18:29,399 want to add support for yes and no as 5410 03:18:27,120 --> 03:18:31,160 full words it's really going to get long 5411 03:18:29,399 --> 03:18:33,399 so how can we solve this well it turns 5412 03:18:31,160 --> 03:18:35,000 out like we can combine some of these 5413 03:18:33,399 --> 03:18:37,680 thoughts so let me try to improve the 5414 03:18:35,000 --> 03:18:40,600 yeses first it turns out if I delete 5415 03:18:37,680 --> 03:18:42,080 that Clause I can actually or things 5416 03:18:40,600 --> 03:18:43,800 together in scratch there's a couple 5417 03:18:42,080 --> 03:18:45,680 puzzle pieces if you didn't discover 5418 03:18:43,800 --> 03:18:47,319 them that literally have the word or and 5419 03:18:45,680 --> 03:18:49,359 the word and on them which allow you to 5420 03:18:47,319 --> 03:18:52,279 combine Boolean Expressions so that 5421 03:18:49,359 --> 03:18:55,040 either this or this is true or this and 5422 03:18:52,279 --> 03:18:58,600 this is true in C you can't just say the 5423 03:18:55,040 --> 03:19:02,120 word or you instead use two vertical 5424 03:18:58,600 --> 03:19:04,600 bars and vertical bars together mean or 5425 03:19:02,120 --> 03:19:08,479 logically and so I can say C equals 5426 03:19:04,600 --> 03:19:10,359 equals quote unquote capital Y agreed 5427 03:19:08,479 --> 03:19:13,199 and now I can get rid of this code down 5428 03:19:10,359 --> 03:19:16,880 here and let me go ahead and say 5429 03:19:13,199 --> 03:19:19,680 vertical vertical bar twice C equals uh 5430 03:19:16,880 --> 03:19:21,960 quote unquote n in all caps and now my 5431 03:19:19,680 --> 03:19:24,040 program's like you know roughly a third 5432 03:19:21,960 --> 03:19:25,960 uh smaller which is good there's less 5433 03:19:24,040 --> 03:19:30,239 redundancy and if I reopen my terminal 5434 03:19:25,960 --> 03:19:34,000 window rerun make of agree SL agree now 5435 03:19:30,239 --> 03:19:37,439 I can type little y or Big Y and same 5436 03:19:34,000 --> 03:19:39,560 thing for lowercase and uppercase n any 5437 03:19:37,439 --> 03:19:40,920 questions then on this syntax whereby 5438 03:19:39,560 --> 03:19:43,319 now you can combine thoughts and just 5439 03:19:40,920 --> 03:19:46,760 kind of tighten things up 5440 03:19:43,319 --> 03:19:46,760 and there'll be other such tricks too 5441 03:19:47,880 --> 03:19:53,399 yeah a really good question is there not 5442 03:19:50,239 --> 03:19:55,720 a function to just ignore the case short 5443 03:19:53,399 --> 03:19:57,199 answer there is and we'll see how to do 5444 03:19:55,720 --> 03:19:59,000 that in actually just about a week's 5445 03:19:57,199 --> 03:20:01,040 time and in other languages there's even 5446 03:19:59,000 --> 03:20:03,279 more ways to just canonicalize the 5447 03:20:01,040 --> 03:20:04,680 user's input throwing away any space 5448 03:20:03,279 --> 03:20:06,760 characters they might have accidentally 5449 03:20:04,680 --> 03:20:09,040 hit forcing everything to lower case in 5450 03:20:06,760 --> 03:20:10,880 C it's going to be a little more work on 5451 03:20:09,040 --> 03:20:12,399 our part to do that but in fact as early 5452 03:20:10,880 --> 03:20:14,279 as next week we'll see how we can do 5453 03:20:12,399 --> 03:20:17,870 that but for now we're comparing indeed 5454 03:20:14,279 --> 03:20:23,920 just these literal values other 5455 03:20:17,870 --> 03:20:26,120 [Music] 5456 03:20:23,920 --> 03:20:28,279 questions really good question so we are 5457 03:20:26,120 --> 03:20:29,880 assuming with this program in all of my 5458 03:20:28,279 --> 03:20:31,439 last ones that the human's cooperating 5459 03:20:29,880 --> 03:20:33,439 when I asked for their name they typed 5460 03:20:31,439 --> 03:20:35,120 in David and not one two three or in 5461 03:20:33,439 --> 03:20:37,399 this case they typed in a single 5462 03:20:35,120 --> 03:20:39,520 character and not a full word so this is 5463 03:20:37,399 --> 03:20:41,680 one of the features often of using a 5464 03:20:39,520 --> 03:20:42,720 library so for instance if I run agree 5465 03:20:41,680 --> 03:20:46,560 again 5466 03:20:42,720 --> 03:20:50,479 and I say something like sure enter it 5467 03:20:46,560 --> 03:20:52,840 rejects it alt together why because s e 5468 03:20:50,479 --> 03:20:54,840 is a string of characters it's not a 5469 03:20:52,840 --> 03:20:57,040 single character now I could just say 5470 03:20:54,840 --> 03:20:59,600 something like X which is neither y nor 5471 03:20:57,040 --> 03:21:00,920 n of course but it tolerates that 5472 03:20:59,600 --> 03:21:03,080 because it's a single character but 5473 03:21:00,920 --> 03:21:05,279 built into cs50's library is some 5474 03:21:03,080 --> 03:21:07,279 built-in rejections of inputs that's not 5475 03:21:05,279 --> 03:21:09,560 expected so if you use get int and the 5476 03:21:07,279 --> 03:21:12,359 user types in not the number one or two 5477 03:21:09,560 --> 03:21:14,160 but cat C A it will just prompt them 5478 03:21:12,359 --> 03:21:15,760 again prompt them again and this is 5479 03:21:14,160 --> 03:21:18,120 where too if you were to do this 5480 03:21:15,760 --> 03:21:20,399 manually in C you end up writing this 5481 03:21:18,120 --> 03:21:21,760 much code just to check for all of these 5482 03:21:20,399 --> 03:21:23,920 errors that's why we use these training 5483 03:21:21,760 --> 03:21:25,600 wheels for a few weeks just to make the 5484 03:21:23,920 --> 03:21:27,399 code more robust but in a few weeks time 5485 03:21:25,600 --> 03:21:29,880 we'll take the library away and you'll 5486 03:21:27,399 --> 03:21:32,439 see and understand how it's doing all 5487 03:21:29,880 --> 03:21:34,399 it's indeed doing all that all right so 5488 03:21:32,439 --> 03:21:36,160 how about this let's now transition to 5489 03:21:34,399 --> 03:21:38,160 something a little more scratch like 5490 03:21:36,160 --> 03:21:40,040 literally uh by creating how about 5491 03:21:38,160 --> 03:21:42,160 another program here called meow so 5492 03:21:40,040 --> 03:21:43,680 meow. C we won't have any audio 5493 03:21:42,160 --> 03:21:46,040 capabilities for this one will just rely 5494 03:21:43,680 --> 03:21:48,479 on print and suppose that I wanted to 5495 03:21:46,040 --> 03:21:50,199 write a program in C that just simulates 5496 03:21:48,479 --> 03:21:51,960 like a cat meowing so I don't need any 5497 03:21:50,199 --> 03:21:54,199 user input just yet so I'm just going to 5498 03:21:51,960 --> 03:21:56,880 use standard i.h I'm going to do my 5499 03:21:54,199 --> 03:21:58,640 usual int main void up here and then I'm 5500 03:21:56,880 --> 03:22:01,760 just going to go ahead and do printf 5501 03:21:58,640 --> 03:22:03,520 meow back sln and let's have this C meow 5502 03:22:01,760 --> 03:22:06,520 three times like last week so I'm going 5503 03:22:03,520 --> 03:22:07,800 to do meow meow meow notice as an aside 5504 03:22:06,520 --> 03:22:09,439 whenever you highlight the lines you'll 5505 03:22:07,800 --> 03:22:11,319 see little dots appear this is just a 5506 03:22:09,439 --> 03:22:13,880 visual cue to you to let you figure out 5507 03:22:11,319 --> 03:22:15,560 how many spaces you've indented vs code 5508 03:22:13,880 --> 03:22:17,279 like a lot of editors will automatically 5509 03:22:15,560 --> 03:22:18,920 indent your code for you I've not been 5510 03:22:17,279 --> 03:22:20,960 hitting the space bar four times every 5511 03:22:18,920 --> 03:22:23,840 time I've not even been hitting tab 5512 03:22:20,960 --> 03:22:26,520 however in C the convention is indeed to 5513 03:22:23,840 --> 03:22:28,960 indent lines where appropriate by four 5514 03:22:26,520 --> 03:22:30,479 spaces so not three not five and these 5515 03:22:28,960 --> 03:22:32,600 dots help you see things so that they 5516 03:22:30,479 --> 03:22:34,120 just line up as a matter of good style 5517 03:22:32,600 --> 03:22:35,640 all right so this program I'm just going 5518 03:22:34,120 --> 03:22:38,040 to stipulate right now is indeed going 5519 03:22:35,640 --> 03:22:41,080 to work make meow which is kind of cute 5520 03:22:38,040 --> 03:22:42,680 and now meow there three times correct 5521 03:22:41,080 --> 03:22:44,080 it's meowing three times but of course 5522 03:22:42,680 --> 03:22:47,279 this is not well designed it wasn't well 5523 03:22:44,080 --> 03:22:48,960 designed in scratch last week why what 5524 03:22:47,279 --> 03:22:51,040 should I be doing 5525 03:22:48,960 --> 03:22:52,800 differently 5526 03:22:51,040 --> 03:22:55,640 yeah 5527 03:22:52,800 --> 03:22:57,199 yeah it's a perfect like opportunity for 5528 03:22:55,640 --> 03:22:58,960 a loop why because if you wanted to 5529 03:22:57,199 --> 03:23:00,920 change maybe the capitalization of these 5530 03:22:58,960 --> 03:23:02,760 words you wanted to change the sound to 5531 03:23:00,920 --> 03:23:04,520 like woof or a dog or something like 5532 03:23:02,760 --> 03:23:06,560 you'd have to change it one two three 5533 03:23:04,520 --> 03:23:08,120 places and that's just kind of stupid 5534 03:23:06,560 --> 03:23:11,120 right in code you should ideally change 5535 03:23:08,120 --> 03:23:13,840 things in one place so how might I do 5536 03:23:11,120 --> 03:23:15,279 that well we could introduce a loop yes 5537 03:23:13,840 --> 03:23:17,040 but we're going to need another building 5538 03:23:15,279 --> 03:23:19,399 block as well that we had in scratch 5539 03:23:17,040 --> 03:23:21,000 namely those things called variables so 5540 03:23:19,399 --> 03:23:24,479 we're call that a variable like in 5541 03:23:21,000 --> 03:23:28,000 algebra x y z whatever can store a value 5542 03:23:24,479 --> 03:23:29,840 for you and a variable in scratch might 5543 03:23:28,000 --> 03:23:31,800 have looked like this you use this 5544 03:23:29,840 --> 03:23:33,920 orange puzzle piece to set a variable of 5545 03:23:31,800 --> 03:23:35,720 any name not just X Y or Z but you could 5546 03:23:33,920 --> 03:23:37,880 call it something more descriptive like 5547 03:23:35,720 --> 03:23:41,560 counter and you can set it equal to some 5548 03:23:37,880 --> 03:23:43,239 value in C the way to do this is similar 5549 03:23:41,560 --> 03:23:45,239 to to Spirit to some of the syntax we've 5550 03:23:43,239 --> 03:23:46,960 seen thus far you start by saying the 5551 03:23:45,239 --> 03:23:48,640 name of the variable you want a single 5552 03:23:46,960 --> 03:23:51,319 equal sign and then the value you want 5553 03:23:48,640 --> 03:23:53,239 to initialize it to copying therefore 5554 03:23:51,319 --> 03:23:55,359 from right to left why because the equal 5555 03:23:53,239 --> 03:23:57,439 sign denotes again assignment from right 5556 03:23:55,359 --> 03:23:58,920 to left this isn't enough though you 5557 03:23:57,439 --> 03:24:01,600 might have the intuition already what's 5558 03:23:58,920 --> 03:24:04,439 missing probably from this line of code 5559 03:24:01,600 --> 03:24:06,199 just to create a variable so we need int 5560 03:24:04,439 --> 03:24:09,239 to make sure the computer knows that 5561 03:24:06,199 --> 03:24:11,359 this is indeed an INT and then lastly 5562 03:24:09,239 --> 03:24:12,680 semicolon as well and that now completes 5563 03:24:11,359 --> 03:24:14,520 the thought so a little more annoying 5564 03:24:12,680 --> 03:24:16,199 than scratch but we're starting to see 5565 03:24:14,520 --> 03:24:17,920 patterns here so not every piece of 5566 03:24:16,199 --> 03:24:20,040 syntax will be new all right if you 5567 03:24:17,920 --> 03:24:21,800 wanted to increment the counter by one 5568 03:24:20,040 --> 03:24:24,399 scratch uses the verb change and they 5569 03:24:21,800 --> 03:24:27,000 mean add the value to counter so if I 5570 03:24:24,399 --> 03:24:29,319 want to increment an existing variable 5571 03:24:27,000 --> 03:24:31,680 called counter this syntax is a little 5572 03:24:29,319 --> 03:24:34,160 more interesting it turns out the code 5573 03:24:31,680 --> 03:24:37,080 looks like this which almost seems like 5574 03:24:34,160 --> 03:24:39,120 a paradox like how can counter equal 5575 03:24:37,080 --> 03:24:41,600 counter plus one like that's not how 5576 03:24:39,120 --> 03:24:43,760 math works but again a single equal sign 5577 03:24:41,600 --> 03:24:45,120 is assignment from right to left so this 5578 03:24:43,760 --> 03:24:47,479 is saying take whatever the value of 5579 03:24:45,120 --> 03:24:49,920 counter is add one to it and copy that 5580 03:24:47,479 --> 03:24:53,479 value from right to left into counter 5581 03:24:49,920 --> 03:24:55,960 itself you still need the semicolon but 5582 03:24:53,479 --> 03:24:59,199 I claim you do not need to mention the 5583 03:24:55,960 --> 03:25:01,279 keyword int when updating an existing 5584 03:24:59,199 --> 03:25:04,040 variable so only when you create a 5585 03:25:01,279 --> 03:25:05,840 variable in C do you use the word string 5586 03:25:04,040 --> 03:25:07,960 or the word int or any of the others 5587 03:25:05,840 --> 03:25:10,120 will eventually see only when creating 5588 03:25:07,960 --> 03:25:11,680 it or initializing It For the First Time 5589 03:25:10,120 --> 03:25:13,720 thereafter if you want to change it it 5590 03:25:11,680 --> 03:25:15,199 just exists it's the word you gave it 5591 03:25:13,720 --> 03:25:17,439 the computer's smart enough to at least 5592 03:25:15,199 --> 03:25:20,359 remember what type it is so this line is 5593 03:25:17,439 --> 03:25:21,920 now complete turns out in code as we'll 5594 03:25:20,359 --> 03:25:23,560 see it's pretty common to want to add 5595 03:25:21,920 --> 03:25:25,080 things together increase increment 5596 03:25:23,560 --> 03:25:27,520 Things by One so there's actually 5597 03:25:25,080 --> 03:25:29,880 different Syntax for the same idea the 5598 03:25:27,520 --> 03:25:32,239 term of art here is syntactic sugar like 5599 03:25:29,880 --> 03:25:33,720 there's often in code many ways to do 5600 03:25:32,239 --> 03:25:35,800 the same thing even though at the end of 5601 03:25:33,720 --> 03:25:38,120 the day they do exactly the same 5602 03:25:35,800 --> 03:25:40,239 functionality so for instance if after a 5603 03:25:38,120 --> 03:25:42,479 few days of cs50 you find this a little 5604 03:25:40,239 --> 03:25:44,760 tedious to keep typing and some program 5605 03:25:42,479 --> 03:25:47,040 you can simplify it to just this this is 5606 03:25:44,760 --> 03:25:49,199 the syntactic sugar you can use plus 5607 03:25:47,040 --> 03:25:51,239 equals and only mention the variable 5608 03:25:49,199 --> 03:25:53,279 name once on the left and it just knows 5609 03:25:51,239 --> 03:25:57,120 that that means the previous thing it's 5610 03:25:53,279 --> 03:25:58,800 just slightly uh more uh more succinct 5611 03:25:57,120 --> 03:26:00,160 this to is such a common thing to add 5612 03:25:58,800 --> 03:26:01,720 one to a value and it doesn't have to be 5613 03:26:00,160 --> 03:26:03,960 one but in this case it is but if it is 5614 03:26:01,720 --> 03:26:07,520 indeed one you can further tighten the 5615 03:26:03,960 --> 03:26:09,800 code up to just do this counter Plus+ so 5616 03:26:07,520 --> 03:26:12,359 anytime in C you see plus plus it means 5617 03:26:09,800 --> 03:26:14,319 literally adding one to that particular 5618 03:26:12,359 --> 03:26:16,160 variable there's other ways to do this 5619 03:26:14,319 --> 03:26:18,520 in the other direction if you want to 5620 03:26:16,160 --> 03:26:20,439 subtract one from a variable you can use 5621 03:26:18,520 --> 03:26:22,640 any of the previous syntax using a minus 5622 03:26:20,439 --> 03:26:25,239 sign instead of Plus or you can more 5623 03:26:22,640 --> 03:26:27,760 succinctly do counter minus minus this 5624 03:26:25,239 --> 03:26:30,680 is the way a typical C programmer would 5625 03:26:27,760 --> 03:26:33,439 do this all right so if we have now 5626 03:26:30,680 --> 03:26:35,520 variables let's go and solve the meowing 5627 03:26:33,439 --> 03:26:37,120 with loops so in scratch we saw Loops 5628 03:26:35,520 --> 03:26:41,120 like this this of course had the cat 5629 03:26:37,120 --> 03:26:42,279 meow three times how do we do this in C 5630 03:26:41,120 --> 03:26:45,800 now this this is where things get a 5631 03:26:42,279 --> 03:26:47,760 little more uh involved codewise but if 5632 03:26:45,800 --> 03:26:50,640 you understand each and every line it 5633 03:26:47,760 --> 03:26:54,160 will follow logically what's going on so 5634 03:26:50,640 --> 03:26:57,120 here I claim is one way to implement a 5635 03:26:54,160 --> 03:26:59,120 loop that iterates three times in C and 5636 03:26:57,120 --> 03:27:01,120 this is kind of ridiculous right like we 5637 03:26:59,120 --> 03:27:03,479 went from two super simple puzzle pieces 5638 03:27:01,120 --> 03:27:05,000 like this to my God like it's 1 2 3 four 5639 03:27:03,479 --> 03:27:06,720 five six lines of code all of which are 5640 03:27:05,000 --> 03:27:08,640 pretty involved so like that escalated 5641 03:27:06,720 --> 03:27:10,120 quickly but what's each line doing and 5642 03:27:08,640 --> 03:27:12,960 we'll see other ways to do this more 5643 03:27:10,120 --> 03:27:16,000 simply so we're izing a variable called 5644 03:27:12,960 --> 03:27:17,760 counter to three just like before why 5645 03:27:16,000 --> 03:27:19,800 well what does it mean to Loop or to 5646 03:27:17,760 --> 03:27:21,800 repeat something three times well it's 5647 03:27:19,800 --> 03:27:23,720 kind of like doing something three times 5648 03:27:21,800 --> 03:27:25,239 and then do it and then count down and 5649 03:27:23,720 --> 03:27:28,239 then do it and then count down and then 5650 03:27:25,239 --> 03:27:29,800 do it until you're all out of counts so 5651 03:27:28,239 --> 03:27:31,880 this is declaring a variable called 5652 03:27:29,800 --> 03:27:34,840 counter setting it equal to three then 5653 03:27:31,880 --> 03:27:37,319 I'm inducing a loop in C which is 5654 03:27:34,840 --> 03:27:39,040 similar in spirit to repeat three but 5655 03:27:37,319 --> 03:27:40,359 you have to do more of the math yourself 5656 03:27:39,040 --> 03:27:41,880 so I'm asking the question in 5657 03:27:40,359 --> 03:27:44,840 parentheses 5658 03:27:41,880 --> 03:27:47,239 while counter is greater than zero what 5659 03:27:44,840 --> 03:27:49,720 do I want to do well Pur the indentation 5660 03:27:47,239 --> 03:27:51,880 inside the curly braces I want to meow 5661 03:27:49,720 --> 03:27:54,239 one time and then to be clear what's 5662 03:27:51,880 --> 03:27:57,399 this last line of code doing if counter 5663 03:27:54,239 --> 03:27:59,439 starts off at three this makes it two by 5664 03:27:57,399 --> 03:28:01,920 subtracting one from it then what 5665 03:27:59,439 --> 03:28:03,800 happens by nature of a loop just like in 5666 03:28:01,920 --> 03:28:05,160 scratch it kind of knows to go back and 5667 03:28:03,800 --> 03:28:07,760 forth even though there's nice pretty 5668 03:28:05,160 --> 03:28:10,560 arrow in scratch and there isn't here C 5669 03:28:07,760 --> 03:28:13,040 knows to do this again and again and 5670 03:28:10,560 --> 03:28:15,439 again con stantly asking this question 5671 03:28:13,040 --> 03:28:17,080 and then updating this value at the end 5672 03:28:15,439 --> 03:28:20,359 so if I highlight just a few of these 5673 03:28:17,080 --> 03:28:21,760 steps the variable starts off at three 5674 03:28:20,359 --> 03:28:23,720 and actually let me simplify two I 5675 03:28:21,760 --> 03:28:25,479 claimed earlier that uh when using 5676 03:28:23,720 --> 03:28:28,319 single variables people very often just 5677 03:28:25,479 --> 03:28:29,520 call it I for INT or c for Char or s for 5678 03:28:28,319 --> 03:28:31,279 string unless you have multiple 5679 03:28:29,520 --> 03:28:32,800 variables so let me tighten the code up 5680 03:28:31,279 --> 03:28:34,560 and this already makes it look a little 5681 03:28:32,800 --> 03:28:37,279 more tolerable let me actually tighten 5682 03:28:34,560 --> 03:28:39,520 it up further and one more step so now 5683 03:28:37,279 --> 03:28:41,520 this is about as tight as succinct as 5684 03:28:39,520 --> 03:28:42,800 you can make this code at the moment so 5685 03:28:41,520 --> 03:28:44,359 what's actually going to happen here 5686 03:28:42,800 --> 03:28:46,760 well the first line of code executes and 5687 03:28:44,359 --> 03:28:48,880 that initializes I to 3 then we check 5688 03:28:46,760 --> 03:28:50,880 the condition while I is greater than 5689 03:28:48,880 --> 03:28:52,960 zero is I greater than zero we'll per my 5690 03:28:50,880 --> 03:28:55,680 three fingers obviously so we print out 5691 03:28:52,960 --> 03:28:58,520 meow on the screen then we subtract one 5692 03:28:55,680 --> 03:29:00,960 from I at which point now we have two as 5693 03:28:58,520 --> 03:29:02,760 the value of I then the code goes back 5694 03:29:00,960 --> 03:29:04,399 to the condition and notice the 5695 03:29:02,760 --> 03:29:06,880 condition there is in parentheses that's 5696 03:29:04,399 --> 03:29:09,080 another Boolean expression so Loops can 5697 03:29:06,880 --> 03:29:10,760 use Boolean Expressions just like 5698 03:29:09,080 --> 03:29:12,399 conditionals use Boolean Expressions to 5699 03:29:10,760 --> 03:29:14,160 make decis ision the loop though is 5700 03:29:12,399 --> 03:29:16,080 deciding not whether to do this thing or 5701 03:29:14,160 --> 03:29:18,439 that but whether to do the same thing 5702 03:29:16,080 --> 03:29:20,319 again and again and again and as it 5703 03:29:18,439 --> 03:29:22,399 ticks through the code one line after 5704 03:29:20,319 --> 03:29:26,000 the other it's ultimately going to get 5705 03:29:22,399 --> 03:29:28,600 down to uh one and then zero and then 5706 03:29:26,000 --> 03:29:31,840 stop so put another way came with some 5707 03:29:28,600 --> 03:29:33,880 props here so suppose uh this bowl here 5708 03:29:31,840 --> 03:29:36,160 is your variable and you initialize it 5709 03:29:33,880 --> 03:29:38,160 to three with like three stress balls 5710 03:29:36,160 --> 03:29:39,960 you can do something three times right 5711 03:29:38,160 --> 03:29:41,319 if I want to give out three stress balls 5712 03:29:39,960 --> 03:29:42,880 here's your chance for free stress ball 5713 03:29:41,319 --> 03:29:45,040 without having to answer any questions 5714 03:29:42,880 --> 03:29:46,920 any okay there we go so here we go 5715 03:29:45,040 --> 03:29:50,720 subtracting one from my variable I'm 5716 03:29:46,920 --> 03:29:52,000 left with two uh oh my God all right uh 5717 03:29:50,720 --> 03:29:56,600 don't tell 5718 03:29:52,000 --> 03:29:59,199 Sounders oh I'm sorry oh okay that ended 5719 03:29:56,600 --> 03:30:01,160 poorly apologies all right but now the 5720 03:29:59,199 --> 03:30:03,160 educational point though is that my 5721 03:30:01,160 --> 03:30:04,680 variable has been uh decremented further 5722 03:30:03,160 --> 03:30:07,040 to just have I'm not throwing that far 5723 03:30:04,680 --> 03:30:08,600 again I can't do this in here we go all 5724 03:30:07,040 --> 03:30:11,520 right here we go and one final 5725 03:30:08,600 --> 03:30:13,640 subtraction and now our variable is left 5726 03:30:11,520 --> 03:30:15,239 so we have three stress balls there and 5727 03:30:13,640 --> 03:30:17,000 that's all a variable is right it's some 5728 03:30:15,239 --> 03:30:19,319 kind of storage it's actually of course 5729 03:30:17,000 --> 03:30:21,080 implemented in the computer's memory but 5730 03:30:19,319 --> 03:30:23,040 metaphorically it's really just like a 5731 03:30:21,080 --> 03:30:24,760 bow with some values and every time you 5732 03:30:23,040 --> 03:30:26,560 add or in this case subtract you're just 5733 03:30:24,760 --> 03:30:28,720 changing the value of that variable and 5734 03:30:26,560 --> 03:30:30,880 then the code meanwhile of course in 5735 03:30:28,720 --> 03:30:32,399 parenthesis is just checking is the bow 5736 03:30:30,880 --> 03:30:37,080 empty is the bowl empty is the bowl 5737 03:30:32,399 --> 03:30:38,840 empty AKA is I greater than zero or not 5738 03:30:37,080 --> 03:30:41,800 any questions on how we've implemented 5739 03:30:38,840 --> 03:30:44,760 Loops in this way 5740 03:30:41,800 --> 03:30:47,600 and I I'll you a stress ball after class 5741 03:30:44,760 --> 03:30:50,680 questions on Loops all right so it turns 5742 03:30:47,600 --> 03:30:52,359 out this is kind of ugly and like this 5743 03:30:50,680 --> 03:30:54,359 really starts to take the fun out of 5744 03:30:52,359 --> 03:30:56,199 programming uh when you have to like 5745 03:30:54,359 --> 03:30:57,640 write out this uh sequence of steps so 5746 03:30:56,199 --> 03:30:59,439 it turns out there's other ways to do 5747 03:30:57,640 --> 03:31:00,800 this but first let's see logically how 5748 03:30:59,439 --> 03:31:03,160 else you might Express this because it's 5749 03:31:00,800 --> 03:31:05,359 a little weird that we keep using zero 5750 03:31:03,160 --> 03:31:07,560 so the one other way to do this would be 5751 03:31:05,359 --> 03:31:09,760 to invert the logic you could absolutely 5752 03:31:07,560 --> 03:31:12,160 start with your variable call it I equal 5753 03:31:09,760 --> 03:31:15,359 to one and then you ask the question is 5754 03:31:12,160 --> 03:31:17,640 I less than or equal to three and notice 5755 03:31:15,359 --> 03:31:19,880 a bit of new syntax here on your uh 5756 03:31:17,640 --> 03:31:21,760 typical keyboard there is no less than 5757 03:31:19,880 --> 03:31:23,160 or equal sign or greater than or equal 5758 03:31:21,760 --> 03:31:25,680 sign like you would write in math class 5759 03:31:23,160 --> 03:31:28,080 with one over the other and so in C you 5760 03:31:25,680 --> 03:31:30,399 use two characters less than followed by 5761 03:31:28,080 --> 03:31:32,359 an equal sign or if appropriate greater 5762 03:31:30,399 --> 03:31:34,880 than followed by an equal sign and that 5763 03:31:32,359 --> 03:31:37,160 logically captures that idea so notice 5764 03:31:34,880 --> 03:31:39,080 that I'm kind of changing my questions 5765 03:31:37,160 --> 03:31:41,479 I'm initializing I to one and then I'm 5766 03:31:39,080 --> 03:31:43,399 going to increment It ultimately to two 5767 03:31:41,479 --> 03:31:44,960 and then three but because I'm doing 5768 03:31:43,399 --> 03:31:48,279 less than or equal to it's still going 5769 03:31:44,960 --> 03:31:50,199 to go from 1 to three so that works too 5770 03:31:48,279 --> 03:31:53,359 we could similarly do this yet another 5771 03:31:50,199 --> 03:31:56,439 way we could initialize I to zero and 5772 03:31:53,359 --> 03:31:58,479 then we could say well I is less than 5773 03:31:56,439 --> 03:32:00,239 three and keep incrementing it and I 5774 03:31:58,479 --> 03:32:02,560 showed this last form is actually the 5775 03:32:00,239 --> 03:32:04,439 most canonical like it might be the most 5776 03:32:02,560 --> 03:32:06,760 humanlike to think in terms of 1 to 5777 03:32:04,439 --> 03:32:09,279 three it might be the most stress ball 5778 03:32:06,760 --> 03:32:11,920 like to think in terms of three to zero 5779 03:32:09,279 --> 03:32:13,760 counting down but Ty typically the go-to 5780 03:32:11,920 --> 03:32:16,239 Syntax for most programmers once you get 5781 03:32:13,760 --> 03:32:18,680 comfortable counting from zero is to 5782 03:32:16,239 --> 03:32:21,279 always start counting from zero and 5783 03:32:18,680 --> 03:32:23,399 count up to less than the value you're 5784 03:32:21,279 --> 03:32:25,199 counting up to so it would be incorrect 5785 03:32:23,399 --> 03:32:27,760 why to change this to less than or equal 5786 03:32:25,199 --> 03:32:29,000 to three here what would happen if I 5787 03:32:27,760 --> 03:32:31,920 Chang the less than to less than or 5788 03:32:29,000 --> 03:32:33,920 equal to it'll me out twice yeah it'll 5789 03:32:31,920 --> 03:32:35,600 out an extra a fourth time in fact total 5790 03:32:33,920 --> 03:32:38,359 right because you'll start at zero then 5791 03:32:35,600 --> 03:32:40,399 one then two then three and less than or 5792 03:32:38,359 --> 03:32:43,040 equal to three sorry three will give you 5793 03:32:40,399 --> 03:32:45,680 the fourth time so we do want it indeed 5794 03:32:43,040 --> 03:32:48,000 to be just a single less than all right 5795 03:32:45,680 --> 03:32:49,439 so now that we have those options let me 5796 03:32:48,000 --> 03:32:50,960 just give you one other and this one 5797 03:32:49,439 --> 03:32:52,640 takes a little more getting used to as 5798 03:32:50,960 --> 03:32:54,760 well but it's probably the more common 5799 03:32:52,640 --> 03:32:57,560 way to write this let me go ahead and 5800 03:32:54,760 --> 03:33:00,040 propose that we implement this as 5801 03:32:57,560 --> 03:33:04,479 follows let me go back to my code here 5802 03:33:00,040 --> 03:33:06,800 let me go into my several printfs 5803 03:33:04,479 --> 03:33:08,920 getting rid of all but one of them 5804 03:33:06,800 --> 03:33:12,000 ultimately and let's implement this in 5805 03:33:08,920 --> 03:33:16,279 code so let's do in I get that's zero 5806 03:33:12,000 --> 03:33:18,800 how about then while I is less than 5807 03:33:16,279 --> 03:33:22,800 three then let's go ahead and say print 5808 03:33:18,800 --> 03:33:26,880 F quote unquote meow uh mellow meow back 5809 03:33:22,800 --> 03:33:28,080 sln and then we have to do I minus minus 5810 03:33:26,880 --> 03:33:30,439 or plus 5811 03:33:28,080 --> 03:33:33,160 plus so plus plus because we're starting 5812 03:33:30,439 --> 03:33:35,439 at zero and going up two but not through 5813 03:33:33,160 --> 03:33:38,239 three so let me go ahead now and make 5814 03:33:35,439 --> 03:33:40,000 meow after clearing my terminal SL meow 5815 03:33:38,239 --> 03:33:42,960 and it's still just as correct but it's 5816 03:33:40,000 --> 03:33:44,920 a little more uh it's a little better 5817 03:33:42,960 --> 03:33:47,000 designed why because now if I want to 5818 03:33:44,920 --> 03:33:48,840 change it from three to 30 times for 5819 03:33:47,000 --> 03:33:52,279 instance I can change it there I can 5820 03:33:48,840 --> 03:33:53,920 recompile my code I can do/ meow and 5821 03:33:52,279 --> 03:33:56,239 done I don't have to copy and paste it 5822 03:33:53,920 --> 03:33:58,000 27 more times to get that effect and I 5823 03:33:56,239 --> 03:34:00,319 can even change what the word is by 5824 03:33:58,000 --> 03:34:02,000 changing it in just one location but it 5825 03:34:00,319 --> 03:34:04,279 turns out there's other ways to do this 5826 03:34:02,000 --> 03:34:07,239 too and let me propose that we introduce 5827 03:34:04,279 --> 03:34:08,760 you to what's called a for loop as well 5828 03:34:07,239 --> 03:34:10,840 so if you want to repeat something three 5829 03:34:08,760 --> 03:34:13,080 times you can absolutely take the while 5830 03:34:10,840 --> 03:34:15,239 Loop approach that we just saw or you 5831 03:34:13,080 --> 03:34:16,960 can do this and this one's a little 5832 03:34:15,239 --> 03:34:19,160 takes a little more getting used to but 5833 03:34:16,960 --> 03:34:21,760 it kind of consolidates into one line 5834 03:34:19,160 --> 03:34:24,640 all of the same logic so notice we have 5835 03:34:21,760 --> 03:34:26,239 the keyword four here and four is just a 5836 03:34:24,640 --> 03:34:28,960 a preposition in this case that 5837 03:34:26,239 --> 03:34:32,040 generally implies here comes a loop 5838 03:34:28,960 --> 03:34:33,640 inside of parenthesis here is not just a 5839 03:34:32,040 --> 03:34:35,920 Boolean expression and this where things 5840 03:34:33,640 --> 03:34:37,800 get a little weird there's three things 5841 03:34:35,920 --> 03:34:39,720 to the left of the semicolon in the 5842 03:34:37,800 --> 03:34:42,080 middle of the two semicolons and to the 5843 03:34:39,720 --> 03:34:43,600 right of the semicolon this is really 5844 03:34:42,080 --> 03:34:45,520 the only other context we'll see 5845 03:34:43,600 --> 03:34:47,319 semicolons and it's weird normally it's 5846 03:34:45,520 --> 03:34:48,800 been at the end of the line now it's two 5847 03:34:47,319 --> 03:34:50,760 of them in the middle of the line but 5848 03:34:48,800 --> 03:34:53,160 this is the way humans decided years ago 5849 03:34:50,760 --> 03:34:56,479 to do it so what is this doing almost 5850 03:34:53,160 --> 03:34:59,680 the same thing it is going to initialize 5851 03:34:56,479 --> 03:35:03,239 a variable called I to zero it's going 5852 03:34:59,680 --> 03:35:05,120 to then check if it's less than three 5853 03:35:03,239 --> 03:35:07,720 it's then going to do what's ever in the 5854 03:35:05,120 --> 03:35:10,120 curly braces is and it's lastly going to 5855 03:35:07,720 --> 03:35:12,960 increment I and repeat so just 5856 03:35:10,120 --> 03:35:15,199 highlighting in turn at first I is 5857 03:35:12,960 --> 03:35:17,640 initialized to zero just like before 5858 03:35:15,199 --> 03:35:19,800 then this condition is checked this is a 5859 03:35:17,640 --> 03:35:22,040 Boolean expression yes or no true or 5860 03:35:19,800 --> 03:35:23,479 false will be its answer and if I is 5861 03:35:22,040 --> 03:35:25,399 less than three which it should be once 5862 03:35:23,479 --> 03:35:28,040 it starts at zero well then we're going 5863 03:35:25,399 --> 03:35:29,800 to go ahead and print out meow then I is 5864 03:35:28,040 --> 03:35:32,640 going to get incremented so it starts at 5865 03:35:29,800 --> 03:35:34,800 zero it goes now to one at that point 5866 03:35:32,640 --> 03:35:37,080 the Boolean expression is checked again 5867 03:35:34,800 --> 03:35:40,000 so you don't keep changing I back to 5868 03:35:37,080 --> 03:35:41,960 zero that first step happens only once 5869 03:35:40,000 --> 03:35:43,960 but now you repeat through those three 5870 03:35:41,960 --> 03:35:46,439 other highlights I check if I is less 5871 03:35:43,960 --> 03:35:49,239 than three it is so I print out meow it 5872 03:35:46,439 --> 03:35:51,199 then increments I I check if I now two 5873 03:35:49,239 --> 03:35:54,120 is less than three it is I print out 5874 03:35:51,199 --> 03:35:56,319 meow I gets incremented I now check is I 5875 03:35:54,120 --> 03:35:58,439 less than three no it's not because 5876 03:35:56,319 --> 03:36:00,560 three is not less than three and so the 5877 03:35:58,439 --> 03:36:03,120 whole thing stops and whatever code is 5878 03:36:00,560 --> 03:36:04,840 below this curly brace if any starts 5879 03:36:03,120 --> 03:36:07,439 executing instead just like in scratch 5880 03:36:04,840 --> 03:36:10,080 you break out of the loop and the the 5881 03:36:07,439 --> 03:36:13,720 puzzle piece being hugged questions then 5882 03:36:10,080 --> 03:36:19,399 about this alternative Syntax 5883 03:36:13,720 --> 03:36:19,399 for Loops AKA a four 5884 03:36:20,279 --> 03:36:25,560 Loop sorry say 5885 03:36:23,319 --> 03:36:27,720 again yeah can I explain again why it 5886 03:36:25,560 --> 03:36:29,640 doesn't reset to zero honestly just 5887 03:36:27,720 --> 03:36:31,640 because like this was the syntax they 5888 03:36:29,640 --> 03:36:33,840 chose this first part before the first 5889 03:36:31,640 --> 03:36:35,520 semicolon is only executed once just 5890 03:36:33,840 --> 03:36:38,359 because that's how it's designed 5891 03:36:35,520 --> 03:36:40,359 everything else Cycles again and again 5892 03:36:38,359 --> 03:36:41,960 and this is just an alternative syntax 5893 03:36:40,359 --> 03:36:43,439 to to using the slightly more lines of 5894 03:36:41,960 --> 03:36:45,399 code it was like six lines of code using 5895 03:36:43,439 --> 03:36:47,319 the while loop logically it's the same 5896 03:36:45,399 --> 03:36:48,800 thing programmers once they get more 5897 03:36:47,319 --> 03:36:50,399 comfortable tend to prefer this because 5898 03:36:48,800 --> 03:36:55,439 it just expresses all your same thoughts 5899 03:36:50,399 --> 03:36:57,439 more succinctly that's all yeah okay so 5900 03:36:55,439 --> 03:36:59,319 let's just work this into my meow 5901 03:36:57,439 --> 03:37:00,960 example let me go back to the code here 5902 03:36:59,319 --> 03:37:02,560 and notice indeed if I highlight all 5903 03:37:00,960 --> 03:37:04,080 these lines I think we can tighten this 5904 03:37:02,560 --> 03:37:08,640 up let me get rid of all of those and 5905 03:37:04,080 --> 03:37:10,520 instead do four in I equals 0 and I'm 5906 03:37:08,640 --> 03:37:13,720 saying equals most programmers would say 5907 03:37:10,520 --> 03:37:16,399 gets so in I gets zero means assignment 5908 03:37:13,720 --> 03:37:19,800 the word get now I'm going to do I is 5909 03:37:16,399 --> 03:37:22,279 less than three i+ plus now in here I'm 5910 03:37:19,800 --> 03:37:25,399 going to do my print F quote unquote 5911 03:37:22,279 --> 03:37:26,600 meow back sln and so it's indeed a 5912 03:37:25,399 --> 03:37:28,040 little tighter I mean two of the lines 5913 03:37:26,600 --> 03:37:30,479 are just curly braces there's really 5914 03:37:28,040 --> 03:37:32,160 only two juicy lines of code now let me 5915 03:37:30,479 --> 03:37:35,319 go ahead and do make 5916 03:37:32,160 --> 03:37:37,279 meow meow and again we're back in 5917 03:37:35,319 --> 03:37:39,239 business with three of them printing 5918 03:37:37,279 --> 03:37:41,000 only all right there's one last 5919 03:37:39,239 --> 03:37:44,080 structure we should explore just cuz 5920 03:37:41,000 --> 03:37:46,120 it's sometimes useful this was a forever 5921 03:37:44,080 --> 03:37:48,160 block and this would be a little weird 5922 03:37:46,120 --> 03:37:49,920 in scratch to just say meow Forever at 5923 03:37:48,160 --> 03:37:51,479 least without waiting but there is 5924 03:37:49,920 --> 03:37:54,479 indeed a forever Block in scratch which 5925 03:37:51,479 --> 03:37:56,359 means do the following forever and I 5926 03:37:54,479 --> 03:37:57,920 proposed I think verbally last week's at 5927 03:37:56,359 --> 03:37:59,840 least one example where this is useful 5928 03:37:57,920 --> 03:38:01,560 meowing forever little annoying but can 5929 03:37:59,840 --> 03:38:03,520 you think of common cases where you 5930 03:38:01,560 --> 03:38:07,600 might want to write code or use a 5931 03:38:03,520 --> 03:38:10,479 program that Loops forever 5932 03:38:07,600 --> 03:38:12,080 yeah yeah playing music like Spotify 5933 03:38:10,479 --> 03:38:14,040 playlist just repeating again and again 5934 03:38:12,080 --> 03:38:16,040 would be some kind of loop for 5935 03:38:14,040 --> 03:38:17,199 collisions checking for collisions and 5936 03:38:16,040 --> 03:38:21,080 scratch so seeing if something's 5937 03:38:17,199 --> 03:38:21,080 bouncing off the wall or another Sprite 5938 03:38:22,520 --> 03:38:27,000 yeah checking for input so yeah get 5939 03:38:25,040 --> 03:38:28,680 string is essentially just waiting there 5940 03:38:27,000 --> 03:38:31,920 forever for me to type in some input 5941 03:38:28,680 --> 03:38:33,640 until I do the time checking the time 5942 03:38:31,920 --> 03:38:35,479 and actually maintaining like human time 5943 03:38:33,640 --> 03:38:36,399 like a wall clock behind you was that 5944 03:38:35,479 --> 03:38:39,520 the 5945 03:38:36,399 --> 03:38:40,960 same okay checking the time and one more 5946 03:38:39,520 --> 03:38:42,520 detecting a key press too like in 5947 03:38:40,960 --> 03:38:44,359 scratch just waiting for some kind of 5948 03:38:42,520 --> 03:38:45,840 event to happen just like on a phone or 5949 03:38:44,359 --> 03:38:47,239 a browser and so there's so many 5950 03:38:45,840 --> 03:38:48,960 examples where you might want to do 5951 03:38:47,239 --> 03:38:51,319 something forever just so you've seen 5952 03:38:48,960 --> 03:38:53,720 the corresponding C building block it's 5953 03:38:51,319 --> 03:38:56,800 a little weird but this is probably the 5954 03:38:53,720 --> 03:38:58,760 most canonical way to do it in C if you 5955 03:38:56,800 --> 03:39:00,560 want to print meow forever which would 5956 03:38:58,760 --> 03:39:02,120 be a little crazy because it literally 5957 03:39:00,560 --> 03:39:04,040 print and take over your computer 5958 03:39:02,120 --> 03:39:06,479 printing forever meow you would 5959 03:39:04,040 --> 03:39:08,479 generally do it like this why well a 5960 03:39:06,479 --> 03:39:09,880 while loop expects in parentheses a 5961 03:39:08,479 --> 03:39:11,760 Boolean expression and a Boolean 5962 03:39:09,880 --> 03:39:13,720 expression is again a yes no a true 5963 03:39:11,760 --> 03:39:16,160 false question but if you want the 5964 03:39:13,720 --> 03:39:18,760 answer to that question always to be yes 5965 03:39:16,160 --> 03:39:20,800 or really always to be true turns out in 5966 03:39:18,760 --> 03:39:23,880 C in a lot of languages well then just 5967 03:39:20,800 --> 03:39:25,720 say true because true T R is never going 5968 03:39:23,880 --> 03:39:27,600 to change magically to false I mean it's 5969 03:39:25,720 --> 03:39:29,960 just a special word in the programming 5970 03:39:27,600 --> 03:39:32,880 language so by saying while true it just 5971 03:39:29,960 --> 03:39:35,199 means do the following forever another 5972 03:39:32,880 --> 03:39:37,880 common Paradigm before true and false 5973 03:39:35,199 --> 03:39:41,000 became common place would be to do uh 5974 03:39:37,880 --> 03:39:43,439 this instead change uh while one you 5975 03:39:41,000 --> 03:39:45,600 might see in online examples and texts 5976 03:39:43,439 --> 03:39:48,880 and the like while one is really the 5977 03:39:45,600 --> 03:39:50,840 same thing any value that is zero is 5978 03:39:48,880 --> 03:39:54,199 generally interpreted as false by a 5979 03:39:50,840 --> 03:39:56,439 computer any value that is one or any 5980 03:39:54,199 --> 03:39:58,800 other nonzero value is generally 5981 03:39:56,439 --> 03:40:00,600 interpreted as true and so this too 5982 03:39:58,800 --> 03:40:02,840 would have the same effect saying while 5983 03:40:00,600 --> 03:40:04,800 true or while one generally speaking 5984 03:40:02,840 --> 03:40:06,880 while true is perhaps a little clearer 5985 03:40:04,800 --> 03:40:09,040 these days now meowing forever is not a 5986 03:40:06,880 --> 03:40:11,040 good thing but suppose I did that by 5987 03:40:09,040 --> 03:40:13,160 intent or by accident well let's try 5988 03:40:11,040 --> 03:40:14,439 this so here I'll go into my code I'm 5989 03:40:13,160 --> 03:40:17,600 going to get rid of my for Loop and 5990 03:40:14,439 --> 03:40:21,120 change my while loop to uh how about 5991 03:40:17,600 --> 03:40:24,960 true um and in this case here well we'll 5992 03:40:21,120 --> 03:40:27,160 keep it let's do this make meow enter 5993 03:40:24,960 --> 03:40:28,960 and you'll see this use of Undeclared 5994 03:40:27,160 --> 03:40:31,960 identifier or true this is actually 5995 03:40:28,960 --> 03:40:33,840 hinting at my mention that the old way 5996 03:40:31,960 --> 03:40:36,560 was zero and one nowadays you could say 5997 03:40:33,840 --> 03:40:38,960 true or false but true and false are 5998 03:40:36,560 --> 03:40:41,239 themselves special words that you have 5999 03:40:38,960 --> 03:40:43,279 to include and it turns out if you want 6000 03:40:41,239 --> 03:40:44,600 to use special Boolean values like this 6001 03:40:43,279 --> 03:40:47,479 there's another header file we haven't 6002 03:40:44,600 --> 03:40:50,359 seen called standard bu that essentially 6003 03:40:47,479 --> 03:40:52,800 creates true and false as keywords 6004 03:40:50,359 --> 03:40:54,800 alternatively cs50 includes that same 6005 03:40:52,800 --> 03:40:56,920 file so it's more common in cs50 to see 6006 03:40:54,800 --> 03:40:59,359 it like this now if I clear my terminal 6007 03:40:56,920 --> 03:41:02,640 window and do make meow and then do/ 6008 03:40:59,359 --> 03:41:04,600 meow and hit enter well unfortunately 6009 03:41:02,640 --> 03:41:06,880 this isn't the best thing to do uh 6010 03:41:04,600 --> 03:41:09,520 infinitely when you're in the cloud 6011 03:41:06,880 --> 03:41:12,160 using a browser this is indeed a a 6012 03:41:09,520 --> 03:41:14,439 browser full screened here um this means 6013 03:41:12,160 --> 03:41:16,720 I'm sending millions of meows over the 6014 03:41:14,439 --> 03:41:17,760 internet to my computer here uh so this 6015 03:41:16,720 --> 03:41:19,399 will happen to you at some point 6016 03:41:17,760 --> 03:41:21,040 probably not with meow but you'll lose 6017 03:41:19,399 --> 03:41:22,120 control over your terminal window why 6018 03:41:21,040 --> 03:41:23,239 because you screwed up and like you have 6019 03:41:22,120 --> 03:41:24,479 an infinite Loop you didn't really 6020 03:41:23,239 --> 03:41:26,880 intend it or maybe you did you were 6021 03:41:24,479 --> 03:41:30,640 curious to see what happens what do you 6022 03:41:26,880 --> 03:41:32,600 do like when when does the meowing stop 6023 03:41:30,640 --> 03:41:35,319 what recourse do we have here all right 6024 03:41:32,600 --> 03:41:36,600 well control c will be your friend 6025 03:41:35,319 --> 03:41:38,880 sometimes you have to hit it a bunch in 6026 03:41:36,600 --> 03:41:41,520 a cloud environment but control C for 6027 03:41:38,880 --> 03:41:43,880 cancel with will interrupt a program 6028 03:41:41,520 --> 03:41:45,359 that's running and I promise that almost 6029 03:41:43,880 --> 03:41:47,640 all of you will at some point 6030 03:41:45,359 --> 03:41:49,479 accidentally introduce an infinite Loop 6031 03:41:47,640 --> 03:41:51,000 because you're math is slightly off when 6032 03:41:49,479 --> 03:41:53,199 in doubt click in the terminal window 6033 03:41:51,000 --> 03:41:55,080 and hit contrl C sometimes multiple 6034 03:41:53,199 --> 03:41:56,760 times and that will indeed cancel 6035 03:41:55,080 --> 03:41:58,520 whatever is happening there in this case 6036 03:41:56,760 --> 03:42:01,960 I might have intended it but sometimes 6037 03:41:58,520 --> 03:42:03,680 it's not in fact intended all right so 6038 03:42:01,960 --> 03:42:05,840 we've been taking for granted this whole 6039 03:42:03,680 --> 03:42:09,159 graphical user interface for some time 6040 03:42:05,840 --> 03:42:10,560 and indeed uh the uh commands that I'm 6041 03:42:09,159 --> 03:42:11,920 typing and the buttons I'm clicking and 6042 03:42:10,560 --> 03:42:14,239 let me just give you a better sense of 6043 03:42:11,920 --> 03:42:16,560 what it is we are using underneath the 6044 03:42:14,239 --> 03:42:18,720 hood this whole time um namely an 6045 03:42:16,560 --> 03:42:20,399 operating system called Linux so I keep 6046 03:42:18,720 --> 03:42:22,040 alluding verbally of course to Macs and 6047 03:42:20,399 --> 03:42:24,399 PCs because almost all of us are running 6048 03:42:22,040 --> 03:42:26,239 Mac OS or Windows on our desktops or 6049 03:42:24,399 --> 03:42:27,560 laptops nowadays but there's lots of 6050 03:42:26,239 --> 03:42:29,279 other operating systems out there and 6051 03:42:27,560 --> 03:42:32,279 one of the most popular one is called 6052 03:42:29,279 --> 03:42:34,760 Linux and Linux is very often used on 6053 03:42:32,279 --> 03:42:36,720 servers nowadays companies that host 6054 03:42:34,760 --> 03:42:38,920 email companies that host websites or 6055 03:42:36,720 --> 03:42:40,560 apps more generally um certain computer 6056 03:42:38,920 --> 03:42:42,239 scientists or computer science students 6057 03:42:40,560 --> 03:42:44,439 often like to brag that they run Linux 6058 03:42:42,239 --> 03:42:46,920 just because that's a thing um but it is 6059 03:42:44,439 --> 03:42:49,680 really just an alternative to Mac OS or 6060 03:42:46,920 --> 03:42:51,680 windows that provides you with both a 6061 03:42:49,680 --> 03:42:54,199 guey if you want it but also and 6062 03:42:51,680 --> 03:42:56,960 especially a command line environment 6063 03:42:54,199 --> 03:42:58,680 now fun fact Windows and Mac OS do have 6064 03:42:56,960 --> 03:43:00,520 terminal windows or the equivalent 6065 03:42:58,680 --> 03:43:02,279 thereof and eventually you might uh use 6066 03:43:00,520 --> 03:43:04,920 it on your own Mac or PC to solve some 6067 03:43:02,279 --> 03:43:06,520 problem but Linux is really known for 6068 03:43:04,920 --> 03:43:08,479 along with other operating systems it's 6069 03:43:06,520 --> 03:43:10,880 command line environment which again I 6070 03:43:08,479 --> 03:43:13,439 distinguished earlier from Guy as a 6071 03:43:10,880 --> 03:43:16,120 command line interface or CLI and that 6072 03:43:13,439 --> 03:43:18,600 refers really to the terminal window so 6073 03:43:16,120 --> 03:43:20,640 if I go back to VSS code here and let me 6074 03:43:18,600 --> 03:43:22,960 in fact go ahead and close my tab and 6075 03:43:20,640 --> 03:43:24,880 focus entirely on the terminal window 6076 03:43:22,960 --> 03:43:27,479 this terminal window is really just your 6077 03:43:24,880 --> 03:43:29,800 command line interface to your very own 6078 03:43:27,479 --> 03:43:32,000 server in the cloud the term of art here 6079 03:43:29,800 --> 03:43:33,479 is you each will have your own container 6080 03:43:32,000 --> 03:43:35,399 in the cloud which is like your own 6081 03:43:33,479 --> 03:43:37,560 computer running somewhere on the 6082 03:43:35,399 --> 03:43:39,199 internet with your own username and 6083 03:43:37,560 --> 03:43:41,439 password to which you have access and 6084 03:43:39,199 --> 03:43:43,239 your own hard drive if you will your own 6085 03:43:41,439 --> 03:43:45,439 home folder that has all of your files 6086 03:43:43,239 --> 03:43:47,359 for the class and it's only accessible 6087 03:43:45,439 --> 03:43:49,560 to you unless you enable live sharing 6088 03:43:47,359 --> 03:43:51,319 thereof so when you're typing commands 6089 03:43:49,560 --> 03:43:53,399 here it looks like you're typing them of 6090 03:43:51,319 --> 03:43:56,439 course on your own Mac or PC but they're 6091 03:43:53,399 --> 03:43:58,760 actually being sent over the browser to 6092 03:43:56,439 --> 03:44:01,640 uh some server in the cloud where you 6093 03:43:58,760 --> 03:44:04,319 are controlling really your own account 6094 03:44:01,640 --> 03:44:05,960 therein so it turns out that there are 6095 03:44:04,319 --> 03:44:07,520 other commands that are worth knowing 6096 03:44:05,960 --> 03:44:09,359 and we'll give you just a few of these 6097 03:44:07,520 --> 03:44:11,040 today and over the coming weeks will you 6098 03:44:09,359 --> 03:44:12,399 have opport ities to play with others as 6099 03:44:11,040 --> 03:44:14,520 well but these are kind of some of the 6100 03:44:12,399 --> 03:44:16,359 basics and they're all incredibly 6101 03:44:14,520 --> 03:44:17,840 succinct because indeed for things 6102 03:44:16,359 --> 03:44:19,319 you're typing at the command line humans 6103 03:44:17,840 --> 03:44:21,159 generally have not wanted to type out 6104 03:44:19,319 --> 03:44:23,600 long commands so a lot of these are 6105 03:44:21,159 --> 03:44:26,159 abbreviations here now perhaps the most 6106 03:44:23,600 --> 03:44:28,680 common one I'll start with first is LS a 6107 03:44:26,159 --> 03:44:31,760 lowercase l and a lowercase s that 6108 03:44:28,680 --> 03:44:33,800 stands for succinctly list so if I go to 6109 03:44:31,760 --> 03:44:36,000 my terminal window now where up until 6110 03:44:33,800 --> 03:44:37,479 now I've only typed code which is a vs 6111 03:44:36,000 --> 03:44:39,880 code thing for creating and opening 6112 03:44:37,479 --> 03:44:42,960 files and make which triggers the compil 6113 03:44:39,880 --> 03:44:45,760 ation of my code what if I now type LS 6114 03:44:42,960 --> 03:44:47,520 this will list all of the files in my 6115 03:44:45,760 --> 03:44:49,840 current folder my hard drive in the 6116 03:44:47,520 --> 03:44:52,399 cloud if you will so if I hit enter 6117 03:44:49,840 --> 03:44:54,640 you'll see a whole bunch of results now 6118 03:44:52,399 --> 03:44:57,800 they're color-coded too the white ones 6119 03:44:54,640 --> 03:44:59,520 here and in. c those are the source code 6120 03:44:57,800 --> 03:45:02,520 files I've written during class today 6121 03:44:59,520 --> 03:45:04,279 agree. C compare. C hello.c and meow. C 6122 03:45:02,520 --> 03:45:06,040 and you can perhaps guess the green ones 6123 03:45:04,279 --> 03:45:07,520 here that just by convention have an 6124 03:45:06,040 --> 03:45:11,000 asterisk on the end to denote that 6125 03:45:07,520 --> 03:45:13,640 they're special represent what 6126 03:45:11,000 --> 03:45:16,560 what are the four others 6127 03:45:13,640 --> 03:45:18,800 yeah yeah the machine code so those are 6128 03:45:16,560 --> 03:45:21,439 my actual programs that are identically 6129 03:45:18,800 --> 03:45:23,479 named minus the C extension and the 6130 03:45:21,439 --> 03:45:24,840 asterisk means that they're executable 6131 03:45:23,479 --> 03:45:26,560 that is in the world of Mac OS or 6132 03:45:24,840 --> 03:45:28,159 Windows you would double click but in 6133 03:45:26,560 --> 03:45:30,000 the world of a command line environment 6134 03:45:28,159 --> 03:45:32,479 that means you do dot slash and then the 6135 03:45:30,000 --> 03:45:35,080 name without the asterisk to execute or 6136 03:45:32,479 --> 03:45:37,479 run the code therein so if I open up my 6137 03:45:35,080 --> 03:45:39,199 file explorer and I'm hitting command B 6138 03:45:37,479 --> 03:45:41,640 on my computer here just as a keyboard 6139 03:45:39,199 --> 03:45:44,279 shortcut you'll see the exact same thing 6140 03:45:41,640 --> 03:45:46,239 so LS is the command line interface for 6141 03:45:44,279 --> 03:45:48,800 listing the files in your account but 6142 03:45:46,239 --> 03:45:51,600 here because I'm using vs code or any 6143 03:45:48,800 --> 03:45:53,359 program like it I also get a graphical 6144 03:45:51,600 --> 03:45:55,080 user interface as well so it's just two 6145 03:45:53,359 --> 03:45:56,439 different places to be you're welcome to 6146 03:45:55,080 --> 03:45:58,319 use whatever you're comfortable with but 6147 03:45:56,439 --> 03:45:59,960 over time will you naturally get more 6148 03:45:58,319 --> 03:46:02,120 comfortable and capable with the 6149 03:45:59,960 --> 03:46:04,239 terminal window alone well what else is 6150 03:46:02,120 --> 03:46:05,920 on this this list here well during the 6151 03:46:04,239 --> 03:46:08,520 break I saw that at least one of you for 6152 03:46:05,920 --> 03:46:10,720 instance had created a file called hello 6153 03:46:08,520 --> 03:46:12,239 instead of hello. so you were in a 6154 03:46:10,720 --> 03:46:14,279 situation where you did this 6155 03:46:12,239 --> 03:46:16,239 accidentally and hit enter and then you 6156 03:46:14,279 --> 03:46:18,399 went ahead and typed in all of your code 6157 03:46:16,239 --> 03:46:20,640 like this and then down in your terminal 6158 03:46:18,399 --> 03:46:24,439 window you were trying to do make hello 6159 03:46:20,640 --> 03:46:27,600 enter and this now didn't actually do 6160 03:46:24,439 --> 03:46:29,239 anything like I can't I'm hitting um I'm 6161 03:46:27,600 --> 03:46:31,080 trying to run the command I got 6162 03:46:29,239 --> 03:46:32,840 permission denied as at least one of you 6163 03:46:31,080 --> 03:46:36,439 did now why is that well let's just do a 6164 03:46:32,840 --> 03:46:38,479 quick check if I do LS I see now hello 6165 03:46:36,439 --> 03:46:40,040 but hello has no asteris next to it 6166 03:46:38,479 --> 03:46:42,239 which means it's not executable that's 6167 03:46:40,040 --> 03:46:44,399 my code why well notice the top of my 6168 03:46:42,239 --> 03:46:46,680 tab confirms oh I screwed up I didn't 6169 03:46:44,399 --> 03:46:48,920 name my file hello.c which it just has 6170 03:46:46,680 --> 03:46:51,319 to be so what do you do well you could 6171 03:46:48,920 --> 03:46:53,399 very hackish like copy this create a new 6172 03:46:51,319 --> 03:46:55,159 file paste it in or no no no like we 6173 03:46:53,399 --> 03:46:56,840 know how to rename things now here 6174 03:46:55,159 --> 03:47:01,239 because that's one of our options let me 6175 03:46:56,840 --> 03:47:04,479 do this let me do MV for move hello and 6176 03:47:01,239 --> 03:47:06,359 then hello.c and hit enter you'll see 6177 03:47:04,479 --> 03:47:09,239 the tab closes cuz hello no longer 6178 03:47:06,359 --> 03:47:12,640 exists but if I now now type LS you'll 6179 03:47:09,239 --> 03:47:15,159 see ah there is hello.c and if I open 6180 03:47:12,640 --> 03:47:18,040 that file now there's all of my same 6181 03:47:15,159 --> 03:47:21,680 code and now if I do make hello make 6182 03:47:18,040 --> 03:47:24,880 hello now I do get an executable file 6183 03:47:21,680 --> 03:47:26,640 wherein the world is restored so MV is 6184 03:47:24,880 --> 03:47:28,479 just a command not just for renaming but 6185 03:47:26,640 --> 03:47:30,479 it also turns out eventually for moving 6186 03:47:28,479 --> 03:47:32,520 files as well you can also create 6187 03:47:30,479 --> 03:47:35,680 directories or folders so for instance 6188 03:47:32,520 --> 03:47:37,359 if I go into vs code again and suppose I 6189 03:47:35,680 --> 03:47:39,720 hover over here and click not on the 6190 03:47:37,359 --> 03:47:41,520 plus file icon but plus folder I can 6191 03:47:39,720 --> 03:47:43,720 create a folder called for instance like 6192 03:47:41,520 --> 03:47:45,720 pset one for problem set one in the 6193 03:47:43,720 --> 03:47:47,640 class and you'll see now that it's empty 6194 03:47:45,720 --> 03:47:50,080 CU all of my other files are in the 6195 03:47:47,640 --> 03:47:52,640 default folder of my account but I could 6196 03:47:50,080 --> 03:47:55,840 also go in there like this and I could 6197 03:47:52,640 --> 03:47:58,399 click on file and now I can create a new 6198 03:47:55,840 --> 03:48:00,279 file called like mario.c which is uh one 6199 03:47:58,399 --> 03:48:02,239 of the first problems for instance but 6200 03:48:00,279 --> 03:48:05,600 you'll notice now that 6201 03:48:02,239 --> 03:48:08,880 mario.c is inside of the pet one folder 6202 03:48:05,600 --> 03:48:10,040 so if I zoom out and I type LS at my 6203 03:48:08,880 --> 03:48:13,880 terminal window 6204 03:48:10,040 --> 03:48:16,040 I won't see mario. C anywhere but I do 6205 03:48:13,880 --> 03:48:17,800 see a pet one folder and it's in light 6206 03:48:16,040 --> 03:48:19,239 blue followed by a slash which you don't 6207 03:48:17,800 --> 03:48:21,600 have to type it just indicates that's a 6208 03:48:19,239 --> 03:48:24,359 folder now I can visually at top left 6209 03:48:21,600 --> 03:48:26,120 obviously see pet one contains mario.c 6210 03:48:24,359 --> 03:48:29,239 but if I try to do something like make 6211 03:48:26,120 --> 03:48:31,279 Mario here no rule to make Target Mario 6212 03:48:29,239 --> 03:48:32,479 like it just doesn't seem to exist and 6213 03:48:31,279 --> 03:48:34,760 that's because you're in the wrong 6214 03:48:32,479 --> 03:48:35,880 directory so in a command line interface 6215 03:48:34,760 --> 03:48:38,239 it's not quite as simple as just 6216 03:48:35,880 --> 03:48:41,199 clicking on a folder and voila it opens 6217 03:48:38,239 --> 03:48:43,840 you have to change into the directory or 6218 03:48:41,199 --> 03:48:45,920 folder and CD is going to be the command 6219 03:48:43,840 --> 03:48:48,840 there so if I want to actually change 6220 03:48:45,920 --> 03:48:51,720 into that directory I can do CD space 6221 03:48:48,840 --> 03:48:53,560 pet one enter and now you'll see my 6222 03:48:51,720 --> 03:48:55,399 prompt changes and this is just a common 6223 03:48:53,560 --> 03:48:57,359 convention but it's not the only one out 6224 03:48:55,399 --> 03:48:58,600 there now I still have a dollar sign 6225 03:48:57,359 --> 03:49:01,399 which indicates where I can type 6226 03:48:58,600 --> 03:49:03,600 commands but before it I see a reminder 6227 03:49:01,399 --> 03:49:05,319 constantly what folder I'm in and we put 6228 03:49:03,600 --> 03:49:07,640 that there deliberately like a lot of 6229 03:49:05,319 --> 03:49:09,520 Linux users do just to remind themselves 6230 03:49:07,640 --> 03:49:11,159 where they are cuz unlike Mac OS where 6231 03:49:09,520 --> 03:49:13,080 you or Windows where you have a nice big 6232 03:49:11,159 --> 03:49:14,399 window telling you where you are at the 6233 03:49:13,080 --> 03:49:17,000 command line you kind of need to be 6234 03:49:14,399 --> 03:49:20,199 reminded textually but now if I type LS 6235 03:49:17,000 --> 03:49:23,920 and hit enter what should I 6236 03:49:20,199 --> 03:49:26,239 see yeah mario.c and now if I want to 6237 03:49:23,920 --> 03:49:28,239 open it uh if I want to uh actually 6238 03:49:26,239 --> 03:49:29,960 compile it I can run make Mario in this 6239 03:49:28,239 --> 03:49:32,199 directory once I actually type out all 6240 03:49:29,960 --> 03:49:34,279 the code rest assured that in problem 6241 03:49:32,199 --> 03:49:35,800 sets in Labs we almost always certainly 6242 03:49:34,279 --> 03:49:38,080 in the first weeks of the class give you 6243 03:49:35,800 --> 03:49:39,640 exactly the commands to type odds are 6244 03:49:38,080 --> 03:49:41,159 because it's new to many of you view you 6245 03:49:39,640 --> 03:49:43,359 will accidentally type the wrong 6246 03:49:41,159 --> 03:49:45,040 commands no big deal just remember that 6247 03:49:43,359 --> 03:49:46,479 you have different ways to solve these 6248 03:49:45,040 --> 03:49:47,840 problems you've got like the graphical 6249 03:49:46,479 --> 03:49:50,080 file explorer which should feel a little 6250 03:49:47,840 --> 03:49:52,319 more familiar but in time you'll start 6251 03:49:50,080 --> 03:49:54,560 to know and honestly probably prefer 6252 03:49:52,319 --> 03:49:58,279 commands like these so CD for change 6253 03:49:54,560 --> 03:50:01,239 directory CP for copy a file uh LS for 6254 03:49:58,279 --> 03:50:03,319 list MK dirt to make a directory create 6255 03:50:01,239 --> 03:50:06,239 a new folder at the command line instead 6256 03:50:03,319 --> 03:50:08,239 of with the button MV for move or rename 6257 03:50:06,239 --> 03:50:11,399 RM 6258 03:50:08,239 --> 03:50:13,560 for remove so be careful with that one 6259 03:50:11,399 --> 03:50:15,520 RM dur remove directory and there's 6260 03:50:13,560 --> 03:50:17,359 dozens hundreds of other commands you 6261 03:50:15,520 --> 03:50:19,399 won't need many of them but we'll start 6262 03:50:17,359 --> 03:50:21,359 to scratch the surface all the more over 6263 03:50:19,399 --> 03:50:23,520 time but ultimately this command line 6264 03:50:21,359 --> 03:50:25,520 interface is going to be a more powerful 6265 03:50:23,520 --> 03:50:26,960 mechanism a more capable mechanism and 6266 03:50:25,520 --> 03:50:28,960 ultimately a more efficient mechanism 6267 03:50:26,960 --> 03:50:30,560 for writing code running commands uh 6268 03:50:28,960 --> 03:50:32,520 solving problems analyzing data more 6269 03:50:30,560 --> 03:50:35,199 generally even though no there's going 6270 03:50:32,520 --> 03:50:37,600 to be some uh some Growing Pains early 6271 03:50:35,199 --> 03:50:40,120 on just because it's probably so new for 6272 03:50:37,600 --> 03:50:41,880 many of you so with that said we have 6273 03:50:40,120 --> 03:50:43,600 some problems still to solve but we 6274 03:50:41,880 --> 03:50:45,359 promised cookies today so let's go ahead 6275 03:50:43,600 --> 03:50:47,399 and take a 10-minute break cookies are 6276 03:50:45,359 --> 03:50:48,760 now served in the transcept and we'll be 6277 03:50:47,399 --> 03:50:52,000 back here in 6278 03:50:48,760 --> 03:50:54,680 10 all right we are back and up until 6279 03:50:52,000 --> 03:50:56,080 now each of the code examples in C we've 6280 03:50:54,680 --> 03:50:57,960 done if been kind of designed to show 6281 03:50:56,080 --> 03:50:59,960 one specific topic but we thought we'd 6282 03:50:57,960 --> 03:51:01,800 try to take a step back and solve a more 6283 03:50:59,960 --> 03:51:04,239 General problem and give you a sense of 6284 03:51:01,800 --> 03:51:05,479 when given a problem set for instance or 6285 03:51:04,239 --> 03:51:07,359 just a programming problem more 6286 03:51:05,479 --> 03:51:08,920 generally like where you even begin and 6287 03:51:07,359 --> 03:51:11,120 how you go about approaching it when 6288 03:51:08,920 --> 03:51:13,239 it's not obvious what the point of the 6289 03:51:11,120 --> 03:51:15,439 exercise is so one of my favorite games 6290 03:51:13,239 --> 03:51:17,080 from yester year is this one here Super 6291 03:51:15,439 --> 03:51:18,920 Mario Brothers that has come in so many 6292 03:51:17,080 --> 03:51:21,439 different forms since but in this 6293 03:51:18,920 --> 03:51:23,720 original uh two-dimensional Sid scroller 6294 03:51:21,439 --> 03:51:25,439 game there was a lot of artwork like 6295 03:51:23,720 --> 03:51:27,800 this so for instance up here in the sky 6296 03:51:25,439 --> 03:51:29,760 were four question marks and we'll find 6297 03:51:27,800 --> 03:51:32,000 that in C in a lot of programming 6298 03:51:29,760 --> 03:51:33,720 languages initially it's a lot easier a 6299 03:51:32,000 --> 03:51:35,199 lot more accessible to focus really on 6300 03:51:33,720 --> 03:51:36,560 black and white type interactive 6301 03:51:35,199 --> 03:51:38,080 programs textually as opposed to 6302 03:51:36,560 --> 03:51:40,560 full-fledged graphics and the like but 6303 03:51:38,080 --> 03:51:42,880 more on the more Gra graphical acoustic 6304 03:51:40,560 --> 03:51:45,199 uh type of programs before long but for 6305 03:51:42,880 --> 03:51:47,960 now let me go over and propose that we 6306 03:51:45,199 --> 03:51:49,640 try to just Implement an asy art aski 6307 03:51:47,960 --> 03:51:52,680 again being the code that Maps numbers 6308 03:51:49,640 --> 03:51:54,760 to letters at least for English um into 6309 03:51:52,680 --> 03:51:56,760 a textual version of these four question 6310 03:51:54,760 --> 03:51:58,640 marks in the sky so for this let me go 6311 03:51:56,760 --> 03:52:00,680 over to vs code I'll create my own 6312 03:51:58,640 --> 03:52:02,520 version of mario.c that will be 6313 03:52:00,680 --> 03:52:04,120 different from the one you're challenged 6314 03:52:02,520 --> 03:52:05,520 with in problem set one indeed in 6315 03:52:04,120 --> 03:52:07,560 problem set one you'll be challenged to 6316 03:52:05,520 --> 03:52:10,319 build a little something like this 6317 03:52:07,560 --> 03:52:12,840 albeit with hashtags uh for asky art 6318 03:52:10,319 --> 03:52:14,319 instead of graphics and in mario.c I 6319 03:52:12,840 --> 03:52:16,439 want to just solve this simple problem 6320 03:52:14,319 --> 03:52:18,720 first so it's all involving output so 6321 03:52:16,439 --> 03:52:21,399 I'll do include standard i.h so I can 6322 03:52:18,720 --> 03:52:23,359 use printf I'll do my int main void more 6323 03:52:21,399 --> 03:52:24,920 on why we keep doing that in future 6324 03:52:23,359 --> 03:52:28,880 weeks and I'm just going to do something 6325 03:52:24,920 --> 03:52:31,359 simple initially like 1 2 3 4 back sln 6326 03:52:28,880 --> 03:52:34,120 this is about the simplest way I can 6327 03:52:31,359 --> 03:52:36,520 Implement four question marks in the sky 6328 03:52:34,120 --> 03:52:38,159 like these here using pure text like 6329 03:52:36,520 --> 03:52:41,359 this so let me go ahead and do make 6330 03:52:38,159 --> 03:52:43,199 Mario / Mario and voila we have those 6331 03:52:41,359 --> 03:52:45,159 four question marks but we've seen of 6332 03:52:43,199 --> 03:52:47,439 course that there are better ways to do 6333 03:52:45,159 --> 03:52:50,120 this and if you wanted to generalize 6334 03:52:47,439 --> 03:52:51,960 this to be five question marks six 60 6335 03:52:50,120 --> 03:52:54,159 different question marks you know Loop 6336 03:52:51,960 --> 03:52:57,120 was always the answer for not repeating 6337 03:52:54,159 --> 03:52:58,880 ourselves so maybe I should rewrite this 6338 03:52:57,120 --> 03:53:01,120 a little bit more flexibly and say 6339 03:52:58,880 --> 03:53:04,040 something like this four in I gets zero 6340 03:53:01,120 --> 03:53:06,080 I less than four I plus plus and then 6341 03:53:04,040 --> 03:53:10,239 inside of the for Loop now I can just do 6342 03:53:06,080 --> 03:53:12,880 a single question mark But I don't think 6343 03:53:10,239 --> 03:53:15,120 what I've just done is correct anyone 6344 03:53:12,880 --> 03:53:18,439 spot the aesthetic bug 6345 03:53:15,120 --> 03:53:22,750 already yeah why is this why is this 6346 03:53:18,439 --> 03:53:24,560 wrong if I want to print the same thing 6347 03:53:22,750 --> 03:53:26,479 [Music] 6348 03:53:24,560 --> 03:53:28,399 yeah yeah so I don't think I want to 6349 03:53:26,479 --> 03:53:29,960 backs slash in after every question mark 6350 03:53:28,399 --> 03:53:32,279 because the goal is again this like row 6351 03:53:29,960 --> 03:53:34,279 of question marks in the sky so if I now 6352 03:53:32,279 --> 03:53:37,479 recompile this make 6353 03:53:34,279 --> 03:53:39,640 Mario Mario okay it's almost there but 6354 03:53:37,479 --> 03:53:42,159 now I have that regression to where the 6355 03:53:39,640 --> 03:53:43,640 dollar sign's not on its own line so I 6356 03:53:42,159 --> 03:53:45,720 think I need a new line but I don't 6357 03:53:43,640 --> 03:53:48,840 think I want it here cuz that was not 6358 03:53:45,720 --> 03:53:51,399 going to end well where do I want it 6359 03:53:48,840 --> 03:53:54,159 instead any Instinct 6360 03:53:51,399 --> 03:53:56,319 yeah yeah so outside the for Loop so 6361 03:53:54,159 --> 03:53:58,279 indeed I can just go below line eight 6362 03:53:56,319 --> 03:54:00,479 and above line nine creating a new one 6363 03:53:58,279 --> 03:54:02,000 and now there's it's totally fine to 6364 03:54:00,479 --> 03:54:03,520 just print a new line like that you 6365 03:54:02,000 --> 03:54:05,479 don't have to print anything else with 6366 03:54:03,520 --> 03:54:08,040 it it's indeed a character unto itself 6367 03:54:05,479 --> 03:54:10,159 so let's do make Mario one last time/ 6368 03:54:08,040 --> 03:54:11,960 Mario okay so now we're back in business 6369 03:54:10,159 --> 03:54:14,399 there well what if we wanted to do some 6370 03:54:11,960 --> 03:54:15,920 other scene from Mario uh such as this 6371 03:54:14,399 --> 03:54:18,159 one here where there's a lot of vertical 6372 03:54:15,920 --> 03:54:20,120 obstacles like the These Bricks here if 6373 03:54:18,159 --> 03:54:22,120 I wanted to print out now a column of 6374 03:54:20,120 --> 03:54:24,760 three bricks and I'll use hashtags for 6375 03:54:22,120 --> 03:54:27,479 these instead of anything graphical well 6376 03:54:24,760 --> 03:54:29,800 I think we're almost there right like I 6377 03:54:27,479 --> 03:54:32,279 think I can now it's almost maybe a 6378 03:54:29,800 --> 03:54:33,399 little easier I can go back here change 6379 03:54:32,279 --> 03:54:35,120 the question mark to something that 6380 03:54:33,399 --> 03:54:37,319 looks more like a brick like this hash 6381 03:54:35,120 --> 03:54:39,120 symbol and I think now I do want the new 6382 03:54:37,319 --> 03:54:42,560 line character because when I now do 6383 03:54:39,120 --> 03:54:44,840 make Mario Mario okay there's my wall of 6384 03:54:42,560 --> 03:54:46,560 four oh but wait I didn't want four I 6385 03:54:44,840 --> 03:54:48,279 wanted to be consistent just with this 6386 03:54:46,560 --> 03:54:50,840 particular scene here so I just want 6387 03:54:48,279 --> 03:54:52,960 three so I can still change it in one 6388 03:54:50,840 --> 03:54:55,120 place and here again is that Paradigm 6389 03:54:52,960 --> 03:54:57,040 even whether you're using four or three 6390 03:54:55,120 --> 03:54:59,720 if you get into the habit of starting 6391 03:54:57,040 --> 03:55:01,920 counting from zero you go on up to but 6392 03:54:59,720 --> 03:55:04,560 not through the value you want to count 6393 03:55:01,920 --> 03:55:06,600 up to so that's why I'm using less than 6394 03:55:04,560 --> 03:55:08,239 instead of less than or equals to there 6395 03:55:06,600 --> 03:55:09,439 so this would be the common Paradigm 6396 03:55:08,239 --> 03:55:12,120 though you could certainly count it like 6397 03:55:09,439 --> 03:55:14,239 we saw earlier in different ways but 6398 03:55:12,120 --> 03:55:15,279 what if things escalate one level 6399 03:55:14,239 --> 03:55:17,080 further and when you're in the 6400 03:55:15,279 --> 03:55:18,159 underground version of Super Mario 6401 03:55:17,080 --> 03:55:20,359 Brothers there's a lot of these 6402 03:55:18,159 --> 03:55:22,359 underground obstructions including like 6403 03:55:20,359 --> 03:55:24,080 grids of bricks like this and let me 6404 03:55:22,359 --> 03:55:27,359 conjecture that if you slice this up 6405 03:55:24,080 --> 03:55:29,680 it's roughly a 3X3 grid of bricks that 6406 03:55:27,359 --> 03:55:32,239 all interlock uh prettily to give us 6407 03:55:29,680 --> 03:55:35,520 just one big large brick like this so if 6408 03:55:32,239 --> 03:55:36,840 I want to print out a 3X3 grid now 6409 03:55:35,520 --> 03:55:38,840 things are getting a little more 6410 03:55:36,840 --> 03:55:41,479 interesting because up until now I 6411 03:55:38,840 --> 03:55:43,840 printed either one row horizontally or 6412 03:55:41,479 --> 03:55:45,439 one column vertically but we haven't 6413 03:55:43,840 --> 03:55:47,120 really seen any code where I'm sort of 6414 03:55:45,439 --> 03:55:49,640 printing or living in two different 6415 03:55:47,120 --> 03:55:52,319 dimensions like the game would imply but 6416 03:55:49,640 --> 03:55:54,399 let me propose that we could do this let 6417 03:55:52,319 --> 03:55:57,600 me go ahead and say all right suppose I 6418 03:55:54,399 --> 03:56:01,120 want to print a 3X3 grid of bricks it's 6419 03:55:57,600 --> 03:56:04,560 really that I want to print what three 6420 03:56:01,120 --> 03:56:06,439 rows of bricks like a grid is three rows 6421 03:56:04,560 --> 03:56:08,319 so if I take the highle idea and reduce 6422 03:56:06,439 --> 03:56:09,800 it to something a little simpler how do 6423 03:56:08,319 --> 03:56:11,800 I do that well let me get rid of the 6424 03:56:09,800 --> 03:56:13,840 print for a moment as I did and let me 6425 03:56:11,800 --> 03:56:15,840 just stipulate that this for Loop even 6426 03:56:13,840 --> 03:56:18,960 though it doesn't do anything useful yet 6427 03:56:15,840 --> 03:56:21,199 will do something how many times just by 6428 03:56:18,960 --> 03:56:22,800 Design all right three times right this 6429 03:56:21,199 --> 03:56:25,040 for Loop is good to go it will do 6430 03:56:22,800 --> 03:56:26,920 something three times by just using I to 6431 03:56:25,040 --> 03:56:30,399 do the counting all right well if I want 6432 03:56:26,920 --> 03:56:32,520 to print out now a row of three bricks 6433 03:56:30,399 --> 03:56:34,439 all on the same line that's pretty 6434 03:56:32,520 --> 03:56:36,680 similar to what we did earlier when I 6435 03:56:34,439 --> 03:56:38,680 just wanted to print out four question 6436 03:56:36,680 --> 03:56:40,319 marks in the sky so we've kind of seen a 6437 03:56:38,680 --> 03:56:42,640 solution there and I dare say we can 6438 03:56:40,319 --> 03:56:45,920 compose one into the other so if I want 6439 03:56:42,640 --> 03:56:49,840 to print out a row of bricks I could 6440 03:56:45,920 --> 03:56:53,000 just do this four in I gets zero I less 6441 03:56:49,840 --> 03:56:55,560 than three I ++ and then inside of this 6442 03:56:53,000 --> 03:57:00,479 inner loop if you will let me print out 6443 03:56:55,560 --> 03:57:02,000 a single brick like this and then I I 6444 03:57:00,479 --> 03:57:03,840 don't like where this is going yet but I 6445 03:57:02,000 --> 03:57:05,080 think I've taken two ideas and I've 6446 03:57:03,840 --> 03:57:07,600 combined them but what might be 6447 03:57:05,080 --> 03:57:09,800 problematic about lines five and seven 6448 03:57:07,600 --> 03:57:13,720 at the moment 6449 03:57:09,800 --> 03:57:13,720 what might be bad here uh yeah and 6450 03:57:13,800 --> 03:57:18,159 back yeah I'm using the same integer ey 6451 03:57:16,439 --> 03:57:19,520 which I feel like could get me into 6452 03:57:18,159 --> 03:57:21,600 trouble right if I'm sort of trying to 6453 03:57:19,520 --> 03:57:23,680 count three things here but then I'm 6454 03:57:21,600 --> 03:57:25,720 hijacking this variable and using it 6455 03:57:23,680 --> 03:57:27,720 inside of the loop like I feel like I 6456 03:57:25,720 --> 03:57:30,080 should avoid this this Collision of 6457 03:57:27,720 --> 03:57:32,520 names and so what's a good alternative 6458 03:57:30,080 --> 03:57:33,880 to I well a programmer if nesting Loops 6459 03:57:32,520 --> 03:57:35,760 in this way would pretty commonly go 6460 03:57:33,880 --> 03:57:38,040 with J you could certainly change this 6461 03:57:35,760 --> 03:57:39,880 to be like rows and columns if you want 6462 03:57:38,040 --> 03:57:42,120 more descriptive variables but I and J 6463 03:57:39,880 --> 03:57:45,000 is pretty canonical so I'm going to go 6464 03:57:42,120 --> 03:57:46,880 ahead and do this j++ instead of i++ 6465 03:57:45,000 --> 03:57:48,680 everywhere and let me try compiling this 6466 03:57:46,880 --> 03:57:50,199 so make Mario 6467 03:57:48,680 --> 03:57:52,600 enter 6468 03:57:50,199 --> 03:57:55,319 Mario okay so a couple of things are 6469 03:57:52,600 --> 03:57:56,960 wrong here this is not a 3X3 grid but if 6470 03:57:55,319 --> 03:57:58,880 you count these things how many did I 6471 03:57:56,960 --> 03:58:01,880 indeed print at 6472 03:57:58,880 --> 03:58:03,560 least can probably just guess logically 6473 03:58:01,880 --> 03:58:05,120 yeah there's nine hashes there 6474 03:58:03,560 --> 03:58:08,159 unfortunately they're all on the same 6475 03:58:05,120 --> 03:58:11,800 line instead of on three different lines 6476 03:58:08,159 --> 03:58:13,199 so where logically can I fix this I'm 6477 03:58:11,800 --> 03:58:16,439 definitely printing all the bricks 6478 03:58:13,199 --> 03:58:16,439 they're just not on the right levels 6479 03:58:18,520 --> 03:58:23,159 yeah yeah so put a new line after the 6480 03:58:21,080 --> 03:58:24,920 first Loop this inner loop if you will 6481 03:58:23,159 --> 03:58:27,080 the nested Loop if you will so let me go 6482 03:58:24,920 --> 03:58:29,439 ahead and print out just a back slash n 6483 03:58:27,080 --> 03:58:30,840 here and what's this doing well I think 6484 03:58:29,439 --> 03:58:32,399 that's going to solve it by just moving 6485 03:58:30,840 --> 03:58:34,600 the cursor to the next line after you've 6486 03:58:32,399 --> 03:58:37,960 done one row so let me go ahead and do 6487 03:58:34,600 --> 03:58:39,920 make Mario enter SL Mario and now we're 6488 03:58:37,960 --> 03:58:41,720 in business so it's a very simplistic 6489 03:58:39,920 --> 03:58:43,800 version of the same graphic but I'm 6490 03:58:41,720 --> 03:58:46,040 leveraging two different ideas now the 6491 03:58:43,800 --> 03:58:48,359 same or the same idea twice rather now 6492 03:58:46,040 --> 03:58:50,960 I'm using one Loop to kind of control my 6493 03:58:48,359 --> 03:58:52,680 cursor going row by row by row but then 6494 03:58:50,960 --> 03:58:55,159 within that Loop I'm doing left to right 6495 03:58:52,680 --> 03:58:57,840 do do dot dot dot with printing out each 6496 03:58:55,159 --> 03:59:00,000 of these individual bricks like this now 6497 03:58:57,840 --> 03:59:01,600 there's a little sloppiness here still 6498 03:59:00,000 --> 03:59:03,640 like if I want this to always be a 6499 03:59:01,600 --> 03:59:05,680 square just because that's what it looks 6500 03:59:03,640 --> 03:59:09,600 like in the game well I could change it 6501 03:59:05,680 --> 03:59:13,479 to be a 4x4 uh Square by doing this or a 6502 03:59:09,600 --> 03:59:15,279 5x5 grid whoops by doing this why is 6503 03:59:13,479 --> 03:59:16,960 this perhaps not the best design to just 6504 03:59:15,279 --> 03:59:19,040 keep changing the numbers when I want to 6505 03:59:16,960 --> 03:59:23,199 change the 6506 03:59:19,040 --> 03:59:23,199 size where could this go AR 6507 03:59:24,600 --> 03:59:27,680 yeah yeah if it's always going to be a 6508 03:59:26,560 --> 03:59:29,319 square and height is going to be the 6509 03:59:27,680 --> 03:59:30,560 same as width I'm just inviting trouble 6510 03:59:29,319 --> 03:59:31,920 here right eventually I'm going to screw 6511 03:59:30,560 --> 03:59:33,000 up I'm going to change one but not the 6512 03:59:31,920 --> 03:59:34,880 other then it's going to come out to be 6513 03:59:33,000 --> 03:59:36,640 a rectangle instead of a proper Square 6514 03:59:34,880 --> 03:59:38,279 so I should probably solve this a little 6515 03:59:36,640 --> 03:59:40,680 differently so let me do that at the top 6516 03:59:38,279 --> 03:59:43,239 of my main function here let me go ahead 6517 03:59:40,680 --> 03:59:45,840 and give myself a variable called maybe 6518 03:59:43,239 --> 03:59:48,000 uh n for the number of bricks I want 6519 03:59:45,840 --> 03:59:50,279 horizontally and vertically and I'll 6520 03:59:48,000 --> 03:59:52,000 just initialize that to three initially 6521 03:59:50,279 --> 03:59:53,880 and instead of putting three here I'll 6522 03:59:52,000 --> 03:59:55,920 literally just use n but I'll do it in 6523 03:59:53,880 --> 03:59:57,359 both places so that now henceforth if I 6524 03:59:55,920 --> 03:59:59,600 ever want to change this and change it 6525 03:59:57,359 --> 04:00:01,279 to four or five or anything else like 6526 03:59:59,600 --> 04:00:02,720 I'm all done like it's better designed 6527 04:00:01,279 --> 04:00:04,880 because there's a lower probability of 6528 04:00:02,720 --> 04:00:06,880 mistakes but I could technically SK 6529 04:00:04,880 --> 04:00:08,840 still screw up somehow I could 6530 04:00:06,880 --> 04:00:12,199 technically accidentally write line of 6531 04:00:08,840 --> 04:00:13,920 code like n++ or you know I could just 6532 04:00:12,199 --> 04:00:15,640 change the value of that variable even 6533 04:00:13,920 --> 04:00:16,680 though I don't want it to ever change 6534 04:00:15,640 --> 04:00:18,199 and maybe it's because I'm a bad 6535 04:00:16,680 --> 04:00:19,560 programmer I copy pasted wrong I'm 6536 04:00:18,199 --> 04:00:22,640 working with someone who doesn't know 6537 04:00:19,560 --> 04:00:25,359 what n represents I can defend myself 6538 04:00:22,640 --> 04:00:27,880 and my code against human error like 6539 04:00:25,359 --> 04:00:29,239 that by going up here to line five and 6540 04:00:27,880 --> 04:00:31,359 instead of just declaring a simple 6541 04:00:29,239 --> 04:00:34,080 variable like we did in scratch I can 6542 04:00:31,359 --> 04:00:36,279 further Harden my code so to speak by 6543 04:00:34,080 --> 04:00:38,760 declaring it to be a constant using the 6544 04:00:36,279 --> 04:00:41,120 keyword const now this is just a feature 6545 04:00:38,760 --> 04:00:43,239 of c and some other languages to protect 6546 04:00:41,120 --> 04:00:46,600 you against Yourself by proactively 6547 04:00:43,239 --> 04:00:48,359 saying N is a constant specifically the 6548 04:00:46,600 --> 04:00:50,920 number five or previously the number 6549 04:00:48,359 --> 04:00:52,560 three you cannot accidentally write code 6550 04:00:50,920 --> 04:00:54,880 elsewhere that changes it the computer 6551 04:00:52,560 --> 04:00:56,560 will throw an error and catch that error 6552 04:00:54,880 --> 04:00:59,040 so it's just a way of programming a 6553 04:00:56,560 --> 04:01:00,720 little more defensively um some 6554 04:00:59,040 --> 04:01:02,640 languages have this some languages don't 6555 04:01:00,720 --> 04:01:04,920 but in general it's a good practice it 6556 04:01:02,640 --> 04:01:07,239 makes your code better designed because 6557 04:01:04,920 --> 04:01:09,520 it just is less vulnerable to Mistakes 6558 04:01:07,239 --> 04:01:11,319 by you colleagues or anyone else using 6559 04:01:09,520 --> 04:01:13,520 the code so let me change this back to 6560 04:01:11,319 --> 04:01:16,000 three just to be our default but now I'm 6561 04:01:13,520 --> 04:01:19,120 using n in both places and if I do make 6562 04:01:16,000 --> 04:01:20,439 mario. Mario we're back to where we 6563 04:01:19,120 --> 04:01:22,720 originally started but the code's a 6564 04:01:20,439 --> 04:01:24,880 little more better design and let me 6565 04:01:22,720 --> 04:01:26,720 note this too all this time I've been 6566 04:01:24,880 --> 04:01:28,479 mentioning that uh there's correctness 6567 04:01:26,720 --> 04:01:30,600 is important design is important there 6568 04:01:28,479 --> 04:01:33,120 was also this matter of style I've been 6569 04:01:30,600 --> 04:01:34,279 very deliberately writing pretty code if 6570 04:01:33,120 --> 04:01:35,760 you will not just the syntax 6571 04:01:34,279 --> 04:01:38,239 highlighting which is automatic but 6572 04:01:35,760 --> 04:01:40,479 notice that I keep indenting everything 6573 04:01:38,239 --> 04:01:43,279 nicely anytime I have curly braces like 6574 04:01:40,479 --> 04:01:45,399 on lines 4 and 14 everything is indented 6575 04:01:43,279 --> 04:01:48,800 one level when I have additional curly 6576 04:01:45,399 --> 04:01:51,840 braces on line 7 and 13 everything is 6577 04:01:48,800 --> 04:01:53,920 nicely indented uh as well technically 6578 04:01:51,840 --> 04:01:56,199 speaking the computer does not care 6579 04:01:53,920 --> 04:01:58,399 about that kind of white space so to 6580 04:01:56,199 --> 04:02:00,600 speak and you could really make a mess 6581 04:01:58,399 --> 04:02:02,479 of things like this because you have a 6582 04:02:00,600 --> 04:02:04,199 strange sense of style or just because 6583 04:02:02,479 --> 04:02:07,520 you're being a little sloppy but this 6584 04:02:04,199 --> 04:02:09,279 code is actually still correct if I 6585 04:02:07,520 --> 04:02:13,680 recompile it let me open up my terminal 6586 04:02:09,279 --> 04:02:15,840 window make uh Mario no errors Mario it 6587 04:02:13,680 --> 04:02:18,359 works perfectly fine but you can imagine 6588 04:02:15,840 --> 04:02:20,479 just how annoying this now is to read 6589 04:02:18,359 --> 04:02:22,239 like certainly for a TA U but certainly 6590 04:02:20,479 --> 04:02:23,439 for you the next day certainly for a 6591 04:02:22,239 --> 04:02:25,680 colleague who has to read your code this 6592 04:02:23,439 --> 04:02:27,720 is just bad style like it still works 6593 04:02:25,680 --> 04:02:29,800 and it's welld designed in that like 6594 04:02:27,720 --> 04:02:31,560 you're uh writing code defensively 6595 04:02:29,800 --> 04:02:34,080 you're using a constant but my God the 6596 04:02:31,560 --> 04:02:35,640 style is atrocious now you'll often find 6597 04:02:34,080 --> 04:02:37,479 that there's tools that can help you 6598 04:02:35,640 --> 04:02:39,640 format your code for you in a manner 6599 04:02:37,479 --> 04:02:41,600 consistent with a courses or a company's 6600 04:02:39,640 --> 04:02:43,239 uh style but this is the kind of muscle 6601 04:02:41,600 --> 04:02:45,800 memory you'll want to develop over time 6602 04:02:43,239 --> 04:02:47,760 to take vs code suggestions as it's 6603 04:02:45,800 --> 04:02:49,359 outputting lines of code for you because 6604 04:02:47,760 --> 04:02:52,000 it's trying to format your code in a 6605 04:02:49,359 --> 04:02:53,840 readable way and oh my God if and when 6606 04:02:52,000 --> 04:02:55,960 you do have bugs in your code and things 6607 04:02:53,840 --> 04:02:57,239 aren't even indented properly there's no 6608 04:02:55,960 --> 04:02:59,000 way you the human are going to be able 6609 04:02:57,239 --> 04:03:00,840 to wrap your mind around what's 6610 04:02:59,000 --> 04:03:02,760 happening and where like you're just 6611 04:03:00,840 --> 04:03:04,800 making the problem harder for yourself 6612 04:03:02,760 --> 04:03:08,159 so do get into this habit too of 6613 04:03:04,800 --> 04:03:09,880 manifesting good style as well all right 6614 04:03:08,159 --> 04:03:12,199 well let me propose that we don't only 6615 04:03:09,880 --> 04:03:15,239 want a 3X3 grid we want this to be a 6616 04:03:12,199 --> 04:03:17,560 little more Dynamic so suppose we moved 6617 04:03:15,239 --> 04:03:20,080 away from a constant to just using an 6618 04:03:17,560 --> 04:03:22,199 integer called n and let's ask the user 6619 04:03:20,080 --> 04:03:24,319 for the size of this grid as by 6620 04:03:22,199 --> 04:03:27,040 prompting them with get int as we've 6621 04:03:24,319 --> 04:03:28,840 done before and I'll store it in N here 6622 04:03:27,040 --> 04:03:32,199 and then I can go ahead and more 6623 04:03:28,840 --> 04:03:35,680 dynamically run make Mario to compile it 6624 04:03:32,199 --> 04:03:38,880 whoops oh I screwed up 6625 04:03:35,680 --> 04:03:41,960 accidentally what is it in suggesting I 6626 04:03:38,880 --> 04:03:41,960 do albeit 6627 04:03:42,479 --> 04:03:47,000 cryptically yeah I forgot to include the 6628 04:03:44,880 --> 04:03:49,560 cs50 header file up top and that's why 6629 04:03:47,000 --> 04:03:51,760 it doesn't know that get int is in fact 6630 04:03:49,560 --> 04:03:53,479 valid so that's an easy fix U I'm just 6631 04:03:51,760 --> 04:03:55,359 going to go up here and include 6632 04:03:53,479 --> 04:03:57,960 cs50.h now I'm going to clear my 6633 04:03:55,359 --> 04:04:00,479 terminal and rerun make Mario now we're 6634 04:03:57,960 --> 04:04:02,199 good Mario and now notice I'm prompted 6635 04:04:00,479 --> 04:04:04,840 for size so if I type in three it's the 6636 04:04:02,199 --> 04:04:06,600 same as before if I type in 10 it's even 6637 04:04:04,840 --> 04:04:08,840 bigger but it happens all now 6638 04:04:06,600 --> 04:04:10,439 automatically but there are some things 6639 04:04:08,840 --> 04:04:13,000 that we're not detecting for instance 6640 04:04:10,439 --> 04:04:14,600 suppose I type in cat well that's 6641 04:04:13,000 --> 04:04:15,640 handled by the get it function as I 6642 04:04:14,600 --> 04:04:16,960 claimed earlier that's one of the 6643 04:04:15,640 --> 04:04:19,720 features of using a library you don't 6644 04:04:16,960 --> 04:04:21,600 have to deal with erroneous input but we 6645 04:04:19,720 --> 04:04:23,359 only designed a function called get int 6646 04:04:21,600 --> 04:04:25,600 to get you an integer we don't know if 6647 04:04:23,359 --> 04:04:27,680 you want it to be positive negative zero 6648 04:04:25,600 --> 04:04:29,359 or some combination thereof and it's 6649 04:04:27,680 --> 04:04:31,399 kind of weird to allow the user to type 6650 04:04:29,359 --> 04:04:34,760 in like negative one for the size of the 6651 04:04:31,399 --> 04:04:36,319 Grid or you know ne3 for the size of the 6652 04:04:34,760 --> 04:04:38,239 grid and indeed your code does nothing 6653 04:04:36,319 --> 04:04:39,720 so at least it's not crashing but that's 6654 04:04:38,239 --> 04:04:41,560 kind of stupid right like it'd be nice 6655 04:04:39,720 --> 04:04:44,479 to force the user if they want a grid to 6656 04:04:41,560 --> 04:04:47,040 give us a positive value so how could we 6657 04:04:44,479 --> 04:04:49,840 do this well I could go up here and I 6658 04:04:47,040 --> 04:04:52,520 could say something like if n is less 6659 04:04:49,840 --> 04:04:54,960 than one so if it's zero or negative 6660 04:04:52,520 --> 04:04:57,920 which I don't want what could I do well 6661 04:04:54,960 --> 04:05:00,640 I could say well prompt the user again 6662 04:04:57,920 --> 04:05:02,960 for the size and now notice I'm not 6663 04:05:00,640 --> 04:05:04,279 declaring and again because once it 6664 04:05:02,960 --> 04:05:06,840 exists you don't have to mention the 6665 04:05:04,279 --> 04:05:09,000 data type again we said that earlier but 6666 04:05:06,840 --> 04:05:11,040 this is kind of stupid why because now 6667 04:05:09,000 --> 04:05:13,399 when You' given the user a second chance 6668 04:05:11,040 --> 04:05:16,600 okay now maybe I'll do all right if this 6669 04:05:13,399 --> 04:05:19,600 version of n is less than one well let's 6670 04:05:16,600 --> 04:05:20,840 just go and prompt the user a third time 6671 04:05:19,600 --> 04:05:22,399 I mean you can kind of see where this is 6672 04:05:20,840 --> 04:05:24,479 stupidly going like this can't be the 6673 04:05:22,399 --> 04:05:26,080 right solution to keep typing 6674 04:05:24,479 --> 04:05:27,600 recursively the same thing again and 6675 04:05:26,080 --> 04:05:28,760 again like where would it stop you'd 6676 04:05:27,600 --> 04:05:30,560 have to give them a finite number of 6677 04:05:28,760 --> 04:05:33,040 chances or just you know make a mess of 6678 04:05:30,560 --> 04:05:35,239 your code so what would be intuitively a 6679 04:05:33,040 --> 04:05:37,479 better solution 6680 04:05:35,239 --> 04:05:39,760 here yeah so some kind of loop we've 6681 04:05:37,479 --> 04:05:41,840 seen a while loop we've seen a four Loop 6682 04:05:39,760 --> 04:05:44,159 so maybe one of those so let me try this 6683 04:05:41,840 --> 04:05:45,840 let me delete this messiness and just go 6684 04:05:44,159 --> 04:05:49,720 back to the first question and let me do 6685 04:05:45,840 --> 04:05:51,800 this so while n is less than one so 6686 04:05:49,720 --> 04:05:53,680 while the number is not what we want 6687 04:05:51,800 --> 04:05:57,359 let's just prompt the user in a loop 6688 04:05:53,680 --> 04:06:00,680 this time for the size again now here 6689 04:05:57,359 --> 04:06:02,800 too this is better because it's only two 6690 04:06:00,680 --> 04:06:05,239 requests for information but clearly 6691 04:06:02,800 --> 04:06:07,479 line six and N are pretty much identical 6692 04:06:05,239 --> 04:06:10,040 other than the int and if I went in and 6693 04:06:07,479 --> 04:06:11,920 changed the size you know uh if I add 6694 04:06:10,040 --> 04:06:13,040 this if I change the wording here change 6695 04:06:11,920 --> 04:06:14,439 it to a different language like I have 6696 04:06:13,040 --> 04:06:17,680 to change it in two places that's bad 6697 04:06:14,439 --> 04:06:19,479 copy paste bad so what might be better 6698 04:06:17,680 --> 04:06:22,000 well it turns out there's another 6699 04:06:19,479 --> 04:06:24,000 Paradigm in C that you can use that gets 6700 04:06:22,000 --> 04:06:25,880 around this problem this duplication of 6701 04:06:24,000 --> 04:06:28,640 code it would be much nicer if I just 6702 04:06:25,880 --> 04:06:31,159 write this code once and I can do that 6703 04:06:28,640 --> 04:06:33,840 using a third type of loop called a do 6704 04:06:31,159 --> 04:06:35,600 while loop so it turns out in C you can 6705 04:06:33,840 --> 04:06:37,800 do this if you want to get the value of 6706 04:06:35,600 --> 04:06:39,520 a variable like n first just decree 6707 04:06:37,800 --> 04:06:42,199 create the variable without an initial 6708 04:06:39,520 --> 04:06:43,680 value so int n semicolon means it we 6709 04:06:42,199 --> 04:06:45,199 don't know what value it has yes but 6710 04:06:43,680 --> 04:06:47,560 that's okay we're going to add a value 6711 04:06:45,199 --> 04:06:50,199 to it eventually then I'm going to say 6712 04:06:47,560 --> 04:06:52,800 this do literally I'm going to open my 6713 04:06:50,199 --> 04:06:55,920 curly braces and what do I want to do I 6714 04:06:52,800 --> 04:06:59,359 want to assign to n the return value of 6715 04:06:55,920 --> 04:07:01,159 get in prompting the user for size well 6716 04:06:59,359 --> 04:07:05,279 when do you want to do that I want to do 6717 04:07:01,159 --> 04:07:08,199 that while n is less than one and this 6718 04:07:05,279 --> 04:07:11,159 code now achieves the exact same goal 6719 04:07:08,199 --> 04:07:13,640 but by never repeating myself why well 6720 04:07:11,159 --> 04:07:16,319 notice on these lines of code now I'm 6721 04:07:13,640 --> 04:07:18,199 literally saying on line six give me a 6722 04:07:16,319 --> 04:07:19,399 variable called n of type integer it 6723 04:07:18,199 --> 04:07:21,920 doesn't have a value initially but 6724 04:07:19,399 --> 04:07:23,319 that's fine you can do that line seven 6725 04:07:21,920 --> 04:07:25,640 says do the following what do you want 6726 04:07:23,319 --> 04:07:27,520 to do get int prompting the user with 6727 04:07:25,640 --> 04:07:30,960 the word size and just store that value 6728 04:07:27,520 --> 04:07:33,359 in N but because code runs top to bottom 6729 04:07:30,960 --> 04:07:35,760 left to right now it's reasonable on 6730 04:07:33,359 --> 04:07:38,080 line 11 to ask that question okay is the 6731 04:07:35,760 --> 04:07:40,760 current value of n which it definitely 6732 04:07:38,080 --> 04:07:42,399 got on line a less than one and if the 6733 04:07:40,760 --> 04:07:45,159 user didn't cooperate they typed in zero 6734 04:07:42,399 --> 04:07:47,640 or Nega 1 or3 what's going to happen 6735 04:07:45,159 --> 04:07:50,600 it's going to go back up here and repeat 6736 04:07:47,640 --> 04:07:53,439 repeat repeat everything in the do while 6737 04:07:50,600 --> 04:07:55,080 loop so a do while loop in C which is 6738 04:07:53,439 --> 04:07:56,399 not something some other languages have 6739 04:07:55,080 --> 04:07:58,439 like python if you know it does not have 6740 04:07:56,399 --> 04:08:00,760 a do while loop this is perhaps the 6741 04:07:58,439 --> 04:08:02,159 cleanest way to achieve this even though 6742 04:08:00,760 --> 04:08:03,920 it's a little weird that you have to 6743 04:08:02,159 --> 04:08:06,560 declare your variable create your 6744 04:08:03,920 --> 04:08:08,800 variable up top and then check it down 6745 04:08:06,560 --> 04:08:11,080 below but other wise it's similar to a 6746 04:08:08,800 --> 04:08:13,199 while loop it just flips the order in 6747 04:08:11,080 --> 04:08:16,279 which you're asking the question any 6748 04:08:13,199 --> 04:08:17,840 questions on this construct and Doh in 6749 04:08:16,279 --> 04:08:19,560 general is super useful when you want to 6750 04:08:17,840 --> 04:08:23,159 get input from the user and make sure it 6751 04:08:19,560 --> 04:08:24,960 meets certain requirements all right so 6752 04:08:23,159 --> 04:08:27,239 now that we have this building block 6753 04:08:24,960 --> 04:08:28,960 after that interlude how can I go about 6754 04:08:27,239 --> 04:08:30,600 cleaning up this code and then let's 6755 04:08:28,960 --> 04:08:33,439 conclude by taking a look at things that 6756 04:08:30,600 --> 04:08:35,840 our code can't do or can't do very well 6757 04:08:33,439 --> 04:08:38,359 or correctly let me propose that in a 6758 04:08:35,840 --> 04:08:40,279 final version of Mario let me just add 6759 04:08:38,359 --> 04:08:43,399 what are called now some comments so it 6760 04:08:40,279 --> 04:08:45,159 turns out in code in C you can Define 6761 04:08:43,399 --> 04:08:46,680 what are called comments which are just 6762 04:08:45,159 --> 04:08:48,040 notes to self some of you discovered 6763 04:08:46,680 --> 04:08:49,439 these in scratch there's little yellow 6764 04:08:48,040 --> 04:08:52,279 sticky notes you can use to add 6765 04:08:49,439 --> 04:08:53,800 citations or explanations in C there's a 6766 04:08:52,279 --> 04:08:55,640 couple of ways to write comments and in 6767 04:08:53,800 --> 04:08:58,159 general comments are notes for yourself 6768 04:08:55,640 --> 04:09:00,399 for your ta for your colleague as to 6769 04:08:58,159 --> 04:09:01,840 what your code is doing and why or how 6770 04:09:00,399 --> 04:09:03,680 it's a little explanatory note in 6771 04:09:01,840 --> 04:09:06,680 English or whatever your human language 6772 04:09:03,680 --> 04:09:10,640 might be so for instance what I might do 6773 04:09:06,680 --> 04:09:13,040 here in my implementation of this 6774 04:09:10,640 --> 04:09:14,880 version of Mario I might first ask 6775 04:09:13,040 --> 04:09:17,040 myself a question like I I might first 6776 04:09:14,880 --> 04:09:19,560 make a note to S like this on a new line 6777 04:09:17,040 --> 04:09:22,479 above this first block of code uh get 6778 04:09:19,560 --> 04:09:25,560 size of Grid it's just an explanatory 6779 04:09:22,479 --> 04:09:27,720 remark in any tur English that generally 6780 04:09:25,560 --> 04:09:30,279 explains the next six or so lines the 6781 04:09:27,720 --> 04:09:31,600 next chunk or block of code if you will 6782 04:09:30,279 --> 04:09:33,680 it would be a little excessive to 6783 04:09:31,600 --> 04:09:34,800 comment every single line at some point 6784 04:09:33,680 --> 04:09:36,720 the programmer should know what 6785 04:09:34,800 --> 04:09:38,119 individual lines of code do but it's 6786 04:09:36,720 --> 04:09:41,080 nice to be able to kind kind of glance 6787 04:09:38,119 --> 04:09:43,479 at this comment on line six that starts 6788 04:09:41,080 --> 04:09:45,159 with two slashes and it gets gray out 6789 04:09:43,479 --> 04:09:47,319 because of syntax highlighting it's not 6790 04:09:45,159 --> 04:09:48,760 logic it's just a note to self it 6791 04:09:47,319 --> 04:09:50,199 generally gives me a little cheat sheet 6792 04:09:48,760 --> 04:09:52,520 as to what the following lines of code 6793 04:09:50,199 --> 04:09:54,520 should be doing and or why and then down 6794 04:09:52,520 --> 04:09:56,520 here well there's a second block of code 6795 04:09:54,520 --> 04:10:00,840 that's a bunch of lines but together 6796 04:09:56,520 --> 04:10:02,800 this just what prints uh grid of bricks 6797 04:10:00,840 --> 04:10:04,560 and so it's another comment to myself 6798 04:10:02,800 --> 04:10:06,000 that just makes it a little more 6799 04:10:04,560 --> 04:10:07,720 understandable what these 20 some odd 6800 04:10:06,000 --> 04:10:10,840 lines of code are doing by adding some 6801 04:10:07,720 --> 04:10:12,920 English explanations thereof but now 6802 04:10:10,840 --> 04:10:15,960 that I have these you know wouldn't it 6803 04:10:12,920 --> 04:10:17,720 be nice if I could kind of abstract 6804 04:10:15,960 --> 04:10:19,479 these pieces of functionality away this 6805 04:10:17,720 --> 04:10:22,040 getting of the size and this printing of 6806 04:10:19,479 --> 04:10:23,840 the Grid in other words suppose that you 6807 04:10:22,040 --> 04:10:25,319 didn't know where to begin with this 6808 04:10:23,840 --> 04:10:27,119 problem and the problem at hand were 6809 04:10:25,319 --> 04:10:29,760 literally Implement a program that 6810 04:10:27,119 --> 04:10:31,880 prints a grid of bricks of some variable 6811 04:10:29,760 --> 04:10:34,239 size three or four or five or whatever 6812 04:10:31,880 --> 04:10:36,479 the human types in if you have really no 6813 04:10:34,239 --> 04:10:38,680 idea where to start comments are 6814 04:10:36,479 --> 04:10:41,279 actually a good way of getting started 6815 04:10:38,680 --> 04:10:42,880 because comments can be a approximation 6816 04:10:41,279 --> 04:10:44,880 of what we called last week pseudo code 6817 04:10:42,880 --> 04:10:46,680 pseudo code is tur English that kind of 6818 04:10:44,880 --> 04:10:48,840 gets your point across like for the uh 6819 04:10:46,680 --> 04:10:50,800 phone book searching like last time so 6820 04:10:48,840 --> 04:10:53,920 if you didn't really know where to begin 6821 04:10:50,800 --> 04:10:57,119 you could do something like this I could 6822 04:10:53,920 --> 04:10:59,840 for instance just say get size of grid 6823 04:10:57,119 --> 04:11:01,640 as my first step and then print grid of 6824 04:10:59,840 --> 04:11:03,680 bricks as my second step and that's it 6825 04:11:01,640 --> 04:11:05,880 for my program thus far this is now 6826 04:11:03,680 --> 04:11:07,880 implemented in pseudo code I have some 6827 04:11:05,880 --> 04:11:09,760 massive placeholders there like I still 6828 04:11:07,880 --> 04:11:11,880 have work to be done but at least I have 6829 04:11:09,760 --> 04:11:14,920 a highle solution to the problem in 6830 04:11:11,880 --> 04:11:16,960 comments and now I can even go this far 6831 04:11:14,920 --> 04:11:18,880 I could say well let's suppose that 6832 04:11:16,960 --> 04:11:21,800 there's just a function already that 6833 04:11:18,880 --> 04:11:24,000 exists called get size I could do 6834 04:11:21,800 --> 04:11:26,640 something like this I could do Inn 6835 04:11:24,000 --> 04:11:28,040 equals get size and now I just have to 6836 04:11:26,640 --> 04:11:29,800 assume for the moment that some 6837 04:11:28,040 --> 04:11:30,880 abstraction called get size exists it 6838 04:11:29,800 --> 04:11:33,319 doesn't this does not come with the 6839 04:11:30,880 --> 04:11:36,319 cs-50 library but I could invent it I 6840 04:11:33,319 --> 04:11:38,159 bet how else might I proceed well let's 6841 04:11:36,319 --> 04:11:40,560 just assume for the moment that there's 6842 04:11:38,159 --> 04:11:43,840 also a function called print grid that 6843 04:11:40,560 --> 04:11:45,840 just prints that a grid of that size n 6844 04:11:43,840 --> 04:11:47,119 so here too is an abstraction these 6845 04:11:45,840 --> 04:11:49,560 puzzle pieces don't exist these 6846 04:11:47,119 --> 04:11:52,080 functions don't yet exist but in C just 6847 04:11:49,560 --> 04:11:54,600 like in scratch I can create my own 6848 04:11:52,080 --> 04:11:56,520 functions how do I do that well let me 6849 04:11:54,600 --> 04:11:58,080 go down later in the file and by 6850 04:11:56,520 --> 04:11:59,880 convention you generally want to leave 6851 04:11:58,080 --> 04:12:01,199 main at the top of your code why because 6852 04:11:59,880 --> 04:12:02,840 it's the main function and it's just 6853 04:12:01,199 --> 04:12:05,119 where the human eye is going to look to 6854 04:12:02,840 --> 04:12:07,119 see what some file of code does and let 6855 04:12:05,119 --> 04:12:10,080 me do this I want to create a function 6856 04:12:07,119 --> 04:12:12,000 of my own called get size whose purpose 6857 04:12:10,080 --> 04:12:14,399 in life is to get the size that the user 6858 04:12:12,000 --> 04:12:16,399 wants I want this function to return an 6859 04:12:14,399 --> 04:12:19,080 integer and the Syntax for doing that is 6860 04:12:16,399 --> 04:12:21,720 this write similar to a variable the 6861 04:12:19,080 --> 04:12:23,159 data type that this function returns I 6862 04:12:21,720 --> 04:12:25,319 don't need this function to take any 6863 04:12:23,159 --> 04:12:26,920 inputs and so I'm going to use a new 6864 04:12:25,319 --> 04:12:28,600 keyword that we've actually been using 6865 04:12:26,920 --> 04:12:30,680 thus far more on it another time just 6866 04:12:28,600 --> 04:12:32,399 called void which just means this get 6867 04:12:30,680 --> 04:12:35,040 size function does not take any inputs 6868 04:12:32,399 --> 04:12:36,520 it does have an output it outputs an INT 6869 04:12:35,040 --> 04:12:38,560 and this is just a weird order in which 6870 04:12:36,520 --> 04:12:40,000 you write it you write the output format 6871 04:12:38,560 --> 04:12:42,880 the name of the function and then the 6872 04:12:40,000 --> 04:12:44,640 inputs if any inside of parentheses and 6873 04:12:42,880 --> 04:12:46,319 now I can Implement get size but I've 6874 04:12:44,640 --> 04:12:48,000 already implemented get size or at least 6875 04:12:46,319 --> 04:12:49,960 now at this point in the story I at 6876 04:12:48,000 --> 04:12:51,640 least know concretely what to do and I 6877 04:12:49,960 --> 04:12:53,600 could figure out eventually with some 6878 04:12:51,640 --> 04:12:55,359 trial and error perhaps all right if I 6879 04:12:53,600 --> 04:12:57,840 declare a variable and I do the 6880 04:12:55,359 --> 04:13:00,880 following n equals get in prompting the 6881 04:12:57,840 --> 04:13:04,080 user for size and I keep doing that 6882 04:13:00,880 --> 04:13:06,359 while n is less than one once that block 6883 04:13:04,080 --> 04:13:09,640 of code is done here is a new keyword in 6884 04:13:06,359 --> 04:13:11,560 C where you can return that value n so I 6885 04:13:09,640 --> 04:13:14,560 keep referring to these values that some 6886 04:13:11,560 --> 04:13:16,600 functions return as return values in C 6887 04:13:14,560 --> 04:13:19,399 there's literally a keyword called 6888 04:13:16,600 --> 04:13:22,080 return that will hand back to any 6889 04:13:19,399 --> 04:13:25,080 function that uses that function the 6890 04:13:22,080 --> 04:13:28,439 value in question so in a nutshell 6891 04:13:25,080 --> 04:13:30,880 between lines 15 and 21 now here is some 6892 04:13:28,439 --> 04:13:33,159 code identical to our solution earlier 6893 04:13:30,880 --> 04:13:35,319 that gets a value n from the user that 6894 04:13:33,159 --> 04:13:38,000 is positive it's one or two or higher 6895 04:13:35,319 --> 04:13:39,800 it's not zero or it's not less than one 6896 04:13:38,000 --> 04:13:43,479 and as soon as we've got that value we 6897 04:13:39,800 --> 04:13:45,119 hand it back as a return value notice 6898 04:13:43,479 --> 04:13:47,399 how I'm using this function on line 6899 04:13:45,119 --> 04:13:49,399 7even just like with get int just like 6900 04:13:47,399 --> 04:13:51,359 with get string I'm calling the function 6901 04:13:49,399 --> 04:13:52,560 nothing in the parenthesis in this case 6902 04:13:51,359 --> 04:13:54,520 but then I'm using the assignment 6903 04:13:52,560 --> 04:13:58,279 operator to copy whatever its return 6904 04:13:54,520 --> 04:14:00,359 value is into my variable n and so now I 6905 04:13:58,279 --> 04:14:03,080 have a function that didn't used to 6906 04:14:00,359 --> 04:14:06,199 exist called get size that gets me a 6907 04:14:03,080 --> 04:14:08,560 positive integer no matter what and now 6908 04:14:06,199 --> 04:14:10,720 for the grid how do I do this how do I 6909 04:14:08,560 --> 04:14:12,359 invent a function called print grid that 6910 04:14:10,720 --> 04:14:14,800 takes a single argument a number and 6911 04:14:12,359 --> 04:14:17,000 prints a grid of that size well let's go 6912 04:14:14,800 --> 04:14:19,319 down here I'm going to write the name of 6913 04:14:17,000 --> 04:14:21,119 this function print grid this function 6914 04:14:19,319 --> 04:14:22,760 just needs to print it has a side effect 6915 04:14:21,119 --> 04:14:25,239 as we keep saying so I'm just going to 6916 04:14:22,760 --> 04:14:27,439 say it has no return value it's just 6917 04:14:25,239 --> 04:14:29,560 void it doesn't have an output per se 6918 04:14:27,439 --> 04:14:31,800 it's just an aesthetic side effect but 6919 04:14:29,560 --> 04:14:34,520 it does take an an argument an argument 6920 04:14:31,800 --> 04:14:36,800 is an input and the Syntax for this in C 6921 04:14:34,520 --> 04:14:37,600 is to name the type of the input it 6922 04:14:36,800 --> 04:14:39,359 takes 6923 04:14:37,600 --> 04:14:41,159 and the name of the variable and I could 6924 04:14:39,359 --> 04:14:43,520 call this anything I want I'll call it 6925 04:14:41,159 --> 04:14:44,960 size I could call it n and it's okay to 6926 04:14:43,520 --> 04:14:46,680 use the same variable in different 6927 04:14:44,960 --> 04:14:49,159 functions but I'll call it size just to 6928 04:14:46,680 --> 04:14:51,119 be distinct and then in this function 6929 04:14:49,159 --> 04:14:54,080 I'm just going to copy from memory the 6930 04:14:51,119 --> 04:14:58,640 same code as before for in I get zero I 6931 04:14:54,080 --> 04:15:03,159 less than size instead of three I ++ 6932 04:14:58,640 --> 04:15:06,760 inside of this four uh int J gets zero J 6933 04:15:03,159 --> 04:15:09,399 is less than size j++ and inside of that 6934 04:15:06,760 --> 04:15:13,080 print print out with print f a single 6935 04:15:09,399 --> 04:15:16,040 hash print out after that Loop a single 6936 04:15:13,080 --> 04:15:18,319 new line and that's it now I did this 6937 04:15:16,040 --> 04:15:20,520 fast admittedly but it's the same code 6938 04:15:18,319 --> 04:15:22,399 that I wrote earlier but now just like I 6939 04:15:20,520 --> 04:15:23,960 did with scratch let me just arbitrarily 6940 04:15:22,399 --> 04:15:26,479 hit enter a bunch of times to like move 6941 04:15:23,960 --> 04:15:28,399 the code out of sight out of mind now I 6942 04:15:26,479 --> 04:15:30,399 have abstractions I have puzzle pieces 6943 04:15:28,399 --> 04:15:32,560 that now exist called get size and print 6944 04:15:30,399 --> 04:15:35,479 grid Syntax for which takes some getting 6945 04:15:32,560 --> 04:15:38,359 used to but they now just exist except I 6946 04:15:35,479 --> 04:15:41,560 do need to do one thing because C is a 6947 04:15:38,359 --> 04:15:44,159 little naive if I try to do make Mario 6948 04:15:41,560 --> 04:15:46,199 now and hit enter implicit Declaration 6949 04:15:44,159 --> 04:15:48,279 of function get size is invalid and 6950 04:15:46,199 --> 04:15:50,560 we've seen that before when I hadn't 6951 04:15:48,279 --> 04:15:53,439 included a file right when I hadn't 6952 04:15:50,560 --> 04:15:54,640 included cs50 Library get in didn't work 6953 04:15:53,439 --> 04:15:56,040 but that's not the issue here because 6954 04:15:54,640 --> 04:15:58,600 this is not from a library I just 6955 04:15:56,040 --> 04:16:00,520 invented this C kind of takes you 6956 04:15:58,600 --> 04:16:02,399 literally and if you define these 6957 04:16:00,520 --> 04:16:06,040 functions at the bottom of your file 6958 04:16:02,399 --> 04:16:08,040 they don't exist on line seven or 10 so 6959 04:16:06,040 --> 04:16:09,479 I could do this I could all right fine 6960 04:16:08,040 --> 04:16:12,080 well let me just kind of highlight all 6961 04:16:09,479 --> 04:16:13,840 of this cut with to my clipboard and 6962 04:16:12,080 --> 04:16:15,199 paste it up here this would solve the 6963 04:16:13,840 --> 04:16:17,159 problem I could just move all of those 6964 04:16:15,199 --> 04:16:18,439 functions at the top of my file that's 6965 04:16:17,159 --> 04:16:19,840 kind of annoying because now main is 6966 04:16:18,439 --> 04:16:21,080 like the bottom of the file you're not 6967 04:16:19,840 --> 04:16:23,319 it's going to take longer to find it 6968 04:16:21,080 --> 04:16:24,840 it's just that's not a clean solution so 6969 04:16:23,319 --> 04:16:27,840 let me put it back where it was at the 6970 04:16:24,840 --> 04:16:30,199 bottom and let me do this this is the 6971 04:16:27,840 --> 04:16:31,880 only time in cs50 and really in C 6972 04:16:30,199 --> 04:16:34,560 programming where copy paste is 6973 04:16:31,880 --> 04:16:37,040 reasonable if you copy and paste the 6974 04:16:34,560 --> 04:16:39,800 first line of code from each function 6975 04:16:37,040 --> 04:16:42,800 and then end it with a semicolon you can 6976 04:16:39,800 --> 04:16:44,880 tease the compiler by giving it just 6977 04:16:42,800 --> 04:16:46,359 enough of a hint at the top of the file 6978 04:16:44,880 --> 04:16:48,680 that okay these functions don't exist 6979 04:16:46,359 --> 04:16:52,119 till down later but here's a hint that 6980 04:16:48,680 --> 04:16:54,600 they will exist this is how you can uh 6981 04:16:52,119 --> 04:16:56,520 convince the compiler to trust you so 6982 04:16:54,600 --> 04:16:59,600 those other functions can still be lower 6983 04:16:56,520 --> 04:17:02,920 in the file below main but now when I do 6984 04:16:59,600 --> 04:17:05,080 make Mario oh damn it oh I said print 6985 04:17:02,920 --> 04:17:07,680 instead of print F that's my 6986 04:17:05,080 --> 04:17:09,880 bad print f 6987 04:17:07,680 --> 04:17:13,479 so if I do make 6988 04:17:09,880 --> 04:17:15,040 Mario Mario now I can type in three and 6989 04:17:13,479 --> 04:17:16,960 we're back in business now this was a 6990 04:17:15,040 --> 04:17:19,560 very heavy-handed way and long way to 6991 04:17:16,960 --> 04:17:21,119 get to a much more complicated solution 6992 04:17:19,560 --> 04:17:23,239 but this solution in some sense is 6993 04:17:21,119 --> 04:17:24,960 better designed why because now 6994 04:17:23,239 --> 04:17:27,199 especially without the comments I mean 6995 04:17:24,960 --> 04:17:29,159 look how short my code is my main 6996 04:17:27,199 --> 04:17:30,960 function is literally two lines of code 6997 04:17:29,159 --> 04:17:33,119 why well I kind of factored out the 6998 04:17:30,960 --> 04:17:34,479 juicy stuff into its own functions and 6999 04:17:33,119 --> 04:17:36,279 now especially if I'm working with 7000 04:17:34,479 --> 04:17:38,159 colleagues or others you could imagine 7001 04:17:36,279 --> 04:17:39,760 splitting up up large programs into 7002 04:17:38,159 --> 04:17:41,640 smaller parts having different people 7003 04:17:39,760 --> 04:17:43,680 Implement different parts so long as you 7004 04:17:41,640 --> 04:17:47,040 all agree in advance on what those 7005 04:17:43,680 --> 04:17:48,439 inputs and those outputs actually are 7006 04:17:47,040 --> 04:17:51,319 all right so let's now consider what 7007 04:17:48,439 --> 04:17:52,640 computers can do well and not so well C 7008 04:17:51,319 --> 04:17:54,359 indeed supports a whole bunch of 7009 04:17:52,640 --> 04:17:55,960 operators mathematically via which we 7010 04:17:54,359 --> 04:17:57,520 can do addition and subtraction 7011 04:17:55,960 --> 04:17:59,119 multiplication division and even 7012 04:17:57,520 --> 04:18:01,239 calculate the remainder when you divide 7013 04:17:59,119 --> 04:18:02,800 one number by another in fact why don't 7014 04:18:01,239 --> 04:18:04,960 we go ahead and use these in a very 7015 04:18:02,800 --> 04:18:06,920 simple program and make our very own 7016 04:18:04,960 --> 04:18:09,000 calculator so let me go over here here 7017 04:18:06,920 --> 04:18:12,279 to vs code let me go ahead and create a 7018 04:18:09,000 --> 04:18:13,720 new file called calculator. C and in 7019 04:18:12,279 --> 04:18:16,080 this file let's go ahead and first 7020 04:18:13,720 --> 04:18:17,000 include a couple of now familiar header 7021 04:18:16,080 --> 04:18:20,800 files 7022 04:18:17,000 --> 04:18:23,080 cs50.h as well as standard i.h let's go 7023 04:18:20,800 --> 04:18:25,560 ahead then and declare main with int 7024 04:18:23,080 --> 04:18:26,880 main void and then inside of main let's 7025 04:18:25,560 --> 04:18:29,479 do something relatively simple let's 7026 04:18:26,880 --> 04:18:31,680 declare an INT and call it X and set it 7027 04:18:29,479 --> 04:18:33,880 equal to whatever the return value is of 7028 04:18:31,680 --> 04:18:36,199 get int prompting the user for a value 7029 04:18:33,880 --> 04:18:38,720 for x let's then give ourselves a second 7030 04:18:36,199 --> 04:18:40,760 variable we'll call it say y set that 7031 04:18:38,720 --> 04:18:42,880 equal to the return value of another 7032 04:18:40,760 --> 04:18:45,199 call to get int prompting the user this 7033 04:18:42,880 --> 04:18:47,119 time for that value Y and then let's 7034 04:18:45,199 --> 04:18:50,040 very simply go ahead at the very end and 7035 04:18:47,119 --> 04:18:52,199 just print out say the sum of X Plus y a 7036 04:18:50,040 --> 04:18:54,720 super simple calculator so I'll use 7037 04:18:52,199 --> 04:18:57,560 print F quote unquote percent I for 7038 04:18:54,720 --> 04:18:59,520 integer back sln to give me the new line 7039 04:18:57,560 --> 04:19:01,720 then I'm going to go ahead and do x + y 7040 04:18:59,520 --> 04:19:03,960 to indeed print out the sum let me go 7041 04:19:01,720 --> 04:19:06,720 down to my terminal window now let me do 7042 04:19:03,960 --> 04:19:08,840 make calculator in order to compile the 7043 04:19:06,720 --> 04:19:11,239 C no error messages so that's good let 7044 04:19:08,840 --> 04:19:14,520 me do/ calulator and let's do something 7045 04:19:11,239 --> 04:19:17,920 like 2 + 2 which of course should equal 7046 04:19:14,520 --> 04:19:19,479 4 and it does but it turns out that 7047 04:19:17,920 --> 04:19:21,399 sometimes there are going to be 7048 04:19:19,479 --> 04:19:23,080 limitations that we bump up against and 7049 04:19:21,399 --> 04:19:24,760 let me get a little more ambitious here 7050 04:19:23,080 --> 04:19:26,600 let me clear my terminal window and let 7051 04:19:24,760 --> 04:19:29,800 me go ahead and rerun calculator again 7052 04:19:26,600 --> 04:19:29,800 and this time let's type in 7053 04:19:30,159 --> 04:19:35,119 O2 billion for x and let's type in the 7054 04:19:33,080 --> 04:19:36,600 same for y and of course now the answer 7055 04:19:35,119 --> 04:19:40,560 of 2 billion plus 2 billion should have 7056 04:19:36,600 --> 04:19:42,560 of course be 4 billion and yet it's not 7057 04:19:40,560 --> 04:19:44,439 so curiously we see of all things a 7058 04:19:42,560 --> 04:19:46,640 negative number here which suggests that 7059 04:19:44,439 --> 04:19:49,359 somehow the plus operator doesn't quite 7060 04:19:46,640 --> 04:19:51,479 work as well as we might like now why 7061 04:19:49,359 --> 04:19:52,960 might this actually be well it turns out 7062 04:19:51,479 --> 04:19:55,119 that inside of your computer is of 7063 04:19:52,960 --> 04:19:56,520 course memory or Ram random access 7064 04:19:55,119 --> 04:19:57,880 memory and depending on the size of your 7065 04:19:56,520 --> 04:19:59,479 computer and the type of computer it 7066 04:19:57,880 --> 04:20:01,159 might very well look a little something 7067 04:19:59,479 --> 04:20:02,760 like this a little circuit board with 7068 04:20:01,159 --> 04:20:04,840 these black little modules on it that 7069 04:20:02,760 --> 04:20:07,000 actually contain all of the btes of your 7070 04:20:04,840 --> 04:20:09,479 computer's memory unfortunately you and 7071 04:20:07,000 --> 04:20:11,600 I only have a finite amount of this 7072 04:20:09,479 --> 04:20:13,239 memory inside of our computers which 7073 04:20:11,600 --> 04:20:14,760 means no matter how high we want to 7074 04:20:13,239 --> 04:20:16,880 count there's ultimately going to be a 7075 04:20:14,760 --> 04:20:18,960 limitation on high how we can count 7076 04:20:16,880 --> 04:20:20,720 because we only have a finite amount of 7077 04:20:18,960 --> 04:20:22,840 memory we don't have an infinite number 7078 04:20:20,720 --> 04:20:25,520 of zeros and ones to play with we have 7079 04:20:22,840 --> 04:20:26,960 to actually be bounded ultimately so 7080 04:20:25,520 --> 04:20:28,600 what's the implication of this well it 7081 04:20:26,960 --> 04:20:31,840 turns out that computers typically use 7082 04:20:28,600 --> 04:20:33,640 as many as 32 bits zeros or ones to 7083 04:20:31,840 --> 04:20:36,040 represent something like an integer or 7084 04:20:33,640 --> 04:20:38,040 in C an INT so for instance the smallest 7085 04:20:36,040 --> 04:20:40,640 number we could represent using 32 ins 7086 04:20:38,040 --> 04:20:44,159 of course using 32 bits of course would 7087 04:20:40,640 --> 04:20:45,880 be zero 32 zeros like this here and the 7088 04:20:44,159 --> 04:20:48,560 biggest number we could represent is by 7089 04:20:45,880 --> 04:20:50,600 changing all of those zeros to ones 7090 04:20:48,560 --> 04:20:53,159 which in this case will ideally give us 7091 04:20:50,600 --> 04:20:56,000 a number that equals roughly 4 billion 7092 04:20:53,159 --> 04:21:00,640 in total it's actually 4 billion 294 mil 7093 04:20:56,000 --> 04:21:02,760 967 295 maximally if you set all 32 of 7094 04:21:00,640 --> 04:21:05,720 those bits to ones and then do out the 7095 04:21:02,760 --> 04:21:07,720 actual math the catch though is that we 7096 04:21:05,720 --> 04:21:09,359 humans and Compu in general also 7097 04:21:07,720 --> 04:21:11,640 sometimes want to and need to be able to 7098 04:21:09,359 --> 04:21:13,560 represent negative numbers so if you 7099 04:21:11,640 --> 04:21:15,760 want to represent negative numbers as 7100 04:21:13,560 --> 04:21:17,479 well as positive numbers in zero you 7101 04:21:15,760 --> 04:21:19,319 can't really just start counting at zero 7102 04:21:17,479 --> 04:21:20,960 and go all the way up to roughly four 7103 04:21:19,319 --> 04:21:23,239 billion you got to kind of split the 7104 04:21:20,960 --> 04:21:25,199 difference and maybe allocate half of 7105 04:21:23,239 --> 04:21:26,720 those patterns of zeros and ones to 7106 04:21:25,199 --> 04:21:29,439 negative numbers and the other half 7107 04:21:26,720 --> 04:21:31,239 roughly to positive numbers so in fact 7108 04:21:29,439 --> 04:21:33,800 in practice when you're using even as 7109 04:21:31,239 --> 04:21:35,119 many as 32 bits the highest most 7110 04:21:33,800 --> 04:21:37,319 computers could count certainly in a 7111 04:21:35,119 --> 04:21:40,640 program like this in C using an INT 7112 04:21:37,319 --> 04:21:42,960 would be roughly 2 billion that is 2 b47 7113 04:21:40,640 --> 04:21:42,960 m 7114 04:22:05,359 --> 04:22:10,119 483,000 flow where you want to use more 7115 04:22:08,279 --> 04:22:12,159 bits than you have available and as a 7116 04:22:10,119 --> 04:22:14,159 result you sort of overflow the 7117 04:22:12,159 --> 04:22:15,680 available space what does this mean in 7118 04:22:14,159 --> 04:22:17,600 fact in real terms well let's suppose 7119 04:22:15,680 --> 04:22:19,560 that you only have three bits but I'm 7120 04:22:17,600 --> 04:22:22,439 going to gray out a fourth bit just to 7121 04:22:19,560 --> 04:22:24,239 convey where we'd like to uh put an 7122 04:22:22,439 --> 04:22:26,960 additional bit ultimately if this of 7123 04:22:24,239 --> 04:22:32,960 course is zero per week zero discussion 7124 04:22:26,960 --> 04:22:35,399 this is 1 2 3 4 5 6 7 now ideally in 7125 04:22:32,960 --> 04:22:37,960 binary if you want to add one more to 7126 04:22:35,399 --> 04:22:39,880 this value seven you're going to have to 7127 04:22:37,960 --> 04:22:43,159 carry the one mathematically and that 7128 04:22:39,880 --> 04:22:44,680 would ideally give you 1 0 0 0 but if 7129 04:22:43,159 --> 04:22:47,159 you don't have four bits and your 7130 04:22:44,680 --> 04:22:50,000 computer's only sophisticated enough to 7131 04:22:47,159 --> 04:22:51,479 have three bits not even 32 but three 7132 04:22:50,000 --> 04:22:54,800 the implication is that you're 7133 04:22:51,479 --> 04:22:58,760 effectively representing not one0 0 but 7134 04:22:54,800 --> 04:23:01,279 rather 0 z0 there's just no room to 7135 04:22:58,760 --> 04:23:03,199 store that fourth bit that I've gray out 7136 04:23:01,279 --> 04:23:05,520 here which is to say that your integer 7137 04:23:03,199 --> 04:23:08,199 might overflow and as soon as you get to 7138 04:23:05,520 --> 04:23:10,319 seven the next number once you add one 7139 04:23:08,199 --> 04:23:12,520 is actually going to be zero or worse as 7140 04:23:10,319 --> 04:23:14,840 we've seen here in my code a negative 7141 04:23:12,520 --> 04:23:16,800 value instead so what could we do to 7142 04:23:14,840 --> 04:23:18,720 perhaps address this kind of concern 7143 04:23:16,800 --> 04:23:20,840 well C does not have just integers or 7144 04:23:18,720 --> 04:23:22,600 ins it also has Longs which as the name 7145 04:23:20,840 --> 04:23:24,640 suggest or just longer integers which 7146 04:23:22,600 --> 04:23:26,640 means they have more bits available to 7147 04:23:24,640 --> 04:23:28,319 them so let me go back into my code here 7148 04:23:26,640 --> 04:23:30,159 I'll clear the terminal window and let 7149 04:23:28,319 --> 04:23:33,800 me go ahead and change my integers to 7150 04:23:30,159 --> 04:23:36,840 literally long here long here I'm going 7151 04:23:33,800 --> 04:23:39,399 to have to change my function in 7152 04:23:36,840 --> 04:23:40,880 uh cs50's library to be not get in but 7153 04:23:39,399 --> 04:23:42,439 get long and that's indeed another 7154 04:23:40,880 --> 04:23:44,000 function we provide in the library let 7155 04:23:42,439 --> 04:23:45,720 me change this get in to get long as 7156 04:23:44,000 --> 04:23:47,439 well I'll keep my variable names the 7157 04:23:45,720 --> 04:23:49,800 same but I do need to make one other 7158 04:23:47,439 --> 04:23:51,920 change it turns out that print F also 7159 04:23:49,800 --> 04:23:53,720 support supports other format codes so 7160 04:23:51,920 --> 04:23:55,840 not just percent I for integers or 7161 04:23:53,720 --> 04:23:59,159 percent s for Strings but also for 7162 04:23:55,840 --> 04:24:01,880 instance percent Li for a long integer 7163 04:23:59,159 --> 04:24:03,760 as well as percent f for floating Point 7164 04:24:01,880 --> 04:24:05,319 values with decimals so with that said 7165 04:24:03,760 --> 04:24:08,520 let's go ahead and change my print F 7166 04:24:05,319 --> 04:24:10,800 line to be not perc I but percent Li I 7167 04:24:08,520 --> 04:24:13,439 now let me go ahead and do make 7168 04:24:10,800 --> 04:24:16,920 calculator again enter no apparent 7169 04:24:13,439 --> 04:24:19,199 errors now do/ calulator and 2 + 2 still 7170 04:24:16,920 --> 04:24:22,000 equals 4 as before but now if I do 7171 04:24:19,199 --> 04:24:25,159 calculator again and let's do 7172 04:24:22,000 --> 04:24:28,920 two billion again as well as 2 billion 7173 04:24:25,159 --> 04:24:30,399 for y previously we overflowed the size 7174 04:24:28,920 --> 04:24:32,119 of an integer and got some weird 7175 04:24:30,399 --> 04:24:34,080 negative number because the pattern was 7176 04:24:32,119 --> 04:24:35,960 misinterpreted if you will as a negative 7177 04:24:34,080 --> 04:24:38,279 number instead but along instead of 7178 04:24:35,960 --> 04:24:41,000 using using 32 bits conventionally uses 7179 04:24:38,279 --> 04:24:43,199 64 bits which means we have more than 7180 04:24:41,000 --> 04:24:45,319 enough spare bits to go when we add 2 7181 04:24:43,199 --> 04:24:47,239 billion plus 2 billion and now in fact 7182 04:24:45,319 --> 04:24:50,080 we get the correct answer of four 7183 04:24:47,239 --> 04:24:53,359 billion which does fit inside of the 7184 04:24:50,080 --> 04:24:55,319 size of a long now along can count up 7185 04:24:53,359 --> 04:24:59,640 quite high and in fact it can count as 7186 04:24:55,319 --> 04:25:01,600 high as this nine quintilian and so that 7187 04:24:59,640 --> 04:25:03,720 will give us quite a bit more Runway but 7188 04:25:01,600 --> 04:25:05,760 of course it too is ultimately going to 7189 04:25:03,720 --> 04:25:07,199 be finite so if you have numbers that 7190 04:25:05,760 --> 04:25:09,880 need to go bigger than that you might 7191 04:25:07,199 --> 04:25:11,040 still very well have a problem now 7192 04:25:09,880 --> 04:25:12,880 there's another problem that we might 7193 04:25:11,040 --> 04:25:14,479 run into as well and we can see it in 7194 04:25:12,880 --> 04:25:16,840 the context of even this simple 7195 04:25:14,479 --> 04:25:19,159 calculator computers also suffer from 7196 04:25:16,840 --> 04:25:20,479 potentially what's called truncation 7197 04:25:19,159 --> 04:25:22,439 where especially when you're doing math 7198 04:25:20,479 --> 04:25:24,319 involving floating Point values that is 7199 04:25:22,439 --> 04:25:27,119 numbers with decimals you might 7200 04:25:24,319 --> 04:25:29,319 accidentally unknowingly truncate the 7201 04:25:27,119 --> 04:25:31,040 value that is lose everything after the 7202 04:25:29,319 --> 04:25:33,040 decimal point so in fact let me go back 7203 04:25:31,040 --> 04:25:35,119 to vs code here I'll clear my terminal 7204 04:25:33,040 --> 04:25:37,560 window and let's still use Longs but 7205 04:25:35,119 --> 04:25:39,920 let's go ahead and use division instead 7206 04:25:37,560 --> 04:25:42,080 of addition here so let me change this 7207 04:25:39,920 --> 04:25:44,080 plus to a divide operator let me go 7208 04:25:42,080 --> 04:25:46,359 ahead and recompile the code down here 7209 04:25:44,080 --> 04:25:48,720 with make calculator let me go ahead and 7210 04:25:46,359 --> 04:25:53,199 run/ calculator and let me go ahead and 7211 04:25:48,720 --> 04:25:55,199 do something like 14x and 3 for y and 7212 04:25:53,199 --> 04:25:58,760 we'll see that well wait a minute 1 7213 04:25:55,199 --> 04:26:00,439 divided 3 I learned should be 13 but in 7214 04:25:58,760 --> 04:26:03,840 a floating point value that should be 7215 04:26:00,439 --> 04:26:05,600 point0 it should be 7216 04:26:03,840 --> 04:26:07,239 0.33333 you know maybe with a little 7217 04:26:05,600 --> 04:26:08,640 line over in grade school but really an 7218 04:26:07,239 --> 04:26:11,080 infinite number of Threes And yet we 7219 04:26:08,640 --> 04:26:13,119 seem to have lost even one of those 7220 04:26:11,080 --> 04:26:15,960 threes after the decimal point because 7221 04:26:13,119 --> 04:26:19,319 the answer is coming back here as just 7222 04:26:15,960 --> 04:26:21,960 zero so why might that be well if I know 7223 04:26:19,319 --> 04:26:23,680 that two integers when divided one by 7224 04:26:21,960 --> 04:26:25,960 the other is supposed to give me a 7225 04:26:23,680 --> 04:26:28,279 fraction a floating point value with a 7226 04:26:25,960 --> 04:26:30,319 decimal point I can't continue to use 7227 04:26:28,279 --> 04:26:32,439 integers or even in this case Longs 7228 04:26:30,319 --> 04:26:34,119 which do not have support for decimal 7229 04:26:32,439 --> 04:26:36,399 points so let me go ahead and change 7230 04:26:34,119 --> 04:26:39,040 this format code here from percent Li I 7231 04:26:36,399 --> 04:26:41,159 to percent F which is again going to 7232 04:26:39,040 --> 04:26:43,399 represent a floating point value instead 7233 04:26:41,159 --> 04:26:47,199 of a long integer or even an integer and 7234 04:26:43,399 --> 04:26:50,680 let me go ahead further and Define maybe 7235 04:26:47,199 --> 04:26:53,560 a third variable Z as a float itself so 7236 04:26:50,680 --> 04:26:56,080 I'll give myself a variable Z equals x / 7237 04:26:53,560 --> 04:26:57,720 Y and now rather than print X ided Y 7238 04:26:56,080 --> 04:26:59,119 let's just go ahead and print Z so now 7239 04:26:57,720 --> 04:27:01,279 I'm operating in a world of floating 7240 04:26:59,119 --> 04:27:04,080 Point values because I know proactively 7241 04:27:01,279 --> 04:27:06,119 that an long or an INT divided by 7242 04:27:04,080 --> 04:27:08,279 another such value if it's meant to have 7243 04:27:06,119 --> 04:27:10,319 a fraction needs to be stored in a 7244 04:27:08,279 --> 04:27:12,279 floating point value something with a 7245 04:27:10,319 --> 04:27:14,520 decimal point well let me go down to my 7246 04:27:12,279 --> 04:27:17,680 terminal window here and rerun make of 7247 04:27:14,520 --> 04:27:22,159 calculator seems to work okay/ calulator 7248 04:27:17,680 --> 04:27:24,239 and let's do 1 / 3 again and still here 7249 04:27:22,159 --> 04:27:25,880 we see all zeros well so we do at least 7250 04:27:24,239 --> 04:27:27,439 see a decimal point so we've made some 7251 04:27:25,880 --> 04:27:29,680 progress thanks to the percent F in the 7252 04:27:27,439 --> 04:27:33,920 float but it seems that we've already 7253 04:27:29,680 --> 04:27:35,319 truncated the value 1 / 3 so how do we 7254 04:27:33,920 --> 04:27:36,920 actually get around this issue well if 7255 04:27:35,319 --> 04:27:38,159 you the program know that you're dealing 7256 04:27:36,920 --> 04:27:39,960 in a world that's going to give you 7257 04:27:38,159 --> 04:27:42,680 floating Point values with decimal 7258 04:27:39,960 --> 04:27:44,560 points you might very well need to use 7259 04:27:42,680 --> 04:27:47,439 what's called a feature known as typ 7260 04:27:44,560 --> 04:27:49,159 casting that is convert one data type to 7261 04:27:47,439 --> 04:27:51,359 another by explicitly telling the 7262 04:27:49,159 --> 04:27:52,880 compiler that you want to do so now how 7263 04:27:51,359 --> 04:27:55,560 do I do this well let's go back to my 7264 04:27:52,880 --> 04:28:00,279 code here and if the issue fundamentally 7265 04:27:55,560 --> 04:28:02,439 is that c is still treating X and Y as 7266 04:28:00,279 --> 04:28:04,640 integers or technically Longs with no 7267 04:28:02,439 --> 04:28:06,600 decimal point and dividing one by the 7268 04:28:04,640 --> 04:28:09,080 other therefore has no room so to speak 7269 04:28:06,600 --> 04:28:11,800 for any numbers after a decimal point 7270 04:28:09,080 --> 04:28:13,840 why don't I proactively do this let me 7271 04:28:11,800 --> 04:28:15,960 using a slightly new syntax with 7272 04:28:13,840 --> 04:28:19,720 parenthesis specify that I want to 7273 04:28:15,960 --> 04:28:22,479 convert X proactively from a long to a 7274 04:28:19,720 --> 04:28:25,760 float let me specify proactively that I 7275 04:28:22,479 --> 04:28:28,119 want to convert y from a long to a float 7276 04:28:25,760 --> 04:28:30,680 as well and now let me go ahead and 7277 04:28:28,119 --> 04:28:32,880 trust that in Z should be the result of 7278 04:28:30,680 --> 04:28:35,920 dividing not a long by a long or an INT 7279 04:28:32,880 --> 04:28:38,199 by an INT but rather a float by a float 7280 04:28:35,920 --> 04:28:41,239 Let Me Clear My terminal window run make 7281 04:28:38,199 --> 04:28:45,760 calculator again seems to work okay/ 7282 04:28:41,239 --> 04:28:48,359 calulator and now 1 3 and hopefully now 7283 04:28:45,760 --> 04:28:49,960 we actually see that my code has 7284 04:28:48,359 --> 04:28:53,279 outputed 7285 04:28:49,960 --> 04:28:55,040 0.333 333 and I think if we kept showing 7286 04:28:53,279 --> 04:28:56,800 more numbers after the decimal point 7287 04:28:55,040 --> 04:28:59,439 we'd theoretically see as many of those 7288 04:28:56,800 --> 04:29:01,880 threes as we want but there is still one 7289 04:28:59,439 --> 04:29:03,359 more catch and especially when we're 7290 04:29:01,880 --> 04:29:05,880 manipulating numbers in this way in a 7291 04:29:03,359 --> 04:29:07,479 computer using a finite amount of memory 7292 04:29:05,880 --> 04:29:09,840 another challenge we might run up 7293 04:29:07,479 --> 04:29:12,279 against besides integer overflow besides 7294 04:29:09,840 --> 04:29:14,920 truncation is this known as floating 7295 04:29:12,279 --> 04:29:17,040 point in Precision just as we can't 7296 04:29:14,920 --> 04:29:19,199 represent as big of an integer as we 7297 04:29:17,040 --> 04:29:20,760 want using int or long alone because 7298 04:29:19,199 --> 04:29:22,840 there is going to be an upper bound 7299 04:29:20,760 --> 04:29:26,000 they're similarly going to be a boundary 7300 04:29:22,840 --> 04:29:28,080 on just how precise our numbers can be 7301 04:29:26,000 --> 04:29:30,080 and indeed let's go back to vs code here 7302 04:29:28,080 --> 04:29:31,479 I'll clear my terminal window yet again 7303 04:29:30,080 --> 04:29:33,600 and this time let me use some slightly 7304 04:29:31,479 --> 04:29:35,040 unley syntax to specify that I don't 7305 04:29:33,600 --> 04:29:36,600 want to see the default number of 7306 04:29:35,040 --> 04:29:38,920 numbers after the the decimal point 7307 04:29:36,600 --> 04:29:42,040 which percent F gives us automatically 7308 04:29:38,920 --> 04:29:44,119 let's go ahead and show me 20 decimal 7309 04:29:42,040 --> 04:29:46,279 point numbers after the decimal point 7310 04:29:44,119 --> 04:29:50,080 and the weird Syntax for this is to do 7311 04:29:46,279 --> 04:29:52,159 not percent F but percent period to zero 7312 04:29:50,080 --> 04:29:54,239 to indicate to see that I want to see 20 7313 04:29:52,159 --> 04:29:56,119 digits not the default after now the 7314 04:29:54,239 --> 04:29:59,080 decimal point let me rerun make 7315 04:29:56,119 --> 04:30:02,520 calculator let me do dot calculat again 7316 04:29:59,080 --> 04:30:04,640 and let's do one let's do three and now 7317 04:30:02,520 --> 04:30:06,520 this is even weirder right from grade 7318 04:30:04,640 --> 04:30:08,439 school you presumably learned that 1 7319 04:30:06,520 --> 04:30:10,159 divided 3 is of course 1/3 but that 7320 04:30:08,439 --> 04:30:13,000 should be 7321 04:30:10,159 --> 04:30:15,239 0.33333 infinitely many times or on 7322 04:30:13,000 --> 04:30:17,119 paper with a little line over it but the 7323 04:30:15,239 --> 04:30:18,960 computer is just doing some weird 7324 04:30:17,119 --> 04:30:23,279 approximation here it's a whole bunch of 7325 04:30:18,960 --> 04:30:25,479 Threes And then 43 267 44 079 7326 04:30:23,279 --> 04:30:27,159 590 well what's really happening under 7327 04:30:25,479 --> 04:30:29,479 the hood well again is this issue of 7328 04:30:27,159 --> 04:30:31,800 floating point in Precision if you only 7329 04:30:29,479 --> 04:30:33,840 have a finite number of bits and in turn 7330 04:30:31,800 --> 04:30:36,239 a finite amount of memory the computer 7331 04:30:33,840 --> 04:30:38,960 can really only be so precise 7332 04:30:36,239 --> 04:30:41,119 intuitively alter Converse or 7333 04:30:38,960 --> 04:30:43,359 equivalently the computer has decided on 7334 04:30:41,119 --> 04:30:45,359 some way of representing floating Point 7335 04:30:43,359 --> 04:30:47,399 values but the catch is per grade school 7336 04:30:45,359 --> 04:30:49,439 math there's an infinite number of 7337 04:30:47,399 --> 04:30:51,359 numbers out there and an infinite number 7338 04:30:49,439 --> 04:30:53,040 of floating Point values because you can 7339 04:30:51,359 --> 04:30:55,520 keep adding more and more digits if you 7340 04:30:53,040 --> 04:30:57,119 want so the computer given the way it's 7341 04:30:55,520 --> 04:30:59,279 implementing these floating Point values 7342 04:30:57,119 --> 04:31:01,920 is essentially giving us the closest 7343 04:30:59,279 --> 04:31:04,560 approximation that it can now how can we 7344 04:31:01,920 --> 04:31:06,199 go about improving the situation well 7345 04:31:04,560 --> 04:31:08,159 there is one alternative instead of 7346 04:31:06,199 --> 04:31:09,920 using float I can use something called a 7347 04:31:08,159 --> 04:31:11,720 double which as the name suggests uses 7348 04:31:09,920 --> 04:31:14,720 twice as many bits as a float so instead 7349 04:31:11,720 --> 04:31:16,119 of 32 typically it will use 64 and 7350 04:31:14,720 --> 04:31:17,880 that's just like the difference between 7351 04:31:16,119 --> 04:31:19,840 a long and an INT which gave us more 7352 04:31:17,880 --> 04:31:22,040 bits but in this case this will be used 7353 04:31:19,840 --> 04:31:24,880 for more Precision let's go ahead and 7354 04:31:22,040 --> 04:31:26,920 Cast X to a double let's cast y to a 7355 04:31:24,880 --> 04:31:30,840 double and now let's go ahead and using 7356 04:31:26,920 --> 04:31:32,720 the same format code percent 2f is still 7357 04:31:30,840 --> 04:31:35,640 okay for doubles let me do make 7358 04:31:32,720 --> 04:31:39,000 calculator let me do do slash calculator 7359 04:31:35,640 --> 04:31:40,560 and now let me do 1 / 3 and we still 7360 04:31:39,000 --> 04:31:42,040 have some of that in precision and we'd 7361 04:31:40,560 --> 04:31:44,640 see even more of it if we looked at more 7362 04:31:42,040 --> 04:31:46,920 than just 20 digits but now we have more 7363 04:31:44,640 --> 04:31:49,399 threes after the decimal point so it's 7364 04:31:46,920 --> 04:31:52,439 at least more more more precise but it's 7365 04:31:49,399 --> 04:31:54,640 not perfect but it it's at least more 7366 04:31:52,439 --> 04:31:56,159 precise so these kinds of issues then 7367 04:31:54,640 --> 04:31:57,399 are going to be necessary to keep in 7368 04:31:56,159 --> 04:31:59,319 mind anytime you do something 7369 04:31:57,399 --> 04:32:01,080 numerically scientifically at least with 7370 04:31:59,319 --> 04:32:02,960 a language like C where you're going to 7371 04:32:01,080 --> 04:32:05,080 bump up against these real world 7372 04:32:02,960 --> 04:32:06,840 limitations of hardware and intern 7373 04:32:05,080 --> 04:32:08,800 language now later in the semester we'll 7374 04:32:06,840 --> 04:32:09,840 transition to a language called Python 7375 04:32:08,800 --> 04:32:11,720 and that's actually going to solve at 7376 04:32:09,840 --> 04:32:13,520 least one of these problems for us but 7377 04:32:11,720 --> 04:32:15,479 just automatically giving us more bits 7378 04:32:13,520 --> 04:32:17,239 so to speak as we need them at least for 7379 04:32:15,479 --> 04:32:19,720 integers but even the issue of floating 7380 04:32:17,239 --> 04:32:21,800 point in Precision is going to remain 7381 04:32:19,720 --> 04:32:23,960 now just how real world are these issues 7382 04:32:21,800 --> 04:32:25,800 well back in the year 1999 we got a 7383 04:32:23,960 --> 04:32:28,040 taste of this when the world realized in 7384 04:32:25,800 --> 04:32:30,199 the Years leading up to that date that 7385 04:32:28,040 --> 04:32:33,159 it might not have been the best idea to 7386 04:32:30,199 --> 04:32:35,600 implement computers and software therein 7387 04:32:33,159 --> 04:32:38,199 by storing years using just two digits 7388 04:32:35,600 --> 04:32:40,520 like instead of storing 1999 to 7389 04:32:38,199 --> 04:32:43,040 represent the year 1999 a lot of 7390 04:32:40,520 --> 04:32:44,640 computers for reasons of space and cost 7391 04:32:43,040 --> 04:32:46,800 were in the habit of kind of cutting a 7392 04:32:44,640 --> 04:32:49,359 corner and just using two digits to keep 7393 04:32:46,800 --> 04:32:51,920 track of the year the problem with that 7394 04:32:49,359 --> 04:32:55,520 is that if systems were not updated by 7395 04:32:51,920 --> 04:32:57,800 the year 1999 to support the year 2000 7396 04:32:55,520 --> 04:32:59,880 2001 and so forth is that just like 7397 04:32:57,800 --> 04:33:02,040 before with integer overflow some 7398 04:32:59,880 --> 04:33:04,840 computers might add one to the year in 7399 04:33:02,040 --> 04:33:06,680 their memory 99 it should be the year 7400 04:33:04,840 --> 04:33:08,959 2000 but if they're only using two 7401 04:33:06,680 --> 04:33:11,039 digits to represent years they might 7402 04:33:08,959 --> 04:33:14,000 mistake the year as some systems may 7403 04:33:11,039 --> 04:33:16,561 very well have for the year 1900 instead 7404 04:33:14,000 --> 04:33:18,680 taking literally a big step backwards if 7405 04:33:16,561 --> 04:33:20,359 you will now you'd like to think that 7406 04:33:18,680 --> 04:33:21,920 kind of issue is behind us especially as 7407 04:33:20,359 --> 04:33:24,080 we understand all the more about the 7408 04:33:21,920 --> 04:33:25,480 limitations of code and Computing but 7409 04:33:24,080 --> 04:33:27,561 we're actually going to run up against 7410 04:33:25,480 --> 04:33:30,600 this very same type of issue again in 7411 04:33:27,561 --> 04:33:31,520 just a few years on January 19th in the 7412 04:33:30,600 --> 04:33:34,639 year 7413 04:33:31,520 --> 04:33:36,600 2038 we will have run out of bits in 7414 04:33:34,639 --> 04:33:39,119 most computers right now to keep track 7415 04:33:36,600 --> 04:33:42,480 of time it turns out years ago humans 7416 04:33:39,119 --> 04:33:44,719 decided to use a 32-bit integer to keep 7417 04:33:42,480 --> 04:33:46,480 track of how many seconds had elapsed 7418 04:33:44,719 --> 04:33:48,920 over time they chose a somewhat 7419 04:33:46,480 --> 04:33:51,520 arbitrary date in the past January 1st 7420 04:33:48,920 --> 04:33:53,920 1970 and they just started counting 7421 04:33:51,520 --> 04:33:55,920 seconds from there on out and so if a 7422 04:33:53,920 --> 04:33:57,799 computer stores some number of seconds 7423 04:33:55,920 --> 04:33:59,400 that tells the computer how many seconds 7424 04:33:57,799 --> 04:34:02,600 have passed since that particular date 7425 04:33:59,400 --> 04:34:04,439 January 1st 1970 unfortunately using a 7426 04:34:02,600 --> 04:34:07,279 32-bit integer as we've seen you can 7427 04:34:04,439 --> 04:34:10,439 only count so high at which point you 7428 04:34:07,279 --> 04:34:12,359 overflow the size of that variable and 7429 04:34:10,439 --> 04:34:14,359 so potentially if we don't get ahead of 7430 04:34:12,359 --> 04:34:17,959 this as humans as a society as computer 7431 04:34:14,359 --> 04:34:21,639 scientist on the date January 19th 7432 04:34:17,959 --> 04:34:23,480 2038 that bit might flip over there by 7433 04:34:21,639 --> 04:34:25,920 overflowing the size of those integers 7434 04:34:23,480 --> 04:34:28,080 bringing us back computationally to 7435 04:34:25,920 --> 04:34:31,039 December 13th 7436 04:34:28,080 --> 04:34:32,561 1901 so this is to say now with all of 7437 04:34:31,039 --> 04:34:34,561 this computational ability and code 7438 04:34:32,561 --> 04:34:36,400 comes a responsibility to actually write 7439 04:34:34,561 --> 04:34:38,160 correct code next week we'll peel back 7440 04:34:36,400 --> 04:34:40,359 some of these layers but for now this 7441 04:34:38,160 --> 04:34:42,561 was week one and best of luck on problem 7442 04:34:40,359 --> 04:34:42,561 set 7443 04:34:44,110 --> 04:34:52,240 [Music] 7444 04:34:58,210 --> 04:35:03,700 [Music] 7445 04:35:04,199 --> 04:35:07,199 one 7446 04:35:22,820 --> 04:35:41,459 [Music] 7447 04:35:45,980 --> 04:35:49,040 [Music] 7448 04:35:55,310 --> 04:35:58,400 [Music] 7449 04:36:05,250 --> 04:36:18,610 [Music] 7450 04:36:32,639 --> 04:36:37,639 all right this is cs50 and this is this 7451 04:36:35,160 --> 04:36:40,000 is week two wherein we're going to take 7452 04:36:37,639 --> 04:36:41,600 a look at a lower level at how things 7453 04:36:40,000 --> 04:36:43,561 work and indeed among the goals of the 7454 04:36:41,600 --> 04:36:44,879 course isn't this bottom up 7455 04:36:43,561 --> 04:36:46,600 understanding so that in a couple of 7456 04:36:44,879 --> 04:36:48,279 weeks time even a few years time when 7457 04:36:46,600 --> 04:36:49,719 you encounter some new technology you'll 7458 04:36:48,279 --> 04:36:51,719 be able to think back hopefully on some 7459 04:36:49,719 --> 04:36:53,119 of this week's and this courses basic 7460 04:36:51,719 --> 04:36:54,760 building blocks and Primitives and 7461 04:36:53,119 --> 04:36:56,879 really just deduce how tomorrow's 7462 04:36:54,760 --> 04:36:58,359 Technologies work but along the way it's 7463 04:36:56,879 --> 04:37:00,080 going to seem it's going to be a little 7464 04:36:58,359 --> 04:37:02,480 hard perhaps to see the forest for the 7465 04:37:00,080 --> 04:37:04,119 tree so to speak and so the goal at the 7466 04:37:02,480 --> 04:37:05,439 end of the day still is going to be 7467 04:37:04,119 --> 04:37:07,039 problem solving and so we thought we'd 7468 04:37:05,439 --> 04:37:09,320 begin today with a look at some of the 7469 04:37:07,039 --> 04:37:11,400 problems we'll talk about or solve this 7470 04:37:09,320 --> 04:37:12,840 coming week uh and for that we have some 7471 04:37:11,400 --> 04:37:14,400 Brave volunteers who have already come 7472 04:37:12,840 --> 04:37:17,359 up if we could turn on some dramatic 7473 04:37:14,400 --> 04:37:20,719 lighting and meet today's 7474 04:37:17,359 --> 04:37:22,439 volunteers so on my left here we have hi 7475 04:37:20,719 --> 04:37:24,199 my name is Alex I'm a first year at the 7476 04:37:22,439 --> 04:37:28,119 college and I'm from Chapo North 7477 04:37:24,199 --> 04:37:30,639 Carolina Welcome to Alex and to Alex's 7478 04:37:28,119 --> 04:37:32,279 right um I'm Sarah I'm from Toronto 7479 04:37:30,639 --> 04:37:34,359 Canada and I'm also a first year student 7480 04:37:32,279 --> 04:37:36,879 at the college wonderful well welcome to 7481 04:37:34,359 --> 04:37:38,320 both Al and Sarah so one of the problems 7482 04:37:36,879 --> 04:37:40,561 you'll perhaps solve this week for 7483 04:37:38,320 --> 04:37:42,680 problem set two is to analyze the 7484 04:37:40,561 --> 04:37:44,240 reading level of a body of text whether 7485 04:37:42,680 --> 04:37:45,879 someone reads at a first grade level 7486 04:37:44,240 --> 04:37:48,719 second grade level third grade level all 7487 04:37:45,879 --> 04:37:51,000 the way up to 12 or 13 or Beyond but 7488 04:37:48,719 --> 04:37:52,520 you've perhaps never quite thought about 7489 04:37:51,000 --> 04:37:54,799 certainly in terms of code like how you 7490 04:37:52,520 --> 04:37:56,561 would analyze some text some book and 7491 04:37:54,799 --> 04:37:58,039 figure out what reading level is it at 7492 04:37:56,561 --> 04:37:59,561 and yet surely our teachers growing up 7493 04:37:58,039 --> 04:38:01,959 kind of knew or had an intuitive sense 7494 04:37:59,561 --> 04:38:03,160 of this so let's consider some sample 7495 04:38:01,959 --> 04:38:05,920 text for instance Alex what have you 7496 04:38:03,160 --> 04:38:12,520 been reading lately um 7497 04:38:05,920 --> 04:38:15,680 One Fish Two Fish Red F Fish Blue Fish 7498 04:38:12,520 --> 04:38:17,600 wonderful so given that what grade level 7499 04:38:15,680 --> 04:38:20,520 would you say Alex is currently reading 7500 04:38:17,600 --> 04:38:23,719 at feel free to just shout it 7501 04:38:20,520 --> 04:38:26,600 out first first so indeed you'll see 7502 04:38:23,719 --> 04:38:28,680 this week if you run your code on Alex's 7503 04:38:26,600 --> 04:38:30,719 text it actually turns out he reads 7504 04:38:28,680 --> 04:38:32,760 below a first grade reading level but 7505 04:38:30,719 --> 04:38:35,520 but why might that be what might your 7506 04:38:32,760 --> 04:38:37,359 intuition be for why we've 7507 04:38:35,520 --> 04:38:40,160 uh why we've accused Alex of reading at 7508 04:38:37,359 --> 04:38:43,039 this level feel free to shout 7509 04:38:40,160 --> 04:38:44,719 out yeah so very few syllables short 7510 04:38:43,039 --> 04:38:47,039 words short sentences and so there's 7511 04:38:44,719 --> 04:38:48,639 some puristic perhaps we can infer from 7512 04:38:47,039 --> 04:38:50,520 that short text that that probably means 7513 04:38:48,639 --> 04:38:52,240 that it's best for younger children now 7514 04:38:50,520 --> 04:38:55,240 Sarah by contrast what have you been 7515 04:38:52,240 --> 04:38:58,359 reading Mr and Miss dersley of number 7516 04:38:55,240 --> 04:39:00,119 four privet Drive were proud to say that 7517 04:38:58,359 --> 04:39:02,000 they were perfectly normal thank you 7518 04:39:00,119 --> 04:39:03,879 very much they were the last people 7519 04:39:02,000 --> 04:39:06,080 you'd expect to be involved in anything 7520 04:39:03,879 --> 04:39:08,600 strange or mysterious ious because they 7521 04:39:06,080 --> 04:39:10,160 just didn't hold with much nonsense all 7522 04:39:08,600 --> 04:39:11,561 right now irrespective of what grade you 7523 04:39:10,160 --> 04:39:13,199 were in when you might have read that 7524 04:39:11,561 --> 04:39:14,119 text what grade level does Sarah seem to 7525 04:39:13,199 --> 04:39:18,080 be reading 7526 04:39:14,119 --> 04:39:19,799 at so eighth grade second grade okay so 7527 04:39:18,080 --> 04:39:21,920 hearing a bit of everything so that at 7528 04:39:19,799 --> 04:39:23,561 least according to code would actually 7529 04:39:21,920 --> 04:39:26,240 be seventh grade and what might the 7530 04:39:23,561 --> 04:39:27,439 intuition there be why is that a higher 7531 04:39:26,240 --> 04:39:31,320 grade level even though we might 7532 04:39:27,439 --> 04:39:33,439 disagree exactly which grade it is comp 7533 04:39:31,320 --> 04:39:35,080 yeah so complicated sentences longer 7534 04:39:33,439 --> 04:39:36,719 sentences so indeed a lot more more 7535 04:39:35,080 --> 04:39:38,240 words were being spoken by Sarah because 7536 04:39:36,719 --> 04:39:40,280 there was so much more there on the page 7537 04:39:38,240 --> 04:39:42,200 so we'll translate these ideas this 7538 04:39:40,280 --> 04:39:44,440 coming week and problem set two if you 7539 04:39:42,200 --> 04:39:45,958 tackle this one to code so that you can 7540 04:39:44,440 --> 04:39:47,680 ultimately infer things of these 7541 04:39:45,958 --> 04:39:49,320 quantitatively but to do so we're going 7542 04:39:47,680 --> 04:39:50,600 to have to understand text so let's 7543 04:39:49,320 --> 04:39:53,400 first thank our volunteers and then 7544 04:39:50,600 --> 04:39:56,718 we'll dive in to that lower 7545 04:39:53,400 --> 04:39:56,718 level stress 7546 04:39:58,798 --> 04:40:04,798 balls sure you can keep those yeah all 7547 04:40:02,840 --> 04:40:06,400 right so besides that let's consider one 7548 04:40:04,798 --> 04:40:08,680 another body of text perhaps that you 7549 04:40:06,400 --> 04:40:10,280 might see this week which is namely a 7550 04:40:08,680 --> 04:40:11,680 little something like this what I have 7551 04:40:10,280 --> 04:40:13,920 here on the screen is what we'll start 7552 04:40:11,680 --> 04:40:15,638 calling today Cipher text it's the 7553 04:40:13,920 --> 04:40:17,600 result of encrypting some piece of 7554 04:40:15,638 --> 04:40:19,080 information and encryption or more 7555 04:40:17,600 --> 04:40:21,120 generally the Art and Science of 7556 04:40:19,080 --> 04:40:22,798 cryptography is all around us it's what 7557 04:40:21,120 --> 04:40:24,320 you're using on the web on your phones 7558 04:40:22,798 --> 04:40:26,878 with your Banks and anything that tries 7559 04:40:24,320 --> 04:40:28,160 to keep data secure is using encryption 7560 04:40:26,878 --> 04:40:30,400 but there's going to be different levels 7561 04:40:28,160 --> 04:40:31,798 of encryption strong encryption weak 7562 04:40:30,400 --> 04:40:34,878 encryption and what you see here on the 7563 04:40:31,798 --> 04:40:36,878 screen isn't all that strong but we'll 7564 04:40:34,878 --> 04:40:39,200 see later today how we might decrypt 7565 04:40:36,878 --> 04:40:41,480 this and actually reveal what the plain 7566 04:40:39,200 --> 04:40:43,600 text is that corresponds to that Cipher 7567 04:40:41,480 --> 04:40:45,520 text but in order to do so we have to 7568 04:40:43,600 --> 04:40:47,040 start taking off some training wheels so 7569 04:40:45,520 --> 04:40:49,040 to speak and believe it or not even 7570 04:40:47,040 --> 04:40:50,878 though your time with C this past week 7571 04:40:49,040 --> 04:40:53,000 for the first time probably might have 7572 04:40:50,878 --> 04:40:55,240 been rather in the weeds and much more 7573 04:40:53,000 --> 04:40:56,560 complicated seemingly than C it turns 7574 04:40:55,240 --> 04:40:58,240 out that along the way we have been 7575 04:40:56,560 --> 04:41:00,120 providing and will continue to provide 7576 04:40:58,240 --> 04:41:02,520 certain training wheels for instance the 7577 04:41:00,120 --> 04:41:05,400 cs50 library is one of them and even 7578 04:41:02,520 --> 04:41:07,320 some of the explanations give up topics 7579 04:41:05,400 --> 04:41:09,000 for now in these early weeks will be 7580 04:41:07,320 --> 04:41:10,638 somewhat simplified abstracted away if 7581 04:41:09,000 --> 04:41:12,360 you will but the goal ultimately is for 7582 04:41:10,638 --> 04:41:14,680 you to understand each and every one of 7583 04:41:12,360 --> 04:41:17,040 those details so that after cs50 you 7584 04:41:14,680 --> 04:41:18,600 really can stand on your own and 7585 04:41:17,040 --> 04:41:21,638 understand and wrap your mind around any 7586 04:41:18,600 --> 04:41:24,000 future Technologies as well so let's 7587 04:41:21,638 --> 04:41:25,480 consider first the very first program 7588 04:41:24,000 --> 04:41:27,798 with which we began last week which was 7589 04:41:25,480 --> 04:41:29,160 this one so hello world and C at the end 7590 04:41:27,798 --> 04:41:30,680 of the day it was really the print a 7591 04:41:29,160 --> 04:41:32,520 function that was doing the interesting 7592 04:41:30,680 --> 04:41:35,120 part of the work but there was a lot of 7593 04:41:32,520 --> 04:41:37,320 technical stuff above and below it the 7594 04:41:35,120 --> 04:41:39,920 the curly braces the parentheses words 7595 04:41:37,320 --> 04:41:42,000 like void and include and then of course 7596 04:41:39,920 --> 04:41:43,760 the angled brackets and more but at the 7597 04:41:42,000 --> 04:41:47,040 end of the day we needed to convert that 7598 04:41:43,760 --> 04:41:48,680 source code in C to machine code the 7599 04:41:47,040 --> 04:41:51,080 zeros and ones in binary that the 7600 04:41:48,680 --> 04:41:53,600 computer understood and to do that of 7601 04:41:51,080 --> 04:41:55,600 course we ran we compiled the code we 7602 04:41:53,600 --> 04:41:57,600 ran make and then we were able to 7603 04:41:55,600 --> 04:41:59,360 actually run that code there so let me 7604 04:41:57,600 --> 04:42:02,280 actually go over here to VSS code and 7605 04:41:59,360 --> 04:42:04,280 really quickly recreate that hello.c 7606 04:42:02,280 --> 04:42:08,520 pretty much by transcribing the same so 7607 04:42:04,280 --> 04:42:12,920 I have here uh include standard 7608 04:42:08,520 --> 04:42:15,120 i.h uh int main void and then in here I 7609 04:42:12,920 --> 04:42:17,760 had quite simply hello comma world with 7610 04:42:15,120 --> 04:42:20,000 my back slend quotes and more now last 7611 04:42:17,760 --> 04:42:22,120 time to compile this I indeed ran make 7612 04:42:20,000 --> 04:42:23,520 hello followed by enter hopefully you 7613 04:42:22,120 --> 04:42:26,080 see no errors and that's a good thing 7614 04:42:23,520 --> 04:42:28,240 and if you do do/ hello you see in fact 7615 04:42:26,080 --> 04:42:31,240 the results of that program but it turns 7616 04:42:28,240 --> 04:42:33,360 out that make is not actually a compiler 7617 04:42:31,240 --> 04:42:35,920 as I alluded to last week it's a program 7618 04:42:33,360 --> 04:42:37,520 that clearly makes your program but it 7619 04:42:35,920 --> 04:42:39,120 itself just automates the process of 7620 04:42:37,520 --> 04:42:41,120 using an actual compiler and there's 7621 04:42:39,120 --> 04:42:43,000 lots of different compilers out there 7622 04:42:41,120 --> 04:42:44,600 and the one that it's actually using 7623 04:42:43,000 --> 04:42:47,520 underneath the hood is a little 7624 04:42:44,600 --> 04:42:49,480 something called clang for C language 7625 04:42:47,520 --> 04:42:51,320 and clang is a pretty popular compiler 7626 04:42:49,480 --> 04:42:53,920 nowadays there's another one that's been 7627 04:42:51,320 --> 04:42:56,080 around for ages called GCC but these are 7628 04:42:53,920 --> 04:42:57,280 just specific names for types of 7629 04:42:56,080 --> 04:42:58,718 compilers that different people 7630 04:42:57,280 --> 04:43:01,280 different companies different groups 7631 04:42:58,718 --> 04:43:04,760 have actually created but if you use in 7632 04:43:01,280 --> 04:43:06,040 week one a compiler yourself manually 7633 04:43:04,760 --> 04:43:07,560 you have to know you have to understand 7634 04:43:06,040 --> 04:43:09,600 a little more about what's going on 7635 04:43:07,560 --> 04:43:11,280 because it's even more cryptic than with 7636 04:43:09,600 --> 04:43:13,120 just make a loan so in fact let me go 7637 04:43:11,280 --> 04:43:14,638 back to my terminal window here let me 7638 04:43:13,120 --> 04:43:18,560 go ahead and clear the screen a little 7639 04:43:14,638 --> 04:43:20,760 bit and just run really the raw compiler 7640 04:43:18,560 --> 04:43:22,638 command so what make is automating for 7641 04:43:20,760 --> 04:43:26,320 me let me actually do this manually for 7642 04:43:22,638 --> 04:43:29,718 just a moment so if I want to compile uh 7643 04:43:26,320 --> 04:43:35,120 hello.c into an executable program I can 7644 04:43:29,718 --> 04:43:38,400 run I can do this uh clang space hello 7645 04:43:35,120 --> 04:43:39,878 C and then enter and now there's no 7646 04:43:38,400 --> 04:43:42,320 output which is a good thing in this 7647 04:43:39,878 --> 04:43:46,120 case no errors but notice this if I go 7648 04:43:42,320 --> 04:43:49,000 ahead and type LS it turns out there's a 7649 04:43:46,120 --> 04:43:50,958 uh a file that's been created suddenly 7650 04:43:49,000 --> 04:43:53,360 in my current folder weirdly called 7651 04:43:50,958 --> 04:43:55,000 a.out that stands for assembler output 7652 04:43:53,360 --> 04:43:57,520 and long story short that's actually the 7653 04:43:55,000 --> 04:43:59,440 default name of a program that's created 7654 04:43:57,520 --> 04:44:01,638 when you just run C by itself now that's 7655 04:43:59,440 --> 04:44:03,360 a pretty uh bad name for a program 7656 04:44:01,638 --> 04:44:06,480 because it doesn't describe what it said 7657 04:44:03,360 --> 04:44:09,840 does so better would be here to perhaps 7658 04:44:06,480 --> 04:44:12,120 do well instead of a.out which yes still 7659 04:44:09,840 --> 04:44:14,440 prints hello. world but isn't really a a 7660 04:44:12,120 --> 04:44:16,000 clearly named program it'd be nice to 7661 04:44:14,440 --> 04:44:17,440 name this hello so what could I do I 7662 04:44:16,000 --> 04:44:19,920 could do like we learned last week well 7663 04:44:17,440 --> 04:44:22,440 I could rename a.out to Hello by using 7664 04:44:19,920 --> 04:44:25,000 linux's MV command so I'm going to move 7665 04:44:22,440 --> 04:44:27,040 a. out to become hello but that too 7666 04:44:25,000 --> 04:44:29,638 seems kind of tedious now I have three 7667 04:44:27,040 --> 04:44:31,440 steps like write my code compile my code 7668 04:44:29,638 --> 04:44:33,520 and then rename it before I can even run 7669 04:44:31,440 --> 04:44:35,400 it like we can do better than that and 7670 04:44:33,520 --> 04:44:37,480 so it turns out that certain commands 7671 04:44:35,400 --> 04:44:39,440 like clang support what we're going to 7672 04:44:37,480 --> 04:44:42,360 start today calling commandline 7673 04:44:39,440 --> 04:44:44,360 arguments a commandline argument unlike 7674 04:44:42,360 --> 04:44:46,878 an argument to a function is just an 7675 04:44:44,360 --> 04:44:49,638 additional word or key phrase that you 7676 04:44:46,878 --> 04:44:51,600 type after a command at your prompt in 7677 04:44:49,638 --> 04:44:53,280 your terminal window that just modifies 7678 04:44:51,600 --> 04:44:55,400 the behavior of that command it 7679 04:44:53,280 --> 04:44:56,680 configures it a little more specifically 7680 04:44:55,400 --> 04:44:58,760 so what you're seeing here on the screen 7681 04:44:56,680 --> 04:45:00,958 is a summary of a better command with 7682 04:44:58,760 --> 04:45:04,958 which to run clang so that now I can 7683 04:45:00,958 --> 04:45:06,638 specify the output of this command this- 7684 04:45:04,958 --> 04:45:07,920 o so what do I mean by that well let me 7685 04:45:06,638 --> 04:45:11,480 go ahead and clear my terminal window 7686 04:45:07,920 --> 04:45:15,440 again and more explicitly type clang d o 7687 04:45:11,480 --> 04:45:16,798 hello hello. C and then enter nothing 7688 04:45:15,440 --> 04:45:18,520 again appears to happen but that's a 7689 04:45:16,798 --> 04:45:21,080 good thing when you see no errors and 7690 04:45:18,520 --> 04:45:24,160 now the program I just created is indeed 7691 04:45:21,080 --> 04:45:27,040 called hello so it achieves really the 7692 04:45:24,160 --> 04:45:28,840 same exact effect as make did but what I 7693 04:45:27,040 --> 04:45:30,400 don't have to do with make is type and 7694 04:45:28,840 --> 04:45:32,000 remember something as long as this 7695 04:45:30,400 --> 04:45:34,638 command and this too is a bit of a white 7696 04:45:32,000 --> 04:45:37,840 lie it turns out we have preconfigured 7697 04:45:34,638 --> 04:45:40,280 vs code in the cloud for you to also use 7698 04:45:37,840 --> 04:45:42,000 some other features of clang that would 7699 04:45:40,280 --> 04:45:44,160 be even more tedious for you to write 7700 04:45:42,000 --> 04:45:47,200 yourselves and so really this is why we 7701 04:45:44,160 --> 04:45:49,760 distill this as ultimately just running 7702 04:45:47,200 --> 04:45:51,680 make so let me pause here to see first 7703 04:45:49,760 --> 04:45:53,798 if there's any questions on what I've 7704 04:45:51,680 --> 04:45:56,240 done by taking my very first program in 7705 04:45:53,798 --> 04:45:57,840 C and just now compiling it first with 7706 04:45:56,240 --> 04:46:00,440 make but then starting over and now 7707 04:45:57,840 --> 04:46:02,718 manually compiling it with clang with 7708 04:46:00,440 --> 04:46:06,360 what we'll call command line arguments - 7709 04:46:02,718 --> 04:46:08,200 o space hello and then the name of the 7710 04:46:06,360 --> 04:46:11,680 file 7711 04:46:08,200 --> 04:46:14,120 yeah yeah so a.out is a historical name 7712 04:46:11,680 --> 04:46:16,360 it refers to assembler output more on 7713 04:46:14,120 --> 04:46:18,080 that soon and it's just the default file 7714 04:46:16,360 --> 04:46:20,840 name that you get automatically if you 7715 04:46:18,080 --> 04:46:22,560 just run the compiler on any file so 7716 04:46:20,840 --> 04:46:24,320 that you have just a standard name for 7717 04:46:22,560 --> 04:46:26,120 it but it's not a very well-named 7718 04:46:24,320 --> 04:46:27,958 program instead of running Microsoft 7719 04:46:26,120 --> 04:46:30,320 Word on your Mac or PC it would like be 7720 04:46:27,958 --> 04:46:31,798 like double clicking on a.out so instead 7721 04:46:30,320 --> 04:46:34,200 with these command line arguments you 7722 04:46:31,798 --> 04:46:37,558 can customize the output of clang and 7723 04:46:34,200 --> 04:46:39,160 call it hello or anything you want other 7724 04:46:37,558 --> 04:46:42,040 questions on what I've done here with 7725 04:46:39,160 --> 04:46:44,440 clang itself the 7726 04:46:42,040 --> 04:46:47,440 compiler 7727 04:46:44,440 --> 04:46:49,240 yeah so- o and you would only know this 7728 04:46:47,440 --> 04:46:53,280 from reading the manual taking a class 7729 04:46:49,240 --> 04:46:56,280 means output so- o means change clangs 7730 04:46:53,280 --> 04:46:58,760 output to be a file called hello instead 7731 04:46:56,280 --> 04:47:00,958 of the default which is a.out and this 7732 04:46:58,760 --> 04:47:02,480 too is again a detail you would have to 7733 04:47:00,958 --> 04:47:03,958 uh look it up on a web page read the 7734 04:47:02,480 --> 04:47:05,760 manual hear someone like me tell you 7735 04:47:03,958 --> 04:47:07,000 about it and in fact there's even more 7736 04:47:05,760 --> 04:47:09,840 than these options but we'll just 7737 04:47:07,000 --> 04:47:12,040 scratch the surface here all right so if 7738 04:47:09,840 --> 04:47:13,600 we now know this what more is actually 7739 04:47:12,040 --> 04:47:16,080 happening underneath the hood well let's 7740 04:47:13,600 --> 04:47:18,798 take a a closer look at not just this 7741 04:47:16,080 --> 04:47:21,120 version of my code but my slightly more 7742 04:47:18,798 --> 04:47:22,760 complicated version last week which 7743 04:47:21,120 --> 04:47:25,240 looked a little something like this 7744 04:47:22,760 --> 04:47:26,760 wherein I added in some Dynamic input 7745 04:47:25,240 --> 04:47:29,160 from the user so I could say not Hello 7746 04:47:26,760 --> 04:47:30,958 World to everyone but hello David or 7747 04:47:29,160 --> 04:47:33,200 hello to whoever actually runs this 7748 04:47:30,958 --> 04:47:35,878 program so in fact let me go ahead and 7749 04:47:33,200 --> 04:47:37,760 change my code here in vs code just to 7750 04:47:35,878 --> 04:47:40,000 match that same code from last week so 7751 04:47:37,760 --> 04:47:41,920 no new code yet I'm just going to in a 7752 04:47:40,000 --> 04:47:44,840 moment compile it in a slightly 7753 04:47:41,920 --> 04:47:47,958 different way so I did last week string 7754 04:47:44,840 --> 04:47:50,320 uh I think answer equals get string 7755 04:47:47,958 --> 04:47:53,000 quote unquote what's your name just like 7756 04:47:50,320 --> 04:47:55,920 in scratch and then down here instead of 7757 04:47:53,000 --> 04:47:58,160 doing world I initially wrote answer but 7758 04:47:55,920 --> 04:48:00,558 that didn't go well what did I 7759 04:47:58,160 --> 04:48:04,840 ultimately do instead to print out hello 7760 04:48:00,558 --> 04:48:07,520 David or hello so and so yeah sorry a 7761 04:48:04,840 --> 04:48:09,718 little louder yeah so percent s the 7762 04:48:07,520 --> 04:48:11,480 so-called format code that printf just 7763 04:48:09,718 --> 04:48:13,718 knows how to deal with and I had to add 7764 04:48:11,480 --> 04:48:14,920 one other thing someone else besides 7765 04:48:13,718 --> 04:48:17,040 percent F 7766 04:48:14,920 --> 04:48:19,360 yeah the name of the variable that I 7767 04:48:17,040 --> 04:48:21,480 want to plug into that placeholder 7768 04:48:19,360 --> 04:48:23,040 percent s and in this case it's answer 7769 04:48:21,480 --> 04:48:24,480 now let me make one refinement only 7770 04:48:23,040 --> 04:48:25,798 because now we're in week two and we're 7771 04:48:24,480 --> 04:48:27,798 going to start writing more lines of 7772 04:48:25,798 --> 04:48:30,240 code even though scratch called the 7773 04:48:27,798 --> 04:48:32,400 return value of the ask puzzle piece 7774 04:48:30,240 --> 04:48:33,760 answer always and see we have full 7775 04:48:32,400 --> 04:48:35,878 control over what our variables are 7776 04:48:33,760 --> 04:48:37,958 called and now it's probably good not to 7777 04:48:35,878 --> 04:48:40,000 just generically always call my variable 7778 04:48:37,958 --> 04:48:41,718 answer if I'm using get string let's 7779 04:48:40,000 --> 04:48:43,798 call it what it is so this is now just a 7780 04:48:41,718 --> 04:48:46,400 matter of style if you will let me 7781 04:48:43,798 --> 04:48:48,520 change the variable to be name just so 7782 04:48:46,400 --> 04:48:51,520 that it's a little clear to me to you to 7783 04:48:48,520 --> 04:48:53,120 a TF or ta exactly what that variable 7784 04:48:51,520 --> 04:48:55,600 represents instead of more generically 7785 04:48:53,120 --> 04:48:57,520 answer all right so that said let me go 7786 04:48:55,600 --> 04:48:59,718 down to my terminal window and last week 7787 04:48:57,520 --> 04:49:02,080 again I ran make to compile this exact 7788 04:48:59,718 --> 04:49:05,480 same program now though let me go ahead 7789 04:49:02,080 --> 04:49:08,160 and just use clang so clang d o I'll 7790 04:49:05,480 --> 04:49:10,798 still call this version hello space 7791 04:49:08,160 --> 04:49:12,320 hello.c so exact same command as before 7792 04:49:10,798 --> 04:49:14,638 the only thing that's different is I've 7793 04:49:12,320 --> 04:49:17,558 added a couple of more lines of code to 7794 04:49:14,638 --> 04:49:19,958 get the user's input let me hit enter 7795 04:49:17,558 --> 04:49:22,160 and now darn it our first error so 7796 04:49:19,958 --> 04:49:23,558 output from clang and make is not a good 7797 04:49:22,160 --> 04:49:26,080 thing and here we're seeing something 7798 04:49:23,558 --> 04:49:29,480 particularly cryptic uh so something in 7799 04:49:26,080 --> 04:49:32,000 function main undefined reference to get 7800 04:49:29,480 --> 04:49:33,718 string and then Linker command failed 7801 04:49:32,000 --> 04:49:35,520 with exit code one so there's actually a 7802 04:49:33,718 --> 04:49:38,200 lot of jargon in there that will tease 7803 04:49:35,520 --> 04:49:39,558 apart today but my hint is that clearly 7804 04:49:38,200 --> 04:49:40,718 my problem's in Maine although that's 7805 04:49:39,558 --> 04:49:42,878 not surprising because there's nothing 7806 04:49:40,718 --> 04:49:45,120 else going on here get string is an 7807 04:49:42,878 --> 04:49:48,240 issue and the uh issue is that it's an 7808 04:49:45,120 --> 04:49:51,320 undefined reference and yet notice I was 7809 04:49:48,240 --> 04:49:52,958 pretty good I added the cs50 header file 7810 04:49:51,320 --> 04:49:55,040 and I said last week that that's enough 7811 04:49:52,958 --> 04:49:57,520 to teach the compiler that functions 7812 04:49:55,040 --> 04:50:00,320 exist but the problem is that even 7813 04:49:57,520 --> 04:50:02,920 though this does in fact teach clang 7814 04:50:00,320 --> 04:50:05,280 that get string exists it is not 7815 04:50:02,920 --> 04:50:07,400 sufficient information for clang to go 7816 04:50:05,280 --> 04:50:09,280 find on the hard drive of the computer 7817 04:50:07,400 --> 04:50:12,160 the zeros and ones that actually 7818 04:50:09,280 --> 04:50:14,120 Implement get string itself so in other 7819 04:50:12,160 --> 04:50:15,958 words this include line per last week is 7820 04:50:14,120 --> 04:50:18,240 a little bit of a hint it's a teaser to 7821 04:50:15,958 --> 04:50:19,920 CLA that you're about to see and use 7822 04:50:18,240 --> 04:50:23,240 this function somewhere but if you 7823 04:50:19,920 --> 04:50:26,320 actually want to use the zeros and ones 7824 04:50:23,240 --> 04:50:28,200 that cs50 wrote some time ago and bake 7825 04:50:26,320 --> 04:50:29,840 those into your program so your program 7826 04:50:28,200 --> 04:50:32,320 actually knows how to get input from the 7827 04:50:29,840 --> 04:50:34,638 user well then I'm going to have to go 7828 04:50:32,320 --> 04:50:36,320 ahead and run a slightly different 7829 04:50:34,638 --> 04:50:37,638 command so let me do this let me clear 7830 04:50:36,320 --> 04:50:40,080 my terminal window just to get rid of 7831 04:50:37,638 --> 04:50:43,400 that distraction and let me propose now 7832 04:50:40,080 --> 04:50:47,240 that we run this command instead almost 7833 04:50:43,400 --> 04:50:50,080 the same as before clang - o space hello 7834 04:50:47,240 --> 04:50:52,480 then hello. C but with one additional 7835 04:50:50,080 --> 04:50:56,160 command line argument at the end and 7836 04:50:52,480 --> 04:50:59,400 this is a-h L not a number one so- L 7837 04:50:56,160 --> 04:51:01,840 cs50 with no space in between those two 7838 04:50:59,400 --> 04:51:03,400 now the L is going to result in all of 7839 04:51:01,840 --> 04:51:06,360 those zeros and ones that actually were 7840 04:51:03,400 --> 04:51:08,840 written by cs50 being linked into your 7841 04:51:06,360 --> 04:51:10,798 code your few lines of code or mine here 7842 04:51:08,840 --> 04:51:13,280 but that's the second step that the 7843 04:51:10,798 --> 04:51:16,558 compiler requires in order to know how 7844 04:51:13,280 --> 04:51:19,320 to actually execute and rather compile 7845 04:51:16,558 --> 04:51:21,280 your code and cs50's and cs50 is not the 7846 04:51:19,320 --> 04:51:23,440 only one that does this if you use any 7847 04:51:21,280 --> 04:51:26,400 third-party library in C that doesn't 7848 04:51:23,440 --> 04:51:28,520 come with the language you would do- L 7849 04:51:26,400 --> 04:51:30,000 such and such where whoever however 7850 04:51:28,520 --> 04:51:31,400 they've named their own library but you 7851 04:51:30,000 --> 04:51:34,240 don't have to do it for built-in things 7852 04:51:31,400 --> 04:51:35,480 like uh like we've been using thus far 7853 04:51:34,240 --> 04:51:37,718 all right so let me go ahead and try 7854 04:51:35,480 --> 04:51:41,520 this I'll go back to vs code here and 7855 04:51:37,718 --> 04:51:44,120 let me go ahead now and run clang - o 7856 04:51:41,520 --> 04:51:46,840 hello then hello. C and now instead of 7857 04:51:44,120 --> 04:51:49,600 just hitting enter - L cs50 with no 7858 04:51:46,840 --> 04:51:51,600 space between the L and the cs50 enter 7859 04:51:49,600 --> 04:51:54,480 now nothing bad happens and now I can 7860 04:51:51,600 --> 04:51:57,718 do/ hello what's your name I'll type in 7861 04:51:54,480 --> 04:51:59,080 David enter and now we see hello David 7862 04:51:57,718 --> 04:52:01,240 now honestly this is where we're really 7863 04:51:59,080 --> 04:52:04,080 getting into the weeds and now this is 7864 04:52:01,240 --> 04:52:05,480 taking this is really just adding new to 7865 04:52:04,080 --> 04:52:07,878 the process of compiling and running 7866 04:52:05,480 --> 04:52:09,920 your code and so the reality is even 7867 04:52:07,878 --> 04:52:11,520 though this is indeed what is happening 7868 04:52:09,920 --> 04:52:13,600 this is why we used last week and we're 7869 04:52:11,520 --> 04:52:15,760 going to continue using this week onward 7870 04:52:13,600 --> 04:52:18,160 make because it just automates that 7871 04:52:15,760 --> 04:52:19,280 whole process for you but it's ideal to 7872 04:52:18,160 --> 04:52:20,760 understand what's going wrong because 7873 04:52:19,280 --> 04:52:22,280 any of the error messages you saw for 7874 04:52:20,760 --> 04:52:24,120 problem set one any of the error 7875 04:52:22,280 --> 04:52:26,000 messages you see for the next few weeks 7876 04:52:24,120 --> 04:52:28,480 probably aren't coming from make they're 7877 04:52:26,000 --> 04:52:30,160 coming from clang underneath the hood 7878 04:52:28,480 --> 04:52:31,718 because make is just automating the 7879 04:52:30,160 --> 04:52:33,120 process but with make you literally just 7880 04:52:31,718 --> 04:52:34,840 write make and then the name of the 7881 04:52:33,120 --> 04:52:37,558 program you don't have to worry about 7882 04:52:34,840 --> 04:52:41,480 any of those command line arguments 7883 04:52:37,558 --> 04:52:43,638 questions then on compiling with- l cs50 7884 04:52:41,480 --> 04:52:46,320 or anything else 7885 04:52:43,638 --> 04:52:49,120 yeah sorry what is the benefit 7886 04:52:46,320 --> 04:52:51,680 of what is the benefit of using clang 7887 04:52:49,120 --> 04:52:53,600 manually none really in fact all main is 7888 04:52:51,680 --> 04:52:55,798 doing is just sa make is doing is saving 7889 04:52:53,600 --> 04:52:57,120 us some keystrokes um if you prefer 7890 04:52:55,798 --> 04:52:58,520 though and you just like to be more in 7891 04:52:57,120 --> 04:53:00,400 control you can totally run clang 7892 04:52:58,520 --> 04:53:05,558 manually if you remember the various 7893 04:53:00,400 --> 04:53:05,558 command line arguments yeah 7894 04:53:06,600 --> 04:53:11,680 exp exactly why did I have to explain 7895 04:53:09,320 --> 04:53:13,680 that is provide a hint to cs50 with the 7896 04:53:11,680 --> 04:53:16,160 cs50.h henter file but I didn't have to 7897 04:53:13,680 --> 04:53:19,200 do that with standard i.h just because 7898 04:53:16,160 --> 04:53:21,280 standard i.h comes with C just like a 7899 04:53:19,200 --> 04:53:23,600 few other libraries come with C that 7900 04:53:21,280 --> 04:53:25,280 we'll start seeing today um cs50 though 7901 04:53:23,600 --> 04:53:27,360 is not built into C everywhere and so 7902 04:53:25,280 --> 04:53:29,878 you do have to explicitly add that one 7903 04:53:27,360 --> 04:53:29,878 there 7904 04:53:30,760 --> 04:53:36,958 yeah a command line argument is a a word 7905 04:53:34,120 --> 04:53:40,360 or phrase that you type at the command 7906 04:53:36,958 --> 04:53:42,480 line AKA your terminal in order to 7907 04:53:40,360 --> 04:53:44,320 influence the behavior of a 7908 04:53:42,480 --> 04:53:45,760 program for whatever you're doing yeah 7909 04:53:44,320 --> 04:53:47,638 it changes the defaults right in our 7910 04:53:45,760 --> 04:53:49,600 guey World graphical user interface you 7911 04:53:47,638 --> 04:53:51,000 and I would probably click some boxes we 7912 04:53:49,600 --> 04:53:52,760 would select some menu options to 7913 04:53:51,000 --> 04:53:54,878 configure a program to behave in the 7914 04:53:52,760 --> 04:53:56,680 same way at a command line interface you 7915 04:53:54,878 --> 04:53:58,638 have to just say everything all at once 7916 04:53:56,680 --> 04:54:01,638 and that's why we have command line 7917 04:53:58,638 --> 04:54:01,638 arguments 7918 04:54:02,320 --> 04:54:08,280 yeah no make is not just for cs50 it's 7919 04:54:05,400 --> 04:54:10,760 used globally in any project really 7920 04:54:08,280 --> 04:54:12,680 nowadays using C C++ even other 7921 04:54:10,760 --> 04:54:14,360 languages as well in fact most every 7922 04:54:12,680 --> 04:54:16,798 command you see in this class unless it 7923 04:54:14,360 --> 04:54:19,558 has 5 zero at the end of it is globally 7924 04:54:16,798 --> 04:54:21,160 used only those suffix with 50 are 7925 04:54:19,558 --> 04:54:22,798 indeed course specific and even those 7926 04:54:21,160 --> 04:54:24,638 will gradually take training wheels off 7927 04:54:22,798 --> 04:54:27,520 of so that you know exactly what those 7928 04:54:24,638 --> 04:54:29,120 commands are doing as well all right so 7929 04:54:27,520 --> 04:54:30,400 what is it that we've just done 7930 04:54:29,120 --> 04:54:32,120 everything we've just done of course I 7931 04:54:30,400 --> 04:54:33,920 keep calling compiling but let's just go 7932 04:54:32,120 --> 04:54:35,760 down one Rabbit Hole so that you 7933 04:54:33,920 --> 04:54:37,320 understand that when you compile code 7934 04:54:35,760 --> 04:54:39,558 there's actually a whole bunch of steps 7935 04:54:37,320 --> 04:54:41,600 happening and this is going to enable uh 7936 04:54:39,558 --> 04:54:44,160 a lot of features like companies can 7937 04:54:41,600 --> 04:54:46,760 write code and then convert it to run it 7938 04:54:44,160 --> 04:54:48,240 on Macs and PCs alike or phones or the 7939 04:54:46,760 --> 04:54:50,200 like so it's not just a matter of 7940 04:54:48,240 --> 04:54:52,680 converting source code to machine code 7941 04:54:50,200 --> 04:54:54,958 there's actually four steps involved in 7942 04:54:52,680 --> 04:54:56,718 what you and I as of last week know as 7943 04:54:54,958 --> 04:54:59,000 compiling and these aren't terms that 7944 04:54:56,718 --> 04:55:00,360 you'll have to keep in mind constantly 7945 04:54:59,000 --> 04:55:02,160 because again we're going to abstract a 7946 04:55:00,360 --> 04:55:03,958 lot of this away but just so we've gone 7947 04:55:02,160 --> 04:55:05,958 down the rabbit hole once let's consider 7948 04:55:03,958 --> 04:55:07,958 each of these four steps that have been 7949 04:55:05,958 --> 04:55:09,760 happening for you for a week 7950 04:55:07,958 --> 04:55:11,680 automatically uh the first of which is 7951 04:55:09,760 --> 04:55:13,080 called pre-processing so what is this 7952 04:55:11,680 --> 04:55:15,440 mean well let's consider that same 7953 04:55:13,080 --> 04:55:17,798 program as before notice that a two of 7954 04:55:15,440 --> 04:55:20,120 the lines of code start with a hash mark 7955 04:55:17,798 --> 04:55:22,400 that is a special symbol in C and it's a 7956 04:55:20,120 --> 04:55:24,080 so-called pre-processor directive you 7957 04:55:22,400 --> 04:55:25,280 don't need to memorize terms like that 7958 04:55:24,080 --> 04:55:26,680 but it just means that it's a little 7959 04:55:25,280 --> 04:55:28,718 different from every other line and 7960 04:55:26,680 --> 04:55:30,878 anything with a hash symbol here should 7961 04:55:28,718 --> 04:55:33,000 be pre-processed that is analyzed 7962 04:55:30,878 --> 04:55:35,160 initially before anything else happens 7963 04:55:33,000 --> 04:55:37,400 so let's consider these two lines up top 7964 04:55:35,160 --> 04:55:39,600 what exactly is happening well it turns 7965 04:55:37,400 --> 04:55:41,718 out with these two lines you have two 7966 04:55:39,600 --> 04:55:44,718 header files of course cs50.h and 7967 04:55:41,718 --> 04:55:47,240 standard. i.h where are those files 7968 04:55:44,718 --> 04:55:49,600 because they've never been in VSS code 7969 04:55:47,240 --> 04:55:51,958 for you seemingly if you type LS if you 7970 04:55:49,600 --> 04:55:54,480 open up the file explorer in the GUI you 7971 04:55:51,958 --> 04:55:57,480 have never seen probably cs50.h or 7972 04:55:54,480 --> 04:56:00,160 standard i.h they just work but that's 7973 04:55:57,480 --> 04:56:01,958 because there's a folder somewhere on 7974 04:56:00,160 --> 04:56:03,920 the uh the hard drive that you're using 7975 04:56:01,958 --> 04:56:06,360 on your Mac or PC or somewhere in the 7976 04:56:03,920 --> 04:56:09,240 cloud as in our case and inside of this 7977 04:56:06,360 --> 04:56:11,320 folder traditionally called sluser SL 7978 04:56:09,240 --> 04:56:12,920 include and user is deliberately 7979 04:56:11,320 --> 04:56:14,240 misspelled it's just slightly more 7980 04:56:12,920 --> 04:56:16,638 succinct although it's a little weird 7981 04:56:14,240 --> 04:56:19,000 why we drop that one letter but user SL 7982 04:56:16,638 --> 04:56:21,878 include is just a folder on the server 7983 04:56:19,000 --> 04:56:24,200 that contains cs50.h standard i.h and a 7984 04:56:21,878 --> 04:56:26,480 bunch of other things as well so in fact 7985 04:56:24,200 --> 04:56:28,600 if you type in uh VSS code in your 7986 04:56:26,480 --> 04:56:31,000 terminal window uh when you're using 7987 04:56:28,600 --> 04:56:33,638 Code spaces in the cloud and type LS 7988 04:56:31,000 --> 04:56:35,320 space SL user include you can can see 7989 04:56:33,638 --> 04:56:36,760 all of the files in that folder but 7990 04:56:35,320 --> 04:56:38,520 we've pre-installed all of that stuff 7991 04:56:36,760 --> 04:56:40,440 for you so let's consider what's 7992 04:56:38,520 --> 04:56:42,878 actually in those files here where if I 7993 04:56:40,440 --> 04:56:46,000 highlight these two lines up top that 7994 04:56:42,878 --> 04:56:47,558 start with hash include well I kind of 7995 04:56:46,000 --> 04:56:50,360 hinted last week that what's in that 7996 04:56:47,558 --> 04:56:53,200 first file is a hint as to what 7997 04:56:50,360 --> 04:56:55,638 functions cs50 wrote for you so you can 7998 04:56:53,200 --> 04:56:58,080 kind of think of these include lines as 7999 04:56:55,638 --> 04:57:00,120 being temporary placeholders for what's 8000 04:56:58,080 --> 04:57:02,120 going to become like a global find and 8001 04:57:00,120 --> 04:57:04,200 replace that is the first thing clang is 8002 04:57:02,120 --> 04:57:05,440 going to do it's pre-process this file 8003 04:57:04,200 --> 04:57:08,040 it's going to look for any line that 8004 04:57:05,440 --> 04:57:10,120 starts with hash include and if it sees 8005 04:57:08,040 --> 04:57:13,200 that it's going to essentially go into 8006 04:57:10,120 --> 04:57:15,160 that file like cs50.h and then just copy 8007 04:57:13,200 --> 04:57:16,920 and paste the contents of that file 8008 04:57:15,160 --> 04:57:18,160 magically there for you you don't see it 8009 04:57:16,920 --> 04:57:20,440 visually on the screen but it's 8010 04:57:18,160 --> 04:57:22,558 happening behind the scenes and so 8011 04:57:20,440 --> 04:57:25,360 really what's happening with this first 8012 04:57:22,558 --> 04:57:29,200 line is that somewhere in 8013 04:57:25,360 --> 04:57:31,440 cs50.h is the Declaration of get string 8014 04:57:29,200 --> 04:57:33,280 like we talked last week and it probably 8015 04:57:31,440 --> 04:57:34,760 looks a little something like this and 8016 04:57:33,280 --> 04:57:37,000 we didn't spend much time on this yet 8017 04:57:34,760 --> 04:57:40,320 this past week but we will in time more 8018 04:57:37,000 --> 04:57:42,840 notice that this is how the a function 8019 04:57:40,320 --> 04:57:44,718 is declared that is it is decreed to 8020 04:57:42,840 --> 04:57:46,840 exist the name of the function of course 8021 04:57:44,718 --> 04:57:48,958 is get string inside of the parenthesis 8022 04:57:46,840 --> 04:57:51,400 are its arguments in this case there's 8023 04:57:48,958 --> 04:57:53,120 one argument to get string I claim today 8024 04:57:51,400 --> 04:57:54,798 but you've known this implicitly and 8025 04:57:53,120 --> 04:57:56,878 it's a prompt it's the prompt that the 8026 04:57:54,798 --> 04:57:58,320 human sees when you use get string what 8027 04:57:56,878 --> 04:58:00,798 is that prompt well it's a string of 8028 04:57:58,320 --> 04:58:02,840 text like quote unquote what's your name 8029 04:58:00,798 --> 04:58:04,400 or anything else that I asked last week 8030 04:58:02,840 --> 04:58:06,680 mean meanwhile get string as we know 8031 04:58:04,400 --> 04:58:08,760 from last week has a return value it 8032 04:58:06,680 --> 04:58:10,680 returns something to you and that too is 8033 04:58:08,760 --> 04:58:12,520 a string so again this is also called a 8034 04:58:10,680 --> 04:58:14,160 functions prototype it's the thing 8035 04:58:12,520 --> 04:58:16,080 toward the end of last week that I just 8036 04:58:14,160 --> 04:58:18,280 copied and pasted from the bottom of my 8037 04:58:16,080 --> 04:58:20,798 file to the top just so that it was like 8038 04:58:18,280 --> 04:58:23,040 this teaser for clang as to what would 8039 04:58:20,798 --> 04:58:26,638 exist later so you can think then of 8040 04:58:23,040 --> 04:58:28,320 these include lines as just kind of uh 8041 04:58:26,638 --> 04:58:29,958 combining all of those function 8042 04:58:28,320 --> 04:58:32,440 declarations in some separate file 8043 04:58:29,958 --> 04:58:34,120 called cs50.h so that you yourself don't 8044 04:58:32,440 --> 04:58:35,840 have to type them every time you use the 8045 04:58:34,120 --> 04:58:38,320 library or worse so that you yourself 8046 04:58:35,840 --> 04:58:39,840 don't have to copy and paste those lines 8047 04:58:38,320 --> 04:58:42,958 this is what clang is doing for you in 8048 04:58:39,840 --> 04:58:45,240 its first step of pre-processing second 8049 04:58:42,958 --> 04:58:48,120 and last in this example what happens 8050 04:58:45,240 --> 04:58:49,878 when clang pre-processes this second 8051 04:58:48,120 --> 04:58:51,480 include line well the only other 8052 04:58:49,878 --> 04:58:54,080 function we care about in this story is 8053 04:58:51,480 --> 04:58:56,958 printf of course which comes with C so 8054 04:58:54,080 --> 04:59:00,200 essentially you can think of printf's 8055 04:58:56,958 --> 04:59:02,558 prototype or Declaration as just being 8056 04:59:00,200 --> 04:59:04,840 this print f is the name of the function 8057 04:59:02,558 --> 04:59:07,760 it takes a string that you want to 8058 04:59:04,840 --> 04:59:10,080 format like hello comma world or hello 8059 04:59:07,760 --> 04:59:11,878 comma percent s and then with dot dot 8060 04:59:10,080 --> 04:59:13,638 dot this actually has technical meaning 8061 04:59:11,878 --> 04:59:16,360 it means of course that you can plug in 8062 04:59:13,638 --> 04:59:17,680 zero variables one variable two or 10 so 8063 04:59:16,360 --> 04:59:19,400 dot dot dot means some number of 8064 04:59:17,680 --> 04:59:21,400 variables now we haven't talked about 8065 04:59:19,400 --> 04:59:23,760 this yet and we won't really in general 8066 04:59:21,400 --> 04:59:26,000 print F actually returns a value a 8067 04:59:23,760 --> 04:59:27,718 number that is an integer but more on 8068 04:59:26,000 --> 04:59:30,160 that perhaps another time it's generally 8069 04:59:27,718 --> 04:59:31,878 not something the programmer tends to 8070 04:59:30,160 --> 04:59:33,440 look at but that's all we mean by 8071 04:59:31,878 --> 04:59:34,840 pre-processing so that the the end of 8072 04:59:33,440 --> 04:59:38,040 this process even though there's more 8073 04:59:34,840 --> 04:59:39,400 lines of code in cs50.h and standard i.h 8074 04:59:38,040 --> 04:59:42,480 what's really just happening is that 8075 04:59:39,400 --> 04:59:44,200 clang in pre-processing the file copies 8076 04:59:42,480 --> 04:59:46,240 and pastes the contents of those files 8077 04:59:44,200 --> 04:59:49,160 into your code so that now your code 8078 04:59:46,240 --> 04:59:51,958 knows about everything get string printf 8079 04:59:49,160 --> 04:59:54,240 and anything else any questions then on 8080 04:59:51,958 --> 04:59:57,240 that first step 8081 04:59:54,240 --> 04:59:57,240 pre-processing 8082 05:00:01,958 --> 05:00:04,958 yes 8083 05:00:09,480 --> 05:00:13,520 good question when you include a file 8084 05:00:11,400 --> 05:00:15,000 does it only include what you need or 8085 05:00:13,520 --> 05:00:16,718 Does it include everything think of it 8086 05:00:15,000 --> 05:00:18,480 as including everything so if it's a big 8087 05:00:16,718 --> 05:00:20,638 file that's a lot of code at the very 8088 05:00:18,480 --> 05:00:22,040 top and that's why if you think back to 8089 05:00:20,638 --> 05:00:23,760 all of the zeros and ones I showed a 8090 05:00:22,040 --> 05:00:25,760 little bit ago as well as last week 8091 05:00:23,760 --> 05:00:27,360 there's a lot of zeros and ones that end 8092 05:00:25,760 --> 05:00:29,440 up on the screen as a result of just 8093 05:00:27,360 --> 05:00:31,120 writing hello world a lot of those zeros 8094 05:00:29,440 --> 05:00:32,958 and ones are perhaps coming from code 8095 05:00:31,120 --> 05:00:34,958 that you didn't actually necessarily 8096 05:00:32,958 --> 05:00:37,680 need but some of it is perhaps there but 8097 05:00:34,958 --> 05:00:40,600 there are ways to optimize that as well 8098 05:00:37,680 --> 05:00:42,718 all right so step two of compiling is 8099 05:00:40,600 --> 05:00:44,280 confusingly called compiling it's just 8100 05:00:42,718 --> 05:00:46,638 this is the term that most everyone uses 8101 05:00:44,280 --> 05:00:49,200 to describe the whole process instead of 8102 05:00:46,638 --> 05:00:51,520 just this one step but once a program 8103 05:00:49,200 --> 05:00:54,320 has been pre-processed uh behind the 8104 05:00:51,520 --> 05:00:55,760 scenes by the compiler for you it looks 8105 05:00:54,320 --> 05:00:57,440 now a little something like this and 8106 05:00:55,760 --> 05:00:59,280 I've put dot dot dots just to imply that 8107 05:00:57,440 --> 05:01:00,520 yes to your question there's more stuff 8108 05:00:59,280 --> 05:01:02,280 above it there's more stuff below it 8109 05:01:00,520 --> 05:01:05,040 it's just not interesting right now for 8110 05:01:02,280 --> 05:01:07,000 us so now we have just C code there's no 8111 05:01:05,040 --> 05:01:08,840 more pre-processor directives at this 8112 05:01:07,000 --> 05:01:10,798 point all of the hash symbols and those 8113 05:01:08,840 --> 05:01:13,000 lines of code have been pre-processed 8114 05:01:10,798 --> 05:01:14,360 and convert it to something else and so 8115 05:01:13,000 --> 05:01:17,320 now and this is where things get a 8116 05:01:14,360 --> 05:01:20,440 little spooky looking uh here now is 8117 05:01:17,320 --> 05:01:23,360 what happens when clang or any compiler 8118 05:01:20,440 --> 05:01:27,120 literally compiles code like this it 8119 05:01:23,360 --> 05:01:29,480 converts it from this in C to this in 8120 05:01:27,120 --> 05:01:31,400 assembly code so this is among the 8121 05:01:29,480 --> 05:01:33,240 scarier languages I myself don't really 8122 05:01:31,400 --> 05:01:34,920 have fond memories this is not language 8123 05:01:33,240 --> 05:01:36,680 that many people program in if you take 8124 05:01:34,920 --> 05:01:38,958 a subsequent class in computer science 8125 05:01:36,680 --> 05:01:40,280 in systems uh a higher level class you 8126 05:01:38,958 --> 05:01:42,400 might actually learn this or some 8127 05:01:40,280 --> 05:01:43,798 variant there of but there's at least a 8128 05:01:42,400 --> 05:01:46,040 few people out there that need to know 8129 05:01:43,798 --> 05:01:48,798 this stuff because this is closer to 8130 05:01:46,040 --> 05:01:51,160 what the computers themselves nowadays 8131 05:01:48,798 --> 05:01:53,638 understand like the Intel CPUs or the 8132 05:01:51,160 --> 05:01:55,680 AMD CPUs the brains of today's computers 8133 05:01:53,638 --> 05:01:58,200 and phones understand stuff that looks 8134 05:01:55,680 --> 05:02:00,958 more like this and less like C now it's 8135 05:01:58,200 --> 05:02:02,878 completely uh esoteric but let me just 8136 05:02:00,958 --> 05:02:04,878 highlight a few phrases there's some 8137 05:02:02,878 --> 05:02:06,958 stuff that's a little familiar there is 8138 05:02:04,878 --> 05:02:08,840 mention of Maine at the top there in 8139 05:02:06,958 --> 05:02:10,360 yellow there is mention of get string 8140 05:02:08,840 --> 05:02:13,320 toward the bottom there is mention of 8141 05:02:10,360 --> 05:02:14,958 prf down below so this is just another 8142 05:02:13,320 --> 05:02:17,160 programming language called Assembly 8143 05:02:14,958 --> 05:02:20,280 Language that decades ago humans myself 8144 05:02:17,160 --> 05:02:22,080 included in school did write code in and 8145 05:02:20,280 --> 05:02:24,080 absolutely some people still write this 8146 05:02:22,080 --> 05:02:27,680 code especially since you can write very 8147 05:02:24,080 --> 05:02:30,400 very efficient code but it's a lot more 8148 05:02:27,680 --> 05:02:32,558 uh Arcane it's a lot more uh lot less 8149 05:02:30,400 --> 05:02:34,440 user friendly so you'll see in Yello now 8150 05:02:32,558 --> 05:02:36,600 the these are the so-called instructions 8151 05:02:34,440 --> 05:02:39,080 that a computer's brain or CPU 8152 05:02:36,600 --> 05:02:42,200 understands pushing values around moving 8153 05:02:39,080 --> 05:02:44,600 them subtracting values uh calling 8154 05:02:42,200 --> 05:02:46,718 functions and move move move so really 8155 05:02:44,600 --> 05:02:49,040 the low-level operations that computers 8156 05:02:46,718 --> 05:02:51,080 understand tend to be arithmetic 8157 05:02:49,040 --> 05:02:54,080 operations subtraction addition and the 8158 05:02:51,080 --> 05:02:55,718 like moving things in and out of memory 8159 05:02:54,080 --> 05:02:57,400 it's just a lot more tedious for folks 8160 05:02:55,718 --> 05:02:59,798 like us to write code like this this is 8161 05:02:57,400 --> 05:03:02,120 why you and I tend to write stuff like 8162 05:02:59,798 --> 05:03:03,558 this and ideally still people like you 8163 05:03:02,120 --> 05:03:05,440 and I tend to drag and drop puzzle 8164 05:03:03,558 --> 05:03:07,718 pieces that sort of abstract all of that 8165 05:03:05,440 --> 05:03:09,920 away further but for now this is again 8166 05:03:07,718 --> 05:03:12,760 called Assembly Language it is what 8167 05:03:09,920 --> 05:03:15,480 happens when the compiler literally 8168 05:03:12,760 --> 05:03:17,440 compiles your code but of course this 8169 05:03:15,480 --> 05:03:20,718 still not zeros and ones so we got two 8170 05:03:17,440 --> 05:03:23,920 steps to go so when a compiler proceeds 8171 05:03:20,718 --> 05:03:25,760 to step three this is where things get 8172 05:03:23,920 --> 05:03:28,600 converted to machine code and when a 8173 05:03:25,760 --> 05:03:30,718 compiler assembles your code for you it 8174 05:03:28,600 --> 05:03:34,160 converts what we just saw on the screen 8175 05:03:30,718 --> 05:03:36,240 here to actual zeros and ones the 8176 05:03:34,160 --> 05:03:38,920 so-called machine code that your phone 8177 05:03:36,240 --> 05:03:41,000 or your computer understands but it's 8178 05:03:38,920 --> 05:03:43,200 worth noting that these are not 8179 05:03:41,000 --> 05:03:46,400 necessarily all of the zeros and ones of 8180 05:03:43,200 --> 05:03:48,680 your program yes they re uh they are the 8181 05:03:46,400 --> 05:03:51,360 zeros and ones that correspond to your 8182 05:03:48,680 --> 05:03:54,920 hello program or printf and get string 8183 05:03:51,360 --> 05:03:57,920 and the like but notice that here we 8184 05:03:54,920 --> 05:04:00,120 need one final step in those zeros and 8185 05:03:57,920 --> 05:04:02,080 ones are only your lines of code but 8186 05:04:00,120 --> 05:04:03,638 what about cs50's lines of code that we 8187 05:04:02,080 --> 05:04:05,040 wrote to Implement get string what about 8188 05:04:03,638 --> 05:04:07,040 the lines of code that humans wrote 8189 05:04:05,040 --> 05:04:08,920 decades ago to implement printf those 8190 05:04:07,040 --> 05:04:11,160 are somewhere on this hard drive like on 8191 05:04:08,920 --> 05:04:13,280 my Mac my PC or somewhere in the cloud 8192 05:04:11,160 --> 05:04:17,320 but we need to combine all of those 8193 05:04:13,280 --> 05:04:20,840 zeros and ones together and Link My code 8194 05:04:17,320 --> 05:04:23,280 with cs50's code with uh standard io's 8195 05:04:20,840 --> 05:04:26,120 code all together and so what happens in 8196 05:04:23,280 --> 05:04:28,320 the last step ultimately is that if we 8197 05:04:26,120 --> 05:04:30,280 have my code here in yellow and then the 8198 05:04:28,320 --> 05:04:32,638 code that cs50 wrote and the code that 8199 05:04:30,280 --> 05:04:35,000 the authors of C itself wrote what 8200 05:04:32,638 --> 05:04:37,400 really is happening is that somewhere we 8201 05:04:35,000 --> 05:04:39,878 have not only hello.c which obviously I 8202 05:04:37,400 --> 05:04:42,000 wrote and wrote with us live here 8203 05:04:39,878 --> 05:04:45,160 there's also let's assume somewhere on 8204 05:04:42,000 --> 05:04:47,360 the computer a cs50.c file that 8205 05:04:45,160 --> 05:04:49,120 coincidentally I and cs50 staff wrote 8206 05:04:47,360 --> 05:04:50,798 years ago and also somewhere on the 8207 05:04:49,120 --> 05:04:52,840 computer there's another file Let Me 8208 05:04:50,798 --> 05:04:54,958 oversimplify by just calling it standard 8209 05:04:52,840 --> 05:04:56,920 io. C in practice it's probably 8210 05:04:54,958 --> 05:04:59,480 specifically called print F.C but 8211 05:04:56,920 --> 05:05:02,040 there's somewhere these two other files 8212 05:04:59,480 --> 05:05:04,558 and so this last step called linking 8213 05:05:02,040 --> 05:05:06,878 takes my zeros and ones from the code I 8214 05:05:04,558 --> 05:05:09,360 just wrote namely this code on the 8215 05:05:06,878 --> 05:05:11,240 screen here it then grabs the zeros and 8216 05:05:09,360 --> 05:05:12,920 ones that cs50 wrote and it grabs the 8217 05:05:11,240 --> 05:05:14,958 zeros and ones that the authors of C 8218 05:05:12,920 --> 05:05:19,638 wrote In order to implement the standard 8219 05:05:14,958 --> 05:05:21,600 IO library and lastly voila links them 8220 05:05:19,638 --> 05:05:23,920 all together and this is the same blob 8221 05:05:21,600 --> 05:05:26,520 of zeros and ones that we saw earlier 8222 05:05:23,920 --> 05:05:28,920 it's just now the result of 8223 05:05:26,520 --> 05:05:31,400 pre-processing your code compiling your 8224 05:05:28,920 --> 05:05:33,320 code assembling your code linking your 8225 05:05:31,400 --> 05:05:35,200 code and my God it's at this point like 8226 05:05:33,320 --> 05:05:37,760 if there were any fun in programming for 8227 05:05:35,200 --> 05:05:39,638 you yet we've just taken it all away we 8228 05:05:37,760 --> 05:05:41,798 just call this whole process compiling 8229 05:05:39,638 --> 05:05:44,080 why because now that we know those steps 8230 05:05:41,798 --> 05:05:45,840 exist and smart people solve that 8231 05:05:44,080 --> 05:05:47,760 problem for us you and I can kind of 8232 05:05:45,840 --> 05:05:49,558 operate at this level of abstraction and 8233 05:05:47,760 --> 05:05:52,798 just assume that compiling converts 8234 05:05:49,558 --> 05:05:54,320 source code to machine code questions 8235 05:05:52,798 --> 05:05:58,360 though on any of these 8236 05:05:54,320 --> 05:05:58,360 intermediate steps 8237 05:06:01,400 --> 05:06:04,400 yeah 8238 05:06:10,120 --> 05:06:13,480 a good question so where are all of 8239 05:06:11,958 --> 05:06:15,320 these zeros in one store because you and 8240 05:06:13,480 --> 05:06:17,480 I we've been using a browser at code. 8241 05:06:15,320 --> 05:06:19,240 cs50. of course is this web-based user 8242 05:06:17,480 --> 05:06:20,638 interface but again recall from last 8243 05:06:19,240 --> 05:06:24,120 week even though you're using a web 8244 05:06:20,638 --> 05:06:25,638 browser to access VSS code that 8245 05:06:24,120 --> 05:06:27,958 web-based version of vs code is 8246 05:06:25,638 --> 05:06:30,240 connected to an actual server somewhere 8247 05:06:27,958 --> 05:06:32,760 in the cloud and on that server you have 8248 05:06:30,240 --> 05:06:34,600 your own account and your own file and 8249 05:06:32,760 --> 05:06:36,080 really your own hard drive virtually in 8250 05:06:34,600 --> 05:06:38,558 the cloud think of it a little like 8251 05:06:36,080 --> 05:06:40,080 Dropbox or box or Google drive or one 8252 05:06:38,558 --> 05:06:41,600 drive or something like that so you have 8253 05:06:40,080 --> 05:06:43,718 a hard drive somewhere out there that 8254 05:06:41,600 --> 05:06:46,680 we've provisioned for you and it's on 8255 05:06:43,718 --> 05:06:49,040 that hard drive that we have uh your 8256 05:06:46,680 --> 05:06:52,200 code that you just wrote or I just wrote 8257 05:06:49,040 --> 05:06:53,920 cs50.c standard I.C and all of the other 8258 05:06:52,200 --> 05:06:55,920 code that implements the math functions 8259 05:06:53,920 --> 05:06:58,480 and everything else that c 8260 05:06:55,920 --> 05:07:01,320 supports good question 8261 05:06:58,480 --> 05:07:04,320 yeah 8262 05:07:01,320 --> 05:07:04,320 c 8263 05:07:12,320 --> 05:07:17,240 good question that uh hash includes 8264 05:07:14,638 --> 05:07:19,240 cs50.h line at the top of my code if I 8265 05:07:17,240 --> 05:07:22,760 just replace that with the contents of 8266 05:07:19,240 --> 05:07:24,360 cs50.c would that work short answer yes 8267 05:07:22,760 --> 05:07:26,160 that would work you could copy all of 8268 05:07:24,360 --> 05:07:28,200 the code there however there's some 8269 05:07:26,160 --> 05:07:29,798 order of operations that might come into 8270 05:07:28,200 --> 05:07:31,480 play and so it's probably not quite as 8271 05:07:29,798 --> 05:07:33,920 simple as copy paste but conceptually 8272 05:07:31,480 --> 05:07:35,440 yes that's what what's happening now 8273 05:07:33,920 --> 05:07:38,558 with that said in 8274 05:07:35,440 --> 05:07:41,240 cs50.h are only the prototypes of the 8275 05:07:38,558 --> 05:07:43,400 functions the hints as to how the 8276 05:07:41,240 --> 05:07:44,520 functions look what their return type is 8277 05:07:43,400 --> 05:07:47,840 what their name is and what their 8278 05:07:44,520 --> 05:07:49,920 arguments are it's in the C file that 8279 05:07:47,840 --> 05:07:51,360 actual code tends to be written and this 8280 05:07:49,920 --> 05:07:53,920 is a little confusing now because you 8281 05:07:51,360 --> 05:07:55,120 and I have only written code in C files 8282 05:07:53,920 --> 05:07:57,280 but in the next few weeks you'll 8283 05:07:55,120 --> 05:07:59,360 actually start writing some of your own 8284 05:07:57,280 --> 05:08:01,600 files as well just like cs50 just like 8285 05:07:59,360 --> 05:08:03,480 standard iio but in essence that line of 8286 05:08:01,600 --> 05:08:05,680 code just makes it easier to use and 8287 05:08:03,480 --> 05:08:08,878 reuse code that's already been written 8288 05:08:05,680 --> 05:08:08,878 and that's the whole point of a 8289 05:08:09,520 --> 05:08:13,120 library I say that little 8290 05:08:13,718 --> 05:08:19,200 louder yes does linking happen when you 8291 05:08:16,000 --> 05:08:22,240 compile your code yes when you run make 8292 05:08:19,200 --> 05:08:24,440 as we have been doing the past week now 8293 05:08:22,240 --> 05:08:26,360 all four of these steps are happening 8294 05:08:24,440 --> 05:08:28,680 pre-processing converts the hash include 8295 05:08:26,360 --> 05:08:30,680 lines to something else compiling 8296 05:08:28,680 --> 05:08:32,760 technically converts it to assembly code 8297 05:08:30,680 --> 05:08:35,240 which the Mac the PC the server more 8298 05:08:32,760 --> 05:08:37,760 closely understands assembly converts 8299 05:08:35,240 --> 05:08:40,000 that language to Binary machine code 8300 05:08:37,760 --> 05:08:42,000 that this computer actually understands 8301 05:08:40,000 --> 05:08:43,600 and then linking combines everything 8302 05:08:42,000 --> 05:08:46,520 together and in fact if you think back a 8303 05:08:43,600 --> 05:08:49,440 few minutes ago to when I did this- L 8304 05:08:46,520 --> 05:08:51,558 cs50 the reason I had to add that and 8305 05:08:49,440 --> 05:08:54,280 the reason my code did not compile at 8306 05:08:51,558 --> 05:08:57,718 first was because I forgot to tell clang 8307 05:08:54,280 --> 05:08:59,760 to link in cs50's zeros and ones per 8308 05:08:57,718 --> 05:09:02,360 that last step I don't need to do- L 8309 05:08:59,760 --> 05:09:03,638 standard IO because it comes with c so 8310 05:09:02,360 --> 05:09:05,400 that would just be tedious for everyone 8311 05:09:03,638 --> 05:09:07,760 in the world but cs50 does not come with 8312 05:09:05,400 --> 05:09:09,558 C so we link that in and to be clear too 8313 05:09:07,760 --> 05:09:10,878 we won't always use cs50's Library 8314 05:09:09,558 --> 05:09:12,360 that'll be yet another pair of training 8315 05:09:10,878 --> 05:09:14,280 wheels we take off in the coming weeks 8316 05:09:12,360 --> 05:09:17,000 but for now it makes a few things 8317 05:09:14,280 --> 05:09:17,000 simpler 8318 05:09:28,080 --> 05:09:32,120 yeah short answer yes so what do the 8319 05:09:30,638 --> 05:09:33,920 zeros and ones the machine code 8320 05:09:32,120 --> 05:09:36,080 translate to yes there is a one toone 8321 05:09:33,920 --> 05:09:38,600 relationship between the machine code 8322 05:09:36,080 --> 05:09:40,200 and the assembly code assembly code it's 8323 05:09:38,600 --> 05:09:42,160 not really English but at least it's 8324 05:09:40,200 --> 05:09:43,878 symbols I recognize it's not zeros and 8325 05:09:42,160 --> 05:09:45,920 ones machine code of course is just 8326 05:09:43,878 --> 05:09:49,360 zeros and ones so back in the day before 8327 05:09:45,920 --> 05:09:51,718 c existed people were programming only 8328 05:09:49,360 --> 05:09:53,920 in assembly code before assembly code 8329 05:09:51,718 --> 05:09:55,440 existed people were coding in zeros and 8330 05:09:53,920 --> 05:09:57,320 ones and you can imagine just how 8331 05:09:55,440 --> 05:09:59,200 painful that was and so each of these 8332 05:09:57,320 --> 05:10:00,600 languages makes life for us sort of 8333 05:09:59,200 --> 05:10:02,760 easier and easier in a few weeks we'll 8334 05:10:00,600 --> 05:10:06,000 transition to python which will in turn 8335 05:10:02,760 --> 05:10:08,680 make C even uh simpler or coding in 8336 05:10:06,000 --> 05:10:13,638 general simpler to do to all right so 8337 05:10:08,680 --> 05:10:15,200 with that said what now can we uh what 8338 05:10:13,638 --> 05:10:17,440 could go wrong with this well it turns 8339 05:10:15,200 --> 05:10:19,320 out that besides compiling technically 8340 05:10:17,440 --> 05:10:21,280 speaking there's decompiling and we've 8341 05:10:19,320 --> 05:10:23,718 not done this and we won't do this but 8342 05:10:21,280 --> 05:10:26,360 it's worth considering for just a moment 8343 05:10:23,718 --> 05:10:28,440 uh if you were to not compile your code 8344 05:10:26,360 --> 05:10:30,240 but decompile it as the word suggests 8345 05:10:28,440 --> 05:10:33,320 this just means reversing the process 8346 05:10:30,240 --> 05:10:36,718 converting it ideally from machine code 8347 05:10:33,320 --> 05:10:39,080 zeros and ones maybe back to C now this 8348 05:10:36,718 --> 05:10:40,760 would be cool perhaps if all you have is 8349 05:10:39,080 --> 05:10:43,160 a program you can convert it and see the 8350 05:10:40,760 --> 05:10:45,480 actual source code what might a downside 8351 05:10:43,160 --> 05:10:49,480 be if if anyone on the Internet is able 8352 05:10:45,480 --> 05:10:52,480 to decompile code on their machine 8353 05:10:49,480 --> 05:10:55,718 yeah okay so it's easier to find bugs in 8354 05:10:52,480 --> 05:10:58,440 the code that oh to exploit so it might 8355 05:10:55,718 --> 05:10:59,958 be easier to uh hack into the software 8356 05:10:58,440 --> 05:11:01,520 by finding mistakes you and I made 8357 05:10:59,958 --> 05:11:03,160 because literally they're staring at you 8358 05:11:01,520 --> 05:11:06,160 in code worries the zeros and ones make 8359 05:11:03,160 --> 05:11:10,280 it way less obvious other downsides of 8360 05:11:06,160 --> 05:11:10,280 what I call decompiling 8361 05:11:12,958 --> 05:11:17,760 yeah 8362 05:11:15,080 --> 05:11:19,600 yeah yeah if your code your work is your 8363 05:11:17,760 --> 05:11:21,120 intellectual property copyrighted or 8364 05:11:19,600 --> 05:11:22,480 otherwise you know that's kind of 8365 05:11:21,120 --> 05:11:23,878 obnoxious that someone can just like run 8366 05:11:22,480 --> 05:11:25,760 a command and boom they can see the 8367 05:11:23,878 --> 05:11:27,760 original code that you wrote now it 8368 05:11:25,760 --> 05:11:29,958 turns out it's not quite as simple as 8369 05:11:27,760 --> 05:11:32,120 that and so even though yes you could 8370 05:11:29,958 --> 05:11:34,120 take a program like hello or even 8371 05:11:32,120 --> 05:11:37,160 Microsoft Word and convert it from zeros 8372 05:11:34,120 --> 05:11:40,120 and ones back to some form of source 8373 05:11:37,160 --> 05:11:41,000 code be it in C or Java or python or 8374 05:11:40,120 --> 05:11:42,958 something else whatever it was 8375 05:11:41,000 --> 05:11:45,400 originally written in odds are it's 8376 05:11:42,958 --> 05:11:47,400 going to be an utter mess to look look 8377 05:11:45,400 --> 05:11:49,920 at why because things like variable 8378 05:11:47,400 --> 05:11:51,680 names are not retained in the zeros and 8379 05:11:49,920 --> 05:11:54,080 ones typically function names might not 8380 05:11:51,680 --> 05:11:56,160 be retained in the zeros and ones the 8381 05:11:54,080 --> 05:11:57,878 code is the logic is but the computer 8382 05:11:56,160 --> 05:11:59,760 doesn't care what pretty variables you 8383 05:11:57,878 --> 05:12:01,400 chose and how nicely named your uh 8384 05:11:59,760 --> 05:12:03,600 functions were it just needs to know 8385 05:12:01,400 --> 05:12:04,920 them as Z in ones moreover if you think 8386 05:12:03,600 --> 05:12:07,638 about last week we introduced things 8387 05:12:04,920 --> 05:12:09,400 like loops and c and besides four Loops 8388 05:12:07,638 --> 05:12:11,878 there's what other kind of loop for 8389 05:12:09,400 --> 05:12:13,520 instance so a while loop and even though 8390 05:12:11,878 --> 05:12:15,120 they look different and you have to 8391 05:12:13,520 --> 05:12:17,400 write different code they achieve 8392 05:12:15,120 --> 05:12:19,958 exactly the same functionality which is 8393 05:12:17,400 --> 05:12:21,958 to say when you compile a for Loop or 8394 05:12:19,958 --> 05:12:24,638 you compile a while loop if they 8395 05:12:21,958 --> 05:12:26,760 logically do the same thing they might 8396 05:12:24,638 --> 05:12:28,400 end up looking identical as zeros and 8397 05:12:26,760 --> 05:12:30,000 ones and so therefore it's not 8398 05:12:28,400 --> 05:12:31,958 necessarily predictable that you'll get 8399 05:12:30,000 --> 05:12:34,600 back the original code why cuz because 8400 05:12:31,958 --> 05:12:36,200 the zeros and ones might not know so to 8401 05:12:34,600 --> 05:12:37,958 speak whether it was a for Loop or a 8402 05:12:36,200 --> 05:12:39,920 while loop so maybe decompiling will 8403 05:12:37,958 --> 05:12:41,840 show you one or the other and honestly 8404 05:12:39,920 --> 05:12:43,718 decompiling while possible and it's one 8405 05:12:41,840 --> 05:12:45,958 way of reverse engineering someone's 8406 05:12:43,718 --> 05:12:47,798 product odds are if you're good enough 8407 05:12:45,958 --> 05:12:49,160 to start reading code that's been 8408 05:12:47,798 --> 05:12:50,920 decompiled and reading through the 8409 05:12:49,160 --> 05:12:52,680 messiness of it odds are you have the 8410 05:12:50,920 --> 05:12:54,958 talent probably to just write that same 8411 05:12:52,680 --> 05:12:56,760 program from scratch yourself now that's 8412 05:12:54,958 --> 05:12:59,400 an overstatement perhaps but it's not 8413 05:12:56,760 --> 05:13:01,200 quite as easy or threatening um as you 8414 05:12:59,400 --> 05:13:04,558 might first think so in general once 8415 05:13:01,200 --> 05:13:06,958 codee is piled it's pretty challenging 8416 05:13:04,558 --> 05:13:08,920 timec consuming costly to reverse 8417 05:13:06,958 --> 05:13:10,680 engineer it um much like it would be in 8418 05:13:08,920 --> 05:13:12,160 the real world right like all of us have 8419 05:13:10,680 --> 05:13:13,440 some kind of phone probably nowadays in 8420 05:13:12,160 --> 05:13:15,600 our pocket there's nothing stopping you 8421 05:13:13,440 --> 05:13:17,558 from opening it up somehow poking around 8422 05:13:15,600 --> 05:13:19,320 recreating what's there that's a huge 8423 05:13:17,558 --> 05:13:20,760 amount of effort most likely and at that 8424 05:13:19,320 --> 05:13:22,400 point maybe you should just invent the 8425 05:13:20,760 --> 05:13:24,840 phone instead of trying to reverse 8426 05:13:22,400 --> 05:13:28,080 engineer it so same kind of idea in the 8427 05:13:24,840 --> 05:13:30,760 physical world any questions then on 8428 05:13:28,080 --> 05:13:32,920 compiling or even decompiling in these 8429 05:13:30,760 --> 05:13:34,958 forms 8430 05:13:32,920 --> 05:13:37,160 all right so odds are at this point not 8431 05:13:34,958 --> 05:13:39,558 only I but you have made mistakes and 8432 05:13:37,160 --> 05:13:42,120 you've written buggy code a bug in a 8433 05:13:39,558 --> 05:13:43,878 code is just a mistake a logical error 8434 05:13:42,120 --> 05:13:46,558 or otherwise where the code just does 8435 05:13:43,878 --> 05:13:48,920 not behave correctly as you intend and 8436 05:13:46,558 --> 05:13:50,840 up until now odds are your debugging 8437 05:13:48,920 --> 05:13:53,120 techniques have been to maybe look back 8438 05:13:50,840 --> 05:13:55,360 at what I did in class maybe ask a 8439 05:13:53,120 --> 05:13:56,840 question online or in person but 8440 05:13:55,360 --> 05:13:59,040 ultimately it'd be nice if you had some 8441 05:13:56,840 --> 05:14:00,878 tools of your own with which to debug 8442 05:13:59,040 --> 05:14:02,958 code and this honestly is a lifelong 8443 05:14:00,878 --> 05:14:04,160 skill you're going to emerge from cs50 8444 05:14:02,958 --> 05:14:05,200 and even 20 years from now you're not 8445 05:14:04,160 --> 05:14:07,760 going to be writing if you're writing 8446 05:14:05,200 --> 05:14:09,798 code at all correct code all of the time 8447 05:14:07,760 --> 05:14:11,718 like all of us on the staff continue to 8448 05:14:09,798 --> 05:14:13,958 write bugs hopefully they get a little 8449 05:14:11,718 --> 05:14:15,760 more sophisticated and not sort of like 8450 05:14:13,958 --> 05:14:17,878 oops I missed a semicolon but even those 8451 05:14:15,760 --> 05:14:19,920 kinds of mistakes we make too but 8452 05:14:17,878 --> 05:14:21,600 there's tools out there and techniques 8453 05:14:19,920 --> 05:14:24,040 that can make your life easier when it 8454 05:14:21,600 --> 05:14:25,558 comes to solving those problems now the 8455 05:14:24,040 --> 05:14:28,200 term bug has actually been around for 8456 05:14:25,558 --> 05:14:31,798 decades but a fun story to tell is that 8457 05:14:28,200 --> 05:14:33,680 the first documented actual bug was 8458 05:14:31,798 --> 05:14:36,520 actually somehow connected to Harvard in 8459 05:14:33,680 --> 05:14:40,160 fact this is the log book relating to 8460 05:14:36,520 --> 05:14:41,520 the Harvard Mark 2 computer from 1947 8461 05:14:40,160 --> 05:14:44,320 whereby if you read the notes here and 8462 05:14:41,520 --> 05:14:46,520 if I zoom in this was an actual moth 8463 05:14:44,320 --> 05:14:48,480 discovered inside of this big Mainframe 8464 05:14:46,520 --> 05:14:50,040 computer that was causing some kind of 8465 05:14:48,480 --> 05:14:51,320 problems and the engineers at the time 8466 05:14:50,040 --> 05:14:53,400 actually thought it was funny that wow 8467 05:14:51,320 --> 05:14:55,160 physical bug actually explains the issue 8468 05:14:53,400 --> 05:14:57,000 and it's been forever uh taped to the 8469 05:14:55,160 --> 05:14:59,718 sheet of paper which I believe now is on 8470 05:14:57,000 --> 05:15:02,360 display in the Smithsonian uh with that 8471 05:14:59,718 --> 05:15:05,120 said this is just represented two of a 8472 05:15:02,360 --> 05:15:07,000 logical bug and that story is actually 8473 05:15:05,120 --> 05:15:08,480 uh that story was often retold by a 8474 05:15:07,000 --> 05:15:11,200 famous mathematician then computer 8475 05:15:08,480 --> 05:15:12,638 scientist really uh Dr Grace Hopper who 8476 05:15:11,200 --> 05:15:15,558 actually worked not only on the Harvard 8477 05:15:12,638 --> 05:15:18,240 Mark 2 computer but its predecessor the 8478 05:15:15,558 --> 05:15:20,120 Harvard Mark 1 and if you ever spent 8479 05:15:18,240 --> 05:15:21,520 time yet in the engineering building 8480 05:15:20,120 --> 05:15:24,320 across the river here you can actually 8481 05:15:21,520 --> 05:15:25,920 see much of this computer which is along 8482 05:15:24,320 --> 05:15:27,400 the wall when you first walk into the 8483 05:15:25,920 --> 05:15:28,840 science and engineering complex and 8484 05:15:27,400 --> 05:15:30,878 indeed as you've probably heard growing 8485 05:15:28,840 --> 05:15:33,240 up this is a Mainframe computer like 8486 05:15:30,878 --> 05:15:35,718 this is what Macs and PCs so to speak 8487 05:15:33,240 --> 05:15:37,040 looked like back in the day with very 8488 05:15:35,718 --> 05:15:38,600 physical things that essentially 8489 05:15:37,040 --> 05:15:40,200 implemented the zeros and ones that you 8490 05:15:38,600 --> 05:15:42,520 and I take for granted now being 8491 05:15:40,200 --> 05:15:43,760 miniaturized in our laptops and phones 8492 05:15:42,520 --> 05:15:45,600 so there's a piece of history there if 8493 05:15:43,760 --> 05:15:47,718 you visit campus that side of Campus 8494 05:15:45,600 --> 05:15:49,520 sometime do take a look but let's 8495 05:15:47,718 --> 05:15:51,718 consider then how we solve not of course 8496 05:15:49,520 --> 05:15:53,160 physical bugs but logical bugs and let's 8497 05:15:51,718 --> 05:15:55,160 consider something like this from last 8498 05:15:53,160 --> 05:15:58,120 week whereby we were trying very simply 8499 05:15:55,160 --> 05:16:00,878 to print like this uh column of three 8500 05:15:58,120 --> 05:16:03,760 bricks using hashtags of sorts so let me 8501 05:16:00,878 --> 05:16:05,240 go over here in just a moment to VSS 8502 05:16:03,760 --> 05:16:07,120 code and I'm going to go ahead and open 8503 05:16:05,240 --> 05:16:08,440 a program I wrote in advance and I'm 8504 05:16:07,120 --> 05:16:10,200 bringing it to class because there's a 8505 05:16:08,440 --> 05:16:12,680 bug in it and I'd like to figure out how 8506 05:16:10,200 --> 05:16:15,480 to solve this bug so let me open up uh 8507 05:16:12,680 --> 05:16:17,120 buggy z.c which is version zero of my 8508 05:16:15,480 --> 05:16:19,200 code and let's just take a quick peek at 8509 05:16:17,120 --> 05:16:21,798 what's here it's pretty short it 8510 05:16:19,200 --> 05:16:24,040 includes only standard i.h it uses 8511 05:16:21,798 --> 05:16:26,360 printf it uses a for Loop and the goal 8512 05:16:24,040 --> 05:16:28,558 quite simply is to print out that column 8513 05:16:26,360 --> 05:16:30,240 of three bricks now it's short enough 8514 05:16:28,558 --> 05:16:31,840 that some of you if you're getting comfy 8515 05:16:30,240 --> 05:16:34,400 already with see you might already see 8516 05:16:31,840 --> 05:16:36,440 The Logical bug it's not a syntax error 8517 05:16:34,400 --> 05:16:38,360 like it will compile and run but there's 8518 05:16:36,440 --> 05:16:41,240 a bug there and suppose that I'm very 8519 05:16:38,360 --> 05:16:44,040 new to see I'm very uncomfortable with C 8520 05:16:41,240 --> 05:16:45,840 it's 2 a.m. and I just can't see the bug 8521 05:16:44,040 --> 05:16:48,160 what are my recourses here for actually 8522 05:16:45,840 --> 05:16:49,878 finding a mistake like this well first 8523 05:16:48,160 --> 05:16:52,320 let's look at the symptom let me go down 8524 05:16:49,878 --> 05:16:54,440 to my terminal window I'm going to use 8525 05:16:52,320 --> 05:16:56,440 make buggy zero because again the file 8526 05:16:54,440 --> 05:16:58,000 is called buggy zero. C I'm not going to 8527 05:16:56,440 --> 05:16:59,718 use clang in fact I'm never really going 8528 05:16:58,000 --> 05:17:00,958 to use clang manually here and out I'm 8529 05:16:59,718 --> 05:17:03,600 just going to use make because it makes 8530 05:17:00,958 --> 05:17:05,480 our lives easier it does compile no 8531 05:17:03,600 --> 05:17:07,638 errors so it's not syntax it's not 8532 05:17:05,480 --> 05:17:11,440 something silly like a missing semicolon 8533 05:17:07,638 --> 05:17:14,638 but when I runbuggy Z I of course see 1 8534 05:17:11,440 --> 05:17:17,320 2 3 4 and this of course does not match 8535 05:17:14,638 --> 05:17:19,840 the zero the one two three bricks that I 8536 05:17:17,320 --> 05:17:22,200 actually intended for that column and 8537 05:17:19,840 --> 05:17:24,160 yet I'm starting counting at zero as I 8538 05:17:22,200 --> 05:17:26,638 usually do I've got three I'm going up 8539 05:17:24,160 --> 05:17:28,080 to three so where is my logical error if 8540 05:17:26,638 --> 05:17:30,240 it hasn't obviously jumped out at you 8541 05:17:28,080 --> 05:17:32,480 already well how can I solve this well 8542 05:17:30,240 --> 05:17:34,080 first and foremost perhaps the best 8543 05:17:32,480 --> 05:17:36,280 technique for solving bugs at least 8544 05:17:34,080 --> 05:17:38,480 early on is just use printf like thus 8545 05:17:36,280 --> 05:17:40,520 far we've used printf to say hello and 8546 05:17:38,480 --> 05:17:42,400 other things on the screen but print def 8547 05:17:40,520 --> 05:17:43,680 is just a function for printing anything 8548 05:17:42,400 --> 05:17:46,000 and there's no reason you can't 8549 05:17:43,680 --> 05:17:47,920 temporarily use print def to like print 8550 05:17:46,000 --> 05:17:49,798 out the contents of variables what's 8551 05:17:47,920 --> 05:17:51,200 going on inside of your program just to 8552 05:17:49,798 --> 05:17:52,440 figure out where your mistake is and 8553 05:17:51,200 --> 05:17:53,600 then you can delete that line of code 8554 05:17:52,440 --> 05:17:56,120 later it doesn't have to stay there 8555 05:17:53,600 --> 05:17:58,638 forever so let me do this instead of 8556 05:17:56,120 --> 05:18:02,480 just printing out in vs code the hash 8557 05:17:58,638 --> 05:18:05,840 symbol let me do a little safety check 8558 05:18:02,480 --> 05:18:09,080 here and print out the value of I so let 8559 05:18:05,840 --> 05:18:11,760 me go ahead and say something like I is 8560 05:18:09,080 --> 05:18:13,240 now I want to say I is this but of 8561 05:18:11,760 --> 05:18:15,280 course this is not how I print out the 8562 05:18:13,240 --> 05:18:17,480 value of I if I want to print out the 8563 05:18:15,280 --> 05:18:20,680 value of I what should I put 8564 05:18:17,480 --> 05:18:22,480 here so percent I for integer instead of 8565 05:18:20,680 --> 05:18:24,160 percent s for string so they're still 8566 05:18:22,480 --> 05:18:25,798 placeholders but we use percent s for 8567 05:18:24,160 --> 05:18:27,718 integers and now if I want to print out 8568 05:18:25,798 --> 05:18:29,958 I I just need the comma as the second 8569 05:18:27,718 --> 05:18:32,320 argument and then I all right let me go 8570 05:18:29,958 --> 05:18:34,360 ahead and back to vs uh to my terminal 8571 05:18:32,320 --> 05:18:36,558 window let me recompile the program 8572 05:18:34,360 --> 05:18:40,240 because I've changed it that still works 8573 05:18:36,558 --> 05:18:42,638 fine do/ bugy Z and now let me increase 8574 05:18:40,240 --> 05:18:44,600 the size of my terminal window here you 8575 05:18:42,638 --> 05:18:46,558 just see some diagnostic information if 8576 05:18:44,600 --> 05:18:47,920 you will like this is not the goal this 8577 05:18:46,558 --> 05:18:50,080 is not what you should be submitting for 8578 05:18:47,920 --> 05:18:52,040 this homework problem we're at one uh 8579 05:18:50,080 --> 05:18:54,360 but it is helping us diagnostically know 8580 05:18:52,040 --> 05:18:56,440 that okay when I is zero here's a hash 8581 05:18:54,360 --> 05:18:58,520 when I is one here's a hash when I is 8582 05:18:56,440 --> 05:19:00,440 two here's a hash when I is three here's 8583 05:18:58,520 --> 05:19:02,920 a hash well wait a minute that's one two 8584 05:19:00,440 --> 05:19:04,958 three four so clearly I'm printing it 8585 05:19:02,920 --> 05:19:06,680 one too many times so let me look back 8586 05:19:04,958 --> 05:19:10,000 at the code here by shrinking my 8587 05:19:06,680 --> 05:19:13,240 terminal window and let me just ask the 8588 05:19:10,000 --> 05:19:16,080 group where is in fact the mistake or 8589 05:19:13,240 --> 05:19:18,400 what equivalently would be the solution 8590 05:19:16,080 --> 05:19:18,400 yeah in the 8591 05:19:19,000 --> 05:19:23,878 middle yeah instead of less than or 8592 05:19:21,160 --> 05:19:25,160 equal to use just less than so you got 8593 05:19:23,878 --> 05:19:26,958 to kind of pick a lane here like if 8594 05:19:25,160 --> 05:19:29,440 you're going to start counting from zero 8595 05:19:26,958 --> 05:19:31,360 you generally use less than and go up to 8596 05:19:29,440 --> 05:19:32,920 but not through the value or if you 8597 05:19:31,360 --> 05:19:34,878 prefer like in the human world counting 8598 05:19:32,920 --> 05:19:36,600 from one on up you can use great less 8599 05:19:34,878 --> 05:19:38,040 than or equal to but you have to be 8600 05:19:36,600 --> 05:19:39,958 consistent and in general as a 8601 05:19:38,040 --> 05:19:41,320 programmer just always start counting 8602 05:19:39,958 --> 05:19:43,320 from zero if you're doing something 8603 05:19:41,320 --> 05:19:45,680 canonical like this but the solution is 8604 05:19:43,320 --> 05:19:47,120 indeed just to change this by changing 8605 05:19:45,680 --> 05:19:50,200 the greater less than or equal to to 8606 05:19:47,120 --> 05:19:52,840 less than if I re compile this program 8607 05:19:50,200 --> 05:19:55,200 with make buggy zero and then do buggy 8608 05:19:52,840 --> 05:19:57,600 zero again and let me increase the size 8609 05:19:55,200 --> 05:20:00,280 of my terminal window now you see okay 8610 05:19:57,600 --> 05:20:02,718 almost the same output but indeed I 8611 05:20:00,280 --> 05:20:05,160 starts at zero goes up two but not 8612 05:20:02,718 --> 05:20:07,760 through three all right so printf in 8613 05:20:05,160 --> 05:20:09,638 short should be can be your first uh 8614 05:20:07,760 --> 05:20:11,440 diagnostic tool instead of just staring 8615 05:20:09,638 --> 05:20:13,798 at the screen or raising your hand I 8616 05:20:11,440 --> 05:20:15,680 mean use printf to see literally what's 8617 05:20:13,798 --> 05:20:17,440 going on inside of your program by just 8618 05:20:15,680 --> 05:20:19,000 printing out things of interest and then 8619 05:20:17,440 --> 05:20:21,040 once you've solved the problem you can 8620 05:20:19,000 --> 05:20:22,840 go back into your code AS I'll do here 8621 05:20:21,040 --> 05:20:24,878 by shrinking my terminal window I'll 8622 05:20:22,840 --> 05:20:26,558 delete the print F line and now I'm 8623 05:20:24,878 --> 05:20:28,280 ready to share this program with the 8624 05:20:26,558 --> 05:20:30,400 world or submit it as homework or the 8625 05:20:28,280 --> 05:20:34,240 like it's just meant there to be 8626 05:20:30,400 --> 05:20:36,840 temporary any questions on printf as a 8627 05:20:34,240 --> 05:20:36,840 debugging 8628 05:20:37,080 --> 05:20:41,958 tool no all right well that only gets us 8629 05:20:40,200 --> 05:20:43,520 so far and honestly as your programs 8630 05:20:41,958 --> 05:20:44,958 grow and grow and grow it's going to 8631 05:20:43,520 --> 05:20:47,200 actually get really annoying to start 8632 05:20:44,958 --> 05:20:48,878 going in and adding printfs then 8633 05:20:47,200 --> 05:20:50,638 removing them and figuring out if you've 8634 05:20:48,878 --> 05:20:52,520 got multiple printfs well which one 8635 05:20:50,638 --> 05:20:54,958 printed what like it just gets messy 8636 05:20:52,520 --> 05:20:57,040 eventually to rely on printf alone so 8637 05:20:54,958 --> 05:20:59,000 being a computer scientist computer 8638 05:20:57,040 --> 05:21:01,840 scientists have written software to make 8639 05:20:59,000 --> 05:21:04,200 it easier to debug code that software is 8640 05:21:01,840 --> 05:21:05,400 what we would generally call a debugger 8641 05:21:04,200 --> 05:21:06,958 which would be the second tool of the 8642 05:21:05,400 --> 05:21:09,558 trade that you can use to actually solve 8643 05:21:06,958 --> 05:21:11,760 problems in your code now in the world 8644 05:21:09,558 --> 05:21:13,878 of VSS code there's actually a debugger 8645 05:21:11,760 --> 05:21:15,638 built in so the graphical user interface 8646 05:21:13,878 --> 05:21:17,440 you're about to see in VSS code isn't 8647 05:21:15,638 --> 05:21:19,958 specific to cs50 it actually comes with 8648 05:21:17,440 --> 05:21:21,878 vs code and it supports C and C++ and 8649 05:21:19,958 --> 05:21:23,958 Java and Python and lots of other 8650 05:21:21,878 --> 05:21:26,798 languages too but it's admittedly a 8651 05:21:23,958 --> 05:21:28,480 little complicated to just start using 8652 05:21:26,798 --> 05:21:30,120 the debugger you have to like create a 8653 05:21:28,480 --> 05:21:31,840 configuration file and do like some 8654 05:21:30,120 --> 05:21:33,958 annoying steps that just get in the way 8655 05:21:31,840 --> 05:21:35,878 of solving real problems so we have 8656 05:21:33,958 --> 05:21:37,558 automated the process for you of just 8657 05:21:35,878 --> 05:21:39,080 starting the debugger and thereafter 8658 05:21:37,558 --> 05:21:40,680 it's sort of Industry standard how you 8659 05:21:39,080 --> 05:21:42,558 use it but we save you the headache of 8660 05:21:40,680 --> 05:21:45,120 having to create those configuration 8661 05:21:42,558 --> 05:21:46,760 files so suppose I want to do this 8662 05:21:45,120 --> 05:21:49,440 suppose I want to try to debug this 8663 05:21:46,760 --> 05:21:52,040 program step by step using special 8664 05:21:49,440 --> 05:21:54,920 software well how can I do that well let 8665 05:21:52,040 --> 05:21:57,040 me propose that if I revert this back to 8666 05:21:54,920 --> 05:21:59,958 the original version where I was less 8667 05:21:57,040 --> 05:22:01,798 than or equal to three I'm pretty sure 8668 05:21:59,958 --> 05:22:03,040 that I was printing to hashes so I'm 8669 05:22:01,798 --> 05:22:05,160 going to do this and you might have done 8670 05:22:03,040 --> 05:22:07,680 this accidentally or never at all but 8671 05:22:05,160 --> 05:22:09,480 notice if you hover over the gutter so 8672 05:22:07,680 --> 05:22:11,280 to speak in vs code the part of it all 8673 05:22:09,480 --> 05:22:14,320 the way to the left of the editor you 8674 05:22:11,280 --> 05:22:16,440 see this sort of grayed out uh Red Dot 8675 05:22:14,320 --> 05:22:18,120 if you click there it becomes a brighter 8676 05:22:16,440 --> 05:22:20,040 Red Dot and this represents what we're 8677 05:22:18,120 --> 05:22:21,600 going to call a break point and this is 8678 05:22:20,040 --> 05:22:23,480 just a visual indicator that you've put 8679 05:22:21,600 --> 05:22:25,680 like a stop sign equivalent there and 8680 05:22:23,480 --> 05:22:28,160 you're telling the debugger in a moment 8681 05:22:25,680 --> 05:22:30,360 stop running my code there why because I 8682 05:22:28,160 --> 05:22:32,360 prefer to step through my code at sort 8683 05:22:30,360 --> 05:22:34,520 of a human speed and not as computer 8684 05:22:32,360 --> 05:22:36,680 speed where it runs all at once so I've 8685 05:22:34,520 --> 05:22:38,360 set my breakpoint which is step one and 8686 05:22:36,680 --> 05:22:40,760 then step two is quite simply this 8687 05:22:38,360 --> 05:22:43,798 instead of running the program itself 8688 05:22:40,760 --> 05:22:47,798 run a command called debug 50 and then 8689 05:22:43,798 --> 05:22:50,040 do/ bugy Z and now this will start your 8690 05:22:47,798 --> 05:22:51,600 program but inside of the debugger which 8691 05:22:50,040 --> 05:22:53,840 is a special program that smart people 8692 05:22:51,600 --> 05:22:56,638 wrote that will Empower you to now step 8693 05:22:53,840 --> 05:22:59,160 through your code line by line at again 8694 05:22:56,638 --> 05:23:01,200 at your own Comfort Pace I'm going to 8695 05:22:59,160 --> 05:23:03,680 hit enter some stuff's going to happen 8696 05:23:01,200 --> 05:23:05,360 on the screen whoops uh notice this is a 8697 05:23:03,680 --> 05:23:07,040 common mistake that I made accidentally 8698 05:23:05,360 --> 05:23:08,400 here looks like I've changed my code I 8699 05:23:07,040 --> 05:23:10,200 did because I went in and changed the 8700 05:23:08,400 --> 05:23:13,638 less than or equal to sign so let me go 8701 05:23:10,200 --> 05:23:17,840 ahead and rerun make buggy zero enter 8702 05:23:13,638 --> 05:23:19,160 good now let me rerun debug 50 enter and 8703 05:23:17,840 --> 05:23:20,718 now some stuff just happened on the 8704 05:23:19,160 --> 05:23:23,480 screen and it takes a moment to get 8705 05:23:20,718 --> 05:23:25,920 started but once it's started you'll see 8706 05:23:23,480 --> 05:23:27,718 this you'll still see your code but 8707 05:23:25,920 --> 05:23:29,480 you'll see this yellow highlight which 8708 05:23:27,718 --> 05:23:30,520 you've probably not seen before and 8709 05:23:29,480 --> 05:23:32,160 notice that it's specifically 8710 05:23:30,520 --> 05:23:34,718 highlighted in the same line that I set 8711 05:23:32,160 --> 05:23:36,958 a breakpoint on why that just means the 8712 05:23:34,718 --> 05:23:39,718 program has EXE the debugger has 8713 05:23:36,958 --> 05:23:42,958 executed all of these lines except for 8714 05:23:39,718 --> 05:23:44,958 line seven it has broken at not in a bad 8715 05:23:42,958 --> 05:23:46,920 way but it has paused execution on line 8716 05:23:44,958 --> 05:23:49,120 seven so it hasn't yet printed any 8717 05:23:46,920 --> 05:23:51,040 hashes and you can see that no hashes in 8718 05:23:49,120 --> 05:23:53,320 the terminal window yet it's paused 8719 05:23:51,040 --> 05:23:56,160 execution but what's interesting with 8720 05:23:53,320 --> 05:23:58,120 the debugger is the stuff over here on 8721 05:23:56,160 --> 05:24:00,280 the left hand side in the debugger here 8722 05:23:58,120 --> 05:24:02,200 you'll see under variables all of your 8723 05:24:00,280 --> 05:24:03,958 so-called local variables and we haven't 8724 05:24:02,200 --> 05:24:05,760 really made a distinction between local 8725 05:24:03,958 --> 05:24:07,320 and something called Global but for now 8726 05:24:05,760 --> 05:24:09,600 local variables just means all of the 8727 05:24:07,320 --> 05:24:12,320 variables that exist in your function so 8728 05:24:09,600 --> 05:24:14,798 I currently has a value of zero okay and 8729 05:24:12,320 --> 05:24:17,320 that makes sense so now how do I step 8730 05:24:14,798 --> 05:24:19,120 through my code and see what it's doing 8731 05:24:17,320 --> 05:24:21,040 well at the top of the screen here 8732 05:24:19,120 --> 05:24:22,480 you'll see some playback icons kind of 8733 05:24:21,040 --> 05:24:25,600 like a video player but they have 8734 05:24:22,480 --> 05:24:27,360 special meaning this first one will just 8735 05:24:25,600 --> 05:24:28,958 play the rest of your program all the 8736 05:24:27,360 --> 05:24:30,280 way to the end so you only click that if 8737 05:24:28,958 --> 05:24:32,120 you sort of solved a problem and you 8738 05:24:30,280 --> 05:24:34,520 just want to run run it to completion 8739 05:24:32,120 --> 05:24:37,000 like before but the next three or next 8740 05:24:34,520 --> 05:24:39,040 two really are really the juiciest the 8741 05:24:37,000 --> 05:24:40,558 second one here if you hover over it 8742 05:24:39,040 --> 05:24:43,638 eventually you'll see that it's called 8743 05:24:40,558 --> 05:24:45,680 step over step over means that the 8744 05:24:43,638 --> 05:24:47,320 debugger will run this currently 8745 05:24:45,680 --> 05:24:48,840 highlighted line of code but it's not 8746 05:24:47,320 --> 05:24:50,638 going to dive into it so if it's a 8747 05:24:48,840 --> 05:24:52,400 function like print F it's not going to 8748 05:24:50,638 --> 05:24:53,798 start stepping through print F line by 8749 05:24:52,400 --> 05:24:55,760 line why because I can pretty much 8750 05:24:53,798 --> 05:24:58,160 assume print F written decades ago is 8751 05:24:55,760 --> 05:25:00,160 correct problems probably with me but 8752 05:24:58,160 --> 05:25:02,878 this next line if I did really want to 8753 05:25:00,160 --> 05:25:04,798 step in into the printf code to figure 8754 05:25:02,878 --> 05:25:06,680 out how it works or find some problem in 8755 05:25:04,798 --> 05:25:08,280 it all these years later you can step 8756 05:25:06,680 --> 05:25:09,798 into printf and then the screen would 8757 05:25:08,280 --> 05:25:11,920 change and you'd see each of the lines 8758 05:25:09,798 --> 05:25:13,240 for print F line by line at least if you 8759 05:25:11,920 --> 05:25:14,840 have the source code for print F 8760 05:25:13,240 --> 05:25:17,280 installed all right I'm going to use the 8761 05:25:14,840 --> 05:25:20,040 first one step over and watch as the 8762 05:25:17,280 --> 05:25:21,878 yellow highlight moves and watch as in 8763 05:25:20,040 --> 05:25:25,320 the terminal window there's a hash 8764 05:25:21,878 --> 05:25:27,200 symbol here we go there's one hash now 8765 05:25:25,320 --> 05:25:29,480 notice line five is highlighted that 8766 05:25:27,200 --> 05:25:31,520 means it has paused on line five line 8767 05:25:29,480 --> 05:25:34,040 five has not yet been exec executed so 8768 05:25:31,520 --> 05:25:36,280 what does that mean the value of I per 8769 05:25:34,040 --> 05:25:38,400 the top left hand corner is still zero 8770 05:25:36,280 --> 05:25:40,958 but as soon as I click step over again 8771 05:25:38,400 --> 05:25:43,480 Watch What Happens at the top left where 8772 05:25:40,958 --> 05:25:46,480 I is a variable on the 8773 05:25:43,480 --> 05:25:48,958 screen now I and it flashed briefly has 8774 05:25:46,480 --> 05:25:50,878 a value of one and now if I step over 8775 05:25:48,958 --> 05:25:53,480 again watch the terminal window there's 8776 05:25:50,878 --> 05:25:55,240 my second hash now let me click step 8777 05:25:53,480 --> 05:25:58,400 over on the for Loop watch the variable 8778 05:25:55,240 --> 05:26:00,840 at top left now one goes to two now let 8779 05:25:58,400 --> 05:26:02,440 me click it again third hash and Here's 8780 05:26:00,840 --> 05:26:04,480 Where The Logical error is perhaps 8781 05:26:02,440 --> 05:26:06,840 revealed let me go ahead and step over 8782 05:26:04,480 --> 05:26:09,160 the loop now I is three wait a minute 8783 05:26:06,840 --> 05:26:10,680 I'm still going to print out a hash 8784 05:26:09,160 --> 05:26:12,000 there it is there's the fourth hash and 8785 05:26:10,680 --> 05:26:13,958 at this point hopefully the light bulb 8786 05:26:12,000 --> 05:26:16,240 proverbially has gone off I realize oh I 8787 05:26:13,958 --> 05:26:18,878 screwed up I can either stop the program 8788 05:26:16,240 --> 05:26:20,718 Al together with the red square or I can 8789 05:26:18,878 --> 05:26:22,798 just let it run all the way to the end 8790 05:26:20,718 --> 05:26:24,120 which just terminates everything at this 8791 05:26:22,798 --> 05:26:25,840 point I just want to get back into my 8792 05:26:24,120 --> 05:26:27,760 code and start fixing things and you can 8793 05:26:25,840 --> 05:26:29,680 close for instance as I will here the 8794 05:26:27,760 --> 05:26:32,360 file explorer just to hide the panel 8795 05:26:29,680 --> 05:26:34,360 that open so that's debug 50 but it's 8796 05:26:32,360 --> 05:26:36,000 not a cs50 thing that just starts the 8797 05:26:34,360 --> 05:26:37,958 debugger for you which is something 8798 05:26:36,000 --> 05:26:42,600 you'd find in most any programming 8799 05:26:37,958 --> 05:26:46,080 environment nowadays questions on 8800 05:26:42,600 --> 05:26:46,080 debugging questions 8801 05:26:46,718 --> 05:26:51,718 yeah good question where does it tell 8802 05:26:48,840 --> 05:26:53,638 you where it went wrong so sadly it does 8803 05:26:51,718 --> 05:26:56,280 not tell you any of that the onus is 8804 05:26:53,638 --> 05:26:57,920 still on you the human to use this tool 8805 05:26:56,280 --> 05:27:00,680 productively to walk through your code 8806 05:26:57,920 --> 05:27:02,080 at a a saner pace but your brain is is 8807 05:27:00,680 --> 05:27:03,558 the one that still needs to solve it and 8808 05:27:02,080 --> 05:27:05,160 I don't doubt down the line with 8809 05:27:03,558 --> 05:27:06,760 artificial intelligence and more 8810 05:27:05,160 --> 05:27:08,280 programs like this will get all the more 8811 05:27:06,760 --> 05:27:09,440 helpful and start answering questions 8812 05:27:08,280 --> 05:27:11,200 like that for us and there are other 8813 05:27:09,440 --> 05:27:12,798 tools we'll introduce you this semester 8814 05:27:11,200 --> 05:27:14,760 that are even more powerful than this 8815 05:27:12,798 --> 05:27:17,000 but for now it's just a tool really to 8816 05:27:14,760 --> 05:27:18,920 slow things down and not have to change 8817 05:27:17,000 --> 05:27:20,718 your code the fact that I had that panel 8818 05:27:18,920 --> 05:27:23,080 on the left that just showed me eyes 8819 05:27:20,718 --> 05:27:24,520 changing value is just an alternative to 8820 05:27:23,080 --> 05:27:29,520 print F and I can step through it a 8821 05:27:24,520 --> 05:27:32,680 little more slowly other questions on 8822 05:27:29,520 --> 05:27:35,120 debugging now let me show you one final 8823 05:27:32,680 --> 05:27:37,120 example with this debugger here and this 8824 05:27:35,120 --> 05:27:41,280 one too I wrote in advance let me close 8825 05:27:37,120 --> 05:27:42,920 buggy z.c and let me open up buggy1.c my 8826 05:27:41,280 --> 05:27:44,200 second version thereof let me close my 8827 05:27:42,920 --> 05:27:46,120 terminal window for a second and give 8828 05:27:44,200 --> 05:27:48,600 you a quick tour of this program which 8829 05:27:46,120 --> 05:27:50,160 similarly has a mistake now at the top 8830 05:27:48,600 --> 05:27:53,320 of this program some familiar includes 8831 05:27:50,160 --> 05:27:54,680 cs50.h and standard i.h this is not 8832 05:27:53,320 --> 05:27:56,558 something we've seen before it's 8833 05:27:54,680 --> 05:27:59,160 specific to this example a function 8834 05:27:56,558 --> 05:28:01,040 called get Negative int takes no 8835 05:27:59,160 --> 05:28:02,760 arguments and it returns an integer what 8836 05:28:01,040 --> 05:28:05,680 does it do it literally gets a negative 8837 05:28:02,760 --> 05:28:07,360 integer ideally from the user fun fact 8838 05:28:05,680 --> 05:28:09,280 though it doesn't correctly that's the 8839 05:28:07,360 --> 05:28:11,760 bug get Negative int is broken at the 8840 05:28:09,280 --> 05:28:13,600 moment so what does Maine do well main 8841 05:28:11,760 --> 05:28:15,920 just calls this function passing in 8842 05:28:13,600 --> 05:28:18,558 nothing in parenthesis no inputs and it 8843 05:28:15,920 --> 05:28:20,160 stores the return value in I and then it 8844 05:28:18,558 --> 05:28:22,558 just prints out I on the screen so 8845 05:28:20,160 --> 05:28:23,878 honestly just by eyeballing this you 8846 05:28:22,558 --> 05:28:25,760 know I feel comfortable enough with 8847 05:28:23,878 --> 05:28:27,360 programming and see I think main is 8848 05:28:25,760 --> 05:28:28,920 correct let me just stipulate main is 8849 05:28:27,360 --> 05:28:30,520 correct but there is going to be a bug 8850 05:28:28,920 --> 05:28:33,760 down here now what's the bug down here 8851 05:28:30,520 --> 05:28:36,320 here well let me look at get Negative in 8852 05:28:33,760 --> 05:28:39,000 implementation notice this first line 12 8853 05:28:36,320 --> 05:28:41,080 is identical to the Prototype up here 8854 05:28:39,000 --> 05:28:43,958 the Prototype is sort of stupidly 8855 05:28:41,080 --> 05:28:45,680 required up here because C reads things 8856 05:28:43,958 --> 05:28:47,920 top to bottom left to right the compiler 8857 05:28:45,680 --> 05:28:50,120 technically does so if you reference get 8858 05:28:47,920 --> 05:28:52,520 Negative in here but you don't implement 8859 05:28:50,120 --> 05:28:54,480 it until down here and you haven't told 8860 05:28:52,520 --> 05:28:56,798 C in advance that it will exist again 8861 05:28:54,480 --> 05:28:58,920 you get the error we saw last week all 8862 05:28:56,798 --> 05:29:00,920 right so how does get Negative int work 8863 05:28:58,920 --> 05:29:03,400 we declare a variable called n 8864 05:29:00,920 --> 05:29:04,958 we've got a do while loop that does what 8865 05:29:03,400 --> 05:29:07,440 it uses get int which comes with the 8866 05:29:04,958 --> 05:29:08,878 cs50 library per last week it prompts 8867 05:29:07,440 --> 05:29:12,240 the user for negative integer quote 8868 05:29:08,878 --> 05:29:16,480 unquote and stores the value in n i then 8869 05:29:12,240 --> 05:29:18,040 do all of this while n is less than zero 8870 05:29:16,480 --> 05:29:19,280 right remember we used a do while loop 8871 05:29:18,040 --> 05:29:21,878 last week to make sure the human 8872 05:29:19,280 --> 05:29:23,718 cooperates and doesn't give us the wrong 8873 05:29:21,878 --> 05:29:26,360 type of value be it positive or negative 8874 05:29:23,718 --> 05:29:27,840 or something else and then we return n 8875 05:29:26,360 --> 05:29:29,920 and there's some subtleties anyone 8876 05:29:27,840 --> 05:29:33,680 recall or have an intuition for why I've 8877 05:29:29,920 --> 05:29:38,920 declared clared in on line 14 instead of 8878 05:29:33,680 --> 05:29:38,920 on line 17 this is a c specific 8879 05:29:42,680 --> 05:29:46,638 thing exactly there's this notion of 8880 05:29:44,920 --> 05:29:49,360 scope in see and we'll continue to see 8881 05:29:46,638 --> 05:29:51,878 this over time whereby a variable only 8882 05:29:49,360 --> 05:29:53,798 exists inside of the most recent curly 8883 05:29:51,878 --> 05:29:56,480 braces that you've opened so if I've 8884 05:29:53,798 --> 05:29:59,080 declared and here on line 14 I can use 8885 05:29:56,480 --> 05:30:00,320 it anywhere between lines 13 and 21 8886 05:29:59,080 --> 05:30:02,480 because those are the nearest curly 8887 05:30:00,320 --> 05:30:05,798 Braes if by contrast as you note if I 8888 05:30:02,480 --> 05:30:07,958 instead said this int n equals get int 8889 05:30:05,798 --> 05:30:10,558 and so forth and didn't have the current 8890 05:30:07,958 --> 05:30:13,320 line 14 well n would exist inside of 8891 05:30:10,558 --> 05:30:15,360 these curly braces but not here which is 8892 05:30:13,320 --> 05:30:17,320 too late and definitely not here so you 8893 05:30:15,360 --> 05:30:19,920 just have to declare it first and then 8894 05:30:17,320 --> 05:30:21,760 use and reuse it as such now let me just 8895 05:30:19,920 --> 05:30:23,320 show you how I can debug this but let me 8896 05:30:21,760 --> 05:30:26,040 show you the symptoms first let me open 8897 05:30:23,320 --> 05:30:28,480 my terminal window let me run make buggy 8898 05:30:26,040 --> 05:30:31,680 one compiles okay so it's not something 8899 05:30:28,480 --> 05:30:33,558 silly like a semicolon dots SL buggy one 8900 05:30:31,680 --> 05:30:35,718 and I'm asked for a negative integer all 8901 05:30:33,558 --> 05:30:38,558 right let me give it negative one 8902 05:30:35,718 --> 05:30:40,320 enter well the main function supposed to 8903 05:30:38,558 --> 05:30:41,840 print out what I typed but it clearly 8904 05:30:40,320 --> 05:30:45,240 didn't it's prompting me again all right 8905 05:30:41,840 --> 05:30:48,840 so maybe it'll like -2 no maybe 8906 05:30:45,240 --> 05:30:50,280 -3 50 okay so it's definitely broken 8907 05:30:48,840 --> 05:30:52,120 right it kind of seems logically to be 8908 05:30:50,280 --> 05:30:53,600 doing the opposite now you can perhaps 8909 05:30:52,120 --> 05:30:55,760 see why this is happening already these 8910 05:30:53,600 --> 05:30:58,280 are deliberately simple programs by uh 8911 05:30:55,760 --> 05:31:00,400 for demonstration sake but let's do this 8912 05:30:58,280 --> 05:31:02,160 let me go ahead and set a break point in 8913 05:31:00,400 --> 05:31:04,280 main even though I'm pretty sure main is 8914 05:31:02,160 --> 05:31:05,920 correct but it just helps me start my 8915 05:31:04,280 --> 05:31:08,280 thought process start with Main and then 8916 05:31:05,920 --> 05:31:09,718 take it from there let me run now uh 8917 05:31:08,280 --> 05:31:13,680 debug 8918 05:31:09,718 --> 05:31:15,520 50 bugy one enter and let's see with 8919 05:31:13,680 --> 05:31:16,920 that breakpoint now the the goey is 8920 05:31:15,520 --> 05:31:18,718 going to reconfigure itself it's going 8921 05:31:16,920 --> 05:31:20,680 to pause on line eight because that's 8922 05:31:18,718 --> 05:31:22,200 the first interesting line inside of 8923 05:31:20,680 --> 05:31:24,000 Maine so I could have just put the 8924 05:31:22,200 --> 05:31:26,160 breakpoint on line eight too it's smart 8925 05:31:24,000 --> 05:31:27,558 enough to know that if I set it on six 8926 05:31:26,160 --> 05:31:29,718 eh you really mean line eight because 8927 05:31:27,558 --> 05:31:31,920 that's the first actual line of code and 8928 05:31:29,718 --> 05:31:35,040 watch when now what happens if I step 8929 05:31:31,920 --> 05:31:37,000 over this line notice that I which at 8930 05:31:35,040 --> 05:31:39,840 the moment seems to have a default value 8931 05:31:37,000 --> 05:31:43,120 of zero more on that another time but if 8932 05:31:39,840 --> 05:31:44,878 I click step over like before I'm 8933 05:31:43,120 --> 05:31:48,240 prompted for a negative integer let me 8934 05:31:44,878 --> 05:31:51,878 type negative 1 enter and now 8935 05:31:48,240 --> 05:31:56,160 notice there's no additional yellow 8936 05:31:51,878 --> 05:31:56,160 highlight why where am I currently stuck 8937 05:31:57,080 --> 05:32:01,160 logically yeah just logically I must be 8938 05:31:59,360 --> 05:32:02,760 in that D while Loop and even if you 8939 05:32:01,160 --> 05:32:04,400 don't understand it like that's the only 8940 05:32:02,760 --> 05:32:06,120 explanation if you keep getting prompted 8941 05:32:04,400 --> 05:32:07,958 surely there's a loop going on there's 8942 05:32:06,120 --> 05:32:10,120 only one Loop in my code so there's 8943 05:32:07,958 --> 05:32:11,718 probably a problem there so okay I can't 8944 05:32:10,120 --> 05:32:13,440 just set a breakpoint in Main and then 8945 05:32:11,718 --> 05:32:16,480 wait for this to work so let me just uh 8946 05:32:13,440 --> 05:32:18,958 let me stop this with the red square and 8947 05:32:16,480 --> 05:32:20,958 let me think all right instead of I can 8948 05:32:18,958 --> 05:32:22,718 still set my break point in main but let 8949 05:32:20,958 --> 05:32:25,280 me rerun the debugger instead and this 8950 05:32:22,718 --> 05:32:28,240 time not step over that line of code let 8951 05:32:25,280 --> 05:32:30,120 me step into that line of code so Watch 8952 05:32:28,240 --> 05:32:32,160 What Happens now instead of clicking the 8953 05:32:30,120 --> 05:32:35,000 second icon here let me click the third 8954 05:32:32,160 --> 05:32:36,718 whose name is indeed step into and watch 8955 05:32:35,000 --> 05:32:40,878 as the yellow highlight does not move to 8956 05:32:36,718 --> 05:32:43,200 line n it dives into line8 the function 8957 05:32:40,878 --> 05:32:46,440 on line eight thereby bringing me whoosh 8958 05:32:43,200 --> 05:32:48,680 down to line 17 it's kind of going down 8959 05:32:46,440 --> 05:32:51,680 into that next function now it didn't 8960 05:32:48,680 --> 05:32:52,798 bother pausing on line 12 or 13 or 14 8961 05:32:51,680 --> 05:32:54,320 because there's nothing intellectually 8962 05:32:52,798 --> 05:32:55,920 interesting there happening yet the 8963 05:32:54,320 --> 05:32:59,160 juicy part really starts it would seem 8964 05:32:55,920 --> 05:33:02,280 in line 17 so now notice n is my 8965 05:32:59,160 --> 05:33:04,558 variable at the top left if I click I 8966 05:33:02,280 --> 05:33:06,840 don't want to click step into now 8967 05:33:04,558 --> 05:33:09,638 though what what would go wrong if I 8968 05:33:06,840 --> 05:33:11,480 click on step into or what would it do 8969 05:33:09,638 --> 05:33:13,840 that I don't think I want to 8970 05:33:11,480 --> 05:33:16,600 do 8971 05:33:13,840 --> 05:33:18,080 yeah yeah it would step into get int but 8972 05:33:16,600 --> 05:33:19,798 I'd like to think that the staff's 8973 05:33:18,080 --> 05:33:21,400 version of get in is correct and that's 8974 05:33:19,798 --> 05:33:24,320 not our problem today so I want to step 8975 05:33:21,400 --> 05:33:26,760 over it and watch now at top left that 8976 05:33:24,320 --> 05:33:28,520 nothing happens yet to the value of n 8977 05:33:26,760 --> 05:33:30,878 until I go to the terminal window now 8978 05:33:28,520 --> 05:33:33,320 and type in something like Nega 1 now 8979 05:33:30,878 --> 05:33:36,160 notice it jumps to line 19 which is the 8980 05:33:33,320 --> 05:33:38,558 next interesting line top left n indeed 8981 05:33:36,160 --> 05:33:41,120 is1 and here's where I can now pause as 8982 05:33:38,558 --> 05:33:43,600 a human and think all right so while n 8983 05:33:41,120 --> 05:33:45,680 is less than zero all right n per the 8984 05:33:43,600 --> 05:33:47,680 top left corner is negative 1 so all 8985 05:33:45,680 --> 05:33:48,920 right while negative 1 is less than zero 8986 05:33:47,680 --> 05:33:50,798 well obviously that's true 8987 05:33:48,920 --> 05:33:52,958 mathematically so what's going to happen 8988 05:33:50,798 --> 05:33:56,878 it's a do while loop so when I click on 8989 05:33:52,958 --> 05:33:58,400 step over again it's going to go to this 8990 05:33:56,878 --> 05:34:01,320 line cuz it's at the end of the inside 8991 05:33:58,400 --> 05:34:03,000 of that Loop and now here it's looping 8992 05:34:01,320 --> 05:34:05,558 through again and again all right let me 8993 05:34:03,000 --> 05:34:07,480 do this once more I'm going to step over 8994 05:34:05,558 --> 05:34:09,000 all right I'm going to type in -2 and 8995 05:34:07,480 --> 05:34:10,680 it's the exact same thing now is my 8996 05:34:09,000 --> 05:34:13,320 chance on the yellow line okay wait a 8997 05:34:10,680 --> 05:34:16,240 minute -2 is obviously less than zero 8998 05:34:13,320 --> 05:34:17,840 let me try this one more time click it 8999 05:34:16,240 --> 05:34:23,160 once here and now all right let me give 9000 05:34:17,840 --> 05:34:26,480 it 50 and now okay while 50 is less than 9001 05:34:23,160 --> 05:34:28,080 zero that's not true so the loop is over 9002 05:34:26,480 --> 05:34:29,958 because it's not going to do it while 50 9003 05:34:28,080 --> 05:34:32,280 is less than zero that's not true so now 9004 05:34:29,958 --> 05:34:34,520 watch when I click step over once more 9005 05:34:32,280 --> 05:34:36,120 it then finishes the loop even though 9006 05:34:34,520 --> 05:34:38,680 there's nothing more to do it's now 9007 05:34:36,120 --> 05:34:41,440 about to return n it jumps back up to 9008 05:34:38,680 --> 05:34:43,000 main where I left off on line nine it 9009 05:34:41,440 --> 05:34:44,878 now prints in my terminal window the 9010 05:34:43,000 --> 05:34:46,878 number 50 and hopefully at this point to 9011 05:34:44,878 --> 05:34:49,160 your question earlier my human brain has 9012 05:34:46,878 --> 05:34:51,480 realized oh I'm an idiot like I flipped 9013 05:34:49,160 --> 05:34:53,520 my my sign there so I probably let me 9014 05:34:51,480 --> 05:34:55,200 stop this I probably want to do 9015 05:34:53,520 --> 05:34:57,840 something like this if the goal is to 9016 05:34:55,200 --> 05:35:02,200 get a negative integer I probably want 9017 05:34:57,840 --> 05:35:05,280 to say well n is for instance greater 9018 05:35:02,200 --> 05:35:07,400 than or equal to zero would work so 9019 05:35:05,280 --> 05:35:09,480 while n is greater than or equal to zero 9020 05:35:07,400 --> 05:35:11,080 keep doing this and that's the logic I 9021 05:35:09,480 --> 05:35:12,760 wanted to express so the debugger just 9022 05:35:11,080 --> 05:35:14,440 saves me from staring at the screen 9023 05:35:12,760 --> 05:35:16,120 raising a hand sort of asking someone 9024 05:35:14,440 --> 05:35:18,600 else at least in this case it allows me 9025 05:35:16,120 --> 05:35:20,798 to go through it at a healthier Pace 9026 05:35:18,600 --> 05:35:22,920 questions now on debug 50 which should 9027 05:35:20,798 --> 05:35:26,480 be your new friend even if it's not your 9028 05:35:22,920 --> 05:35:26,480 first instinct after 9029 05:35:26,600 --> 05:35:31,480 printf any questions on debug 50 no all 9030 05:35:29,680 --> 05:35:33,840 right all right well there's one last 9031 05:35:31,480 --> 05:35:35,400 Technique we can equip you with here um 9032 05:35:33,840 --> 05:35:38,240 and that is in addition to printf and a 9033 05:35:35,400 --> 05:35:40,638 debugger no joke um a rubber duck is 9034 05:35:38,240 --> 05:35:42,558 actually a reasonably recommended uh 9035 05:35:40,638 --> 05:35:44,240 solution to finding bugs in your code to 9036 05:35:42,558 --> 05:35:46,120 your question earlier the duck two is 9037 05:35:44,240 --> 05:35:47,718 not going to solve the problem for you 9038 05:35:46,120 --> 05:35:49,718 but if you've wondered why this little 9039 05:35:47,718 --> 05:35:51,400 guy has been here for so long um there's 9040 05:35:49,718 --> 05:35:53,520 this technique has its own Wikipedia 9041 05:35:51,400 --> 05:35:56,240 article of called rubber duck debugging 9042 05:35:53,520 --> 05:35:57,840 the idea of which is that if you're home 9043 05:35:56,240 --> 05:35:59,840 in your dorm room wrestling with some 9044 05:35:57,840 --> 05:36:01,798 bug in your code printf didn't quite 9045 05:35:59,840 --> 05:36:03,480 reveal the source to you debugger isn't 9046 05:36:01,798 --> 05:36:06,000 really helping honestly maybe it would 9047 05:36:03,480 --> 05:36:07,798 help to just sound out what problem 9048 05:36:06,000 --> 05:36:10,040 you're having similar to going to office 9049 05:36:07,798 --> 05:36:11,360 hours talking to a a TA or a professor 9050 05:36:10,040 --> 05:36:13,240 just walking through your problems 9051 05:36:11,360 --> 05:36:14,958 because in sort of talking to the duck 9052 05:36:13,240 --> 05:36:19,080 about you know the fact that you're 9053 05:36:14,958 --> 05:36:21,240 doing this while uh n is less than zero 9054 05:36:19,080 --> 05:36:23,360 and then if it is I wait a minute I'm an 9055 05:36:21,240 --> 05:36:25,400 idiot not just for talking to the rubber 9056 05:36:23,360 --> 05:36:27,840 duck you realize hopefully in expressing 9057 05:36:25,400 --> 05:36:29,958 yourself literally verbally you probably 9058 05:36:27,840 --> 05:36:31,920 will hear with non-zero probability like 9059 05:36:29,958 --> 05:36:33,638 some illogic in your statement and just 9060 05:36:31,920 --> 05:36:37,040 by sounding things out you'll realize 9061 05:36:33,638 --> 05:36:38,558 like oh that's my problem and so frankly 9062 05:36:37,040 --> 05:36:40,440 if you have roommates you can also use a 9063 05:36:38,558 --> 05:36:42,000 roommate for this but the rubber duck is 9064 05:36:40,440 --> 05:36:43,840 just sort of a go-to when your roommates 9065 05:36:42,000 --> 05:36:45,760 have no interest in your you know C 9066 05:36:43,840 --> 05:36:48,680 problem set talking something through 9067 05:36:45,760 --> 05:36:50,798 that um as follow as such and this is an 9068 05:36:48,680 --> 05:36:52,160 invaluable technique I admittedly tend 9069 05:36:50,798 --> 05:36:53,760 not to do it so much with a rubber duck 9070 05:36:52,160 --> 05:36:55,718 but ideally with colleagues human 9071 05:36:53,760 --> 05:36:58,200 colleagues but just talking through 9072 05:36:55,718 --> 05:37:00,878 things often will help you just realize 9073 05:36:58,200 --> 05:37:03,320 oh I said something logical now I can go 9074 05:37:00,878 --> 05:37:05,120 back to the code so don't solve problems 9075 05:37:03,320 --> 05:37:06,638 by staring at your screen endlessly for 9076 05:37:05,120 --> 05:37:08,638 minutes for hours at that point it's 9077 05:37:06,638 --> 05:37:10,120 time for a break time to walk away time 9078 05:37:08,638 --> 05:37:12,760 to talk to the duck if you've already 9079 05:37:10,120 --> 05:37:14,520 exhausted some of those other tools um 9080 05:37:12,760 --> 05:37:16,600 as an aside on your way out today at the 9081 05:37:14,520 --> 05:37:19,798 end of class we have H clearly plenty of 9082 05:37:16,600 --> 05:37:21,400 rubber ducks uh for you um and uh it's 9083 05:37:19,798 --> 05:37:23,440 become a thing over the years at least 9084 05:37:21,400 --> 05:37:25,600 among some uh to bring the duck with 9085 05:37:23,440 --> 05:37:28,240 them when they travel and send us photos 9086 05:37:25,600 --> 05:37:31,958 here for instance is uh cs50's rubber 9087 05:37:28,240 --> 05:37:34,120 duck debugger AKA ddb for duck debugger 9088 05:37:31,958 --> 05:37:36,680 which is a pun on a geekier program 9089 05:37:34,120 --> 05:37:38,558 called GDB the ganu debugger which is an 9090 05:37:36,680 --> 05:37:41,280 actual piece of software for debugging 9091 05:37:38,558 --> 05:37:44,920 this is cs50's debugger uh in the hills 9092 05:37:41,280 --> 05:37:47,920 of Puerto Rico uh also here on the sea 9093 05:37:44,920 --> 05:37:49,600 uh he made its way to San Francisco here 9094 05:37:47,920 --> 05:37:52,000 uh also down by Fisherman's dwarf by the 9095 05:37:49,600 --> 05:37:53,638 sea lions if familiar uh here at 9096 05:37:52,000 --> 05:37:55,440 Stanford where there's a William Gates 9097 05:37:53,638 --> 05:37:58,718 computer science building for computer 9098 05:37:55,440 --> 05:38:01,680 science uh down the road in SF at Google 9099 05:37:58,718 --> 05:38:04,000 uh and this is the Tre fountain in Rome 9100 05:38:01,680 --> 05:38:05,360 and lastly uh the Coliseum so we'll be 9101 05:38:04,000 --> 05:38:08,240 curious to see in the coming years where 9102 05:38:05,360 --> 05:38:10,080 your duck two travels so that then was 9103 05:38:08,240 --> 05:38:11,840 quite a bit why don't we go ahead here 9104 05:38:10,080 --> 05:38:13,558 and take a short five minute break no 9105 05:38:11,840 --> 05:38:17,360 snacks yet you're welcome to get up or 9106 05:38:13,558 --> 05:38:20,558 sit down we'll return in about five all 9107 05:38:17,360 --> 05:38:23,120 right so we are back and if the goal 9108 05:38:20,558 --> 05:38:24,760 ultimately today is to have a better 9109 05:38:23,120 --> 05:38:26,878 understanding of things like strings so 9110 05:38:24,760 --> 05:38:28,718 that we can solve problems with text 9111 05:38:26,878 --> 05:38:30,760 let's consider some simpler types of 9112 05:38:28,718 --> 05:38:32,320 data first how we might represent those 9113 05:38:30,760 --> 05:38:34,240 and then see if that doesn't lead us to 9114 05:38:32,320 --> 05:38:36,320 a discovery as to like how strings in 9115 05:38:34,240 --> 05:38:38,680 just today's modern software is using 9116 05:38:36,320 --> 05:38:41,840 things like that so when we talked on 9117 05:38:38,680 --> 05:38:43,840 week zero about representation of data 9118 05:38:41,840 --> 05:38:46,920 we had different ways of doing it in 9119 05:38:43,840 --> 05:38:48,718 terms of binary and decimal and uh unary 9120 05:38:46,920 --> 05:38:50,798 even when we started talking about the 9121 05:38:48,718 --> 05:38:54,120 same last week in code we started 9122 05:38:50,798 --> 05:38:56,600 talking about uh data types instead and 9123 05:38:54,120 --> 05:38:58,400 these data types were a way of telling 9124 05:38:56,600 --> 05:39:00,638 the computer like do you want an integer 9125 05:38:58,400 --> 05:39:03,040 do you want a character do you want a 9126 05:39:00,638 --> 05:39:05,320 floating point value like a real number 9127 05:39:03,040 --> 05:39:07,478 or even a string as we've seen but it 9128 05:39:05,320 --> 05:39:10,200 turns out that computers of course only 9129 05:39:07,478 --> 05:39:12,520 have finite amounts of resources your 9130 05:39:10,200 --> 05:39:14,360 computer only has a fixed amount of 9131 05:39:12,520 --> 05:39:16,718 memory or RAM and that actually has very 9132 05:39:14,360 --> 05:39:18,160 real world implications so for instance 9133 05:39:16,718 --> 05:39:21,000 here are some of the data types we've 9134 05:39:18,160 --> 05:39:24,120 seen thus far and it turns out that each 9135 05:39:21,000 --> 05:39:26,798 of these in C has a specific number of 9136 05:39:24,120 --> 05:39:28,878 bits allocated to it now admittedly this 9137 05:39:26,798 --> 05:39:30,718 can vary by System it's not so much the 9138 05:39:28,878 --> 05:39:32,080 case nowaday days but for many years for 9139 05:39:30,718 --> 05:39:34,000 decades computers were getting better 9140 05:39:32,080 --> 05:39:35,760 and better the earliest computers might 9141 05:39:34,000 --> 05:39:37,798 have used fewer bits for some of these 9142 05:39:35,760 --> 05:39:39,160 data types more modern computers might 9143 05:39:37,798 --> 05:39:40,440 use more bits so the numbers you're 9144 05:39:39,160 --> 05:39:42,520 about to see are pretty much where we 9145 05:39:40,440 --> 05:39:46,280 are present day so when it comes to 9146 05:39:42,520 --> 05:39:48,478 these data types A bu which is true or 9147 05:39:46,280 --> 05:39:50,558 false somewhat curiously uses a whole 9148 05:39:48,478 --> 05:39:52,360 bite even though that's way Overkill 9149 05:39:50,558 --> 05:39:54,400 because for a bull true or false you of 9150 05:39:52,360 --> 05:39:56,360 course only need one bit but it turns 9151 05:39:54,400 --> 05:39:59,080 out even though it's wasteful to use 9152 05:39:56,360 --> 05:40:00,600 eight bits or one bite just to represent 9153 05:39:59,080 --> 05:40:02,760 true or false it's just easier for 9154 05:40:00,600 --> 05:40:04,920 computers so a bull tends to be one bite 9155 05:40:02,760 --> 05:40:07,680 an INT which we've been using a lot uses 9156 05:40:04,920 --> 05:40:09,600 four bytes typically or 32 bits and if I 9157 05:40:07,680 --> 05:40:12,400 do some quick math from week zero with 9158 05:40:09,600 --> 05:40:14,478 32 bits you have four billion possible 9159 05:40:12,400 --> 05:40:16,200 values roughly but if you want to 9160 05:40:14,478 --> 05:40:18,240 represent positive and negative that 9161 05:40:16,200 --> 05:40:20,440 means you can represent roughly -2 9162 05:40:18,240 --> 05:40:21,958 billion all the way up to positive2 9163 05:40:20,440 --> 05:40:25,000 billion so that's the range typically 9164 05:40:21,958 --> 05:40:26,638 within if that's to few numbers for you 9165 05:40:25,000 --> 05:40:29,718 turns out there's things called Longs 9166 05:40:26,638 --> 05:40:32,320 and longs use 64 bits which allow you to 9167 05:40:29,718 --> 05:40:34,160 have like a quintilian number of 9168 05:40:32,320 --> 05:40:36,400 possibilities which is a lot certainly a 9169 05:40:34,160 --> 05:40:38,680 lot more than 4 billion so sometimes you 9170 05:40:36,400 --> 05:40:40,920 might use a long but even that's finite 9171 05:40:38,680 --> 05:40:42,798 and so uh as we discussed at the end of 9172 05:40:40,920 --> 05:40:44,558 last week bad things can happen if you 9173 05:40:42,798 --> 05:40:46,600 make certain assumptions as the data 9174 05:40:44,558 --> 05:40:48,360 because of things like Inger overflow or 9175 05:40:46,600 --> 05:40:50,120 the like where things wrap around then 9176 05:40:48,360 --> 05:40:51,360 there's a float which is a real number 9177 05:40:50,120 --> 05:40:54,680 something with a decimal point by 9178 05:40:51,360 --> 05:40:56,760 convention it's four bytes or 32 bits 9179 05:40:54,680 --> 05:40:57,958 which gives you in short only a specific 9180 05:40:56,760 --> 05:41:00,280 amount of precision it doesn't 9181 05:40:57,958 --> 05:41:01,680 necessarily dictate how many numbers to 9182 05:41:00,280 --> 05:41:04,558 the left or to the right in the 9183 05:41:01,680 --> 05:41:07,000 aggregate ultimately you have though uh 9184 05:41:04,558 --> 05:41:08,920 three uh 4 billion possible permutations 9185 05:41:07,000 --> 05:41:10,840 still if you need more Precision for 9186 05:41:08,920 --> 05:41:13,280 scientific for medical for financial 9187 05:41:10,840 --> 05:41:16,360 applications you might use eight bytes 9188 05:41:13,280 --> 05:41:18,280 AKA a double which just gives you more 9189 05:41:16,360 --> 05:41:20,558 digits of precision they eventually get 9190 05:41:18,280 --> 05:41:22,200 imprecise per the example we looked at 9191 05:41:20,558 --> 05:41:24,718 last week but it at least gets you 9192 05:41:22,200 --> 05:41:27,160 further down the line as an aside in 9193 05:41:24,718 --> 05:41:29,240 really really important applications in 9194 05:41:27,160 --> 05:41:30,878 finance and medicine and Military 9195 05:41:29,240 --> 05:41:33,080 operations and the like where you really 9196 05:41:30,878 --> 05:41:35,718 can't have rounding errors long story 9197 05:41:33,080 --> 05:41:38,520 short humans have developed libraries in 9198 05:41:35,718 --> 05:41:40,280 C and other languages that use more even 9199 05:41:38,520 --> 05:41:41,958 than 8 bytes so there are solutions to 9200 05:41:40,280 --> 05:41:44,160 these problems but they're always finite 9201 05:41:41,958 --> 05:41:46,160 you have to pick uh an upper bound then 9202 05:41:44,160 --> 05:41:48,478 there's Char which we saw briefly last 9203 05:41:46,160 --> 05:41:50,718 week when I asked the user for y or n 9204 05:41:48,478 --> 05:41:51,958 for yes or no and then there's string 9205 05:41:50,718 --> 05:41:54,320 which I'm going to propose as a question 9206 05:41:51,958 --> 05:41:57,360 mark because a string totally depends 9207 05:41:54,320 --> 05:42:00,160 like high h i exclamation point would 9208 05:41:57,360 --> 05:42:02,600 seem to be three bytes d a ID would seem 9209 05:42:00,160 --> 05:42:04,638 to be five so strings clearly are 9210 05:42:02,600 --> 05:42:06,478 variable based on what you or the human 9211 05:42:04,638 --> 05:42:09,200 type in so we'll see what this means 9212 05:42:06,478 --> 05:42:11,040 though in just a bit this though is the 9213 05:42:09,200 --> 05:42:13,000 thing inside of your Mac your PC your 9214 05:42:11,040 --> 05:42:15,200 phone might not look exactly like this 9215 05:42:13,000 --> 05:42:16,920 but this is a a memory module for a 9216 05:42:15,200 --> 05:42:18,360 modern computer and let's go ahead and 9217 05:42:16,920 --> 05:42:20,080 use this really as just representative 9218 05:42:18,360 --> 05:42:21,958 of the finite amount of memory that any 9219 05:42:20,080 --> 05:42:24,478 computer indeed has let's zoom in on one 9220 05:42:21,958 --> 05:42:27,280 of these little black chips on the uh uh 9221 05:42:24,478 --> 05:42:29,440 circuit board here zoom in and let me 9222 05:42:27,280 --> 05:42:31,600 propose that this rectangle really 9223 05:42:29,440 --> 05:42:33,760 represents some number of bytes like 9224 05:42:31,600 --> 05:42:35,718 tucked inside of this little black uh 9225 05:42:33,760 --> 05:42:37,600 circuit on the board is maybe I don't 9226 05:42:35,718 --> 05:42:39,200 know a gigabyte a billion bytes maybe 9227 05:42:37,600 --> 05:42:40,638 it's a 100 btes some number of bytes it 9228 05:42:39,200 --> 05:42:42,718 totally depends on the computer and how 9229 05:42:40,638 --> 05:42:45,600 much you paid for the Stick of memory 9230 05:42:42,718 --> 05:42:48,280 but if there's a finite number of bytes 9231 05:42:45,600 --> 05:42:50,160 physically implemented somehow digitally 9232 05:42:48,280 --> 05:42:51,638 inside of this Hardware well then it 9233 05:42:50,160 --> 05:42:53,440 stands to reason that we could number 9234 05:42:51,638 --> 05:42:56,400 those bytes we can just arbitrarily 9235 05:42:53,440 --> 05:42:58,638 decide that the top left corner is bite 9236 05:42:56,400 --> 05:43:00,360 number one or really bite number zero 9237 05:42:58,638 --> 05:43:02,360 the one next to it is number one then 9238 05:43:00,360 --> 05:43:04,120 number two number three dot dot dot 9239 05:43:02,360 --> 05:43:06,400 number two billion or whatever it is 9240 05:43:04,120 --> 05:43:09,080 however big this this memory is so if 9241 05:43:06,400 --> 05:43:11,520 you are use a variable in a c program 9242 05:43:09,080 --> 05:43:13,200 that's only one bite like a Char it 9243 05:43:11,520 --> 05:43:15,080 might literally be stored in that top 9244 05:43:13,200 --> 05:43:17,040 left hand corner of the memory like in 9245 05:43:15,080 --> 05:43:19,360 practice you don't care where physically 9246 05:43:17,040 --> 05:43:21,440 it is but really the artist rendition 9247 05:43:19,360 --> 05:43:23,360 would be this a Char might use one of 9248 05:43:21,440 --> 05:43:25,320 those single btes somewhere in the 9249 05:43:23,360 --> 05:43:27,200 computer's memory if you use an INT 9250 05:43:25,320 --> 05:43:29,600 which is four bytes it would give you 9251 05:43:27,200 --> 05:43:32,000 four bytes contiguous that is left to 9252 05:43:29,600 --> 05:43:33,280 right top to bottom but all 32 bits 9253 05:43:32,000 --> 05:43:34,798 would be next to each other so the 9254 05:43:33,280 --> 05:43:36,478 computer knows that those indeed all 9255 05:43:34,798 --> 05:43:38,600 belong to the same int if you need a 9256 05:43:36,478 --> 05:43:40,600 long or a double for that matter then 9257 05:43:38,600 --> 05:43:42,718 you might use a full eight bytes in this 9258 05:43:40,600 --> 05:43:44,638 case and you just keep using and using 9259 05:43:42,718 --> 05:43:46,200 this memory kind of like a a canvas you 9260 05:43:44,638 --> 05:43:48,280 know almost in Photoshop or a 9261 05:43:46,200 --> 05:43:49,878 spreadsheet where you can just move uh 9262 05:43:48,280 --> 05:43:51,080 pixels or you can move data around 9263 05:43:49,878 --> 05:43:53,638 that's really what your computer's 9264 05:43:51,080 --> 05:43:57,040 memory is a canvas for storing uh 9265 05:43:53,638 --> 05:43:58,200 information in units of bytes or 8 Bits 9266 05:43:57,040 --> 05:43:59,840 now we don't need to keep looking at 9267 05:43:58,200 --> 05:44:01,760 these circuit boards can abstract it 9268 05:43:59,840 --> 05:44:03,280 away as we often do and let's go ahead 9269 05:44:01,760 --> 05:44:05,520 and zoom in on this grid just to 9270 05:44:03,280 --> 05:44:07,680 consider some very specific variables so 9271 05:44:05,520 --> 05:44:09,840 let me zoom in and now I see fewer but 9272 05:44:07,680 --> 05:44:11,878 larger uh boxes on the screen Each of 9273 05:44:09,840 --> 05:44:13,958 which again represents a bite and now 9274 05:44:11,878 --> 05:44:16,878 let me propose that we play with some 9275 05:44:13,958 --> 05:44:19,320 actual code so here in C albeit without 9276 05:44:16,878 --> 05:44:21,360 a full program or three ins score one 9277 05:44:19,320 --> 05:44:24,958 score two score three I have 9278 05:44:21,360 --> 05:44:27,320 coincidentally given myself two uh uh 9279 05:44:24,958 --> 05:44:28,958 two scores at around 72 and 73 and then 9280 05:44:27,320 --> 05:44:30,798 a pretty low score at 33 of course 9281 05:44:28,958 --> 05:44:32,320 course last week or two weeks ago this 9282 05:44:30,798 --> 05:44:33,920 would have been high but now we're 9283 05:44:32,320 --> 05:44:36,360 dealing with actual integers so these 9284 05:44:33,920 --> 05:44:38,400 are three so so scores on my quizzes or 9285 05:44:36,360 --> 05:44:40,080 tests or the like so let me go to vs 9286 05:44:38,400 --> 05:44:42,840 code here and let's make a program 9287 05:44:40,080 --> 05:44:44,958 called scores. C so I'm going to write 9288 05:44:42,840 --> 05:44:46,798 code scores. C that's going to give me 9289 05:44:44,958 --> 05:44:49,240 my new file and let me go ahead and 9290 05:44:46,798 --> 05:44:53,120 Implement something like this include 9291 05:44:49,240 --> 05:44:55,638 standard i.h int main void and then 9292 05:44:53,120 --> 05:44:59,958 inside of here let me do int score one 9293 05:44:55,638 --> 05:45:03,400 will be 72 uh int score two be 73 and 9294 05:44:59,958 --> 05:45:04,920 int score three will be 33 and then let 9295 05:45:03,400 --> 05:45:06,680 me just do something like write a 9296 05:45:04,920 --> 05:45:08,200 program to average my three test scores 9297 05:45:06,680 --> 05:45:12,160 together something like that so let me 9298 05:45:08,200 --> 05:45:14,120 do print F quote unquote my average is 9299 05:45:12,160 --> 05:45:17,320 and I'm going to go ahead and do say 9300 05:45:14,120 --> 05:45:19,040 percent I back sln and now let me plug 9301 05:45:17,320 --> 05:45:20,558 in the results and this is kind of grade 9302 05:45:19,040 --> 05:45:22,760 school math now how do I compute the 9303 05:45:20,558 --> 05:45:26,520 average of three values well just like 9304 05:45:22,760 --> 05:45:29,120 in on paper I can do score one plus 9305 05:45:26,520 --> 05:45:30,558 score two plus score three in 9306 05:45:29,120 --> 05:45:32,840 parentheses because of order of 9307 05:45:30,558 --> 05:45:34,878 operations divided by three since 9308 05:45:32,840 --> 05:45:36,280 there's three total scores all right so 9309 05:45:34,878 --> 05:45:38,040 I think this checks out and indeed you 9310 05:45:36,280 --> 05:45:40,718 can use parentheses and operators like 9311 05:45:38,040 --> 05:45:43,360 plus and your code like this in C let me 9312 05:45:40,718 --> 05:45:45,000 go ahead now and do make scores no 9313 05:45:43,360 --> 05:45:46,920 syntax error so that's good nothing 9314 05:45:45,000 --> 05:45:48,680 missing there and now let me do dot 9315 05:45:46,920 --> 05:45:50,400 scores and see what my test average is 9316 05:45:48,680 --> 05:45:53,040 all right you know it's not great but I 9317 05:45:50,400 --> 05:45:57,718 I I think I still passed and indeed my 9318 05:45:53,040 --> 05:45:59,400 average here is is 59 is it precisely 59 9319 05:45:57,718 --> 05:46:01,160 though well let's see let's let's 9320 05:45:59,400 --> 05:46:02,638 actually instead of using an INT how 9321 05:46:01,160 --> 05:46:05,200 about we go ahead and use something like 9322 05:46:02,638 --> 05:46:07,080 a floating point value here and let me 9323 05:46:05,200 --> 05:46:09,840 go ahead and do this so let me recompile 9324 05:46:07,080 --> 05:46:11,240 my code make scores huh all right I've 9325 05:46:09,840 --> 05:46:13,280 got an issue let me zoom in on my 9326 05:46:11,240 --> 05:46:15,718 terminal window we've not seen this one 9327 05:46:13,280 --> 05:46:19,200 necessarily before but error on line 9328 05:46:15,718 --> 05:46:21,080 nine format specifies type double which 9329 05:46:19,200 --> 05:46:23,120 is a lot of precision but the argument 9330 05:46:21,080 --> 05:46:24,120 has Type in so what does this mean well 9331 05:46:23,120 --> 05:46:26,080 it's showing me with these green 9332 05:46:24,120 --> 05:46:28,878 squigglies that something's bad between 9333 05:46:26,080 --> 05:46:31,400 the percent F and this thing over here 9334 05:46:28,878 --> 05:46:33,360 well on the left I'm implying a float or 9335 05:46:31,400 --> 05:46:34,760 a double for that matter on the right 9336 05:46:33,360 --> 05:46:37,240 though what data type are score one 9337 05:46:34,760 --> 05:46:39,400 score two score three all right so 9338 05:46:37,240 --> 05:46:40,958 they're ins so Cent does not like this 9339 05:46:39,400 --> 05:46:43,040 the compiler just doesn't like that I'm 9340 05:46:40,958 --> 05:46:44,840 using ins on the right but I want floats 9341 05:46:43,040 --> 05:46:47,080 on the left so there's going to be 9342 05:46:44,840 --> 05:46:48,840 different ways of solving this one way 9343 05:46:47,080 --> 05:46:51,120 would be to just ignore the problem like 9344 05:46:48,840 --> 05:46:55,120 I originally did and just go back to 9345 05:46:51,120 --> 05:46:57,280 percent I or as an aside percent D is 9346 05:46:55,120 --> 05:46:59,680 often an alternative to percent I for a 9347 05:46:57,280 --> 05:47:01,638 decimal number but we use perc I cuz it 9348 05:46:59,680 --> 05:47:03,478 sounds like int so percent I is fine 9349 05:47:01,638 --> 05:47:05,280 here too but I don't want to just avoid 9350 05:47:03,478 --> 05:47:07,120 the problem I want to actually display a 9351 05:47:05,280 --> 05:47:08,718 floating point value so how can I fix 9352 05:47:07,120 --> 05:47:11,160 this well it turns out I can solve this 9353 05:47:08,718 --> 05:47:12,798 in a few different ways the simplest is 9354 05:47:11,160 --> 05:47:14,878 just to make sure that at least one 9355 05:47:12,798 --> 05:47:16,798 number on the right is a floating point 9356 05:47:14,878 --> 05:47:20,320 value like 9357 05:47:16,798 --> 05:47:22,478 3.0 instead of just three now I think 9358 05:47:20,320 --> 05:47:25,958 clang will be happier let me do make 9359 05:47:22,478 --> 05:47:28,040 scores enter and indeed it's okay why as 9360 05:47:25,958 --> 05:47:30,040 soon as you have at least one more 9361 05:47:28,040 --> 05:47:31,958 precise data data type on the right it 9362 05:47:30,040 --> 05:47:33,478 just treats everything at that point as 9363 05:47:31,958 --> 05:47:36,840 floating point value so that the math 9364 05:47:33,478 --> 05:47:38,958 works out so/ scores enter and now there 9365 05:47:36,840 --> 05:47:40,400 we go right you know uh some of us might 9366 05:47:38,958 --> 05:47:43,680 really want that third of a point our 9367 05:47:40,400 --> 05:47:45,360 average was not 59 it's 59 and a third 9368 05:47:43,680 --> 05:47:47,680 as in this case here all right so we've 9369 05:47:45,360 --> 05:47:49,878 solved that there as an aside though 9370 05:47:47,680 --> 05:47:51,638 there's one other technique to uh to 9371 05:47:49,878 --> 05:47:53,878 show here if you didn't want to change 9372 05:47:51,638 --> 05:47:55,638 it to 3.0 because that's a little weird 9373 05:47:53,878 --> 05:47:57,558 because I there were literally three 9374 05:47:55,638 --> 05:47:59,558 scores it's not like that needs to have 9375 05:47:57,558 --> 05:48:03,920 a decimal point you could also 9376 05:47:59,558 --> 05:48:06,160 explicitly convert the three to a float 9377 05:48:03,920 --> 05:48:08,558 by saying in parentheses float this is 9378 05:48:06,160 --> 05:48:10,760 what's called type casting and this will 9379 05:48:08,558 --> 05:48:12,558 just convert the thing right after it to 9380 05:48:10,760 --> 05:48:15,360 that data type if it's possible so if I 9381 05:48:12,558 --> 05:48:17,718 do this again make scores no errors now 9382 05:48:15,360 --> 05:48:19,600 do/ scores and I get in fact the same 9383 05:48:17,718 --> 05:48:21,400 result there's a bit of a rounding issue 9384 05:48:19,600 --> 05:48:24,000 here but we know the rounding relates to 9385 05:48:21,400 --> 05:48:26,240 the imprecision from last week for now 9386 05:48:24,000 --> 05:48:28,760 let me just be happy with my 59.3 9387 05:48:26,240 --> 05:48:32,200 something I'll take that for now but 9388 05:48:28,760 --> 05:48:34,958 this is you know is close to um a good 9389 05:48:32,200 --> 05:48:37,240 enough correct answer for me now but how 9390 05:48:34,958 --> 05:48:38,558 do I think about now what's going on 9391 05:48:37,240 --> 05:48:40,000 inside of the computer's memory well 9392 05:48:38,558 --> 05:48:42,958 let's consider here's that same grid of 9393 05:48:40,000 --> 05:48:44,798 memory each box represents a bite where 9394 05:48:42,958 --> 05:48:47,280 are score one score two and score three 9395 05:48:44,798 --> 05:48:48,840 in my memory well score one let me just 9396 05:48:47,280 --> 05:48:51,958 propose is at the top left but it's 9397 05:48:48,840 --> 05:48:54,558 taking up four boxes for four bytes 9398 05:48:51,958 --> 05:48:55,920 score two probably ends up right next to 9399 05:48:54,558 --> 05:48:57,080 it in memory though this isn't always 9400 05:48:55,920 --> 05:48:59,798 going to be the case but I've chosen 9401 05:48:57,080 --> 05:49:02,240 simple examples 73 is next to it also 9402 05:48:59,798 --> 05:49:04,558 taking up four bytes and then lastly 33 9403 05:49:02,240 --> 05:49:07,280 is in score three uh down there 9404 05:49:04,558 --> 05:49:08,798 underneath now if we don't if we really 9405 05:49:07,280 --> 05:49:10,400 look at the computer's memory look at it 9406 05:49:08,798 --> 05:49:13,878 with some kind of microscope or the like 9407 05:49:10,400 --> 05:49:17,478 there's actually 32 bits 32 bits 32 bits 9408 05:49:13,878 --> 05:49:19,360 in each of those four uh groups of four 9409 05:49:17,478 --> 05:49:20,878 bytes representing those values but 9410 05:49:19,360 --> 05:49:22,320 again for today's purposes onwards we 9411 05:49:20,878 --> 05:49:23,878 don't really need to think again and 9412 05:49:22,320 --> 05:49:26,080 again in binary it's just indeed these 9413 05:49:23,878 --> 05:49:28,120 decimal numbers being stored there but I 9414 05:49:26,080 --> 05:49:29,760 claim now this isn't the best design 9415 05:49:28,120 --> 05:49:32,120 even if if you have never programmed 9416 05:49:29,760 --> 05:49:34,558 before cs50 what you're looking at here 9417 05:49:32,120 --> 05:49:36,478 on the screen as an excerpt in what 9418 05:49:34,558 --> 05:49:37,798 sense is this perhaps bad design even 9419 05:49:36,478 --> 05:49:39,000 though it's a correct way of storing 9420 05:49:37,798 --> 05:49:42,878 three test 9421 05:49:39,000 --> 05:49:42,878 scores what's kind of bad here 9422 05:49:45,280 --> 05:49:50,558 yeah yeah always do exactly what you did 9423 05:49:48,000 --> 05:49:52,878 extrapolate to four scores five scores 9424 05:49:50,558 --> 05:49:54,000 50 scores this can't be that welld 9425 05:49:52,878 --> 05:49:55,878 designed because now you're going to 9426 05:49:54,000 --> 05:49:57,600 have four lines of code five lines of 9427 05:49:55,878 --> 05:49:59,680 code 50 lines of code that are almost 9428 05:49:57,600 --> 05:50:01,478 identical except for this like arbitrary 9429 05:49:59,680 --> 05:50:03,760 number that we're updating at the end of 9430 05:50:01,478 --> 05:50:05,840 the variable so indeed there's probably 9431 05:50:03,760 --> 05:50:08,040 going to be a better way even though at 9432 05:50:05,840 --> 05:50:10,320 least in C we haven't yet seen that 9433 05:50:08,040 --> 05:50:12,400 technique but the solution today onward 9434 05:50:10,320 --> 05:50:15,840 is going to be something called an array 9435 05:50:12,400 --> 05:50:18,200 an array is a uh way of storing your 9436 05:50:15,840 --> 05:50:20,638 data back to back to back in the 9437 05:50:18,200 --> 05:50:24,000 computer's memory in such a way that you 9438 05:50:20,638 --> 05:50:26,360 can access each individual member easily 9439 05:50:24,000 --> 05:50:28,798 put another way with an array you can 9440 05:50:26,360 --> 05:50:31,320 instead do something like this instead 9441 05:50:28,798 --> 05:50:33,840 of saying int score one int score 2 in 9442 05:50:31,320 --> 05:50:36,718 score three giving each a value you can 9443 05:50:33,840 --> 05:50:39,120 first tell the computer please give me a 9444 05:50:36,718 --> 05:50:41,280 variable called scores plural that you 9445 05:50:39,120 --> 05:50:44,040 can call it anything you want of size 9446 05:50:41,280 --> 05:50:45,958 three Each of which will be an integer 9447 05:50:44,040 --> 05:50:49,240 that is to say this is how you declare 9448 05:50:45,958 --> 05:50:51,840 an array in C that will have enough room 9449 05:50:49,240 --> 05:50:53,680 to store three integers put another way 9450 05:50:51,840 --> 05:50:57,958 this is the technical way of telling the 9451 05:50:53,680 --> 05:50:58,920 computer please give me uh 12 bytes in 9452 05:50:57,958 --> 05:51:01,798 total 9453 05:50:58,920 --> 05:51:03,120 3 * 4 each for an inch so give me 12 9454 05:51:01,798 --> 05:51:05,160 bytes in total and what the computer 9455 05:51:03,120 --> 05:51:07,040 will do is guarantee that they're back 9456 05:51:05,160 --> 05:51:09,240 to back to back in the computer's memory 9457 05:51:07,040 --> 05:51:10,760 and that'll be useful in just a moment 9458 05:51:09,240 --> 05:51:12,400 so let me go ahead and do something 9459 05:51:10,760 --> 05:51:14,718 useful with this let me store three 9460 05:51:12,400 --> 05:51:17,718 actual scores here's how I could now 9461 05:51:14,718 --> 05:51:20,520 store those same numeric scores in this 9462 05:51:17,718 --> 05:51:23,000 array syntax is a little different but 9463 05:51:20,520 --> 05:51:24,840 there's one variable called scores but 9464 05:51:23,000 --> 05:51:26,958 if you want to go to its first location 9465 05:51:24,840 --> 05:51:29,080 starting today you use square brackets 9466 05:51:26,958 --> 05:51:32,360 and go to location zero first which 9467 05:51:29,080 --> 05:51:34,160 because things in C are zero indexed uh 9468 05:51:32,360 --> 05:51:36,718 so to speak you start counting at zero 9469 05:51:34,160 --> 05:51:38,638 the first int is at bracket zero second 9470 05:51:36,718 --> 05:51:40,798 int is at bracket 1 third int is at 9471 05:51:38,638 --> 05:51:42,718 bracket two so it's not 1 two three it's 9472 05:51:40,798 --> 05:51:44,718 literally 0 one2 and this is not 9473 05:51:42,718 --> 05:51:48,600 something you have control over you must 9474 05:51:44,718 --> 05:51:50,680 start at zero so these lines now create 9475 05:51:48,600 --> 05:51:53,680 an array of size three and then insert 9476 05:51:50,680 --> 05:51:55,840 one two three values into that array but 9477 05:51:53,680 --> 05:51:57,760 the upside now is that you only have one 9478 05:51:55,840 --> 05:52:00,680 name of the variable to remember it's 9479 05:51:57,760 --> 05:52:03,120 just called scores yes you need to go 9480 05:52:00,680 --> 05:52:05,520 into the array to get individual values 9481 05:52:03,120 --> 05:52:07,280 you need to index into it using those 9482 05:52:05,520 --> 05:52:08,958 square brackets but at least you don't 9483 05:52:07,280 --> 05:52:11,558 have this hackish approach of declaring 9484 05:52:08,958 --> 05:52:13,958 a separate variable for each and every 9485 05:52:11,558 --> 05:52:16,760 one of these values so let me go back to 9486 05:52:13,958 --> 05:52:18,718 scores do c here and let me propose that 9487 05:52:16,760 --> 05:52:20,600 I do this let me just kind of use that 9488 05:52:18,718 --> 05:52:22,520 same idea to do the following let me get 9489 05:52:20,600 --> 05:52:25,080 rid of these three separate integers let 9490 05:52:22,520 --> 05:52:28,638 me give myself an INT scores array of 9491 05:52:25,080 --> 05:52:32,160 size three and then scores bracket Z 9492 05:52:28,638 --> 05:52:35,920 will as before be 72 scores bracket 1 9493 05:52:32,160 --> 05:52:38,120 will be 73 and scores bracket 2 will be 9494 05:52:35,920 --> 05:52:40,440 33 and let me get rid of the Little Dot 9495 05:52:38,120 --> 05:52:44,120 there all right so now if I go ahead and 9496 05:52:40,440 --> 05:52:49,040 run this again with make uh scores 9497 05:52:44,120 --> 05:52:51,440 enter huh what did I do wrong here I 9498 05:52:49,040 --> 05:52:55,000 think I got a little too ahead of myself 9499 05:52:51,440 --> 05:52:58,718 let me increase my terminal 9500 05:52:55,000 --> 05:53:01,600 window let's focus on line 10 here first 9501 05:52:58,718 --> 05:53:06,120 error use of Undeclared identifier score 9502 05:53:01,600 --> 05:53:06,120 one what did I do here that was dumb 9503 05:53:06,440 --> 05:53:10,798 yeah right so I didn't declare score one 9504 05:53:09,200 --> 05:53:12,878 I've got old code right so I just kind 9505 05:53:10,798 --> 05:53:14,320 of honestly got ahead of myself here not 9506 05:53:12,878 --> 05:53:15,958 even intentionally so let me go ahead 9507 05:53:14,320 --> 05:53:17,718 and Shrink my terminal window again I 9508 05:53:15,958 --> 05:53:20,000 need to finish my thought here so let me 9509 05:53:17,718 --> 05:53:23,240 clear my terminal and let me change this 9510 05:53:20,000 --> 05:53:25,680 now to be scores brackets zero plus 9511 05:53:23,240 --> 05:53:27,040 scores bracket 1 plus scores bracket two 9512 05:53:25,680 --> 05:53:29,160 so it's a little more verbose because 9513 05:53:27,040 --> 05:53:31,520 I've got these these um square brackets 9514 05:53:29,160 --> 05:53:34,200 so to speak but I think now my code is 9515 05:53:31,520 --> 05:53:37,160 consistent so let me make scores now now 9516 05:53:34,200 --> 05:53:39,000 compiles do/ scores gives me indeed the 9517 05:53:37,160 --> 05:53:41,040 same rough average with those same 9518 05:53:39,000 --> 05:53:44,240 values all right so let me go ahead and 9519 05:53:41,040 --> 05:53:45,600 maybe um tidy enhance this a little bit 9520 05:53:44,240 --> 05:53:47,718 it's a little silly to have to write a 9521 05:53:45,600 --> 05:53:50,600 special program just to check your 9522 05:53:47,718 --> 05:53:52,840 average of three test scores like 72 73 9523 05:53:50,600 --> 05:53:55,240 33 why don't I actually make the program 9524 05:53:52,840 --> 05:53:57,718 Dynamic and ask the human for that 9525 05:53:55,240 --> 05:53:59,840 average uh for those scores so instead 9526 05:53:57,718 --> 05:54:03,600 let me do do this how about we get rid 9527 05:53:59,840 --> 05:54:06,240 of the 72 and change this to get int and 9528 05:54:03,600 --> 05:54:08,840 I'll just prompt the user for a score 9529 05:54:06,240 --> 05:54:11,478 let me get rid of the 33 and change 73 9530 05:54:08,840 --> 05:54:13,558 and get this to be get int score quote 9531 05:54:11,478 --> 05:54:15,718 unquote and then lastly get rid of the 9532 05:54:13,558 --> 05:54:18,958 33 and replace it with get int quote 9533 05:54:15,718 --> 05:54:22,760 unquote score get int is a cs50 thing 9534 05:54:18,958 --> 05:54:24,680 for now so I need to include cs50.h as 9535 05:54:22,760 --> 05:54:26,558 always but I think now it's sort of a 9536 05:54:24,680 --> 05:54:28,200 better program because now I can compile 9537 05:54:26,558 --> 05:54:30,440 it once I can even share it with my 9538 05:54:28,200 --> 05:54:32,400 friends and now any of us can average 9539 05:54:30,440 --> 05:54:34,400 three scores on some classes test they 9540 05:54:32,400 --> 05:54:36,760 don't need to know the code or rewrite 9541 05:54:34,400 --> 05:54:40,440 the code just to T type in their scores 9542 05:54:36,760 --> 05:54:44,040 so make scores worked scores now I can 9543 05:54:40,440 --> 05:54:46,638 type anything I want maybe it's a 72 73 9544 05:54:44,040 --> 05:54:49,840 33 still get the same answer or maybe 9545 05:54:46,638 --> 05:54:52,680 I'm having a better semester 100 100 9546 05:54:49,840 --> 05:54:54,520 maybe 99 and now we get still a pretty 9547 05:54:52,680 --> 05:54:55,840 high score there but now it's Dynamic 9548 05:54:54,520 --> 05:54:57,400 now you don't need the source code you 9549 05:54:55,840 --> 05:54:59,638 don't need to recompile the program it's 9550 05:54:57,400 --> 05:55:02,080 just going to work work again and again 9551 05:54:59,638 --> 05:55:04,240 but this too let me propose that this 9552 05:55:02,080 --> 05:55:06,400 code is correct if I want to get three 9553 05:55:04,240 --> 05:55:09,320 scores from the user but these 9554 05:55:06,400 --> 05:55:12,200 highlighted lines now 6 through n are 9555 05:55:09,320 --> 05:55:14,240 they welld designed would you 9556 05:55:12,200 --> 05:55:16,680 say 9557 05:55:14,240 --> 05:55:19,240 yeah yeah right this is we can use a 9558 05:55:16,680 --> 05:55:20,840 loop is the spoiler here why I mean my 9559 05:55:19,240 --> 05:55:21,878 God it's like the same code again and 9560 05:55:20,840 --> 05:55:23,680 again and again the only thing that's 9561 05:55:21,878 --> 05:55:26,080 changing is the number and you it should 9562 05:55:23,680 --> 05:55:27,798 have kind of had some code smell again 9563 05:55:26,080 --> 05:55:29,080 because if I keep typing the same thing 9564 05:55:27,798 --> 05:55:31,280 again again and again like that's 9565 05:55:29,080 --> 05:55:33,840 clearly an opportunity to better design 9566 05:55:31,280 --> 05:55:36,920 something so let me do this let me go 9567 05:55:33,840 --> 05:55:39,280 ahead and still create my ver my array 9568 05:55:36,920 --> 05:55:43,520 of size three and but let me use our old 9569 05:55:39,280 --> 05:55:47,400 friend the for Loop for in I equals z i 9570 05:55:43,520 --> 05:55:50,240 Less Than 3 I ++ and then in here let me 9571 05:55:47,400 --> 05:55:51,798 do scores bracket we haven't seen this 9572 05:55:50,240 --> 05:55:55,920 before but any 9573 05:55:51,798 --> 05:55:59,040 intuition scores bracket I because that 9574 05:55:55,920 --> 05:56:01,600 will use whatever I is be it Z or one or 9575 05:55:59,040 --> 05:56:04,040 two in iteration and then I can get an 9576 05:56:01,600 --> 05:56:06,798 INT asking the user for score without 9577 05:56:04,040 --> 05:56:08,440 having to repeat myself again and again 9578 05:56:06,798 --> 05:56:13,478 so hopefully if I didn't make any typos 9579 05:56:08,440 --> 05:56:14,798 make scores all good do/ score 72 73 33 9580 05:56:13,478 --> 05:56:16,600 and we're back in business but the code 9581 05:56:14,798 --> 05:56:19,638 is arguably now better designed because 9582 05:56:16,600 --> 05:56:21,520 now I haven't actually uh I haven't 9583 05:56:19,638 --> 05:56:23,680 actually hardcoded the scores and I 9584 05:56:21,520 --> 05:56:26,120 haven't actually copied and pasted any 9585 05:56:23,680 --> 05:56:27,558 of that code well if we consider now 9586 05:56:26,120 --> 05:56:29,120 what's going on inside of the computer's 9587 05:56:27,558 --> 05:56:31,360 memory it's pretty much the same in 9588 05:56:29,120 --> 05:56:33,558 terms of the values but instead of the 9589 05:56:31,360 --> 05:56:36,240 variables being literally score one 9590 05:56:33,558 --> 05:56:39,160 score two score three there's just one 9591 05:56:36,240 --> 05:56:42,320 variable it's an array called scores but 9592 05:56:39,160 --> 05:56:44,440 you can index into its three locations 9593 05:56:42,320 --> 05:56:46,280 by using scores bracket Z to get the 9594 05:56:44,440 --> 05:56:48,280 first scores bracket one to get the 9595 05:56:46,280 --> 05:56:50,840 second scores bracket two to get the 9596 05:56:48,280 --> 05:56:53,520 third but this is key the memory is 9597 05:56:50,840 --> 05:56:55,718 contiguous it's only the screen is only 9598 05:56:53,520 --> 05:56:57,920 so large so it wraps around but 9599 05:56:55,718 --> 05:57:00,080 physically digitally the memory is 9600 05:56:57,920 --> 05:57:02,040 contiguous top to bottom left to right 9601 05:57:00,080 --> 05:57:04,878 and that's important why because the 9602 05:57:02,040 --> 05:57:07,760 brackets indicate 0 1 2 that each of 9603 05:57:04,878 --> 05:57:09,920 these integers is just one integer away 9604 05:57:07,760 --> 05:57:12,000 from the next it can't be randomly down 9605 05:57:09,920 --> 05:57:14,958 here all of a sudden it's got to be back 9606 05:57:12,000 --> 05:57:18,558 to back to back all right now equipped 9607 05:57:14,958 --> 05:57:21,440 with that Paradigm what more could we 9608 05:57:18,558 --> 05:57:24,120 actually do here well it turns out it's 9609 05:57:21,440 --> 05:57:26,878 worth knowing that it's possible in code 9610 05:57:24,120 --> 05:57:28,280 to even pass arrays around as arguments 9611 05:57:26,878 --> 05:57:30,280 and let me just whip this program up 9612 05:57:28,280 --> 05:57:32,520 somewhat quickly just so you've seen it 9613 05:57:30,280 --> 05:57:35,520 before long but let me go ahead and do 9614 05:57:32,520 --> 05:57:38,000 this let me propose that I create a 9615 05:57:35,520 --> 05:57:39,638 function that does this averaging for me 9616 05:57:38,000 --> 05:57:43,360 so I'm going to create a function called 9617 05:57:39,638 --> 05:57:46,718 average that returns a float uh and the 9618 05:57:43,360 --> 05:57:48,680 arguments this thing is going to take uh 9619 05:57:46,718 --> 05:57:50,478 let's see it's going to be the array so 9620 05:57:48,680 --> 05:57:51,760 it turns out if you want to take in an 9621 05:57:50,478 --> 05:57:55,360 array of numbers you can call it 9622 05:57:51,760 --> 05:57:58,280 anything you want this is how you tell C 9623 05:57:55,360 --> 05:58:00,400 that a function takes not an integer but 9624 05:57:58,280 --> 05:58:01,798 in Array of integers and you don't have 9625 05:58:00,400 --> 05:58:03,160 to call it array I'm doing that just for 9626 05:58:01,798 --> 05:58:04,878 the sake of discussion it can be called 9627 05:58:03,160 --> 05:58:06,600 X it can be numbers it can be anything 9628 05:58:04,878 --> 05:58:09,160 else I'm just calling an array to be 9629 05:58:06,600 --> 05:58:11,638 super explicit as to what it is there 9630 05:58:09,160 --> 05:58:12,920 now how do I change my code down here 9631 05:58:11,638 --> 05:58:15,600 what I think I'm going to do for the 9632 05:58:12,920 --> 05:58:17,878 moment is just this I'm going to get rid 9633 05:58:15,600 --> 05:58:19,478 of this code here where I manually 9634 05:58:17,878 --> 05:58:21,760 computed the average and let me just 9635 05:58:19,478 --> 05:58:25,080 call the average function here by 9636 05:58:21,760 --> 05:58:26,920 passing in the whole array of scores so 9637 05:58:25,080 --> 05:58:28,320 this is just an example of abstraction 9638 05:58:26,920 --> 05:58:30,000 like now I have a fun function called 9639 05:58:28,320 --> 05:58:31,840 average I don't care I don't have to 9640 05:58:30,000 --> 05:58:33,718 remember how it works once I implement 9641 05:58:31,840 --> 05:58:35,798 it it just kind of tightens up my main 9642 05:58:33,718 --> 05:58:38,040 code a little bit but I do still have to 9643 05:58:35,798 --> 05:58:39,798 implement this so later in my file let 9644 05:58:38,040 --> 05:58:41,320 me repeat myself before the only time 9645 05:58:39,798 --> 05:58:43,200 it's okay and see to repeat yourself 9646 05:58:41,320 --> 05:58:46,920 again and again by typing out again 9647 05:58:43,200 --> 05:58:49,000 average and then int array Open Bracket 9648 05:58:46,920 --> 05:58:50,840 but now not a semicolon now I have to 9649 05:58:49,000 --> 05:58:54,558 implement this thing and I can implement 9650 05:58:50,840 --> 05:58:58,080 this in a bunch of different ways uh but 9651 05:58:54,558 --> 05:59:01,760 I don't know huh in advance I can't just 9652 05:58:58,080 --> 05:59:06,638 do this I can't just do array bracket 0 9653 05:59:01,760 --> 05:59:09,400 plus array bracket 1 plus array bracket 9654 05:59:06,638 --> 05:59:13,080 2 unless unless this program is only 9655 05:59:09,400 --> 05:59:15,200 ever going to work on three numbers so 9656 05:59:13,080 --> 05:59:17,160 huh let me let me go ahead and do this 9657 05:59:15,200 --> 05:59:20,360 let me first propose that there's a poor 9658 05:59:17,160 --> 05:59:23,360 design here in my main function what 9659 05:59:20,360 --> 05:59:23,360 value have I repeated 9660 05:59:23,958 --> 05:59:28,840 twice among the highlighted lines what 9661 05:59:26,080 --> 05:59:30,080 jumps out at use twice L of the array 9662 05:59:28,840 --> 05:59:32,200 yeah the length of the array is just 9663 05:59:30,080 --> 05:59:33,840 three now it's not a huge deal that I 9664 05:59:32,200 --> 05:59:35,920 type the number three on line eight on 9665 05:59:33,840 --> 05:59:37,400 line nine but this is exactly the kind 9666 05:59:35,920 --> 05:59:38,920 of like shortcut that's going to get you 9667 05:59:37,400 --> 05:59:40,120 in trouble eventually why because 9668 05:59:38,920 --> 05:59:41,760 eventually you or someone else is going 9669 05:59:40,120 --> 05:59:43,240 to go in make the array bigger or 9670 05:59:41,760 --> 05:59:45,320 smaller and you're not going to realize 9671 05:59:43,240 --> 05:59:47,160 that magically that same number is in 9672 05:59:45,320 --> 05:59:48,718 two places and indeed this is what a 9673 05:59:47,160 --> 05:59:50,240 programmer would often call a magic 9674 05:59:48,718 --> 05:59:52,320 number a magic number is one that just 9675 05:59:50,240 --> 05:59:54,240 kind of appears magically and you're on 9676 05:59:52,320 --> 05:59:55,520 the honor System to change it here if 9677 05:59:54,240 --> 05:59:57,120 you change it here and then you change 9678 05:59:55,520 --> 05:59:58,600 it over here like that's not going to 9679 05:59:57,120 --> 06:00:00,320 end well if it the onus is on the 9680 05:59:58,600 --> 06:00:02,240 programmer to remember where they 9681 06:00:00,320 --> 06:00:05,120 hardcoded that is rote out three 9682 06:00:02,240 --> 06:00:06,400 explicitly so anytime you reuse a value 9683 06:00:05,120 --> 06:00:08,680 like this you know what we should 9684 06:00:06,400 --> 06:00:11,000 probably do what we did last week which 9685 06:00:08,680 --> 06:00:12,878 was to declare a variable uh perhaps at 9686 06:00:11,000 --> 06:00:15,440 the very top of my program so it's super 9687 06:00:12,878 --> 06:00:17,440 obvious what it is called maybe n and 9688 06:00:15,440 --> 06:00:19,000 set that equal to three better yet what 9689 06:00:17,440 --> 06:00:20,798 did I do last week to make sure that I 9690 06:00:19,000 --> 06:00:23,840 Can't Screw Up and accidentally change 9691 06:00:20,798 --> 06:00:26,200 that value yeah constant and the keyword 9692 06:00:23,840 --> 06:00:28,320 there was just const for short and now I 9693 06:00:26,200 --> 06:00:30,680 have a global variable Global in the 9694 06:00:28,320 --> 06:00:33,200 sense that I can access it anywhere that 9695 06:00:30,680 --> 06:00:35,558 is called n it's an INT and it's always 9696 06:00:33,200 --> 06:00:37,718 going to be three and now I can improve 9697 06:00:35,558 --> 06:00:40,798 my main function a little bit by just 9698 06:00:37,718 --> 06:00:42,360 changing the threes to n so now if I if 9699 06:00:40,798 --> 06:00:44,080 a colleague realize oh wait a minute 9700 06:00:42,360 --> 06:00:45,798 there's four tests this year you change 9701 06:00:44,080 --> 06:00:50,000 n to four recompile the code and it just 9702 06:00:45,798 --> 06:00:51,638 works everywhere else except in my 9703 06:00:50,000 --> 06:00:54,200 average function let me change it back 9704 06:00:51,638 --> 06:00:58,160 to three just for consistency this is 9705 06:00:54,200 --> 06:01:00,160 not going to fly now to just uh sum up 9706 06:00:58,160 --> 06:01:04,600 things like this for instance and then 9707 06:01:00,160 --> 06:01:06,920 return this divided three why will this 9708 06:01:04,600 --> 06:01:10,160 not work 9709 06:01:06,920 --> 06:01:13,160 now as I've defined 9710 06:01:10,160 --> 06:01:13,160 it 9711 06:01:17,040 --> 06:01:21,798 yeah okay I might be in returning an 9712 06:01:19,878 --> 06:01:23,718 integer value when I intend to return a 9713 06:01:21,798 --> 06:01:25,638 float per this but I think I'm okay CU I 9714 06:01:23,718 --> 06:01:27,440 Ed that little trick where I made sure 9715 06:01:25,638 --> 06:01:29,718 that at least one of the numbers in my 9716 06:01:27,440 --> 06:01:31,440 my arithmetic expression is in fact a 9717 06:01:29,718 --> 06:01:33,958 floating point value and just by adding 9718 06:01:31,440 --> 06:01:35,958 the point zero make sure that everything 9719 06:01:33,958 --> 06:01:38,080 gets treated as a float so I think 9720 06:01:35,958 --> 06:01:41,520 that's 9721 06:01:38,080 --> 06:01:41,520 okay sorry a little 9722 06:01:43,360 --> 06:01:47,600 ladder exactly so left hand's not 9723 06:01:45,920 --> 06:01:50,320 talking to the right hand here and that 9724 06:01:47,600 --> 06:01:51,638 my current implementation of average is 9725 06:01:50,320 --> 06:01:53,798 still assuming that there's only going 9726 06:01:51,638 --> 06:01:55,680 to be three tests or whatever but wait a 9727 06:01:53,798 --> 06:01:59,520 minute I just went through the trouble 9728 06:01:55,680 --> 06:02:01,478 of modifying this to be n generically 9729 06:01:59,520 --> 06:02:03,400 and if I change this to four I'm not 9730 06:02:01,478 --> 06:02:04,840 going to be happy perhaps with my 9731 06:02:03,400 --> 06:02:06,760 average because now I'm going to ignore 9732 06:02:04,840 --> 06:02:08,760 one of my test scores Al together so let 9733 06:02:06,760 --> 06:02:12,000 me change this back to three and 9734 06:02:08,760 --> 06:02:13,520 unfortunately if it's a variable now n 9735 06:02:12,000 --> 06:02:16,520 and therefore I have literally a 9736 06:02:13,520 --> 06:02:19,760 variable number of scores how do I take 9737 06:02:16,520 --> 06:02:22,320 the average of a variable number of 9738 06:02:19,760 --> 06:02:24,600 things I mean what's my building block 9739 06:02:22,320 --> 06:02:24,600 there 9740 06:02:26,360 --> 06:02:29,360 yeah 9741 06:02:30,440 --> 06:02:34,120 yeah why don't I use a loop that goes 9742 06:02:32,160 --> 06:02:35,520 through the array and adds things up as 9743 06:02:34,120 --> 06:02:36,680 you go I mean kind of like grade school 9744 06:02:35,520 --> 06:02:38,478 as you take the average on your 9745 06:02:36,680 --> 06:02:39,878 calculator or paper pencil you just keep 9746 06:02:38,478 --> 06:02:41,600 adding the numbers together and then you 9747 06:02:39,878 --> 06:02:43,600 divide at the End by the total number of 9748 06:02:41,600 --> 06:02:45,840 things so how can I do this well let me 9749 06:02:43,600 --> 06:02:48,558 change my implementation of average to 9750 06:02:45,840 --> 06:02:50,760 First declare a a variable called sum or 9751 06:02:48,558 --> 06:02:52,160 whatever set it equal to zero so this is 9752 06:02:50,760 --> 06:02:53,760 like me on my piece of paper getting 9753 06:02:52,160 --> 06:02:55,240 ready to count or my calculator of 9754 06:02:53,760 --> 06:02:57,958 course when you turn it on typically 9755 06:02:55,240 --> 06:03:02,240 defaults to zero and now let me do four 9756 06:02:57,958 --> 06:03:05,798 in I equals z i is less than a VAR I no 9757 06:03:02,240 --> 06:03:09,200 I didn't do that n i is less than n 9758 06:03:05,798 --> 06:03:13,320 i++ and now in here let me go ahead and 9759 06:03:09,200 --> 06:03:16,798 add to the current sum uh whatever is in 9760 06:03:13,320 --> 06:03:19,600 the arrays location I and then down here 9761 06:03:16,798 --> 06:03:25,280 I think I can just return sum divided by 9762 06:03:19,600 --> 06:03:27,478 3 nope not 3.0 n perhaps here and 9763 06:03:25,280 --> 06:03:29,040 actually I think I'm going to get let's 9764 06:03:27,478 --> 06:03:30,520 make sure it's a float let's use the the 9765 06:03:29,040 --> 06:03:32,558 type casting trick just to make sure I 9766 06:03:30,520 --> 06:03:34,638 don't accidentally Short change someone 9767 06:03:32,558 --> 06:03:36,360 and uh throw away everything after the 9768 06:03:34,638 --> 06:03:37,920 decimal point so it just escalated 9769 06:03:36,360 --> 06:03:39,478 quickly right like average just got a 9770 06:03:37,920 --> 06:03:41,240 lot more involved it's just not just a 9771 06:03:39,478 --> 06:03:43,760 single one line of code but now it's 9772 06:03:41,240 --> 06:03:46,958 Dynamic I initialize a variable called 9773 06:03:43,760 --> 06:03:49,440 sum to zero in this loop I go through 9774 06:03:46,958 --> 06:03:51,840 and just keep adding to sum which is 9775 06:03:49,440 --> 06:03:54,200 initially zero whatever is in Array 9776 06:03:51,840 --> 06:03:56,638 bracket I or specifically array bracket 9777 06:03:54,200 --> 06:03:58,878 zero array bracket one array bracket two 9778 06:03:56,638 --> 06:04:00,878 that gives me a total sum that I return 9779 06:03:58,878 --> 06:04:02,798 divided by the total number of things 9780 06:04:00,878 --> 06:04:04,840 now this I can tighten slightly recall 9781 06:04:02,798 --> 06:04:06,600 that this is syntactic sugar for just 9782 06:04:04,840 --> 06:04:08,718 adding things I can't use plus plus 9783 06:04:06,600 --> 06:04:11,600 because that only literally adds one but 9784 06:04:08,718 --> 06:04:14,400 I can use here plus 9785 06:04:11,600 --> 06:04:16,680 equals questions on this implementation 9786 06:04:14,400 --> 06:04:18,638 here really the only takeaway or the 9787 06:04:16,680 --> 06:04:21,360 most important takeaway is that this is 9788 06:04:18,638 --> 06:04:24,120 the Syntax for how you tell a function 9789 06:04:21,360 --> 06:04:26,320 that to it expects a whole array not a 9790 06:04:24,120 --> 06:04:27,878 single variable like an INT or the like 9791 06:04:26,320 --> 06:04:30,558 you literally use square brackets but 9792 06:04:27,878 --> 06:04:33,558 you don't specify the length inside 9793 06:04:30,558 --> 06:04:33,558 there 9794 06:04:35,638 --> 06:04:39,840 yeah what about the variable at the 9795 06:04:41,600 --> 06:04:46,280 top good question what do I have it 9796 06:04:43,680 --> 06:04:49,520 defined as at the top this variable n it 9797 06:04:46,280 --> 06:04:52,600 must be an integer if you're going to uh 9798 06:04:49,520 --> 06:04:55,798 use it inside of in arrays square 9799 06:04:52,600 --> 06:04:58,520 brackets here so this line 10 notice no 9800 06:04:55,798 --> 06:05:00,958 longer says three it says n and so 9801 06:04:58,520 --> 06:05:03,080 whatever n is three or four or something 9802 06:05:00,958 --> 06:05:04,798 else that's how many integers I will get 9803 06:05:03,080 --> 06:05:06,878 in that array and it must be by 9804 06:05:04,798 --> 06:05:08,478 definition of an array an integer that 9805 06:05:06,878 --> 06:05:10,360 goes in those square brackets and here's 9806 06:05:08,478 --> 06:05:12,478 a common source of confusion when you 9807 06:05:10,360 --> 06:05:14,000 create the array that is declare it you 9808 06:05:12,478 --> 06:05:15,478 use square brackets like this where you 9809 06:05:14,000 --> 06:05:18,320 put the total number of elements you 9810 06:05:15,478 --> 06:05:20,558 want when you subsequently use the array 9811 06:05:18,320 --> 06:05:22,520 like I'm doing here you don't mention 9812 06:05:20,558 --> 06:05:24,520 int again just like you don't mention in 9813 06:05:22,520 --> 06:05:26,718 again and again once a variable exists 9814 06:05:24,520 --> 06:05:30,000 you use the square bracket still but you 9815 06:05:26,718 --> 06:05:32,840 don't use n you use zero or one or two 9816 06:05:30,000 --> 06:05:34,200 or generically here I so when C was 9817 06:05:32,840 --> 06:05:36,320 designed they sometimes use the same 9818 06:05:34,200 --> 06:05:39,040 Syntax for two different ideas or 9819 06:05:36,320 --> 06:05:39,040 contexts 9820 06:05:42,120 --> 06:05:47,080 yeah good question do I have to include 9821 06:05:44,638 --> 06:05:50,160 line six short answer yes because of the 9822 06:05:47,080 --> 06:05:51,680 reason we ran into last week C or clang 9823 06:05:50,160 --> 06:05:54,718 really reads your code top to bottom 9824 06:05:51,680 --> 06:05:57,320 left to right and so if the compiler 9825 06:05:54,718 --> 06:05:59,920 sees some mention of this function 9826 06:05:57,320 --> 06:06:01,798 average on line 16 but you haven't told 9827 06:05:59,920 --> 06:06:03,798 the compiler that average exists you're 9828 06:06:01,798 --> 06:06:05,320 going to get an error on the screen so 9829 06:06:03,798 --> 06:06:07,680 the conventional way to do that is you 9830 06:06:05,320 --> 06:06:09,280 just copy paste the one first line of 9831 06:06:07,680 --> 06:06:13,558 code from the function it's so-called 9832 06:06:09,280 --> 06:06:13,558 prototype or Declaration 9833 06:06:14,638 --> 06:06:19,000 yeah really good question uh in a 9834 06:06:17,000 --> 06:06:20,320 perfect segue is there a library you can 9835 06:06:19,000 --> 06:06:22,760 use if you don't know the size of the 9836 06:06:20,320 --> 06:06:26,600 array no and so if any of you have 9837 06:06:22,760 --> 06:06:28,200 programmed in uh in uh Java or python or 9838 06:06:26,600 --> 06:06:31,120 other Lang languages you can actually 9839 06:06:28,200 --> 06:06:33,040 just ask the array like how big is it in 9840 06:06:31,120 --> 06:06:34,878 C you and I the programmers have to 9841 06:06:33,040 --> 06:06:35,878 remember it and so short answer no 9842 06:06:34,878 --> 06:06:38,080 there's no function that will just 9843 06:06:35,878 --> 06:06:40,400 automatically do this for us and in fact 9844 06:06:38,080 --> 06:06:43,000 let me make a more subtle claim that 9845 06:06:40,400 --> 06:06:44,638 it's fine to use Global variables like 9846 06:06:43,000 --> 06:06:46,760 this if they're really for configuration 9847 06:06:44,638 --> 06:06:48,240 options why it's just convenient to put 9848 06:06:46,760 --> 06:06:50,360 them at the very top of the file because 9849 06:06:48,240 --> 06:06:51,718 everyone you your colleagues your Tas 9850 06:06:50,360 --> 06:06:54,000 are going to see them at the top of the 9851 06:06:51,718 --> 06:06:55,878 code but you really shouldn't be using 9852 06:06:54,000 --> 06:06:57,638 them everywhere throughout your code 9853 06:06:55,878 --> 06:06:59,638 it'd be better if the average function 9854 06:06:57,638 --> 06:07:02,200 itself we're independent of that special 9855 06:06:59,638 --> 06:07:03,958 variable so by that I mean this you know 9856 06:07:02,200 --> 06:07:07,798 what I should really do if I really want 9857 06:07:03,958 --> 06:07:10,440 to be welld designed I should pass in 9858 06:07:07,798 --> 06:07:12,638 the length of the array to the average 9859 06:07:10,440 --> 06:07:14,760 function I should give the average 9860 06:07:12,638 --> 06:07:16,638 function a second argument I'll call it 9861 06:07:14,760 --> 06:07:18,878 length for instance but I could call it 9862 06:07:16,638 --> 06:07:21,160 anything I want and so rather than 9863 06:07:18,878 --> 06:07:22,958 putting n all the way down here at the 9864 06:07:21,160 --> 06:07:25,840 bottom of my file let me just 9865 06:07:22,958 --> 06:07:27,240 dynamically say length instead and this 9866 06:07:25,840 --> 06:07:29,360 is a subtlety and no need need to get 9867 06:07:27,240 --> 06:07:31,478 too tripped up over this but this now is 9868 06:07:29,360 --> 06:07:33,878 just an example of how the same function 9869 06:07:31,478 --> 06:07:38,360 can take not one but two arguments but 9870 06:07:33,878 --> 06:07:40,160 indeed in C you must remember yourself 9871 06:07:38,360 --> 06:07:42,718 what the length of an array is you can't 9872 06:07:40,160 --> 06:07:43,958 just ask the array via some syntax like 9873 06:07:42,718 --> 06:07:48,638 you can those of you who've programmed 9874 06:07:43,958 --> 06:07:48,638 before in Java or python 9875 06:07:54,440 --> 06:07:58,638 yeah good question would it be better to 9876 06:07:57,200 --> 06:08:01,120 write a function that computes the size 9877 06:07:58,638 --> 06:08:03,240 short answer can't do that in C as soon 9878 06:08:01,120 --> 06:08:06,120 as you pass an array into a function in 9879 06:08:03,240 --> 06:08:07,958 C you cannot figure out its size if it's 9880 06:08:06,120 --> 06:08:10,360 a generic array like that of integers 9881 06:08:07,958 --> 06:08:12,360 there are special cases that uh you can 9882 06:08:10,360 --> 06:08:13,840 do that but in general no it's just not 9883 06:08:12,360 --> 06:08:15,400 possible in see and if that's some 9884 06:08:13,840 --> 06:08:17,200 frustration honestly this is why more 9885 06:08:15,400 --> 06:08:19,040 modern languages add that feature why 9886 06:08:17,200 --> 06:08:20,600 because it was really annoying um as I'm 9887 06:08:19,040 --> 06:08:22,120 alluding here to not having that 9888 06:08:20,600 --> 06:08:23,718 information now just to make sure I 9889 06:08:22,120 --> 06:08:25,798 didn't screw up anywhere let me compile 9890 06:08:23,718 --> 06:08:27,440 this final version of 9891 06:08:25,798 --> 06:08:32,558 scores 9892 06:08:27,440 --> 06:08:34,320 suspense all good/ scores 72 73 33 and 9893 06:08:32,558 --> 06:08:35,760 we're still back in business so this 9894 06:08:34,320 --> 06:08:36,840 version is more complicated and as 9895 06:08:35,760 --> 06:08:38,520 always we'll have this version on the 9896 06:08:36,840 --> 06:08:40,638 course's website for reference but the 9897 06:08:38,520 --> 06:08:42,600 point really is that arrays not only can 9898 06:08:40,638 --> 06:08:45,400 be used as containers to store multiple 9899 06:08:42,600 --> 06:08:48,320 values three or more in this case um you 9900 06:08:45,400 --> 06:08:52,040 can also even pass them around as 9901 06:08:48,320 --> 06:08:53,798 arguments as such all right now besides 9902 06:08:52,040 --> 06:08:55,360 that let's let's simplify for just a 9903 06:08:53,798 --> 06:08:57,360 moment and consider now the world of 9904 06:08:55,360 --> 06:08:59,240 chars if we've just got single bites 9905 06:08:57,360 --> 06:09:00,958 where uh where does this lead us and how 9906 06:08:59,240 --> 06:09:02,680 does this get us ultimately to strings 9907 06:09:00,958 --> 06:09:04,878 to solve problems like readability and 9908 06:09:02,680 --> 06:09:06,520 cryptography and the like well here for 9909 06:09:04,878 --> 06:09:09,000 instance are three lines of code out of 9910 06:09:06,520 --> 06:09:10,160 context that simply store three chars 9911 06:09:09,000 --> 06:09:12,320 and you can already see where this is 9912 06:09:10,160 --> 06:09:14,600 going having three variables called C1 9913 06:09:12,320 --> 06:09:16,400 C2 C3 is clearly going to end up being 9914 06:09:14,600 --> 06:09:18,080 bad design because of all the silly 9915 06:09:16,400 --> 06:09:19,760 redundancy here but notice I'm using 9916 06:09:18,080 --> 06:09:21,840 single quotes like last week because 9917 06:09:19,760 --> 06:09:24,000 these are single chars what does this 9918 06:09:21,840 --> 06:09:25,240 look like in the computer's memory well 9919 06:09:24,000 --> 06:09:29,000 it looks a little something like this if 9920 06:09:25,240 --> 06:09:30,878 we clear out the old memory C1 C2 C3 9921 06:09:29,000 --> 06:09:32,478 probably will end up here maybe not 9922 06:09:30,878 --> 06:09:35,958 literally in the top leftand Corner this 9923 06:09:32,478 --> 06:09:38,878 is just an artist rendition but C1 C2 C3 9924 06:09:35,958 --> 06:09:40,520 will probably end up like that now 9925 06:09:38,878 --> 06:09:44,760 what's really there it's really those 9926 06:09:40,520 --> 06:09:46,878 same three numbers 72 73 33 but how many 9927 06:09:44,760 --> 06:09:50,240 bits does a bite 9928 06:09:46,878 --> 06:09:52,760 have just eight so if we were to look at 9929 06:09:50,240 --> 06:09:54,798 the binary representation of these 9930 06:09:52,760 --> 06:09:56,798 characters it would only be eight bits 9931 06:09:54,798 --> 06:09:59,478 each that's enough to store small 9932 06:09:56,798 --> 06:10:01,878 numbers like 72 73 33 we're not dealing 9933 06:09:59,478 --> 06:10:03,240 with Unicode and emoji and the like but 9934 06:10:01,878 --> 06:10:05,040 the point is the same you don't have to 9935 06:10:03,240 --> 06:10:06,478 use four bytes to store these numbers 9936 06:10:05,040 --> 06:10:08,160 you can use a different data type like 9937 06:10:06,478 --> 06:10:10,400 chars and underneath the hood it's 9938 06:10:08,160 --> 06:10:13,638 indeed going to use just single bytes 9939 06:10:10,400 --> 06:10:15,520 for each but this is sort of like a this 9940 06:10:13,638 --> 06:10:17,080 isn't really how we Implement strings 9941 06:10:15,520 --> 06:10:19,280 right when you wanted to say hi last 9942 06:10:17,080 --> 06:10:20,798 week or this we use double quotes and we 9943 06:10:19,280 --> 06:10:23,000 wrote all of the things together and 9944 06:10:20,798 --> 06:10:24,520 used one variable not three right when I 9945 06:10:23,000 --> 06:10:27,520 typed in David I didn't have a variable 9946 06:10:24,520 --> 06:10:30,840 for d a v i d i had one variable called 9947 06:10:27,520 --> 06:10:32,160 name that stored the whole thing so in C 9948 06:10:30,840 --> 06:10:34,760 we keep talking about these things 9949 06:10:32,160 --> 06:10:36,320 called strings we'll see eventually that 9950 06:10:34,760 --> 06:10:38,600 strings are not necessarily what they 9951 06:10:36,320 --> 06:10:40,080 seem to be but for now the key thing 9952 06:10:38,600 --> 06:10:42,120 about strings is that they're variable 9953 06:10:40,080 --> 06:10:44,120 length uh so to speak right they might 9954 06:10:42,120 --> 06:10:46,760 be three characters high or five 9955 06:10:44,120 --> 06:10:49,520 characters David or anything uh smaller 9956 06:10:46,760 --> 06:10:51,520 or larger so how do we go about 9957 06:10:49,520 --> 06:10:53,240 implementing strings if all we have at 9958 06:10:51,520 --> 06:10:55,600 the end of the day is my memory well 9959 06:10:53,240 --> 06:10:57,958 here is an example of just creating uh 9960 06:10:55,600 --> 06:10:59,680 declaring and defining a string called s 9961 06:10:57,958 --> 06:11:02,080 s because it's just a simple string and 9962 06:10:59,680 --> 06:11:03,558 quote unquote high in double quotes what 9963 06:11:02,080 --> 06:11:05,558 does this look like in the computer's 9964 06:11:03,558 --> 06:11:07,040 memory well let's clear it again and 9965 06:11:05,558 --> 06:11:09,320 here now because it's technically stored 9966 06:11:07,040 --> 06:11:11,878 in one variable s here is how I might 9967 06:11:09,320 --> 06:11:14,320 draw it as an artist it's three bytes in 9968 06:11:11,878 --> 06:11:18,638 total H exclamation point but there's no 9969 06:11:14,320 --> 06:11:23,360 C1 C2 C3 it's just the whole thing is uh 9970 06:11:18,638 --> 06:11:25,600 s but it turns out that a string fun 9971 06:11:23,360 --> 06:11:29,680 fact is really just what underneath the 9972 06:11:25,600 --> 06:11:32,040 hood kind of leading up to this what is 9973 06:11:29,680 --> 06:11:35,040 a string if this is how it's laid out in 9974 06:11:32,040 --> 06:11:36,320 memory literally it's just an array of 9975 06:11:35,040 --> 06:11:38,520 characters and we didn't have to know 9976 06:11:36,320 --> 06:11:39,718 about arrays last week to use strings 9977 06:11:38,520 --> 06:11:42,040 this is where again the training wheels 9978 06:11:39,718 --> 06:11:44,280 are starting to come off but a string is 9979 06:11:42,040 --> 06:11:46,240 just an array of characters Hi 9980 06:11:44,280 --> 06:11:50,160 exclamation point for instance so 9981 06:11:46,240 --> 06:11:53,200 technically an array a string called s 9982 06:11:50,160 --> 06:11:54,718 is really a variable called s that 9983 06:11:53,200 --> 06:11:56,718 allows you to get at the first character 9984 06:11:54,718 --> 06:11:58,638 with s bracket zero if you want s 9985 06:11:56,718 --> 06:12:00,718 bracket 1 s bracket2 you can literally 9986 06:11:58,638 --> 06:12:03,120 get individual characters just by 9987 06:12:00,718 --> 06:12:05,920 treating S as though it's an array which 9988 06:12:03,120 --> 06:12:09,320 it really is underneath the hood in this 9989 06:12:05,920 --> 06:12:12,200 case but there's a catch how do you know 9990 06:12:09,320 --> 06:12:15,000 where strings end in the past when I 9991 06:12:12,200 --> 06:12:16,920 drew some integers on the screen I know 9992 06:12:15,000 --> 06:12:19,240 I claim they always take up four bytes 9993 06:12:16,920 --> 06:12:21,798 if I had drawn a long it always takes up 9994 06:12:19,240 --> 06:12:23,920 eight bytes if I drawn a a character it 9995 06:12:21,798 --> 06:12:27,200 always takes up one bite but how many 9996 06:12:23,920 --> 06:12:28,600 bites does a string take up yeah I mean 9997 06:12:27,200 --> 06:12:30,680 that's kind of the right answer in this 9998 06:12:28,600 --> 06:12:33,840 case three it would seem but if it's 9999 06:12:30,680 --> 06:12:36,120 David that's a good five characters but 10000 06:12:33,840 --> 06:12:37,958 where do we put the number three where 10001 06:12:36,120 --> 06:12:40,040 do you put the number five right this is 10002 06:12:37,958 --> 06:12:42,200 literally all that's into your computer 10003 06:12:40,040 --> 06:12:45,120 this is all our building blocks in front 10004 06:12:42,200 --> 06:12:47,000 of us so how can we where does the three 10005 06:12:45,120 --> 06:12:48,760 go where does the five go well it turns 10006 06:12:47,000 --> 06:12:50,160 out you can solve this in a couple of 10007 06:12:48,760 --> 06:12:52,440 different ways but the way humans 10008 06:12:50,160 --> 06:12:55,360 decided to implement strings years ago 10009 06:12:52,440 --> 06:12:57,798 is indeed an array but they added one 10010 06:12:55,360 --> 06:13:00,360 extra bite at the end of every such 10011 06:12:57,798 --> 06:13:03,240 string array just to make clear with a 10012 06:13:00,360 --> 06:13:05,718 so-called Sentinel value that the string 10013 06:13:03,240 --> 06:13:07,718 ends here why so that if you have two 10014 06:13:05,718 --> 06:13:10,558 strings in the computer's memory like hi 10015 06:13:07,718 --> 06:13:12,120 and by you know where the barrier is 10016 06:13:10,558 --> 06:13:14,360 between like the exclamation point of 10017 06:13:12,120 --> 06:13:16,638 one and the letter B in the next right 10018 06:13:14,360 --> 06:13:19,200 you need some kind of delimiter and so 10019 06:13:16,638 --> 06:13:21,920 what really is underneath the hood is 10020 06:13:19,200 --> 06:13:24,558 this when you store a string in memory 10021 06:13:21,920 --> 06:13:26,400 when you type in a string as the user if 10022 06:13:24,558 --> 06:13:30,120 you type in three characters it's it's 10023 06:13:26,400 --> 06:13:31,920 going to use 3 + 1 = 4 bytes in total if 10024 06:13:30,120 --> 06:13:35,558 you type in David it's going to use 5 + 10025 06:13:31,920 --> 06:13:38,760 1al 6 bytes in total why because C 10026 06:13:35,558 --> 06:13:40,760 automatically adds this special zero at 10027 06:13:38,760 --> 06:13:42,600 the end of the string I've drawn it with 10028 06:13:40,760 --> 06:13:45,520 back SL Zer because this is how you 10029 06:13:42,600 --> 06:13:47,280 represent zero as a Char as a character 10030 06:13:45,520 --> 06:13:49,558 but this is literally just zero as we'll 10031 06:13:47,280 --> 06:13:52,840 soon see so anytime there's a string in 10032 06:13:49,558 --> 06:13:54,878 memory it always takes up one more bite 10033 06:13:52,840 --> 06:13:57,000 than you yourself as the programmer or 10034 06:13:54,878 --> 06:13:58,718 human typed in in fact if we convert 10035 06:13:57,000 --> 06:14:00,520 this again just for discussion sake to 10036 06:13:58,718 --> 06:14:01,680 those integers what's literally stored 10037 06:14:00,520 --> 06:14:06,080 in the computer's memory is going to be 10038 06:14:01,680 --> 06:14:08,000 72 73 33 and now a zero and the computer 10039 06:14:06,080 --> 06:14:09,558 because of c and how it was invented 10040 06:14:08,000 --> 06:14:12,080 it's just smart enough to know that when 10041 06:14:09,558 --> 06:14:14,600 you print out a string it prints out 10042 06:14:12,080 --> 06:14:16,120 every character until it sees a zero and 10043 06:14:14,600 --> 06:14:18,320 then it just stops printing in 10044 06:14:16,120 --> 06:14:21,040 particular printf knows how this works 10045 06:14:18,320 --> 06:14:22,920 and this is why print F knows when to 10046 06:14:21,040 --> 06:14:24,600 stop printing decimal numbers are not 10047 06:14:22,920 --> 06:14:26,280 that enlightening we'll generally write 10048 06:14:24,600 --> 06:14:29,120 the characters like this and again back 10049 06:14:26,280 --> 06:14:30,840 back sl0 is just special symbology like 10050 06:14:29,120 --> 06:14:33,120 it's what the programmer types to make 10051 06:14:30,840 --> 06:14:35,440 clear that you're not saying hi zero 10052 06:14:33,120 --> 06:14:40,200 you're saying hi and then it's a special 10053 06:14:35,440 --> 06:14:41,638 zero specifically it is eight zero bits 10054 06:14:40,200 --> 06:14:44,120 that indicate that it's the end of the 10055 06:14:41,638 --> 06:14:46,680 string technically that back sl0 if you 10056 06:14:44,120 --> 06:14:48,958 want to be fancy it's called null 10057 06:14:46,680 --> 06:14:50,360 NL and it turns out you've seen this 10058 06:14:48,958 --> 06:14:52,160 before that we didn't call it out here's 10059 06:14:50,360 --> 06:14:55,120 that same asky chart from the past 10060 06:14:52,160 --> 06:14:59,760 couple of weeks if I highlight this what 10061 06:14:55,120 --> 06:15:01,360 is decimal number zero mapping to n 10062 06:14:59,760 --> 06:15:03,878 which is just programmer speak for the 10063 06:15:01,360 --> 06:15:06,798 special null character all zero bits 10064 06:15:03,878 --> 06:15:09,000 that means the string ends here this all 10065 06:15:06,798 --> 06:15:11,718 happens automatically for you you do not 10066 06:15:09,000 --> 06:15:12,440 need to create these null characters or 10067 06:15:11,718 --> 06:15:15,000 these 10068 06:15:12,440 --> 06:15:18,920 zeros any questions 10069 06:15:15,000 --> 06:15:22,558 then on this implementation thus 10070 06:15:18,920 --> 06:15:24,680 far any questions here no well let me do 10071 06:15:22,558 --> 06:15:26,878 this let me go back to vs code in a 10072 06:15:24,680 --> 06:15:28,080 second and let's actually corroborate 10073 06:15:26,878 --> 06:15:30,840 this with some code let me go ahead and 10074 06:15:28,080 --> 06:15:32,760 create a small program called high. C 10075 06:15:30,840 --> 06:15:36,400 and how about we do this let me include 10076 06:15:32,760 --> 06:15:38,718 standard i.h let me include uh let me 10077 06:15:36,400 --> 06:15:40,400 type out int main void as always and now 10078 06:15:38,718 --> 06:15:43,680 let me do something simple and kind of 10079 06:15:40,400 --> 06:15:46,680 bad but Char C1 equals quote unquote H 10080 06:15:43,680 --> 06:15:49,040 in single quotes Char C2 equals quote 10081 06:15:46,680 --> 06:15:51,680 unquote I in single quotes and lastly 10082 06:15:49,040 --> 06:15:53,718 Char C3 equals exclamation point in 10083 06:15:51,680 --> 06:15:55,958 single quotes and now let me just print 10084 06:15:53,718 --> 06:15:57,600 this out I can't use percent s cuz that 10085 06:15:55,958 --> 06:15:59,680 is not a string that's literally three 10086 06:15:57,600 --> 06:16:02,280 chars cuz that's the design decision I 10087 06:15:59,680 --> 06:16:04,520 made but I could do this percent C 10088 06:16:02,280 --> 06:16:07,320 percent C percent C which we haven't 10089 06:16:04,520 --> 06:16:10,120 seen before but percent s is string 10090 06:16:07,320 --> 06:16:13,160 percent I is int percent C is indeed uh 10091 06:16:10,120 --> 06:16:16,478 Char so let me put a back sln at the end 10092 06:16:13,160 --> 06:16:19,280 for cleanliness and now do C1 C2 C3 so 10093 06:16:16,478 --> 06:16:21,718 this is like a Char based version of 10094 06:16:19,280 --> 06:16:24,120 printing string so let me make high and 10095 06:16:21,718 --> 06:16:26,240 then let me do/ high and it looks like I 10096 06:16:24,120 --> 06:16:28,000 use print f with percent s but I did 10097 06:16:26,240 --> 06:16:30,200 things very manually by printing out 10098 06:16:28,000 --> 06:16:31,920 each individual character what's cool 10099 06:16:30,200 --> 06:16:33,760 now though is that once you know that 10100 06:16:31,920 --> 06:16:35,920 characters are just numbers and strings 10101 06:16:33,760 --> 06:16:37,920 are just characters you can kind of poke 10102 06:16:35,920 --> 06:16:41,958 around let me change all three 10103 06:16:37,920 --> 06:16:44,280 placeholders to percent i instead and 10104 06:16:41,958 --> 06:16:46,160 this is totally fine too let me rerun 10105 06:16:44,280 --> 06:16:50,400 this make 10106 06:16:46,160 --> 06:16:51,718 High um actually let me make one change 10107 06:16:50,400 --> 06:16:54,120 just so we can see this let me add 10108 06:16:51,718 --> 06:16:58,360 spaces just for Aesthetics sake let me 10109 06:16:54,120 --> 06:16:59,718 do make high do SL High enter and voila 10110 06:16:58,360 --> 06:17:01,718 like now you can actually see the 10111 06:16:59,718 --> 06:17:03,760 numbers that I claimed back in week zero 10112 06:17:01,718 --> 06:17:05,360 were in fact happening underneath the 10113 06:17:03,760 --> 06:17:06,760 hood well this is not how you would make 10114 06:17:05,360 --> 06:17:09,040 strings it'd be incredibly tedious to 10115 06:17:06,760 --> 06:17:10,718 have three variables for threel Words 10116 06:17:09,040 --> 06:17:12,320 five variables for five letter words 10117 06:17:10,718 --> 06:17:14,798 we've been using of course strings since 10118 06:17:12,320 --> 06:17:17,680 last week so let's do that instead uh 10119 06:17:14,798 --> 06:17:21,558 string uh s equals quote unquote double 10120 06:17:17,680 --> 06:17:23,280 quotes high for this no because of these 10121 06:17:21,558 --> 06:17:25,320 training wheels I need to include the 10122 06:17:23,280 --> 06:17:27,280 cs50 library but we'll come back to that 10123 06:17:25,320 --> 06:17:29,680 in the coming week but for now I'm going 10124 06:17:27,280 --> 06:17:32,120 to go ahead and create a string s called 10125 06:17:29,680 --> 06:17:34,760 quote unquote high and now I'm going to 10126 06:17:32,120 --> 06:17:37,718 change this to be my familiar percent s 10127 06:17:34,760 --> 06:17:39,920 and now just print out s itself this of 10128 06:17:37,718 --> 06:17:41,920 course is the same thing as last week 10129 06:17:39,920 --> 06:17:44,760 high gives me the exact same thing but 10130 06:17:41,920 --> 06:17:47,320 now we're dealing of course with strings 10131 06:17:44,760 --> 06:17:49,280 but how can we see a little beyond that 10132 06:17:47,320 --> 06:17:51,718 well how about this let's poke around 10133 06:17:49,280 --> 06:17:54,280 further with today's Primitives even 10134 06:17:51,718 --> 06:17:56,400 though s is a string I could technically 10135 06:17:54,280 --> 06:17:59,200 print out its first character with 10136 06:17:56,400 --> 06:18:00,520 percent C by doing s bracket z i could 10137 06:17:59,200 --> 06:18:02,200 technically print out its second 10138 06:18:00,520 --> 06:18:04,240 character with percent C by doing s 10139 06:18:02,200 --> 06:18:06,360 bracket one I could print out its third 10140 06:18:04,240 --> 06:18:08,400 character with percent C and printing 10141 06:18:06,360 --> 06:18:10,280 out s bracket 2 so again this just 10142 06:18:08,400 --> 06:18:12,080 derives logically from my understanding 10143 06:18:10,280 --> 06:18:16,520 now of that strings or arrays as you 10144 06:18:12,080 --> 06:18:18,600 note let me do make let me do make high 10145 06:18:16,520 --> 06:18:20,718 SL high and no visual change but I'm 10146 06:18:18,600 --> 06:18:22,638 just kind of now tinkering around and in 10147 06:18:20,718 --> 06:18:25,600 fact if you're really curious let me do 10148 06:18:22,638 --> 06:18:28,600 this let me change these back to I back 10149 06:18:25,600 --> 06:18:30,920 to I I oops back to I and let me add a 10150 06:18:28,600 --> 06:18:34,600 fourth one because if I'm really curious 10151 06:18:30,920 --> 06:18:36,638 now let's see it what's S braet 3 this 10152 06:18:34,600 --> 06:18:39,798 is the fourth bite and even though the 10153 06:18:36,638 --> 06:18:42,280 string itself is hi I think we can 10154 06:18:39,798 --> 06:18:46,280 corroborate this whole null thing make 10155 06:18:42,280 --> 06:18:47,798 high SL High enter and there it is you 10156 06:18:46,280 --> 06:18:49,558 could have done this last week if you 10157 06:18:47,798 --> 06:18:51,478 really wanted to geek out on strings but 10158 06:18:49,558 --> 06:18:53,600 like for now it's just revealing what's 10159 06:18:51,478 --> 06:18:57,840 going on underneath the hood questions 10160 06:18:53,600 --> 06:18:57,840 then on what these strings are 10161 06:19:00,320 --> 06:19:04,240 yeah why do we need the 10162 06:19:04,520 --> 06:19:08,600 bracket uh uh why do you not need 10163 06:19:06,600 --> 06:19:12,400 brackets good question why do I not need 10164 06:19:08,600 --> 06:19:14,478 brackets on line six uh to because uh s 10165 06:19:12,400 --> 06:19:17,280 is a string we'll see in a couple of 10166 06:19:14,478 --> 06:19:20,160 weeks that s is essentially implemented 10167 06:19:17,280 --> 06:19:22,080 underneath the hood indeed as an array 10168 06:19:20,160 --> 06:19:25,400 but that happens automatically for you 10169 06:19:22,080 --> 06:19:27,200 you can treat S as just a variable name 10170 06:19:25,400 --> 06:19:28,760 without square brackets you will use 10171 06:19:27,200 --> 06:19:31,080 square brackets when you have arrays of 10172 06:19:28,760 --> 06:19:33,280 ins or you manually create arrays of 10173 06:19:31,080 --> 06:19:35,638 chars or doubles or Floats or anything 10174 06:19:33,280 --> 06:19:37,360 else but strings are special why I mean 10175 06:19:35,638 --> 06:19:39,478 every program you write seems to use 10176 06:19:37,360 --> 06:19:41,400 strings text in some form we're humans 10177 06:19:39,478 --> 06:19:43,320 we like text not just numbers and and 10178 06:19:41,400 --> 06:19:45,840 such so this is just treated a little 10179 06:19:43,320 --> 06:19:47,478 specially in C and many other languages 10180 06:19:45,840 --> 06:19:51,478 as 10181 06:19:47,478 --> 06:19:53,080 well other questions on this here no 10182 06:19:51,478 --> 06:19:55,200 let's add then one other string to the 10183 06:19:53,080 --> 06:19:56,478 mix so instead of just saying hi why 10184 06:19:55,200 --> 06:19:59,440 don't we consider version of the program 10185 06:19:56,478 --> 06:20:01,798 that says both high and by and I claim 10186 06:19:59,440 --> 06:20:03,120 now that that back sl0 that null 10187 06:20:01,798 --> 06:20:05,200 character is going to be ever more 10188 06:20:03,120 --> 06:20:06,840 important now if we've got two strings 10189 06:20:05,200 --> 06:20:08,638 in memory so that c knows how to 10190 06:20:06,840 --> 06:20:09,798 distinguish one from the other so let me 10191 06:20:08,638 --> 06:20:11,760 go ahead and just get rid of these two 10192 06:20:09,798 --> 06:20:14,200 lines for the moment let me recreate 10193 06:20:11,760 --> 06:20:16,280 string s equals quote unquote double 10194 06:20:14,200 --> 06:20:17,840 quotes High Let me give myself another 10195 06:20:16,280 --> 06:20:19,520 one and because I'm just playing around 10196 06:20:17,840 --> 06:20:22,958 I'll choose very short variable names 10197 06:20:19,520 --> 06:20:24,840 string T equals quote unquote by 10198 06:20:22,958 --> 06:20:26,840 exclamation point and then let me just 10199 06:20:24,840 --> 06:20:31,240 print them both out uh let me go ahead 10200 06:20:26,840 --> 06:20:35,840 and print out percent s back sln comma s 10201 06:20:31,240 --> 06:20:38,360 and then print F percent s uh back sln 10202 06:20:35,840 --> 06:20:41,878 and then T so very simple demonstration 10203 06:20:38,360 --> 06:20:43,798 of just these two variables make high do 10204 06:20:41,878 --> 06:20:46,360 SL high and of course it prints out two 10205 06:20:43,798 --> 06:20:47,760 lines one after the other what's 10206 06:20:46,360 --> 06:20:48,878 actually going on underneath the hood 10207 06:20:47,760 --> 06:20:50,680 well let's go back to the computer's 10208 06:20:48,878 --> 06:20:52,680 memory High I think it's going to be I 10209 06:20:50,680 --> 06:20:55,000 claim pretty much the same so s I'll 10210 06:20:52,680 --> 06:20:57,040 claim is in the top left followed by the 10211 06:20:55,000 --> 06:20:59,600 back sl0 and that's important now 10212 06:20:57,040 --> 06:21:01,320 because by probably is going to end up 10213 06:20:59,600 --> 06:21:02,638 there and Visually it wraps just by 10214 06:21:01,320 --> 06:21:05,160 nature of how I've drawn this grid of 10215 06:21:02,638 --> 06:21:09,478 bytes but it's continuous B ye 10216 06:21:05,160 --> 06:21:11,798 exclamation point null AKA back0 this is 10217 06:21:09,478 --> 06:21:15,520 Now helpful to print F because now 10218 06:21:11,798 --> 06:21:18,478 printf knows where one begins and ends 10219 06:21:15,520 --> 06:21:20,520 by way of that special null character 10220 06:21:18,478 --> 06:21:23,520 but we can poke around now too what else 10221 06:21:20,520 --> 06:21:27,478 can I do here how about this how about I 10222 06:21:23,520 --> 06:21:31,000 go into my code here back to BS code and 10223 06:21:27,478 --> 06:21:33,680 let me go ahead and say something like 10224 06:21:31,000 --> 06:21:35,280 well if I've got two of these uh strings 10225 06:21:33,680 --> 06:21:38,080 you know let's put them in an array 10226 06:21:35,280 --> 06:21:40,440 let's kind of do this sort of arrays in 10227 06:21:38,080 --> 06:21:43,760 arrays sort of inception style here so 10228 06:21:40,440 --> 06:21:45,798 string words bracket two so give me an 10229 06:21:43,760 --> 06:21:47,160 array of two strings is what I'm saying 10230 06:21:45,798 --> 06:21:48,520 here in code even though we've not done 10231 06:21:47,160 --> 06:21:51,320 it with strings yet we only did it with 10232 06:21:48,520 --> 06:21:54,160 ins and now let me do this the first 10233 06:21:51,320 --> 06:21:57,360 word AKA words bracket zero will equal 10234 06:21:54,160 --> 06:22:00,920 as before high and now words bracket one 10235 06:21:57,360 --> 06:22:02,958 will equal quote unquote by bite so by 10236 06:22:00,920 --> 06:22:05,280 and now I've done the exact same thing 10237 06:22:02,958 --> 06:22:07,040 but again I'm just avoiding having s t 10238 06:22:05,280 --> 06:22:09,280 QR and like all these different 10239 06:22:07,040 --> 06:22:11,878 variables in my code I just now I'm 10240 06:22:09,280 --> 06:22:14,120 treating them as one single array of 10241 06:22:11,878 --> 06:22:15,440 strings how do I change my code down 10242 06:22:14,120 --> 06:22:17,520 here well if I want to print the first 10243 06:22:15,440 --> 06:22:18,958 word I do words bracket zero and if I 10244 06:22:17,520 --> 06:22:21,080 want to print the second word I do words 10245 06:22:18,958 --> 06:22:22,558 bracket one this is not a useful 10246 06:22:21,080 --> 06:22:24,200 exercise at the moment because I'm just 10247 06:22:22,558 --> 06:22:26,080 making my code more complicated but 10248 06:22:24,200 --> 06:22:27,840 again it allows us to post poke around 10249 06:22:26,080 --> 06:22:31,120 and see what's going on because there is 10250 06:22:27,840 --> 06:22:34,478 that high and by but watch this if I 10251 06:22:31,120 --> 06:22:37,160 really want to be cool I can do this 10252 06:22:34,478 --> 06:22:41,320 let's print out percent C percent C 10253 06:22:37,160 --> 06:22:44,680 percent C back sln and then here percent 10254 06:22:41,320 --> 06:22:47,400 C percent C percent C percent C so four 10255 06:22:44,680 --> 06:22:50,280 of those and now here's where things get 10256 06:22:47,400 --> 06:22:52,200 interesting words is an array of strings 10257 06:22:50,280 --> 06:22:55,240 but again if I may what's a 10258 06:22:52,200 --> 06:22:57,840 string an array of characters so just 10259 06:22:55,240 --> 06:22:59,718 use the same logic if words is an array 10260 06:22:57,840 --> 06:23:01,558 of strings you get at the first string 10261 06:22:59,718 --> 06:23:03,520 with words bracket zero how do you get 10262 06:23:01,558 --> 06:23:07,440 at the first character in the first 10263 06:23:03,520 --> 06:23:10,638 string bracket Zer words bracket Z 10264 06:23:07,440 --> 06:23:14,160 bracket one and lastly words bracket Z 10265 06:23:10,638 --> 06:23:16,798 bracket two and now down here words 10266 06:23:14,160 --> 06:23:19,040 bracket one but the first character is 10267 06:23:16,798 --> 06:23:21,558 there words bracket one the second 10268 06:23:19,040 --> 06:23:23,798 character is here words bracket one the 10269 06:23:21,558 --> 06:23:25,478 third character is here whoops third 10270 06:23:23,798 --> 06:23:27,798 character is here and words bracket one 10271 06:23:25,478 --> 06:23:29,280 one the fourth character is here like 10272 06:23:27,798 --> 06:23:31,200 this is not how people program this is 10273 06:23:29,280 --> 06:23:33,840 only for demonstration sake my God it's 10274 06:23:31,200 --> 06:23:37,920 so tedious and verbose already but if I 10275 06:23:33,840 --> 06:23:40,440 make high now do SL high now I'm like 10276 06:23:37,920 --> 06:23:42,958 manually Reinventing percent s if I 10277 06:23:40,440 --> 06:23:45,120 forgot it existed using percent C alone 10278 06:23:42,958 --> 06:23:47,440 but you can indeed manipulate arrays in 10279 06:23:45,120 --> 06:23:50,280 this way but because strings are arrays 10280 06:23:47,440 --> 06:23:53,798 of characters you can manipulate strings 10281 06:23:50,280 --> 06:23:57,040 uh in this way too any question now on 10282 06:23:53,798 --> 06:24:00,280 this syntax 10283 06:23:57,040 --> 06:24:02,760 any questions here no no all right well 10284 06:24:00,280 --> 06:24:04,440 let's go ahead and propose that we solve 10285 06:24:02,760 --> 06:24:06,920 a couple of other problems we might not 10286 06:24:04,440 --> 06:24:08,240 have as before but first a quick visual 10287 06:24:06,920 --> 06:24:10,160 of what's been going on underneath the 10288 06:24:08,240 --> 06:24:12,760 hood here if here again is where we left 10289 06:24:10,160 --> 06:24:15,400 off on the screen high and by uh back to 10290 06:24:12,760 --> 06:24:19,520 back here is really how I just treated 10291 06:24:15,400 --> 06:24:22,320 these things s bracket 0123 and then t0 10292 06:24:19,520 --> 06:24:25,878 1 2 3 4 but really once I put them in an 10293 06:24:22,320 --> 06:24:27,760 array the picture becomes this words Z 10294 06:24:25,878 --> 06:24:29,600 is the whole High words bracket one is 10295 06:24:27,760 --> 06:24:31,600 the whole by but if I really get into 10296 06:24:29,600 --> 06:24:33,798 the weeds and start indexing into 10297 06:24:31,600 --> 06:24:37,200 individual characters in those strings 10298 06:24:33,798 --> 06:24:40,638 all I'm using is new syntax in order to 10299 06:24:37,200 --> 06:24:43,558 represent these same values here 10300 06:24:40,638 --> 06:24:47,680 questions then on these 10301 06:24:43,558 --> 06:24:51,520 representations before we Forge 10302 06:24:47,680 --> 06:24:51,520 ahead no 10303 06:24:52,280 --> 06:24:57,280 yeah does the new line character say 10304 06:24:55,040 --> 06:24:57,280 that one 10305 06:24:58,160 --> 06:25:02,638 more ah really good question does the 10306 06:25:00,200 --> 06:25:04,760 new line character take up any space uh 10307 06:25:02,638 --> 06:25:06,638 it does when so far as printf is 10308 06:25:04,760 --> 06:25:09,600 concerned but I'm not storing the back 10309 06:25:06,638 --> 06:25:12,240 slash n in my strings printf is being 10310 06:25:09,600 --> 06:25:14,680 manually handed that thing 10311 06:25:12,240 --> 06:25:16,558 instead all right so let's go ahead then 10312 06:25:14,680 --> 06:25:18,120 and consider how we might solve some 10313 06:25:16,558 --> 06:25:22,080 problems that have Arisen now with these 10314 06:25:18,120 --> 06:25:24,360 strings as follows here suppose I let's 10315 06:25:22,080 --> 06:25:26,000 do this let me go back to VSS code here 10316 06:25:24,360 --> 06:25:29,958 and let me go ahead and open up a new 10317 06:25:26,000 --> 06:25:31,360 file called how about uh length uh. C 10318 06:25:29,958 --> 06:25:32,680 and let's consider for a moment how I 10319 06:25:31,360 --> 06:25:34,920 might actually figure out what the 10320 06:25:32,680 --> 06:25:36,400 length of a string is which is distinct 10321 06:25:34,920 --> 06:25:37,760 from the length of an array I claimed 10322 06:25:36,400 --> 06:25:39,718 earlier you cannot figure out 10323 06:25:37,760 --> 06:25:41,958 dynamically what the length of an array 10324 06:25:39,718 --> 06:25:44,040 is but I can figure out the length of a 10325 06:25:41,958 --> 06:25:46,280 string specifically because of this 10326 06:25:44,040 --> 06:25:48,280 implementation detail of that null 10327 06:25:46,280 --> 06:25:50,798 character so let me go ahead and do this 10328 06:25:48,280 --> 06:25:54,080 let me include cs50.h in this second 10329 06:25:50,798 --> 06:25:57,160 program here let me include standard i.h 10330 06:25:54,080 --> 06:25:59,478 as before and let me do this int main 10331 06:25:57,160 --> 06:26:01,160 void and in the first thing I'll do is 10332 06:25:59,478 --> 06:26:03,240 just get a string from the user I'll ask 10333 06:26:01,160 --> 06:26:05,878 the user as always for their name so 10334 06:26:03,240 --> 06:26:08,558 I'll call get string and say what's your 10335 06:26:05,878 --> 06:26:10,280 name question mark as always and then 10336 06:26:08,558 --> 06:26:12,798 down here if I want to figure out the 10337 06:26:10,280 --> 06:26:15,760 length of this string and print the name 10338 06:26:12,798 --> 06:26:17,360 the print the length out on the screen 10339 06:26:15,760 --> 06:26:18,638 well I can kind of do this similar in 10340 06:26:17,360 --> 06:26:20,120 spirit to the average where I'm 10341 06:26:18,638 --> 06:26:23,680 accumulating something let me go ahead 10342 06:26:20,120 --> 06:26:26,080 and initialize n to zero let me give 10343 06:26:23,680 --> 06:26:27,400 myself huh it's not not a for Loop 10344 06:26:26,080 --> 06:26:29,240 because I don't have a I don't know in 10345 06:26:27,400 --> 06:26:30,680 advance how long it is but what if I do 10346 06:26:29,240 --> 06:26:36,080 this 10347 06:26:30,680 --> 06:26:39,520 while the value at name bracket n does 10348 06:26:36,080 --> 06:26:42,120 not equal single quote back 10349 06:26:39,520 --> 06:26:43,958 sl0 crazy syntax at the moment but it's 10350 06:26:42,120 --> 06:26:46,200 just the culmination of these various 10351 06:26:43,958 --> 06:26:47,958 building blocks let me just finish the 10352 06:26:46,200 --> 06:26:50,718 thought here 10353 06:26:47,958 --> 06:26:53,920 n++ and then down here let's just print 10354 06:26:50,718 --> 06:26:55,638 out with print F and percent I that 10355 06:26:53,920 --> 06:26:57,600 value of n 10356 06:26:55,638 --> 06:27:00,280 so I claim this is going to show me the 10357 06:26:57,600 --> 06:27:02,600 length of any string I type in whether 10358 06:27:00,280 --> 06:27:04,920 it's high or by or David or anything 10359 06:27:02,600 --> 06:27:05,718 else I initialize a variable to zero and 10360 06:27:04,920 --> 06:27:08,240 that's good because that's where you 10361 06:27:05,718 --> 06:27:11,000 start counting in general while name 10362 06:27:08,240 --> 06:27:12,878 bracket 0 does not equal back0 what is 10363 06:27:11,000 --> 06:27:14,958 this saying well if name is the string 10364 06:27:12,878 --> 06:27:17,320 the user typed in and name is just an 10365 06:27:14,958 --> 06:27:19,320 array as you noted then name bracket 10366 06:27:17,320 --> 06:27:20,878 zero is going to be the first character 10367 06:27:19,320 --> 06:27:23,600 and I'm asking the question well does 10368 06:27:20,878 --> 06:27:26,680 the first character not equal back0 and 10369 06:27:23,600 --> 06:27:28,920 if I type in David D is it's not so I 10370 06:27:26,680 --> 06:27:31,120 keep going and I add one to n then I'm 10371 06:27:28,920 --> 06:27:32,798 going to check name bracket one well if 10372 06:27:31,120 --> 06:27:35,920 I typed in David name bracket one is 10373 06:27:32,798 --> 06:27:38,120 going to be a a does not equal back0 and 10374 06:27:35,920 --> 06:27:40,878 so it's going to go again and again and 10375 06:27:38,120 --> 06:27:43,840 again but five steps in total later it's 10376 06:27:40,878 --> 06:27:45,558 going to get to the bite after David 10377 06:27:43,840 --> 06:27:48,240 realize wait a minute that is a back 10378 06:27:45,558 --> 06:27:50,680 slash n the Loop finishes and I print 10379 06:27:48,240 --> 06:27:53,760 out the total length arrays in general 10380 06:27:50,680 --> 06:27:56,280 do not have this null character however 10381 06:27:53,760 --> 06:27:58,120 strings do again strings are special 10382 06:27:56,280 --> 06:28:00,400 versus all of the other data types we've 10383 06:27:58,120 --> 06:28:03,320 talked about thus far but how could I 10384 06:28:00,400 --> 06:28:04,958 for instance uh do this differently well 10385 06:28:03,320 --> 06:28:07,280 let's actually Factor this out as a 10386 06:28:04,958 --> 06:28:10,120 function as I've I've commonly done but 10387 06:28:07,280 --> 06:28:12,080 rather than implement it myself you know 10388 06:28:10,120 --> 06:28:14,798 what it turns out what's nice about 10389 06:28:12,080 --> 06:28:16,200 strings being so common there are many 10390 06:28:14,798 --> 06:28:17,798 other people who have solved these 10391 06:28:16,200 --> 06:28:21,400 problems before and in fact there's a 10392 06:28:17,798 --> 06:28:24,280 whole string library in C it is used by 10393 06:28:21,400 --> 06:28:27,280 way of a header file called string.h and 10394 06:28:24,280 --> 06:28:29,000 what string H is is a library of string 10395 06:28:27,280 --> 06:28:32,440 related functions in fact you can see in 10396 06:28:29,000 --> 06:28:34,638 cs50's manual pages for C uh the 10397 06:28:32,440 --> 06:28:36,600 string.h functions at least those that 10398 06:28:34,638 --> 06:28:37,798 we recommend as most useful and in 10399 06:28:36,600 --> 06:28:38,958 particular if you poke around there 10400 06:28:37,798 --> 06:28:42,040 you'll see that there's a function 10401 06:28:38,958 --> 06:28:43,760 called Sterling means string length it 10402 06:28:42,040 --> 06:28:45,320 was named very succinctly just because 10403 06:28:43,760 --> 06:28:47,718 it's a little easier to type than string 10404 06:28:45,320 --> 06:28:49,680 length but Sterling tells you the length 10405 06:28:47,718 --> 06:28:51,840 of a string so how might I use this in 10406 06:28:49,680 --> 06:28:54,320 my code here well it turns out I can 10407 06:28:51,840 --> 06:28:57,520 simplify this quite a bit let me get rid 10408 06:28:54,320 --> 06:29:00,160 of my Loop get rid of my uh my counting 10409 06:28:57,520 --> 06:29:04,798 manually and do something like this int 10410 06:29:00,160 --> 06:29:07,080 n equals Sterling of the human's name 10411 06:29:04,798 --> 06:29:09,600 name and now I'll just use print f as 10412 06:29:07,080 --> 06:29:12,600 before with percent I back sln and 10413 06:29:09,600 --> 06:29:15,440 output the value of n but but there's a 10414 06:29:12,600 --> 06:29:17,400 bug at the moment what have I forgotten 10415 06:29:15,440 --> 06:29:19,840 to 10416 06:29:17,400 --> 06:29:22,080 do yeah I have to include the header 10417 06:29:19,840 --> 06:29:23,920 file at the top of the screen so let me 10418 06:29:22,080 --> 06:29:27,320 at the top of the code so let me also 10419 06:29:23,920 --> 06:29:29,958 include string.h at the top of my file 10420 06:29:27,320 --> 06:29:32,600 so that c knows that in fact sterl 10421 06:29:29,958 --> 06:29:33,878 exists let me go ahead and make length 10422 06:29:32,600 --> 06:29:36,840 as 10423 06:29:33,878 --> 06:29:39,400 before length uh or actually really for 10424 06:29:36,840 --> 06:29:41,760 the first time what's your name David 10425 06:29:39,400 --> 06:29:43,840 and hopefully I'm going to see in fact 10426 06:29:41,760 --> 06:29:46,360 five by contrast if I run it again and 10427 06:29:43,840 --> 06:29:48,558 type in high exclamation point now I see 10428 06:29:46,360 --> 06:29:50,120 three so Sterling is just one of the 10429 06:29:48,558 --> 06:29:52,200 functions in that library and there's so 10430 06:29:50,120 --> 06:29:53,718 many more in fact yet another library 10431 06:29:52,200 --> 06:29:55,718 that might be useful moving forward is 10432 06:29:53,718 --> 06:29:58,120 this one C 10433 06:29:55,718 --> 06:30:00,360 which relates to C data types and lots 10434 06:29:58,120 --> 06:30:01,920 of functions therein that can be useful 10435 06:30:00,360 --> 06:30:04,280 for instance if you review its 10436 06:30:01,920 --> 06:30:06,520 documentation in the manual pages online 10437 06:30:04,280 --> 06:30:09,240 you'll see that there are functions via 10438 06:30:06,520 --> 06:30:12,718 which we can solve problems like this 10439 06:30:09,240 --> 06:30:18,080 let me go ahead and propose here let me 10440 06:30:12,718 --> 06:30:20,920 see let's do um example here 10441 06:30:18,080 --> 06:30:23,478 involving how about uh checking if 10442 06:30:20,920 --> 06:30:26,638 something is uppercase or lowercase and 10443 06:30:23,478 --> 06:30:28,798 converting it uh to uppercase only let 10444 06:30:26,638 --> 06:30:31,680 me go back to vs code and code a program 10445 06:30:28,798 --> 06:30:33,320 called uppercase Doc in this file I'm 10446 06:30:31,680 --> 06:30:34,160 going to start by including now as 10447 06:30:33,320 --> 06:30:37,680 always 10448 06:30:34,160 --> 06:30:40,558 cs50.h I'm going to include standard i.h 10449 06:30:37,680 --> 06:30:42,160 and I'm going to add one other to the 10450 06:30:40,558 --> 06:30:44,920 mix which is 10451 06:30:42,160 --> 06:30:47,760 string.h now2 so I can access the length 10452 06:30:44,920 --> 06:30:50,040 of things as needed int main void comes 10453 06:30:47,760 --> 06:30:50,958 next and then within my main function 10454 06:30:50,040 --> 06:30:52,840 I'm going to go ahead and declare a 10455 06:30:50,958 --> 06:30:54,920 string called s I'm going to call get 10456 06:30:52,840 --> 06:30:57,200 string as before and I'm going to go 10457 06:30:54,920 --> 06:30:58,958 head and just ask the user for a string 10458 06:30:57,200 --> 06:31:00,638 called before I want to do it before and 10459 06:30:58,958 --> 06:31:02,958 after whatever the user types in is 10460 06:31:00,638 --> 06:31:06,200 before but I want to force everything to 10461 06:31:02,958 --> 06:31:09,760 uppercase thereafter let me now in this 10462 06:31:06,200 --> 06:31:12,440 Loop here do this let me print F quote 10463 06:31:09,760 --> 06:31:16,600 unquote after just so we can see this on 10464 06:31:12,440 --> 06:31:22,478 the screen and let me do four in I gets 10465 06:31:16,600 --> 06:31:23,958 zero I is less than stir Lang of s I ++ 10466 06:31:22,478 --> 06:31:25,478 what am I about to do I'm about to 10467 06:31:23,958 --> 06:31:27,680 iterate over every every character in 10468 06:31:25,478 --> 06:31:30,398 the string from left to right from zero 10469 06:31:27,680 --> 06:31:32,920 on up two but not through the length of 10470 06:31:30,398 --> 06:31:35,280 s and how do I check if something is 10471 06:31:32,920 --> 06:31:38,080 lowercase so that I can actually force 10472 06:31:35,280 --> 06:31:41,718 it to uppercase well it turns out I 10473 06:31:38,080 --> 06:31:45,240 could do this literally if the character 10474 06:31:41,718 --> 06:31:48,718 in s at location I is greater than or 10475 06:31:45,240 --> 06:31:51,360 equal to Capital a Ampersand Ampersand 10476 06:31:48,718 --> 06:31:55,080 which means and instead of or which we 10477 06:31:51,360 --> 06:31:57,798 saw in the past s braet i is less than 10478 06:31:55,080 --> 06:32:00,240 or equal to little Z that means 10479 06:31:57,798 --> 06:32:03,120 logically in English that this is indeed 10480 06:32:00,240 --> 06:32:05,680 a lowercase how do I now convert it to 10481 06:32:03,120 --> 06:32:07,398 uppercase this character well I could 10482 06:32:05,680 --> 06:32:09,000 just literally print out the same 10483 06:32:07,398 --> 06:32:10,920 character but that would not be the 10484 06:32:09,000 --> 06:32:14,160 answer here because that's not changing 10485 06:32:10,920 --> 06:32:16,280 the value but what could I do instead 10486 06:32:14,160 --> 06:32:20,160 well let me actually pull up here real 10487 06:32:16,280 --> 06:32:23,398 fast the asky chart as before and let's 10488 06:32:20,160 --> 06:32:25,160 see if we can't glean some insight if I 10489 06:32:23,398 --> 06:32:26,600 pull up the same ask each chart and 10490 06:32:25,160 --> 06:32:29,000 suppose the human has typed in a 10491 06:32:26,600 --> 06:32:32,398 lowercase a that's 10492 06:32:29,000 --> 06:32:34,478 97 what letter I want to convert it to 10493 06:32:32,398 --> 06:32:39,478 uppercase a so what number do I want to 10494 06:32:34,478 --> 06:32:41,160 convert the 97 to per week zero so 65 we 10495 06:32:39,478 --> 06:32:43,080 keep coming back to that one what if the 10496 06:32:41,160 --> 06:32:47,200 user types in lowercase B I want to 10497 06:32:43,080 --> 06:32:49,958 change the 98 value to 66 and so forth 10498 06:32:47,200 --> 06:32:52,320 and any quick math how far part of those 10499 06:32:49,958 --> 06:32:54,160 so it's always 32 like uppercase to 10500 06:32:52,320 --> 06:32:58,120 lower case is always wonderfully good 10501 06:32:54,160 --> 06:32:59,520 design 32 away one from the other so 10502 06:32:58,120 --> 06:33:01,120 what does this mean well I think we saw 10503 06:32:59,520 --> 06:33:03,200 earlier that underneath the hood a Char 10504 06:33:01,120 --> 06:33:05,280 is just a number you can certainly do 10505 06:33:03,200 --> 06:33:06,920 arithmetic on it and here again if you 10506 06:33:05,280 --> 06:33:09,638 understand these lower level Primitives 10507 06:33:06,920 --> 06:33:12,080 what if I do this whatever s bracket I 10508 06:33:09,638 --> 06:33:14,600 is if I know on line 13 that it's up 10509 06:33:12,080 --> 06:33:16,520 that it's lowercase do I want to add or 10510 06:33:14,600 --> 06:33:18,958 subtract 10511 06:33:16,520 --> 06:33:24,040 32 so I want to subtract because I want 10512 06:33:18,958 --> 06:33:25,478 to go from like 97 to 65 or 98 to 66 so 10513 06:33:24,040 --> 06:33:27,920 indeed if you do some quick math that 10514 06:33:25,478 --> 06:33:30,760 gives you 32 so it suffices to just 10515 06:33:27,920 --> 06:33:33,120 treat chars as numbers subtract the 32 10516 06:33:30,760 --> 06:33:36,240 and printing it with percent C I think 10517 06:33:33,120 --> 06:33:37,638 will just convert lowercase to uppercase 10518 06:33:36,240 --> 06:33:39,878 if you now fast forward to like the real 10519 06:33:37,638 --> 06:33:41,280 world Microsoft Word or Google Docs if 10520 06:33:39,878 --> 06:33:43,080 you've ever chosen the menu option that 10521 06:33:41,280 --> 06:33:44,958 forces things to uppercase or lowercase 10522 06:33:43,080 --> 06:33:46,360 on occasion literally that's what 10523 06:33:44,958 --> 06:33:47,840 Microsoft and Google have done they 10524 06:33:46,360 --> 06:33:49,558 iterate over every character in the 10525 06:33:47,840 --> 06:33:52,520 document check if it's lowercase and if 10526 06:33:49,558 --> 06:33:55,520 so they subtract 32 from it and show you 10527 06:33:52,520 --> 06:33:57,600 the new value what if though it is not a 10528 06:33:55,520 --> 06:33:59,160 lowercase letter I think I can keep it 10529 06:33:57,600 --> 06:34:02,080 easy and just print out the current 10530 06:33:59,160 --> 06:34:04,280 letter unchanged if my goal is to Simply 10531 06:34:02,080 --> 06:34:06,920 Force things to all uppercase and that 10532 06:34:04,280 --> 06:34:09,320 letter then would be S bracket I so let 10533 06:34:06,920 --> 06:34:12,398 me go ahead now and make uppercase 10534 06:34:09,320 --> 06:34:14,680 hopefully no errors do/ uppercase and 10535 06:34:12,398 --> 06:34:16,478 I'll now now type in David with an 10536 06:34:14,680 --> 06:34:19,920 uppercase D but lowercase everything 10537 06:34:16,478 --> 06:34:22,280 else but now the after version is David 10538 06:34:19,920 --> 06:34:24,478 an aesthetic bug notice here I forgot to 10539 06:34:22,280 --> 06:34:26,920 include just for prettiness sake about 10540 06:34:24,478 --> 06:34:29,798 back sln at the end no problem I'll add 10541 06:34:26,920 --> 06:34:33,398 that let me fix my mistake make 10542 06:34:29,798 --> 06:34:35,240 uppercase SL uppercase enter daid enter 10543 06:34:33,398 --> 06:34:37,000 and voila and I I deliberately added 10544 06:34:35,240 --> 06:34:38,638 another space after the after just so 10545 06:34:37,000 --> 06:34:40,760 they would line up pretty even though 10546 06:34:38,638 --> 06:34:43,440 before and after have different numbers 10547 06:34:40,760 --> 06:34:45,760 of letters questions then on this 10548 06:34:43,440 --> 06:34:47,280 implementation of forcing something to 10549 06:34:45,760 --> 06:34:48,638 uppercase which in and of itself is not 10550 06:34:47,280 --> 06:34:50,638 all that enlightening but is 10551 06:34:48,638 --> 06:34:52,958 representative now of how you can 10552 06:34:50,638 --> 06:34:55,958 leverage these low-level 10553 06:34:52,958 --> 06:34:58,120 Primitives question 10554 06:34:55,958 --> 06:34:59,638 now all right well this honestly is 10555 06:34:58,120 --> 06:35:01,360 tedious my God like does Microsoft 10556 06:34:59,638 --> 06:35:02,878 Google everyone you have to literally 10557 06:35:01,360 --> 06:35:05,080 write out this code just to do something 10558 06:35:02,878 --> 06:35:07,000 simple well no that's again why we have 10559 06:35:05,080 --> 06:35:09,080 things like libraries and increasingly 10560 06:35:07,000 --> 06:35:11,160 now for problem sets projects and Beyond 10561 06:35:09,080 --> 06:35:12,958 well you just use libraries more often 10562 06:35:11,160 --> 06:35:15,320 off the shelf so as to solve problems 10563 06:35:12,958 --> 06:35:18,440 that surely other people have had before 10564 06:35:15,320 --> 06:35:21,200 you so how can I now use this uh Library 10565 06:35:18,440 --> 06:35:23,478 ctype.h well let me go back into my code 10566 06:35:21,200 --> 06:35:25,600 let me include this among my header 10567 06:35:23,478 --> 06:35:27,478 files here I tend just so I can skim 10568 06:35:25,600 --> 06:35:28,680 things easily I tend to alphabetize my 10569 06:35:27,478 --> 06:35:30,080 headers but that's not strictly 10570 06:35:28,680 --> 06:35:32,160 necessary but it allows me at a glance 10571 06:35:30,080 --> 06:35:34,478 to realize did I or did I not include 10572 06:35:32,160 --> 06:35:37,080 something I need now let me go ahead and 10573 06:35:34,478 --> 06:35:40,440 do this it turns out if you read the 10574 06:35:37,080 --> 06:35:42,718 documentation for the ctype library 10575 06:35:40,440 --> 06:35:46,000 there is a function wonderfully called 10576 06:35:42,718 --> 06:35:48,920 if is lower that takes in a character as 10577 06:35:46,000 --> 06:35:51,320 its argument essentially so s bracket I 10578 06:35:48,920 --> 06:35:52,958 and if that returns true a Boolean value 10579 06:35:51,320 --> 06:35:55,160 if you will well I'm going to force it 10580 06:35:52,958 --> 06:35:57,798 to lowercase but I don't have to do this 10581 06:35:55,160 --> 06:35:59,520 this math anymore turns out in the ctype 10582 06:35:57,798 --> 06:36:01,600 library there's also a function called 10583 06:35:59,520 --> 06:36:03,718 two upper that takes a character as 10584 06:36:01,600 --> 06:36:05,360 input like s bracket I and it just does 10585 06:36:03,718 --> 06:36:07,520 the math for you so that you can 10586 06:36:05,360 --> 06:36:09,160 abstract away the 32 thing and just know 10587 06:36:07,520 --> 06:36:11,280 that someone else has solved that 10588 06:36:09,160 --> 06:36:13,240 problem for you otherwise I can leave my 10589 06:36:11,280 --> 06:36:15,638 code unchanged down below because I'm 10590 06:36:13,240 --> 06:36:18,600 not changing anything else so if I do 10591 06:36:15,638 --> 06:36:21,798 make uppercase now and then do SL 10592 06:36:18,600 --> 06:36:24,080 uppercase daavid with just a capital D 10593 06:36:21,798 --> 06:36:26,000 and now it still works but if you read 10594 06:36:24,080 --> 06:36:28,200 the documentation further it turns out 10595 06:36:26,000 --> 06:36:30,040 that two upper is smart if you pass in a 10596 06:36:28,200 --> 06:36:31,878 character to two upper that's lowercase 10597 06:36:30,040 --> 06:36:33,958 it obviously converts its uppercase by 10598 06:36:31,878 --> 06:36:36,440 doing that math but if you pass in a 10599 06:36:33,958 --> 06:36:38,160 character to to Upper that's already 10600 06:36:36,440 --> 06:36:40,398 uppercase the documentation you would 10601 06:36:38,160 --> 06:36:43,200 see tells you that it leaves it 10602 06:36:40,398 --> 06:36:46,040 unchanged so I can tighten all of this 10603 06:36:43,200 --> 06:36:48,920 up I can get rid of the whole else I can 10604 06:36:46,040 --> 06:36:50,840 get rid of the whole if and arguably now 10605 06:36:48,920 --> 06:36:54,520 Implement a program that's just as 10606 06:36:50,840 --> 06:36:56,600 correct but better designed why fewer 10607 06:36:54,520 --> 06:36:58,398 lines of code easier to read Le lower 10608 06:36:56,600 --> 06:37:00,360 probability of mistakes assuming the 10609 06:36:58,398 --> 06:37:02,440 library is correct it just makes it 10610 06:37:00,360 --> 06:37:04,920 easier and faster for me now to write 10611 06:37:02,440 --> 06:37:08,320 code so if I now do one last time make 10612 06:37:04,920 --> 06:37:11,160 uppercase enter uppercase and type in my 10613 06:37:08,320 --> 06:37:13,320 name still working but now notice we've 10614 06:37:11,160 --> 06:37:16,680 whittel this down to far fewer lines of 10615 06:37:13,320 --> 06:37:21,558 code albeit using now this additional 10616 06:37:16,680 --> 06:37:24,798 Library questions then on how we did 10617 06:37:21,558 --> 06:37:26,398 this no well even even though this code 10618 06:37:24,798 --> 06:37:29,040 I dare say is correct it's not 10619 06:37:26,398 --> 06:37:31,798 necessarily welld designed just yet in 10620 06:37:29,040 --> 06:37:33,398 fact there's one line of code one 10621 06:37:31,798 --> 06:37:36,240 function call in this current 10622 06:37:33,398 --> 06:37:38,760 implementation that's more inefficient 10623 06:37:36,240 --> 06:37:41,920 than it needs to be and allow me to draw 10624 06:37:38,760 --> 06:37:44,558 our attention to this here line 10 10625 06:37:41,920 --> 06:37:46,638 wherein we're calling Sterling but we're 10626 06:37:44,558 --> 06:37:49,240 calling it inside of this for Loop 10627 06:37:46,638 --> 06:37:51,320 specifically inside of the condition and 10628 06:37:49,240 --> 06:37:52,680 why might that not necessarily be the 10629 06:37:51,320 --> 06:37:55,558 best 10630 06:37:52,680 --> 06:37:57,440 idea well is the length of the string s 10631 06:37:55,558 --> 06:37:59,200 changing ever I mean certainly not 10632 06:37:57,440 --> 06:38:01,558 within the span of this Loop and so here 10633 06:37:59,200 --> 06:38:04,398 we are within our for loop on line 10 11 10634 06:38:01,558 --> 06:38:06,080 12 and 13 asking on every iteration that 10635 06:38:04,398 --> 06:38:07,638 same question what's the length of s 10636 06:38:06,080 --> 06:38:10,040 what's the length of s what's the length 10637 06:38:07,638 --> 06:38:11,320 of s and in turn we're calling Sterling 10638 06:38:10,040 --> 06:38:13,558 every time even though we're getting 10639 06:38:11,320 --> 06:38:15,360 back the same answer so I dare say a 10640 06:38:13,558 --> 06:38:17,600 better solution here would be to maybe 10641 06:38:15,360 --> 06:38:19,600 figure out the length of s earlier on in 10642 06:38:17,600 --> 06:38:20,718 my code and maybe declare a variable or 10643 06:38:19,600 --> 06:38:22,478 perhaps do something that's 10644 06:38:20,718 --> 06:38:24,440 syntactically a little more elegant and 10645 06:38:22,478 --> 06:38:26,160 in fact a very common design in Loop 10646 06:38:24,440 --> 06:38:28,520 like this would be to declare not just 10647 06:38:26,160 --> 06:38:31,200 one variable like I but to actually 10648 06:38:28,520 --> 06:38:33,520 declare a second variable called n for 10649 06:38:31,200 --> 06:38:36,798 instance where n is just some number Set 10650 06:38:33,520 --> 06:38:38,798 n equal to the length of s but 10651 06:38:36,798 --> 06:38:41,478 thereafter inside of this condition 10652 06:38:38,798 --> 06:38:44,520 instead of calling Sterling of s again 10653 06:38:41,478 --> 06:38:47,478 and again and again what might I now do 10654 06:38:44,520 --> 06:38:49,120 I could instead just compare I against n 10655 06:38:47,478 --> 06:38:50,920 itself because n now will only be 10656 06:38:49,120 --> 06:38:52,718 calculated once when it's initialized 10657 06:38:50,920 --> 06:38:54,080 just as I is initialized to zero and 10658 06:38:52,718 --> 06:38:56,520 thereafter we're going to be compar 10659 06:38:54,080 --> 06:38:57,718 comparing I which is changing against n 10660 06:38:56,520 --> 06:39:00,520 which will not be so it's going to be 10661 06:38:57,718 --> 06:39:02,280 marginally more efficient by Design now 10662 06:39:00,520 --> 06:39:04,478 with that said a good compiler could 10663 06:39:02,280 --> 06:39:06,440 also recognize that there was this 10664 06:39:04,478 --> 06:39:08,320 optimization possibility and maybe do it 10665 06:39:06,440 --> 06:39:10,000 for us but for now best to get into the 10666 06:39:08,320 --> 06:39:12,958 Habit best to develop the muscle memory 10667 06:39:10,000 --> 06:39:17,840 for making those better design decisions 10668 06:39:12,958 --> 06:39:21,600 yourselves questions then on how we did 10669 06:39:17,840 --> 06:39:24,280 this no all right a few final building 10670 06:39:21,600 --> 06:39:25,718 blocks for the day so we started by 10671 06:39:24,280 --> 06:39:28,638 talking about those command line 10672 06:39:25,718 --> 06:39:30,878 arguments that clang uses whereby 10673 06:39:28,638 --> 06:39:34,360 anything after the command that you type 10674 06:39:30,878 --> 06:39:37,600 at a prompt be it make or clang or even 10675 06:39:34,360 --> 06:39:39,680 CD in Linux any word thereafter or 10676 06:39:37,600 --> 06:39:42,000 something cryptic like- O is a 10677 06:39:39,680 --> 06:39:43,440 commandline argument it's an input to 10678 06:39:42,000 --> 06:39:44,798 the command it's different from a 10679 06:39:43,440 --> 06:39:46,558 function argument because a function 10680 06:39:44,798 --> 06:39:48,280 argument of course is an input to a 10681 06:39:46,558 --> 06:39:49,760 function but it's the same idea it's 10682 06:39:48,280 --> 06:39:52,398 just different syntax after the dollar 10683 06:39:49,760 --> 06:39:53,718 sign at the prompt well it turns out 10684 06:39:52,398 --> 06:39:56,680 that command line arguments are 10685 06:39:53,718 --> 06:39:58,638 something you can now use in your own 10686 06:39:56,680 --> 06:40:01,798 programs by 10687 06:39:58,638 --> 06:40:04,080 accessing uh words after the prompt and 10688 06:40:01,798 --> 06:40:06,958 let me propose that we invent we invent 10689 06:40:04,080 --> 06:40:09,840 this as follows let me propose that we 10690 06:40:06,958 --> 06:40:13,558 switch back to vs code here and I'll 10691 06:40:09,840 --> 06:40:15,200 open a new file here called greet doc so 10692 06:40:13,558 --> 06:40:17,200 in greet Doc is going to be a program 10693 06:40:15,200 --> 06:40:18,680 that very simply greets the user had we 10694 06:40:17,200 --> 06:40:21,160 written this last week we would have 10695 06:40:18,680 --> 06:40:25,160 done this include 10696 06:40:21,160 --> 06:40:28,478 cs50.h and then include uh standard 10697 06:40:25,160 --> 06:40:30,680 i.h and then int main void and then we 10698 06:40:28,478 --> 06:40:33,718 might do something simple like string 10699 06:40:30,680 --> 06:40:36,160 name equals get string quote unquote 10700 06:40:33,718 --> 06:40:38,240 what's your name question mark and then 10701 06:40:36,160 --> 06:40:40,638 we would have printed out as always 10702 06:40:38,240 --> 06:40:43,000 hello comma percent s and then plugging 10703 06:40:40,638 --> 06:40:45,160 in that name so this is the same program 10704 06:40:43,000 --> 06:40:47,200 we've implemented many times just to 10705 06:40:45,160 --> 06:40:49,600 make sure it works although nope that's 10706 06:40:47,200 --> 06:40:52,200 not quite the same program semicolon in 10707 06:40:49,600 --> 06:40:55,398 the wrong place this now is the same 10708 06:40:52,200 --> 06:40:57,718 program so make greet /g greet and I'll 10709 06:40:55,398 --> 06:41:00,200 type in my own name hello David so we're 10710 06:40:57,718 --> 06:41:02,440 back there now what's arguably a little 10711 06:41:00,200 --> 06:41:04,760 Annoying about this program if I type in 10712 06:41:02,440 --> 06:41:06,440 something else like Carter enter you 10713 06:41:04,760 --> 06:41:09,080 know I have to run the program wait for 10714 06:41:06,440 --> 06:41:11,360 the prompt type in my name hit enter and 10715 06:41:09,080 --> 06:41:13,240 that's fine but imagine if every program 10716 06:41:11,360 --> 06:41:15,120 worked like this like make suppose you 10717 06:41:13,240 --> 06:41:16,520 could only type make then you wait for a 10718 06:41:15,120 --> 06:41:17,798 prompt then you type the name of the 10719 06:41:16,520 --> 06:41:20,600 program you want to make then you hit 10720 06:41:17,798 --> 06:41:22,240 enter or worse in Linux when you have to 10721 06:41:20,600 --> 06:41:24,200 change directories as you might have for 10722 06:41:22,240 --> 06:41:26,360 problem set one what if you had to type 10723 06:41:24,200 --> 06:41:28,000 CD enter now type the name of the folder 10724 06:41:26,360 --> 06:41:30,280 you want to change into enter I mean it 10725 06:41:28,000 --> 06:41:32,160 just slows life down and so it just gets 10726 06:41:30,280 --> 06:41:33,718 annoying quickly so commandline 10727 06:41:32,160 --> 06:41:36,920 arguments just let you express your 10728 06:41:33,718 --> 06:41:39,440 whole thought all at once so how can I 10729 06:41:36,920 --> 06:41:41,558 do this well if I want to express the 10730 06:41:39,440 --> 06:41:45,558 notion of command line arguments in my 10731 06:41:41,558 --> 06:41:48,398 code I could do something like this I 10732 06:41:45,558 --> 06:41:51,040 could for the very first time go up and 10733 06:41:48,398 --> 06:41:53,878 get rid of this void which as of today 10734 06:41:51,040 --> 06:41:56,680 means this program takes no command line 10735 06:41:53,878 --> 06:42:03,320 arguments and I can change it to exactly 10736 06:41:56,680 --> 06:42:05,160 this int ARG C string argv with brackets 10737 06:42:03,320 --> 06:42:07,760 now it's cryptic admittedly and let me 10738 06:42:05,160 --> 06:42:11,160 zoom in but I think we can perhaps infer 10739 06:42:07,760 --> 06:42:12,798 now what's going on if main Now does not 10740 06:42:11,160 --> 06:42:15,398 have void as its input which means it 10741 06:42:12,798 --> 06:42:17,320 takes no arguments surely the spoiler 10742 06:42:15,398 --> 06:42:20,120 here is that now main will take command 10743 06:42:17,320 --> 06:42:25,120 line argument somehow any guesses as to 10744 06:42:20,120 --> 06:42:27,200 what argv is or will be 10745 06:42:25,120 --> 06:42:30,080 what might this 10746 06:42:27,200 --> 06:42:33,360 represent it's an array of strings right 10747 06:42:30,080 --> 06:42:33,360 by way of the syntax 10748 06:42:34,398 --> 06:42:38,080 yeah exactly it will be all of the 10749 06:42:36,478 --> 06:42:40,798 characters or really all of the words 10750 06:42:38,080 --> 06:42:43,240 that you type at the prompt argc as an 10751 06:42:40,798 --> 06:42:45,280 INT any 10752 06:42:43,240 --> 06:42:47,398 guess 10753 06:42:45,280 --> 06:42:48,680 argument count is what it generally 10754 06:42:47,398 --> 06:42:50,398 stands for though technically you could 10755 06:42:48,680 --> 06:42:52,280 call these things any anything but this 10756 06:42:50,398 --> 06:42:54,600 is the convention because I claimed 10757 06:42:52,280 --> 06:42:56,840 earlier that arrays don't keep track of 10758 06:42:54,600 --> 06:42:58,798 their own length if you want to know how 10759 06:42:56,840 --> 06:43:00,840 many words the human typed at the prompt 10760 06:42:58,798 --> 06:43:03,280 after your program's name you have to be 10761 06:43:00,840 --> 06:43:06,398 told not just the array of the words but 10762 06:43:03,280 --> 06:43:07,718 the length of that array the strings you 10763 06:43:06,398 --> 06:43:09,920 can figure out the length of using 10764 06:43:07,718 --> 06:43:12,680 Sterling but you can't figure out the 10765 06:43:09,920 --> 06:43:14,440 length of the array of strings the 10766 06:43:12,680 --> 06:43:17,080 collection of words that the human typed 10767 06:43:14,440 --> 06:43:19,638 in so how can I now use this well let me 10768 06:43:17,080 --> 06:43:22,398 go ahead and do this let me go ahead and 10769 06:43:19,638 --> 06:43:24,240 change this program now just to be print 10770 06:43:22,398 --> 06:43:29,558 F quote unquote 10771 06:43:24,240 --> 06:43:32,520 hello comma uh percent s back sln then 10772 06:43:29,558 --> 06:43:34,760 Arvy bracket 1 so this is not the best 10773 06:43:32,520 --> 06:43:39,000 version of my code yet but it's my first 10774 06:43:34,760 --> 06:43:43,360 make greet and now let me dog greet 10775 06:43:39,000 --> 06:43:46,440 David all at once enter hello David now 10776 06:43:43,360 --> 06:43:48,440 let me run it again dogre Carter enter 10777 06:43:46,440 --> 06:43:50,240 hello Carter you know it's a marginal 10778 06:43:48,440 --> 06:43:52,160 Improvement but I don't have to wait for 10779 06:43:50,240 --> 06:43:53,958 get string to prompt me to hit enter 10780 06:43:52,160 --> 06:43:57,398 it's just speeding things up you know is 10781 06:43:53,958 --> 06:43:59,600 fast one less command to type in but I 10782 06:43:57,398 --> 06:44:02,718 deliberately did bracket one but where 10783 06:43:59,600 --> 06:44:06,440 what's the beginning of arv it would be 10784 06:44:02,718 --> 06:44:08,760 bracket zero well what's that this is 10785 06:44:06,440 --> 06:44:11,718 sometimes useful though for now it's not 10786 06:44:08,760 --> 06:44:14,718 suppose I recompile my code and run this 10787 06:44:11,718 --> 06:44:18,280 program now greet David anyone want to 10788 06:44:14,718 --> 06:44:18,280 guess what's in argv 10789 06:44:18,398 --> 06:44:25,000 Z say 10790 06:44:20,160 --> 06:44:26,840 again greet enter hello. SLG greet so if 10791 06:44:25,000 --> 06:44:28,798 you want sort of inception style your 10792 06:44:26,840 --> 06:44:30,760 program to figure out what its own name 10793 06:44:28,798 --> 06:44:33,000 is or at least how it was executed at 10794 06:44:30,760 --> 06:44:35,440 the command line at the terminal you can 10795 06:44:33,000 --> 06:44:37,360 look at ARG v0 in general probably not 10796 06:44:35,440 --> 06:44:39,600 that useful probably better to start 10797 06:44:37,360 --> 06:44:41,878 looking at bracket one which was the 10798 06:44:39,600 --> 06:44:43,440 first word after the program name and if 10799 06:44:41,878 --> 06:44:46,000 there were more I could do this how 10800 06:44:43,440 --> 06:44:49,398 about RV bracket 2 let me add in a 10801 06:44:46,000 --> 06:44:53,760 second percent s let me recompile greet 10802 06:44:49,398 --> 06:44:56,280 let me do greet David maen enter and 10803 06:44:53,760 --> 06:44:57,878 that two now works taking in two words 10804 06:44:56,280 --> 06:44:59,320 at the prompt if I really want to be 10805 06:44:57,878 --> 06:45:02,398 smart at this now I could do something 10806 06:44:59,320 --> 06:45:06,558 like this though how about if the count 10807 06:45:02,398 --> 06:45:08,760 of arguments AKA AR C equals equals 2 10808 06:45:06,558 --> 06:45:12,160 then assume that the human typed in only 10809 06:45:08,760 --> 06:45:16,320 their first name and do print F uh hello 10810 06:45:12,160 --> 06:45:20,000 comma percent s back sln and then uh 10811 06:45:16,320 --> 06:45:22,160 argv bracket 1 else if the human did not 10812 06:45:20,000 --> 06:45:24,958 provide exactly two arguments the name 10813 06:45:22,160 --> 06:45:26,798 of the program and their own name let's 10814 06:45:24,958 --> 06:45:28,638 just print out a default value L they 10815 06:45:26,798 --> 06:45:31,080 forgot their name or they typed in two 10816 06:45:28,638 --> 06:45:33,160 names or three names let's just do uh 10817 06:45:31,080 --> 06:45:35,440 hello comma world as a default and we'll 10818 06:45:33,160 --> 06:45:38,558 just ignore what the human typed in if I 10819 06:45:35,440 --> 06:45:42,760 recompile this make greet I can dog 10820 06:45:38,558 --> 06:45:46,000 greet and David again enter oops uh 10821 06:45:42,760 --> 06:45:50,040 sorry what am I missing yeah so newbie 10822 06:45:46,000 --> 06:45:53,160 mistake else all right make greet again 10823 06:45:50,040 --> 06:45:54,040 dog greet David enter there's my hello 10824 06:45:53,160 --> 06:45:56,200 David 10825 06:45:54,040 --> 06:45:58,000 but if I omit my name I just get the 10826 06:45:56,200 --> 06:46:00,200 generic like a default value and if I 10827 06:45:58,000 --> 06:46:02,000 get a little curious and I type in both 10828 06:46:00,200 --> 06:46:03,798 names then I get ignored two why because 10829 06:46:02,000 --> 06:46:06,160 I just haven't built in support for arxy 10830 06:46:03,798 --> 06:46:08,920 of three I could do anything I want but 10831 06:46:06,160 --> 06:46:11,360 now we have access to these kinds of 10832 06:46:08,920 --> 06:46:13,718 building blocks all right what else 10833 06:46:11,360 --> 06:46:16,000 might I do here well it turns out there 10834 06:46:13,718 --> 06:46:19,320 might be some final features for us to 10835 06:46:16,000 --> 06:46:20,760 now execute um notice though that in C 10836 06:46:19,320 --> 06:46:23,558 despite what you might see in books or 10837 06:46:20,760 --> 06:46:25,558 online tutorials nowadays the two 10838 06:46:23,558 --> 06:46:27,000 official formats for defining a main 10839 06:46:25,558 --> 06:46:30,440 function are either this which we've 10840 06:46:27,000 --> 06:46:33,280 been using now for Two Plus weeks or now 10841 06:46:30,440 --> 06:46:36,600 this whereby you change the void to int 10842 06:46:33,280 --> 06:46:38,360 argc and then for now string argv and 10843 06:46:36,600 --> 06:46:40,120 then empty brackets and we'll see that 10844 06:46:38,360 --> 06:46:42,040 this two is an a simplification some 10845 06:46:40,120 --> 06:46:43,558 training wheels if you will but for now 10846 06:46:42,040 --> 06:46:45,080 those are the two forms even though you 10847 06:46:43,558 --> 06:46:47,360 will see in online tutorials and even 10848 06:46:45,080 --> 06:46:49,520 books some people use Maine in different 10849 06:46:47,360 --> 06:46:51,320 ways these are the two now to keep in 10850 06:46:49,520 --> 06:46:52,840 mind and I'll note that these command 10851 06:46:51,320 --> 06:46:54,360 line arguments are kind of all over the 10852 06:46:52,840 --> 06:46:55,760 place didn't probably expect to see this 10853 06:46:54,360 --> 06:46:57,520 word on the screen here and what does it 10854 06:46:55,760 --> 06:46:58,920 mean well it turns out that for decades 10855 06:46:57,520 --> 06:47:01,080 there's actually this program that comes 10856 06:46:58,920 --> 06:47:03,600 with Linux systems in particular called 10857 06:47:01,080 --> 06:47:05,200 coway why probably because someone had 10858 06:47:03,600 --> 06:47:07,840 too much free time once and decided to 10859 06:47:05,200 --> 06:47:10,478 write a program that creates asy art out 10860 06:47:07,840 --> 06:47:13,200 of a cow saying something textually on 10861 06:47:10,478 --> 06:47:15,760 the screen but you use coway Just For 10862 06:47:13,200 --> 06:47:18,840 Fun by way of command line arguments so 10863 06:47:15,760 --> 06:47:22,000 for instance let me propose that uh I go 10864 06:47:18,840 --> 06:47:23,718 back to vs code here not because I want 10865 06:47:22,000 --> 06:47:25,440 to write any code but I just want to use 10866 06:47:23,718 --> 06:47:27,478 my terminal window and let me uh 10867 06:47:25,440 --> 06:47:30,000 maximize my terminal window here and let 10868 06:47:27,478 --> 06:47:34,080 me go ahead and type in something like 10869 06:47:30,000 --> 06:47:35,398 how about c space moo so cow is not a 10870 06:47:34,080 --> 06:47:37,600 program I wrote It's been around for 10871 06:47:35,398 --> 06:47:39,878 decades but we installed it in vs code 10872 06:47:37,600 --> 06:47:41,600 for you in the cloud it takes at least 10873 06:47:39,878 --> 06:47:43,920 one command line argument what do you 10874 06:47:41,600 --> 06:47:46,240 want the cow to say I can say cow say 10875 06:47:43,920 --> 06:47:48,718 moo and hit enter and voila there's my 10876 06:47:46,240 --> 06:47:51,160 asky art of a cow saying moo on the 10877 06:47:48,718 --> 06:47:54,398 screen it can say multiple words so I 10878 06:47:51,160 --> 06:47:56,240 can say hello world and enter and now it 10879 06:47:54,398 --> 06:47:57,958 says hello world so this is just an 10880 06:47:56,240 --> 06:47:59,398 example of a silly program that uses 10881 06:47:57,958 --> 06:48:02,080 command line arguments but it takes 10882 06:47:59,398 --> 06:48:04,320 others too just like clang use this 10883 06:48:02,080 --> 06:48:06,680 convention of hyphens to change the 10884 06:48:04,320 --> 06:48:08,120 output of the program Dash something is 10885 06:48:06,680 --> 06:48:09,558 just a super common convention with 10886 06:48:08,120 --> 06:48:12,280 commandline arguments when you want a 10887 06:48:09,558 --> 06:48:15,520 very tur notation for some option like 10888 06:48:12,280 --> 06:48:18,000 output um in C I read the documentation 10889 06:48:15,520 --> 06:48:19,840 and it turns out there's a-f command 10890 06:48:18,000 --> 06:48:22,760 line argument that allows you to change 10891 06:48:19,840 --> 06:48:28,000 the uh the appearance of the cow if you 10892 06:48:22,760 --> 06:48:30,440 will so if I do coway dasf duck and then 10893 06:48:28,000 --> 06:48:32,600 some other word like quack it's no 10894 06:48:30,440 --> 06:48:35,000 longer a cow that command line argument 10895 06:48:32,600 --> 06:48:37,638 turns it into a tiny adorable duck 10896 06:48:35,000 --> 06:48:38,760 instead and then lastly just for fun 10897 06:48:37,638 --> 06:48:40,760 because I spent way too much time 10898 06:48:38,760 --> 06:48:44,440 playing with command line arguments uh 10899 06:48:40,760 --> 06:48:46,680 c-f dragon and then how about like raar 10900 06:48:44,440 --> 06:48:49,200 enter you can even get this on the 10901 06:48:46,680 --> 06:48:50,760 screen here so this too is just an 10902 06:48:49,200 --> 06:48:52,920 example of what you can do with these 10903 06:48:50,760 --> 06:48:54,798 command line arguments now that we have 10904 06:48:52,920 --> 06:48:56,840 this building block and there's one 10905 06:48:54,798 --> 06:48:58,760 final thing we can now do with code 10906 06:48:56,840 --> 06:49:00,440 there's one last feature today that 10907 06:48:58,760 --> 06:49:03,440 we'll introduce before we now connect 10908 06:49:00,440 --> 06:49:06,680 all of these dots to readability and and 10909 06:49:03,440 --> 06:49:08,840 encryption by talking lastly about 10910 06:49:06,680 --> 06:49:11,520 something called exit status it turns 10911 06:49:08,840 --> 06:49:14,558 out that whenever your main function 10912 06:49:11,520 --> 06:49:16,360 exits it returns a secret integer that 10913 06:49:14,558 --> 06:49:18,638 you can figure out as the programmer or 10914 06:49:16,360 --> 06:49:21,000 an advanced user what it was and these 10915 06:49:18,638 --> 06:49:22,920 exit codes exit statuses are typically 10916 06:49:21,000 --> 06:49:24,080 used to indicate errors so for instance 10917 06:49:22,920 --> 06:49:26,000 over over the past couple of years if 10918 06:49:24,080 --> 06:49:27,760 you us zoom and you ever got some kind 10919 06:49:26,000 --> 06:49:29,638 of error you might have seen a screen 10920 06:49:27,760 --> 06:49:31,680 like this it's usually not that helpful 10921 06:49:29,638 --> 06:49:34,478 maybe tells you to click report problem 10922 06:49:31,680 --> 06:49:37,000 or contact support but very often in our 10923 06:49:34,478 --> 06:49:39,280 human world on Macs PCS and phones you 10924 06:49:37,000 --> 06:49:41,478 see cryptic error codes like literally 10925 06:49:39,280 --> 06:49:43,320 numbers that probably only Zoom knows or 10926 06:49:41,478 --> 06:49:45,478 Microsoft or Google or whatever company 10927 06:49:43,320 --> 06:49:48,200 wrote the software you're using but that 10928 06:49:45,478 --> 06:49:50,680 number corresponds to a specific error 10929 06:49:48,200 --> 06:49:53,280 that some human somewhere knows might 10930 06:49:50,680 --> 06:49:54,840 very well happen these are used similar 10931 06:49:53,280 --> 06:49:57,000 L although under a different name that 10932 06:49:54,840 --> 06:49:59,200 we'll talk about later in the term uh on 10933 06:49:57,000 --> 06:50:02,000 the web as well have you ever seen this 10934 06:49:59,200 --> 06:50:03,600 maybe not character but number so 404 10935 06:50:02,000 --> 06:50:07,558 means 10936 06:50:03,600 --> 06:50:09,320 what so error yes but really not found 10937 06:50:07,558 --> 06:50:10,680 so why I mean this is the most Arcane 10938 06:50:09,320 --> 06:50:12,520 thing and we'll talk in a few weeks 10939 06:50:10,680 --> 06:50:14,360 about like what this and other numbers 10940 06:50:12,520 --> 06:50:15,920 mean but numbers are all around us in 10941 06:50:14,360 --> 06:50:17,280 technology and they very often mean 10942 06:50:15,920 --> 06:50:19,200 something to the technical people who 10943 06:50:17,280 --> 06:50:21,040 wrote the software less so to humans 10944 06:50:19,200 --> 06:50:23,638 like you and me why so many of us 10945 06:50:21,040 --> 06:50:25,120 recognize 404 is kind of weird that like 10946 06:50:23,638 --> 06:50:27,000 that's been around long enough that we 10947 06:50:25,120 --> 06:50:29,080 all know it but it really is just a 10948 06:50:27,000 --> 06:50:31,558 special number that represents an error 10949 06:50:29,080 --> 06:50:33,360 of some sort so it turns out the last 10950 06:50:31,558 --> 06:50:34,840 thing we'll reveal today about what 10951 06:50:33,360 --> 06:50:38,360 we've been taking for granted for two 10952 06:50:34,840 --> 06:50:40,160 weeks is what the int is in Maine we've 10953 06:50:38,360 --> 06:50:42,160 seen just a moment ago that the thing in 10954 06:50:40,160 --> 06:50:43,840 the parentheses which up until now has 10955 06:50:42,160 --> 06:50:47,600 been void which means no command line 10956 06:50:43,840 --> 06:50:49,478 arguments now in Arc string arv brackets 10957 06:50:47,600 --> 06:50:51,680 just means yes command line arguments 10958 06:50:49,478 --> 06:50:53,798 and we've seen how to access them so the 10959 06:50:51,680 --> 06:50:56,200 last piece of the puzzle honestly of all 10960 06:50:53,798 --> 06:50:58,958 the cryptic syntax the past two weeks is 10961 06:50:56,200 --> 06:51:01,080 just what int means int is always there 10962 06:50:58,958 --> 06:51:03,040 for Maine and it indicates that Maine 10963 06:51:01,080 --> 06:51:05,120 will always return an integer even 10964 06:51:03,040 --> 06:51:09,120 though you and I have never done so 10965 06:51:05,120 --> 06:51:11,120 explicitly usually Maine returns Zero by 10966 06:51:09,120 --> 06:51:13,280 default but it would be weird if you saw 10967 06:51:11,120 --> 06:51:14,718 an error message saying zero so zero is 10968 06:51:13,280 --> 06:51:15,798 just hidden you would never see it on 10969 06:51:14,718 --> 06:51:17,878 the screen but it's happening 10970 06:51:15,798 --> 06:51:20,200 automatically by way of how C is 10971 06:51:17,878 --> 06:51:22,478 designed so let me write one final 10972 06:51:20,200 --> 06:51:24,760 program here I'll call it for instance 10973 06:51:22,478 --> 06:51:28,240 status . C to show you these exit 10974 06:51:24,760 --> 06:51:29,920 statuses code of status. c and then up 10975 06:51:28,240 --> 06:51:31,320 here let me do something simple like 10976 06:51:29,920 --> 06:51:34,878 include 10977 06:51:31,320 --> 06:51:38,200 cs50.h then include standard i.h and 10978 06:51:34,878 --> 06:51:40,200 then int main uh let's do actually let's 10979 06:51:38,200 --> 06:51:43,718 use a command line argument in argc 10980 06:51:40,200 --> 06:51:49,320 string argv so that's copy paste but now 10981 06:51:43,718 --> 06:51:50,558 let's do this if argc does not equal to 10982 06:51:49,320 --> 06:51:53,040 why don't we do something like this 10983 06:51:50,558 --> 06:51:54,600 let's not just um default to hello world 10984 06:51:53,040 --> 06:51:56,920 like last time let's yell at the user so 10985 06:51:54,600 --> 06:51:59,478 let's say something like printf missing 10986 06:51:56,920 --> 06:52:01,040 command line argument so that they know 10987 06:51:59,478 --> 06:52:04,320 they screwed up and they need to run the 10988 06:52:01,040 --> 06:52:08,840 program again correctly else let's go 10989 06:52:04,320 --> 06:52:12,520 ahead and say print out uh as before 10990 06:52:08,840 --> 06:52:15,398 hello comma percent s and then plug in 10991 06:52:12,520 --> 06:52:18,000 argv bracket one so the human's name 10992 06:52:15,398 --> 06:52:20,398 from The Prompt now at this point let me 10993 06:52:18,000 --> 06:52:22,600 go ahead and run 10994 06:52:20,398 --> 06:52:25,040 statusstatus and I'll will type nothing 10995 06:52:22,600 --> 06:52:28,200 first first I get yelled at this time 10996 06:52:25,040 --> 06:52:31,120 I'll type it again/ status David and it 10997 06:52:28,200 --> 06:52:34,440 works properly but now let me show you a 10998 06:52:31,120 --> 06:52:36,600 somewhat secret cryptic command you can 10999 06:52:34,440 --> 06:52:38,000 type this at your prompt and it's just a 11000 06:52:36,600 --> 06:52:40,120 coincidence that there's another dollar 11001 06:52:38,000 --> 06:52:42,760 sign Echo dollar sign question mark 11002 06:52:40,120 --> 06:52:44,958 totally Arcane but it allows you to see 11003 06:52:42,760 --> 06:52:47,120 what exit status your program has ended 11004 06:52:44,958 --> 06:52:51,080 with so let me run this again the wrong 11005 06:52:47,120 --> 06:52:53,000 way/ status okay I get the error message 11006 06:52:51,080 --> 06:52:54,760 what was secretly returned I can't can't 11007 06:52:53,000 --> 06:52:56,798 see it there's obviously no error screen 11008 06:52:54,760 --> 06:52:59,280 but by typing Echo dollar sign question 11009 06:52:56,798 --> 06:53:02,440 mark I can see that oh my program 11010 06:52:59,280 --> 06:53:04,798 automatically by default return zero 11011 06:53:02,440 --> 06:53:07,680 however if I run it again correctly 11012 06:53:04,798 --> 06:53:10,478 status David enter this is the correct 11013 06:53:07,680 --> 06:53:12,200 version but if I run Echo question mark 11014 06:53:10,478 --> 06:53:14,760 status again it's still entered with 11015 06:53:12,200 --> 06:53:16,398 zero and long story short this is just a 11016 06:53:14,760 --> 06:53:18,638 missed opportunity when something goes 11017 06:53:16,398 --> 06:53:20,878 wrong why don't I return a value other 11018 06:53:18,638 --> 06:53:22,760 than zero zero by default means success 11019 06:53:20,878 --> 06:53:26,040 and it's always there automatically but 11020 06:53:22,760 --> 06:53:29,398 but you can control this I can go into 11021 06:53:26,040 --> 06:53:32,080 my code here and return one else if 11022 06:53:29,398 --> 06:53:33,878 something works fine I can return zero 11023 06:53:32,080 --> 06:53:36,360 by default and honestly if I omit the 11024 06:53:33,878 --> 06:53:38,040 return zero again zero automatically is 11025 06:53:36,360 --> 06:53:40,000 returned so let me go ahead and though 11026 06:53:38,040 --> 06:53:42,280 be explicit just so I know what's going 11027 06:53:40,000 --> 06:53:44,920 on make status 11028 06:53:42,280 --> 06:53:48,360 again/ status and let's do this 11029 06:53:44,920 --> 06:53:50,760 correctly with David enter hello David 11030 06:53:48,360 --> 06:53:53,200 Echo question mark uh Echo dollar sign 11031 06:53:50,760 --> 06:53:56,080 question mark zero so all is well 11032 06:53:53,200 --> 06:53:59,080 but now if I do/ status and nothing or 11033 06:53:56,080 --> 06:54:01,040 multiple things but not just David enter 11034 06:53:59,080 --> 06:54:03,600 I get the error message but now if I do 11035 06:54:01,040 --> 06:54:06,680 Echo dollar sign question mark waa there 11036 06:54:03,600 --> 06:54:08,958 now is the one so what does this now 11037 06:54:06,680 --> 06:54:10,160 mean this is in the graphical world we 11038 06:54:08,958 --> 06:54:11,320 would just show something like this on 11039 06:54:10,160 --> 06:54:12,798 the screen which is a little more 11040 06:54:11,320 --> 06:54:14,360 informative to the user but even in the 11041 06:54:12,798 --> 06:54:16,080 Linux world we don't have a guey 11042 06:54:14,360 --> 06:54:17,798 necessarily even for the programs we've 11043 06:54:16,080 --> 06:54:19,760 written you can check these exit 11044 06:54:17,798 --> 06:54:21,320 statuses and in fact more comfortable 11045 06:54:19,760 --> 06:54:24,000 more advanced programmers when they 11046 06:54:21,320 --> 06:54:27,320 write code that calls programs be it 11047 06:54:24,000 --> 06:54:29,520 coway or anything else you can encode 11048 06:54:27,320 --> 06:54:31,638 check what the exit status is of a 11049 06:54:29,520 --> 06:54:34,000 program and then decide did my program 11050 06:54:31,638 --> 06:54:36,878 work or did it not and now let's connect 11051 06:54:34,000 --> 06:54:40,240 the final dots uh before we adjourn for 11052 06:54:36,878 --> 06:54:42,200 some fruit snacks uh cryptography namely 11053 06:54:40,240 --> 06:54:44,558 one of the applications this week via 11054 06:54:42,200 --> 06:54:46,440 which you'll be able to send if you will 11055 06:54:44,558 --> 06:54:48,478 secret messages and better yet decre 11056 06:54:46,440 --> 06:54:50,360 secret messages this will be in addition 11057 06:54:48,478 --> 06:54:52,680 to perhaps analyzing the readability of 11058 06:54:50,360 --> 06:54:54,478 text using puristic like we identified 11059 06:54:52,680 --> 06:54:56,398 at the start of class 2 so a 11060 06:54:54,478 --> 06:54:58,920 cryptography is just the art the science 11061 06:54:56,398 --> 06:55:00,760 of encrypting information scrambling 11062 06:54:58,920 --> 06:55:03,760 information so that if you have a secret 11063 06:55:00,760 --> 06:55:05,760 message to send in so-called plain text 11064 06:55:03,760 --> 06:55:07,520 you can run it through some algorithm 11065 06:55:05,760 --> 06:55:10,558 and turn it into what's called Cipher 11066 06:55:07,520 --> 06:55:12,440 text thereby encrypting it and only 11067 06:55:10,558 --> 06:55:14,600 someone who knows what algorithm you've 11068 06:55:12,440 --> 06:55:17,120 used and what input you've used to the 11069 06:55:14,600 --> 06:55:18,718 algorithm theoretically can decrypt that 11070 06:55:17,120 --> 06:55:20,638 process and convert it back to the 11071 06:55:18,718 --> 06:55:22,958 original message so if we use our mental 11072 06:55:20,638 --> 06:55:25,398 model from last week here is a problem 11073 06:55:22,958 --> 06:55:27,200 here is an input and output the goal I 11074 06:55:25,398 --> 06:55:29,200 claim here is to take some plain text 11075 06:55:27,200 --> 06:55:30,638 like the message you want to send think 11076 06:55:29,200 --> 06:55:32,360 back to grade school if you ever passed 11077 06:55:30,638 --> 06:55:34,478 a note to a friend or to your crush 11078 06:55:32,360 --> 06:55:35,680 saying I love you it's a little awkward 11079 06:55:34,478 --> 06:55:37,360 if the teacher or someone else 11080 06:55:35,680 --> 06:55:39,360 intercepts the paper and in English it 11081 06:55:37,360 --> 06:55:40,558 just says I love you or whatever it is 11082 06:55:39,360 --> 06:55:42,920 it'd be nice if you had at least 11083 06:55:40,558 --> 06:55:44,680 encrypted it in some way but the other 11084 06:55:42,920 --> 06:55:46,360 person needs to know what algorithm you 11085 06:55:44,680 --> 06:55:48,798 used and what inputs you used to that 11086 06:55:46,360 --> 06:55:50,840 algorithm so that ultimately they can 11087 06:55:48,798 --> 06:55:53,280 decode the so-called Cipher text which 11088 06:55:50,840 --> 06:55:55,320 is the output so what go inside of the 11089 06:55:53,280 --> 06:55:57,200 box today well an algorithm as it 11090 06:55:55,320 --> 06:55:59,240 relates to cryptography is called a 11091 06:55:57,200 --> 06:56:01,398 cipher and a cipher is a fancy name for 11092 06:55:59,240 --> 06:56:04,718 an algorithm that encrypts text from 11093 06:56:01,398 --> 06:56:06,638 plain text to Cipher text the catch is 11094 06:56:04,718 --> 06:56:09,080 there needs to be not just the algorithm 11095 06:56:06,638 --> 06:56:11,200 there needs to be an input to it and so 11096 06:56:09,080 --> 06:56:12,958 for instance you might draw the picture 11097 06:56:11,200 --> 06:56:14,520 like this for the first time today and 11098 06:56:12,958 --> 06:56:16,200 we've seen this in code you can give 11099 06:56:14,520 --> 06:56:18,440 multiple inputs or arguments to 11100 06:56:16,200 --> 06:56:19,920 functions so in this black box can you 11101 06:56:18,440 --> 06:56:23,040 imagine passing in the message you want 11102 06:56:19,920 --> 06:56:25,200 to send and then some Secret so for 11103 06:56:23,040 --> 06:56:27,320 instance suppose that the simplest thing 11104 06:56:25,200 --> 06:56:29,200 I could think of as a kid was instead of 11105 06:56:27,320 --> 06:56:31,360 sending the letter A why don't I write 11106 06:56:29,200 --> 06:56:33,440 the letter b instead of the letter B why 11107 06:56:31,360 --> 06:56:35,558 don't I write the letter c so I can kind 11108 06:56:33,440 --> 06:56:38,320 of shift the English alphabet by one 11109 06:56:35,558 --> 06:56:41,040 space so a becomes b b becomes C dot dot 11110 06:56:38,320 --> 06:56:42,600 dot Z becomes a you can wrap around at 11111 06:56:41,040 --> 06:56:44,760 the end and let's assume no punctuation 11112 06:56:42,600 --> 06:56:48,280 in this part of the story so that's a 11113 06:56:44,760 --> 06:56:51,120 very simple algorithm add a value to 11114 06:56:48,280 --> 06:56:53,440 each letter and send the value as the 11115 06:56:51,120 --> 06:56:55,320 cipher text and now the teacher the 11116 06:56:53,440 --> 06:56:57,718 classmate they have to know that you use 11117 06:56:55,320 --> 06:56:59,840 not only this rotational algorithm also 11118 06:56:57,718 --> 06:57:01,520 known as a Caesar Cipher they also need 11119 06:56:59,840 --> 06:57:03,520 to know what number you use did you add 11120 06:57:01,520 --> 06:57:06,120 one to every letter two to every letter 11121 06:57:03,520 --> 06:57:08,240 25 to every letter now if they're super 11122 06:57:06,120 --> 06:57:10,280 smart and probably not the the young age 11123 06:57:08,240 --> 06:57:11,798 in this story they could also just try 11124 06:57:10,280 --> 06:57:13,360 all possibilities and that would be an 11125 06:57:11,798 --> 06:57:15,240 attack on the algorithm this is not a 11126 06:57:13,360 --> 06:57:17,398 sophisticated algorithm but it's enough 11127 06:57:15,240 --> 06:57:19,600 to send a message in class so if the two 11128 06:57:17,398 --> 06:57:23,398 inputs now are high as the plain text 11129 06:57:19,600 --> 06:57:25,040 message and one as the so-called key the 11130 06:57:23,398 --> 06:57:28,000 secret number that only you and the 11131 06:57:25,040 --> 06:57:30,478 other person know you might uh be able 11132 06:57:28,000 --> 06:57:32,600 to encrypt a message from one way to the 11133 06:57:30,478 --> 06:57:36,360 other and so in this case for instance 11134 06:57:32,600 --> 06:57:37,680 high would become i j exclamation point 11135 06:57:36,360 --> 06:57:39,040 in this version of the algorithm we're 11136 06:57:37,680 --> 06:57:40,878 not going to bother with numbers or 11137 06:57:39,040 --> 06:57:43,080 punctuation will only operate on a 11138 06:57:40,878 --> 06:57:45,320 through z be it uppercase or lowercase 11139 06:57:43,080 --> 06:57:48,320 so now if you were to receive a a slip 11140 06:57:45,320 --> 06:57:50,520 of paper in class with i j on it you 11141 06:57:48,320 --> 06:57:51,958 know you the recipient would know what 11142 06:57:50,520 --> 06:57:54,160 it is so long as you know that the 11143 06:57:51,958 --> 06:57:55,718 sender used one because you just reverse 11144 06:57:54,160 --> 06:57:57,638 the algorithm and you subtract one 11145 06:57:55,718 --> 06:57:58,958 instead the teacher you know they 11146 06:57:57,638 --> 06:58:00,160 probably don't know what this means and 11147 06:57:58,958 --> 06:58:02,360 they're not going to spend time hacking 11148 06:58:00,160 --> 06:58:03,718 the message so it just looks scrambled 11149 06:58:02,360 --> 06:58:05,760 to them and that's what we get from 11150 06:58:03,718 --> 06:58:07,398 encryption someone who intercepts it be 11151 06:58:05,760 --> 06:58:09,638 it in class or in the real world on the 11152 06:58:07,398 --> 06:58:11,840 Internet or anywhere else can't actually 11153 06:58:09,638 --> 06:58:14,040 figure out ideally what it is you have 11154 06:58:11,840 --> 06:58:15,798 sent the opposite of course is indeed 11155 06:58:14,040 --> 06:58:18,920 called decryption but the process is the 11156 06:58:15,798 --> 06:58:21,120 same we now pass in negative one and so 11157 06:58:18,920 --> 06:58:22,920 how about this why don't we end with a 11158 06:58:21,120 --> 06:58:25,878 demonstration here 11159 06:58:22,920 --> 06:58:28,478 u y JT xbt 11160 06:58:25,878 --> 06:58:31,160 dt50 there's a bit of a tell there if we 11161 06:58:28,478 --> 06:58:34,120 pass that in and do Nega -1 well how do 11162 06:58:31,160 --> 06:58:35,920 we get out the plain text originally 11163 06:58:34,120 --> 06:58:39,320 well if this is the cipher text and we 11164 06:58:35,920 --> 06:58:44,478 subtract one from each letter I think U 11165 06:58:39,320 --> 06:58:49,840 becomes t i becomes h j becomes i t 11166 06:58:44,478 --> 06:58:55,398 becomes s x becomes w b becomes a t 11167 06:58:49,840 --> 06:58:58,160 becomes s d becomes c t becomes s and 11168 06:58:55,398 --> 06:59:00,070 this was indeed cs50 have a duck on your 11169 06:58:58,160 --> 06:59:03,150 way out and some snacks in the 11170 06:59:00,070 --> 06:59:11,269 [Applause] 11171 06:59:03,150 --> 06:59:11,269 [Music] 11172 06:59:21,240 --> 06:59:25,570 lobby 11173 06:59:22,350 --> 06:59:25,570 [Music] 11174 06:59:41,860 --> 06:59:54,840 [Music] 11175 07:00:05,020 --> 07:00:08,080 [Music] 11176 07:00:14,360 --> 07:00:17,470 [Music] 11177 07:00:21,478 --> 07:00:24,120 oh 11178 07:00:24,290 --> 07:00:37,650 [Music] 11179 07:00:51,600 --> 07:00:57,040 all right all right this is cs50 and 11180 07:00:54,398 --> 07:00:58,840 this is week three already wherein we'll 11181 07:00:57,040 --> 07:01:00,958 take a look back actually at week zero 11182 07:00:58,840 --> 07:01:02,320 where we first began and in week zero 11183 07:01:00,958 --> 07:01:04,398 recall that everything was very 11184 07:01:02,320 --> 07:01:06,040 intuitive in a sense we talked not just 11185 07:01:04,398 --> 07:01:08,080 about representation of information but 11186 07:01:06,040 --> 07:01:09,520 algorithms and we talked about tearing a 11187 07:01:08,080 --> 07:01:11,718 phone book again and again and that 11188 07:01:09,520 --> 07:01:13,878 somehow got us to a better solution but 11189 07:01:11,718 --> 07:01:15,398 today we'll try to start formalizing 11190 07:01:13,878 --> 07:01:17,080 some of those ideas and capturing some 11191 07:01:15,398 --> 07:01:20,520 of those same ideas not in pseudo code 11192 07:01:17,080 --> 07:01:22,240 just yet but in uh actual code as well 11193 07:01:20,520 --> 07:01:24,398 but we'll also consider the the 11194 07:01:22,240 --> 07:01:26,280 efficiency of those algorithms like just 11195 07:01:24,398 --> 07:01:27,478 how good how welld designed our 11196 07:01:26,280 --> 07:01:28,840 algorithms actually are and if you 11197 07:01:27,478 --> 07:01:30,878 recall when we did the phone book 11198 07:01:28,840 --> 07:01:32,760 example where in I first had an 11199 07:01:30,878 --> 07:01:34,680 algorithm searching one page at a time 11200 07:01:32,760 --> 07:01:36,240 and then second when two pages at a time 11201 07:01:34,680 --> 07:01:38,600 and then third started tearing the thing 11202 07:01:36,240 --> 07:01:40,718 in half recall that we with a wave of 11203 07:01:38,600 --> 07:01:42,958 the hand kind of analyzed it as follows 11204 07:01:40,718 --> 07:01:44,638 we proposed that if the x-axis here is 11205 07:01:42,958 --> 07:01:47,000 the size of the problem like number of 11206 07:01:44,638 --> 07:01:49,040 pages in a phone book and the y- axis is 11207 07:01:47,000 --> 07:01:51,120 the time required to solve the problem 11208 07:01:49,040 --> 07:01:52,840 in seconds minutes page tears whatever 11209 07:01:51,120 --> 07:01:54,320 your unit of measuring is recall that 11210 07:01:52,840 --> 07:01:56,478 the first algorithm was this straight 11211 07:01:54,320 --> 07:01:58,558 line such that if you had n pages in the 11212 07:01:56,478 --> 07:02:00,280 phone book it might have this slope of N 11213 07:01:58,558 --> 07:02:03,398 and there's this one toone relationship 11214 07:02:00,280 --> 07:02:05,280 between pages and tears two pages at a 11215 07:02:03,398 --> 07:02:07,360 time of course was twice as fast but 11216 07:02:05,280 --> 07:02:09,398 still really the same shape the yellow 11217 07:02:07,360 --> 07:02:11,520 line here indicating that yeah it's n / 11218 07:02:09,398 --> 07:02:12,878 two maybe plus one if you have to double 11219 07:02:11,520 --> 07:02:15,360 back as we discussed but it's really 11220 07:02:12,878 --> 07:02:17,840 still fundamentally the same algorithm 11221 07:02:15,360 --> 07:02:20,200 one or two pages at a time but the third 11222 07:02:17,840 --> 07:02:22,398 algorithm recall was this one here in 11223 07:02:20,200 --> 07:02:24,680 green where we called it logarithmic in 11224 07:02:22,398 --> 07:02:27,320 terms of how fast or how slow it was and 11225 07:02:24,680 --> 07:02:28,600 indeed the implication of this algorithm 11226 07:02:27,320 --> 07:02:31,040 was that we could even double the size 11227 07:02:28,600 --> 07:02:32,680 of the phone book and no big deal one 11228 07:02:31,040 --> 07:02:34,280 additional page tear and we take yet 11229 07:02:32,680 --> 07:02:36,160 another thousand page bite out of the 11230 07:02:34,280 --> 07:02:37,920 phone book so today we'll revisit some 11231 07:02:36,160 --> 07:02:39,558 of these ideas formalize them a bit but 11232 07:02:37,920 --> 07:02:42,160 also translate some of them ultimately 11233 07:02:39,558 --> 07:02:43,958 to code and all of that now is possible 11234 07:02:42,160 --> 07:02:44,958 because we have this lower level 11235 07:02:43,958 --> 07:02:46,718 understanding perhaps of like what's 11236 07:02:44,958 --> 07:02:49,160 actually inside of your computer this of 11237 07:02:46,718 --> 07:02:50,680 course is your computer's Ram or memory 11238 07:02:49,160 --> 07:02:51,920 and recall that if we kind of start to 11239 07:02:50,680 --> 07:02:53,718 abstract this away your compter 11240 07:02:51,920 --> 07:02:55,080 computer's memory is really just a grid 11241 07:02:53,718 --> 07:02:57,280 of bites in fact we don't have to look 11242 07:02:55,080 --> 07:02:59,398 at the hardware anymore and we looked at 11243 07:02:57,280 --> 07:03:01,718 a grid of bites like this whereby each 11244 07:02:59,398 --> 07:03:04,958 of these bites could be used to store a 11245 07:03:01,718 --> 07:03:07,520 Char an INT a long or even an entire 11246 07:03:04,958 --> 07:03:09,638 string at that but let's focus perhaps 11247 07:03:07,520 --> 07:03:11,160 just on a subset of this because last 11248 07:03:09,638 --> 07:03:13,958 week of course we emphasized really 11249 07:03:11,160 --> 07:03:16,120 arrays storing things in arrays and that 11250 07:03:13,958 --> 07:03:18,240 allowed us to start storing entire 11251 07:03:16,120 --> 07:03:20,000 strings sequences of characters and even 11252 07:03:18,240 --> 07:03:22,478 arrays of integers if we want to have 11253 07:03:20,000 --> 07:03:24,638 multiple ones and not just multiple 11254 07:03:22,478 --> 07:03:26,760 variables as well but the catch is that 11255 07:03:24,638 --> 07:03:28,200 if you look inside of an array in the 11256 07:03:26,760 --> 07:03:30,398 computer's memory and for instance 11257 07:03:28,200 --> 07:03:32,360 suppose these integers here are stored 11258 07:03:30,398 --> 07:03:34,320 it's pretty easy for us humans to glance 11259 07:03:32,360 --> 07:03:36,320 at this and immediately find the number 11260 07:03:34,320 --> 07:03:37,958 50 you sort of have this bird's eye view 11261 07:03:36,320 --> 07:03:39,558 from where you're seated of everything 11262 07:03:37,958 --> 07:03:41,840 on the screen and so it's pretty obvious 11263 07:03:39,558 --> 07:03:44,000 how you get to the number 50 but in the 11264 07:03:41,840 --> 07:03:46,120 world of computers of course it turns 11265 07:03:44,000 --> 07:03:48,320 out that this is hardware and computers 11266 07:03:46,120 --> 07:03:50,320 for today's purposes can only do one 11267 07:03:48,320 --> 07:03:52,920 thing at a time they can't just take it 11268 07:03:50,320 --> 07:03:55,280 all in and find instantly some number 11269 07:03:52,920 --> 07:03:57,958 like 50 so perhaps a decent metaphor is 11270 07:03:55,280 --> 07:04:00,040 to consider the array of memory inside 11271 07:03:57,958 --> 07:04:02,360 of your computer really is a sequence of 11272 07:04:00,040 --> 07:04:05,000 of closed doors and if the computer 11273 07:04:02,360 --> 07:04:06,638 wants to find some value in an array it 11274 07:04:05,000 --> 07:04:09,080 has to do the digital equivalent of 11275 07:04:06,638 --> 07:04:11,520 opening each of these doors one at a 11276 07:04:09,080 --> 07:04:13,840 time now how can code do that well of 11277 07:04:11,520 --> 07:04:16,240 course we introduced indices or indexes 11278 07:04:13,840 --> 07:04:18,160 last week whereby we by convention call 11279 07:04:16,240 --> 07:04:20,680 the first element of an array location 11280 07:04:18,160 --> 07:04:23,320 zero the second location one the Third 11281 07:04:20,680 --> 07:04:25,280 location two and so forth so-called zero 11282 07:04:23,320 --> 07:04:26,958 indexed and this allowed us to now 11283 07:04:25,280 --> 07:04:28,398 bridge this conceptual world of like 11284 07:04:26,958 --> 07:04:30,000 what's going on in memory with actual 11285 07:04:28,398 --> 07:04:31,958 code because now we had this square 11286 07:04:30,000 --> 07:04:34,798 bracket syntax via which we could go 11287 07:04:31,958 --> 07:04:37,280 searching for something if we so choose 11288 07:04:34,798 --> 07:04:39,520 and it turns out if I now uh paint these 11289 07:04:37,280 --> 07:04:41,000 red instead of yellow it would seem that 11290 07:04:39,520 --> 07:04:43,160 we actually have a pretty good physical 11291 07:04:41,000 --> 07:04:46,120 metaphor here standing in place for what 11292 07:04:43,160 --> 07:04:47,600 would be a computer's uh array of memory 11293 07:04:46,120 --> 07:04:49,920 if for instance you're storing some 11294 07:04:47,600 --> 07:04:51,920 seven numbers like that and so today we 11295 07:04:49,920 --> 07:04:53,718 begin with a look of a specific type typ 11296 07:04:51,920 --> 07:04:55,240 of algorithm that is for searching like 11297 07:04:53,718 --> 07:04:57,398 searching is all over the place all of 11298 07:04:55,240 --> 07:04:59,080 us have probably gone to google.com or 11299 07:04:57,398 --> 07:05:01,160 some equivalent like already multiple 11300 07:04:59,080 --> 07:05:03,080 times per day and getting back answers 11301 07:05:01,160 --> 07:05:05,320 fast is what companies like Google are 11302 07:05:03,080 --> 07:05:07,160 really good at so how are they doing 11303 07:05:05,320 --> 07:05:09,680 that how are they storing information in 11304 07:05:07,160 --> 07:05:11,160 computers uh memory well let's consider 11305 07:05:09,680 --> 07:05:13,680 what this really is it's really just a 11306 07:05:11,160 --> 07:05:15,240 problem as it was back in week zero the 11307 07:05:13,680 --> 07:05:17,520 input though to the problem for now 11308 07:05:15,240 --> 07:05:18,920 might be this array of seven lockers so 11309 07:05:17,520 --> 07:05:20,398 that's the input to the problem inside 11310 07:05:18,920 --> 07:05:22,718 of which is a number and maybe for 11311 07:05:20,398 --> 07:05:25,798 Simplicity now we just want a yes no a 11312 07:05:22,718 --> 07:05:28,760 true false answer a bull that is to say 11313 07:05:25,798 --> 07:05:31,120 of whether or not some number like 50 is 11314 07:05:28,760 --> 07:05:33,440 in that array it's not quite as fancy as 11315 07:05:31,120 --> 07:05:35,000 Google that doesn't just tell you yes we 11316 07:05:33,440 --> 07:05:36,680 have search results it actually gives 11317 07:05:35,000 --> 07:05:38,600 you the search results but for now we'll 11318 07:05:36,680 --> 07:05:41,040 keep it simple and just output as part 11319 07:05:38,600 --> 07:05:42,600 of this problem yes or no true or false 11320 07:05:41,040 --> 07:05:45,200 we have found the number we're looking 11321 07:05:42,600 --> 07:05:47,520 for given an input like that array but 11322 07:05:45,200 --> 07:05:49,240 it turns out inside of this black box 11323 07:05:47,520 --> 07:05:51,798 that we keep coming back to there's all 11324 07:05:49,240 --> 07:05:53,160 sorts of possible algorithms and we talk 11325 07:05:51,798 --> 07:05:54,920 about this at a high level conceptually 11326 07:05:53,160 --> 07:05:57,200 in week zero with the phone book but 11327 07:05:54,920 --> 07:05:59,280 today let's consider it a little more 11328 07:05:57,200 --> 07:06:00,760 concretely uh by way of a game that some 11329 07:05:59,280 --> 07:06:02,798 of you might have grown up with namely 11330 07:06:00,760 --> 07:06:04,000 Monopoly and so behind these doors it 11331 07:06:02,798 --> 07:06:06,120 turns out we'll be hidden some 11332 07:06:04,000 --> 07:06:07,760 denominations of Monopoly money but for 11333 07:06:06,120 --> 07:06:12,000 this we now have two volunteers if you'd 11334 07:06:07,760 --> 07:06:16,000 like to greet the world hi I'm 11335 07:06:12,000 --> 07:06:16,958 Jackson yay hi my name is Stephanie 11336 07:06:16,000 --> 07:06:19,520 Steph and you want to say a little 11337 07:06:16,958 --> 07:06:22,280 something about yourselves years house 11338 07:06:19,520 --> 07:06:24,718 first year living in Matthews 11339 07:06:22,280 --> 07:06:26,798 nice and I'm a first year in Canada nice 11340 07:06:24,718 --> 07:06:29,680 well Welcome to our two 11341 07:06:26,798 --> 07:06:31,120 volunteers so why don't we do this would 11342 07:06:29,680 --> 07:06:32,440 one of you like to volunteer the other 11343 07:06:31,120 --> 07:06:36,080 to go 11344 07:06:32,440 --> 07:06:38,080 first all go first okay all right so 11345 07:06:36,080 --> 07:06:40,080 Stephanie's up first and behind one of 11346 07:06:38,080 --> 07:06:42,000 these doors here we've hidden the 11347 07:06:40,080 --> 07:06:44,200 Monopoly money 50 and so we'd like you 11348 07:06:42,000 --> 07:06:45,920 to find the 50 we'll tell you nothing 11349 07:06:44,200 --> 07:06:47,760 more about the lockers but we would like 11350 07:06:45,920 --> 07:06:48,680 you to execute a certain algorithm and 11351 07:06:47,760 --> 07:06:50,320 in fact I'm going to give you some 11352 07:06:48,680 --> 07:06:51,718 pseudo code for this and I'm going to 11353 07:06:50,320 --> 07:06:54,320 give you the name for it it's called 11354 07:06:51,718 --> 07:06:55,478 linear search and as the name implies 11355 07:06:54,320 --> 07:06:57,000 you're pretty much going to end up 11356 07:06:55,478 --> 07:06:58,398 walking in sort of a straight line but 11357 07:06:57,000 --> 07:07:00,360 how are you going to do this well let me 11358 07:06:58,398 --> 07:07:02,398 propose that in a moment your first step 11359 07:07:00,360 --> 07:07:04,798 will be to think kind of like a loop for 11360 07:07:02,398 --> 07:07:06,760 each door from left to right what do we 11361 07:07:04,798 --> 07:07:10,440 want you to do on each iteration well if 11362 07:07:06,760 --> 07:07:12,000 50 is behind that door then we want to 11363 07:07:10,440 --> 07:07:13,760 go ahead and have you return true and 11364 07:07:12,000 --> 07:07:16,160 sort of hold up the 50 proudly if you 11365 07:07:13,760 --> 07:07:18,040 will for the group otherwise if you get 11366 07:07:16,160 --> 07:07:19,760 through that whole Loop and you haven't 11367 07:07:18,040 --> 07:07:22,000 found the number 50 you can just throw 11368 07:07:19,760 --> 07:07:24,638 up your hands and disappointment false 11369 07:07:22,000 --> 07:07:26,878 you've not found the number 50 so to be 11370 07:07:24,638 --> 07:07:29,718 clear step one is going to be for each 11371 07:07:26,878 --> 07:07:32,000 uh door from left to right how would you 11372 07:07:29,718 --> 07:07:32,000 like to 11373 07:07:33,600 --> 07:07:40,000 begin yep oh and then yep there we go 11374 07:07:37,280 --> 07:07:43,320 yep oh and if you'd like to at least 11375 07:07:40,000 --> 07:07:47,558 tell oh good good acting here what have 11376 07:07:43,320 --> 07:07:49,440 you found instead it's not 50 but 20 oh 11377 07:07:47,558 --> 07:07:51,958 okay so step one was a fail so let's 11378 07:07:49,440 --> 07:07:53,638 move on to step two inside of the Loop 11379 07:07:51,958 --> 07:07:57,440 what are you going to do next I'm going 11380 07:07:53,638 --> 07:07:57,440 to move to the next door 11381 07:07:58,718 --> 07:08:07,840 okay almost okay almost sort of a 500 11382 07:08:03,360 --> 07:08:07,840 instead next Locker I would rather 11383 07:08:08,440 --> 07:08:11,440 take 11384 07:08:11,718 --> 07:08:18,920 no okay we're not telling the 11385 07:08:15,280 --> 07:08:20,398 audience oh okay so keep going this is 11386 07:08:18,920 --> 07:08:23,398 step three 11387 07:08:20,398 --> 07:08:23,398 now 11388 07:08:24,440 --> 07:08:27,840 oh 11389 07:08:25,170 --> 07:08:33,160 [Music] 11390 07:08:27,840 --> 07:08:33,160 man five okay few more lockers to 11391 07:08:35,478 --> 07:08:39,280 check a little sad 11392 07:08:40,958 --> 07:08:44,920 guys all right second to last 11393 07:08:45,600 --> 07:08:52,600 step this one kind of close all right 11394 07:08:50,080 --> 07:08:57,638 and finally the last clearly you've been 11395 07:08:52,600 --> 07:09:00,440 perhaps set up here let's go all right 11396 07:08:57,638 --> 07:09:00,440 so the number 11397 07:09:01,398 --> 07:09:05,840 50 and Stephanie if I may let me ask you 11398 07:09:03,920 --> 07:09:08,040 a question here so on the screen this is 11399 07:09:05,840 --> 07:09:10,440 the pseudo code you just executed 11400 07:09:08,040 --> 07:09:12,040 suppose though I had done what many of 11401 07:09:10,440 --> 07:09:13,920 us have gotten into the habit of doing 11402 07:09:12,040 --> 07:09:16,280 when you have a if condition you often 11403 07:09:13,920 --> 07:09:18,280 have an else Branch as well suppose that 11404 07:09:16,280 --> 07:09:20,840 I had done this now and I'm marking it 11405 07:09:18,280 --> 07:09:22,680 in red to be clear this is wrong but 11406 07:09:20,840 --> 07:09:25,080 what would have have been bad about this 11407 07:09:22,680 --> 07:09:28,320 code using an if and an else might you 11408 07:09:25,080 --> 07:09:28,320 say any 11409 07:09:31,600 --> 07:09:37,200 instincts 11410 07:09:33,558 --> 07:09:38,638 um then you would end up like cancelling 11411 07:09:37,200 --> 07:09:41,040 canceling the code before you found the 11412 07:09:38,638 --> 07:09:42,200 50 yeah exactly just be eternally sad 11413 07:09:41,040 --> 07:09:44,520 indeed when Stephanie had opened the 11414 07:09:42,200 --> 07:09:46,240 first Locker she had found 20 20 of 11415 07:09:44,520 --> 07:09:47,958 course is not 50 she would have decreed 11416 07:09:46,240 --> 07:09:49,680 false but of course she hadn't checked 11417 07:09:47,958 --> 07:09:51,398 all of the rest of the locker so that 11418 07:09:49,680 --> 07:09:53,478 would seem to be a key detail that would 11419 07:09:51,398 --> 07:09:55,558 this implementation of the SoDo coat we 11420 07:09:53,478 --> 07:09:57,798 actually do go through as we did and 11421 07:09:55,558 --> 07:10:00,320 only return false not even with an else 11422 07:09:57,798 --> 07:10:02,240 but just at the end of the loop such 11423 07:10:00,320 --> 07:10:04,680 that we only reach that line if we don't 11424 07:10:02,240 --> 07:10:05,680 return truer uh earlier than that well 11425 07:10:04,680 --> 07:10:06,760 let's go ahead and do this let me take 11426 07:10:05,680 --> 07:10:08,280 the mic from you if you'd like to take a 11427 07:10:06,760 --> 07:10:09,680 seat next to Jackson Jackson in just a 11428 07:10:08,280 --> 07:10:12,040 moment we'll have you come up Carter if 11429 07:10:09,680 --> 07:10:13,958 you don't mind reorganizing the uh 11430 07:10:12,040 --> 07:10:15,920 lockers for us but in the meantime let 11431 07:10:13,958 --> 07:10:17,718 me point out how we might now translate 11432 07:10:15,920 --> 07:10:19,520 that same idea to code pretty high level 11433 07:10:17,718 --> 07:10:21,558 pretty English oriented with that pseudo 11434 07:10:19,520 --> 07:10:24,240 code but really now as of last week we 11435 07:10:21,558 --> 07:10:26,320 have syntax via which Stephanie and soon 11436 07:10:24,240 --> 07:10:28,600 Jackson could treat this Locker the set 11437 07:10:26,320 --> 07:10:30,798 of lockers as really indeed an array 11438 07:10:28,600 --> 07:10:32,558 using bracket notation so we can now get 11439 07:10:30,798 --> 07:10:34,478 a little closer in our pseudo code to 11440 07:10:32,558 --> 07:10:36,520 actual code and the way a computer 11441 07:10:34,478 --> 07:10:38,760 scientist for instance would translate 11442 07:10:36,520 --> 07:10:40,398 fairly high level English pseudo code 11443 07:10:38,760 --> 07:10:42,718 like this to something that's a little 11444 07:10:40,398 --> 07:10:44,478 closer to C or any language that 11445 07:10:42,718 --> 07:10:45,958 supports arrays would be a little more 11446 07:10:44,478 --> 07:10:48,000 cryptically like this but you'll see 11447 07:10:45,958 --> 07:10:51,240 more of this syntax in the coming days 11448 07:10:48,000 --> 07:10:52,718 for I from 0 to n minus one this is 11449 07:10:51,240 --> 07:10:54,520 still pseudo code but that's kind of 11450 07:10:52,718 --> 07:10:56,120 like the english-like way of expressing 11451 07:10:54,520 --> 07:10:59,680 what we've known come to know as a for 11452 07:10:56,120 --> 07:11:01,080 Loop if 50 is behind doors bracket I so 11453 07:10:59,680 --> 07:11:03,000 I'm assuming for the sake of discussion 11454 07:11:01,080 --> 07:11:05,638 that doors now is the name of my 11455 07:11:03,000 --> 07:11:07,120 variable this array of seven doors but 11456 07:11:05,638 --> 07:11:09,200 then the rest of the logic the rest of 11457 07:11:07,120 --> 07:11:10,638 the pseudo code really is the same way 11458 07:11:09,200 --> 07:11:12,120 and so you'll find in time that 11459 07:11:10,638 --> 07:11:13,760 programmers computer scientists more 11460 07:11:12,120 --> 07:11:16,558 generally when you start expressing 11461 07:11:13,760 --> 07:11:19,000 ideas algorithms to someone else instead 11462 07:11:16,558 --> 07:11:21,000 of maybe operating at this level here 11463 07:11:19,000 --> 07:11:22,718 you now have in your vocabulary really a 11464 07:11:21,000 --> 07:11:24,520 new new syntax that you can be a little 11465 07:11:22,718 --> 07:11:26,558 more specific not getting so into the 11466 07:11:24,520 --> 07:11:28,240 weeds of writing actual C code but at 11467 07:11:26,558 --> 07:11:30,280 least now doing something that's a 11468 07:11:28,240 --> 07:11:32,240 little closer to manipulating an array 11469 07:11:30,280 --> 07:11:36,520 like this so Jackson would you like to 11470 07:11:32,240 --> 07:11:38,798 uh stand on up all right yes yes support 11471 07:11:36,520 --> 07:11:41,840 for Jackson here too 11472 07:11:38,798 --> 07:11:43,120 nice and here now I'm going to allow you 11473 07:11:41,840 --> 07:11:44,958 an assumption that Stephanie did not 11474 07:11:43,120 --> 07:11:46,600 have Stephanie clearly was really doing 11475 07:11:44,958 --> 07:11:49,120 her best searching from left to right 11476 07:11:46,600 --> 07:11:50,478 using linear searches we'll Now call it 11477 07:11:49,120 --> 07:11:52,080 but they were pretty much in random 11478 07:11:50,478 --> 07:11:53,920 order right there was a 20 over there 11479 07:11:52,080 --> 07:11:56,040 there was a one over there and then a 50 11480 07:11:53,920 --> 07:11:58,080 so we deliberately jumbled things up and 11481 07:11:56,040 --> 07:12:00,080 did not sort the numbers for her but 11482 07:11:58,080 --> 07:12:02,200 Carter kindly has just come up to give 11483 07:12:00,080 --> 07:12:04,478 you a leg up Jackson by sorting the 11484 07:12:02,200 --> 07:12:07,120 numbers in advance and we'd like you 11485 07:12:04,478 --> 07:12:08,840 this time much like in week zero to do 11486 07:12:07,120 --> 07:12:11,080 something again and again but this time 11487 07:12:08,840 --> 07:12:12,718 using what we'll Now call binary search 11488 07:12:11,080 --> 07:12:14,638 it's exactly the same algorithm 11489 07:12:12,718 --> 07:12:16,478 conceptually as we did in week zero but 11490 07:12:14,638 --> 07:12:18,638 if we translate to the context of this 11491 07:12:16,478 --> 07:12:20,280 array we might now might say something 11492 07:12:18,638 --> 07:12:22,320 like this the first step for Jackson 11493 07:12:20,280 --> 07:12:24,280 might be to ask ask the question if 50 11494 07:12:22,320 --> 07:12:25,958 is behind the middle door where 11495 07:12:24,280 --> 07:12:28,080 presumably he's done some mental math to 11496 07:12:25,958 --> 07:12:29,878 figure out what the middle is then he's 11497 07:12:28,080 --> 07:12:31,478 going to just return true and hopefully 11498 07:12:29,878 --> 07:12:34,200 we'll get lucky and 50 will be right 11499 07:12:31,478 --> 07:12:36,120 there of course there's two other 11500 07:12:34,200 --> 07:12:39,320 possibilities at least which would be 11501 07:12:36,120 --> 07:12:41,680 what 50 is with respect to these 11502 07:12:39,320 --> 07:12:44,398 doors yeah so to the left or to the 11503 07:12:41,680 --> 07:12:46,558 right alternatively so if 50 is less 11504 07:12:44,398 --> 07:12:48,440 than the middle door then presumably 11505 07:12:46,558 --> 07:12:50,200 Jackson's going to want to go left else 11506 07:12:48,440 --> 07:12:52,000 if 50 is greater than the middle door 11507 07:12:50,200 --> 07:12:54,398 he's going to want to go right much like 11508 07:12:52,000 --> 07:12:56,240 I did uh physically last week with the 11509 07:12:54,398 --> 07:12:58,360 phone book dividing and conquering left 11510 07:12:56,240 --> 07:13:00,600 to right but there's actually a a fourth 11511 07:12:58,360 --> 07:13:02,680 case let's put it on the board first 11512 07:13:00,600 --> 07:13:03,638 what else might happen here that Jackson 11513 07:13:02,680 --> 07:13:07,120 should 11514 07:13:03,638 --> 07:13:09,120 consider yeah oh it's not there it's not 11515 07:13:07,120 --> 07:13:11,160 there so let me actually go back and 11516 07:13:09,120 --> 07:13:12,718 amend my my pseudo code here and just 11517 07:13:11,160 --> 07:13:15,398 say Jackson if we don't hand you any 11518 07:13:12,718 --> 07:13:16,920 doors at all or eventually as he's 11519 07:13:15,398 --> 07:13:18,718 dividing and conquering if he's left 11520 07:13:16,920 --> 07:13:20,558 with no more doors we have to handle 11521 07:13:18,718 --> 07:13:22,398 that situation so that the def behavior 11522 07:13:20,558 --> 07:13:23,680 is defined find all right so with that 11523 07:13:22,398 --> 07:13:25,120 said Jackson do you want to go ahead and 11524 07:13:23,680 --> 07:13:26,638 find us the number 50 and walk us 11525 07:13:25,120 --> 07:13:29,718 through verbally what you're doing and 11526 07:13:26,638 --> 07:13:32,240 finding all right so it looks like this 11527 07:13:29,718 --> 07:13:33,120 one is the middle door so I'm going to 11528 07:13:32,240 --> 07:13:38,920 open 11529 07:13:33,120 --> 07:13:40,718 it but it's 20 not 50 oh sad okay what's 11530 07:13:38,920 --> 07:13:43,878 going through your head now so now I'm 11531 07:13:40,718 --> 07:13:46,520 looking because 50 is higher than 20 I 11532 07:13:43,878 --> 07:13:48,200 want to look to the right good um and 11533 07:13:46,520 --> 07:13:53,680 look for the new middle door which would 11534 07:13:48,200 --> 07:13:56,600 be here nice and it's 100 sad um but 50 11535 07:13:53,680 --> 07:14:00,878 is less than 100 so now we know to look 11536 07:13:56,600 --> 07:14:03,718 left which would be here and Tada nice 11537 07:14:00,878 --> 07:14:05,440 very well done this time around 11538 07:14:03,718 --> 07:14:08,240 too 11539 07:14:05,440 --> 07:14:10,120 so thank you first to our volunteers 11540 07:14:08,240 --> 07:14:12,040 here and in fact um since you're fan of 11541 07:14:10,120 --> 07:14:14,200 Monopoly as we're so informed we have 11542 07:14:12,040 --> 07:14:17,440 the Cambridge edition of Monopoly with 11543 07:14:14,200 --> 07:14:19,120 all your Harvard favorites here you go 11544 07:14:17,440 --> 07:14:22,878 thank you so thank you to our volunteers 11545 07:14:19,120 --> 07:14:22,878 for finding us 50 11546 07:14:23,080 --> 07:14:28,160 so kind of was more popular than we 11547 07:14:25,280 --> 07:14:29,680 expected so here we can translate this 11548 07:14:28,160 --> 07:14:32,160 one more time into something a little 11549 07:14:29,680 --> 07:14:34,280 closer to code and again still pseudo 11550 07:14:32,160 --> 07:14:36,120 code but here now might be another 11551 07:14:34,280 --> 07:14:37,718 formulation of exactly what Jackson just 11552 07:14:36,120 --> 07:14:39,080 did just using the nomenclature now of 11553 07:14:37,718 --> 07:14:40,958 arrays where you can be a little more 11554 07:14:39,080 --> 07:14:42,398 precise with your instructions and still 11555 07:14:40,958 --> 07:14:44,280 leave it to someone else to translate 11556 07:14:42,398 --> 07:14:45,600 this finally to code but here we have 11557 07:14:44,280 --> 07:14:48,798 same question at the beginning if no 11558 07:14:45,600 --> 07:14:51,440 doors left return false if 50 is behind 11559 07:14:48,798 --> 07:14:52,920 doors bracket middle so I'm assuming 11560 07:14:51,440 --> 07:14:54,520 here because this is pseudo code that 11561 07:14:52,920 --> 07:14:56,718 somewhere I've done the mental math or 11562 07:14:54,520 --> 07:14:59,558 the actual math to figure out what the 11563 07:14:56,718 --> 07:15:01,718 index of middle is for instance if these 11564 07:14:59,558 --> 07:15:07,398 are seven doors in an array this would 11565 07:15:01,718 --> 07:15:09,240 be location zero 1 2 3 4 5 6 so somehow 11566 07:15:07,398 --> 07:15:12,398 I've taken the total number of doors 11567 07:15:09,240 --> 07:15:13,878 seven divided by two to find the middle 11568 07:15:12,398 --> 07:15:15,478 that's three and a half we have to deal 11569 07:15:13,878 --> 07:15:17,398 with rounding but suffice it to say 11570 07:15:15,478 --> 07:15:19,398 there's a well- defined formula for 11571 07:15:17,398 --> 07:15:21,040 finding the middle index given the total 11572 07:15:19,398 --> 07:15:23,000 number of lockers divide by two and then 11573 07:15:21,040 --> 07:15:24,718 round accordingly so that's presumably 11574 07:15:23,000 --> 07:15:27,920 what Jackson did just by counting in his 11575 07:15:24,718 --> 07:15:30,200 head to find us door number three not 11576 07:15:27,920 --> 07:15:32,200 the third door the fourth door but door 11577 07:15:30,200 --> 07:15:34,398 bracket three so this is just saying if 11578 07:15:32,200 --> 07:15:36,000 50 is behind door is bracket middle 11579 07:15:34,398 --> 07:15:39,040 return true that was not the case he 11580 07:15:36,000 --> 07:15:42,160 found a $20 bill instead else if 50 is 11581 07:15:39,040 --> 07:15:44,760 less than the uh doors bracket middle go 11582 07:15:42,160 --> 07:15:48,920 ahead and now it gets interesting search 11583 07:15:44,760 --> 07:15:50,958 doors zero through doors middle minus 11584 07:15:48,920 --> 07:15:53,280 one so it's getting a little more to the 11585 07:15:50,958 --> 07:15:55,920 Weeds now but if middle is three this 11586 07:15:53,280 --> 07:15:58,360 one here well we want to now have 11587 07:15:55,920 --> 07:16:00,200 Jackson search if 50 had been uh if the 11588 07:15:58,360 --> 07:16:03,280 number had been less we want to start at 11589 07:16:00,200 --> 07:16:05,000 bracket zero and go up through this one 11590 07:16:03,280 --> 07:16:06,200 when we deliberately subtract one CU 11591 07:16:05,000 --> 07:16:08,080 what's the point of looking in the same 11592 07:16:06,200 --> 07:16:12,320 Locker again we might as well do zero 11593 07:16:08,080 --> 07:16:14,280 through middle minus one else if 50 is 11594 07:16:12,320 --> 07:16:16,558 greater than doors bracket middle which 11595 07:16:14,280 --> 07:16:18,798 it was what did we then do Jackson 11596 07:16:16,558 --> 07:16:21,240 intuitively searched for doors middle 11597 07:16:18,798 --> 07:16:23,000 plus one through door 11598 07:16:21,240 --> 07:16:24,478 n minus one and honestly it gets a 11599 07:16:23,000 --> 07:16:25,878 little Annoying having the pluses and 11600 07:16:24,478 --> 07:16:28,360 the minuses here but just think of what 11601 07:16:25,878 --> 07:16:30,478 it means this is the middle door and 11602 07:16:28,360 --> 07:16:33,040 Jackson then did proceed to search 11603 07:16:30,478 --> 07:16:34,440 through doors middle plus one because 11604 07:16:33,040 --> 07:16:37,280 there's no point in searching this one 11605 07:16:34,440 --> 07:16:39,878 again and then the last element in any 11606 07:16:37,280 --> 07:16:42,958 array of size n where n is just our 11607 07:16:39,878 --> 07:16:44,680 go-to number uh for the size is always 11608 07:16:42,958 --> 07:16:46,320 going to be n minus one it's not going 11609 07:16:44,680 --> 07:16:48,798 to be n it's going to be n minus one 11610 07:16:46,320 --> 07:16:50,718 because we always start counting at Rays 11611 07:16:48,798 --> 07:16:52,360 at zero so here then we have a 11612 07:16:50,718 --> 07:16:54,840 translation into pseudo code that's a 11613 07:16:52,360 --> 07:16:56,360 little closer to C of this exact same 11614 07:16:54,840 --> 07:16:58,360 idea and here we come full circle to 11615 07:16:56,360 --> 07:17:00,600 week zero like in week zero is pretty 11616 07:16:58,360 --> 07:17:02,478 intuitive to imagine dividing and 11617 07:17:00,600 --> 07:17:04,718 conquering a problem like this but if 11618 07:17:02,478 --> 07:17:06,478 you now think back to actual your iPhone 11619 07:17:04,718 --> 07:17:08,160 your Android phone or the like when 11620 07:17:06,478 --> 07:17:10,280 you're doing autocomplete and searching 11621 07:17:08,160 --> 07:17:12,360 the list it's possible if you don't have 11622 07:17:10,280 --> 07:17:14,878 many friends or family or colleagues in 11623 07:17:12,360 --> 07:17:16,920 the phone you know what linear search 11624 07:17:14,878 --> 07:17:19,080 just checking every name for the person 11625 07:17:16,920 --> 07:17:21,120 you're searching for might be perfectly 11626 07:17:19,080 --> 07:17:22,600 fine but odds are your phones being 11627 07:17:21,120 --> 07:17:24,798 smarter than that especially if you 11628 07:17:22,600 --> 07:17:26,240 start to have dozens hundreds thousands 11629 07:17:24,798 --> 07:17:27,878 of people in your contacts over the 11630 07:17:26,240 --> 07:17:30,478 years what would be better than linear 11631 07:17:27,878 --> 07:17:32,520 search well perhaps binary search but 11632 07:17:30,478 --> 07:17:34,680 but but there's an assumption a 11633 07:17:32,520 --> 07:17:37,280 requirement which is what why was 11634 07:17:34,680 --> 07:17:40,520 Jackson ultimately able to find the 50 11635 07:17:37,280 --> 07:17:42,878 in just like three steps instead of a 11636 07:17:40,520 --> 07:17:45,398 full seven like 11637 07:17:42,878 --> 07:17:47,160 Stephanie because the array was sorted 11638 07:17:45,398 --> 07:17:48,920 and so this is sort of a teaser for what 11639 07:17:47,160 --> 07:17:50,360 we'll have to come back to later today 11640 07:17:48,920 --> 07:17:52,200 well you know how much effort did it 11641 07:17:50,360 --> 07:17:54,200 take someone like Carter how much effort 11642 07:17:52,200 --> 07:17:56,120 does it take your phone to sort all of 11643 07:17:54,200 --> 07:17:57,718 those names and numbers in advance 11644 07:17:56,120 --> 07:17:59,120 because maybe it's not actually worth 11645 07:17:57,718 --> 07:18:01,040 the amount of time now someone like 11646 07:17:59,120 --> 07:18:03,398 Google probably somehow keeps the 11647 07:18:01,040 --> 07:18:05,320 database of web pages sorted you could 11648 07:18:03,398 --> 07:18:07,160 imagine it being super slow if when you 11649 07:18:05,320 --> 07:18:09,760 type in cats or something else into 11650 07:18:07,160 --> 07:18:11,920 google.com if they searched linearly 11651 07:18:09,760 --> 07:18:13,638 over their entire data set ideally 11652 07:18:11,920 --> 07:18:16,440 they're doing something a little smarter 11653 07:18:13,638 --> 07:18:18,160 than that so we'll formalize now exactly 11654 07:18:16,440 --> 07:18:20,160 this kind of analysis and it's not going 11655 07:18:18,160 --> 07:18:22,160 to be so much mathy as it still will be 11656 07:18:20,160 --> 07:18:24,558 in intuitive but we'll introduce you to 11657 07:18:22,160 --> 07:18:26,200 some some jargon some terminology that 11658 07:18:24,558 --> 07:18:28,240 most any programmer or computer 11659 07:18:26,200 --> 07:18:31,160 scientist might use when analyzing their 11660 07:18:28,240 --> 07:18:33,120 own algorithms let's formalize now what 11661 07:18:31,160 --> 07:18:35,000 this kind of analysis is so right right 11662 07:18:33,120 --> 07:18:37,200 now I claim binary search better than 11663 07:18:35,000 --> 07:18:39,120 linear search but how much better and 11664 07:18:37,200 --> 07:18:40,878 why exactly well it all comes back to 11665 07:18:39,120 --> 07:18:43,120 this kind of graph so this recall is how 11666 07:18:40,878 --> 07:18:45,920 we analyzed the phone book back in week 11667 07:18:43,120 --> 07:18:47,760 zero and recall that indeed we had these 11668 07:18:45,920 --> 07:18:49,638 these formulas rough formulas that 11669 07:18:47,760 --> 07:18:51,878 describe the running time of those three 11670 07:18:49,638 --> 07:18:53,320 algorithms one page at a time two pages 11671 07:18:51,878 --> 07:18:55,798 at a time and then tearing the thing 11672 07:18:53,320 --> 07:18:58,040 again and again in half and precisely if 11673 07:18:55,798 --> 07:18:59,718 you count it up the number of pages I 11674 07:18:58,040 --> 07:19:01,600 was touching or the number of pages I 11675 07:18:59,718 --> 07:19:03,718 was tearing it's fair to say that the 11676 07:19:01,600 --> 07:19:06,040 first algorithm in the worst case might 11677 07:19:03,718 --> 07:19:07,920 have taken n total Pages it didn't 11678 07:19:06,040 --> 07:19:09,398 because I was searching for John Harvard 11679 07:19:07,920 --> 07:19:11,040 at the time which is somewhat early in 11680 07:19:09,398 --> 07:19:12,638 the alphabet but if I were searching 11681 07:19:11,040 --> 07:19:14,718 with some for someone with the last name 11682 07:19:12,638 --> 07:19:16,638 of Z I would have had to keep going and 11683 07:19:14,718 --> 07:19:18,520 going in the worst case through all n 11684 07:19:16,638 --> 07:19:20,240 Pages not as bad for the second 11685 07:19:18,520 --> 07:19:22,000 algorithm and that's why we do n divided 11686 07:19:20,240 --> 07:19:25,920 by two and even that's a bit of a white 11687 07:19:22,000 --> 07:19:27,878 lie right it's probably n / 2 + 1 in 11688 07:19:25,920 --> 07:19:29,360 case I have to double back but again I'm 11689 07:19:27,878 --> 07:19:31,000 sort of doing this more generally to 11690 07:19:29,360 --> 07:19:32,360 capture the essence of these things and 11691 07:19:31,000 --> 07:19:34,920 then we really got into the weeds with 11692 07:19:32,360 --> 07:19:36,520 like log base 2 of n for that third and 11693 07:19:34,920 --> 07:19:38,680 final algorithm and at the time we 11694 07:19:36,520 --> 07:19:41,040 claimed anytime you're doing dividing 11695 07:19:38,680 --> 07:19:42,120 something in half in half in half odds 11696 07:19:41,040 --> 07:19:43,638 are there's going to be some kind of 11697 07:19:42,120 --> 07:19:45,600 logarithm involved and we'll see that 11698 07:19:43,638 --> 07:19:47,958 today but today we're going to actually 11699 07:19:45,600 --> 07:19:49,878 start using computer science terminology 11700 07:19:47,958 --> 07:19:52,798 and we're going to sort of formalize 11701 07:19:49,878 --> 07:19:55,558 this imprecision if you will we are not 11702 07:19:52,798 --> 07:19:57,958 going to care generally about exactly 11703 07:19:55,558 --> 07:19:59,000 how many steps some algorithm takes 11704 07:19:57,958 --> 07:20:01,240 because that's not going to be that 11705 07:19:59,000 --> 07:20:02,920 enlightening especially if maybe you 11706 07:20:01,240 --> 07:20:04,520 have a faster computer tomorrow than you 11707 07:20:02,920 --> 07:20:06,920 did today it wouldn't really be fair to 11708 07:20:04,520 --> 07:20:08,558 compare numbers too precisely we really 11709 07:20:06,920 --> 07:20:11,520 kind of want to with a wave of the hand 11710 07:20:08,558 --> 07:20:13,798 just get a sense of roughly how slow or 11711 07:20:11,520 --> 07:20:15,798 how fast an algorithm is so the notation 11712 07:20:13,798 --> 07:20:18,398 here is deliberate that is literally a 11713 07:20:15,798 --> 07:20:21,160 capital O often italicized referred to 11714 07:20:18,398 --> 07:20:24,440 as Big O and so the first algorithm is 11715 07:20:21,160 --> 07:20:27,440 in Big O of n the second algorithm is in 11716 07:20:24,440 --> 07:20:30,798 Big O of n / two the third algorithm is 11717 07:20:27,440 --> 07:20:33,920 in Big O of log base 2 of n but even 11718 07:20:30,798 --> 07:20:36,638 that is kind of unnecessary detail when 11719 07:20:33,920 --> 07:20:38,878 using Big O notation you really don't 11720 07:20:36,638 --> 07:20:40,920 care about we'll see the smaller ordered 11721 07:20:38,878 --> 07:20:42,878 terms right we're not going to care 11722 07:20:40,920 --> 07:20:45,200 about the divided by two because you 11723 07:20:42,878 --> 07:20:47,840 know what the shape of these algorithms 11724 07:20:45,200 --> 07:20:49,080 is almost the same and really the idea 11725 07:20:47,840 --> 07:20:50,478 the algorithm itself is sort of 11726 07:20:49,080 --> 07:20:52,718 fundamentally the same okay and instead 11727 07:20:50,478 --> 07:20:54,958 of one page at a time I'm doing two but 11728 07:20:52,718 --> 07:20:57,160 if you throw millions of pages billions 11729 07:20:54,958 --> 07:20:58,638 of pages at me those algorithms are 11730 07:20:57,160 --> 07:21:00,558 really going to kind of perform the same 11731 07:20:58,638 --> 07:21:01,840 as n gets really large goes off toward 11732 07:21:00,558 --> 07:21:03,760 infinity and the same is true for 11733 07:21:01,840 --> 07:21:05,240 logarithms even if you're a little rusty 11734 07:21:03,760 --> 07:21:07,840 it turns out that whether you do the 11735 07:21:05,240 --> 07:21:10,080 math with log base 2 log base 3 log base 11736 07:21:07,840 --> 07:21:11,878 10 you can just multiply one by the 11737 07:21:10,080 --> 07:21:13,958 other to really get the same formula 11738 07:21:11,878 --> 07:21:15,760 this is only to say a computer scientist 11739 07:21:13,958 --> 07:21:18,398 would generally say that the first two 11740 07:21:15,760 --> 07:21:20,840 algorithms are on the order of eps the 11741 07:21:18,398 --> 07:21:23,280 third algorithm is on the order of log 11742 07:21:20,840 --> 07:21:25,360 end steps and we don't really care 11743 07:21:23,280 --> 07:21:27,240 precisely what we mean beyond that and 11744 07:21:25,360 --> 07:21:28,798 this Big O notation as we'll see and 11745 07:21:27,240 --> 07:21:31,440 actually let me let me zoom out if you 11746 07:21:28,798 --> 07:21:33,958 can imagine suddenly making the x- axis 11747 07:21:31,440 --> 07:21:35,878 much longer so more pages on the screen 11748 07:21:33,958 --> 07:21:37,718 at once it is indeed going to be the 11749 07:21:35,878 --> 07:21:39,440 shapes of these curves that matter 11750 07:21:37,718 --> 07:21:41,760 because imagine in your mind's eye as 11751 07:21:39,440 --> 07:21:43,840 you zoom out zoom out zoom out zoom out 11752 07:21:41,760 --> 07:21:46,280 and as n gets much much much bigger on 11753 07:21:43,840 --> 07:21:48,520 the xaxis the red and the yellow line 11754 07:21:46,280 --> 07:21:50,680 are essentially going to look the same 11755 07:21:48,520 --> 07:21:53,080 once n is sufficiently large but the 11756 07:21:50,680 --> 07:21:54,558 green line is never going to look the 11757 07:21:53,080 --> 07:21:55,760 same it's going to be a fundamentally 11758 07:21:54,558 --> 07:21:58,558 different shape and so that's the 11759 07:21:55,760 --> 07:22:02,160 intuition of bigo to get a sense of 11760 07:21:58,558 --> 07:22:04,280 these uh rates of performance like this 11761 07:22:02,160 --> 07:22:06,718 so here then is Big O here is perhaps a 11762 07:22:04,280 --> 07:22:08,040 cheat sheet of like the common formulas 11763 07:22:06,718 --> 07:22:10,160 that a computer scientist certainly in 11764 07:22:08,040 --> 07:22:11,920 an introductory context might use when 11765 07:22:10,160 --> 07:22:14,398 analyzing algorithms and let's consider 11766 07:22:11,920 --> 07:22:15,958 for a moment which of our first two 11767 07:22:14,398 --> 07:22:17,958 algorithms linear search and binary 11768 07:22:15,958 --> 07:22:20,280 search fall into these categories so 11769 07:22:17,958 --> 07:22:23,760 I've ordered them from sort of slowest 11770 07:22:20,280 --> 07:22:25,200 to fastest so order of n s it's not 11771 07:22:23,760 --> 07:22:27,200 something we've actually seen yet but it 11772 07:22:25,200 --> 07:22:29,000 tends to be slow because it's quadratic 11773 07:22:27,200 --> 07:22:30,920 you're doing n * n that's got to add up 11774 07:22:29,000 --> 07:22:33,520 to a lot of steps better today is going 11775 07:22:30,920 --> 07:22:35,760 to be n Lin even better is going to be n 11776 07:22:33,520 --> 07:22:39,320 even better than that is login and best 11777 07:22:35,760 --> 07:22:41,080 is soall order of one like one step or 11778 07:22:39,320 --> 07:22:43,840 maybe two steps maybe even a thousand 11779 07:22:41,080 --> 07:22:46,318 steps but a fixed finite number of steps 11780 07:22:43,840 --> 07:22:48,638 that never changes no how matter how big 11781 07:22:46,318 --> 07:22:51,638 n is so given this chart just to be 11782 07:22:48,638 --> 07:22:54,478 clear linear search let's consider the 11783 07:22:51,638 --> 07:22:57,000 worst case in the worst case how many 11784 07:22:54,478 --> 07:22:59,798 steps did it take someone like Stephanie 11785 07:22:57,000 --> 07:23:03,160 to find the uh solution to the problem 11786 07:22:59,798 --> 07:23:05,760 assuming not seven doors but n doors 11787 07:23:03,160 --> 07:23:08,080 yeah so on the order of N and in this 11788 07:23:05,760 --> 07:23:10,360 case it's exactly n but you know what 11789 07:23:08,080 --> 07:23:11,958 you know maybe it's arguably too n right 11790 07:23:10,360 --> 07:23:13,398 because it took Stephanie a couple of 11791 07:23:11,958 --> 07:23:14,840 steps like she had to lift the latch she 11792 07:23:13,398 --> 07:23:16,638 had to open the door maybe it's three 11793 07:23:14,840 --> 07:23:19,240 steps you had to show the money so now 11794 07:23:16,638 --> 07:23:21,160 it's 3n 2 N but there we don't really 11795 07:23:19,240 --> 07:23:23,200 care about that level of precision we 11796 07:23:21,160 --> 07:23:25,440 really just care about the fundamental 11797 07:23:23,200 --> 07:23:27,760 number of operations so we'll say yes on 11798 07:23:25,440 --> 07:23:30,160 the order of n so that might be an upper 11799 07:23:27,760 --> 07:23:32,798 bound we'll call this for linear search 11800 07:23:30,160 --> 07:23:34,958 and how about binary search in Jackson's 11801 07:23:32,798 --> 07:23:37,000 case or in general me and week zero if 11802 07:23:34,958 --> 07:23:40,840 there's end doors how many steps did it 11803 07:23:37,000 --> 07:23:43,240 take Jackson or me using binary 11804 07:23:40,840 --> 07:23:45,360 search in this case it was literally 11805 07:23:43,240 --> 07:23:48,120 three but that's not a 11806 07:23:45,360 --> 07:23:50,638 formula yeah so it's on the order of log 11807 07:23:48,120 --> 07:23:52,240 n and indeed if there's seven doors well 11808 07:23:50,638 --> 07:23:53,798 that's almost eight if you just do a 11809 07:23:52,240 --> 07:23:55,958 little bit of rounding and indeed if you 11810 07:23:53,798 --> 07:23:57,958 take log base 2 of eight okay so that 11811 07:23:55,958 --> 07:23:58,958 does actually give us three so the math 11812 07:23:57,958 --> 07:24:00,520 actually checks out and if you're not 11813 07:23:58,958 --> 07:24:03,120 comy with logarithms no big deal just 11814 07:24:00,520 --> 07:24:04,878 think about it intuitively uh logarithm 11815 07:24:03,120 --> 07:24:07,398 of base two is just dividing something 11816 07:24:04,878 --> 07:24:10,318 again and again so on this chart when we 11817 07:24:07,398 --> 07:24:12,840 consider Big O which to be clear allows 11818 07:24:10,318 --> 07:24:14,240 you to describe the order of an 11819 07:24:12,840 --> 07:24:16,318 algorithm's running time like the 11820 07:24:14,240 --> 07:24:19,398 magnitude of it but it also describes 11821 07:24:16,318 --> 07:24:22,040 more specifically an upper bound so in 11822 07:24:19,398 --> 07:24:24,798 the worst case for instance these are 11823 07:24:22,040 --> 07:24:27,040 pretty good measures of how good uh or 11824 07:24:24,798 --> 07:24:29,080 rather of how bad linear search and 11825 07:24:27,040 --> 07:24:30,760 binary search might be why well suppose 11826 07:24:29,080 --> 07:24:32,798 you're searching a thousand page phone 11827 07:24:30,760 --> 07:24:34,000 book and the person's name starts with z 11828 07:24:32,798 --> 07:24:36,040 the algorithm is still going to be on 11829 07:24:34,000 --> 07:24:38,680 the order of n steps why because it 11830 07:24:36,040 --> 07:24:42,240 might take you as many as all end steps 11831 07:24:38,680 --> 07:24:44,360 to find it now that's not necessarily 11832 07:24:42,240 --> 07:24:47,160 going to be the case in practice if I 11833 07:24:44,360 --> 07:24:48,520 use Big O as an upper bound well it 11834 07:24:47,160 --> 07:24:50,600 would be nice if there's sort of a 11835 07:24:48,520 --> 07:24:52,360 corresponding lower bound especially if 11836 07:24:50,600 --> 07:24:55,760 you want to consider not just best uh 11837 07:24:52,360 --> 07:24:57,398 worst cases but maybe best cases so what 11838 07:24:55,760 --> 07:25:00,040 might we use here well so this is a 11839 07:24:57,398 --> 07:25:01,398 capital Greek omega symbol so Omega is 11840 07:25:00,040 --> 07:25:03,878 the symbol that a computer scientist 11841 07:25:01,398 --> 07:25:05,958 uses generally to describe a lower bound 11842 07:25:03,878 --> 07:25:08,000 on an algorithm often in the context of 11843 07:25:05,958 --> 07:25:10,398 best case though not necessarily so a 11844 07:25:08,000 --> 07:25:12,318 lower bound means how few steps might an 11845 07:25:10,398 --> 07:25:14,080 algorithm take and here too same 11846 07:25:12,318 --> 07:25:16,958 formulas and we'll fill in these blanks 11847 07:25:14,080 --> 07:25:19,240 over time some algorithms might always 11848 07:25:16,958 --> 07:25:21,398 take a minimum of n squ steps or on the 11849 07:25:19,240 --> 07:25:25,760 order of n steps some might only take n 11850 07:25:21,398 --> 07:25:28,798 logn or n or log n or one so something 11851 07:25:25,760 --> 07:25:30,440 like uh linear search when Stephanie 11852 07:25:28,798 --> 07:25:32,360 started with linear search she didn't 11853 07:25:30,440 --> 07:25:34,840 get lucky this time on stage but what if 11854 07:25:32,360 --> 07:25:38,000 she had and the first door she opened 11855 07:25:34,840 --> 07:25:41,440 were 50 how much you then describe the 11856 07:25:38,000 --> 07:25:43,120 lower bound on linear search in this 11857 07:25:41,440 --> 07:25:46,398 so-called best 11858 07:25:43,120 --> 07:25:50,398 case using this list of possible 11859 07:25:46,398 --> 07:25:53,240 answers yeah yeah so Omega of one so in 11860 07:25:50,398 --> 07:25:55,360 the best case the lower bound on how 11861 07:25:53,240 --> 07:25:57,360 many steps it might take uh linear 11862 07:25:55,360 --> 07:25:59,760 search to find something might just be 11863 07:25:57,360 --> 07:26:02,080 one step why because maybe Stephanie had 11864 07:25:59,760 --> 07:26:04,520 gotten lucky and we had pre-filled these 11865 07:26:02,080 --> 07:26:06,120 lockers with the numbers in some other 11866 07:26:04,520 --> 07:26:08,200 order such that she might have open the 11867 07:26:06,120 --> 07:26:09,638 first locker and waila the number 50 11868 07:26:08,200 --> 07:26:12,200 could have been there so a lower bound 11869 07:26:09,638 --> 07:26:14,398 arguably could indeed be Omega of one 11870 07:26:12,200 --> 07:26:16,760 for linear search and how about now for 11871 07:26:14,398 --> 07:26:18,840 Jackson he used binary search so he 11872 07:26:16,760 --> 07:26:21,478 dived right into the middle of the 11873 07:26:18,840 --> 07:26:25,080 problem but what would be a lower bound 11874 07:26:21,478 --> 07:26:27,040 on binary search using this logic yeah 11875 07:26:25,080 --> 07:26:29,040 yeah so again Omega of one why because 11876 07:26:27,040 --> 07:26:30,600 maybe he just gets lucky and indeed 11877 07:26:29,040 --> 07:26:33,280 right in the middle of the lockers could 11878 07:26:30,600 --> 07:26:36,120 have been the number 50 it wasn't and so 11879 07:26:33,280 --> 07:26:38,200 more Germain in Jackson's uh actual 11880 07:26:36,120 --> 07:26:41,160 practice would have been the Big O 11881 07:26:38,200 --> 07:26:42,840 discussion but Big O and Omega upper 11882 07:26:41,160 --> 07:26:44,318 bound and lower bound just allow a 11883 07:26:42,840 --> 07:26:45,600 computer scientist to kind of wrestle 11884 07:26:44,318 --> 07:26:47,080 with what could happen maybe in the 11885 07:26:45,600 --> 07:26:48,958 worst case what can happen in the best 11886 07:26:47,080 --> 07:26:50,760 case and you can even get even more 11887 07:26:48,958 --> 07:26:52,558 precise like the average case or the 11888 07:26:50,760 --> 07:26:54,280 like and this is indeed what Engineers 11889 07:26:52,558 --> 07:26:56,440 might do at a whiteboard in a company in 11890 07:26:54,280 --> 07:26:58,000 a university when designing an algorithm 11891 07:26:56,440 --> 07:26:59,920 and trying to make arguments as to like 11892 07:26:58,000 --> 07:27:02,160 why their algorithm is better than 11893 07:26:59,920 --> 07:27:04,520 someone else's by way of these kinds of 11894 07:27:02,160 --> 07:27:07,958 analyses and just so you've seen it it 11895 07:27:04,520 --> 07:27:10,878 turns out that if some algorithm happens 11896 07:27:07,958 --> 07:27:13,080 to have an identical upper bound and 11897 07:27:10,878 --> 07:27:14,958 lower bound you can actually use a 11898 07:27:13,080 --> 07:27:17,160 capital Greek Theta as well and this is 11899 07:27:14,958 --> 07:27:19,680 the last of the the Greek symbols today 11900 07:27:17,160 --> 07:27:21,680 but a Greek Theta indicates a coinci 11901 07:27:19,680 --> 07:27:24,000 idents of both upper bound and lower 11902 07:27:21,680 --> 07:27:25,318 bound that is they are one and the same 11903 07:27:24,000 --> 07:27:26,878 that was not the case for our discussion 11904 07:27:25,318 --> 07:27:29,680 a second ago of linear search not the 11905 07:27:26,878 --> 07:27:32,440 case for binary search but you could use 11906 07:27:29,680 --> 07:27:33,840 the same kinds of formulas if it turns 11907 07:27:32,440 --> 07:27:36,760 out that your upper bound and lower 11908 07:27:33,840 --> 07:27:38,398 bound are the same so for instance if I 11909 07:27:36,760 --> 07:27:42,760 were to count everyone like literally in 11910 07:27:38,398 --> 07:27:44,360 this room 1 2 3 4 five six and so forth 11911 07:27:42,760 --> 07:27:48,520 you could actually say that counting in 11912 07:27:44,360 --> 07:27:50,120 that way is in Theta of n right because 11913 07:27:48,520 --> 07:27:52,318 I in the best case it's going to take me 11914 07:27:50,120 --> 07:27:53,638 end points at the uh people in the 11915 07:27:52,318 --> 07:27:55,000 audience in the worst case it's going to 11916 07:27:53,638 --> 07:27:56,680 take me n it's always going to take me 11917 07:27:55,000 --> 07:27:58,398 end steps if I want to count everyone in 11918 07:27:56,680 --> 07:28:00,200 the room you can't really do better than 11919 07:27:58,398 --> 07:28:02,000 that unless you skip people so that 11920 07:28:00,200 --> 07:28:05,000 would be an example off the cuff of 11921 07:28:02,000 --> 07:28:09,520 something where Theta is instead germine 11922 07:28:05,000 --> 07:28:11,958 are any questions now on Big O on Omega 11923 07:28:09,520 --> 07:28:14,318 or Theta which are now just more formal 11924 07:28:11,958 --> 07:28:16,680 tools in the toolkit for talking about 11925 07:28:14,318 --> 07:28:18,600 the design of our 11926 07:28:16,680 --> 07:28:21,120 algorithms any 11927 07:28:18,600 --> 07:28:25,878 questions 11928 07:28:21,120 --> 07:28:28,120 no seeing none yeah oh is this yes no 11929 07:28:25,878 --> 07:28:30,160 okay so we're good so let's go ahead and 11930 07:28:28,120 --> 07:28:32,280 translate this perhaps to some some 11931 07:28:30,160 --> 07:28:34,718 actual code let me go over to vs code 11932 07:28:32,280 --> 07:28:36,240 here and let's see if we can't now 11933 07:28:34,718 --> 07:28:38,760 translate some of these ideas to some 11934 07:28:36,240 --> 07:28:40,478 actual code not so much using new syntax 11935 07:28:38,760 --> 07:28:42,318 yet we're going to still operate in this 11936 07:28:40,478 --> 07:28:43,878 world of arrays like last week so let me 11937 07:28:42,318 --> 07:28:46,638 go ahead and create a program called 11938 07:28:43,878 --> 07:28:48,718 search. C by executing code space 11939 07:28:46,638 --> 07:28:50,160 search. c in my terminal and then up 11940 07:28:48,718 --> 07:28:52,840 here let's go ahead and include our 11941 07:28:50,160 --> 07:28:56,000 usual so include cs50.h so I can get 11942 07:28:52,840 --> 07:28:59,160 some input include standard i.h so I can 11943 07:28:56,000 --> 07:29:00,638 print some output we'll do int main void 11944 07:28:59,160 --> 07:29:02,440 which the meaning of which we did start 11945 07:29:00,638 --> 07:29:04,240 to tease apart last week the fact that 11946 07:29:02,440 --> 07:29:06,080 it's void again today just means no 11947 07:29:04,240 --> 07:29:08,160 command line arguments and let me go 11948 07:29:06,080 --> 07:29:11,000 ahead and do this let me go ahead and 11949 07:29:08,160 --> 07:29:13,080 declare just for discussion's sake a 11950 07:29:11,000 --> 07:29:14,840 static array like an array that never 11951 07:29:13,080 --> 07:29:18,240 changes and the Syntax for this is going 11952 07:29:14,840 --> 07:29:20,360 to be give me an array called numbers 11953 07:29:18,240 --> 07:29:22,240 using the square bracket not ation and 11954 07:29:20,360 --> 07:29:26,958 I'm going to immediately initialize it 11955 07:29:22,240 --> 07:29:29,080 to 20 500 10 5 100 1 and 50 reminiscent 11956 07:29:26,958 --> 07:29:31,520 of those same denominations as before so 11957 07:29:29,080 --> 07:29:34,160 this is a slightly new syntax that we've 11958 07:29:31,520 --> 07:29:35,920 uh perhaps not seen and the curly braces 11959 07:29:34,160 --> 07:29:38,080 here which are different from for loops 11960 07:29:35,920 --> 07:29:40,718 and while loops and functions just tell 11961 07:29:38,080 --> 07:29:42,878 the compiler please give me an array of 11962 07:29:40,718 --> 07:29:44,600 whatever size this is containing those 11963 07:29:42,878 --> 07:29:47,600 numbers left to right I could 11964 07:29:44,600 --> 07:29:49,520 alternatively use last week's syntax of 11965 07:29:47,600 --> 07:29:52,318 saying something like this let's see one 11966 07:29:49,520 --> 07:29:55,000 2 3 4 5 six seven denominations I could 11967 07:29:52,318 --> 07:29:59,360 alternatively do this and then I could 11968 07:29:55,000 --> 07:30:04,398 say numbers bracket 0 uh numbers bracket 11969 07:29:59,360 --> 07:30:06,558 0 equals 20 numbers bracket 1 equal 500 11970 07:30:04,398 --> 07:30:08,120 and I could do this five more times 11971 07:30:06,558 --> 07:30:09,638 that's just a little tedious if you know 11972 07:30:08,120 --> 07:30:11,520 the numbers in advance you don't have to 11973 07:30:09,638 --> 07:30:13,200 tell the compiler how many there are you 11974 07:30:11,520 --> 07:30:17,680 can just let it figure it out that your 11975 07:30:13,200 --> 07:30:20,040 numbers are be will be 20 500 10 5 100 1 11976 07:30:17,680 --> 07:30:22,318 and 5050 so this is how you statically 11977 07:30:20,040 --> 07:30:24,080 Define an array all right let me just go 11978 07:30:22,318 --> 07:30:26,478 ahead and ask the user now for a number 11979 07:30:24,080 --> 07:30:28,200 we'll call it n by using get int and 11980 07:30:26,478 --> 07:30:30,478 prompting them for a number so nothing 11981 07:30:28,200 --> 07:30:33,280 new there and now let me go ahead and 11982 07:30:30,478 --> 07:30:34,878 Implement linear search and the pseudo 11983 07:30:33,280 --> 07:30:37,160 code we had for this before used some 11984 07:30:34,878 --> 07:30:40,840 array like notation let me go ahead then 11985 07:30:37,160 --> 07:30:42,558 and start similarly four in I and it's 11986 07:30:40,840 --> 07:30:44,440 you almost always start counting at I by 11987 07:30:42,558 --> 07:30:46,398 convention so that's perhaps a good 11988 07:30:44,440 --> 07:30:48,920 starting point I'm going to do this so 11989 07:30:46,398 --> 07:30:50,760 long as I is less than seven not the 11990 07:30:48,920 --> 07:30:52,318 best design to hard code the seven but 11991 07:30:50,760 --> 07:30:53,638 this is just for demonstration sake for 11992 07:30:52,318 --> 07:30:55,160 now because I know how many numbers I 11993 07:30:53,638 --> 07:30:57,120 put in there and then I'm going to do 11994 07:30:55,160 --> 07:30:58,958 i++ so now I have the beginnings of a 11995 07:30:57,120 --> 07:31:01,200 loop that will just allow me to iterate 11996 07:30:58,958 --> 07:31:05,600 over the entire array and let me ask 11997 07:31:01,200 --> 07:31:08,398 this if the current number at location I 11998 07:31:05,600 --> 07:31:10,718 equals equals n which is the number the 11999 07:31:08,398 --> 07:31:12,440 human typed in then let's go ahead and 12000 07:31:10,718 --> 07:31:16,160 do something simple like print F quote 12001 07:31:12,440 --> 07:31:18,040 unquote found back sln and then per our 12002 07:31:16,160 --> 07:31:19,638 discussion last week to indicate that 12003 07:31:18,040 --> 07:31:22,478 this is successful I'm going to going to 12004 07:31:19,638 --> 07:31:24,080 return zero if I found it and if I don't 12005 07:31:22,478 --> 07:31:27,760 find it I'm just going to go down here 12006 07:31:24,080 --> 07:31:29,718 and by default say not found back sln 12007 07:31:27,760 --> 07:31:32,040 and just for convention whoops just for 12008 07:31:29,718 --> 07:31:34,478 good measure per convention I'll return 12009 07:31:32,040 --> 07:31:36,878 one or really any value other than zero 12010 07:31:34,478 --> 07:31:39,520 zero recall means success and any other 12011 07:31:36,878 --> 07:31:41,000 integer tends to mean error of some sort 12012 07:31:39,520 --> 07:31:44,160 irrespective of the number I'm looking 12013 07:31:41,000 --> 07:31:45,680 for so just to revisit the only thing 12014 07:31:44,160 --> 07:31:48,080 that's new here is the syntax for 12015 07:31:45,680 --> 07:31:50,280 creating an array of seven numbers these 12016 07:31:48,080 --> 07:31:52,760 numbers and then after after that we 12017 07:31:50,280 --> 07:31:55,120 have really highlighted here an 12018 07:31:52,760 --> 07:31:56,840 implementation of linear search I mean 12019 07:31:55,120 --> 07:31:59,040 this is the C version I dare say of what 12020 07:31:56,840 --> 07:32:00,798 Stephanie did on the board whereas now 12021 07:31:59,040 --> 07:32:03,520 the array is called numbers instead of 12022 07:32:00,798 --> 07:32:05,558 doors but I think it's pretty much the 12023 07:32:03,520 --> 07:32:08,360 same let me go ahead and open my 12024 07:32:05,558 --> 07:32:11,920 terminal window and run make 12025 07:32:08,360 --> 07:32:13,080 search seems to compile okay search and 12026 07:32:11,920 --> 07:32:15,240 let's go ahead and search for a number 12027 07:32:13,080 --> 07:32:17,280 we'll start with what we did before 50 12028 07:32:15,240 --> 07:32:20,440 and it's found let's go ahead and run it 12029 07:32:17,280 --> 07:32:22,478 again/ search let's search for maybe 20 12030 07:32:20,440 --> 07:32:24,478 at the beginning that one too is found 12031 07:32:22,478 --> 07:32:28,440 let's run it one more time searching for 12032 07:32:24,478 --> 07:32:31,080 like 1,000 which is not uh in among the 12033 07:32:28,440 --> 07:32:33,080 uh denominations and that one indeed is 12034 07:32:31,080 --> 07:32:35,000 not found so we've taken an idea from 12035 07:32:33,080 --> 07:32:38,360 week zero now formalized in week three 12036 07:32:35,000 --> 07:32:40,798 and just translated it now to code 12037 07:32:38,360 --> 07:32:43,240 questions on this implementation of 12038 07:32:40,798 --> 07:32:45,520 linear 12039 07:32:43,240 --> 07:32:49,958 search linear 12040 07:32:45,520 --> 07:32:52,478 search nothing oh so successful so far 12041 07:32:49,958 --> 07:32:53,958 today okay so let's see if we can't 12042 07:32:52,478 --> 07:32:55,558 maybe make this a little more 12043 07:32:53,958 --> 07:32:57,080 interesting and see if we can't trip 12044 07:32:55,558 --> 07:33:00,120 over a detail that's going to be 12045 07:32:57,080 --> 07:33:02,478 important in C instead of doing numbers 12046 07:33:00,120 --> 07:33:04,240 let me go ahead and do this we'll stay 12047 07:33:02,478 --> 07:33:05,200 on theme with Monopoly and I went down 12048 07:33:04,240 --> 07:33:06,798 the rabbit hole of reading the 12049 07:33:05,200 --> 07:33:09,318 wikkipedia article on Monopoly and the 12050 07:33:06,798 --> 07:33:11,240 original uh pieces or tokens that came 12051 07:33:09,318 --> 07:33:12,718 with Monopoly and it turns out we can 12052 07:33:11,240 --> 07:33:14,878 represent those with strings so I'm 12053 07:33:12,718 --> 07:33:18,000 going to create an array called strings 12054 07:33:14,878 --> 07:33:20,360 plural of whatever size I defined here 12055 07:33:18,000 --> 07:33:22,840 and the very first first Monopoly pieces 12056 07:33:20,360 --> 07:33:27,040 back in the day were a battleship that 12057 07:33:22,840 --> 07:33:29,520 you could play with a boot a cannon an 12058 07:33:27,040 --> 07:33:32,240 iron a 12059 07:33:29,520 --> 07:33:33,760 thimble and a top hat some of which you 12060 07:33:32,240 --> 07:33:35,440 might know from the game nowadays turns 12061 07:33:33,760 --> 07:33:37,680 out they've been changing these uh had 12062 07:33:35,440 --> 07:33:39,638 no idea over the years so here is now an 12063 07:33:37,680 --> 07:33:42,160 array of strings let me go ahead and 12064 07:33:39,638 --> 07:33:44,600 prompt the user now not for an integer 12065 07:33:42,160 --> 07:33:46,798 anymore I want to Now search for one of 12066 07:33:44,600 --> 07:33:48,840 these strings still using linear search 12067 07:33:46,798 --> 07:33:51,558 so let me create a string s set it equal 12068 07:33:48,840 --> 07:33:54,200 to string prompt the user for a string 12069 07:33:51,558 --> 07:33:57,558 to search for and then I think my code 12070 07:33:54,200 --> 07:33:59,840 here is almost the same except for one 12071 07:33:57,558 --> 07:34:03,040 detail I now have an array called 12072 07:33:59,840 --> 07:34:05,120 strings I now have a variable called s 12073 07:34:03,040 --> 07:34:07,040 but it turns out for reasons we'll 12074 07:34:05,120 --> 07:34:10,360 explore in more detail next week this 12075 07:34:07,040 --> 07:34:13,280 line of code is not going to work and it 12076 07:34:10,360 --> 07:34:14,958 turns out the reason has to do with what 12077 07:34:13,280 --> 07:34:17,478 we discussed last week of like what a 12078 07:34:14,958 --> 07:34:20,760 string really is and what is a string 12079 07:34:17,478 --> 07:34:23,878 again a string is an array and it turns 12080 07:34:20,760 --> 07:34:26,680 out though that equals equals is not 12081 07:34:23,878 --> 07:34:29,120 going to generously compare all of the 12082 07:34:26,680 --> 07:34:31,318 characters in an array for you just 12083 07:34:29,120 --> 07:34:33,000 because you use equal equals it turns 12084 07:34:31,318 --> 07:34:37,558 out it's not going to compare every 12085 07:34:33,000 --> 07:34:39,920 letter and so thankfully there is in the 12086 07:34:37,558 --> 07:34:42,160 uh string library that we introduced 12087 07:34:39,920 --> 07:34:43,680 last week a solution to this problem the 12088 07:34:42,160 --> 07:34:45,920 reason for the problem we'll explore in 12089 07:34:43,680 --> 07:34:47,318 more detail next week but for now just 12090 07:34:45,920 --> 07:34:49,558 know that when you want to compare 12091 07:34:47,318 --> 07:34:51,478 strings in C especially if you've come 12092 07:34:49,558 --> 07:34:53,478 into the class knowing a bit of Java or 12093 07:34:51,478 --> 07:34:55,160 python or some other language you cannot 12094 07:34:53,478 --> 07:34:58,040 use equals equals even though you could 12095 07:34:55,160 --> 07:35:00,920 in scratch you cannot in C so what I 12096 07:34:58,040 --> 07:35:03,398 have to actually do here is this I have 12097 07:35:00,920 --> 07:35:05,760 to ask the question does the return 12098 07:35:03,398 --> 07:35:07,000 value of a function called stir compare 12099 07:35:05,760 --> 07:35:10,920 or stir 12100 07:35:07,000 --> 07:35:15,360 comp equal zero when passed in the 12101 07:35:10,920 --> 07:35:17,040 current string and that user input so if 12102 07:35:15,360 --> 07:35:19,478 you read the documentation for this 12103 07:35:17,040 --> 07:35:22,120 function called stir compare you'll see 12104 07:35:19,478 --> 07:35:24,718 that it takes two strings as input first 12105 07:35:22,120 --> 07:35:26,398 one and second one it then someone 12106 07:35:24,718 --> 07:35:28,000 decades ago wrote the code that probably 12107 07:35:26,398 --> 07:35:30,240 uses a four Loop or a while loop to 12108 07:35:28,000 --> 07:35:33,360 compare every character in each of those 12109 07:35:30,240 --> 07:35:36,360 strings and it turns out it returns zero 12110 07:35:33,360 --> 07:35:38,600 if they are in fact equal turns out too 12111 07:35:36,360 --> 07:35:41,638 it will return a positive number or a 12112 07:35:38,600 --> 07:35:43,840 negative number in other situations any 12113 07:35:41,638 --> 07:35:46,558 intuition for why it might actually be 12114 07:35:43,840 --> 07:35:49,718 useful to have a function that allows 12115 07:35:46,558 --> 07:35:51,280 you to check if two strings are equal 12116 07:35:49,718 --> 07:35:55,240 if they're not equal what else might be 12117 07:35:51,280 --> 07:35:55,240 interesting to know when comparing two 12118 07:35:56,318 --> 07:36:00,200 strings are if certain values 12119 07:36:01,600 --> 07:36:06,080 are okay possibly maybe you want to know 12120 07:36:03,958 --> 07:36:07,958 just how similar they are um and that's 12121 07:36:06,080 --> 07:36:11,200 indeed an algorithm unto itself but stir 12122 07:36:07,958 --> 07:36:11,200 compare is a little simpler than 12123 07:36:14,000 --> 07:36:18,080 that exactly if you're trying to like 12124 07:36:16,360 --> 07:36:19,478 alphabetize a whole list of strings just 12125 07:36:18,080 --> 07:36:21,360 like your phone probably is for your 12126 07:36:19,478 --> 07:36:23,958 context or address book it turns out 12127 07:36:21,360 --> 07:36:26,160 that stir compare will actually return a 12128 07:36:23,958 --> 07:36:28,958 positive number or a negative number or 12129 07:36:26,160 --> 07:36:31,760 a zero based on whether maybe it comes 12130 07:36:28,958 --> 07:36:33,840 alphabetically first or later or or in 12131 07:36:31,760 --> 07:36:36,000 fact equal so that can be a useful thing 12132 07:36:33,840 --> 07:36:37,360 and that's just a teaser for a lower 12133 07:36:36,000 --> 07:36:39,440 level explanation that we'll see next 12134 07:36:37,360 --> 07:36:41,200 week so now let me cross my fingers and 12135 07:36:39,440 --> 07:36:43,318 see if I got this right let me go ahead 12136 07:36:41,200 --> 07:36:47,440 and do make 12137 07:36:43,318 --> 07:36:49,160 search did compile okay albeit slowly 12138 07:36:47,440 --> 07:36:51,878 do/ search and let's search for 12139 07:36:49,160 --> 07:36:53,760 something like the thimble and we see 12140 07:36:51,878 --> 07:36:55,200 that that's indeed found otherwise let's 12141 07:36:53,760 --> 07:36:56,958 search for something that I know isn't 12142 07:36:55,200 --> 07:37:00,240 there like a race car which was there 12143 07:36:56,958 --> 07:37:02,680 when I grew up but huh segmentation 12144 07:37:00,240 --> 07:37:04,120 fault core dumped like and actually some 12145 07:37:02,680 --> 07:37:06,080 of you have tripped over this error 12146 07:37:04,120 --> 07:37:08,440 before anyone want to admit seeing this 12147 07:37:06,080 --> 07:37:10,600 so yeah not something we've talked about 12148 07:37:08,440 --> 07:37:12,760 and um honestly not something I intended 12149 07:37:10,600 --> 07:37:15,760 just now but that too we'll see next 12150 07:37:12,760 --> 07:37:17,798 week any intuition for why my program 12151 07:37:15,760 --> 07:37:20,680 just 12152 07:37:17,798 --> 07:37:23,120 broke I didn't really change the logic 12153 07:37:20,680 --> 07:37:24,600 it's still linear search let me hide the 12154 07:37:23,120 --> 07:37:26,200 terminal so you can see all of the code 12155 07:37:24,600 --> 07:37:28,840 at once the only thing I did was 12156 07:37:26,200 --> 07:37:31,718 switched from integers to Strings and I 12157 07:37:28,840 --> 07:37:33,520 switched to stir compare here but 12158 07:37:31,718 --> 07:37:35,000 segmentation fault happened and the 12159 07:37:33,520 --> 07:37:38,040 teaser is that that somehow relates to 12160 07:37:35,000 --> 07:37:38,040 the computer's memory 12161 07:37:39,600 --> 07:37:46,200 yeah yeah and this is subtle but spot on 12162 07:37:42,360 --> 07:37:49,360 so 1 2 3 4 5 six elements total in this 12163 07:37:46,200 --> 07:37:51,120 array versus the seven number 12164 07:37:49,360 --> 07:37:53,080 of Monopoly denominations that we had 12165 07:37:51,120 --> 07:37:54,798 earlier and this is where see sort of 12166 07:37:53,080 --> 07:37:57,080 case in point this came back to bite me 12167 07:37:54,798 --> 07:37:58,798 the fact that I hardcoded this value as 12168 07:37:57,080 --> 07:38:00,840 to opposed to maybe separating it out as 12169 07:37:58,798 --> 07:38:02,798 a constant or declaring it higher up 12170 07:38:00,840 --> 07:38:05,798 kind of bit me here because now I'm 12171 07:38:02,798 --> 07:38:07,920 iterating over an array of size six but 12172 07:38:05,798 --> 07:38:09,558 clearly I'm going one step too far 12173 07:38:07,920 --> 07:38:12,120 because I'm literally going to iterate 12174 07:38:09,558 --> 07:38:14,398 seven times not six so it's as though 12175 07:38:12,120 --> 07:38:16,000 I'm looking at memory that's over here 12176 07:38:14,398 --> 07:38:17,840 and indeed next week we'll focus on 12177 07:38:16,000 --> 07:38:19,478 memory and that's just a bad thing so 12178 07:38:17,840 --> 07:38:21,280 odds are not even and seeing your code 12179 07:38:19,478 --> 07:38:23,920 from this past week if any of you have 12180 07:38:21,280 --> 07:38:26,080 had segmentation faults odds are you 12181 07:38:23,920 --> 07:38:28,398 touched memory that you shouldn't have 12182 07:38:26,080 --> 07:38:30,680 you maybe looped too many times you 12183 07:38:28,398 --> 07:38:32,360 might have uh used a negative number to 12184 07:38:30,680 --> 07:38:34,200 get into your array in general you 12185 07:38:32,360 --> 07:38:35,760 touched memory that you shouldn't have 12186 07:38:34,200 --> 07:38:37,478 and you touched a segment of memory that 12187 07:38:35,760 --> 07:38:39,718 you shouldn't have the fix though at 12188 07:38:37,478 --> 07:38:41,798 least in my case is simple just don't do 12189 07:38:39,718 --> 07:38:43,718 that so let me go ahead and recompile 12190 07:38:41,798 --> 07:38:46,558 this make uh 12191 07:38:43,718 --> 07:38:49,398 search do/ search and I'll search again 12192 07:38:46,558 --> 07:38:51,160 for uh race car and 12193 07:38:49,398 --> 07:38:53,558 and now it does not crash but it does 12194 07:38:51,160 --> 07:38:54,760 tell me it's not found so subtle but 12195 07:38:53,558 --> 07:38:57,440 something you might yourself have 12196 07:38:54,760 --> 07:39:01,040 tripped over already questions then on 12197 07:38:57,440 --> 07:39:04,360 what I just did intentionally or 12198 07:39:01,040 --> 07:39:04,360 otherwise yeah in 12199 07:39:06,280 --> 07:39:11,080 front return don't return Z return so 12200 07:39:10,080 --> 07:39:13,318 what 12201 07:39:11,080 --> 07:39:15,840 the a really good question so the 12202 07:39:13,318 --> 07:39:18,318 program will still work even if I don't 12203 07:39:15,840 --> 07:39:21,280 return zero or return one in fact let me 12204 07:39:18,318 --> 07:39:23,160 go ahead head and do that and just hide 12205 07:39:21,280 --> 07:39:25,558 my terminal window for a second let's 12206 07:39:23,160 --> 07:39:28,240 get rid of the return here let's get rid 12207 07:39:25,558 --> 07:39:30,520 of the return here however Watch What 12208 07:39:28,240 --> 07:39:33,120 Happens here uh let me go ahead and 12209 07:39:30,520 --> 07:39:35,080 recompile this make search Let Me scroll 12210 07:39:33,120 --> 07:39:37,318 up in my code here let me go ahead and 12211 07:39:35,080 --> 07:39:39,160 do do/ search and let me go ahead and 12212 07:39:37,318 --> 07:39:41,120 search for the first thing in the list 12213 07:39:39,160 --> 07:39:42,638 Battleship so I know that this should be 12214 07:39:41,120 --> 07:39:45,398 found I hit 12215 07:39:42,638 --> 07:39:46,760 enter huh interesting so it's saying 12216 07:39:45,398 --> 07:39:50,558 found not found but do you see why 12217 07:39:46,760 --> 07:39:50,558 logically in this case 12218 07:39:51,958 --> 07:39:56,000 exactly so the loop is still running so 12219 07:39:54,160 --> 07:39:58,878 there's a couple of solutions to this I 12220 07:39:56,000 --> 07:40:00,840 could for instance somehow break out of 12221 07:39:58,878 --> 07:40:03,760 the code here but that's going to still 12222 07:40:00,840 --> 07:40:05,760 result in line 18 executing I could then 12223 07:40:03,760 --> 07:40:07,920 instead just return here I don't 12224 07:40:05,760 --> 07:40:09,840 strictly need to return one down at the 12225 07:40:07,920 --> 07:40:11,558 bottom but I made this claim last week 12226 07:40:09,840 --> 07:40:13,440 that it tends to be helpful as your 12227 07:40:11,558 --> 07:40:15,680 programs get more sophisticated to at 12228 07:40:13,440 --> 07:40:17,478 least signify just like a real world 12229 07:40:15,680 --> 07:40:20,200 programmer error codes when something 12230 07:40:17,478 --> 07:40:22,558 goes wrong so returning zero in main is 12231 07:40:20,200 --> 07:40:24,600 the easiest way to signify my code is 12232 07:40:22,558 --> 07:40:26,520 done I'm ready to exit successfully 12233 07:40:24,600 --> 07:40:28,160 that's it but down here I could 12234 07:40:26,520 --> 07:40:29,600 absolutely still return zero because 12235 07:40:28,160 --> 07:40:31,878 it's not a huge deal it's not really an 12236 07:40:29,600 --> 07:40:33,240 error that deserves annoying the user 12237 07:40:31,878 --> 07:40:35,318 with some kind of popup that something 12238 07:40:33,240 --> 07:40:37,600 went wrong but return one is just a 12239 07:40:35,318 --> 07:40:39,200 lower level way of signaling H it didn't 12240 07:40:37,600 --> 07:40:41,558 really find what I was looking for and 12241 07:40:39,200 --> 07:40:44,478 remember from last week you can see this 12242 07:40:41,558 --> 07:40:46,160 as follows if I recompile this again now 12243 07:40:44,478 --> 07:40:50,638 that I've reverted those changes so make 12244 07:40:46,160 --> 07:40:53,440 search and if I do uh /search and search 12245 07:40:50,638 --> 07:40:55,000 for Battleship which is indeed found 12246 07:40:53,440 --> 07:40:57,160 recall I can execute this magical 12247 07:40:55,000 --> 07:40:58,638 command Echo dollar sign question mark 12248 07:40:57,160 --> 07:41:02,240 which you're not going to often execute 12249 07:40:58,638 --> 07:41:05,200 but it shows you what main returned if I 12250 07:41:02,240 --> 07:41:08,080 run search again and search for race car 12251 07:41:05,200 --> 07:41:09,638 which is not found I see not found but I 12252 07:41:08,080 --> 07:41:12,000 can also run this command again and see 12253 07:41:09,638 --> 07:41:13,520 that oh it returned one so now if you 12254 07:41:12,000 --> 07:41:15,040 fast forward a few months a few years 12255 07:41:13,520 --> 07:41:17,200 when you're actually writing code in a 12256 07:41:15,040 --> 07:41:19,280 company or for larger projects you might 12257 07:41:17,200 --> 07:41:20,878 want to be automating software you might 12258 07:41:19,280 --> 07:41:23,600 not want the human to necessarily be 12259 07:41:20,878 --> 07:41:25,638 running it manually you might want um 12260 07:41:23,600 --> 07:41:27,680 code to be automated by some nightly 12261 07:41:25,638 --> 07:41:30,958 process or something like that using 12262 07:41:27,680 --> 07:41:33,840 these exit codes can a program determine 12263 07:41:30,958 --> 07:41:38,080 yes or no that other code succeeded or 12264 07:41:33,840 --> 07:41:39,958 failed other questions on linear search 12265 07:41:38,080 --> 07:41:42,240 in this 12266 07:41:39,958 --> 07:41:47,000 way no all right well let's translate 12267 07:41:42,240 --> 07:41:49,440 this to one other feature of uh C here 12268 07:41:47,000 --> 07:41:51,280 by incorporating these ideas now into 12269 07:41:49,440 --> 07:41:54,558 one other program so I'm going to create 12270 07:41:51,280 --> 07:41:56,798 a phone book in C by doing code space 12271 07:41:54,558 --> 07:41:56,798 phone 12272 07:41:57,798 --> 07:42:02,160 book.car a phone book for an actual name 12273 07:42:00,478 --> 07:42:03,638 and getting back a number so I'm going 12274 07:42:02,160 --> 07:42:05,878 to go ahead and quickly include some of 12275 07:42:03,638 --> 07:42:09,120 the same things cs50.h so we can get 12276 07:42:05,878 --> 07:42:10,920 input uh standard io. so we can print 12277 07:42:09,120 --> 07:42:12,878 output and I'm going to preemptively 12278 07:42:10,920 --> 07:42:16,160 include string.h in case we need that 12279 07:42:12,878 --> 07:42:18,200 one as well uh int main void no need for 12280 07:42:16,160 --> 07:42:20,600 uh command line arguments today and let 12281 07:42:18,200 --> 07:42:23,240 me me give myself now an array of names 12282 07:42:20,600 --> 07:42:25,718 for this phone book so string names 12283 07:42:23,240 --> 07:42:27,440 equals and then in curly braces how 12284 07:42:25,718 --> 07:42:29,280 about Carter will be one person in the 12285 07:42:27,440 --> 07:42:30,798 phone book and David myself will be the 12286 07:42:29,280 --> 07:42:32,360 other so we'll keep it short so we don't 12287 07:42:30,798 --> 07:42:34,798 have to type too many names but this is 12288 07:42:32,360 --> 07:42:36,878 a phone book with two people thus far 12289 07:42:34,798 --> 07:42:38,798 suppose now we want to also store 12290 07:42:36,878 --> 07:42:40,478 Carter's phone number in mind so it's 12291 07:42:38,798 --> 07:42:42,798 not just saying found or not found it's 12292 07:42:40,478 --> 07:42:45,440 literally looking up our phone numbers 12293 07:42:42,798 --> 07:42:48,120 like a proper phone book well at the 12294 07:42:45,440 --> 07:42:49,840 moment there's really no way to do this 12295 07:42:48,120 --> 07:42:52,440 I could do something hackish like I 12296 07:42:49,840 --> 07:42:55,878 could put a number like 12297 07:42:52,440 --> 07:43:00,798 617495 1000 after Carter I could maybe 12298 07:42:55,878 --> 07:43:02,360 do something like 949 uh 468 2750 after 12299 07:43:00,798 --> 07:43:04,000 me but now you're kind of doing the 12300 07:43:02,360 --> 07:43:05,878 whole apples and oranges thing right 12301 07:43:04,000 --> 07:43:08,120 like now it's not strings it's a string 12302 07:43:05,878 --> 07:43:10,558 int string int all right so maybe I 12303 07:43:08,120 --> 07:43:12,558 could just make all of these strings but 12304 07:43:10,558 --> 07:43:14,200 now it's just a conceptual mixing of 12305 07:43:12,558 --> 07:43:15,840 apples and oranges like yes that's an 12306 07:43:14,200 --> 07:43:17,440 array of four strings but now you're on 12307 07:43:15,840 --> 07:43:19,360 the honor System to know that the first 12308 07:43:17,440 --> 07:43:21,398 string is a name the second string is a 12309 07:43:19,360 --> 07:43:23,520 number the third string is like you can 12310 07:43:21,398 --> 07:43:26,360 do it but it's a bit of a hack so to 12311 07:43:23,520 --> 07:43:28,160 speak so what might be cleaner than this 12312 07:43:26,360 --> 07:43:30,160 instead of combining our phone numbers 12313 07:43:28,160 --> 07:43:33,360 into the same array as our names what 12314 07:43:30,160 --> 07:43:36,840 else might we do that's perhaps a little 12315 07:43:33,360 --> 07:43:40,398 better say a little 12316 07:43:36,840 --> 07:43:41,440 Lou a 2d array uh possibly something we 12317 07:43:40,398 --> 07:43:42,718 could do I'm going to keep it even 12318 07:43:41,440 --> 07:43:44,638 simpler now because we haven't used 12319 07:43:42,718 --> 07:43:47,120 those by name even though that is we saw 12320 07:43:44,638 --> 07:43:48,478 last week technically what argv is what 12321 07:43:47,120 --> 07:43:50,200 else could I do if I want to store names 12322 07:43:48,478 --> 07:43:51,920 and numbers 12323 07:43:50,200 --> 07:43:53,398 yeahp yeah let me go with this 12324 07:43:51,920 --> 07:43:54,958 suggestion just it's a little simpler 12325 07:43:53,398 --> 07:43:56,600 rather than complicate things in 12326 07:43:54,958 --> 07:43:59,760 literally different dimensions let me go 12327 07:43:56,600 --> 07:44:02,280 ahead and do string well I could do int 12328 07:43:59,760 --> 07:44:05,318 numbers but you know what so that we can 12329 07:44:02,280 --> 07:44:07,240 support punctuation like dashes or even 12330 07:44:05,318 --> 07:44:08,558 parentheses or country codes I'm going 12331 07:44:07,240 --> 07:44:11,280 to do this instead I'm going to do 12332 07:44:08,558 --> 07:44:13,398 string numbers so that I can represent 12333 07:44:11,280 --> 07:44:17,160 Carter's number as quote unquote plus 12334 07:44:13,398 --> 07:44:19,478 one for the US 617 495 1,000 complete 12335 07:44:17,160 --> 07:44:21,638 with hyphens as is us convention and 12336 07:44:19,478 --> 07:44:24,760 then for mine I'll go ahead and do + one 12337 07:44:21,638 --> 07:44:28,680 949 how about 468 12338 07:44:24,760 --> 07:44:30,120 275 semi colon and now down below let's 12339 07:44:28,680 --> 07:44:32,878 actually enable the user to search this 12340 07:44:30,120 --> 07:44:35,120 phone book just like in week zero we did 12341 07:44:32,878 --> 07:44:37,200 string name equals get string and let's 12342 07:44:35,120 --> 07:44:39,318 ask the user for a name presumably David 12343 07:44:37,200 --> 07:44:41,878 or Carter or someone else and now let's 12344 07:44:39,318 --> 07:44:45,398 re-implement linear search so four in I 12345 07:44:41,878 --> 07:44:47,798 gets zero I is less than two and do as I 12346 07:44:45,398 --> 07:44:49,920 say not as I do I think we should beware 12347 07:44:47,798 --> 07:44:53,798 this coding but we'll keep it simple for 12348 07:44:49,920 --> 07:44:55,360 now i++ and then in this for loop I 12349 07:44:53,798 --> 07:44:58,520 think we have all of the ingredients to 12350 07:44:55,360 --> 07:45:02,318 solve this so if the return value of 12351 07:44:58,520 --> 07:45:04,878 stir compare of all of the names bracket 12352 07:45:02,318 --> 07:45:07,638 I comparing against the name that the 12353 07:45:04,878 --> 07:45:09,680 human typed in if all of that equals 12354 07:45:07,638 --> 07:45:11,520 equals zero that is all of the 12355 07:45:09,680 --> 07:45:14,080 characters in those two strings are 12356 07:45:11,520 --> 07:45:16,000 equal then I think we can go ahead and 12357 07:45:14,080 --> 07:45:18,160 say found just like last time but you 12358 07:45:16,000 --> 07:45:20,440 know what let's actually print Carter or 12359 07:45:18,160 --> 07:45:24,280 my phone number so found percent s and 12360 07:45:20,440 --> 07:45:26,240 we'll plug in numbers bracket I and then 12361 07:45:24,280 --> 07:45:28,718 just for consistency I'll return zero 12362 07:45:26,240 --> 07:45:31,478 here and down here how about I'll say 12363 07:45:28,718 --> 07:45:34,600 something like uh print F not found just 12364 07:45:31,478 --> 07:45:36,638 to be clear and then I'll return one as 12365 07:45:34,600 --> 07:45:39,000 well so just a recap here's all of the 12366 07:45:36,638 --> 07:45:40,798 code it's almost the same as before 12367 07:45:39,000 --> 07:45:42,760 except now it's useful I'm not just 12368 07:45:40,798 --> 07:45:45,200 saying found or not found I found a 12369 07:45:42,760 --> 07:45:48,160 number in Monopoly or I found a a piece 12370 07:45:45,200 --> 07:45:49,520 in Monopoly I'm looking up in one array 12371 07:45:48,160 --> 07:45:52,000 of the strings and then I'm printing 12372 07:45:49,520 --> 07:45:56,398 from the other array the answer so let 12373 07:45:52,000 --> 07:45:58,920 me go ahead here and run the compiler 12374 07:45:56,398 --> 07:46:01,680 make phone book enter okay that's 12375 07:45:58,920 --> 07:46:04,280 promising no errors do/ phonebook now 12376 07:46:01,680 --> 07:46:06,520 and let's search for for instance Carter 12377 07:46:04,280 --> 07:46:07,798 enter all right so we found Carter's 12378 07:46:06,520 --> 07:46:10,240 number all right let me do that again 12379 07:46:07,798 --> 07:46:11,520 phone book Let's search for David all 12380 07:46:10,240 --> 07:46:12,680 right we seem to have found David's 12381 07:46:11,520 --> 07:46:14,680 number all right let's do it one last 12382 07:46:12,680 --> 07:46:17,680 time phone book enter and now we'll 12383 07:46:14,680 --> 07:46:19,040 search for like John Harvard enter not 12384 07:46:17,680 --> 07:46:21,200 found 12385 07:46:19,040 --> 07:46:23,440 all right so I dare say albeit with 12386 07:46:21,200 --> 07:46:25,478 minimal testing this code is 12387 07:46:23,440 --> 07:46:27,798 correct would anyone now like to 12388 07:46:25,478 --> 07:46:30,080 critique the design does something rub 12389 07:46:27,798 --> 07:46:32,558 you the wrong way perhaps about this 12390 07:46:30,080 --> 07:46:32,558 approach 12391 07:46:33,080 --> 07:46:37,798 here and as always think about how if 12392 07:46:36,360 --> 07:46:39,638 the program maybe gets longer more 12393 07:46:37,798 --> 07:46:42,440 complicated how decisions like this 12394 07:46:39,638 --> 07:46:45,958 might unfold 12395 07:46:42,440 --> 07:46:48,280 yeah okay so if I is less than two so 12396 07:46:45,958 --> 07:46:49,398 technically I if I change the number of 12397 07:46:48,280 --> 07:46:50,920 people in this phone book I'm going to 12398 07:46:49,398 --> 07:46:52,398 have to update I and we've already seen 12399 07:46:50,920 --> 07:46:55,040 that I get myself into trouble so that's 12400 07:46:52,398 --> 07:46:55,040 bad design 12401 07:47:02,878 --> 07:47:07,600 goodes yeah so again I'm sort of 12402 07:47:05,360 --> 07:47:09,680 trusting myself not to screw up if I add 12403 07:47:07,600 --> 07:47:11,760 John or anyone else to the first array 12404 07:47:09,680 --> 07:47:13,600 but I forget to add their number to the 12405 07:47:11,760 --> 07:47:15,440 second array you know eventually things 12406 07:47:13,600 --> 07:47:17,318 are going to drift and be inconsistent 12407 07:47:15,440 --> 07:47:18,920 and then the just code will be incorrect 12408 07:47:17,318 --> 07:47:21,600 at that point so so sort of a poor 12409 07:47:18,920 --> 07:47:25,958 design setting me up for future failure 12410 07:47:21,600 --> 07:47:25,958 if you will other thoughts 12411 07:47:31,318 --> 07:47:36,080 yeah yeah really good we're assuming the 12412 07:47:33,718 --> 07:47:38,440 same order from left to right the names 12413 07:47:36,080 --> 07:47:40,040 go and from left to right the numbers go 12414 07:47:38,440 --> 07:47:41,478 but that's kind of just the honor System 12415 07:47:40,040 --> 07:47:43,520 like there's literally nothing in code 12416 07:47:41,478 --> 07:47:45,638 preventing me from reversing the order 12417 07:47:43,520 --> 07:47:47,478 for whatever reason or maybe sorting the 12418 07:47:45,638 --> 07:47:49,398 names like they're sorted now and maybe 12419 07:47:47,478 --> 07:47:51,280 that's deliberate but maybe it's not so 12420 07:47:49,398 --> 07:47:52,958 this honor System here too is just not 12421 07:47:51,280 --> 07:47:55,478 good right I could put a comment in here 12422 07:47:52,958 --> 07:47:58,360 to remind myself to uh you know note to 12423 07:47:55,478 --> 07:47:59,718 self always update arrays the same way 12424 07:47:58,360 --> 07:48:00,920 but like that's something's going to 12425 07:47:59,718 --> 07:48:04,280 happen eventually especially when we 12426 07:48:00,920 --> 07:48:06,360 have not two but three but 30 300 names 12427 07:48:04,280 --> 07:48:08,840 and numbers it would be nice to keep all 12428 07:48:06,360 --> 07:48:10,638 of the related data together and so in 12429 07:48:08,840 --> 07:48:12,958 fact the one new feature of C we'll 12430 07:48:10,638 --> 07:48:16,040 introduce today is one that actually 12431 07:48:12,958 --> 07:48:18,600 allows us to implement our very own data 12432 07:48:16,040 --> 07:48:21,558 structures you can think of a raay as a 12433 07:48:18,600 --> 07:48:23,638 very lightweight data structure and that 12434 07:48:21,558 --> 07:48:25,478 allows you to Cluster related data back 12435 07:48:23,638 --> 07:48:27,718 to back to back to back and this is how 12436 07:48:25,478 --> 07:48:29,680 strings are implemented they are a data 12437 07:48:27,718 --> 07:48:31,958 structure effectively implemented with 12438 07:48:29,680 --> 07:48:33,840 an array but with c and with other 12439 07:48:31,958 --> 07:48:36,040 languages it turns out you can invent 12440 07:48:33,840 --> 07:48:37,600 your own data types whether they're one 12441 07:48:36,040 --> 07:48:42,360 dimensional two-dimensional even or 12442 07:48:37,600 --> 07:48:45,000 Beyond and with uh with C can you 12443 07:48:42,360 --> 07:48:47,120 specifically create your own types that 12444 07:48:45,000 --> 07:48:49,160 have their own name so for instance 12445 07:48:47,120 --> 07:48:52,760 wouldn't it have been nice if C came 12446 07:48:49,160 --> 07:48:55,798 with not just Char and int and float and 12447 07:48:52,760 --> 07:48:58,280 uh uh long and and others wouldn't it be 12448 07:48:55,798 --> 07:49:00,600 nice if C came with a data type called 12449 07:48:58,280 --> 07:49:02,318 person and ideally a person would have a 12450 07:49:00,600 --> 07:49:04,478 name and a number now that's a little 12451 07:49:02,318 --> 07:49:06,638 naive and unrealistic like why would 12452 07:49:04,478 --> 07:49:08,558 they define a person to have just those 12453 07:49:06,638 --> 07:49:10,398 two Fields certainly people could have 12454 07:49:08,558 --> 07:49:12,160 disagreed what a person is so they leave 12455 07:49:10,398 --> 07:49:14,160 it to us like the authors of C gave us 12456 07:49:12,160 --> 07:49:16,440 all of these Primitives inss and floats 12457 07:49:14,160 --> 07:49:17,878 and strings and so forth but it's up to 12458 07:49:16,440 --> 07:49:20,040 us now to use those in a more 12459 07:49:17,878 --> 07:49:23,840 interesting way so that we can create an 12460 07:49:20,040 --> 07:49:25,878 array of person variables if you will 12461 07:49:23,840 --> 07:49:28,080 inside of an array called people just to 12462 07:49:25,878 --> 07:49:29,798 pluralize it here so how are we going to 12463 07:49:28,080 --> 07:49:32,958 do this well for now let's just 12464 07:49:29,798 --> 07:49:34,718 stipulate that a person in the world 12465 07:49:32,958 --> 07:49:36,200 will have a name and a number that we 12466 07:49:34,718 --> 07:49:37,398 could argue all day long what else a 12467 07:49:36,200 --> 07:49:39,760 person should have and that's fine you 12468 07:49:37,398 --> 07:49:42,520 can invent your own person 12469 07:49:39,760 --> 07:49:44,798 eventually at the moment I'm using just 12470 07:49:42,520 --> 07:49:46,478 two variables to define a person's name 12471 07:49:44,798 --> 07:49:49,080 and number but wouldn't it be nice to 12472 07:49:46,478 --> 07:49:52,160 encapsulate that is combined these two 12473 07:49:49,080 --> 07:49:54,718 data types into a new and improved data 12474 07:49:52,160 --> 07:49:56,760 type called person and the Syntax for 12475 07:49:54,718 --> 07:49:59,200 that is going to be this so it's a bit 12476 07:49:56,760 --> 07:50:00,920 of a mouthful but you can perhaps infer 12477 07:49:59,200 --> 07:50:02,878 what some of this is doing here so it 12478 07:50:00,920 --> 07:50:04,760 turns out C has a keyword called type 12479 07:50:02,878 --> 07:50:07,440 def as the name kind of suggests this 12480 07:50:04,760 --> 07:50:09,760 allows you to Define your own type 12481 07:50:07,440 --> 07:50:12,120 struct is an indication that it's a 12482 07:50:09,760 --> 07:50:14,160 structure it's like a a structure that 12483 07:50:12,120 --> 07:50:16,200 has multiple values inside of it that 12484 07:50:14,160 --> 07:50:17,600 you are trying to Define and then at the 12485 07:50:16,200 --> 07:50:19,680 very bottom here outside of the Curve 12486 07:50:17,600 --> 07:50:22,240 early braces is the name of the type 12487 07:50:19,680 --> 07:50:24,240 that you want to create so you don't 12488 07:50:22,240 --> 07:50:25,878 have discretion over using type def or 12489 07:50:24,240 --> 07:50:27,558 struct in this particular case but you 12490 07:50:25,878 --> 07:50:30,040 can name the thing whatever you want and 12491 07:50:27,558 --> 07:50:32,160 you can put anything in the structure 12492 07:50:30,040 --> 07:50:34,600 that you want as well and as soon as 12493 07:50:32,160 --> 07:50:37,080 this semicolon is executed at the bottom 12494 07:50:34,600 --> 07:50:40,398 of the code every line thereafter can 12495 07:50:37,080 --> 07:50:43,360 now have access to a person data type 12496 07:50:40,398 --> 07:50:46,760 whether as a single variable or as an 12497 07:50:43,360 --> 07:50:49,000 entire array so if I want to build on 12498 07:50:46,760 --> 07:50:51,520 this then let me go ahead and do this 12499 07:50:49,000 --> 07:50:55,398 let me go back to my C code here and I'm 12500 07:50:51,520 --> 07:50:57,478 going to go ahead and uh change just a 12501 07:50:55,398 --> 07:50:59,478 couple of things let's go ahead and do 12502 07:50:57,478 --> 07:51:02,440 this I'm going to go ahead and first get 12503 07:50:59,478 --> 07:51:05,200 rid of those two hardcoded arrays and 12504 07:51:02,440 --> 07:51:09,280 let me go ahead and at the top of my 12505 07:51:05,200 --> 07:51:11,718 file invent this type so type def struct 12506 07:51:09,280 --> 07:51:14,120 inside of it will be a string name and 12507 07:51:11,718 --> 07:51:15,878 then a string number and then the name 12508 07:51:14,120 --> 07:51:17,760 of this structure will be person and 12509 07:51:15,878 --> 07:51:19,398 best practice would have me Define at 12510 07:51:17,760 --> 07:51:21,200 the very top of my file so that any of 12511 07:51:19,398 --> 07:51:23,638 my functions in fact could use it even 12512 07:51:21,200 --> 07:51:26,760 though I just have Main in this case now 12513 07:51:23,638 --> 07:51:30,080 if I want it I could do this like person 12514 07:51:26,760 --> 07:51:31,958 P1 and person P2 but we know from last 12515 07:51:30,080 --> 07:51:34,478 week like that already is bad design if 12516 07:51:31,958 --> 07:51:36,558 you want to have multiple instances of 12517 07:51:34,478 --> 07:51:39,280 the same type of variable we should 12518 07:51:36,558 --> 07:51:39,280 probably use what 12519 07:51:39,478 --> 07:51:44,718 instead and yeah an array so let me not 12520 07:51:43,000 --> 07:51:47,958 even go down that road let me instead 12521 07:51:44,718 --> 07:51:49,798 just do this person uh will be the type 12522 07:51:47,958 --> 07:51:51,600 of the array but I'm going to call it I 12523 07:51:49,798 --> 07:51:53,120 could call it persons but in English we 12524 07:51:51,600 --> 07:51:55,878 typically say people so I'll call the 12525 07:51:53,120 --> 07:51:57,200 array people and I want two people to 12526 07:51:55,878 --> 07:51:58,440 exist in this array though I could 12527 07:51:57,200 --> 07:52:02,160 certainly change that number to be 12528 07:51:58,440 --> 07:52:04,398 anything I want how now do you put a 12529 07:52:02,160 --> 07:52:06,160 name inside of a person and then put the 12530 07:52:04,398 --> 07:52:08,200 number inside of that same person well 12531 07:52:06,160 --> 07:52:10,240 slightly new syntax today I'm going to 12532 07:52:08,200 --> 07:52:12,718 go ahead and say this people bracket 12533 07:52:10,240 --> 07:52:14,878 zero just gives me the first person in 12534 07:52:12,718 --> 07:52:18,398 the array that's not new but if you want 12535 07:52:14,878 --> 07:52:21,040 to go inside of that person in memory 12536 07:52:18,398 --> 07:52:23,840 you use a DOT and then you just specify 12537 07:52:21,040 --> 07:52:25,680 the name of the attribute therein so if 12538 07:52:23,840 --> 07:52:27,718 I want to set the first person's name to 12539 07:52:25,680 --> 07:52:29,160 Carter I just use that so-called dot 12540 07:52:27,718 --> 07:52:31,638 notation and then if I want to set 12541 07:52:29,160 --> 07:52:33,478 Carter's number using dot notation I 12542 07:52:31,638 --> 07:52:36,558 would do this plus one 12543 07:52:33,478 --> 07:52:38,840 617495 1000 and then if I want to do the 12544 07:52:36,558 --> 07:52:40,000 same for myself I would now do people 12545 07:52:38,840 --> 07:52:42,878 bracket 12546 07:52:40,000 --> 07:52:46,200 1name equals quote unquote David and 12547 07:52:42,878 --> 07:52:48,840 then people bracket one still number 12548 07:52:46,200 --> 07:52:51,120 equals quote unquote + 1 12549 07:52:48,840 --> 07:52:54,600 949468 12550 07:52:51,120 --> 07:52:56,798 2750 and now at the bottom of my file I 12551 07:52:54,600 --> 07:53:00,280 think my logic can pretty much stay the 12552 07:52:56,798 --> 07:53:01,920 same I can still on this line here 12553 07:53:00,280 --> 07:53:04,120 prompt the user for the name of the 12554 07:53:01,920 --> 07:53:05,520 person they want to look up for now even 12555 07:53:04,120 --> 07:53:06,760 though I admit it's not the best design 12556 07:53:05,520 --> 07:53:08,200 I'm just doing this for demonstration 12557 07:53:06,760 --> 07:53:10,558 sake I'm going to leave the two there 12558 07:53:08,200 --> 07:53:12,040 because I know I have two people but 12559 07:53:10,558 --> 07:53:14,440 down here this is going to have to 12560 07:53:12,040 --> 07:53:16,798 change I don't want to compare names 12561 07:53:14,440 --> 07:53:20,040 bracket I anymore what do I want to type 12562 07:53:16,798 --> 07:53:24,798 here as the first first argument to stir 12563 07:53:20,040 --> 07:53:28,478 compare what do I want to do here 12564 07:53:24,798 --> 07:53:31,280 yeah so people i. name yeah so I want to 12565 07:53:28,478 --> 07:53:32,958 go into the people array at the I 12566 07:53:31,280 --> 07:53:34,878 location because that's what my Loop is 12567 07:53:32,958 --> 07:53:37,120 doing it's updating I again and again 12568 07:53:34,878 --> 07:53:38,600 and then look at name and that's good I 12569 07:53:37,120 --> 07:53:40,360 think now I need to change this too what 12570 07:53:38,600 --> 07:53:42,878 do I want to print if the person is 12571 07:53:40,360 --> 07:53:45,600 found someone 12572 07:53:42,878 --> 07:53:49,280 else what do I want to print here if I 12573 07:53:45,600 --> 07:53:49,280 found the person's name 12574 07:53:49,318 --> 07:53:55,798 yeah say a little 12575 07:53:52,200 --> 07:53:56,840 louder perfect so people bracket. number 12576 07:53:55,798 --> 07:53:59,318 if indeed I want to print the 12577 07:53:56,840 --> 07:54:00,878 corresponding number to this person and 12578 07:53:59,318 --> 07:54:03,680 then I think the rest of my code can 12579 07:54:00,878 --> 07:54:06,120 stay the same so let me go ahead and run 12580 07:54:03,680 --> 07:54:08,760 make phonebook to recompile this version 12581 07:54:06,120 --> 07:54:11,040 so far so good/ phonebook let's go ahead 12582 07:54:08,760 --> 07:54:12,240 and type in Carter's name found all 12583 07:54:11,040 --> 07:54:14,638 right let's go ahead and run it again 12584 07:54:12,240 --> 07:54:16,200 David's name found let's go ahead and 12585 07:54:14,638 --> 07:54:18,760 run it one more time type in John 12586 07:54:16,200 --> 07:54:22,080 Harvard for instance not found in this 12587 07:54:18,760 --> 07:54:23,478 case so fundamentally the code isn't all 12588 07:54:22,080 --> 07:54:25,920 that different linear search is still 12589 07:54:23,478 --> 07:54:27,840 behaving the same way and I admit this 12590 07:54:25,920 --> 07:54:30,240 is kind of ugly looking like we've kind 12591 07:54:27,840 --> 07:54:32,680 of made a two-line solution like five 12592 07:54:30,240 --> 07:54:34,878 lines of code now but if we fast forward 12593 07:54:32,680 --> 07:54:37,280 a week or two when we start saving 12594 07:54:34,878 --> 07:54:40,318 information to files uh we'll introduce 12595 07:54:37,280 --> 07:54:42,280 you to files like CSV value CSV files 12596 07:54:40,318 --> 07:54:43,718 comma separated values or spreadsheet 12597 07:54:42,280 --> 07:54:45,558 files which you've surely opened on your 12598 07:54:43,718 --> 07:54:46,878 Mac or PC at some point in the past 12599 07:54:45,558 --> 07:54:48,878 suffice it to say We'll soon learn 12600 07:54:46,878 --> 07:54:51,360 Techni for storing information like 12601 07:54:48,878 --> 07:54:52,840 names and numbers in files and at that 12602 07:54:51,360 --> 07:54:54,440 point we're not going to do any of this 12603 07:54:52,840 --> 07:54:56,240 hackish sort of hardcoding of the number 12604 07:54:54,440 --> 07:54:57,478 two and manually typing my name and 12605 07:54:56,240 --> 07:54:59,318 Carter's name and number into our 12606 07:54:57,478 --> 07:55:01,040 program we'll read the information 12607 07:54:59,318 --> 07:55:02,760 dynamically from a file and in a few 12608 07:55:01,040 --> 07:55:05,680 weeks we'll read it dynamically from a 12609 07:55:02,760 --> 07:55:07,718 database instead but this is for now 12610 07:55:05,680 --> 07:55:10,558 just syntactically how we can create an 12611 07:55:07,718 --> 07:55:12,840 array of size two containing one person 12612 07:55:10,558 --> 07:55:14,440 each we can update the name and number 12613 07:55:12,840 --> 07:55:15,958 of the first person update the name and 12614 07:55:14,440 --> 07:55:18,878 the number of the second person and then 12615 07:55:15,958 --> 07:55:20,638 later search across those names and 12616 07:55:18,878 --> 07:55:23,240 print out the corresponding numbers and 12617 07:55:20,638 --> 07:55:26,600 in this sense this is a better design 12618 07:55:23,240 --> 07:55:29,240 why because my person data type 12619 07:55:26,600 --> 07:55:31,360 encapsulates now everything that it 12620 07:55:29,240 --> 07:55:33,398 means to be a person at least in this 12621 07:55:31,360 --> 07:55:36,000 narrow world and if I want to add 12622 07:55:33,398 --> 07:55:37,638 something to the notion of a person for 12623 07:55:36,000 --> 07:55:40,240 instance I could go up to my typed Def 12624 07:55:37,638 --> 07:55:42,638 and tomorrow add an address to every 12625 07:55:40,240 --> 07:55:44,200 person and start reading that in as well 12626 07:55:42,638 --> 07:55:47,200 and now it's not the honor System it's 12627 07:55:44,200 --> 07:55:49,160 not a names array a numbers array and a 12628 07:55:47,200 --> 07:55:51,280 addesses array and everything else you 12629 07:55:49,160 --> 07:55:53,360 might imagine related to a person it's 12630 07:55:51,280 --> 07:55:57,200 all encapsulated which is a a term of 12631 07:55:53,360 --> 07:55:58,520 art inside of the same type reminiscent 12632 07:55:57,200 --> 07:55:59,920 if some of you have programmed before of 12633 07:55:58,520 --> 07:56:02,558 something called objectoriented 12634 07:55:59,920 --> 07:56:06,318 programming but we're not there yet C is 12635 07:56:02,558 --> 07:56:09,240 not that questions on this use of struct 12636 07:56:06,318 --> 07:56:12,878 or this new syntax the dot operator 12637 07:56:09,240 --> 07:56:16,080 being really the juicy part 12638 07:56:12,878 --> 07:56:19,080 here any questions 12639 07:56:16,080 --> 07:56:19,080 yeah 12640 07:56:21,718 --> 07:56:28,280 on what line 12641 07:56:24,120 --> 07:56:30,080 number 16 so yes so syntactically we 12642 07:56:28,280 --> 07:56:32,360 introduced the square brackets last week 12643 07:56:30,080 --> 07:56:34,280 so doing people bracket zero just means 12644 07:56:32,360 --> 07:56:36,200 go to the first person in the array that 12645 07:56:34,280 --> 07:56:38,360 was like when Stephanie literally opened 12646 07:56:36,200 --> 07:56:39,920 this door that's uh that's doors bracket 12647 07:56:38,360 --> 07:56:42,398 zero but this is of course people 12648 07:56:39,920 --> 07:56:44,920 bracket zero instead today the dot is a 12649 07:56:42,398 --> 07:56:47,558 new piece of syntax it means go inside 12650 07:56:44,920 --> 07:56:50,280 of that person in memory 12651 07:56:47,558 --> 07:56:51,760 and look at the name therein and set it 12652 07:56:50,280 --> 07:56:53,680 equal to Carter and do the same for 12653 07:56:51,760 --> 07:56:55,798 number so that's all it's like open the 12654 07:56:53,680 --> 07:56:59,558 locker door go inside of it and check or 12655 07:56:55,798 --> 07:56:59,558 set the name and the number 12656 07:57:07,200 --> 07:57:12,920 yeah attributes is fine uh good question 12657 07:57:10,478 --> 07:57:15,360 in the struct can you set default values 12658 07:57:12,920 --> 07:57:17,120 short answer no and this is where C 12659 07:57:15,360 --> 07:57:19,080 becomes less featurable than more more 12660 07:57:17,120 --> 07:57:21,318 modern languages like Python and Java 12661 07:57:19,080 --> 07:57:23,200 and others where you can in fact do that 12662 07:57:21,318 --> 07:57:24,798 so when we transition to python in a few 12663 07:57:23,200 --> 07:57:26,638 weeks time we'll see how we can start 12664 07:57:24,798 --> 07:57:28,638 solving problems like that but for now 12665 07:57:26,638 --> 07:57:32,520 it's up to you to initialize name and 12666 07:57:28,638 --> 07:57:32,520 number to something 12667 07:57:42,558 --> 07:57:46,600 yeah really good question how can we 12668 07:57:44,958 --> 07:57:47,478 adjust or critique the design of what 12669 07:57:46,600 --> 07:57:49,398 I'm doing 12670 07:57:47,478 --> 07:57:51,680 this is one of the few situations where 12671 07:57:49,398 --> 07:57:54,600 I would say hypocritically do as I say 12672 07:57:51,680 --> 07:57:56,718 not as I do I am using pretty ugly lines 12673 07:57:54,600 --> 07:57:58,920 like this just to introduce the syntax 12674 07:57:56,718 --> 07:58:00,478 but my claim pedagogically today is that 12675 07:57:58,920 --> 07:58:02,440 eventually when we start storing names 12676 07:58:00,478 --> 07:58:04,398 and numbers or other things in files or 12677 07:58:02,440 --> 07:58:06,840 in databases you won't have this 12678 07:58:04,398 --> 07:58:08,398 redundancy you'll have one line of code 12679 07:58:06,840 --> 07:58:10,638 or two lines of code that read the 12680 07:58:08,398 --> 07:58:13,160 information from the file or database 12681 07:58:10,638 --> 07:58:14,798 and then fill the entire array with that 12682 07:58:13,160 --> 07:58:16,520 data for now I'm just doing it manually 12683 07:58:14,798 --> 07:58:19,520 so is to keep our Focus only on the new 12684 07:58:16,520 --> 07:58:22,318 syntax but that's it so forgive the bad 12685 07:58:19,520 --> 07:58:23,638 Design By Design today other questions 12686 07:58:22,318 --> 07:58:26,718 on 12687 07:58:23,638 --> 07:58:27,760 this all right that's been a lot already 12688 07:58:26,718 --> 07:58:29,440 why don't we go ahead and take our 12689 07:58:27,760 --> 07:58:31,120 10-minute break with snacks first we 12690 07:58:29,440 --> 07:58:35,638 have some delightful brownies in the 12691 07:58:31,120 --> 07:58:37,398 lobby all right we are back and up until 12692 07:58:35,638 --> 07:58:39,920 now it clearly seems to be a good thing 12693 07:58:37,398 --> 07:58:42,000 if your data is sorted because you can 12694 07:58:39,920 --> 07:58:43,520 use binary search you know a little more 12695 07:58:42,000 --> 07:58:46,120 some little something more about the 12696 07:58:43,520 --> 07:58:49,600 data uh but it turns out that sorting of 12697 07:58:46,120 --> 07:58:53,280 itself is kind of a problem to solve too 12698 07:58:49,600 --> 07:58:55,240 and you might think well if sorting is 12699 07:58:53,280 --> 07:58:56,920 going to be pretty fast we absolutely 12700 07:58:55,240 --> 07:58:58,040 should do it before we start searching 12701 07:58:56,920 --> 07:59:00,280 because that'll just speed up all of our 12702 07:58:58,040 --> 07:59:02,080 searches but if sorting is slow that 12703 07:59:00,280 --> 07:59:04,200 kind of invites the question well should 12704 07:59:02,080 --> 07:59:06,200 we bother sorting our data if we're only 12705 07:59:04,200 --> 07:59:08,318 going to search the data maybe once 12706 07:59:06,200 --> 07:59:10,080 maybe twice and so here is going to be 12707 07:59:08,318 --> 07:59:11,760 potentially a trade-off so let's 12708 07:59:10,080 --> 07:59:13,160 consider what it means really to sort 12709 07:59:11,760 --> 07:59:15,840 data in our case it's just going to be 12710 07:59:13,160 --> 07:59:17,318 simple and use numbers but it might in 12711 07:59:15,840 --> 07:59:19,760 the case of the Googles of the World be 12712 07:59:17,318 --> 07:59:21,478 actual web pages or persons or the like 12713 07:59:19,760 --> 07:59:25,120 so here is our typical picture for 12714 07:59:21,478 --> 07:59:27,280 sorting input uh for solving any problem 12715 07:59:25,120 --> 07:59:30,558 input at left and output at right the 12716 07:59:27,280 --> 07:59:33,680 input to our sort problem is going to be 12717 07:59:30,558 --> 07:59:35,760 uh some unsorted set of values and the 12718 07:59:33,680 --> 07:59:37,840 output ideally will be the same set of 12719 07:59:35,760 --> 07:59:39,840 values sorted and if we do this 12720 07:59:37,840 --> 07:59:41,920 concretely let's suppose that we want to 12721 07:59:39,840 --> 07:59:45,638 go about sorting this list of numbers 7 12722 07:59:41,920 --> 07:59:47,840 2 5 4 1 6 03 so it's all of the numbers 12723 07:59:45,638 --> 07:59:49,798 from 0 to 7 but they're somehow jumbled 12724 07:59:47,840 --> 07:59:51,200 up randomly that's going to be the input 12725 07:59:49,798 --> 07:59:53,440 to the problem and the goal is now to 12726 07:59:51,200 --> 07:59:57,840 sort those so that you indeed get out 0 12727 07:59:53,440 --> 07:59:59,638 1 2 3 4 5 67 instead so it turns out 12728 07:59:57,840 --> 08:00:02,718 there's lots of different ways we can 12729 07:59:59,638 --> 08:00:04,478 actually sort numbers like these here um 12730 08:00:02,718 --> 08:00:06,920 and in fact just to complement our 12731 08:00:04,478 --> 08:00:08,878 search example earlier could we perhaps 12732 08:00:06,920 --> 08:00:10,200 quickly get some eight volunteers to 12733 08:00:08,878 --> 08:00:12,760 come up if you're comfortable appearing 12734 08:00:10,200 --> 08:00:17,160 on the internet if you want to do one 12735 08:00:12,760 --> 08:00:20,798 okay uh two three four five 6 7 eight 12736 08:00:17,160 --> 08:00:20,798 how about all right come on 12737 08:00:22,958 --> 08:00:27,878 down all 12738 08:00:25,840 --> 08:00:30,280 right come on over here and I'll give 12739 08:00:27,878 --> 08:00:32,680 you each a number and if you want to 12740 08:00:30,280 --> 08:00:35,318 start to organize yourselves in the same 12741 08:00:32,680 --> 08:00:38,760 order you see the numbers on the 12742 08:00:35,318 --> 08:00:40,520 board okay so look up on the overhead 12743 08:00:38,760 --> 08:00:42,478 and organize yourselves from left to 12744 08:00:40,520 --> 08:00:44,680 right in that same 12745 08:00:42,478 --> 08:00:46,200 order and let's have the first of you 12746 08:00:44,680 --> 08:00:47,160 perfect if you want to come uh right 12747 08:00:46,200 --> 08:00:49,360 over here 12748 08:00:47,160 --> 08:00:51,878 how about right in line with this all 12749 08:00:49,360 --> 08:00:54,680 right and a few more 12750 08:00:51,878 --> 08:00:57,040 numbers all right number 12751 08:00:54,680 --> 08:00:59,878 two six 12752 08:00:57,040 --> 08:01:02,398 and perfect just the right number all 12753 08:00:59,878 --> 08:01:04,318 right no uhoh all right there we go 12754 08:01:02,398 --> 08:01:09,638 number three all right so let's just do 12755 08:01:04,318 --> 08:01:11,080 a quick check we have 7 2 5 4 1 603 very 12756 08:01:09,638 --> 08:01:12,240 good so far do you want to just scooch a 12757 08:01:11,080 --> 08:01:14,360 little this way just to make a little 12758 08:01:12,240 --> 08:01:16,840 more of room all right and let's 12759 08:01:14,360 --> 08:01:18,520 consider now who we have here on stage 12760 08:01:16,840 --> 08:01:21,160 you want to each say a quick hello to 12761 08:01:18,520 --> 08:01:23,760 the audience hi my name is Ryan uh I'm a 12762 08:01:21,160 --> 08:01:26,200 first year from Penny 12763 08:01:23,760 --> 08:01:29,120 Packer hi my name is Cel I'm a first 12764 08:01:26,200 --> 08:01:30,520 year at Strauss um hi my name is Lucy 12765 08:01:29,120 --> 08:01:32,958 I'm a first year from 12766 08:01:30,520 --> 08:01:33,878 greo hi my name is Shiloh I'm a first 12767 08:01:32,958 --> 08:01:36,000 year in 12768 08:01:33,878 --> 08:01:38,120 Wigglesworth hi my name is Jack and I'm 12769 08:01:36,000 --> 08:01:40,398 a first year in 12770 08:01:38,120 --> 08:01:43,040 St hi my name is Katherine I'm a first 12771 08:01:40,398 --> 08:01:45,798 year in stra hi my name is Michael and 12772 08:01:43,040 --> 08:01:48,080 my first year at Penny Packer hi my name 12773 08:01:45,798 --> 08:01:51,200 is Muhammad at my first here in massuse 12774 08:01:48,080 --> 08:01:53,080 nice welcome aboard all right so let's 12775 08:01:51,200 --> 08:01:55,440 consider now how we might go about 12776 08:01:53,080 --> 08:01:58,040 sorting Our Kind volunteers here the 12777 08:01:55,440 --> 08:02:00,080 goal being to get them into order from 12778 08:01:58,040 --> 08:02:01,718 uh smallest to largest so that 12779 08:02:00,080 --> 08:02:03,200 presumably then we can use something 12780 08:02:01,718 --> 08:02:05,360 smarter than just linear search we could 12781 08:02:03,200 --> 08:02:07,558 actually use binary search assuming that 12782 08:02:05,360 --> 08:02:08,760 they are already then sorted so let me 12783 08:02:07,558 --> 08:02:10,000 propose that we first consider an 12784 08:02:08,760 --> 08:02:12,718 algorithm that actually has a name 12785 08:02:10,000 --> 08:02:15,080 called selection sort and selection sort 12786 08:02:12,718 --> 08:02:16,798 is going to be one that literally has me 12787 08:02:15,080 --> 08:02:19,120 or really you as the programmer 12788 08:02:16,798 --> 08:02:21,558 selecting the smallest element again and 12789 08:02:19,120 --> 08:02:23,360 again and then putting them into the 12790 08:02:21,558 --> 08:02:25,478 appropriate place so let me go ahead and 12791 08:02:23,360 --> 08:02:27,080 start this here uh starting with the 12792 08:02:25,478 --> 08:02:28,440 number seven at the moment seven is the 12793 08:02:27,080 --> 08:02:30,240 smallest number I found so I'm going to 12794 08:02:28,440 --> 08:02:31,920 make mental note of that with a mental 12795 08:02:30,240 --> 08:02:33,520 variable if you will I'm going to move 12796 08:02:31,920 --> 08:02:34,920 on now oh number two is obviously 12797 08:02:33,520 --> 08:02:36,600 smaller so I'm just going to update my 12798 08:02:34,920 --> 08:02:38,760 mental reminder that two is now the 12799 08:02:36,600 --> 08:02:41,638 smallest effectively forgetting for now 12800 08:02:38,760 --> 08:02:43,600 number seven uh five not smaller four 12801 08:02:41,638 --> 08:02:45,920 not smaller one smaller and I'm going to 12802 08:02:43,600 --> 08:02:47,958 make mental note of that six not smaller 12803 08:02:45,920 --> 08:02:49,600 zero smaller I'll make mental note of 12804 08:02:47,958 --> 08:02:52,040 that having forgotten now everything 12805 08:02:49,600 --> 08:02:53,760 else and now number three is not smaller 12806 08:02:52,040 --> 08:02:56,080 so what's your name again Michael so 12807 08:02:53,760 --> 08:02:57,958 Michael is number zero he belongs of 12808 08:02:56,080 --> 08:03:02,718 course way down there but unfortunately 12809 08:02:57,958 --> 08:03:04,398 you are Ryan Ryan Ryan is in the way so 12810 08:03:02,718 --> 08:03:05,558 what should we do how should we start to 12811 08:03:04,398 --> 08:03:08,558 sort this 12812 08:03:05,558 --> 08:03:12,638 list where should number zero 12813 08:03:08,558 --> 08:03:12,638 go yeah do you want to say the 12814 08:03:13,200 --> 08:03:16,958 L yeah so let's just go ahead and swap 12815 08:03:15,520 --> 08:03:19,040 so if you want to go ahead and zero go 12816 08:03:16,958 --> 08:03:20,920 on where seven is we need to make room 12817 08:03:19,040 --> 08:03:22,600 for number seven it would kind of be 12818 08:03:20,920 --> 08:03:24,920 cheating if maybe everyone kind of 12819 08:03:22,600 --> 08:03:26,120 politely stepped over to the side why 12820 08:03:24,920 --> 08:03:27,840 because if we imagine all of our 12821 08:03:26,120 --> 08:03:29,638 volunteers here to be in Array like 12822 08:03:27,840 --> 08:03:31,878 that's a crazy amount of work to have 12823 08:03:29,638 --> 08:03:33,478 every element in the array shift to the 12824 08:03:31,878 --> 08:03:35,558 left just to make room so we're going to 12825 08:03:33,478 --> 08:03:37,440 keep it simple and just evict whoever is 12826 08:03:35,558 --> 08:03:39,000 there now maybe we get lucky and number 12827 08:03:37,440 --> 08:03:41,080 seven is actually closer to its 12828 08:03:39,000 --> 08:03:42,680 destination maybe we get unlucky and it 12829 08:03:41,080 --> 08:03:45,160 goes farther away but we've at least 12830 08:03:42,680 --> 08:03:47,200 solved one problem if we had n problems 12831 08:03:45,160 --> 08:03:49,120 at first now we have n minus one because 12832 08:03:47,200 --> 08:03:51,240 number zero is indeed in the right place 12833 08:03:49,120 --> 08:03:53,520 so if I continue to act this out let me 12834 08:03:51,240 --> 08:03:56,240 go ahead and say two okay currently the 12835 08:03:53,520 --> 08:04:00,520 smallest five no four no one currently 12836 08:03:56,240 --> 08:04:02,680 the smallest I'll make mental note 6 7 3 12837 08:04:00,520 --> 08:04:05,520 and now let me pause one is obviously 12838 08:04:02,680 --> 08:04:07,798 the now smallest element so did I need 12839 08:04:05,520 --> 08:04:10,000 to keep going well it turns out at least 12840 08:04:07,798 --> 08:04:12,040 as I've defined selection sort I do need 12841 08:04:10,000 --> 08:04:13,798 to keep going because I only claim that 12842 08:04:12,040 --> 08:04:15,638 I'm using one variable in my mind to 12843 08:04:13,798 --> 08:04:17,318 remember the then smallest element I'm 12844 08:04:15,638 --> 08:04:19,398 not smart enough like us humans to 12845 08:04:17,318 --> 08:04:20,878 remember oh wait a minute one is 12846 08:04:19,398 --> 08:04:22,840 definitely the smallest now I don't have 12847 08:04:20,878 --> 08:04:24,520 that whole recollection so I just am 12848 08:04:22,840 --> 08:04:26,600 keeping track of the now smallest so 12849 08:04:24,520 --> 08:04:29,200 number one what your name was Jack Jack 12850 08:04:26,600 --> 08:04:31,318 where should jack go probably there and 12851 08:04:29,200 --> 08:04:33,240 what's your name itel itel okay so Jack 12852 08:04:31,318 --> 08:04:35,840 and itel if you want to swap places 12853 08:04:33,240 --> 08:04:37,478 we've now solved two of the end total 12854 08:04:35,840 --> 08:04:39,318 problems and now we'll do it a little 12855 08:04:37,478 --> 08:04:41,280 faster if each of you want to sort of 12856 08:04:39,318 --> 08:04:44,478 start to swap as I find the right person 12857 08:04:41,280 --> 08:04:47,120 so five smallest four is smaller two is 12858 08:04:44,478 --> 08:04:49,440 smaller got to keep checking okay two 12859 08:04:47,120 --> 08:04:51,120 was smaller all right now I'm going to 12860 08:04:49,440 --> 08:04:53,920 go back to the beginning all right four 12861 08:04:51,120 --> 08:04:55,760 is small five is not six is not seven is 12862 08:04:53,920 --> 08:04:58,718 oh three is small where do you want to 12863 08:04:55,760 --> 08:05:00,318 go okay good I'm going to go back here 12864 08:04:58,718 --> 08:05:02,000 and I can be a little smart I don't have 12865 08:05:00,318 --> 08:05:03,878 to go all the way to the end because I 12866 08:05:02,000 --> 08:05:05,920 know these folks are already sorted so I 12867 08:05:03,878 --> 08:05:08,040 can at least optimize slightly so now 12868 08:05:05,920 --> 08:05:09,718 five is small six is small seven is four 12869 08:05:08,040 --> 08:05:11,040 four is smaller if you want to go in 12870 08:05:09,718 --> 08:05:13,080 place 12871 08:05:11,040 --> 08:05:15,280 there and now here things get 12872 08:05:13,080 --> 08:05:16,600 interesting I can optimize by not 12873 08:05:15,280 --> 08:05:18,958 looking at these folks anymore more cuz 12874 08:05:16,600 --> 08:05:21,878 they're obviously problem solved but now 12875 08:05:18,958 --> 08:05:24,120 five is small six is not seven is not 12876 08:05:21,878 --> 08:05:25,600 okay five you can stay where you are now 12877 08:05:24,120 --> 08:05:27,760 a human in the room is obviously going 12878 08:05:25,600 --> 08:05:29,840 to question why I'm wasting any more 12879 08:05:27,760 --> 08:05:32,240 time but with selection sort as I've 12880 08:05:29,840 --> 08:05:35,280 defined it thus far I still have to now 12881 08:05:32,240 --> 08:05:37,360 check six is smallest not seven and now 12882 08:05:35,280 --> 08:05:39,080 my final step okay they're all in place 12883 08:05:37,360 --> 08:05:40,920 so here too is this dichotomy between 12884 08:05:39,080 --> 08:05:42,558 what we all have is this bird's eye view 12885 08:05:40,920 --> 08:05:44,200 of the whole problem where it's obvious 12886 08:05:42,558 --> 08:05:46,318 where everyone needs to go but a 12887 08:05:44,200 --> 08:05:48,120 computer implementing this with an array 12888 08:05:46,318 --> 08:05:49,760 really has to be more methodical and 12889 08:05:48,120 --> 08:05:51,558 we're actually saving a step here if we 12890 08:05:49,760 --> 08:05:53,280 were really doing this none of these 12891 08:05:51,558 --> 08:05:54,958 numbers would be visible all eight of 12892 08:05:53,280 --> 08:05:56,920 our volunteers would be inside of a 12893 08:05:54,958 --> 08:05:58,878 locked door and only then could we see 12894 08:05:56,920 --> 08:06:01,160 them one at a time but we're focusing 12895 08:05:58,878 --> 08:06:02,360 now just on the Sorting aspect so let me 12896 08:06:01,160 --> 08:06:04,600 just before we do one other 12897 08:06:02,360 --> 08:06:07,040 demonstration here proposed that what I 12898 08:06:04,600 --> 08:06:10,878 really just did here in pseudo code was 12899 08:06:07,040 --> 08:06:13,160 something like this 4 I from 0er to n 12900 08:06:10,878 --> 08:06:15,280 minus one keeping in mind that zero is 12901 08:06:13,160 --> 08:06:17,040 always the left of the array n minus one 12902 08:06:15,280 --> 08:06:19,360 is always the right end of of the array 12903 08:06:17,040 --> 08:06:21,878 for I from 0 to N minus1 I found the 12904 08:06:19,360 --> 08:06:24,718 smallest number between numbers bracket 12905 08:06:21,878 --> 08:06:26,520 I and numbers bracket n minus one and 12906 08:06:24,718 --> 08:06:28,240 that's the very geeky way of expressing 12907 08:06:26,520 --> 08:06:30,558 this optimization I'm always starting 12908 08:06:28,240 --> 08:06:32,360 from numbers bracket I wherever I am and 12909 08:06:30,558 --> 08:06:33,920 then everything else to the right and 12910 08:06:32,360 --> 08:06:36,600 that's what was allowing me to ignore 12911 08:06:33,920 --> 08:06:38,760 the already sorted volunteers if though 12912 08:06:36,600 --> 08:06:41,120 my last line says swap smallest number 12913 08:06:38,760 --> 08:06:42,680 with numbers I I think that implements 12914 08:06:41,120 --> 08:06:45,200 what our humans were doing by physically 12915 08:06:42,680 --> 08:06:46,958 walking to another spot all right so 12916 08:06:45,200 --> 08:06:49,398 that then would what we'll call 12917 08:06:46,958 --> 08:06:51,200 selection sort let's go ahead and take a 12918 08:06:49,398 --> 08:06:52,318 second approach here using an algorithm 12919 08:06:51,200 --> 08:06:53,878 that I'm going to call bubble sort but 12920 08:06:52,318 --> 08:06:55,280 to do this we need you all to reset to 12921 08:06:53,878 --> 08:06:56,520 your original locations we have a little 12922 08:06:55,280 --> 08:06:59,040 cheat sheet on the board if you'd like 12923 08:06:56,520 --> 08:07:00,280 to go back to this position here and let 12924 08:06:59,040 --> 08:07:02,000 me take a fundamentally different 12925 08:07:00,280 --> 08:07:03,638 approach because I'm not really liking 12926 08:07:02,000 --> 08:07:05,920 selection sort as is because it's kind 12927 08:07:03,638 --> 08:07:07,760 of a lot of walking back and forth and a 12928 08:07:05,920 --> 08:07:09,840 lot of walking suggests like a lot of 12929 08:07:07,760 --> 08:07:12,200 lot of steps again and again so what 12930 08:07:09,840 --> 08:07:13,680 might I do instead well bubble sort is 12931 08:07:12,200 --> 08:07:15,638 going to have me focus a little more 12932 08:07:13,680 --> 08:07:17,080 intuitively on just smaller problems and 12933 08:07:15,638 --> 08:07:19,160 let's see if this gets me somewhere else 12934 08:07:17,080 --> 08:07:21,318 so if I just look at this list without 12935 08:07:19,160 --> 08:07:23,360 looking at everyone else seven and two 12936 08:07:21,318 --> 08:07:25,240 this is obviously a problem why because 12937 08:07:23,360 --> 08:07:27,160 you're out of order so let's just solve 12938 08:07:25,240 --> 08:07:29,638 one tiny problem first so seven and two 12939 08:07:27,160 --> 08:07:31,160 why don't you swap I know two is in a 12940 08:07:29,638 --> 08:07:33,318 better place now because she's 12941 08:07:31,160 --> 08:07:35,680 definitely lower uh less than seven so I 12942 08:07:33,318 --> 08:07:37,798 think I can now move on seven and five 12943 08:07:35,680 --> 08:07:40,398 problem so let's solve that seven and 12944 08:07:37,798 --> 08:07:42,600 four problem let's solve that s and one 12945 08:07:40,398 --> 08:07:44,638 let's solve that 7 and six let's solve 12946 08:07:42,600 --> 08:07:48,280 that 7 and zero solve that 7 and three 12947 08:07:44,638 --> 08:07:49,718 solve that okay done sorted right all 12948 08:07:48,280 --> 08:07:51,280 obviously not if you just glance at 12949 08:07:49,718 --> 08:07:52,920 these numbers here but we have 12950 08:07:51,280 --> 08:07:55,520 fundamentally taken a bite out of the 12951 08:07:52,920 --> 08:07:58,280 problem seven is indeed in the right 12952 08:07:55,520 --> 08:08:02,120 place so we maximally have n minus one 12953 08:07:58,280 --> 08:08:03,878 other problems to solve so how do I do 12954 08:08:02,120 --> 08:08:06,680 this I think I can just repeat the same 12955 08:08:03,878 --> 08:08:10,280 logic let me go over here two and five 12956 08:08:06,680 --> 08:08:13,878 good five and four no five and one no 12957 08:08:10,280 --> 08:08:17,638 five and six yes 6 and zero no six and 12958 08:08:13,878 --> 08:08:18,920 three no so so now we've solved two of 12959 08:08:17,638 --> 08:08:20,558 the problems and what's nice about 12960 08:08:18,920 --> 08:08:22,360 Bubble sword at least as this glance 12961 08:08:20,558 --> 08:08:24,080 it's nice and simple it's nice and local 12962 08:08:22,360 --> 08:08:26,080 and you just keep incrementally solving 12963 08:08:24,080 --> 08:08:27,958 more and more problems so let's go ahead 12964 08:08:26,080 --> 08:08:30,360 and do this again and I'll do it we can 12965 08:08:27,958 --> 08:08:33,600 do it faster two and four we know are 12966 08:08:30,360 --> 08:08:37,440 good four and one four and five five and 12967 08:08:33,600 --> 08:08:40,798 zero five and three five and six six and 12968 08:08:37,440 --> 08:08:43,958 seven good so we go back two and one ah 12969 08:08:40,798 --> 08:08:46,600 now another problem solve two and four 12970 08:08:43,958 --> 08:08:48,798 four and zero four and three 12971 08:08:46,600 --> 08:08:51,240 four and five five and six six and seven 12972 08:08:48,798 --> 08:08:53,280 and so notice too as per its name the 12973 08:08:51,240 --> 08:08:54,840 largest elements have bubbled their way 12974 08:08:53,280 --> 08:08:56,520 up to the top and that's what seems to 12975 08:08:54,840 --> 08:08:58,240 be happening just as we're fixing some 12976 08:08:56,520 --> 08:09:01,760 remaining problems so almost done one 12977 08:08:58,240 --> 08:09:04,398 and two two and zero two and three three 12978 08:09:01,760 --> 08:09:07,240 and four four and five five and six six 12979 08:09:04,398 --> 08:09:09,958 and seven almost done obviously to us 12980 08:09:07,240 --> 08:09:12,000 humans it looks done how do I know as 12981 08:09:09,958 --> 08:09:14,040 the computer for sure what would be the 12982 08:09:12,000 --> 08:09:17,000 most Surefire way for me to now oh it's 12983 08:09:14,040 --> 08:09:19,798 not done sorry uh that's a bug 12984 08:09:17,000 --> 08:09:21,200 okay one and zero okay one and two two 12985 08:09:19,798 --> 08:09:22,680 and three three and four three four and 12986 08:09:21,200 --> 08:09:25,360 five five and six six and seven okay so 12987 08:09:22,680 --> 08:09:28,360 now it's obviously sorted to the rest of 12988 08:09:25,360 --> 08:09:29,840 us on stage how could I confirm as much 12989 08:09:28,360 --> 08:09:31,958 as code right you're doing it with your 12990 08:09:29,840 --> 08:09:33,638 mind just glancing at this how would the 12991 08:09:31,958 --> 08:09:36,240 computer the code know for sure that 12992 08:09:33,638 --> 08:09:39,558 this list is now sorted 12993 08:09:36,240 --> 08:09:42,040 yeah let's do one more time and look uh 12994 08:09:39,558 --> 08:09:42,040 draw what 12995 08:09:43,558 --> 08:09:47,878 conclusion yeah let's do it one more 12996 08:09:45,558 --> 08:09:50,040 time even though it's a little wasteful 12997 08:09:47,878 --> 08:09:52,360 but logically if I go through the whole 12998 08:09:50,040 --> 08:09:54,798 list comparing pairs again again and 12999 08:09:52,360 --> 08:09:56,558 again and I don't do any work that time 13000 08:09:54,798 --> 08:09:58,360 now it's obviously logically safe to 13001 08:09:56,558 --> 08:09:59,920 just stop because otherwise I'm wasting 13002 08:09:58,360 --> 08:10:02,600 my time doing the same thing again and 13003 08:09:59,920 --> 08:10:03,878 again if no one's actually moving so I'm 13004 08:10:02,600 --> 08:10:05,120 afraid we don't have Monopoly games for 13005 08:10:03,878 --> 08:10:06,558 all of you but we do have eight stress 13006 08:10:05,120 --> 08:10:07,878 balls and a round of applause if we 13007 08:10:06,558 --> 08:10:09,478 could for our 13008 08:10:07,878 --> 08:10:11,798 volunteers if you want to put your 13009 08:10:09,478 --> 08:10:15,080 numbers on the Shelf 13010 08:10:11,798 --> 08:10:17,160 there so if we consider for a moment 13011 08:10:15,080 --> 08:10:20,040 thank you thank you so 13012 08:10:17,160 --> 08:10:24,160 much 13013 08:10:20,040 --> 08:10:26,878 sure thank you thanks sure so if we 13014 08:10:24,160 --> 08:10:29,318 consider now these two algorithms which 13015 08:10:26,878 --> 08:10:31,600 one is better any intuition for whether 13016 08:10:29,318 --> 08:10:33,878 selection sort the first is better or 13017 08:10:31,600 --> 08:10:36,240 worse than bubble sort the 13018 08:10:33,878 --> 08:10:39,240 second any 13019 08:10:36,240 --> 08:10:39,240 thoughts 13020 08:10:41,040 --> 08:10:45,798 yeah okay so bubbl swort seemed like 13021 08:10:44,240 --> 08:10:47,798 less work especially since I was 13022 08:10:45,798 --> 08:10:49,440 focusing on those localized problems 13023 08:10:47,798 --> 08:10:52,558 other 13024 08:10:49,440 --> 08:10:55,160 intuition selection sort versus bubble 13025 08:10:52,558 --> 08:10:56,878 sort well let me propose that we try to 13026 08:10:55,160 --> 08:10:58,680 like quantize this so we can actually 13027 08:10:56,878 --> 08:11:00,798 analyze it in some way and this is not 13028 08:10:58,680 --> 08:11:02,160 an exercise we'll do constantly for lots 13029 08:11:00,798 --> 08:11:04,040 of algorithms but these are pretty 13030 08:11:02,160 --> 08:11:06,000 representative of algorithms so we can 13031 08:11:04,040 --> 08:11:07,478 wrap our minds around indeed the 13032 08:11:06,000 --> 08:11:09,798 performance or the design of these 13033 08:11:07,478 --> 08:11:12,760 things so here is my pseudo code for 13034 08:11:09,798 --> 08:11:14,878 selection sort whereby as it's as per 13035 08:11:12,760 --> 08:11:17,478 its name I just iteratively select the 13036 08:11:14,878 --> 08:11:19,798 next smallest element El again and again 13037 08:11:17,478 --> 08:11:21,600 so how can we go about analyzing 13038 08:11:19,798 --> 08:11:23,638 something like this well we could just 13039 08:11:21,600 --> 08:11:25,840 do it on paper pencil and count up the 13040 08:11:23,638 --> 08:11:28,000 number of steps that seem to be implied 13041 08:11:25,840 --> 08:11:29,958 logically by the code we could literally 13042 08:11:28,000 --> 08:11:31,878 comp count like the number of steps I 13043 08:11:29,958 --> 08:11:33,440 was taking again and again left to right 13044 08:11:31,878 --> 08:11:36,000 we could also just com uh count the 13045 08:11:33,440 --> 08:11:37,680 number of comparisons I was making with 13046 08:11:36,000 --> 08:11:39,000 each of the persons involved and I was 13047 08:11:37,680 --> 08:11:41,160 doing it kind of quickly in selection 13048 08:11:39,000 --> 08:11:42,520 sort but every time I was looking at a 13049 08:11:41,160 --> 08:11:44,000 person trying to decide do I want to 13050 08:11:42,520 --> 08:11:46,040 remember that number is small as that 13051 08:11:44,000 --> 08:11:48,080 number I was comparing two values with 13052 08:11:46,040 --> 08:11:49,878 an equals equals or less than or greater 13053 08:11:48,080 --> 08:11:52,040 than sign at least if we had done this 13054 08:11:49,878 --> 08:11:54,000 in code so that tends to be the norm 13055 08:11:52,040 --> 08:11:55,520 when analyzing algorithms like these 13056 08:11:54,000 --> 08:11:58,600 counting the number of comparisons 13057 08:11:55,520 --> 08:12:00,080 because it's kind of a global uh it's 13058 08:11:58,600 --> 08:12:01,600 kind of a global unit of measure we can 13059 08:12:00,080 --> 08:12:05,318 use to compare different algorithms 13060 08:12:01,600 --> 08:12:07,440 entirely so think too that in the 13061 08:12:05,318 --> 08:12:09,240 general case when we have more than 13062 08:12:07,440 --> 08:12:11,440 eight volunteers more than seven doors 13063 08:12:09,240 --> 08:12:13,240 we can generalize our our array in 13064 08:12:11,440 --> 08:12:15,680 general as this is the first element at 13065 08:12:13,240 --> 08:12:18,958 bracket zero and the end of it is always 13066 08:12:15,680 --> 08:12:21,360 n minus one so arrays uh or doors in 13067 08:12:18,958 --> 08:12:23,958 this case or volunteers are always 13068 08:12:21,360 --> 08:12:26,200 numerically indexed from zero on up to n 13069 08:12:23,958 --> 08:12:28,520 minus one if there's n of them in total 13070 08:12:26,200 --> 08:12:31,520 so how do we analyze the code of 13071 08:12:28,520 --> 08:12:34,680 selection sort well how many steps did 13072 08:12:31,520 --> 08:12:36,798 it take me to find the first smallest 13073 08:12:34,680 --> 08:12:38,398 element or more precisely how many 13074 08:12:36,798 --> 08:12:40,920 comparisons did I need to make when I 13075 08:12:38,398 --> 08:12:44,000 walked left to right to find our first 13076 08:12:40,920 --> 08:12:47,040 smallest person which ended up being 13077 08:12:44,000 --> 08:12:48,798 zero how many comparisons did I do when 13078 08:12:47,040 --> 08:12:50,280 walking left to right if there were 13079 08:12:48,798 --> 08:12:53,760 eight people on 13080 08:12:50,280 --> 08:12:55,760 stage how many total comparisons did I 13081 08:12:53,760 --> 08:12:59,080 do like if there's eight people I 13082 08:12:55,760 --> 08:13:02,000 compared these folks then then like this 13083 08:12:59,080 --> 08:13:03,200 person this person yeah yeah so seven 13084 08:13:02,000 --> 08:13:04,878 total right because if there's eight 13085 08:13:03,200 --> 08:13:07,120 people on stage you can only do seven 13086 08:13:04,878 --> 08:13:08,760 comparisons total because otherwise 13087 08:13:07,120 --> 08:13:10,638 you'd be comparing one number to itself 13088 08:13:08,760 --> 08:13:12,798 so it seems like in the general case if 13089 08:13:10,638 --> 08:13:14,920 you've got n numbers that you're trying 13090 08:13:12,798 --> 08:13:17,398 to sort finding the smallest element 13091 08:13:14,920 --> 08:13:20,240 first takes n minus one comparisons 13092 08:13:17,398 --> 08:13:21,680 maybe n it's total steps left or right 13093 08:13:20,240 --> 08:13:23,718 but the number of comparisons which I 13094 08:13:21,680 --> 08:13:25,840 claim is just a useful unit of measure 13095 08:13:23,718 --> 08:13:28,440 is n minus one how about finding the 13096 08:13:25,840 --> 08:13:30,398 next smallest person how many steps did 13097 08:13:28,440 --> 08:13:33,798 it take me to find the next smallest 13098 08:13:30,398 --> 08:13:33,798 number which ended up being the number 13099 08:13:33,878 --> 08:13:38,718 one 13100 08:13:36,318 --> 08:13:40,440 yeah yeah so just n minus two why 13101 08:13:38,718 --> 08:13:41,558 because I'd already solved one problem 13102 08:13:40,440 --> 08:13:42,958 someone was already in the right 13103 08:13:41,558 --> 08:13:44,718 position it would be silly to keep 13104 08:13:42,958 --> 08:13:46,200 counting them again and again so I can 13105 08:13:44,718 --> 08:13:48,638 Whittle down my number of comparisons 13106 08:13:46,200 --> 08:13:50,080 for the next past n minus 2 the third 13107 08:13:48,638 --> 08:13:52,600 past to find the third smallest number 13108 08:13:50,080 --> 08:13:54,878 would be n minus 3 and then dot dot dot 13109 08:13:52,600 --> 08:13:56,958 presumably this story this formula ends 13110 08:13:54,878 --> 08:13:59,160 when you have just one final pair the 13111 08:13:56,958 --> 08:14:00,840 people at the end to compare so if this 13112 08:13:59,160 --> 08:14:02,958 is looking a little reminiscent of some 13113 08:14:00,840 --> 08:14:04,360 kind of recurrence from high school or 13114 08:14:02,958 --> 08:14:05,840 high school math or physics or the like 13115 08:14:04,360 --> 08:14:07,878 let me just stipulate that if you 13116 08:14:05,840 --> 08:14:12,240 actually do out this math and generalize 13117 08:14:07,878 --> 08:14:13,600 it that is the same thing as n * nus1 / 13118 08:14:12,240 --> 08:14:15,558 2 and if you're Rusty on that no big 13119 08:14:13,600 --> 08:14:17,718 deal just kind of commit to memory that 13120 08:14:15,558 --> 08:14:18,878 anytime you add up this kind of series 13121 08:14:17,718 --> 08:14:20,440 something plus something slightly 13122 08:14:18,878 --> 08:14:22,000 smaller plus something slightly smaller 13123 08:14:20,440 --> 08:14:25,600 Each of which differs by one you're 13124 08:14:22,000 --> 08:14:27,878 going to get this formula n * n -1/ 2 if 13125 08:14:25,600 --> 08:14:30,120 we of course uh uh multiply that out 13126 08:14:27,878 --> 08:14:32,080 that's really n^ 2 minus n all divided 13127 08:14:30,120 --> 08:14:36,160 by two if we keep multiplying it out 13128 08:14:32,080 --> 08:14:38,558 that's n^2 / 2 minus n /2 and now we 13129 08:14:36,160 --> 08:14:40,920 have kind of a vocabulary with which we 13130 08:14:38,558 --> 08:14:42,878 can talk about the efficiency the design 13131 08:14:40,920 --> 08:14:44,398 of this algorithm but honestly I don't 13132 08:14:42,878 --> 08:14:49,120 really care about this level of 13133 08:14:44,398 --> 08:14:52,120 precision like n^2 / 2us n / 2 as n gets 13134 08:14:49,120 --> 08:14:54,120 really large which of these symbols 13135 08:14:52,120 --> 08:14:56,280 which of these terms is really going to 13136 08:14:54,120 --> 08:14:58,398 dominate become the biggest influencer 13137 08:14:56,280 --> 08:15:00,718 on the total value of 13138 08:14:58,398 --> 08:15:02,280 steps right it's the square right like 13139 08:15:00,718 --> 08:15:03,840 it's definitely not n divided by two 13140 08:15:02,280 --> 08:15:06,040 that's shaving some time off but N 13141 08:15:03,840 --> 08:15:08,520 squared as n gets big is going to get 13142 08:15:06,040 --> 08:15:10,920 really big if n is 100 then n squar is 13143 08:15:08,520 --> 08:15:12,920 bigger if N is a million n squ is really 13144 08:15:10,920 --> 08:15:15,080 bigger and so at the end of the day when 13145 08:15:12,920 --> 08:15:17,000 we really just talking about sort of a 13146 08:15:15,080 --> 08:15:19,398 wave of of the hand analysis and upper 13147 08:15:17,000 --> 08:15:22,558 bound if you will let's just say that 13148 08:15:19,398 --> 08:15:25,040 selection sort as analyzed here it's on 13149 08:15:22,558 --> 08:15:27,160 the order of n s steps it's not 13150 08:15:25,040 --> 08:15:30,840 precisely n squar steps but you know 13151 08:15:27,160 --> 08:15:34,160 what n^2 divided two the intuition here 13152 08:15:30,840 --> 08:15:36,238 might be that well it's half of that you 13153 08:15:34,160 --> 08:15:38,558 n squ is what really matters as n gets 13154 08:15:36,238 --> 08:15:39,878 really really large and that's when you 13155 08:15:38,558 --> 08:15:41,440 start thinking about and trying to solve 13156 08:15:39,878 --> 08:15:42,920 the Google problems of the world when n 13157 08:15:41,440 --> 08:15:44,680 gets large that's when you have to be 13158 08:15:42,920 --> 08:15:47,280 smarter than just sort of naive 13159 08:15:44,680 --> 08:15:49,878 implementations of any algorithm so 13160 08:15:47,280 --> 08:15:52,360 where then does this algorithm fall into 13161 08:15:49,878 --> 08:15:55,000 this categorization here well n^2 it 13162 08:15:52,360 --> 08:15:57,718 turns out is on the order of n squ steps 13163 08:15:55,000 --> 08:16:01,000 in the worst case whether it's sorted or 13164 08:15:57,718 --> 08:16:04,318 not it turns out though lower bound if 13165 08:16:01,000 --> 08:16:06,160 we consider this same code suppose the 13166 08:16:04,318 --> 08:16:07,840 best case scenario like our eight 13167 08:16:06,160 --> 08:16:10,080 volunteers came up on stage and just 13168 08:16:07,840 --> 08:16:12,120 because they already sorted themselves 0 13169 08:16:10,080 --> 08:16:14,440 through seven suppose they just happen 13170 08:16:12,120 --> 08:16:15,798 to be in that state how many steps would 13171 08:16:14,440 --> 08:16:20,600 selection store 13172 08:16:15,798 --> 08:16:24,360 take to sort an already sorted list of 13173 08:16:20,600 --> 08:16:24,360 volunteers any intuition 13174 08:16:25,798 --> 08:16:31,478 yeah would it still be 13175 08:16:29,120 --> 08:16:33,798 n so for the first pass it would still 13176 08:16:31,478 --> 08:16:35,680 be seven for the first per uh pass 13177 08:16:33,798 --> 08:16:37,760 across the humans because even though 13178 08:16:35,680 --> 08:16:39,638 yeah I'm claiming zero is here I don't 13179 08:16:37,760 --> 08:16:41,558 know that zero is the smallest until I 13180 08:16:39,638 --> 08:16:44,160 make my way all the way over there doing 13181 08:16:41,558 --> 08:16:46,360 all seven comparisons okay fine first 13182 08:16:44,160 --> 08:16:48,318 pass took seven more generally n minus 13183 08:16:46,360 --> 08:16:50,398 one steps what if I look for the next 13184 08:16:48,318 --> 08:16:53,478 smallest element and the humans in this 13185 08:16:50,398 --> 08:16:55,680 story are already sorted 0 through 7 13186 08:16:53,478 --> 08:16:57,718 well yes the number one's here and I see 13187 08:16:55,680 --> 08:16:59,840 them first but I don't know they're the 13188 08:16:57,718 --> 08:17:01,718 smallest until I compare against 13189 08:16:59,840 --> 08:17:03,398 everyone else get to the end of the list 13190 08:17:01,718 --> 08:17:05,878 oh well that was stupid I already had 13191 08:17:03,398 --> 08:17:07,680 the smallest person in hand then and so 13192 08:17:05,878 --> 08:17:10,318 this pseudo code this implementation of 13193 08:17:07,680 --> 08:17:12,238 selection sort is sort of fixed like 13194 08:17:10,318 --> 08:17:15,238 this there's no special case that says 13195 08:17:12,238 --> 08:17:17,398 if already sorted quit early it's always 13196 08:17:15,238 --> 08:17:19,920 going to take n squ steps and so in this 13197 08:17:17,398 --> 08:17:23,440 case if we borrow our uh jargon from 13198 08:17:19,920 --> 08:17:26,000 earlier using Omega notation just to be 13199 08:17:23,440 --> 08:17:29,318 clear selection sword is also going to 13200 08:17:26,000 --> 08:17:31,080 be in this Incarnation on an Omega of n 13201 08:17:29,318 --> 08:17:32,840 s because even in the best case where 13202 08:17:31,080 --> 08:17:34,958 the list is already sorted you're going 13203 08:17:32,840 --> 08:17:37,080 to waste a huge amount of time 13204 08:17:34,958 --> 08:17:38,318 essentially verifying as much or 13205 08:17:37,080 --> 08:17:40,680 discovering as much even though we 13206 08:17:38,318 --> 08:17:42,638 humans of course could see it right away 13207 08:17:40,680 --> 08:17:45,280 so selection sort would seem to take 13208 08:17:42,638 --> 08:17:47,798 both N squared steps in the uh worst 13209 08:17:45,280 --> 08:17:49,080 case n s steps in the best case and so 13210 08:17:47,798 --> 08:17:50,878 you know what we can use our Theta 13211 08:17:49,080 --> 08:17:52,760 terminology for that here would be an 13212 08:17:50,878 --> 08:17:55,000 algorithm just like counting earlier 13213 08:17:52,760 --> 08:17:57,080 that always takes N squared steps no 13214 08:17:55,000 --> 08:17:59,160 matter whether the array is sorted or 13215 08:17:57,080 --> 08:18:00,558 not from the get-go all right so 13216 08:17:59,160 --> 08:18:02,760 hopefully we can do better and someone 13217 08:18:00,558 --> 08:18:04,878 proposed earlier that bubble sort felt 13218 08:18:02,760 --> 08:18:07,440 like it was using fewer steps well let's 13219 08:18:04,878 --> 08:18:10,478 consider that next with bubble sort we 13220 08:18:07,440 --> 08:18:12,238 had this pseudo code I claim whereby 13221 08:18:10,478 --> 08:18:16,040 let's focus on the inside of the code 13222 08:18:12,238 --> 08:18:19,718 first down here what was I doing for I 13223 08:18:16,040 --> 08:18:22,440 from 0 to n minus 2 that's curious we've 13224 08:18:19,718 --> 08:18:24,760 never seen n minus 2 before but I asked 13225 08:18:22,440 --> 08:18:28,040 this question if numbers bracket I and 13226 08:18:24,760 --> 08:18:30,478 numbers bracket I + 1 are out of order 13227 08:18:28,040 --> 08:18:32,200 swap them so that was when I was 13228 08:18:30,478 --> 08:18:34,200 pointing at our first two volunteers 13229 08:18:32,200 --> 08:18:37,080 here I saw that they were out of order 13230 08:18:34,200 --> 08:18:40,878 so I swapped them how come I'm doing 13231 08:18:37,080 --> 08:18:42,360 that again and again up to nus 2 though 13232 08:18:40,878 --> 08:18:44,718 instead of n 13233 08:18:42,360 --> 08:18:47,638 minus1 which we've always used up until 13234 08:18:44,718 --> 08:18:50,558 now as our rightmost 13235 08:18:47,638 --> 08:18:55,160 boundary any intuition for why I'm doing 13236 08:18:50,558 --> 08:18:55,160 this from 0 to n minus 2 13237 08:18:59,200 --> 08:19:05,120 yeah exactly because I'm looking at the 13238 08:19:02,398 --> 08:19:07,840 E person per this pseudo code here and 13239 08:19:05,120 --> 08:19:09,718 the E plus one person I better make sure 13240 08:19:07,840 --> 08:19:11,318 I don't go Step Beyond the at boundaries 13241 08:19:09,718 --> 08:19:13,440 of my array so if you think of like my 13242 08:19:11,318 --> 08:19:15,478 left hand when my back was two here 13243 08:19:13,440 --> 08:19:17,878 pointing at the current person at the 13244 08:19:15,478 --> 08:19:19,160 first position my right hand for this if 13245 08:19:17,878 --> 08:19:20,798 conditioner is essentially pointing at 13246 08:19:19,160 --> 08:19:23,000 the person next to them and you want to 13247 08:19:20,798 --> 08:19:24,520 iterate with your left hand all through 13248 08:19:23,000 --> 08:19:26,520 these people but you don't want your 13249 08:19:24,520 --> 08:19:28,040 left hand to point at the last person 13250 08:19:26,520 --> 08:19:30,398 you want it to point at the second to 13251 08:19:28,040 --> 08:19:33,600 last person but we know that the last 13252 08:19:30,398 --> 08:19:34,718 person is always at n minus one so the 13253 08:19:33,600 --> 08:19:37,040 second to last person just 13254 08:19:34,718 --> 08:19:39,000 mathematically is at n minus 2 so it's a 13255 08:19:37,040 --> 08:19:40,798 subtlety but this is like a Segal 13256 08:19:39,000 --> 08:19:43,280 waiting to happen if you implemented 13257 08:19:40,798 --> 08:19:45,120 bubble sort using n minus one you will 13258 08:19:43,280 --> 08:19:47,200 my right hand would go beyond the 13259 08:19:45,120 --> 08:19:49,318 boundaries of the array so just bad all 13260 08:19:47,200 --> 08:19:51,360 right so why am I saying this end times 13261 08:19:49,318 --> 08:19:55,238 Well we did it very organically with 13262 08:19:51,360 --> 08:19:57,160 humans but each time someone uh each 13263 08:19:55,238 --> 08:19:59,000 pass I did through the array someone 13264 08:19:57,160 --> 08:20:01,920 bubbled their way up to the end number 13265 08:19:59,000 --> 08:20:04,478 seven then number six then number five 13266 08:20:01,920 --> 08:20:07,638 so if on each pass through the array of 13267 08:20:04,478 --> 08:20:09,840 volunteers I was solving at Mo at least 13268 08:20:07,638 --> 08:20:12,238 one problem it seems like bubble sort 13269 08:20:09,840 --> 08:20:14,478 can just run n times total to solve all 13270 08:20:12,238 --> 08:20:15,798 n problems cuz the first pass will get 13271 08:20:14,478 --> 08:20:17,398 at least one one number into place 13272 08:20:15,798 --> 08:20:19,120 second pass second number into place you 13273 08:20:17,398 --> 08:20:20,760 might get lucky and it would do more but 13274 08:20:19,120 --> 08:20:23,120 worst case this feels like enough just 13275 08:20:20,760 --> 08:20:27,120 do this blindly end times and they'll 13276 08:20:23,120 --> 08:20:28,200 all line up together well technically 13277 08:20:27,120 --> 08:20:29,798 all right now we're getting into the 13278 08:20:28,200 --> 08:20:31,840 weeds technically you can just repeat it 13279 08:20:29,798 --> 08:20:33,398 n minus one times because if you solve 13280 08:20:31,840 --> 08:20:35,520 all n minus one other problems and 13281 08:20:33,398 --> 08:20:37,238 you're left with one like literally that 13282 08:20:35,520 --> 08:20:38,520 person's where they need to be just 13283 08:20:37,238 --> 08:20:39,680 logically if you've already sorted 13284 08:20:38,520 --> 08:20:41,760 everything else and you've got just the 13285 08:20:39,680 --> 08:20:43,840 one left it's already bubbled up so how 13286 08:20:41,760 --> 08:20:45,840 do we analyze this well in bubble sore 13287 08:20:43,840 --> 08:20:49,040 we might do something like this I'm 13288 08:20:45,840 --> 08:20:51,120 essentially doing n minus one Things N 13289 08:20:49,040 --> 08:20:52,280 minus one times now let me back up to 13290 08:20:51,120 --> 08:20:54,760 the pseudo code because this one's a 13291 08:20:52,280 --> 08:20:57,040 little less obvious this is where you 13292 08:20:54,760 --> 08:20:59,478 can actually mathematically infer from 13293 08:20:57,040 --> 08:21:01,760 your Loop uh how many steps you're 13294 08:20:59,478 --> 08:21:03,798 taking so this first line literally says 13295 08:21:01,760 --> 08:21:04,878 repeat the following n minus one times 13296 08:21:03,798 --> 08:21:06,520 so that's going to translate very 13297 08:21:04,878 --> 08:21:09,360 straightforwardly to our mathematical 13298 08:21:06,520 --> 08:21:11,478 formula do something n minus one times 13299 08:21:09,360 --> 08:21:13,920 this Loop just because I'm using four 13300 08:21:11,478 --> 08:21:15,680 Loop terminology it's framed a little 13301 08:21:13,920 --> 08:21:20,398 differently but but if you're iterating 13302 08:21:15,680 --> 08:21:23,398 from 0 to nus 2 you're iterating a total 13303 08:21:20,398 --> 08:21:24,680 of nus1 times and again the arithmetic 13304 08:21:23,398 --> 08:21:26,680 is getting a little annoying but this 13305 08:21:24,680 --> 08:21:29,718 just means do the following n minus one 13306 08:21:26,680 --> 08:21:32,440 times so do n minus one Things N minus 13307 08:21:29,718 --> 08:21:35,478 one times we can now uh run out the math 13308 08:21:32,440 --> 08:21:38,160 as follows we have the formula n minus1 13309 08:21:35,478 --> 08:21:42,360 * n minus1 we do our little foil method 13310 08:21:38,160 --> 08:21:45,520 here N2 -1 * N - 1 * n + 1 we can 13311 08:21:42,360 --> 08:21:47,318 combine like terms n^ 2 - 2 n plus one 13312 08:21:45,520 --> 08:21:49,238 but at this point when n gets really 13313 08:21:47,318 --> 08:21:51,440 large which term are we really going to 13314 08:21:49,238 --> 08:21:55,680 care about this is on the order 13315 08:21:51,440 --> 08:21:58,080 of yeah n s so at least ASM totically 13316 08:21:55,680 --> 08:22:00,398 ASM totically means as n approaches 13317 08:21:58,080 --> 08:22:02,080 Infinity gets really large turns out 13318 08:22:00,398 --> 08:22:04,238 that the upper bounds on selection sort 13319 08:22:02,080 --> 08:22:06,120 and bubble sort are essentially the same 13320 08:22:04,238 --> 08:22:07,520 now if we really nitpicked and compared 13321 08:22:06,120 --> 08:22:09,318 the total number of comparisons they 13322 08:22:07,520 --> 08:22:10,920 might differ slightly but as n gets 13323 08:22:09,318 --> 08:22:12,638 large honestly you're barely going to 13324 08:22:10,920 --> 08:22:15,360 notice the difference it would seem 13325 08:22:12,638 --> 08:22:18,718 between these two algorithms 13326 08:22:15,360 --> 08:22:20,478 but what about um the lower bound if the 13327 08:22:18,718 --> 08:22:24,280 upper bound on Bubble sword is also Big 13328 08:22:20,478 --> 08:22:26,840 O of n what about the lower bound here 13329 08:22:24,280 --> 08:22:29,238 well with this pseudo code what would 13330 08:22:26,840 --> 08:22:30,440 the lower bound be on bubble sort even 13331 08:22:29,238 --> 08:22:31,878 in the best case when all of the 13332 08:22:30,440 --> 08:22:34,680 volunteers are 13333 08:22:31,878 --> 08:22:36,718 sorted any 13334 08:22:34,680 --> 08:22:38,798 intuition in this pseudo code yeah in 13335 08:22:36,718 --> 08:22:38,798 the 13336 08:22:43,680 --> 08:22:46,840 middle 13337 08:22:45,238 --> 08:22:48,360 good question isn't bubble sorts 13338 08:22:46,840 --> 08:22:51,120 designed such that you wouldn't need to 13339 08:22:48,360 --> 08:22:54,040 compare numbers that have already uh 13340 08:22:51,120 --> 08:22:56,520 bubbled up that's what's happening here 13341 08:22:54,040 --> 08:22:59,080 in the middle implicitly I'm always 13342 08:22:56,520 --> 08:23:00,600 going from left to right but remember 13343 08:22:59,080 --> 08:23:01,878 that even when I screwed up at the end 13344 08:23:00,600 --> 08:23:04,080 and the last two people were out of 13345 08:23:01,878 --> 08:23:05,958 order I do always need to restart at the 13346 08:23:04,080 --> 08:23:07,238 beginning because the big numbers are 13347 08:23:05,958 --> 08:23:09,680 going that way and the small numbers are 13348 08:23:07,238 --> 08:23:09,680 coming this 13349 08:23:11,200 --> 08:23:15,200 way so that is true there are some 13350 08:23:13,478 --> 08:23:17,600 slight optimizations that I'm kind of 13351 08:23:15,200 --> 08:23:18,920 glossing over here let me stipulate that 13352 08:23:17,600 --> 08:23:20,440 it would still end up being on the order 13353 08:23:18,920 --> 08:23:23,120 of n squ but that would definitely shave 13354 08:23:20,440 --> 08:23:25,238 off some actual running time here but 13355 08:23:23,120 --> 08:23:26,798 what if the list is already sorted our 13356 08:23:25,238 --> 08:23:29,040 our pseudo code at the moment has no 13357 08:23:26,798 --> 08:23:31,398 allowance for if list is already sorted 13358 08:23:29,040 --> 08:23:33,840 quit early so we're going to blindly do 13359 08:23:31,398 --> 08:23:36,600 n minus one Things N minus one times 13360 08:23:33,840 --> 08:23:39,040 unless we modify our pseudo code as I 13361 08:23:36,600 --> 08:23:41,600 did verbally earlier I propose this 13362 08:23:39,040 --> 08:23:43,638 inside of that outer loop if you make a 13363 08:23:41,600 --> 08:23:45,878 pass across all of the volunteers and 13364 08:23:43,638 --> 08:23:47,238 your mental counter has made no swaps 13365 08:23:45,878 --> 08:23:48,958 you have to keep track with some kind of 13366 08:23:47,238 --> 08:23:50,638 variable well then you might as well 13367 08:23:48,958 --> 08:23:52,798 stop because if you do a whole pass and 13368 08:23:50,638 --> 08:23:55,680 make no swaps why would you waste time 13369 08:23:52,798 --> 08:23:59,440 doing it again expecting different 13370 08:23:55,680 --> 08:24:01,520 Behavior so to help visualize these 13371 08:23:59,440 --> 08:24:03,760 whereby now bubble sort can be 13372 08:24:01,520 --> 08:24:06,398 advantageous if the data is already 13373 08:24:03,760 --> 08:24:08,398 sorted or mostly sorted why because it 13374 08:24:06,398 --> 08:24:10,638 does have this short circuit detail at 13375 08:24:08,398 --> 08:24:13,760 least if we implement it like that how 13376 08:24:10,638 --> 08:24:15,558 can we go about um visualizing these 13377 08:24:13,760 --> 08:24:17,798 things a little more clearly well let me 13378 08:24:15,558 --> 08:24:20,120 go ahead and do this let me pull up here 13379 08:24:17,798 --> 08:24:22,318 a visualization of exactly these 13380 08:24:20,120 --> 08:24:24,080 algorithms thanks to a third party tool 13381 08:24:22,318 --> 08:24:25,840 here that's going to help us visualize 13382 08:24:24,080 --> 08:24:27,878 these sorting algorithms as follows 13383 08:24:25,840 --> 08:24:29,760 small bars represent small numbers big 13384 08:24:27,878 --> 08:24:32,680 bars represent big numbers and so the 13385 08:24:29,760 --> 08:24:34,440 idea now is when I hit a button here to 13386 08:24:32,680 --> 08:24:36,080 get all of the small bars this way all 13387 08:24:34,440 --> 08:24:37,798 of the big bars this way so just like 13388 08:24:36,080 --> 08:24:40,200 our volunteers but instead of holding 13389 08:24:37,798 --> 08:24:42,238 lighted numbers its bars representing 13390 08:24:40,200 --> 08:24:45,160 their magnitude so let's go ahead and 13391 08:24:42,238 --> 08:24:48,238 start with for instance select 13392 08:24:45,160 --> 08:24:50,958 sort and you'll see in pink is being 13393 08:24:48,238 --> 08:24:52,798 highlighted the current number that is 13394 08:24:50,958 --> 08:24:54,440 being selected and then pulled all the 13395 08:24:52,798 --> 08:24:58,200 way to the left so this is selection 13396 08:24:54,440 --> 08:25:00,520 sort and again it's selecting the next 13397 08:24:58,200 --> 08:25:03,958 smallest element but you can see here 13398 08:25:00,520 --> 08:25:06,600 all the more visibly that just like my 13399 08:25:03,958 --> 08:25:08,638 human feet were taking a lot of steps so 13400 08:25:06,600 --> 08:25:11,398 is this algorithm touching these 13401 08:25:08,638 --> 08:25:13,878 elements again and again and again and 13402 08:25:11,398 --> 08:25:15,440 this is why the N squ is really a thing 13403 08:25:13,878 --> 08:25:17,160 there's got to be some inherent 13404 08:25:15,440 --> 08:25:18,718 redundancy here like why do we keep 13405 08:25:17,160 --> 08:25:20,840 looking at the same darn elements again 13406 08:25:18,718 --> 08:25:22,638 and again we do in terms of our pseudo 13407 08:25:20,840 --> 08:25:24,798 code need to do so but it's this 13408 08:25:22,638 --> 08:25:27,798 redundant comparisons that kind of 13409 08:25:24,798 --> 08:25:29,798 explains why n s is indeed the case so 13410 08:25:27,798 --> 08:25:31,080 now it's done small bars here big bars 13411 08:25:29,798 --> 08:25:32,398 there and I had to just keep talking 13412 08:25:31,080 --> 08:25:34,718 there to kill time because it's 13413 08:25:32,398 --> 08:25:36,718 relatively slow well let me randomize 13414 08:25:34,718 --> 08:25:38,440 the array just so we start with a 13415 08:25:36,718 --> 08:25:40,680 different order and now let me click on 13416 08:25:38,440 --> 08:25:43,520 bubbl sore and you'll see similar idea 13417 08:25:40,680 --> 08:25:45,760 but different algorithm so now the two 13418 08:25:43,520 --> 08:25:47,878 bars in pink are the two that are being 13419 08:25:45,760 --> 08:25:49,478 compared and fixed potentially if 13420 08:25:47,878 --> 08:25:50,840 they're out of order and you can see 13421 08:25:49,478 --> 08:25:54,040 already that the biggest bars are 13422 08:25:50,840 --> 08:25:56,398 bubbling their way up to the top but now 13423 08:25:54,040 --> 08:25:58,478 you can also see like this redundancy 13424 08:25:56,398 --> 08:26:00,478 like we keep swooping through the list 13425 08:25:58,478 --> 08:26:02,718 again and again just like I kept walking 13426 08:26:00,478 --> 08:26:04,558 back and forth and this is n squar this 13427 08:26:02,718 --> 08:26:06,840 is not that many bars what 10 20 there's 13428 08:26:04,558 --> 08:26:09,120 like 40 or something bars I'm guessing 13429 08:26:06,840 --> 08:26:11,798 that's pretty slow already just to sort 13430 08:26:09,120 --> 08:26:13,280 40 numbers and I think it's going to get 13431 08:26:11,798 --> 08:26:15,160 tedious if I keep talking over this so 13432 08:26:13,280 --> 08:26:18,040 let's just assume that this two is 13433 08:26:15,160 --> 08:26:20,238 relatively slow had I gotten lucky and 13434 08:26:18,040 --> 08:26:22,200 the list were almost sorted already 13435 08:26:20,238 --> 08:26:24,040 bubble sort would have been pretty fast 13436 08:26:22,200 --> 08:26:26,200 but this was a truly random array so we 13437 08:26:24,040 --> 08:26:29,160 did not get lucky so indeed the worst 13438 08:26:26,200 --> 08:26:31,080 case might be what's kicking in here so 13439 08:26:29,160 --> 08:26:33,080 I don't I feel like it'll be 13440 08:26:31,080 --> 08:26:34,840 anticlimactic like holding in a sneeze 13441 08:26:33,080 --> 08:26:37,680 if I don't let you see the end of this 13442 08:26:34,840 --> 08:26:39,958 so here we go nothing interesting is 13443 08:26:37,680 --> 08:26:45,718 about to happen almost 13444 08:26:39,958 --> 08:26:47,160 done ah okay done all right so thank you 13445 08:26:45,718 --> 08:26:51,878 thank 13446 08:26:47,160 --> 08:26:53,878 you so still somewhat slow though how 13447 08:26:51,878 --> 08:26:56,238 though can we perhaps do a little better 13448 08:26:53,878 --> 08:26:58,160 fundamentally so we can do so if we 13449 08:26:56,238 --> 08:27:00,000 introduce yet another technique and this 13450 08:26:58,160 --> 08:27:01,558 one isn't so much a function of code as 13451 08:27:00,000 --> 08:27:04,200 it is concept and it's something that 13452 08:27:01,558 --> 08:27:06,680 you might have seen in the real world um 13453 08:27:04,200 --> 08:27:10,440 but perhaps not so obviously so so it 13454 08:27:06,680 --> 08:27:12,638 turns out in programming recursion 13455 08:27:10,440 --> 08:27:15,080 refers to the ability of a function to 13456 08:27:12,638 --> 08:27:17,360 call itself in the world of mathematics 13457 08:27:15,080 --> 08:27:19,520 if you have a function f if F appears on 13458 08:27:17,360 --> 08:27:21,398 both the left side and the right side of 13459 08:27:19,520 --> 08:27:23,040 a formula that would be a recursive 13460 08:27:21,398 --> 08:27:24,680 function in the math world too whenever 13461 08:27:23,040 --> 08:27:28,080 f is defined in terms of itself or in 13462 08:27:24,680 --> 08:27:31,280 our case in Compu in programming anytime 13463 08:27:28,080 --> 08:27:32,958 a function calls itself that function is 13464 08:27:31,280 --> 08:27:34,360 said to be recursive and this is 13465 08:27:32,958 --> 08:27:35,680 actually something we've seen already in 13466 08:27:34,360 --> 08:27:38,000 class even though we didn't call it as 13467 08:27:35,680 --> 08:27:41,360 much so for instance consider this 13468 08:27:38,000 --> 08:27:44,680 pseudo code um from earlier whereby this 13469 08:27:41,360 --> 08:27:47,080 was the pseudo code for searching via AR 13470 08:27:44,680 --> 08:27:49,478 search a whole bunch of doors if no 13471 08:27:47,080 --> 08:27:51,600 doors are left return false that was the 13472 08:27:49,478 --> 08:27:53,280 the additional conditional we added but 13473 08:27:51,600 --> 08:27:55,558 then if number behind middle door return 13474 08:27:53,280 --> 08:27:58,520 true and here's the interesting part if 13475 08:27:55,558 --> 08:28:00,840 number is less than middle door search 13476 08:27:58,520 --> 08:28:03,000 the left half else if number is greater 13477 08:28:00,840 --> 08:28:05,878 than middle door search the right half 13478 08:28:03,000 --> 08:28:08,238 this pseudo code earlier was itself 13479 08:28:05,878 --> 08:28:10,000 recursive why because here is an 13480 08:28:08,238 --> 08:28:12,120 algorithm for searching but what's the 13481 08:28:10,000 --> 08:28:15,040 algorithm telling us well on this line 13482 08:28:12,120 --> 08:28:17,238 and this line it's telling us to search 13483 08:28:15,040 --> 08:28:19,080 something else so even though it's not 13484 08:28:17,238 --> 08:28:21,080 explicitly defined in code as having a 13485 08:28:19,080 --> 08:28:22,840 name if this is a search algorithm and 13486 08:28:21,080 --> 08:28:25,638 yet the search algorithm is using a 13487 08:28:22,840 --> 08:28:27,638 search algorithm this pseudo code is 13488 08:28:25,638 --> 08:28:29,600 recursive now that could quickly get you 13489 08:28:27,638 --> 08:28:33,238 into trouble if a function just calls 13490 08:28:29,600 --> 08:28:36,238 itself again and again and again but why 13491 08:28:33,238 --> 08:28:39,798 intuitively is it not problematic that 13492 08:28:36,238 --> 08:28:43,120 this code this pseudo code calls 13493 08:28:39,798 --> 08:28:46,120 itself why will the algorithm still stop 13494 08:28:43,120 --> 08:28:46,120 yeah 13495 08:28:46,840 --> 08:28:50,718 exactly it has some exit condition like 13496 08:28:48,638 --> 08:28:53,160 if no do is less and more more 13497 08:28:50,718 --> 08:28:55,478 importantly anytime you search the left 13498 08:28:53,160 --> 08:28:56,958 half you're searching a smaller version 13499 08:28:55,478 --> 08:28:58,798 of the problem anytime you search the 13500 08:28:56,958 --> 08:29:00,398 right half you're searching a smaller 13501 08:28:58,798 --> 08:29:02,360 version of the problem literally half 13502 08:29:00,398 --> 08:29:03,958 the size so this is why in the phone 13503 08:29:02,360 --> 08:29:06,160 book obviously I couldn't tear the phone 13504 08:29:03,958 --> 08:29:07,680 book in half uh infinitely many times 13505 08:29:06,160 --> 08:29:09,840 because it was literally getting smaller 13506 08:29:07,680 --> 08:29:12,520 each time so recursion is this ability 13507 08:29:09,840 --> 08:29:13,840 to call yourself if you will it's but 13508 08:29:12,520 --> 08:29:15,958 what's important is that you do it on a 13509 08:29:13,840 --> 08:29:18,238 small smaller smaller problem so that 13510 08:29:15,958 --> 08:29:20,318 eventually you have no more problems to 13511 08:29:18,238 --> 08:29:23,238 solve or no more data no more doors at 13512 08:29:20,318 --> 08:29:25,440 all so these two lines here would be the 13513 08:29:23,238 --> 08:29:27,238 recursive elements here but if we go 13514 08:29:25,440 --> 08:29:28,840 back to week zero we could have used 13515 08:29:27,238 --> 08:29:31,120 recursion in some other way so this was 13516 08:29:28,840 --> 08:29:33,160 our Cudo code for the phone book back in 13517 08:29:31,120 --> 08:29:34,638 week zero and recall that we described 13518 08:29:33,160 --> 08:29:36,840 these yellow lines as really 13519 08:29:34,638 --> 08:29:38,958 representing a loop some kind of cycle 13520 08:29:36,840 --> 08:29:41,440 again and again but there was a missed 13521 08:29:38,958 --> 08:29:43,638 opportunity here what if I had reinvent 13522 08:29:41,440 --> 08:29:44,798 reimplemented this code to do this 13523 08:29:43,638 --> 08:29:47,200 instead 13524 08:29:44,798 --> 08:29:49,040 instead of saying open to middle of left 13525 08:29:47,200 --> 08:29:51,600 half of book and then go back to line 13526 08:29:49,040 --> 08:29:53,280 three like literally inducing a loop or 13527 08:29:51,600 --> 08:29:55,638 open to Middle right half a book and go 13528 08:29:53,280 --> 08:29:57,520 back to line three inducing another loop 13529 08:29:55,638 --> 08:30:00,558 why don't I just recognize that what I'm 13530 08:29:57,520 --> 08:30:03,200 staring at now is a algorithm for 13531 08:30:00,558 --> 08:30:05,280 searching a phone book and if you want 13532 08:30:03,200 --> 08:30:08,600 to search a smaller phone book like a 13533 08:30:05,280 --> 08:30:10,558 through M or n through Z we'll just use 13534 08:30:08,600 --> 08:30:13,040 the same algorithm so I can replace 13535 08:30:10,558 --> 08:30:14,878 these yellow lines with just this 13536 08:30:13,040 --> 08:30:16,600 casually speaking search left half a 13537 08:30:14,878 --> 08:30:18,160 book search right half a book this would 13538 08:30:16,600 --> 08:30:20,478 be implicitly and now I can shorten the 13539 08:30:18,160 --> 08:30:22,120 whole thing a recursive implementation 13540 08:30:20,478 --> 08:30:23,920 of the phone book pseudo code from week 13541 08:30:22,120 --> 08:30:25,520 zero and it's recursive because if this 13542 08:30:23,920 --> 08:30:27,798 is a search algorithm and you're saying 13543 08:30:25,520 --> 08:30:29,600 go search something else that's fine 13544 08:30:27,798 --> 08:30:31,440 that's recursive but because you're 13545 08:30:29,600 --> 08:30:34,680 searching half of the phone book it's 13546 08:30:31,440 --> 08:30:37,280 indeed going to get smaller and smaller 13547 08:30:34,680 --> 08:30:39,718 even in the real world or the real 13548 08:30:37,280 --> 08:30:41,318 virtual world you can see recursive data 13549 08:30:39,718 --> 08:30:42,718 structures in the wild or at least in 13550 08:30:41,318 --> 08:30:44,798 Super Mario Brothers like this let me 13551 08:30:42,718 --> 08:30:47,280 get rid of all the distractions here and 13552 08:30:44,798 --> 08:30:50,120 focus on this pyramid where you have one 13553 08:30:47,280 --> 08:30:52,280 block then two then three then four well 13554 08:30:50,120 --> 08:30:54,080 this itself is technically recursively 13555 08:30:52,280 --> 08:30:57,558 defined in the sense that well what is a 13556 08:30:54,080 --> 08:30:59,478 pyramid of height four well it's really 13557 08:30:57,558 --> 08:31:01,238 what how would you describe a pyramid of 13558 08:30:59,478 --> 08:31:05,200 height four is actually the same thing 13559 08:31:01,238 --> 08:31:05,200 as a pyramid 13560 08:31:05,638 --> 08:31:10,318 of of of height three plus one 13561 08:31:08,798 --> 08:31:11,920 additional layer well what's a pyramid 13562 08:31:10,318 --> 08:31:14,200 of height three well it's technically a 13563 08:31:11,920 --> 08:31:16,280 pyramid of height two plus one 13564 08:31:14,200 --> 08:31:17,920 additional layer and so even physical 13565 08:31:16,280 --> 08:31:19,798 structures can be recursive if you can 13566 08:31:17,920 --> 08:31:21,840 Define them in terms of itself now at 13567 08:31:19,798 --> 08:31:23,920 some point you have to say that if the 13568 08:31:21,840 --> 08:31:26,200 pyramid is of height one there's just 13569 08:31:23,920 --> 08:31:27,878 one block you can't forever say it's 13570 08:31:26,200 --> 08:31:29,558 defined in terms of a height negative 1 13571 08:31:27,878 --> 08:31:31,398 negative2 you would never stop so you 13572 08:31:29,558 --> 08:31:32,958 have to kind of have a special case 13573 08:31:31,398 --> 08:31:34,638 there but let's go ahead and translate 13574 08:31:32,958 --> 08:31:37,200 something like this in fact to code let 13575 08:31:34,638 --> 08:31:39,398 me go back to uh vs code here and let me 13576 08:31:37,200 --> 08:31:42,280 Implement a program called iteration 13577 08:31:39,398 --> 08:31:44,200 that refers to a loop iterating and let 13578 08:31:42,280 --> 08:31:45,760 me Implement a very simple pyramid like 13579 08:31:44,200 --> 08:31:49,360 that so let me go ahead and include the 13580 08:31:45,760 --> 08:31:52,878 cs50 library I'll include our standard 13581 08:31:49,360 --> 08:31:54,558 i.h in main void no command line 13582 08:31:52,878 --> 08:31:56,200 arguments today and let's go ahead and 13583 08:31:54,558 --> 08:31:58,798 do this let's declare a variable called 13584 08:31:56,200 --> 08:32:02,200 height ask the human for the height of 13585 08:31:58,798 --> 08:32:04,360 this pyramid and then let's go ahead and 13586 08:32:02,200 --> 08:32:06,638 draw a pyramid of that height now of 13587 08:32:04,360 --> 08:32:08,318 course draw does not yet exist so I'm 13588 08:32:06,638 --> 08:32:10,120 going to need to invent the draw a 13589 08:32:08,318 --> 08:32:11,680 function let me go ahead and Define a 13590 08:32:10,120 --> 08:32:12,840 function that doesn't have a return 13591 08:32:11,680 --> 08:32:14,160 value it's just going to have side 13592 08:32:12,840 --> 08:32:16,760 effects it's just going to print bricks 13593 08:32:14,160 --> 08:32:19,718 on the screen call draw and it takes in 13594 08:32:16,760 --> 08:32:21,600 an integer n as its input and how am I 13595 08:32:19,718 --> 08:32:24,000 going to implement this well again I 13596 08:32:21,600 --> 08:32:26,160 want to print one block then two then 13597 08:32:24,000 --> 08:32:27,440 three then four that's pretty 13598 08:32:26,160 --> 08:32:29,080 straightforward at least once you're 13599 08:32:27,440 --> 08:32:32,280 comfortable with loops let me go back to 13600 08:32:29,080 --> 08:32:37,318 the code here let me go ahead and say 4 13601 08:32:32,280 --> 08:32:38,920 into I gets zero I is less than n i ++ 13602 08:32:37,318 --> 08:32:41,840 and that's going to iterate essentially 13603 08:32:38,920 --> 08:32:44,238 row by row and on each row I want to 13604 08:32:41,840 --> 08:32:46,398 print out one then two then three then 13605 08:32:44,238 --> 08:32:48,558 four bricks but I'm iterating from Z to 13606 08:32:46,398 --> 08:32:50,840 1 to two to three so I think that's okay 13607 08:32:48,558 --> 08:32:54,080 I can just say something like 4 in J 13608 08:32:50,840 --> 08:32:57,478 gets zero J Let's Be Clever about this 13609 08:32:54,080 --> 08:33:00,398 is less than I 13610 08:32:57,478 --> 08:33:02,600 j++ and now let me go ahead and inside 13611 08:33:00,398 --> 08:33:06,160 of this loop I think I can get away with 13612 08:33:02,600 --> 08:33:08,398 just printing out a single hash sign but 13613 08:33:06,160 --> 08:33:10,920 then outside of that Loop similar to 13614 08:33:08,398 --> 08:33:13,080 last week I'm going to print my new line 13615 08:33:10,920 --> 08:33:15,520 separately so a little non-obvious at 13616 08:33:13,080 --> 08:33:18,000 first but this outer loop iterates row 13617 08:33:15,520 --> 08:33:21,318 by row line by line if you will and then 13618 08:33:18,000 --> 08:33:26,478 the inner loop just Mak sure that when I 13619 08:33:21,318 --> 08:33:29,760 equals zero um let's see oh nope there's 13620 08:33:26,478 --> 08:33:33,080 a bug I need to make sure that it's j is 13621 08:33:29,760 --> 08:33:35,040 less than I + 1 so when I is zero on my 13622 08:33:33,080 --> 08:33:38,638 first line of output I'm going to print 13623 08:33:35,040 --> 08:33:41,318 out one brick when I is one I'm going to 13624 08:33:38,638 --> 08:33:44,398 print out two bricks and so forth so let 13625 08:33:41,318 --> 08:33:48,318 me go ahead and run make iteration 13626 08:33:44,398 --> 08:33:50,798 all right and now seems to compile 13627 08:33:48,318 --> 08:33:52,638 uhoh huh implicit Declaration of 13628 08:33:50,798 --> 08:33:54,238 function draw so I'm making week one 13629 08:33:52,638 --> 08:33:56,558 mistakes 13630 08:33:54,238 --> 08:33:59,040 again what say 13631 08:33:56,558 --> 08:34:00,440 again yeah the the Prototype is missing 13632 08:33:59,040 --> 08:34:02,558 I didn't declare it at the top that's an 13633 08:34:00,440 --> 08:34:04,798 easy fix and the only time really it's 13634 08:34:02,558 --> 08:34:06,638 okay and necessary to copy paste let me 13635 08:34:04,798 --> 08:34:09,120 copy the functions declaration there end 13636 08:34:06,638 --> 08:34:11,558 it with a semicolon so that clang now 13637 08:34:09,120 --> 08:34:14,120 knows that draw will exist make 13638 08:34:11,558 --> 08:34:16,000 iteration now it works thank you / 13639 08:34:14,120 --> 08:34:18,318 iteration we'll type in something like 13640 08:34:16,000 --> 08:34:21,200 four and there we have it our pyramid of 13641 08:34:18,318 --> 08:34:23,000 height 1 2 3 4 that looks pretty similar 13642 08:34:21,200 --> 08:34:24,160 to this albeit using hashes so that's 13643 08:34:23,000 --> 08:34:26,040 how we would have implemented this like 13644 08:34:24,160 --> 08:34:28,638 two weeks ago in week one maybe last 13645 08:34:26,040 --> 08:34:31,120 week but just using arrays but let me 13646 08:34:28,638 --> 08:34:33,238 propose that we could do something 13647 08:34:31,120 --> 08:34:35,238 recursively instead let me close this 13648 08:34:33,238 --> 08:34:38,760 version of the code and let me go back 13649 08:34:35,238 --> 08:34:39,920 to VSS code and open up recursion doc 13650 08:34:38,760 --> 08:34:42,040 just to demonstrate something 13651 08:34:39,920 --> 08:34:43,718 recursively and I'll do it incorrectly 13652 08:34:42,040 --> 08:34:46,920 deliberately the first time so let me 13653 08:34:43,718 --> 08:34:51,318 include cs50.h let me include standard 13654 08:34:46,920 --> 08:34:55,398 i.h let me do uh int main void and let 13655 08:34:51,318 --> 08:34:58,318 me just blindly draw a pyramid initially 13656 08:34:55,398 --> 08:34:59,878 of height one but now in my draw 13657 08:34:58,318 --> 08:35:02,080 function let me reimplement it a little 13658 08:34:59,878 --> 08:35:03,840 differently so my draw function this 13659 08:35:02,080 --> 08:35:05,360 time is still going to take a number n 13660 08:35:03,840 --> 08:35:09,000 but that's how many hashes it's going to 13661 08:35:05,360 --> 08:35:12,160 print so let's do four into I gets zero 13662 08:35:09,000 --> 08:35:14,840 I is less than n i 13663 08:35:12,160 --> 08:35:18,718 ++ then let's go ahead and print out a 13664 08:35:14,840 --> 08:35:22,000 single hash mark here and then after 13665 08:35:18,718 --> 08:35:24,718 that let's print out the end of the line 13666 08:35:22,000 --> 08:35:28,760 just as before but now this of course is 13667 08:35:24,718 --> 08:35:30,638 only going to draw a single um row it's 13668 08:35:28,760 --> 08:35:33,040 going to print out one hash or two 13669 08:35:30,638 --> 08:35:35,638 hashes or three hashes but only on one 13670 08:35:33,040 --> 08:35:38,040 line let me now incorrectly but just 13671 08:35:35,638 --> 08:35:40,398 kind of curiously say all right well if 13672 08:35:38,040 --> 08:35:42,440 this draws a pyramid of height one let's 13673 08:35:40,398 --> 08:35:44,840 just use ourself to draw a pyramid of 13674 08:35:42,440 --> 08:35:47,398 height n plus one so the first time I 13675 08:35:44,840 --> 08:35:49,360 call draw it will print out one hash 13676 08:35:47,398 --> 08:35:52,040 then the second time I call draw it will 13677 08:35:49,360 --> 08:35:53,958 print out two hashes then three then 13678 08:35:52,040 --> 08:35:58,360 four so we're kind of laying These 13679 08:35:53,958 --> 08:36:01,520 Bricks down from top to bottom uh make 13680 08:35:58,360 --> 08:36:03,958 recursion uh oops I screwed up again so 13681 08:36:01,520 --> 08:36:06,360 let's copy the Prototype here let's put 13682 08:36:03,958 --> 08:36:09,280 this down over here semicolon let's do 13683 08:36:06,360 --> 08:36:12,280 this again uh make recursion all right 13684 08:36:09,280 --> 08:36:13,798 all good/ recursion and now let me 13685 08:36:12,280 --> 08:36:16,238 increase the size of my terminal window 13686 08:36:13,798 --> 08:36:17,558 just so you can see more of the output 13687 08:36:16,238 --> 08:36:20,638 and here we 13688 08:36:17,558 --> 08:36:23,000 have okay bad but thank you so we have 13689 08:36:20,638 --> 08:36:24,558 an infinitely tall pyramid and it's just 13690 08:36:23,000 --> 08:36:26,600 flying across the screen which is why it 13691 08:36:24,558 --> 08:36:28,920 looks kind of like a mess but I printed 13692 08:36:26,600 --> 08:36:30,600 out a pyramid of height one and then two 13693 08:36:28,920 --> 08:36:32,920 and then three and then four and 13694 08:36:30,600 --> 08:36:35,160 unfortunately what am I lacking any sort 13695 08:36:32,920 --> 08:36:36,558 of quick condition any kind of condition 13696 08:36:35,160 --> 08:36:38,638 that says wait a minute when it's too 13697 08:36:36,558 --> 08:36:40,920 tall stop all together so this is an 13698 08:36:38,638 --> 08:36:43,080 infinite Loop but it's not a loop it's a 13699 08:36:40,920 --> 08:36:45,398 recursive call and actually doing this 13700 08:36:43,080 --> 08:36:46,798 in en is very bad we'll see next week 13701 08:36:45,398 --> 08:36:48,878 that if you call a function too many 13702 08:36:46,798 --> 08:36:51,080 times you can actually trigger yet one 13703 08:36:48,878 --> 08:36:52,318 yet another of those segmentation faults 13704 08:36:51,080 --> 08:36:54,398 cuz you're using too much memory 13705 08:36:52,318 --> 08:36:56,000 essentially but for now I haven't 13706 08:36:54,398 --> 08:36:58,160 triggered that yet control C is your 13707 08:36:56,000 --> 08:36:59,360 friend to cancel and as an aside if 13708 08:36:58,160 --> 08:37:01,318 you're playing along at home or play 13709 08:36:59,360 --> 08:37:02,958 with this code later I actually cheated 13710 08:37:01,318 --> 08:37:05,318 here we have a special clang 13711 08:37:02,958 --> 08:37:07,440 configuration feature that prevents you 13712 08:37:05,318 --> 08:37:09,398 from calling a function like that and 13713 08:37:07,440 --> 08:37:11,238 creating a problem I overrode it just to 13714 08:37:09,398 --> 08:37:12,478 for demonstration sake but odds are at 13715 08:37:11,238 --> 08:37:14,398 home you wouldn't be able to compile 13716 08:37:12,478 --> 08:37:17,000 this code yourself but let me do a 13717 08:37:14,398 --> 08:37:20,120 proper version recursively of this code 13718 08:37:17,000 --> 08:37:21,840 as follows let me go back into the code 13719 08:37:20,120 --> 08:37:24,200 here let me go ahead and not just 13720 08:37:21,840 --> 08:37:26,080 blindly start drawing one then two then 13721 08:37:24,200 --> 08:37:28,840 three layers of bricks let me prompt the 13722 08:37:26,080 --> 08:37:31,600 human as before for the height of the 13723 08:37:28,840 --> 08:37:33,798 pyramid they want using our get in 13724 08:37:31,600 --> 08:37:35,680 function and now let me call draw of 13725 08:37:33,798 --> 08:37:38,160 height again so now I'm going back to 13726 08:37:35,680 --> 08:37:40,558 the looplike version but instead of 13727 08:37:38,160 --> 08:37:43,440 using a loop now this is where recursion 13728 08:37:40,558 --> 08:37:46,718 gets rather elegant if you will let me 13729 08:37:43,440 --> 08:37:48,600 go ahead and execute and code uh the 13730 08:37:46,718 --> 08:37:52,080 draw function as 13731 08:37:48,600 --> 08:37:53,878 follows per your definition if a pyramid 13732 08:37:52,080 --> 08:37:56,360 of height four is really just a pyramid 13733 08:37:53,878 --> 08:37:57,878 of height three plus another row well 13734 08:37:56,360 --> 08:38:00,558 let's take that literally let me go back 13735 08:37:57,878 --> 08:38:02,840 to my code and if you want to draw a 13736 08:38:00,558 --> 08:38:05,398 pyramid of height four well go right 13737 08:38:02,840 --> 08:38:07,760 ahead and draw a pyramid of height uh 13738 08:38:05,398 --> 08:38:09,760 three first or more generally n minus 13739 08:38:07,760 --> 08:38:11,920 one but what's the second step well once 13740 08:38:09,760 --> 08:38:14,238 you've drawn a pyramid of height three 13741 08:38:11,920 --> 08:38:15,920 draw an extra row so I at least have to 13742 08:38:14,238 --> 08:38:18,318 bite off that part of the problem myself 13743 08:38:15,920 --> 08:38:21,478 so let me just do for in I gets zero I 13744 08:38:18,318 --> 08:38:24,120 is less than n i++ and let me the 13745 08:38:21,478 --> 08:38:26,280 programmer of this function print out my 13746 08:38:24,120 --> 08:38:28,200 hashes and then at the very bottom print 13747 08:38:26,280 --> 08:38:30,638 out a new line so the cursor moves to 13748 08:38:28,200 --> 08:38:33,680 the next line but this is kind of 13749 08:38:30,638 --> 08:38:35,520 elegant now I dare say in that draw is 13750 08:38:33,680 --> 08:38:37,760 recursive because I'm literally 13751 08:38:35,520 --> 08:38:39,558 translating from English to code this 13752 08:38:37,760 --> 08:38:41,200 idea that a pyramid of height four is 13753 08:38:39,558 --> 08:38:44,080 really just a pyramid of height three so 13754 08:38:41,200 --> 08:38:46,280 I do that first and I'm sort of trusting 13755 08:38:44,080 --> 08:38:48,360 that this will work then I just have to 13756 08:38:46,280 --> 08:38:50,080 lay one more layer of bricks four of 13757 08:38:48,360 --> 08:38:52,238 them so if n is four this is just a 13758 08:38:50,080 --> 08:38:54,920 simple for Loop Allah week one that will 13759 08:38:52,238 --> 08:38:56,840 print out an additional layer but this 13760 08:38:54,920 --> 08:39:00,680 of course is going to be problematic 13761 08:38:56,840 --> 08:39:03,558 eventually why it's not done yet this 13762 08:39:00,680 --> 08:39:06,360 program how many times will draw call 13763 08:39:03,558 --> 08:39:06,360 itself in this 13764 08:39:06,638 --> 08:39:10,718 model infinitely many times 13765 08:39:12,238 --> 08:39:16,398 why yeah there's no there's no 13766 08:39:14,360 --> 08:39:18,680 equivalent of quit like if you've 13767 08:39:16,398 --> 08:39:20,318 printed enough already then quit well 13768 08:39:18,680 --> 08:39:22,160 how do we capture that well I don't 13769 08:39:20,318 --> 08:39:23,600 think we want this to go negative right 13770 08:39:22,160 --> 08:39:26,280 it would make no sense to draw a 13771 08:39:23,600 --> 08:39:28,958 negative height pyramid so I think we 13772 08:39:26,280 --> 08:39:32,040 can just pluck off as the programmer a 13773 08:39:28,958 --> 08:39:33,718 an easy case an easy answer a so-called 13774 08:39:32,040 --> 08:39:35,840 base case and I'm just going to do this 13775 08:39:33,718 --> 08:39:39,398 at the top of my draw function let me 13776 08:39:35,840 --> 08:39:42,318 just say if n is less than or heck less 13777 08:39:39,398 --> 08:39:44,440 than or equal to zero that's it go ahead 13778 08:39:42,318 --> 08:39:47,440 and just return there's nothing more to 13779 08:39:44,440 --> 08:39:50,558 do and that simple condition technically 13780 08:39:47,440 --> 08:39:52,958 known as a base case will ensure that 13781 08:39:50,558 --> 08:39:55,440 the code doesn't run forever why well 13782 08:39:52,958 --> 08:39:57,760 suppose that draw uh is called with an 13783 08:39:55,440 --> 08:39:59,760 argument of four four is of course not 13784 08:39:57,760 --> 08:40:01,558 less than zero so we don't return but we 13785 08:39:59,760 --> 08:40:02,600 do draw a pyramid of height three and 13786 08:40:01,558 --> 08:40:04,558 here's where things get a little 13787 08:40:02,600 --> 08:40:07,360 mentally tricky you don't move on to 13788 08:40:04,558 --> 08:40:09,238 line 20 until draw has been called so 13789 08:40:07,360 --> 08:40:11,478 when draw is called with an argument of 13790 08:40:09,238 --> 08:40:13,160 three it's as though you're executing 13791 08:40:11,478 --> 08:40:14,680 from the top of this function again 13792 08:40:13,160 --> 08:40:18,000 three is not less than zero so what do 13793 08:40:14,680 --> 08:40:19,958 you do you draw two okay how do you draw 13794 08:40:18,000 --> 08:40:22,318 two well two is not less than zero so 13795 08:40:19,958 --> 08:40:25,040 you don't return so you draw one got to 13796 08:40:22,318 --> 08:40:26,360 be careful here draw one and now we go 13797 08:40:25,040 --> 08:40:28,280 ahead back to the beginning how do you 13798 08:40:26,360 --> 08:40:32,200 draw One well one is not less than zero 13799 08:40:28,280 --> 08:40:33,638 so you don't return you draw height zero 13800 08:40:32,200 --> 08:40:36,600 how do you draw height zero wait a 13801 08:40:33,638 --> 08:40:39,120 minute 0o is less than or equal to zero 13802 08:40:36,600 --> 08:40:41,080 and you return and so it's kind of like 13803 08:40:39,120 --> 08:40:43,238 this mental stack this to-do list you 13804 08:40:41,080 --> 08:40:45,120 keep postponing executing these lower 13805 08:40:43,238 --> 08:40:46,878 lines of code because you keep 13806 08:40:45,120 --> 08:40:49,798 restarting restarting restarting the 13807 08:40:46,878 --> 08:40:51,440 draw function until finally one of those 13808 08:40:49,798 --> 08:40:53,878 function calls says there's nothing to 13809 08:40:51,440 --> 08:40:56,040 do return and now the whole thing starts 13810 08:40:53,878 --> 08:40:57,638 to unravel if you will and you pick back 13811 08:40:56,040 --> 08:40:59,600 up where you left off and this is 13812 08:40:57,638 --> 08:41:00,958 perhaps the best uh scenario we won't do 13813 08:40:59,600 --> 08:41:03,318 it in class but if you'd like to wrestle 13814 08:41:00,958 --> 08:41:05,718 through this on your own using debug 50 13815 08:41:03,318 --> 08:41:07,680 to keep stepping into step into step 13816 08:41:05,718 --> 08:41:09,638 into each of those lines logically 13817 08:41:07,680 --> 08:41:11,520 you'll see exactly what's actually 13818 08:41:09,638 --> 08:41:14,040 happening so let me go to my terminal 13819 08:41:11,520 --> 08:41:15,920 and do make recursion which is now this 13820 08:41:14,040 --> 08:41:18,200 correct version of the code do/ 13821 08:41:15,920 --> 08:41:21,000 recursion let's type in a height of four 13822 08:41:18,200 --> 08:41:23,798 and voila now we have that same pyramid 13823 08:41:21,000 --> 08:41:25,360 not using iteration per se though 13824 08:41:23,798 --> 08:41:27,520 admittedly we're using iteration to 13825 08:41:25,360 --> 08:41:31,120 print the additional layer we're now 13826 08:41:27,520 --> 08:41:32,760 using draw recursively to print all of 13827 08:41:31,120 --> 08:41:35,080 the smaller pyramids that need come 13828 08:41:32,760 --> 08:41:35,080 before 13829 08:41:36,318 --> 08:41:40,478 it no question is can you only use 13830 08:41:38,638 --> 08:41:42,638 recursion with a void function no not at 13831 08:41:40,478 --> 08:41:44,160 all in fact it's very common to have a 13832 08:41:42,638 --> 08:41:46,478 return value like an integer or 13833 08:41:44,160 --> 08:41:48,360 something else so that uh you can 13834 08:41:46,478 --> 08:41:50,878 actually do something constructively 13835 08:41:48,360 --> 08:41:53,160 with that actual value other questions 13836 08:41:50,878 --> 08:41:53,160 on 13837 08:41:53,200 --> 08:41:56,718 this say a little 13838 08:41:56,760 --> 08:42:03,520 lad when is line 21 getting executed so 13839 08:42:00,200 --> 08:42:04,718 if you continue to UNR let me uh scroll 13840 08:42:03,520 --> 08:42:09,680 down a bit more so you can see the top 13841 08:42:04,718 --> 08:42:14,360 of the code so line 21 will be executed 13842 08:42:09,680 --> 08:42:16,878 once line 19 is done executing itself 13843 08:42:14,360 --> 08:42:20,520 now in the story I told we kept calling 13844 08:42:16,878 --> 08:42:22,600 draw again again again but as soon as 13845 08:42:20,520 --> 08:42:25,318 one of those function calls where n 13846 08:42:22,600 --> 08:42:27,558 equals z returns immediately then we 13847 08:42:25,318 --> 08:42:28,798 don't keep drawing again and again so 13848 08:42:27,558 --> 08:42:31,638 now if you kind of think of the process 13849 08:42:28,798 --> 08:42:35,718 as reversing then you continue to line 13850 08:42:31,638 --> 08:42:38,760 21 then a line 21 again then line 21 13851 08:42:35,718 --> 08:42:39,878 again and as the sort of logic unravels 13852 08:42:38,760 --> 08:42:41,160 and next week we'll actually paint a 13853 08:42:39,878 --> 08:42:43,920 picture of what's actually happening in 13854 08:42:41,160 --> 08:42:45,718 the computer's memory but for now it's 13855 08:42:43,920 --> 08:42:47,040 just it's very similar to the pseudo 13856 08:42:45,718 --> 08:42:48,798 code for the phone book you're just 13857 08:42:47,040 --> 08:42:51,040 searching again and again but you're 13858 08:42:48,798 --> 08:42:54,080 waiting until the very end to get back 13859 08:42:51,040 --> 08:42:55,558 the final result uh can Google Now whom 13860 08:42:54,080 --> 08:42:57,840 I keep mentioning by coincidence today 13861 08:42:55,558 --> 08:43:01,280 is full of programmers of course um 13862 08:42:57,840 --> 08:43:03,280 here's a fun exercise let me uh go back 13863 08:43:01,280 --> 08:43:04,958 to a browser I'm going to go ahead and 13864 08:43:03,280 --> 08:43:06,760 search for recursion because I want to 13865 08:43:04,958 --> 08:43:08,760 learn a little something about recursion 13866 08:43:06,760 --> 08:43:11,440 uh here is kind of an internet meme or 13867 08:43:08,760 --> 08:43:16,520 joke if I zoom in here the engineers at 13868 08:43:11,440 --> 08:43:16,520 Google are kind of funny funny see 13869 08:43:17,280 --> 08:43:22,600 why ah there we go yes yes this is 13870 08:43:21,280 --> 08:43:24,238 recursion and there's going to be so 13871 08:43:22,600 --> 08:43:25,878 many memes you'll come across now where 13872 08:43:24,238 --> 08:43:27,718 recursion like if you've ever pointed a 13873 08:43:25,878 --> 08:43:29,120 camera at the TV that's showing the 13874 08:43:27,718 --> 08:43:30,680 camera and you sort of see yourself for 13875 08:43:29,120 --> 08:43:32,958 the image again and again that's really 13876 08:43:30,680 --> 08:43:34,398 recursion and in that case it only stops 13877 08:43:32,958 --> 08:43:37,120 once you hit the base case of a single 13878 08:43:34,398 --> 08:43:38,878 Pixel um but this is a very funny joke 13879 08:43:37,120 --> 08:43:41,558 in some circles uh when it comes to 13880 08:43:38,878 --> 08:43:43,798 recursion uh and Google so how can we 13881 08:43:41,558 --> 08:43:47,000 actually use Google or rather how can we 13882 08:43:43,798 --> 08:43:49,680 actually use recursion constructively 13883 08:43:47,000 --> 08:43:51,398 well let me propose that we actually 13884 08:43:49,680 --> 08:43:53,840 introduce a third and final algorithm 13885 08:43:51,398 --> 08:43:56,360 for sorting that hopefully does better 13886 08:43:53,840 --> 08:43:58,798 than the two sorts thus far we've done 13887 08:43:56,360 --> 08:44:00,160 selection sort and bubble sort bubble 13888 08:43:58,798 --> 08:44:02,000 sort we liked a little better at least 13889 08:44:00,160 --> 08:44:03,600 in so far is in the best case where the 13890 08:44:02,000 --> 08:44:04,878 list is already sorted bubble sort is at 13891 08:44:03,600 --> 08:44:07,280 least smarter and it will actually 13892 08:44:04,878 --> 08:44:09,600 terminate early giving us a better lower 13893 08:44:07,280 --> 08:44:11,718 Bound in terms of our Omega notation but 13894 08:44:09,600 --> 08:44:13,718 it turns out that recursion and this is 13895 08:44:11,718 --> 08:44:15,280 not necessarily a feature of recursion 13896 08:44:13,718 --> 08:44:17,080 but something we can now leverage it 13897 08:44:15,280 --> 08:44:18,478 turns out using recursion we can take a 13898 08:44:17,080 --> 08:44:21,280 fundamentally different approach to 13899 08:44:18,478 --> 08:44:24,238 sorting a whole bunch of numbers in such 13900 08:44:21,280 --> 08:44:26,680 a way that we can do far fewer 13901 08:44:24,238 --> 08:44:29,520 comparisons and ideally speed up our 13902 08:44:26,680 --> 08:44:31,360 final results so here is the pseudo code 13903 08:44:29,520 --> 08:44:33,360 for what we're about to see for 13904 08:44:31,360 --> 08:44:36,478 something called merge sort and it 13905 08:44:33,360 --> 08:44:39,558 really is this tur sort the left half of 13906 08:44:36,478 --> 08:44:42,040 numbers sort the right half of numbers 13907 08:44:39,558 --> 08:44:44,798 merge the sorted halves right this is 13908 08:44:42,040 --> 08:44:46,440 almost sort of non sensical because I uh 13909 08:44:44,798 --> 08:44:48,440 if you're asked for an algorithm to sort 13910 08:44:46,440 --> 08:44:50,080 and you respond with well sort the left 13911 08:44:48,440 --> 08:44:51,680 half sort the right half like that's 13912 08:44:50,080 --> 08:44:53,080 being sort of difficult because well I'm 13913 08:44:51,680 --> 08:44:54,440 asking you for a sorting algorithm 13914 08:44:53,080 --> 08:44:56,600 you're just telling me to sort the left 13915 08:44:54,440 --> 08:44:58,958 half and the right half but implicit in 13916 08:44:56,600 --> 08:45:01,000 that last line merging is a pretty 13917 08:44:58,958 --> 08:45:03,000 powerful feature of this sort now we do 13918 08:45:01,000 --> 08:45:04,920 need another base case at the top so let 13919 08:45:03,000 --> 08:45:07,280 me add this if we find ourselves with a 13920 08:45:04,920 --> 08:45:08,920 list an array of size one well that 13921 08:45:07,280 --> 08:45:10,478 array is obviously sorted if there's 13922 08:45:08,920 --> 08:45:12,080 only one element in it there's no work 13923 08:45:10,478 --> 08:45:14,318 to be done so that's going to be our 13924 08:45:12,080 --> 08:45:17,160 base case but allowing us now in just 13925 08:45:14,318 --> 08:45:19,840 these what uh four six lines of pseudo 13926 08:45:17,160 --> 08:45:21,760 code to actually sort some elements but 13927 08:45:19,840 --> 08:45:23,718 let's focus first on just a subset of 13928 08:45:21,760 --> 08:45:25,958 this let's consider for a moment what it 13929 08:45:23,718 --> 08:45:27,398 means to merge sorted have so Carter has 13930 08:45:25,958 --> 08:45:29,318 wonderfully come up to volunteer here 13931 08:45:27,398 --> 08:45:31,718 just to help us reset these numbers 13932 08:45:29,318 --> 08:45:34,440 suppose that in the middle of the story 13933 08:45:31,718 --> 08:45:36,398 we're about to tell we have two sorted 13934 08:45:34,440 --> 08:45:38,718 halves I've already sorted the left half 13935 08:45:36,398 --> 08:45:40,840 of these numbers and indeed 2 4 57 is 13936 08:45:38,718 --> 08:45:43,040 sorted from smallest to largest and the 13937 08:45:40,840 --> 08:45:46,318 right half appears to be already sorted 13938 08:45:43,040 --> 08:45:48,238 0136 already sorted so in my pseudo code 13939 08:45:46,318 --> 08:45:49,958 we're already done sorting the left half 13940 08:45:48,238 --> 08:45:52,478 and the right half somehow but we'll see 13941 08:45:49,958 --> 08:45:54,558 how in a moment well how do I go about 13942 08:45:52,478 --> 08:45:56,360 merging these two halves well because 13943 08:45:54,558 --> 08:45:58,638 they're sorted already and you want to 13944 08:45:56,360 --> 08:46:01,878 merge them in order I think we can flip 13945 08:45:58,638 --> 08:46:04,318 down we can hide all but the first 13946 08:46:01,878 --> 08:46:07,360 numbers in each of these subl lists so 13947 08:46:04,318 --> 08:46:08,600 here we have a half that starts with two 13948 08:46:07,360 --> 08:46:09,920 and I don't really care what the other 13949 08:46:08,600 --> 08:46:12,280 numbers are because they're clearly 13950 08:46:09,920 --> 08:46:15,000 larger than two I can focus only on Two 13951 08:46:12,280 --> 08:46:16,360 And Z too zero also we know that zero is 13952 08:46:15,000 --> 08:46:17,760 the smallest there so let's just ignore 13953 08:46:16,360 --> 08:46:21,120 the numbers that Carter kindly flipped 13954 08:46:17,760 --> 08:46:23,718 down so how do I merge these two lists 13955 08:46:21,120 --> 08:46:26,398 into a new sorted larger list well I 13956 08:46:23,718 --> 08:46:28,600 compare the two on my left with the zero 13957 08:46:26,398 --> 08:46:30,878 on my right obviously which comes first 13958 08:46:28,600 --> 08:46:32,120 the zero so let me put this down here 13959 08:46:30,878 --> 08:46:34,398 and Carter if you want to give us the 13960 08:46:32,120 --> 08:46:36,558 next element now I have two sorted 13961 08:46:34,398 --> 08:46:38,958 halves but I've already plucked one off 13962 08:46:36,558 --> 08:46:40,878 so now I compare the two against the one 13963 08:46:38,958 --> 08:46:43,398 one obviously comes next so I'm going to 13964 08:46:40,878 --> 08:46:45,040 take out the one and put it in place 13965 08:46:43,398 --> 08:46:46,718 here now I'm going to compare the two 13966 08:46:45,040 --> 08:46:49,718 halves again two and three which do I 13967 08:46:46,718 --> 08:46:51,878 merge first obviously the two comes next 13968 08:46:49,718 --> 08:46:53,680 and now notice each time I do this my 13969 08:46:51,878 --> 08:46:55,440 hands are theoretically making forward 13970 08:46:53,680 --> 08:46:57,360 progress I'm not doubling back like I 13971 08:46:55,440 --> 08:46:59,398 kept doing with selection sort or bubble 13972 08:46:57,360 --> 08:47:01,798 sort back and forth back and forth my 13973 08:46:59,398 --> 08:47:03,360 fingers are constantly advancing forward 13974 08:47:01,798 --> 08:47:06,558 and that's going to be a key detail so I 13975 08:47:03,360 --> 08:47:09,120 compare four and three three obviously I 13976 08:47:06,558 --> 08:47:12,840 compare three and uh I compare four and 13977 08:47:09,120 --> 08:47:16,238 six four obviously I compare five five 13978 08:47:12,840 --> 08:47:20,040 and six five obviously and then I 13979 08:47:16,238 --> 08:47:21,600 compare seven and six six of course and 13980 08:47:20,040 --> 08:47:23,160 then lastly we have just one element 13981 08:47:21,600 --> 08:47:25,238 left and even though I'm kind of moving 13982 08:47:23,160 --> 08:47:26,798 awkwardly as a human my hands 13983 08:47:25,238 --> 08:47:28,718 technically were only moving to the 13984 08:47:26,798 --> 08:47:30,280 right I was never looping back doing 13985 08:47:28,718 --> 08:47:32,120 something again and again and that's 13986 08:47:30,280 --> 08:47:34,558 perhaps the intuition and just enough 13987 08:47:32,120 --> 08:47:37,280 room for the seven so that then is how 13988 08:47:34,558 --> 08:47:39,120 you would merge two sorted halves we 13989 08:47:37,280 --> 08:47:40,840 started with left half sorted right half 13990 08:47:39,120 --> 08:47:42,160 sorted and merging is just like what you 13991 08:47:40,840 --> 08:47:43,920 would do as a human and Carter just 13992 08:47:42,160 --> 08:47:47,680 flips the numbers down so our Focus was 13993 08:47:43,920 --> 08:47:50,318 only on the smallest elements in each 13994 08:47:47,680 --> 08:47:54,958 any questions before we Forge ahead with 13995 08:47:50,318 --> 08:47:58,040 what it means then to be merged in this 13996 08:47:54,958 --> 08:47:59,478 way so now here is an original list we 13997 08:47:58,040 --> 08:48:00,920 deliberately put it at the top because 13998 08:47:59,478 --> 08:48:03,200 there's one detail of Mer sort that's 13999 08:48:00,920 --> 08:48:04,840 key mer sort is technically going to use 14000 08:48:03,200 --> 08:48:06,238 a little more space and so whereas 14001 08:48:04,840 --> 08:48:08,238 previously we just kept moving our 14002 08:48:06,238 --> 08:48:09,680 humans around and swapping people and 14003 08:48:08,238 --> 08:48:12,040 making sure they stayed ultimately in 14004 08:48:09,680 --> 08:48:13,798 the original positions with mer sort 14005 08:48:12,040 --> 08:48:16,280 pretend ends that here's our original 14006 08:48:13,798 --> 08:48:17,958 array of uh memory I'm going to need at 14007 08:48:16,280 --> 08:48:19,200 least one other array of memory and I'm 14008 08:48:17,958 --> 08:48:21,160 going to cheat and I'm going to use even 14009 08:48:19,200 --> 08:48:22,840 more memory but technically I could 14010 08:48:21,160 --> 08:48:24,840 actually go back and forth between one 14011 08:48:22,840 --> 08:48:26,558 array and a secondary array but it isn't 14012 08:48:24,840 --> 08:48:29,798 going to it is going to take me more 14013 08:48:26,558 --> 08:48:32,840 space so how do I go about implementing 14014 08:48:29,798 --> 08:48:35,080 merge sort on this Al on this code well 14015 08:48:32,840 --> 08:48:37,238 let's consider this here's a array of 14016 08:48:35,080 --> 08:48:38,878 size eight if only one number quit 14017 08:48:37,238 --> 08:48:40,920 obviously not applicable so let's focus 14018 08:48:38,878 --> 08:48:42,680 on the juicy part there sort the left 14019 08:48:40,920 --> 08:48:44,040 half of the numbers all right how do I 14020 08:48:42,680 --> 08:48:45,558 sort the left half of the numbers I'm 14021 08:48:44,040 --> 08:48:48,520 going to just nudge them over just to be 14022 08:48:45,558 --> 08:48:51,280 clear which is the left half how here is 14023 08:48:48,520 --> 08:48:52,840 now a sub list of size four how do I 14024 08:48:51,280 --> 08:48:55,478 sort the left 14025 08:48:52,840 --> 08:48:57,718 half well do I have an algorithm for 14026 08:48:55,478 --> 08:48:59,280 sorting yeah what do I do here's a list 14027 08:48:57,718 --> 08:49:02,680 of size four how do I sort it what's 14028 08:48:59,280 --> 08:49:04,638 step one sort the left half so I now 14029 08:49:02,680 --> 08:49:07,958 sort of conceptually in my mind take 14030 08:49:04,638 --> 08:49:10,398 this sublist of size four and I sort it 14031 08:49:07,958 --> 08:49:11,798 by first sorting the left half focusing 14032 08:49:10,398 --> 08:49:14,120 now on the seven and two all right 14033 08:49:11,798 --> 08:49:16,718 here's a list of size two how do I sort 14034 08:49:14,120 --> 08:49:20,520 a list of size 14035 08:49:16,718 --> 08:49:22,680 two sorry I think we just keep following 14036 08:49:20,520 --> 08:49:24,958 our instruction sort the left half all 14037 08:49:22,680 --> 08:49:27,798 right here is a list of size one how do 14038 08:49:24,958 --> 08:49:30,680 I sort a list of size 14039 08:49:27,798 --> 08:49:33,160 one I'm done like it's done so I leave 14040 08:49:30,680 --> 08:49:35,280 this alone what was the next step in the 14041 08:49:33,160 --> 08:49:37,360 story I've just sorted the left half of 14042 08:49:35,280 --> 08:49:40,478 the left half of the left half what 14043 08:49:37,360 --> 08:49:43,120 comes next I sort the right half of the 14044 08:49:40,478 --> 08:49:45,680 left half of the left half and I'm done 14045 08:49:43,120 --> 08:49:49,600 cuz it's just a list of size one what 14046 08:49:45,680 --> 08:49:50,878 comes after this merge so this is where 14047 08:49:49,600 --> 08:49:52,718 it gets a little trippy because you have 14048 08:49:50,878 --> 08:49:54,638 to remember where we're pausing the 14049 08:49:52,718 --> 08:49:56,318 story to do things recursively again and 14050 08:49:54,638 --> 08:49:58,160 again but if I've just sorted the left 14051 08:49:56,318 --> 08:50:00,200 half and I've just sorted the right half 14052 08:49:58,160 --> 08:50:02,318 now I merge them together this is a 14053 08:50:00,200 --> 08:50:04,398 super short list so we don't need um uh 14054 08:50:02,318 --> 08:50:06,680 Carter's help here as before but I think 14055 08:50:04,398 --> 08:50:07,840 the first number I take here is the two 14056 08:50:06,680 --> 08:50:09,878 and then the second number I take 14057 08:50:07,840 --> 08:50:12,360 because it's the only option is the 14058 08:50:09,878 --> 08:50:15,000 seven but what's nice now is that notice 14059 08:50:12,360 --> 08:50:17,558 the left half of the left half is indeed 14060 08:50:15,000 --> 08:50:19,160 sorted because I trivially sorted the 14061 08:50:17,558 --> 08:50:20,760 left half of it and the right half of it 14062 08:50:19,160 --> 08:50:23,000 but then merging is really where the 14063 08:50:20,760 --> 08:50:24,878 magic happens all right again if you rem 14064 08:50:23,000 --> 08:50:27,238 if you rewind now in your mind if I've 14065 08:50:24,878 --> 08:50:29,360 just sorted the left half of the left 14066 08:50:27,238 --> 08:50:33,200 half what happens 14067 08:50:29,360 --> 08:50:35,680 next sort the right half of the left 14068 08:50:33,200 --> 08:50:37,760 half so again you kind of rewind in time 14069 08:50:35,680 --> 08:50:39,798 so how do I do this I've got a list of 14070 08:50:37,760 --> 08:50:42,600 size two I sort the left half the just 14071 08:50:39,798 --> 08:50:45,360 the five done sort the right half four 14072 08:50:42,600 --> 08:50:48,160 done now the interesting part I merge 14073 08:50:45,360 --> 08:50:50,878 the left half and the right half of the 14074 08:50:48,160 --> 08:50:54,398 right half of the left half so what do I 14075 08:50:50,878 --> 08:50:56,238 do four comes down here five comes down 14076 08:50:54,398 --> 08:50:59,680 here and 14077 08:50:56,238 --> 08:51:02,238 now notice what I have left half is 14078 08:50:59,680 --> 08:51:04,680 sorted right half is sorted if you 14079 08:51:02,238 --> 08:51:07,040 rewind in time where is my next step 14080 08:51:04,680 --> 08:51:08,760 three merge the two halves and so this 14081 08:51:07,040 --> 08:51:10,238 is what Carter helped me do before let's 14082 08:51:08,760 --> 08:51:11,920 focus only on the smallest elements just 14083 08:51:10,238 --> 08:51:13,878 so there's less distraction I compare 14084 08:51:11,920 --> 08:51:16,160 the two and the four two comes first so 14085 08:51:13,878 --> 08:51:18,478 let's obviously put that here now I 14086 08:51:16,160 --> 08:51:20,478 compare the new beginning of this list 14087 08:51:18,478 --> 08:51:23,558 and the old beginning of this list four 14088 08:51:20,478 --> 08:51:25,238 obviously comes next and now I compare 14089 08:51:23,558 --> 08:51:27,040 the seven against the five five 14090 08:51:25,238 --> 08:51:29,080 obviously comes next and now lastly I'm 14091 08:51:27,040 --> 08:51:31,160 left with one number so now I'm down to 14092 08:51:29,080 --> 08:51:32,920 the seven so even if you've kind of lost 14093 08:51:31,160 --> 08:51:34,680 track of some of the nuances here if you 14094 08:51:32,920 --> 08:51:36,440 just kind of take a step back we have 14095 08:51:34,680 --> 08:51:38,798 the original right half here still 14096 08:51:36,440 --> 08:51:42,478 untouched but the left half of the 14097 08:51:38,798 --> 08:51:44,558 original input is now indeed sorted all 14098 08:51:42,478 --> 08:51:46,398 by way of doing sorting left half right 14099 08:51:44,558 --> 08:51:48,558 half left half right half but with those 14100 08:51:46,398 --> 08:51:51,680 merges in between all right so if we've 14101 08:51:48,558 --> 08:51:53,478 just sorted the left half we rewind all 14102 08:51:51,680 --> 08:51:56,360 the way to the beginning what do I now 14103 08:51:53,478 --> 08:51:58,200 do all right so sort the right half so 14104 08:51:56,360 --> 08:52:00,160 sort the right half how do I sort a list 14105 08:51:58,200 --> 08:52:02,280 of size four well I first sort the left 14106 08:52:00,160 --> 08:52:04,798 half the one and the six how do I sort a 14107 08:52:02,280 --> 08:52:06,398 list of size two you sort the left half 14108 08:52:04,798 --> 08:52:08,718 just the number one obviously there's no 14109 08:52:06,398 --> 08:52:12,398 work to be done done sorting the left 14110 08:52:08,718 --> 08:52:17,440 half six done sorting the right half now 14111 08:52:12,398 --> 08:52:20,080 what do I do I merge the left half here 14112 08:52:17,440 --> 08:52:21,798 with the right half here and that one's 14113 08:52:20,080 --> 08:52:24,200 pretty straightforward now what do I do 14114 08:52:21,798 --> 08:52:25,680 I've just merged so now I sorted I've 14115 08:52:24,200 --> 08:52:27,600 just sorted the left half of the right 14116 08:52:25,680 --> 08:52:30,558 half so now I sort the right half of the 14117 08:52:27,600 --> 08:52:32,840 right half so I consider the zero done I 14118 08:52:30,558 --> 08:52:35,000 consider the three done I now merge 14119 08:52:32,840 --> 08:52:37,840 these two together zero of course comes 14120 08:52:35,000 --> 08:52:39,600 first then comes the three and now I'm 14121 08:52:37,840 --> 08:52:42,040 at the point of the story where I've 14122 08:52:39,600 --> 08:52:44,360 sorted the left half of the right half 14123 08:52:42,040 --> 08:52:47,080 and the right half of the right half so 14124 08:52:44,360 --> 08:52:48,798 step three is merge and I'll do it again 14125 08:52:47,080 --> 08:52:51,398 like we did with Carter all right one 14126 08:52:48,798 --> 08:52:53,280 and zero obviously the zero comes first 14127 08:52:51,398 --> 08:52:55,360 now compare the one and the three 14128 08:52:53,280 --> 08:52:56,958 obviously the one comes first compare 14129 08:52:55,360 --> 08:52:59,920 the six and the three obviously the 14130 08:52:56,958 --> 08:53:03,238 three and then lastly the sixth so now 14131 08:52:59,920 --> 08:53:04,958 where are we we've taken the left half 14132 08:53:03,238 --> 08:53:06,920 of the whole thing and sorted the left 14133 08:53:04,958 --> 08:53:09,200 and sorted it we then took the right 14134 08:53:06,920 --> 08:53:11,718 half of the whole thing and sorted it so 14135 08:53:09,200 --> 08:53:14,718 now we're at lastly step three for the 14136 08:53:11,718 --> 08:53:16,718 last last time what do we do merge and 14137 08:53:14,718 --> 08:53:19,440 so just to be consistent let me push 14138 08:53:16,718 --> 08:53:21,040 these down and let's compare left hand 14139 08:53:19,440 --> 08:53:22,718 to right hand noticing that they only 14140 08:53:21,040 --> 08:53:25,120 make forward progress none of this back 14141 08:53:22,718 --> 08:53:27,160 and forth comparisons two and zero of 14142 08:53:25,120 --> 08:53:30,120 course the zero so we'll put that in 14143 08:53:27,160 --> 08:53:33,318 place two and one of course the one so 14144 08:53:30,120 --> 08:53:35,958 we put that in place two and three we 14145 08:53:33,318 --> 08:53:38,638 merge in of course the two in this case 14146 08:53:35,958 --> 08:53:42,160 four and three we now merge in the three 14147 08:53:38,638 --> 08:53:45,360 in this case four and six we now merge 14148 08:53:42,160 --> 08:53:49,238 of course the four in place and now we 14149 08:53:45,360 --> 08:53:49,238 compare five and six we keep the 14150 08:53:50,558 --> 08:53:57,160 five 14151 08:53:53,360 --> 08:54:01,280 bug okay well pretend that the five is 14152 08:53:57,160 --> 08:54:04,478 on uh oh this is why all right so now we 14153 08:54:01,280 --> 08:54:07,920 compare the seven and the six sixth is 14154 08:54:04,478 --> 08:54:10,120 gone and lastly seven is the last one in 14155 08:54:07,920 --> 08:54:11,478 place and even though I grant that of 14156 08:54:10,120 --> 08:54:13,478 all the algorithms this is probably the 14157 08:54:11,478 --> 08:54:15,840 hardest one to stay on top of especially 14158 08:54:13,478 --> 08:54:18,318 when I'm doing it as a voiceover realize 14159 08:54:15,840 --> 08:54:20,360 that what we've just done is only those 14160 08:54:18,318 --> 08:54:22,200 three steps recursively we started with 14161 08:54:20,360 --> 08:54:23,958 a list of size eight we sorted the left 14162 08:54:22,200 --> 08:54:25,680 half we sorted the right half and then 14163 08:54:23,958 --> 08:54:27,440 we merged the two together but if you go 14164 08:54:25,680 --> 08:54:29,760 down each of those rabbit holes so to 14165 08:54:27,440 --> 08:54:31,238 speak sorting the left half involves 14166 08:54:29,760 --> 08:54:33,120 sorting the left half of the left half 14167 08:54:31,238 --> 08:54:35,558 and the right half of the left half and 14168 08:54:33,120 --> 08:54:37,478 so forth but this germ of an idea of 14169 08:54:35,558 --> 08:54:39,638 really dividing and conquering the 14170 08:54:37,478 --> 08:54:41,398 problem not such that you're having the 14171 08:54:39,638 --> 08:54:44,040 problem and only dealing with one half 14172 08:54:41,398 --> 08:54:45,760 clearly we're sorting one half and the 14173 08:54:44,040 --> 08:54:48,200 other half and merging them together 14174 08:54:45,760 --> 08:54:50,878 ultimately it does still lead us to the 14175 08:54:48,200 --> 08:54:53,040 same solution and if we visualize the 14176 08:54:50,878 --> 08:54:55,760 remnants of this now if I depict this as 14177 08:54:53,040 --> 08:54:57,318 follows where on the screen here you see 14178 08:54:55,760 --> 08:54:59,478 where the numbers originally started in 14179 08:54:57,318 --> 08:55:01,360 the top row from left to right 14180 08:54:59,478 --> 08:55:03,520 essentially even though this is in a 14181 08:55:01,360 --> 08:55:06,680 different order I divided that list of 14182 08:55:03,520 --> 08:55:08,718 size eight ultimately into eight lists 14183 08:55:06,680 --> 08:55:10,360 of size one and that's where the base 14184 08:55:08,718 --> 08:55:12,520 case kicked in and just said okay we're 14185 08:55:10,360 --> 08:55:15,318 done sorting that and after that 14186 08:55:12,520 --> 08:55:17,478 logically I then sorted I merged two 14187 08:55:15,318 --> 08:55:19,280 lists of size one into many lists of 14188 08:55:17,478 --> 08:55:21,440 size two and those lists of size two 14189 08:55:19,280 --> 08:55:23,760 into lists of size four and then finally 14190 08:55:21,440 --> 08:55:27,360 the list of size four into one big list 14191 08:55:23,760 --> 08:55:28,878 sorted of size eight and so I put forth 14192 08:55:27,360 --> 08:55:31,360 this picture with the little line 14193 08:55:28,878 --> 08:55:34,798 indicators here because how many times 14194 08:55:31,360 --> 08:55:36,958 did I divide divide divide in half or 14195 08:55:34,798 --> 08:55:40,040 really double double double so exponent 14196 08:55:36,958 --> 08:55:43,318 is the opposite oh spoiler uh how many 14197 08:55:40,040 --> 08:55:45,000 times did I divide so three concretely 14198 08:55:43,318 --> 08:55:48,200 but if there's eight elements total and 14199 08:55:45,000 --> 08:55:50,680 there's N More generally it really is a 14200 08:55:48,200 --> 08:55:52,398 matter of dividing and conquering login 14201 08:55:50,680 --> 08:55:54,280 times you start this and you can divide 14202 08:55:52,398 --> 08:55:55,920 one two three times login times or 14203 08:55:54,280 --> 08:55:58,318 conversely you can start here and 14204 08:55:55,920 --> 08:56:01,120 exponentially uh double double double 14205 08:55:58,318 --> 08:56:04,000 three times which is log n but on every 14206 08:56:01,120 --> 08:56:06,318 row every shelf literally I made a fuss 14207 08:56:04,000 --> 08:56:08,238 about pointing my hands only from the 14208 08:56:06,318 --> 08:56:09,600 left to the right constantly advancing 14209 08:56:08,238 --> 08:56:12,398 them such that every time I did those 14210 08:56:09,600 --> 08:56:14,238 merges I touched every element once and 14211 08:56:12,398 --> 08:56:16,920 only once there was none of this back 14212 08:56:14,238 --> 08:56:21,318 and forth back and forth on stage so if 14213 08:56:16,920 --> 08:56:25,520 I'm doing something log n times if I'm 14214 08:56:21,318 --> 08:56:28,718 doing rather N Things log n times what 14215 08:56:25,520 --> 08:56:30,200 would be our Big O formula perhaps N 14216 08:56:28,718 --> 08:56:33,440 Things log n 14217 08:56:30,200 --> 08:56:35,318 times yeah so n log n the order of n log 14218 08:56:33,440 --> 08:56:38,080 n is indeed how we would describe the 14219 08:56:35,318 --> 08:56:40,878 running time of merge sort and so of all 14220 08:56:38,080 --> 08:56:44,120 of the sorts thus far we've seen that 14221 08:56:40,878 --> 08:56:45,878 merge sort here actually is n log n 14222 08:56:44,120 --> 08:56:47,760 which is strictly better than n squ 14223 08:56:45,878 --> 08:56:49,718 which is where both merge uh both 14224 08:56:47,760 --> 08:56:52,440 selection sort and bubble sort landed 14225 08:56:49,718 --> 08:56:54,040 but it's also slower than linear search 14226 08:56:52,440 --> 08:56:55,760 for instance but you would rather expect 14227 08:56:54,040 --> 08:56:58,478 that if you have to do a lot of work up 14228 08:56:55,760 --> 08:56:59,520 front sorting some elements versus just 14229 08:56:58,478 --> 08:57:01,160 searching them you're going to have to 14230 08:56:59,520 --> 08:57:02,680 put in more effort and so the question 14231 08:57:01,160 --> 08:57:04,398 of whether or not you should just search 14232 08:57:02,680 --> 08:57:06,478 something blindly with linear search and 14233 08:57:04,398 --> 08:57:08,680 not bother sorting it really boils down 14234 08:57:06,478 --> 08:57:10,120 to can you afford to spend this amount 14235 08:57:08,680 --> 08:57:11,638 of time and if you're the Googles of the 14236 08:57:10,120 --> 08:57:13,680 world odds are you don't want to be 14237 08:57:11,638 --> 08:57:16,600 searching their database linearly every 14238 08:57:13,680 --> 08:57:18,760 time why because you can sort it once 14239 08:57:16,600 --> 08:57:20,760 and then benefit millions billions of 14240 08:57:18,760 --> 08:57:22,398 people subsequently using something like 14241 08:57:20,760 --> 08:57:24,440 binary search or frankly in practice 14242 08:57:22,398 --> 08:57:26,080 something even fancier and faster than 14243 08:57:24,440 --> 08:57:28,160 binary search but there's always going 14244 08:57:26,080 --> 08:57:30,238 to be this tradeoff you can achieve 14245 08:57:28,160 --> 08:57:32,520 binary search only if the elements are 14246 08:57:30,238 --> 08:57:34,120 sorted how much does it cost you to sort 14247 08:57:32,520 --> 08:57:36,120 them well maybe N squared if you use 14248 08:57:34,120 --> 08:57:39,840 some of the earlier algorithms but it 14249 08:57:36,120 --> 08:57:42,080 turns out n log n is pretty fast as well 14250 08:57:39,840 --> 08:57:45,080 so at the end of the day 14251 08:57:42,080 --> 08:57:47,080 these running times involve tradeoffs 14252 08:57:45,080 --> 08:57:48,840 and indeed in merge sort 2 I should note 14253 08:57:47,080 --> 08:57:51,280 that the lower bound on merge sort is 14254 08:57:48,840 --> 08:57:52,638 also going to be Omega of n log n as 14255 08:57:51,280 --> 08:57:54,280 such we can describe it in terms of our 14256 08:57:52,638 --> 08:57:57,200 Theta notation saying that merge short 14257 08:57:54,280 --> 08:57:59,478 is indeed in Theta of n log n so 14258 08:57:57,200 --> 08:58:01,120 generally speaking probably better to 14259 08:57:59,478 --> 08:58:03,238 use something like merge sort or some 14260 08:58:01,120 --> 08:58:04,920 other algorithm that's in N log n in 14261 08:58:03,238 --> 08:58:06,680 practice most programmers are not 14262 08:58:04,920 --> 08:58:07,878 implementing these sorting algorithms 14263 08:58:06,680 --> 08:58:10,238 themselves odds are they're using a 14264 08:58:07,878 --> 08:58:11,798 library off the shelf that themselves 14265 08:58:10,238 --> 08:58:13,840 have made the decision as to which of 14266 08:58:11,798 --> 08:58:15,080 these algorithms to do but generally 14267 08:58:13,840 --> 08:58:17,398 speaking and we're seeing now this for 14268 08:58:15,080 --> 08:58:19,798 the first time if you want to improve 14269 08:58:17,398 --> 08:58:21,920 time like use less time write faster 14270 08:58:19,798 --> 08:58:24,000 code you got to pay a price and that 14271 08:58:21,920 --> 08:58:25,558 might be your human time just takes you 14272 08:58:24,000 --> 08:58:27,200 more time to code up something more 14273 08:58:25,558 --> 08:58:29,760 sophisticated more difficult to 14274 08:58:27,200 --> 08:58:32,398 implement or you need to spend something 14275 08:58:29,760 --> 08:58:33,878 like space and as these shelves suggest 14276 08:58:32,398 --> 08:58:35,638 that too is one of the key details of 14277 08:58:33,878 --> 08:58:38,040 merge sort you can't just have the 14278 08:58:35,638 --> 08:58:40,478 elements swapping in place you need at 14279 08:58:38,040 --> 08:58:42,280 least an auxiliary array so that when 14280 08:58:40,478 --> 08:58:44,040 you do the merging you have a place to 14281 08:58:42,280 --> 08:58:45,878 put them and this is excessive this 14282 08:58:44,040 --> 08:58:47,360 amount of memory I could have just gone 14283 08:58:45,878 --> 08:58:48,520 back and forth between top shelf and 14284 08:58:47,360 --> 08:58:50,520 bottom shelf but it's a little more 14285 08:58:48,520 --> 08:58:52,878 interesting to go top down but you do 14286 08:58:50,520 --> 08:58:54,878 need more space back in the day decades 14287 08:58:52,878 --> 08:58:56,318 ago space was really expensive and so 14288 08:58:54,878 --> 08:58:59,040 you know what it might have been better 14289 08:58:56,318 --> 08:59:00,958 to not use merge sort use bubble sort or 14290 08:58:59,040 --> 08:59:03,080 uh selection sort even or some other 14291 08:59:00,958 --> 08:59:04,798 algorithm alog together nowadays space 14292 08:59:03,080 --> 08:59:06,318 is relatively cheap and so these are 14293 08:59:04,798 --> 08:59:08,920 more acceptable trade-offs but it 14294 08:59:06,318 --> 08:59:10,398 totally depends on the application the 14295 08:59:08,920 --> 08:59:12,000 very last thing we thought we'd do is 14296 08:59:10,398 --> 08:59:13,478 show you an actual compar comparison of 14297 08:59:12,000 --> 08:59:15,000 some of these sorting algorithms it's 14298 08:59:13,478 --> 08:59:18,160 about 60 seconds long and it will 14299 08:59:15,000 --> 08:59:21,080 compare for you uh selection sort bubble 14300 08:59:18,160 --> 08:59:23,238 sort and merge sort in parallel 14301 08:59:21,080 --> 08:59:25,120 simultaneously uh with some fun sorting 14302 08:59:23,238 --> 08:59:27,878 music showing you ultimately what it 14303 08:59:25,120 --> 08:59:30,798 really means to be an O of n s or better 14304 08:59:27,878 --> 08:59:35,878 yet Big O of n 14305 08:59:30,798 --> 08:59:38,650 Logan selection on the top bubble on the 14306 08:59:35,878 --> 08:59:40,360 bottom merge in the 14307 08:59:38,650 --> 08:59:42,500 [Music] 14308 08:59:40,360 --> 08:59:49,160 middle 14309 08:59:42,500 --> 08:59:49,160 [Music] 14310 09:00:10,780 --> 09:00:14,169 [Applause] 14311 09:00:16,570 --> 09:00:23,509 [Music] 14312 09:00:32,558 --> 09:00:37,160 all right that's it for cs50 we'll see 14313 09:00:34,798 --> 09:00:37,160 you next 14314 09:00:38,260 --> 09:00:42,440 [Music] 14315 09:00:40,238 --> 09:00:46,349 time 14316 09:00:42,440 --> 09:00:46,349 [Music] 14317 09:00:52,340 --> 09:01:00,669 [Music] 14318 09:01:16,950 --> 09:01:35,589 [Music] 14319 09:01:40,160 --> 09:01:43,160 e 14320 09:01:49,440 --> 09:01:52,550 [Music] 14321 09:01:59,370 --> 09:02:10,889 [Music] 14322 09:02:18,830 --> 09:02:30,520 [Music] 14323 09:02:27,600 --> 09:02:32,440 all right this is cs50 and this is week 14324 09:02:30,520 --> 09:02:34,200 four and if you think back a few weeks 14325 09:02:32,440 --> 09:02:35,600 ago already in week zero we started 14326 09:02:34,200 --> 09:02:37,958 talking about like what images are and 14327 09:02:35,600 --> 09:02:40,360 we talked about representation of images 14328 09:02:37,958 --> 09:02:41,878 as this grid of pixels and each pixel 14329 09:02:40,360 --> 09:02:43,558 has like some pattern of bits that 14330 09:02:41,878 --> 09:02:45,000 defines its color well it turns out 14331 09:02:43,558 --> 09:02:46,760 today we'll take a deeper look 14332 09:02:45,000 --> 09:02:49,160 underneath the hood at how things like 14333 09:02:46,760 --> 09:02:50,798 images and so much more is actually 14334 09:02:49,160 --> 09:02:53,878 implemented using just these zeros and 14335 09:02:50,798 --> 09:02:55,478 ones and how now as a programmer you can 14336 09:02:53,878 --> 09:02:57,680 actually harness that for better for 14337 09:02:55,478 --> 09:02:59,398 worse to better understand and better 14338 09:02:57,680 --> 09:03:01,080 manipulate what's going on inside of a 14339 09:02:59,398 --> 09:03:03,280 computer's memory using a language like 14340 09:03:01,080 --> 09:03:04,520 C in fact even this bowl of stress balls 14341 09:03:03,280 --> 09:03:06,160 that we keep happening is just a 14342 09:03:04,520 --> 09:03:08,200 photograph of course but if you think 14343 09:03:06,160 --> 09:03:10,238 back to week zero if you sort of enhance 14344 09:03:08,200 --> 09:03:12,040 enhance enhance this image like they do 14345 09:03:10,238 --> 09:03:13,798 in the movies it actually doesn't work 14346 09:03:12,040 --> 09:03:16,080 out the way you would think from 14347 09:03:13,798 --> 09:03:18,638 Hollywood as I keep continue to zoom in 14348 09:03:16,080 --> 09:03:20,360 and zoom in and zoom in on a screen like 14349 09:03:18,638 --> 09:03:22,360 this you'll see that yes it gets bigger 14350 09:03:20,360 --> 09:03:24,558 but if it gets too big what do you start 14351 09:03:22,360 --> 09:03:26,558 to notice the so-called pixelation and 14352 09:03:24,558 --> 09:03:29,120 indeed you can see the individual dots 14353 09:03:26,558 --> 09:03:31,680 so next time you watch some uh show or 14354 09:03:29,120 --> 09:03:33,398 movie on uh TV that has this sort of 14355 09:03:31,680 --> 09:03:34,840 notion of enhancing you know there's 14356 09:03:33,398 --> 09:03:36,718 actually a finite limit there you can 14357 09:03:34,840 --> 09:03:38,478 only enhance so far as there's actually 14358 09:03:36,718 --> 09:03:40,478 information there but once you zoom in 14359 09:03:38,478 --> 09:03:42,080 to a certain level like this like that's 14360 09:03:40,478 --> 09:03:43,958 all that there you're not going to see 14361 09:03:42,080 --> 09:03:46,000 the glint of the suspect in some crime 14362 09:03:43,958 --> 09:03:47,840 drama in their eye just because you've 14363 09:03:46,000 --> 09:03:50,360 enhanced the image there's only a finite 14364 09:03:47,840 --> 09:03:52,798 amount of information actually there but 14365 09:03:50,360 --> 09:03:54,000 we'll see today too that by 14366 09:03:52,798 --> 09:03:55,398 understanding what's going on inside of 14367 09:03:54,000 --> 09:03:57,280 a computer's memory we can start to 14368 09:03:55,398 --> 09:03:59,558 represent and even create and code more 14369 09:03:57,280 --> 09:04:02,200 interesting things so for instance here 14370 09:03:59,558 --> 09:04:05,160 is a bit map if you will which is a term 14371 09:04:02,200 --> 09:04:06,760 of art a bit map is a type of image and 14372 09:04:05,160 --> 09:04:08,360 it's a map of bits in the sense that you 14373 09:04:06,760 --> 09:04:10,200 have this coordinate system of up top 14374 09:04:08,360 --> 09:04:12,558 down left right at least in this artist 14375 09:04:10,200 --> 09:04:15,520 representation here and suppose that 14376 09:04:12,558 --> 09:04:18,080 maybe we all defi decide as a as as the 14377 09:04:15,520 --> 09:04:20,280 world that one shall represent the color 14378 09:04:18,080 --> 09:04:23,360 white and zero shall represent the color 14379 09:04:20,280 --> 09:04:27,120 black what might this map of bits this 14380 09:04:23,360 --> 09:04:31,280 bit map actually be can you see through 14381 09:04:27,120 --> 09:04:33,040 it yeah it is indeed a smiley face so an 14382 09:04:31,280 --> 09:04:35,398 amazing eye if I actually turn all of 14383 09:04:33,040 --> 09:04:36,958 the ones to White just to visualize this 14384 09:04:35,398 --> 09:04:38,200 you'll see indeed this is what was 14385 09:04:36,958 --> 09:04:40,080 embedded there but of course on our 14386 09:04:38,200 --> 09:04:41,718 computer monitors and phones we have 14387 09:04:40,080 --> 09:04:44,040 this grid of square is this grid of 14388 09:04:41,718 --> 09:04:45,840 pixels so indeed if you were to actually 14389 09:04:44,040 --> 09:04:47,558 see on your screen a smiley face like a 14390 09:04:45,840 --> 09:04:49,000 black and white one at that what's 14391 09:04:47,558 --> 09:04:50,680 probably going on underneath the hood is 14392 09:04:49,000 --> 09:04:53,200 just some pattern of zeros and ones and 14393 09:04:50,680 --> 09:04:55,558 maybe single bits one bit color if you 14394 09:04:53,200 --> 09:04:58,120 will where one here represents white and 14395 09:04:55,558 --> 09:04:59,840 zero represents black so if you kind of 14396 09:04:58,120 --> 09:05:01,840 like this thing it turns out you can do 14397 09:04:59,840 --> 09:05:03,558 pretty uh pretty beautiful pretty 14398 09:05:01,840 --> 09:05:05,318 interesting pretty artistically inclined 14399 09:05:03,558 --> 09:05:08,200 things if you go to this URL at your 14400 09:05:05,318 --> 09:05:09,718 leisure cs50. lart it'll actually 14401 09:05:08,200 --> 09:05:11,120 redirect you to a Google spreadsheet 14402 09:05:09,718 --> 09:05:12,958 that we've made in advance and we've 14403 09:05:11,120 --> 09:05:15,520 kind of Shrunk the rows and columns to 14404 09:05:12,958 --> 09:05:16,958 resemble a grid of pixels tiny little 14405 09:05:15,520 --> 09:05:20,160 squares all of which are white by 14406 09:05:16,958 --> 09:05:21,840 default not unlike this easel here that 14407 09:05:20,160 --> 09:05:23,200 we have a couple of volunteers working 14408 09:05:21,840 --> 09:05:24,478 away at in fact would you guys like to 14409 09:05:23,200 --> 09:05:27,000 come forward for a moment and say a 14410 09:05:24,478 --> 09:05:29,080 quick hello before we come back to you 14411 09:05:27,000 --> 09:05:31,440 uh hello my name is Daniel I'm from 14412 09:05:29,080 --> 09:05:33,958 Chicago welcome to Daniel and hi 14413 09:05:31,440 --> 09:05:36,280 everyone I'm Adam and I'm from chindon 14414 09:05:33,958 --> 09:05:38,238 Tobago nice well welcome to you both 14415 09:05:36,280 --> 09:05:40,280 thank you you'll see that in their hands 14416 09:05:38,238 --> 09:05:41,680 are actually a whole bunch of pixels uh 14417 09:05:40,280 --> 09:05:42,840 Post-it notes that we've handed them in 14418 09:05:41,680 --> 09:05:44,238 in advance so if you don't mind we'll 14419 09:05:42,840 --> 09:05:45,760 come back to you in a couple of minutes 14420 09:05:44,238 --> 09:05:48,280 and see what they've created if you will 14421 09:05:45,760 --> 09:05:49,360 on this grid of white paper much like 14422 09:05:48,280 --> 09:05:51,000 you could create on this Google 14423 09:05:49,360 --> 09:05:53,280 spreadsheet in fact feel free to send us 14424 09:05:51,000 --> 09:05:57,200 your Creations if so inclined uh via the 14425 09:05:53,280 --> 09:05:59,000 URL you'll get at cs50. lart now let's 14426 09:05:57,200 --> 09:06:00,360 come back to week zero where we Define 14427 09:05:59,000 --> 09:06:02,318 some of the building blocks for images 14428 09:06:00,360 --> 09:06:04,160 we talked about RGB which is just red 14429 09:06:02,318 --> 09:06:05,958 green blue and it's just one of the 14430 09:06:04,160 --> 09:06:07,878 systems a popular system via which you 14431 09:06:05,958 --> 09:06:11,160 can represent any color of the rainbow 14432 09:06:07,878 --> 09:06:13,040 using some combination of red and green 14433 09:06:11,160 --> 09:06:14,960 and blue and if any of you are 14434 09:06:13,040 --> 09:06:17,558 artistically inclined or have used 14435 09:06:14,960 --> 09:06:19,558 Photoshop or similar programs you might 14436 09:06:17,558 --> 09:06:21,878 typically have some means of selecting a 14437 09:06:19,558 --> 09:06:23,918 color by some grid like this but down 14438 09:06:21,878 --> 09:06:27,122 here notice there's explicit mentions of 14439 09:06:23,918 --> 09:06:30,238 the types of color systems in use R GB 14440 09:06:27,122 --> 09:06:32,398 and in fact here you see 0000 0 and up 14441 09:06:30,238 --> 09:06:33,840 here under new you see the color black 14442 09:06:32,398 --> 09:06:35,960 and that implies that if you have no red 14443 09:06:33,840 --> 09:06:38,360 no green no blue well that indeed would 14444 09:06:35,960 --> 09:06:40,040 represent by convention the color black 14445 09:06:38,360 --> 09:06:42,800 by contrast if we play around with 14446 09:06:40,040 --> 09:06:45,000 Photoshop or similar program if you have 14447 09:06:42,800 --> 09:06:48,680 a lot of red a lot of green and a lot of 14448 09:06:45,000 --> 09:06:50,960 blue for instance 255 255 255 like 14449 09:06:48,680 --> 09:06:53,122 really crank it up to the max value you 14450 09:06:50,960 --> 09:06:55,398 can represent with eight bits per week 14451 09:06:53,122 --> 09:06:57,320 zero well then it turns out you get the 14452 09:06:55,398 --> 09:06:59,800 color white here and we can play with 14453 09:06:57,320 --> 09:07:03,320 these numbers endlessly for instance if 14454 09:06:59,800 --> 09:07:06,000 we use 255 of red but zero green and 14455 09:07:03,320 --> 09:07:07,680 zero blue not surprisingly the square at 14456 09:07:06,000 --> 09:07:09,878 the top of the screen Becomes Of course 14457 09:07:07,680 --> 09:07:12,160 red entirely because it's all red and no 14458 09:07:09,878 --> 09:07:15,200 green no blue if we change it instead to 14459 09:07:12,160 --> 09:07:17,122 255 for green but zero for red and blue 14460 09:07:15,200 --> 09:07:19,200 of course we get green and then lastly 14461 09:07:17,122 --> 09:07:21,438 if we crank up the blue but leave red 14462 09:07:19,200 --> 09:07:23,680 and green as zero we of course get blue 14463 09:07:21,438 --> 09:07:25,000 but all this while down here highlighted 14464 09:07:23,680 --> 09:07:27,078 is something that maybe some of you have 14465 09:07:25,000 --> 09:07:28,680 seen before like some combination of 14466 09:07:27,078 --> 09:07:31,122 numbers and letters if any of you have 14467 09:07:28,680 --> 09:07:32,718 made personal web pages or use programs 14468 09:07:31,122 --> 09:07:34,960 like Photoshop you might have used these 14469 09:07:32,718 --> 09:07:37,398 so-called color codes so indeed the 14470 09:07:34,960 --> 09:07:40,200 world has this convention whereby using 14471 09:07:37,398 --> 09:07:42,160 six digits or sometimes three you can 14472 09:07:40,200 --> 09:07:45,040 represent a little more succinctly some 14473 09:07:42,160 --> 09:07:48,800 amount of red green blue and you'll see 14474 09:07:45,040 --> 09:07:51,718 here maybe by inference that if RGB is0 14475 09:07:48,800 --> 09:07:53,640 0 255 respectively perhaps where we're 14476 09:07:51,718 --> 09:07:56,640 going with this is that 14477 09:07:53,640 --> 09:07:58,800 0000 FF is just an alternative way of 14478 09:07:56,640 --> 09:08:02,078 expressing the exact same idea no red no 14479 09:07:58,800 --> 09:08:03,640 green and a lot of blue but why is that 14480 09:08:02,078 --> 09:08:05,598 and in fact we'll come full circle here 14481 09:08:03,640 --> 09:08:06,918 to introducing something that we could 14482 09:08:05,598 --> 09:08:08,960 have done in week zero but it doesn't 14483 09:08:06,918 --> 09:08:11,278 really solve a problem then but today as 14484 09:08:08,960 --> 09:08:13,160 we focus more on images and on memory 14485 09:08:11,278 --> 09:08:15,238 itself turns out understanding these 14486 09:08:13,160 --> 09:08:16,718 patterns is pretty useful so back in 14487 09:08:15,238 --> 09:08:19,160 week zero we talked of course about 14488 09:08:16,718 --> 09:08:21,800 binary and binary by implying two only 14489 09:08:19,160 --> 09:08:23,640 gives you two digits zero and one you 14490 09:08:21,800 --> 09:08:25,598 and I as humans almost always use the 14491 09:08:23,640 --> 09:08:28,438 decimal system in normal conversation 14492 09:08:25,598 --> 09:08:30,960 deck meaning 10 so we have 0 through n 14493 09:08:28,438 --> 09:08:34,598 instead if a human like us wants to 14494 09:08:30,960 --> 09:08:36,800 count up as high as 10 or 11 or 12 we 14495 09:08:34,598 --> 09:08:39,758 don't have a digit per se for 10 11 and 14496 09:08:36,800 --> 09:08:43,360 12 we start reusing digits so it's 1 0 1 14497 09:08:39,758 --> 09:08:45,878 1 1 2 and so forth but in other systems 14498 09:08:43,360 --> 09:08:49,360 not binary not decimal but systems 14499 09:08:45,878 --> 09:08:51,558 called heximal hex implying 16 there are 14500 09:08:49,360 --> 09:08:53,640 actually more digits than these which 14501 09:08:51,558 --> 09:08:55,960 might come as a surprise um it's not 14502 09:08:53,640 --> 09:08:57,558 pairs of digits like in decimal single 14503 09:08:55,960 --> 09:08:58,960 digits and frankly it doesn't really 14504 09:08:57,558 --> 09:08:59,918 matter what the digits are because at 14505 09:08:58,960 --> 09:09:01,360 the end of the day these are just 14506 09:08:59,918 --> 09:09:03,278 symbols that you and I immediately 14507 09:09:01,360 --> 09:09:05,360 associate with some notion of math but 14508 09:09:03,278 --> 09:09:07,680 just Strokes on the screen that 14509 09:09:05,360 --> 09:09:10,078 represent some have represent some 14510 09:09:07,680 --> 09:09:11,040 actual value so it turns out that by 14511 09:09:10,078 --> 09:09:13,800 convention 14512 09:09:11,040 --> 09:09:15,800 when you want more than n 10 digits 0 14513 09:09:13,800 --> 09:09:20,320 through 9 you start using letters of the 14514 09:09:15,800 --> 09:09:21,878 English alphabet A B C D E and F and you 14515 09:09:20,320 --> 09:09:23,360 can represent them in lower case it's 14516 09:09:21,878 --> 09:09:24,718 case insensitive so it doesn't really 14517 09:09:23,360 --> 09:09:26,680 matter you might see it in upper case or 14518 09:09:24,718 --> 09:09:29,278 lower case but this is how you can count 14519 09:09:26,680 --> 09:09:31,480 Beyond N9 not using decimal but using 14520 09:09:29,278 --> 09:09:33,278 indeed something called hexadecimal if 14521 09:09:31,480 --> 09:09:35,438 we get really technical this is also 14522 09:09:33,278 --> 09:09:37,238 known as base 16 and it's the same idea 14523 09:09:35,438 --> 09:09:40,918 as week zero where instead of using base 14524 09:09:37,238 --> 09:09:43,598 2 for binary base 10 for decimal use 16 14525 09:09:40,918 --> 09:09:45,640 as the base for heximal and so if we run 14526 09:09:43,598 --> 09:09:48,278 through just some simple examples here 14527 09:09:45,640 --> 09:09:51,640 in the world of heximal your columns are 14528 09:09:48,278 --> 09:09:54,640 just powers of 16 16 to the 0er 16 to 14529 09:09:51,640 --> 09:09:56,918 the 1 16 to the 2 and so forth but in 14530 09:09:54,640 --> 09:09:58,480 the world of hex we usually at least 14531 09:09:56,918 --> 09:10:00,878 thus far and today we'll see just pairs 14532 09:09:58,480 --> 09:10:03,160 of digits like this so here for instance 14533 09:10:00,878 --> 09:10:05,238 is the ones column and the 16's column 14534 09:10:03,160 --> 09:10:06,960 if we multiply that out so if you wanted 14535 09:10:05,238 --> 09:10:09,758 to represent the number you and I know 14536 09:10:06,960 --> 09:10:12,758 in in uh the real world as zero in 14537 09:10:09,758 --> 09:10:14,558 heximal it would just be 0 0 if you want 14538 09:10:12,758 --> 09:10:19,122 to represent the number one it would be 14539 09:10:14,558 --> 09:10:23,520 01 and from there we get 02 03 04 0 five 14540 09:10:19,122 --> 09:10:25,000 06 07 08 09 now things get potentially 14541 09:10:23,520 --> 09:10:27,718 interesting in decimal it would 14542 09:10:25,000 --> 09:10:31,878 obviously become 10 but in heximal it 14543 09:10:27,718 --> 09:10:34,438 just becomes 0 a and then 0 B which is 14544 09:10:31,878 --> 09:10:37,320 to say if I rewind after nine comes in 14545 09:10:34,438 --> 09:10:39,238 heximal if I pronounce it in in decimal 14546 09:10:37,320 --> 09:10:43,160 this is how you'd represent 10 this is 14547 09:10:39,238 --> 09:10:46,800 how you'd represent 11 12 13 14 and then 14548 09:10:43,160 --> 09:10:49,040 lastly in heximal the 16th value is f 14549 09:10:46,800 --> 09:10:52,160 which is just always going to represent 14550 09:10:49,040 --> 09:10:53,918 15 so where to how do we connect this to 14551 09:10:52,160 --> 09:10:57,078 some of the past math well once you get 14552 09:10:53,918 --> 09:10:59,558 to Zer F in heximal if f is the highest 14553 09:10:57,078 --> 09:11:02,360 you can count just like in decimal nine 14554 09:10:59,558 --> 09:11:05,200 is the highest you can count what comes 14555 09:11:02,360 --> 09:11:07,520 next if this is 15 I claim how do I 14556 09:11:05,200 --> 09:11:09,360 represent 16 in heximal with what 14557 09:11:07,520 --> 09:11:12,040 pattern of 14558 09:11:09,360 --> 09:11:15,960 symbols what pattern of symbols for 14559 09:11:12,040 --> 09:11:17,598 heximal yeah so one Z not 10 even though 14560 09:11:15,960 --> 09:11:20,640 you might read it like that as a typical 14561 09:11:17,598 --> 09:11:22,122 human but one zero because why well even 14562 09:11:20,640 --> 09:11:23,878 if this is completely new to you the 14563 09:11:22,122 --> 09:11:25,800 whole column system the places are 14564 09:11:23,878 --> 09:11:28,520 exactly the same intuitively so you need 14565 09:11:25,800 --> 09:11:30,122 a one in the 16's place and a zero in 14566 09:11:28,520 --> 09:11:32,238 the ones place and we won't count all 14567 09:11:30,122 --> 09:11:34,918 the way up to 255 but we count if we 14568 09:11:32,238 --> 09:11:38,122 count a little higher this would be 1 Z 14569 09:11:34,918 --> 09:11:43,200 AKA 16 in decimal this would be 1 one 14570 09:11:38,122 --> 09:11:45,238 AKA 17 in decimal and then 18 19 20 and 14571 09:11:43,200 --> 09:11:48,040 so forth dot dot dot and we can count 14572 09:11:45,238 --> 09:11:50,840 all the way up to FF CU if f is the 14573 09:11:48,040 --> 09:11:52,878 biggest digit in hexadecimal FF is 14574 09:11:50,840 --> 09:11:55,122 indeed as high as we can count and if 14575 09:11:52,878 --> 09:11:58,800 each F represents 15 well let's just do 14576 09:11:55,122 --> 09:12:01,398 the math like in week zero so 16 * F + 1 14577 09:11:58,800 --> 09:12:03,320 * f is how all of us learned to do uh 14578 09:12:01,398 --> 09:12:07,878 math in grade school even though not in 14579 09:12:03,320 --> 09:12:11,840 heximal that's of course 16 * 15 + 1 * 14580 09:12:07,878 --> 09:12:14,840 15 multiply that out you get 240 plus 15 14581 09:12:11,840 --> 09:12:19,320 and air go you can count as high as 14582 09:12:14,840 --> 09:12:20,680 255 using two heximal digits now this is 14583 09:12:19,320 --> 09:12:22,160 not the kind of thing where like this is 14584 09:12:20,680 --> 09:12:23,918 going to be an interesting exercise 14585 09:12:22,160 --> 09:12:25,398 mentally to ever convert in your head 14586 09:12:23,918 --> 09:12:27,718 generally you'll get used to the fact 14587 09:12:25,398 --> 09:12:29,598 that after nine comes a and the biggest 14588 09:12:27,718 --> 09:12:30,800 digit is f and you'll just start to see 14589 09:12:29,598 --> 09:12:32,960 patterns like this in the worlds of 14590 09:12:30,800 --> 09:12:36,520 Photoshop web pages in a few weeks and 14591 09:12:32,960 --> 09:12:39,000 Beyond but like why is heximal useful 14592 09:12:36,520 --> 09:12:41,122 like why are we complicating the world 14593 09:12:39,000 --> 09:12:42,800 and adding on top of decimal something 14594 09:12:41,122 --> 09:12:45,040 else well it turns out that a single 14595 09:12:42,800 --> 09:12:47,840 decimal digit like f the biggest one for 14596 09:12:45,040 --> 09:12:49,960 instance is 15 and here let me just 14597 09:12:47,840 --> 09:12:53,480 propose a bit of Mental Math how many 14598 09:12:49,960 --> 09:12:56,000 bits do you need to represent the number 14599 09:12:53,480 --> 09:12:59,598 15 in binary if you've got the ones 14600 09:12:56,000 --> 09:13:01,122 place two's Place fours and so forth how 14601 09:12:59,598 --> 09:13:03,960 many bits 14602 09:13:01,122 --> 09:13:06,718 total so fewer than five to count this 14603 09:13:03,960 --> 09:13:09,160 highest 15 I think but 14604 09:13:06,718 --> 09:13:13,398 close someone 14605 09:13:09,160 --> 09:13:15,078 else sing in hand yeah so four bits I 14606 09:13:13,398 --> 09:13:18,200 think suffice because if you want to 14607 09:13:15,078 --> 09:13:20,640 count as high as F that is to say 15 I 14608 09:13:18,200 --> 09:13:22,360 think if you have four bits you can do 14609 09:13:20,640 --> 09:13:24,480 that because if over here is the one's 14610 09:13:22,360 --> 09:13:26,558 place from week zero for binary this is 14611 09:13:24,480 --> 09:13:28,360 the two's place this is The Four's place 14612 09:13:26,558 --> 09:13:33,200 this is the e place do out some quick 14613 09:13:28,360 --> 09:13:35,598 math so 8 + 4 is 12 + 2 is 14 + 1 is 15 14614 09:13:33,200 --> 09:13:37,718 so it turns out that by convenience 14615 09:13:35,598 --> 09:13:40,520 heximal digits can just be represented 14616 09:13:37,718 --> 09:13:43,040 consistently with four bits or fewer but 14617 09:13:40,520 --> 09:13:44,718 four and four of course is half of eight 14618 09:13:43,040 --> 09:13:46,320 and eight is like everywhere like eight 14619 09:13:44,718 --> 09:13:48,360 bits is a bite which is again just a 14620 09:13:46,320 --> 09:13:50,078 convention we've seen and so the reason 14621 09:13:48,360 --> 09:13:52,840 that you see hexad desmal in the world 14622 09:13:50,078 --> 09:13:55,398 of Photoshop and eventually web pages is 14623 09:13:52,840 --> 09:13:57,238 it actually just Maps really nicely to 14624 09:13:55,398 --> 09:13:59,640 expressing binary numbers more 14625 09:13:57,238 --> 09:14:03,238 succinctly with a fixed number of digits 14626 09:13:59,640 --> 09:14:06,878 so for instance anytime you see 11 one 14627 09:14:03,238 --> 09:14:08,238 one one 1111 in the world as binary you 14628 09:14:06,878 --> 09:14:10,598 know what that's a little tedious to 14629 09:14:08,238 --> 09:14:12,598 both say and write you can represent 14630 09:14:10,598 --> 09:14:15,680 more succinctly 14631 09:14:12,598 --> 09:14:20,122 any uh group of four one bits more 14632 09:14:15,680 --> 09:14:23,320 succinctly in heximal as just F so 111 14633 09:14:20,122 --> 09:14:25,160 1111 in binary more succinctly and more 14634 09:14:23,320 --> 09:14:27,122 commonly now in the world of Photoshop 14635 09:14:25,160 --> 09:14:29,040 memory images and the like is 14636 09:14:27,122 --> 09:14:30,598 represented more succinctly as FF and 14637 09:14:29,040 --> 09:14:32,840 that's why because it just Maps really 14638 09:14:30,598 --> 09:14:36,200 nicely to four bits and so we can be a 14639 09:14:32,840 --> 09:14:38,398 little more succinct so any questions on 14640 09:14:36,200 --> 09:14:40,398 heximal which is just another way of 14641 09:14:38,398 --> 09:14:43,078 representing information but using the 14642 09:14:40,398 --> 09:14:45,398 same grade school approach yeah good 14643 09:14:43,078 --> 09:14:48,360 question if you represent 15 with f it 14644 09:14:45,398 --> 09:14:50,558 would use four bits so base systems are 14645 09:14:48,360 --> 09:14:52,758 really just a way for us humans on paper 14646 09:14:50,558 --> 09:14:55,160 or on screens to represent information 14647 09:14:52,758 --> 09:14:57,278 if F represents the decimal number 15 14648 09:14:55,160 --> 09:15:00,320 the computer underneath the hood has to 14649 09:14:57,278 --> 09:15:02,680 use four bits to represent it so one 14650 09:15:00,320 --> 09:15:05,558 heximal digit by convention always 14651 09:15:02,680 --> 09:15:07,718 implies four bits underneath the hood so 14652 09:15:05,558 --> 09:15:10,680 therefore if you have two heximal digits 14653 09:15:07,718 --> 09:15:12,718 like 0 0 that means eight zero bits 14654 09:15:10,680 --> 09:15:15,398 underneath the hood like for red or for 14655 09:15:12,718 --> 09:15:17,360 green if you see FF now we know that's 14656 09:15:15,398 --> 09:15:19,758 four one bits and another four one Bits 14657 09:15:17,360 --> 09:15:22,200 And if we do out the math that's 255 14658 09:15:19,758 --> 09:15:26,680 that's why in Photoshop 14659 09:15:22,200 --> 09:15:29,078 000000 FF means no red no green and 14660 09:15:26,680 --> 09:15:30,680 255 of blue and it's just way more 14661 09:15:29,078 --> 09:15:33,438 succinct than writing out like what 8 14662 09:15:30,680 --> 09:15:35,520 plus 8 plus 8 24 zeros and ones and it's 14663 09:15:33,438 --> 09:15:37,320 just cleaner than even using decimal 14664 09:15:35,520 --> 09:15:39,438 when you're using units of eight which 14665 09:15:37,320 --> 09:15:40,680 again computers just use everywhere so 14666 09:15:39,438 --> 09:15:42,640 it's just another system it's not one 14667 09:15:40,680 --> 09:15:44,918 you need to dwell on very much but again 14668 09:15:42,640 --> 09:15:46,718 it's fundamentally no different from 14669 09:15:44,918 --> 09:15:49,480 binary or decimal we're just using a 14670 09:15:46,718 --> 09:15:52,122 slightly different base no all right 14671 09:15:49,480 --> 09:15:54,122 well we had this blank canvas here and I 14672 09:15:52,122 --> 09:15:55,960 think uh are you two perhaps ready to 14673 09:15:54,122 --> 09:15:57,078 reveal for the world what you've created 14674 09:15:55,960 --> 09:15:59,718 do you want to go ahead and I'll I'll 14675 09:15:57,078 --> 09:16:03,000 swivel it around for you all right here 14676 09:15:59,718 --> 09:16:07,278 we go big reveal and today's pixel 14677 09:16:03,000 --> 09:16:09,918 art a round of applause if we 14678 09:16:07,278 --> 09:16:11,122 could very nicely done well thank you 14679 09:16:09,918 --> 09:16:12,640 both if you want to come up after and 14680 09:16:11,122 --> 09:16:14,122 tear this off and bring it home you're 14681 09:16:12,640 --> 09:16:15,680 welcome to and keep the Post-it notes 14682 09:16:14,122 --> 09:16:18,878 too well thank you to our volunteers 14683 09:16:15,680 --> 09:16:20,160 there let's now translate this to really 14684 09:16:18,878 --> 09:16:21,918 more technical worlds where we're going 14685 09:16:20,160 --> 09:16:23,598 to see and consider it more often 14686 09:16:21,918 --> 09:16:25,122 because in fact sometimes when you've 14687 09:16:23,598 --> 09:16:27,278 had error messages over the past few 14688 09:16:25,122 --> 09:16:29,480 weeks from clang the compiler you might 14689 09:16:27,278 --> 09:16:31,000 have even seen evidence of heximal we 14690 09:16:29,480 --> 09:16:32,878 didn't call it out it wasn't useful to 14691 09:16:31,000 --> 09:16:36,200 know at the time but it turns out a lot 14692 09:16:32,878 --> 09:16:39,320 of programs use and a lot of code uses 14693 09:16:36,200 --> 09:16:41,598 heximal for those reasons of more prec 14694 09:16:39,320 --> 09:16:43,278 more representation so for instance 14695 09:16:41,598 --> 09:16:44,960 where else might we see it well here's 14696 09:16:43,278 --> 09:16:46,640 that picture we keep pulling up of our 14697 09:16:44,960 --> 09:16:48,758 computer's memory and each of these 14698 09:16:46,640 --> 09:16:50,398 squares in this grid represents a bite 14699 09:16:48,758 --> 09:16:52,160 sort of top left to bottom right in the 14700 09:16:50,398 --> 09:16:54,480 computer's memory but again just an 14701 09:16:52,160 --> 09:16:56,438 artist's representation a few weeks ago 14702 09:16:54,480 --> 09:16:58,238 I claimed that each of these bytes can 14703 09:16:56,438 --> 09:17:00,438 be numbered of course like this is bite 14704 09:16:58,238 --> 09:17:02,078 zero at top left then bite one then bite 14705 09:17:00,438 --> 09:17:04,558 two then bite two billion if you have 14706 09:17:02,078 --> 09:17:07,640 two gigabytes of memory and so we could 14707 09:17:04,558 --> 09:17:11,160 just number them like this 0 through 15 14708 09:17:07,640 --> 09:17:13,480 on up 16 17 18 and so forth but per the 14709 09:17:11,160 --> 09:17:15,680 reasons earlier it's just more common in 14710 09:17:13,480 --> 09:17:17,398 computer systems and in software to 14711 09:17:15,680 --> 09:17:19,800 actually use hexadecimal just to 14712 09:17:17,398 --> 09:17:22,680 describe the locations of the addresses 14713 09:17:19,800 --> 09:17:24,160 of things in memory so instead a typical 14714 09:17:22,680 --> 09:17:27,320 programmer or computer scientist would 14715 09:17:24,160 --> 09:17:29,040 call these first 16 bytes 0 through F 14716 09:17:27,320 --> 09:17:32,078 just because but that's because it's a 14717 09:17:29,040 --> 09:17:34,398 predictable number of bits so if we keep 14718 09:17:32,078 --> 09:17:39,160 going beyond that you would get not 10 14719 09:17:34,398 --> 09:17:40,878 not 11 not 12 but in heximal 1 0 1 1 1 2 14720 09:17:39,160 --> 09:17:43,878 and so forth all the way down on the 14721 09:17:40,878 --> 09:17:45,558 screen to 1 F and if I you know shrunk 14722 09:17:43,878 --> 09:17:48,398 this down or had a bigger monitor we 14723 09:17:45,558 --> 09:17:51,840 would see eventually 255 bytes later 14724 09:17:48,398 --> 09:17:53,360 from the start 255 as well but there's a 14725 09:17:51,840 --> 09:17:55,840 potential problem here with using 14726 09:17:53,360 --> 09:17:58,360 heximal in this way there's an 14727 09:17:55,840 --> 09:18:01,680 ambiguity can anyone imagine like what 14728 09:17:58,360 --> 09:18:06,200 can go wrong if we use hex to just 14729 09:18:01,680 --> 09:18:08,558 simply describe locations in memory like 14730 09:18:06,200 --> 09:18:11,160 this 14731 09:18:08,558 --> 09:18:13,438 yeah yeah yeah like one zero might also 14732 09:18:11,160 --> 09:18:14,918 be 10 and you know maybe if you're you 14733 09:18:13,438 --> 09:18:17,000 know really thorough okay wait a minute 14734 09:18:14,918 --> 09:18:18,680 it can't be 10 cuz here's F over here so 14735 09:18:17,000 --> 09:18:20,160 it's obviously not decimal but why 14736 09:18:18,680 --> 09:18:21,278 create potential confusion especially 14737 09:18:20,160 --> 09:18:22,840 when you're collaborating building 14738 09:18:21,278 --> 09:18:24,480 something with someone we want to avoid 14739 09:18:22,840 --> 09:18:27,200 that ambiguity and so the convention 14740 09:18:24,480 --> 09:18:28,800 humans decided on years ago is that if 14741 09:18:27,200 --> 09:18:31,640 you want to make clear that a number is 14742 09:18:28,800 --> 09:18:35,040 in hexadecimal just by convention you 14743 09:18:31,640 --> 09:18:37,238 prefix all of the digits with Zer X the 14744 09:18:35,040 --> 09:18:39,360 X is not like another character it's not 14745 09:18:37,238 --> 09:18:40,320 like a 17th character it's just a human 14746 09:18:39,360 --> 09:18:42,918 conven 14747 09:18:40,320 --> 09:18:45,640 of putting 0x to imply here comes 14748 09:18:42,918 --> 09:18:48,800 heximal and now it's unambiguous so now 14749 09:18:45,640 --> 09:18:50,640 we see 0x10 obviously is not 10 as we 14750 09:18:48,800 --> 09:18:53,160 know it in decimal but rather it's the 14751 09:18:50,640 --> 09:18:55,840 number that comes after a single F so 14752 09:18:53,160 --> 09:18:57,758 it's really the number in decimal 16 so 14753 09:18:55,840 --> 09:19:00,840 0x anytime you see it that's just a 14754 09:18:57,758 --> 09:19:04,640 visual cue that what is ahead is 14755 09:19:00,840 --> 09:19:06,438 actually heximal so let's now start 14756 09:19:04,640 --> 09:19:07,960 playing around with this information so 14757 09:19:06,438 --> 09:19:09,758 here's a super simple line of code from 14758 09:19:07,960 --> 09:19:12,200 like week one where I'm just declaring 14759 09:19:09,758 --> 09:19:14,122 ing a variable n and I'm defining it to 14760 09:19:12,200 --> 09:19:15,720 be the value 50 and this is out of 14761 09:19:14,122 --> 09:19:17,720 context we probably need a main function 14762 09:19:15,720 --> 09:19:19,520 and all of that but let's just rewind to 14763 09:19:17,720 --> 09:19:21,520 week one where we actually saw code like 14764 09:19:19,520 --> 09:19:23,276 this and do something useful with a line 14765 09:19:21,520 --> 09:19:25,916 of code like this so let me go over here 14766 09:19:23,276 --> 09:19:28,360 to VSS code and in vs code I'll create a 14767 09:19:25,916 --> 09:19:30,800 program called how about addresses since 14768 09:19:28,360 --> 09:19:32,960 the goal of this uh the goal here is to 14769 09:19:30,800 --> 09:19:35,120 just play around ultimately with a 14770 09:19:32,960 --> 09:19:37,520 variable like n and let me go ahead and 14771 09:19:35,120 --> 09:19:40,756 do this I'll include how about standard 14772 09:19:37,520 --> 09:19:44,360 i.h I'll do int main void so no command 14773 09:19:40,756 --> 09:19:46,120 line Arguments for now in N gets 50 and 14774 09:19:44,360 --> 09:19:48,240 now so that we can do something mildly 14775 09:19:46,120 --> 09:19:50,640 useful with it let's just go use print F 14776 09:19:48,240 --> 09:19:53,120 and print out with percent I and then a 14777 09:19:50,640 --> 09:19:54,560 new line whatever that value of n is so 14778 09:19:53,120 --> 09:19:56,560 this is not going to be interesting per 14779 09:19:54,560 --> 09:19:58,596 se it's just week one stuff where I'm 14780 09:19:56,560 --> 09:20:00,360 defining a variable and printing it out 14781 09:19:58,596 --> 09:20:02,480 to the screen so let me go down to my 14782 09:20:00,360 --> 09:20:05,040 terminal window and do make 14783 09:20:02,480 --> 09:20:07,240 addresses no errors so that's good I'll 14784 09:20:05,040 --> 09:20:10,160 do do/ addresses and of course I should 14785 09:20:07,240 --> 09:20:12,120 see the number 50 here now what's going 14786 09:20:10,160 --> 09:20:15,436 on underneath the hood let's translate 14787 09:20:12,120 --> 09:20:16,756 now code to really what's going on under 14788 09:20:15,436 --> 09:20:18,520 under underneath the hood of the 14789 09:20:16,756 --> 09:20:20,520 computer so if this is our grid of 14790 09:20:18,520 --> 09:20:21,880 memory I don't necessarily know as the 14791 09:20:20,520 --> 09:20:23,560 programmer and I definitely don't care 14792 09:20:21,880 --> 09:20:24,680 as the programmer where exactly it's 14793 09:20:23,560 --> 09:20:26,200 ending up in memory that's the whole 14794 09:20:24,680 --> 09:20:27,756 point of using Code let the computer 14795 09:20:26,200 --> 09:20:30,000 figure this out but at least 14796 09:20:27,756 --> 09:20:32,480 conceptually I know that by declaring a 14797 09:20:30,000 --> 09:20:34,400 line of code like that the number 50 14798 09:20:32,480 --> 09:20:37,680 ends up somewhere in the computer's 14799 09:20:34,400 --> 09:20:40,040 memory and it's assigned the name n a 14800 09:20:37,680 --> 09:20:42,596 symbol n via which I the programmer can 14801 09:20:40,040 --> 09:20:46,880 refer to it and I very deliberately used 14802 09:20:42,596 --> 09:20:48,756 four of these squares for what 14803 09:20:46,880 --> 09:20:52,200 reason what might be the reason for 14804 09:20:48,756 --> 09:20:53,680 using four squares specifically yeah 14805 09:20:52,200 --> 09:20:55,400 yeah so an integer is four bytes at 14806 09:20:53,680 --> 09:20:57,360 least most of the time on Modern systems 14807 09:20:55,400 --> 09:20:59,200 an integer is four bytes on an older 14808 09:20:57,360 --> 09:21:02,080 computer it might just use one or maybe 14809 09:20:59,200 --> 09:21:03,276 even uh two bytes But Here by convention 14810 09:21:02,080 --> 09:21:04,560 we're almost always going to see four 14811 09:21:03,276 --> 09:21:06,520 bytes I don't know if it's going to end 14812 09:21:04,560 --> 09:21:08,320 up here it might end up over here but 14813 09:21:06,520 --> 09:21:10,360 for now who cares I just know that the 14814 09:21:08,320 --> 09:21:13,000 computer can store the the information 14815 09:21:10,360 --> 09:21:15,596 in this way underneath the hood so let's 14816 09:21:13,000 --> 09:21:18,000 now introduce another feature of C that 14817 09:21:15,596 --> 09:21:20,080 we haven't had occasion to use just yet 14818 09:21:18,000 --> 09:21:21,800 that's going to allow us to start poking 14819 09:21:20,080 --> 09:21:23,436 around the computer's memory For Better 14820 09:21:21,800 --> 09:21:25,436 or For Worse and this is one of those 14821 09:21:23,436 --> 09:21:27,840 situations where you're about to learn 14822 09:21:25,436 --> 09:21:29,680 acquire a a skill a power that can 14823 09:21:27,840 --> 09:21:31,200 actually come back to bite you because 14824 09:21:29,680 --> 09:21:33,080 once you know how to start poking around 14825 09:21:31,200 --> 09:21:34,596 a computer's memory you can do very 14826 09:21:33,080 --> 09:21:36,360 powerful things and next week we'll see 14827 09:21:34,596 --> 09:21:38,160 what you can build in a computer's 14828 09:21:36,360 --> 09:21:39,640 memory but you can also screw up pretty 14829 09:21:38,160 --> 09:21:41,160 easily and cause more of those 14830 09:21:39,640 --> 09:21:43,596 segmentation faults that a few of you 14831 09:21:41,160 --> 09:21:45,320 have already suffered so with that said 14832 09:21:43,596 --> 09:21:47,680 let's just stipulate that you know what 14833 09:21:45,320 --> 09:21:49,960 I don't care necessarily where the 50 is 14834 09:21:47,680 --> 09:21:52,000 in memory but I know it exists at some 14835 09:21:49,960 --> 09:21:53,720 address in memory and just so I have an 14836 09:21:52,000 --> 09:21:56,520 easy address to pronounce let's just 14837 09:21:53,720 --> 09:21:58,520 suppose it lives at ox123 so that's the 14838 09:21:56,520 --> 09:22:00,276 address in memory in heximal by 14839 09:21:58,520 --> 09:22:01,680 convention and that just happens to be 14840 09:22:00,276 --> 09:22:05,080 where it ends up when I write that line 14841 09:22:01,680 --> 09:22:06,960 of code but it turns out C has some 14842 09:22:05,080 --> 09:22:09,000 other operators we can use when we've 14843 09:22:06,960 --> 09:22:10,840 seen the asterisk before the star and 14844 09:22:09,000 --> 09:22:11,916 we've used it for multiplication but 14845 09:22:10,840 --> 09:22:13,400 today we're going to use it for 14846 09:22:11,916 --> 09:22:15,200 something more powerful and we're also 14847 09:22:13,400 --> 09:22:17,320 going to introduce an Amper sand which 14848 09:22:15,200 --> 09:22:20,200 allows us to do something as well the 14849 09:22:17,320 --> 09:22:23,840 Amper sand operator is going to allow us 14850 09:22:20,200 --> 09:22:25,840 to get the address of a piece of data in 14851 09:22:23,840 --> 09:22:28,040 memory like by literally putting 14852 09:22:25,840 --> 09:22:31,160 Ampersand before the name of a variable 14853 09:22:28,040 --> 09:22:33,400 C will tell us tell you what address 14854 09:22:31,160 --> 09:22:35,596 that variable lives at maybe it's ox123 14855 09:22:33,400 --> 09:22:38,360 maybe it's Ox 456 who knows but that 14856 09:22:35,596 --> 09:22:40,276 will give you back the answer the Star 14857 09:22:38,360 --> 09:22:42,276 does the opposite it's sort of means go 14858 09:22:40,276 --> 09:22:44,520 there so using the star otherwise known 14859 09:22:42,276 --> 09:22:46,560 as the D reference operator I can 14860 09:22:44,520 --> 09:22:48,480 actually go to a specific address if I 14861 09:22:46,560 --> 09:22:52,400 want and we'll see what this means in 14862 09:22:48,480 --> 09:22:54,080 code so how can I leverage this in some 14863 09:22:52,400 --> 09:22:55,840 mildly interesting way to start poking 14864 09:22:54,080 --> 09:22:58,000 around but eventually we'll use this 14865 09:22:55,840 --> 09:23:00,916 primitive to build more interesting 14866 09:22:58,000 --> 09:23:03,240 things so let me go back to save vs code 14867 09:23:00,916 --> 09:23:05,276 here and let me go ahead and do this 14868 09:23:03,240 --> 09:23:06,960 I'll clear my terminal to start fresh 14869 09:23:05,276 --> 09:23:10,400 and I'll introduce another format code 14870 09:23:06,960 --> 09:23:13,040 for printf percent p and for now just 14871 09:23:10,400 --> 09:23:15,200 take on faith that this it is percent P 14872 09:23:13,040 --> 09:23:18,400 because but percent p is going to allow 14873 09:23:15,200 --> 09:23:21,400 me to print the address of a variable if 14874 09:23:18,400 --> 09:23:24,360 I additionally tell C get the address of 14875 09:23:21,400 --> 09:23:25,840 n so I'm changing percent I to percent p 14876 09:23:24,360 --> 09:23:28,276 and that's just something you have to do 14877 09:23:25,840 --> 09:23:30,360 when printing addresses for now but I 14878 09:23:28,276 --> 09:23:32,120 need to change an ampersand in front of 14879 09:23:30,360 --> 09:23:34,720 the variable name so I don't print in 14880 09:23:32,120 --> 09:23:36,596 the number 50 I print out something like 14881 09:23:34,720 --> 09:23:37,800 ox123 and it's not going to be as simple 14882 09:23:36,596 --> 09:23:39,720 as that we'll see on the screen though 14883 09:23:37,800 --> 09:23:43,080 where it actually ended up in my code 14884 09:23:39,720 --> 09:23:45,520 spaces memory so here we go uh dot uh 14885 09:23:43,080 --> 09:23:49,200 down in my terminal make addresses again 14886 09:23:45,520 --> 09:23:51,960 to recompile and now dot SL addresses 14887 09:23:49,200 --> 09:23:55,916 should reveal not the value of 50 but 14888 09:23:51,960 --> 09:23:57,916 the address of 50 and there it is it's 14889 09:23:55,916 --> 09:23:59,916 pretty long it's not quite as simple and 14890 09:23:57,916 --> 09:24:01,960 pretty as ox123 but there's the ox 14891 09:23:59,916 --> 09:24:06,916 meaning here's a hexadecimal address and 14892 09:24:01,960 --> 09:24:08,436 it's 7f FC c784 a04 C suffice it to say 14893 09:24:06,916 --> 09:24:10,960 your codes space and even your Macs and 14894 09:24:08,436 --> 09:24:13,520 PCs nowadays have a lot of memory that's 14895 09:24:10,960 --> 09:24:16,520 why in part this address is so big not 14896 09:24:13,520 --> 09:24:19,880 as small as the thing on my slide so 14897 09:24:16,520 --> 09:24:21,800 this at the moment isn't that useful yet 14898 09:24:19,880 --> 09:24:24,720 but it introduces us to a concept that 14899 09:24:21,800 --> 09:24:27,276 we'll Now call pointers and pointers are 14900 09:24:24,720 --> 09:24:29,840 admittedly one of the more challenging 14901 09:24:27,276 --> 09:24:31,880 aspects of c and if in future life you 14902 09:24:29,840 --> 09:24:33,640 tell friends that oh I took a class 14903 09:24:31,880 --> 09:24:35,080 called cs50 and we learned C like you'll 14904 09:24:33,640 --> 09:24:37,360 probably get kind of a look at people 14905 09:24:35,080 --> 09:24:40,080 like why did you learn C or like oh c 14906 09:24:37,360 --> 09:24:42,596 was hard and it's largely because of 14907 09:24:40,080 --> 09:24:45,160 this topic which isn't to say that it's 14908 09:24:42,596 --> 09:24:46,520 that hard to wrap your mind around but 14909 09:24:45,160 --> 09:24:48,840 it's definitely very different and it's 14910 09:24:46,520 --> 09:24:50,596 not a feature that you can harness in 14911 09:24:48,840 --> 09:24:53,320 higher level languages that we'll see in 14912 09:24:50,596 --> 09:24:55,276 class two like Python and Java and the 14913 09:24:53,320 --> 09:24:56,916 like C is about as close to the 14914 09:24:55,276 --> 09:24:59,240 computer's Hardware so to speak that you 14915 09:24:56,916 --> 09:25:00,960 can get before things get actually scary 14916 09:24:59,240 --> 09:25:02,840 the so-called Assembly Language we saw 14917 09:25:00,960 --> 09:25:04,680 in week two when I had a link and 14918 09:25:02,840 --> 09:25:06,240 compile and assemble and all of that 14919 09:25:04,680 --> 09:25:07,756 like that gets really lowlevel and you 14920 09:25:06,240 --> 09:25:09,880 really have to be an expert with the 14921 09:25:07,756 --> 09:25:12,276 computer's CPU or brain to understand 14922 09:25:09,880 --> 09:25:13,960 that but with C you can actually poke 14923 09:25:12,276 --> 09:25:15,800 around the computer's memory and do 14924 09:25:13,960 --> 09:25:17,640 powerful things with that but again with 14925 09:25:15,800 --> 09:25:20,360 great power comes responsibility it's 14926 09:25:17,640 --> 09:25:22,160 very easy to break programs by misusing 14927 09:25:20,360 --> 09:25:25,320 memory or just having a bug that touches 14928 09:25:22,160 --> 09:25:28,916 memory in some way that you don't intend 14929 09:25:25,320 --> 09:25:31,320 so pointers at the end of the day are 14930 09:25:28,916 --> 09:25:33,880 pretty much what we just saw a pointer 14931 09:25:31,320 --> 09:25:37,720 is really just a variable that contains 14932 09:25:33,880 --> 09:25:39,160 the address of some value a pointer is a 14933 09:25:37,720 --> 09:25:41,120 variable that contains the address of 14934 09:25:39,160 --> 09:25:42,800 some value or more simply it's fine to 14935 09:25:41,120 --> 09:25:44,436 think of it as an address a pointer is 14936 09:25:42,800 --> 09:25:46,916 an address of something in the 14937 09:25:44,436 --> 09:25:50,120 computer's memory now what might we do 14938 09:25:46,916 --> 09:25:52,560 to uh to uh actualize this well here's 14939 09:25:50,120 --> 09:25:54,756 two lines of code it turns out by using 14940 09:25:52,560 --> 09:25:57,560 our two new operators today I can 14941 09:25:54,756 --> 09:26:00,240 declare an INT call it n and assign it a 14942 09:25:57,560 --> 09:26:03,276 value like 50 just like before if I want 14943 09:26:00,240 --> 09:26:04,880 to store the address of n in a variable 14944 09:26:03,276 --> 09:26:07,320 and not just print it immediately via 14945 09:26:04,880 --> 09:26:09,080 printf I can declare a variable for 14946 09:26:07,320 --> 09:26:11,120 instance called P but I could call it 14947 09:26:09,080 --> 09:26:14,040 anything I want like any variable but 14948 09:26:11,120 --> 09:26:17,480 because it's an address it's not int P 14949 09:26:14,040 --> 09:26:19,720 it has to be int star P so to speak and 14950 09:26:17,480 --> 09:26:21,960 the star here on the left hand side of 14951 09:26:19,720 --> 09:26:25,200 the equal sign is just a clue to C that 14952 09:26:21,960 --> 09:26:27,720 means p is going to be a pointer that is 14953 09:26:25,200 --> 09:26:30,120 p is going to be the address of what the 14954 09:26:27,720 --> 09:26:31,480 address of an integer now technically 14955 09:26:30,120 --> 09:26:33,040 it's still an integer itself right 14956 09:26:31,480 --> 09:26:35,680 because an address is just a number 14957 09:26:33,040 --> 09:26:38,160 whether it's 1 2 3 or ox123 so this is 14958 09:26:35,680 --> 09:26:40,120 really just a semantic difference so in 14959 09:26:38,160 --> 09:26:42,640 Star p just means that this variable 14960 09:26:40,120 --> 09:26:44,840 doesn't contain any old number like 50 14961 09:26:42,640 --> 09:26:46,800 it specifically contains a number that 14962 09:26:44,840 --> 09:26:49,720 is the address of something 14963 09:26:46,800 --> 09:26:52,120 else so how can I now use this well let 14964 09:26:49,720 --> 09:26:55,320 me go back to VSS code and let me 14965 09:26:52,120 --> 09:26:56,800 propose that we add a line of code like 14966 09:26:55,320 --> 09:26:58,640 that so instead of just directly 14967 09:26:56,800 --> 09:27:01,160 printing out that value let's go ahead 14968 09:26:58,640 --> 09:27:04,480 and Define a second variable called uh P 14969 09:27:01,160 --> 09:27:08,040 that's of type in Star P set it equal to 14970 09:27:04,480 --> 09:27:09,720 Ampersand n and then this time let's not 14971 09:27:08,040 --> 09:27:11,916 just print out Amper sand n let's 14972 09:27:09,720 --> 09:27:13,800 actually print out the value of P so the 14973 09:27:11,916 --> 09:27:16,360 only two new things here if I zoom in 14974 09:27:13,800 --> 09:27:18,480 are I've used not only the Amper sand on 14975 09:27:16,360 --> 09:27:21,200 the right to get the address of n I'm 14976 09:27:18,480 --> 09:27:23,596 now using the star on the Left To Tell C 14977 09:27:21,200 --> 09:27:26,040 that P is still a still a variable as 14978 09:27:23,596 --> 09:27:29,000 always but it's a pointer it is the 14979 09:27:26,040 --> 09:27:30,080 address of some other value like this 14980 09:27:29,000 --> 09:27:31,720 and I'm still going to print it with the 14981 09:27:30,080 --> 09:27:33,360 same format code percent P so that 14982 09:27:31,720 --> 09:27:37,840 doesn't change so let me go ahead and 14983 09:27:33,360 --> 09:27:40,680 zoom out and do make addresses and slash 14984 09:27:37,840 --> 09:27:43,400 addresses and there it is exactly the 14985 09:27:40,680 --> 09:27:46,120 same thing now in and of itself not that 14986 09:27:43,400 --> 09:27:48,360 useful yet but the fact that you can now 14987 09:27:46,120 --> 09:27:50,756 access the addresses of things in memory 14988 09:27:48,360 --> 09:27:52,720 means that we'll be able to build things 14989 09:27:50,756 --> 09:27:55,080 and construct things and Link things 14990 09:27:52,720 --> 09:27:57,756 together by knowing where they live so 14991 09:27:55,080 --> 09:27:58,950 to speak so any questions on this 14992 09:27:57,756 --> 09:28:02,050 technique thus far 14993 09:27:58,950 --> 09:28:02,050 [Music] 14994 09:28:02,360 --> 09:28:07,880 yeah a good question on line six must it 14995 09:28:05,040 --> 09:28:10,160 be starp and Ampersand and in this case 14996 09:28:07,880 --> 09:28:11,596 yes because what am I doing on the left 14997 09:28:10,160 --> 09:28:14,400 and I'll get rid of the equal sign for 14998 09:28:11,596 --> 09:28:16,960 now this would give me a variable called 14999 09:28:14,400 --> 09:28:18,840 P that's not an integer per se but 15000 09:28:16,960 --> 09:28:20,560 that's the address of an integer but 15001 09:28:18,840 --> 09:28:22,800 without the equal sign I'm not storing 15002 09:28:20,560 --> 09:28:27,240 anything in that variable so by adding 15003 09:28:22,800 --> 09:28:29,436 the equal sign and then Ampersand N I am 15004 09:28:27,240 --> 09:28:31,276 explicitly figuring out with Ampersand 15005 09:28:29,436 --> 09:28:34,080 what the address of n is which already 15006 09:28:31,276 --> 09:28:36,276 exists per line five and tucking it away 15007 09:28:34,080 --> 09:28:40,240 in this new variable called 15008 09:28:36,276 --> 09:28:42,520 P other questions yeah every time you 15009 09:28:40,240 --> 09:28:44,080 run good question every time I run the 15010 09:28:42,520 --> 09:28:46,596 program it uses up a different piece of 15011 09:28:44,080 --> 09:28:47,880 memory short answer yes computers though 15012 09:28:46,596 --> 09:28:49,720 long story short also have something 15013 09:28:47,880 --> 09:28:51,000 called virtual memory so if you run it 15014 09:28:49,720 --> 09:28:53,240 again and again you might actually see 15015 09:28:51,000 --> 09:28:55,560 the same addresses on the same Mac or PC 15016 09:28:53,240 --> 09:28:58,120 or cloud-based server but we'll see in a 15017 09:28:55,560 --> 09:29:00,560 bit where uh at a high level it's laid 15018 09:28:58,120 --> 09:29:05,000 out but it will always exist at some 15019 09:29:00,560 --> 09:29:07,916 address good question yeah some correct 15020 09:29:05,000 --> 09:29:09,320 Ampersand n is the address of N and in 15021 09:29:07,916 --> 09:29:13,040 Star p 15022 09:29:09,320 --> 09:29:15,756 is a pointer called p and honestly in an 15023 09:29:13,040 --> 09:29:17,560 Ideal World if C were made today and not 15024 09:29:15,756 --> 09:29:19,840 decades ago when humans were first 15025 09:29:17,560 --> 09:29:21,560 creating languages you know ideally we 15026 09:29:19,840 --> 09:29:23,276 would just have a data type called 15027 09:29:21,560 --> 09:29:24,960 pointer and then this would be a little 15028 09:29:23,276 --> 09:29:26,480 less complicated because it would 15029 09:29:24,960 --> 09:29:28,160 literally be what it says you know the 15030 09:29:26,480 --> 09:29:30,240 humans who invented SE didn't do this 15031 09:29:28,160 --> 09:29:32,560 but this is the idea so pointer is not a 15032 09:29:30,240 --> 09:29:34,916 legitimate word in the code it is a term 15033 09:29:32,560 --> 09:29:37,080 of Art in English but this is really 15034 09:29:34,916 --> 09:29:39,720 just the idea but the way you express 15035 09:29:37,080 --> 09:29:43,680 pointer as a data type type is a little 15036 09:29:39,720 --> 09:29:47,756 more cryptic as int star P here but 15037 09:29:43,680 --> 09:29:49,160 notice in line seven when I print out p 15038 09:29:47,756 --> 09:29:50,916 i don't use a star I don't use an 15039 09:29:49,160 --> 09:29:52,320 ampersand y I literally just want to 15040 09:29:50,916 --> 09:29:53,720 print the value of p and we've been 15041 09:29:52,320 --> 09:29:55,480 doing that since week one if you want to 15042 09:29:53,720 --> 09:29:58,560 print a variable just describe the 15043 09:29:55,480 --> 09:30:01,000 variable by its name no special syntax 15044 09:29:58,560 --> 09:30:03,640 any other questions on this thus 15045 09:30:01,000 --> 09:30:06,200 far uh what's the advantage of using 15046 09:30:03,640 --> 09:30:08,320 pointers with pointers we'll see today 15047 09:30:06,200 --> 09:30:09,840 some applications of them really the 15048 09:30:08,320 --> 09:30:11,276 idea is going to come to fruition next 15049 09:30:09,840 --> 09:30:13,400 week when we're going to create what are 15050 09:30:11,276 --> 09:30:15,520 called uh data structures in memory 15051 09:30:13,400 --> 09:30:17,520 where we can build not just uh for 15052 09:30:15,520 --> 09:30:19,360 instance uh one-dimensional data 15053 09:30:17,520 --> 09:30:20,360 structures like an array we'll see next 15054 09:30:19,360 --> 09:30:22,000 week we can actually create the 15055 09:30:20,360 --> 09:30:23,596 equivalent of two-dimensional data 15056 09:30:22,000 --> 09:30:25,756 structures or even threedimensional data 15057 09:30:23,596 --> 09:30:27,276 structures by using these addresses and 15058 09:30:25,756 --> 09:30:28,480 sort of linking things together and 15059 09:30:27,276 --> 09:30:30,880 we'll see the beginnings of that this 15060 09:30:28,480 --> 09:30:32,640 week but for now focus at least for now 15061 09:30:30,880 --> 09:30:34,756 on just really the syntax and what these 15062 09:30:32,640 --> 09:30:39,160 building blocks can do for us um does 15063 09:30:34,756 --> 09:30:41,276 the poin have to be does the p uh in 15064 09:30:39,160 --> 09:30:42,800 does the pop pointer have to be an point 15065 09:30:41,276 --> 09:30:44,276 to an integer short answer no and we'll 15066 09:30:42,800 --> 09:30:45,520 come back to this for now for the sake 15067 09:30:44,276 --> 09:30:47,560 of discussion we're only dealing with 15068 09:30:45,520 --> 09:30:49,400 integers like the number 50 uh you 15069 09:30:47,560 --> 09:30:51,960 mentioned strings or characters 15070 09:30:49,400 --> 09:30:53,880 absolutely we're about to go there soon 15071 09:30:51,960 --> 09:30:55,720 so you can use the address of anything 15072 09:30:53,880 --> 09:30:57,880 you want in the computer's memory so in 15073 09:30:55,720 --> 09:30:59,200 fact let's translate this now to just 15074 09:30:57,880 --> 09:31:00,756 the same picture just to help you wrap 15075 09:30:59,200 --> 09:31:03,000 your minds around what these two lines 15076 09:31:00,756 --> 09:31:04,960 of code really fundamentally are doing 15077 09:31:03,000 --> 09:31:07,520 so if I come back to my grid of memory 15078 09:31:04,960 --> 09:31:09,120 here let's plop the number 50 in the 15079 09:31:07,520 --> 09:31:10,840 variable n at the bottom right like it 15080 09:31:09,120 --> 09:31:13,276 was before so this is that first line of 15081 09:31:10,840 --> 09:31:16,240 code as before but with the new second 15082 09:31:13,276 --> 09:31:19,560 line of code as soon as I create P what 15083 09:31:16,240 --> 09:31:21,240 do I do well first remember that n lives 15084 09:31:19,560 --> 09:31:22,800 somewhere in the computer's memory 15085 09:31:21,240 --> 09:31:24,240 usually I don't care precisely where it 15086 09:31:22,800 --> 09:31:26,596 is but for the sake of discussion let's 15087 09:31:24,240 --> 09:31:28,756 suppose it's at ox123 which is easier to 15088 09:31:26,596 --> 09:31:30,840 say than where it actually ended up and 15089 09:31:28,756 --> 09:31:32,800 now what is p well p is just another 15090 09:31:30,840 --> 09:31:35,120 variable and variables live in memory 15091 09:31:32,800 --> 09:31:39,120 too so let me just hypothesize that P 15092 09:31:35,120 --> 09:31:41,160 lives up here and it turns out that P 15093 09:31:39,120 --> 09:31:44,320 once you assign it the value of 15094 09:31:41,160 --> 09:31:46,916 Ampersand N means that c will take a 15095 09:31:44,320 --> 09:31:49,720 look at the variable n realize oh it 15096 09:31:46,916 --> 09:31:52,640 lives at ox123 and what goes in the 15097 09:31:49,720 --> 09:31:55,240 value of p is literally 15098 09:31:52,640 --> 09:31:57,120 ox123 so again it's still an integer 15099 09:31:55,240 --> 09:31:59,880 which is confusing but it's technically 15100 09:31:57,120 --> 09:32:02,596 an integer being used as an address and 15101 09:31:59,880 --> 09:32:04,040 now just a a prompt here notice that 15102 09:32:02,596 --> 09:32:06,436 this pointer is pretty darn big it's 15103 09:32:04,040 --> 09:32:07,800 like eight squares what's the 15104 09:32:06,436 --> 09:32:09,080 implication of that because I did that 15105 09:32:07,800 --> 09:32:11,240 deliberately 15106 09:32:09,080 --> 09:32:14,200 how big must a pointer apparently be in 15107 09:32:11,240 --> 09:32:15,480 most modern systems would you say okay 15108 09:32:14,200 --> 09:32:17,240 good computers today are very big you 15109 09:32:15,480 --> 09:32:18,720 have gigabytes of RAM in your computer 15110 09:32:17,240 --> 09:32:20,080 you therefore need big pointers to be 15111 09:32:18,720 --> 09:32:21,840 able to point at memory that's 15112 09:32:20,080 --> 09:32:23,320 conceptually pretty far away so to be 15113 09:32:21,840 --> 09:32:25,596 clear how many bytes does a pointer 15114 09:32:23,320 --> 09:32:27,840 apparently take up well it seems to take 15115 09:32:25,596 --> 09:32:29,840 up eight in total integers by convention 15116 09:32:27,840 --> 09:32:32,080 nowadays are usually four pointers 15117 09:32:29,840 --> 09:32:33,560 though nowadays are typically eight in 15118 09:32:32,080 --> 09:32:34,960 this case so I'm drawing it in a manner 15119 09:32:33,560 --> 09:32:36,560 consistent with the reality even though 15120 09:32:34,960 --> 09:32:38,480 at the end of the day it's not really 15121 09:32:36,560 --> 09:32:41,080 that interesting what values are in here 15122 09:32:38,480 --> 09:32:42,560 in fact let's emerge from these weeds I 15123 09:32:41,080 --> 09:32:44,240 don't really care what else is going on 15124 09:32:42,560 --> 09:32:45,596 in my computer's memory at the moment 15125 09:32:44,240 --> 09:32:48,640 because I've only got those two lines of 15126 09:32:45,596 --> 09:32:50,596 Juicy code defining n and defining P so 15127 09:32:48,640 --> 09:32:52,360 let's hide all of the other squares and 15128 09:32:50,596 --> 09:32:55,756 honestly I mean it when I say that 15129 09:32:52,360 --> 09:32:57,720 programmers need to know that a variable 15130 09:32:55,756 --> 09:32:59,680 exists somewhere in memory and needs to 15131 09:32:57,720 --> 09:33:01,596 be able to get that address using like 15132 09:32:59,680 --> 09:33:04,080 the Ampersand but you're never going to 15133 09:33:01,596 --> 09:33:06,200 print F like I did the actual address 15134 09:33:04,080 --> 09:33:07,800 like it's not generally interesting 15135 09:33:06,200 --> 09:33:09,080 unless you're debugging your code but 15136 09:33:07,800 --> 09:33:11,276 you're not going to like start typing 15137 09:33:09,080 --> 09:33:13,040 out crazy Ox numbers in your code to 15138 09:33:11,276 --> 09:33:15,040 move things around you just need to know 15139 09:33:13,040 --> 09:33:17,756 that the computer can figure out where 15140 09:33:15,040 --> 09:33:20,200 things are so frankly by that logic who 15141 09:33:17,756 --> 09:33:23,160 cares that it's ox123 right tomorrow it 15142 09:33:20,200 --> 09:33:25,480 could be Ox 456 or something else so one 15143 09:33:23,160 --> 09:33:28,596 of the ways to think of a pointer is 15144 09:33:25,480 --> 09:33:32,040 literally as a variable that points at 15145 09:33:28,596 --> 09:33:33,840 something else and indeed in this case P 15146 09:33:32,040 --> 09:33:36,080 yeah technically it has an address and 15147 09:33:33,840 --> 09:33:37,640 yeah technically it's ox123 in the story 15148 09:33:36,080 --> 09:33:41,560 but honestly who cares I just need to 15149 09:33:37,640 --> 09:33:44,240 know that you using p i can get to the 15150 09:33:41,560 --> 09:33:45,520 value n and so what are these addresses 15151 09:33:44,240 --> 09:33:47,800 and in fact if Carter wouldn't mind 15152 09:33:45,520 --> 09:33:49,040 joining me up here for a moment what are 15153 09:33:47,800 --> 09:33:50,840 these addresses well just like in our 15154 09:33:49,040 --> 09:33:51,720 human world we have mailboxes even 15155 09:33:50,840 --> 09:33:53,320 though you might not check it very 15156 09:33:51,720 --> 09:33:55,916 frequently nowadays but to get physical 15157 09:33:53,320 --> 09:33:58,040 mail every uh home every business has a 15158 09:33:55,916 --> 09:34:00,160 unique address the uh science and 15159 09:33:58,040 --> 09:34:03,840 engineering complex is 150 Western 15160 09:34:00,160 --> 09:34:05,320 Avenue Austin Massachusetts 02134 USA 15161 09:34:03,840 --> 09:34:07,840 and theoretically that uniquely 15162 09:34:05,320 --> 09:34:10,240 identifies that building in the world 15163 09:34:07,840 --> 09:34:13,240 well here we we have two mailboxes um 15164 09:34:10,240 --> 09:34:16,080 over here we have a value n that happens 15165 09:34:13,240 --> 09:34:18,680 to live I'll claim it address ox123 and 15166 09:34:16,080 --> 09:34:21,596 then over here I claim there's another 15167 09:34:18,680 --> 09:34:23,116 address uh called by name p i don't 15168 09:34:21,596 --> 09:34:24,400 actually care where it is even though it 15169 09:34:23,116 --> 09:34:26,680 definitely exists somewhere in the 15170 09:34:24,400 --> 09:34:29,240 computer's memory but if this is p which 15171 09:34:26,680 --> 09:34:31,480 is a variable and that's n another 15172 09:34:29,240 --> 09:34:33,200 variable ideally this mailbox would be 15173 09:34:31,480 --> 09:34:34,720 twice as big because of the number of 15174 09:34:33,200 --> 09:34:36,960 btes using but Home Depot only had 15175 09:34:34,720 --> 09:34:39,000 identical size mailboxes but here is p 15176 09:34:36,960 --> 09:34:43,040 one variable there is in another 15177 09:34:39,000 --> 09:34:45,400 variable if I open up this mailbox what 15178 09:34:43,040 --> 09:34:48,160 should I find inside of it based on our 15179 09:34:45,400 --> 09:34:50,116 story thus far like what value will I 15180 09:34:48,160 --> 09:34:54,436 pull out dramatically in just a 15181 09:34:50,116 --> 09:34:55,596 moment yeah I think ox1 123 Now using 15182 09:34:54,436 --> 09:34:57,720 this you can kind of think of this as 15183 09:34:55,596 --> 09:34:59,400 like x marks the spot no pun intended 15184 09:34:57,720 --> 09:35:02,276 where I can now like walk around the 15185 09:34:59,400 --> 09:35:03,680 computer's memory and find my way to 15186 09:35:02,276 --> 09:35:05,080 that location by sort of following the 15187 09:35:03,680 --> 09:35:07,080 treasure map or if I want it more 15188 09:35:05,080 --> 09:35:08,880 dramatically uh thanks to our little 15189 09:35:07,080 --> 09:35:10,800 Yale foam finger here here you can think 15190 09:35:08,880 --> 09:35:13,116 of it more abstractly as p is just 15191 09:35:10,800 --> 09:35:14,596 pointing at n okay that's not going over 15192 09:35:13,116 --> 09:35:17,880 well so let's switch over to the Harvard 15193 09:35:14,596 --> 09:35:17,880 one so p is 15194 09:35:19,000 --> 09:35:24,160 pointing so p is pointing at n and so it 15195 09:35:22,240 --> 09:35:26,436 turns out we will be able to write code 15196 09:35:24,160 --> 09:35:28,436 now that will do the equivalent of me 15197 09:35:26,436 --> 09:35:29,916 walking over to n but for now Carter if 15198 09:35:28,436 --> 09:35:33,436 you want to reveal what's in the mailbox 15199 09:35:29,916 --> 09:35:35,160 we should see indeed the number 50 so 15200 09:35:33,436 --> 09:35:36,756 that's really all that's oh I feel 15201 09:35:35,160 --> 09:35:40,116 Carter's waiting for Applause so like 15202 09:35:36,756 --> 09:35:40,116 really well nicely done 15203 09:35:42,320 --> 09:35:46,276 thank you so that's just like a physical 15204 09:35:44,160 --> 09:35:47,960 metaphor of what's going on here in one 15205 09:35:46,276 --> 09:35:49,400 variable we have an address and that 15206 09:35:47,960 --> 09:35:51,560 variable by convention is called a 15207 09:35:49,400 --> 09:35:54,320 pointer in the other variable per week 15208 09:35:51,560 --> 09:35:57,080 one we just have a value like n and you 15209 09:35:54,320 --> 09:35:58,436 can yes follow the map and walk yourself 15210 09:35:57,080 --> 09:36:00,560 to that particular address and we'll see 15211 09:35:58,436 --> 09:36:02,160 how to do that in code but what's really 15212 09:36:00,560 --> 09:36:04,080 interesting is this abstraction that 15213 09:36:02,160 --> 09:36:07,200 pointers literally or really I guess 15214 09:36:04,080 --> 09:36:09,960 figuratively point at some other value 15215 09:36:07,200 --> 09:36:11,916 in memory all right questions then on 15216 09:36:09,960 --> 09:36:13,560 pointers in this form pointers point to 15217 09:36:11,916 --> 09:36:15,560 each other can pointers point to each 15218 09:36:13,560 --> 09:36:17,160 other so yes there's things called 15219 09:36:15,560 --> 09:36:19,756 double pointers we're not going to see 15220 09:36:17,160 --> 09:36:22,240 them anytime soon but using star star 15221 09:36:19,756 --> 09:36:24,596 you can express an address of an address 15222 09:36:22,240 --> 09:36:27,080 um but we won't see that just yet other 15223 09:36:24,596 --> 09:36:30,436 questions on 15224 09:36:27,080 --> 09:36:30,436 pointers yeah in 15225 09:36:31,560 --> 09:36:37,040 front our array so to summarize our 15226 09:36:34,520 --> 09:36:38,360 arrays then pointers so short answer 15227 09:36:37,040 --> 09:36:40,040 there's a relationship and we'll come 15228 09:36:38,360 --> 09:36:42,080 back to that in a little bit but arrays 15229 09:36:40,040 --> 09:36:43,436 are technically different from pointers 15230 09:36:42,080 --> 09:36:45,200 but we we're going to be able to blur 15231 09:36:43,436 --> 09:36:46,680 the lines a little bit by using one like 15232 09:36:45,200 --> 09:36:49,080 the other but let me come back to that 15233 09:36:46,680 --> 09:36:52,240 in just a bit of time all right so if we 15234 09:36:49,080 --> 09:36:54,960 have now this mental model if you will 15235 09:36:52,240 --> 09:36:57,596 of like what a pointer is in memory I 15236 09:36:54,960 --> 09:36:59,360 think we can start to peel back a layer 15237 09:36:57,596 --> 09:37:00,960 of uh simplification that we've been 15238 09:36:59,360 --> 09:37:03,720 assuming for the past few weeks since 15239 09:37:00,960 --> 09:37:05,200 week one so a string recall is a 15240 09:37:03,720 --> 09:37:07,080 sequence of characters and so if you 15241 09:37:05,200 --> 09:37:08,916 want to create a string that says Hi in 15242 09:37:07,080 --> 09:37:11,400 all caps and an next exclamation point 15243 09:37:08,916 --> 09:37:12,840 we do string s equals quote unquote high 15244 09:37:11,400 --> 09:37:14,640 and we can hard code it like this or we 15245 09:37:12,840 --> 09:37:16,360 could use get string but for now just 15246 09:37:14,640 --> 09:37:19,160 assume that I hardcoded it into my code 15247 09:37:16,360 --> 09:37:20,880 to always say hi in all caps with an 15248 09:37:19,160 --> 09:37:22,680 exclamation point well what does that 15249 09:37:20,880 --> 09:37:24,560 look like in the computer's memory well 15250 09:37:22,680 --> 09:37:26,080 let's stop looking at the entire memory 15251 09:37:24,560 --> 09:37:29,916 let's just focus on really what's going 15252 09:37:26,080 --> 09:37:32,640 on once you create a string called s and 15253 09:37:29,916 --> 09:37:35,400 store in it hi you know that a couple of 15254 09:37:32,640 --> 09:37:36,960 things are happening H and I and the 15255 09:37:35,400 --> 09:37:39,916 exclamation point are ending up in the 15256 09:37:36,960 --> 09:37:41,916 computer's memory we know from week two 15257 09:37:39,916 --> 09:37:45,480 that this thing the so-called null 15258 09:37:41,916 --> 09:37:47,320 character n AKA back slz is also being 15259 09:37:45,480 --> 09:37:48,916 added for you and it's somewhere in 15260 09:37:47,320 --> 09:37:50,480 memory at the moment I don't really care 15261 09:37:48,916 --> 09:37:51,960 where I drew it at the bottom right yes 15262 09:37:50,480 --> 09:37:53,960 it has an address but for now it just 15263 09:37:51,960 --> 09:37:57,560 ends up somewhere and in fact here's a 15264 09:37:53,960 --> 09:38:00,116 little visual cue as to how this happens 15265 09:37:57,560 --> 09:38:02,480 in C anytime you use double quotes to 15266 09:38:00,116 --> 09:38:04,800 give you a string you can imagine that 15267 09:38:02,480 --> 09:38:07,320 the double quotes are like a a clue to 15268 09:38:04,800 --> 09:38:09,320 not only store Hi exclamation point but 15269 09:38:07,320 --> 09:38:11,240 also put the null character there for 15270 09:38:09,320 --> 09:38:13,400 you and this is in contrast to what 15271 09:38:11,240 --> 09:38:15,276 chars if you want individual characters 15272 09:38:13,400 --> 09:38:17,756 what syntax did we use 15273 09:38:15,276 --> 09:38:20,276 instead so single quotes single quotes 15274 09:38:17,756 --> 09:38:22,116 do not add magically a back SL zero they 15275 09:38:20,276 --> 09:38:23,480 literally just store one character so 15276 09:38:22,116 --> 09:38:25,720 again strings have always been a little 15277 09:38:23,480 --> 09:38:27,800 special you get some extra an extra bite 15278 09:38:25,720 --> 09:38:29,360 for free so that you know where the 15279 09:38:27,800 --> 09:38:31,436 string ends and functions like stir 15280 09:38:29,360 --> 09:38:33,720 compare can then find their way there so 15281 09:38:31,436 --> 09:38:36,960 in memory it might indeed look a little 15282 09:38:33,720 --> 09:38:38,756 like this and if we assume that there's 15283 09:38:36,960 --> 09:38:39,640 going to be somewhere in memory these 15284 09:38:38,756 --> 09:38:41,960 things are going to be somewhere in 15285 09:38:39,640 --> 09:38:43,436 memory we can address them per week two 15286 09:38:41,960 --> 09:38:45,720 by way of the name of the variable so if 15287 09:38:43,436 --> 09:38:46,880 s is the name of the variable S braet 0 15288 09:38:45,720 --> 09:38:48,756 is how you would refer to the first 15289 09:38:46,880 --> 09:38:50,720 letter s bracket 1 s braet two and if 15290 09:38:48,756 --> 09:38:53,560 you really want s bracket 3 would get 15291 09:38:50,720 --> 09:38:57,160 you at the uh null character at the very 15292 09:38:53,560 --> 09:38:59,640 end but what is s so technically in this 15293 09:38:57,160 --> 09:39:02,320 line of code here not only is the 15294 09:38:59,640 --> 09:39:05,840 computer giving you memory for h i 15295 09:39:02,320 --> 09:39:07,916 exclamation point back sl0 we turns out 15296 09:39:05,840 --> 09:39:09,480 that s itself must take up some amount 15297 09:39:07,916 --> 09:39:10,560 of space right because s is the variable 15298 09:39:09,480 --> 09:39:11,916 and every time we' talked about 15299 09:39:10,560 --> 09:39:13,680 variables thus far I've given you a 15300 09:39:11,916 --> 09:39:16,080 rectangle on the screen in which to 15301 09:39:13,680 --> 09:39:18,800 store its value so let's assume for the 15302 09:39:16,080 --> 09:39:22,240 sake of discussion that the H is at 15303 09:39:18,800 --> 09:39:24,160 ox123 and I is at o x124 exclamation 15304 09:39:22,240 --> 09:39:28,520 points at o x125 and the null characters 15305 09:39:24,160 --> 09:39:30,116 at ox1 126 well what then is s well s is 15306 09:39:28,520 --> 09:39:31,800 just going to be some other variable and 15307 09:39:30,116 --> 09:39:33,880 I'll draw it somewhat abstractly without 15308 09:39:31,800 --> 09:39:35,360 all the other boxes up here and I'll 15309 09:39:33,880 --> 09:39:39,040 claim that the name of this variable is 15310 09:39:35,360 --> 09:39:42,040 s but it turns out what is s really how 15311 09:39:39,040 --> 09:39:44,960 do strings Really Work Well s is a 15312 09:39:42,040 --> 09:39:46,360 variable and has been since week one but 15313 09:39:44,960 --> 09:39:48,200 when you define it what the computer is 15314 09:39:46,360 --> 09:39:50,596 doing for you automatically is when it 15315 09:39:48,200 --> 09:39:52,520 knows you want to store Hi exclamation 15316 09:39:50,596 --> 09:39:54,360 point it puts that somewhere in memory 15317 09:39:52,520 --> 09:39:56,040 the computer then figures out for you 15318 09:39:54,360 --> 09:39:59,040 what's the address of the very first 15319 09:39:56,040 --> 09:40:01,160 character and it stores that address and 15320 09:39:59,040 --> 09:40:03,080 only that address in the variable you 15321 09:40:01,160 --> 09:40:05,360 created on the left hand side of the 15322 09:40:03,080 --> 09:40:07,360 equal sign and that's enough like to 15323 09:40:05,360 --> 09:40:09,276 represent a string with three letters of 15324 09:40:07,360 --> 09:40:12,080 the alph alphabet or punctuation you 15325 09:40:09,276 --> 09:40:14,560 don't need three variables you just need 15326 09:40:12,080 --> 09:40:17,680 one you just need to know the beginning 15327 09:40:14,560 --> 09:40:19,800 of the string why why is it sufficient 15328 09:40:17,680 --> 09:40:24,116 for a variable to Only Store the first 15329 09:40:19,800 --> 09:40:24,116 byes address and not all of the bytes 15330 09:40:24,756 --> 09:40:30,400 addresses exactly because of the design 15331 09:40:27,276 --> 09:40:32,560 of strings per week two we always null 15332 09:40:30,400 --> 09:40:34,240 terminate them so it suffices to only 15333 09:40:32,560 --> 09:40:35,720 remember the first bites address because 15334 09:40:34,240 --> 09:40:37,756 from there you can sort of follow the 15335 09:40:35,720 --> 09:40:40,116 breadcrumbs bite after bite after bite 15336 09:40:37,756 --> 09:40:41,916 and until you see the new line Sorry the 15337 09:40:40,116 --> 09:40:44,000 the null character you know that all of 15338 09:40:41,916 --> 09:40:46,160 those characters are apparently part of 15339 09:40:44,000 --> 09:40:48,116 the same string so this is what's been 15340 09:40:46,160 --> 09:40:50,116 going on in the me computer's memory all 15341 09:40:48,116 --> 09:40:52,200 since week one and in fact if we 15342 09:40:50,116 --> 09:40:56,640 abstract this away you can really think 15343 09:40:52,200 --> 09:40:59,596 of s as being just this really a pointer 15344 09:40:56,640 --> 09:41:02,000 to that chunk of memory so in fact what 15345 09:40:59,596 --> 09:41:04,320 do we have here well in the left to 15346 09:41:02,000 --> 09:41:06,480 recap on the code here on the left hand 15347 09:41:04,320 --> 09:41:08,436 side string that's what ensures that 15348 09:41:06,480 --> 09:41:10,400 we'll actually be able to store a string 15349 09:41:08,436 --> 09:41:12,840 in a variable called s we're going to 15350 09:41:10,400 --> 09:41:14,240 have on the uh right hand side though 15351 09:41:12,840 --> 09:41:16,240 the actual value so let me switch back 15352 09:41:14,240 --> 09:41:18,560 to VSS code here and let me change my 15353 09:41:16,240 --> 09:41:22,840 code to no longer involve integers alone 15354 09:41:18,560 --> 09:41:24,596 so I'm going to add the uh cs50 Library 15355 09:41:22,840 --> 09:41:26,916 just so that I can use some shortcuts in 15356 09:41:24,596 --> 09:41:28,116 there cs50.h and then in my main 15357 09:41:26,916 --> 09:41:30,400 function I'm going to go ahead and do 15358 09:41:28,116 --> 09:41:32,360 this string s equals quote unquote high 15359 09:41:30,400 --> 09:41:33,436 in all caps exclamation point and then 15360 09:41:32,360 --> 09:41:36,480 I'm going to go ahead and print out 15361 09:41:33,436 --> 09:41:38,436 using percent S as always back sln the 15362 09:41:36,480 --> 09:41:40,000 value of s so this program at the moment 15363 09:41:38,436 --> 09:41:42,680 not interesting at all it's just week 15364 09:41:40,000 --> 09:41:46,240 one stuff again/ addresses indeed prints 15365 09:41:42,680 --> 09:41:48,640 out high but it turns out that now that 15366 09:41:46,240 --> 09:41:50,756 I know this what's really been going on 15367 09:41:48,640 --> 09:41:52,596 underneath the hood all this time well 15368 09:41:50,756 --> 09:41:55,116 here's that same line of code that 15369 09:41:52,596 --> 09:41:57,240 defines the variable called s and it 15370 09:41:55,116 --> 09:41:59,916 turns out anyone want to guess what 15371 09:41:57,240 --> 09:42:02,596 string is actually a synonym 15372 09:41:59,916 --> 09:42:04,240 for string it turns out is kind of a 15373 09:42:02,596 --> 09:42:06,756 white lie we've been telling since week 15374 09:42:04,240 --> 09:42:10,000 one there is no such thing as string as 15375 09:42:06,756 --> 09:42:11,960 a keyword in in C it's technically a 15376 09:42:10,000 --> 09:42:15,000 cs50 thing 15377 09:42:11,960 --> 09:42:16,720 yeah it's a pointer to a character so 15378 09:42:15,000 --> 09:42:18,800 really all this time we've kind of been 15379 09:42:16,720 --> 09:42:21,720 lying to you there is no string quote 15380 09:42:18,800 --> 09:42:25,000 unquote it's actually Char star and if I 15381 09:42:21,720 --> 09:42:27,960 may it dramatically here go the training 15382 09:42:25,000 --> 09:42:30,520 wheels like okay that didn't land very 15383 09:42:27,960 --> 09:42:32,240 well so uh what have we been doing well 15384 09:42:30,520 --> 09:42:33,596 it turns out that string is a much 15385 09:42:32,240 --> 09:42:35,160 easier way conceptually to think about 15386 09:42:33,596 --> 09:42:36,520 what a string of characters is like my 15387 09:42:35,160 --> 09:42:38,040 God if we had to start in week one by 15388 09:42:36,520 --> 09:42:39,480 having you type char 15389 09:42:38,040 --> 09:42:41,200 like yeah you might get past it but like 15390 09:42:39,480 --> 09:42:42,880 this is just way too much ugly syntax 15391 09:42:41,200 --> 09:42:45,000 not intellectually interesting at all so 15392 09:42:42,880 --> 09:42:47,680 we abstract it away what a Char star was 15393 09:42:45,000 --> 09:42:49,596 in the first week of C by telling you 15394 09:42:47,680 --> 09:42:51,276 it's actually called string now string 15395 09:42:49,596 --> 09:42:52,640 is a term of art like C programmers 15396 09:42:51,276 --> 09:42:54,560 programmers are in any language we'll 15397 09:42:52,640 --> 09:42:56,596 use the word string to mean a sequence 15398 09:42:54,560 --> 09:42:59,720 of characters but in C it's not 15399 09:42:56,596 --> 09:43:01,640 technically a word unto itself it's 15400 09:42:59,720 --> 09:43:04,200 rather a synonym that we ourselves 15401 09:43:01,640 --> 09:43:06,000 created in some form so in fact how did 15402 09:43:04,200 --> 09:43:08,200 we do this well think back to just last 15403 09:43:06,000 --> 09:43:10,880 week last week I proposed that it'd be 15404 09:43:08,200 --> 09:43:12,720 really nice if we had a person data type 15405 09:43:10,880 --> 09:43:14,360 which the creators of C did not think of 15406 09:43:12,720 --> 09:43:16,880 decades ago but that's okay we can 15407 09:43:14,360 --> 09:43:20,040 Define it ourselves what did we do here 15408 09:43:16,880 --> 09:43:22,640 well using syntax like this recall that 15409 09:43:20,040 --> 09:43:24,960 we defined a person to be what to be 15410 09:43:22,640 --> 09:43:27,436 this structure this structure using the 15411 09:43:24,960 --> 09:43:29,160 new keyword last week struct means that 15412 09:43:27,436 --> 09:43:30,240 a person is just a name and a number and 15413 09:43:29,160 --> 09:43:34,320 it could have been other things we just 15414 09:43:30,240 --> 09:43:36,916 kept it simple but how did I associate 15415 09:43:34,320 --> 09:43:39,320 person with that structure well we 15416 09:43:36,916 --> 09:43:41,756 claimed that it was this value here type 15417 09:43:39,320 --> 09:43:45,080 def which as you might expect defines a 15418 09:43:41,756 --> 09:43:48,320 data type so what did we do as cs50 back 15419 09:43:45,080 --> 09:43:50,800 in week one without telling you well we 15420 09:43:48,320 --> 09:43:52,756 could have done something like this like 15421 09:43:50,800 --> 09:43:53,916 int itself is a little cryptic and maybe 15422 09:43:52,756 --> 09:43:55,840 we should have to keep things even 15423 09:43:53,916 --> 09:43:57,720 simpler said hey everyone turns out you 15424 09:43:55,840 --> 09:43:59,276 can Define integers in C and if you 15425 09:43:57,720 --> 09:44:01,116 wanted to do this well if you want to 15426 09:43:59,276 --> 09:44:04,080 create the keyword integer as a data 15427 09:44:01,116 --> 09:44:06,520 type you can just typ def it to int so 15428 09:44:04,080 --> 09:44:09,436 typed def creates the word on the far 15429 09:44:06,520 --> 09:44:11,720 right integer and U creates a synonym 15430 09:44:09,436 --> 09:44:14,116 for it in this case called int so what 15431 09:44:11,720 --> 09:44:16,680 did we do in week one without telling 15432 09:44:14,116 --> 09:44:20,040 you we have a line of code like this in 15433 09:44:16,680 --> 09:44:23,480 the cs-50 library that Associates quote 15434 09:44:20,040 --> 09:44:26,240 unquote string with more cryptically 15435 09:44:23,480 --> 09:44:29,160 charar and this is why in week one 15436 09:44:26,240 --> 09:44:31,116 onward anytime you use the cs50 library 15437 09:44:29,160 --> 09:44:33,360 you can write the word string as though 15438 09:44:31,116 --> 09:44:34,276 it's a real C data type and that's just 15439 09:44:33,360 --> 09:44:35,756 because we wanted to have this 15440 09:44:34,276 --> 09:44:37,400 abstraction these training wheels on for 15441 09:44:35,756 --> 09:44:39,276 the first weeks so we don't have to get 15442 09:44:37,400 --> 09:44:40,756 weeds of all this crazy memory stuff we 15443 09:44:39,276 --> 09:44:43,040 can sort of talk about strings at a 15444 09:44:40,756 --> 09:44:45,200 higher level but that's all they are 15445 09:44:43,040 --> 09:44:48,840 strings are the address of the first 15446 09:44:45,200 --> 09:44:52,360 character in that sequence of characters 15447 09:44:48,840 --> 09:44:55,080 questions now on any of these details 15448 09:44:52,360 --> 09:44:56,916 yeah strings 15449 09:44:55,080 --> 09:44:59,880 liar good question what about the 15450 09:44:56,916 --> 09:45:01,880 strings Library which we have used um 15451 09:44:59,880 --> 09:45:03,800 unrelated so it does not define the word 15452 09:45:01,880 --> 09:45:06,200 string everything in there actually 15453 09:45:03,800 --> 09:45:08,040 relates to char stars and so in fact if 15454 09:45:06,200 --> 09:45:10,400 you've used the CS5 15455 09:45:08,040 --> 09:45:12,276 uh manual uh which is just our 15456 09:45:10,400 --> 09:45:14,800 userfriendly version of the actual 15457 09:45:12,276 --> 09:45:16,116 manual pages for the official language C 15458 09:45:14,800 --> 09:45:17,960 you'll see throughout that now if you 15459 09:45:16,116 --> 09:45:19,720 start poking around or turning off less 15460 09:45:17,960 --> 09:45:21,436 comfortable mode you'll actually see 15461 09:45:19,720 --> 09:45:23,916 that we've changed any mentions of 15462 09:45:21,436 --> 09:45:25,840 charar in the official documentation for 15463 09:45:23,916 --> 09:45:27,916 these first weeks to just string to 15464 09:45:25,840 --> 09:45:30,596 simplify it but underneath the hood C 15465 09:45:27,916 --> 09:45:32,640 does not know the word string per se as 15466 09:45:30,596 --> 09:45:34,320 a keyword but it's absolutely a concept 15467 09:45:32,640 --> 09:45:36,116 that like every program in the world 15468 09:45:34,320 --> 09:45:37,720 knows about and in fact in other 15469 09:45:36,116 --> 09:45:39,916 languages in Python for instance there 15470 09:45:37,720 --> 09:45:40,916 will actually be a proper string 15471 09:45:39,916 --> 09:45:42,520 although it's not going to be called 15472 09:45:40,916 --> 09:45:46,400 string it's going to be called stir St 15473 09:45:42,520 --> 09:45:48,840 Str for short questions on these strings 15474 09:45:46,400 --> 09:45:50,880 here well let me propose there's one 15475 09:45:48,840 --> 09:45:53,880 other feature of this syntax that we can 15476 09:45:50,880 --> 09:45:55,916 now leverage as follows let me propose 15477 09:45:53,880 --> 09:45:58,436 that if we go back to the previous 15478 09:45:55,916 --> 09:46:00,640 version of my code here wherein let me 15479 09:45:58,436 --> 09:46:02,916 switch back to vs code in just a moment 15480 09:46:00,640 --> 09:46:05,756 I'm going to rewind in vs code to the 15481 09:46:02,916 --> 09:46:07,680 integer version of my code from before 15482 09:46:05,756 --> 09:46:09,480 and most recently it looked looked like 15483 09:46:07,680 --> 09:46:12,756 this before when we were using integers 15484 09:46:09,480 --> 09:46:14,400 only and not in fact strings at all let 15485 09:46:12,756 --> 09:46:16,400 me propose that there's this other 15486 09:46:14,400 --> 09:46:18,276 feature of C that we can use that 15487 09:46:16,400 --> 09:46:20,000 actually allows us to go to an address 15488 09:46:18,276 --> 09:46:21,680 so at the moment let me just rewind and 15489 09:46:20,000 --> 09:46:23,680 do make addresses to remind you what 15490 09:46:21,680 --> 09:46:26,200 this program do did when it was using 15491 09:46:23,680 --> 09:46:28,720 integers alone and there's that address 15492 09:46:26,200 --> 09:46:31,276 why because on line seven notice I'm 15493 09:46:28,720 --> 09:46:32,720 printing out the value of P which is a 15494 09:46:31,276 --> 09:46:35,276 pointer so of course it's going to look 15495 09:46:32,720 --> 09:46:37,596 like an address but let me zoom out now 15496 09:46:35,276 --> 09:46:40,360 and make one change and instead of 15497 09:46:37,596 --> 09:46:42,560 printing out P how can I use today's 15498 09:46:40,360 --> 09:46:45,160 second new operator not the Ampersand 15499 09:46:42,560 --> 09:46:47,800 but the star to actually go to that 15500 09:46:45,160 --> 09:46:49,596 address well what I can actually do on 15501 09:46:47,800 --> 09:46:51,960 this line of code is this if I want to 15502 09:46:49,596 --> 09:46:54,800 print out the actual integer 50 that's 15503 09:46:51,960 --> 09:46:58,116 in that variable or equivalently at that 15504 09:46:54,800 --> 09:46:59,480 address I can go to P here and not print 15505 09:46:58,116 --> 09:47:03,960 P literally because that's just an 15506 09:46:59,480 --> 09:47:05,916 address I can now say star p and star p 15507 09:47:03,960 --> 09:47:08,276 means go there more technically 15508 09:47:05,916 --> 09:47:09,680 dreference p that is follow the treasure 15509 09:47:08,276 --> 09:47:12,276 map to the actual address and do what 15510 09:47:09,680 --> 09:47:14,080 Carter did open the mailbox and print 15511 09:47:12,276 --> 09:47:17,276 whatever was in the mailbox which recall 15512 09:47:14,080 --> 09:47:21,160 was the actual number 50 so let me try 15513 09:47:17,276 --> 09:47:23,160 this let me recompile the code so make 15514 09:47:21,160 --> 09:47:24,840 addresses okay let me clear my terminal 15515 09:47:23,160 --> 09:47:26,880 window do/ addresses this time I 15516 09:47:24,840 --> 09:47:30,720 shouldn't see the ox anything I should 15517 09:47:26,880 --> 09:47:33,160 see just the number 50 in this case and 15518 09:47:30,720 --> 09:47:34,840 here to is kind of a unfortunate design 15519 09:47:33,160 --> 09:47:38,276 decision certainly pedagogically I would 15520 09:47:34,840 --> 09:47:39,880 say in C if I zoom in on this code star 15521 09:47:38,276 --> 09:47:41,360 is unfortunately being used in two 15522 09:47:39,880 --> 09:47:42,840 different ways in an Ideal World they 15523 09:47:41,360 --> 09:47:44,680 would have used three different symbols 15524 09:47:42,840 --> 09:47:46,840 to make this more semantically clear but 15525 09:47:44,680 --> 09:47:49,640 this is what we're stuck with so in line 15526 09:47:46,840 --> 09:47:51,560 six when you declare a pointer that is a 15527 09:47:49,640 --> 09:47:53,520 variable that stores an address you put 15528 09:47:51,560 --> 09:47:55,840 the type of variable that you want to 15529 09:47:53,520 --> 09:47:57,596 point at then a star just because and 15530 09:47:55,840 --> 09:47:59,080 then the name of the variable and on the 15531 09:47:57,596 --> 09:48:01,800 right hand side you actually get the 15532 09:47:59,080 --> 09:48:04,116 address of whatever using Ampersand but 15533 09:48:01,800 --> 09:48:06,116 when you want to go to an address you 15534 09:48:04,116 --> 09:48:08,436 want to dreference a pointer you don't 15535 09:48:06,116 --> 09:48:09,720 use int again and we've never done that 15536 09:48:08,436 --> 09:48:11,240 right once you declare a variable you 15537 09:48:09,720 --> 09:48:12,720 never again mention the data type but in 15538 09:48:11,240 --> 09:48:15,360 the world of pointers now if you want to 15539 09:48:12,720 --> 09:48:19,116 not print out P but go to whatever 15540 09:48:15,360 --> 09:48:21,320 address p is storing you use star P here 15541 09:48:19,116 --> 09:48:23,480 so a good visual indicator would be when 15542 09:48:21,320 --> 09:48:25,756 you declare a pointer that is make it 15543 09:48:23,480 --> 09:48:28,080 exist in your program you have to 15544 09:48:25,756 --> 09:48:30,160 declare the data type with the star but 15545 09:48:28,080 --> 09:48:32,040 when you use a pointer you just use the 15546 09:48:30,160 --> 09:48:33,720 star in an Ideal World this would be a 15547 09:48:32,040 --> 09:48:36,200 completely different symbol but again 15548 09:48:33,720 --> 09:48:38,840 this is what we have questions now on 15549 09:48:36,200 --> 09:48:40,880 that syntax 15550 09:48:38,840 --> 09:48:43,840 [Music] 15551 09:48:40,880 --> 09:48:45,640 yeah uh why can't we just do the Amper 15552 09:48:43,840 --> 09:48:47,560 sand here are you saying it was still a 15553 09:48:45,640 --> 09:48:49,240 little quiet so strictly speaking we do 15554 09:48:47,560 --> 09:48:52,680 not need line six so this is really for 15555 09:48:49,240 --> 09:48:54,916 pedagogical sake that I am um defining a 15556 09:48:52,680 --> 09:48:56,436 separate variable p and then printing it 15557 09:48:54,916 --> 09:48:58,436 out at this point though I'm just kind 15558 09:48:56,436 --> 09:49:00,276 of you know going in circles if you will 15559 09:48:58,436 --> 09:49:02,000 because more simple would have been what 15560 09:49:00,276 --> 09:49:04,360 I would have done in week one which 15561 09:49:02,000 --> 09:49:06,680 would be get rid of P alt together get 15562 09:49:04,360 --> 09:49:08,320 rid of P here and just print out n right 15563 09:49:06,680 --> 09:49:11,116 but today we're just giving you this new 15564 09:49:08,320 --> 09:49:12,640 building block these this new syntax via 15565 09:49:11,116 --> 09:49:14,360 which you can figure out the address of 15566 09:49:12,640 --> 09:49:17,320 something and then reverse the process 15567 09:49:14,360 --> 09:49:19,916 later and actually go to it as 15568 09:49:17,320 --> 09:49:21,360 well other questions on what we've done 15569 09:49:19,916 --> 09:49:23,360 here with these 15570 09:49:21,360 --> 09:49:25,640 pointers all right well let's context 15571 09:49:23,360 --> 09:49:28,160 switch back to the string now and see 15572 09:49:25,640 --> 09:49:31,960 what more we can do with this here in 15573 09:49:28,160 --> 09:49:34,960 the case of our strings here let me uh 15574 09:49:31,960 --> 09:49:37,200 refine this to zoom out let me delete 15575 09:49:34,960 --> 09:49:39,596 the integer related code here let me do 15576 09:49:37,200 --> 09:49:41,960 string s equals quote unquote high in 15577 09:49:39,596 --> 09:49:45,160 all caps let me go ahead and for the 15578 09:49:41,960 --> 09:49:48,276 moment include cs50.h at the top so that 15579 09:49:45,160 --> 09:49:50,756 indeed I can use the keyword s or string 15580 09:49:48,276 --> 09:49:52,560 rather and let me go ahead now and do 15581 09:49:50,756 --> 09:49:55,640 something more than I did last time last 15582 09:49:52,560 --> 09:49:57,480 time I did print F of percent s back sln 15583 09:49:55,640 --> 09:49:59,596 and then I printed out s and again I'll 15584 09:49:57,480 --> 09:50:01,480 recompile this just for clarity make 15585 09:49:59,596 --> 09:50:03,720 addresses addresses that just prints out 15586 09:50:01,480 --> 09:50:05,320 high so that's again week one stuff but 15587 09:50:03,720 --> 09:50:08,240 now that we have this other bit of 15588 09:50:05,320 --> 09:50:10,680 syntax we can do some interesting things 15589 09:50:08,240 --> 09:50:12,916 too so for instance suppose I want to 15590 09:50:10,680 --> 09:50:15,040 print out not s itself but what if I 15591 09:50:12,916 --> 09:50:17,640 want to print out the address of s like 15592 09:50:15,040 --> 09:50:19,720 at what memory location is s well I can 15593 09:50:17,640 --> 09:50:21,840 change my percent s to percent P which 15594 09:50:19,720 --> 09:50:24,116 now we know P is for pointer so percent 15595 09:50:21,840 --> 09:50:26,880 p means print out the value of a pointer 15596 09:50:24,116 --> 09:50:29,960 that is an address and here I can 15597 09:50:26,880 --> 09:50:33,200 actually print out s itself but why that 15598 09:50:29,960 --> 09:50:36,040 is we'll see in a moment let me do this 15599 09:50:33,200 --> 09:50:37,596 here go the training wheels string does 15600 09:50:36,040 --> 09:50:39,840 not technically EX exist but it does if 15601 09:50:37,596 --> 09:50:41,680 I'm using the cs-50 library but if I get 15602 09:50:39,840 --> 09:50:43,080 rid of the cs50 library as I'm 15603 09:50:41,680 --> 09:50:44,880 metaphorically doing by taking off the 15604 09:50:43,080 --> 09:50:46,320 training wheels I can't use the word 15605 09:50:44,880 --> 09:50:48,000 string anymore and in fact let me make 15606 09:50:46,320 --> 09:50:50,160 this mistake deliberately as you might 15607 09:50:48,000 --> 09:50:52,480 have accidentally in past weeks here is 15608 09:50:50,160 --> 09:50:55,116 the error message I get if I forget the 15609 09:50:52,480 --> 09:50:57,320 cs50 library use of Undeclared 15610 09:50:55,116 --> 09:50:58,880 identifier string did you mean standard 15611 09:50:57,320 --> 09:51:00,640 in it's trying to be helpful but it's 15612 09:50:58,880 --> 09:51:02,560 not because I didn't mean standard in so 15613 09:51:00,640 --> 09:51:05,000 indeed this is confirmation that c does 15614 09:51:02,560 --> 09:51:07,200 not know the word string exists at least 15615 09:51:05,000 --> 09:51:10,276 as a keyword exists as a concept but not 15616 09:51:07,200 --> 09:51:12,000 a keyword so I could fix this by adding 15617 09:51:10,276 --> 09:51:13,680 back the cs50 library but that's kind of 15618 09:51:12,000 --> 09:51:15,560 a step backwards educationally is 15619 09:51:13,680 --> 09:51:17,840 instead of a step forward what could I 15620 09:51:15,560 --> 09:51:22,040 do instead to fix this now if the 15621 09:51:17,840 --> 09:51:22,040 training wheels are now off 15622 09:51:22,720 --> 09:51:27,916 yeah exactly replace string quote 15623 09:51:25,360 --> 09:51:29,116 unquote with Char star instead so I'm 15624 09:51:27,916 --> 09:51:31,596 going to go ahead and change this to 15625 09:51:29,116 --> 09:51:34,040 char technically you can put the literal 15626 09:51:31,596 --> 09:51:35,520 star here the asterisk or you can put it 15627 09:51:34,040 --> 09:51:37,240 there or you can put it here by 15628 09:51:35,520 --> 09:51:39,276 convention is to do what I done from the 15629 09:51:37,240 --> 09:51:41,276 beginning put the star next to the name 15630 09:51:39,276 --> 09:51:44,400 of the variable as opposed to anywhere 15631 09:51:41,276 --> 09:51:45,800 else uh let me go ahead now and or sorry 15632 09:51:44,400 --> 09:51:47,200 I meant to add the spaces there you 15633 09:51:45,800 --> 09:51:49,276 could do this too but this would be the 15634 09:51:47,200 --> 09:51:52,480 most normal convention so now let's do 15635 09:51:49,276 --> 09:51:55,160 this make addresses compile is okay now 15636 09:51:52,480 --> 09:51:59,000 do slash addresses what should I see 15637 09:51:55,160 --> 09:52:00,160 high or something else feel free to just 15638 09:51:59,000 --> 09:52:03,520 call it 15639 09:52:00,160 --> 09:52:06,276 out so still high you say someone 15640 09:52:03,520 --> 09:52:07,880 else memory location a memory location 15641 09:52:06,276 --> 09:52:09,116 all right so could be one of the two 15642 09:52:07,880 --> 09:52:10,960 options right either I'm going to see 15643 09:52:09,116 --> 09:52:12,960 the string or I'm going to see a memory 15644 09:52:10,960 --> 09:52:14,200 address though I do in fact see a memory 15645 09:52:12,960 --> 09:52:16,436 address and this one's quite different 15646 09:52:14,200 --> 09:52:19,200 from the integer one but does anyone now 15647 09:52:16,436 --> 09:52:21,596 want to explain why you were correct why 15648 09:52:19,200 --> 09:52:25,720 am I seeing the address down here and 15649 09:52:21,596 --> 09:52:25,720 not hi it's subtle 15650 09:52:25,960 --> 09:52:30,880 yeah exactly because I left my percent P 15651 09:52:29,080 --> 09:52:32,756 there which means hey print F show me a 15652 09:52:30,880 --> 09:52:35,080 pointer but this is where printf is 15653 09:52:32,756 --> 09:52:38,116 smart and has been smart since week zero 15654 09:52:35,080 --> 09:52:40,320 humans who invented printf decades ago 15655 09:52:38,116 --> 09:52:42,596 uh wrote code that notices that okay 15656 09:52:40,320 --> 09:52:45,320 percent s means to treat the following 15657 09:52:42,596 --> 09:52:47,596 value not as just an address per se that 15658 09:52:45,320 --> 09:52:49,240 gets printed literally but printed as 15659 09:52:47,596 --> 09:52:51,800 with the mailbox demo is sort of a 15660 09:52:49,240 --> 09:52:54,116 treasure map that leads you to the 15661 09:52:51,800 --> 09:52:55,960 address of a character so simply by 15662 09:52:54,116 --> 09:52:58,000 changing one character percent P to 15663 09:52:55,960 --> 09:53:00,680 percent s and if I now do make addresses 15664 09:52:58,000 --> 09:53:02,960 again and slash addresses this now is 15665 09:53:00,680 --> 09:53:05,596 identical to week one but hopefully 15666 09:53:02,960 --> 09:53:08,436 makes sense because percent s is just a 15667 09:53:05,596 --> 09:53:11,880 clue to print f that means go to this 15668 09:53:08,436 --> 09:53:14,916 address in s print out every character 15669 09:53:11,880 --> 09:53:16,756 there and thereafter until you see what 15670 09:53:14,916 --> 09:53:18,680 the null character and then stop 15671 09:53:16,756 --> 09:53:21,596 printing anything more and this is why 15672 09:53:18,680 --> 09:53:24,040 hi has printed since week one today we 15673 09:53:21,596 --> 09:53:25,480 can see the address percent P but this 15674 09:53:24,040 --> 09:53:28,360 combination of having access to 15675 09:53:25,480 --> 09:53:30,000 addresses and the null Terminator is all 15676 09:53:28,360 --> 09:53:32,040 the information printf needs to actually 15677 09:53:30,000 --> 09:53:34,276 do something more useful by like 15678 09:53:32,040 --> 09:53:37,200 printing the actual 15679 09:53:34,276 --> 09:53:42,596 strings any questions now on this 15680 09:53:37,200 --> 09:53:45,480 approach to percent s yeah in 15681 09:53:42,596 --> 09:53:46,960 back oh so why is it traditionally being 15682 09:53:45,480 --> 09:53:48,680 used in this way honestly like the word 15683 09:53:46,960 --> 09:53:50,520 string has been around for decades it's 15684 09:53:48,680 --> 09:53:52,320 not a keyword you should be able to type 15685 09:53:50,520 --> 09:53:54,680 in C unless you're using a library like 15686 09:53:52,320 --> 09:53:56,400 cs50 um and so percent s just means 15687 09:53:54,680 --> 09:53:59,116 string so even though it doesn't exist 15688 09:53:56,400 --> 09:54:01,116 as a keyword percent s connotes string 15689 09:53:59,116 --> 09:54:02,320 and humans decades ago like today just 15690 09:54:01,116 --> 09:54:03,520 kind of know what that means so they 15691 09:54:02,320 --> 09:54:05,520 could have chosen any letter of the 15692 09:54:03,520 --> 09:54:09,800 alphabet but s sort of makes the most 15693 09:54:05,520 --> 09:54:11,960 sense all right well let's in back other 15694 09:54:09,800 --> 09:54:14,640 question good question before let me 15695 09:54:11,960 --> 09:54:18,960 zoom in I did not use a star before the 15696 09:54:14,640 --> 09:54:21,560 S why well it's subtle here but printf 15697 09:54:18,960 --> 09:54:24,480 is was invented years ago to know given 15698 09:54:21,560 --> 09:54:25,960 an address like in the variable s printf 15699 09:54:24,480 --> 09:54:27,880 knows to go there so if we looked at the 15700 09:54:25,960 --> 09:54:30,880 source code that some human wrote years 15701 09:54:27,880 --> 09:54:32,720 ago for C we would likely see the actual 15702 09:54:30,880 --> 09:54:34,480 uh asterisk that you're referring to 15703 09:54:32,720 --> 09:54:37,916 printf is taking on the responsibility 15704 09:54:34,480 --> 09:54:40,800 for going to S if you were to do uh star 15705 09:54:37,916 --> 09:54:43,596 s here instead an asterisk and then s 15706 09:54:40,800 --> 09:54:46,200 that would now be literally a character 15707 09:54:43,596 --> 09:54:47,720 because if I say p uh star s that means 15708 09:54:46,200 --> 09:54:49,000 go to the address in s and all you're 15709 09:54:47,720 --> 09:54:51,916 going to find there is a single 15710 09:54:49,000 --> 09:54:53,960 character what printf wants to know is 15711 09:54:51,916 --> 09:54:55,916 not what is the character there what is 15712 09:54:53,960 --> 09:54:58,400 the address of that character why 15713 09:54:55,916 --> 09:55:01,276 because printf needs to walk through the 15714 09:54:58,400 --> 09:55:03,640 rest of those characters looking for the 15715 09:55:01,276 --> 09:55:05,480 final null character and in fact let me 15716 09:55:03,640 --> 09:55:07,520 see with a bit more syntax if we can 15717 09:55:05,480 --> 09:55:09,720 highlight this a bit more let me do this 15718 09:55:07,520 --> 09:55:11,400 in addition to printing s let's try out 15719 09:55:09,720 --> 09:55:14,596 our syntax in another way let me print 15720 09:55:11,400 --> 09:55:16,596 out with percent s how about uh not s 15721 09:55:14,596 --> 09:55:20,240 here but let's print out some addresses 15722 09:55:16,596 --> 09:55:22,880 percent s back sln close quote and then 15723 09:55:20,240 --> 09:55:24,680 let's print out how about this the first 15724 09:55:22,880 --> 09:55:27,640 character in the string s would be 15725 09:55:24,680 --> 09:55:30,800 called s bracket Z but how do I get the 15726 09:55:27,640 --> 09:55:32,276 address of the first character in s well 15727 09:55:30,800 --> 09:55:33,960 I could technically just use today's new 15728 09:55:32,276 --> 09:55:35,800 primitive I can just add an ampon that 15729 09:55:33,960 --> 09:55:40,160 always gives me the address of some 15730 09:55:35,800 --> 09:55:41,596 value so when I end this thought and 15731 09:55:40,160 --> 09:55:44,000 clear my terminal window and run make 15732 09:55:41,596 --> 09:55:46,000 addresses still compiles when I run 15733 09:55:44,000 --> 09:55:48,880 addresses in just a moment any guesses 15734 09:55:46,000 --> 09:55:50,200 as to what I will see line by line this 15735 09:55:48,880 --> 09:55:52,160 will print out two 15736 09:55:50,200 --> 09:55:54,240 things and you don't have to remember 15737 09:55:52,160 --> 09:55:57,240 what the actual number was but at a high 15738 09:55:54,240 --> 09:55:59,756 level what will be printed now the same 15739 09:55:57,240 --> 09:56:01,360 thing twice why well when I run this 15740 09:55:59,756 --> 09:56:03,520 what I'm printing here and let me zoom 15741 09:56:01,360 --> 09:56:05,680 in at the bottom I ined see two really 15742 09:56:03,520 --> 09:56:08,596 long addresses but they're in fact the 15743 09:56:05,680 --> 09:56:11,400 same why well that's because again if s 15744 09:56:08,596 --> 09:56:14,040 is the address of a character as implied 15745 09:56:11,400 --> 09:56:16,960 Now by either the cs50 word string or 15746 09:56:14,040 --> 09:56:19,840 the actual phrase Char star well then s 15747 09:56:16,960 --> 09:56:23,640 is just an address by contrast per week 15748 09:56:19,840 --> 09:56:25,436 two s bracket 0 is a Char always has 15749 09:56:23,640 --> 09:56:27,240 been a Char a specific Char but if you 15750 09:56:25,436 --> 09:56:29,560 want the address of that Char you just 15751 09:56:27,240 --> 09:56:31,436 add the Ampersand well it turns out that 15752 09:56:29,560 --> 09:56:33,276 strings per the definition we keep 15753 09:56:31,436 --> 09:56:34,840 emphasizing is just the address of the 15754 09:56:33,276 --> 09:56:36,436 first character in a string so of course 15755 09:56:34,840 --> 09:56:38,320 if you do this you're going to see the 15756 09:56:36,436 --> 09:56:40,116 exact same thing and if I do this a bit 15757 09:56:38,320 --> 09:56:41,960 more generally you don't want to copy 15758 09:56:40,116 --> 09:56:43,800 paste but this is just for uh 15759 09:56:41,960 --> 09:56:45,680 visualization sake let me print out all 15760 09:56:43,800 --> 09:56:47,596 the characters so another another 15761 09:56:45,680 --> 09:56:50,400 another and let me change this to print 15762 09:56:47,596 --> 09:56:52,880 out the address of bracket 1 bracket two 15763 09:56:50,400 --> 09:56:54,800 and bracket three so all four characters 15764 09:56:52,880 --> 09:56:57,040 Hi exclamation point and the null 15765 09:56:54,800 --> 09:56:59,000 character notice I'm using percent P for 15766 09:56:57,040 --> 09:57:01,840 all of them so if I now do make 15767 09:56:59,000 --> 09:57:04,200 addresses and do/ addresses now notice 15768 09:57:01,840 --> 09:57:06,200 and this is kind of cool the first two 15769 09:57:04,200 --> 09:57:10,160 are indeed still the same but what's no 15770 09:57:06,200 --> 09:57:10,160 noteworthy about the other values on the 15771 09:57:10,320 --> 09:57:14,360 screen yeah they're consecutive each of 15772 09:57:12,680 --> 09:57:15,596 these is just one bite away even if 15773 09:57:14,360 --> 09:57:17,480 you're not good at hex yet and there's a 15774 09:57:15,596 --> 09:57:18,916 crazy number of digits here who cares 15775 09:57:17,480 --> 09:57:21,960 they're all the same except for the last 15776 09:57:18,916 --> 09:57:23,400 ones four four and then 5 six seven and 15777 09:57:21,960 --> 09:57:25,680 this confirms what I've been claiming 15778 09:57:23,400 --> 09:57:27,320 for weeks is that in an array all of the 15779 09:57:25,680 --> 09:57:31,116 characters are back to back to back 15780 09:57:27,320 --> 09:57:32,880 contiguous One Bites away so with just 15781 09:57:31,116 --> 09:57:34,480 this Ampersand with just this star like 15782 09:57:32,880 --> 09:57:36,480 it's actually a pretty cool tool in the 15783 09:57:34,480 --> 09:57:39,040 toolkit to have because you can start to 15784 09:57:36,480 --> 09:57:41,596 poke around what's actually going on 15785 09:57:39,040 --> 09:57:44,480 inside of the computer's memory and in 15786 09:57:41,596 --> 09:57:46,960 fact if we do this I can introduce one 15787 09:57:44,480 --> 09:57:50,436 other cool trick here if you will let me 15788 09:57:46,960 --> 09:57:52,520 propose that we can actually now do 15789 09:57:50,436 --> 09:57:54,320 arithmetic on pointers and you don't 15790 09:57:52,520 --> 09:57:56,436 have to you'll see a simpler way to do 15791 09:57:54,320 --> 09:57:58,880 this but now that you have perhaps this 15792 09:57:56,436 --> 09:58:00,880 underlying understanding of where things 15793 09:57:58,880 --> 09:58:02,720 are in memory and it's just addresses we 15794 09:58:00,880 --> 09:58:05,116 can actually do something kind of neat 15795 09:58:02,720 --> 09:58:07,756 we can do something like this uh let me 15796 09:58:05,116 --> 09:58:10,276 go back to how about uh the string 15797 09:58:07,756 --> 09:58:12,916 version of this with high and let me do 15798 09:58:10,276 --> 09:58:14,720 this instead let me um clean this up a 15799 09:58:12,916 --> 09:58:16,680 bit get rid of some of these lines of 15800 09:58:14,720 --> 09:58:19,480 code and let me do this let me print out 15801 09:58:16,680 --> 09:58:20,916 percent C percent C percent C let me get 15802 09:58:19,480 --> 09:58:23,436 rid of all these Amper Sands we're going 15803 09:58:20,916 --> 09:58:25,756 to roll back to like week two stuff just 15804 09:58:23,436 --> 09:58:28,520 to be clear when I compile and run this 15805 09:58:25,756 --> 09:58:31,080 version of the program and I'll zoom in 15806 09:58:28,520 --> 09:58:33,800 what should get printed on the screen 15807 09:58:31,080 --> 09:58:36,840 this is just week two stuff now no 15808 09:58:33,800 --> 09:58:40,000 pointers per se yeah 15809 09:58:36,840 --> 09:58:41,480 mhm just Hi exclamation point one per 15810 09:58:40,000 --> 09:58:43,560 line because I have all of these back 15811 09:58:41,480 --> 09:58:46,480 slash ends so let me do that let me go 15812 09:58:43,560 --> 09:58:49,000 down here make addresses enter okay 15813 09:58:46,480 --> 09:58:51,596 pretty good/ addresses and indeed Hi 15814 09:58:49,000 --> 09:58:52,800 exclamation point But now if you're 15815 09:58:51,596 --> 09:58:54,200 getting a little more comfortable and 15816 09:58:52,800 --> 09:58:55,756 it's fine if you're not yet today but 15817 09:58:54,200 --> 09:58:57,080 over the coming week or weeks as you get 15818 09:58:55,756 --> 09:58:59,400 a little more comfortable with the 15819 09:58:57,080 --> 09:59:01,276 equivalence of addresses with our 15820 09:58:59,400 --> 09:59:03,400 definition in the past of arrays and 15821 09:59:01,276 --> 09:59:05,880 strings and all of this you can start to 15822 09:59:03,400 --> 09:59:08,320 play around and I can do this instead if 15823 09:59:05,880 --> 09:59:10,840 I I want to print out the first 15824 09:59:08,320 --> 09:59:12,480 character in the string I could do like 15825 09:59:10,840 --> 09:59:13,880 week two s bracket zero like that will 15826 09:59:12,480 --> 09:59:15,320 always work and you can keep using that 15827 09:59:13,880 --> 09:59:18,040 that's not a cs50 thing it's just a 15828 09:59:15,320 --> 09:59:21,640 convenience in C but I could technically 15829 09:59:18,040 --> 09:59:23,160 print out not s because s is an address 15830 09:59:21,640 --> 09:59:26,276 but what would be the syntax I could use 15831 09:59:23,160 --> 09:59:28,116 to say print out the character at 15832 09:59:26,276 --> 09:59:30,960 s any 15833 09:59:28,116 --> 09:59:31,880 Instinct how can I say go to the address 15834 09:59:30,960 --> 09:59:34,960 in 15835 09:59:31,880 --> 09:59:38,320 s it's one of two possible answers 15836 09:59:34,960 --> 09:59:39,756 today so of our two new uh uh of our two 15837 09:59:38,320 --> 09:59:42,480 new operators today we have the 15838 09:59:39,756 --> 09:59:45,520 Ampersand and the star which one will 15839 09:59:42,480 --> 09:59:47,596 lead us to what is that an address so 15840 09:59:45,520 --> 09:59:50,320 the star so in fact if I want to print 15841 09:59:47,596 --> 09:59:53,680 out what is that address zero at the 15842 09:59:50,320 --> 09:59:55,480 address S I can just do star s and if 15843 09:59:53,680 --> 09:59:57,200 you really want to get fancy how do you 15844 09:59:55,480 --> 09:59:58,720 print out the second character that's 15845 09:59:57,200 --> 10:00:01,276 immediately to the right of it so to 15846 09:59:58,720 --> 10:00:02,916 speak well you can go to with the D 15847 10:00:01,276 --> 10:00:03,880 reference operator and do you want to 15848 10:00:02,916 --> 10:00:08,200 answer this 15849 10:00:03,880 --> 10:00:09,840 one s+ one Argo pointer arithmetic like 15850 10:00:08,200 --> 10:00:11,680 you can do math simple addition 15851 10:00:09,840 --> 10:00:14,040 subtraction whatever on pointers if you 15852 10:00:11,680 --> 10:00:15,880 want and you can do this here too so 15853 10:00:14,040 --> 10:00:17,200 star you want to pluck this one off too 15854 10:00:15,880 --> 10:00:18,756 how do I print out the last character 15855 10:00:17,200 --> 10:00:21,960 the 15856 10:00:18,756 --> 10:00:23,276 third s plus2 right because if you know 15857 10:00:21,960 --> 10:00:24,640 and understand that like a string is 15858 10:00:23,276 --> 10:00:26,520 just a sequence of characters every 15859 10:00:24,640 --> 10:00:28,880 character is just a bite and these bites 15860 10:00:26,520 --> 10:00:30,560 are back to back to back you can just go 15861 10:00:28,880 --> 10:00:32,800 wherever you want in the computer's 15862 10:00:30,560 --> 10:00:35,560 memory and here I can do make addresses 15863 10:00:32,800 --> 10:00:37,680 again/ addresses and voila we now have 15864 10:00:35,560 --> 10:00:39,596 high exclamation point so we haven't 15865 10:00:37,680 --> 10:00:41,116 printed out anything new but again just 15866 10:00:39,596 --> 10:00:43,080 by using these two new operators the 15867 10:00:41,116 --> 10:00:45,040 erson and the star you can figure out 15868 10:00:43,080 --> 10:00:47,160 the address of something and you can go 15869 10:00:45,040 --> 10:00:48,800 to the address of something okay 15870 10:00:47,160 --> 10:00:51,400 question in 15871 10:00:48,800 --> 10:00:52,880 back indeed it ends up being the exact 15872 10:00:51,400 --> 10:00:55,520 same and so I might have used this term 15873 10:00:52,880 --> 10:00:57,320 before the Amper sand technique where 15874 10:00:55,520 --> 10:00:59,680 sorry the square bracket technique where 15875 10:00:57,320 --> 10:01:01,320 you do s bracket 0o s bracket One S 15876 10:00:59,680 --> 10:01:03,320 bracket 2 that's actually what we would 15877 10:01:01,320 --> 10:01:04,916 really call syntactic Sugar like it 15878 10:01:03,320 --> 10:01:06,360 works and you can use it you should use 15879 10:01:04,916 --> 10:01:07,916 it it's nice and simple 15880 10:01:06,360 --> 10:01:10,200 but the square bracket notation 15881 10:01:07,916 --> 10:01:12,480 underneath the hood is essentially being 15882 10:01:10,200 --> 10:01:13,480 converted to this which this is not fun 15883 10:01:12,480 --> 10:01:14,640 right like this is when you want to show 15884 10:01:13,480 --> 10:01:16,560 off to your friends like you know how to 15885 10:01:14,640 --> 10:01:19,000 do cool stuff in code but this is not as 15886 10:01:16,560 --> 10:01:20,596 readable as just s bracket Z and one and 15887 10:01:19,000 --> 10:01:22,200 two but that's all that's happening 15888 10:01:20,596 --> 10:01:24,116 underneath the hood and so again this is 15889 10:01:22,200 --> 10:01:25,560 why in cs50 we spend time on some of 15890 10:01:24,116 --> 10:01:27,320 these lower level building blocks 15891 10:01:25,560 --> 10:01:28,756 because if you assume that indeed your 15892 10:01:27,320 --> 10:01:31,596 computer's memory is just this grid of 15893 10:01:28,756 --> 10:01:34,040 btes and you have now the code ability 15894 10:01:31,596 --> 10:01:36,080 in code to get an address and go to an 15895 10:01:34,040 --> 10:01:37,596 address you can start doing any anything 15896 10:01:36,080 --> 10:01:39,360 you want and you can poke around a 15897 10:01:37,596 --> 10:01:41,320 computer's memory at any location and 15898 10:01:39,360 --> 10:01:42,960 here in lies the danger like I'm kind of 15899 10:01:41,320 --> 10:01:45,360 on the honor System right now that if my 15900 10:01:42,960 --> 10:01:47,560 string is high exclamation point it's 15901 10:01:45,360 --> 10:01:49,720 kind of up to me to go to the first bite 15902 10:01:47,560 --> 10:01:51,480 the second and the third but I could get 15903 10:01:49,720 --> 10:01:53,756 kind of crazy now and if I want to see 15904 10:01:51,480 --> 10:01:55,200 what's going on in the computer's memory 15905 10:01:53,756 --> 10:01:58,400 I mean there's nothing stopping me from 15906 10:01:55,200 --> 10:02:01,720 doing like s+ 50 and let's see what's 15907 10:01:58,400 --> 10:02:03,840 there so make addresses do/ addresses Hi 15908 10:02:01,720 --> 10:02:06,400 and then okay nothing it seems well how 15909 10:02:03,840 --> 10:02:07,880 about 5,000 bytes away let's poke around 15910 10:02:06,400 --> 10:02:10,756 what's inside of the computer's memory 15911 10:02:07,880 --> 10:02:13,720 so make addresses again uh make 15912 10:02:10,756 --> 10:02:15,480 addresses addresses enter okay still 15913 10:02:13,720 --> 10:02:19,200 nothing there let's try 15914 10:02:15,480 --> 10:02:22,160 50,000 all right do make addresses do SL 15915 10:02:19,200 --> 10:02:23,520 addresses okay there we see it so you've 15916 10:02:22,160 --> 10:02:25,000 probably done this some of you by 15917 10:02:23,520 --> 10:02:26,480 accident because you probably went too 15918 10:02:25,000 --> 10:02:28,800 far to the left or to the right in an 15919 10:02:26,480 --> 10:02:30,360 array touching memory that you shouldn't 15920 10:02:28,800 --> 10:02:33,320 suffice it to say I should not go 15921 10:02:30,360 --> 10:02:35,240 blindly touching 50,000 bytes away cuz 15922 10:02:33,320 --> 10:02:36,880 who knows what's there and indeed in 15923 10:02:35,240 --> 10:02:39,276 your computer computer when a program is 15924 10:02:36,880 --> 10:02:41,160 running the computer uh segments it into 15925 10:02:39,276 --> 10:02:43,276 different segments of memory and if you 15926 10:02:41,160 --> 10:02:44,916 get a little too greedy and you touch 15927 10:02:43,276 --> 10:02:46,800 another segment of memory that 15928 10:02:44,916 --> 10:02:48,680 technically was not allocated to you by 15929 10:02:46,800 --> 10:02:50,880 Mac OS or Windows or Linux or the 15930 10:02:48,680 --> 10:02:52,560 operating system bad things happen and 15931 10:02:50,880 --> 10:02:54,720 you get a segmentation fault and that 15932 10:02:52,560 --> 10:02:56,880 means it's a bug in your code so you can 15933 10:02:54,720 --> 10:02:58,800 now do this and this means hackers too 15934 10:02:56,880 --> 10:03:01,596 can do things like this if they can 15935 10:02:58,800 --> 10:03:03,240 somehow inject code into your C program 15936 10:03:01,596 --> 10:03:05,116 maybe they can poke around the 15937 10:03:03,240 --> 10:03:06,680 computer's memory and indeed this is 15938 10:03:05,116 --> 10:03:08,960 kind of the technique whereby maybe a 15939 10:03:06,680 --> 10:03:10,360 really sophisticated hacker can jump to 15940 10:03:08,960 --> 10:03:12,840 this memory this memory this memory 15941 10:03:10,360 --> 10:03:14,596 looking for something like your password 15942 10:03:12,840 --> 10:03:16,160 or your financial information or 15943 10:03:14,596 --> 10:03:17,880 anything that's in the program but at 15944 10:03:16,160 --> 10:03:20,520 some other address there's nothing 15945 10:03:17,880 --> 10:03:22,360 stopping an adversary at least right now 15946 10:03:20,520 --> 10:03:24,400 from poking around if they can execute 15947 10:03:22,360 --> 10:03:25,916 code on your computer from doing this 15948 10:03:24,400 --> 10:03:27,880 kind of thing so there and again is the 15949 10:03:25,916 --> 10:03:30,000 power of C but also the danger and 15950 10:03:27,880 --> 10:03:31,840 you'll absolutely suffer more segals in 15951 10:03:30,000 --> 10:03:34,080 the coming days but ultimately the goal 15952 10:03:31,840 --> 10:03:37,000 is going to be to help you solve them 15953 10:03:34,080 --> 10:03:40,680 ultimately uh and fix things um but for 15954 10:03:37,000 --> 10:03:42,880 now I think that was that was quite a 15955 10:03:40,680 --> 10:03:44,960 bit so let me propose that we go ahead 15956 10:03:42,880 --> 10:03:46,680 and take our longer break here maybe 10 15957 10:03:44,960 --> 10:03:48,960 minutes and have ourselves some whoopy 15958 10:03:46,680 --> 10:03:53,200 pies in the transcept we'll be back in 15959 10:03:48,960 --> 10:03:55,360 10 all right so we're back and to recap 15960 10:03:53,200 --> 10:03:57,320 where we left off you now have this new 15961 10:03:55,360 --> 10:03:59,240 capability in code to do pointer 15962 10:03:57,320 --> 10:04:01,276 arithmetic like treat addresses as 15963 10:03:59,240 --> 10:04:03,320 numbers which they really are in heximal 15964 10:04:01,276 --> 10:04:04,756 or otherwise and like add them together 15965 10:04:03,320 --> 10:04:07,040 and kind of poke around a computer's 15966 10:04:04,756 --> 10:04:09,436 memory and it was asked during break 15967 10:04:07,040 --> 10:04:11,200 actually how we might further harness 15968 10:04:09,436 --> 10:04:12,720 this in the context of string so I 15969 10:04:11,200 --> 10:04:15,360 didn't change the code we wrote just 15970 10:04:12,720 --> 10:04:18,000 before break recall that we last broke 15971 10:04:15,360 --> 10:04:20,080 the program by checking out bytes 50,000 15972 10:04:18,000 --> 10:04:21,720 bytes away but let's not do that and 15973 10:04:20,080 --> 10:04:24,320 let's actually try printing out not 15974 10:04:21,720 --> 10:04:26,480 individual characters like I did per the 15975 10:04:24,320 --> 10:04:29,040 percent C but why don't we try printing 15976 10:04:26,480 --> 10:04:30,960 out strings and substrings if you will 15977 10:04:29,040 --> 10:04:33,240 so let me clear my terminal window let 15978 10:04:30,960 --> 10:04:35,800 me change all of these percent C's to 15979 10:04:33,240 --> 10:04:37,640 percent s percent s percent s and then 15980 10:04:35,800 --> 10:04:39,560 let me rewind to what we've been doing 15981 10:04:37,640 --> 10:04:41,400 since week one with strings which is 15982 10:04:39,560 --> 10:04:43,756 just print them out for instance with 15983 10:04:41,400 --> 10:04:46,116 that first line and the only difference 15984 10:04:43,756 --> 10:04:48,360 at the moment is that now I took off the 15985 10:04:46,116 --> 10:04:52,240 training wheels I got rid of cs50.h 15986 10:04:48,360 --> 10:04:54,596 wherein string is typ Def to char star 15987 10:04:52,240 --> 10:04:56,116 for you got rid of that so now on line 15988 10:04:54,596 --> 10:04:57,596 five I'm declaring S as being a Char 15989 10:04:56,116 --> 10:04:59,240 star which just means the address of a 15990 10:04:57,596 --> 10:05:00,720 character and print f is smart enough to 15991 10:04:59,240 --> 10:05:02,880 know that the end of a string is 15992 10:05:00,720 --> 10:05:05,040 wherever that null character is but now 15993 10:05:02,880 --> 10:05:07,000 that I can do pointer arithmetic notice 15994 10:05:05,040 --> 10:05:09,520 that I could do something like this if I 15995 10:05:07,000 --> 10:05:13,200 want to print out s i just print out s 15996 10:05:09,520 --> 10:05:15,040 suppose I do s+ one here and s+2 here 15997 10:05:13,200 --> 10:05:19,240 again after changing percent C to 15998 10:05:15,040 --> 10:05:21,800 percent s any intuition around what this 15999 10:05:19,240 --> 10:05:23,436 code will now print on the screen line 16000 10:05:21,800 --> 10:05:25,360 by 16001 10:05:23,436 --> 10:05:27,400 line yeah 16002 10:05:25,360 --> 10:05:30,276 thoughts okay reasonable conjecture 16003 10:05:27,400 --> 10:05:32,430 maybe the memory address of H that of I 16004 10:05:30,276 --> 10:05:33,640 that of exclamation point but other 16005 10:05:32,430 --> 10:05:35,960 [Music] 16006 10:05:33,640 --> 10:05:37,596 thoughts yeah I think it's going to do 16007 10:05:35,960 --> 10:05:39,800 the latter it's going to print high in 16008 10:05:37,596 --> 10:05:42,080 the usual way because honestly line five 16009 10:05:39,800 --> 10:05:44,200 is this rather line six is the same as 16010 10:05:42,080 --> 10:05:45,360 like week one stuff except we took off 16011 10:05:44,200 --> 10:05:47,436 the training wheel of string and we're 16012 10:05:45,360 --> 10:05:50,200 calling it Char star but I think line 16013 10:05:47,436 --> 10:05:52,560 seven is indeed going to print out I and 16014 10:05:50,200 --> 10:05:54,116 line eight is just going to print out 16015 10:05:52,560 --> 10:05:56,320 because it'll be just the exclamation 16016 10:05:54,116 --> 10:05:59,116 point prti will still be smart enough to 16017 10:05:56,320 --> 10:06:01,040 know where each of those substrings a 16018 10:05:59,116 --> 10:06:02,480 portions of the string End by the same 16019 10:06:01,040 --> 10:06:05,480 logic as always but let me go ahead and 16020 10:06:02,480 --> 10:06:09,756 zoom out run make addresses enter 16021 10:06:05,480 --> 10:06:11,840 compiles okay/ addresses and now indeed 16022 10:06:09,756 --> 10:06:13,840 this is all a string is it's a sequence 16023 10:06:11,840 --> 10:06:16,480 of characters identified by its first 16024 10:06:13,840 --> 10:06:18,680 bite if you then start poking around and 16025 10:06:16,480 --> 10:06:20,640 tell printf to print at what's at the 16026 10:06:18,680 --> 10:06:22,360 next bite or the next next bite it's 16027 10:06:20,640 --> 10:06:24,200 going to do its same thing printing out 16028 10:06:22,360 --> 10:06:26,916 that character and everything after it 16029 10:06:24,200 --> 10:06:28,596 up until that null character so again 16030 10:06:26,916 --> 10:06:30,360 even though there's like a lot going on 16031 10:06:28,596 --> 10:06:32,040 we've introduced these two new operators 16032 10:06:30,360 --> 10:06:33,480 like there's nothing that's happening 16033 10:06:32,040 --> 10:06:35,916 today that hasn't been happening for 16034 10:06:33,480 --> 10:06:37,320 weeks but hopefully through this week uh 16035 10:06:35,916 --> 10:06:39,040 this week's lecture this week's problem 16036 10:06:37,320 --> 10:06:40,840 set and Beyond you'll start to realize 16037 10:06:39,040 --> 10:06:42,960 that now you just have more tools via 16038 10:06:40,840 --> 10:06:46,240 which to harness those lower level 16039 10:06:42,960 --> 10:06:48,480 implementation details so last week too 16040 10:06:46,240 --> 10:06:51,000 recall one other implementation detail I 16041 10:06:48,480 --> 10:06:53,200 claimed that you could not compare two 16042 10:06:51,000 --> 10:06:57,720 strings quite as easily as you could 16043 10:06:53,200 --> 10:06:59,360 compare to uh integers for instance and 16044 10:06:57,720 --> 10:07:00,960 I told you to use a different function 16045 10:06:59,360 --> 10:07:03,596 instead that you probably used one or 16046 10:07:00,960 --> 10:07:06,320 more times with the past problem set how 16047 10:07:03,596 --> 10:07:08,560 are you supposed to compare strings 16048 10:07:06,320 --> 10:07:10,040 apparently yeah so string compare stir 16049 10:07:08,560 --> 10:07:12,040 comp that additional function that we 16050 10:07:10,040 --> 10:07:14,116 said H you just have to use it for now 16051 10:07:12,040 --> 10:07:16,080 but you might have a little intuition 16052 10:07:14,116 --> 10:07:17,520 already as to like why we have to use 16053 10:07:16,080 --> 10:07:20,080 Stir compare and we can't just use 16054 10:07:17,520 --> 10:07:22,400 equals equals to compare strings like 16055 10:07:20,080 --> 10:07:24,720 any intuition for this already why was 16056 10:07:22,400 --> 10:07:27,436 Stir compare necessary last week 16057 10:07:24,720 --> 10:07:29,040 equ perfect equals equals would compare 16058 10:07:27,436 --> 10:07:31,320 literally the two memory addresses 16059 10:07:29,040 --> 10:07:33,436 instead of the actual strings character 16060 10:07:31,320 --> 10:07:35,320 by character and unless the memory 16061 10:07:33,436 --> 10:07:37,400 addresses are literally the same so you 16062 10:07:35,320 --> 10:07:39,640 compare the that exact same memory 16063 10:07:37,400 --> 10:07:41,040 address two different strings probably 16064 10:07:39,640 --> 10:07:43,000 are not going to be considered equal 16065 10:07:41,040 --> 10:07:44,960 even if to us humans they indeed look 16066 10:07:43,000 --> 10:07:47,520 equal so let's see this let me go ahead 16067 10:07:44,960 --> 10:07:49,560 and close addresses. C and actually 16068 10:07:47,520 --> 10:07:51,960 before I do one last mention one of the 16069 10:07:49,560 --> 10:07:54,720 powerful things about Pointer arithmetic 16070 10:07:51,960 --> 10:07:57,680 as an aside is that c and really the 16071 10:07:54,720 --> 10:08:00,200 compiler is smart enough to know how 16072 10:07:57,680 --> 10:08:01,520 many bytes to keep adding and adding and 16073 10:08:00,200 --> 10:08:03,116 by that I mean this right now we got 16074 10:08:01,520 --> 10:08:04,560 lucky because a string is a sequence of 16075 10:08:03,116 --> 10:08:06,520 characters and by definition every 16076 10:08:04,560 --> 10:08:08,560 character is is a single bite you can 16077 10:08:06,520 --> 10:08:11,200 poke around and do s+ one to get the 16078 10:08:08,560 --> 10:08:13,320 next bite S Plus 2 to get the uh the 16079 10:08:11,200 --> 10:08:14,960 third bite however if we weren't dealing 16080 10:08:13,320 --> 10:08:17,520 with strings suppose we were dealing 16081 10:08:14,960 --> 10:08:19,436 with integers that were in an array back 16082 10:08:17,520 --> 10:08:22,520 to back to back if you wanted to get at 16083 10:08:19,436 --> 10:08:24,596 the next integer you could still do plus 16084 10:08:22,520 --> 10:08:26,160 one or plus two to get at the next or 16085 10:08:24,596 --> 10:08:28,040 the next next integer you would not 16086 10:08:26,160 --> 10:08:30,480 start to get into the weeds of doing 16087 10:08:28,040 --> 10:08:32,276 plus four and then plus eight you don't 16088 10:08:30,480 --> 10:08:34,040 have to know or care how big the data 16089 10:08:32,276 --> 10:08:35,680 types are in the computer C and the 16090 10:08:34,040 --> 10:08:37,596 compiler will figure that out for you 16091 10:08:35,680 --> 10:08:39,276 based on the data type in question so 16092 10:08:37,596 --> 10:08:41,800 keep that in mind if ever doing this on 16093 10:08:39,276 --> 10:08:43,160 a different data type uh than chars all 16094 10:08:41,800 --> 10:08:46,040 right so let me go ahead and open up a 16095 10:08:43,160 --> 10:08:47,596 file that I wrote in advance most of and 16096 10:08:46,040 --> 10:08:49,436 let me hide my terminal window and show 16097 10:08:47,596 --> 10:08:51,320 you this so here is a program called 16098 10:08:49,436 --> 10:08:53,916 compare. C whose purpose in life is to 16099 10:08:51,320 --> 10:08:55,596 compare two strings I'm back to using 16100 10:08:53,916 --> 10:08:57,596 the cs50 library because at least for 16101 10:08:55,596 --> 10:08:59,680 now in probably a couple more weeks it 16102 10:08:57,596 --> 10:09:02,080 is so much easier to get input from the 16103 10:08:59,680 --> 10:09:03,840 user using cs50's function get in but 16104 10:09:02,080 --> 10:09:05,880 we'll conclude today by taking off those 16105 10:09:03,840 --> 10:09:07,520 training wheels as well so you can see 16106 10:09:05,880 --> 10:09:10,200 how you can actually get user input with 16107 10:09:07,520 --> 10:09:12,916 nothing cs50 specific so line six and 16108 10:09:10,200 --> 10:09:15,200 seven pretty boring week one stuff get 16109 10:09:12,916 --> 10:09:16,880 an INT called I get an INT called J and 16110 10:09:15,200 --> 10:09:19,720 store them in two variables I and J 16111 10:09:16,880 --> 10:09:21,720 respectively if I equals equals J print 16112 10:09:19,720 --> 10:09:23,680 out the same else print out that they're 16113 10:09:21,720 --> 10:09:25,200 different let me just stipulate for time 16114 10:09:23,680 --> 10:09:26,880 sake I'm pretty sure this code is 16115 10:09:25,200 --> 10:09:28,800 correct this will get two integers from 16116 10:09:26,880 --> 10:09:30,960 the human it will compare them and tell 16117 10:09:28,800 --> 10:09:33,080 me correctly if they're the same or 16118 10:09:30,960 --> 10:09:36,116 different and I'll prove as much by 16119 10:09:33,080 --> 10:09:38,880 running make compare 16120 10:09:36,116 --> 10:09:41,400 slash compare and I'll type in 50 for I 16121 10:09:38,880 --> 10:09:45,040 50 for J and they're the same and now 16122 10:09:41,400 --> 10:09:46,240 I'll do how about 50 and say 13 and 16123 10:09:45,040 --> 10:09:48,080 those are different so let me just 16124 10:09:46,240 --> 10:09:49,916 stipulate this code is indeed correct 16125 10:09:48,080 --> 10:09:52,400 would have worked in week one also works 16126 10:09:49,916 --> 10:09:55,116 now in week four but let me now change 16127 10:09:52,400 --> 10:09:57,916 it to compare not two integers but as I 16128 10:09:55,116 --> 10:10:00,200 hinted maybe two strings instead so let 16129 10:09:57,916 --> 10:10:03,880 me go ahead and change this line of code 16130 10:10:00,200 --> 10:10:06,800 to maybe be string s equals get string 16131 10:10:03,880 --> 10:10:08,560 uh asking the user for s s uh then let's 16132 10:10:06,800 --> 10:10:10,360 change this second line here to be 16133 10:10:08,560 --> 10:10:13,040 string T just to keep the variable name 16134 10:10:10,360 --> 10:10:14,960 short for now and T is a good uh Choice 16135 10:10:13,040 --> 10:10:17,840 after s for something like this get 16136 10:10:14,960 --> 10:10:20,200 string prompt the human for T and then 16137 10:10:17,840 --> 10:10:22,680 let's change our I and J here to do the 16138 10:10:20,200 --> 10:10:25,200 wrong thing per the intuition earlier if 16139 10:10:22,680 --> 10:10:26,916 s equals equals T then print out the 16140 10:10:25,200 --> 10:10:28,840 same else print out that they're 16141 10:10:26,916 --> 10:10:30,116 different now if I want I could take off 16142 10:10:28,840 --> 10:10:31,916 at least some of the training wheels I 16143 10:10:30,116 --> 10:10:34,116 could change this to char star I could 16144 10:10:31,916 --> 10:10:35,596 change this to char star either is fine 16145 10:10:34,116 --> 10:10:36,880 I still need the Cs library though 16146 10:10:35,596 --> 10:10:38,680 because I'm using get string because 16147 10:10:36,880 --> 10:10:40,480 it's actually hard as we'll see today to 16148 10:10:38,680 --> 10:10:42,480 get strings manually without using a 16149 10:10:40,480 --> 10:10:44,800 library but I'll keep it using string 16150 10:10:42,480 --> 10:10:46,200 just for now with the library all right 16151 10:10:44,800 --> 10:10:48,720 make compare 16152 10:10:46,200 --> 10:10:51,116 again compare and now let me go ahead 16153 10:10:48,720 --> 10:10:53,756 and type in for instance Hi exclamation 16154 10:10:51,116 --> 10:10:57,080 point Enter and Hi exclamation point 16155 10:10:53,756 --> 10:10:58,756 Enter and they're different all they're 16156 10:10:57,080 --> 10:11:00,400 obviously not visually but they are 16157 10:10:58,756 --> 10:11:02,560 underneath the hood and you probably do 16158 10:11:00,400 --> 10:11:04,240 have the intuition for this already 16159 10:11:02,560 --> 10:11:05,840 whereby what's going on underneath the 16160 10:11:04,240 --> 10:11:08,116 hood is that we're comparing 16161 10:11:05,840 --> 10:11:10,116 accidentally the two memory addresses so 16162 10:11:08,116 --> 10:11:11,720 in fact let's go there let's consider 16163 10:11:10,116 --> 10:11:13,560 the memory and let me zoom out now so I 16164 10:11:11,720 --> 10:11:14,960 can just have more btes to play with so 16165 10:11:13,560 --> 10:11:17,040 the squares are a little smaller than 16166 10:11:14,960 --> 10:11:19,880 before just so we can fit more in them 16167 10:11:17,040 --> 10:11:23,160 and let me propose that when I declare s 16168 10:11:19,880 --> 10:11:24,720 on what was line six a moment ago it 16169 10:11:23,160 --> 10:11:26,080 ends up somewhere in memory like the top 16170 10:11:24,720 --> 10:11:29,436 leftand corner of my picture for 16171 10:11:26,080 --> 10:11:32,116 discussion sake and when I uh execute 16172 10:11:29,436 --> 10:11:34,400 that same line of code and get string is 16173 10:11:32,116 --> 10:11:36,520 called and I type in high exclamation 16174 10:11:34,400 --> 10:11:37,960 point we know from week one that get 16175 10:11:36,520 --> 10:11:39,680 string puts it somewhere in the 16176 10:11:37,960 --> 10:11:41,276 computer's memory and I'll propose that 16177 10:11:39,680 --> 10:11:43,560 it's in like the bottom left hand corner 16178 10:11:41,276 --> 10:11:45,080 of the screen here what happens after 16179 10:11:43,560 --> 10:11:47,756 that well I know even though I don't 16180 10:11:45,080 --> 10:11:49,276 generally care that Hi exclamation point 16181 10:11:47,756 --> 10:11:53,320 in the null character exist at some 16182 10:11:49,276 --> 10:11:56,000 address like ox123 12 4 125 126 for 16183 10:11:53,320 --> 10:11:59,436 discussion sake and what's in s same as 16184 10:11:56,000 --> 10:12:02,200 before break 0x1 23 so that's all that's 16185 10:11:59,436 --> 10:12:03,560 happening again on line six which is 16186 10:12:02,200 --> 10:12:06,360 pretty much the same as when we were 16187 10:12:03,560 --> 10:12:08,480 getting an S earlier but notice now with 16188 10:12:06,360 --> 10:12:11,200 line seven when I get a second variable 16189 10:12:08,480 --> 10:12:13,240 called T and I call get string again and 16190 10:12:11,200 --> 10:12:15,320 by coincidence as the human I type the 16191 10:12:13,240 --> 10:12:17,276 same thing well what happens here T gets 16192 10:12:15,320 --> 10:12:20,080 its own chunk of memory maybe at the top 16193 10:12:17,276 --> 10:12:21,916 right uh that second version of high 16194 10:12:20,080 --> 10:12:23,480 gets somewhere else in memory you know 16195 10:12:21,916 --> 10:12:25,320 the computer could be smart and notice 16196 10:12:23,480 --> 10:12:26,800 it's the same but C doesn't generally do 16197 10:12:25,320 --> 10:12:28,800 that for you it just plops it somewhere 16198 10:12:26,800 --> 10:12:33,320 else in memory and maybe it's at address 16199 10:12:28,800 --> 10:12:34,436 Ox 456 457 458 459 or wherever but you 16200 10:12:33,320 --> 10:12:36,320 can perhaps see where this is going 16201 10:12:34,436 --> 10:12:38,640 already T now of course contains the 16202 10:12:36,320 --> 10:12:42,240 address of that first bite and so in my 16203 10:12:38,640 --> 10:12:45,276 code on line nine when I compare s and t 16204 10:12:42,240 --> 10:12:48,080 for equality suffice it to say they are 16205 10:12:45,276 --> 10:12:49,840 not equal because of the way the uh 16206 10:12:48,080 --> 10:12:51,840 strings are laid out in the computer's 16207 10:12:49,840 --> 10:12:53,596 memory it's indeed looks the same the 16208 10:12:51,840 --> 10:12:55,680 same values are there but if we abstract 16209 10:12:53,596 --> 10:12:59,756 away further you can really see that s 16210 10:12:55,680 --> 10:13:01,596 and t are not the same themselves and so 16211 10:12:59,756 --> 10:13:03,596 how did we fix this or really how did we 16212 10:13:01,596 --> 10:13:05,040 avoid this last week without spilling 16213 10:13:03,596 --> 10:13:06,916 the beans and going down this Rabbit 16214 10:13:05,040 --> 10:13:08,800 Hole explaining like why you have to use 16215 10:13:06,916 --> 10:13:11,720 Stir compare well if I go back to my 16216 10:13:08,800 --> 10:13:14,360 code here let's do it now the right way 16217 10:13:11,720 --> 10:13:16,960 let me go ahead and include uh a line of 16218 10:13:14,360 --> 10:13:20,840 code that says string compare of s comma 16219 10:13:16,960 --> 10:13:22,800 T both as inputs and then if you recall 16220 10:13:20,840 --> 10:13:24,916 what does stir compare return when two 16221 10:13:22,800 --> 10:13:27,960 strings are equal there's three possible 16222 10:13:24,916 --> 10:13:29,916 return values so zero so one is for if 16223 10:13:27,960 --> 10:13:32,160 it comes alphabetically or atically 16224 10:13:29,916 --> 10:13:34,360 first or second but for now I just want 16225 10:13:32,160 --> 10:13:35,520 zero if I want to use S compare I do 16226 10:13:34,360 --> 10:13:38,240 need 16227 10:13:35,520 --> 10:13:40,360 string.h so string.h does exist that's 16228 10:13:38,240 --> 10:13:42,840 not a cs50 thing there's no keyword 16229 10:13:40,360 --> 10:13:45,320 string as a data type that's a cs50 16230 10:13:42,840 --> 10:13:47,720 thing but string.h does exist so I think 16231 10:13:45,320 --> 10:13:51,200 now with that change on line 10 if I do 16232 10:13:47,720 --> 10:13:53,880 make compare and slash compare and then 16233 10:13:51,200 --> 10:13:56,596 run again uh type again High exclamation 16234 10:13:53,880 --> 10:13:59,320 point High exclamation point I think now 16235 10:13:56,596 --> 10:14:01,880 they're the same and just as a uh second 16236 10:13:59,320 --> 10:14:04,436 check high in L caps maybe high in 16237 10:14:01,880 --> 10:14:06,756 lowercase those are in fact different 16238 10:14:04,436 --> 10:14:09,436 why well stir compare which was written 16239 10:14:06,756 --> 10:14:11,800 by some other human decades ago is just 16240 10:14:09,436 --> 10:14:15,160 smart enough to know that it should go 16241 10:14:11,800 --> 10:14:17,596 to S and go to T start comparing them 16242 10:14:15,160 --> 10:14:20,116 left to right stopping once it hits one 16243 10:14:17,596 --> 10:14:23,040 or both null characters and return zero 16244 10:14:20,116 --> 10:14:27,080 only if everything in s and and T are 16245 10:14:23,040 --> 10:14:28,480 exactly the same are any questions then 16246 10:14:27,080 --> 10:14:30,960 on this 16247 10:14:28,480 --> 10:14:34,436 here any questions on why we're using 16248 10:14:30,960 --> 10:14:36,596 stir compare all right if no yeah oh in 16249 10:14:34,436 --> 10:14:36,596 the 16250 10:14:38,840 --> 10:14:43,400 middle yes so so why does why is it not 16251 10:14:41,756 --> 10:14:45,200 the case with integers so it turns out 16252 10:14:43,400 --> 10:14:48,436 it's not the case with integers with 16253 10:14:45,200 --> 10:14:50,116 floats with bulls with uh doubles with 16254 10:14:48,436 --> 10:14:52,916 Longs like literally every other data 16255 10:14:50,116 --> 10:14:54,680 type works correctly strings though are 16256 10:14:52,916 --> 10:14:56,560 special they are useful enough in 16257 10:14:54,680 --> 10:14:58,080 programming and have been for decades 16258 10:14:56,560 --> 10:14:59,840 that the authors of printf and the 16259 10:14:58,080 --> 10:15:01,960 authors of stir compare and bunches of 16260 10:14:59,840 --> 10:15:04,200 other functions Sterling for that matter 16261 10:15:01,960 --> 10:15:05,756 just kind of treat strings special 16262 10:15:04,200 --> 10:15:07,436 because they're just useful right we 16263 10:15:05,756 --> 10:15:08,960 humans interact using language be it 16264 10:15:07,436 --> 10:15:12,080 English or anything else and so it's 16265 10:15:08,960 --> 10:15:14,520 just useful to have into the language C 16266 10:15:12,080 --> 10:15:17,276 just sort of uh first class support for 16267 10:15:14,520 --> 10:15:19,640 this notion of strings of human text so 16268 10:15:17,276 --> 10:15:21,680 the short answer is just because like it 16269 10:15:19,640 --> 10:15:23,756 just uh is necessar strings are 16270 10:15:21,680 --> 10:15:25,596 different they're implemented with this 16271 10:15:23,756 --> 10:15:28,040 address and the null character 16272 10:15:25,596 --> 10:15:29,680 everything else though is just a value 16273 10:15:28,040 --> 10:15:33,480 but a string again is a white lie it's 16274 10:15:29,680 --> 10:15:36,480 an address it's not a thing unto itself 16275 10:15:33,480 --> 10:15:36,480 good question yeah in front 16276 10:15:37,756 --> 10:15:44,116 oh really good question so in my code 16277 10:15:39,800 --> 10:15:46,436 here in vs code what if I do this 16278 10:15:44,116 --> 10:15:49,240 instead of stir compare and instead of 16279 10:15:46,436 --> 10:15:53,080 if s equals equals T what if I start 16280 10:15:49,240 --> 10:15:54,916 playing around using star s and star T 16281 10:15:53,080 --> 10:15:56,596 really interesting case to consider 16282 10:15:54,916 --> 10:15:58,880 let's go back to our sort of deductive 16283 10:15:56,596 --> 10:16:01,360 logic here so star the asteris operator 16284 10:15:58,880 --> 10:16:04,040 today means go there so when I've typed 16285 10:16:01,360 --> 10:16:06,560 in high once and then high again both 16286 10:16:04,040 --> 10:16:09,436 uppercase for instance what is at the 16287 10:16:06,560 --> 10:16:13,360 address s literally someone 16288 10:16:09,436 --> 10:16:16,116 else what is at the address s yeah uh so 16289 10:16:13,360 --> 10:16:18,840 not quite at the address not so not what 16290 10:16:16,116 --> 10:16:22,560 is the address what is at the address 16291 10:16:18,840 --> 10:16:26,160 ox123 H and what is at the address Ox 16292 10:16:22,560 --> 10:16:28,200 456 H also and so here you're kind of 16293 10:16:26,160 --> 10:16:31,160 cheating like you're comparing the first 16294 10:16:28,200 --> 10:16:32,960 character of both strings but not every 16295 10:16:31,160 --> 10:16:34,880 other one now you could be really 16296 10:16:32,960 --> 10:16:37,200 pedantic and here again this is like a 16297 10:16:34,880 --> 10:16:39,756 good use of uh codee but you could do 16298 10:16:37,200 --> 10:16:41,680 this if that and how about this 16299 10:16:39,756 --> 10:16:46,520 craziness so star 16300 10:16:41,680 --> 10:16:47,916 s+1 equals equals star t + one and and 16301 10:16:46,520 --> 10:16:49,400 you could do this for every character 16302 10:16:47,916 --> 10:16:51,200 manually but that's why stir compare 16303 10:16:49,400 --> 10:16:52,960 exists it does all of this for you but 16304 10:16:51,200 --> 10:16:54,240 that's why and that's the intuition so I 16305 10:16:52,960 --> 10:16:56,640 would encourage you too anytime there's 16306 10:16:54,240 --> 10:16:58,360 something kind of weird going on there's 16307 10:16:56,640 --> 10:16:59,960 I realize we might be um straining 16308 10:16:58,360 --> 10:17:02,520 credibility now we haven't told you that 16309 10:16:59,960 --> 10:17:05,160 many white lies and so most everything 16310 10:17:02,520 --> 10:17:07,040 that we've seen thus far can explain 16311 10:17:05,160 --> 10:17:09,436 pretty much all of the behavior up until 16312 10:17:07,040 --> 10:17:11,560 now from week one onward in C so let me 16313 10:17:09,436 --> 10:17:14,360 revert this back to the right way if s 16314 10:17:11,560 --> 10:17:16,080 compare of S&T equals equals z this now 16315 10:17:14,360 --> 10:17:17,560 is the right version of the code and now 16316 10:17:16,080 --> 10:17:19,800 here is again where you can play so let 16317 10:17:17,560 --> 10:17:22,000 me do this let me um clear my terminal 16318 10:17:19,800 --> 10:17:23,680 window just to tidy things up let me get 16319 10:17:22,000 --> 10:17:25,160 rid of all of this comparison stuff and 16320 10:17:23,680 --> 10:17:27,000 let's just see what's going on as you 16321 10:17:25,160 --> 10:17:28,800 are welcome to in your own code let's 16322 10:17:27,000 --> 10:17:31,320 print out for instance as we might have 16323 10:17:28,800 --> 10:17:33,400 in week one the value of s itself in a 16324 10:17:31,320 --> 10:17:34,880 new line comma s and then let's just 16325 10:17:33,400 --> 10:17:36,320 print out T just to make sure it 16326 10:17:34,880 --> 10:17:37,960 compiles and I'm not doing anything 16327 10:17:36,320 --> 10:17:40,520 wrong but this is not going to be that 16328 10:17:37,960 --> 10:17:42,360 interesting and frankly I don't need 16329 10:17:40,520 --> 10:17:45,800 string.h anymore because I'm not using 16330 10:17:42,360 --> 10:17:50,400 stir compare so make addresses addresses 16331 10:17:45,800 --> 10:17:53,520 there's my um oh sorry that's fun okay 16332 10:17:50,400 --> 10:17:55,640 not percent T percent s here too ignore 16333 10:17:53,520 --> 10:17:58,480 that let's do this again make a oh and 16334 10:17:55,640 --> 10:17:59,960 that's the wrong program okay dot SL 16335 10:17:58,480 --> 10:18:02,916 let's do make 16336 10:17:59,960 --> 10:18:04,720 compare compare and let's type in high 16337 10:18:02,916 --> 10:18:06,436 again and high again and and now we just 16338 10:18:04,720 --> 10:18:08,080 see the two strings I'm not comparing 16339 10:18:06,436 --> 10:18:10,720 but now we can kind of play around right 16340 10:18:08,080 --> 10:18:13,160 instead of printing out percent s which 16341 10:18:10,720 --> 10:18:16,720 prints the string how do I print the 16342 10:18:13,160 --> 10:18:19,640 address in S I just need to make a 16343 10:18:16,720 --> 10:18:22,560 slight change if I want to see not 16344 10:18:19,640 --> 10:18:25,560 what's at s but I want to see s the 16345 10:18:22,560 --> 10:18:25,560 address 16346 10:18:26,596 --> 10:18:32,960 yeah perfect so change percent s in both 16347 10:18:29,560 --> 10:18:34,680 places here to percent P so now printf 16348 10:18:32,960 --> 10:18:36,000 will treat it literally as an dress it's 16349 10:18:34,680 --> 10:18:37,480 not going to do any fanciness this with 16350 10:18:36,000 --> 10:18:38,960 a loop from left to right looking for 16351 10:18:37,480 --> 10:18:40,840 the null character it's just going to 16352 10:18:38,960 --> 10:18:43,276 print out S&T so let me clear my 16353 10:18:40,840 --> 10:18:46,560 terminal run make compare whoops let's 16354 10:18:43,276 --> 10:18:49,480 do make comparecompare enter type in 16355 10:18:46,560 --> 10:18:52,360 high type in high again and now you see 16356 10:18:49,480 --> 10:18:53,880 oh so this is interesting it's not quite 16357 10:18:52,360 --> 10:18:56,240 as straightforward as the other values 16358 10:18:53,880 --> 10:18:59,080 which were slight one bite away they're 16359 10:18:56,240 --> 10:19:01,320 almost the same but this one ends in b0 16360 10:18:59,080 --> 10:19:04,436 this one ends in F0 so they're indeed 16361 10:19:01,320 --> 10:19:06,000 separated by some number of bytes not 16362 10:19:04,436 --> 10:19:08,680 just one but a few because these strings 16363 10:19:06,000 --> 10:19:11,200 are indeed longer all right so once 16364 10:19:08,680 --> 10:19:13,720 you've seen this here how can we now 16365 10:19:11,200 --> 10:19:15,520 maybe leverage this to solve other 16366 10:19:13,720 --> 10:19:17,960 problems well let me propose that we do 16367 10:19:15,520 --> 10:19:20,800 this let me uh zoom out here let me 16368 10:19:17,960 --> 10:19:22,276 close compare and let me open up another 16369 10:19:20,800 --> 10:19:23,276 program I wrote part of in advance 16370 10:19:22,276 --> 10:19:27,480 called 16371 10:19:23,276 --> 10:19:31,000 copy.c so copy. C in theory makes a copy 16372 10:19:27,480 --> 10:19:32,596 of a string how on line eight I'm using 16373 10:19:31,000 --> 10:19:35,000 the same thing as before get string 16374 10:19:32,596 --> 10:19:37,040 storing in a string or char star and 16375 10:19:35,000 --> 10:19:39,520 asking the user for it then I'm not 16376 10:19:37,040 --> 10:19:42,800 asking get string again I'm just making 16377 10:19:39,520 --> 10:19:46,000 a copy super simply with line 10 here 16378 10:19:42,800 --> 10:19:47,596 string T equals s now intuitively I 16379 10:19:46,000 --> 10:19:49,160 think that's how I would copy a variable 16380 10:19:47,596 --> 10:19:51,360 right that's how we've copied variables 16381 10:19:49,160 --> 10:19:54,560 every week thus far and see but 16382 10:19:51,360 --> 10:19:56,400 something's going to go wrong in line 12 16383 10:19:54,560 --> 10:19:58,840 in English does someone want to explain 16384 10:19:56,400 --> 10:20:00,960 what you think line 12 does don't worry 16385 10:19:58,840 --> 10:20:02,960 about finding any bugs or mistakes but 16386 10:20:00,960 --> 10:20:04,756 what does line 12 seem to be doing using 16387 10:20:02,960 --> 10:20:06,640 two upper which which is thanks to the 16388 10:20:04,756 --> 10:20:10,240 ctype library which I've included the 16389 10:20:06,640 --> 10:20:12,840 header file for 16390 10:20:10,240 --> 10:20:14,400 yeah yeah right it's kind of like ugly 16391 10:20:12,840 --> 10:20:16,756 syntax but this would seem to be 16392 10:20:14,400 --> 10:20:19,000 capitalizing the first letter of T 16393 10:20:16,756 --> 10:20:20,596 specifically and just changing it so we 16394 10:20:19,000 --> 10:20:22,436 have t bracket Zer here because we want 16395 10:20:20,596 --> 10:20:24,560 to save the change and we're passing to 16396 10:20:22,436 --> 10:20:26,520 two upper the first character here so 16397 10:20:24,560 --> 10:20:29,756 this is how we did uppercase in the past 16398 10:20:26,520 --> 10:20:31,800 and now I print out s and t respectively 16399 10:20:29,756 --> 10:20:34,680 using percent s so this feels like it 16400 10:20:31,800 --> 10:20:37,360 should work I copied s and stored it in 16401 10:20:34,680 --> 10:20:40,640 t on line 10 and then I change T and 16402 10:20:37,360 --> 10:20:42,800 only t on line 12 but you can perhaps if 16403 10:20:40,640 --> 10:20:45,240 you're comfy thus far see where this is 16404 10:20:42,800 --> 10:20:48,560 going if I do make 16405 10:20:45,240 --> 10:20:51,080 copy copy and let me type in lowercase 16406 10:20:48,560 --> 10:20:53,240 Hi exclamation point this time just once 16407 10:20:51,080 --> 10:20:55,960 so I'm going to hit enter and watch what 16408 10:20:53,240 --> 10:20:59,560 we see for the value of s and 16409 10:20:55,960 --> 10:21:03,240 t huh the new value of SN T at the end 16410 10:20:59,560 --> 10:21:04,800 of my program seems to be what it seems 16411 10:21:03,240 --> 10:21:08,276 to be 16412 10:21:04,800 --> 10:21:10,756 the same high is capitalized both times 16413 10:21:08,276 --> 10:21:13,160 so what's the intuition then for this 16414 10:21:10,756 --> 10:21:15,840 why did this just 16415 10:21:13,160 --> 10:21:18,720 happen yeah and 16416 10:21:15,840 --> 10:21:22,116 back yeah I assigned S&T the same memory 16417 10:21:18,720 --> 10:21:24,116 address so it did copy s into T but C 16418 10:21:22,116 --> 10:21:26,116 takes this very literally what is s it's 16419 10:21:24,116 --> 10:21:28,000 an address what is T it's a copy of that 16420 10:21:26,116 --> 10:21:30,040 address if you want to copy the whole 16421 10:21:28,000 --> 10:21:31,800 string like a normal human would expect 16422 10:21:30,040 --> 10:21:34,080 hey you or someone has to do a lot more 16423 10:21:31,800 --> 10:21:35,800 work you have to go to that address copy 16424 10:21:34,080 --> 10:21:37,880 this character this one this one this 16425 10:21:35,800 --> 10:21:39,400 one and copy it to a new location in 16426 10:21:37,880 --> 10:21:41,080 memory that does not happen 16427 10:21:39,400 --> 10:21:42,596 automatically here for you in C it does 16428 10:21:41,080 --> 10:21:43,840 in some other languages those of you 16429 10:21:42,596 --> 10:21:45,640 who've programmed in certain higher 16430 10:21:43,840 --> 10:21:46,680 level languages this just works as you 16431 10:21:45,640 --> 10:21:48,520 would hope and that's one of the 16432 10:21:46,680 --> 10:21:50,520 benefits of python and other languages 16433 10:21:48,520 --> 10:21:52,480 that we'll soon see but for now it 16434 10:21:50,520 --> 10:21:54,680 literally takes at face value what this 16435 10:21:52,480 --> 10:21:56,436 is copy the address into this address 16436 10:21:54,680 --> 10:21:58,840 and I'll make that more clear by getting 16437 10:21:56,436 --> 10:22:00,880 rid of the string keyword which again is 16438 10:21:58,840 --> 10:22:02,680 just a type def this is technically an 16439 10:22:00,880 --> 10:22:04,200 address here this is technically an 16440 10:22:02,680 --> 10:22:06,680 address here so what's being being 16441 10:22:04,200 --> 10:22:08,720 copied is the value of that address not 16442 10:22:06,680 --> 10:22:11,320 all of the characters that might very 16443 10:22:08,720 --> 10:22:13,800 well follow it so I should make one note 16444 10:22:11,320 --> 10:22:15,840 to here I'm going to start getting more 16445 10:22:13,800 --> 10:22:18,200 in the habit of trying to avoid 16446 10:22:15,840 --> 10:22:20,800 segmentation faults because things could 16447 10:22:18,200 --> 10:22:23,880 go wrong here for instance online 12 16448 10:22:20,800 --> 10:22:25,880 previously I was kind of blindly naively 16449 10:22:23,880 --> 10:22:28,520 dangerously assuming that there will be 16450 10:22:25,880 --> 10:22:29,916 at least one character in s or t that 16451 10:22:28,520 --> 10:22:31,720 might not be the case if the user Just 16452 10:22:29,916 --> 10:22:33,680 Hits enter there's no characters to 16453 10:22:31,720 --> 10:22:35,756 uppercase and so this is reckless of me 16454 10:22:33,680 --> 10:22:38,040 and could theoretically create a sigall 16455 10:22:35,756 --> 10:22:39,916 so I should probably start to be smarter 16456 10:22:38,040 --> 10:22:43,200 and say something like this if the 16457 10:22:39,916 --> 10:22:45,596 length of T is greater than zero okay 16458 10:22:43,200 --> 10:22:47,520 now it's safe to actually capitalize the 16459 10:22:45,596 --> 10:22:49,560 first letter and that will decrease the 16460 10:22:47,520 --> 10:22:52,160 probability now of those segmentation 16461 10:22:49,560 --> 10:22:54,116 faults by just not making any 16462 10:22:52,160 --> 10:22:56,000 assumptions about what the human does 16463 10:22:54,116 --> 10:22:58,320 almost always the your programs will 16464 10:22:56,000 --> 10:23:00,400 crash when you've made a mistake yes but 16465 10:22:58,320 --> 10:23:02,116 the user gives you an input that you 16466 10:23:00,400 --> 10:23:04,360 yourself did not 16467 10:23:02,116 --> 10:23:06,000 expect so what does this all look like 16468 10:23:04,360 --> 10:23:08,400 in memory well let's go back to the Big 16469 10:23:06,000 --> 10:23:11,080 Grid this time focusing on the copying 16470 10:23:08,400 --> 10:23:13,560 of values and let's do this here's S as 16471 10:23:11,080 --> 10:23:15,880 in this new program just declared to be 16472 10:23:13,560 --> 10:23:17,680 a Char star uh here is where my 16473 10:23:15,880 --> 10:23:19,240 lowercase High maybe ended up in the 16474 10:23:17,680 --> 10:23:22,400 computer's memory that's probably at 16475 10:23:19,240 --> 10:23:24,160 ox123 12 4 125 whatever something like 16476 10:23:22,400 --> 10:23:27,916 that and that's of course what ends up 16477 10:23:24,160 --> 10:23:30,160 in s as a value when I declare T I do 16478 10:23:27,916 --> 10:23:34,436 get a second variable called T just like 16479 10:23:30,160 --> 10:23:37,680 before but when I copy s into T what 16480 10:23:34,436 --> 10:23:40,000 happens it's really just literally ox1 2 16481 10:23:37,680 --> 10:23:42,040 3 whatever the value of s is is now also 16482 10:23:40,000 --> 10:23:43,596 the value of T and so if we abstract 16483 10:23:42,040 --> 10:23:46,240 this away at a high level get rid of all 16484 10:23:43,596 --> 10:23:48,596 of those extra squares this is what s 16485 10:23:46,240 --> 10:23:50,680 and t now are they're indeed copies but 16486 10:23:48,596 --> 10:23:52,756 copies of each other not copies of the 16487 10:23:50,680 --> 10:23:55,436 underlying characters and so if you 16488 10:23:52,756 --> 10:23:57,240 follow those arrows um and try to print 16489 10:23:55,436 --> 10:23:58,916 them both out after capitalizing one or 16490 10:23:57,240 --> 10:24:01,116 the other you're going to unfortunately 16491 10:23:58,916 --> 10:24:04,680 end up capitalizing not just one of them 16492 10:24:01,116 --> 10:24:07,160 s but both of them s and T because 16493 10:24:04,680 --> 10:24:09,240 literally it's the same address any 16494 10:24:07,160 --> 10:24:12,080 questions then on this 16495 10:24:09,240 --> 10:24:14,640 visualization 16496 10:24:12,080 --> 10:24:18,720 yeah good question is this pass by 16497 10:24:14,640 --> 10:24:21,480 reference we haven't um we have not seen 16498 10:24:18,720 --> 10:24:23,400 in detail an example like that um right 16499 10:24:21,480 --> 10:24:24,560 now you're copying by value but 16500 10:24:23,400 --> 10:24:25,880 references will come into play and 16501 10:24:24,560 --> 10:24:27,560 remind me in a bit if I haven't used 16502 10:24:25,880 --> 10:24:31,360 that term yet but this is just copying 16503 10:24:27,560 --> 10:24:32,680 things by could have ended poorly value 16504 10:24:31,360 --> 10:24:36,360 other 16505 10:24:32,680 --> 10:24:39,200 questions no all right so with this in 16506 10:24:36,360 --> 10:24:41,200 mind like how do we actually copy things 16507 10:24:39,200 --> 10:24:42,880 properly for this we actually need 16508 10:24:41,200 --> 10:24:44,720 another building block so today we give 16509 10:24:42,880 --> 10:24:47,000 you two functions one of which is called 16510 10:24:44,720 --> 10:24:49,000 Malo one of which is called free and 16511 10:24:47,000 --> 10:24:50,720 these are used all of the time by like 16512 10:24:49,000 --> 10:24:52,720 every piece of software you and I use on 16513 10:24:50,720 --> 10:24:54,360 our Macs PCS and phones whether it's 16514 10:24:52,720 --> 10:24:57,320 written in C or some equivalent other 16515 10:24:54,360 --> 10:24:59,840 language Malo is for memory allocation 16516 10:24:57,320 --> 10:25:01,800 it's a function that you can use to ask 16517 10:24:59,840 --> 10:25:03,840 the operating system Mac OS Linux 16518 10:25:01,800 --> 10:25:06,160 Windows anything for some number of of 16519 10:25:03,840 --> 10:25:08,040 bytes one bite 100 bytes a gigabyte of 16520 10:25:06,160 --> 10:25:10,320 memory you can ask malog for however 16521 10:25:08,040 --> 10:25:13,436 much memory you want in advance it will 16522 10:25:10,320 --> 10:25:15,400 return to you the address of the first 16523 10:25:13,436 --> 10:25:18,000 bite of memory that it found free for 16524 10:25:15,400 --> 10:25:20,520 you unlike a string it is not null 16525 10:25:18,000 --> 10:25:22,160 terminated and so the danger with malok 16526 10:25:20,520 --> 10:25:24,680 is that it's on the honor System if you 16527 10:25:22,160 --> 10:25:26,680 ask it for one bite or 10 bytes you the 16528 10:25:24,680 --> 10:25:28,960 programmer in like a variable have to 16529 10:25:26,680 --> 10:25:31,160 remember how many byes you requested one 16530 10:25:28,960 --> 10:25:32,800 or 10 or the like strings do that for 16531 10:25:31,160 --> 10:25:34,240 you not when we're getting now to this 16532 10:25:32,800 --> 10:25:35,756 low level Mal just going to give you 16533 10:25:34,240 --> 10:25:37,680 some memory and it's up to you to manage 16534 10:25:35,756 --> 10:25:38,960 it free does the opposite when you're 16535 10:25:37,680 --> 10:25:40,960 done with some chunk of memory you can 16536 10:25:38,960 --> 10:25:42,596 free it by passing in that same address 16537 10:25:40,960 --> 10:25:44,276 and just hand it back to Mac OS windows 16538 10:25:42,596 --> 10:25:46,040 or Linux and say I'm done with this you 16539 10:25:44,276 --> 10:25:49,320 can let me use this for something else 16540 10:25:46,040 --> 10:25:52,360 later um as an aside if your computer 16541 10:25:49,320 --> 10:25:53,680 has ever like frozen or hung like the 16542 10:25:52,360 --> 10:25:55,640 whole thing maybe just spontaneously 16543 10:25:53,680 --> 10:25:57,320 reboots yet another reason for a bug 16544 10:25:55,640 --> 10:25:59,276 like that might be if you write a 16545 10:25:57,320 --> 10:26:00,800 program with a bug that keeps Mal 16546 10:25:59,276 --> 10:26:02,756 loocking Mal loocking Mal loocking that 16547 10:26:00,800 --> 10:26:04,800 is asking for more and more and more 16548 10:26:02,756 --> 10:26:06,560 memory but you make a mistake and you 16549 10:26:04,800 --> 10:26:08,040 never free it well eventually the 16550 10:26:06,560 --> 10:26:09,880 computer is going to literally run out 16551 10:26:08,040 --> 10:26:12,200 of memory and something's going to go 16552 10:26:09,880 --> 10:26:13,880 wrong and that's often when computers 16553 10:26:12,200 --> 10:26:15,916 freeze like they're just out of memory 16554 10:26:13,880 --> 10:26:17,436 it has the memory there but the program 16555 10:26:15,916 --> 10:26:19,360 was trying to use too much of it 16556 10:26:17,436 --> 10:26:21,116 endlessly so this too will be a mistake 16557 10:26:19,360 --> 10:26:22,916 that some of us will surely make in the 16558 10:26:21,116 --> 10:26:25,320 coming weeks but hopefully you'll now 16559 10:26:22,916 --> 10:26:28,360 see the solution so let me go back to uh 16560 10:26:25,320 --> 10:26:29,800 vs code here and let me propose that we 16561 10:26:28,360 --> 10:26:31,400 do the following I'll hide my terminal 16562 10:26:29,800 --> 10:26:33,960 window for a moment and I'm going to 16563 10:26:31,400 --> 10:26:35,200 introduce another header file up here 16564 10:26:33,960 --> 10:26:36,880 and I promise there's not going to be 16565 10:26:35,200 --> 10:26:41,040 too many more of these but this one is 16566 10:26:36,880 --> 10:26:43,200 called standard li. for standard library 16567 10:26:41,040 --> 10:26:45,200 and in this file are the Declarations 16568 10:26:43,200 --> 10:26:47,160 the prototypes for malok and free and a 16569 10:26:45,200 --> 10:26:50,040 bunch of other stuff as well it lets me 16570 10:26:47,160 --> 10:26:52,720 now manage my own memory so let's focus 16571 10:26:50,040 --> 10:26:54,360 now on line 11 line 11 is where I went 16572 10:26:52,720 --> 10:26:56,200 wrong before because conceptually I want 16573 10:26:54,360 --> 10:26:58,916 to copy the whole string but of course 16574 10:26:56,200 --> 10:27:01,116 I'm only copying modestly the individual 16575 10:26:58,916 --> 10:27:03,276 address so how do I copy the whole darn 16576 10:27:01,116 --> 10:27:05,916 thing well what I need to do is this 16577 10:27:03,276 --> 10:27:08,400 when when I declare T to be the address 16578 10:27:05,916 --> 10:27:10,160 of something in memory why don't I set T 16579 10:27:08,400 --> 10:27:11,916 to be the address of a free chunk of 16580 10:27:10,160 --> 10:27:14,000 memory so let me ask the operating 16581 10:27:11,916 --> 10:27:15,116 system give me this many bytes tell me 16582 10:27:14,000 --> 10:27:17,320 what the address is and I'm going to 16583 10:27:15,116 --> 10:27:19,116 store that in t initially just so I know 16584 10:27:17,320 --> 10:27:20,916 where there's free space for me so how 16585 10:27:19,116 --> 10:27:22,880 do I do that well quite simply I call 16586 10:27:20,916 --> 10:27:25,320 malok and then I pass in the number of 16587 10:27:22,880 --> 10:27:27,560 bytes that I need now for Hi exclamation 16588 10:27:25,320 --> 10:27:29,560 point I think I need three although wait 16589 10:27:27,560 --> 10:27:31,360 no I really need four because of the 16590 10:27:29,560 --> 10:27:33,040 null character but I don't think I 16591 10:27:31,360 --> 10:27:34,400 should be hardcoding numbers like this 16592 10:27:33,040 --> 10:27:37,116 cuz who knows what the human's going to 16593 10:27:34,400 --> 10:27:40,560 type in so I can actually use Sterling 16594 10:27:37,116 --> 10:27:43,000 of s and then plus one this will ask 16595 10:27:40,560 --> 10:27:44,560 malok then for however many btes 16596 10:27:43,000 --> 10:27:46,960 corresponds to the number of characters 16597 10:27:44,560 --> 10:27:48,400 the human typed in plus one for again 16598 10:27:46,960 --> 10:27:50,320 the null character so it's just being 16599 10:27:48,400 --> 10:27:54,040 smart and defensive rather than choosing 16600 10:27:50,320 --> 10:27:56,200 a number myself but now all T is is a 16601 10:27:54,040 --> 10:27:58,640 pointer if you will to some random chunk 16602 10:27:56,200 --> 10:28:00,080 of free space so there's nothing there 16603 10:27:58,640 --> 10:28:01,436 yet or there's you know bits there but 16604 10:28:00,080 --> 10:28:02,800 who knows what value they are they're 16605 10:28:01,436 --> 10:28:05,560 certainly not identical to what the 16606 10:28:02,800 --> 10:28:08,596 human type did in I now have to do this 16607 10:28:05,560 --> 10:28:12,436 so how can I copy one string into the 16608 10:28:08,596 --> 10:28:14,040 other well let me do this instead of uh 16609 10:28:12,436 --> 10:28:18,116 capitalizing something just yet let me 16610 10:28:14,040 --> 10:28:20,840 do this how about four in I gets zero I 16611 10:28:18,116 --> 10:28:23,596 is less than the length of 16612 10:28:20,840 --> 10:28:24,840 s uh and then i++ so I'm going to 16613 10:28:23,596 --> 10:28:27,080 iterate for the whole length of the 16614 10:28:24,840 --> 10:28:29,960 string and in here I'm just going to do 16615 10:28:27,080 --> 10:28:32,840 this the E character in t should be 16616 10:28:29,960 --> 10:28:34,320 identical to the E character in s so I'm 16617 10:28:32,840 --> 10:28:37,240 just literally copying from right to 16618 10:28:34,320 --> 10:28:38,880 left each and every character in s and I 16619 10:28:37,240 --> 10:28:40,880 can trust that there's enough memory in 16620 10:28:38,880 --> 10:28:42,880 t why cuz I asked for that many bytes 16621 10:28:40,880 --> 10:28:45,200 plus one now there's technically a bug 16622 10:28:42,880 --> 10:28:49,200 here I actually should probably do this 16623 10:28:45,200 --> 10:28:51,880 I should do plus one here or if you 16624 10:28:49,200 --> 10:28:53,880 prefer I should do less than or equal to 16625 10:28:51,880 --> 10:28:56,360 the Sterling but I think it's a little 16626 10:28:53,880 --> 10:28:58,916 clear to do the plus one why do I for 16627 10:28:56,360 --> 10:29:02,160 the first time want to go just beyond 16628 10:28:58,916 --> 10:29:04,596 the boundary of s and copy one more 16629 10:29:02,160 --> 10:29:06,200 bite yeah yeah I need the null character 16630 10:29:04,596 --> 10:29:08,116 like I could technically manually add it 16631 10:29:06,200 --> 10:29:09,840 with some additional line of code but I 16632 10:29:08,116 --> 10:29:12,720 might as well just copy it because back 16633 10:29:09,840 --> 10:29:14,480 sl0 is back sl0 so this time and 16634 10:29:12,720 --> 10:29:16,276 probably only this time it's reasonable 16635 10:29:14,480 --> 10:29:18,000 and correct to go just beyond the 16636 10:29:16,276 --> 10:29:19,880 boundary of your string so you copy the 16637 10:29:18,000 --> 10:29:22,800 null terminating character so that the 16638 10:29:19,880 --> 10:29:24,800 computer also knows where T ends and now 16639 10:29:22,800 --> 10:29:28,080 I think what I can do a little more 16640 10:29:24,800 --> 10:29:33,520 safely is this let me go down here and 16641 10:29:28,080 --> 10:29:35,880 say t braet 0 equals 2 upper of t uh two 16642 10:29:33,520 --> 10:29:37,360 upper of T bracket Z so same line of 16643 10:29:35,880 --> 10:29:39,200 code as before if I actually want to be 16644 10:29:37,360 --> 10:29:42,400 really safe I should probably do this so 16645 10:29:39,200 --> 10:29:44,320 if the stir L of T is greater than zero 16646 10:29:42,400 --> 10:29:46,680 so there's at least one bite there okay 16647 10:29:44,320 --> 10:29:48,840 now it's safe to blindly capitalize the 16648 10:29:46,680 --> 10:29:50,880 first character and I think that now 16649 10:29:48,840 --> 10:29:53,320 puts me in better shape so let me try 16650 10:29:50,880 --> 10:29:57,596 this now let me uh open up my terminal 16651 10:29:53,320 --> 10:29:59,756 make uh copy do slash copy I'm going to 16652 10:29:57,596 --> 10:30:02,276 type in Hi exclamation point in all 16653 10:29:59,756 --> 10:30:05,000 lowercase crossing my fingers this time 16654 10:30:02,276 --> 10:30:09,116 and now if I zoom in it indeed 16655 10:30:05,000 --> 10:30:11,080 capitalized only T and not s in this 16656 10:30:09,116 --> 10:30:14,640 case so pictorially let me switch over 16657 10:30:11,080 --> 10:30:17,400 here here is as before the variable s 16658 10:30:14,640 --> 10:30:20,400 pointing at high in all lowercase when I 16659 10:30:17,400 --> 10:30:21,720 call malok though that gives me a chunk 16660 10:30:20,400 --> 10:30:24,560 of memory that I'm going to store the 16661 10:30:21,720 --> 10:30:27,000 address in t of so if T is some other 16662 10:30:24,560 --> 10:30:28,640 variable as it is in my code and there's 16663 10:30:27,000 --> 10:30:29,916 some other available chunk of memory I 16664 10:30:28,640 --> 10:30:33,756 don't know where it is but let's assume 16665 10:30:29,916 --> 10:30:36,320 as always it's at Ox 456 4557 4558 N9 so 16666 10:30:33,756 --> 10:30:38,880 four bytes total what is now happening 16667 10:30:36,320 --> 10:30:40,320 well T is defined as pointing to that 16668 10:30:38,880 --> 10:30:42,480 because that's what Malo gives us the 16669 10:30:40,320 --> 10:30:45,200 address of the first bite of the free 16670 10:30:42,480 --> 10:30:47,400 memory and now with my for Loop I'm just 16671 10:30:45,200 --> 10:30:49,360 iterating over it copying the H then the 16672 10:30:47,400 --> 10:30:52,960 I then the exclamation point and then 16673 10:30:49,360 --> 10:30:57,320 for good measure the back slash Zer 16674 10:30:52,960 --> 10:31:00,596 instead questions then on this process 16675 10:30:57,320 --> 10:31:03,880 here a really good question um if I 16676 10:31:00,596 --> 10:31:06,480 omitted in my code the 16677 10:31:03,880 --> 10:31:08,240 uh plus one and I didn't do less than or 16678 10:31:06,480 --> 10:31:10,240 equal to so that I'm copying the fourth 16679 10:31:08,240 --> 10:31:11,880 bite odds are in this program because 16680 10:31:10,240 --> 10:31:14,596 it's so short you wouldn't notice that 16681 10:31:11,880 --> 10:31:18,800 there's an actual error but what could 16682 10:31:14,596 --> 10:31:20,756 happen is When I Call printf On T if 16683 10:31:18,800 --> 10:31:24,400 there's no null bite there it might 16684 10:31:20,756 --> 10:31:25,916 print h i exclamation point some random 16685 10:31:24,400 --> 10:31:27,520 value some random value some random 16686 10:31:25,916 --> 10:31:29,560 value some random value until it gets 16687 10:31:27,520 --> 10:31:32,320 lucky and there happens to be a zero 16688 10:31:29,560 --> 10:31:34,560 bite a null bite by chance for instance 16689 10:31:32,320 --> 10:31:37,040 so if you don't include the back sl0 16690 10:31:34,560 --> 10:31:39,080 some way that's going to happen and I 16691 10:31:37,040 --> 10:31:41,240 say some way I could even do this I 16692 10:31:39,080 --> 10:31:43,400 could technically just copy the length 16693 10:31:41,240 --> 10:31:46,116 of the string s and at the very bottom 16694 10:31:43,400 --> 10:31:49,756 here I could do something like T bracket 16695 10:31:46,116 --> 10:31:53,680 I uh sorry T bracket um 16696 10:31:49,756 --> 10:31:55,240 Sterling of T I could do this but this 16697 10:31:53,680 --> 10:31:57,480 is just not necessary like I could 16698 10:31:55,240 --> 10:31:58,840 manually add it at the end of the string 16699 10:31:57,480 --> 10:32:01,080 but again i' claim that it's just 16700 10:31:58,840 --> 10:32:02,480 simpler to borrow that is copy the one 16701 10:32:01,080 --> 10:32:04,596 that's already in s because it's the 16702 10:32:02,480 --> 10:32:06,916 same thing at the end of the day good 16703 10:32:04,596 --> 10:32:08,440 question other questions on this copying 16704 10:32:06,916 --> 10:32:10,116 correctly 16705 10:32:08,440 --> 10:32:12,276 [Music] 16706 10:32:10,116 --> 10:32:15,240 now all right is there any room for 16707 10:32:12,276 --> 10:32:16,756 improvement here well let me propose a 16708 10:32:15,240 --> 10:32:19,840 slight optimization this is kind of a 16709 10:32:16,756 --> 10:32:22,840 throwback now to week one turns out that 16710 10:32:19,840 --> 10:32:25,240 arguably my line 13 here wherein I have 16711 10:32:22,840 --> 10:32:27,116 this for Loop now that I'm doing things 16712 10:32:25,240 --> 10:32:29,160 in Loops again and again and using a 16713 10:32:27,116 --> 10:32:32,040 function like Sterling this is correct 16714 10:32:29,160 --> 10:32:35,680 it will iterate from zero on up to the 16715 10:32:32,040 --> 10:32:39,360 length of I a length of uh s plus one 16716 10:32:35,680 --> 10:32:41,436 but it's kind of stupid of me to write 16717 10:32:39,360 --> 10:32:43,160 this for Loop in this way why well 16718 10:32:41,436 --> 10:32:45,436 here's my initialization on the left 16719 10:32:43,160 --> 10:32:48,200 here's my condition in the middle and in 16720 10:32:45,436 --> 10:32:51,160 general calling a function inside of 16721 10:32:48,200 --> 10:32:54,000 your condition is probably not very good 16722 10:32:51,160 --> 10:32:56,276 design like why why is it bad for me to 16723 10:32:54,000 --> 10:32:58,200 be calling a function like Sterling in 16724 10:32:56,276 --> 10:33:00,720 this condition in the middle of my for 16725 10:32:58,200 --> 10:33:00,720 Loop 16726 10:33:01,960 --> 10:33:05,840 yeah yeah you're just calling it again 16727 10:33:04,480 --> 10:33:07,560 and again for no reason like the length 16728 10:33:05,840 --> 10:33:08,840 of s never changes so like why are you 16729 10:33:07,560 --> 10:33:11,596 wasting everyone's Time by calling 16730 10:33:08,840 --> 10:33:14,000 Sterling of s again again again again 16731 10:33:11,596 --> 10:33:15,840 just to check this inequality whether I 16732 10:33:14,000 --> 10:33:17,200 is less than that value so it turns out 16733 10:33:15,840 --> 10:33:18,720 if you haven't discovered this already 16734 10:33:17,200 --> 10:33:19,720 there's a slight optimization we can do 16735 10:33:18,720 --> 10:33:22,276 here that has nothing to do 16736 10:33:19,720 --> 10:33:24,436 fundamentally with strings or pointers 16737 10:33:22,276 --> 10:33:26,720 just with better design I can actually 16738 10:33:24,436 --> 10:33:29,360 Define two variables at once I could do 16739 10:33:26,720 --> 10:33:32,436 this let me remove this whole condition 16740 10:33:29,360 --> 10:33:36,320 and let me add a comma after I equals 0 16741 10:33:32,436 --> 10:33:41,116 set n or any variable equal to the stirl 16742 10:33:36,320 --> 10:33:43,320 of s + 1 and then after the semicolon 16743 10:33:41,116 --> 10:33:45,680 just ask the question while I is less 16744 10:33:43,320 --> 10:33:47,756 than n so it's almost the same but 16745 10:33:45,680 --> 10:33:49,596 notice now my condition in the very 16746 10:33:47,756 --> 10:33:52,360 middle of this Loop is at least 16747 10:33:49,596 --> 10:33:54,680 comparing two static values n never 16748 10:33:52,360 --> 10:33:56,680 change sorry one static value n never 16749 10:33:54,680 --> 10:33:58,480 changes all that changes is I but I'm 16750 10:33:56,680 --> 10:34:00,560 not foolishly calling sterling sterling 16751 10:33:58,480 --> 10:34:02,680 sterling again and again why well how 16752 10:34:00,560 --> 10:34:05,720 does Sterling work similar in spirit to 16753 10:34:02,680 --> 10:34:07,840 PR print F Sterling given the name of a 16754 10:34:05,720 --> 10:34:09,596 string looks at the first character and 16755 10:34:07,840 --> 10:34:11,480 then starts looking through the entire 16756 10:34:09,596 --> 10:34:13,800 string looking for the null character 16757 10:34:11,480 --> 10:34:15,360 and we saw this in week two counting up 16758 10:34:13,800 --> 10:34:17,170 how many characters are there so it's 16759 10:34:15,360 --> 10:34:20,239 just a waste of time again and 16760 10:34:17,170 --> 10:34:20,239 [Music] 16761 10:34:22,840 --> 10:34:27,040 again totally if you wanted to use n 16762 10:34:25,400 --> 10:34:28,800 multiple times you could absolutely take 16763 10:34:27,040 --> 10:34:31,276 it out of the for Loop put it right 16764 10:34:28,800 --> 10:34:33,200 after s is defined and reuse n and again 16765 10:34:31,276 --> 10:34:34,960 and again absolutely but in General 16766 10:34:33,200 --> 10:34:37,040 consider this when designing your for 16767 10:34:34,960 --> 10:34:38,916 Loops even though modern compilers like 16768 10:34:37,040 --> 10:34:41,160 cang can actually fix this problem this 16769 10:34:38,916 --> 10:34:43,320 inefficiency for you good practice would 16770 10:34:41,160 --> 10:34:45,436 be don't call functions unnecessarily 16771 10:34:43,320 --> 10:34:48,640 especially if the answer is always going 16772 10:34:45,436 --> 10:34:51,520 to be the same all right so what else 16773 10:34:48,640 --> 10:34:53,640 should I perhaps refine here well how 16774 10:34:51,520 --> 10:34:56,360 about I do one last thing and just 16775 10:34:53,640 --> 10:34:58,880 comment on what exactly could go wrong 16776 10:34:56,360 --> 10:35:00,400 here well a couple of things well 16777 10:34:58,880 --> 10:35:02,276 actually this is just silly too like 16778 10:35:00,400 --> 10:35:04,680 surely someone before me in the world 16779 10:35:02,276 --> 10:35:07,040 has had to copy a string before surely 16780 10:35:04,680 --> 10:35:08,960 there's a function like called stir copy 16781 10:35:07,040 --> 10:35:10,800 maybe like stir compare like stir Ling 16782 10:35:08,960 --> 10:35:12,200 and indeed there is so let me propose 16783 10:35:10,800 --> 10:35:14,000 that we actually get rid of this whole 16784 10:35:12,200 --> 10:35:17,000 for Loop and we actually just call a 16785 10:35:14,000 --> 10:35:19,960 function called stir copy no o just St 16786 10:35:17,000 --> 10:35:22,436 cpy and pass in the destination which is 16787 10:35:19,960 --> 10:35:24,596 T first and then the source that you 16788 10:35:22,436 --> 10:35:27,080 want to copy into the destination and 16789 10:35:24,596 --> 10:35:29,200 that takes the place entirely of that 16790 10:35:27,080 --> 10:35:30,640 whole Loop so again I demonstrated the 16791 10:35:29,200 --> 10:35:32,240 loop first just to be very pedantic 16792 10:35:30,640 --> 10:35:33,520 about it but that's wasting time you're 16793 10:35:32,240 --> 10:35:36,040 wasting time writing lines of code you 16794 10:35:33,520 --> 10:35:38,960 don't need to stir copy is what you can 16795 10:35:36,040 --> 10:35:41,000 use here instead and so this has now 16796 10:35:38,960 --> 10:35:43,640 always existed and what more can I do 16797 10:35:41,000 --> 10:35:45,116 well as one final point it turns out 16798 10:35:43,640 --> 10:35:47,520 that there's actually things that can go 16799 10:35:45,116 --> 10:35:49,240 wrong in this code even besides the 16800 10:35:47,520 --> 10:35:50,560 string being too short like if the human 16801 10:35:49,240 --> 10:35:51,840 Just Hits enter and there are no 16802 10:35:50,560 --> 10:35:53,116 characters I don't want to blindly 16803 10:35:51,840 --> 10:35:55,320 capitalize the first character that 16804 10:35:53,116 --> 10:35:57,040 doesn't exist that's why I added that if 16805 10:35:55,320 --> 10:35:58,800 condition but there's other things that 16806 10:35:57,040 --> 10:36:01,560 can go wrong and we introduced those to 16807 10:35:58,800 --> 10:36:05,800 you today it turns out that functions 16808 10:36:01,560 --> 10:36:08,520 like get string and functions like Malo 16809 10:36:05,800 --> 10:36:10,436 return potentially a special value and 16810 10:36:08,520 --> 10:36:13,040 wonderfully confusingly it's also called 16811 10:36:10,436 --> 10:36:14,320 null but with two L's all right so left 16812 10:36:13,040 --> 10:36:18,040 hand and right hand weren't talking so 16813 10:36:14,320 --> 10:36:20,520 well like decades ago NL is a back SL 16814 10:36:18,040 --> 10:36:22,480 zero it's a single character as it 16815 10:36:20,520 --> 10:36:23,560 always has been for a couple of weeks 16816 10:36:22,480 --> 10:36:26,276 now 16817 10:36:23,560 --> 10:36:28,960 n is technically a pointer it's an 16818 10:36:26,276 --> 10:36:31,436 address but it's address zero it's like 16819 10:36:28,960 --> 10:36:33,276 the top left hand corner if you will of 16820 10:36:31,436 --> 10:36:35,480 your computer's memory that just nothing 16821 10:36:33,276 --> 10:36:39,116 is ever supposed to go in by convention 16822 10:36:35,480 --> 10:36:40,840 so null is a synonym for zero but it's 16823 10:36:39,116 --> 10:36:43,520 specifically an address now why is this 16824 10:36:40,840 --> 10:36:44,840 useful well suppose that in my code here 16825 10:36:43,520 --> 10:36:45,916 something goes wrong with get string 16826 10:36:44,840 --> 10:36:47,640 suppose you're being a little crazy and 16827 10:36:45,916 --> 10:36:49,116 you type in way too long of a string 16828 10:36:47,640 --> 10:36:50,840 it's not just high but it's like an 16829 10:36:49,116 --> 10:36:52,520 entire essay of text and there's not 16830 10:36:50,840 --> 10:36:55,116 enough memory in the computer how does 16831 10:36:52,520 --> 10:36:56,880 get string signal to the programmer whoa 16832 10:36:55,116 --> 10:36:58,960 like that's way too big of a string I 16833 10:36:56,880 --> 10:37:01,560 can't fit it in memory well we never 16834 10:36:58,960 --> 10:37:04,200 told you this but all of this time it 16835 10:37:01,560 --> 10:37:06,360 turns out that that get string will 16836 10:37:04,200 --> 10:37:09,000 return this special value called null if 16837 10:37:06,360 --> 10:37:10,596 something goes wrong so to be really 16838 10:37:09,000 --> 10:37:14,400 careful now you should do something like 16839 10:37:10,596 --> 10:37:16,640 this if s equals equals literally null 16840 10:37:14,400 --> 10:37:18,640 then you better um exit the program 16841 10:37:16,640 --> 10:37:20,080 entirely and return like one or two or 16842 10:37:18,640 --> 10:37:23,040 three to signify that something went 16843 10:37:20,080 --> 10:37:25,116 wrong don't uh go any further similarly 16844 10:37:23,040 --> 10:37:26,596 with malok it's possible if you ask for 16845 10:37:25,116 --> 10:37:27,680 way too much memory that could fail 16846 10:37:26,596 --> 10:37:29,480 especially if you're asking now for 16847 10:37:27,680 --> 10:37:32,040 double the memory after the human typed 16848 10:37:29,480 --> 10:37:33,596 something in so if T equals equals null 16849 10:37:32,040 --> 10:37:35,680 then you know what but let's also return 16850 10:37:33,596 --> 10:37:37,960 one or some other value to just get out 16851 10:37:35,680 --> 10:37:40,160 before something crashes or freezes on 16852 10:37:37,960 --> 10:37:41,916 the human as well so honestly I tend not 16853 10:37:40,160 --> 10:37:43,240 to do this always in class because the 16854 10:37:41,916 --> 10:37:44,680 code just gets so bloated and 16855 10:37:43,240 --> 10:37:46,680 complicated but you absolutely in 16856 10:37:44,680 --> 10:37:48,680 practice need to start doing this 16857 10:37:46,680 --> 10:37:50,116 otherwise you will be responsible for 16858 10:37:48,680 --> 10:37:52,276 the freezes and the crashes and the 16859 10:37:50,116 --> 10:37:54,520 reboots that users in the real world 16860 10:37:52,276 --> 10:37:55,916 might actually encounter otherwise of 16861 10:37:54,520 --> 10:37:58,276 course if we get to the bottom of this 16862 10:37:55,916 --> 10:38:00,880 program now I should probably return 16863 10:37:58,276 --> 10:38:03,240 zero explicitly or implicitly to just 16864 10:38:00,880 --> 10:38:05,320 signify that everything is is 16865 10:38:03,240 --> 10:38:07,960 successful but there's one other thing I 16866 10:38:05,320 --> 10:38:09,480 haven't done we introduced malok but 16867 10:38:07,960 --> 10:38:12,080 what did I claim also 16868 10:38:09,480 --> 10:38:14,040 existed so free I'm also being a little 16869 10:38:12,080 --> 10:38:15,436 reckless now here I am not practicing 16870 10:38:14,040 --> 10:38:17,436 what I'm preaching I'm asking the 16871 10:38:15,436 --> 10:38:19,080 computer for memory via get string I'm 16872 10:38:17,436 --> 10:38:21,000 asking the computer for more memory my 16873 10:38:19,080 --> 10:38:23,360 via malok and I'm never technically 16874 10:38:21,000 --> 10:38:25,200 handing it back so really what I should 16875 10:38:23,360 --> 10:38:29,360 be doing at the very bottom of my 16876 10:38:25,200 --> 10:38:32,000 program to is freeing the memory I've 16877 10:38:29,360 --> 10:38:34,360 asked for so henceforth it is a rule a 16878 10:38:32,000 --> 10:38:36,640 law if you will in see whenever you 16879 10:38:34,360 --> 10:38:38,880 allocate memory with malok or certain 16880 10:38:36,640 --> 10:38:40,640 other functions as well you the 16881 10:38:38,880 --> 10:38:42,960 programmer must free it when you're all 16882 10:38:40,640 --> 10:38:44,360 done with it now this is a bit of an 16883 10:38:42,960 --> 10:38:46,116 overstatement because technically when 16884 10:38:44,360 --> 10:38:47,240 programs quit they'll free the memory 16885 10:38:46,116 --> 10:38:48,880 automatically so you're not going to 16886 10:38:47,240 --> 10:38:50,320 break someone's Mac or PC because you 16887 10:38:48,880 --> 10:38:51,680 necessarily have this bug but for 16888 10:38:50,320 --> 10:38:53,160 programs that are running all the time 16889 10:38:51,680 --> 10:38:55,360 like someone keeps a chrome their 16890 10:38:53,160 --> 10:38:57,200 browser open Microsoft Word or the like 16891 10:38:55,360 --> 10:38:59,200 bad things will happen if over time you 16892 10:38:57,200 --> 10:39:01,200 never never never call free and the 16893 10:38:59,200 --> 10:39:03,276 program keeps running so always get into 16894 10:39:01,200 --> 10:39:05,680 this habit here you you do not need to 16895 10:39:03,276 --> 10:39:07,596 free memory that comes from get string 16896 10:39:05,680 --> 10:39:09,916 because the cs50 library automatically 16897 10:39:07,596 --> 10:39:12,320 frees it for you but you anytime you use 16898 10:39:09,916 --> 10:39:15,116 malok henceforth as you did or I did 16899 10:39:12,320 --> 10:39:18,160 here you must free that by just passing 16900 10:39:15,116 --> 10:39:22,800 in the same address you got back 16901 10:39:18,160 --> 10:39:27,240 questions now on malok and 16902 10:39:22,800 --> 10:39:27,240 free questions 16903 10:39:31,116 --> 10:39:37,640 yeah oh really good question so free 16904 10:39:34,680 --> 10:39:40,116 just so what does free do so free um 16905 10:39:37,640 --> 10:39:41,640 just uh lets the computer know that you 16906 10:39:40,116 --> 10:39:43,240 are done with that chunk of memory which 16907 10:39:41,640 --> 10:39:45,520 means that if you have a another line of 16908 10:39:43,240 --> 10:39:47,880 code elsewhere that same memory might be 16909 10:39:45,520 --> 10:39:48,720 reused and can be used again and again 16910 10:39:47,880 --> 10:39:50,080 and that's going to be necessary 16911 10:39:48,720 --> 10:39:51,800 certainly for any long running program 16912 10:39:50,080 --> 10:39:53,200 you can't ask for memory constantly 16913 10:39:51,800 --> 10:39:55,160 you'll eventually run out so you need to 16914 10:39:53,200 --> 10:39:56,436 free it in this way other languages as 16915 10:39:55,160 --> 10:39:57,800 an aside python you get another 16916 10:39:56,436 --> 10:39:59,400 motivation in a couple of weeks for it 16917 10:39:57,800 --> 10:40:01,400 is going to be Python and certain other 16918 10:39:59,400 --> 10:40:03,520 languages manage all this headache for 16919 10:40:01,400 --> 10:40:06,880 you but in C the goal here is to really 16920 10:40:03,520 --> 10:40:08,596 harness these capabilities ourselves all 16921 10:40:06,880 --> 10:40:09,756 right so it turns out like almost 16922 10:40:08,596 --> 10:40:10,916 everyone in the room everyone in the 16923 10:40:09,756 --> 10:40:12,276 room myself included you're going to 16924 10:40:10,916 --> 10:40:13,960 screw up when it comes to anything 16925 10:40:12,276 --> 10:40:16,200 memory related if you haven't already 16926 10:40:13,960 --> 10:40:17,880 segals are in your future but hopefully 16927 10:40:16,200 --> 10:40:19,960 there's tools via which you can detect 16928 10:40:17,880 --> 10:40:22,320 these things and fix them proactively 16929 10:40:19,960 --> 10:40:24,360 and not just use print def uh or debug 16930 10:40:22,320 --> 10:40:26,400 50 or rubber duck we actually have 16931 10:40:24,360 --> 10:40:28,276 another tool we can equip you with now 16932 10:40:26,400 --> 10:40:31,116 that help you find some mistakes so let 16933 10:40:28,276 --> 10:40:32,800 me do this let me close copy.c let me 16934 10:40:31,116 --> 10:40:34,960 open a program I wrote In Advance called 16935 10:40:32,800 --> 10:40:36,640 memory. C that doesn't do anything 16936 10:40:34,960 --> 10:40:38,680 really interesting but it's going to 16937 10:40:36,640 --> 10:40:41,320 have two bugs in it notice that I've 16938 10:40:38,680 --> 10:40:43,436 included standard i.h as always I've 16939 10:40:41,320 --> 10:40:45,200 also included standard li. which is 16940 10:40:43,436 --> 10:40:48,880 necessary now for anything related to 16941 10:40:45,200 --> 10:40:50,680 malok Andor free and the like line six 16942 10:40:48,880 --> 10:40:53,240 it's a little weird what I've done here 16943 10:40:50,680 --> 10:40:56,480 but this is like the manual way of 16944 10:40:53,240 --> 10:40:59,040 asking for enough memory for an array in 16945 10:40:56,480 --> 10:41:03,520 week two how do we ask for memory for an 16946 10:40:59,040 --> 10:41:05,560 array you very simply say int X3 and 16947 10:41:03,520 --> 10:41:07,596 that gives you an array called X of size 16948 10:41:05,560 --> 10:41:10,596 three but if you do it manually Now 16949 10:41:07,596 --> 10:41:13,320 using Malo what you have to do is use 16950 10:41:10,596 --> 10:41:15,680 syntax like this you call malok you ask 16951 10:41:13,320 --> 10:41:17,160 for three things times however big an 16952 10:41:15,680 --> 10:41:19,200 INT is now we know it's four so you 16953 10:41:17,160 --> 10:41:21,276 could literally write 12 here but this 16954 10:41:19,200 --> 10:41:23,000 is more generic so three times the size 16955 10:41:21,276 --> 10:41:25,360 of an integer will give you 12 16956 10:41:23,000 --> 10:41:28,480 dynamically and what do Malo return the 16957 10:41:25,360 --> 10:41:30,116 address of the first bite you get back 16958 10:41:28,480 --> 10:41:32,240 what do I want to put that well I want 16959 10:41:30,116 --> 10:41:34,000 to put it in a variable now the variable 16960 10:41:32,240 --> 10:41:36,560 can't just be int X because that's a 16961 10:41:34,000 --> 10:41:38,680 number it's not an address per se if I 16962 10:41:36,560 --> 10:41:40,800 want to store this address in a variable 16963 10:41:38,680 --> 10:41:43,320 I could call it X I could call it P but 16964 10:41:40,800 --> 10:41:45,320 in Star X just means that X is now the 16965 10:41:43,320 --> 10:41:46,640 address of a chunk of memory 16966 10:41:45,320 --> 10:41:49,480 specifically a chunk of memory that's 16967 10:41:46,640 --> 10:41:52,680 big enough not for one but for three ins 16968 10:41:49,480 --> 10:41:55,360 in total all right now I'm just sort of 16969 10:41:52,680 --> 10:41:57,480 naively putting our old friends 72 73 16970 10:41:55,360 --> 10:42:00,040 and 33 at the first second and third 16971 10:41:57,480 --> 10:42:01,680 locations in memory but perhaps based on 16972 10:42:00,040 --> 10:42:03,520 week two or week four I'm clearly 16973 10:42:01,680 --> 10:42:05,160 screwing up up here in a couple of ways 16974 10:42:03,520 --> 10:42:08,276 someone want to identify at least one 16975 10:42:05,160 --> 10:42:10,916 bug what did I do 16976 10:42:08,276 --> 10:42:13,160 wrong yeah like this is now you know 16977 10:42:10,916 --> 10:42:15,200 amateur uh stuff like I should be zero 16978 10:42:13,160 --> 10:42:17,680 indexing not one indexing so this has 16979 10:42:15,200 --> 10:42:21,240 got to be 0 one2 ultimately and other 16980 10:42:17,680 --> 10:42:24,320 bugs that are maybe more week four 16981 10:42:21,240 --> 10:42:26,480 specific other bugs it's more subtle 16982 10:42:24,320 --> 10:42:28,080 yeah I'm not freeing the memory right so 16983 10:42:26,480 --> 10:42:29,680 I'm not practicing what I'm preaching by 16984 10:42:28,080 --> 10:42:31,240 freeing this memory now suppose these 16985 10:42:29,680 --> 10:42:32,480 are non obvious and honestly after like 16986 10:42:31,240 --> 10:42:34,840 an hour or two of this like this 16987 10:42:32,480 --> 10:42:38,720 shouldn't be obvious yet it will be over 16988 10:42:34,840 --> 10:42:40,680 time how could I find these bugs with uh 16989 10:42:38,720 --> 10:42:42,560 software as opposed to just staring at 16990 10:42:40,680 --> 10:42:44,640 the thing or asking someone for help 16991 10:42:42,560 --> 10:42:46,916 well let me propose this let me first go 16992 10:42:44,640 --> 10:42:49,960 ahead and run make memory to compile the 16993 10:42:46,916 --> 10:42:51,880 program and it seems to work look fine 16994 10:42:49,960 --> 10:42:55,080 there's no syntax errors at least 16995 10:42:51,880 --> 10:42:56,320 dotmemory notice seems to work fine too 16996 10:42:55,080 --> 10:42:57,680 now this program doesn't do anything 16997 10:42:56,320 --> 10:42:59,596 interesting there's no printf or 16998 10:42:57,680 --> 10:43:01,596 anything like that but it didn't crash 16999 10:42:59,596 --> 10:43:04,116 there's no segmentation fault but that 17000 10:43:01,596 --> 10:43:06,560 doesn't mean there aren't bugs latent in 17001 10:43:04,116 --> 10:43:08,720 the software and this is true sadly of 17002 10:43:06,560 --> 10:43:11,200 all of today's software like Chrome and 17003 10:43:08,720 --> 10:43:13,160 Microsoft Word and other programs surely 17004 10:43:11,200 --> 10:43:15,116 have memory related bugs that people at 17005 10:43:13,160 --> 10:43:16,680 Google and Microsoft haven't yet found 17006 10:43:15,116 --> 10:43:18,596 but there are tools at least to find the 17007 10:43:16,680 --> 10:43:20,240 most obvious of those bugs and we're 17008 10:43:18,596 --> 10:43:23,040 going to introduce you now to a program 17009 10:43:20,240 --> 10:43:24,840 called valren so valren it's a fairly 17010 10:43:23,040 --> 10:43:27,320 fancy program but we'll use it for very 17011 10:43:24,840 --> 10:43:29,960 simple ways we'll look at your code and 17012 10:43:27,320 --> 10:43:31,480 find memory errors as it's executing and 17013 10:43:29,960 --> 10:43:34,240 try to help you understand where they 17014 10:43:31,480 --> 10:43:36,040 are so let me go back to vs code here 17015 10:43:34,240 --> 10:43:37,116 memory seems to be fine you know I feel 17016 10:43:36,040 --> 10:43:39,160 like okay I'm going to submit this 17017 10:43:37,116 --> 10:43:40,916 homework all is good no error messages 17018 10:43:39,160 --> 10:43:42,720 that's no longer the case now you need 17019 10:43:40,916 --> 10:43:44,240 to poke a little more at your code to 17020 10:43:42,720 --> 10:43:46,320 see if maybe there's still some bug 17021 10:43:44,240 --> 10:43:49,640 there so let me do this 17022 10:43:46,320 --> 10:43:51,436 valren and then space dotmemory so just 17023 10:43:49,640 --> 10:43:53,436 like debug 50 you run it on a program 17024 10:43:51,436 --> 10:43:55,520 you already compiled valren I'm going to 17025 10:43:53,436 --> 10:43:57,320 run it on a program I already compiled 17026 10:43:55,520 --> 10:44:00,240 let me uh zoom in on my terminal window 17027 10:43:57,320 --> 10:44:02,680 so we can see more at once and enter all 17028 10:44:00,240 --> 10:44:04,160 right the output is crazy cryp IC for no 17029 10:44:02,680 --> 10:44:06,436 good reason there's lots of numbers and 17030 10:44:04,160 --> 10:44:08,160 equal signs it's a lot of clutter but 17031 10:44:06,436 --> 10:44:10,916 there is some juicy information here and 17032 10:44:08,160 --> 10:44:13,240 let me start from the top down invalid 17033 10:44:10,916 --> 10:44:15,640 write of size four so write means to 17034 10:44:13,240 --> 10:44:18,240 change a value read means to like access 17035 10:44:15,640 --> 10:44:20,596 a value and this is again esoteric like 17036 10:44:18,240 --> 10:44:24,400 a lot of our error messages are but it 17037 10:44:20,596 --> 10:44:26,276 looks like uh after a block of size 12 17038 10:44:24,400 --> 10:44:28,520 allock and then there's these weird hex 17039 10:44:26,276 --> 10:44:30,200 notation there's some mention of malok 17040 10:44:28,520 --> 10:44:32,756 but honestly the juicy part here is 17041 10:44:30,200 --> 10:44:34,916 memory. C line six that's probably my 17042 10:44:32,756 --> 10:44:37,040 fault so let's look at line six per that 17043 10:44:34,916 --> 10:44:39,960 output let me shrink the terminal window 17044 10:44:37,040 --> 10:44:41,320 look at line six okay 12 is now Germain 17045 10:44:39,960 --> 10:44:43,680 right if you did the mental math of the 17046 10:44:41,320 --> 10:44:48,116 size of an INT times three 12 is somehow 17047 10:44:43,680 --> 10:44:50,640 involved here but line six is now uh 17048 10:44:48,116 --> 10:44:52,520 happening next year that's where the 17049 10:44:50,640 --> 10:44:56,520 memory came from what is this let me 17050 10:44:52,520 --> 10:44:59,916 Zoom back in where is there invalid 17051 10:44:56,520 --> 10:45:03,360 right of size four like what's perhaps 17052 10:44:59,916 --> 10:45:05,276 going wrong here invalid right of size 17053 10:45:03,360 --> 10:45:08,080 four what does that mean it's like a 17054 10:45:05,276 --> 10:45:10,080 very technical way of explaining the bug 17055 10:45:08,080 --> 10:45:11,840 is actually one line later on line seven 17056 10:45:10,080 --> 10:45:14,160 as we already identified 17057 10:45:11,840 --> 10:45:15,840 yeah indeed and I I misspoke a moment 17058 10:45:14,160 --> 10:45:17,520 ago the bug actually arises here with 17059 10:45:15,840 --> 10:45:20,040 line nine so after the allocation of 17060 10:45:17,520 --> 10:45:21,680 memory I'm somehow writing four bytes 17061 10:45:20,040 --> 10:45:23,240 incorrectly and unfortunately the onus 17062 10:45:21,680 --> 10:45:24,916 is kind of on you to sort of think 17063 10:45:23,240 --> 10:45:27,200 through deductively like what could that 17064 10:45:24,916 --> 10:45:28,880 mean but I'm clearly touching four bytes 17065 10:45:27,200 --> 10:45:30,480 of memory in these few lines of code 17066 10:45:28,880 --> 10:45:32,400 that I shouldn't be and hopefully here 17067 10:45:30,480 --> 10:45:34,720 is the light bulb already went off ear 17068 10:45:32,400 --> 10:45:36,880 oh I'm not zero indexing okay that must 17069 10:45:34,720 --> 10:45:39,240 mean that X bracket 3 as you note is 17070 10:45:36,880 --> 10:45:41,880 just too far past the chunk of memory so 17071 10:45:39,240 --> 10:45:43,756 I'm invalidly writing to four bytes that 17072 10:45:41,880 --> 10:45:45,480 I shouldn't be so again it's not super 17073 10:45:43,756 --> 10:45:48,080 obvious this is not super userfriendly 17074 10:45:45,480 --> 10:45:50,360 but at least it does give you a clue as 17075 10:45:48,080 --> 10:45:52,320 to where that bug is so the fix there is 17076 10:45:50,360 --> 10:45:54,960 going to be quite simply to change the 17077 10:45:52,320 --> 10:45:57,000 one to a zero the two to a one and the 17078 10:45:54,960 --> 10:45:58,360 three to a two that'll fix that but 17079 10:45:57,000 --> 10:46:00,520 there's still a second error and let me 17080 10:45:58,360 --> 10:46:02,596 look at the cryptic output again Heap 17081 10:46:00,520 --> 10:46:05,756 summary some stuff there okay this does 17082 10:46:02,596 --> 10:46:07,720 not sound good down here 12 bytes in one 17083 10:46:05,756 --> 10:46:10,520 blocks are definitely lost in Lost 17084 10:46:07,720 --> 10:46:12,320 record one of one very Arcane output two 17085 10:46:10,520 --> 10:46:14,720 but clearly related to line six again 17086 10:46:12,320 --> 10:46:16,276 our allocation of memory now here too 17087 10:46:14,720 --> 10:46:19,276 it's not obvious what the solution is 17088 10:46:16,276 --> 10:46:21,680 but memory is lost AKA this is a memory 17089 10:46:19,276 --> 10:46:24,436 leak and now the deductions kind of up 17090 10:46:21,680 --> 10:46:26,916 to you why what is leak oh wait I didn't 17091 10:46:24,436 --> 10:46:29,276 call free and so the second solution 17092 10:46:26,916 --> 10:46:31,116 here is probably to free x at the very 17093 10:46:29,276 --> 10:46:32,916 end of the program and if you really 17094 10:46:31,116 --> 10:46:35,360 want to be you should probably check 17095 10:46:32,916 --> 10:46:37,756 like I proposed earlier if x is null 17096 10:46:35,360 --> 10:46:39,200 just get out now while you still can and 17097 10:46:37,756 --> 10:46:40,800 don't even touch those other lines of 17098 10:46:39,200 --> 10:46:43,520 code but if you get to the bottom return 17099 10:46:40,800 --> 10:46:45,400 zero but really the takeaways are I 17100 10:46:43,520 --> 10:46:48,400 fixed my zero indexing of the array to 17101 10:46:45,400 --> 10:46:50,040 avoid the invalid right of size four and 17102 10:46:48,400 --> 10:46:52,116 now I'm freeing the memory that I asked 17103 10:46:50,040 --> 10:46:54,560 for so there should be no leak lost all 17104 10:46:52,116 --> 10:46:57,560 right let's try this again make 17105 10:46:54,560 --> 10:46:59,200 memorymemory no visible errors yet but 17106 10:46:57,560 --> 10:47:02,320 let me now increase my terminal window 17107 10:46:59,200 --> 10:47:05,720 again do valren of dotmemory crossing my 17108 10:47:02,320 --> 10:47:07,960 fingers and now all Heap blocks were 17109 10:47:05,720 --> 10:47:09,400 freed no leaks are possible I don't see 17110 10:47:07,960 --> 10:47:10,720 any invalid rights there's still a crazy 17111 10:47:09,400 --> 10:47:13,276 amount of output but none of it is 17112 10:47:10,720 --> 10:47:15,520 erroneous it's not bad now I fixed my 17113 10:47:13,276 --> 10:47:16,880 memory bugs and so now my my ta my TF 17114 10:47:15,520 --> 10:47:18,480 they're not going to find them either 17115 10:47:16,880 --> 10:47:21,840 because at least valr has proactively 17116 10:47:18,480 --> 10:47:24,000 done that for me questions then on 17117 10:47:21,840 --> 10:47:25,360 valgren generally it's those two types 17118 10:47:24,000 --> 10:47:27,840 of Errors you might trip over there's 17119 10:47:25,360 --> 10:47:30,640 not twoo much else in the way of Arcane 17120 10:47:27,840 --> 10:47:33,880 output questions then on 17121 10:47:30,640 --> 10:47:35,596 this no all right well what else might 17122 10:47:33,880 --> 10:47:38,720 be going on so someone alluded to this 17123 10:47:35,596 --> 10:47:40,880 earlier what happens when you for 17124 10:47:38,720 --> 10:47:42,680 instance forget the null Terminator or 17125 10:47:40,880 --> 10:47:45,596 you generally start poking around memory 17126 10:47:42,680 --> 10:47:47,360 that you yourself didn't ask for or uh 17127 10:47:45,596 --> 10:47:49,240 looking at values you didn't put there 17128 10:47:47,360 --> 10:47:51,916 well let me go ahead and open this code 17129 10:47:49,240 --> 10:47:54,000 of garbage. C in honor of Oscar the 17130 10:47:51,916 --> 10:47:56,116 Grouch here of sorts and here is a 17131 10:47:54,000 --> 10:47:57,520 simple program if I hide my terminal 17132 10:47:56,116 --> 10:48:00,480 window that just does something kind of 17133 10:47:57,520 --> 10:48:03,080 arbitrary I first declare an array uh 17134 10:48:00,480 --> 10:48:05,916 called scores uh but I made it crazy big 17135 10:48:03,080 --> 10:48:08,240 like 1024 like that's a lot of uh 17136 10:48:05,916 --> 10:48:10,436 integers but so be it and then I iterate 17137 10:48:08,240 --> 10:48:12,240 over those integers and I print each of 17138 10:48:10,436 --> 10:48:14,680 those scores out so I'm using week two 17139 10:48:12,240 --> 10:48:16,480 syntax here but based on this program 17140 10:48:14,680 --> 10:48:19,240 what have I clearly not done that I did 17141 10:48:16,480 --> 10:48:22,800 do back in week two I've allocated the 17142 10:48:19,240 --> 10:48:25,720 array I'm printing the array but but 17143 10:48:22,800 --> 10:48:27,200 Bute yeah I didn't initialize any values 17144 10:48:25,720 --> 10:48:28,916 for that array back in week two we 17145 10:48:27,200 --> 10:48:30,436 didn't do 1024 we did like three and I 17146 10:48:28,916 --> 10:48:31,880 typed in like three test scores or 17147 10:48:30,436 --> 10:48:33,560 something like that he here I'm 17148 10:48:31,880 --> 10:48:34,520 allocating memory even more than that 17149 10:48:33,560 --> 10:48:36,320 just because I really want to be 17150 10:48:34,520 --> 10:48:38,040 dramatic with what I'm demonstrating but 17151 10:48:36,320 --> 10:48:41,000 I'm not initializing those values to 17152 10:48:38,040 --> 10:48:43,040 anything and so here it turns out in C 17153 10:48:41,000 --> 10:48:44,800 generally if you do not initialize a 17154 10:48:43,040 --> 10:48:46,800 variable or you do not initialize an 17155 10:48:44,800 --> 10:48:48,840 array with explicit values there are 17156 10:48:46,800 --> 10:48:51,000 going to be garbage values there so to 17157 10:48:48,840 --> 10:48:53,680 speak remnants of that memory having 17158 10:48:51,000 --> 10:48:55,560 been used before probably by some other 17159 10:48:53,680 --> 10:48:57,240 function of yours some Library function 17160 10:48:55,560 --> 10:48:59,116 or something else while your program is 17161 10:48:57,240 --> 10:49:01,000 running not a huge deal with a super 17162 10:48:59,116 --> 10:49:03,160 small program like this but for anything 17163 10:49:01,000 --> 10:49:04,720 sizable memory is going to be used and 17164 10:49:03,160 --> 10:49:06,680 unused and used and unused that is 17165 10:49:04,720 --> 10:49:08,480 macked and freed again and again there's 17166 10:49:06,680 --> 10:49:11,040 going to be lots of garbage values in 17167 10:49:08,480 --> 10:49:13,436 the computer's memory by default so if I 17168 10:49:11,040 --> 10:49:16,400 open my terminal window here uh let me 17169 10:49:13,436 --> 10:49:18,560 do make garbage uh let me zoom in on my 17170 10:49:16,400 --> 10:49:21,680 terminal so we can see the output when I 17171 10:49:18,560 --> 10:49:24,400 runt garbage theoretically I should see 17172 10:49:21,680 --> 10:49:26,116 1,24 integers but none of which have 17173 10:49:24,400 --> 10:49:27,756 been initialized now I'm going to get 17174 10:49:26,116 --> 10:49:29,276 lucky with some of them and it looks 17175 10:49:27,756 --> 10:49:31,640 like wow okay a lot of them are 17176 10:49:29,276 --> 10:49:33,320 initialized to zero and C does in some 17177 10:49:31,640 --> 10:49:35,240 context initialize memory for you to 17178 10:49:33,320 --> 10:49:37,640 zero at least at the beginning but not 17179 10:49:35,240 --> 10:49:39,276 again and again typically but if I start 17180 10:49:37,640 --> 10:49:41,160 scrolling backwards in time at this 17181 10:49:39,276 --> 10:49:45,040 array of size 17182 10:49:41,160 --> 10:49:47,080 1024 where did these values come from so 17183 10:49:45,040 --> 10:49:48,880 just random positive and negative 17184 10:49:47,080 --> 10:49:50,320 numbers interspersed among the zeros 17185 10:49:48,880 --> 10:49:53,320 well that's because I'm literally poking 17186 10:49:50,320 --> 10:49:55,480 around a random 1,24 bytes of the 17187 10:49:53,320 --> 10:49:57,000 computer's memory who knows what's there 17188 10:49:55,480 --> 10:49:58,840 so the lesson here is that garbage 17189 10:49:57,000 --> 10:50:01,160 values are indeed this like term of art 17190 10:49:58,840 --> 10:50:03,436 it means that a variable that you might 17191 10:50:01,160 --> 10:50:04,840 have uh defined that you might have 17192 10:50:03,436 --> 10:50:07,200 declared if you don't give it an 17193 10:50:04,840 --> 10:50:08,596 explicit value who knows what's going to 17194 10:50:07,200 --> 10:50:10,400 be there and the lesson here is just 17195 10:50:08,596 --> 10:50:13,000 don't do that always initialize 17196 10:50:10,400 --> 10:50:15,040 variables to something either yourself 17197 10:50:13,000 --> 10:50:18,560 or prompting the human for 17198 10:50:15,040 --> 10:50:20,880 it questions about garbage 17199 10:50:18,560 --> 10:50:22,560 values you'll see them sometimes if you 17200 10:50:20,880 --> 10:50:24,756 print things you shouldn't or touch 17201 10:50:22,560 --> 10:50:26,680 arrays beyond their 17202 10:50:24,756 --> 10:50:28,360 boundaries all right so maybe to make 17203 10:50:26,680 --> 10:50:29,840 this a little visual too it turns out 17204 10:50:28,360 --> 10:50:31,160 that a lot of things can go wrong 17205 10:50:29,840 --> 10:50:32,800 unfortunately with poers and we've seen 17206 10:50:31,160 --> 10:50:34,116 some of them and here's another program 17207 10:50:32,800 --> 10:50:37,240 that's a little contrived it's very 17208 10:50:34,116 --> 10:50:38,840 simple and it just is about manipulating 17209 10:50:37,240 --> 10:50:40,400 values it doesn't do anything useful per 17210 10:50:38,840 --> 10:50:42,720 se except demonstrate some of today's 17211 10:50:40,400 --> 10:50:44,680 Concepts so in main here let me propose 17212 10:50:42,720 --> 10:50:45,840 that we declare a pointer called X 17213 10:50:44,680 --> 10:50:47,880 that's going to store eventually the 17214 10:50:45,840 --> 10:50:49,040 address of an integer apparently here's 17215 10:50:47,880 --> 10:50:51,200 another one called y that's going to 17216 10:50:49,040 --> 10:50:53,840 store the address of an integer as well 17217 10:50:51,200 --> 10:50:55,680 here now I'm allocating enough memory to 17218 10:50:53,840 --> 10:50:57,800 fit one integer now technically it's 17219 10:50:55,680 --> 10:51:00,360 four we know that but size of in just 17220 10:50:57,800 --> 10:51:01,880 gives me that answer dynamically so it 17221 10:51:00,360 --> 10:51:03,480 will work on all systems 17222 10:51:01,880 --> 10:51:08,400 and I'm going to store the address that 17223 10:51:03,480 --> 10:51:11,040 Malo finds for me in X then I go to x 17224 10:51:08,400 --> 10:51:13,520 and put the number 42 there all right 17225 10:51:11,040 --> 10:51:15,800 why it's the sort of meaning of life the 17226 10:51:13,520 --> 10:51:18,080 universe and everything here but star X 17227 10:51:15,800 --> 10:51:20,200 again just means go to that address and 17228 10:51:18,080 --> 10:51:22,720 put a value there so why I don't know 17229 10:51:20,200 --> 10:51:25,756 but it's just uh correct at this point 17230 10:51:22,720 --> 10:51:28,640 but what about this line here star y 17231 10:51:25,756 --> 10:51:31,596 equals 13 unlucky in this case what's 17232 10:51:28,640 --> 10:51:33,040 bad about this line here star y It's a 17233 10:51:31,596 --> 10:51:35,680 combination now of today's Primitives 17234 10:51:33,040 --> 10:51:37,800 and that point here 17235 10:51:35,680 --> 10:51:39,916 yeah yeah we didn't ask the computer to 17236 10:51:37,800 --> 10:51:41,756 allocate any space so why was not 17237 10:51:39,916 --> 10:51:43,640 initialized with an equal sign at any 17238 10:51:41,756 --> 10:51:46,520 point to anything and so what is inside 17239 10:51:43,640 --> 10:51:47,960 y so to speak like a garbage value maybe 17240 10:51:46,520 --> 10:51:49,080 it's zero which isn't bad because at 17241 10:51:47,960 --> 10:51:51,040 least it's nice and simple but maybe 17242 10:51:49,080 --> 10:51:52,916 it's some crazy large uh positive number 17243 10:51:51,040 --> 10:51:54,720 some crazy large negative number either 17244 10:51:52,916 --> 10:51:57,400 way odds are if I go to this address or 17245 10:51:54,720 --> 10:51:59,200 that address randomly with star y bad 17246 10:51:57,400 --> 10:52:00,680 things are going to happen and so let me 17247 10:51:59,200 --> 10:52:02,160 go ahead and propose well let's not do 17248 10:52:00,680 --> 10:52:04,880 that let's let's actually do this 17249 10:52:02,160 --> 10:52:07,080 instead assign y equal to X and we've 17250 10:52:04,880 --> 10:52:10,560 done that before and then I can go to Y 17251 10:52:07,080 --> 10:52:12,276 now and change what was a 42 to a 13 17252 10:52:10,560 --> 10:52:15,116 again why this is just for educational 17253 10:52:12,276 --> 10:52:18,040 sake but for now this does not crash 17254 10:52:15,116 --> 10:52:19,756 because I only dreference y with star y 17255 10:52:18,040 --> 10:52:22,480 after actually giving it a value albe it 17256 10:52:19,756 --> 10:52:24,160 a duplicate value similar to our copy 17257 10:52:22,480 --> 10:52:25,800 example earlier so our friends at 17258 10:52:24,160 --> 10:52:27,596 Stanford have put together a wonderful 17259 10:52:25,800 --> 10:52:29,596 visual it's about 2 minutes long allow 17260 10:52:27,596 --> 10:52:32,116 me to dramatically dim the lights if we 17261 10:52:29,596 --> 10:52:35,820 could and play with what happens with 17262 10:52:32,116 --> 10:52:37,000 memory when you do bad things like 17263 10:52:35,820 --> 10:52:40,360 [Music] 17264 10:52:37,000 --> 10:52:44,000 this hey Binky wake up it's time for 17265 10:52:40,360 --> 10:52:47,800 pointer fun what's that learn about 17266 10:52:44,000 --> 10:52:48,960 pointers Oh goodie well to get started I 17267 10:52:47,800 --> 10:52:51,880 guess we're going to need a couple 17268 10:52:48,960 --> 10:52:54,400 pointers okay this code allocates two 17269 10:52:51,880 --> 10:52:56,596 pointers which can point to integers 17270 10:52:54,400 --> 10:52:57,720 okay well I see the two pointers but 17271 10:52:56,596 --> 10:53:00,000 they don't seem to be pointing to 17272 10:52:57,720 --> 10:53:01,720 anything that's right initially pointers 17273 10:53:00,000 --> 10:53:03,880 don't point to anything the things they 17274 10:53:01,720 --> 10:53:06,116 point to are called Pointes and setting 17275 10:53:03,880 --> 10:53:07,840 them up is a separate step oh right 17276 10:53:06,116 --> 10:53:10,240 right I knew that the Pointes are 17277 10:53:07,840 --> 10:53:13,560 separate or so how do you allocate a 17278 10:53:10,240 --> 10:53:16,320 point e okay well this code allocates a 17279 10:53:13,560 --> 10:53:19,596 new integer point and this part sets X 17280 10:53:16,320 --> 10:53:22,116 to point to it hey that looks better so 17281 10:53:19,596 --> 10:53:24,160 make it do something okay I'll 17282 10:53:22,116 --> 10:53:27,080 dereference the pointer X to store the 17283 10:53:24,160 --> 10:53:29,320 number 42 into its Point e for this 17284 10:53:27,080 --> 10:53:32,480 trick I'll need my magic wand of 17285 10:53:29,320 --> 10:53:35,800 dereferencing your magic wand of D 17286 10:53:32,480 --> 10:53:37,720 referencing uh that that's great this is 17287 10:53:35,800 --> 10:53:39,276 what the code looks like I'll just set 17288 10:53:37,720 --> 10:53:43,400 up the number 17289 10:53:39,276 --> 10:53:45,880 and hey look there it goes so doing a d 17290 10:53:43,400 --> 10:53:47,960 reference on X follows the arrow to 17291 10:53:45,880 --> 10:53:50,680 access its Point T in this case to store 17292 10:53:47,960 --> 10:53:53,040 42 in there hey try using it to store 17293 10:53:50,680 --> 10:53:57,400 the number 13 through the other pointer 17294 10:53:53,040 --> 10:54:00,160 why okay I'll just go over here to Y and 17295 10:53:57,400 --> 10:54:04,040 get the number 13 set up and then take 17296 10:54:00,160 --> 10:54:08,000 the wand of d referencing and 17297 10:54:04,040 --> 10:54:10,040 just oh hey that didn't work say uh 17298 10:54:08,000 --> 10:54:12,436 Binky I don't think de referencing Y is 17299 10:54:10,040 --> 10:54:14,640 a good idea cuz uh you know setting up 17300 10:54:12,436 --> 10:54:17,880 the point T is a separate step and uh I 17301 10:54:14,640 --> 10:54:20,200 don't think we ever did it good point 17302 10:54:17,880 --> 10:54:22,360 yeah we we allocated the pointer y but 17303 10:54:20,200 --> 10:54:25,520 we never set it to point to a point 17304 10:54:22,360 --> 10:54:27,680 T very observant hey you're looking good 17305 10:54:25,520 --> 10:54:30,240 there Binky can you fix it so that y 17306 10:54:27,680 --> 10:54:32,880 points to the same point as X sure I'll 17307 10:54:30,240 --> 10:54:34,276 use my magic wand of pointer assignment 17308 10:54:32,880 --> 10:54:36,916 is that going to be a problem like 17309 10:54:34,276 --> 10:54:38,640 before no this doesn't touch the Pointes 17310 10:54:36,916 --> 10:54:42,200 it just changes one pointer to point to 17311 10:54:38,640 --> 10:54:45,080 the same thing is another oh I see now y 17312 10:54:42,200 --> 10:54:47,520 points to the same place as X so so wait 17313 10:54:45,080 --> 10:54:49,116 now Y is fixed it has a point e so you 17314 10:54:47,520 --> 10:54:53,320 can try the wand of D referencing again 17315 10:54:49,116 --> 10:54:56,240 to send the 13 over uh okay here it 17316 10:54:53,320 --> 10:54:58,000 goes hey look at that now D referencing 17317 10:54:56,240 --> 10:54:59,840 works on why and because the pointers 17318 10:54:58,000 --> 10:55:03,240 are sharing that one point e they both 17319 10:54:59,840 --> 10:55:05,320 see the 13 yeah sharing uh whatever so 17320 10:55:03,240 --> 10:55:07,960 are we going to switch places now oh 17321 10:55:05,320 --> 10:55:09,720 look we're out of time but L thanks to 17322 10:55:07,960 --> 10:55:11,436 Professor Nick parlante of Stanford for 17323 10:55:09,720 --> 10:55:13,480 spending a huge amount of time doing 17324 10:55:11,436 --> 10:55:14,720 stop motion animation for that but 17325 10:55:13,480 --> 10:55:17,880 hopefully now you have a sense of what 17326 10:55:14,720 --> 10:55:19,800 two can go wrong when you misuse memory 17327 10:55:17,880 --> 10:55:21,080 in this way but at the end of the day we 17328 10:55:19,800 --> 10:55:22,880 really only have these four new building 17329 10:55:21,080 --> 10:55:25,320 blocks today like the star operator the 17330 10:55:22,880 --> 10:55:26,960 Ampersand operator malok and free and 17331 10:55:25,320 --> 10:55:28,276 really with that and the underlying 17332 10:55:26,960 --> 10:55:30,240 understanding of what your computer is 17333 10:55:28,276 --> 10:55:32,320 doing underneath the hood we have this 17334 10:55:30,240 --> 10:55:33,960 way now to really manipulate things in 17335 10:55:32,320 --> 10:55:35,360 memory For Better or For Worse and 17336 10:55:33,960 --> 10:55:37,360 eventually we'll see how we can build 17337 10:55:35,360 --> 10:55:39,880 things but we can also now use today's 17338 10:55:37,360 --> 10:55:41,200 Primitives to better explain some things 17339 10:55:39,880 --> 10:55:43,160 that we've been asking you to take for 17340 10:55:41,200 --> 10:55:46,436 granted over the past several weeks so 17341 10:55:43,160 --> 10:55:49,240 for instance let me propose that we uh 17342 10:55:46,436 --> 10:55:51,400 do one volunteer up here if we could 17343 10:55:49,240 --> 10:55:53,240 could we get one volunteer who's you 17344 10:55:51,400 --> 10:55:54,640 want to come straight up yep right in 17345 10:55:53,240 --> 10:55:57,400 the middle come on you'll have to take a 17346 10:55:54,640 --> 10:55:57,400 left right 17347 10:55:59,880 --> 10:56:04,400 there 17348 10:56:01,800 --> 10:56:06,680 all right so we have two empty glasses 17349 10:56:04,400 --> 10:56:08,320 here and two colors of liquid and we 17350 10:56:06,680 --> 10:56:10,080 have let me give you the mic if you'd 17351 10:56:08,320 --> 10:56:13,680 like to say hello to the 17352 10:56:10,080 --> 10:56:15,596 group hello um I'm Moen uh I'm in the 17353 10:56:13,680 --> 10:56:17,200 and first year all right welcome well 17354 10:56:15,596 --> 10:56:18,916 well welcome here I'm going to go ahead 17355 10:56:17,200 --> 10:56:21,400 and fill these two glasses with this 17356 10:56:18,916 --> 10:56:25,436 colored liquid um purple here on my 17357 10:56:21,400 --> 10:56:25,436 right let's fill up a glass 17358 10:56:25,720 --> 10:56:32,276 here yeah don't drink uh and now we'll 17359 10:56:29,080 --> 10:56:33,840 put some orange in here 17360 10:56:32,276 --> 10:56:36,916 and what we'd like you to do for the 17361 10:56:33,840 --> 10:56:38,880 audience if you don't mind is swap the 17362 10:56:36,916 --> 10:56:40,436 two values we've got a purple value and 17363 10:56:38,880 --> 10:56:42,240 orange value and I'd like the purple 17364 10:56:40,436 --> 10:56:45,360 liquid in this glass and the orange 17365 10:56:42,240 --> 10:56:45,360 liquid in that glass 17366 10:56:45,840 --> 10:56:51,320 please can I have another glass oh okay 17367 10:56:48,480 --> 10:56:53,520 good intuition but for the microphone uh 17368 10:56:51,320 --> 10:56:55,520 can I have another glass so you can and 17369 10:56:53,520 --> 10:56:57,276 just in fact I brought one here for you 17370 10:56:55,520 --> 10:56:58,880 why are you asking for this though cuz 17371 10:56:57,276 --> 10:57:00,680 if I just pour this into this and it'll 17372 10:56:58,880 --> 10:57:03,400 get mixed up right so obviously we need 17373 10:57:00,680 --> 10:57:05,960 like a temporary variable if you will so 17374 10:57:03,400 --> 10:57:09,680 here is your temporary 17375 10:57:05,960 --> 10:57:12,596 variable and you want yeah there's 17376 10:57:09,680 --> 10:57:14,640 yeah all right so pouring the value of 17377 10:57:12,596 --> 10:57:16,400 the orange glass into this temporary 17378 10:57:14,640 --> 10:57:20,040 variable if you 17379 10:57:16,400 --> 10:57:22,240 will all right and now pouring the value 17380 10:57:20,040 --> 10:57:24,800 of the purple glass into the former 17381 10:57:22,240 --> 10:57:24,800 orange 17382 10:57:26,000 --> 10:57:31,756 glass and 17383 10:57:28,640 --> 10:57:34,400 now the temporary value goes back into 17384 10:57:31,756 --> 10:57:35,720 the original purple glass and now I 17385 10:57:34,400 --> 10:57:38,960 think we give you a round of applause 17386 10:57:35,720 --> 10:57:42,880 for having done that very well 17387 10:57:38,960 --> 10:57:44,240 okay thank you all right so so it should 17388 10:57:42,880 --> 10:57:45,880 go without saying that in the real world 17389 10:57:44,240 --> 10:57:47,840 like that's how you do this and in fact 17390 10:57:45,880 --> 10:57:50,240 in code that's pretty much how you have 17391 10:57:47,840 --> 10:57:52,040 to do this although ask us some time for 17392 10:57:50,240 --> 10:57:53,800 a super fancy way of doing it without a 17393 10:57:52,040 --> 10:57:56,200 temporary variable turns out that is 17394 10:57:53,800 --> 10:57:57,680 possible using bits but for now let's 17395 10:57:56,200 --> 10:57:59,040 suppose that indeed this demonstrates 17396 10:57:57,680 --> 10:58:00,916 what is the reality in code if you want 17397 10:57:59,040 --> 10:58:03,000 to swap two values you need to have have 17398 10:58:00,916 --> 10:58:05,840 something like a temporary variable so 17399 10:58:03,000 --> 10:58:07,480 for instance on the screen here is a uh 17400 10:58:05,840 --> 10:58:09,116 the beginning of a function called swap 17401 10:58:07,480 --> 10:58:11,880 whose purpose in life is to as you just 17402 10:58:09,116 --> 10:58:13,436 did swap two values call it a and b so 17403 10:58:11,880 --> 10:58:15,160 orange and purple respectively are now 17404 10:58:13,436 --> 10:58:16,880 just a and b and integers to keep things 17405 10:58:15,160 --> 10:58:18,480 simple well here is the corresponding 17406 10:58:16,880 --> 10:58:20,400 code if I may to what you just enacted 17407 10:58:18,480 --> 10:58:22,560 as a human you declared a temporary 17408 10:58:20,400 --> 10:58:24,160 variable a called temp in this case 17409 10:58:22,560 --> 10:58:26,080 which was like me handing you The Empty 17410 10:58:24,160 --> 10:58:29,520 Glass and you stored the orange liquid 17411 10:58:26,080 --> 10:58:32,520 in it akaa you then changed the value of 17412 10:58:29,520 --> 10:58:34,520 the formerly orange glass to be equal to 17413 10:58:32,520 --> 10:58:36,240 the Purple by pouring one into the other 17414 10:58:34,520 --> 10:58:37,596 and then you did the opposite there now 17415 10:58:36,240 --> 10:58:39,480 at the end of this you still have a 17416 10:58:37,596 --> 10:58:41,080 temporary variable that's now empty so 17417 10:58:39,480 --> 10:58:42,360 it's temporary in literally that sense 17418 10:58:41,080 --> 10:58:45,080 like you just don't need it anymore but 17419 10:58:42,360 --> 10:58:47,756 it was necessary along the way so I dare 17420 10:58:45,080 --> 10:58:51,040 say this code is correct logically like 17421 10:58:47,756 --> 10:58:53,520 this will swap two values A and B thanks 17422 10:58:51,040 --> 10:58:55,360 to the use of that temporary variable 17423 10:58:53,520 --> 10:58:57,200 unfortunately though if I actually do 17424 10:58:55,360 --> 10:58:58,756 this in practice let me go over to VSS 17425 10:58:57,200 --> 10:59:01,960 code here and open a program I wrote in 17426 10:58:58,756 --> 10:59:04,480 advance called swap. C which does this 17427 10:59:01,960 --> 10:59:06,040 as follows in here notice I have my 17428 10:59:04,480 --> 10:59:07,840 prototype for a swap function at the 17429 10:59:06,040 --> 10:59:09,756 very top and let me scroll down to the 17430 10:59:07,840 --> 10:59:12,596 very bottom there is that exact same 17431 10:59:09,756 --> 10:59:14,400 code so I'm uh the same code for 17432 10:59:12,596 --> 10:59:16,916 swapping two values A and B which I'm 17433 10:59:14,400 --> 10:59:18,276 claiming for now is correct now if I go 17434 10:59:16,916 --> 10:59:19,756 back up here what is main going to do 17435 10:59:18,276 --> 10:59:21,436 for us main is really just meant to be a 17436 10:59:19,756 --> 10:59:23,680 demonstration of the correctness of your 17437 10:59:21,436 --> 10:59:26,000 algorithm so here I declare on line 17438 10:59:23,680 --> 10:59:28,756 seven and eight two variables X and Y 17439 10:59:26,000 --> 10:59:30,640 being 1 and two arbitrarily respectively 17440 10:59:28,756 --> 10:59:32,840 I then on line 10 just print out what 17441 10:59:30,640 --> 10:59:35,276 the value of x is and Y is just so I can 17442 10:59:32,840 --> 10:59:37,480 see it on the screen I then call the 17443 10:59:35,276 --> 10:59:39,116 swap function on line 11 and then I 17444 10:59:37,480 --> 10:59:41,680 literally print the exact same thing 17445 10:59:39,116 --> 10:59:43,960 again I print X and Y hopefully it'll 17446 10:59:41,680 --> 10:59:45,720 obviously be the opposite so I think 17447 10:59:43,960 --> 10:59:48,640 logically swap is indeed correct let me 17448 10:59:45,720 --> 10:59:51,436 do make Swap and then do/ Swap and I 17449 10:59:48,640 --> 10:59:56,080 should see X is one y is 2 and then 17450 10:59:51,436 --> 10:59:58,720 hopefully X is 2 Y is 1 enter but I 17451 10:59:56,080 --> 11:00:00,880 don't and it did work in the sense that 17452 10:59:58,720 --> 11:00:03,436 the code compiled the code ran so it's 17453 11:00:00,880 --> 11:00:05,640 not like some bug in that sense but 17454 11:00:03,436 --> 11:00:07,360 because I don't quite understand what's 17455 11:00:05,640 --> 11:00:10,000 going on underneath the hood at least as 17456 11:00:07,360 --> 11:00:13,560 of right now or prior weeks this code 17457 11:00:10,000 --> 11:00:15,276 here is indeed buggy in some way but 17458 11:00:13,560 --> 11:00:17,080 does anyone have an intuition perhaps 17459 11:00:15,276 --> 11:00:18,880 based on today's discussion is to like 17460 11:00:17,080 --> 11:00:21,080 why this code while logically correct 17461 11:00:18,880 --> 11:00:22,680 clearly works in reality apparently does 17462 11:00:21,080 --> 11:00:26,916 not work in 17463 11:00:22,680 --> 11:00:26,916 C any intuition 17464 11:00:27,560 --> 11:00:31,040 yeah perfect and to summarize here's 17465 11:00:29,596 --> 11:00:33,160 that term of art I prom 17466 11:00:31,040 --> 11:00:35,200 when you call a function and pass in two 17467 11:00:33,160 --> 11:00:38,240 arguments Like A and B you're passing 17468 11:00:35,200 --> 11:00:40,520 those arguments by value so copies of 17469 11:00:38,240 --> 11:00:43,080 those values effectively and so when 17470 11:00:40,520 --> 11:00:45,840 swap is actually called here sorry when 17471 11:00:43,080 --> 11:00:47,080 you pass in X and Y we call them A and B 17472 11:00:45,840 --> 11:00:49,596 but that's just a convention we could 17473 11:00:47,080 --> 11:00:52,160 call the parameters anything we want 17474 11:00:49,596 --> 11:00:55,000 what A and B are are indeed the values 17475 11:00:52,160 --> 11:00:57,520 of X and Y respectively but copies of 17476 11:00:55,000 --> 11:00:59,800 the values so this code here is very 17477 11:00:57,520 --> 11:01:02,436 successfully in vs code 2 swapping the 17478 11:00:59,800 --> 11:01:04,720 values of a A and B but as you note 17479 11:01:02,436 --> 11:01:06,960 because I'm passing them in by value 17480 11:01:04,720 --> 11:01:09,640 literally one literally two and not by 17481 11:01:06,960 --> 11:01:12,756 another term of art by reference AKA by 17482 11:01:09,640 --> 11:01:15,720 their addresses swap has no capability 17483 11:01:12,756 --> 11:01:17,276 in C to go to those locations swap the 17484 11:01:15,720 --> 11:01:19,840 actual locations just like we did 17485 11:01:17,276 --> 11:01:21,436 successfully in reality but I think we 17486 11:01:19,840 --> 11:01:23,360 really have the syntax already for 17487 11:01:21,436 --> 11:01:24,880 solving this if we consider that really 17488 11:01:23,360 --> 11:01:26,840 this is just an issue of scope and we've 17489 11:01:24,880 --> 11:01:28,840 talked a bit about scope in the past 17490 11:01:26,840 --> 11:01:30,800 whereby scope refers to the context in 17491 11:01:28,840 --> 11:01:32,240 which a variable lives and generally 17492 11:01:30,800 --> 11:01:34,480 I've claimed that a variable exists 17493 11:01:32,240 --> 11:01:36,116 between the most recent curly braces and 17494 11:01:34,480 --> 11:01:38,640 that's pretty much true for the swap 17495 11:01:36,116 --> 11:01:40,756 function because a and b i now claim 17496 11:01:38,640 --> 11:01:43,000 again exist only in the context of these 17497 11:01:40,756 --> 11:01:45,276 curly braces they have no effect on Main 17498 11:01:43,000 --> 11:01:47,520 up top which has different variables X 17499 11:01:45,276 --> 11:01:48,880 and Y but we can consider now what's 17500 11:01:47,520 --> 11:01:50,240 really going on underneath the hood and 17501 11:01:48,880 --> 11:01:52,160 here's that same picture of memory as 17502 11:01:50,240 --> 11:01:53,720 we've seen in the past if we zoom in and 17503 11:01:52,160 --> 11:01:55,880 see on these little black chips this is 17504 11:01:53,720 --> 11:01:57,840 a bunch of bites of memory if I create a 17505 11:01:55,880 --> 11:01:59,800 grid out of it just to kind of highlight 17506 11:01:57,840 --> 11:02:01,560 that we can address each of these bites 17507 11:01:59,800 --> 11:02:04,000 throw away the plastic circuit board and 17508 11:02:01,560 --> 11:02:06,520 focus only on those bites what's going 17509 11:02:04,000 --> 11:02:08,520 on underneath the hood when functions 17510 11:02:06,520 --> 11:02:10,840 are called in C which you've been doing 17511 11:02:08,520 --> 11:02:12,480 for weeks now well this rectangle of 17512 11:02:10,840 --> 11:02:15,080 memory if we kind of abstract it away 17513 11:02:12,480 --> 11:02:16,960 further is generally broken up into 17514 11:02:15,080 --> 11:02:19,360 different regions or segments like I 17515 11:02:16,960 --> 11:02:20,560 called them earlier and different things 17516 11:02:19,360 --> 11:02:22,480 get put in different parts of the 17517 11:02:20,560 --> 11:02:24,960 computer's memory and without getting 17518 11:02:22,480 --> 11:02:26,840 too into the weeds when you double click 17519 11:02:24,960 --> 11:02:31,080 a program on your macro PC or when you 17520 11:02:26,840 --> 11:02:33,080 do/ something on a Linux you are loading 17521 11:02:31,080 --> 11:02:35,560 your machine code into the computer's 17522 11:02:33,080 --> 11:02:37,080 memory from the computer's hard drive so 17523 11:02:35,560 --> 11:02:39,400 all the zeros and ones that compose 17524 11:02:37,080 --> 11:02:42,000 Microsoft Word or Chrome or whatever are 17525 11:02:39,400 --> 11:02:43,800 loaded into the computer's memory or RAM 17526 11:02:42,000 --> 11:02:45,596 and by convention it's put up top in the 17527 11:02:43,800 --> 11:02:47,916 so-called machine code area and that's 17528 11:02:45,596 --> 11:02:50,360 how the CPU has access to them quickly 17529 11:02:47,916 --> 11:02:52,640 at that below that are what are be going 17530 11:02:50,360 --> 11:02:54,520 to be our globals so Global variables 17531 11:02:52,640 --> 11:02:56,720 which we haven't used very much in C but 17532 11:02:54,520 --> 11:02:58,480 you can declare them outside of main at 17533 11:02:56,720 --> 11:03:00,320 the very top of your files if you have 17534 11:02:58,480 --> 11:03:02,436 globals they end up up there as well 17535 11:03:00,320 --> 11:03:04,756 just FYI and then there's this big chunk 17536 11:03:02,436 --> 11:03:06,560 of memory that we saw valgren mention 17537 11:03:04,756 --> 11:03:08,240 indirectly earlier called the Heap and 17538 11:03:06,560 --> 11:03:10,360 it's kind of like Heap literally like 17539 11:03:08,240 --> 11:03:13,240 it's a heap of memory that you can use 17540 11:03:10,360 --> 11:03:15,596 as you see fit and the Heap is where 17541 11:03:13,240 --> 11:03:17,200 malok grabs memory from so initially 17542 11:03:15,596 --> 11:03:19,276 there's nothing in the Heap it's just a 17543 11:03:17,200 --> 11:03:21,800 big chunk of free space anytime you call 17544 11:03:19,276 --> 11:03:24,436 malok Malo kind of carves out from the 17545 11:03:21,800 --> 11:03:26,116 Heap area more and more bites and Malo 17546 11:03:24,436 --> 11:03:28,040 keeps track of essentially which bites 17547 11:03:26,116 --> 11:03:30,276 have already been allocated so initially 17548 11:03:28,040 --> 11:03:31,640 it looks empty but different bites 17549 11:03:30,276 --> 11:03:33,200 squares if you will keep getting 17550 11:03:31,640 --> 11:03:35,880 requested again and again as a program 17551 11:03:33,200 --> 11:03:37,640 runs thanks to functions like malok and 17552 11:03:35,880 --> 11:03:39,276 it grows if you will conceptually down 17553 11:03:37,640 --> 11:03:41,480 so the more and more memory you request 17554 11:03:39,276 --> 11:03:42,916 from malok it starts up here but then 17555 11:03:41,480 --> 11:03:44,596 the next chunk you get is down here 17556 11:03:42,916 --> 11:03:46,436 conceptually the next chunk is down here 17557 11:03:44,596 --> 11:03:48,080 down here so it kind of fills the 17558 11:03:46,436 --> 11:03:50,040 available space in the computer's 17559 11:03:48,080 --> 11:03:53,080 overall memory but there's this other 17560 11:03:50,040 --> 11:03:54,640 chunk of memory called the stack and 17561 11:03:53,080 --> 11:03:57,080 just like a stack of trays in like 17562 11:03:54,640 --> 11:03:59,400 annenburg or a cafeteria kind of grow 17563 11:03:57,080 --> 11:04:02,400 upward so does the stack of memory and 17564 11:03:59,400 --> 11:04:05,436 it turns out the stack is where 17565 11:04:02,400 --> 11:04:07,960 functions have variables and have 17566 11:04:05,436 --> 11:04:10,436 arguments stored temporarily so whenever 17567 11:04:07,960 --> 11:04:12,040 you call a function and it has variables 17568 11:04:10,436 --> 11:04:14,080 inside of it or it has arguments there 17569 11:04:12,040 --> 11:04:16,320 too this is the chunk of memory and the 17570 11:04:14,080 --> 11:04:18,200 computer's overall block of memory that 17571 11:04:16,320 --> 11:04:20,840 are used for functions but anytime you 17572 11:04:18,200 --> 11:04:22,160 call malok it's memory up here right at 17573 11:04:20,840 --> 11:04:23,720 the end of the day they just had to pick 17574 11:04:22,160 --> 11:04:24,800 a direction top bottom and technically 17575 11:04:23,720 --> 11:04:26,596 it's an artist rendition you could 17576 11:04:24,800 --> 11:04:28,480 Circle this thing around any orientation 17577 11:04:26,596 --> 11:04:30,240 you want but you're just using a finite 17578 11:04:28,480 --> 11:04:32,916 amount of memory in this conventional 17579 11:04:30,240 --> 11:04:34,720 way Malo Starts Here functions start 17580 11:04:32,916 --> 11:04:37,080 here now you can kind of see where like 17581 11:04:34,720 --> 11:04:38,880 bad things can happen and indeed one of 17582 11:04:37,080 --> 11:04:40,960 the other reasons programs computers can 17583 11:04:38,880 --> 11:04:42,756 crash is if you ask for way too much 17584 11:04:40,960 --> 11:04:45,116 memory from the Heap by calling malok 17585 11:04:42,756 --> 11:04:47,080 many many many times or if you call way 17586 11:04:45,116 --> 11:04:49,320 too many functions or accidentally per 17587 11:04:47,080 --> 11:04:51,116 last week you recurse infinitely many 17588 11:04:49,320 --> 11:04:52,880 times you might have a segmentation 17589 11:04:51,116 --> 11:04:54,960 fault and that's because you're using 17590 11:04:52,880 --> 11:04:57,080 too much stack memory so this is bound 17591 11:04:54,960 --> 11:04:59,436 to be a problem eventually and the onus 17592 11:04:57,080 --> 11:05:01,520 is on the programmer to just minimize 17593 11:04:59,436 --> 11:05:03,360 the probability of doing that and really 17594 11:05:01,520 --> 11:05:05,000 avoid the possibility of doing that by 17595 11:05:03,360 --> 11:05:07,360 just checking return values like 17596 11:05:05,000 --> 11:05:09,560 checking if malok or get string return 17597 11:05:07,360 --> 11:05:11,756 null because you can proactively with 17598 11:05:09,560 --> 11:05:13,436 conditionals make sure that these two 17599 11:05:11,756 --> 11:05:16,040 things do not Collide by just making 17600 11:05:13,436 --> 11:05:17,520 sure that you get back non-null values 17601 11:05:16,040 --> 11:05:19,800 so let's consider the stack in the 17602 11:05:17,520 --> 11:05:20,756 context of Swap and what's really 17603 11:05:19,800 --> 11:05:21,960 happening here and Carter if you 17604 11:05:20,756 --> 11:05:24,960 wouldn't mind helping me animate the 17605 11:05:21,960 --> 11:05:28,040 screen here when I call the main 17606 11:05:24,960 --> 11:05:30,520 function of any program it is allocated 17607 11:05:28,040 --> 11:05:31,840 a slice of memory called a frame at the 17608 11:05:30,520 --> 11:05:33,720 bottom of this stack so if Carter you 17609 11:05:31,840 --> 11:05:35,436 want to go ahead and uh Advance here 17610 11:05:33,720 --> 11:05:37,560 here's like the First Slice of memory 17611 11:05:35,436 --> 11:05:39,680 that will always be used by main whether 17612 11:05:37,560 --> 11:05:42,436 it has commandline arguments or local 17613 11:05:39,680 --> 11:05:45,040 variables it just ends up here in memory 17614 11:05:42,436 --> 11:05:47,520 suppose now per our swap. C program that 17615 11:05:45,040 --> 11:05:49,520 main calls swap well where does the 17616 11:05:47,520 --> 11:05:51,360 memory for swap end up like right up 17617 11:05:49,520 --> 11:05:54,116 here so swap had two variable two 17618 11:05:51,360 --> 11:05:55,840 arguments uh A and B and it also had a 17619 11:05:54,116 --> 11:05:57,916 temporary variable so all of those end 17620 11:05:55,840 --> 11:05:59,720 up in here in memory and if you want to 17621 11:05:57,916 --> 11:06:02,400 go ahead and Advance again Carter once 17622 11:05:59,720 --> 11:06:03,880 swap is done executing whether it just 17623 11:06:02,400 --> 11:06:06,080 returns because there's no more lines of 17624 11:06:03,880 --> 11:06:08,520 code or you explicitly return this 17625 11:06:06,080 --> 11:06:10,560 memory is just freed up automatically 17626 11:06:08,520 --> 11:06:12,160 you don't call free you don't undo malok 17627 11:06:10,560 --> 11:06:14,840 this just all happens automatically and 17628 11:06:12,160 --> 11:06:16,756 has been since week one now technically 17629 11:06:14,840 --> 11:06:18,560 it's still there even though we've 17630 11:06:16,756 --> 11:06:20,640 removed it from the picture and there's 17631 11:06:18,560 --> 11:06:22,276 your first hint of garbage values right 17632 11:06:20,640 --> 11:06:23,960 like there's still zeros and ones there 17633 11:06:22,276 --> 11:06:26,200 and they're left in the original the 17634 11:06:23,960 --> 11:06:28,200 previous configuration and so the reason 17635 11:06:26,200 --> 11:06:29,680 you get random values in the memory is 17636 11:06:28,200 --> 11:06:31,720 because even though we hav't drawn swap 17637 11:06:29,680 --> 11:06:33,116 here there was stuff there a moment ago 17638 11:06:31,720 --> 11:06:35,360 it's going to be there the next time you 17639 11:06:33,116 --> 11:06:36,360 use that same memory now let's go ahead 17640 11:06:35,360 --> 11:06:39,240 and step through this a little more 17641 11:06:36,360 --> 11:06:41,596 methodically main has two variables 17642 11:06:39,240 --> 11:06:44,240 called X and Y one and two so let's 17643 11:06:41,596 --> 11:06:46,080 advance and represent X is one y is two 17644 11:06:44,240 --> 11:06:48,360 taking up these two chunks of memory 17645 11:06:46,080 --> 11:06:51,480 when we call swamp now swamp gets a new 17646 11:06:48,360 --> 11:06:53,400 slice of memory that then gives us three 17647 11:06:51,480 --> 11:06:56,040 variables A and B technically the 17648 11:06:53,400 --> 11:06:58,276 arguments and temp so what happens well 17649 11:06:56,040 --> 11:07:00,640 because functions automatically pass in 17650 11:06:58,276 --> 11:07:04,360 values by value or rather pass in 17651 11:07:00,640 --> 11:07:07,000 arguments by value X gets copied into a 17652 11:07:04,360 --> 11:07:08,880 y gets copied into B and then once we 17653 11:07:07,000 --> 11:07:11,040 start executing the algorithm all the 17654 11:07:08,880 --> 11:07:12,800 watered glasses well what happens here 17655 11:07:11,040 --> 11:07:16,000 so if I execute the first line of code 17656 11:07:12,800 --> 11:07:18,880 temp equals a temp gets a copy of a what 17657 11:07:16,000 --> 11:07:21,200 happens next a equals B so a takes on a 17658 11:07:18,880 --> 11:07:23,800 copy of B and now we do the final swap 17659 11:07:21,200 --> 11:07:25,916 in the glasses b equals temp b gets a 17660 11:07:23,800 --> 11:07:27,840 copy of temp now we don't have to change 17661 11:07:25,916 --> 11:07:29,560 temp because it's essentially empty 17662 11:07:27,840 --> 11:07:31,116 although there's the garbage value one 17663 11:07:29,560 --> 11:07:33,040 is always now going to be there until we 17664 11:07:31,116 --> 11:07:35,400 reuse that memory the important thing 17665 11:07:33,040 --> 11:07:37,436 though is that a and b have been swapped 17666 11:07:35,400 --> 11:07:40,276 but what obviously has not been swapped 17667 11:07:37,436 --> 11:07:42,400 as is Manifest is when swap returns X 17668 11:07:40,276 --> 11:07:44,436 and Y are untouched because copies 17669 11:07:42,400 --> 11:07:45,756 thereof were passed in so we need a 17670 11:07:44,436 --> 11:07:47,276 solution to this problem and if we 17671 11:07:45,756 --> 11:07:48,680 advance one more time if you don't mind 17672 11:07:47,276 --> 11:07:51,320 let me step over here but then call you 17673 11:07:48,680 --> 11:07:53,560 back in a second this code here is 17674 11:07:51,320 --> 11:07:55,960 logically correct this is what you did 17675 11:07:53,560 --> 11:07:57,640 but this is now a detail of C you can't 17676 11:07:55,960 --> 11:07:59,240 just swap the things by value because 17677 11:07:57,640 --> 11:08:01,436 you're only changing it in the scope of 17678 11:07:59,240 --> 11:08:03,400 the swap function function but I think 17679 11:08:01,436 --> 11:08:06,640 if we change it to this and add some 17680 11:08:03,400 --> 11:08:08,560 annoying syntax we can solve the problem 17681 11:08:06,640 --> 11:08:10,436 just like you can declare variables as 17682 11:08:08,560 --> 11:08:13,080 storing addresses you can declare 17683 11:08:10,436 --> 11:08:15,480 arguments to functions AKA parameters as 17684 11:08:13,080 --> 11:08:17,800 taking addresses this new version of 17685 11:08:15,480 --> 11:08:20,240 swap means that a shall be the address 17686 11:08:17,800 --> 11:08:22,160 of an integer B shall be the address of 17687 11:08:20,240 --> 11:08:24,200 an integer and now it gets a little 17688 11:08:22,160 --> 11:08:25,916 cryptic here temp is the same because 17689 11:08:24,200 --> 11:08:28,400 it's just an integer like it was in week 17690 11:08:25,916 --> 11:08:31,436 one nothing special about temp but if 17691 11:08:28,400 --> 11:08:33,240 you want to get the value at a you do 17692 11:08:31,436 --> 11:08:35,400 star a and that goes to the address 17693 11:08:33,240 --> 11:08:37,520 grabs the number one presumably if you 17694 11:08:35,400 --> 11:08:39,240 want to change the value of a you go to 17695 11:08:37,520 --> 11:08:41,000 that address you follow the treasure map 17696 11:08:39,240 --> 11:08:43,240 to the other mailbox and you set it 17697 11:08:41,000 --> 11:08:46,116 equal to whatever is at the value of B 17698 11:08:43,240 --> 11:08:47,756 you go to B as well last line you go to 17699 11:08:46,116 --> 11:08:49,840 B now and change it to be whatever the 17700 11:08:47,756 --> 11:08:51,720 temporary variable was which happened to 17701 11:08:49,840 --> 11:08:53,960 be the same as a so that's where the 17702 11:08:51,720 --> 11:08:55,276 final value gets swapped but here 17703 11:08:53,960 --> 11:08:56,640 there's a lot more like crisscrossing 17704 11:08:55,276 --> 11:08:58,000 metaphorically across the stage where 17705 11:08:56,640 --> 11:09:00,116 you're going to all of these different 17706 11:08:58,000 --> 11:09:01,840 addresses in the swap function to make 17707 11:09:00,116 --> 11:09:03,480 these changes so if we advance now to 17708 11:09:01,840 --> 11:09:05,960 the pictoral version of this here's the 17709 11:09:03,480 --> 11:09:07,880 same story as before with Maine and X 17710 11:09:05,960 --> 11:09:10,160 and Y are 1 and two respectively when 17711 11:09:07,880 --> 11:09:12,800 swap gets called now notice and I'll do 17712 11:09:10,160 --> 11:09:14,960 it with arrows here a is effectively 17713 11:09:12,800 --> 11:09:16,916 pointing to X B is effectively pointing 17714 11:09:14,960 --> 11:09:18,320 to Y if we really get into the weeds 17715 11:09:16,916 --> 11:09:20,116 these are actually like addresses but 17716 11:09:18,320 --> 11:09:22,000 who cares about the specifics it's 17717 11:09:20,116 --> 11:09:25,240 really just the concept here so now what 17718 11:09:22,000 --> 11:09:27,680 happens in temp gets star a star a means 17719 11:09:25,240 --> 11:09:29,040 start at a and go there Follow the arrow 17720 11:09:27,680 --> 11:09:31,080 if you will sort of shoots in ladder 17721 11:09:29,040 --> 11:09:34,240 Style and then that's one so we put one 17722 11:09:31,080 --> 11:09:35,960 and 10th all right star a equals star B 17723 11:09:34,240 --> 11:09:38,400 so let's do it from right to left star b 17724 11:09:35,960 --> 11:09:40,560 means Follow the arrow it's two and then 17725 11:09:38,400 --> 11:09:42,680 what do you do Follow the arrow it's now 17726 11:09:40,560 --> 11:09:45,000 two because you copy one to the other 17727 11:09:42,680 --> 11:09:47,480 from right to left and then lastly star 17728 11:09:45,000 --> 11:09:50,480 b gets temp so start at B go to B and 17729 11:09:47,480 --> 11:09:52,360 now store whatever the value is in temp 17730 11:09:50,480 --> 11:09:54,200 so just by having this basic new syntax 17731 11:09:52,360 --> 11:09:56,160 of like ampersands and stars and so 17732 11:09:54,200 --> 11:09:57,756 forth we can actually now go to places 17733 11:09:56,160 --> 11:10:00,080 and circumvent what is otherwise a 17734 11:09:57,756 --> 11:10:02,320 feature of C that these variables are 17735 11:10:00,080 --> 11:10:04,756 locally scoped but you can still access 17736 11:10:02,320 --> 11:10:05,880 things in other functions as well so 17737 11:10:04,756 --> 11:10:08,000 thank you so much for helping step 17738 11:10:05,880 --> 11:10:10,116 through this so we now have a 17739 11:10:08,000 --> 11:10:12,480 application of this that explains why 17740 11:10:10,116 --> 11:10:14,160 now in this version of the C code this 17741 11:10:12,480 --> 11:10:17,276 would actually now work so in fact let 17742 11:10:14,160 --> 11:10:19,040 me go back to my swap code here and let 17743 11:10:17,276 --> 11:10:21,720 me change the function ever so slightly 17744 11:10:19,040 --> 11:10:24,880 in vs code so let me scroll down leaving 17745 11:10:21,720 --> 11:10:27,756 main the same and let me change swaps 17746 11:10:24,880 --> 11:10:30,436 prototype to taking in addresses let me 17747 11:10:27,756 --> 11:10:33,200 go to a here let me go to a here let me 17748 11:10:30,436 --> 11:10:35,276 go to B here and let me go to B here as 17749 11:10:33,200 --> 11:10:37,116 well but nothing else changes this 17750 11:10:35,276 --> 11:10:39,436 change here in particular is enough of a 17751 11:10:37,116 --> 11:10:42,680 clue to see that means when you call 17752 11:10:39,436 --> 11:10:45,720 Swap and pass into values I'm expecting 17753 11:10:42,680 --> 11:10:47,560 addresses now not integers but now that 17754 11:10:45,720 --> 11:10:51,960 I've made this change I do need to go up 17755 11:10:47,560 --> 11:10:53,720 to Main and make one change does anyone 17756 11:10:51,960 --> 11:10:57,400 have the intuition for what now need 17757 11:10:53,720 --> 11:10:59,400 change in main so that I pass in X and Y 17758 11:10:57,400 --> 11:11:02,930 by reference that is by address rather 17759 11:10:59,400 --> 11:11:05,596 than than by value or copy oh yeah and 17760 11:11:02,930 --> 11:11:08,400 [Music] 17761 11:11:05,596 --> 11:11:10,596 back so close so on the swap line it's 17762 11:11:08,400 --> 11:11:13,276 not star that I want in front of the X 17763 11:11:10,596 --> 11:11:13,276 and the Y it's 17764 11:11:13,640 --> 11:11:18,640 instead what's the other one it's the 17765 11:11:16,596 --> 11:11:20,640 Amper sand why because if I want to 17766 11:11:18,640 --> 11:11:21,840 enable swap to go somewhere just like 17767 11:11:20,640 --> 11:11:24,160 Carter and I played this game with the 17768 11:11:21,840 --> 11:11:26,480 mailboxes I need to inform swap of the 17769 11:11:24,160 --> 11:11:28,520 address of X and the address of Y and 17770 11:11:26,480 --> 11:11:30,480 again per the beginning of today's class 17771 11:11:28,520 --> 11:11:32,000 Ampersand is the text via which we do 17772 11:11:30,480 --> 11:11:33,960 that so I add an ampersand here to get 17773 11:11:32,000 --> 11:11:36,436 the address of X Ampersand here to get 17774 11:11:33,960 --> 11:11:38,200 the address of Y and now this code lines 17775 11:11:36,436 --> 11:11:40,080 up with the picture that Carter just 17776 11:11:38,200 --> 11:11:43,400 helped us walk through and so when I run 17777 11:11:40,080 --> 11:11:46,320 make swap here I have a mistake oh what 17778 11:11:43,400 --> 11:11:48,960 did I do wrong not intentional but I 17779 11:11:46,320 --> 11:11:52,000 guess worth pointing out I screwed up 17780 11:11:48,960 --> 11:11:55,560 here it doesn't like Ampersand 17781 11:11:52,000 --> 11:11:57,360 X because of something on line three 17782 11:11:55,560 --> 11:12:01,436 which is way early in the code what did 17783 11:11:57,360 --> 11:12:01,436 I screw up yeah in the middle 17784 11:12:01,916 --> 11:12:05,436 yeah so this is why we you should not 17785 11:12:04,000 --> 11:12:06,960 copy paste even though it's necessary 17786 11:12:05,436 --> 11:12:08,960 for things like function prototypes if I 17787 11:12:06,960 --> 11:12:10,840 change swap at the bottom I need to 17788 11:12:08,960 --> 11:12:12,436 change its prototype so let me add the 17789 11:12:10,840 --> 11:12:14,640 star there add the star there or just 17790 11:12:12,436 --> 11:12:16,756 recopy paste it at the top of the file 17791 11:12:14,640 --> 11:12:19,320 now let me do make swap again let me now 17792 11:12:16,756 --> 11:12:23,640 do do/ Swap and I should now see X is 17793 11:12:19,320 --> 11:12:25,916 one y is 2 and hopefully X is 2 Y is 1 17794 11:12:23,640 --> 11:12:27,520 which I now do so the logic is the same 17795 11:12:25,916 --> 11:12:29,640 the algorithm is the same all the weak 17796 11:12:27,520 --> 11:12:31,116 zero stuff is the same except now and 17797 11:12:29,640 --> 11:12:33,720 four you just have a bit more 17798 11:12:31,116 --> 11:12:35,560 expressiveness via which you can tell 17799 11:12:33,720 --> 11:12:39,720 the computer exactly what you want to 17800 11:12:35,560 --> 11:12:42,400 manipulate and how any questions then on 17801 11:12:39,720 --> 11:12:45,840 this technique 17802 11:12:42,400 --> 11:12:47,116 here no all right well when we fix this 17803 11:12:45,840 --> 11:12:48,596 there's still going to be problems and 17804 11:12:47,116 --> 11:12:50,200 just so you've seen some terms of art 17805 11:12:48,596 --> 11:12:51,840 here this is bad whenever you have two 17806 11:12:50,200 --> 11:12:53,800 arrows pointing at one another certainly 17807 11:12:51,840 --> 11:12:55,200 if you might use and reuse more and more 17808 11:12:53,800 --> 11:12:56,720 memory and it turns out there's some 17809 11:12:55,200 --> 11:12:57,680 terms of art that might suddenly now 17810 11:12:56,720 --> 11:13:00,320 make sense especially if you've 17811 11:12:57,680 --> 11:13:02,400 programmed before bad things Can Happen 17812 11:13:00,320 --> 11:13:03,596 by this design but there's really only 17813 11:13:02,400 --> 11:13:05,080 this kind of design because it's a 17814 11:13:03,596 --> 11:13:06,320 finite amount of memory so at some point 17815 11:13:05,080 --> 11:13:08,240 bad things are going to happen no matter 17816 11:13:06,320 --> 11:13:09,680 what if a computer runs out of memory so 17817 11:13:08,240 --> 11:13:11,400 it's not that this was a poor decision 17818 11:13:09,680 --> 11:13:13,560 it's just sort of a necessary one given 17819 11:13:11,400 --> 11:13:15,720 finite amounts of memory in a computer 17820 11:13:13,560 --> 11:13:17,480 but a heap overflow so to speak is when 17821 11:13:15,720 --> 11:13:19,720 you actually overflow the Heap and touch 17822 11:13:17,480 --> 11:13:21,360 memory that you shouldn't up there stack 17823 11:13:19,720 --> 11:13:22,960 Overflow is when you somehow overflow 17824 11:13:21,360 --> 11:13:24,880 the stack and touch memory that you 17825 11:13:22,960 --> 11:13:26,320 shouldn't down there so with that said 17826 11:13:24,880 --> 11:13:28,000 these are really just problems that can 17827 11:13:26,320 --> 11:13:29,840 happen and there specific incarnations 17828 11:13:28,000 --> 11:13:32,000 of what are generally called buffer 17829 11:13:29,840 --> 11:13:33,640 overflows a buffer like in the YouTube 17830 11:13:32,000 --> 11:13:35,080 sense is just like a chunk of memory 17831 11:13:33,640 --> 11:13:37,080 that in the case of YouTube stores like 17832 11:13:35,080 --> 11:13:38,840 the next few seconds or minutes of video 17833 11:13:37,080 --> 11:13:40,520 but generally speaking a buffer is just 17834 11:13:38,840 --> 11:13:42,640 a chunk of memory that the computer is 17835 11:13:40,520 --> 11:13:44,756 using for some purpose be it the stack 17836 11:13:42,640 --> 11:13:46,960 be it the Heap be it an array in the 17837 11:13:44,756 --> 11:13:48,880 computer and so buffer overflows are 17838 11:13:46,960 --> 11:13:52,200 what happens when you just have logical 17839 11:13:48,880 --> 11:13:53,960 bugs in your code but with these 17840 11:13:52,200 --> 11:13:56,240 Primitives now in mind we wanted to 17841 11:13:53,960 --> 11:13:58,000 conclude with a final Revelations and 17842 11:13:56,240 --> 11:13:59,960 that's how some functions like these 17843 11:13:58,000 --> 11:14:01,720 here work the other thing in the cs50 17844 11:13:59,960 --> 11:14:03,680 library besides the type def for quote 17845 11:14:01,720 --> 11:14:04,916 unquote string is of course all of these 17846 11:14:03,680 --> 11:14:07,360 functions and we give you these 17847 11:14:04,916 --> 11:14:09,720 functions because honestly in C it is 17848 11:14:07,360 --> 11:14:12,436 hard it's annoying it's painful it's 17849 11:14:09,720 --> 11:14:14,400 difficult to get user input correctly 17850 11:14:12,436 --> 11:14:16,276 it's very easy when you don't know how 17851 11:14:14,400 --> 11:14:18,360 much how much the humans going to type 17852 11:14:16,276 --> 11:14:20,320 to write buggy code when it comes to it 17853 11:14:18,360 --> 11:14:21,960 and indeed it's really hard to store it 17854 11:14:20,320 --> 11:14:24,200 correctly without accidentally having 17855 11:14:21,960 --> 11:14:26,596 some kind of buffer overflow so for 17856 11:14:24,200 --> 11:14:27,640 instance let me show you a program here 17857 11:14:26,596 --> 11:14:29,360 I'm going to go ahead and write this one 17858 11:14:27,640 --> 11:14:31,720 from scratch so let me go ahead and open 17859 11:14:29,360 --> 11:14:33,360 a file called get. C where I'm going to 17860 11:14:31,720 --> 11:14:35,400 go ahead and mimic the idea of getting 17861 11:14:33,360 --> 11:14:37,080 integers manually without the cs50 17862 11:14:35,400 --> 11:14:39,840 library so I'm going to include standard 17863 11:14:37,080 --> 11:14:41,800 io. only I'm going to Define main as not 17864 11:14:39,840 --> 11:14:43,276 taking any command line arguments and 17865 11:14:41,800 --> 11:14:46,040 then I'm going to do something like this 17866 11:14:43,276 --> 11:14:47,640 give me a variable x with no value yet 17867 11:14:46,040 --> 11:14:49,520 and normally I would do something like 17868 11:14:47,640 --> 11:14:51,520 get int but let me take that away no 17869 11:14:49,520 --> 11:14:54,320 more training wheels for get int either 17870 11:14:51,520 --> 11:14:57,756 so let me just Define the int X let me 17871 11:14:54,320 --> 11:14:59,756 then just print out something like uh a 17872 11:14:57,756 --> 11:15:01,276 prompt and I'll just do x c just to make 17873 11:14:59,756 --> 11:15:03,400 it obvious to the human what we're 17874 11:15:01,276 --> 11:15:05,916 waiting for and now I'm going to use a 17875 11:15:03,400 --> 11:15:07,960 builtin c function to get user input I'm 17876 11:15:05,916 --> 11:15:09,640 going to call a function called scan F 17877 11:15:07,960 --> 11:15:11,800 which sort of scans the user's keyboard 17878 11:15:09,640 --> 11:15:13,436 for input I'm going to scan it for an 17879 11:15:11,800 --> 11:15:16,000 integer so just like print F I'm going 17880 11:15:13,436 --> 11:15:18,436 to use percent I because I expect an INT 17881 11:15:16,000 --> 11:15:20,880 and then I want to tell scanf where to 17882 11:15:18,436 --> 11:15:23,400 put the human's integer from the 17883 11:15:20,880 --> 11:15:25,800 keyboard it is not correct though to say 17884 11:15:23,400 --> 11:15:28,400 x because if I say x I run into the same 17885 11:15:25,800 --> 11:15:30,160 swap problem scanf no function can 17886 11:15:28,400 --> 11:15:32,040 change the value of X unless I pass it 17887 11:15:30,160 --> 11:15:34,960 not by value but 17888 11:15:32,040 --> 11:15:37,520 by reference so we're back to our 17889 11:15:34,960 --> 11:15:40,560 Ampersand friend and now it has like a a 17890 11:15:37,520 --> 11:15:42,596 treasure map to the actual location of X 17891 11:15:40,560 --> 11:15:43,880 and can therefore change it and so now 17892 11:15:42,596 --> 11:15:45,640 at the very end of this program let me 17893 11:15:43,880 --> 11:15:49,000 do something simple like let's just go 17894 11:15:45,640 --> 11:15:52,200 ahead and print out with print F uh the 17895 11:15:49,000 --> 11:15:54,756 value of x using percent I as always 17896 11:15:52,200 --> 11:15:56,480 plugging in X not Ampersand X this is 17897 11:15:54,756 --> 11:15:59,240 now week one stuff I want to print the 17898 11:15:56,480 --> 11:16:00,880 actual integer value of x so the only 17899 11:15:59,240 --> 11:16:03,200 change here is that instead of using get 17900 11:16:00,880 --> 11:16:06,200 in I'm now using this new function that 17901 11:16:03,200 --> 11:16:09,320 as of today exists called scanf so let 17902 11:16:06,200 --> 11:16:11,916 me go ahead and run get make get to 17903 11:16:09,320 --> 11:16:15,116 create this program doget and let's go 17904 11:16:11,916 --> 11:16:18,080 ahead and type in a value for x 50 enter 17905 11:16:15,116 --> 11:16:20,240 and it just works so it turns out get in 17906 11:16:18,080 --> 11:16:21,916 is pretty simple to implement however 17907 11:16:20,240 --> 11:16:24,360 notice what does not work if I type in 17908 11:16:21,916 --> 11:16:27,480 cat for instance cat gets converted to 17909 11:16:24,360 --> 11:16:29,840 zero and meanwhile get in recall will 17910 11:16:27,480 --> 11:16:31,720 reprompt the user if a human not type an 17911 11:16:29,840 --> 11:16:33,160 actual integer you get automatically 17912 11:16:31,720 --> 11:16:35,360 reprompt so that's one of the features 17913 11:16:33,160 --> 11:16:37,160 we for cs50 added to get in just to make 17914 11:16:35,360 --> 11:16:38,960 your programs more user friendly but 17915 11:16:37,160 --> 11:16:40,720 otherwise get in is pretty 17916 11:16:38,960 --> 11:16:43,360 straightforward to re-implement using 17917 11:16:40,720 --> 11:16:45,276 scanf unfortunately that's not true for 17918 11:16:43,360 --> 11:16:47,400 Strings because how do you know when you 17919 11:16:45,276 --> 11:16:49,040 write your code what word the human's 17920 11:16:47,400 --> 11:16:51,800 going to eventually type in how long 17921 11:16:49,040 --> 11:16:53,520 they're greeting like high is if it's uh 17922 11:16:51,800 --> 11:16:54,840 if their name is David or Carter or 17923 11:16:53,520 --> 11:16:57,240 anything else you just don't know in 17924 11:16:54,840 --> 11:16:59,680 advance how much memory you need so how 17925 11:16:57,240 --> 11:17:02,436 might we do this in with string well let 17926 11:16:59,680 --> 11:17:03,916 me go ahead and declare a string s 17927 11:17:02,436 --> 11:17:06,320 although you know what there's no cs50 17928 11:17:03,916 --> 11:17:09,040 Library so we do Char star s today 17929 11:17:06,320 --> 11:17:12,276 instead and that gives me not a string 17930 11:17:09,040 --> 11:17:14,360 per se but a pointer that will Point 17931 11:17:12,276 --> 11:17:16,320 presumably to a string ideally I would 17932 11:17:14,360 --> 11:17:18,400 use this get string but again we've 17933 11:17:16,320 --> 11:17:20,640 taken that training wheel away so now 17934 11:17:18,400 --> 11:17:22,360 that I have a pointer s suppose I prompt 17935 11:17:20,640 --> 11:17:24,916 the human for a value for S just like 17936 11:17:22,360 --> 11:17:27,720 before let me use scan F now and tell 17937 11:17:24,916 --> 11:17:29,880 the user that I expect to read a string 17938 11:17:27,720 --> 11:17:33,560 percent s from the keyboard 17939 11:17:29,880 --> 11:17:35,880 uh and store it in s now this is subtle 17940 11:17:33,560 --> 11:17:38,400 I don't technically need an Amper sand 17941 11:17:35,880 --> 11:17:40,200 here even though I did for an INT and I 17942 11:17:38,400 --> 11:17:42,640 would for a float and a double and a 17943 11:17:40,200 --> 11:17:44,756 long and a bull and a Char why do I not 17944 11:17:42,640 --> 11:17:47,320 need an Amper sand in this story to pass 17945 11:17:44,756 --> 11:17:50,880 by 17946 11:17:47,320 --> 11:17:52,720 reference because s is it's already an 17947 11:17:50,880 --> 11:17:55,400 address again strings are just special 17948 11:17:52,720 --> 11:17:57,080 strings now are always addresses so you 17949 11:17:55,400 --> 11:17:58,640 don't need to additionally add an Amper 17950 11:17:57,080 --> 11:18:00,480 sand here that's the only subtle 17951 11:17:58,640 --> 11:18:01,840 difference here but now if I go ahead 17952 11:18:00,480 --> 11:18:04,596 and print out at the very end what the 17953 11:18:01,840 --> 11:18:07,400 value of s is using percent S as before 17954 11:18:04,596 --> 11:18:09,640 this program looks like it's almost the 17955 11:18:07,400 --> 11:18:13,400 same as the int version but let's do 17956 11:18:09,640 --> 11:18:14,880 make get and okay so this is not good 17957 11:18:13,400 --> 11:18:16,400 all right so it doesn't like an 17958 11:18:14,880 --> 11:18:18,276 initialized value so let me make it 17959 11:18:16,400 --> 11:18:19,520 happy I said earlier to always 17960 11:18:18,276 --> 11:18:21,040 initialize my variable so let's 17961 11:18:19,520 --> 11:18:22,436 initialize it to null so that at least 17962 11:18:21,040 --> 11:18:25,560 something is there that's your good 17963 11:18:22,436 --> 11:18:27,320 default value nowadays now if I do doget 17964 11:18:25,560 --> 11:18:30,116 now we're good and let me type in 17965 11:18:27,320 --> 11:18:32,596 something like cat 17966 11:18:30,116 --> 11:18:33,956 okay cat is not X well let me try 17967 11:18:32,596 --> 11:18:37,160 another word maybe it's just cat is 17968 11:18:33,956 --> 11:18:38,880 wrong dog okay let me try David it just 17969 11:18:37,160 --> 11:18:42,360 doesn't seem to be working moreover it's 17970 11:18:38,880 --> 11:18:45,520 printing it as a zero what logically 17971 11:18:42,360 --> 11:18:47,276 though is the bug here scanf worked a 17972 11:18:45,520 --> 11:18:48,800 moment ago for integers but it's not 17973 11:18:47,276 --> 11:18:51,160 working for Strings and it seems to be 17974 11:18:48,800 --> 11:18:52,040 forgetting C A it's forgetting dog it's 17975 11:18:51,160 --> 11:18:53,880 forgetting 17976 11:18:52,040 --> 11:18:56,916 David 17977 11:18:53,880 --> 11:18:59,276 why what's Happening 17978 11:18:56,916 --> 11:19:00,916 Here think back to our yellow pictures 17979 11:18:59,276 --> 11:19:03,596 of 17980 11:19:00,916 --> 11:19:06,080 memory when I 17981 11:19:03,596 --> 11:19:08,320 yeah it might be reading just the null 17982 11:19:06,080 --> 11:19:10,480 itself because s is being initialized to 17983 11:19:08,320 --> 11:19:12,680 null and what step have I forgotten from 17984 11:19:10,480 --> 11:19:15,520 just like a few minutes ago what did I 17985 11:19:12,680 --> 11:19:18,680 not actually request of the computer 17986 11:19:15,520 --> 11:19:21,400 like actual memory to store the C A the 17987 11:19:18,680 --> 11:19:23,436 dog the d v d right there's nowhere have 17988 11:19:21,400 --> 11:19:26,160 I asked the computer for some amount of 17989 11:19:23,436 --> 11:19:28,480 memory and so technically it might be 17990 11:19:26,160 --> 11:19:30,640 reading it into some garbage location 17991 11:19:28,480 --> 11:19:32,720 and that's really the problem here S is 17992 11:19:30,640 --> 11:19:34,640 initialized to null now and so in fact 17993 11:19:32,720 --> 11:19:36,160 it is printing zero as null but I'm not 17994 11:19:34,640 --> 11:19:38,596 seeing any of the other letters because 17995 11:19:36,160 --> 11:19:40,840 there was nowhere to put them C A do D 17996 11:19:38,596 --> 11:19:42,800 ID because I didn't ask for three bytes 17997 11:19:40,840 --> 11:19:44,436 four bytes five bytes 100 bytes there's 17998 11:19:42,800 --> 11:19:46,800 no use of malok there's no use of an 17999 11:19:44,436 --> 11:19:49,360 array there's no memory allocated for 18000 11:19:46,800 --> 11:19:51,116 anything other than the pointer itself 18001 11:19:49,360 --> 11:19:53,200 and this is where honestly life gets 18002 11:19:51,116 --> 11:19:54,956 hard with scanf like I could solve this 18003 11:19:53,200 --> 11:19:56,880 problem in a couple of ways let me go 18004 11:19:54,956 --> 11:19:59,040 ahead and do this instead of declaring s 18005 11:19:56,880 --> 11:20:02,800 to be a pointer let me declare s to 18006 11:19:59,040 --> 11:20:04,880 actually be a an array of four chars and 18007 11:20:02,800 --> 11:20:08,596 now let me go ahead and recompile the 18008 11:20:04,880 --> 11:20:12,116 code so make get doget and I'll type in 18009 11:20:08,596 --> 11:20:14,116 cat now that now works why well I'm 18010 11:20:12,116 --> 11:20:16,160 allocating an explicit array of size 18011 11:20:14,116 --> 11:20:18,680 four enough for a one two three letters 18012 11:20:16,160 --> 11:20:20,800 plus a null character here's where to 18013 11:20:18,680 --> 11:20:22,596 someone's question earlier it turns out 18014 11:20:20,800 --> 11:20:25,400 then in some context you can treat 18015 11:20:22,596 --> 11:20:26,720 arrays as though they are Pointers 18016 11:20:25,400 --> 11:20:28,800 themselves see we'll sort of do the 18017 11:20:26,720 --> 11:20:30,956 conversion for you but for now just 18018 11:20:28,800 --> 11:20:33,720 assume that s is just an array of size 18019 11:20:30,956 --> 11:20:35,400 four and if you pass it into to scanf 18020 11:20:33,720 --> 11:20:37,320 that's like a treasure map that leads to 18021 11:20:35,400 --> 11:20:41,640 those four bytes so scanf can now 18022 11:20:37,320 --> 11:20:43,800 successfully fill it with c a d o but 18023 11:20:41,640 --> 11:20:47,276 but let's try this again let's type in 18024 11:20:43,800 --> 11:20:49,080 David and here okay we got lucky but I 18025 11:20:47,276 --> 11:20:51,116 technically touched memory that I should 18026 11:20:49,080 --> 11:20:52,360 not and in fact if I typed in a long 18027 11:20:51,116 --> 11:20:54,800 enough string and I don't think I could 18028 11:20:52,360 --> 11:20:57,000 do it very easily like by without typing 18029 11:20:54,800 --> 11:20:59,200 this thousands or hundreds of times 18030 11:20:57,000 --> 11:21:01,596 still okay but you'll notice that it's 18031 11:20:59,200 --> 11:21:03,080 forgotten the rest of it now right so 18032 11:21:01,596 --> 11:21:04,240 somewhere we went beyond the boundary of 18033 11:21:03,080 --> 11:21:06,240 the array and we just don't have enough 18034 11:21:04,240 --> 11:21:07,436 storage space for that entire thing so 18035 11:21:06,240 --> 11:21:09,160 what do you do in your program if you 18036 11:21:07,436 --> 11:21:10,956 don't know how long the person's name or 18037 11:21:09,160 --> 11:21:14,800 the the the animal name is going to be 18038 11:21:10,956 --> 11:21:16,080 what do you do 40 400 4,000 40,000 like 18039 11:21:14,800 --> 11:21:17,956 at some point you have to draw a line in 18040 11:21:16,080 --> 11:21:20,200 the sand and that's why like getting 18041 11:21:17,956 --> 11:21:22,880 user input is so annoying in a language 18042 11:21:20,200 --> 11:21:25,320 like C and that's why get string exists 18043 11:21:22,880 --> 11:21:27,360 what we do if you're curious is we look 18044 11:21:25,320 --> 11:21:29,560 at the user's input and we take baby 18045 11:21:27,360 --> 11:21:31,200 steps we look at it one character at a 18046 11:21:29,560 --> 11:21:33,240 time and every time we see another 18047 11:21:31,200 --> 11:21:34,800 character we actually call malok again 18048 11:21:33,240 --> 11:21:36,320 and say no I need more than one bite I 18049 11:21:34,800 --> 11:21:38,080 need two oh wait they typed in three 18050 11:21:36,320 --> 11:21:39,680 letters I need three instead of two oh I 18051 11:21:38,080 --> 11:21:41,640 need four instead of two and we have 18052 11:21:39,680 --> 11:21:43,480 this crazy loop essentially that keeps 18053 11:21:41,640 --> 11:21:45,200 asking for more and more memory but by 18054 11:21:43,480 --> 11:21:47,116 taking baby steps and honestly if you 18055 11:21:45,200 --> 11:21:48,840 all had to do that in week one my God 18056 11:21:47,116 --> 11:21:50,800 like we couldn't even write hello world 18057 11:21:48,840 --> 11:21:52,640 anymore and so that's why these training 18058 11:21:50,800 --> 11:21:54,276 wheels exist at least early on and 18059 11:21:52,640 --> 11:21:56,916 that's why in higher level languages 18060 11:21:54,276 --> 11:21:59,000 like in uh python you don't have to do 18061 11:21:56,916 --> 11:22:01,560 this at all it just works as you'd 18062 11:21:59,000 --> 11:22:03,320 expect so what more can we do well 18063 11:22:01,560 --> 11:22:05,756 you'll see in problems set for this 18064 11:22:03,320 --> 11:22:08,520 coming week if I open up an example like 18065 11:22:05,756 --> 11:22:10,720 this phone book. C you'll see that you 18066 11:22:08,520 --> 11:22:12,480 can manipulate files now that you have a 18067 11:22:10,720 --> 11:22:15,240 vocabulary for pointers it's going to be 18068 11:22:12,480 --> 11:22:17,160 new quickly but here we have an example 18069 11:22:15,240 --> 11:22:19,040 of how I have a program using some 18070 11:22:17,160 --> 11:22:20,596 familiar libraries here but as I claim 18071 11:22:19,040 --> 11:22:22,680 in my comment this saves names and 18072 11:22:20,596 --> 11:22:24,680 numbers to a CSV file all of my examples 18073 11:22:22,680 --> 11:22:26,240 thus far I type in some words I type in 18074 11:22:24,680 --> 11:22:27,596 some names and some phone numbers and 18075 11:22:26,240 --> 11:22:29,276 they disappear because we only store 18076 11:22:27,596 --> 11:22:31,756 them in memory but if you want to store 18077 11:22:29,276 --> 11:22:32,840 data in like a CSV file comma separated 18078 11:22:31,756 --> 11:22:35,040 values which is like a simple 18079 11:22:32,840 --> 11:22:36,800 spreadsheet like Excel and apple numbers 18080 11:22:35,040 --> 11:22:38,276 and Google Sheets can open you can 18081 11:22:36,800 --> 11:22:40,756 actually do this yourself so just as a 18082 11:22:38,276 --> 11:22:42,756 teaser for this week here on line n I'm 18083 11:22:40,756 --> 11:22:45,040 using a new data type not a cs50 thing 18084 11:22:42,756 --> 11:22:46,720 this is a c thing called file but if you 18085 11:22:45,040 --> 11:22:49,276 want to manipulate files you need to use 18086 11:22:46,720 --> 11:22:51,360 addresses that is pointers so here is me 18087 11:22:49,276 --> 11:22:53,436 creating a variable called file that's 18088 11:22:51,360 --> 11:22:55,276 going to point to an actual file on the 18089 11:22:53,436 --> 11:22:57,560 hard drive on the server or your Mac or 18090 11:22:55,276 --> 11:22:59,560 PC fop is going to be a new function 18091 11:22:57,560 --> 11:23:01,680 you'll use that will open open a file 18092 11:22:59,560 --> 11:23:04,000 and it will return effectively a pointer 18093 11:23:01,680 --> 11:23:05,956 there to in memory the file name I want 18094 11:23:04,000 --> 11:23:08,160 to open is phonebook .csv and in this 18095 11:23:05,956 --> 11:23:09,756 example it's going to be uh a pen mode 18096 11:23:08,160 --> 11:23:11,840 it will keep allowing me to add more and 18097 11:23:09,756 --> 11:23:13,360 more names and numbers to this file 18098 11:23:11,840 --> 11:23:14,640 here's some old get string stuff because 18099 11:23:13,360 --> 11:23:16,916 I'm not going to reinvent get string 18100 11:23:14,640 --> 11:23:18,756 with scan F but down here is a slightly 18101 11:23:16,916 --> 11:23:20,480 new function it's not print F but 18102 11:23:18,756 --> 11:23:22,240 fprintf and it turns out it's very easy 18103 11:23:20,480 --> 11:23:24,240 to print things not to the screen but to 18104 11:23:22,240 --> 11:23:26,320 a file with fprintf and it takes an 18105 11:23:24,240 --> 11:23:28,200 additional argument instead of starting 18106 11:23:26,320 --> 11:23:30,360 with the quoted string you'll have to 18107 11:23:28,200 --> 11:23:32,116 like say what file you want to write to 18108 11:23:30,360 --> 11:23:34,840 and fr print F we'll figure out how to 18109 11:23:32,116 --> 11:23:36,840 get the uh the bits into that file 18110 11:23:34,840 --> 11:23:38,956 passing in something like name comma 18111 11:23:36,840 --> 11:23:41,956 number so if I run this somewhat quickly 18112 11:23:38,956 --> 11:23:45,680 here let me do this let me pre-create a 18113 11:23:41,956 --> 11:23:47,480 file called uh phonebook do CSV and in 18114 11:23:45,680 --> 11:23:49,756 phone book. CSV I'm going to create a 18115 11:23:47,480 --> 11:23:51,116 temporary row here name comma number 18116 11:23:49,756 --> 11:23:53,800 just so that there's something in this 18117 11:23:51,116 --> 11:23:56,756 file and now let me go ahead and do this 18118 11:23:53,800 --> 11:23:58,560 and split my screen here if I have 18119 11:23:56,756 --> 11:24:00,680 phonebook.com 18120 11:23:58,560 --> 11:24:03,756 and phone book. C on the left let me 18121 11:24:00,680 --> 11:24:06,160 compile make phonebook which is the C 18122 11:24:03,756 --> 11:24:07,596 version phonebook and now I'm prompted 18123 11:24:06,160 --> 11:24:10,916 for a name and a number so I'll type in 18124 11:24:07,596 --> 11:24:16,560 David and then for instance + one 18125 11:24:10,916 --> 11:24:18,640 949 uh what is it 468 275 o enter oh 18126 11:24:16,560 --> 11:24:20,956 damn it bug uh pretend that didn't 18127 11:24:18,640 --> 11:24:23,436 happen I forgot to hit enter in the file 18128 11:24:20,956 --> 11:24:29,436 so let's do this again if I run the 18129 11:24:23,436 --> 11:24:34,000 program Again David and plus one 94 9 18130 11:24:29,436 --> 11:24:37,916 468 2750 enter it's been saved now to 18131 11:24:34,000 --> 11:24:42,200 the file and if I close this file and I 18132 11:24:37,916 --> 11:24:42,200 reopen code of phonebook.com 18133 11:24:48,756 --> 11:24:53,640 or the like and I've actually created an 18134 11:24:51,640 --> 11:24:54,916 actual CSV file uh if you're smiling 18135 11:24:53,640 --> 11:24:56,276 because I keep repeating my phone number 18136 11:24:54,916 --> 11:24:58,000 out loud I would encourage you to call 18137 11:24:56,276 --> 11:24:59,720 or text that number sometime it might 18138 11:24:58,000 --> 11:25:02,040 very well well be a an Easter egg of 18139 11:24:59,720 --> 11:25:04,596 source but via these functions here do 18140 11:25:02,040 --> 11:25:07,040 we have now the ability to write files 18141 11:25:04,596 --> 11:25:08,800 uh input and output and among the goals 18142 11:25:07,040 --> 11:25:10,436 then for this week as we'll see are to 18143 11:25:08,800 --> 11:25:12,116 actually play with images in the spirit 18144 11:25:10,436 --> 11:25:13,596 of something like Instagram filters or 18145 11:25:12,116 --> 11:25:16,200 the like and we'll introduce you for 18146 11:25:13,596 --> 11:25:17,720 instance to a file format called bmps 18147 11:25:16,200 --> 11:25:20,320 which to come full circle to the start 18148 11:25:17,720 --> 11:25:21,720 of class are just maps of bits but more 18149 11:25:20,320 --> 11:25:23,916 than just single bits for white and 18150 11:25:21,720 --> 11:25:25,560 black but rather colorful patterns as 18151 11:25:23,916 --> 11:25:26,916 well and we'll give you images like this 18152 11:25:25,560 --> 11:25:28,840 of the week's Bridge here across the 18153 11:25:26,916 --> 11:25:30,880 river at Harvard and you've run 18154 11:25:28,840 --> 11:25:33,080 after writing your own code in C and 18155 11:25:30,880 --> 11:25:34,520 understanding how the data stored in the 18156 11:25:33,080 --> 11:25:36,276 computer's memory you'll be able to 18157 11:25:34,520 --> 11:25:38,596 apply your own Instagram like filters to 18158 11:25:36,276 --> 11:25:40,596 make things uh grayscale instead or 18159 11:25:38,596 --> 11:25:41,956 sepia in this case you can even flip the 18160 11:25:40,596 --> 11:25:44,520 bits around so that the thing is a 18161 11:25:41,956 --> 11:25:45,956 mirror image you can blur things further 18162 11:25:44,520 --> 11:25:47,680 or if you really are feeling more 18163 11:25:45,956 --> 11:25:50,160 comfortable you can even write code that 18164 11:25:47,680 --> 11:25:52,276 finds the edges of the image and creates 18165 11:25:50,160 --> 11:25:54,560 works of art like these so all that in 18166 11:25:52,276 --> 11:25:56,840 more in problem set four we will see you 18167 11:25:54,560 --> 11:25:56,840 next 18168 11:25:57,200 --> 11:26:00,200 time 18169 11:26:01,690 --> 11:26:05,599 [Music] 18170 11:26:11,580 --> 11:26:19,910 [Music] 18171 11:26:36,200 --> 11:26:49,169 [Music] 18172 11:26:51,740 --> 11:26:54,839 [Music] 18173 11:26:59,340 --> 11:27:02,410 [Music] 18174 11:27:08,690 --> 11:27:11,800 [Music] 18175 11:27:18,630 --> 11:27:31,989 [Music] 18176 11:27:46,116 --> 11:27:51,720 all right this is cs50 and this is week 18177 11:27:49,200 --> 11:27:54,596 five which is going to be our last week 18178 11:27:51,720 --> 11:27:57,000 in see uh but what that means is that 18179 11:27:54,596 --> 11:27:58,840 we'll have 18180 11:27:57,000 --> 11:28:00,840 okay 18181 11:27:58,840 --> 11:28:02,276 but with this week with last week and 18182 11:28:00,840 --> 11:28:04,320 really all of the weeks prior have you 18183 11:28:02,276 --> 11:28:05,680 been hopefully acquiring if slowly and 18184 11:28:04,320 --> 11:28:07,200 with some challenge like some 18185 11:28:05,680 --> 11:28:08,840 fundamental building blocks that are 18186 11:28:07,200 --> 11:28:10,596 still going to underly everything we 18187 11:28:08,840 --> 11:28:12,800 continue doing in the semester even as 18188 11:28:10,596 --> 11:28:14,116 we transition to so-called higher level 18189 11:28:12,800 --> 11:28:16,200 languages next week indeed we'll 18190 11:28:14,116 --> 11:28:18,200 introduce python a very popular language 18191 11:28:16,200 --> 11:28:20,000 that does not have pointers does not 18192 11:28:18,200 --> 11:28:21,360 have memory management at this very low 18193 11:28:20,000 --> 11:28:23,480 level but that's really just because 18194 11:28:21,360 --> 11:28:24,840 someone else wrote the code that will do 18195 11:28:23,480 --> 11:28:26,596 that for you and it's going to make our 18196 11:28:24,840 --> 11:28:28,596 lives easier because it means when you 18197 11:28:26,596 --> 11:28:30,480 want to solve a problem concept up here 18198 11:28:28,596 --> 11:28:31,840 to just get real work done or build 18199 11:28:30,480 --> 11:28:33,880 something amazing you don't have to 18200 11:28:31,840 --> 11:28:35,756 really get into the same weeds as we 18201 11:28:33,880 --> 11:28:38,276 have been deliberately this week and now 18202 11:28:35,756 --> 11:28:40,116 last but the goal ultimately is that you 18203 11:28:38,276 --> 11:28:42,200 better understand and can better harness 18204 11:28:40,116 --> 11:28:44,320 than all that a computer can do even in 18205 11:28:42,200 --> 11:28:46,040 those higher level languages So today 18206 11:28:44,320 --> 11:28:47,880 we're going to focus particularly on 18207 11:28:46,040 --> 11:28:49,800 data structures how you might structure 18208 11:28:47,880 --> 11:28:52,240 your data in memory which really amounts 18209 11:28:49,800 --> 11:28:54,560 to building things digitally stitching 18210 11:28:52,240 --> 11:28:56,200 together ideas and Concepts in memory 18211 11:28:54,560 --> 11:28:58,000 using this new building block from last 18212 11:28:56,200 --> 11:29:00,360 week which of course are these pointers 18213 11:28:58,000 --> 11:29:02,520 pointers allow you to store addresses in 18214 11:29:00,360 --> 11:29:04,240 memory like in variables but with those 18215 11:29:02,520 --> 11:29:06,200 simple addresses we can sort of leave 18216 11:29:04,240 --> 11:29:08,916 these breadcrumbs we can point from here 18217 11:29:06,200 --> 11:29:10,720 to there and we can conceptually stitch 18218 11:29:08,916 --> 11:29:12,276 pieces of data together but there's 18219 11:29:10,720 --> 11:29:13,800 going to be different ways of doing that 18220 11:29:12,276 --> 11:29:16,160 and today we'll focus first on what's 18221 11:29:13,800 --> 11:29:17,916 generally known as an abstract data type 18222 11:29:16,160 --> 11:29:19,720 and similar to a type in C more 18223 11:29:17,916 --> 11:29:21,200 generally it does actually have some 18224 11:29:19,720 --> 11:29:23,080 properties in it but the underlying 18225 11:29:21,200 --> 11:29:25,560 implementation details of an abstract 18226 11:29:23,080 --> 11:29:27,160 data type are ultimately up to you that 18227 11:29:25,560 --> 11:29:29,640 is to say an abstract data type can 18228 11:29:27,160 --> 11:29:31,956 represent one thing and can do something 18229 11:29:29,640 --> 11:29:34,080 but how you implement it allows you some 18230 11:29:31,956 --> 11:29:35,480 discretion underneath the hood so for 18231 11:29:34,080 --> 11:29:38,840 instance in the world of computer 18232 11:29:35,480 --> 11:29:40,720 science a q is actually a technical term 18233 11:29:38,840 --> 11:29:42,956 this is a type of data structure that we 18234 11:29:40,720 --> 11:29:45,680 could theoretically build in code in C 18235 11:29:42,956 --> 11:29:47,480 or really any other language but a q has 18236 11:29:45,680 --> 11:29:49,360 properties just like cu's in the real 18237 11:29:47,480 --> 11:29:51,596 world for instance if you've ever lined 18238 11:29:49,360 --> 11:29:53,756 up for something to get food in a uh and 18239 11:29:51,596 --> 11:29:55,680 get food in a restaurant or go into a 18240 11:29:53,756 --> 11:29:58,160 store or wait for the airport to clear 18241 11:29:55,680 --> 11:30:00,200 you well you've lined up in a queue Q 18242 11:29:58,160 --> 11:30:02,560 being some sort of line but what's 18243 11:30:00,200 --> 11:30:06,276 noteworthy about Q's are specific 18244 11:30:02,560 --> 11:30:08,400 properties they are first in first out 18245 11:30:06,276 --> 11:30:10,160 data structures either virtually or in 18246 11:30:08,400 --> 11:30:12,160 the human world which is to say the 18247 11:30:10,160 --> 11:30:14,080 first person in the line should ideally 18248 11:30:12,160 --> 11:30:15,320 be served first at the restaurant or the 18249 11:30:14,080 --> 11:30:17,320 first person in the line should get 18250 11:30:15,320 --> 11:30:19,520 through airport security first by 18251 11:30:17,320 --> 11:30:21,436 contrast if it weren't first in first 18252 11:30:19,520 --> 11:30:23,520 out you can imagine how frustrated you 18253 11:30:21,436 --> 11:30:24,916 would be if you have this sort of 18254 11:30:23,520 --> 11:30:26,436 inherent unfairness in fact if you've 18255 11:30:24,916 --> 11:30:28,160 ever been in line at a store a 18256 11:30:26,436 --> 11:30:29,720 supermarket or the like and all of a 18257 11:30:28,160 --> 11:30:31,520 sudden they maybe open a new line and 18258 11:30:29,720 --> 11:30:33,436 now the person behind you gets to kind 18259 11:30:31,520 --> 11:30:35,276 of cut ahead and go forward that's 18260 11:30:33,436 --> 11:30:37,480 because they've broken the concept of 18261 11:30:35,276 --> 11:30:39,200 the cube so it has this inherent 18262 11:30:37,480 --> 11:30:41,800 potential for unfairness unless you 18263 11:30:39,200 --> 11:30:43,560 maintain this first in first out 18264 11:30:41,800 --> 11:30:45,800 property this would be true too for like 18265 11:30:43,560 --> 11:30:47,560 a to-do list just for productivity sake 18266 11:30:45,800 --> 11:30:50,116 if you're in the habit on paper or 18267 11:30:47,560 --> 11:30:51,320 virtually making a to-do list ideally 18268 11:30:50,116 --> 11:30:53,116 you probably want to go through that 18269 11:30:51,320 --> 11:30:55,116 list top to bottom so that you actually 18270 11:30:53,116 --> 11:30:57,000 get the first stuff you thought of done 18271 11:30:55,116 --> 11:30:59,360 first rather than always focusing on 18272 11:30:57,000 --> 11:31:02,116 your most recent thought now within the 18273 11:30:59,360 --> 11:31:04,400 world of Q's there's generally two 18274 11:31:02,116 --> 11:31:06,240 operations two functions if you will 18275 11:31:04,400 --> 11:31:08,800 that any Q would have either in the real 18276 11:31:06,240 --> 11:31:10,436 world or in the virtual NQ is usually 18277 11:31:08,800 --> 11:31:12,436 the technical term to mean adding 18278 11:31:10,436 --> 11:31:14,956 something to a q but specifically it 18279 11:31:12,436 --> 11:31:16,680 means adding it to the end of the que so 18280 11:31:14,956 --> 11:31:18,756 that someone isn't cutting in line for 18281 11:31:16,680 --> 11:31:20,680 instance to go up front and then DQ is 18282 11:31:18,756 --> 11:31:22,520 just the opposite when it's time for the 18283 11:31:20,680 --> 11:31:24,116 first person in line to be served the 18284 11:31:22,520 --> 11:31:26,840 time for the first person in line to go 18285 11:31:24,116 --> 11:31:29,520 through security they are dced so to 18286 11:31:26,840 --> 11:31:31,436 speak so technical concept ultimately as 18287 11:31:29,520 --> 11:31:33,240 it's implemented in code but it's really 18288 11:31:31,436 --> 11:31:35,480 just a real world concept and these are 18289 11:31:33,240 --> 11:31:38,240 in contrast to another abstract data 18290 11:31:35,480 --> 11:31:40,800 type that we might call a stack and a 18291 11:31:38,240 --> 11:31:42,756 stack much like a stack of trays in the 18292 11:31:40,800 --> 11:31:44,560 cafeteria has sort of fundamentally 18293 11:31:42,756 --> 11:31:46,400 different properties you can still add 18294 11:31:44,560 --> 11:31:47,756 and remove things from them but consider 18295 11:31:46,400 --> 11:31:49,720 what happens whenever they clean all the 18296 11:31:47,756 --> 11:31:52,160 trays in the cafeteria or the dining 18297 11:31:49,720 --> 11:31:53,560 hall they put one of the trays down here 18298 11:31:52,160 --> 11:31:55,276 and then the next one on top of it and 18299 11:31:53,560 --> 11:31:57,320 then the next one on top of it it it and 18300 11:31:55,276 --> 11:31:59,240 so forth but of course which tray do you 18301 11:31:57,320 --> 11:32:01,080 presume presumably take as a user of 18302 11:31:59,240 --> 11:32:04,240 that physical 18303 11:32:01,080 --> 11:32:05,320 stack the top one presumably right like 18304 11:32:04,240 --> 11:32:06,840 you're not going to fuss down here and 18305 11:32:05,320 --> 11:32:09,240 try to pull one out and so that would 18306 11:32:06,840 --> 11:32:12,080 seem to have an opposite property lifo 18307 11:32:09,240 --> 11:32:14,200 last in first out is what characterizes 18308 11:32:12,080 --> 11:32:15,596 something like a stack and that just 18309 11:32:14,200 --> 11:32:17,360 makes sense certainly in the physical 18310 11:32:15,596 --> 11:32:18,756 world of stacking all of those cafeteria 18311 11:32:17,360 --> 11:32:21,916 trays because it just makes more sense 18312 11:32:18,756 --> 11:32:24,320 to grab the most recently added one the 18313 11:32:21,916 --> 11:32:26,276 last added one first and at least in 18314 11:32:24,320 --> 11:32:28,276 that context the trays don't necessarily 18315 11:32:26,276 --> 11:32:29,680 care what order they're using used in 18316 11:32:28,276 --> 11:32:31,880 but even then you could imagine that 18317 11:32:29,680 --> 11:32:33,840 maybe there's some old dirty nasty tray 18318 11:32:31,880 --> 11:32:35,360 at the very bottom that like never gets 18319 11:32:33,840 --> 11:32:36,956 used because you're constantly 18320 11:32:35,360 --> 11:32:38,956 replenishing the stack so there might 18321 11:32:36,956 --> 11:32:41,436 very well be side effects of these kinds 18322 11:32:38,956 --> 11:32:43,116 of features um you might be familiar 18323 11:32:41,436 --> 11:32:45,436 with using Gmail for instance or really 18324 11:32:43,116 --> 11:32:48,040 any email program what you're looking at 18325 11:32:45,436 --> 11:32:49,320 in your inbox is technically a stack at 18326 11:32:48,040 --> 11:32:51,436 least if you've left the defaults 18327 11:32:49,320 --> 11:32:54,000 configured why you get a new email where 18328 11:32:51,436 --> 11:32:55,916 does it end up not like five pages of 18329 11:32:54,000 --> 11:32:57,200 emails later presumably right at the top 18330 11:32:55,916 --> 11:32:58,840 and the next one's right at the top 18331 11:32:57,200 --> 11:33:00,240 right at the the top right at the top 18332 11:32:58,840 --> 11:33:01,880 and so if you're like me you're guilty 18333 11:33:00,240 --> 11:33:04,080 of eventually losing track of some 18334 11:33:01,880 --> 11:33:06,040 emails why because you've pushed so many 18335 11:33:04,080 --> 11:33:08,200 more emails onto the stack that you sort 18336 11:33:06,040 --> 11:33:10,200 of lose track of the things you got 18337 11:33:08,200 --> 11:33:11,956 earliest last in first out though is 18338 11:33:10,200 --> 11:33:13,116 maintained the most recent email you get 18339 11:33:11,956 --> 11:33:15,200 might very well be the first one you 18340 11:33:13,116 --> 11:33:17,680 reply to but that's not necessarily good 18341 11:33:15,200 --> 11:33:19,640 for responsiveness to everyone else out 18342 11:33:17,680 --> 11:33:23,240 there uh similarly if you store like all 18343 11:33:19,640 --> 11:33:25,320 of your sweaters in a stack like this uh 18344 11:33:23,240 --> 11:33:27,200 the uh like a pile of black ones below 18345 11:33:25,320 --> 11:33:28,560 which is a red and then a blue stack 18346 11:33:27,200 --> 11:33:30,080 might be perfectly fine for keeping 18347 11:33:28,560 --> 11:33:31,436 things organized it's sort of the sane 18348 11:33:30,080 --> 11:33:33,360 way to do it if you just have a shelf in 18349 11:33:31,436 --> 11:33:35,680 your dorm room or home but what's going 18350 11:33:33,360 --> 11:33:37,916 to be a side effect of using a stack to 18351 11:33:35,680 --> 11:33:42,400 store your sweaters if they're these in 18352 11:33:37,916 --> 11:33:44,680 this way as opposed to a queue 18353 11:33:42,400 --> 11:33:46,560 yeah it's harder to get the red and blue 18354 11:33:44,680 --> 11:33:48,840 one so presumably you're going to much 18355 11:33:46,560 --> 11:33:51,080 more often wear for instance if you will 18356 11:33:48,840 --> 11:33:54,320 black instead there now the operations 18357 11:33:51,080 --> 11:33:56,116 for adding things to a stack are similar 18358 11:33:54,320 --> 11:33:59,200 in spirit but just different vocabulary 18359 11:33:56,116 --> 11:34:00,400 you push something on top of a stack um 18360 11:33:59,200 --> 11:34:02,116 even though it's more like in the tray 18361 11:34:00,400 --> 11:34:03,640 world you sort of place it or rest it 18362 11:34:02,116 --> 11:34:05,840 but pushing means adding something to 18363 11:34:03,640 --> 11:34:08,520 the top of the stack and popping means 18364 11:34:05,840 --> 11:34:10,560 some removing something also from the 18365 11:34:08,520 --> 11:34:13,200 top of the stack so it's not a matter of 18366 11:34:10,560 --> 11:34:15,160 enqing at the end and deqing at the 18367 11:34:13,200 --> 11:34:17,040 beginning with a stack everything's 18368 11:34:15,160 --> 11:34:19,596 happening on top you're pushing onto the 18369 11:34:17,040 --> 11:34:21,320 top and then popping off of the top now 18370 11:34:19,596 --> 11:34:22,880 when it comes to actual code how might 18371 11:34:21,320 --> 11:34:24,756 we Implement something like this well 18372 11:34:22,880 --> 11:34:26,520 let's just focus on really how you might 18373 11:34:24,756 --> 11:34:28,360 implement the data structure itself and 18374 11:34:26,520 --> 11:34:30,040 we won't Implement any functions you 18375 11:34:28,360 --> 11:34:31,880 might implement the notion of a stack 18376 11:34:30,040 --> 11:34:34,276 like this we've seen type def before it 18377 11:34:31,880 --> 11:34:36,596 just means Define a new type of my own 18378 11:34:34,276 --> 11:34:38,916 struct means here comes a structure AKA 18379 11:34:36,596 --> 11:34:41,436 a data structure of one or more 18380 11:34:38,916 --> 11:34:43,276 variables within and let's suppose like 18381 11:34:41,436 --> 11:34:45,800 last time we've had we defined already 18382 11:34:43,276 --> 11:34:47,320 like a person data type using a separate 18383 11:34:45,800 --> 11:34:49,320 type Def and every person has like a 18384 11:34:47,320 --> 11:34:50,956 name and a number or whatever let me 18385 11:34:49,320 --> 11:34:52,840 just stipulate that person exists 18386 11:34:50,956 --> 11:34:56,400 already so here you might have to 18387 11:34:52,840 --> 11:34:58,956 implement a stack an array called people 18388 11:34:56,400 --> 11:35:02,000 of some capacity maybe it's an an array 18389 11:34:58,956 --> 11:35:04,160 of size 10 or 100 or whatever this is a 18390 11:35:02,000 --> 11:35:06,240 constant here in this context capacity 18391 11:35:04,160 --> 11:35:09,560 and every element in that array is a 18392 11:35:06,240 --> 11:35:11,560 person structure and I now have this to 18393 11:35:09,560 --> 11:35:13,800 size now this almost seems like a 18394 11:35:11,560 --> 11:35:16,116 synonym for capacity but maybe 18395 11:35:13,800 --> 11:35:18,520 intuitively anyone want to propose why 18396 11:35:16,116 --> 11:35:21,040 I'm apparently maintaining separately 18397 11:35:18,520 --> 11:35:23,596 the capacity of the stack but also the 18398 11:35:21,040 --> 11:35:25,200 size of the stack why these two 18399 11:35:23,596 --> 11:35:29,320 distinctions 18400 11:35:25,200 --> 11:35:29,320 here yeah 18401 11:35:35,480 --> 11:35:39,520 perfect the capacity is how high the 18402 11:35:37,276 --> 11:35:42,040 stack could be like how much room is 18403 11:35:39,520 --> 11:35:44,040 there for those sweatshirts in my closet 18404 11:35:42,040 --> 11:35:45,880 for instance whereas size is just 18405 11:35:44,040 --> 11:35:47,756 literally at this moment in time how 18406 11:35:45,880 --> 11:35:50,560 many sweatshirts are in the stack it's 18407 11:35:47,756 --> 11:35:52,840 either capacity or fewer presumably in 18408 11:35:50,560 --> 11:35:54,596 total there so what is capacity well we 18409 11:35:52,840 --> 11:35:56,436 could implement this in you know perhaps 18410 11:35:54,596 --> 11:35:58,520 a familiar way I might just Define a 18411 11:35:56,436 --> 11:36:01,080 const somewhere else in my code of type 18412 11:35:58,520 --> 11:36:03,116 int that just defines it to be capacity 18413 11:36:01,080 --> 11:36:06,276 50 but what perhaps is going to be the 18414 11:36:03,116 --> 11:36:10,116 downside of implementing a stack in this 18415 11:36:06,276 --> 11:36:11,756 way of how using an array inside here 18416 11:36:10,116 --> 11:36:14,680 like what's a downside now of 18417 11:36:11,756 --> 11:36:17,320 implementing a stack using an array and 18418 11:36:14,680 --> 11:36:20,480 this size variable 18419 11:36:17,320 --> 11:36:24,200 within what's a caveat here 18420 11:36:20,480 --> 11:36:24,200 perhaps any hands 18421 11:36:24,640 --> 11:36:28,080 yeah okay so it's going to be harder to 18422 11:36:26,596 --> 11:36:30,040 reach elements that aren't the last one 18423 11:36:28,080 --> 11:36:31,520 that is the most recently added one so 18424 11:36:30,040 --> 11:36:33,520 there could be some sweatshirts so to 18425 11:36:31,520 --> 11:36:35,560 speak way down below so we've seen that 18426 11:36:33,520 --> 11:36:37,560 before too but at some point too a 18427 11:36:35,560 --> 11:36:39,200 limitation of this design is is what 18428 11:36:37,560 --> 11:36:41,200 it's going to be finite right I can 18429 11:36:39,200 --> 11:36:43,680 maximally fit in this example 50 18430 11:36:41,200 --> 11:36:45,756 sweatshirts or 50 emails or 50 cafeteria 18431 11:36:43,680 --> 11:36:47,436 Trace which is fine but at least it's 18432 11:36:45,756 --> 11:36:49,400 indeed finite and at least in the 18433 11:36:47,436 --> 11:36:51,800 computer's memory it might be nice to 18434 11:36:49,400 --> 11:36:53,520 use more and more and more maybe as more 18435 11:36:51,800 --> 11:36:55,720 things are getting added to the computer 18436 11:36:53,520 --> 11:36:58,480 so maybe I make this 500 or heck why 18437 11:36:55,720 --> 11:37:00,480 don't I make it 5,000 or 50,000 well 18438 11:36:58,480 --> 11:37:03,360 what's the tradeoff there if I want to 18439 11:37:00,480 --> 11:37:04,880 have enough room to grow seems like I 18440 11:37:03,360 --> 11:37:07,880 should just crank up the value of 18441 11:37:04,880 --> 11:37:11,756 capacity endlessly but why might I not 18442 11:37:07,880 --> 11:37:14,116 want to change the 50 to 500 or 5,000 or 18443 11:37:11,756 --> 11:37:18,040 50,000 what's the trade-off there 18444 11:37:14,116 --> 11:37:18,040 perhaps just intuitively 18445 11:37:19,160 --> 11:37:22,720 yeah okay you don't want to touch memory 18446 11:37:21,080 --> 11:37:24,596 that you're not supposed to be touching 18447 11:37:22,720 --> 11:37:26,320 and in this case it wouldn't be that 18448 11:37:24,596 --> 11:37:28,320 wouldn't be a risk per se unless you 18449 11:37:26,320 --> 11:37:30,596 indeed overflow the stack but there's a 18450 11:37:28,320 --> 11:37:34,360 related issue in asking for that much 18451 11:37:30,596 --> 11:37:34,360 memory what would another downside 18452 11:37:38,756 --> 11:37:43,756 be yeah okay exactly so if you've got a 18453 11:37:41,720 --> 11:37:46,080 capacity of 5,000 but you're only using 18454 11:37:43,756 --> 11:37:47,560 one of those elements it's it's awkward 18455 11:37:46,080 --> 11:37:49,680 to say it non-technically which is just 18456 11:37:47,560 --> 11:37:51,480 to say very very wasteful right that's 18457 11:37:49,680 --> 11:37:53,320 just bad design it's correct it will 18458 11:37:51,480 --> 11:37:54,680 work for up to 5,000 elements but my 18459 11:37:53,320 --> 11:37:57,000 gosh you're wasting 18460 11:37:54,680 --> 11:37:58,756 4,999 extra spots and that's not going 18461 11:37:57,000 --> 11:38:00,480 to end well especially if you're using 18462 11:37:58,756 --> 11:38:02,116 more data structures in memory like your 18463 11:38:00,480 --> 11:38:04,116 Mac your PC your phone is surely going 18464 11:38:02,116 --> 11:38:05,756 to run out of memory if you ask for that 18465 11:38:04,116 --> 11:38:08,520 much so it' be nice if there is a bit 18466 11:38:05,756 --> 11:38:10,240 more dynamism there whether it's a stack 18467 11:38:08,520 --> 11:38:12,436 or a CU both of which might be 18468 11:38:10,240 --> 11:38:15,116 implemented a little similarly in spirit 18469 11:38:12,436 --> 11:38:16,916 but let's conclude this abstraction by 18470 11:38:15,116 --> 11:38:18,480 comparing thanks to a friend of ours 18471 11:38:16,916 --> 11:38:20,320 Professor Shannon Duval of Elon 18472 11:38:18,480 --> 11:38:22,000 University who kindly put together this 18473 11:38:20,320 --> 11:38:23,880 graphical animation that's just under 2 18474 11:38:22,000 --> 11:38:25,436 minutes long that paints a picture of 18475 11:38:23,880 --> 11:38:26,596 these two types of abstract data 18476 11:38:25,436 --> 11:38:29,200 structures and then we'll dive in 18477 11:38:26,596 --> 11:38:30,880 underneath to how we might Implement uh 18478 11:38:29,200 --> 11:38:32,970 problems like these if we could dim the 18479 11:38:30,880 --> 11:38:36,150 lights 18480 11:38:32,970 --> 11:38:36,150 [Music] 18481 11:38:36,320 --> 11:38:41,116 dramatically once upon a time there was 18482 11:38:38,640 --> 11:38:43,840 a guy named Jack when it came to making 18483 11:38:41,116 --> 11:38:45,596 friends Jack did not have the Knack so 18484 11:38:43,840 --> 11:38:48,040 Jack went to talk to the most popular 18485 11:38:45,596 --> 11:38:50,436 guy he knew he went up to Lou and asked 18486 11:38:48,040 --> 11:38:53,200 what do I do Lou saw that his friend was 18487 11:38:50,436 --> 11:38:54,640 really distressed well Lou began just 18488 11:38:53,200 --> 11:38:57,080 look how you're dressed don't you have 18489 11:38:54,640 --> 11:38:59,956 any clothes with a different look yeah 18490 11:38:57,080 --> 11:39:02,000 yes said Jack I sure do come to my house 18491 11:38:59,956 --> 11:39:04,436 and I'll show them to you so they went 18492 11:39:02,000 --> 11:39:06,040 off to Jacks and Jack showed Lou the box 18493 11:39:04,436 --> 11:39:08,560 where he kept all his shirts and his 18494 11:39:06,040 --> 11:39:10,640 pants and his socks L said I see you 18495 11:39:08,560 --> 11:39:12,400 have all your clothes in a pile why 18496 11:39:10,640 --> 11:39:14,916 don't you wear some others once in a 18497 11:39:12,400 --> 11:39:17,400 while Jack said well when I remove 18498 11:39:14,916 --> 11:39:19,800 clothes and socks I wash them and put 18499 11:39:17,400 --> 11:39:22,640 them away in the box then comes the next 18500 11:39:19,800 --> 11:39:25,360 morning and up I hop I go to the box and 18501 11:39:22,640 --> 11:39:27,756 get my clothes off the top Lou quickly 18502 11:39:25,360 --> 11:39:30,596 realized the problem with Jack he kept 18503 11:39:27,756 --> 11:39:32,640 clothes CDs and books in a stack when he 18504 11:39:30,596 --> 11:39:35,520 reached for something to read or to wear 18505 11:39:32,640 --> 11:39:37,080 he chose the top book or underwear then 18506 11:39:35,520 --> 11:39:39,360 when he was done he would put it right 18507 11:39:37,080 --> 11:39:41,480 back back it would go on top of the 18508 11:39:39,360 --> 11:39:43,680 stack I know the solution said a 18509 11:39:41,480 --> 11:39:46,200 triumphant Lou you need to learn to 18510 11:39:43,680 --> 11:39:48,200 start using a queue Lou took Jack's 18511 11:39:46,200 --> 11:39:49,800 clothes and hung them in a closet and 18512 11:39:48,200 --> 11:39:52,400 when he had emptied the box he just 18513 11:39:49,800 --> 11:39:54,360 tossed it then he said now Jack at the 18514 11:39:52,400 --> 11:39:56,360 end of the day put your clothes on the 18515 11:39:54,360 --> 11:39:57,560 left when you put them away then 18516 11:39:56,360 --> 11:39:59,756 tomorrow morning morning when you see 18517 11:39:57,560 --> 11:40:02,040 the sun shine get your clothes from the 18518 11:39:59,756 --> 11:40:04,756 right from the end of the line don't you 18519 11:40:02,040 --> 11:40:06,640 see said Lou it will be so nice you'll 18520 11:40:04,756 --> 11:40:08,916 wear everything once before you wear 18521 11:40:06,640 --> 11:40:11,276 something twice and with everything in 18522 11:40:08,916 --> 11:40:13,400 cues in his closet and shelf Jack 18523 11:40:11,276 --> 11:40:17,596 started to feel quite sure of himself 18524 11:40:13,400 --> 11:40:17,596 all thanks to Lou and his wonderful 18525 11:40:18,240 --> 11:40:25,596 Q all right so sure so that paints a 18526 11:40:24,080 --> 11:40:27,160 picture of these two abstract data 18527 11:40:25,596 --> 11:40:28,320 structures but if we really were to dive 18528 11:40:27,160 --> 11:40:30,040 underneath the hood we could Implement 18529 11:40:28,320 --> 11:40:32,160 them in a number of different ways but 18530 11:40:30,040 --> 11:40:33,880 we really I think need some building 18531 11:40:32,160 --> 11:40:35,480 blocks via which we could solve problems 18532 11:40:33,880 --> 11:40:37,840 like those and we'll see today to some 18533 11:40:35,480 --> 11:40:40,000 others as well so let's rewind back to 18534 11:40:37,840 --> 11:40:41,596 week two where we imple we introduced 18535 11:40:40,000 --> 11:40:43,880 you to your very first data structure 18536 11:40:41,596 --> 11:40:46,320 that is an array and an array of recall 18537 11:40:43,880 --> 11:40:48,916 was just a chunk of memory whereby 18538 11:40:46,320 --> 11:40:51,596 elements were stored by Design back to 18539 11:40:48,916 --> 11:40:54,200 back to back it's an array of contiguous 18540 11:40:51,596 --> 11:40:56,040 memory specifically so with an array we 18541 11:40:54,200 --> 11:40:58,880 could certainly store not just one thing 18542 11:40:56,040 --> 11:41:01,040 but two or three or even more and so for 18543 11:40:58,880 --> 11:41:02,680 instance if we treat uh my computer's 18544 11:41:01,040 --> 11:41:04,436 memory as this sort of abstraction here 18545 11:41:02,680 --> 11:41:06,880 and pictured here are three bytes or 18546 11:41:04,436 --> 11:41:08,360 some uh uh multiplication thereof 18547 11:41:06,880 --> 11:41:10,680 suppose we're storing in the computer's 18548 11:41:08,360 --> 11:41:13,116 memory an array of size three storing 18549 11:41:10,680 --> 11:41:15,840 the digits one two and three well 18550 11:41:13,116 --> 11:41:17,640 remember that if we zoom out per last 18551 11:41:15,840 --> 11:41:19,916 week there's other stuff going on in 18552 11:41:17,640 --> 11:41:21,640 memory so even if we want to add another 18553 11:41:19,916 --> 11:41:23,596 number to this array that we didn't 18554 11:41:21,640 --> 11:41:25,956 think of when we first started the 18555 11:41:23,596 --> 11:41:27,480 program like the number four ideally we 18556 11:41:25,956 --> 11:41:29,436 would put it right here next to it 18557 11:41:27,480 --> 11:41:30,840 otherwise it's no longer an array so by 18558 11:41:29,436 --> 11:41:32,640 definition if we're using an array it's 18559 11:41:30,840 --> 11:41:34,480 got to end up right there after the 18560 11:41:32,640 --> 11:41:36,400 three but what else is going on inside 18561 11:41:34,480 --> 11:41:38,160 of your computer's memory well assuming 18562 11:41:36,400 --> 11:41:40,000 your program is of any length and you've 18563 11:41:38,160 --> 11:41:41,480 got other variables other functions 18564 11:41:40,000 --> 11:41:43,200 you've been running it for a while 18565 11:41:41,480 --> 11:41:45,360 there's a lot going on in your memory is 18566 11:41:43,200 --> 11:41:47,480 being used and reused so for instance 18567 11:41:45,360 --> 11:41:50,480 somewhere in memory might be immediately 18568 11:41:47,480 --> 11:41:52,840 adjacent to this like hello comma world 18569 11:41:50,480 --> 11:41:54,160 back0 the null character just because 18570 11:41:52,840 --> 11:41:56,200 maybe you have another variable 18571 11:41:54,160 --> 11:41:57,840 somewhere in there that is storing that 18572 11:41:56,200 --> 11:41:59,840 particular string alongside your 18573 11:41:57,840 --> 11:42:01,360 existing array of size three and all of 18574 11:41:59,840 --> 11:42:03,240 these Oscar the grouches here really 18575 11:42:01,360 --> 11:42:05,040 just represent what we called last week 18576 11:42:03,240 --> 11:42:06,800 garbage values like there's obviously 18577 11:42:05,040 --> 11:42:08,200 bits there because they don't disappear 18578 11:42:06,800 --> 11:42:10,080 they're always going to be inside of the 18579 11:42:08,200 --> 11:42:11,840 computer somehow implemented but we 18580 11:42:10,080 --> 11:42:13,436 don't really know or care what they are 18581 11:42:11,840 --> 11:42:14,956 they're the remnants of those bytes 18582 11:42:13,436 --> 11:42:17,116 having been used for other older 18583 11:42:14,956 --> 11:42:19,480 variables previous function calls or the 18584 11:42:17,116 --> 11:42:21,840 like but the problem clearly here is 18585 11:42:19,480 --> 11:42:23,480 that okay 1 two 3 is there but the H is 18586 11:42:21,840 --> 11:42:25,916 here and unless I want to start uh 18587 11:42:23,480 --> 11:42:28,840 taking a bite out of my string by 18588 11:42:25,916 --> 11:42:30,840 overriding the H with a four like we 18589 11:42:28,840 --> 11:42:32,320 just can't fit it right there and yet 18590 11:42:30,840 --> 11:42:33,720 even though there's Oscars all over the 18591 11:42:32,320 --> 11:42:35,956 place those are indeed garbage values 18592 11:42:33,720 --> 11:42:37,640 and therefore we could use that space 18593 11:42:35,956 --> 11:42:39,436 because it's technically unused we just 18594 11:42:37,640 --> 11:42:42,480 don't know or care what the values are 18595 11:42:39,436 --> 11:42:44,400 so where could I put 1 2 3 4 well my 18596 11:42:42,480 --> 11:42:46,436 gosh like I have all this memory down 18597 11:42:44,400 --> 11:42:49,360 here that's unused I could certainly 18598 11:42:46,436 --> 11:42:53,560 change those garbage values to be 1 2 3 18599 11:42:49,360 --> 11:42:55,596 4 but to do that I might need to do a 18600 11:42:53,560 --> 11:42:57,756 bit of work here right it's not just a 18601 11:42:55,596 --> 11:43:00,320 matter of just saying boom and it 18602 11:42:57,756 --> 11:43:01,320 happens now with c and with code I'd 18603 11:43:00,320 --> 11:43:02,956 have to do this a little more 18604 11:43:01,320 --> 11:43:04,840 methodically so let me abstract away 18605 11:43:02,956 --> 11:43:06,560 everything else that's a distraction let 18606 11:43:04,840 --> 11:43:08,680 me assume that there is indeed at least 18607 11:43:06,560 --> 11:43:09,956 four bytes available for four numbers 18608 11:43:08,680 --> 11:43:11,720 just down here though we could have put 18609 11:43:09,956 --> 11:43:14,840 them in a bunch of different spots 18610 11:43:11,720 --> 11:43:17,480 what's involved now in moving the one 18611 11:43:14,840 --> 11:43:19,480 two three to this new chunk of memory so 18612 11:43:17,480 --> 11:43:21,040 we can add the four well I think 18613 11:43:19,480 --> 11:43:23,720 conceptually we're going to have to copy 18614 11:43:21,040 --> 11:43:25,800 the one from old to new copy the two 18615 11:43:23,720 --> 11:43:28,080 from old to new copy the three from old 18616 11:43:25,800 --> 11:43:29,800 to new and then ultimately we can get 18617 11:43:28,080 --> 11:43:31,800 rid of the old memory those three 18618 11:43:29,800 --> 11:43:33,680 original bites could now look like Oscar 18619 11:43:31,800 --> 11:43:35,680 the Grouch and just be garbage values 18620 11:43:33,680 --> 11:43:38,080 for all intents and purposes but now I 18621 11:43:35,680 --> 11:43:41,720 have room for a fourth bite wherein I 18622 11:43:38,080 --> 11:43:44,720 can put the number four so this is nice 18623 11:43:41,720 --> 11:43:47,160 but what's a downside of this 18624 11:43:44,720 --> 11:43:48,276 approach what's a downside of solving 18625 11:43:47,160 --> 11:43:50,680 the problem in this way where the 18626 11:43:48,276 --> 11:43:53,436 problem at hand is just to grow the 18627 11:43:50,680 --> 11:43:55,560 array so to speak to increase its size 18628 11:43:53,436 --> 11:43:58,680 to fit one or more 18629 11:43:55,560 --> 11:44:02,040 numbers seems pretty 18630 11:43:58,680 --> 11:44:02,040 straightforward but 18631 11:44:04,320 --> 11:44:08,116 yeah okay maybe it's out of order but I 18632 11:44:06,680 --> 11:44:09,720 think that's okay because the order is 18633 11:44:08,116 --> 11:44:11,756 just matters that it's relative so so 18634 11:44:09,720 --> 11:44:12,916 long it's it's still contiguous back to 18635 11:44:11,756 --> 11:44:14,640 back to back in a different chunk of 18636 11:44:12,916 --> 11:44:17,436 memory I think we're okay there it's not 18637 11:44:14,640 --> 11:44:20,240 like I changed it for 4 3 2 one but a 18638 11:44:17,436 --> 11:44:20,240 reasonable hunch 18639 11:44:21,480 --> 11:44:25,640 yeah yeah like I don't really plan ahead 18640 11:44:24,276 --> 11:44:27,840 here like if I have to add another 18641 11:44:25,640 --> 11:44:29,116 number like five or anything else well I 18642 11:44:27,840 --> 11:44:30,436 might have to jump through these hoops 18643 11:44:29,116 --> 11:44:32,360 again maybe I get lucky and maybe 18644 11:44:30,436 --> 11:44:33,800 there's space there but not if I have 18645 11:44:32,360 --> 11:44:36,640 other variables and other things going 18646 11:44:33,800 --> 11:44:39,916 on that too might be used at some point 18647 11:44:36,640 --> 11:44:43,200 other thoughts yeah slow efficiency slow 18648 11:44:39,916 --> 11:44:45,956 effic in slow efficiency why have 18649 11:44:43,200 --> 11:44:48,756 to again yeah I mean it's just in it's 18650 11:44:45,956 --> 11:44:51,840 just inefficient it's sort of bad design 18651 11:44:48,756 --> 11:44:53,880 arguably why because I had to copy all 18652 11:44:51,840 --> 11:44:55,640 of my original work down here and as you 18653 11:44:53,880 --> 11:44:57,360 note if I want to add a fifth number I'm 18654 11:44:55,640 --> 11:45:00,160 going to have to copy it again and again 18655 11:44:57,360 --> 11:45:02,720 and again and do things end times again 18656 11:45:00,160 --> 11:45:04,956 and again now maybe that's necessary 18657 11:45:02,720 --> 11:45:06,160 we'll soon see for sure but it feels 18658 11:45:04,956 --> 11:45:07,520 like this is not going to end well 18659 11:45:06,160 --> 11:45:10,240 especially if the array isn't of size 18660 11:45:07,520 --> 11:45:12,560 three or four but 300 400 your computer 18661 11:45:10,240 --> 11:45:14,276 ends up spending so much time just 18662 11:45:12,560 --> 11:45:16,160 spinning its Wheels I mean honestly 18663 11:45:14,276 --> 11:45:18,640 better might be this like if this is my 18664 11:45:16,160 --> 11:45:20,880 same array physically incarnated Now 1 18665 11:45:18,640 --> 11:45:23,040 two 3 it's literally on the edge of the 18666 11:45:20,880 --> 11:45:25,360 shelf so there's no room for the number 18667 11:45:23,040 --> 11:45:27,520 four you know maybe where we could take 18668 11:45:25,360 --> 11:45:29,200 this story is well let's just find room 18669 11:45:27,520 --> 11:45:31,880 for the four like let's just put the 18670 11:45:29,200 --> 11:45:33,916 four for instance over here replacing 18671 11:45:31,880 --> 11:45:36,436 some available garbage value some spare 18672 11:45:33,916 --> 11:45:38,956 bite over here but now wait a minute 18673 11:45:36,436 --> 11:45:40,360 I've broken the definition of an array 18674 11:45:38,956 --> 11:45:42,956 right it's I can't have one two three 18675 11:45:40,360 --> 11:45:44,880 and then four over here so maybe there 18676 11:45:42,956 --> 11:45:46,160 maybe there could be a mechanism if I 18677 11:45:44,880 --> 11:45:48,276 put this thing on again where when you 18678 11:45:46,160 --> 11:45:51,160 get to the end of the existing elements 18679 11:45:48,276 --> 11:45:53,040 maybe I just somehow digitally point to 18680 11:45:51,160 --> 11:45:54,596 the fourth array and maybe we can kind 18681 11:45:53,040 --> 11:45:56,436 of stitch together all of these 18682 11:45:54,596 --> 11:45:58,880 different values in memory so that if 18683 11:45:56,436 --> 11:46:01,240 you follow the arrows so to speak we can 18684 11:45:58,880 --> 11:46:03,276 reconstruct exactly what the order is 18685 11:46:01,240 --> 11:46:05,320 even without having to find or make room 18686 11:46:03,276 --> 11:46:07,480 here or pick up all of these numbers and 18687 11:46:05,320 --> 11:46:09,240 move all of them over there so that's 18688 11:46:07,480 --> 11:46:12,720 perhaps the direction in which we'll go 18689 11:46:09,240 --> 11:46:15,080 here so let's see how we might get to 18690 11:46:12,720 --> 11:46:17,596 that spot as follows let me go ahead and 18691 11:46:15,080 --> 11:46:21,360 open up say VSS code here let me open up 18692 11:46:17,596 --> 11:46:23,116 a program called list. C in my terminal 18693 11:46:21,360 --> 11:46:25,200 and let me go ahead and whip up a 18694 11:46:23,116 --> 11:46:27,000 relatively simple program that just 18695 11:46:25,200 --> 11:46:29,560 demonstrates what we did back in week 18696 11:46:27,000 --> 11:46:32,680 two when we introduced arrays as follows 18697 11:46:29,560 --> 11:46:35,680 let me include uh standard io. so we can 18698 11:46:32,680 --> 11:46:37,880 print stuff out let me do int main void 18699 11:46:35,680 --> 11:46:42,360 no command line Arguments for now let me 18700 11:46:37,880 --> 11:46:44,436 give myself a an array uh called list of 18701 11:46:42,360 --> 11:46:46,240 size three and I'll just hardcode it to 18702 11:46:44,436 --> 11:46:48,080 keep it simple for lecture sake Each of 18703 11:46:46,240 --> 11:46:49,720 which is going to be an integer and now 18704 11:46:48,080 --> 11:46:51,916 just so we have some specifics to talk 18705 11:46:49,720 --> 11:46:53,956 about let me put it list bracket zero 18706 11:46:51,916 --> 11:46:55,720 the number one list bracket one the 18707 11:46:53,956 --> 11:46:57,756 number two and list bracket two the 18708 11:46:55,720 --> 11:46:59,840 number three so I'm just translating 18709 11:46:57,756 --> 11:47:01,276 into codee what we just had uh 18710 11:46:59,840 --> 11:47:03,276 pictorially on the screen and also 18711 11:47:01,276 --> 11:47:05,200 physically here with these numbers on 18712 11:47:03,276 --> 11:47:07,360 the desk now let's just do something 18713 11:47:05,200 --> 11:47:10,880 mildly useful for this how about we do 18714 11:47:07,360 --> 11:47:13,240 four in I gets zero I is less than three 18715 11:47:10,880 --> 11:47:14,880 I ++ let's just print each of these 18716 11:47:13,240 --> 11:47:16,436 numbers out just to make sure they're 18717 11:47:14,880 --> 11:47:19,756 indeed in memory as I intended so 18718 11:47:16,436 --> 11:47:21,840 percent I back sln comma I and then a 18719 11:47:19,756 --> 11:47:24,080 semicolon and I think that's it for now 18720 11:47:21,840 --> 11:47:25,916 so nothing interesting no problem solved 18721 11:47:24,080 --> 11:47:27,840 just yet just a proof of concept so that 18722 11:47:25,916 --> 11:47:28,840 now when I clear my terminal and run 18723 11:47:27,840 --> 11:47:32,240 make 18724 11:47:28,840 --> 11:47:34,400 list no apparent errors at the terminal 18725 11:47:32,240 --> 11:47:37,596 and so when I now do do/ list I should 18726 11:47:34,400 --> 11:47:39,596 see hopefully from left to right 1 two 3 18727 11:47:37,596 --> 11:47:42,160 but of course if I want to add a fourth 18728 11:47:39,596 --> 11:47:44,160 number now there's no mechanism for such 18729 11:47:42,160 --> 11:47:46,276 certainly in the code that I just wrote 18730 11:47:44,160 --> 11:47:48,720 I could go back in here and change this 18731 11:47:46,276 --> 11:47:51,200 to a four I could go down here and 18732 11:47:48,720 --> 11:47:53,756 change lists bracket 3 equals 4 I could 18733 11:47:51,200 --> 11:47:55,320 just manually change the code recompile 18734 11:47:53,756 --> 11:47:56,956 the code but of course that doesn't give 18735 11:47:55,320 --> 11:47:59,560 me any additional run way for the fifth 18736 11:47:56,956 --> 11:48:00,880 or sixth number so let me try to take a 18737 11:47:59,560 --> 11:48:04,276 different approach drawing some 18738 11:48:00,880 --> 11:48:06,956 inspiration from last week if I want to 18739 11:48:04,276 --> 11:48:08,520 allocate memory dynamically maybe 18740 11:48:06,956 --> 11:48:10,800 because I don't know when I wrote the 18741 11:48:08,520 --> 11:48:12,560 program how many bytes I want we have 18742 11:48:10,800 --> 11:48:14,000 another function as of last week that 18743 11:48:12,560 --> 11:48:16,040 does not require that you commit in 18744 11:48:14,000 --> 11:48:17,320 advance to a certain number of bytes via 18745 11:48:16,040 --> 11:48:20,040 what function can you just ask the 18746 11:48:17,320 --> 11:48:22,596 operating system for a chunk of memory 18747 11:48:20,040 --> 11:48:24,880 okay so Malo to allocate memory now an 18748 11:48:22,596 --> 11:48:26,800 array is just a chunk of memory and even 18749 11:48:24,880 --> 11:48:29,080 though since week two 've been using 18750 11:48:26,800 --> 11:48:31,160 this syntactic sugar this convenience of 18751 11:48:29,080 --> 11:48:33,560 just using square brackets and indexing 18752 11:48:31,160 --> 11:48:35,320 into it it's just making it easier to 18753 11:48:33,560 --> 11:48:37,560 manipulate a chunk of memory that's 18754 11:48:35,320 --> 11:48:39,800 contiguous all together back to back to 18755 11:48:37,560 --> 11:48:41,240 back so today just like last week we can 18756 11:48:39,800 --> 11:48:43,200 take those sort of training wheels off 18757 11:48:41,240 --> 11:48:45,520 and maybe be a little more deliberate in 18758 11:48:43,200 --> 11:48:48,800 how we allocate memory let me go for 18759 11:48:45,520 --> 11:48:51,040 instance and do this let me delete my 18760 11:48:48,800 --> 11:48:53,756 contents of my main function here go 18761 11:48:51,040 --> 11:48:57,160 back into Main and let me propose now 18762 11:48:53,756 --> 11:48:59,320 that I declare for instance how about my 18763 11:48:57,160 --> 11:49:02,276 list no longer as an array but as a 18764 11:48:59,320 --> 11:49:05,756 pointer so int star list and I'm going 18765 11:49:02,276 --> 11:49:07,756 to go ahead and initialize this to be 18766 11:49:05,756 --> 11:49:09,240 how about a chunk of three integers for 18767 11:49:07,756 --> 11:49:10,880 now so I'm still going to hardcode it 18768 11:49:09,240 --> 11:49:13,240 but I'm taking a step toward more 18769 11:49:10,880 --> 11:49:15,956 dynamism for now so let me allocate 18770 11:49:13,240 --> 11:49:17,400 three times whatever the size is of an 18771 11:49:15,956 --> 11:49:19,116 INT but it's usually going to be four 18772 11:49:17,400 --> 11:49:21,840 bytes as we know so this is really going 18773 11:49:19,116 --> 11:49:24,756 to be 3 * 4al 12 but it's a little more 18774 11:49:21,840 --> 11:49:26,560 Dynamic and now what can I do down here 18775 11:49:24,756 --> 11:49:30,720 well this is just a chunk of memory 18776 11:49:26,560 --> 11:49:35,360 so I can do literally list bracket 0 uh 18777 11:49:30,720 --> 11:49:39,560 equal 1 list bracket 1 = 2 list bracket 18778 11:49:35,360 --> 11:49:41,800 2 equals 3 and voila achieve the exact 18779 11:49:39,560 --> 11:49:43,520 same effect because again an array is 18780 11:49:41,800 --> 11:49:46,200 just a chunk of contiguous memory but 18781 11:49:43,520 --> 11:49:48,000 malok gives you any old chunk of 18782 11:49:46,200 --> 11:49:50,560 contiguous memory so you can rather 18783 11:49:48,000 --> 11:49:52,116 treat one like the other here now if you 18784 11:49:50,560 --> 11:49:54,116 really want to be cool you could do 18785 11:49:52,116 --> 11:49:57,116 something like this instead you could 18786 11:49:54,116 --> 11:49:58,916 dreference the address in list and go 18787 11:49:57,116 --> 11:50:02,436 there you could go down here and 18788 11:49:58,916 --> 11:50:04,276 dreference list + one and go there but 18789 11:50:02,436 --> 11:50:06,000 honestly no one really writes code like 18790 11:50:04,276 --> 11:50:07,720 this it's just too cryptic it's a little 18791 11:50:06,000 --> 11:50:09,916 too far over the line at least for most 18792 11:50:07,720 --> 11:50:11,520 people and so I think the syntactic 18793 11:50:09,916 --> 11:50:13,040 sugar as I keep describing it just the 18794 11:50:11,520 --> 11:50:14,840 more user friendly square bracket 18795 11:50:13,040 --> 11:50:16,436 notation does the exact same thing 18796 11:50:14,840 --> 11:50:18,880 figures out the pointer arithmetic and 18797 11:50:16,436 --> 11:50:21,596 puts each of these integers in the right 18798 11:50:18,880 --> 11:50:23,480 chunks therein now just to be super 18799 11:50:21,596 --> 11:50:26,720 pedantic let me make sure if something 18800 11:50:23,480 --> 11:50:28,116 went wrong so if list equals equals n 18801 11:50:26,720 --> 11:50:29,880 that means that something went wrong 18802 11:50:28,116 --> 11:50:31,916 like my computer is out of memory which 18803 11:50:29,880 --> 11:50:34,520 we should check for typically so let me 18804 11:50:31,916 --> 11:50:36,080 just immediately return one signaling 18805 11:50:34,520 --> 11:50:37,880 anything other than zero which means 18806 11:50:36,080 --> 11:50:39,800 success typically just to get out of 18807 11:50:37,880 --> 11:50:42,800 this program because something's wrong 18808 11:50:39,800 --> 11:50:45,916 but now let me propose that I've had a 18809 11:50:42,800 --> 11:50:48,756 uh well let's do this for in I gets zero 18810 11:50:45,916 --> 11:50:50,596 I less than three i++ though a better 18811 11:50:48,756 --> 11:50:52,240 design would always be to use a con but 18812 11:50:50,596 --> 11:50:54,080 I'm just doing this for demonstration 18813 11:50:52,240 --> 11:50:55,680 sake let's print out each of these ins 18814 11:50:54,080 --> 11:50:58,000 too and just make sure I didn't mess 18815 11:50:55,680 --> 11:51:01,240 anything up and let me open my terminal 18816 11:50:58,000 --> 11:51:04,720 window again let me do make list 18817 11:51:01,240 --> 11:51:07,116 again okay huh implicitly declaring 18818 11:51:04,720 --> 11:51:10,320 Library function Malo with type void 18819 11:51:07,116 --> 11:51:12,916 star something something implicitly de 18820 11:51:10,320 --> 11:51:16,276 declaring is the operative words there 18821 11:51:12,916 --> 11:51:18,596 what did I mess up 18822 11:51:16,276 --> 11:51:21,320 yeah yeah I forgot the header file in 18823 11:51:18,596 --> 11:51:23,560 which malok is declared I remember now 18824 11:51:21,320 --> 11:51:25,116 okay that's in standard li. and it's 18825 11:51:23,560 --> 11:51:28,436 fine to look stuff like that up if you 18826 11:51:25,116 --> 11:51:30,680 forget so let me include standard libh 18827 11:51:28,436 --> 11:51:34,480 now let me clear my terminal run make 18828 11:51:30,680 --> 11:51:38,750 list again okay now we're good /list and 18829 11:51:34,480 --> 11:51:40,000 now what did I do 18830 11:51:38,750 --> 11:51:43,640 [Music] 18831 11:51:40,000 --> 11:51:47,520 wrong oh okay not intended but teachable 18832 11:51:43,640 --> 11:51:49,880 moment what did I do wrong 18833 11:51:47,520 --> 11:51:52,680 yeah yeah I'm printing the values of i 18834 11:51:49,880 --> 11:51:54,956 instead of what is at location I in the 18835 11:51:52,680 --> 11:51:56,756 array so what I actually meant to do was 18836 11:51:54,956 --> 11:51:58,480 print this out thank you you so now let 18837 11:51:56,756 --> 11:52:00,880 me recompile make 18838 11:51:58,480 --> 11:52:02,680 listlist and now okay those are the 18839 11:52:00,880 --> 11:52:05,276 three values I was expecting not the 18840 11:52:02,680 --> 11:52:07,000 indices thereof now let me propose that 18841 11:52:05,276 --> 11:52:09,800 for the sake of discussion that I I 18842 11:52:07,000 --> 11:52:12,520 regret having only allocated space for 18843 11:52:09,800 --> 11:52:14,680 three integers and maybe I really should 18844 11:52:12,520 --> 11:52:15,880 have allocated enough space for four now 18845 11:52:14,680 --> 11:52:17,400 this is not how you would do this in 18846 11:52:15,880 --> 11:52:19,040 practice because presumably if you have 18847 11:52:17,400 --> 11:52:21,276 a change of thought just go back in and 18848 11:52:19,040 --> 11:52:23,080 correct the code but let me propose that 18849 11:52:21,276 --> 11:52:25,200 somewhere in here is a more complicated 18850 11:52:23,080 --> 11:52:26,880 program and time passes dot dot dot 18851 11:52:25,200 --> 11:52:29,276 there's a lot of other interesting code 18852 11:52:26,880 --> 11:52:31,520 there but at some point I might want to 18853 11:52:29,276 --> 11:52:33,240 give myself more memory so how can I do 18854 11:52:31,520 --> 11:52:35,560 this well let me just ask the operating 18855 11:52:33,240 --> 11:52:37,000 system now for four new bytes of memory 18856 11:52:35,560 --> 11:52:38,840 so that we can at least in version one 18857 11:52:37,000 --> 11:52:41,080 implement the ID on the board where I 18858 11:52:38,840 --> 11:52:43,596 just copied the three bytes into the new 18859 11:52:41,080 --> 11:52:45,360 four bytes and then added a fourth value 18860 11:52:43,596 --> 11:52:47,320 so I'm going to use malok again and I'm 18861 11:52:45,360 --> 11:52:49,560 going to say here's a new pointer I'll 18862 11:52:47,320 --> 11:52:51,200 call it temp TMP for short which is 18863 11:52:49,560 --> 11:52:53,360 quite common when you just need it 18864 11:52:51,200 --> 11:52:55,800 briefly I'm going to then call malok 18865 11:52:53,360 --> 11:52:58,840 again I'm going to say give me four 18866 11:52:55,800 --> 11:53:02,520 integers using size of let me again make 18867 11:52:58,840 --> 11:53:03,956 sure so if temp equals equals null 18868 11:53:02,520 --> 11:53:06,160 something went wrong so let me just 18869 11:53:03,956 --> 11:53:09,160 immediately return one and for good 18870 11:53:06,160 --> 11:53:11,720 measure before I return one one let me 18871 11:53:09,160 --> 11:53:13,596 free the original list so that I don't 18872 11:53:11,720 --> 11:53:15,680 leak memory so I'm not just immediately 18873 11:53:13,596 --> 11:53:18,840 returning one I'm being a good citizen 18874 11:53:15,680 --> 11:53:21,560 and remembering well if this Malo call 18875 11:53:18,840 --> 11:53:25,040 did succeed and indeed I got as far as 18876 11:53:21,560 --> 11:53:27,756 line 18 but then line 18 failed I should 18877 11:53:25,040 --> 11:53:29,160 free the memory that I previously macked 18878 11:53:27,756 --> 11:53:30,880 so again that's the rule of thumb if you 18879 11:53:29,160 --> 11:53:33,240 allocate it you should be the one to 18880 11:53:30,880 --> 11:53:34,756 free it even before you're about to quit 18881 11:53:33,240 --> 11:53:36,800 now once I've done that I think I need 18882 11:53:34,756 --> 11:53:38,916 to do what we did pictorially on the 18883 11:53:36,800 --> 11:53:41,320 screen where I need to copy the One the 18884 11:53:38,916 --> 11:53:43,436 two the three from the old array into 18885 11:53:41,320 --> 11:53:46,480 the new so how might I do this well let 18886 11:53:43,436 --> 11:53:49,000 me give myself a loop so for in I gets 18887 11:53:46,480 --> 11:53:51,720 zero I less than three i++ because the 18888 11:53:49,000 --> 11:53:53,956 size of the original is still the same 18889 11:53:51,720 --> 11:53:57,116 let me go ahead and treat the new chunk 18890 11:53:53,956 --> 11:53:59,080 of memory called 10 temp as an array 18891 11:53:57,116 --> 11:54:00,400 itself and so I can absolutely use these 18892 11:53:59,080 --> 11:54:02,160 square brackets just like before it's 18893 11:54:00,400 --> 11:54:04,596 just a chunk of memory I'm treating it 18894 11:54:02,160 --> 11:54:06,956 like an array and let me add to that 18895 11:54:04,596 --> 11:54:09,560 value whatever is at the original list 18896 11:54:06,956 --> 11:54:12,320 at location I as well so this again is 18897 11:54:09,560 --> 11:54:15,680 just this exercise of copying from right 18898 11:54:12,320 --> 11:54:18,160 uh from old to new step by step the one 18899 11:54:15,680 --> 11:54:20,720 the two and the three but I still need 18900 11:54:18,160 --> 11:54:23,680 one additional step if my goal at hand 18901 11:54:20,720 --> 11:54:25,200 now is to have ultimately a fourth value 18902 11:54:23,680 --> 11:54:26,916 here well I'm just going to hard coose 18903 11:54:25,200 --> 11:54:29,000 this for demonstration sake and I'm 18904 11:54:26,916 --> 11:54:32,240 going to go to the very last location of 18905 11:54:29,000 --> 11:54:35,840 temp which is of size four which means 18906 11:54:32,240 --> 11:54:38,840 the last element in temp is temp 18907 11:54:35,840 --> 11:54:40,840 bracket three because it's zero indexed 18908 11:54:38,840 --> 11:54:42,240 but there's four total spaces there and 18909 11:54:40,840 --> 11:54:43,916 I'm just going to arbitrarily for the 18910 11:54:42,240 --> 11:54:47,276 sake of discussion put the number four 18911 11:54:43,916 --> 11:54:50,116 there and that is what happened when we 18912 11:54:47,276 --> 11:54:52,640 proposed uh changing the final garbage 18913 11:54:50,116 --> 11:54:54,400 value there to that four but now I need 18914 11:54:52,640 --> 11:54:56,320 to do what the slide did for us sort of 18915 11:54:54,400 --> 11:54:58,520 magically on the screen I should now do 18916 11:54:56,320 --> 11:55:00,320 a couple of final things I should free 18917 11:54:58,520 --> 11:55:02,640 the original list which I've not done 18918 11:55:00,320 --> 11:55:04,640 yet CU I only called free earlier in 18919 11:55:02,640 --> 11:55:08,160 cases of error and that was just to be 18920 11:55:04,640 --> 11:55:10,956 safe I can now free the list and now if 18921 11:55:08,160 --> 11:55:14,360 I want to inform the computer that I 18922 11:55:10,956 --> 11:55:16,560 want list quote unquote my variable 18923 11:55:14,360 --> 11:55:18,596 called list to point at not the old 18924 11:55:16,560 --> 11:55:21,320 chunk like it originally did but the new 18925 11:55:18,596 --> 11:55:22,680 chunk I think I can just do this list 18926 11:55:21,320 --> 11:55:25,080 equals 18927 11:55:22,680 --> 11:55:27,116 TMP and again that's just saying that if 18928 11:55:25,080 --> 11:55:29,436 list is a pointer which it was cuz look 18929 11:55:27,116 --> 11:55:33,240 at the very top line here on line eight 18930 11:55:29,436 --> 11:55:36,480 on line six I declared list to be a 18931 11:55:33,240 --> 11:55:38,480 pointer uh to a chunk of memory temp 18932 11:55:36,480 --> 11:55:41,480 meanwhile is a separate pointer to a 18933 11:55:38,480 --> 11:55:44,080 chunk of memory so down here this line 18934 11:55:41,480 --> 11:55:45,560 33 is just a matter of my saying okay 18935 11:55:44,080 --> 11:55:47,756 now henceforth because I've already 18936 11:55:45,560 --> 11:55:49,680 freed the old chunk of memory my list 18937 11:55:47,756 --> 11:55:52,000 variable should Point not at this chunk 18938 11:55:49,680 --> 11:55:55,840 of three bytes but this chunk of four 18939 11:55:52,000 --> 11:55:58,720 bytes or really 12 in total now uh or 18940 11:55:55,840 --> 11:56:02,596 rather 16 now because we have four such 18941 11:55:58,720 --> 11:56:05,436 bytes questions now on this code the 18942 11:56:02,596 --> 11:56:06,916 point of which was quite simply to 18943 11:56:05,436 --> 11:56:09,880 demonstrate how we could Implement and 18944 11:56:06,916 --> 11:56:12,360 code this idea of Fairly correctly but 18945 11:56:09,880 --> 11:56:14,756 inefficiently allocating a new array of 18946 11:56:12,360 --> 11:56:16,916 sufficient size and then populating it 18947 11:56:14,756 --> 11:56:21,240 with a new fourth 18948 11:56:16,916 --> 11:56:24,400 value questions on what we've just done 18949 11:56:21,240 --> 11:56:27,400 here no 18950 11:56:24,400 --> 11:56:27,400 yeah 18951 11:56:30,070 --> 11:56:35,360 [Music] 18952 11:56:33,240 --> 11:56:38,520 good question at this point in the story 18953 11:56:35,360 --> 11:56:40,160 with line 33 do I not have two different 18954 11:56:38,520 --> 11:56:42,320 variables pointing at the same chunk of 18955 11:56:40,160 --> 11:56:44,040 memory short answer yes but here's where 18956 11:56:42,320 --> 11:56:46,080 the semantics are perhaps compelling 18957 11:56:44,040 --> 11:56:47,880 list is the variable that I intend to 18958 11:56:46,080 --> 11:56:49,640 use longer term and keep around in 18959 11:56:47,880 --> 11:56:51,200 memory and again assume that there's 18960 11:56:49,640 --> 11:56:52,640 even more code going on here that we 18961 11:56:51,200 --> 11:56:54,880 just didn't write yet so it's useful to 18962 11:56:52,640 --> 11:56:58,200 have that that variable temp was just 18963 11:56:54,880 --> 11:57:00,116 kind of a necessary evil because up here 18964 11:56:58,200 --> 11:57:02,276 it would not have been correct to do 18965 11:57:00,116 --> 11:57:05,480 this it would not have been correct to 18966 11:57:02,276 --> 11:57:07,116 say list on line 18 equals the new chunk 18967 11:57:05,480 --> 11:57:10,800 of memory because this would have 18968 11:57:07,116 --> 11:57:13,640 represented a memory leak if I pre uh 18969 11:57:10,800 --> 11:57:15,720 prematurely change temp to point not at 18970 11:57:13,640 --> 11:57:17,560 the old chunk but the new chunk at that 18971 11:57:15,720 --> 11:57:19,916 point no one's pointing at the old chunk 18972 11:57:17,560 --> 11:57:21,400 and so I've lost those three bites vren 18973 11:57:19,916 --> 11:57:24,640 for instance would yell at you for 18974 11:57:21,400 --> 11:57:27,116 having lost as many btes in memory so in 18975 11:57:24,640 --> 11:57:29,040 this case here I do leave this as temp 18976 11:57:27,116 --> 11:57:30,720 yes it's duplicative at this point but 18977 11:57:29,040 --> 11:57:32,276 it's it's not a huge deal if it was just 18978 11:57:30,720 --> 11:57:35,640 meant semantically to be a temporary 18979 11:57:32,276 --> 11:57:38,680 value but down here at the risk of one 18980 11:57:35,640 --> 11:57:40,640 more line of code I still want to to be 18981 11:57:38,680 --> 11:57:43,040 a good citizen free list and maybe just 18982 11:57:40,640 --> 11:57:46,596 for good measure return zero explicitly 18983 11:57:43,040 --> 11:57:50,596 but notice it's not doing it twice per 18984 11:57:46,596 --> 11:57:53,800 se on line 31 what am I freeing the 18985 11:57:50,596 --> 11:57:56,480 original address of list the three 18986 11:57:53,800 --> 11:57:57,956 integer version then I change what list 18987 11:57:56,480 --> 11:57:59,320 points at so it's pointing at a 18988 11:57:57,956 --> 11:58:01,436 completely different chunk of memory 18989 11:57:59,320 --> 11:58:03,360 this one of size four So eventually when 18990 11:58:01,436 --> 11:58:05,560 I'm all done using this memory for this 18991 11:58:03,360 --> 11:58:07,480 demonstration I still need to free list 18992 11:58:05,560 --> 11:58:09,360 but at this point in the story line 40 18993 11:58:07,480 --> 11:58:11,680 it's pointing at the new chunk of memory 18994 11:58:09,360 --> 11:58:15,720 which I similarly need to hand back to 18995 11:58:11,680 --> 11:58:15,720 the operating system by free 18996 11:58:16,840 --> 11:58:22,200 yeah when would temp equal null so let 18997 11:58:19,880 --> 11:58:24,080 me scroll back up slightly this is being 18998 11:58:22,200 --> 11:58:27,596 a good citizen and a good programmer 18999 11:58:24,080 --> 11:58:30,480 whenever it comes to using malok malok 19000 11:58:27,596 --> 11:58:32,200 can return null if the computer's out of 19001 11:58:30,480 --> 11:58:33,756 memory so this is maybe a much bigger 19002 11:58:32,200 --> 11:58:35,756 program you've got other things going on 19003 11:58:33,756 --> 11:58:38,200 in it and so you just don't have enough 19004 11:58:35,756 --> 11:58:39,680 memory available to be handed malok 19005 11:58:38,200 --> 11:58:42,400 needs to signal to you that there's some 19006 11:58:39,680 --> 11:58:44,720 error and so it will by convention per 19007 11:58:42,400 --> 11:58:46,916 the documentation per the manual pages 19008 11:58:44,720 --> 11:58:48,640 return null so this is just me being a 19009 11:58:46,916 --> 11:58:50,240 good citizen otherwise here's another 19010 11:58:48,640 --> 11:58:52,360 error that might cause your program to 19011 11:58:50,240 --> 11:58:54,276 crash with a segmentation fault if you 19012 11:58:52,360 --> 11:58:57,956 get back null but you assume that it's 19013 11:58:54,276 --> 11:59:00,276 good memory uh going to address zero AKA 19014 11:58:57,956 --> 11:59:02,100 null will crash your program 19015 11:59:00,276 --> 11:59:06,400 intentionally 19016 11:59:02,100 --> 11:59:06,400 [Music] 19017 11:59:08,436 --> 11:59:15,400 yeah correct if I were to change my 19018 11:59:10,880 --> 11:59:18,276 final line 40 here to be free temp this 19019 11:59:15,400 --> 11:59:20,520 would also work as well and here this is 19020 11:59:18,276 --> 11:59:22,240 really a matter of design it's a very 19021 11:59:20,520 --> 11:59:24,480 nitpicky thing we could probably debate 19022 11:59:22,240 --> 11:59:27,276 it but because at this point in the 19023 11:59:24,480 --> 11:59:29,276 story my main variable for remembering 19024 11:59:27,276 --> 11:59:31,000 where the list is is called list this is 19025 11:59:29,276 --> 11:59:32,916 sort of the more responsible way to do 19026 11:59:31,000 --> 11:59:35,320 it freeing the list just so that my 19027 11:59:32,916 --> 11:59:37,200 colleagues my ta doesn't sort of wonder 19028 11:59:35,320 --> 11:59:38,956 why are you freeing temporary ver memory 19029 11:59:37,200 --> 11:59:40,436 that you already freed like it just is a 19030 11:59:38,956 --> 11:59:42,956 semantic thing at this point but good 19031 11:59:40,436 --> 11:59:45,436 Instinct it would also work correct 19032 11:59:42,956 --> 11:59:46,756 maybe just not good design all right so 19033 11:59:45,436 --> 11:59:48,520 it turns out that this gets annoying 19034 11:59:46,756 --> 11:59:50,596 quickly as it did in the picture of 19035 11:59:48,520 --> 11:59:52,276 doing all of this duplication and even 19036 11:59:50,596 --> 11:59:54,240 though technically it's necessary to 19037 11:59:52,276 --> 11:59:55,680 copy those values if you need a newer 19038 11:59:54,240 --> 11:59:58,276 bigger chunk of memory 19039 11:59:55,680 --> 12:00:00,756 there is at least a function in C that 19040 11:59:58,276 --> 12:00:03,000 simplifies a lot of this for us and in 19041 12:00:00,756 --> 12:00:06,276 fact let me go ahead and do this instead 19042 12:00:03,000 --> 12:00:09,080 of using malok this second time on line 19043 12:00:06,276 --> 12:00:11,520 18 in addition to the first time I used 19044 12:00:09,080 --> 12:00:13,276 it on line six I'm actually going to try 19045 12:00:11,520 --> 12:00:15,680 and introduce another function called 19046 12:00:13,276 --> 12:00:18,200 realloc which as the name suggests tries 19047 12:00:15,680 --> 12:00:20,276 to reallocate memory for you and it 19048 12:00:18,200 --> 12:00:23,000 works a little differently from malok 19049 12:00:20,276 --> 12:00:25,160 realloc expects two arguments the first 19050 12:00:23,000 --> 12:00:27,640 one is what is the chunk of memory that 19051 12:00:25,160 --> 12:00:30,436 you want to try to grow or Shrink that 19052 12:00:27,640 --> 12:00:32,400 is reallocate to be a different size and 19053 12:00:30,436 --> 12:00:35,160 then you specify what size you would 19054 12:00:32,400 --> 12:00:39,116 want and indeed in this case I want four 19055 12:00:35,160 --> 12:00:42,720 times size of int and that will now give 19056 12:00:39,116 --> 12:00:45,640 me hopefully a new address of a chunk of 19057 12:00:42,720 --> 12:00:48,240 memory that's big enough to fit all four 19058 12:00:45,640 --> 12:00:51,756 numbers but what's wonderful about 19059 12:00:48,240 --> 12:00:53,956 realloc is that it will handle all of 19060 12:00:51,756 --> 12:00:56,160 the copying for me so in fact I'm going 19061 12:00:53,956 --> 12:00:59,436 to go down here I'm going to get rid of 19062 12:00:56,160 --> 12:01:02,200 all of this this extra for Loop and what 19063 12:00:59,436 --> 12:01:03,640 I'm simply going to do instead is this 19064 12:01:02,200 --> 12:01:07,680 once I can 19065 12:01:03,640 --> 12:01:09,720 trust after lines 18 through 23 that 19066 12:01:07,680 --> 12:01:11,840 rioc worked and it didn't return null 19067 12:01:09,720 --> 12:01:13,880 because I'm out of memory I can just say 19068 12:01:11,840 --> 12:01:16,240 okay just immediately remember that the 19069 12:01:13,880 --> 12:01:18,916 new list points at this new chunk of 19070 12:01:16,240 --> 12:01:20,680 memory instead and then I can still now 19071 12:01:18,916 --> 12:01:22,720 do this line but I can tweak the 19072 12:01:20,680 --> 12:01:28,680 semantics here and just say list bracket 19073 12:01:22,720 --> 12:01:31,436 3 the new Final a in uh um the new list 19074 12:01:28,680 --> 12:01:33,756 is for I don't need to free this here I 19075 12:01:31,436 --> 12:01:35,800 don't need to do this all I need now at 19076 12:01:33,756 --> 12:01:37,840 the bottom is the final for Loop to just 19077 12:01:35,800 --> 12:01:39,756 print out these values so in short even 19078 12:01:37,840 --> 12:01:43,040 though that was somewhat quick using 19079 12:01:39,756 --> 12:01:44,916 realloc just moves the entire copying 19080 12:01:43,040 --> 12:01:46,800 process that I implemented myself a 19081 12:01:44,916 --> 12:01:48,756 moment ago using a for Loop it just 19082 12:01:46,800 --> 12:01:50,160 moves it to realloc and lets it deal 19083 12:01:48,756 --> 12:01:52,000 with the copying for me it's no more 19084 12:01:50,160 --> 12:01:53,596 efficient but at least means uh I'm 19085 12:01:52,000 --> 12:01:55,360 writing less code which is more pleasant 19086 12:01:53,596 --> 12:01:57,360 and hopefully the people who wrote aloc 19087 12:01:55,360 --> 12:01:59,360 or realloc are smarter than me and they 19088 12:01:57,360 --> 12:02:00,880 just will introduce bugs with lower 19089 12:01:59,360 --> 12:02:04,120 probability 19090 12:02:00,880 --> 12:02:07,169 too all right that was a lot any 19091 12:02:04,120 --> 12:02:07,169 [Music] 19092 12:02:07,480 --> 12:02:12,560 questions good question why do you still 19093 12:02:09,800 --> 12:02:17,560 need to make list equal temp as I did on 19094 12:02:12,560 --> 12:02:21,160 line 24 so ideally I would do this 19095 12:02:17,560 --> 12:02:23,756 ideally I would just change this line 18 19096 12:02:21,160 --> 12:02:25,596 to be list that is to say call re or 19097 12:02:23,756 --> 12:02:27,680 actually even better ideally I would 19098 12:02:25,596 --> 12:02:29,916 just say realloc this list to be of this 19099 12:02:27,680 --> 12:02:31,916 new size but again things can go wrong 19100 12:02:29,916 --> 12:02:33,596 when allocating memory you need to check 19101 12:02:31,916 --> 12:02:36,160 a return value to see if it was 19102 12:02:33,596 --> 12:02:37,916 successful or not and so we need to use 19103 12:02:36,160 --> 12:02:40,640 a return value okay so let's not 19104 12:02:37,916 --> 12:02:42,840 introduce temp let's just use list but 19105 12:02:40,640 --> 12:02:46,116 here's where a memory leak might happen 19106 12:02:42,840 --> 12:02:48,000 in the off chance realloc fails and 19107 12:02:46,116 --> 12:02:50,560 doesn't have enough memory for your four 19108 12:02:48,000 --> 12:02:53,480 bytes therefore it returns by definition 19109 12:02:50,560 --> 12:02:55,480 null you can't overwrite the original 19110 12:02:53,480 --> 12:02:58,000 value of list with null 19111 12:02:55,480 --> 12:02:59,956 to then check it why because now who 19112 12:02:58,000 --> 12:03:02,360 remembers where the original Three byes 19113 12:02:59,956 --> 12:03:04,840 were if you prematurely change the value 19114 12:03:02,360 --> 12:03:06,880 of list you've lost you've uh leaked 19115 12:03:04,840 --> 12:03:09,080 memory in that sense and so that's why 19116 12:03:06,880 --> 12:03:12,000 let me undo this change I declare a 19117 12:03:09,080 --> 12:03:13,840 temporary pointer for the sole purpose 19118 12:03:12,000 --> 12:03:16,116 of making sure I can check the return 19119 12:03:13,840 --> 12:03:18,320 value and then once it's good now I'll 19120 12:03:16,116 --> 12:03:20,756 update the value of list so it's sort of 19121 12:03:18,320 --> 12:03:23,000 doing a Switcheroo by making sure first 19122 12:03:20,756 --> 12:03:26,080 that you have a new value to swap with 19123 12:03:23,000 --> 12:03:28,956 the old other question 19124 12:03:26,080 --> 12:03:28,956 on this code 19125 12:03:32,596 --> 12:03:37,200 yeah indeed realloc automatically frees 19126 12:03:35,480 --> 12:03:39,276 the previous memory for you and better 19127 12:03:37,200 --> 12:03:42,080 yet it's even smarter than that if you 19128 12:03:39,276 --> 12:03:43,720 get lucky and they happens to be space 19129 12:03:42,080 --> 12:03:46,160 right after your existing chunk of 19130 12:03:43,720 --> 12:03:48,560 memory so one two three garbage value 19131 12:03:46,160 --> 12:03:51,080 instead of one two three hello world 19132 12:03:48,560 --> 12:03:53,560 realloc won't even bother copying things 19133 12:03:51,080 --> 12:03:55,400 from old to new it will just say okay 19134 12:03:53,560 --> 12:03:57,480 I'm going to now reserve for you more 19135 12:03:55,400 --> 12:03:59,200 bites than you originally asked for so 19136 12:03:57,480 --> 12:04:00,640 it doesn't have to waste time doing that 19137 12:03:59,200 --> 12:04:02,480 copying and so in that sense this 19138 12:04:00,640 --> 12:04:04,320 version is now not only still correct 19139 12:04:02,480 --> 12:04:06,160 it's even better designed because we're 19140 12:04:04,320 --> 12:04:07,800 not wasting time with that for Loop we 19141 12:04:06,160 --> 12:04:09,480 might have to resort to it if there is 19142 12:04:07,800 --> 12:04:11,520 in fact hello world or something else in 19143 12:04:09,480 --> 12:04:14,436 the way but hopefully we'll get lucky 19144 12:04:11,520 --> 12:04:18,880 and save those steps other questions on 19145 12:04:14,436 --> 12:04:22,240 this this manipulation of code 19146 12:04:18,880 --> 12:04:22,240 here yeah in the 19147 12:04:23,680 --> 12:04:27,840 middle 19148 12:04:26,520 --> 12:04:30,480 what if you want to resize a 19149 12:04:27,840 --> 12:04:33,160 two-dimensional array so very similar in 19150 12:04:30,480 --> 12:04:35,160 Spirit uh whereby you can use the same 19151 12:04:33,160 --> 12:04:37,480 trickery let me wave my hand at that for 19152 12:04:35,160 --> 12:04:38,916 now just because I think that's going to 19153 12:04:37,480 --> 12:04:40,520 sort of significantly increase the 19154 12:04:38,916 --> 12:04:42,560 complexity but very same Primitives 19155 12:04:40,520 --> 12:04:45,080 ultimately a two-dimensional array is 19156 12:04:42,560 --> 12:04:47,680 essentially just a doubly long or 19157 12:04:45,080 --> 12:04:49,640 quadratically longer list of memory that 19158 12:04:47,680 --> 12:04:51,720 using square bracket notation is doing 19159 12:04:49,640 --> 12:04:53,560 some of that mental math for you but 19160 12:04:51,720 --> 12:04:55,560 it's fundamentally no different of 19161 12:04:53,560 --> 12:04:59,276 what's going on underneath the 19162 12:04:55,560 --> 12:05:01,080 hood all right so with that said and 19163 12:04:59,276 --> 12:05:02,640 that code under our belt even though 19164 12:05:01,080 --> 12:05:04,916 that's not going to be something you'll 19165 12:05:02,640 --> 12:05:07,400 frequently need to code yourself let's 19166 12:05:04,916 --> 12:05:09,320 propose now how we might go about 19167 12:05:07,400 --> 12:05:12,116 building some actual data structures 19168 12:05:09,320 --> 12:05:15,400 ourselves the new ingredient here being 19169 12:05:12,116 --> 12:05:17,360 this reality that if you want to resize 19170 12:05:15,400 --> 12:05:19,520 a chunk of memory so is to make room for 19171 12:05:17,360 --> 12:05:21,436 things we now have that ability memory 19172 12:05:19,520 --> 12:05:23,240 addresses and pointers just give us the 19173 12:05:21,436 --> 12:05:25,200 ability to like Point around at things 19174 12:05:23,240 --> 12:05:27,320 and move things around in memory but now 19175 12:05:25,200 --> 12:05:29,240 that we have malok and even realloc you 19176 12:05:27,320 --> 12:05:32,240 can imagine maybe rewinding and you 19177 12:05:29,240 --> 12:05:34,116 could Implement that stack that Q using 19178 12:05:32,240 --> 12:05:36,200 not an array per se because you have to 19179 12:05:34,116 --> 12:05:38,276 commit to an array size in advance but 19180 12:05:36,200 --> 12:05:40,880 if you implement your stack or your que 19181 12:05:38,276 --> 12:05:42,680 using a pointer and then malok and 19182 12:05:40,880 --> 12:05:44,560 realloc and maybe someone else writes 19183 12:05:42,680 --> 12:05:47,200 all that code for you perhaps now you 19184 12:05:44,560 --> 12:05:50,040 can imagine that okay now the stack can 19185 12:05:47,200 --> 12:05:52,080 grow or Shrink by using realloc 19186 12:05:50,040 --> 12:05:54,640 accordingly you don't have to preact pre 19187 12:05:52,080 --> 12:05:57,240 um preemptively say give me five bytes 19188 12:05:54,640 --> 12:05:59,116 or 50 or 500 or 5,000 you can say just 19189 12:05:57,240 --> 12:06:01,436 give me one initially and if I need more 19190 12:05:59,116 --> 12:06:03,200 I'll realloc realloc reloc and if you 19191 12:06:01,436 --> 12:06:05,160 keep popping things off the stack you 19192 12:06:03,200 --> 12:06:07,200 can realloc in the other direction and 19193 12:06:05,160 --> 12:06:08,840 ask for fewer and fewer bytes and the 19194 12:06:07,200 --> 12:06:10,560 operating system can take that memory 19195 12:06:08,840 --> 12:06:12,360 back as well so we now have this 19196 12:06:10,560 --> 12:06:14,360 building block let's see what we can do 19197 12:06:12,360 --> 12:06:16,160 with it so we've had a few pieces of 19198 12:06:14,360 --> 12:06:17,756 syntax in recent weeks all of which 19199 12:06:16,160 --> 12:06:20,480 we're going to combine now in just a 19200 12:06:17,756 --> 12:06:22,000 slightly more clever way so struct is 19201 12:06:20,480 --> 12:06:23,360 this keyword in C that lets us build our 19202 12:06:22,000 --> 12:06:25,116 own structure in memory like a 19203 12:06:23,360 --> 12:06:27,116 collection of two or three or more 19204 12:06:25,116 --> 12:06:31,000 variables like a person that we've seen 19205 12:06:27,116 --> 12:06:33,880 before the dot operator recall we've 19206 12:06:31,000 --> 12:06:36,116 used when you do have a struct like a a 19207 12:06:33,880 --> 12:06:39,880 person and you want to go inside of it 19208 12:06:36,116 --> 12:06:42,200 so like uh person. name or person. 19209 12:06:39,880 --> 12:06:43,800 number we did this a few weeks ago now 19210 12:06:42,200 --> 12:06:45,720 but the dot Operator just allows you to 19211 12:06:43,800 --> 12:06:47,880 go inside of a structure and get the 19212 12:06:45,720 --> 12:06:50,040 individual variables within and then the 19213 12:06:47,880 --> 12:06:52,276 star operator unfortunately has a lot of 19214 12:06:50,040 --> 12:06:54,640 uses now one was multiplication like my 19215 12:06:52,276 --> 12:06:57,240 God that was easy back in the day now 19216 12:06:54,640 --> 12:06:59,596 it's used to declare pointers it's also 19217 12:06:57,240 --> 12:07:02,680 used to dreference pointers so to make 19218 12:06:59,596 --> 12:07:04,480 one exist and then go to that address 19219 12:07:02,680 --> 12:07:06,640 unfortunately it's the same symbol for 19220 12:07:04,480 --> 12:07:08,640 all of those but it's all related but 19221 12:07:06,640 --> 12:07:10,040 with these three symbols it turns out 19222 12:07:08,640 --> 12:07:12,680 you're going to get one last one today 19223 12:07:10,040 --> 12:07:15,000 and my God it finally looks like the 19224 12:07:12,680 --> 12:07:17,116 concept it turns out there's a clever 19225 12:07:15,000 --> 12:07:19,116 way anytime you want to use the dot and 19226 12:07:17,116 --> 12:07:21,520 the star together that is to go 19227 12:07:19,116 --> 12:07:23,480 somewhere and go to an address and then 19228 12:07:21,520 --> 12:07:25,916 look inside of a structure you can 19229 12:07:23,480 --> 12:07:27,240 actually literally use an arrow symbol 19230 12:07:25,916 --> 12:07:30,000 on your keyboard it's not a single 19231 12:07:27,240 --> 12:07:32,080 keystroke it's a hyphen and then an open 19232 12:07:30,000 --> 12:07:33,840 angle bracket but at least it looks like 19233 12:07:32,080 --> 12:07:35,276 an arrow and we'll see indeed in code 19234 12:07:33,840 --> 12:07:36,880 today the things I was drawing 19235 12:07:35,276 --> 12:07:38,800 pictorially on the screen last time with 19236 12:07:36,880 --> 12:07:41,720 yellow arrows you can actually now 19237 12:07:38,800 --> 12:07:44,240 Express as well in code and so here we 19238 12:07:41,720 --> 12:07:46,200 have our next data structure called a 19239 12:07:44,240 --> 12:07:48,956 linked list and this is one of the most 19240 12:07:46,200 --> 12:07:50,240 useful powerful Concepts in C it's the 19241 12:07:48,956 --> 12:07:52,240 kind of thing that you can take for 19242 12:07:50,240 --> 12:07:54,436 granted in Java and Python and higher 19243 12:07:52,240 --> 12:07:56,520 level languages but today we'll see how 19244 12:07:54,436 --> 12:07:59,080 we or others can actually build these 19245 12:07:56,520 --> 12:08:01,400 things just using these same Primitives 19246 12:07:59,080 --> 12:08:04,360 so a linked list is going to allow us to 19247 12:08:01,400 --> 12:08:06,240 actually do what we you know used a foam 19248 12:08:04,360 --> 12:08:07,916 finger for last week allow us to link 19249 12:08:06,240 --> 12:08:09,680 together for instance these three values 19250 12:08:07,916 --> 12:08:11,080 maybe with that fourth value over there 19251 12:08:09,680 --> 12:08:13,080 and then if there's a fifth you know 19252 12:08:11,080 --> 12:08:15,200 maybe this other foam finger points even 19253 12:08:13,080 --> 12:08:17,680 farther overway to that fifth value the 19254 12:08:15,200 --> 12:08:19,756 key being that you can stitch together 19255 12:08:17,680 --> 12:08:21,720 fancier data structures without having 19256 12:08:19,756 --> 12:08:23,680 to like pick all of these up and find 19257 12:08:21,720 --> 12:08:25,480 new space you just have to at least 19258 12:08:23,680 --> 12:08:27,880 connect the dots somehow we just need to 19259 12:08:25,480 --> 12:08:29,400 somehow point from one to the other and 19260 12:08:27,880 --> 12:08:31,800 that's going to make things much more 19261 12:08:29,400 --> 12:08:33,360 efficient it would seem so how do we get 19262 12:08:31,800 --> 12:08:35,276 there so here's my computer's memory as 19263 12:08:33,360 --> 12:08:37,360 always suppose that I'm storing the 19264 12:08:35,276 --> 12:08:40,080 value one somewhere in there and it's at 19265 12:08:37,360 --> 12:08:41,956 ox123 address whatever and I'm storing 19266 12:08:40,080 --> 12:08:45,840 the number two somewhere else in memory 19267 12:08:41,956 --> 12:08:49,956 Ox 456 and number three at address Ox 19268 12:08:45,840 --> 12:08:51,840 789 this is not an array by definition 19269 12:08:49,956 --> 12:08:53,436 why even though it's the only three 19270 12:08:51,840 --> 12:08:56,200 things on the screen what makes this not 19271 12:08:53,436 --> 12:08:58,880 an array it's not contiguous so this 19272 12:08:56,200 --> 12:09:00,360 violates the definition of an array but 19273 12:08:58,880 --> 12:09:02,276 you know especially since they're 19274 12:09:00,360 --> 12:09:04,520 sequential it kind of looks to a human 19275 12:09:02,276 --> 12:09:06,640 like a list so it would be nice if there 19276 12:09:04,520 --> 12:09:09,360 were a data type called list and there 19277 12:09:06,640 --> 12:09:11,320 isn't in C there will be in Python but 19278 12:09:09,360 --> 12:09:13,520 you know what if I could somehow like 19279 12:09:11,320 --> 12:09:15,596 Stitch together these three values so I 19280 12:09:13,520 --> 12:09:17,436 can get from one to the next to the next 19281 12:09:15,596 --> 12:09:19,560 then I think we could achieve the idea 19282 12:09:17,436 --> 12:09:21,400 the concept of a list without this 19283 12:09:19,560 --> 12:09:24,880 really annoying constraint that they all 19284 12:09:21,400 --> 12:09:26,320 be contiguous as in an array so how do I 19285 12:09:24,880 --> 12:09:29,116 do that well at the end of the day I 19286 12:09:26,320 --> 12:09:31,320 only have memory at my disposal right 19287 12:09:29,116 --> 12:09:33,116 there's no more training wheels to take 19288 12:09:31,320 --> 12:09:35,680 off here this is what we've got 19289 12:09:33,116 --> 12:09:37,360 underneath the hood of a computer so if 19290 12:09:35,680 --> 12:09:39,520 all I have is memory I think the 19291 12:09:37,360 --> 12:09:42,680 solution to this problem of stitching 19292 12:09:39,520 --> 12:09:44,520 together those values in a list must be 19293 12:09:42,680 --> 12:09:46,276 to spend a bit more memory that's 19294 12:09:44,520 --> 12:09:48,276 literally the only resource we have 19295 12:09:46,276 --> 12:09:50,320 right now so let me propose that if we 19296 12:09:48,276 --> 12:09:52,520 want to create a list conceptually out 19297 12:09:50,320 --> 12:09:55,116 of three values that are in random 19298 12:09:52,520 --> 12:09:57,436 although pictorially pretty positions in 19299 12:09:55,116 --> 12:09:59,116 memory let me just add a little bit more 19300 12:09:57,436 --> 12:10:00,880 memory to the picture so in addition to 19301 12:09:59,116 --> 12:10:02,680 storing the one I'm going to leave my 19302 12:10:00,880 --> 12:10:04,520 space myself some room a little scratch 19303 12:10:02,680 --> 12:10:06,680 pad if you will to use some other bits 19304 12:10:04,520 --> 12:10:09,160 as well same for the two same for the 19305 12:10:06,680 --> 12:10:12,560 three and you can perhaps see where this 19306 12:10:09,160 --> 12:10:17,720 is going based on last week if I want to 19307 12:10:12,560 --> 12:10:20,276 somehow connect the one to the two any 19308 12:10:17,720 --> 12:10:22,240 instincts as to what I should write in 19309 12:10:20,276 --> 12:10:25,840 this box here that would lead me 19310 12:10:22,240 --> 12:10:28,720 effectively from one to the two 19311 12:10:25,840 --> 12:10:28,720 what could go here 19312 12:10:30,116 --> 12:10:33,360 yeah we could store the address of Two 19313 12:10:32,200 --> 12:10:35,800 And so specifically what would you have 19314 12:10:33,360 --> 12:10:35,800 me write 19315 12:10:37,956 --> 12:10:43,040 here perfect ideally I would just put in 19316 12:10:40,720 --> 12:10:45,040 this box another integer one that 19317 12:10:43,040 --> 12:10:46,560 happens to be represented in heximal but 19318 12:10:45,040 --> 12:10:48,756 that's just a base system it's just a 19319 12:10:46,560 --> 12:10:51,800 human thing for us to look at I'm going 19320 12:10:48,756 --> 12:10:54,360 to put the value Ox 456 here so let me 19321 12:10:51,800 --> 12:10:55,956 go ahead and reveal that Ox 456 goes 19322 12:10:54,360 --> 12:10:57,240 there you can perhaps see further where 19323 12:10:55,956 --> 12:10:59,436 this is going well if I want to get from 19324 12:10:57,240 --> 12:11:01,320 the two to the three I think I need to 19325 12:10:59,436 --> 12:11:04,756 put below the two the address of the 19326 12:11:01,320 --> 12:11:06,800 three which gives me Ox 789 now if three 19327 12:11:04,756 --> 12:11:08,720 is the end of the list I don't want to 19328 12:11:06,800 --> 12:11:10,436 let it be some garbage value because 19329 12:11:08,720 --> 12:11:12,276 that would imply that it who knows where 19330 12:11:10,436 --> 12:11:13,720 it's pointing I need some definitive 19331 12:11:12,276 --> 12:11:16,480 value and just what would your instincts 19332 12:11:13,720 --> 12:11:18,720 be if I want to make clear with some 19333 12:11:16,480 --> 12:11:21,116 special Sentinel value that the buck 19334 12:11:18,720 --> 12:11:24,680 stops here what do I put what my my 19335 12:11:21,116 --> 12:11:27,680 options be yeah so null not n per se but 19336 12:11:24,680 --> 12:11:29,520 n l which was the new keyword we 19337 12:11:27,680 --> 12:11:32,200 introduced last week which just 19338 12:11:29,520 --> 12:11:35,200 represents an empty pointer if you will 19339 12:11:32,200 --> 12:11:37,276 technically the address o x0 so 19340 12:11:35,200 --> 12:11:39,000 literally the zero address and what 19341 12:11:37,276 --> 12:11:41,276 humans did years ago they just decided 19342 12:11:39,000 --> 12:11:43,240 you know what nothing should ever live 19343 12:11:41,276 --> 12:11:45,520 at address zero in memory we're just 19344 12:11:43,240 --> 12:11:47,840 going to reserve that one special bite 19345 12:11:45,520 --> 12:11:50,040 to be a special signal a sentinel value 19346 12:11:47,840 --> 12:11:52,080 such that if you ever see a zero address 19347 12:11:50,040 --> 12:11:54,360 in a pointer it just means it's it's 19348 12:11:52,080 --> 12:11:55,596 invalid it does not exist now now we 19349 12:11:54,360 --> 12:11:58,276 write that though a little more 19350 12:11:55,596 --> 12:12:00,200 pleasantly for the eyes as just n l in 19351 12:11:58,276 --> 12:12:01,916 all caps and that's a key word in C as 19352 12:12:00,200 --> 12:12:03,840 well but of course last week I claimed 19353 12:12:01,916 --> 12:12:05,560 that who cares where things are in 19354 12:12:03,840 --> 12:12:07,360 memory and honestly like this quickly 19355 12:12:05,560 --> 12:12:09,400 gets tedious even worrying about these 19356 12:12:07,360 --> 12:12:11,680 values so let me abstract this away and 19357 12:12:09,400 --> 12:12:14,040 propose that if we want to remember 19358 12:12:11,680 --> 12:12:16,040 where all of these numbers are in memory 19359 12:12:14,040 --> 12:12:19,160 let's give oursel one final piece of 19360 12:12:16,040 --> 12:12:21,436 memory that just allows us to start the 19361 12:12:19,160 --> 12:12:23,520 whole process let me allocate on the 19362 12:12:21,436 --> 12:12:26,240 left hand side here not room for a 19363 12:12:23,520 --> 12:12:28,436 number like 1 two 3 just room for a 19364 12:12:26,240 --> 12:12:31,596 pointer that henceforth I think I'll 19365 12:12:28,436 --> 12:12:34,680 call list by convention and then store 19366 12:12:31,596 --> 12:12:36,520 in that one additional pointer a value 19367 12:12:34,680 --> 12:12:38,160 that just kickstarts the whole process 19368 12:12:36,520 --> 12:12:40,320 this is the sort of treasure map if you 19369 12:12:38,160 --> 12:12:43,360 will that you get handed and this has 19370 12:12:40,320 --> 12:12:46,000 the address of the very first actual 19371 12:12:43,360 --> 12:12:47,436 node in memory now technically we could 19372 12:12:46,000 --> 12:12:49,200 just start with this but it turns out 19373 12:12:47,436 --> 12:12:52,000 we'll see it's just a little cleaner to 19374 12:12:49,200 --> 12:12:53,756 use a simple single pointer that leads 19375 12:12:52,000 --> 12:12:55,640 to the things you care about as opposed 19376 12:12:53,756 --> 12:12:57,276 to just starting with the first element 19377 12:12:55,640 --> 12:12:59,276 why well if you ever want to get rid of 19378 12:12:57,276 --> 12:13:00,756 this element it'd be nice if you could 19379 12:12:59,276 --> 12:13:02,720 at least still hang on to an empty sheet 19380 12:13:00,756 --> 12:13:05,000 of paper that indicates that the list is 19381 12:13:02,720 --> 12:13:06,640 empty would be one argument for that so 19382 12:13:05,000 --> 12:13:09,040 again who cares about these addresses 19383 12:13:06,640 --> 12:13:11,240 now now with the wave of the hand let's 19384 12:13:09,040 --> 12:13:13,800 just abstract it away and there are our 19385 12:13:11,240 --> 12:13:16,000 pointers each of those addresses in the 19386 12:13:13,800 --> 12:13:18,916 rec uh the squares at the bottom are 19387 12:13:16,000 --> 12:13:21,080 simply pointing to the next element in 19388 12:13:18,916 --> 12:13:22,520 the list the jargon to introduce here 19389 12:13:21,080 --> 12:13:25,116 would be that now that we have these 19390 12:13:22,520 --> 12:13:27,360 integers 1 2 3 but they're in these like 19391 12:13:25,116 --> 12:13:29,840 wrappers if you will these structures 19392 12:13:27,360 --> 12:13:32,520 that have metadata that is additional 19393 12:13:29,840 --> 12:13:34,400 data that is related to but not the data 19394 12:13:32,520 --> 12:13:36,480 you actually care about this is data 19395 12:13:34,400 --> 12:13:39,520 this is metadata this thing here 19396 12:13:36,480 --> 12:13:40,956 rectangularly we'll call a node n o and 19397 12:13:39,520 --> 12:13:43,596 it's just a term of art that means it's 19398 12:13:40,956 --> 12:13:44,560 like a container in code for storing 19399 12:13:43,596 --> 12:13:47,680 some 19400 12:13:44,560 --> 12:13:50,360 values this then is a linked list and 19401 12:13:47,680 --> 12:13:52,756 this then is the sort of graphical 19402 12:13:50,360 --> 12:13:54,520 incarnation of like one node pointing to 19403 12:13:52,756 --> 12:13:56,480 the other in this case case they happen 19404 12:13:54,520 --> 12:13:59,360 to be by chance and by design of this 19405 12:13:56,480 --> 12:14:00,916 desk contiguous initially but there's no 19406 12:13:59,360 --> 12:14:02,240 requirement that they be such the one 19407 12:14:00,916 --> 12:14:03,480 could be over there the two over there 19408 12:14:02,240 --> 12:14:05,880 the three over there I would just need 19409 12:14:03,480 --> 12:14:08,840 more foam fingers to point at one to the 19410 12:14:05,880 --> 12:14:10,400 next questions on this concept of a 19411 12:14:08,840 --> 12:14:13,800 linked 19412 12:14:10,400 --> 12:14:13,800 list yeah and 19413 12:14:14,270 --> 12:14:20,436 [Music] 19414 12:14:16,916 --> 12:14:20,436 back can you say that 19415 12:14:22,800 --> 12:14:27,360 again a good question do traditional 19416 12:14:25,720 --> 12:14:29,720 arrays start with a pointer that's 19417 12:14:27,360 --> 12:14:32,400 outside of the structure short answer no 19418 12:14:29,720 --> 12:14:34,840 arrays are special in C and certain 19419 12:14:32,400 --> 12:14:37,756 other languages and the name of an array 19420 12:14:34,840 --> 12:14:40,360 is technically a symbol if you will that 19421 12:14:37,756 --> 12:14:42,360 the computer the program knows maps to a 19422 12:14:40,360 --> 12:14:45,956 specific location in memory it's just a 19423 12:14:42,360 --> 12:14:48,276 label a synonym for a memory address it 19424 12:14:45,956 --> 12:14:50,436 does not take up space so to be clear 19425 12:14:48,276 --> 12:14:52,880 the name of an array does not take up 19426 12:14:50,436 --> 12:14:54,480 space like that extra Square on the left 19427 12:14:52,880 --> 12:14:56,116 but you do need that extra Square on the 19428 12:14:54,480 --> 12:14:58,520 left when implementing a link list so 19429 12:14:56,116 --> 12:15:00,240 that you can determine if the list is of 19430 12:14:58,520 --> 12:15:03,116 size zero there's nothing being pointed 19431 12:15:00,240 --> 12:15:05,276 at or size three in this case we're sort 19432 12:15:03,116 --> 12:15:08,276 of taking on more responsibility 19433 12:15:05,276 --> 12:15:08,276 ourselves 19434 12:15:12,040 --> 12:15:16,700 yeah how do you point to the next 19435 12:15:14,360 --> 12:15:19,820 element can you 19436 12:15:16,700 --> 12:15:19,820 [Music] 19437 12:15:20,040 --> 12:15:23,560 elaborate ah good question if each of 19438 12:15:22,160 --> 12:15:25,956 these elements is pointing to the next 19439 12:15:23,560 --> 12:15:27,720 how is three point to the others short 19440 12:15:25,956 --> 12:15:29,276 answer it doesn't at least in this 19441 12:15:27,720 --> 12:15:32,080 design we have more technically what's 19442 12:15:29,276 --> 12:15:34,040 called a singly linked list and as the 19443 12:15:32,080 --> 12:15:36,320 arrows imply it only goes in One 19444 12:15:34,040 --> 12:15:38,320 Direction so if you somehow find incode 19445 12:15:36,320 --> 12:15:40,840 maybe a for Loop maybe a while loop 19446 12:15:38,320 --> 12:15:43,956 somehow you're sort of encode over here 19447 12:15:40,840 --> 12:15:45,916 you have no way in code to go backwards 19448 12:15:43,956 --> 12:15:49,080 unless we changed this to a doubly link 19449 12:15:45,916 --> 12:15:51,240 list where I add another box that lets 19450 12:15:49,080 --> 12:15:53,840 me have arrows in both directions or 19451 12:15:51,240 --> 12:15:56,360 maybe I just kind of make it uh uh 19452 12:15:53,840 --> 12:15:58,360 circular and I connect the three back to 19453 12:15:56,360 --> 12:16:00,240 the one which you can totally do but 19454 12:15:58,360 --> 12:16:01,520 that tends to you know make life harder 19455 12:16:00,240 --> 12:16:02,880 because now you have to figure out when 19456 12:16:01,520 --> 12:16:04,880 you're stuck in a loop in your data 19457 12:16:02,880 --> 12:16:08,000 structure but it's doable as well but as 19458 12:16:04,880 --> 12:16:11,640 is it's a dead end by Design other 19459 12:16:08,000 --> 12:16:11,640 questions on this design 19460 12:16:12,080 --> 12:16:15,756 here all right well how might we 19461 12:16:14,080 --> 12:16:17,116 implement this structure in code well 19462 12:16:15,756 --> 12:16:19,360 let me just connect the dots to 19463 12:16:17,116 --> 12:16:21,000 something like we've seen before here 19464 12:16:19,360 --> 12:16:23,956 like this is how a couple of weeks ago 19465 12:16:21,000 --> 12:16:25,400 we introduced the notion of a a person 19466 12:16:23,956 --> 12:16:27,436 and we claimed a person might have a 19467 12:16:25,400 --> 12:16:28,680 name and a number last week of course we 19468 12:16:27,436 --> 12:16:30,160 took off some of these training wheels 19469 12:16:28,680 --> 12:16:32,720 and a string is really technically a 19470 12:16:30,160 --> 12:16:34,520 Char star in both cases but really 19471 12:16:32,720 --> 12:16:37,680 there's no conceptual difference beyond 19472 12:16:34,520 --> 12:16:40,480 that but let's use this same Paradigm to 19473 12:16:37,680 --> 12:16:42,040 implement a node as I described it in 19474 12:16:40,480 --> 12:16:43,116 that picture so let me get rid of the 19475 12:16:42,040 --> 12:16:45,080 name and the number because that's 19476 12:16:43,116 --> 12:16:47,480 related only to a person and let me 19477 12:16:45,080 --> 12:16:49,756 rename this structure for discussion 19478 12:16:47,480 --> 12:16:52,200 sake to node that then invites the 19479 12:16:49,756 --> 12:16:55,400 question well what needs to go inside of 19480 12:16:52,200 --> 12:16:58,596 a node well minimally an 19481 12:16:55,400 --> 12:17:00,640 integer but this is now where we need to 19482 12:16:58,596 --> 12:17:02,116 think a little harder just conceptually 19483 12:17:00,640 --> 12:17:04,480 even if you have no idea how to type it 19484 12:17:02,116 --> 12:17:06,160 at the keyboard what else needs to be 19485 12:17:04,480 --> 12:17:08,756 part of a node based on these 19486 12:17:06,160 --> 12:17:12,596 rectangular pictures that we've 19487 12:17:08,756 --> 12:17:15,320 drawn what more do we need 19488 12:17:12,596 --> 12:17:17,680 yeah yeah we need a pointer to another 19489 12:17:15,320 --> 12:17:20,400 node so if I don't know how to implement 19490 12:17:17,680 --> 12:17:22,160 this yet you know it could be something 19491 12:17:20,400 --> 12:17:23,956 like you know pointer to another node 19492 12:17:22,160 --> 12:17:26,640 how do I do that well you know what but 19493 12:17:23,956 --> 12:17:30,436 it turns out you would ideally say this 19494 12:17:26,640 --> 12:17:33,000 if you know that the next node is itself 19495 12:17:30,436 --> 12:17:34,916 a node by definition well anytime we've 19496 12:17:33,000 --> 12:17:36,520 needed a pointer we just use the data 19497 12:17:34,916 --> 12:17:38,480 type and a star and I'm going to 19498 12:17:36,520 --> 12:17:41,080 arbitrarily but I think reasonably call 19499 12:17:38,480 --> 12:17:44,040 this second square at the bottom of 19500 12:17:41,080 --> 12:17:46,840 those rectangles next as the name of my 19501 12:17:44,040 --> 12:17:49,360 attribute here but node star just 19502 12:17:46,840 --> 12:17:51,680 connotes that the next variable is going 19503 12:17:49,360 --> 12:17:53,640 to be not a node per se but the address 19504 12:17:51,680 --> 12:17:57,560 of a node and that's exactly what we did 19505 12:17:53,640 --> 12:17:59,596 you had me put Ox 456 Ox 789 in that box 19506 12:17:57,560 --> 12:18:01,200 which is the address of another node so 19507 12:17:59,596 --> 12:18:04,436 the way we would Express this in code 19508 12:18:01,200 --> 12:18:07,160 would be node star next but we could 19509 12:18:04,436 --> 12:18:09,480 call the variable anything we want now 19510 12:18:07,160 --> 12:18:11,116 this is a bit of a white lie but we'll 19511 12:18:09,480 --> 12:18:13,756 fix this right now this code won't 19512 12:18:11,116 --> 12:18:16,200 actually compile C takes you pretty 19513 12:18:13,756 --> 12:18:18,680 literally recall and if you use some 19514 12:18:16,200 --> 12:18:20,640 term at the top of your file that you 19515 12:18:18,680 --> 12:18:22,040 don't Define until later in your file 19516 12:18:20,640 --> 12:18:23,116 you're going to see some error message 19517 12:18:22,040 --> 12:18:25,040 right we've seen this when I've messed 19518 12:18:23,116 --> 12:18:27,080 up and forgot to include the function 19519 12:18:25,040 --> 12:18:29,480 prototypes at the top of my code this is 19520 12:18:27,080 --> 12:18:31,720 related in spirit I seem here on my 1 19521 12:18:29,480 --> 12:18:33,840 two 3 fourth line of code I'm trying to 19522 12:18:31,720 --> 12:18:35,680 use this new term of art that I invented 19523 12:18:33,840 --> 12:18:39,040 here in my code called node even though 19524 12:18:35,680 --> 12:18:40,880 it's a CS term as well but nowhere Above 19525 12:18:39,040 --> 12:18:42,596 This it would seem did I even Define 19526 12:18:40,880 --> 12:18:44,240 what a node is it's not a data type and 19527 12:18:42,596 --> 12:18:45,840 C every computer scientist know what a 19528 12:18:44,240 --> 12:18:48,720 node is but it doesn't come for free 19529 12:18:45,840 --> 12:18:50,480 with the language so I need to do 19530 12:18:48,720 --> 12:18:53,160 something else like I need this word 19531 12:18:50,480 --> 12:18:54,640 here to come first so that I can use it 19532 12:18:53,160 --> 12:18:58,200 here and so we have this sort of Catch 19533 12:18:54,640 --> 12:19:00,080 22 like how can a structure be 19534 12:18:58,200 --> 12:19:02,720 self-referential that is point to 19535 12:19:00,080 --> 12:19:04,596 another version of itself if the word 19536 12:19:02,720 --> 12:19:06,840 doesn't yet exist so the solution to 19537 12:19:04,596 --> 12:19:09,116 this in C which we didn't need for a 19538 12:19:06,840 --> 12:19:11,756 person because there was no notion of 19539 12:19:09,116 --> 12:19:13,880 listing connecting as a list we need one 19540 12:19:11,756 --> 12:19:16,160 more keyword here that we didn't need 19541 12:19:13,880 --> 12:19:18,880 for a person and we reuse that keyword 19542 12:19:16,160 --> 12:19:21,596 here so kind of an annoying detail but 19543 12:19:18,880 --> 12:19:24,840 if we preemptively call this whole thing 19544 12:19:21,596 --> 12:19:26,840 struct node you can now refer to the 19545 12:19:24,840 --> 12:19:29,080 thing on the inside as a struct node 19546 12:19:26,840 --> 12:19:32,200 star but then you can shorten the name 19547 12:19:29,080 --> 12:19:34,276 of the whole thing from struct node to 19548 12:19:32,200 --> 12:19:35,880 just node sort of an annoying sequence 19549 12:19:34,276 --> 12:19:38,360 of steps but in short anytime you're 19550 12:19:35,880 --> 12:19:40,116 building a node a linked list in memory 19551 12:19:38,360 --> 12:19:41,800 this is just the Paradigm you use type 19552 12:19:40,116 --> 12:19:44,116 def struct the name of the thing you 19553 12:19:41,800 --> 12:19:46,116 want to Define like node you use that 19554 12:19:44,116 --> 12:19:47,800 name on the inside if you want to point 19555 12:19:46,116 --> 12:19:51,116 from one to another and then you can 19556 12:19:47,800 --> 12:19:55,916 shorten it down here to just be called 19557 12:19:51,116 --> 12:20:00,040 node questions then on this code 19558 12:19:55,916 --> 12:20:03,080 here questions on what we just did well 19559 12:20:00,040 --> 12:20:05,520 if I rewind just a moment to that final 19560 12:20:03,080 --> 12:20:06,880 picture what would be the upside to be 19561 12:20:05,520 --> 12:20:10,116 clear of having jumped through these 19562 12:20:06,880 --> 12:20:12,200 hoops and added this complexity if you 19563 12:20:10,116 --> 12:20:14,680 will what problem did we just solve by 19564 12:20:12,200 --> 12:20:15,600 linking together these three values to 19565 12:20:14,680 --> 12:20:17,116 be clear 19566 12:20:15,600 --> 12:20:20,800 [Music] 19567 12:20:17,116 --> 12:20:23,320 yeah making lists that 19568 12:20:20,800 --> 12:20:25,320 are that are not contiguous if you will 19569 12:20:23,320 --> 12:20:27,640 so making lists that are not contiguous 19570 12:20:25,320 --> 12:20:29,200 in memory the upside of which is that if 19571 12:20:27,640 --> 12:20:31,200 I want to add the number four to this 19572 12:20:29,200 --> 12:20:33,160 list it looks like I could choose from 19573 12:20:31,200 --> 12:20:35,560 any chunks of available memory on the 19574 12:20:33,160 --> 12:20:38,276 screen I just need to sort of point from 19575 12:20:35,560 --> 12:20:39,916 the end of the current list to wherever 19576 12:20:38,276 --> 12:20:42,400 that other one is in memory what I don't 19577 12:20:39,916 --> 12:20:44,160 need to do to be clear is copy the One 19578 12:20:42,400 --> 12:20:47,040 the two or the three everything can just 19579 12:20:44,160 --> 12:20:48,756 stay put which means TimeWise I can do 19580 12:20:47,040 --> 12:20:50,400 this much more quickly it would seem 19581 12:20:48,756 --> 12:20:52,800 without copying things again and again 19582 12:20:50,400 --> 12:20:54,956 and even without using realloc to let it 19583 12:20:52,800 --> 12:20:56,680 do all of the copying potentially for me 19584 12:20:54,956 --> 12:20:58,436 all right but as we'll start seeing even 19585 12:20:56,680 --> 12:21:00,916 more in the coming weeks every time we 19586 12:20:58,436 --> 12:21:03,680 benefit and solve some problem we pay a 19587 12:21:00,916 --> 12:21:05,840 price there's a tradeoff what is a 19588 12:21:03,680 --> 12:21:09,436 downside as you might perceive now of 19589 12:21:05,840 --> 12:21:12,000 using a linked list instead of an array 19590 12:21:09,436 --> 12:21:13,596 yeah as much memory yeah I mean we use 19591 12:21:12,000 --> 12:21:15,160 twice as much memory because now in 19592 12:21:13,596 --> 12:21:17,560 addition to storing the integers one two 19593 12:21:15,160 --> 12:21:19,200 three I also need to store a pointer for 19594 12:21:17,560 --> 12:21:21,840 each of those and honestly even this 19595 12:21:19,200 --> 12:21:23,680 picture is a bit of uh simplification 19596 12:21:21,840 --> 12:21:25,720 technically in most systems today each 19597 12:21:23,680 --> 12:21:27,360 int would be four bytes technically 19598 12:21:25,720 --> 12:21:28,956 today most pointers though would be 19599 12:21:27,360 --> 12:21:30,520 eight bytes I just didn't want to draw 19600 12:21:28,956 --> 12:21:31,956 this weird shape on the board where the 19601 12:21:30,520 --> 12:21:34,000 bottom square is even bigger than the 19602 12:21:31,956 --> 12:21:36,040 top square but technically we're using 19603 12:21:34,000 --> 12:21:37,720 even more than twice as much space for 19604 12:21:36,040 --> 12:21:39,720 these pointers so there's that trade-off 19605 12:21:37,720 --> 12:21:41,680 now thankfully decades after SE was 19606 12:21:39,720 --> 12:21:43,800 invented memory is generally much 19607 12:21:41,680 --> 12:21:45,596 cheaper nowadays and so it's okay to 19608 12:21:43,800 --> 12:21:46,640 sort of spend more of it if you need to 19609 12:21:45,596 --> 12:21:48,596 and it depends on what you want to 19610 12:21:46,640 --> 12:21:50,840 optimize for but that's absolutely here 19611 12:21:48,596 --> 12:21:55,160 a downside what's another downside of 19612 12:21:50,840 --> 12:21:55,160 having transitioned to in a uh link list 19613 12:21:55,320 --> 12:22:00,240 you can't index into it now I haven't 19614 12:21:58,040 --> 12:22:03,320 even tried in code but when you have a 19615 12:22:00,240 --> 12:22:06,436 linked list you can no longer use square 19616 12:22:03,320 --> 12:22:08,116 bracket notation because why well square 19617 12:22:06,436 --> 12:22:10,436 bracket notation just assumes the 19618 12:22:08,116 --> 12:22:12,756 contiguousness of memory location zero 19619 12:22:10,436 --> 12:22:14,436 is here location one is literally one to 19620 12:22:12,756 --> 12:22:16,160 the right location two is literally one 19621 12:22:14,436 --> 12:22:17,640 to the right one to the right these 19622 12:22:16,160 --> 12:22:19,956 things even though I've drawn it from 19623 12:22:17,640 --> 12:22:22,240 right to left to just keep things pretty 19624 12:22:19,956 --> 12:22:23,720 there are gaps here and this is just my 19625 12:22:22,240 --> 12:22:25,520 interpretation of this these gaps could 19626 12:22:23,720 --> 12:22:27,360 be big they could be narrow they could 19627 12:22:25,520 --> 12:22:29,360 be down here up here they could be 19628 12:22:27,360 --> 12:22:31,800 anywhere so long as we're linking things 19629 12:22:29,360 --> 12:22:33,596 together in this list the computer can't 19630 12:22:31,800 --> 12:22:35,400 just use bracket zero bracket 1 bracket 19631 12:22:33,596 --> 12:22:37,880 two anymore because it can't do simple 19632 12:22:35,400 --> 12:22:39,560 arithmetic and jump to like the middle 19633 12:22:37,880 --> 12:22:41,400 and now here's perhaps the worst price 19634 12:22:39,560 --> 12:22:43,320 we've paid if you don't have square 19635 12:22:41,400 --> 12:22:44,160 bracket notation or really you don't 19636 12:22:43,320 --> 12:22:46,240 have 19637 12:22:44,160 --> 12:22:48,640 contiguousness what algorithm did we 19638 12:22:46,240 --> 12:22:52,080 just sacrifice for this 19639 12:22:48,640 --> 12:22:56,040 dynamism if you rewind even back to week 19640 12:22:52,080 --> 12:22:58,880 zero and we gave it a name in week 19641 12:22:56,040 --> 12:23:00,520 three what algorithm can we not use now 19642 12:22:58,880 --> 12:23:02,640 if we can't assume that the memory is 19643 12:23:00,520 --> 12:23:06,756 back to back to back to 19644 12:23:02,640 --> 12:23:08,560 back binary search why because binary 19645 12:23:06,756 --> 12:23:10,916 search just like the phone book back in 19646 12:23:08,560 --> 12:23:12,956 the first week requires being able to 19647 12:23:10,916 --> 12:23:14,596 arithmetically jump right to the middle 19648 12:23:12,956 --> 12:23:16,080 right take the total length of it divide 19649 12:23:14,596 --> 12:23:18,160 by two and boom you're right there in 19650 12:23:16,080 --> 12:23:20,916 the Middle with some simple arithmetic 19651 12:23:18,160 --> 12:23:23,240 here they might be laid out again with 19652 12:23:20,916 --> 12:23:24,800 these big or small gaps there's no 19653 12:23:23,240 --> 12:23:26,400 simple math I can do to just jump 19654 12:23:24,800 --> 12:23:28,400 immediately to the one in the middle and 19655 12:23:26,400 --> 12:23:29,956 in fact again if this TV were bigger the 19656 12:23:28,400 --> 12:23:32,520 two could technically be in memory be 19657 12:23:29,956 --> 12:23:33,916 way down here or even way over here the 19658 12:23:32,520 --> 12:23:35,320 foam finger could be pointing in any 19659 12:23:33,916 --> 12:23:37,436 number of directions depending on where 19660 12:23:35,320 --> 12:23:39,756 malok put the thing there's just no way 19661 12:23:37,436 --> 12:23:41,596 to do binary search and so it would seem 19662 12:23:39,756 --> 12:23:43,800 that we've paid another price indeed in 19663 12:23:41,596 --> 12:23:46,520 terms of it performance we're now 19664 12:23:43,800 --> 12:23:48,520 talking about linear time again so 19665 12:23:46,520 --> 12:23:50,480 that's a regression now that's also a 19666 12:23:48,520 --> 12:23:51,800 lot things like feels like a good time 19667 12:23:50,480 --> 12:23:53,400 for some muffins and fruit out in the 19668 12:23:51,800 --> 12:23:55,116 lobby and when we come back we'll try to 19669 12:23:53,400 --> 12:23:58,040 solve the problem we just created so see 19670 12:23:55,116 --> 12:23:59,596 you in 10 so we are back and let's see 19671 12:23:58,040 --> 12:24:01,480 if we can't now take some of these 19672 12:23:59,596 --> 12:24:03,160 higher level concepts of like stitching 19673 12:24:01,480 --> 12:24:05,436 together these nodes in memory and 19674 12:24:03,160 --> 12:24:07,040 translate it to some actual code but 19675 12:24:05,436 --> 12:24:08,596 we'll do it step by step first before I 19676 12:24:07,040 --> 12:24:09,800 actually start writing it in vs code so 19677 12:24:08,596 --> 12:24:12,000 if Carter you wouldn't mind helping me 19678 12:24:09,800 --> 12:24:14,160 step through with some visuals let me 19679 12:24:12,000 --> 12:24:15,436 propose that line by line we solve some 19680 12:24:14,160 --> 12:24:17,360 of the problems that we've just created 19681 12:24:15,436 --> 12:24:20,520 for ourselves in building this thing in 19682 12:24:17,360 --> 12:24:23,000 memory so let's go ahead and first 19683 12:24:20,520 --> 12:24:25,400 consider how we could build a length 19684 12:24:23,000 --> 12:24:27,276 list containing the numbers indeed one 19685 12:24:25,400 --> 12:24:28,480 then two then three and let's translate 19686 12:24:27,276 --> 12:24:29,800 each of those steps to code and then 19687 12:24:28,480 --> 12:24:31,640 we'll put it all together into something 19688 12:24:29,800 --> 12:24:34,000 that actually runs so how about first 19689 12:24:31,640 --> 12:24:37,040 step here will just be this to declare a 19690 12:24:34,000 --> 12:24:38,596 pointer called list that's initially has 19691 12:24:37,040 --> 12:24:40,840 no value at least at this point in the 19692 12:24:38,596 --> 12:24:42,756 story list is the name of the variable 19693 12:24:40,840 --> 12:24:44,240 node star just means that this is 19694 12:24:42,756 --> 12:24:46,756 essentially going to be our little 19695 12:24:44,240 --> 12:24:48,880 square over here that points to the 19696 12:24:46,756 --> 12:24:50,956 beginning of the list of course it's 19697 12:24:48,880 --> 12:24:53,436 ideal If It ultimately has a value 19698 12:24:50,956 --> 12:24:55,756 because when we initially I'll call this 19699 12:24:53,436 --> 12:24:57,400 line of code it just gives us indeed 19700 12:24:55,756 --> 12:24:58,680 that square over here on the left but 19701 12:24:57,400 --> 12:25:01,276 it's got a garbage value because there's 19702 12:24:58,680 --> 12:25:02,956 no equal sign on the other side there so 19703 12:25:01,276 --> 12:25:05,880 let's propose that we do one more step 19704 12:25:02,956 --> 12:25:08,276 here and actually initialize it to null 19705 12:25:05,880 --> 12:25:10,680 so that if only we know that it's not 19706 12:25:08,276 --> 12:25:12,240 garbage it at least has some known value 19707 12:25:10,680 --> 12:25:13,640 and null is a good way of signifying 19708 12:25:12,240 --> 12:25:16,400 that at this point in the story The List 19709 12:25:13,640 --> 12:25:18,480 is empty indeed null indicates there's 19710 12:25:16,400 --> 12:25:20,200 no nodes in the list so that picture 19711 12:25:18,480 --> 12:25:21,480 would now look like this whereby let's 19712 12:25:20,200 --> 12:25:22,840 just draw instead of writing null 19713 12:25:21,480 --> 12:25:24,520 everywhere I'll just leave the squares 19714 12:25:22,840 --> 12:25:27,480 blank when it's not a garbage value per 19715 12:25:24,520 --> 12:25:29,560 se it's literally Ox Z or null all right 19716 12:25:27,480 --> 12:25:32,400 so that's it for building a link list of 19717 12:25:29,560 --> 12:25:33,840 size zero like we're sort of done then 19718 12:25:32,400 --> 12:25:35,800 but we want to now add a one and then a 19719 12:25:33,840 --> 12:25:38,040 two then a three so next step here might 19720 12:25:35,800 --> 12:25:41,276 be this if I want to allocate the first 19721 12:25:38,040 --> 12:25:42,956 of my rectangles on our previous picture 19722 12:25:41,276 --> 12:25:45,240 I'm going to call malok and I'm going to 19723 12:25:42,956 --> 12:25:47,080 ask for enough memory to fit a whole 19724 12:25:45,240 --> 12:25:48,800 node now technically I think that's 19725 12:25:47,080 --> 12:25:50,400 going to be like four bytes for the int 19726 12:25:48,800 --> 12:25:52,276 and eight bytes for the pointer even 19727 12:25:50,400 --> 12:25:54,240 though I did not draw it to scale on the 19728 12:25:52,276 --> 12:25:56,640 board so that's technically going to be 19729 12:25:54,240 --> 12:25:58,360 what 12 bytes but again size of node 19730 12:25:56,640 --> 12:26:00,000 just figures out how many bytes I 19731 12:25:58,360 --> 12:26:02,080 actually need dynamically that's going 19732 12:26:00,000 --> 12:26:03,880 to return to me the address of that 19733 12:26:02,080 --> 12:26:05,880 chunk of memory which apparently I'm 19734 12:26:03,880 --> 12:26:08,480 going to store inside of a temporary 19735 12:26:05,880 --> 12:26:10,640 variable called n for short for node but 19736 12:26:08,480 --> 12:26:12,400 let's see what this does pictorially so 19737 12:26:10,640 --> 12:26:15,276 when this line of code is executed I 19738 12:26:12,400 --> 12:26:17,320 first get on the left that variable n 19739 12:26:15,276 --> 12:26:19,000 it's got a garbage value by default 19740 12:26:17,320 --> 12:26:20,956 because I haven't executed the whole 19741 12:26:19,000 --> 12:26:22,436 thing from right to left meanwhile on 19742 12:26:20,956 --> 12:26:24,480 the right hand side of the expression 19743 12:26:22,436 --> 12:26:25,956 I've got now a node somewhere in memory 19744 12:26:24,480 --> 12:26:27,916 it happened to be free here this is 19745 12:26:25,956 --> 12:26:29,480 where malok put it for me but it does 19746 12:26:27,916 --> 12:26:32,080 have two garbage values initially but 19747 12:26:29,480 --> 12:26:34,436 because it's a node per my type def 19748 12:26:32,080 --> 12:26:37,400 earlier every node I proposed is going 19749 12:26:34,436 --> 12:26:39,080 to have a number and a next pointer so 19750 12:26:37,400 --> 12:26:40,880 we can see those labeled here but 19751 12:26:39,080 --> 12:26:42,756 they've got two garbage values initially 19752 12:26:40,880 --> 12:26:46,116 but all I care about initially is that 19753 12:26:42,756 --> 12:26:47,880 ultimately n is pointing at that chunk 19754 12:26:46,116 --> 12:26:48,956 of code so initially if we could back up 19755 12:26:47,880 --> 12:26:53,116 two 19756 12:26:48,956 --> 12:26:55,116 steps we have two steps so we have initi 19757 12:26:53,116 --> 12:26:58,080 one step forward we have this line of 19758 12:26:55,116 --> 12:27:00,320 code gives us this variable here which 19759 12:26:58,080 --> 12:27:02,116 has garbage when this side of the 19760 12:27:00,320 --> 12:27:04,400 expression is executed that allocates 19761 12:27:02,116 --> 12:27:06,400 the memory and then when we copy from 19762 12:27:04,400 --> 12:27:08,080 right to left the address of that chunk 19763 12:27:06,400 --> 12:27:10,040 of memory that's what gives us 19764 12:27:08,080 --> 12:27:11,840 conceptually this arrow and the garbage 19765 12:27:10,040 --> 12:27:13,480 goes away because it's a valid pointer 19766 12:27:11,840 --> 12:27:14,916 now of course there's still two garbage 19767 12:27:13,480 --> 12:27:16,800 values there because we haven't set this 19768 12:27:14,916 --> 12:27:18,200 node to store a number like the number 19769 12:27:16,800 --> 12:27:20,596 one so let's go ahead and execute one 19770 12:27:18,200 --> 12:27:22,360 other line of code like this which while 19771 12:27:20,596 --> 12:27:24,520 cryptic looking is just an application 19772 12:27:22,360 --> 12:27:27,560 of ideas we've seen in week four and 19773 12:27:24,520 --> 12:27:30,276 prior star N means to start at this 19774 12:27:27,560 --> 12:27:32,160 variable and go there Follow the arrow 19775 12:27:30,276 --> 12:27:34,276 is what the star or the D reference 19776 12:27:32,160 --> 12:27:36,200 operator does for us and then the dot 19777 12:27:34,276 --> 12:27:38,720 operator recall when we first introduce 19778 12:27:36,200 --> 12:27:40,916 structs like for a person struct allows 19779 12:27:38,720 --> 12:27:43,520 us to go at the number field or the next 19780 12:27:40,916 --> 12:27:44,840 field so if I do star n and then in 19781 12:27:43,520 --> 12:27:47,160 parentheses to make sure order of 19782 12:27:44,840 --> 12:27:49,200 operations is preserved do number and 19783 12:27:47,160 --> 12:27:50,800 then assign it the actual number one 19784 12:27:49,200 --> 12:27:53,116 which puts the one in the top of that 19785 12:27:50,800 --> 12:27:54,640 rectangle now admittedly this syntax is 19786 12:27:53,116 --> 12:27:56,276 not very user friendly it's annoying to 19787 12:27:54,640 --> 12:27:58,400 remember you have to the parentheses so 19788 12:27:56,276 --> 12:28:00,200 there's another Syntax for this whenever 19789 12:27:58,400 --> 12:28:03,160 you're doing two things like this in 19790 12:28:00,200 --> 12:28:05,400 code dereferencing a pointer that is 19791 12:28:03,160 --> 12:28:07,520 going to an address and then further 19792 12:28:05,400 --> 12:28:09,640 using the dot notation to go inside of 19793 12:28:07,520 --> 12:28:12,360 the structure you find that wonderfully 19794 12:28:09,640 --> 12:28:14,680 C gives us this syntax whereby you can 19795 12:28:12,360 --> 12:28:16,720 just change the star and the parentheses 19796 12:28:14,680 --> 12:28:18,160 and the dot to just be an arrow and 19797 12:28:16,720 --> 12:28:19,720 again it's not a single character on 19798 12:28:18,160 --> 12:28:21,680 your keyboard it's a hyphen and then an 19799 12:28:19,720 --> 12:28:23,480 open angle bracket but I kind of like 19800 12:28:21,680 --> 12:28:26,240 the semantics of this because this code 19801 12:28:23,480 --> 12:28:28,436 now pretty much matches the picture n 19802 12:28:26,240 --> 12:28:30,560 arrow leads you to the value that you 19803 12:28:28,436 --> 12:28:32,640 want to access or ultimately change in 19804 12:28:30,560 --> 12:28:34,400 this way there's one step though we've 19805 12:28:32,640 --> 12:28:36,436 forgotten of course which is that we 19806 12:28:34,400 --> 12:28:38,276 can't leave this garbage value here 19807 12:28:36,436 --> 12:28:40,360 because the garbage value is some 19808 12:28:38,276 --> 12:28:42,000 unknown value that effectively is 19809 12:28:40,360 --> 12:28:43,880 pointing who knows where and we don't 19810 12:28:42,000 --> 12:28:45,880 want to accidentally misinterpret that 19811 12:28:43,880 --> 12:28:49,000 garbage value as being a valid address 19812 12:28:45,880 --> 12:28:51,360 and risk going there so of course what 19813 12:28:49,000 --> 12:28:53,596 value should we put here instead our old 19814 12:28:51,360 --> 12:28:55,200 friend null just to signify that this is 19815 12:28:53,596 --> 12:28:56,916 indeed the end of the list and we could 19816 12:28:55,200 --> 12:28:58,596 do that with a line of code like this 19817 12:28:56,916 --> 12:29:01,240 and again we'll canote as much by just 19818 12:28:58,596 --> 12:29:03,160 leaving that empty box blank so now we 19819 12:29:01,240 --> 12:29:05,080 have a list of size one let's go ahead 19820 12:29:03,160 --> 12:29:08,800 and add the second number to it as with 19821 12:29:05,080 --> 12:29:11,560 these lines here list equals n allows us 19822 12:29:08,800 --> 12:29:13,560 to remember that indeed we have this 19823 12:29:11,560 --> 12:29:15,240 list here so if we can step one step 19824 12:29:13,560 --> 12:29:17,596 forward here's what the picture now 19825 12:29:15,240 --> 12:29:20,000 looks like and technically let's go one 19826 12:29:17,596 --> 12:29:21,800 step further here this is now really 19827 12:29:20,000 --> 12:29:25,360 what's going on in memory once my list 19828 12:29:21,800 --> 12:29:27,956 of size exists my main variable called 19829 12:29:25,360 --> 12:29:29,596 list is pointing at exactly that first 19830 12:29:27,956 --> 12:29:31,640 node at this point in the story I don't 19831 12:29:29,596 --> 12:29:33,400 need to know or care about the temporary 19832 12:29:31,640 --> 12:29:35,000 variable that I called n even though it 19833 12:29:33,400 --> 12:29:36,720 might very well still be there but 19834 12:29:35,000 --> 12:29:39,200 indeed this now represents that link 19835 12:29:36,720 --> 12:29:41,080 list let's now indeed add the number two 19836 12:29:39,200 --> 12:29:43,200 so with the same line of code as before 19837 12:29:41,080 --> 12:29:45,200 I'm going to allocate another node size 19838 12:29:43,200 --> 12:29:46,800 of node ideally I would be checking for 19839 12:29:45,200 --> 12:29:48,840 null here but we're doing the juicy 19840 12:29:46,800 --> 12:29:50,480 Parts only on the slides let's now go 19841 12:29:48,840 --> 12:29:53,756 ahead and depict that so what happens 19842 12:29:50,480 --> 12:29:54,720 with this this brings back our n pointer 19843 12:29:53,756 --> 12:29:56,400 which might have been there the whole 19844 12:29:54,720 --> 12:29:57,800 time but we're doing this step by step 19845 12:29:56,400 --> 12:29:59,436 it's a garbage value though because we 19846 12:29:57,800 --> 12:30:01,116 haven't yet copied from right to left 19847 12:29:59,436 --> 12:30:03,436 Malo of course gives us a second chunk 19848 12:30:01,116 --> 12:30:05,160 of memory which maybe ends up there with 19849 12:30:03,436 --> 12:30:06,720 two garbage values by default I've 19850 12:30:05,160 --> 12:30:08,520 omitted the labels now just because 19851 12:30:06,720 --> 12:30:11,080 they're still going to be number and 19852 12:30:08,520 --> 12:30:12,916 next respectively once we copy from 19853 12:30:11,080 --> 12:30:15,436 right to left the garbage value indeed 19854 12:30:12,916 --> 12:30:17,360 becomes an arrow Oscar disappears 19855 12:30:15,436 --> 12:30:19,520 because it's now indeed a valid pointer 19856 12:30:17,360 --> 12:30:21,880 pointing here now the values themselves 19857 12:30:19,520 --> 12:30:24,520 number and next are invalid garbage 19858 12:30:21,880 --> 12:30:26,200 values so here is where we can now start 19859 12:30:24,520 --> 12:30:28,116 using our new syntax like the arrow 19860 12:30:26,200 --> 12:30:31,116 notation or the star and the dot if you 19861 12:30:28,116 --> 12:30:33,480 prefer and we can change the value of n 19862 12:30:31,116 --> 12:30:35,916 Follow the arrow to number and that 19863 12:30:33,480 --> 12:30:39,800 becomes two similarly we can do this 19864 12:30:35,916 --> 12:30:42,116 again and set n arrow next so start at n 19865 12:30:39,800 --> 12:30:44,596 Follow the arrow access the next field 19866 12:30:42,116 --> 12:30:46,240 and set that equal to null now we're not 19867 12:30:44,596 --> 12:30:48,200 quite done yet because we haven't 19868 12:30:46,240 --> 12:30:50,000 actually linked things together so 19869 12:30:48,200 --> 12:30:52,400 here's now where things get interesting 19870 12:30:50,000 --> 12:30:54,200 how do I combine these two well let me 19871 12:30:52,400 --> 12:30:56,756 me propose this let me propose on our 19872 12:30:54,200 --> 12:31:00,360 next line here we actually update for 19873 12:30:56,756 --> 12:31:02,160 Now list equal to n that is to say 19874 12:31:00,360 --> 12:31:04,720 whatever address this is whatever it's 19875 12:31:02,160 --> 12:31:06,436 pointing at change list to be the same 19876 12:31:04,720 --> 12:31:08,880 address that is point at the same thing 19877 12:31:06,436 --> 12:31:11,080 so if n is pointing here let's change 19878 12:31:08,880 --> 12:31:13,840 list to point here and go ahead and do 19879 12:31:11,080 --> 12:31:16,680 that Carter if you could I don't like 19880 12:31:13,840 --> 12:31:19,840 this can you go one further step this is 19881 12:31:16,680 --> 12:31:22,040 bad what is wrong about my sequence of 19882 12:31:19,840 --> 12:31:23,800 operations here where I updated list to 19883 12:31:22,040 --> 12:31:25,916 point my new 19884 12:31:23,800 --> 12:31:28,560 node 19885 12:31:25,916 --> 12:31:30,520 yeah yeah we lost the pointer to the 19886 12:31:28,560 --> 12:31:33,680 other node so I don't even care about 19887 12:31:30,520 --> 12:31:36,080 the ordering 21 or one two the bigger 19888 12:31:33,680 --> 12:31:37,916 problem now as the lack of arrows over 19889 12:31:36,080 --> 12:31:40,916 there suggests is that I have a memory 19890 12:31:37,916 --> 12:31:42,880 leak I have orphaned my original node in 19891 12:31:40,916 --> 12:31:45,040 the sense that nothing is pointing at it 19892 12:31:42,880 --> 12:31:46,520 anymore now absolutely I could fix this 19893 12:31:45,040 --> 12:31:47,560 by adding some temporary variables I 19894 12:31:46,520 --> 12:31:49,480 could add it to the mix but at this 19895 12:31:47,560 --> 12:31:51,916 point in the story I have not done any 19896 12:31:49,480 --> 12:31:53,520 such uh recollection thereof so let me 19897 12:31:51,916 --> 12:31:55,040 back this up and let's go forward in the 19898 12:31:53,520 --> 12:31:57,436 slides this is where we left off a 19899 12:31:55,040 --> 12:31:59,596 moment ago I think I need to take into 19900 12:31:57,436 --> 12:32:00,800 account order of operations and I'm 19901 12:31:59,596 --> 12:32:02,640 going to keep this simple I'm not going 19902 12:32:00,800 --> 12:32:05,436 to care about the order of the numbers 19903 12:32:02,640 --> 12:32:08,320 for now I'm fine with a list that is two 19904 12:32:05,436 --> 12:32:11,040 and then one so with that said let me go 19905 12:32:08,320 --> 12:32:13,200 ahead and update I think this box here 19906 12:32:11,040 --> 12:32:16,000 to point at my original node so let's 19907 12:32:13,200 --> 12:32:21,160 see how we can do this in code okay n 19908 12:32:16,000 --> 12:32:22,916 arrow next so n arrow next should equal 19909 12:32:21,160 --> 12:32:25,276 the current list and this is a little 19910 12:32:22,916 --> 12:32:27,200 weird again but recall what list is list 19911 12:32:25,276 --> 12:32:30,040 is this pointer here that just contains 19912 12:32:27,200 --> 12:32:32,916 the address of the original address of 19913 12:32:30,040 --> 12:32:34,916 the list or equivalently it contains 19914 12:32:32,916 --> 12:32:37,000 this Arrow whatever it's pointing at so 19915 12:32:34,916 --> 12:32:39,276 what this means in this line of code n 19916 12:32:37,000 --> 12:32:41,720 bracket next means start at n Follow the 19917 12:32:39,276 --> 12:32:44,756 arrow access the next pointer and set it 19918 12:32:41,720 --> 12:32:48,160 equal to whatever list equals so if list 19919 12:32:44,756 --> 12:32:50,840 is pointing here then next should point 19920 12:32:48,160 --> 12:32:52,400 there as well this I think is safe 19921 12:32:50,840 --> 12:32:54,040 because now we have redundancy now we've 19922 12:32:52,400 --> 12:32:56,200 got two pointers pointing at the 19923 12:32:54,040 --> 12:32:59,276 original list and now I think we can do 19924 12:32:56,200 --> 12:33:01,360 another step whereby we update list to 19925 12:32:59,276 --> 12:33:02,840 equal n same line of code before that 19926 12:33:01,360 --> 12:33:04,800 got us into trouble but I'm doing it 19927 12:33:02,840 --> 12:33:08,480 second now instead of first when I 19928 12:33:04,800 --> 12:33:10,916 execute list equals n this now sets list 19929 12:33:08,480 --> 12:33:13,880 equal to the same thing that n equals 19930 12:33:10,916 --> 12:33:17,596 and so now I have successfully inserted 19931 12:33:13,880 --> 12:33:19,360 my new node containing two into the list 19932 12:33:17,596 --> 12:33:20,956 and in fact if we advance one more we 19933 12:33:19,360 --> 12:33:22,596 can just clear up the Clutter assume 19934 12:33:20,956 --> 12:33:24,680 that the temporary variable is gone from 19935 12:33:22,596 --> 12:33:26,436 the story now we have a linked list 19936 12:33:24,680 --> 12:33:28,160 where admittedly ordering is wrong it's 19937 12:33:26,436 --> 12:33:30,756 21 instead of one two but at least it's 19938 12:33:28,160 --> 12:33:32,956 linked correctly and I didn't orphan or 19939 12:33:30,756 --> 12:33:35,720 leak any 19940 12:33:32,956 --> 12:33:36,756 memory questions on this sequence of 19941 12:33:35,720 --> 12:33:38,370 steps 19942 12:33:36,756 --> 12:33:41,419 here yeah in 19943 12:33:38,370 --> 12:33:41,419 [Music] 19944 12:33:44,800 --> 12:33:49,800 back yeah spot on so this would fall 19945 12:33:47,276 --> 12:33:51,520 under that category of a stack if you 19946 12:33:49,800 --> 12:33:53,756 will although I've not called it that by 19947 12:33:51,520 --> 12:33:56,040 name because I just pushed the number 19948 12:33:53,756 --> 12:33:57,880 two onto this data structure if you will 19949 12:33:56,040 --> 12:34:00,040 and indeed it ended up at the beginning 19950 12:33:57,880 --> 12:34:01,200 of the list instead of the end and so 19951 12:34:00,040 --> 12:34:02,880 here's where we see a distinction 19952 12:34:01,200 --> 12:34:04,756 between an abstract data structure which 19953 12:34:02,880 --> 12:34:06,520 is where we began a stack is a thing 19954 12:34:04,756 --> 12:34:09,560 like the pile of sweaters that just has 19955 12:34:06,520 --> 12:34:12,956 push and pop properties and lifo access 19956 12:34:09,560 --> 12:34:14,560 like uh last in first out how do you 19957 12:34:12,956 --> 12:34:16,040 implement something like that in memory 19958 12:34:14,560 --> 12:34:18,596 well it would seem that you could 19959 12:34:16,040 --> 12:34:20,680 implement the notion of a stack here not 19960 12:34:18,596 --> 12:34:23,160 for sweaters but for numbers using a 19961 12:34:20,680 --> 12:34:26,840 linked list so long as you implement 19962 12:34:23,160 --> 12:34:29,116 insertion AKA pushing by prepending new 19963 12:34:26,840 --> 12:34:30,276 values to the list by prepending again 19964 12:34:29,116 --> 12:34:32,080 and again and if Carter you don't mind 19965 12:34:30,276 --> 12:34:34,116 hitting the keyboard one more time if I 19966 12:34:32,080 --> 12:34:36,480 wanted to add the number three now you 19967 12:34:34,116 --> 12:34:38,756 would could imagine prepending it to the 19968 12:34:36,480 --> 12:34:41,080 list why well honestly especially as 19969 12:34:38,756 --> 12:34:43,520 this list gets longer and longer I kind 19970 12:34:41,080 --> 12:34:45,596 of like the appeal of prepending these 19971 12:34:43,520 --> 12:34:48,520 elements why because even if this list 19972 12:34:45,596 --> 12:34:50,480 gets crazy long and way way out here you 19973 12:34:48,520 --> 12:34:52,560 didn't notice me following all of the 19974 12:34:50,480 --> 12:34:54,400 arrows earlier to do the insert if I 19975 12:34:52,560 --> 12:34:56,756 want to insert a fourth number a fifth 19976 12:34:54,400 --> 12:34:58,956 number a sixth number all I have to do 19977 12:34:56,756 --> 12:35:01,840 is like insert it here if you will point 19978 12:34:58,956 --> 12:35:03,560 it at the original uh start of the list 19979 12:35:01,840 --> 12:35:05,596 then update this pointer and done and I 19980 12:35:03,560 --> 12:35:07,956 would say that's like two steps give or 19981 12:35:05,596 --> 12:35:10,360 take it's not going to be end steps as 19982 12:35:07,956 --> 12:35:12,596 it would be if I had to upend the new 19983 12:35:10,360 --> 12:35:14,436 nodes to the end of the list now of 19984 12:35:12,596 --> 12:35:15,840 course we've sacrificed ordering of 19985 12:35:14,436 --> 12:35:17,320 these numbers they're literally in the 19986 12:35:15,840 --> 12:35:19,040 opposite order or whatever order they 19987 12:35:17,320 --> 12:35:21,240 were inserted in but that might very 19988 12:35:19,040 --> 12:35:23,040 well be okay depending on the goal at 19989 12:35:21,240 --> 12:35:24,640 hand all right thank you to Carter for 19990 12:35:23,040 --> 12:35:28,800 stepping through this what if now we 19991 12:35:24,640 --> 12:35:28,800 wanted to translate this oh sure thank 19992 12:35:29,080 --> 12:35:34,560 you it's all for you none for me in this 19993 12:35:31,880 --> 12:35:36,560 example so here we have perhaps a way of 19994 12:35:34,560 --> 12:35:38,040 translating this now to some actual code 19995 12:35:36,560 --> 12:35:40,080 and this will be the last of like the 19996 12:35:38,040 --> 12:35:41,756 sort of intense code here just to give 19997 12:35:40,080 --> 12:35:44,916 you a sense of how we can translate this 19998 12:35:41,756 --> 12:35:47,040 idea now to actual step so this is list. 19999 12:35:44,916 --> 12:35:49,040 C and VSS code here let me go ahead and 20000 12:35:47,040 --> 12:35:52,640 make a couple of changes up top let me 20001 12:35:49,040 --> 12:35:56,160 go ahead and how about uh declaring a 20002 12:35:52,640 --> 12:35:58,720 node using typ def uh struct node using 20003 12:35:56,160 --> 12:36:00,916 our new framing as before I'm going to 20004 12:35:58,720 --> 12:36:03,720 give every node a number as I proposed 20005 12:36:00,916 --> 12:36:06,160 and every node a pointer to the next 20006 12:36:03,720 --> 12:36:07,880 element which is going to be implemented 20007 12:36:06,160 --> 12:36:10,160 just as before and I'm going to simplify 20008 12:36:07,880 --> 12:36:12,040 the whole name as just node so all of 20009 12:36:10,160 --> 12:36:14,436 that is is the exact same type depth 20010 12:36:12,040 --> 12:36:16,840 that we proposed earlier now let me go 20011 12:36:14,436 --> 12:36:18,756 ahead and get rid of all of this code 20012 12:36:16,840 --> 12:36:20,276 which we wrote earlier and recall that 20013 12:36:18,756 --> 12:36:22,276 this was the most recent version that 20014 12:36:20,276 --> 12:36:24,360 was not a linked list this was just in 20015 12:36:22,276 --> 12:36:26,320 Array that we allocated and then 20016 12:36:24,360 --> 12:36:28,160 reallocated so this is sort of the old 20017 12:36:26,320 --> 12:36:30,000 way of doing things but it was 20018 12:36:28,160 --> 12:36:32,680 inefficient because we might have to 20019 12:36:30,000 --> 12:36:34,560 lean on a for Loop or lean on realloc to 20020 12:36:32,680 --> 12:36:36,840 copy everything around we're now going 20021 12:36:34,560 --> 12:36:41,116 to reimplement the notion of a list as 20022 12:36:36,840 --> 12:36:42,956 an actual linked list not as an array so 20023 12:36:41,116 --> 12:36:44,320 my main function now might do something 20024 12:36:42,956 --> 12:36:46,116 like this and I'm going to really just 20025 12:36:44,320 --> 12:36:47,880 copy the lines of code that we just 20026 12:36:46,116 --> 12:36:50,400 stepped through on the board so let me 20027 12:36:47,880 --> 12:36:52,160 give myself a uh special variable called 20028 12:36:50,400 --> 12:36:53,880 list that's going to be initialized to 20029 12:36:52,160 --> 12:36:55,160 null and this is just my pointer the 20030 12:36:53,880 --> 12:36:57,200 square on the left hand side of the 20031 12:36:55,160 --> 12:36:58,680 screen that represents the start of the 20032 12:36:57,200 --> 12:37:01,360 list and if it's null it means the list 20033 12:36:58,680 --> 12:37:04,596 is empty so done I'm done implementing a 20034 12:37:01,360 --> 12:37:06,200 linked list of size zero well now how do 20035 12:37:04,596 --> 12:37:07,756 I want to run this code well let me 20036 12:37:06,200 --> 12:37:09,640 propose for the sake of discussion that 20037 12:37:07,756 --> 12:37:11,436 this version of the program will take 20038 12:37:09,640 --> 12:37:12,880 command line arguments so I want to be 20039 12:37:11,436 --> 12:37:15,436 able to do something like this I want to 20040 12:37:12,880 --> 12:37:17,240 run this program ultimately and type in 20041 12:37:15,436 --> 12:37:20,160 three command line arguments like this 1 20042 12:37:17,240 --> 12:37:22,956 2 3 and I want my program in a couple 20043 12:37:20,160 --> 12:37:25,360 minutes to allocate one two three nodes 20044 12:37:22,956 --> 12:37:27,640 and Stitch them together just like the 20045 12:37:25,360 --> 12:37:29,400 visualization on the board uh I could 20046 12:37:27,640 --> 12:37:31,116 use get int but it's just going to be 20047 12:37:29,400 --> 12:37:32,436 faster if we use command line arguments 20048 12:37:31,116 --> 12:37:34,320 so again I'm just borrowing some 20049 12:37:32,436 --> 12:37:36,000 Concepts from week two but none of 20050 12:37:34,320 --> 12:37:39,720 that's possible yet until I change my 20051 12:37:36,000 --> 12:37:43,596 code here so let's do this in argc uh 20052 12:37:39,720 --> 12:37:45,360 string uh argv but you know what we know 20053 12:37:43,596 --> 12:37:47,680 that strings are not actually a thing 20054 12:37:45,360 --> 12:37:49,400 anymore so I can change my command line 20055 12:37:47,680 --> 12:37:52,116 argument definition to be what it really 20056 12:37:49,400 --> 12:37:54,200 is it's really charar but it's the exact 20057 12:37:52,116 --> 12:37:56,040 same thing as in week two just strings 20058 12:37:54,200 --> 12:37:58,160 are no more at least without the 20059 12:37:56,040 --> 12:38:01,480 training wheels on anymore like last 20060 12:37:58,160 --> 12:38:06,720 week and now let me do this uh for in I 20061 12:38:01,480 --> 12:38:08,080 equal 1 uh I is less than ARG C i++ so 20062 12:38:06,720 --> 12:38:09,400 what I'm doing with this Loop is I just 20063 12:38:08,080 --> 12:38:11,680 want to iterate over the command line 20064 12:38:09,400 --> 12:38:14,160 argument so I have one number at a time 20065 12:38:11,680 --> 12:38:18,520 from The Prompt um what else do I want 20066 12:38:14,160 --> 12:38:22,276 to do here uh well let's go ahead and 20067 12:38:18,520 --> 12:38:26,400 how about do this um let's get 20068 12:38:22,276 --> 12:38:28,480 a number so in number equals arv braet I 20069 12:38:26,400 --> 12:38:31,040 so a couple of notes Here one I'm 20070 12:38:28,480 --> 12:38:33,560 starting my for loop at one instead of 20071 12:38:31,040 --> 12:38:35,240 zero but I'm going up to RC RC is 20072 12:38:33,560 --> 12:38:37,200 argument count how many words are at the 20073 12:38:35,240 --> 12:38:40,200 prompt why am I starting at one instead 20074 12:38:37,200 --> 12:38:44,560 of zero though given my 20075 12:38:40,200 --> 12:38:44,560 goal why am I starting at one 20076 12:38:48,720 --> 12:38:53,276 yeah yeah so the first value in RV is is 20077 12:38:51,756 --> 12:38:54,916 actually the name of the program that's 20078 12:38:53,276 --> 12:38:56,520 obviously not a number so I want the 20079 12:38:54,916 --> 12:38:57,756 second value so I'm going to start 20080 12:38:56,520 --> 12:38:59,800 iterating over those command line 20081 12:38:57,756 --> 12:39:01,520 arguments at I equals 1 so that's all I 20082 12:38:59,800 --> 12:39:04,200 just want to get the actual numbers at 20083 12:39:01,520 --> 12:39:08,000 the prompt um unfortunately argv bracket 20084 12:39:04,200 --> 12:39:09,800 I is a string AKA Char star that is not 20085 12:39:08,000 --> 12:39:11,916 an INT so this line of code won't work 20086 12:39:09,800 --> 12:39:14,276 but can anyone think back to like week 20087 12:39:11,916 --> 12:39:15,916 two where we had a function for 20088 12:39:14,276 --> 12:39:19,640 converting strings to 20089 12:39:15,916 --> 12:39:21,276 integers anyone yeah so a to I is a 20090 12:39:19,640 --> 12:39:23,276 function that converts asky to an 20091 12:39:21,276 --> 12:39:25,040 integer assuming what you give it as an 20092 12:39:23,276 --> 12:39:27,000 argument looks like a number like one or 20093 12:39:25,040 --> 12:39:28,956 two or three so let me fix this let me 20094 12:39:27,000 --> 12:39:30,956 actually do the conversion if I were 20095 12:39:28,956 --> 12:39:32,560 really being careful I would error check 20096 12:39:30,956 --> 12:39:34,040 this make sure that there's no digits 20097 12:39:32,560 --> 12:39:35,756 just like you might have in problem set 20098 12:39:34,040 --> 12:39:37,436 two but for today's purposes I'm just 20099 12:39:35,756 --> 12:39:39,756 going to assume the honor System that 20100 12:39:37,436 --> 12:39:42,000 the user me is going to run the program 20101 12:39:39,756 --> 12:39:43,800 correctly all right so now that I have a 20102 12:39:42,000 --> 12:39:45,640 variable containing the number from the 20103 12:39:43,800 --> 12:39:48,520 command line let's just allocate a node 20104 12:39:45,640 --> 12:39:50,360 for it so let me do node star n just 20105 12:39:48,520 --> 12:39:53,436 like we did in the visualization and 20106 12:39:50,360 --> 12:39:56,436 let's malok enough space for the size of 20107 12:39:53,436 --> 12:40:00,640 one such node here I now need to just be 20108 12:39:56,436 --> 12:40:02,480 super safe so if n equals equals null 20109 12:40:00,640 --> 12:40:04,200 like if I'm out of memory you know what 20110 12:40:02,480 --> 12:40:07,680 let me go ahead and just immediately 20111 12:40:04,200 --> 12:40:11,276 return one here otherwise if that's not 20112 12:40:07,680 --> 12:40:14,160 the case let me go ahead and update the 20113 12:40:11,276 --> 12:40:16,560 number field of this new node which it 20114 12:40:14,160 --> 12:40:18,880 line 24 does exist because it did not 20115 12:40:16,560 --> 12:40:20,916 return null so I did not exit early with 20116 12:40:18,880 --> 12:40:23,200 return and let me just store whatever 20117 12:40:20,916 --> 12:40:26,360 number that human typed in first so the 20118 12:40:23,200 --> 12:40:28,956 return value of a to I which per line 17 20119 12:40:26,360 --> 12:40:33,756 is in my variable called number and then 20120 12:40:28,956 --> 12:40:38,520 let me go ahead and just prepend this to 20121 12:40:33,756 --> 12:40:41,400 the list let me go ahead and say that um 20122 12:40:38,520 --> 12:40:43,000 this next field first has a known value 20123 12:40:41,400 --> 12:40:45,080 null just so that we get rid of that 20124 12:40:43,000 --> 12:40:47,436 second garbage value and let me go ahead 20125 12:40:45,080 --> 12:40:49,436 and now prepend it to the list so if I 20126 12:40:47,436 --> 12:40:52,360 want to prepend it that means this new 20127 12:40:49,436 --> 12:40:56,200 node must have a next field field that 20128 12:40:52,360 --> 12:40:59,000 points to the current beginning of the 20129 12:40:56,200 --> 12:41:01,000 list and again the goal here is to preen 20130 12:40:59,000 --> 12:41:03,400 Preen Preen so whatever the current list 20131 12:41:01,000 --> 12:41:07,680 is let's change it so that this new node 20132 12:41:03,400 --> 12:41:10,160 points to that existing list and now 20133 12:41:07,680 --> 12:41:13,040 step two as before was to update the 20134 12:41:10,160 --> 12:41:14,916 actual list to point at this node so 20135 12:41:13,040 --> 12:41:16,720 recall in red on the screen before I 20136 12:41:14,916 --> 12:41:18,756 screwed up originally and I only did 20137 12:41:16,720 --> 12:41:21,080 this line by moving the pointer too 20138 12:41:18,756 --> 12:41:22,956 early if you will but I fixed that once 20139 12:41:21,080 --> 12:41:25,360 Carter helped me rewind and we got rid 20140 12:41:22,956 --> 12:41:28,840 of the red line which indicated error 20141 12:41:25,360 --> 12:41:30,596 and I just do n arrow next to change the 20142 12:41:28,840 --> 12:41:32,400 next field of this new node to point to 20143 12:41:30,596 --> 12:41:34,840 the existing list so I'm not orphaning 20144 12:41:32,400 --> 12:41:38,560 anything all right at this point in the 20145 12:41:34,840 --> 12:41:40,800 story I think my code is 20146 12:41:38,560 --> 12:41:42,320 correct not batting very well though 20147 12:41:40,800 --> 12:41:43,720 today but I think my code is correct but 20148 12:41:42,320 --> 12:41:45,360 the program doesn't do anything 20149 12:41:43,720 --> 12:41:47,360 interesting so it would be nice to kind 20150 12:41:45,360 --> 12:41:49,436 of now iterate over this link list in 20151 12:41:47,360 --> 12:41:51,480 memory whatever its order is and print 20152 12:41:49,436 --> 12:41:53,240 things out well how do we do that well 20153 12:41:51,480 --> 12:41:55,680 it turns out if you want to iterate over 20154 12:41:53,240 --> 12:41:57,320 a linked List the general Paradigm is to 20155 12:41:55,680 --> 12:41:59,116 do something like this to define a 20156 12:41:57,320 --> 12:42:00,520 temporary variable I could call it temp 20157 12:41:59,116 --> 12:42:03,400 but another convention that you might as 20158 12:42:00,520 --> 12:42:05,040 well see is called pointer PTR for short 20159 12:42:03,400 --> 12:42:06,400 but you can call it anything you want 20160 12:42:05,040 --> 12:42:08,436 and you can have a temporary variable 20161 12:42:06,400 --> 12:42:10,276 first point at the first node in the 20162 12:42:08,436 --> 12:42:12,160 list and then in some kind of loop like 20163 12:42:10,276 --> 12:42:13,680 a while loop you point it at the second 20164 12:42:12,160 --> 12:42:15,200 node in the list and then you keep 20165 12:42:13,680 --> 12:42:16,400 iterating you point it at the last node 20166 12:42:15,200 --> 12:42:18,756 in the list and then eventually you 20167 12:42:16,400 --> 12:42:20,880 iterate too far effectively pointing at 20168 12:42:18,756 --> 12:42:22,720 null at which point your while loop can 20169 12:42:20,880 --> 12:42:24,436 presumably terminate so how do I 20170 12:42:22,720 --> 12:42:26,756 Implement that idea of allocating a 20171 12:42:24,436 --> 12:42:28,480 temporary pointer that just points at 20172 12:42:26,756 --> 12:42:30,276 each node in the list and lets me print 20173 12:42:28,480 --> 12:42:32,480 out ultimately each of those numbers 20174 12:42:30,276 --> 12:42:35,160 well let's go back to my code here and 20175 12:42:32,480 --> 12:42:37,840 let me do this let me go ahead and 20176 12:42:35,160 --> 12:42:39,520 declare this temporary pointer which is 20177 12:42:37,840 --> 12:42:41,240 going to be a node star also why because 20178 12:42:39,520 --> 12:42:42,756 it's the address of a node the first the 20179 12:42:41,240 --> 12:42:44,160 second the third and I'm going to set 20180 12:42:42,756 --> 12:42:46,360 that equal to whatever the beginning of 20181 12:42:44,160 --> 12:42:48,080 the list is so that is going to be 20182 12:42:46,360 --> 12:42:49,756 equivalent to this version of the 20183 12:42:48,080 --> 12:42:52,160 picture here where pointer is just 20184 12:42:49,756 --> 12:42:53,720 temporarily pointing at the first node 20185 12:42:52,160 --> 12:42:55,840 in the list it's not pointing at list 20186 12:42:53,720 --> 12:42:57,880 per se it's pointing at the first node 20187 12:42:55,840 --> 12:43:00,276 in the list which list is also pointing 20188 12:42:57,880 --> 12:43:01,800 at itself all right once I've done this 20189 12:43:00,276 --> 12:43:03,800 I think I can translate this to code 20190 12:43:01,800 --> 12:43:06,916 that's a little new but it's 20191 12:43:03,800 --> 12:43:10,360 conceptually familiar perhaps now while 20192 12:43:06,916 --> 12:43:12,720 that pointer does not equal null so 20193 12:43:10,360 --> 12:43:14,800 while I have a valid pointer like my 20194 12:43:12,720 --> 12:43:16,680 finger or that arrow is pointing at an 20195 12:43:14,800 --> 12:43:18,276 actual node in memory well let me go 20196 12:43:16,680 --> 12:43:21,360 ahead and print it out so let me print 20197 12:43:18,276 --> 12:43:25,640 out with percent I back sln whatever is 20198 12:43:21,360 --> 12:43:27,640 in the current node at the number field 20199 12:43:25,640 --> 12:43:29,400 within and again this is going to have 20200 12:43:27,640 --> 12:43:32,480 the effect hopefully of first printing 20201 12:43:29,400 --> 12:43:35,116 the three and I think I just need to Now 20202 12:43:32,480 --> 12:43:36,756 update the pointer so that on the next 20203 12:43:35,116 --> 12:43:39,400 iteration it's pointing at the next 20204 12:43:36,756 --> 12:43:42,000 value so if this is where the story is 20205 12:43:39,400 --> 12:43:43,880 how do I update pointer to point at the 20206 12:43:42,000 --> 12:43:45,840 second element of the list well I want 20207 12:43:43,880 --> 12:43:48,160 pointer to point at the two and I want 20208 12:43:45,840 --> 12:43:49,720 pointer to eventually point at the three 20209 12:43:48,160 --> 12:43:51,680 well how do I do that well the way in 20210 12:43:49,720 --> 12:43:54,000 code I can follow these arrows is as 20211 12:43:51,680 --> 12:43:56,116 follows if I currently have pointer 20212 12:43:54,000 --> 12:43:59,320 pointing at this node but I want to 20213 12:43:56,116 --> 12:44:01,640 point it at the next node I can borrow 20214 12:43:59,320 --> 12:44:04,240 this pointer here so whatever this 20215 12:44:01,640 --> 12:44:07,080 address is in the first node aka the 20216 12:44:04,240 --> 12:44:09,000 next field I can copy that into pointer 20217 12:44:07,080 --> 12:44:10,720 because then pointer will point at 20218 12:44:09,000 --> 12:44:13,116 whatever this is pointing at by just 20219 12:44:10,720 --> 12:44:16,000 setting one equal to the other so once 20220 12:44:13,116 --> 12:44:19,080 I've done that the picture will 20221 12:44:16,000 --> 12:44:21,916 become this and how do I translate that 20222 12:44:19,080 --> 12:44:24,080 to code it while new syntax is 20223 12:44:21,916 --> 12:44:26,880 surprisingly straightforward all I need 20224 12:44:24,080 --> 12:44:30,320 do is say pointer after printing it 20225 12:44:26,880 --> 12:44:32,520 equals whatever pointer currently is but 20226 12:44:30,320 --> 12:44:34,840 grab its next field 20227 12:44:32,520 --> 12:44:37,160 instead and this is a very common 20228 12:44:34,840 --> 12:44:38,800 Paradigm when iterating over a link list 20229 12:44:37,160 --> 12:44:41,560 and you're using some temporary variable 20230 12:44:38,800 --> 12:44:43,320 like pointer you can simply set pointer 20231 12:44:41,560 --> 12:44:46,560 equal to pointer next and what that 20232 12:44:43,320 --> 12:44:50,000 means here is as follows if this is 20233 12:44:46,560 --> 12:44:52,360 pointer pointing from here down to here 20234 12:44:50,000 --> 12:44:54,560 pointer next is Follow the arrow grab 20235 12:44:52,360 --> 12:44:56,520 the next field so if you set pointer 20236 12:44:54,560 --> 12:44:59,916 equal to this thing that's the same 20237 12:44:56,520 --> 12:45:02,916 thing as pointing this at this same box 20238 12:44:59,916 --> 12:45:04,276 and indeed if I advance to the next 20239 12:45:02,916 --> 12:45:05,560 slide even though the arrows are 20240 12:45:04,276 --> 12:45:06,800 technically pointing at different parts 20241 12:45:05,560 --> 12:45:09,116 of the rectangles that's just for 20242 12:45:06,800 --> 12:45:10,680 graphic sake pointer is now pointing at 20243 12:45:09,116 --> 12:45:12,880 the second node and when I do this again 20244 12:45:10,680 --> 12:45:15,116 on my next iteration it points at this 20245 12:45:12,880 --> 12:45:18,436 and then this last step notice when I 20246 12:45:15,116 --> 12:45:20,800 keep doing pointer equals pointer next 20247 12:45:18,436 --> 12:45:23,880 this will become eventually this value 20248 12:45:20,800 --> 12:45:26,480 but what's this value in this link list 20249 12:45:23,880 --> 12:45:28,840 it's null technically so this Arrow will 20250 12:45:26,480 --> 12:45:31,240 eventually take on this value when I set 20251 12:45:28,840 --> 12:45:34,756 pointer equal to pointer next and at 20252 12:45:31,240 --> 12:45:36,560 that point PTR my temporary pointer is 20253 12:45:34,756 --> 12:45:38,720 going to be null so it might as well 20254 12:45:36,560 --> 12:45:42,000 look like this pictorially and what does 20255 12:45:38,720 --> 12:45:43,756 that mean for my Loop once pointer is 20256 12:45:42,000 --> 12:45:45,480 null because you've walked off the end 20257 12:45:43,756 --> 12:45:49,520 of the length list what's going to be 20258 12:45:45,480 --> 12:45:53,200 true of this Loop here started in line 20259 12:45:49,520 --> 12:45:55,520 32 any OB observations 20260 12:45:53,200 --> 12:45:57,480 here what's going to be true what will 20261 12:45:55,520 --> 12:45:59,000 happen now as soon as we hit the end of 20262 12:45:57,480 --> 12:46:01,596 the list yeah 20263 12:45:59,000 --> 12:46:03,640 sorry the loop is going to break out why 20264 12:46:01,596 --> 12:46:05,880 because line 32 which is constantly 20265 12:46:03,640 --> 12:46:07,956 asking well pointer does not equal null 20266 12:46:05,880 --> 12:46:10,160 well if pointer finally equals null 20267 12:46:07,956 --> 12:46:12,880 three steps later the four the while 20268 12:46:10,160 --> 12:46:14,080 loop is now done and so what I can do at 20269 12:46:12,880 --> 12:46:15,880 the end of this program once I've 20270 12:46:14,080 --> 12:46:17,116 printed out those values well first 20271 12:46:15,880 --> 12:46:20,116 let's go ahead and open my terminal 20272 12:46:17,116 --> 12:46:22,560 window let's make list okay a compile 20273 12:46:20,116 --> 12:46:25,000 do/ list and let me try the same values 20274 12:46:22,560 --> 12:46:27,160 one and two and three that's going to 20275 12:46:25,000 --> 12:46:28,800 again allocate one node two node three 20276 12:46:27,160 --> 12:46:30,840 nodes by prepending prepending 20277 12:46:28,800 --> 12:46:32,320 prepending each of those values and it's 20278 12:46:30,840 --> 12:46:34,640 then going to iterate over them from 20279 12:46:32,320 --> 12:46:36,640 left to right and so when I hit enter 20280 12:46:34,640 --> 12:46:39,240 now what should I see on the screen if 20281 12:46:36,640 --> 12:46:39,240 my code is 20282 12:46:39,360 --> 12:46:43,800 correct what will I see feel fre to just 20283 12:46:41,680 --> 12:46:47,080 call it 20284 12:46:43,800 --> 12:46:49,720 out 321 because I've prepended 20285 12:46:47,080 --> 12:46:52,160 presumably and here we go I indeed see 20286 12:46:49,720 --> 12:46:53,560 321 so the list is is backwards but all 20287 12:46:52,160 --> 12:46:55,400 of the elements are there now 20288 12:46:53,560 --> 12:46:56,880 technically if I ran valgrind on this 20289 12:46:55,400 --> 12:46:59,080 valgren would not be happy because I 20290 12:46:56,880 --> 12:47:00,916 have never freed any of my memory so I 20291 12:46:59,080 --> 12:47:02,680 should probably now have a second Loop 20292 12:47:00,916 --> 12:47:05,276 here that does something like this let 20293 12:47:02,680 --> 12:47:06,880 me again set pointer equal to list I 20294 12:47:05,276 --> 12:47:09,080 don't need to redeclare it because I've 20295 12:47:06,880 --> 12:47:10,400 already created this thing on line 31 I 20296 12:47:09,080 --> 12:47:12,276 just want to reset it to be the 20297 12:47:10,400 --> 12:47:14,480 beginning of the list again and now I 20298 12:47:12,276 --> 12:47:18,956 can do the same kind of thing while PTR 20299 12:47:14,480 --> 12:47:20,880 not equals null go ahead and do this 20300 12:47:18,956 --> 12:47:25,320 well I don't want to just do free 20301 12:47:20,880 --> 12:47:28,640 pointer and then do pointer gets pointer 20302 12:47:25,320 --> 12:47:30,596 next y my goal is to free all of my 20303 12:47:28,640 --> 12:47:32,560 memory but I think this is going to get 20304 12:47:30,596 --> 12:47:34,000 me in trouble pointer equals list just 20305 12:47:32,560 --> 12:47:36,160 gives me a temporary pointer that points 20306 12:47:34,000 --> 12:47:38,200 at the three and then eventually the two 20307 12:47:36,160 --> 12:47:40,116 and then the one how well while pointer 20308 12:47:38,200 --> 12:47:41,916 not equal null I'm freeing the pointer 20309 12:47:40,116 --> 12:47:45,116 so this is like saying to Malo free that 20310 12:47:41,916 --> 12:47:46,956 node free that node free that node but 20311 12:47:45,116 --> 12:47:48,320 what's the problem with what I've just 20312 12:47:46,956 --> 12:47:52,500 done 20313 12:47:48,320 --> 12:47:54,596 here this code is technically Bugg 20314 12:47:52,500 --> 12:47:57,756 [Music] 20315 12:47:54,596 --> 12:48:00,520 yeah exactly after you call free on 20316 12:47:57,756 --> 12:48:03,360 pointer You Are by social contract with 20317 12:48:00,520 --> 12:48:05,276 c not allowed to touch pointer anymore 20318 12:48:03,360 --> 12:48:06,276 it is invalid now it's still going to be 20319 12:48:05,276 --> 12:48:08,560 a number it's still going to be a 20320 12:48:06,276 --> 12:48:11,116 pattern of bits but it's invalid and 20321 12:48:08,560 --> 12:48:13,800 you'll very often get a segmentation 20322 12:48:11,116 --> 12:48:15,640 fault if you tempt fate in that way so I 20323 12:48:13,800 --> 12:48:17,756 can't free the pointer and then use it 20324 12:48:15,640 --> 12:48:19,360 literally the next line the solution 20325 12:48:17,756 --> 12:48:21,480 here kind of like our swapping of the 20326 12:48:19,360 --> 12:48:23,000 liquids last time was to maybe just have 20327 12:48:21,480 --> 12:48:25,320 a temporary variable so I can do a 20328 12:48:23,000 --> 12:48:26,680 Switcheroo and so a common way to solve 20329 12:48:25,320 --> 12:48:28,200 this problem to get the order of 20330 12:48:26,680 --> 12:48:29,800 operations right would be to do 20331 12:48:28,200 --> 12:48:33,160 something like this give yourself a 20332 12:48:29,800 --> 12:48:36,116 temporary pointer like node star next 20333 12:48:33,160 --> 12:48:39,680 set it equal to the place you want to go 20334 12:48:36,116 --> 12:48:42,240 next so one step ahead now you can free 20335 12:48:39,680 --> 12:48:44,436 pointer and then you can update pointer 20336 12:48:42,240 --> 12:48:46,520 to be that next value so essentially you 20337 12:48:44,436 --> 12:48:48,800 need need like two hands now you create 20338 12:48:46,520 --> 12:48:51,560 on line 41 another pointer that if this 20339 12:48:48,800 --> 12:48:53,320 is pointing at the first Noe the three 20340 12:48:51,560 --> 12:48:55,800 your new pointer is pointing at the two 20341 12:48:53,320 --> 12:48:58,560 temporarily so now you can tell malok 20342 12:48:55,800 --> 12:49:00,436 via free release this memory but I 20343 12:48:58,560 --> 12:49:03,720 haven't forgotten where I want to go 20344 12:49:00,436 --> 12:49:06,240 next and so I can now continue on so a 20345 12:49:03,720 --> 12:49:08,276 common Paradigm for just iterating over 20346 12:49:06,240 --> 12:49:10,680 these nodes and then freeing them a 20347 12:49:08,276 --> 12:49:12,276 couple of observations strictly speaking 20348 12:49:10,680 --> 12:49:14,840 I could have Consolidated this I don't 20349 12:49:12,276 --> 12:49:16,640 need two Loops to print the nodes and 20350 12:49:14,840 --> 12:49:18,116 then free the nodes I could do that all 20351 12:49:16,640 --> 12:49:19,956 at once but let's assume that there's 20352 12:49:18,116 --> 12:49:21,116 other stuff of interest in my program 20353 12:49:19,956 --> 12:49:22,840 and I don't want to just immediately 20354 12:49:21,116 --> 12:49:25,200 free it there's one other bug that I 20355 12:49:22,840 --> 12:49:27,400 should probably address here there is 20356 12:49:25,200 --> 12:49:29,800 still a potential memory leak up here 20357 12:49:27,400 --> 12:49:32,240 and this one is super subtle the valind 20358 12:49:29,800 --> 12:49:35,240 would help you find it notice that in 20359 12:49:32,240 --> 12:49:36,916 this Loop here when I'm calling malok 20360 12:49:35,240 --> 12:49:39,640 this line of code is fine if the first 20361 12:49:36,916 --> 12:49:41,000 line of malok fails and returns null 20362 12:49:39,640 --> 12:49:43,560 because I immediately return and I'm 20363 12:49:41,000 --> 12:49:45,480 done but what if the second call but not 20364 12:49:43,560 --> 12:49:47,956 the first or the third call but not the 20365 12:49:45,480 --> 12:49:50,276 first or second fail this line of code 20366 12:49:47,956 --> 12:49:52,480 has me returning immediately you really 20367 12:49:50,276 --> 12:49:54,320 need to to do some garbage collection so 20368 12:49:52,480 --> 12:49:56,720 to speak whereby you really need to go 20369 12:49:54,320 --> 12:49:58,596 in and free any nodes that you did 20370 12:49:56,720 --> 12:49:59,640 allocate successfully earlier honestly 20371 12:49:58,596 --> 12:50:01,800 that's going to be a pin in the neck we 20372 12:49:59,640 --> 12:50:03,320 won't do that here but probably what I'd 20373 12:50:01,800 --> 12:50:05,480 want to do is write a function called 20374 12:50:03,320 --> 12:50:07,436 free list or something like that and 20375 12:50:05,480 --> 12:50:09,956 call that function to free any nodes I 20376 12:50:07,436 --> 12:50:11,840 had previously created so it's not quite 20377 12:50:09,956 --> 12:50:17,000 at the finish line but the building 20378 12:50:11,840 --> 12:50:19,800 blocks are indeed here questions on this 20379 12:50:17,000 --> 12:50:21,680 code and I think it's safe for me to 20380 12:50:19,800 --> 12:50:23,200 promise that it won't escalate further 20381 12:50:21,680 --> 12:50:26,060 from 20382 12:50:23,200 --> 12:50:27,200 that questions on 20383 12:50:26,060 --> 12:50:29,880 [Music] 20384 12:50:27,200 --> 12:50:31,596 this no well let me show you one 20385 12:50:29,880 --> 12:50:33,040 alternative that you might prefer and 20386 12:50:31,596 --> 12:50:34,956 I'm pretty sure this isn't an escalation 20387 12:50:33,040 --> 12:50:36,880 it's just an alternative formulation 20388 12:50:34,956 --> 12:50:38,916 another way you can iterate over nodes 20389 12:50:36,880 --> 12:50:40,840 in a list could be this instead of a 20390 12:50:38,916 --> 12:50:42,520 while loop for instance let me actually 20391 12:50:40,840 --> 12:50:44,320 show you one other piece of syntax here 20392 12:50:42,520 --> 12:50:46,436 you could technically use a for Loop you 20393 12:50:44,320 --> 12:50:48,400 could give yourself a node pointer here 20394 12:50:46,436 --> 12:50:50,400 that initialized is initialized to the 20395 12:50:48,400 --> 12:50:52,080 list you can then check in your for Loop 20396 12:50:50,400 --> 12:50:54,680 that it's not equal to null and then you 20397 12:50:52,080 --> 12:50:56,680 can do your update as usual like this 20398 12:50:54,680 --> 12:50:58,720 either of these are equivalent even 20399 12:50:56,680 --> 12:51:00,680 though this one I suspect looks scarier 20400 12:50:58,720 --> 12:51:02,800 it's doing the exact same thing in one 20401 12:51:00,680 --> 12:51:04,756 line instead of two but there's no 20402 12:51:02,800 --> 12:51:06,756 reason we can't use four Loops instead 20403 12:51:04,756 --> 12:51:07,840 of while Loops to achieve the same idea 20404 12:51:06,756 --> 12:51:09,520 but I'll leave these two as 20405 12:51:07,840 --> 12:51:11,400 demonstrations of one approach or the 20406 12:51:09,520 --> 12:51:13,480 other but that's just like in week one 20407 12:51:11,400 --> 12:51:15,880 four Loops while Loops whatever looks 20408 12:51:13,480 --> 12:51:18,080 simpler to you even though admittedly 20409 12:51:15,880 --> 12:51:20,880 neither of these probably looks super 20410 12:51:18,080 --> 12:51:23,400 clean all right so let's take the back 20411 12:51:20,880 --> 12:51:25,360 to things more conceptual here up until 20412 12:51:23,400 --> 12:51:28,116 now we've been inserting elements into 20413 12:51:25,360 --> 12:51:30,080 this link list by prepending them let's 20414 12:51:28,116 --> 12:51:31,640 consider what the running time then is 20415 12:51:30,080 --> 12:51:34,200 of these operations so if I've got a 20416 12:51:31,640 --> 12:51:35,916 link list of size three or size N More 20417 12:51:34,200 --> 12:51:37,520 generally time has passed and I've added 20418 12:51:35,916 --> 12:51:38,916 a lot of things to it what's going to be 20419 12:51:37,520 --> 12:51:41,880 the running time for instance of 20420 12:51:38,916 --> 12:51:43,720 searching a linked list for some value 20421 12:51:41,880 --> 12:51:45,320 and I'll tell you already it's not login 20422 12:51:43,720 --> 12:51:49,040 because again binary search is off the 20423 12:51:45,320 --> 12:51:50,520 table as per before break so what might 20424 12:51:49,040 --> 12:51:54,080 the running time be of searching a 20425 12:51:50,520 --> 12:51:55,640 linked list for some value like two or 20426 12:51:54,080 --> 12:51:59,916 three or 1 or 20427 12:51:55,640 --> 12:52:03,040 50 what might the running time 20428 12:51:59,916 --> 12:52:05,276 be o of I heard it over here o of n and 20429 12:52:03,040 --> 12:52:07,596 y who was that oh in the middle here why 20430 12:52:05,276 --> 12:52:07,596 o of 20431 12:52:07,910 --> 12:52:11,956 [Music] 20432 12:52:10,080 --> 12:52:13,520 n exactly you're going to have to go 20433 12:52:11,956 --> 12:52:15,116 through every item in the list starting 20434 12:52:13,520 --> 12:52:16,400 from the left from the beginning which 20435 12:52:15,116 --> 12:52:18,240 is how we've been drawing things and 20436 12:52:16,400 --> 12:52:19,560 connecting the dots and in the worst 20437 12:52:18,240 --> 12:52:21,160 case the element might very well be at 20438 12:52:19,560 --> 12:52:24,480 the very end so it's going to be Big O 20439 12:52:21,160 --> 12:52:26,436 of n what about insertion how many steps 20440 12:52:24,480 --> 12:52:28,680 in terms of Big O notation has it been 20441 12:52:26,436 --> 12:52:32,840 taking me to insert elements into the 20442 12:52:28,680 --> 12:52:32,840 link list using this prepended 20443 12:52:32,956 --> 12:52:38,040 design yeah so it's technically constant 20444 12:52:35,596 --> 12:52:39,680 time Big O of one and again one is just 20445 12:52:38,040 --> 12:52:41,436 representative of any constant it could 20446 12:52:39,680 --> 12:52:43,756 technically be two steps or three steps 20447 12:52:41,436 --> 12:52:47,520 or even 10 steps or 100 steps but if 20448 12:52:43,756 --> 12:52:49,680 it's always finite and fixed then indeed 20449 12:52:47,520 --> 12:52:51,436 you can say it's in Big O of one now why 20450 12:52:49,680 --> 12:52:53,480 is that well over again no matter how 20451 12:52:51,436 --> 12:52:55,360 long this list gets so long as there's 20452 12:52:53,480 --> 12:52:57,640 memory available for me I can just kind 20453 12:52:55,360 --> 12:52:59,000 of uh create a little splice at the 20454 12:52:57,640 --> 12:53:01,160 beginning of the list to put in the new 20455 12:52:59,000 --> 12:53:02,956 node update the original list and I'm on 20456 12:53:01,160 --> 12:53:04,240 my way and it keeps getting longer even 20457 12:53:02,956 --> 12:53:06,956 though it might not be spread out in 20458 12:53:04,240 --> 12:53:09,596 memory so big O of one is possible with 20459 12:53:06,956 --> 12:53:12,000 these link lists if I indeed Preen 20460 12:53:09,596 --> 12:53:13,640 things of course if I Preen things 20461 12:53:12,000 --> 12:53:14,596 everything's going to get out of order 20462 12:53:13,640 --> 12:53:16,436 potentially and we're going to have 20463 12:53:14,596 --> 12:53:17,756 maybe the stack property instead of a q 20464 12:53:16,436 --> 12:53:20,080 property so we might want to do things 20465 12:53:17,756 --> 12:53:20,956 slightly differently so instead of doing 20466 12:53:20,080 --> 12:53:23,400 this 20467 12:53:20,956 --> 12:53:25,880 whereby we kept prepending prepending 20468 12:53:23,400 --> 12:53:28,040 prepending suppose we append to the end 20469 12:53:25,880 --> 12:53:29,680 of the list instead so if we now insert 20470 12:53:28,040 --> 12:53:31,680 the one the two and the three as we 20471 12:53:29,680 --> 12:53:33,680 might want to for a q to maintain that 20472 12:53:31,680 --> 12:53:36,400 fairness property we might start with an 20473 12:53:33,680 --> 12:53:39,480 empty list we might add the one we might 20474 12:53:36,400 --> 12:53:41,000 append the two append the three and so 20475 12:53:39,480 --> 12:53:42,756 it just is sort of laid out differently 20476 12:53:41,000 --> 12:53:44,360 in memory and again if I can come to you 20477 12:53:42,756 --> 12:53:46,680 in the middle what's the running time of 20478 12:53:44,360 --> 12:53:49,116 search again when the link list uses 20479 12:53:46,680 --> 12:53:51,480 this append 20480 12:53:49,116 --> 12:53:52,720 implementation yeah still Big O of n 20481 12:53:51,480 --> 12:53:54,320 because in the worst case you're going 20482 12:53:52,720 --> 12:53:56,240 to have to go through the whole list 20483 12:53:54,320 --> 12:53:58,080 just to find it and notice it doesn't 20484 12:53:56,240 --> 12:54:00,000 matter if you have an intuition now that 20485 12:53:58,080 --> 12:54:02,400 the bigger numbers might very well be at 20486 12:54:00,000 --> 12:54:03,880 the end you have no way to jump to the 20487 12:54:02,400 --> 12:54:05,720 end you have no way to jump to the 20488 12:54:03,880 --> 12:54:07,956 middle or do anything resembling binary 20489 12:54:05,720 --> 12:54:10,680 search every search has to start from 20490 12:54:07,956 --> 12:54:12,276 the left and follow the arrows again and 20491 12:54:10,680 --> 12:54:13,720 again all right so I don't think we've 20492 12:54:12,276 --> 12:54:16,840 done any better there and in fact what 20493 12:54:13,720 --> 12:54:18,596 is insertions running time now in Big O 20494 12:54:16,840 --> 12:54:20,880 when we're appending to the list in this 20495 12:54:18,596 --> 12:54:23,560 way as we might to implement a Q instead 20496 12:54:20,880 --> 12:54:25,916 of a stack what's the running time of 20497 12:54:23,560 --> 12:54:28,360 inserting a new 20498 12:54:25,916 --> 12:54:31,520 value Big O 20499 12:54:28,360 --> 12:54:34,880 of so not Big O of one in this case but 20500 12:54:31,520 --> 12:54:37,240 Big O of n because if I'm appending by 20501 12:54:34,880 --> 12:54:38,956 definition I have to start here and 20502 12:54:37,240 --> 12:54:40,436 Traverse the whole thing looking for the 20503 12:54:38,956 --> 12:54:41,916 end now this is a bit of an 20504 12:54:40,436 --> 12:54:43,880 overstatement right you could obviously 20505 12:54:41,916 --> 12:54:46,360 optimize this slightly by maybe adding 20506 12:54:43,880 --> 12:54:48,756 another variable that always points to 20507 12:54:46,360 --> 12:54:50,276 the last element sort of a cheat sheet a 20508 12:54:48,756 --> 12:54:52,080 shortcut that gets you all the way to to 20509 12:54:50,276 --> 12:54:53,320 the end that's totally fine it's not 20510 12:54:52,080 --> 12:54:55,240 doesn't really fit the traditional 20511 12:54:53,320 --> 12:54:56,956 definition of a singly linked list but 20512 12:54:55,240 --> 12:54:58,596 there's absolutely smart engineering 20513 12:54:56,956 --> 12:55:00,596 solutions to these kinds of problems but 20514 12:54:58,596 --> 12:55:02,880 as designed it would indeed be Big O of 20515 12:55:00,596 --> 12:55:04,400 end to insert to if you've got to go all 20516 12:55:02,880 --> 12:55:06,640 the way to the end and you're not using 20517 12:55:04,400 --> 12:55:08,360 a little extra memory to get yourself 20518 12:55:06,640 --> 12:55:11,040 there quickly well what if we want to 20519 12:55:08,360 --> 12:55:12,800 take things one last step and not just a 20520 12:55:11,040 --> 12:55:15,080 pen blindly because even though I 20521 12:55:12,800 --> 12:55:17,040 inserted one two three if I inserted 20522 12:55:15,080 --> 12:55:18,596 them in random order they would end up 20523 12:55:17,040 --> 12:55:21,240 in random order what if you want to 20524 12:55:18,596 --> 12:55:22,916 maintain a sorted list from smallest to 20525 12:55:21,240 --> 12:55:24,756 largest well then you might want to 20526 12:55:22,916 --> 12:55:27,916 insert numbers like this starting from 20527 12:55:24,756 --> 12:55:30,160 an empty list we might have a two then 20528 12:55:27,916 --> 12:55:32,000 we might try inserting a one but we want 20529 12:55:30,160 --> 12:55:34,276 to keep it sorted so now we're going to 20530 12:55:32,000 --> 12:55:36,480 prepend in our code but then you might 20531 12:55:34,276 --> 12:55:38,480 want to insert a four so you would 20532 12:55:36,480 --> 12:55:39,916 append the four because you're probably 20533 12:55:38,480 --> 12:55:42,080 going to look for the right spot to 20534 12:55:39,916 --> 12:55:43,596 insert it then we're going to insert a 20535 12:55:42,080 --> 12:55:45,200 three and this one's getting a little 20536 12:55:43,596 --> 12:55:47,560 annoying because now you have to like 20537 12:55:45,200 --> 12:55:50,400 iterate over the list look for the right 20538 12:55:47,560 --> 12:55:52,320 spot and then do a little smarter of a 20539 12:55:50,400 --> 12:55:54,436 splice but it's possible but you don't 20540 12:55:52,320 --> 12:55:56,596 want to Orphan the four for instance and 20541 12:55:54,436 --> 12:55:58,800 then ultimately we get back to this 20542 12:55:56,596 --> 12:56:00,720 question what would the performance be 20543 12:55:58,800 --> 12:56:04,680 of your linked list if you're trying to 20544 12:56:00,720 --> 12:56:06,200 maintain sorted order well search I 20545 12:56:04,680 --> 12:56:09,320 think is going to be Big O of n for the 20546 12:56:06,200 --> 12:56:13,080 same reasons as before what about 20547 12:56:09,320 --> 12:56:16,520 insertion big go of what for inserting 20548 12:56:13,080 --> 12:56:18,800 into a sorted linked 20549 12:56:16,520 --> 12:56:21,480 list yeah in the worst 20550 12:56:18,800 --> 12:56:23,360 case yeah it's still Big O ofen so it's 20551 12:56:21,480 --> 12:56:25,240 no worse than but it's not really any 20552 12:56:23,360 --> 12:56:27,160 better than a pending but we gain the 20553 12:56:25,240 --> 12:56:28,916 additional property of maintaining a 20554 12:56:27,160 --> 12:56:30,560 sorted list which might very well be 20555 12:56:28,916 --> 12:56:32,200 useful if you're sorting your contacts 20556 12:56:30,560 --> 12:56:33,800 in your phone or something like that 20557 12:56:32,200 --> 12:56:35,756 where it just maintain makes sense to 20558 12:56:33,800 --> 12:56:37,480 maintain sorted order now in the code 20559 12:56:35,756 --> 12:56:39,756 for online today if you take a look at 20560 12:56:37,480 --> 12:56:43,160 some of the final versions of code like 20561 12:56:39,756 --> 12:56:44,840 list 6. C and list 5.c as we'll post on 20562 12:56:43,160 --> 12:56:46,520 the website you can actually see code 20563 12:56:44,840 --> 12:56:48,320 that will solve all three of these 20564 12:56:46,520 --> 12:56:50,320 problems the Preen version that we wrote 20565 12:56:48,320 --> 12:56:52,560 live the appen version which we talk 20566 12:56:50,320 --> 12:56:54,756 through as well as this sorted order one 20567 12:56:52,560 --> 12:56:56,080 but I think I'll avoid showing it live 20568 12:56:54,756 --> 12:56:57,640 just because I do think that starts to 20569 12:56:56,080 --> 12:56:59,000 escalate quickly but I think we have 20570 12:56:57,640 --> 12:57:01,116 enough of a building block if we're 20571 12:56:59,000 --> 12:57:03,116 comfortable with prepending to at least 20572 12:57:01,116 --> 12:57:04,400 solve some real world problems with 20573 12:57:03,116 --> 12:57:07,520 these link 20574 12:57:04,400 --> 12:57:10,956 lists questions then on link list which 20575 12:57:07,520 --> 12:57:14,640 we'll now leave behind on their own but 20576 12:57:10,956 --> 12:57:16,880 now use this technique to solve fancier 20577 12:57:14,640 --> 12:57:21,000 problems but much less 20578 12:57:16,880 --> 12:57:24,000 code questions on linked list all right 20579 12:57:21,000 --> 12:57:25,720 so to C recap we've kind of taken a s 20580 12:57:24,000 --> 12:57:27,480 side step with link list like we have 20581 12:57:25,720 --> 12:57:30,200 this dynamism now where we can grow and 20582 12:57:27,480 --> 12:57:31,436 Shrink our chunks of memory without over 20583 12:57:30,200 --> 12:57:33,240 allocating or accidentally 20584 12:57:31,436 --> 12:57:35,240 underallocation as in the world of an 20585 12:57:33,240 --> 12:57:37,240 array we don't have to worry about 20586 12:57:35,240 --> 12:57:38,800 copying values endlessly because once 20587 12:57:37,240 --> 12:57:40,360 you allocate the node it can just stay 20588 12:57:38,800 --> 12:57:42,480 wherever it is in memory and you can 20589 12:57:40,360 --> 12:57:44,520 just maintain uh you can just Stitch it 20590 12:57:42,480 --> 12:57:46,436 together somehow but unfortunately we've 20591 12:57:44,520 --> 12:57:48,080 sacrificed what we started the class 20592 12:57:46,436 --> 12:57:50,040 with in week zero which was like binary 20593 12:57:48,080 --> 12:57:52,680 search divide and conquer which was like 20594 12:57:50,040 --> 12:57:54,400 gave us that log and running time which 20595 12:57:52,680 --> 12:57:56,320 was really compelling if you think back 20596 12:57:54,400 --> 12:57:58,680 to the demonstrations and the the 20597 12:57:56,320 --> 12:58:02,040 visuals can we get the best of both 20598 12:57:58,680 --> 12:58:04,200 worlds can we get the sort of uh speed 20599 12:58:02,040 --> 12:58:06,000 of binary search something logarithmic 20600 12:58:04,200 --> 12:58:08,320 but the dynamism of something like a 20601 12:58:06,000 --> 12:58:10,360 link list well we can actually I think 20602 12:58:08,320 --> 12:58:12,800 if we start to think not in a single 20603 12:58:10,360 --> 12:58:15,000 Dimension just the x-axis if you will 20604 12:58:12,800 --> 12:58:17,200 but two Dimensions such that our data 20605 12:58:15,000 --> 12:58:19,320 structures can maybe now have width and 20606 12:58:17,200 --> 12:58:21,320 height if you will and so a tree is 20607 12:58:19,320 --> 12:58:23,080 perhaps the right term here much like a 20608 12:58:21,320 --> 12:58:24,840 family tree if you have sort of your 20609 12:58:23,080 --> 12:58:26,596 elders up here in the tree and then the 20610 12:58:24,840 --> 12:58:28,480 branches below them for their children 20611 12:58:26,596 --> 12:58:29,916 and grandchildren and the like that's 20612 12:58:28,480 --> 12:58:31,800 actually what a computer scientist means 20613 12:58:29,916 --> 12:58:33,680 when they talk about trees not a tree 20614 12:58:31,800 --> 12:58:35,640 that grows up like this but really one 20615 12:58:33,680 --> 12:58:37,360 that typically is depicted growing down 20616 12:58:35,640 --> 12:58:39,080 although this is just an artist's 20617 12:58:37,360 --> 12:58:41,000 depiction no matter what but there are 20618 12:58:39,080 --> 12:58:43,400 certain types of trees in the world 20619 12:58:41,000 --> 12:58:45,596 called binary search trees that are 20620 12:58:43,400 --> 12:58:48,520 structured on paper and in visually like 20621 12:58:45,596 --> 12:58:50,276 a uh family tree but they have a special 20622 12:58:48,520 --> 12:58:52,840 property that lends themselves to 20623 12:58:50,276 --> 12:58:55,116 exactly that feature binary search so 20624 12:58:52,840 --> 12:58:57,040 for instance here is an array back from 20625 12:58:55,116 --> 12:58:59,956 week two and I've sorted a whole bunch 20626 12:58:57,040 --> 12:59:02,000 of numbers here in from 1 to seven we 20627 12:58:59,956 --> 12:59:04,240 know we can do binary search on this 20628 12:59:02,000 --> 12:59:06,680 structure if it's implemented as an 20629 12:59:04,240 --> 12:59:09,680 array but what feature do arrays to be 20630 12:59:06,680 --> 12:59:12,000 clear not have that link lists 20631 12:59:09,680 --> 12:59:14,320 do today's kind of a seesaw like what 20632 12:59:12,000 --> 12:59:16,520 did we just gain by adding link list 20633 12:59:14,320 --> 12:59:18,756 that arrays do not 20634 12:59:16,520 --> 12:59:21,596 allow 20635 12:59:18,756 --> 12:59:23,360 yeah 20636 12:59:21,596 --> 12:59:25,040 yeah you can insert more elements 20637 12:59:23,360 --> 12:59:26,640 without having to copy or moving 20638 12:59:25,040 --> 12:59:29,436 everything else around like right now in 20639 12:59:26,640 --> 12:59:31,756 this single Dimension if these values to 20640 12:59:29,436 --> 12:59:33,240 the left and or right are already used 20641 12:59:31,756 --> 12:59:34,880 then you have to move everything and 20642 12:59:33,240 --> 12:59:36,640 that's where we started today's story so 20643 12:59:34,880 --> 12:59:38,560 arrays kind of paint you into a corner 20644 12:59:36,640 --> 12:59:40,520 because you have to by definition decide 20645 12:59:38,560 --> 12:59:42,840 in advance how big they are well 20646 12:59:40,520 --> 12:59:45,640 couldn't we have some kind of array that 20647 12:59:42,840 --> 12:59:48,480 can still grow but still is contiguous 20648 12:59:45,640 --> 12:59:51,240 so we can do binary search in some way 20649 12:59:48,480 --> 12:59:53,400 well yes if we sort of rethink how we 20650 12:59:51,240 --> 12:59:56,840 Implement binary search Let Me propose 20651 12:59:53,400 --> 12:59:58,436 that this I've chosen these seven 20652 12:59:56,840 --> 13:00:01,720 elements in the array much like the 20653 12:59:58,436 --> 13:00:03,240 lockers from uh week two to be ordered 20654 13:00:01,720 --> 13:00:05,200 from smallest to largest I've 20655 13:00:03,240 --> 13:00:07,276 highlighted now in yellow the middle 20656 13:00:05,200 --> 13:00:09,080 elements here and if we were telling the 20657 13:00:07,276 --> 13:00:11,520 story of week two going left or going 20658 13:00:09,080 --> 13:00:13,520 right let me highlight in red the middle 20659 13:00:11,520 --> 13:00:15,436 elements of the left half and the right 20660 13:00:13,520 --> 13:00:18,160 half and then let me further highlight 20661 13:00:15,436 --> 13:00:21,400 in green the other elements in between 20662 13:00:18,160 --> 13:00:23,116 those and there's a pattern here as you 20663 13:00:21,400 --> 13:00:24,640 might notice whereby there's one yellow 20664 13:00:23,116 --> 13:00:26,560 in the middle and then there's the two 20665 13:00:24,640 --> 13:00:28,680 red and the four green there's kind of 20666 13:00:26,560 --> 13:00:30,400 an implicit structure there if you will 20667 13:00:28,680 --> 13:00:32,436 and what if I do start to think in two 20668 13:00:30,400 --> 13:00:35,840 dimensions and instead of laying out an 20669 13:00:32,436 --> 13:00:38,116 array of lockers like this on the x-axis 20670 13:00:35,840 --> 13:00:41,320 only what if I kind of like Slide the 20671 13:00:38,116 --> 13:00:44,276 four up and pull the uh the the one the 20672 13:00:41,320 --> 13:00:46,680 three the five down and kind of draw 20673 13:00:44,276 --> 13:00:49,880 this in two Dimensions instead well let 20674 13:00:46,680 --> 13:00:52,560 me do that as by separating these things 20675 13:00:49,880 --> 13:00:54,320 like this such that now let me propose 20676 13:00:52,560 --> 13:00:56,400 that each of these squares maybe it 20677 13:00:54,320 --> 13:00:58,560 doesn't have to be contiguous it can be 20678 13:00:56,400 --> 13:01:01,240 anywhere in the computer's memory but I 20679 13:00:58,560 --> 13:01:03,200 can't have these crazy gaps among them 20680 13:01:01,240 --> 13:01:06,000 how could I perhaps keep these things 20681 13:01:03,200 --> 13:01:08,956 connected conceptually what should I add 20682 13:01:06,000 --> 13:01:08,956 to the picture if you 20683 13:01:09,200 --> 13:01:15,756 will yeah branches say again so branches 20684 13:01:13,596 --> 13:01:17,796 metaphorically here and more technically 20685 13:01:15,756 --> 13:01:20,480 in the language of C maybe just some 20686 13:01:17,796 --> 13:01:22,240 arrows some pointers so I won't bother 20687 13:01:20,480 --> 13:01:23,436 drawing things as rectangles constantly 20688 13:01:22,240 --> 13:01:25,480 let me propose that we're now just 20689 13:01:23,436 --> 13:01:27,080 abstracting away what a node is but let 20690 13:01:25,480 --> 13:01:29,116 me claim that each of these squares now 20691 13:01:27,080 --> 13:01:30,840 is a node and a node might have a number 20692 13:01:29,116 --> 13:01:33,320 but it might also have a pointer heck 20693 13:01:30,840 --> 13:01:35,080 maybe even two or more pointers and let 20694 13:01:33,320 --> 13:01:37,720 me draw those now I don't care about 20695 13:01:35,080 --> 13:01:39,360 addresses like ox1 2 3 4 5 6 7 8 n 20696 13:01:37,720 --> 13:01:42,320 anymore let's just draw our pointers 20697 13:01:39,360 --> 13:01:46,560 with arrows but now let me propose that 20698 13:01:42,320 --> 13:01:49,520 we could very well think about this as a 20699 13:01:46,560 --> 13:01:52,400 tree storing what was previous 20700 13:01:49,520 --> 13:01:55,040 previously array data but now each of 20701 13:01:52,400 --> 13:01:56,640 these nodes can be anywhere in memory 20702 13:01:55,040 --> 13:01:58,320 and moreover even though I've kind of 20703 13:01:56,640 --> 13:02:00,000 painted myself into a corner visually on 20704 13:01:58,320 --> 13:02:01,916 the screen so long as there's more 20705 13:02:00,000 --> 13:02:04,200 memory in the computer I could put the 20706 13:02:01,916 --> 13:02:06,360 number zero over here I could put the 20707 13:02:04,200 --> 13:02:08,520 number eight over here and I'm if I'm 20708 13:02:06,360 --> 13:02:11,480 smart I could probably if I want to 20709 13:02:08,520 --> 13:02:13,160 insert other numbers like 2.5 or 1.5 or 20710 13:02:11,480 --> 13:02:15,480 values in between you know I bet we 20711 13:02:13,160 --> 13:02:17,240 could kind of make room by swiveling 20712 13:02:15,480 --> 13:02:19,756 things around and just kind of hanging 20713 13:02:17,240 --> 13:02:21,880 things off of these branches slightly 20714 13:02:19,756 --> 13:02:24,160 differently and so what does this gain 20715 13:02:21,880 --> 13:02:26,880 me well if I instead start to model my 20716 13:02:24,160 --> 13:02:29,360 data not single dimensionally but in two 20717 13:02:26,880 --> 13:02:32,360 dimensions and I connect those nodes 20718 13:02:29,360 --> 13:02:34,160 with these pointers what can I now do I 20719 13:02:32,360 --> 13:02:36,080 think I just gave myself back binary 20720 13:02:34,160 --> 13:02:38,400 search why suppose I'm searching for the 20721 13:02:36,080 --> 13:02:39,680 number five how do I find it Well just 20722 13:02:38,400 --> 13:02:41,160 like in a family tree where you might 20723 13:02:39,680 --> 13:02:42,840 visually start reading from top to 20724 13:02:41,160 --> 13:02:44,680 bottom I'm always going to start from 20725 13:02:42,840 --> 13:02:47,596 the so-called root of a binary search 20726 13:02:44,680 --> 13:02:49,200 tree this is just like the list pointer 20727 13:02:47,596 --> 13:02:50,000 that kicks off the whole link list 20728 13:02:49,200 --> 13:02:51,840 process 20729 13:02:50,000 --> 13:02:53,640 this is the so-called route all right 20730 13:02:51,840 --> 13:02:56,080 here I am at the number four I want to 20731 13:02:53,640 --> 13:02:57,400 find the number five what decision can I 20732 13:02:56,080 --> 13:02:59,276 make when I see that I'm currently at 20733 13:02:57,400 --> 13:03:03,080 the number four just like the phone book 20734 13:02:59,276 --> 13:03:06,360 from week zero where is five 20735 13:03:03,080 --> 13:03:07,916 not it's not to the left and if I were 20736 13:03:06,360 --> 13:03:09,436 had you know built a little mobile here 20737 13:03:07,916 --> 13:03:12,200 or something we could very dramatically 20738 13:03:09,436 --> 13:03:14,320 snip off this Branch this is this is 20739 13:03:12,200 --> 13:03:16,436 like very lowbudget animation these 20740 13:03:14,320 --> 13:03:18,560 nodes could like fall to the ground and 20741 13:03:16,436 --> 13:03:20,436 we're left with half of essentially a 20742 13:03:18,560 --> 13:03:21,956 tree but what do I now know it's 20743 13:03:20,436 --> 13:03:24,116 obviously the five to the right so let 20744 13:03:21,956 --> 13:03:25,680 me go to the right six is obviously not 20745 13:03:24,116 --> 13:03:27,880 the one I'm looking for but what do I 20746 13:03:25,680 --> 13:03:30,640 now know about the five well five is 20747 13:03:27,880 --> 13:03:32,880 less than the sixth so I can sort of 20748 13:03:30,640 --> 13:03:34,480 snip this off here because I know it's 20749 13:03:32,880 --> 13:03:36,160 not going to be down there and I can 20750 13:03:34,480 --> 13:03:38,400 follow the remaining Arrow here and 20751 13:03:36,160 --> 13:03:40,200 voila I just found it and now without 20752 13:03:38,400 --> 13:03:42,200 getting into the weeds of the math I've 20753 13:03:40,200 --> 13:03:44,080 got here what Seven Elements that's 20754 13:03:42,200 --> 13:03:46,596 roughly eight if I round up and if I do 20755 13:03:44,080 --> 13:03:48,880 some log base two I actually 1 two three 20756 13:03:46,596 --> 13:03:50,040 is the key detail here the height of 20757 13:03:48,880 --> 13:03:52,436 this tree 20758 13:03:50,040 --> 13:03:55,240 is three because I took a list of size 20759 13:03:52,436 --> 13:03:57,200 seven and IED it and IED it in order to 20760 13:03:55,240 --> 13:03:59,400 let it dangle in these two Dimensions 20761 13:03:57,200 --> 13:04:02,200 plus or minus one for rounding sake so 20762 13:03:59,400 --> 13:04:04,840 what do I get back I now have binary 20763 13:04:02,200 --> 13:04:06,840 search but it's not like H the middle of 20764 13:04:04,840 --> 13:04:08,916 the middle of the middle I now follow 20765 13:04:06,840 --> 13:04:10,796 these arrows in one of two directions so 20766 13:04:08,916 --> 13:04:13,160 each of these nodes now has an INT and 20767 13:04:10,796 --> 13:04:14,916 maybe a left pointer and a right pointer 20768 13:04:13,160 --> 13:04:17,160 but you can call them anything you want 20769 13:04:14,916 --> 13:04:19,520 and so I've gotten back binary search 20770 13:04:17,160 --> 13:04:22,360 and dynamism because if you want to add 20771 13:04:19,520 --> 13:04:24,756 zero or eight or 9 or 10 we can just 20772 13:04:22,360 --> 13:04:26,640 dangle them at the bottom of the binary 20773 13:04:24,756 --> 13:04:28,480 search tree so what would this look like 20774 13:04:26,640 --> 13:04:31,840 in code but we won't actually implement 20775 13:04:28,480 --> 13:04:34,320 it line by line well here was previously 20776 13:04:31,840 --> 13:04:36,320 our definition of a node for a link list 20777 13:04:34,320 --> 13:04:37,640 which was onedimensional if you will 20778 13:04:36,320 --> 13:04:39,916 even though it might bounce up and down 20779 13:04:37,640 --> 13:04:41,640 on the screen it was still just a line 20780 13:04:39,916 --> 13:04:44,040 if you will well let me get rid of the 20781 13:04:41,640 --> 13:04:45,560 single pointer in the linked list let me 20782 13:04:44,040 --> 13:04:47,560 make a little bit of room here in this 20783 13:04:45,560 --> 13:04:49,956 type death and let me propose that we 20784 13:04:47,560 --> 13:04:52,560 just add two pointers each of which is a 20785 13:04:49,956 --> 13:04:55,160 struct node star one will be called left 20786 13:04:52,560 --> 13:04:57,840 by convention one will be called right 20787 13:04:55,160 --> 13:04:59,956 by convention and so long as someone not 20788 13:04:57,840 --> 13:05:01,956 me not today not in class writes the 20789 13:04:59,956 --> 13:05:04,160 code that stitches together this data 20790 13:05:01,956 --> 13:05:06,160 structure too handling both the left 20791 13:05:04,160 --> 13:05:08,560 child and the right child so to speak I 20792 13:05:06,160 --> 13:05:11,240 think we can indeed stitch together that 20793 13:05:08,560 --> 13:05:13,596 two-dimensional structure and moreover 20794 13:05:11,240 --> 13:05:16,116 once you have this in memory you can 20795 13:05:13,596 --> 13:05:18,200 translate pretty elegantly to code 20796 13:05:16,116 --> 13:05:21,796 binary search itself using a principle 20797 13:05:18,200 --> 13:05:24,160 we talked about recently too here is for 20798 13:05:21,796 --> 13:05:26,360 instance a function that I'll write by 20799 13:05:24,160 --> 13:05:28,320 just clicking through steps called 20800 13:05:26,360 --> 13:05:30,276 search whose purpose in life is to 20801 13:05:28,320 --> 13:05:33,000 return a Boolean true or false the 20802 13:05:30,276 --> 13:05:34,520 number I'm looking for is in the tree 20803 13:05:33,000 --> 13:05:36,560 this search function therefore takes two 20804 13:05:34,520 --> 13:05:38,840 arguments the number I'm looking for 20805 13:05:36,560 --> 13:05:41,520 called number and then a pointer to the 20806 13:05:38,840 --> 13:05:43,360 tree the so-called root of the tree now 20807 13:05:41,520 --> 13:05:45,680 how can I Implement binary search in 20808 13:05:43,360 --> 13:05:48,240 code will recall our brief discussion of 20809 13:05:45,680 --> 13:05:50,200 recursion it turns out recursion is a 20810 13:05:48,240 --> 13:05:51,956 beautiful technique and honestly more 20811 13:05:50,200 --> 13:05:54,200 obvious technique when you have two 20812 13:05:51,956 --> 13:05:56,680 dimensional structures which finally 20813 13:05:54,200 --> 13:05:58,956 after five plus weeks we now do here's 20814 13:05:56,680 --> 13:06:01,000 maybe my first line of code here if the 20815 13:05:58,956 --> 13:06:02,520 tree is null then obviously return false 20816 13:06:01,000 --> 13:06:04,320 you've handed me an empty tree there's 20817 13:06:02,520 --> 13:06:05,596 nothing going on obviously the number 20818 13:06:04,320 --> 13:06:07,756 you're looking for is not going to be 20819 13:06:05,596 --> 13:06:09,796 here so that's my like safe base case to 20820 13:06:07,756 --> 13:06:11,756 make sure I don't screw up and recurse 20821 13:06:09,796 --> 13:06:14,520 infinitely well what else might be the 20822 13:06:11,756 --> 13:06:18,116 case well if the number I'm looking for 20823 13:06:14,520 --> 13:06:20,116 is less than the tree's own number and 20824 13:06:18,116 --> 13:06:21,680 now recall that trees a node star so 20825 13:06:20,116 --> 13:06:23,680 even though I'm calling it a tree it's 20826 13:06:21,680 --> 13:06:25,796 really the current node that's been 20827 13:06:23,680 --> 13:06:28,520 passed in so if the number I'm looking 20828 13:06:25,796 --> 13:06:31,520 for is less than the current nodes 20829 13:06:28,520 --> 13:06:33,276 number then I must know that the number 20830 13:06:31,520 --> 13:06:35,000 I'm looking for is to the left so to 20831 13:06:33,276 --> 13:06:37,436 speak so how can I solve that well this 20832 13:06:35,000 --> 13:06:39,640 is where the magic of recursion just 20833 13:06:37,436 --> 13:06:43,160 return whatever the answer is to calling 20834 13:06:39,640 --> 13:06:44,680 search again but on a sub tree if you 20835 13:06:43,160 --> 13:06:47,240 will this is the sort of equivalent of 20836 13:06:44,680 --> 13:06:49,480 snipping off half of the tree pass in 20837 13:06:47,240 --> 13:06:51,916 the left sub tree if you will with the 20838 13:06:49,480 --> 13:06:53,640 same number else if the number you're 20839 13:06:51,916 --> 13:06:56,320 looking for isn't less than the current 20840 13:06:53,640 --> 13:06:58,560 nodes number but greater than snip off 20841 13:06:56,320 --> 13:07:00,956 the other subtree instead and just 20842 13:06:58,560 --> 13:07:03,400 return whatever search says it finds in 20843 13:07:00,956 --> 13:07:05,240 the right subtree here and then there's 20844 13:07:03,400 --> 13:07:06,720 a fourth and final case what else could 20845 13:07:05,240 --> 13:07:08,630 be true 20846 13:07:06,720 --> 13:07:10,520 logically 20847 13:07:08,630 --> 13:07:12,560 [Music] 20848 13:07:10,520 --> 13:07:15,560 yeah perfect if the number you're 20849 13:07:12,560 --> 13:07:17,840 looking for equals equals the number in 20850 13:07:15,560 --> 13:07:19,720 this node then I'm just going to return 20851 13:07:17,840 --> 13:07:23,160 true and you might recall from our 20852 13:07:19,720 --> 13:07:25,080 recurring discussions of design I don't 20853 13:07:23,160 --> 13:07:27,040 strictly need to ask that explicitly 20854 13:07:25,080 --> 13:07:29,000 either there's no node it's to the left 20855 13:07:27,040 --> 13:07:31,480 it's to the right or you found it so I 20856 13:07:29,000 --> 13:07:34,596 can just Whittle that down as usual to 20857 13:07:31,480 --> 13:07:36,400 an else and this now returns my true so 20858 13:07:34,596 --> 13:07:38,040 here too this is where recursion once 20859 13:07:36,400 --> 13:07:40,160 you get comfy with it sort of gets 20860 13:07:38,040 --> 13:07:41,880 pretty elegant and cool in the sense 20861 13:07:40,160 --> 13:07:43,880 that wow even though there's a lot of 20862 13:07:41,880 --> 13:07:45,360 lines here I mean there's only a few 20863 13:07:43,880 --> 13:07:46,880 interesting lines a lot of it's like 20864 13:07:45,360 --> 13:07:48,480 Curly braces at that which strictly 20865 13:07:46,880 --> 13:07:50,880 speaking I could get rid of and so 20866 13:07:48,480 --> 13:07:52,880 recurs lends itself to Elegance when it 20867 13:07:50,880 --> 13:07:55,360 comes to traversing these 20868 13:07:52,880 --> 13:07:56,956 two-dimensional data structures as well 20869 13:07:55,360 --> 13:08:00,240 so that is in code how you might 20870 13:07:56,956 --> 13:08:04,560 Implement something like 20871 13:08:00,240 --> 13:08:07,080 search questions then on these 20872 13:08:04,560 --> 13:08:08,956 trees we have dynamism we can insert 20873 13:08:07,080 --> 13:08:10,880 more nodes to them they're faster 20874 13:08:08,956 --> 13:08:12,680 because we get B search back but but but 20875 13:08:10,880 --> 13:08:14,596 there's got to be a price paid any 20876 13:08:12,680 --> 13:08:16,520 downsides or question or 20877 13:08:14,596 --> 13:08:18,520 downside okay let me come back to that 20878 13:08:16,520 --> 13:08:20,680 in just one sec downside though what 20879 13:08:18,520 --> 13:08:22,840 price of we paid for this dynamism and 20880 13:08:20,680 --> 13:08:24,436 for this binary 20881 13:08:22,840 --> 13:08:27,520 searchability even though I've 20882 13:08:24,436 --> 13:08:29,276 abstracted it away in the 20883 13:08:27,520 --> 13:08:32,080 picture say 20884 13:08:29,276 --> 13:08:33,560 again we're using a lot of memory right 20885 13:08:32,080 --> 13:08:34,956 I'm kind of misleading you now because 20886 13:08:33,560 --> 13:08:36,320 I'm just drawing these little squares 20887 13:08:34,956 --> 13:08:38,000 with the simple numbers but there's 20888 13:08:36,320 --> 13:08:41,320 actually three things in there a four 20889 13:08:38,000 --> 13:08:43,480 byte integer an 8 by left pointer a 8 by 20890 13:08:41,320 --> 13:08:46,116 right pointer so we're already up to 16 20891 13:08:43,480 --> 13:08:47,956 20 bytes now to store individual ins 20892 13:08:46,116 --> 13:08:49,840 that's probably okay though if memory is 20893 13:08:47,956 --> 13:08:52,880 relatively cheap and voluminous as it 20894 13:08:49,840 --> 13:08:54,720 nowadays is but these are the kinds of 20895 13:08:52,880 --> 13:08:57,640 trade-offs and here too you see a hint 20896 13:08:54,720 --> 13:09:00,040 of why some people still do like and use 20897 13:08:57,640 --> 13:09:01,720 C and in fact it's so omnipresent 20898 13:09:00,040 --> 13:09:03,680 because when you have C you can really 20899 13:09:01,720 --> 13:09:05,240 fine-tune how much memory is being used 20900 13:09:03,680 --> 13:09:07,400 for Better or For Worse under the hood 20901 13:09:05,240 --> 13:09:09,400 as we transition soon to python these 20902 13:09:07,400 --> 13:09:11,560 decisions get made for you and you have 20903 13:09:09,400 --> 13:09:13,240 much much less control about how many me 20904 13:09:11,560 --> 13:09:14,720 how much memory is being used by your 20905 13:09:13,240 --> 13:09:17,840 program because someone else made those 20906 13:09:14,720 --> 13:09:20,200 designed decisions for you 20907 13:09:17,840 --> 13:09:21,956 question 20908 13:09:20,200 --> 13:09:24,320 is it bad if we don't know the parent 20909 13:09:21,956 --> 13:09:27,160 node uh not necessarily there's no 20910 13:09:24,320 --> 13:09:29,040 reason why you need to have pointers in 20911 13:09:27,160 --> 13:09:31,320 both directions however that can lend 20912 13:09:29,040 --> 13:09:33,320 itself to efficiency by spending more 20913 13:09:31,320 --> 13:09:34,880 space and having arrows go up too you 20914 13:09:33,320 --> 13:09:36,880 can actually save more time when 20915 13:09:34,880 --> 13:09:38,400 searching the tree in other context this 20916 13:09:36,880 --> 13:09:40,320 though would be the canonical way the 20917 13:09:38,400 --> 13:09:42,320 typical way to implement it um but 20918 13:09:40,320 --> 13:09:44,480 absolutely just like a doubly link list 20919 13:09:42,320 --> 13:09:48,000 that could help you solve other problems 20920 13:09:44,480 --> 13:09:50,040 too all right so turns out I'm kind of 20921 13:09:48,000 --> 13:09:52,480 overselling binary search trees there 20922 13:09:50,040 --> 13:09:54,916 are perversions of them so to speak 20923 13:09:52,480 --> 13:09:57,200 whereby they won't actually behave as 20924 13:09:54,916 --> 13:09:59,276 advertised for instance here's a a good 20925 13:09:57,200 --> 13:10:01,276 situation suppose you've got an empty 20926 13:09:59,276 --> 13:10:03,160 tree initially and you insert the number 20927 13:10:01,276 --> 13:10:04,520 two well it's got to go somewhere so it 20928 13:10:03,160 --> 13:10:06,436 might as well become the root of this 20929 13:10:04,520 --> 13:10:08,200 binary search tree and let's assume that 20930 13:10:06,436 --> 13:10:10,000 someone wrote the code to do this now 20931 13:10:08,200 --> 13:10:11,756 you want to insert the number one and 20932 13:10:10,000 --> 13:10:14,000 you want to maintain the searchability 20933 13:10:11,756 --> 13:10:16,756 of this tree well it's important to note 20934 13:10:14,000 --> 13:10:18,796 that binary search tree is different 20935 13:10:16,756 --> 13:10:21,360 from tree if you just got a tree in 20936 13:10:18,796 --> 13:10:23,116 memory there is no social contract with 20937 13:10:21,360 --> 13:10:25,200 where the numbers need to go they can be 20938 13:10:23,116 --> 13:10:27,640 completely random all over the place 20939 13:10:25,200 --> 13:10:30,436 binary search tree means that you can do 20940 13:10:27,640 --> 13:10:32,240 binary search means that any node here 20941 13:10:30,436 --> 13:10:35,276 is going to be greater than every node 20942 13:10:32,240 --> 13:10:37,160 here and less than every node here and 20943 13:10:35,276 --> 13:10:39,116 that's a definition it's a recursive 20944 13:10:37,160 --> 13:10:42,880 structural definition that must be true 20945 13:10:39,116 --> 13:10:44,240 to be a binary search tree or BST so if 20946 13:10:42,880 --> 13:10:46,720 we maintain that property ourselves let 20947 13:10:44,240 --> 13:10:48,400 me insert two let me insert one one 20948 13:10:46,720 --> 13:10:51,000 belongs there by that definition let me 20949 13:10:48,400 --> 13:10:53,200 insert three three belongs there by that 20950 13:10:51,000 --> 13:10:56,116 definition but I kind of got lucky in 20951 13:10:53,200 --> 13:10:59,200 that I in the story inserted two and 20952 13:10:56,116 --> 13:11:00,720 then one and then three let me propose a 20953 13:10:59,200 --> 13:11:03,116 sort of perversion of the algorithm 20954 13:11:00,720 --> 13:11:05,796 whereby we just get unlucky let me 20955 13:11:03,116 --> 13:11:08,240 propose that we insert in uh insert one 20956 13:11:05,796 --> 13:11:10,200 first and then we insert two well where 20957 13:11:08,240 --> 13:11:11,956 does two go well logically it goes to 20958 13:11:10,200 --> 13:11:14,640 the right because it's larger all right 20959 13:11:11,956 --> 13:11:17,840 now the user inserts three where does it 20960 13:11:14,640 --> 13:11:20,080 go okay it goes there logically and how 20961 13:11:17,840 --> 13:11:22,680 does this story un fold the user inserts 20962 13:11:20,080 --> 13:11:26,360 four five six it's wonderfully sorted in 20963 13:11:22,680 --> 13:11:28,320 advance by luck but this is a perversion 20964 13:11:26,360 --> 13:11:30,560 of the structure in what 20965 13:11:28,320 --> 13:11:35,116 sense it's still technically a binary 20966 13:11:30,560 --> 13:11:38,160 search tree but what does it look more 20967 13:11:35,116 --> 13:11:40,520 like it really is devolving if you will 20968 13:11:38,160 --> 13:11:42,480 into a linked list and so if you the 20969 13:11:40,520 --> 13:11:45,400 programmer don't Implement a binary 20970 13:11:42,480 --> 13:11:47,116 search tree with some kind of repairs 20971 13:11:45,400 --> 13:11:49,596 going on such that as soon as something 20972 13:11:47,116 --> 13:11:51,000 gets whoa a little too long in stringy I 20973 13:11:49,596 --> 13:11:52,680 think I can fix this it's going to be an 20974 13:11:51,000 --> 13:11:54,320 annoying line number of lines of code 20975 13:11:52,680 --> 13:11:57,160 which we're not going to write here or 20976 13:11:54,320 --> 13:11:58,480 or in a pet but we could kind of pivot 20977 13:11:57,160 --> 13:12:00,560 this thing right and we could just 20978 13:11:58,480 --> 13:12:02,956 rejigger things so that the two becomes 20979 13:12:00,560 --> 13:12:04,756 the new root the one becomes the left 20980 13:12:02,956 --> 13:12:06,200 child the three becomes the right child 20981 13:12:04,756 --> 13:12:08,560 but that's what like two three plus 20982 13:12:06,200 --> 13:12:10,956 lines of code it's possible it's doable 20983 13:12:08,560 --> 13:12:13,640 but it's it's extra work it's extra code 20984 13:12:10,956 --> 13:12:16,080 so unless you write that code though and 20985 13:12:13,640 --> 13:12:17,840 maintain balance of these trees just 20986 13:12:16,080 --> 13:12:19,916 because it's a binary search tree does 20987 13:12:17,840 --> 13:12:22,160 not mean its height is going to be log 20988 13:12:19,916 --> 13:12:24,640 base 2 of n the height could be log it 20989 13:12:22,160 --> 13:12:26,360 could be n in which case you don't get 20990 13:12:24,640 --> 13:12:28,560 those properties so when it comes to 20991 13:12:26,360 --> 13:12:30,956 looking up in a balanced binary search 20992 13:12:28,560 --> 13:12:32,436 tree yes it's log in but if it's 20993 13:12:30,956 --> 13:12:34,400 unbalanced if you don't add that 20994 13:12:32,436 --> 13:12:36,640 additional logic and those repairs so to 20995 13:12:34,400 --> 13:12:39,000 speak you could it could devolve into 20996 13:12:36,640 --> 13:12:40,916 Big O of N and this is a whole category 20997 13:12:39,000 --> 13:12:42,436 of algorithms and fanciness that you 20998 13:12:40,916 --> 13:12:44,000 would explore in a higher level course 20999 13:12:42,436 --> 13:12:45,520 on algorithms and data structures 21000 13:12:44,000 --> 13:12:47,320 there's lots of way to do that sort of 21001 13:12:45,520 --> 13:12:49,436 fixing that I'm alluding to in the 21002 13:12:47,320 --> 13:12:51,916 picture there on the screen 21003 13:12:49,436 --> 13:12:54,160 screen all right a few other data 21004 13:12:51,916 --> 13:12:56,956 structures if you will toward an end of 21005 13:12:54,160 --> 13:13:00,240 a sort of computer science Holy Grail so 21006 13:12:56,956 --> 13:13:02,480 log in is repeatedly a really good place 21007 13:13:00,240 --> 13:13:04,756 to end up we started in week zero when 21008 13:13:02,480 --> 13:13:06,680 we got log in we lost it this earlier 21009 13:13:04,756 --> 13:13:08,720 today by introducing link list but we 21010 13:13:06,680 --> 13:13:11,480 just got it back albeit at the price of 21011 13:13:08,720 --> 13:13:13,000 spending more space but the Holy Grail 21012 13:13:11,480 --> 13:13:14,916 so to speak when it comes to algorithms 21013 13:13:13,000 --> 13:13:16,840 would not be Big O of n certainly 21014 13:13:14,916 --> 13:13:18,596 definitely not n squar like our mer like 21015 13:13:16,840 --> 13:13:20,956 our bubble sorts and selection sorts and 21016 13:13:18,596 --> 13:13:23,200 not even Big O of Logan what's better 21017 13:13:20,956 --> 13:13:26,436 than all of 21018 13:13:23,200 --> 13:13:27,880 those big O of one constant time right 21019 13:13:26,436 --> 13:13:29,596 that's the Holy Grail because if we 21020 13:13:27,880 --> 13:13:32,240 could store huge amounts of data but 21021 13:13:29,596 --> 13:13:34,680 find it instantly in one step or two 21022 13:13:32,240 --> 13:13:36,436 steps or heck even 10 or 20 steps but 21023 13:13:34,680 --> 13:13:38,596 independent of the size of the data 21024 13:13:36,436 --> 13:13:40,200 structure that's pretty powerful I mean 21025 13:13:38,596 --> 13:13:41,796 that's the secret sauce of the Googles 21026 13:13:40,200 --> 13:13:44,596 and the twitters of the world trying to 21027 13:13:41,796 --> 13:13:47,320 get back results really really fast well 21028 13:13:44,596 --> 13:13:49,040 it turns out another abstract data type 21029 13:13:47,320 --> 13:13:50,320 or abstract data structure might be 21030 13:13:49,040 --> 13:13:52,200 something called a dictionary just like 21031 13:13:50,320 --> 13:13:54,276 the maram Webster Oxford English 21032 13:13:52,200 --> 13:13:56,560 dictionaries that you might know which 21033 13:13:54,276 --> 13:13:57,840 associate say words with definitions 21034 13:13:56,560 --> 13:13:59,480 well you can think of a dictionary 21035 13:13:57,840 --> 13:14:01,956 really abstractly as this like two 21036 13:13:59,480 --> 13:14:03,320 columns maybe on a spreadsheet of sorts 21037 13:14:01,956 --> 13:14:04,320 where the left column represents 21038 13:14:03,320 --> 13:14:05,720 something and the right column 21039 13:14:04,320 --> 13:14:07,640 represents something else like the word 21040 13:14:05,720 --> 13:14:09,000 is on the left and its definition is on 21041 13:14:07,640 --> 13:14:10,796 the right and that's almost literally 21042 13:14:09,000 --> 13:14:12,796 what a dictionary is on paper you've got 21043 13:14:10,796 --> 13:14:15,240 all the words and all the definitions 21044 13:14:12,796 --> 13:14:18,080 right next to it but more generally in 21045 13:14:15,240 --> 13:14:20,520 Computing a dictionary really just has 21046 13:14:18,080 --> 13:14:22,520 not words and definitions per se but key 21047 13:14:20,520 --> 13:14:24,080 value pairs this is a term of Art and 21048 13:14:22,520 --> 13:14:25,480 we're going to see this again and again 21049 13:14:24,080 --> 13:14:28,400 especially as we transition to web 21050 13:14:25,480 --> 13:14:31,240 programming keys and values key is what 21051 13:14:28,400 --> 13:14:34,796 you use to look for something the value 21052 13:14:31,240 --> 13:14:36,680 is what you find ultimately via that key 21053 13:14:34,796 --> 13:14:38,756 so that's the generic term there we've 21054 13:14:36,680 --> 13:14:40,040 seen key value pairs really in the past 21055 13:14:38,756 --> 13:14:42,436 in week zero we talked about your 21056 13:14:40,040 --> 13:14:43,916 contacts in your iPhone or Android phone 21057 13:14:42,436 --> 13:14:45,916 uh being an app that has a whole bunch 21058 13:14:43,916 --> 13:14:48,160 of contexts presumably alphabetized by 21059 13:14:45,916 --> 13:14:49,880 first name or last name or the like well 21060 13:14:48,160 --> 13:14:51,756 one one of those contact cards 21061 13:14:49,880 --> 13:14:53,956 ultimately has someone's number for 21062 13:14:51,756 --> 13:14:56,480 instance like John Harvard in this case 21063 13:14:53,956 --> 13:14:58,560 so in that type of application the keys 21064 13:14:56,480 --> 13:15:01,640 is the name like John Harvard that you 21065 13:14:58,560 --> 13:15:03,480 use to find information and the value is 21066 13:15:01,640 --> 13:15:05,040 the number that you find there or if 21067 13:15:03,480 --> 13:15:07,240 there's more information like where he 21068 13:15:05,040 --> 13:15:09,240 lives and uh email address and the like 21069 13:15:07,240 --> 13:15:11,840 the whole contact card could be the 21070 13:15:09,240 --> 13:15:14,000 value thereof the key is what you use to 21071 13:15:11,840 --> 13:15:16,520 look up John Harvard now back in week 21072 13:15:14,000 --> 13:15:17,916 zero oh and rather the corresponding 21073 13:15:16,520 --> 13:15:19,596 table then if we draw this in two 21074 13:15:17,916 --> 13:15:21,320 columns wouldn't be word and definition 21075 13:15:19,596 --> 13:15:23,400 or key value generically it would be 21076 13:15:21,320 --> 13:15:25,360 name and number for instance so we're 21077 13:15:23,400 --> 13:15:27,596 just slapping some new terminology on 21078 13:15:25,360 --> 13:15:29,240 this old contact problem well this is 21079 13:15:27,596 --> 13:15:32,640 the picture we drew way back in week 21080 13:15:29,240 --> 13:15:34,596 zero whereby I claimed that log of n was 21081 13:15:32,640 --> 13:15:36,916 really really good and indeed it was and 21082 13:15:34,596 --> 13:15:38,756 has been since but the Holy Grail would 21083 13:15:36,916 --> 13:15:41,400 indeed be something more like this in 21084 13:15:38,756 --> 13:15:43,436 this dashed Green Line constant time and 21085 13:15:41,400 --> 13:15:45,080 maybe not literally one step but a fixed 21086 13:15:43,436 --> 13:15:47,596 number of steps that even as the problem 21087 13:15:45,080 --> 13:15:50,956 gets huge and you go way way out on the 21088 13:15:47,596 --> 13:15:54,240 right of the X AIS the problem does not 21089 13:15:50,956 --> 13:15:55,956 depend on the side the uh the uh the 21090 13:15:54,240 --> 13:15:58,916 time to solve the problem does not 21091 13:15:55,956 --> 13:16:00,796 depend at all on the size of the problem 21092 13:15:58,916 --> 13:16:03,596 itself you can have a thousand contacts 21093 13:16:00,796 --> 13:16:05,240 or 100,000 contacts constant time means 21094 13:16:03,596 --> 13:16:07,720 it takes the same number of steps no 21095 13:16:05,240 --> 13:16:09,160 matter what well how can we get to that 21096 13:16:07,720 --> 13:16:11,040 point well there's a couple of final 21097 13:16:09,160 --> 13:16:12,360 building blocks today and there's one 21098 13:16:11,040 --> 13:16:14,916 called hashing and this is something 21099 13:16:12,360 --> 13:16:17,680 that will recur a few times but for now 21100 13:16:14,916 --> 13:16:19,160 hashing is all about taking as input 21101 13:16:17,680 --> 13:16:22,756 some value 21102 13:16:19,160 --> 13:16:25,116 and outputting a simpler version thereof 21103 13:16:22,756 --> 13:16:27,000 so for instance here's a a gratuitously 21104 13:16:25,116 --> 13:16:29,160 large deck of cards which are all the 21105 13:16:27,000 --> 13:16:30,680 more visible as a result and in a deck 21106 13:16:29,160 --> 13:16:32,916 of cards typically you've got like what 21107 13:16:30,680 --> 13:16:34,720 52 cards plus maybe the Jokers and 21108 13:16:32,916 --> 13:16:37,080 whatnot and each of those cards has a 21109 13:16:34,720 --> 13:16:38,956 number of sorts and a suit on it and 21110 13:16:37,080 --> 13:16:41,360 here are literally four buckets on the 21111 13:16:38,956 --> 13:16:43,240 stage and how might I go about sorting 21112 13:16:41,360 --> 13:16:44,916 these cards not just by number but also 21113 13:16:43,240 --> 13:16:46,756 by suit well you could certainly like 21114 13:16:44,916 --> 13:16:48,320 spread them all out and sort of make a 21115 13:16:46,756 --> 13:16:49,680 mess of things and just kind of reason 21116 13:16:48,320 --> 13:16:51,840 your way through it and get everything 21117 13:16:49,680 --> 13:16:53,596 in order according to suit and corded by 21118 13:16:51,840 --> 13:16:55,400 number but most of us even if you don't 21119 13:16:53,596 --> 13:16:57,080 have four buckets at home probably are 21120 13:16:55,400 --> 13:16:59,000 going to do something a little more 21121 13:16:57,080 --> 13:17:01,360 intuitive feels like an optimization 21122 13:16:59,000 --> 13:17:02,796 where if I find like the nine of Hearts 21123 13:17:01,360 --> 13:17:04,560 I'm going to put that into the hearts 21124 13:17:02,796 --> 13:17:06,360 bucket the King of Spades I'm going to 21125 13:17:04,560 --> 13:17:08,680 put that into the Spades bucket the jack 21126 13:17:06,360 --> 13:17:11,080 of diamonds over here and I'll do this 21127 13:17:08,680 --> 13:17:13,720 with the Queen of Diamonds and uh the 21128 13:17:11,080 --> 13:17:15,520 Ace of clubs here and the three here and 21129 13:17:13,720 --> 13:17:18,200 the 10 here and even though it's still 21130 13:17:15,520 --> 13:17:20,560 going to be 52 steps why am I and maybe 21131 13:17:18,200 --> 13:17:21,916 at home like why would you perhaps do 21132 13:17:20,560 --> 13:17:26,400 this step 21133 13:17:21,916 --> 13:17:26,400 first what's the value of bucketization 21134 13:17:48,080 --> 13:17:51,040 reduces the probability of errors or the 21135 13:17:49,560 --> 13:17:53,320 like and what I'm doing here to give it 21136 13:17:51,040 --> 13:17:55,360 a technical term is that I'm hashing the 21137 13:17:53,320 --> 13:17:58,320 values I'm taking as input a card like 21138 13:17:55,360 --> 13:18:01,276 this and I'm reducing it more simply 21139 13:17:58,320 --> 13:18:04,360 from a larger domain to a much smaller 21140 13:18:01,276 --> 13:18:06,756 range if you will so here's a domain of 21141 13:18:04,360 --> 13:18:09,596 like 52 possibilities I want to map that 21142 13:18:06,756 --> 13:18:12,160 to a range of four possible outcomes the 21143 13:18:09,596 --> 13:18:14,360 Diamonds the clubs the carts or the 21144 13:18:12,160 --> 13:18:15,880 Spades here and by doing that I'm just 21145 13:18:14,360 --> 13:18:17,520 shrinking the size of the problem so 21146 13:18:15,880 --> 13:18:19,956 hashing does that it's like literally an 21147 13:18:17,520 --> 13:18:22,320 F ofx type Arrangement whereby you pass 21148 13:18:19,956 --> 13:18:25,040 something in and you get back a simpler 21149 13:18:22,320 --> 13:18:27,720 known value well a hash function more 21150 13:18:25,040 --> 13:18:29,880 technically is the algorithm or even the 21151 13:18:27,720 --> 13:18:32,720 math or even the code that implements 21152 13:18:29,880 --> 13:18:34,916 that idea converting something bigger to 21153 13:18:32,720 --> 13:18:37,956 something smaller to this indeed finite 21154 13:18:34,916 --> 13:18:42,320 range of values and it turns out that 21155 13:18:37,956 --> 13:18:45,160 hash tables are a wonderful application 21156 13:18:42,320 --> 13:18:47,360 of arrays and length lists to try to 21157 13:18:45,160 --> 13:18:49,520 Leverage The Best of Both Worlds the 21158 13:18:47,360 --> 13:18:52,720 goal being theoretically to achieve that 21159 13:18:49,520 --> 13:18:53,956 Holy Grail of constant time and that's 21160 13:18:52,720 --> 13:18:54,916 going to be a bit of an overstatement 21161 13:18:53,956 --> 13:18:56,956 because you're not always going to 21162 13:18:54,916 --> 13:18:59,000 achieve it exactly but at least we can 21163 13:18:56,956 --> 13:19:00,880 get a little closer there too so with 21164 13:18:59,000 --> 13:19:03,276 hash tables you have something that 21165 13:19:00,880 --> 13:19:04,956 looks like this this is just an array 21166 13:19:03,276 --> 13:19:06,916 this is an artist rendition of drawing 21167 13:19:04,956 --> 13:19:09,796 it vertically instead of horizontally 21168 13:19:06,916 --> 13:19:12,796 but that's just a a detail graphically 21169 13:19:09,796 --> 13:19:15,360 and this array for instance maybe uh is 21170 13:19:12,796 --> 13:19:17,360 of size 26 and where am I going with 21171 13:19:15,360 --> 13:19:20,320 this well how does Apple how does Google 21172 13:19:17,360 --> 13:19:22,956 Store your contexts alphabetically in 21173 13:19:20,320 --> 13:19:24,436 your phone and search for things quickly 21174 13:19:22,956 --> 13:19:25,720 well they might they probably 21175 13:19:24,436 --> 13:19:27,520 alphabetize at least in English 21176 13:19:25,720 --> 13:19:28,796 according to a through z or if we 21177 13:19:27,520 --> 13:19:31,360 convert that to numbers it's like what 21178 13:19:28,796 --> 13:19:33,796 65 through whatever or really 0 through 21179 13:19:31,360 --> 13:19:36,116 25 suffices if we're using an array of 21180 13:19:33,796 --> 13:19:38,240 size 26 we start counting at zero and we 21181 13:19:36,116 --> 13:19:40,596 count up to 25 but let's abstract that 21182 13:19:38,240 --> 13:19:42,320 away as just letters of the alphabet so 21183 13:19:40,596 --> 13:19:44,116 maybe what Google and apple are doing in 21184 13:19:42,320 --> 13:19:46,116 your phone is storing all of the A's up 21185 13:19:44,116 --> 13:19:48,400 there all of the Z's down there and 21186 13:19:46,116 --> 13:19:50,040 everything else in between and so this 21187 13:19:48,400 --> 13:19:51,560 works pretty well if you start adding 21188 13:19:50,040 --> 13:19:53,240 your friends and your family so for 21189 13:19:51,560 --> 13:19:55,796 instance and I'll get rid of the letter 21190 13:19:53,240 --> 13:19:57,916 so it's to not distract uh alvus might 21191 13:19:55,796 --> 13:20:00,080 go in that first spot because a you 21192 13:19:57,916 --> 13:20:02,200 subtract the 65 maps to zero so we put 21193 13:20:00,080 --> 13:20:03,880 him in the first bucket the a bucket uh 21194 13:20:02,200 --> 13:20:05,400 maybe Zacharias ends up all the way at 21195 13:20:03,880 --> 13:20:07,436 the end there and then in the middle 21196 13:20:05,400 --> 13:20:08,956 might here be Hermione and if we do this 21197 13:20:07,436 --> 13:20:12,200 dot dot dot you keep adding all of your 21198 13:20:08,956 --> 13:20:14,160 classmates you might get a uh contact 21199 13:20:12,200 --> 13:20:17,200 database that has all of this data here 21200 13:20:14,160 --> 13:20:18,596 in now each of these nodes they're drawn 21201 13:20:17,200 --> 13:20:21,000 differently because this is just another 21202 13:20:18,596 --> 13:20:23,040 artist rendition these rectangles these 21203 13:20:21,000 --> 13:20:24,756 long rectangles represent a contact card 21204 13:20:23,040 --> 13:20:26,956 like John Harvard's that's got the name 21205 13:20:24,756 --> 13:20:31,560 maybe email definitely phone number and 21206 13:20:26,956 --> 13:20:34,116 things like that so this seems great why 21207 13:20:31,560 --> 13:20:36,080 how can I find Albus well I go to the a 21208 13:20:34,116 --> 13:20:37,880 bucket how do I find Zacharias I go to 21209 13:20:36,080 --> 13:20:41,560 the Z bucket how do I find Hermione I go 21210 13:20:37,880 --> 13:20:44,276 to the H bucket but but but I've done 21211 13:20:41,560 --> 13:20:46,680 this very deliberately what problem will 21212 13:20:44,276 --> 13:20:49,840 arise eventually assuming you have 21213 13:20:46,680 --> 13:20:49,840 enough classmates 21214 13:20:51,680 --> 13:20:55,796 yeah there'll be too many people too 21215 13:20:53,160 --> 13:20:57,680 many contacts for all of the available 21216 13:20:55,796 --> 13:21:00,560 spaces in the array there's still some 21217 13:20:57,680 --> 13:21:02,880 room here but I'm pretty sure if I think 21218 13:21:00,560 --> 13:21:05,596 back to this particular class uh we've 21219 13:21:02,880 --> 13:21:08,756 got not hermy but also Harry who's also 21220 13:21:05,596 --> 13:21:10,640 an H Hagrid who's also an H so where do 21221 13:21:08,756 --> 13:21:13,240 I put them I could just put them 21222 13:21:10,640 --> 13:21:15,320 arbitrarily in any of the open spots but 21223 13:21:13,240 --> 13:21:16,916 then you lose the immediacy of jumping 21224 13:21:15,320 --> 13:21:19,000 right to the H right to the a right to 21225 13:21:16,916 --> 13:21:20,796 the Z but now that we have link lists we 21226 13:21:19,000 --> 13:21:23,360 can kind of combine these ideas right 21227 13:21:20,796 --> 13:21:25,200 use an array to get to the first letter 21228 13:21:23,360 --> 13:21:28,160 of the name you care about and then if 21229 13:21:25,200 --> 13:21:29,880 you have a collision so to speak whereby 21230 13:21:28,160 --> 13:21:31,840 someone's already there you don't do 21231 13:21:29,880 --> 13:21:33,276 something stupid like put Harry down 21232 13:21:31,840 --> 13:21:35,000 here just because it's available or 21233 13:21:33,276 --> 13:21:36,640 maybe Hagrid down here just because it's 21234 13:21:35,000 --> 13:21:38,680 available because then you're losing the 21235 13:21:36,640 --> 13:21:40,916 immediacy of the lookup why don't you 21236 13:21:38,680 --> 13:21:43,436 just kind of stitch them together in a 21237 13:21:40,916 --> 13:21:46,040 linked list now what does this mean this 21238 13:21:43,436 --> 13:21:47,956 means for most of the characters here 21239 13:21:46,040 --> 13:21:49,840 you have constant time lookup you look 21240 13:21:47,956 --> 13:21:53,040 up alvas boom you're done Zacharias boom 21241 13:21:49,840 --> 13:21:55,720 you're done okay Harry Hermione Hagrid 21242 13:21:53,040 --> 13:21:57,320 it might be one two or three steps so 21243 13:21:55,720 --> 13:21:59,160 that's actually devolving into something 21244 13:21:57,320 --> 13:22:01,200 linear but here we make a distinction 21245 13:21:59,160 --> 13:22:03,756 today between theoretical running times 21246 13:22:01,200 --> 13:22:05,796 which we keep talking about and honestly 21247 13:22:03,756 --> 13:22:08,276 a clock on the wall running times that 21248 13:22:05,796 --> 13:22:11,400 actual humans care about this is way 21249 13:22:08,276 --> 13:22:13,796 faster than a linked list because you 21250 13:22:11,400 --> 13:22:16,520 don't have to search every name it's 21251 13:22:13,796 --> 13:22:18,240 even faster than a an array because you 21252 13:22:16,520 --> 13:22:20,680 don't need to do binary search 21253 13:22:18,240 --> 13:22:24,116 you can literally for most of the names 21254 13:22:20,680 --> 13:22:25,720 find them in constant time one step and 21255 13:22:24,116 --> 13:22:27,276 again it's not theoretically constant 21256 13:22:25,720 --> 13:22:28,796 because these if you only befriend 21257 13:22:27,276 --> 13:22:31,880 people who have H names it's going to be 21258 13:22:28,796 --> 13:22:33,436 a crazy long link list anyway so again 21259 13:22:31,880 --> 13:22:35,756 it really kind of depends on what the 21260 13:22:33,436 --> 13:22:38,240 nature of the data is here but this is 21261 13:22:35,756 --> 13:22:39,720 pretty close to constant time and in 21262 13:22:38,240 --> 13:22:41,276 fact how could we get even closer how 21263 13:22:39,720 --> 13:22:44,480 could we reduce the probability of 21264 13:22:41,276 --> 13:22:46,596 collisions for the H's or any other 21265 13:22:44,480 --> 13:22:49,680 letters how could we avoid putting too 21266 13:22:46,596 --> 13:22:49,680 many H names together 21267 13:22:51,956 --> 13:22:54,360 say a little 21268 13:22:57,400 --> 13:23:01,040 L okay yeah so we could add another 21269 13:22:59,756 --> 13:23:02,640 dimension if you will but let's not add 21270 13:23:01,040 --> 13:23:04,360 a third dimension per se but let's 21271 13:23:02,640 --> 13:23:06,400 indeed look at not just the first letter 21272 13:23:04,360 --> 13:23:08,160 of everyone's name but the first and the 21273 13:23:06,400 --> 13:23:10,916 second and in fact let's see if that 21274 13:23:08,160 --> 13:23:12,520 gets us a little uh further along so let 21275 13:23:10,916 --> 13:23:13,840 me go ahead and propose if you go 21276 13:23:12,520 --> 13:23:15,116 through the whole Harry Potter Universe 21277 13:23:13,840 --> 13:23:17,160 there's actually a lot of collisions if 21278 13:23:15,116 --> 13:23:19,480 we keep going and so we've got the L's 21279 13:23:17,160 --> 13:23:21,400 here the RS the S's and so forth well 21280 13:23:19,480 --> 13:23:23,640 let's clean this up here Hermione 21281 13:23:21,400 --> 13:23:25,680 originally went to the H location but 21282 13:23:23,640 --> 13:23:27,480 let's decrease the probability of 21283 13:23:25,680 --> 13:23:29,916 collisions there and everywhere else 21284 13:23:27,480 --> 13:23:32,040 instead of putting hermion Harry and 21285 13:23:29,916 --> 13:23:34,436 Hagrid Al together let's go ahead and do 21286 13:23:32,040 --> 13:23:36,596 this instead instead of labeling these 21287 13:23:34,436 --> 13:23:38,400 buckets A through Z let's just give 21288 13:23:36,596 --> 13:23:40,480 ourselves more buckets so in fact this 21289 13:23:38,400 --> 13:23:43,080 might be H well instead of H maybe this 21290 13:23:40,480 --> 13:23:45,796 should be ha and then this should be HB 21291 13:23:43,080 --> 13:23:47,880 HC HD h e HF now some of those are a 21292 13:23:45,796 --> 13:23:50,320 little nonsensical cuz I can't think of 21293 13:23:47,880 --> 13:23:52,596 names that match most of those but it's 21294 13:23:50,320 --> 13:23:54,796 deterministic at least we know the 21295 13:23:52,596 --> 13:23:56,596 bucket will be there which is important 21296 13:23:54,796 --> 13:23:59,116 even if it's empty and now we can put 21297 13:23:56,596 --> 13:24:01,320 Hermione here we can put Harry here but 21298 13:23:59,116 --> 13:24:03,276 uhoh we didn't do this perfectly well 21299 13:24:01,320 --> 13:24:05,240 Hagrid still Collide so let me come back 21300 13:24:03,276 --> 13:24:07,640 to you how can we reduce the probability 21301 13:24:05,240 --> 13:24:09,480 of Harry and Hagrid 21302 13:24:07,640 --> 13:24:11,520 colliding yeah so we could look at the 21303 13:24:09,480 --> 13:24:15,596 third letter okay so let me try that 21304 13:24:11,520 --> 13:24:18,796 instead of ha let's look at haa ha ha C 21305 13:24:15,596 --> 13:24:22,116 dot dot dot haq dot dot dot h h e q h e 21306 13:24:18,796 --> 13:24:24,116 r hes and so forth and now I think those 21307 13:24:22,116 --> 13:24:27,720 names and probably all the others we saw 21308 13:24:24,116 --> 13:24:29,756 are now much more cleanly distributed 21309 13:24:27,720 --> 13:24:31,360 there's much lower probability of 21310 13:24:29,756 --> 13:24:33,040 collisions unless two people have like 21311 13:24:31,360 --> 13:24:35,916 almost the same names or one is like a 21312 13:24:33,040 --> 13:24:38,116 prefix of the other but but but even 21313 13:24:35,916 --> 13:24:40,480 though we're now closer than ever to 21314 13:24:38,116 --> 13:24:42,160 constant time because the odds that we 21315 13:24:40,480 --> 13:24:44,320 hit a collision and have to devolve to a 21316 13:24:42,160 --> 13:24:46,320 link list or much lower what's the 21317 13:24:44,320 --> 13:24:49,160 downside that's not completely obvious 21318 13:24:46,320 --> 13:24:52,240 from how I've depicted this on 21319 13:24:49,160 --> 13:24:55,680 screen what's the price I'm paying here 21320 13:24:52,240 --> 13:24:57,796 yeah this is a huge amount of memory the 21321 13:24:55,680 --> 13:25:01,360 number of cells here in the array is now 21322 13:24:57,796 --> 13:25:03,916 what 26 * 26 * 26 for the first the 21323 13:25:01,360 --> 13:25:05,756 second and the third possible characters 21324 13:25:03,916 --> 13:25:07,160 all combinatorically combined here 21325 13:25:05,756 --> 13:25:09,240 that's a lot I didn't even draw them I 21326 13:25:07,160 --> 13:25:11,436 have the dot dot dot to evoke that 21327 13:25:09,240 --> 13:25:13,840 instead that's a huge amount of memory 21328 13:25:11,436 --> 13:25:15,756 this is a very sparse data set now and 21329 13:25:13,840 --> 13:25:19,880 odds are you're going to waste so much 21330 13:25:15,756 --> 13:25:21,956 memory even for the names like Hae ha 21331 13:25:19,880 --> 13:25:23,360 like HQ like I can't even think of names 21332 13:25:21,956 --> 13:25:26,680 so many of those buckets are going to be 21333 13:25:23,360 --> 13:25:29,116 empty not to mention the AAA and the zzz 21334 13:25:26,680 --> 13:25:31,040 and everything else in between so it's a 21335 13:25:29,116 --> 13:25:32,640 trade-off and it might be too expensive 21336 13:25:31,040 --> 13:25:34,040 a trade-off and so you might have to 21337 13:25:32,640 --> 13:25:36,000 tolerate something like the collisions 21338 13:25:34,040 --> 13:25:39,520 we had earlier whereby even though they 21339 13:25:36,000 --> 13:25:42,240 might very well happen at least you uh 21340 13:25:39,520 --> 13:25:44,000 are decreasing the probability by 21341 13:25:42,240 --> 13:25:45,840 perhaps having more buckets like this 21342 13:25:44,000 --> 13:25:47,640 and in fact if I rewind now to where we 21343 13:25:45,840 --> 13:25:50,000 might have gone with this here here's 21344 13:25:47,640 --> 13:25:52,360 how we might represent these nodes in 21345 13:25:50,000 --> 13:25:54,080 the tree previously in the past we've 21346 13:25:52,360 --> 13:25:57,276 had a person who had a string name and a 21347 13:25:54,080 --> 13:26:00,520 string number AKA now Char star and so 21348 13:25:57,276 --> 13:26:03,560 here now might be how in this hash table 21349 13:26:00,520 --> 13:26:05,640 we represent someone's name and number 21350 13:26:03,560 --> 13:26:07,796 as well as a pointer to the next element 21351 13:26:05,640 --> 13:26:09,560 in the list we let me rewind just to the 21352 13:26:07,796 --> 13:26:10,720 picture here we keep drawing different 21353 13:26:09,560 --> 13:26:12,160 shapes because again these are 21354 13:26:10,720 --> 13:26:14,160 abstractions who really cares if they're 21355 13:26:12,160 --> 13:26:15,956 to scale now we've got enough room for 21356 13:26:14,160 --> 13:26:18,000 the person's name not pictured on the 21357 13:26:15,956 --> 13:26:20,320 screen is herm's number that's somewhere 21358 13:26:18,000 --> 13:26:22,480 in this rectangle but yes pictured here 21359 13:26:20,320 --> 13:26:25,040 in this little square is a pointer to 21360 13:26:22,480 --> 13:26:27,000 the next node in the list so by storing 21361 13:26:25,040 --> 13:26:28,956 name and number maybe her address maybe 21362 13:26:27,000 --> 13:26:31,640 her mailing address whatever in addition 21363 13:26:28,956 --> 13:26:34,160 to a pointer allows each of these nodes 21364 13:26:31,640 --> 13:26:36,240 to be connectable just like the nodes in 21365 13:26:34,160 --> 13:26:38,720 a linked list but where they're starting 21366 13:26:36,240 --> 13:26:42,320 is in an array so the array gets us like 21367 13:26:38,720 --> 13:26:44,720 126 of or gets us um Narrows the problem 21368 13:26:42,320 --> 13:26:46,840 from size 26 to one gets us to the link 21369 13:26:44,720 --> 13:26:49,796 list in question hopefully it's a single 21370 13:26:46,840 --> 13:26:52,560 person or perhaps it has more than that 21371 13:26:49,796 --> 13:26:54,756 meanwhile what is the hash table itself 21372 13:26:52,560 --> 13:26:57,000 the hash table the whole thing is 21373 13:26:54,756 --> 13:26:59,956 literally just an array I've hardcoded 21374 13:26:57,000 --> 13:27:02,320 the simplest version of size 26 but what 21375 13:26:59,956 --> 13:27:04,840 do each of those boxes in the vertical 21376 13:27:02,320 --> 13:27:06,436 array represent just a pointer to 21377 13:27:04,840 --> 13:27:07,796 potentially a node a node in a length 21378 13:27:06,436 --> 13:27:10,200 list and if there's no one there if 21379 13:27:07,796 --> 13:27:11,916 there's no one in location y or X or the 21380 13:27:10,200 --> 13:27:14,480 like in that Universe well it's just a 21381 13:27:11,916 --> 13:27:16,276 null pointer signifying there's no uh 21382 13:27:14,480 --> 13:27:18,840 one there but if there is it's going to 21383 13:27:16,276 --> 13:27:21,520 be a pointer to a valid node from which 21384 13:27:18,840 --> 13:27:23,040 we can get to any of the others as well 21385 13:27:21,520 --> 13:27:24,200 and that so-called hash function just 21386 13:27:23,040 --> 13:27:26,880 like the one I did with the greeting 21387 13:27:24,200 --> 13:27:28,520 cards well it's just an a black box if 21388 13:27:26,880 --> 13:27:30,756 you will but implemented somewhere in 21389 13:27:28,520 --> 13:27:34,000 code like in C and so if you pass in 21390 13:27:30,756 --> 13:27:35,596 Albus what is the hash value of Albus 21391 13:27:34,000 --> 13:27:38,520 well in the first version of the story 21392 13:27:35,596 --> 13:27:41,276 with 26 buckets it should be a zero if 21393 13:27:38,520 --> 13:27:43,640 you pass in Zacharias it should be 25 21394 13:27:41,276 --> 13:27:46,680 and so just as my cards were being 21395 13:27:43,640 --> 13:27:48,796 hashed to one of 1 2 3 four values now 21396 13:27:46,680 --> 13:27:52,080 these names are being hashed to one of 21397 13:27:48,796 --> 13:27:54,756 26 possibilities or 26 time 26 or 26 to 21398 13:27:52,080 --> 13:27:56,956 the 3 power if you have more and more 21399 13:27:54,756 --> 13:27:59,916 granularity than 21400 13:27:56,956 --> 13:28:05,160 that questions on this implementation 21401 13:27:59,916 --> 13:28:05,160 now of this idea of a hash 21402 13:28:05,630 --> 13:28:10,280 [Music] 21403 13:28:13,796 --> 13:28:19,520 table if uh if you say that again with 21404 13:28:17,400 --> 13:28:19,520 the 21405 13:28:26,420 --> 13:28:30,400 [Music] 21406 13:28:27,916 --> 13:28:31,796 null oh a good question so if there's so 21407 13:28:30,400 --> 13:28:33,840 much spareness there's all of these 21408 13:28:31,796 --> 13:28:35,640 empty cells in the array couldn't you 21409 13:28:33,840 --> 13:28:37,160 just go in and free them or delete them 21410 13:28:35,640 --> 13:28:41,000 or just kind of shrink the array and not 21411 13:28:37,160 --> 13:28:43,400 have a a AA and aab and a a only have 21412 13:28:41,000 --> 13:28:45,840 the prefixes two or three characters 21413 13:28:43,400 --> 13:28:48,116 that you need you absolutely could do 21414 13:28:45,840 --> 13:28:50,596 that but now what you you lose is the 21415 13:28:48,116 --> 13:28:53,680 arithmetic benefit of being able to map 21416 13:28:50,596 --> 13:28:56,520 each letter to a number if you start uh 21417 13:28:53,680 --> 13:28:58,276 freeing up unused space you don't know 21418 13:28:56,520 --> 13:29:00,400 that Zacharias is necessarily at 21419 13:28:58,276 --> 13:29:02,436 location 25 Albus is still going to be 21420 13:29:00,400 --> 13:29:04,040 at location zero but if you've deleted 21421 13:29:02,436 --> 13:29:05,916 some of the elements in the middle 21422 13:29:04,040 --> 13:29:08,720 Zacharias could be at 24 if you've 21423 13:29:05,916 --> 13:29:10,320 deleted one 23 if you've deleted another 21424 13:29:08,720 --> 13:29:12,240 and so you don't have that arithmetic 21425 13:29:10,320 --> 13:29:14,796 immediacy that you need in order to 21426 13:29:12,240 --> 13:29:16,720 index into the array with constant time 21427 13:29:14,796 --> 13:29:19,160 and the same is going to be true if it's 21428 13:29:16,720 --> 13:29:20,596 uh two letters or three letters you need 21429 13:29:19,160 --> 13:29:22,560 to be able to trust that you can do some 21430 13:29:20,596 --> 13:29:24,756 quick math and jump to the right index 21431 13:29:22,560 --> 13:29:26,756 in constant time and that's again the 21432 13:29:24,756 --> 13:29:29,160 appeal of these arrays so when it comes 21433 13:29:26,756 --> 13:29:31,160 to the running time of a hash table 21434 13:29:29,160 --> 13:29:33,400 inserting values into it searching for 21435 13:29:31,160 --> 13:29:35,796 values therein at the end of the day it 21436 13:29:33,400 --> 13:29:38,200 technically is Big O of n because in the 21437 13:29:35,796 --> 13:29:40,680 craziest case you might have a huge 21438 13:29:38,200 --> 13:29:42,880 fancy hash table but everyone in the 21439 13:29:40,680 --> 13:29:44,756 universe has a name starting with h and 21440 13:29:42,880 --> 13:29:47,080 then it just evolves into a really long 21441 13:29:44,756 --> 13:29:48,756 link list just like a binary search tree 21442 13:29:47,080 --> 13:29:50,640 could do the same but if you choose a 21443 13:29:48,756 --> 13:29:52,080 smarter hash function maybe you mitigate 21444 13:29:50,640 --> 13:29:54,000 that and you don't rely only on the 21445 13:29:52,080 --> 13:29:56,160 first letter but on the second or the 21446 13:29:54,000 --> 13:29:57,840 third as well or some other combination 21447 13:29:56,160 --> 13:30:00,080 of that input and make your hash 21448 13:29:57,840 --> 13:30:02,116 function smarter odds are if you get a 21449 13:30:00,080 --> 13:30:04,680 good hash function you want it get it to 21450 13:30:02,116 --> 13:30:06,840 be more of like order of n divided by K 21451 13:30:04,680 --> 13:30:09,000 where K means constant mathematically 21452 13:30:06,840 --> 13:30:10,560 and so K is the number of buckets so 21453 13:30:09,000 --> 13:30:12,360 ideally you want like a uniform 21454 13:30:10,560 --> 13:30:14,400 distribution you want like this many 21455 13:30:12,360 --> 13:30:16,720 people here this many people here you 21456 13:30:14,400 --> 13:30:18,400 don't want there to be some or no people 21457 13:30:16,720 --> 13:30:20,400 you want a uniform statistical 21458 13:30:18,400 --> 13:30:21,840 distribution and maybe you get that from 21459 13:30:20,400 --> 13:30:24,000 Human names maybe you don't but that's 21460 13:30:21,840 --> 13:30:26,000 kind of the challenge of a hash function 21461 13:30:24,000 --> 13:30:28,040 of course Big O of n over K is not a 21462 13:30:26,000 --> 13:30:30,720 thing because we always throw away 21463 13:30:28,040 --> 13:30:33,360 constants like K so it's still Big O of 21464 13:30:30,720 --> 13:30:35,160 n but again the distinction today is 21465 13:30:33,360 --> 13:30:37,160 that okay yes academically you learned 21466 13:30:35,160 --> 13:30:39,480 in cs50 that sure it's Big O ofin but my 21467 13:30:37,160 --> 13:30:41,720 God it's 26 times faster if you do the 21468 13:30:39,480 --> 13:30:44,200 hash function well and you spread 21469 13:30:41,720 --> 13:30:46,200 everyone out over the hash table and 21470 13:30:44,200 --> 13:30:47,480 that's the appeal of these kinds of 21471 13:30:46,200 --> 13:30:50,880 structures 21472 13:30:47,480 --> 13:30:54,276 and we've got one more for you if I may 21473 13:30:50,880 --> 13:30:56,880 something now known as a try so it turns 21474 13:30:54,276 --> 13:31:00,080 out that a try is even cooler if you 21475 13:30:56,880 --> 13:31:03,916 like this kind of thing in that it does 21476 13:31:00,080 --> 13:31:05,916 not devolve into Big O of n it is truly 21477 13:31:03,916 --> 13:31:08,756 constant time but there's going to be a 21478 13:31:05,916 --> 13:31:12,160 price there's going to be a gotcha a try 21479 13:31:08,756 --> 13:31:14,560 is sort of a fancier tree and it's short 21480 13:31:12,160 --> 13:31:17,116 for retrieval but pronounced try for 21481 13:31:14,560 --> 13:31:21,000 weird historical reasons but a try is a 21482 13:31:17,116 --> 13:31:22,756 tree each of whose nodes is an array 21483 13:31:21,000 --> 13:31:24,080 right so this is all like crazy mashups 21484 13:31:22,756 --> 13:31:25,360 now people started inventing data 21485 13:31:24,080 --> 13:31:26,680 structures just by combining different 21486 13:31:25,360 --> 13:31:30,480 ones unfortunately a lot of the good 21487 13:31:26,680 --> 13:31:32,276 ideas are taken but you just have 21488 13:31:30,480 --> 13:31:33,520 benefits from certain aspects of those 21489 13:31:32,276 --> 13:31:34,880 data structures and combining them 21490 13:31:33,520 --> 13:31:36,880 ideally gives you the best of both 21491 13:31:34,880 --> 13:31:39,160 worlds so to speak so here might be the 21492 13:31:36,880 --> 13:31:41,116 root of a try it's literally a big node 21493 13:31:39,160 --> 13:31:43,520 a big rectangle but it's actually an 21494 13:31:41,116 --> 13:31:45,796 array so there's like 26 locations in 21495 13:31:43,520 --> 13:31:48,520 this picture here and here's how you use 21496 13:31:45,796 --> 13:31:51,640 a try for instance to store names just 21497 13:31:48,520 --> 13:31:54,000 like the hash table it you treat each of 21498 13:31:51,640 --> 13:31:55,520 the elements of that array in that node 21499 13:31:54,000 --> 13:31:58,200 as like a letter of the alphabet so a 21500 13:31:55,520 --> 13:32:00,796 through z or 0 through 25 and if you 21501 13:31:58,200 --> 13:32:03,040 want to store someone's name in here you 21502 13:32:00,796 --> 13:32:06,480 do so as follows if you want to store 21503 13:32:03,040 --> 13:32:08,400 like uh an H you index into the H 21504 13:32:06,480 --> 13:32:10,480 location and if you want to store the 21505 13:32:08,400 --> 13:32:13,640 second letter of someone's name like an 21506 13:32:10,480 --> 13:32:15,680 a well you add another node below it and 21507 13:32:13,640 --> 13:32:18,436 such one is connected to the other and 21508 13:32:15,680 --> 13:32:20,720 you then identify the a in that array 21509 13:32:18,436 --> 13:32:23,240 and then you go on and maybe put a g if 21510 13:32:20,720 --> 13:32:26,240 the goal is to store spoiler now Hagrid 21511 13:32:23,240 --> 13:32:28,360 in this data structure and then the r 21512 13:32:26,240 --> 13:32:30,160 and the I and then the D but when you 21513 13:32:28,360 --> 13:32:32,000 get to the D the end of the name you 21514 13:32:30,160 --> 13:32:34,640 have to somehow flag that this is the 21515 13:32:32,000 --> 13:32:36,400 end of a name that we've embedded into 21516 13:32:34,640 --> 13:32:38,000 this data structure so whereas all of 21517 13:32:36,400 --> 13:32:39,680 these are called out in white just to 21518 13:32:38,000 --> 13:32:42,040 make obvious what we're connecting to 21519 13:32:39,680 --> 13:32:44,116 what green has to be like a bull that's 21520 13:32:42,040 --> 13:32:46,756 true that just indicates like the buck 21521 13:32:44,116 --> 13:32:49,596 stops here like d is the last letter in 21522 13:32:46,756 --> 13:32:52,360 someone's actual name and what's kind of 21523 13:32:49,596 --> 13:32:54,436 cool now about a try is that we can 21524 13:32:52,360 --> 13:32:56,520 repeat this for other names as well so 21525 13:32:54,436 --> 13:32:58,956 for instance here is where we might put 21526 13:32:56,520 --> 13:33:03,480 Harry as well and notice they share a 21527 13:32:58,956 --> 13:33:05,596 common prefix ha for Hagrid ha for Harry 21528 13:33:03,480 --> 13:33:07,360 so we're reusing some of these nodes 21529 13:33:05,596 --> 13:33:09,480 some of these arrays we can even slip 21530 13:33:07,360 --> 13:33:13,320 Hermione in here too borrowing only the 21531 13:33:09,480 --> 13:33:16,400 H but she gets the H then the E then r r 21532 13:33:13,320 --> 13:33:18,796 m i o n e and so forth and we Mark at 21533 13:33:16,400 --> 13:33:22,000 the end of her name too that she's in 21534 13:33:18,796 --> 13:33:25,276 there now what's the takeaway here well 21535 13:33:22,000 --> 13:33:28,080 what is the running time of a try how 21536 13:33:25,276 --> 13:33:29,560 many steps does it take to find someone 21537 13:33:28,080 --> 13:33:31,360 in this data structure and let me zoom 21538 13:33:29,560 --> 13:33:33,400 out so that it sort of suddenly becomes 21539 13:33:31,360 --> 13:33:36,596 a massive data structure with even more 21540 13:33:33,400 --> 13:33:37,720 in it uh maybe it looks sorry no I'll 21541 13:33:36,596 --> 13:33:38,880 keep it on this one maybe it looks a 21542 13:33:37,720 --> 13:33:40,880 little something like this with just 21543 13:33:38,880 --> 13:33:44,880 these three names but how many steps 21544 13:33:40,880 --> 13:33:47,360 does it take to find Hagrid or Harry or 21545 13:33:44,880 --> 13:33:49,240 Hermione no matter how many names are in 21546 13:33:47,360 --> 13:33:54,400 this data structure there's three at the 21547 13:33:49,240 --> 13:33:58,480 moment but it takes what h a g r i d so 21548 13:33:54,400 --> 13:34:02,480 six steps to find Hagrid uh h a r r y 21549 13:33:58,480 --> 13:34:05,880 five steps to find Harry h e r m i o n e 21550 13:34:02,480 --> 13:34:08,160 eight steps to find Hermione but notice 21551 13:34:05,880 --> 13:34:10,480 that those steps are only dependent on 21552 13:34:08,160 --> 13:34:12,436 what the lengths of the human's names 21553 13:34:10,480 --> 13:34:14,520 and let's assume that no one's going to 21554 13:34:12,436 --> 13:34:16,400 have a infinitely long name it's going 21555 13:34:14,520 --> 13:34:18,520 to max out at what like eight no maybe 21556 13:34:16,400 --> 13:34:20,640 eight 18 maybe 20 30 there's actually 21557 13:34:18,520 --> 13:34:22,596 some pretty long human names out there 21558 13:34:20,640 --> 13:34:26,080 but it's going to be finite you know 21559 13:34:22,596 --> 13:34:28,720 it's abounded whereas most contexts n 21560 13:34:26,080 --> 13:34:30,360 could grow forever so what's compelling 21561 13:34:28,720 --> 13:34:32,276 here is if you assume that the longest 21562 13:34:30,360 --> 13:34:34,956 name is I don't know 50 for the sake of 21563 13:34:32,276 --> 13:34:36,560 a theme here then you know that finding 21564 13:34:34,956 --> 13:34:39,400 anyone in this data structure will take 21565 13:34:36,560 --> 13:34:41,840 you no more than 50 steps 50 is thus a 21566 13:34:39,400 --> 13:34:44,400 constant which means you have big O of 21567 13:34:41,840 --> 13:34:46,080 one running time it doesn't matter if 21568 13:34:44,400 --> 13:34:47,880 there's a million people in this phone 21569 13:34:46,080 --> 13:34:49,360 book or a billion people in this phone 21570 13:34:47,880 --> 13:34:50,916 book that's going to definitely add more 21571 13:34:49,360 --> 13:34:55,276 nodes to it but it's still going to take 21572 13:34:50,916 --> 13:34:58,040 you h a r uh sorry h a g r i d six steps 21573 13:34:55,276 --> 13:35:00,520 to find Hagrid h a r r y five steps to 21574 13:34:58,040 --> 13:35:03,360 find Harry even if there's a billion 21575 13:35:00,520 --> 13:35:05,916 other people in that data structure so 21576 13:35:03,360 --> 13:35:07,240 now we actually do seem to have constant 21577 13:35:05,916 --> 13:35:09,520 time if you assume that there's going to 21578 13:35:07,240 --> 13:35:12,320 be a bound on the length of the name why 21579 13:35:09,520 --> 13:35:13,796 don't we use tries for everything then 21580 13:35:12,320 --> 13:35:15,756 what's the price we're paying for this 21581 13:35:13,796 --> 13:35:19,160 data structure even though we've 21582 13:35:15,756 --> 13:35:22,160 represented just three characters here 21583 13:35:19,160 --> 13:35:24,116 yeah it's a lot of memory yeah and you 21584 13:35:22,160 --> 13:35:26,160 can see it even with these three names 21585 13:35:24,116 --> 13:35:28,640 most of the squares on the screen are 21586 13:35:26,160 --> 13:35:30,400 empty like bites and bits that are there 21587 13:35:28,640 --> 13:35:31,560 and are allocated and they need to be 21588 13:35:30,400 --> 13:35:33,756 there because you need to be able to do 21589 13:35:31,560 --> 13:35:35,560 that arithmetic thing of this being zero 21590 13:35:33,756 --> 13:35:37,000 this being 25 so you can jump from boom 21591 13:35:35,560 --> 13:35:39,840 boom boom boom based on each of the 21592 13:35:37,000 --> 13:35:41,596 letters but it's a hugely sparse data 21593 13:35:39,840 --> 13:35:44,000 structure which means it takes up a 21594 13:35:41,596 --> 13:35:46,520 crazy amount of memory now maybe that's 21595 13:35:44,000 --> 13:35:47,756 tolerable especially for short names but 21596 13:35:46,520 --> 13:35:49,680 that's going to be the trade-off as well 21597 13:35:47,756 --> 13:35:52,596 and this is such a tension in Computing 21598 13:35:49,680 --> 13:35:54,596 almost any time you want to improve time 21599 13:35:52,596 --> 13:35:56,160 you want to speed up the efficiency the 21600 13:35:54,596 --> 13:35:58,916 speed of your algorithm you're going to 21601 13:35:56,160 --> 13:36:00,956 spend space if by contrast you want to 21602 13:35:58,916 --> 13:36:03,000 decrease the amount of space you might 21603 13:36:00,956 --> 13:36:05,116 very well have to increase the running 21604 13:36:03,000 --> 13:36:07,116 time it is indeed this seesaw back and 21605 13:36:05,116 --> 13:36:09,400 forth and you your colleagues your 21606 13:36:07,116 --> 13:36:11,880 company need to decide what resource is 21607 13:36:09,400 --> 13:36:13,320 the most precious heck it might be much 21608 13:36:11,880 --> 13:36:15,080 harder to code one of these data 21609 13:36:13,320 --> 13:36:16,720 structures than another you're a human 21610 13:36:15,080 --> 13:36:19,040 your time is valuable do you really want 21611 13:36:16,720 --> 13:36:20,520 to spend hours implementing a try when 21612 13:36:19,040 --> 13:36:22,480 you know hey in 30 minutes I can bang 21613 13:36:20,520 --> 13:36:24,436 out an array nowadays or a linked list 21614 13:36:22,480 --> 13:36:26,116 even there too development time is going 21615 13:36:24,436 --> 13:36:27,840 to be yet another resource and why 21616 13:36:26,116 --> 13:36:30,240 sometimes there's good code or bad code 21617 13:36:27,840 --> 13:36:31,916 it depends on what you're prioritizing 21618 13:36:30,240 --> 13:36:34,160 so what do each of these nodes look like 21619 13:36:31,916 --> 13:36:35,916 in a try well we can keep calling it a 21620 13:36:34,160 --> 13:36:37,200 node this is a very generic term for 21621 13:36:35,916 --> 13:36:40,000 just a container in these data 21622 13:36:37,200 --> 13:36:43,000 structures in this story though let me 21623 13:36:40,000 --> 13:36:46,400 claim that everyone has a number like a 21624 13:36:43,000 --> 13:36:49,116 phone number a string akhr star every 21625 13:36:46,400 --> 13:36:52,000 every node has 26 children or 21626 13:36:49,116 --> 13:36:54,436 technically an array of size 26 that can 21627 13:36:52,000 --> 13:36:57,040 point to more of these nodes notice that 21628 13:36:54,436 --> 13:36:59,400 I don't need to store the name of 21629 13:36:57,040 --> 13:37:01,360 someone in a try because it's implicit 21630 13:36:59,400 --> 13:37:03,040 in the path that you take to find them 21631 13:37:01,360 --> 13:37:05,480 so that's a minor optimization but it 21632 13:37:03,040 --> 13:37:07,276 saves us some space but this would be 21633 13:37:05,480 --> 13:37:10,640 just a different data structure we could 21634 13:37:07,276 --> 13:37:12,840 use to actually solve this problem as 21635 13:37:10,640 --> 13:37:14,436 well albeit at a very expensive cost and 21636 13:37:12,840 --> 13:37:16,880 what do we need our variable to be that 21637 13:37:14,436 --> 13:37:19,080 stores the try just like four we just 21638 13:37:16,880 --> 13:37:21,480 need a single pointer that hangs on to 21639 13:37:19,080 --> 13:37:23,680 the root of this structure that's null 21640 13:37:21,480 --> 13:37:25,160 if it's empty or non-null if it's 21641 13:37:23,680 --> 13:37:28,480 actually pointing at 21642 13:37:25,160 --> 13:37:30,756 something any questions then on 21643 13:37:28,480 --> 13:37:32,720 tries and if it's feeling like a lot the 21644 13:37:30,756 --> 13:37:35,680 fire the the fire hydrant it is we 21645 13:37:32,720 --> 13:37:38,200 started with arrays then link list then 21646 13:37:35,680 --> 13:37:39,400 tries but questions on how we've just 21647 13:37:38,200 --> 13:37:41,020 assembled from these basic building 21648 13:37:39,400 --> 13:37:43,680 blocks 21649 13:37:41,020 --> 13:37:46,560 [Music] 21650 13:37:43,680 --> 13:37:50,240 yeah a good question why is this not uh 21651 13:37:46,560 --> 13:37:52,320 size 26 uh it's just like with the try 21652 13:37:50,240 --> 13:37:54,880 just like with the link list before it 21653 13:37:52,320 --> 13:37:56,520 just tends to be en code convenient to 21654 13:37:54,880 --> 13:37:58,200 have a separate additional pointer 21655 13:37:56,520 --> 13:37:59,680 that's small that just points to the 21656 13:37:58,200 --> 13:38:01,596 beginning of the data structure because 21657 13:37:59,680 --> 13:38:03,520 that way it can be null thereby clearly 21658 13:38:01,596 --> 13:38:05,640 indicating there are no nodes the whole 21659 13:38:03,520 --> 13:38:08,596 structure is empty if you allocated one 21660 13:38:05,640 --> 13:38:10,160 of those nodes you absolutely could but 21661 13:38:08,596 --> 13:38:12,080 then you'd be just wasting space even if 21662 13:38:10,160 --> 13:38:13,880 it's empty and it creates an ambiguity 21663 13:38:12,080 --> 13:38:15,840 so just having a single pointer linked 21664 13:38:13,880 --> 13:38:18,916 to the beginnings of all of these things 21665 13:38:15,840 --> 13:38:25,240 is a good thing other questions now on 21666 13:38:18,916 --> 13:38:28,840 tries or trees or hash tables or 21667 13:38:25,240 --> 13:38:31,840 arrays so what problems might arise well 21668 13:38:28,840 --> 13:38:35,276 here's a counter example what names are 21669 13:38:31,840 --> 13:38:35,276 manifest in this try 21670 13:38:35,480 --> 13:38:39,480 here feel free to just call it 21671 13:38:40,520 --> 13:38:47,080 out what do you 21672 13:38:42,596 --> 13:38:49,520 see Danielle and Danielle so presumably 21673 13:38:47,080 --> 13:38:51,276 if these are two uh names here one of 21674 13:38:49,520 --> 13:38:53,040 which is a prefix of another notice that 21675 13:38:51,276 --> 13:38:54,720 the data structure still works and I 21676 13:38:53,040 --> 13:38:56,116 chose you know a friend's name and then 21677 13:38:54,720 --> 13:38:57,680 appended a couple of more characters to 21678 13:38:56,116 --> 13:39:01,116 it that's also a name because we have 21679 13:38:57,680 --> 13:39:02,840 here d a n i e l and the green 21680 13:39:01,116 --> 13:39:04,200 technically is implemented as a bull or 21681 13:39:02,840 --> 13:39:05,756 something like that that indicates a 21682 13:39:04,200 --> 13:39:08,320 word stops here but we don't want to 21683 13:39:05,756 --> 13:39:10,956 preclude storing Danielle as well who's 21684 13:39:08,320 --> 13:39:12,680 a super string if you will of Daniel and 21685 13:39:10,956 --> 13:39:14,520 so that's okay too so long as the 21686 13:39:12,680 --> 13:39:17,080 structure allows for the pointers to 21687 13:39:14,520 --> 13:39:19,320 keep going so even that works out okay 21688 13:39:17,080 --> 13:39:20,916 whereas it might not have otherwise and 21689 13:39:19,320 --> 13:39:23,720 in terms of the running time just to be 21690 13:39:20,916 --> 13:39:27,160 clear at the end of the day tries do 21691 13:39:23,720 --> 13:39:29,320 give you actual constant time for 21692 13:39:27,160 --> 13:39:31,400 insertions lookups deletions and the 21693 13:39:29,320 --> 13:39:34,360 like because it's dependent only on the 21694 13:39:31,400 --> 13:39:36,360 length of the input the key if you will 21695 13:39:34,360 --> 13:39:39,680 and not on how many other people are in 21696 13:39:36,360 --> 13:39:41,956 your phone or a dress book and now 21697 13:39:39,680 --> 13:39:43,916 thought we'd conclude with a visual if 21698 13:39:41,956 --> 13:39:47,080 you've gotten out into the square anyone 21699 13:39:43,916 --> 13:39:49,240 recognize this okay okay sweet green a 21700 13:39:47,080 --> 13:39:51,596 local salad place what are we looking at 21701 13:39:49,240 --> 13:39:52,880 here and what's its connection to today 21702 13:39:51,596 --> 13:39:54,320 um you're about to become all the 21703 13:39:52,880 --> 13:39:56,720 geekier in the real world cuz you will 21704 13:39:54,320 --> 13:39:59,560 start to see data structures 21705 13:39:56,720 --> 13:40:03,000 everywhere what is this or how does this 21706 13:39:59,560 --> 13:40:03,880 work maybe in salad form who's been to 21707 13:40:03,000 --> 13:40:06,880 Sweet 21708 13:40:03,880 --> 13:40:08,110 green okay either of you so how does 21709 13:40:06,880 --> 13:40:11,169 this 21710 13:40:08,110 --> 13:40:11,169 [Music] 21711 13:40:15,040 --> 13:40:19,756 work okay good so if you order a salad 21712 13:40:17,720 --> 13:40:22,756 for someone named L when it's ready they 21713 13:40:19,756 --> 13:40:24,720 put it in the l section here and so this 21714 13:40:22,756 --> 13:40:26,200 is kind of a set of key value pairs 21715 13:40:24,720 --> 13:40:28,080 right if L is the first letter of 21716 13:40:26,200 --> 13:40:30,080 someone's name the value hopefully is 21717 13:40:28,080 --> 13:40:32,080 the salad and so what you kind of have 21718 13:40:30,080 --> 13:40:33,796 here is a dictionary key value pairs 21719 13:40:32,080 --> 13:40:36,040 where it's not words and definitions 21720 13:40:33,796 --> 13:40:38,240 it's names and salads and you can think 21721 13:40:36,040 --> 13:40:40,160 of this too as kind of a hash table why 21722 13:40:38,240 --> 13:40:42,200 even though it actually doesn't fit on 21723 13:40:40,160 --> 13:40:43,916 one long shelf cu the store is only so 21724 13:40:42,200 --> 13:40:45,840 big this is really an array and 21725 13:40:43,916 --> 13:40:47,880 apparently a is missing or maybe it's 21726 13:40:45,840 --> 13:40:49,520 around around the corner but this array 21727 13:40:47,880 --> 13:40:51,200 just happens to wrap onto multiple lines 21728 13:40:49,520 --> 13:40:53,560 but it's still conceptually a single 21729 13:40:51,200 --> 13:40:56,680 Dimension but suppose two people have 21730 13:40:53,560 --> 13:40:56,680 the name L what do they do 21731 13:40:58,680 --> 13:41:03,040 typically yeah so maybe they they well 21732 13:41:01,480 --> 13:41:04,240 if they run put that much effort into it 21733 13:41:03,040 --> 13:41:05,720 they might look at the second letter and 21734 13:41:04,240 --> 13:41:07,480 then the third letter odds are this is 21735 13:41:05,720 --> 13:41:09,160 not that interesting a problem to solve 21736 13:41:07,480 --> 13:41:11,796 optimally in that way but they probably 21737 13:41:09,160 --> 13:41:13,400 do start stacking the salads on top of 21738 13:41:11,796 --> 13:41:15,040 each other maybe scooching it over just 21739 13:41:13,400 --> 13:41:16,840 a little bit and so what do you have 21740 13:41:15,040 --> 13:41:19,160 there well now you start to view the 21741 13:41:16,840 --> 13:41:20,480 lens through like cs50 glasses like okay 21742 13:41:19,160 --> 13:41:21,596 you have an array and then you have like 21743 13:41:20,480 --> 13:41:23,080 these link lists that are sort of 21744 13:41:21,596 --> 13:41:24,680 growing here but even then you run into 21745 13:41:23,080 --> 13:41:26,000 a problem why because it's not really a 21746 13:41:24,680 --> 13:41:28,080 link list because at some point you're 21747 13:41:26,000 --> 13:41:29,916 going to hit the boundary here so it's 21748 13:41:28,080 --> 13:41:31,560 kind of like an array of arrays because 21749 13:41:29,916 --> 13:41:33,880 you can only fit what like three or four 21750 13:41:31,560 --> 13:41:35,596 salads here and so long story short we 21751 13:41:33,880 --> 13:41:37,756 started today deliberately talking about 21752 13:41:35,596 --> 13:41:39,596 real world things like stacks and cues 21753 13:41:37,756 --> 13:41:41,756 and even though it did escalate quickly 21754 13:41:39,596 --> 13:41:43,956 into binary search trees and hash tables 21755 13:41:41,756 --> 13:41:45,560 and tries even those things are 21756 13:41:43,956 --> 13:41:47,400 everywhere even though they don't call 21757 13:41:45,560 --> 13:41:49,320 them as such these are just solutions to 21758 13:41:47,400 --> 13:41:51,276 problems and now with this final week of 21759 13:41:49,320 --> 13:41:53,160 SE under your belt you have all the more 21760 13:41:51,276 --> 13:41:55,400 of a technical toolkit via which to 21761 13:41:53,160 --> 13:41:57,276 implement these things and code next 21762 13:41:55,400 --> 13:41:58,640 week we'll be able to trust that someone 21763 13:41:57,276 --> 13:42:00,756 else solved all these problems we'll 21764 13:41:58,640 --> 13:42:02,916 introduce Python and lines of code like 21765 13:42:00,756 --> 13:42:04,400 this will finally become lines of code 21766 13:42:02,916 --> 13:42:06,160 like that so that's the promise ahead 21767 13:42:04,400 --> 13:42:08,820 and we'll see you next 21768 13:42:06,160 --> 13:42:12,690 [Applause] 21769 13:42:08,820 --> 13:42:12,690 [Music] 21770 13:42:14,880 --> 13:42:17,880 time 21771 13:42:22,920 --> 13:42:31,250 [Music] 21772 13:42:51,140 --> 13:42:54,309 [Music] 21773 13:42:56,360 --> 13:43:00,110 [Music] 21774 13:43:14,796 --> 13:43:17,796 yeah 21775 13:43:31,350 --> 13:43:38,220 [Applause] 21776 13:43:31,580 --> 13:43:38,220 [Music] 21777 13:43:38,290 --> 13:43:42,120 [Applause] 21778 13:43:44,756 --> 13:43:47,756 yeah 21779 13:43:51,540 --> 13:43:56,970 [Music] 21780 13:43:57,400 --> 13:44:03,520 all right this is cs50 and this is week 21781 13:44:00,756 --> 13:44:06,240 six wherein we finally transitioned from 21782 13:44:03,520 --> 13:44:08,756 scratch to see to now Python and indeed 21783 13:44:06,240 --> 13:44:11,720 this is going to be somewhat of a unique 21784 13:44:08,756 --> 13:44:14,400 experience in that just like a few weeks 21785 13:44:11,720 --> 13:44:16,160 past perhaps for the first time and now 21786 13:44:14,400 --> 13:44:17,520 uh today you're going to learn a new 21787 13:44:16,160 --> 13:44:20,160 language but the goal isn't just to 21788 13:44:17,520 --> 13:44:22,360 throw another fire hose of content and 21789 13:44:20,160 --> 13:44:24,200 syntax and whatnot at you but rather to 21790 13:44:22,360 --> 13:44:26,040 really equip you all to actually teach 21791 13:44:24,200 --> 13:44:27,560 yourself new languages in the future and 21792 13:44:26,040 --> 13:44:29,436 so indeed what we'll do today what we'll 21793 13:44:27,560 --> 13:44:31,916 do this coming week is sort of prepare 21794 13:44:29,436 --> 13:44:33,640 you to stand on your own and once python 21795 13:44:31,916 --> 13:44:35,000 is p a and the world has moved on to 21796 13:44:33,640 --> 13:44:36,916 some other language in some number of 21797 13:44:35,000 --> 13:44:38,276 years you'll be well equipped to figure 21798 13:44:36,916 --> 13:44:40,040 out how to wrap your mind around some 21799 13:44:38,276 --> 13:44:41,956 new syntax some new language and solve 21800 13:44:40,040 --> 13:44:44,040 problems as well now you recall in week 21801 13:44:41,956 --> 13:44:46,320 zero this is where we started just 21802 13:44:44,040 --> 13:44:48,640 saying hello to the world and that 21803 13:44:46,320 --> 13:44:50,480 quickly escalated just a week later in C 21804 13:44:48,640 --> 13:44:52,480 to see be something much much more 21805 13:44:50,480 --> 13:44:54,200 cryptic and if you've still sort of 21806 13:44:52,480 --> 13:44:55,520 struggled with some of the syntax find 21807 13:44:54,200 --> 13:44:57,640 yourself checking your notes or your 21808 13:44:55,520 --> 13:44:59,360 previous code like that's totally normal 21809 13:44:57,640 --> 13:45:02,200 and that's one of the reasons why there 21810 13:44:59,360 --> 13:45:04,080 are languages besides C out there among 21811 13:45:02,200 --> 13:45:06,240 them this language called python humans 21812 13:45:04,080 --> 13:45:07,916 over the decades have realized gee that 21813 13:45:06,240 --> 13:45:09,840 wasn't necessarily the best designed 21814 13:45:07,916 --> 13:45:11,680 decision or humans have realized wow you 21815 13:45:09,840 --> 13:45:14,360 know what now that computers have gotten 21816 13:45:11,680 --> 13:45:16,400 faster with more memory and more faster 21817 13:45:14,360 --> 13:45:18,956 CPUs we can actually do more with our 21818 13:45:16,400 --> 13:45:20,680 programming languages so just as human 21819 13:45:18,956 --> 13:45:22,436 languages evolve so do actual 21820 13:45:20,680 --> 13:45:24,240 programming languages and even within a 21821 13:45:22,436 --> 13:45:26,200 programming language there's typically 21822 13:45:24,240 --> 13:45:29,320 different versions We for instance have 21823 13:45:26,200 --> 13:45:32,116 been using version C11 of C which was 21824 13:45:29,320 --> 13:45:33,880 updated in 2011 um but python itself 21825 13:45:32,116 --> 13:45:36,320 continues to evolve and it's now up to 21826 13:45:33,880 --> 13:45:37,756 version three plus and so there too 21827 13:45:36,320 --> 13:45:40,116 these things will evolve in the coming 21828 13:45:37,756 --> 13:45:42,400 days thankfully what you're about to see 21829 13:45:40,116 --> 13:45:44,240 is hello world for the third time but 21830 13:45:42,400 --> 13:45:47,240 it's going to be literally this none of 21831 13:45:44,240 --> 13:45:50,436 the crazy syntax above or below fewer 21832 13:45:47,240 --> 13:45:52,200 semicolons if any fewer curly braces and 21833 13:45:50,436 --> 13:45:54,640 really a lot of the distractions get out 21834 13:45:52,200 --> 13:45:56,520 of the way so to get there let's 21835 13:45:54,640 --> 13:45:58,596 consider exactly how we've been 21836 13:45:56,520 --> 13:46:00,956 programming up until now so you write a 21837 13:45:58,596 --> 13:46:02,956 program in C and you've got hopefully no 21838 13:46:00,956 --> 13:46:05,000 syntax error so you're ready to build it 21839 13:46:02,956 --> 13:46:06,640 that is compil it and so you've run make 21840 13:46:05,000 --> 13:46:09,560 and then you've run the program like/ 21841 13:46:06,640 --> 13:46:11,040 hello or if you think back to week two 21842 13:46:09,560 --> 13:46:12,400 where we took a peak underneath the hood 21843 13:46:11,040 --> 13:46:14,596 of what make is doing it's really 21844 13:46:12,400 --> 13:46:16,276 running the actual compiler something 21845 13:46:14,596 --> 13:46:18,160 called clang maybe with some command 21846 13:46:16,276 --> 13:46:21,320 line arguments creating a program called 21847 13:46:18,160 --> 13:46:22,596 hello and then you could do do/ hello so 21848 13:46:21,320 --> 13:46:24,240 today you're going to start doing 21849 13:46:22,596 --> 13:46:26,596 something similar in spirit but fewer 21850 13:46:24,240 --> 13:46:28,200 steps no longer will you have to compile 21851 13:46:26,596 --> 13:46:30,040 your code and then run it and then maybe 21852 13:46:28,200 --> 13:46:32,240 fix or change it and then compile your 21853 13:46:30,040 --> 13:46:34,520 code and run it and then repeat repeat 21854 13:46:32,240 --> 13:46:36,796 the process of running your code is 21855 13:46:34,520 --> 13:46:38,520 going to be distilled into just a single 21856 13:46:36,796 --> 13:46:40,956 step and the way to think of this for 21857 13:46:38,520 --> 13:46:43,320 now is that where a c is frequently used 21858 13:46:40,956 --> 13:46:45,520 as indeed a compiled language whereby 21859 13:46:43,320 --> 13:46:46,796 you convert it first to zeros and ones 21860 13:46:45,520 --> 13:46:48,916 Python's going to let you speed things 21861 13:46:46,796 --> 13:46:50,640 up whereby you the human programmer 21862 13:46:48,916 --> 13:46:51,916 don't have to compile it you're just 21863 13:46:50,640 --> 13:46:54,680 going to run what's called an 21864 13:46:51,916 --> 13:46:56,796 interpreter which by Design is named the 21865 13:46:54,680 --> 13:46:59,040 exact same thing as the language itself 21866 13:46:56,796 --> 13:47:00,956 and by running this program installed in 21867 13:46:59,040 --> 13:47:02,880 VSS code or eventually on your own Max 21868 13:47:00,956 --> 13:47:05,200 or PCS this is just going to tell your 21869 13:47:02,880 --> 13:47:07,200 computer to interpret this code and 21870 13:47:05,200 --> 13:47:08,680 figure out how to get down to that lower 21871 13:47:07,200 --> 13:47:10,680 level of zeros and ones but you don't 21872 13:47:08,680 --> 13:47:14,320 have to compile the code yourself 21873 13:47:10,680 --> 13:47:15,756 anymore so with that said let's consider 21874 13:47:14,320 --> 13:47:17,560 what the code is going to look like side 21875 13:47:15,756 --> 13:47:19,276 by side in fact let's look back at some 21876 13:47:17,560 --> 13:47:21,400 scratch blocks just like we did with C 21877 13:47:19,276 --> 13:47:22,796 in week one and do some side by sides 21878 13:47:21,400 --> 13:47:23,956 because even though some of the syntax 21879 13:47:22,796 --> 13:47:26,360 this week and Beyond's going to be 21880 13:47:23,956 --> 13:47:28,400 different like the ideas are truly going 21881 13:47:26,360 --> 13:47:30,840 to be the same there's not all that much 21882 13:47:28,400 --> 13:47:32,560 intellectually new just yet so whereas 21883 13:47:30,840 --> 13:47:34,400 in week zero we might have said hello to 21884 13:47:32,560 --> 13:47:36,720 the world with this purple puzzle piece 21885 13:47:34,400 --> 13:47:40,200 today of course uh or rather in week one 21886 13:47:36,720 --> 13:47:41,596 it looked like this in C but today 21887 13:47:40,200 --> 13:47:43,796 moving forward it's going to quite 21888 13:47:41,596 --> 13:47:45,880 simply look like this instead and if we 21889 13:47:43,796 --> 13:47:48,276 go back and forth for just a moment here 21890 13:47:45,880 --> 13:47:50,596 again is the version in C noticing the 21891 13:47:48,276 --> 13:47:53,040 very seike characteristics and just at a 21892 13:47:50,596 --> 13:47:55,320 glance here in Python I claim it's now 21893 13:47:53,040 --> 13:47:56,840 this what do you apparently need not 21894 13:47:55,320 --> 13:48:00,680 worry about 21895 13:47:56,840 --> 13:48:01,956 anymore what's gone so semicolon is gone 21896 13:48:00,680 --> 13:48:03,720 and indeed you don't need those to 21897 13:48:01,956 --> 13:48:07,080 finish most of your thoughts anymore 21898 13:48:03,720 --> 13:48:08,436 anything else so the back sln is absent 21899 13:48:07,080 --> 13:48:10,240 and that's kind of curious because we're 21900 13:48:08,436 --> 13:48:11,916 still going to get a new line but we'll 21901 13:48:10,240 --> 13:48:13,360 see that it's become the default and 21902 13:48:11,916 --> 13:48:15,436 this one's a little more subtle but now 21903 13:48:13,360 --> 13:48:17,560 the function is called print instead of 21904 13:48:15,436 --> 13:48:20,160 print F so it's a little more familiar 21905 13:48:17,560 --> 13:48:22,596 in that sense all right so when it comes 21906 13:48:20,160 --> 13:48:24,200 to using libraries that is code that 21907 13:48:22,596 --> 13:48:25,916 other people have written in the past 21908 13:48:24,200 --> 13:48:28,720 we've done things like hash include 21909 13:48:25,916 --> 13:48:31,116 cs50.h to use cs50's own header file or 21910 13:48:28,720 --> 13:48:32,880 standard IO or standard lib or string or 21911 13:48:31,116 --> 13:48:34,720 any number of other header files you 21912 13:48:32,880 --> 13:48:36,360 have all used well moving forward we're 21913 13:48:34,720 --> 13:48:38,680 going to give you for this first week a 21914 13:48:36,360 --> 13:48:40,436 similar cs-50 Library just very 21915 13:48:38,680 --> 13:48:42,200 short-term uh training wheels that will 21916 13:48:40,436 --> 13:48:44,436 quickly take off because in reality it's 21917 13:48:42,200 --> 13:48:46,276 a lot easier to do things in python as 21918 13:48:44,436 --> 13:48:48,956 we'll see but the Syntax for this now is 21919 13:48:46,276 --> 13:48:51,756 going to be to import the cs50 library 21920 13:48:48,956 --> 13:48:53,560 in this way and when we have now this 21921 13:48:51,756 --> 13:48:54,840 ability we can actually start writing 21922 13:48:53,560 --> 13:48:57,756 some code right away in fact let me 21923 13:48:54,840 --> 13:48:59,400 switch over to vs code here and just as 21924 13:48:57,756 --> 13:49:01,796 in the past I'll create a new file but 21925 13:48:59,400 --> 13:49:02,956 instead of creating something called C 21926 13:49:01,796 --> 13:49:05,840 I'm going to go ahead and create my 21927 13:49:02,956 --> 13:49:08,160 first program called hello.py using Code 21928 13:49:05,840 --> 13:49:09,756 space hello.py that of course gives me 21929 13:49:08,160 --> 13:49:12,276 this new tab and let me actually quite 21930 13:49:09,756 --> 13:49:14,880 simply do what I proposed print quote 21931 13:49:12,276 --> 13:49:17,040 unquote hello world without the back 21932 13:49:14,880 --> 13:49:19,796 slash without the semicolon without the 21933 13:49:17,040 --> 13:49:21,756 F in print and now let me go down to my 21934 13:49:19,796 --> 13:49:24,000 terminal window and I don't have to 21935 13:49:21,756 --> 13:49:26,756 compile it I don't have to do dot slash 21936 13:49:24,000 --> 13:49:28,320 i instead run a program called python 21937 13:49:26,756 --> 13:49:30,276 whose purpose in life is now to 21938 13:49:28,320 --> 13:49:32,756 interpret my code top to bottom left to 21939 13:49:30,276 --> 13:49:35,916 right and if I run python of hello.py 21940 13:49:32,756 --> 13:49:37,880 crossing my fingers as always voila now 21941 13:49:35,916 --> 13:49:39,720 I have printed out hello world so we 21942 13:49:37,880 --> 13:49:41,400 seem to have gotten the new line for 21943 13:49:39,720 --> 13:49:43,200 free in this sense where it's 21944 13:49:41,400 --> 13:49:44,956 automatically happening the dollar sign 21945 13:49:43,200 --> 13:49:47,880 isn't weirdly on the same line like it 21946 13:49:44,956 --> 13:49:50,160 want was in week one but that's just a a 21947 13:49:47,880 --> 13:49:52,360 minor detail here if we switch back to 21948 13:49:50,160 --> 13:49:54,480 now some other capabilities well indeed 21949 13:49:52,360 --> 13:49:56,596 with the cs50 library you can also not 21950 13:49:54,480 --> 13:49:58,436 just import the library itself but 21951 13:49:56,596 --> 13:49:59,756 specific functions and you'll see that 21952 13:49:58,436 --> 13:50:02,240 temporarily we're going to give you a 21953 13:49:59,756 --> 13:50:04,596 helper function called get string just 21954 13:50:02,240 --> 13:50:06,240 like in C that just makes it work 21955 13:50:04,596 --> 13:50:07,400 exactly the same way as in C and we'll 21956 13:50:06,240 --> 13:50:09,520 see a couple of other functions that 21957 13:50:07,400 --> 13:50:10,880 will just make life easier initially but 21958 13:50:09,520 --> 13:50:12,640 quickly will we take those training 21959 13:50:10,880 --> 13:50:15,276 wheels off so that nothing is indeed 21960 13:50:12,640 --> 13:50:17,320 cs50 specific all right well how about 21961 13:50:15,276 --> 13:50:19,596 functions more generally in Python let's 21962 13:50:17,320 --> 13:50:21,796 do a whirlwind tour if you will much 21963 13:50:19,596 --> 13:50:24,360 like we did in that first week of C 21964 13:50:21,796 --> 13:50:26,000 comparing one to the other so back in 21965 13:50:24,360 --> 13:50:27,840 our world of scratch one of the first 21966 13:50:26,000 --> 13:50:30,200 programs we wrote was this one here 21967 13:50:27,840 --> 13:50:32,796 whereby we asked the human their name we 21968 13:50:30,200 --> 13:50:34,956 then used the return value that was sort 21969 13:50:32,796 --> 13:50:38,596 of automatically stored in this answer 21970 13:50:34,956 --> 13:50:40,560 variable as a second argument to join so 21971 13:50:38,596 --> 13:50:43,040 that we could say hello David or hello 21972 13:50:40,560 --> 13:50:45,796 Carter so this was back in week one uh 21973 13:50:43,040 --> 13:50:47,400 week zero in week one we converted it to 21974 13:50:45,796 --> 13:50:49,400 this and here's a perfect example of 21975 13:50:47,400 --> 13:50:50,956 things like escalating quickly and again 21976 13:50:49,400 --> 13:50:52,640 this is why we start in scratch there's 21977 13:50:50,956 --> 13:50:54,560 just so much distraction here to achieve 21978 13:50:52,640 --> 13:50:56,596 the same idea but even today we're going 21979 13:50:54,560 --> 13:51:00,200 to chip away at some of that syntax so 21980 13:50:56,596 --> 13:51:01,720 in C we had to declare the argument as a 21981 13:51:00,200 --> 13:51:03,276 we had to declare the variable as a 21982 13:51:01,720 --> 13:51:05,880 string here we of course had the 21983 13:51:03,276 --> 13:51:08,360 semicolon and more well in Python the 21984 13:51:05,880 --> 13:51:12,000 comparable code now is going to look 21985 13:51:08,360 --> 13:51:14,160 more simply like this so semicolon is 21986 13:51:12,000 --> 13:51:16,560 again gone on both lines for that matter 21987 13:51:14,160 --> 13:51:19,436 so that's good what else appears to have 21988 13:51:16,560 --> 13:51:21,000 changed or disappeared yeah type of 21989 13:51:19,436 --> 13:51:23,320 variable yeah so I didn't have to 21990 13:51:21,000 --> 13:51:25,640 specifically say that answer is now a 21991 13:51:23,320 --> 13:51:27,916 string and indeed python is is 21992 13:51:25,640 --> 13:51:30,320 dynamically typed and in fact it will 21993 13:51:27,916 --> 13:51:33,240 infer from Context exactly what it is 21994 13:51:30,320 --> 13:51:36,916 you are storing in that variable other 21995 13:51:33,240 --> 13:51:36,916 details that seem a little bit 21996 13:51:37,640 --> 13:51:41,360 different little bit different what else 21997 13:51:39,756 --> 13:51:44,276 jumps out at you here I'll go back this 21998 13:51:41,360 --> 13:51:46,116 was the C version and maybe Focus now on 21999 13:51:44,276 --> 13:51:47,880 the second line because we've rather 22000 13:51:46,116 --> 13:51:52,360 exhausted the first here's now the 22001 13:51:47,880 --> 13:51:52,360 python version what's different here 22002 13:51:54,680 --> 13:51:59,560 yeah yeah there's no percent s anymore 22003 13:51:57,560 --> 13:52:02,116 there's no second argument at the moment 22004 13:51:59,560 --> 13:52:03,796 per se to print now it is still a little 22005 13:52:02,116 --> 13:52:05,436 weird it's as though I've like deployed 22006 13:52:03,796 --> 13:52:06,880 some addition here arithmetically but 22007 13:52:05,436 --> 13:52:08,560 that's not the case some of you have 22008 13:52:06,880 --> 13:52:10,840 programmed before and plus some of you 22009 13:52:08,560 --> 13:52:13,080 might know means what in this 22010 13:52:10,840 --> 13:52:14,840 context so to combine or more 22011 13:52:13,080 --> 13:52:15,956 technically anyone know the buzz word 22012 13:52:14,840 --> 13:52:18,160 yeah 22013 13:52:15,956 --> 13:52:19,956 to concatenate so to concatenate is like 22014 13:52:18,160 --> 13:52:21,756 the fancy way of what scratch calls 22015 13:52:19,956 --> 13:52:23,240 joining which is to take one string on 22016 13:52:21,756 --> 13:52:25,680 the left one string on the right and to 22017 13:52:23,240 --> 13:52:28,040 join them together to glue them together 22018 13:52:25,680 --> 13:52:29,596 if you will so this is not addition it 22019 13:52:28,040 --> 13:52:31,116 would be if it were numbers involved 22020 13:52:29,596 --> 13:52:33,080 instead but because we've got a string 22021 13:52:31,116 --> 13:52:34,680 hello comma and another string 22022 13:52:33,080 --> 13:52:36,756 implicitly in this variable based on 22023 13:52:34,680 --> 13:52:38,560 what the human typed in in response to 22024 13:52:36,756 --> 13:52:41,596 this get string function that's going to 22025 13:52:38,560 --> 13:52:43,796 concatenate hello comma space and then 22026 13:52:41,596 --> 13:52:45,436 David or Carter or whatever the human 22027 13:52:43,796 --> 13:52:46,840 has typed in but turns out there's going 22028 13:52:45,436 --> 13:52:48,080 to be different ways to do this in 22029 13:52:46,840 --> 13:52:49,756 Python and we'll show you a few 22030 13:52:48,080 --> 13:52:52,276 different ones and here too try not to 22031 13:52:49,756 --> 13:52:53,680 get too hung up on or frustrated by like 22032 13:52:52,276 --> 13:52:55,040 all of the different ways you can solve 22033 13:52:53,680 --> 13:52:56,956 problems odds are you're going to be 22034 13:52:55,040 --> 13:52:58,640 picking up tips and techniques for years 22035 13:52:56,956 --> 13:53:00,116 to come if you continue programming so 22036 13:52:58,640 --> 13:53:02,756 let's just give you a few of the 22037 13:53:00,116 --> 13:53:04,756 possible ways so here's a second way you 22038 13:53:02,756 --> 13:53:07,360 could print out hello comma David or 22039 13:53:04,756 --> 13:53:08,796 hello comma Carter but what has changed 22040 13:53:07,360 --> 13:53:11,756 in the previous version I used 22041 13:53:08,796 --> 13:53:13,520 concatenation explicitly and the space 22042 13:53:11,756 --> 13:53:15,560 here is important grammatically just so 22043 13:53:13,520 --> 13:53:17,360 we get that in the final phrase 22044 13:53:15,560 --> 13:53:20,480 now I'm proposing to get rid of that 22045 13:53:17,360 --> 13:53:22,276 space to add a comma outside of the 22046 13:53:20,480 --> 13:53:24,640 double quotes as well but if you think 22047 13:53:22,276 --> 13:53:27,400 back to C this probably just means that 22048 13:53:24,640 --> 13:53:29,840 print similar in spirit to print F can 22049 13:53:27,400 --> 13:53:31,560 take not just one argument but even two 22050 13:53:29,840 --> 13:53:32,916 and in fact because of this comma in the 22051 13:53:31,560 --> 13:53:35,640 middle that's outside of the double 22052 13:53:32,916 --> 13:53:38,000 quotes it's hello comma and then it will 22053 13:53:35,640 --> 13:53:39,880 be automatically concatenated with even 22054 13:53:38,000 --> 13:53:42,160 without using the plus to whatever the 22055 13:53:39,880 --> 13:53:44,880 value of answer is and by default just 22056 13:53:42,160 --> 13:53:46,596 for grammatical prettiness the print fun 22057 13:53:44,880 --> 13:53:48,720 function always gives you a space for 22058 13:53:46,596 --> 13:53:50,320 free in between each of the multiple 22059 13:53:48,720 --> 13:53:52,080 arguments you pass in we'll see how you 22060 13:53:50,320 --> 13:53:54,000 can override that down the line but for 22061 13:53:52,080 --> 13:53:56,720 now that's just another way to do it now 22062 13:53:54,000 --> 13:53:58,276 perhaps the better if slightly cryptic 22063 13:53:56,720 --> 13:54:00,160 way to do this or just the increasingly 22064 13:53:58,276 --> 13:54:03,000 common way is probably this third 22065 13:54:00,160 --> 13:54:04,596 version which looks a little weird too 22066 13:54:03,000 --> 13:54:05,916 and probably the weirdness jumps out 22067 13:54:04,596 --> 13:54:08,756 we've automatic we've suddenly 22068 13:54:05,916 --> 13:54:10,596 introduced these like Curly braces which 22069 13:54:08,756 --> 13:54:13,640 I promised were mostly gone and they are 22070 13:54:10,596 --> 13:54:16,756 but inside of this string here I've done 22071 13:54:13,640 --> 13:54:18,640 a curly brace which might mean what just 22072 13:54:16,756 --> 13:54:20,116 intuitively and here is sort of an 22073 13:54:18,640 --> 13:54:22,116 example of how you learn a new language 22074 13:54:20,116 --> 13:54:23,640 just kind of infer from Context how 22075 13:54:22,116 --> 13:54:26,360 python probably 22076 13:54:23,640 --> 13:54:27,796 works what might this mean yeah able to 22077 13:54:26,360 --> 13:54:29,680 tell that this is not one of the actual 22078 13:54:27,796 --> 13:54:32,080 stat 22079 13:54:29,680 --> 13:54:33,956 inside yeah so this is an indication 22080 13:54:32,080 --> 13:54:35,756 because the curly braces because this is 22081 13:54:33,956 --> 13:54:38,080 the way python was designed that we want 22082 13:54:35,756 --> 13:54:41,320 to plug in the value of answer not 22083 13:54:38,080 --> 13:54:43,400 literally a ANS w r and the fancy word 22084 13:54:41,320 --> 13:54:45,520 here is that the answer variable will be 22085 13:54:43,400 --> 13:54:48,000 interpolated that is substituted with 22086 13:54:45,520 --> 13:54:49,436 its actual value but but but and this is 22087 13:54:48,000 --> 13:54:51,520 actually weird looking and this was 22088 13:54:49,436 --> 13:54:53,240 introduced a few years ago to python 22089 13:54:51,520 --> 13:54:55,400 what else did I have to change to make 22090 13:54:53,240 --> 13:54:57,596 these curly braces work 22091 13:54:55,400 --> 13:55:00,320 apparently 22092 13:54:57,596 --> 13:55:02,276 yeah yeah there's this weird F and so 22093 13:55:00,320 --> 13:55:04,756 it's sort of like part of print F but 22094 13:55:02,276 --> 13:55:06,480 now it's inside the curly it's inside 22095 13:55:04,756 --> 13:55:08,360 the parenthesis there this is just the 22096 13:55:06,480 --> 13:55:09,880 way python designed this so a few years 22097 13:55:08,360 --> 13:55:12,360 ago when they introduced what are called 22098 13:55:09,880 --> 13:55:14,956 format strings or F strings you 22099 13:55:12,360 --> 13:55:18,040 literally prefix your quoted string with 22100 13:55:14,956 --> 13:55:19,916 the uh letter F and then you can use 22101 13:55:18,040 --> 13:55:21,360 trickery like this like putting curly 22102 13:55:19,916 --> 13:55:23,360 braces so that the value will be 22103 13:55:21,360 --> 13:55:24,956 substituted automatically if you forget 22104 13:55:23,360 --> 13:55:27,040 the F you're going to literally see 22105 13:55:24,956 --> 13:55:29,480 hello comma curly brace answer close 22106 13:55:27,040 --> 13:55:32,200 curly brace if you add the F it's indeed 22107 13:55:29,480 --> 13:55:34,400 interpolated the value is plugged in all 22108 13:55:32,200 --> 13:55:37,080 right questions on how we can just say 22109 13:55:34,400 --> 13:55:38,796 hello to the world via python in this 22110 13:55:37,080 --> 13:55:42,240 case 22111 13:55:38,796 --> 13:55:44,160 yeah if you do this without the without 22112 13:55:42,240 --> 13:55:46,916 the F if you omit the F you will 22113 13:55:44,160 --> 13:55:49,916 literally see c h e l l o comma curly 22114 13:55:46,916 --> 13:55:51,720 brace a NS w r close curly brace so in 22115 13:55:49,916 --> 13:55:54,040 fact let's do this let me go back to vs 22116 13:55:51,720 --> 13:55:57,200 code here quickly I've still got my file 22117 13:55:54,040 --> 13:55:59,240 called hello.py open and let me go ahead 22118 13:55:57,200 --> 13:56:03,040 and change this ever so slightly so I'm 22119 13:55:59,240 --> 13:56:05,880 going to go ahead and uh let's say from 22120 13:56:03,040 --> 13:56:08,276 cs50 import get string and that's just 22121 13:56:05,880 --> 13:56:10,880 the new syntax I propose using to import 22122 13:56:08,276 --> 13:56:13,596 a function from someone else's Library 22123 13:56:10,880 --> 13:56:16,436 I'm going to now go ahead and ask the 22124 13:56:13,596 --> 13:56:18,400 question uh let's go ahead and use get 22125 13:56:16,436 --> 13:56:20,880 string storing the result and answer so 22126 13:56:18,400 --> 13:56:23,240 get string quote unquote what's your 22127 13:56:20,880 --> 13:56:25,240 name question mark and then on this line 22128 13:56:23,240 --> 13:56:27,240 I'm going to deliberately make a mistake 22129 13:56:25,240 --> 13:56:30,880 here exactly to your question let me 22130 13:56:27,240 --> 13:56:32,720 just say hello comma answer and just 22131 13:56:30,880 --> 13:56:34,360 this now even though answer is a 22132 13:56:32,720 --> 13:56:36,840 variable Python's not going to be so 22133 13:56:34,360 --> 13:56:38,840 presumptuous as to just plug in the 22134 13:56:36,840 --> 13:56:40,916 value of a variable called answer what 22135 13:56:38,840 --> 13:56:43,596 it's going to do of course is if I type 22136 13:56:40,916 --> 13:56:45,276 in my name whoops I typed too fast let 22137 13:56:43,596 --> 13:56:47,840 me go ahead and rerun that again if I 22138 13:56:45,276 --> 13:56:50,560 run python of hello.py type in my name 22139 13:56:47,840 --> 13:56:53,040 and hit enter I get hello comma answer 22140 13:56:50,560 --> 13:56:55,560 well let me do one better let me apply 22141 13:56:53,040 --> 13:56:58,200 these curly braces as before let me 22142 13:56:55,560 --> 13:57:00,276 rerun python of hello.py What's Your 22143 13:56:58,200 --> 13:57:02,040 Name daav ID and here's again the answer 22144 13:57:00,276 --> 13:57:04,080 to your question now we get literally 22145 13:57:02,040 --> 13:57:05,840 the curly braces so the fix here 22146 13:57:04,080 --> 13:57:08,680 ultimately is just going to be to add 22147 13:57:05,840 --> 13:57:11,680 the F there rerun my program again with 22148 13:57:08,680 --> 13:57:13,200 daav ID and now hello comma David so 22149 13:57:11,680 --> 13:57:15,436 this is admittedly a little more cryptic 22150 13:57:13,200 --> 13:57:17,200 than the ones with the Plus the comma 22151 13:57:15,436 --> 13:57:18,436 but this is just increasingly common why 22152 13:57:17,200 --> 13:57:19,720 because you can read it left to right 22153 13:57:18,436 --> 13:57:21,200 it's nice and convenient it's less 22154 13:57:19,720 --> 13:57:22,756 cryptic than the percent s's so it's 22155 13:57:21,200 --> 13:57:25,756 sort of a new and improved version if 22156 13:57:22,756 --> 13:57:27,400 you will of printf in C based on Decades 22157 13:57:25,756 --> 13:57:32,040 of experience of programmers doing 22158 13:57:27,400 --> 13:57:33,040 things like this questions on printing 22159 13:57:32,040 --> 13:57:35,756 in this 22160 13:57:33,040 --> 13:57:36,640 way we're now on our way to programming 22161 13:57:35,756 --> 13:57:39,040 in 22162 13:57:36,640 --> 13:57:41,796 Python anything all right well what more 22163 13:57:39,040 --> 13:57:44,880 can we do with this language here well 22164 13:57:41,796 --> 13:57:46,480 let me propose that we consider that we 22165 13:57:44,880 --> 13:57:49,520 have for 22166 13:57:46,480 --> 13:57:52,720 instance a few other features that we 22167 13:57:49,520 --> 13:57:57,040 can add to the mix as well 22168 13:57:52,720 --> 13:57:59,640 namely let's say some data types as well 22169 13:57:57,040 --> 13:58:01,000 so let me flip over here to um back to 22170 13:57:59,640 --> 13:58:03,160 the slides and there's different data 22171 13:58:01,000 --> 13:58:05,000 types in python as we'll soon see but 22172 13:58:03,160 --> 13:58:07,756 they're not as explicit as we already 22173 13:58:05,000 --> 13:58:09,400 saw by using a string from get string 22174 13:58:07,756 --> 13:58:12,160 you don't have to explicitly State what 22175 13:58:09,400 --> 13:58:14,880 it is but you solve recall and see all 22176 13:58:12,160 --> 13:58:17,000 of these various data types and then in 22177 13:58:14,880 --> 13:58:18,956 python kind of nicely enough this list 22178 13:58:17,000 --> 13:58:21,360 is about to get shorter and so here is 22179 13:58:18,956 --> 13:58:23,116 our list in C here is an abbreviated 22180 13:58:21,360 --> 13:58:25,116 list in Python so we're still going to 22181 13:58:23,116 --> 13:58:27,680 have strings but they're going to be 22182 13:58:25,116 --> 13:58:29,160 more succinctly called stirs now St Str 22183 13:58:27,680 --> 13:58:30,480 we're still going to have in for 22184 13:58:29,160 --> 13:58:32,116 integers we're still going to have 22185 13:58:30,480 --> 13:58:33,796 floats for floating Point values we're 22186 13:58:32,116 --> 13:58:35,680 even going to have bulls for true and 22187 13:58:33,796 --> 13:58:39,040 false but what's missing now from the 22188 13:58:35,680 --> 13:58:41,320 list is long and floats and why is that 22189 13:58:39,040 --> 13:58:43,640 or rather long and double well recall 22190 13:58:41,320 --> 13:58:46,640 that in C those used more bits well in 22191 13:58:43,640 --> 13:58:48,796 Python the smaller data types previously 22192 13:58:46,640 --> 13:58:50,680 int and Float themselves just use more 22193 13:58:48,796 --> 13:58:52,796 bits for you and so you don't need to 22194 13:58:50,680 --> 13:58:54,840 distinguish between small and large you 22195 13:58:52,796 --> 13:58:57,360 just use one data type and the language 22196 13:58:54,840 --> 13:58:58,436 gives you a bigger range than before it 22197 13:58:57,360 --> 13:59:00,916 turns out though there's going to be 22198 13:58:58,436 --> 13:59:02,360 some other features as well of python 22199 13:59:00,916 --> 13:59:04,160 these data types one of which will be 22200 13:59:02,360 --> 13:59:06,116 called range another of which will be 22201 13:59:04,160 --> 13:59:07,436 list So Gone will be arrays we'll 22202 13:59:06,116 --> 13:59:10,596 actually use something literally called 22203 13:59:07,436 --> 13:59:13,000 a list tle sort of like XY pairs for 22204 13:59:10,596 --> 13:59:15,080 coordinates and things like that uh dict 22205 13:59:13,000 --> 13:59:17,240 for dictionaries so we have built-in 22206 13:59:15,080 --> 13:59:19,080 capabilities for storing keys and values 22207 13:59:17,240 --> 13:59:20,276 we'll see and even a set sort of 22208 13:59:19,080 --> 13:59:21,400 mathematically a set is like a 22209 13:59:20,276 --> 13:59:23,200 collection of values but it 22210 13:59:21,400 --> 13:59:24,916 automatically gets rid of duplicates for 22211 13:59:23,200 --> 13:59:28,000 you so all of these things we could 22212 13:59:24,916 --> 13:59:30,360 absolutely Implement in C if we wanted 22213 13:59:28,000 --> 13:59:32,400 and indeed in problem set five you've 22214 13:59:30,360 --> 13:59:35,040 been implementing your very own spell 22215 13:59:32,400 --> 13:59:37,320 checker using some form of hashtable 22216 13:59:35,040 --> 13:59:39,520 well it turns out that in Python you can 22217 13:59:37,320 --> 13:59:41,480 solve those same problems but perhaps a 22218 13:59:39,520 --> 13:59:44,360 little more readily in fact let me go 22219 13:59:41,480 --> 13:59:47,040 back over here to vs code and let me 22220 13:59:44,360 --> 13:59:49,756 propose that I do the following let me 22221 13:59:47,040 --> 13:59:52,360 go ahead and create a file called 22222 13:59:49,756 --> 13:59:54,916 dictionary. let me propose that I try to 22223 13:59:52,360 --> 13:59:57,640 implement say problem set five our spell 22224 13:59:54,916 --> 14:00:00,436 checker in Python instead of c and 22225 13:59:57,640 --> 14:00:02,320 Achieve ultimately the same kind of 22226 14:00:00,436 --> 14:00:04,360 behavior uh whereby I'll be able to 22227 14:00:02,320 --> 14:00:05,720 spell check a whole bunch of words so 22228 14:00:04,360 --> 14:00:06,880 this is jumping the gun a little bit 22229 14:00:05,720 --> 14:00:08,880 because you're about to see syntax will 22230 14:00:06,880 --> 14:00:10,040 revisit over the course of today but for 22231 14:00:08,880 --> 14:00:12,480 now I've got a new file called 22232 14:00:10,040 --> 14:00:14,756 dictionary. py and let me begin to 22233 14:00:12,480 --> 14:00:16,596 create uh some placehold ERS for 22234 14:00:14,756 --> 14:00:18,360 functions we'll see in just a bit that 22235 14:00:16,596 --> 14:00:20,560 in Python you can define a function 22236 14:00:18,360 --> 14:00:23,240 called check and that check function can 22237 14:00:20,560 --> 14:00:25,520 take a word as it's input and I'll come 22238 14:00:23,240 --> 14:00:27,840 back to this in just a moment in Python 22239 14:00:25,520 --> 14:00:29,080 I can define a second function like load 22240 14:00:27,840 --> 14:00:31,796 which itself will take a whole 22241 14:00:29,080 --> 14:00:33,796 dictionary just like in problem set five 22242 14:00:31,796 --> 14:00:36,276 and I'll go ahead and come back to the 22243 14:00:33,796 --> 14:00:38,000 implementation of this meanwhile we 22244 14:00:36,276 --> 14:00:40,640 might similarly Implement a function 22245 14:00:38,000 --> 14:00:42,160 called size which takes no arguments but 22246 14:00:40,640 --> 14:00:44,240 ultimately is going to return the size 22247 14:00:42,160 --> 14:00:46,400 of my dictionary of words and then last 22248 14:00:44,240 --> 14:00:48,916 lastly for consistency with problem set 22249 14:00:46,400 --> 14:00:50,720 five we might Define an unload function 22250 14:00:48,916 --> 14:00:52,360 whose purpose in life is to free any 22251 14:00:50,720 --> 14:00:54,520 memory that you've been using just to 22252 14:00:52,360 --> 14:00:55,956 give it back to the computer now odds 22253 14:00:54,520 --> 14:00:57,916 are whether you're still working on 22254 14:00:55,956 --> 14:01:00,200 speller or half finished speller like 22255 14:00:57,916 --> 14:01:02,200 you wrote a decent amount of lines of 22256 14:01:00,200 --> 14:01:04,560 code and indeed it's been by design a 22257 14:01:02,200 --> 14:01:07,200 challenge but one of the reasons for 22258 14:01:04,560 --> 14:01:08,956 these higher level languages like python 22259 14:01:07,200 --> 14:01:11,560 is that you can stand on the shoulders 22260 14:01:08,956 --> 14:01:13,640 of programmers before you and solve very 22261 14:01:11,560 --> 14:01:15,160 common problems much more quickly so 22262 14:01:13,640 --> 14:01:16,916 that you can f us on building your new 22263 14:01:15,160 --> 14:01:18,640 app or your web application or your own 22264 14:01:16,916 --> 14:01:22,200 project to solve problems of interest to 22265 14:01:18,640 --> 14:01:24,796 you so at the risk of crushing some 22266 14:01:22,200 --> 14:01:26,596 Spirits let me propose that in Python if 22267 14:01:24,796 --> 14:01:28,720 you want a dictionary for something like 22268 14:01:26,596 --> 14:01:30,436 a spell checker well that's fine go 22269 14:01:28,720 --> 14:01:32,916 ahead and give yourself a variable like 22270 14:01:30,436 --> 14:01:36,000 words to store all of those words and 22271 14:01:32,916 --> 14:01:38,400 just assign it equal to a dictionary or 22272 14:01:36,000 --> 14:01:40,756 dict for short in Python that will give 22273 14:01:38,400 --> 14:01:42,720 you a hashtable now it turns out in 22274 14:01:40,756 --> 14:01:44,640 speller recall you don't need to worry 22275 14:01:42,720 --> 14:01:46,680 about words and definition conditions 22276 14:01:44,640 --> 14:01:48,240 it's just about spellchecking the words 22277 14:01:46,680 --> 14:01:50,520 so strictly speaking we don't need keys 22278 14:01:48,240 --> 14:01:52,160 and values we just need keys so I'm 22279 14:01:50,520 --> 14:01:53,596 going to save myself a few more key 22280 14:01:52,160 --> 14:01:56,200 strokes by just saying that technically 22281 14:01:53,596 --> 14:01:57,756 in Python using a set suffices again a 22282 14:01:56,200 --> 14:01:59,480 set is just a collection of values with 22283 14:01:57,756 --> 14:02:02,000 no duplicates but they don't necessarily 22284 14:01:59,480 --> 14:02:04,276 have uh keys and values it's just one or 22285 14:02:02,000 --> 14:02:06,436 the other but now that I have on line 22286 14:02:04,276 --> 14:02:09,436 one I claim the equivalent in Python of 22287 14:02:06,436 --> 14:02:11,560 a hash table I can actually do something 22288 14:02:09,436 --> 14:02:14,640 like this here's how I might implement 22289 14:02:11,560 --> 14:02:17,000 the check function in Python if the word 22290 14:02:14,640 --> 14:02:20,276 passed into this function is in my 22291 14:02:17,000 --> 14:02:25,436 variable called words well return true 22292 14:02:20,276 --> 14:02:27,840 else go ahead and return false done wait 22293 14:02:25,436 --> 14:02:29,680 you're thinking if anything at all maybe 22294 14:02:27,840 --> 14:02:31,720 we want to handle lowercase instead of 22295 14:02:29,680 --> 14:02:33,116 just uppercase and lowercase well you 22296 14:02:31,720 --> 14:02:34,796 know what in Python if you want to force 22297 14:02:33,116 --> 14:02:36,320 a whole word to lowercase you don't have 22298 14:02:34,796 --> 14:02:37,916 to iterate over it with a loop you don't 22299 14:02:36,320 --> 14:02:40,200 have to use any of that ctype functions 22300 14:02:37,916 --> 14:02:41,560 or anything just say word. lower and 22301 14:02:40,200 --> 14:02:43,796 that will convert the whole thing to 22302 14:02:41,560 --> 14:02:45,400 lowercase for parody with the dictionary 22303 14:02:43,796 --> 14:02:47,880 all right how about something like the 22304 14:02:45,400 --> 14:02:49,756 load function in Python well in Python 22305 14:02:47,880 --> 14:02:52,160 you can open files just like in C for 22306 14:02:49,756 --> 14:02:54,640 instance in Python I might do open the 22307 14:02:52,160 --> 14:02:57,160 dictionary argument in read mode just 22308 14:02:54,640 --> 14:02:59,520 like fopen in Python I might do 22309 14:02:57,160 --> 14:03:03,400 something like this for each line in 22310 14:02:59,520 --> 14:03:06,916 that file let me go ahead and add to my 22311 14:03:03,400 --> 14:03:10,276 words variable that line and then let me 22312 14:03:06,916 --> 14:03:11,880 go ahead and close that file and I think 22313 14:03:10,276 --> 14:03:14,160 I'm done I'm just going to go ahead and 22314 14:03:11,880 --> 14:03:15,840 return true just because I think think 22315 14:03:14,160 --> 14:03:18,200 I'm already done now here too I could 22316 14:03:15,840 --> 14:03:20,276 nitpick a little bit technically if I'm 22317 14:03:18,200 --> 14:03:22,000 reading in every line from the file 22318 14:03:20,276 --> 14:03:23,840 every line in the dictionary ends with 22319 14:03:22,000 --> 14:03:26,080 technically a back sln but there's an 22320 14:03:23,840 --> 14:03:27,840 easy way to get rid of that uh just like 22321 14:03:26,080 --> 14:03:29,360 you might in see with an alternative 22322 14:03:27,840 --> 14:03:32,480 syntax what I'm actually going to do is 22323 14:03:29,360 --> 14:03:35,320 this let me grab from the current line 22324 14:03:32,480 --> 14:03:37,840 the current word by stripping off with 22325 14:03:35,320 --> 14:03:39,640 reverse strip R strip a function will 22326 14:03:37,840 --> 14:03:41,596 again see that just gets rid of the 22327 14:03:39,640 --> 14:03:43,320 trailing new line the back sln at the 22328 14:03:41,596 --> 14:03:45,640 end of that line and what I really want 22329 14:03:43,320 --> 14:03:47,756 to do then is add this word to that 22330 14:03:45,640 --> 14:03:49,916 dictionary meanwhile if I want to figure 22331 14:03:47,756 --> 14:03:51,400 out what the size is of my dictionary 22332 14:03:49,916 --> 14:03:53,320 well and see you're probably writing 22333 14:03:51,400 --> 14:03:55,320 code to iterate over all of those lines 22334 14:03:53,320 --> 14:03:57,640 and you're just going to uh count them 22335 14:03:55,320 --> 14:03:59,360 up using a variable not so in Python you 22336 14:03:57,640 --> 14:04:02,200 can just return the length of those 22337 14:03:59,360 --> 14:04:04,080 words and better still in Python you 22338 14:04:02,200 --> 14:04:07,640 don't have to manage your own memory no 22339 14:04:04,080 --> 14:04:09,756 more Malo no more free no more manual 22340 14:04:07,640 --> 14:04:12,200 thinking about memory the language just 22341 14:04:09,756 --> 14:04:14,040 deals with all of that for you so you 22342 14:04:12,200 --> 14:04:16,640 know what it suffices for me to just 22343 14:04:14,040 --> 14:04:20,360 return true and claim that unloading is 22344 14:04:16,640 --> 14:04:21,916 done for me and that's it again whether 22345 14:04:20,360 --> 14:04:23,840 you're in the middle of or already 22346 14:04:21,916 --> 14:04:26,480 finished this might perhaps suggest some 22347 14:04:23,840 --> 14:04:28,956 frustration but also Enlightenment in 22348 14:04:26,480 --> 14:04:31,480 this in that this is why higher level 22349 14:04:28,956 --> 14:04:33,560 languages exist you can build on top of 22350 14:04:31,480 --> 14:04:35,160 the same principles the same ideas with 22351 14:04:33,560 --> 14:04:37,640 which you've been dealing struggling 22352 14:04:35,160 --> 14:04:39,640 even this past week but you can now 22353 14:04:37,640 --> 14:04:42,116 express yourself all the more succinctly 22354 14:04:39,640 --> 14:04:44,240 like this one line implements a hash 22355 14:04:42,116 --> 14:04:46,916 table for you and all of this now now 22356 14:04:44,240 --> 14:04:50,276 just uses that hash table in a simpler 22357 14:04:46,916 --> 14:04:51,956 way any questions now on this keeping in 22358 14:04:50,276 --> 14:04:54,596 mind that the point nonetheless of 22359 14:04:51,956 --> 14:04:56,080 speller and P said 5 is to understand 22360 14:04:54,596 --> 14:04:59,480 what's really going on underneath the 22361 14:04:56,080 --> 14:05:01,436 hood and better still to notice this 22362 14:04:59,480 --> 14:05:02,796 this might seem all rather amazing but 22363 14:05:01,436 --> 14:05:04,796 let me go ahead and do this I've 22364 14:05:02,796 --> 14:05:06,320 actually got a couple of versions of 22365 14:05:04,796 --> 14:05:08,116 speller written here and I've got a 22366 14:05:06,320 --> 14:05:09,956 version written in C that I won't show 22367 14:05:08,116 --> 14:05:12,436 the source code for but I'm going to go 22368 14:05:09,956 --> 14:05:15,840 ahead and make that version of speller 22369 14:05:12,436 --> 14:05:18,240 in C and I'm going to go ahead here and 22370 14:05:15,840 --> 14:05:20,080 let's say split my window here for just 22371 14:05:18,240 --> 14:05:22,200 a moment and I'm going to go into a 22372 14:05:20,080 --> 14:05:24,480 python version of spell really that I 22373 14:05:22,200 --> 14:05:27,560 just wrote and on the left hand side 22374 14:05:24,480 --> 14:05:30,756 here let me go ahead and run speller the 22375 14:05:27,560 --> 14:05:32,520 version I compiled in C using a big text 22376 14:05:30,756 --> 14:05:34,956 like uh the Sherlock Holmes text which 22377 14:05:32,520 --> 14:05:37,320 is a whole lot of words in it and on the 22378 14:05:34,956 --> 14:05:39,240 right hand side let me run python of 22379 14:05:37,320 --> 14:05:40,840 spell. Pi which is a separate file I 22380 14:05:39,240 --> 14:05:43,916 wrote in advance just like we give you 22381 14:05:40,840 --> 14:05:45,720 speller.c and I'll similarly run this on 22382 14:05:43,916 --> 14:05:48,320 the Sherlock Holmes text and I'm going 22383 14:05:45,720 --> 14:05:50,116 to do my best to hit enter on the left 22384 14:05:48,320 --> 14:05:52,160 and the right of my screen at the same 22385 14:05:50,116 --> 14:05:53,840 time but we should see hopefully the 22386 14:05:52,160 --> 14:05:56,436 same list of misspelled words and the 22387 14:05:53,840 --> 14:05:59,160 timings thereof so here we go on the 22388 14:05:56,436 --> 14:06:02,240 right here we go on the 22389 14:05:59,160 --> 14:06:04,596 left all right sort of a race to see 22390 14:06:02,240 --> 14:06:09,160 which one wins here C is on the left 22391 14:06:04,596 --> 14:06:11,840 python is on the right okay 22392 14:06:09,160 --> 14:06:14,436 interesting hopefully Python's close 22393 14:06:11,840 --> 14:06:16,320 behind note that some of the is internet 22394 14:06:14,436 --> 14:06:19,080 delay and so it might not necessarily be 22395 14:06:16,320 --> 14:06:21,240 a crazy number of seconds but the system 22396 14:06:19,080 --> 14:06:23,000 is indeed using if we measure at a low 22397 14:06:21,240 --> 14:06:26,436 level how much time the CPU spent 22398 14:06:23,000 --> 14:06:27,916 executing my code C took a total of 1.64 22399 14:06:26,436 --> 14:06:29,320 seconds that was pretty fast even though 22400 14:06:27,916 --> 14:06:31,956 it took a moment more for all of the 22401 14:06:29,320 --> 14:06:34,880 btes to come over the Internet the 22402 14:06:31,956 --> 14:06:37,596 python version though took what 2.44 22403 14:06:34,880 --> 14:06:39,080 seconds so what might an inference be I 22404 14:06:37,596 --> 14:06:41,116 mean one maybe I'm just better at 22405 14:06:39,080 --> 14:06:44,520 programming in c than I am in Python 22406 14:06:41,116 --> 14:06:49,916 which is probably not true 22407 14:06:44,520 --> 14:06:49,916 but what else might you infer from this 22408 14:06:51,480 --> 14:06:55,320 example should we maybe give up on 22409 14:06:53,596 --> 14:06:58,240 python stick with 22410 14:06:55,320 --> 14:07:00,400 C no so where what might be going on 22411 14:06:58,240 --> 14:07:01,916 here like why is the python version that 22412 14:07:00,400 --> 14:07:04,320 I claim is correct and I think the 22413 14:07:01,916 --> 14:07:06,956 numbers all line up just not the 22414 14:07:04,320 --> 14:07:08,240 times where's the trade-off here well 22415 14:07:06,956 --> 14:07:10,796 here again is sort of this design 22416 14:07:08,240 --> 14:07:10,796 tradeoff 22417 14:07:12,640 --> 14:07:18,320 yeah yeah exactly in order to save the 22418 14:07:16,400 --> 14:07:19,956 human programmer time there's a lot more 22419 14:07:18,320 --> 14:07:22,240 features built into python more 22420 14:07:19,956 --> 14:07:24,200 functions more automatic management of 22421 14:07:22,240 --> 14:07:26,520 memory and so forth and you have to pay 22422 14:07:24,200 --> 14:07:28,360 a price like someone else's code is 22423 14:07:26,520 --> 14:07:29,640 doing all of that work for you but if 22424 14:07:28,360 --> 14:07:31,320 they've written some number of lines of 22425 14:07:29,640 --> 14:07:33,680 code those are just more lines of code 22426 14:07:31,320 --> 14:07:35,720 that need to be executed for you whereas 22427 14:07:33,680 --> 14:07:38,640 here the computer is at the risk of 22428 14:07:35,720 --> 14:07:40,640 oversimplifying only running my lines of 22429 14:07:38,640 --> 14:07:42,160 code so there's just less overhead and 22430 14:07:40,640 --> 14:07:43,956 so this is a Perpetual trade-off 22431 14:07:42,160 --> 14:07:45,956 typically when using a more userfriendly 22432 14:07:43,956 --> 14:07:48,360 and more modern language one of the 22433 14:07:45,956 --> 14:07:49,480 prices you might pay is performance now 22434 14:07:48,360 --> 14:07:50,796 there's a lot of smart computer 22435 14:07:49,480 --> 14:07:53,756 scientists in the world though trying to 22436 14:07:50,796 --> 14:07:55,680 push back on those same tradeoffs and so 22437 14:07:53,756 --> 14:07:58,116 these interpreters like the command I 22438 14:07:55,680 --> 14:08:00,000 wrote python technically can especially 22439 14:07:58,116 --> 14:08:01,480 if you run a program again and again 22440 14:08:00,000 --> 14:08:03,756 they can actually sort of secretly 22441 14:08:01,480 --> 14:08:05,880 behind the scenes compile your code for 22442 14:08:03,756 --> 14:08:07,360 you down to zeros and ones and then the 22443 14:08:05,880 --> 14:08:09,276 second the third the fourth time you run 22444 14:08:07,360 --> 14:08:10,956 that program it might very well be 22445 14:08:09,276 --> 14:08:13,080 faster so this is a bit of a head fake 22446 14:08:10,956 --> 14:08:15,756 here in that I'm running them once and 22447 14:08:13,080 --> 14:08:17,436 only once but we could get benefit over 22448 14:08:15,756 --> 14:08:19,240 time if we kept running the python 22449 14:08:17,436 --> 14:08:21,116 version again and again and perhaps 22450 14:08:19,240 --> 14:08:23,116 fine-tune the performance but in general 22451 14:08:21,116 --> 14:08:25,116 there's going to be this trade-off now 22452 14:08:23,116 --> 14:08:26,956 would you rather spend the 60 seconds I 22453 14:08:25,116 --> 14:08:30,040 wrote implementing a spell checker or 22454 14:08:26,956 --> 14:08:32,480 the 6 hours 16 hours you might be or 22455 14:08:30,040 --> 14:08:34,240 have spent implementing the same and C 22456 14:08:32,480 --> 14:08:35,956 you know probably not for productivity 22457 14:08:34,240 --> 14:08:38,200 sake this is why we have these 22458 14:08:35,956 --> 14:08:41,320 additional languages just for fun let me 22459 14:08:38,200 --> 14:08:43,000 flip over to another screen here and 22460 14:08:41,320 --> 14:08:45,560 open up a version of python that's 22461 14:08:43,000 --> 14:08:48,880 actually on on my in just a second on my 22462 14:08:45,560 --> 14:08:50,276 own uh Mac instead of the cloud so that 22463 14:08:48,880 --> 14:08:52,160 I can actually do something with 22464 14:08:50,276 --> 14:08:54,520 Graphics so here I just have a black and 22465 14:08:52,160 --> 14:08:56,360 white terminal window on my very own Mac 22466 14:08:54,520 --> 14:08:58,116 and I've pre-installed python just like 22467 14:08:56,360 --> 14:09:00,956 we've done so for VSS code in the cloud 22468 14:08:58,116 --> 14:09:03,796 for you uh notice that I've got this uh 22469 14:09:00,956 --> 14:09:05,640 photo of uh perhaps one of your favorite 22470 14:09:03,796 --> 14:09:08,240 TV shows here with the cast of The 22471 14:09:05,640 --> 14:09:10,720 Office notice all of the faces in this 22472 14:09:08,240 --> 14:09:13,240 image here and let me propose that we 22473 14:09:10,720 --> 14:09:16,200 try to find one face in the crowd sort 22474 14:09:13,240 --> 14:09:18,240 of CSI style whereby we want to find 22475 14:09:16,200 --> 14:09:20,640 perhaps the stranton Strangler so to 22476 14:09:18,240 --> 14:09:23,320 speak and so here is an example of this 22477 14:09:20,640 --> 14:09:25,320 this guy's face now how do we go about 22478 14:09:23,320 --> 14:09:26,840 finding this specific face in the crowd 22479 14:09:25,320 --> 14:09:28,080 well our human eyes obviously can pluck 22480 14:09:26,840 --> 14:09:30,436 him out especially if you're familiar 22481 14:09:28,080 --> 14:09:33,916 with the show but let me go ahead and do 22482 14:09:30,436 --> 14:09:36,360 this instead let me go ahead and propose 22483 14:09:33,916 --> 14:09:38,796 that we run code that I already wrote in 22484 14:09:36,360 --> 14:09:40,116 advance here this is a Python program 22485 14:09:38,796 --> 14:09:41,880 with more lines of code that we won't 22486 14:09:40,116 --> 14:09:45,040 dwell on for today but it's meant to 22487 14:09:41,880 --> 14:09:47,400 motivate what we can do from a a pillow 22488 14:09:45,040 --> 14:09:50,560 uh Library implying a python image 22489 14:09:47,400 --> 14:09:52,116 Library I want to import some type of 22490 14:09:50,560 --> 14:09:53,436 information called some type of some 22491 14:09:52,116 --> 14:09:55,680 feature called image so that I can 22492 14:09:53,436 --> 14:09:57,436 manipulate images not unlike our own 22493 14:09:55,680 --> 14:09:59,560 problem set 4 and this is kind of 22494 14:09:57,436 --> 14:10:03,080 powerful you in Python you can just 22495 14:09:59,560 --> 14:10:05,520 import face recognition as a library 22496 14:10:03,080 --> 14:10:07,276 that someone else wrote from there I'm 22497 14:10:05,520 --> 14:10:08,880 going to create a variable called image 22498 14:10:07,276 --> 14:10:11,916 I'm going to use this face recognitions 22499 14:10:08,880 --> 14:10:13,436 libraries load image file function it's 22500 14:10:11,916 --> 14:10:15,436 a little verbose but it's similar in 22501 14:10:13,436 --> 14:10:18,200 Spirit to F open and I'm going to open 22502 14:10:15,436 --> 14:10:20,756 office. jpeg I'm going to then declare a 22503 14:10:18,200 --> 14:10:23,040 second variable called face locations 22504 14:10:20,756 --> 14:10:24,560 plural because what I'm expecting to get 22505 14:10:23,040 --> 14:10:27,560 back per the documentation for this 22506 14:10:24,560 --> 14:10:29,880 library is a list of all of the faces 22507 14:10:27,560 --> 14:10:31,480 locations that are detected all right 22508 14:10:29,880 --> 14:10:33,720 then I'm going to iterate over each of 22509 14:10:31,480 --> 14:10:35,436 those uh faces using a for Loop that 22510 14:10:33,720 --> 14:10:37,480 we'll see in more detail I'm going to 22511 14:10:35,436 --> 14:10:40,200 then infer what the top right bottom and 22512 14:10:37,480 --> 14:10:43,916 left Corners are of that face and then 22513 14:10:40,200 --> 14:10:46,756 what I'm going to do here is show that 22514 14:10:43,916 --> 14:10:52,560 face alone if I've detected the face in 22515 14:10:46,756 --> 14:10:55,640 question so let me go ahead here and run 22516 14:10:52,560 --> 14:10:57,360 detect. and we'll see not just the one 22517 14:10:55,640 --> 14:11:00,240 face we're looking for but if I run 22518 14:10:57,360 --> 14:11:02,436 python of detect. piy it's going to do 22519 14:11:00,240 --> 14:11:06,680 all of the analysis I'll see a big 22520 14:11:02,436 --> 14:11:10,000 opening here now of all of the faces 22521 14:11:06,680 --> 14:11:11,756 that were detected in this here program 22522 14:11:10,000 --> 14:11:13,796 okay some better than others I guess if 22523 14:11:11,756 --> 14:11:16,320 you zoom in on catching someone typical 22524 14:11:13,796 --> 14:11:18,276 Angela if you now want to Now find that 22525 14:11:16,320 --> 14:11:20,080 one face I think we need to train the 22526 14:11:18,276 --> 14:11:21,596 software a bit more so let me actually 22527 14:11:20,080 --> 14:11:24,000 open up a second program called 22528 14:11:21,596 --> 14:11:25,840 recognize that's got more going on but 22529 14:11:24,000 --> 14:11:27,320 let me with a wave of a hand point out 22530 14:11:25,840 --> 14:11:30,880 that I'm now loading not only the 22531 14:11:27,320 --> 14:11:33,240 office. JPEG but also toby. JPEG to sort 22532 14:11:30,880 --> 14:11:36,080 of train the algorithm to find that 22533 14:11:33,240 --> 14:11:38,956 specific face and so now if I run this 22534 14:11:36,080 --> 14:11:41,956 second version recognize. with python of 22535 14:11:38,956 --> 14:11:43,796 recognize. py hold my breath for just a 22536 14:11:41,956 --> 14:11:46,160 moment it's an analyzing presumably all 22537 14:11:43,796 --> 14:11:49,000 of the faces you see the same original 22538 14:11:46,160 --> 14:11:51,276 photo but do you see one such face 22539 14:11:49,000 --> 14:11:54,240 highlighted here this adversion of the 22540 14:11:51,276 --> 14:11:56,320 code found Toby highlighted him with 22541 14:11:54,240 --> 14:11:57,916 this green and voila we have face 22542 14:11:56,320 --> 14:11:59,240 recognition so for better for worse this 22543 14:11:57,916 --> 14:12:01,320 is what's happening increasingly 22544 14:11:59,240 --> 14:12:02,796 societally nowadays and honestly even 22545 14:12:01,320 --> 14:12:04,200 though I didn't write the code live 22546 14:12:02,796 --> 14:12:06,240 because it's a good dozen or more lines 22547 14:12:04,200 --> 14:12:08,480 of code it's not terribly many and 22548 14:12:06,240 --> 14:12:10,520 literally all the authorities all we 22549 14:12:08,480 --> 14:12:12,796 have to do is import face recognition 22550 14:12:10,520 --> 14:12:14,956 and voila you have access like these 22551 14:12:12,796 --> 14:12:16,916 techn IES are here already but let's 22552 14:12:14,956 --> 14:12:19,956 consider for just a moment how did we 22553 14:12:16,916 --> 14:12:21,000 find Toby like how might that Library 22554 14:12:19,956 --> 14:12:22,916 even though we're not going to look at 22555 14:12:21,000 --> 14:12:24,796 its implementation details how does it 22556 14:12:22,916 --> 14:12:28,720 find Toby and distinguish him from all 22557 14:12:24,796 --> 14:12:30,640 of these other faces in the crowd what 22558 14:12:28,720 --> 14:12:33,000 might it be doing 22559 14:12:30,640 --> 14:12:34,796 intuitively think back even to pet 4 22560 14:12:33,000 --> 14:12:37,480 like what you yourselves have access to 22561 14:12:34,796 --> 14:12:37,480 data wise 22562 14:12:38,796 --> 14:12:45,080 yeah you know pixels in one area area 22563 14:12:44,040 --> 14:12:47,116 and a lot 22564 14:12:45,080 --> 14:12:52,436 of 22565 14:12:47,116 --> 14:12:52,436 that it's a lot of a lot of 22566 14:12:54,436 --> 14:12:59,480 simar yeah exactly and to summarize for 22567 14:12:57,000 --> 14:13:00,956 for camera here we have trained the 22568 14:12:59,480 --> 14:13:02,640 software if you will by giving it a 22569 14:13:00,956 --> 14:13:04,880 photo of Toby's face by so by looking 22570 14:13:02,640 --> 14:13:06,080 for the same or really similar pixels 22571 14:13:04,880 --> 14:13:08,436 especially if it's a slightly different 22572 14:13:06,080 --> 14:13:10,436 image of Toby we can perhaps identify 22573 14:13:08,436 --> 14:13:12,040 him in the crowd and what really is a 22574 14:13:10,436 --> 14:13:13,916 human face well at the end of the day 22575 14:13:12,040 --> 14:13:15,596 the computer only knows it as a pattern 22576 14:13:13,916 --> 14:13:17,840 of bits or really at a higher level a 22577 14:13:15,596 --> 14:13:19,880 pattern of pixels so maybe a human face 22578 14:13:17,840 --> 14:13:22,320 is perhaps best defined in general as 22579 14:13:19,880 --> 14:13:24,116 like two eyes and a nose and a mouth 22580 14:13:22,320 --> 14:13:26,200 that even though all of us look similar 22581 14:13:24,116 --> 14:13:27,956 structurally odds are the measurement 22582 14:13:26,200 --> 14:13:29,756 between the eyes and the nose and the 22583 14:13:27,956 --> 14:13:31,596 width of the mouth the skin tone and all 22584 14:13:29,756 --> 14:13:33,880 of these other physical characteristics 22585 14:13:31,596 --> 14:13:35,480 are patterns that software could perhaps 22586 14:13:33,880 --> 14:13:36,880 detect and then look sort of 22587 14:13:35,480 --> 14:13:39,200 statistically through the image looking 22588 14:13:36,880 --> 14:13:41,200 for the closest possible match to these 22589 14:13:39,200 --> 14:13:43,080 various measurement shapes colors and 22590 14:13:41,200 --> 14:13:44,200 sizes and the like and indeed that might 22591 14:13:43,080 --> 14:13:47,276 might be the intuition but what's 22592 14:13:44,200 --> 14:13:50,880 powerful here again is just how easy and 22593 14:13:47,276 --> 14:13:52,640 readily available this technology now is 22594 14:13:50,880 --> 14:13:55,436 all right so with that said let's 22595 14:13:52,640 --> 14:13:56,756 propose to consider what more we can do 22596 14:13:55,436 --> 14:13:58,640 with python itself get back to the 22597 14:13:56,756 --> 14:14:00,840 fundamentals so that you yourselves can 22598 14:13:58,640 --> 14:14:04,000 start to implement something along those 22599 14:14:00,840 --> 14:14:06,436 same lines so besides having access to 22600 14:14:04,000 --> 14:14:08,720 things like a get string function um the 22601 14:14:06,436 --> 14:14:12,040 cs50 library provides a few other things 22602 14:14:08,720 --> 14:14:14,040 as well namely in C we had these but in 22603 14:14:12,040 --> 14:14:15,880 Python we're going to have fewer in 22604 14:14:14,040 --> 14:14:17,720 Python our library short term is going 22605 14:14:15,880 --> 14:14:19,720 to give you not only get string but also 22606 14:14:17,720 --> 14:14:21,160 get in and get float why it's actually 22607 14:14:19,720 --> 14:14:23,436 just kind of annoying as we'll student 22608 14:14:21,160 --> 14:14:25,240 see to get back an integer or a float 22609 14:14:23,436 --> 14:14:28,116 from a user and just make sure that it's 22610 14:14:25,240 --> 14:14:30,276 an INT and a float and not like a word 22611 14:14:28,116 --> 14:14:33,160 like cat or dog or some string that's 22612 14:14:30,276 --> 14:14:35,320 not actually a number well we can import 22613 14:14:33,160 --> 14:14:37,160 not just the specific function get 22614 14:14:35,320 --> 14:14:39,360 string but we can actually import all of 22615 14:14:37,160 --> 14:14:41,520 these functions one at a time like this 22616 14:14:39,360 --> 14:14:44,000 as we'll soon see or you can even in 22617 14:14:41,520 --> 14:14:46,000 Python import specific functions from a 22618 14:14:44,000 --> 14:14:47,720 file one of you asked a while back when 22619 14:14:46,000 --> 14:14:50,480 you import when you include something 22620 14:14:47,720 --> 14:14:52,680 like cs50.h or standard i.h you're 22621 14:14:50,480 --> 14:14:54,880 actually getting all of the code in that 22622 14:14:52,680 --> 14:14:57,320 file which potentially can add bulk to 22623 14:14:54,880 --> 14:14:59,596 your own program or time in this case 22624 14:14:57,320 --> 14:15:02,040 when you import specific functions from 22625 14:14:59,596 --> 14:15:04,116 python you can be a little more narrowly 22626 14:15:02,040 --> 14:15:06,756 uh precise as to what it is you want to 22627 14:15:04,116 --> 14:15:08,720 have access to all right so with that 22628 14:15:06,756 --> 14:15:11,080 said let's go ahead and see what 22629 14:15:08,720 --> 14:15:12,596 conditionals look like in Python so in 22630 14:15:11,080 --> 14:15:13,480 the left hand side again here we'll see 22631 14:15:12,596 --> 14:15:14,956 scrp 22632 14:15:13,480 --> 14:15:17,000 and here for instance was just kind of a 22633 14:15:14,956 --> 14:15:21,040 contrived example asking if x is less 22634 14:15:17,000 --> 14:15:23,276 than y then say x is less than y in C it 22635 14:15:21,040 --> 14:15:25,880 looked like this in Python now it's 22636 14:15:23,276 --> 14:15:29,840 going to look like this instead and 22637 14:15:25,880 --> 14:15:31,360 here's before in C and here's after and 22638 14:15:29,840 --> 14:15:33,320 just to call out a few of the obvious 22639 14:15:31,360 --> 14:15:36,360 differences what has changed in Python 22640 14:15:33,320 --> 14:15:36,360 for conditionals it would 22641 14:15:36,436 --> 14:15:42,240 seem sort of what's the difference 22642 14:15:40,080 --> 14:15:44,116 yeah yeah so there's no more curly 22643 14:15:42,240 --> 14:15:46,000 braces and indeed you don't use those 22644 14:15:44,116 --> 14:15:47,880 what appears to be taking their place if 22645 14:15:46,000 --> 14:15:49,956 you might 22646 14:15:47,880 --> 14:15:52,680 infer what seems to have taken their 22647 14:15:49,956 --> 14:15:54,796 place what do you think so the colon at 22648 14:15:52,680 --> 14:15:56,796 the start of this line here but also 22649 14:15:54,796 --> 14:15:59,360 even more important now is this 22650 14:15:56,796 --> 14:16:00,840 indentation below it so some of you and 22651 14:15:59,360 --> 14:16:03,000 we know this from Office hours have a 22652 14:16:00,840 --> 14:16:04,400 habit of like uh indenting everything on 22653 14:16:03,000 --> 14:16:06,560 the left right and it's just kind of 22654 14:16:04,400 --> 14:16:09,596 this crazy mess to look at frustrating 22655 14:16:06,560 --> 14:16:11,080 for you surely but C and clang is pretty 22656 14:16:09,596 --> 14:16:13,640 tolerant when it comes to things like 22657 14:16:11,080 --> 14:16:15,480 Whit space in a program python uh-uh 22658 14:16:13,640 --> 14:16:17,956 they realized years ago that let's help 22659 14:16:15,480 --> 14:16:20,116 humans help themselves and just require 22660 14:16:17,956 --> 14:16:21,756 standard indentation so four spaces 22661 14:16:20,116 --> 14:16:23,720 would be the norm here but because it's 22662 14:16:21,756 --> 14:16:26,160 indented below that colon that indeed 22663 14:16:23,720 --> 14:16:27,680 indicates that this now is part of that 22664 14:16:26,160 --> 14:16:31,040 condition something else has gone 22665 14:16:27,680 --> 14:16:33,720 missing versus C in this conditional 22666 14:16:31,040 --> 14:16:36,160 what else is a little 22667 14:16:33,720 --> 14:16:37,756 simplified yeah so no more parentheses 22668 14:16:36,160 --> 14:16:39,680 you can still use them especially when 22669 14:16:37,756 --> 14:16:41,720 you need to logically to do order of 22670 14:16:39,680 --> 14:16:42,956 operations like in math but in this case 22671 14:16:41,720 --> 14:16:44,640 if you just want to ask a simple 22672 14:16:42,956 --> 14:16:46,360 question like if x less than y you can 22673 14:16:44,640 --> 14:16:48,080 just do it like that how about when you 22674 14:16:46,360 --> 14:16:50,436 have an if else well this is almost the 22675 14:16:48,080 --> 14:16:52,040 same here with these same changes in C 22676 14:16:50,436 --> 14:16:53,720 this looked like this and it's starting 22677 14:16:52,040 --> 14:16:55,720 to get a bit bulky at least if we use 22678 14:16:53,720 --> 14:16:57,400 our curly braces in this way in Python 22679 14:16:55,720 --> 14:16:58,880 we can tighten things up further even 22680 14:16:57,400 --> 14:17:01,000 though strictly speaking in C you don't 22681 14:16:58,880 --> 14:17:03,560 always need the curly braces but here 22682 14:17:01,000 --> 14:17:05,436 gone are the parentheses again gone are 22683 14:17:03,560 --> 14:17:07,480 the curly braces indentation is 22684 14:17:05,436 --> 14:17:09,756 consistent and we've just added another 22685 14:17:07,480 --> 14:17:11,756 keyword else with a colon but no more 22686 14:17:09,756 --> 14:17:14,756 semicolons as well how about something 22687 14:17:11,756 --> 14:17:17,240 larger like this and if else if else 22688 14:17:14,756 --> 14:17:20,240 this one's a little curious but in C it 22689 14:17:17,240 --> 14:17:22,916 looked like this if else if else in 22690 14:17:20,240 --> 14:17:24,720 Python it now looks like this and 22691 14:17:22,916 --> 14:17:26,080 there's perhaps one curiosity here that 22692 14:17:24,720 --> 14:17:27,480 honestly all these years later I still 22693 14:17:26,080 --> 14:17:30,400 can't remember how to spell it half the 22694 14:17:27,480 --> 14:17:34,240 time what's weird about 22695 14:17:30,400 --> 14:17:37,796 this what do you spot as 22696 14:17:34,240 --> 14:17:37,796 different uh yeah over 22697 14:17:37,956 --> 14:17:43,840 here yeah instead of else if it's l if 22698 14:17:41,480 --> 14:17:45,640 why apparently El space if was just too 22699 14:17:43,840 --> 14:17:47,640 many keystrokes for humans to type so 22700 14:17:45,640 --> 14:17:49,360 they condensed it into this way probably 22701 14:17:47,640 --> 14:17:51,116 means it's a little more distinguishable 22702 14:17:49,360 --> 14:17:52,680 too for the computer between the if and 22703 14:17:51,116 --> 14:17:54,720 the else to but just something to 22704 14:17:52,680 --> 14:17:56,480 remember now it's indeed L if and not 22705 14:17:54,720 --> 14:17:58,160 else if all right so what about 22706 14:17:56,480 --> 14:18:01,200 variables in Python I've used a couple 22707 14:17:58,160 --> 14:18:03,560 of them already but let's let's um 22708 14:18:01,200 --> 14:18:04,956 distill exactly how you define uh and 22709 14:18:03,560 --> 14:18:06,080 declare these things as well so in 22710 14:18:04,956 --> 14:18:07,840 scratch if we wanted to create a 22711 14:18:06,080 --> 14:18:09,916 variable called counter and set it equal 22712 14:18:07,840 --> 14:18:11,956 initially to zero we would do something 22713 14:18:09,916 --> 14:18:13,680 like this specify that it's an INT use 22714 14:18:11,956 --> 14:18:16,040 the assignment op operator and the 22715 14:18:13,680 --> 14:18:18,360 thought with a semicolon in Python it's 22716 14:18:16,040 --> 14:18:20,200 just simpler you name the variable you 22717 14:18:18,360 --> 14:18:21,956 use the assignment operator as before 22718 14:18:20,200 --> 14:18:23,560 you set it equal to some value and 22719 14:18:21,956 --> 14:18:25,276 that's it you don't mention the type you 22720 14:18:23,560 --> 14:18:27,360 don't mention the semicolon or anything 22721 14:18:25,276 --> 14:18:29,640 more what if you want to change a 22722 14:18:27,360 --> 14:18:31,400 variable like the counter by one that is 22723 14:18:29,640 --> 14:18:33,796 incremented by one you have a few 22724 14:18:31,400 --> 14:18:35,360 different ways here in C we saw syntax 22725 14:18:33,796 --> 14:18:37,360 like this where you can say counter 22726 14:18:35,360 --> 14:18:40,160 equals counter plus one which again 22727 14:18:37,360 --> 14:18:42,436 feels like illogical how can counter 22728 14:18:40,160 --> 14:18:44,880 equal counter plus one but again we read 22729 14:18:42,436 --> 14:18:47,160 this code really right to left updating 22730 14:18:44,880 --> 14:18:48,720 its value by one um in Python it's 22731 14:18:47,160 --> 14:18:50,596 almost the same you just get rid of the 22732 14:18:48,720 --> 14:18:51,880 semicolon so that logic is there but 22733 14:18:50,596 --> 14:18:53,480 recall in C we could do something 22734 14:18:51,880 --> 14:18:55,880 slightly different that we can also do 22735 14:18:53,480 --> 14:18:58,320 in Python in Python you can also more 22736 14:18:55,880 --> 14:19:00,200 succinctly do this plus equals and then 22737 14:18:58,320 --> 14:19:01,720 whatever number you want to add or you 22738 14:19:00,200 --> 14:19:04,880 can even change it to subtract if you 22739 14:19:01,720 --> 14:19:06,756 prefer sadly gone is something you've 22740 14:19:04,880 --> 14:19:08,560 probably typed a whole lot what was the 22741 14:19:06,756 --> 14:19:11,756 other way you can add 22742 14:19:08,560 --> 14:19:13,160 one Plus+ is no more sadly in Python 22743 14:19:11,756 --> 14:19:15,360 just too many ways to do the same thing 22744 14:19:13,160 --> 14:19:17,596 so they got rid of it in favor of just 22745 14:19:15,360 --> 14:19:18,880 this syntax here so keep that in mind as 22746 14:19:17,596 --> 14:19:21,000 well what about loops when you want to 22747 14:19:18,880 --> 14:19:22,720 do something in Python again and again 22748 14:19:21,000 --> 14:19:25,796 well in scratch in week zero here's how 22749 14:19:22,720 --> 14:19:27,360 we meowed three times specifically in C 22750 14:19:25,796 --> 14:19:28,840 we had a couple of ways of doing this 22751 14:19:27,360 --> 14:19:30,680 this was like the more mechanical 22752 14:19:28,840 --> 14:19:32,756 approach where you create a variable 22753 14:19:30,680 --> 14:19:35,480 called I you set it equal to zero you 22754 14:19:32,756 --> 14:19:37,000 then do while I is less than three the 22755 14:19:35,480 --> 14:19:39,640 following and then you yourself 22756 14:19:37,000 --> 14:19:41,116 increment I again and again mechanical 22757 14:19:39,640 --> 14:19:42,720 in the sense that like you have to 22758 14:19:41,116 --> 14:19:44,320 implement all of these gears and make 22759 14:19:42,720 --> 14:19:46,680 them turn yourself but this was a 22760 14:19:44,320 --> 14:19:48,916 correct way to do that in Python we can 22761 14:19:46,680 --> 14:19:51,360 still achieve the same idea but we don't 22762 14:19:48,916 --> 14:19:52,796 need the int keyword we don't need any 22763 14:19:51,360 --> 14:19:54,240 of the semicolons we don't need the 22764 14:19:52,796 --> 14:19:56,680 parentheses we don't need the curly 22765 14:19:54,240 --> 14:19:58,200 braces we can't use the Plus+ so maybe 22766 14:19:56,680 --> 14:20:00,956 that's a minor step backwards if you're 22767 14:19:58,200 --> 14:20:03,200 a fan but otherwise the code the logic 22768 14:20:00,956 --> 14:20:05,436 is exactly the same but there's other 22769 14:20:03,200 --> 14:20:07,480 ways to achieve this same idea recall 22770 14:20:05,436 --> 14:20:09,480 that in C we could also do this you 22771 14:20:07,480 --> 14:20:11,756 could use a for Loop which it does 22772 14:20:09,480 --> 14:20:13,436 exactly the same thing both are correct 22773 14:20:11,756 --> 14:20:15,116 both are arguably welld designed it's 22774 14:20:13,436 --> 14:20:17,596 kind of to each their own when it comes 22775 14:20:15,116 --> 14:20:19,640 to choosing between these in Python 22776 14:20:17,596 --> 14:20:22,160 though we're going to have to think 22777 14:20:19,640 --> 14:20:25,276 through how to do this so you don't do 22778 14:20:22,160 --> 14:20:27,640 the same for loop as in C the closest I 22779 14:20:25,276 --> 14:20:30,080 could come up with is this where you say 22780 14:20:27,640 --> 14:20:32,480 four I or whatever variable you want to 22781 14:20:30,080 --> 14:20:34,360 do the counting in literally the 22782 14:20:32,480 --> 14:20:36,240 preposition and then you use square 22783 14:20:34,360 --> 14:20:38,080 brackets here and we've used square 22784 14:20:36,240 --> 14:20:41,680 brackets before in the context of like 22785 14:20:38,080 --> 14:20:43,880 arrays and things like that and the 012 22786 14:20:41,680 --> 14:20:45,360 looks like an array in some sense even 22787 14:20:43,880 --> 14:20:47,200 though we've also seen arrays with curly 22788 14:20:45,360 --> 14:20:49,360 braces but these square brackets for now 22789 14:20:47,200 --> 14:20:51,520 denote a list python does not have 22790 14:20:49,360 --> 14:20:54,240 arrays an array is that contiguous chunk 22791 14:20:51,520 --> 14:20:56,840 of memory back to back to back that you 22792 14:20:54,240 --> 14:20:59,360 have to resize somehow by moving things 22793 14:20:56,840 --> 14:21:02,360 around in memory as per two weeks ago in 22794 14:20:59,360 --> 14:21:04,116 Python though you can just create a list 22795 14:21:02,360 --> 14:21:05,956 like this using square brackets and 22796 14:21:04,116 --> 14:21:08,116 better still as we'll see you can add or 22797 14:21:05,956 --> 14:21:11,000 even remove things from that list down 22798 14:21:08,116 --> 14:21:13,276 the road um this though is not going to 22799 14:21:11,000 --> 14:21:15,720 be very welld designed this will work 22800 14:21:13,276 --> 14:21:18,000 this will iterate in Python three 22801 14:21:15,720 --> 14:21:19,680 times but what might rub you the wrong 22802 14:21:18,000 --> 14:21:22,000 way about this design even if you've 22803 14:21:19,680 --> 14:21:25,200 never seen python before how does this 22804 14:21:22,000 --> 14:21:25,200 example not end well 22805 14:21:25,880 --> 14:21:30,116 yeah yeah like if you're making a large 22806 14:21:28,116 --> 14:21:31,840 list you have to type out each one of 22807 14:21:30,116 --> 14:21:34,240 these numbers like comma three comma 22808 14:21:31,840 --> 14:21:36,796 four comma 5 comma dot dot do 50 comma 22809 14:21:34,240 --> 14:21:38,360 dot dot dot 500 like surely that's not 22810 14:21:36,796 --> 14:21:40,240 the best solution to have all of these 22811 14:21:38,360 --> 14:21:42,680 numbers on the code on the screen 22812 14:21:40,240 --> 14:21:44,596 wrapping endlessly on the screen so in 22813 14:21:42,680 --> 14:21:46,640 Python another way to do this would be 22814 14:21:44,596 --> 14:21:48,880 to use a function called range which 22815 14:21:46,640 --> 14:21:51,840 technically is a data type unto itself 22816 14:21:48,880 --> 14:21:53,840 and this returns to you as many values 22817 14:21:51,840 --> 14:21:55,400 as you ask for it range takes some other 22818 14:21:53,840 --> 14:21:56,560 arguments as well but the simplest use 22819 14:21:55,400 --> 14:21:59,880 case here is if you want back the 22820 14:21:56,560 --> 14:22:02,080 numbers 0 1 and two a total of three 22821 14:21:59,880 --> 14:22:04,276 values you say hey python please give me 22822 14:22:02,080 --> 14:22:06,400 a range of three values and by default 22823 14:22:04,276 --> 14:22:09,000 they start at zero on up but this is 22824 14:22:06,400 --> 14:22:11,360 more efficient than it would be to 22825 14:22:09,000 --> 14:22:12,916 hardcode the entire list at once and the 22826 14:22:11,360 --> 14:22:14,160 best metaphor I could up with is 22827 14:22:12,916 --> 14:22:15,680 something like this like here for 22828 14:22:14,160 --> 14:22:17,480 instance is a deck of cards this is sort 22829 14:22:15,680 --> 14:22:20,560 of normal human size and there's 22830 14:22:17,480 --> 14:22:23,240 presumably 52 cards here so writing out 22831 14:22:20,560 --> 14:22:24,720 0 through 51 on code would be a little 22832 14:22:23,240 --> 14:22:27,080 ridiculous for the reasons you know it 22833 14:22:24,720 --> 14:22:29,080 would just be very unwieldy and ugly and 22834 14:22:27,080 --> 14:22:30,680 wrapping and all of that it would be the 22835 14:22:29,080 --> 14:22:32,560 phys it would be the virtual equivalent 22836 14:22:30,680 --> 14:22:34,436 of me like handing you all of these 22837 14:22:32,560 --> 14:22:35,756 cards at once to just deal with and 22838 14:22:34,436 --> 14:22:37,400 right you know they're not that big but 22839 14:22:35,756 --> 14:22:39,160 like it's a lot of cards to hold on to 22840 14:22:37,400 --> 14:22:41,400 it requires a lot of memory or physical 22841 14:22:39,160 --> 14:22:43,436 storage if you will what range does 22842 14:22:41,400 --> 14:22:46,756 metaphorically is if you ask me for 22843 14:22:43,436 --> 14:22:49,520 three cards I hand you them one at a 22844 14:22:46,756 --> 14:22:51,560 time like this so that at any point in 22845 14:22:49,520 --> 14:22:54,080 time you only have one number in the 22846 14:22:51,560 --> 14:22:56,040 computer's memory until you're handed 22847 14:22:54,080 --> 14:22:57,480 the next the alternative the previous 22848 14:22:56,040 --> 14:22:59,916 version would be to hand me all three 22849 14:22:57,480 --> 14:23:01,916 cards at once or all 52 cards at once 22850 14:22:59,916 --> 14:23:04,360 but in this case range is just way more 22851 14:23:01,916 --> 14:23:05,756 efficient you can do range of a thousand 22852 14:23:04,360 --> 14:23:07,756 that's not going to give you a list of a 22853 14:23:05,756 --> 14:23:10,520 thousand values all at once it's going 22854 14:23:07,756 --> 14:23:13,240 to give you a thousand values one at a 22855 14:23:10,520 --> 14:23:16,720 time reducing memory sign signicantly in 22856 14:23:13,240 --> 14:23:18,640 the computer itself all right so besides 22857 14:23:16,720 --> 14:23:20,680 this what about doing something forever 22858 14:23:18,640 --> 14:23:22,320 in scratch well we could do this 22859 14:23:20,680 --> 14:23:24,796 literally with a forever block which 22860 14:23:22,320 --> 14:23:27,200 didn't quite exist in C in C we had had 22861 14:23:24,796 --> 14:23:30,160 to hack it together by saying while true 22862 14:23:27,200 --> 14:23:32,880 because true is by definition TR always 22863 14:23:30,160 --> 14:23:35,360 true so this just in uh deliberately 22864 14:23:32,880 --> 14:23:37,200 induces an infinite Loop for us in 22865 14:23:35,360 --> 14:23:39,360 Python the logic's going to be almost 22866 14:23:37,200 --> 14:23:41,360 the same and infinite Loops in Python 22867 14:23:39,360 --> 14:23:43,000 tend to actually be even more common 22868 14:23:41,360 --> 14:23:45,560 because you can always break out of them 22869 14:23:43,000 --> 14:23:47,796 as you couldn't see in Python it looks 22870 14:23:45,560 --> 14:23:50,520 like this and this is slightly more 22871 14:23:47,796 --> 14:23:52,756 subtle but gone are the curly braces 22872 14:23:50,520 --> 14:23:54,360 gone or the parentheses but ever so 22873 14:23:52,756 --> 14:23:56,796 slight difference 22874 14:23:54,360 --> 14:23:58,520 toal capital T for true and it's going 22875 14:23:56,796 --> 14:23:59,640 to be a capital f for false stupid 22876 14:23:58,520 --> 14:24:00,956 little differences eventually you're 22877 14:23:59,640 --> 14:24:02,276 going to mistype one or the other but 22878 14:24:00,956 --> 14:24:04,400 these are the kinds of things to keep an 22879 14:24:02,276 --> 14:24:06,956 eye out and to start recognizing in your 22880 14:24:04,400 --> 14:24:09,520 mind's eye when you read code questions 22881 14:24:06,956 --> 14:24:11,080 now on any of these building 22882 14:24:09,520 --> 14:24:14,080 blocks 22883 14:24:11,080 --> 14:24:14,080 yeah 22884 14:24:14,916 --> 14:24:20,916 I in the for Loop was I re uh it was set 22885 14:24:18,956 --> 14:24:23,320 to zero on the first iteration then one 22886 14:24:20,916 --> 14:24:25,596 on the next then two on the third and 22887 14:24:23,320 --> 14:24:28,956 the same thing for range it just doesn't 22888 14:24:25,596 --> 14:24:30,796 use up as much memory all at once other 22889 14:24:28,956 --> 14:24:33,436 questions now on any of these building 22890 14:24:30,796 --> 14:24:35,560 blocks of 22891 14:24:33,436 --> 14:24:37,436 python no all right well let's go ahead 22892 14:24:35,560 --> 14:24:39,840 and build something a little more than 22893 14:24:37,436 --> 14:24:42,080 hello let me propose that over here we 22894 14:24:39,840 --> 14:24:44,080 Implement maybe the the simplest of 22895 14:24:42,080 --> 14:24:47,400 calculators here so let me go back to vs 22896 14:24:44,080 --> 14:24:50,720 code here open my terminal uh window and 22897 14:24:47,400 --> 14:24:53,240 open up say a file called 22898 14:24:50,720 --> 14:24:55,000 calculator. and in calculator. we'll 22899 14:24:53,240 --> 14:24:56,480 have an opportunity to explore some of 22900 14:24:55,000 --> 14:24:58,480 these building blocks but we'll allow 22901 14:24:56,480 --> 14:25:00,276 things to escalate pretty quickly to 22902 14:24:58,480 --> 14:25:02,680 more interesting examples so that we can 22903 14:25:00,276 --> 14:25:04,080 do the same thing ultimately as well and 22904 14:25:02,680 --> 14:25:06,276 in fact let me go ahead and do this 22905 14:25:04,080 --> 14:25:08,200 moreover I've brought some code with me 22906 14:25:06,276 --> 14:25:11,160 in advance uh for instance something 22907 14:25:08,200 --> 14:25:12,640 called calculator 0. C from the first 22908 14:25:11,160 --> 14:25:14,596 week of C 22909 14:25:12,640 --> 14:25:16,720 and let me go ahead and split my window 22910 14:25:14,596 --> 14:25:19,560 here in fact so that I can now do 22911 14:25:16,720 --> 14:25:23,320 something like uh this let me move this 22912 14:25:19,560 --> 14:25:24,596 over here here calculator. Pi so now I 22913 14:25:23,320 --> 14:25:27,400 have on the left of my screen 22914 14:25:24,596 --> 14:25:28,520 calculator. C or calculator z.c because 22915 14:25:27,400 --> 14:25:30,640 that's the first version I made and 22916 14:25:28,520 --> 14:25:32,640 calculator. Pi on the right let me go 22917 14:25:30,640 --> 14:25:34,240 ahead and Implement really the same idea 22918 14:25:32,640 --> 14:25:37,480 here so on the right hand side the 22919 14:25:34,240 --> 14:25:40,596 analog of including cs50.h would be from 22920 14:25:37,480 --> 14:25:42,276 cs50 import get int if I want to indeed 22921 14:25:40,596 --> 14:25:43,796 use this function now I'm going to go 22922 14:25:42,276 --> 14:25:45,480 ahead and give myself a variable X 22923 14:25:43,796 --> 14:25:47,000 without defining its type I'm going to 22924 14:25:45,480 --> 14:25:49,956 use this get int function and I'm going 22925 14:25:47,000 --> 14:25:51,240 to prompt the user for X just like in C 22926 14:25:49,956 --> 14:25:54,320 I'm then going to go ahead and prompt 22927 14:25:51,240 --> 14:25:56,400 the user for another int like y here 22928 14:25:54,320 --> 14:25:59,320 just like in C and at the very end I'm 22929 14:25:56,400 --> 14:26:01,640 going to go ahead and do print X Plus Y 22930 14:25:59,320 --> 14:26:03,520 and that's it now granted I have some 22931 14:26:01,640 --> 14:26:05,080 comments in my C version of the code 22932 14:26:03,520 --> 14:26:07,040 just to remind you of what each line is 22933 14:26:05,080 --> 14:26:08,956 doing but I've still distilled this into 22934 14:26:07,040 --> 14:26:10,720 like six lines or really four if I get 22935 14:26:08,956 --> 14:26:13,116 rid of the blank line so it's already 22936 14:26:10,720 --> 14:26:15,040 perhaps a bit tighter here here but 22937 14:26:13,116 --> 14:26:17,116 there's also it's tighter because 22938 14:26:15,040 --> 14:26:20,360 something really important historically 22939 14:26:17,116 --> 14:26:22,080 is missing what did I seem to Omit 22940 14:26:20,360 --> 14:26:24,596 altogether that we haven't really 22941 14:26:22,080 --> 14:26:27,480 highlighted yet 22942 14:26:24,596 --> 14:26:29,560 yeah yeah the main function is gone and 22943 14:26:27,480 --> 14:26:31,116 in fact maybe you took for granted that 22944 14:26:29,560 --> 14:26:32,916 it just worked a moment ago when I wrote 22945 14:26:31,116 --> 14:26:34,480 hello but I didn't have a main function 22946 14:26:32,916 --> 14:26:36,400 in hello either and this too is a 22947 14:26:34,480 --> 14:26:38,080 feature of python and a lot of other 22948 14:26:36,400 --> 14:26:39,880 languages as well instead of having to 22949 14:26:38,080 --> 14:26:41,080 adhere to these long-standing Traditions 22950 14:26:39,880 --> 14:26:42,720 if you just want to write code and get 22951 14:26:41,080 --> 14:26:44,720 something done fine just write code and 22952 14:26:42,720 --> 14:26:47,040 get something done without necessarily 22953 14:26:44,720 --> 14:26:49,880 all of the same boiler plate so whatever 22954 14:26:47,040 --> 14:26:51,956 is in your python file left indented if 22955 14:26:49,880 --> 14:26:53,880 you will by default is just going to be 22956 14:26:51,956 --> 14:26:56,240 the code that The Interpreter runs top 22957 14:26:53,880 --> 14:26:59,320 to bottom left to right well let me go 22958 14:26:56,240 --> 14:27:00,956 ahead now and run code like this let me 22959 14:26:59,320 --> 14:27:02,956 go ahead and open that back up my 22960 14:27:00,956 --> 14:27:05,360 terminal window run python of 22961 14:27:02,956 --> 14:27:07,640 calculator. piy and I'll do X is one y 22962 14:27:05,360 --> 14:27:09,596 is 2 and as you might expect it gives me 22963 14:27:07,640 --> 14:27:11,480 three slight aesthetic bug I put my 22964 14:27:09,596 --> 14:27:12,956 space in the wrong place here so that's 22965 14:27:11,480 --> 14:27:14,916 a new mistake let me fix that 22966 14:27:12,956 --> 14:27:17,360 aesthetically let me rerun python of 22967 14:27:14,916 --> 14:27:20,116 calculator. Pi type in one type in two 22968 14:27:17,360 --> 14:27:23,080 and voila there is now my same version 22969 14:27:20,116 --> 14:27:24,360 again but let me propose now that we get 22970 14:27:23,080 --> 14:27:26,160 rid of this training wheel we don't want 22971 14:27:24,360 --> 14:27:27,680 to keep taking one step forward and then 22972 14:27:26,160 --> 14:27:30,276 two steps back by adding these training 22973 14:27:27,680 --> 14:27:32,796 wheels so let me instead do this in my 22974 14:27:30,276 --> 14:27:34,956 version of calculator. suppose that we 22975 14:27:32,796 --> 14:27:38,160 take away already the training wheel 22976 14:27:34,956 --> 14:27:40,276 that is the cs50 library here and let me 22977 14:27:38,160 --> 14:27:42,640 instead then use just Python's built-in 22978 14:27:40,276 --> 14:27:45,116 function called input which literally 22979 14:27:42,640 --> 14:27:48,116 does just that it gets input from the 22980 14:27:45,116 --> 14:27:50,160 user and it stores it as before in X and 22981 14:27:48,116 --> 14:27:52,360 Y so this is not cs50 specific this is 22982 14:27:50,160 --> 14:27:54,080 real world Python Programming well let 22983 14:27:52,360 --> 14:27:57,436 me go ahead and run again python of 22984 14:27:54,080 --> 14:28:00,400 calculator. Pi and of course if x is one 22985 14:27:57,436 --> 14:28:03,116 and Y is 2 X + Y should of course still 22986 14:28:00,400 --> 14:28:06,360 be three 22987 14:28:03,116 --> 14:28:08,520 hm it's apparently 12 according to 22988 14:28:06,360 --> 14:28:10,720 python until cs50's Library gets 22989 14:28:08,520 --> 14:28:15,276 involved but does anyone want to 22990 14:28:10,720 --> 14:28:15,276 infer what's just went wrong 22991 14:28:17,160 --> 14:28:22,596 yeah exactly the input function by 22992 14:28:19,956 --> 14:28:24,240 Design always returns a string of text 22993 14:28:22,596 --> 14:28:25,916 after all that's what the human typed in 22994 14:28:24,240 --> 14:28:27,916 and even though yes I type the number 22995 14:28:25,916 --> 14:28:30,276 keys on the keyboard it's still coming 22996 14:28:27,916 --> 14:28:32,320 back is all text now maybe we should use 22997 14:28:30,276 --> 14:28:34,436 like a get in function well that doesn't 22998 14:28:32,320 --> 14:28:37,116 exist in Python all you can do is get 22999 14:28:34,436 --> 14:28:39,840 textual input a string from the user but 23000 14:28:37,116 --> 14:28:41,916 we can convert one to the other and so a 23001 14:28:39,840 --> 14:28:44,916 fix for this so that we don't accident 23002 14:28:41,916 --> 14:28:46,720 Al concatenate that is join X+ y 23003 14:28:44,916 --> 14:28:48,916 together would be to do something like 23004 14:28:46,720 --> 14:28:51,680 this let me go back to my python code 23005 14:28:48,916 --> 14:28:53,640 here and whereas in C we could 23006 14:28:51,680 --> 14:28:55,956 previously do type casting we could 23007 14:28:53,640 --> 14:28:57,916 convert one type to another that 23008 14:28:55,956 --> 14:28:59,756 generally wasn't the case when you were 23009 14:28:57,916 --> 14:29:02,040 doing something complex like a string to 23010 14:28:59,756 --> 14:29:04,360 an INT you could do a Char to an INT and 23011 14:29:02,040 --> 14:29:06,400 vice versa but for a string recall there 23012 14:29:04,360 --> 14:29:08,956 was a special function in the ctype 23013 14:29:06,400 --> 14:29:12,240 library called a to I like ask e to 23014 14:29:08,956 --> 14:29:13,956 integer that's uh that closest analog 23015 14:29:12,240 --> 14:29:16,276 here and in fact the way to do this in 23016 14:29:13,956 --> 14:29:18,480 Python would be to use a function called 23017 14:29:16,276 --> 14:29:20,276 int which indeed is the name of the data 23018 14:29:18,480 --> 14:29:23,436 type 2 even though I have not yet had to 23019 14:29:20,276 --> 14:29:26,160 type it and I can convert the output of 23020 14:29:23,436 --> 14:29:29,916 the input function automatically from a 23021 14:29:26,160 --> 14:29:32,080 string immediately to an INT and now if 23022 14:29:29,916 --> 14:29:34,276 I go back to my terminal window rerun 23023 14:29:32,080 --> 14:29:37,240 python of calculator. piy with one and 23024 14:29:34,276 --> 14:29:39,360 two for X and Y now I'm back in business 23025 14:29:37,240 --> 14:29:41,756 so that then is for instance what the 23026 14:29:39,360 --> 14:29:43,116 cs50 library does if temporarily this 23027 14:29:41,756 --> 14:29:45,240 week is it just deals with the 23028 14:29:43,116 --> 14:29:47,436 conversion for you and in fact bad 23029 14:29:45,240 --> 14:29:50,000 things could happen if I type the wrong 23030 14:29:47,436 --> 14:29:51,720 thing like dog or cat instead of a 23031 14:29:50,000 --> 14:29:53,720 number but we'll cross that bridge in 23032 14:29:51,720 --> 14:29:55,596 just a moment as well all right what if 23033 14:29:53,720 --> 14:29:57,116 we do something slightly different now 23034 14:29:55,596 --> 14:29:59,240 with our calculator instead of just 23035 14:29:57,116 --> 14:30:02,000 addition let me go ahead and do how 23036 14:29:59,240 --> 14:30:05,796 about uh div instead of addition let's 23037 14:30:02,000 --> 14:30:08,720 do division instead so Z equals x / y 23038 14:30:05,796 --> 14:30:10,480 thereby giving me a third variable Z let 23039 14:30:08,720 --> 14:30:13,680 me go ahead and run python of 23040 14:30:10,480 --> 14:30:17,080 calculator. piy again I'll type in one 23041 14:30:13,680 --> 14:30:19,436 I'll type in three this time and what 23042 14:30:17,080 --> 14:30:20,956 prog what problem do you think we're 23043 14:30:19,436 --> 14:30:23,720 about to 23044 14:30:20,956 --> 14:30:26,000 see or is it gone what happened when I 23045 14:30:23,720 --> 14:30:28,276 did this in C albeit with some slightly 23046 14:30:26,000 --> 14:30:31,276 more cryptic syntax when I divided one 23047 14:30:28,276 --> 14:30:35,080 number like one one divided by 23048 14:30:31,276 --> 14:30:38,040 three anyone recall 23049 14:30:35,080 --> 14:30:41,080 yeah yeah so it would round down to the 23050 14:30:38,040 --> 14:30:44,160 nearest integer whereby you experience 23051 14:30:41,080 --> 14:30:46,080 trunc so if you take an integer like one 23052 14:30:44,160 --> 14:30:48,720 you divide it by another integer like 23053 14:30:46,080 --> 14:30:53,160 three that technically should be 23054 14:30:48,720 --> 14:30:55,320 0.33333 infinitely long but in uh C 23055 14:30:53,160 --> 14:30:56,916 recall you truncate the value if you 23056 14:30:55,320 --> 14:30:58,520 divide an INT by an INT you get back an 23057 14:30:56,916 --> 14:31:00,680 INT which means you get only the integer 23058 14:30:58,520 --> 14:31:02,880 part which was the zero now python 23059 14:31:00,680 --> 14:31:05,000 actually handles this for us and avoids 23060 14:31:02,880 --> 14:31:07,596 the truncation but it leaves us still 23061 14:31:05,000 --> 14:31:10,680 with one other problem here which is 23062 14:31:07,596 --> 14:31:12,040 going to be for instance not necessarily 23063 14:31:10,680 --> 14:31:13,916 visible at a GL 23064 14:31:12,040 --> 14:31:16,320 this looks correct this has solved the 23065 14:31:13,916 --> 14:31:17,880 problem in C so truncation does not 23066 14:31:16,320 --> 14:31:19,680 happen the integers are automatically 23067 14:31:17,880 --> 14:31:22,200 converted to a float a floating point 23068 14:31:19,680 --> 14:31:23,320 value but what other problem did we trip 23069 14:31:22,200 --> 14:31:28,116 over back 23070 14:31:23,320 --> 14:31:28,116 in week uh 23071 14:31:28,160 --> 14:31:33,320 one what else got a little dicey when 23072 14:31:31,480 --> 14:31:36,596 dealing with simple 23073 14:31:33,320 --> 14:31:37,880 arithmetic anyone recall well the syntax 23074 14:31:36,596 --> 14:31:40,000 in Python's a little different but let 23075 14:31:37,880 --> 14:31:42,040 me go ahead and do this it turns out in 23076 14:31:40,000 --> 14:31:43,756 Python if you want to see more 23077 14:31:42,040 --> 14:31:45,480 significant digits than what I'm seeing 23078 14:31:43,756 --> 14:31:47,916 here by the default which is a dozen or 23079 14:31:45,480 --> 14:31:50,560 so let me go ahead and print out Z as 23080 14:31:47,916 --> 14:31:52,276 follows let me first print out a format 23081 14:31:50,560 --> 14:31:54,160 string because I want to format Z in an 23082 14:31:52,276 --> 14:31:56,116 interesting way and notice this would 23083 14:31:54,160 --> 14:31:57,596 have no effect on the difference this is 23084 14:31:56,116 --> 14:31:59,436 just a format string that for no 23085 14:31:57,596 --> 14:32:01,880 compelling reason at the moment is 23086 14:31:59,436 --> 14:32:04,276 interpolating z in those curly braces 23087 14:32:01,880 --> 14:32:06,080 using an F string or format string if I 23088 14:32:04,276 --> 14:32:08,276 run this again with one and three we'll 23089 14:32:06,080 --> 14:32:10,436 see indeed the exact same thing but when 23090 14:32:08,276 --> 14:32:12,360 you use an F string you indeed have the 23091 14:32:10,436 --> 14:32:15,200 ability to format that string more 23092 14:32:12,360 --> 14:32:17,200 precisely just like with percent F in 23093 14:32:15,200 --> 14:32:20,520 Python you could start to fine-tune how 23094 14:32:17,200 --> 14:32:23,360 many significant digits you see in uh p 23095 14:32:20,520 --> 14:32:24,720 in C rather in Python you can do the 23096 14:32:23,360 --> 14:32:26,276 same but the syntax is a little 23097 14:32:24,720 --> 14:32:29,560 different if you want the computer to 23098 14:32:26,276 --> 14:32:32,080 interpolate Z and show you 50 23099 14:32:29,560 --> 14:32:34,080 significant digits that is 50 numbers 23100 14:32:32,080 --> 14:32:36,160 after the decimal point syntax is 23101 14:32:34,080 --> 14:32:37,840 similar to C but it's a little different 23102 14:32:36,160 --> 14:32:40,956 you literally put a colon after the 23103 14:32:37,840 --> 14:32:43,116 variable's name 50 means show me the 23104 14:32:40,956 --> 14:32:45,276 decimal point and then 50 digits to the 23105 14:32:43,116 --> 14:32:47,680 right and the F just indicates please 23106 14:32:45,276 --> 14:32:50,080 treat this as a floating point value so 23107 14:32:47,680 --> 14:32:54,320 now if I rerun python of calculator. Pi 23108 14:32:50,080 --> 14:32:56,276 divide 1 by 3 unfortunately python has 23109 14:32:54,320 --> 14:32:58,756 not solved all of the world's problems 23110 14:32:56,276 --> 14:33:00,956 for us this again was an example of 23111 14:32:58,756 --> 14:33:03,000 floating point in Precision so that 23112 14:33:00,956 --> 14:33:04,200 problem is still latent so just because 23113 14:33:03,000 --> 14:33:05,436 the world has advanced doesn't 23114 14:33:04,200 --> 14:33:07,480 necessarily mean that all of our 23115 14:33:05,436 --> 14:33:09,840 problems from C have gone away there are 23116 14:33:07,480 --> 14:33:12,000 solutions using third-party libraries 23117 14:33:09,840 --> 14:33:13,956 for scientific calculations and the like 23118 14:33:12,000 --> 14:33:17,596 but out of the box floating point in 23119 14:33:13,956 --> 14:33:20,640 Precision is still an issue meanwhile 23120 14:33:17,596 --> 14:33:22,480 there was one other problem in C that we 23121 14:33:20,640 --> 14:33:25,040 ran into involving numbers and that was 23122 14:33:22,480 --> 14:33:27,360 this integer overflow recall that an 23123 14:33:25,040 --> 14:33:28,956 integer in C only took up what like 32 23124 14:33:27,360 --> 14:33:30,596 bits typically which meant you could 23125 14:33:28,956 --> 14:33:31,956 count as high as four billion or maybe 23126 14:33:30,596 --> 14:33:34,116 if you're doing positive and negatives 23127 14:33:31,956 --> 14:33:35,400 as high as two billion after which weird 23128 14:33:34,116 --> 14:33:37,320 things would happen the number would go 23129 14:33:35,400 --> 14:33:39,840 to zero or negative or just it would 23130 14:33:37,320 --> 14:33:41,400 overflow or wrap back around well 23131 14:33:39,840 --> 14:33:44,400 wonderfully in Python they did it at 23132 14:33:41,400 --> 14:33:46,596 least address this whereby you can count 23133 14:33:44,400 --> 14:33:48,796 as high as you want and python will just 23134 14:33:46,596 --> 14:33:51,276 use more and more and more and more bits 23135 14:33:48,796 --> 14:33:54,040 and bites to store really big numbers so 23136 14:33:51,276 --> 14:33:57,240 integer overflow is not a thing with 23137 14:33:54,040 --> 14:33:59,200 that said python is limited to how many 23138 14:33:57,240 --> 14:34:01,400 digits it will show you on the screen at 23139 14:33:59,200 --> 14:34:04,320 once as a string but mathematically your 23140 14:34:01,400 --> 14:34:06,796 math will be correct now so we've taken 23141 14:34:04,320 --> 14:34:08,880 a couple steps forward One Step sideways 23142 14:34:06,796 --> 14:34:11,320 but indeed we've solved some of our 23143 14:34:08,880 --> 14:34:16,320 problems here all right questions now 23144 14:34:11,320 --> 14:34:17,956 now on any of these examples thus 23145 14:34:16,320 --> 14:34:21,080 far 23146 14:34:17,956 --> 14:34:22,276 question all right well how about uh how 23147 14:34:21,080 --> 14:34:25,560 about another problem that we 23148 14:34:22,276 --> 14:34:27,240 encountered in C let's revisit it here 23149 14:34:25,560 --> 14:34:28,880 in python as well so let me go ahead and 23150 14:34:27,240 --> 14:34:33,240 on the left hand side here let me open 23151 14:34:28,880 --> 14:34:37,680 up a file called say compare let's see 23152 14:34:33,240 --> 14:34:39,360 uh how about a file called compare 3. C 23153 14:34:37,680 --> 14:34:41,200 on the left and let me go ahead and 23154 14:34:39,360 --> 14:34:43,276 create a new file on the right called 23155 14:34:41,200 --> 14:34:45,756 compare. Pi because recall that bad 23156 14:34:43,276 --> 14:34:48,360 things happened when we needed to 23157 14:34:45,756 --> 14:34:50,436 compare two values in C so on the left 23158 14:34:48,360 --> 14:34:53,000 here is a reminder of what we once did 23159 14:34:50,436 --> 14:34:55,276 in C whereby if we want to compare 23160 14:34:53,000 --> 14:34:58,400 values we can get an INT in C stored in 23161 14:34:55,276 --> 14:35:00,796 x a get int in C stored in y we then 23162 14:34:58,400 --> 14:35:03,320 have our familiar conditional logic here 23163 14:35:00,796 --> 14:35:05,400 just printing out if x is less than y or 23164 14:35:03,320 --> 14:35:08,720 not well we can certainly do the same 23165 14:35:05,400 --> 14:35:10,596 thing ultimately in Python by using some 23166 14:35:08,720 --> 14:35:12,640 fairly familiar syntax and let's just 23167 14:35:10,596 --> 14:35:16,680 demonstrate this one quickly let me go 23168 14:35:12,640 --> 14:35:18,880 over here too I'll do from cs50 import 23169 14:35:16,680 --> 14:35:21,596 uh get int even though I could do this 23170 14:35:18,880 --> 14:35:23,436 instead with the input function itself x 23171 14:35:21,596 --> 14:35:26,080 equals get int and I'll prompt the user 23172 14:35:23,436 --> 14:35:28,436 for that y equals get int and I'll 23173 14:35:26,080 --> 14:35:29,756 prompt the user for that after that 23174 14:35:28,436 --> 14:35:32,320 recall that I can say without 23175 14:35:29,756 --> 14:35:35,560 parentheses if x is less than y then 23176 14:35:32,320 --> 14:35:38,640 print out without the F uh X is less 23177 14:35:35,560 --> 14:35:42,796 than y then I can go ahead and say else 23178 14:35:38,640 --> 14:35:46,240 if x is greater than y i can print out 23179 14:35:42,796 --> 14:35:47,880 uh quote unquote X is greater than y if 23180 14:35:46,240 --> 14:35:49,360 you'd like to interject now what did I 23181 14:35:47,880 --> 14:35:55,400 screw 23182 14:35:49,360 --> 14:35:57,796 up anyone yeah L if right so L if L if x 23183 14:35:55,400 --> 14:36:01,276 is greater than y else this part's the 23184 14:35:57,796 --> 14:36:03,240 same print X is equal to Y so there's 23185 14:36:01,276 --> 14:36:05,116 not all that much new there's no New 23186 14:36:03,240 --> 14:36:06,480 Logic going on here but at least 23187 14:36:05,116 --> 14:36:08,160 syntactically it's a little cleaner 23188 14:36:06,480 --> 14:36:10,320 indeed this program is only 11 lines 23189 14:36:08,160 --> 14:36:12,680 long albeit without any comments let me 23190 14:36:10,320 --> 14:36:15,596 go ahead and run python of compare. Pi 23191 14:36:12,680 --> 14:36:18,520 let's see is 1 less than two indeed 23192 14:36:15,596 --> 14:36:20,640 let's run it again is 2 less than 1 no 23193 14:36:18,520 --> 14:36:23,956 it's greater than and let's lastly type 23194 14:36:20,640 --> 14:36:26,040 in one and one twice X is equal to Y so 23195 14:36:23,956 --> 14:36:27,640 we've got a pretty side by side onetoone 23196 14:36:26,040 --> 14:36:30,160 conversion here let's do something a 23197 14:36:27,640 --> 14:36:32,480 little more interesting then and see how 23198 14:36:30,160 --> 14:36:34,480 about I open instead something where we 23199 14:36:32,480 --> 14:36:37,956 actually compared for a purpose so if I 23200 14:36:34,480 --> 14:36:42,320 open up from last uh from earlier in the 23201 14:36:37,956 --> 14:36:44,596 course how about uh agree do C which 23202 14:36:42,320 --> 14:36:46,680 prompt the user to agree to something or 23203 14:36:44,596 --> 14:36:49,116 not and let me code up a new version 23204 14:36:46,680 --> 14:36:51,320 here called agree. pi and I'll do this 23205 14:36:49,116 --> 14:36:53,840 on the right hand side with agree. pi 23206 14:36:51,320 --> 14:36:55,560 but on gree do c on the left notice that 23207 14:36:53,840 --> 14:36:59,400 this is how we did this sort of yes no 23208 14:36:55,560 --> 14:37:02,320 thing in C we compared c a character 23209 14:36:59,400 --> 14:37:04,276 equal to single quotes y or equal to 23210 14:37:02,320 --> 14:37:06,640 single quotes little Y and then the same 23211 14:37:04,276 --> 14:37:07,840 thing for n now in Python this one's 23212 14:37:06,640 --> 14:37:09,680 actually going to be a little bit 23213 14:37:07,840 --> 14:37:12,436 different here let me go ahead and in 23214 14:37:09,680 --> 14:37:14,796 the python version of this let me do 23215 14:37:12,436 --> 14:37:16,720 something like this uh we'll use get 23216 14:37:14,796 --> 14:37:19,720 string uh actually no we'll just use 23217 14:37:16,720 --> 14:37:22,916 input in this case so let's do uh s 23218 14:37:19,720 --> 14:37:25,840 equals input and we'll ask the user the 23219 14:37:22,916 --> 14:37:29,796 same thing do you agree question mark 23220 14:37:25,840 --> 14:37:33,000 then let's go ahead and say if s equals 23221 14:37:29,796 --> 14:37:36,080 equals how about uh 23222 14:37:33,000 --> 14:37:38,276 y huh how do I do this well a few things 23223 14:37:36,080 --> 14:37:40,000 turns out I'm going to do this s equals 23224 14:37:38,276 --> 14:37:44,000 equals little y then I'm going to go 23225 14:37:40,000 --> 14:37:46,640 ahead and print out a agreed and L if s 23226 14:37:44,000 --> 14:37:48,680 equals equals capital N or S equals 23227 14:37:46,640 --> 14:37:51,160 equals lowercase n I'm going to go ahead 23228 14:37:48,680 --> 14:37:52,956 and print out not agreed and I claimed 23229 14:37:51,160 --> 14:37:55,720 for the moment that this is identical 23230 14:37:52,956 --> 14:37:58,080 now to the program on the right the 23231 14:37:55,720 --> 14:37:59,840 program on the left in C but what's 23232 14:37:58,080 --> 14:38:01,956 different so we're still doing the same 23233 14:37:59,840 --> 14:38:04,160 kind of logic these equal equals for 23234 14:38:01,956 --> 14:38:05,840 comparing for equality but notice that 23235 14:38:04,160 --> 14:38:07,400 nicely enough python got rid of the two 23236 14:38:05,840 --> 14:38:09,200 vertical bars and it's just literally 23237 14:38:07,400 --> 14:38:11,436 the word or if you recall seeing 23238 14:38:09,200 --> 14:38:13,956 Ampersand Ampersand to express a logical 23239 14:38:11,436 --> 14:38:16,360 and in C you can just write literally 23240 14:38:13,956 --> 14:38:18,640 the word and and so here's a hint of why 23241 14:38:16,360 --> 14:38:20,160 python tends to be pretty popular people 23242 14:38:18,640 --> 14:38:21,680 just like that it's a little closer to 23243 14:38:20,160 --> 14:38:24,880 English there's a little less of the 23244 14:38:21,680 --> 14:38:27,200 cryptic syntax here now this is correct 23245 14:38:24,880 --> 14:38:29,596 as this code will now work but I've also 23246 14:38:27,200 --> 14:38:32,040 used double quotes instead of single 23247 14:38:29,596 --> 14:38:34,520 quotes and I also omitted a few minutes 23248 14:38:32,040 --> 14:38:37,320 ago for my list of data types in Python 23249 14:38:34,520 --> 14:38:39,796 the word Char in Python there are no 23250 14:38:37,320 --> 14:38:41,400 chars there are no individual characters 23251 14:38:39,796 --> 14:38:43,916 if you want to manipulate individual 23252 14:38:41,400 --> 14:38:47,240 character you use a string that is to 23253 14:38:43,916 --> 14:38:49,560 say a stir of size one now in Python you 23254 14:38:47,240 --> 14:38:51,000 can use single quotes or double quotes 23255 14:38:49,560 --> 14:38:52,840 I'm deliberately using double quotes 23256 14:38:51,000 --> 14:38:55,560 everywhere just for consistency with how 23257 14:38:52,840 --> 14:38:57,320 we treat strings in C it's pretty common 23258 14:38:55,560 --> 14:38:58,840 though to use single quotes instead if 23259 14:38:57,320 --> 14:39:00,796 only because on most keyboards you don't 23260 14:38:58,840 --> 14:39:02,080 have to hold the shift key anymore I 23261 14:39:00,796 --> 14:39:03,520 mean humans have really started to 23262 14:39:02,080 --> 14:39:05,640 optimize just how quickly they want to 23263 14:39:03,520 --> 14:39:07,560 be able to code so using a single quote 23264 14:39:05,640 --> 14:39:09,560 tends to be pretty popular in Python and 23265 14:39:07,560 --> 14:39:12,880 other languages as well they are 23266 14:39:09,560 --> 14:39:14,480 fundamentally the same uh single or 23267 14:39:12,880 --> 14:39:17,880 double unlike in C where they have 23268 14:39:14,480 --> 14:39:19,480 meaning so this is correct I claim and 23269 14:39:17,880 --> 14:39:21,756 in fact let me run this real quick I'll 23270 14:39:19,480 --> 14:39:23,320 open up my terminal window here let me 23271 14:39:21,756 --> 14:39:26,720 get rid of the version and see run 23272 14:39:23,320 --> 14:39:27,956 python of agree. piy and I'll type in y 23273 14:39:26,720 --> 14:39:29,640 okay I'll run it again and type in 23274 14:39:27,956 --> 14:39:32,040 little Y and I'll stipulate it's going 23275 14:39:29,640 --> 14:39:34,200 to work for no as well but this isn't 23276 14:39:32,040 --> 14:39:35,840 necessarily the only way we can do this 23277 14:39:34,200 --> 14:39:39,720 there are other ways to implement the 23278 14:39:35,840 --> 14:39:42,840 same idea and in fact I can go about 23279 14:39:39,720 --> 14:39:46,720 doing this this instead let me go back 23280 14:39:42,840 --> 14:39:49,520 up to my code here and we saw a hint of 23281 14:39:46,720 --> 14:39:51,200 this earlier we know that lists exist in 23282 14:39:49,520 --> 14:39:53,480 Python and you can create them just by 23283 14:39:51,200 --> 14:39:55,040 using square brackets so what if I 23284 14:39:53,480 --> 14:39:58,796 simplify the code a little bit and just 23285 14:39:55,040 --> 14:40:02,720 say if s is in the following list of 23286 14:39:58,796 --> 14:40:04,840 values capital y or lowercase y it's not 23287 14:40:02,720 --> 14:40:06,276 all that different logically but it's a 23288 14:40:04,840 --> 14:40:10,720 little tighter it's a little more 23289 14:40:06,276 --> 14:40:13,200 compact so L ifs is in capital N or 23290 14:40:10,720 --> 14:40:14,756 lowercase n i can express that same idea 23291 14:40:13,200 --> 14:40:16,796 too so here again it's just getting a 23292 14:40:14,756 --> 14:40:18,040 little more pleasant to write code 23293 14:40:16,796 --> 14:40:19,400 there's less like hitting of the 23294 14:40:18,040 --> 14:40:21,596 keyboard you can express yourself a 23295 14:40:19,400 --> 14:40:24,796 little more succinctly and using the 23296 14:40:21,596 --> 14:40:27,916 keyword in Python will figure out how to 23297 14:40:24,796 --> 14:40:30,000 search the entire list for whatever the 23298 14:40:27,916 --> 14:40:32,160 value of s is and if it finds it it will 23299 14:40:30,000 --> 14:40:35,116 return true automatically else it will 23300 14:40:32,160 --> 14:40:38,796 return false so if I run agree. Pi again 23301 14:40:35,116 --> 14:40:41,796 and type in capital y or lowercase y 23302 14:40:38,796 --> 14:40:44,240 that still now works well I can typen 23303 14:40:41,796 --> 14:40:46,640 tighten this up further if I want to add 23304 14:40:44,240 --> 14:40:49,360 more features well what if I want to 23305 14:40:46,640 --> 14:40:54,240 support not just why Big Y and little y 23306 14:40:49,360 --> 14:40:56,080 but how about yes or yes or in case the 23307 14:40:54,240 --> 14:40:57,796 user's yelling or you know someone who 23308 14:40:56,080 --> 14:41:00,000 doesn't really isn't good with caps lock 23309 14:40:57,796 --> 14:41:03,116 types in yes wait a minute but it could 23310 14:41:00,000 --> 14:41:06,160 be weird like do we want to support this 23311 14:41:03,116 --> 14:41:08,040 or this I mean this this just gets 23312 14:41:06,160 --> 14:41:09,400 really tedious quickly combinatorically 23313 14:41:08,040 --> 14:41:11,916 if you consider all of these possible 23314 14:41:09,400 --> 14:41:13,596 permutations what would be smarter than 23315 14:41:11,916 --> 14:41:16,436 doing something like this if you want to 23316 14:41:13,596 --> 14:41:19,000 just be able to tolerate yes in any form 23317 14:41:16,436 --> 14:41:22,040 of capitalization like logically what 23318 14:41:19,000 --> 14:41:25,480 would be nice maybe whatever theut is 23319 14:41:22,040 --> 14:41:27,320 you justf over to all lower and then 23320 14:41:25,480 --> 14:41:29,200 exactly super common Paradigm why don't 23321 14:41:27,320 --> 14:41:31,276 we just force the users's input to all 23322 14:41:29,200 --> 14:41:33,240 lowercase or all uppercase doesn't 23323 14:41:31,276 --> 14:41:35,320 matter so long as we're self-consistent 23324 14:41:33,240 --> 14:41:37,080 and just compare against all uppercase 23325 14:41:35,320 --> 14:41:39,560 or all lowercase and that will get rid 23326 14:41:37,080 --> 14:41:42,116 of all of the possible permutations 23327 14:41:39,560 --> 14:41:43,480 otherwise now in C we might have done 23328 14:41:42,116 --> 14:41:46,520 something like this we might have 23329 14:41:43,480 --> 14:41:50,040 simplified this whole list and just said 23330 14:41:46,520 --> 14:41:54,000 let's say uh we'll do how about 23331 14:41:50,040 --> 14:41:55,880 lowercase so y or yes and we'll just 23332 14:41:54,000 --> 14:41:58,596 leave it at that but we need to force 23333 14:41:55,880 --> 14:42:00,840 now s to lowercase well in C we would 23334 14:41:58,596 --> 14:42:03,160 have used the ctype library we would 23335 14:42:00,840 --> 14:42:05,040 have done like two lower and called that 23336 14:42:03,160 --> 14:42:07,276 function passing it in although not 23337 14:42:05,040 --> 14:42:09,840 really cuz in C type those operate on 23338 14:42:07,276 --> 14:42:11,240 individual characters or chars not whole 23339 14:42:09,840 --> 14:42:12,916 strings we have actually didn't see a 23340 14:42:11,240 --> 14:42:16,720 function that could convert the whole 23341 14:42:12,916 --> 14:42:18,116 string in C to lowercase but in Python 23342 14:42:16,720 --> 14:42:21,276 we're going to benefit from some other 23343 14:42:18,116 --> 14:42:23,720 feature as well it turns out that python 23344 14:42:21,276 --> 14:42:24,880 supports what's called objectoriented 23345 14:42:23,720 --> 14:42:26,720 programming and we're only going to 23346 14:42:24,880 --> 14:42:28,276 scratch the surface of this in cs50 but 23347 14:42:26,720 --> 14:42:30,276 if you take a higher level course in 23348 14:42:28,276 --> 14:42:32,680 programming or CS you explore this as a 23349 14:42:30,276 --> 14:42:34,160 different Paradigm up until now in C 23350 14:42:32,680 --> 14:42:36,160 we've been focusing on what's called 23351 14:42:34,160 --> 14:42:38,000 really procedural programming you write 23352 14:42:36,160 --> 14:42:40,560 procedures you write functions top to 23353 14:42:38,000 --> 14:42:42,796 bottom uh left to right and when you 23354 14:42:40,560 --> 14:42:44,720 want to change some value we were in the 23355 14:42:42,796 --> 14:42:46,880 habit of using a procedure that is a 23356 14:42:44,720 --> 14:42:48,916 function you would pass something like a 23357 14:42:46,880 --> 14:42:50,680 variable into a function like to Upper 23358 14:42:48,916 --> 14:42:52,880 or to lower and it would do its thing 23359 14:42:50,680 --> 14:42:55,520 and hand you back a value well it turns 23360 14:42:52,880 --> 14:42:58,240 out that it would be nicer programming 23361 14:42:55,520 --> 14:43:00,480 wise if some data types just had 23362 14:42:58,240 --> 14:43:02,200 built-in functionality like why do we 23363 14:43:00,480 --> 14:43:03,796 have our variables over here and all of 23364 14:43:02,200 --> 14:43:05,520 our helper functions like two upper and 23365 14:43:03,796 --> 14:43:07,200 two lower over here such that we 23366 14:43:05,520 --> 14:43:09,240 constantly have to pass one into the 23367 14:43:07,200 --> 14:43:11,160 other it would be nice to sort of bake 23368 14:43:09,240 --> 14:43:14,040 into our data types buil-in 23369 14:43:11,160 --> 14:43:17,520 functionality so that you can change 23370 14:43:14,040 --> 14:43:19,080 variables using their own default be uh 23371 14:43:17,520 --> 14:43:20,640 buil-in functionality and so 23372 14:43:19,080 --> 14:43:23,916 object-oriented programming otherwise 23373 14:43:20,640 --> 14:43:26,480 known as oop is a technique whereby 23374 14:43:23,916 --> 14:43:31,116 certain types of values like a string 23375 14:43:26,480 --> 14:43:33,720 AKA stir not only have properties inside 23376 14:43:31,116 --> 14:43:37,360 of them attributes just like a struct in 23377 14:43:33,720 --> 14:43:40,560 C your data can also have functions 23378 14:43:37,360 --> 14:43:42,200 built into them as well so whereas in C 23379 14:43:40,560 --> 14:43:44,680 which is not objectoriented you have 23380 14:43:42,200 --> 14:43:46,436 strs and strs can only store data like a 23381 14:43:44,680 --> 14:43:49,956 name and a number when implementing a 23382 14:43:46,436 --> 14:43:52,116 person in Python you can for instance 23383 14:43:49,956 --> 14:43:54,796 have not just a structure otherwise 23384 14:43:52,116 --> 14:43:57,596 known as a class storing a name and a 23385 14:43:54,796 --> 14:44:00,240 number you can have a function like call 23386 14:43:57,596 --> 14:44:02,956 that person or email that person or 23387 14:44:00,240 --> 14:44:05,080 actual verbs or actions associated with 23388 14:44:02,956 --> 14:44:07,360 that piece of data now in the context of 23389 14:44:05,080 --> 14:44:09,640 strings it turns out that strings come 23390 14:44:07,360 --> 14:44:12,916 with a lot of useful functionality and 23391 14:44:09,640 --> 14:44:14,680 in fact this URL here which is in docs. 23392 14:44:12,916 --> 14:44:17,080 python.org which is the official 23393 14:44:14,680 --> 14:44:19,796 documentation for python you'll see a 23394 14:44:17,080 --> 14:44:22,040 whole list of methods that is functions 23395 14:44:19,796 --> 14:44:24,916 that come with strings that you can 23396 14:44:22,040 --> 14:44:27,040 actually use to modify their values and 23397 14:44:24,916 --> 14:44:28,916 what I mean by this is the following if 23398 14:44:27,040 --> 14:44:30,880 we go through the documentation poke 23399 14:44:28,916 --> 14:44:33,400 around it turns out that strings come 23400 14:44:30,880 --> 14:44:34,840 with a function called Lower and if you 23401 14:44:33,400 --> 14:44:36,596 want to use that function you just have 23402 14:44:34,840 --> 14:44:39,956 to use slightly different syntax than in 23403 14:44:36,596 --> 14:44:42,796 C you do not do to lower and you do not 23404 14:44:39,956 --> 14:44:46,320 say as I just did lower because this 23405 14:44:42,796 --> 14:44:48,596 function is built into s itself and just 23406 14:44:46,320 --> 14:44:51,116 like in C when you want to go inside of 23407 14:44:48,596 --> 14:44:53,560 a variable like a structure and access a 23408 14:44:51,116 --> 14:44:55,956 piece of data inside of it like name or 23409 14:44:53,560 --> 14:44:58,840 number when you also have functions 23410 14:44:55,956 --> 14:45:00,596 built into data types AKA methods a 23411 14:44:58,840 --> 14:45:04,480 method is just a function that is built 23412 14:45:00,596 --> 14:45:08,040 into a piece of data you can do s do 23413 14:45:04,480 --> 14:45:10,276 lower open pen closed pen in this case 23414 14:45:08,040 --> 14:45:16,520 and I can do this down here as well if 23415 14:45:10,276 --> 14:45:19,040 if s do lower in quote unquote uh n or 23416 14:45:16,520 --> 14:45:20,756 no the whole thing I can force this 23417 14:45:19,040 --> 14:45:21,840 whole thing to lowercase so the only 23418 14:45:20,756 --> 14:45:23,640 difference here now is in 23419 14:45:21,840 --> 14:45:25,560 object-oriented programming instead of 23420 14:45:23,640 --> 14:45:28,240 constantly passing a value into a 23421 14:45:25,560 --> 14:45:31,596 function you just access a function 23422 14:45:28,240 --> 14:45:33,400 that's inside of the value it just works 23423 14:45:31,596 --> 14:45:34,880 because of how the language itself is 23424 14:45:33,400 --> 14:45:36,116 defined and the only way you know that 23425 14:45:34,880 --> 14:45:38,840 these functions exist is the 23426 14:45:36,116 --> 14:45:43,720 documentation a Class A book a website 23427 14:45:38,840 --> 14:45:43,720 or the like questions now on this 23428 14:45:44,436 --> 14:45:48,436 technique all right I claim this is 23429 14:45:46,436 --> 14:45:50,796 correct now even though you've never 23430 14:45:48,436 --> 14:45:52,956 programmed most of you in Python before 23431 14:45:50,796 --> 14:45:55,160 not super well-designed there's an 23432 14:45:52,956 --> 14:45:58,240 subtle inefficiency now on lines three 23433 14:45:55,160 --> 14:46:01,916 and five together what's dumb about how 23434 14:45:58,240 --> 14:46:04,916 I've used lower might you 23435 14:46:01,916 --> 14:46:04,916 think 23436 14:46:06,160 --> 14:46:09,880 yeah yeah if you're going to use the 23437 14:46:08,116 --> 14:46:11,720 same function twice and ask the same 23438 14:46:09,880 --> 14:46:13,520 question expecting the same answer why 23439 14:46:11,720 --> 14:46:15,080 are you calling the function itself 23440 14:46:13,520 --> 14:46:16,680 twice maybe we should just store the 23441 14:46:15,080 --> 14:46:18,360 result in a variable so we could do this 23442 14:46:16,680 --> 14:46:19,596 in a couple of different ways we for 23443 14:46:18,360 --> 14:46:21,360 instance could go up here and create 23444 14:46:19,596 --> 14:46:24,400 another variable called T and set that 23445 14:46:21,360 --> 14:46:27,080 equal to s. lower and then we could just 23446 14:46:24,400 --> 14:46:28,880 change this to be T here but honestly I 23447 14:46:27,080 --> 14:46:30,756 don't think we technically need another 23448 14:46:28,880 --> 14:46:32,916 variable Al together here I could just 23449 14:46:30,756 --> 14:46:36,200 do something like this let's change the 23450 14:46:32,916 --> 14:46:38,436 value of s to be the lowercase version 23451 14:46:36,200 --> 14:46:40,200 thereof and so now I can quite simply 23452 14:46:38,436 --> 14:46:42,560 refer to S again and again like this 23453 14:46:40,200 --> 14:46:45,400 this reusing that same value now to be 23454 14:46:42,560 --> 14:46:47,276 sure I have now just lost the user's 23455 14:46:45,400 --> 14:46:49,400 original input and if I care about that 23456 14:46:47,276 --> 14:46:51,116 if they typed in all caps I have no idea 23457 14:46:49,400 --> 14:46:52,956 anymore so maybe I do want to use a 23458 14:46:51,116 --> 14:46:54,756 separate variable altogether but a 23459 14:46:52,956 --> 14:46:57,160 takeaway here too is that strings in 23460 14:46:54,756 --> 14:47:00,160 Python are technically what we'll call 23461 14:46:57,160 --> 14:47:02,756 immutable that is they cannot be changed 23462 14:47:00,160 --> 14:47:05,320 this was not true in C once we gave you 23463 14:47:02,756 --> 14:47:07,276 arrays in week two or memory in week 23464 14:47:05,320 --> 14:47:08,796 four you could go to town on a string 23465 14:47:07,276 --> 14:47:10,796 and change any of the characters you 23466 14:47:08,796 --> 14:47:12,680 want upper casing lower casing changing 23467 14:47:10,796 --> 14:47:16,680 it shortening it and so forth but in 23468 14:47:12,680 --> 14:47:19,040 this case uh this returns a copy of s 23469 14:47:16,680 --> 14:47:21,400 forced to lowercase it doesn't change 23470 14:47:19,040 --> 14:47:23,560 the original string that is the memory 23471 14:47:21,400 --> 14:47:25,560 the bytes in the computer's memory when 23472 14:47:23,560 --> 14:47:27,320 you assign it back to S you're 23473 14:47:25,560 --> 14:47:29,596 essentially forgetting about the old 23474 14:47:27,320 --> 14:47:31,040 version of s but because python does 23475 14:47:29,596 --> 14:47:33,000 memory management for you there's no 23476 14:47:31,040 --> 14:47:35,080 maloc there's no free python 23477 14:47:33,000 --> 14:47:37,720 automatically frees up the original 23478 14:47:35,080 --> 14:47:40,040 bytes like yees and hands them back to 23479 14:47:37,720 --> 14:47:43,276 the operating system for you all right 23480 14:47:40,040 --> 14:47:45,916 questions now on this 23481 14:47:43,276 --> 14:47:49,320 technique questions on 23482 14:47:45,916 --> 14:47:51,160 this in general I'll call out the python 23483 14:47:49,320 --> 14:47:52,560 documentation will start to be your 23484 14:47:51,160 --> 14:47:54,040 friend because in class we'll only 23485 14:47:52,560 --> 14:47:56,400 scratch the surface with some of these 23486 14:47:54,040 --> 14:47:57,560 things but in docs. python.org for 23487 14:47:56,400 --> 14:47:59,520 instance there's a whole reference of 23488 14:47:57,560 --> 14:48:01,360 all of the built-in functions that come 23489 14:47:59,520 --> 14:48:03,240 with the language as well as for 23490 14:48:01,360 --> 14:48:05,116 instance those with the string all right 23491 14:48:03,240 --> 14:48:06,720 well let me go ahead and before we take 23492 14:48:05,116 --> 14:48:09,000 a break let's go ahead and create 23493 14:48:06,720 --> 14:48:12,200 something a little familiar to based on 23494 14:48:09,000 --> 14:48:14,916 our week here in C let me propose that 23495 14:48:12,200 --> 14:48:17,000 we revisit those examples in involving 23496 14:48:14,916 --> 14:48:18,840 some meow so for instance when we had 23497 14:48:17,000 --> 14:48:20,956 our cat meow back in the first week and 23498 14:48:18,840 --> 14:48:23,200 then second in C we did something that 23499 14:48:20,956 --> 14:48:25,360 was a little stupid at first whereby we 23500 14:48:23,200 --> 14:48:27,720 created a file as I'll do here this time 23501 14:48:25,360 --> 14:48:30,276 called meow. p and if I want a cat to 23502 14:48:27,720 --> 14:48:33,116 meow three times I could run it once 23503 14:48:30,276 --> 14:48:35,360 like this little copy paste and now 23504 14:48:33,116 --> 14:48:37,160 python of meow. py and I'm done now 23505 14:48:35,360 --> 14:48:39,276 we've visited this example like two 23506 14:48:37,160 --> 14:48:41,480 times at least now in scratch it and see 23507 14:48:39,276 --> 14:48:43,636 it's correct I'll stipulate but what's 23508 14:48:41,480 --> 14:48:47,200 obviously poorly 23509 14:48:43,636 --> 14:48:49,520 designed what's the fault here 23510 14:48:47,200 --> 14:48:50,840 yeah it should just be a loop right like 23511 14:48:49,520 --> 14:48:52,520 why type it three times literally 23512 14:48:50,840 --> 14:48:54,160 copying and pasting is almost always a 23513 14:48:52,520 --> 14:48:55,520 bad thing except in C when you have the 23514 14:48:54,160 --> 14:48:57,276 function prototypes that you need to 23515 14:48:55,520 --> 14:48:59,160 borrow but in this case this is just 23516 14:48:57,276 --> 14:49:01,636 inefficient so what could we do better 23517 14:48:59,160 --> 14:49:03,116 here in Python well in Python we could 23518 14:49:01,636 --> 14:49:04,756 probably change this in a few different 23519 14:49:03,116 --> 14:49:07,200 ways we could borrow some of the syntax 23520 14:49:04,756 --> 14:49:09,000 we proposed in slide form earlier like 23521 14:49:07,200 --> 14:49:12,200 give me a variable called I set it to 23522 14:49:09,000 --> 14:49:14,000 zero no semicolon while I is less than 23523 14:49:12,200 --> 14:49:16,200 three if I want to do this three times I 23524 14:49:14,000 --> 14:49:19,200 can go ahead and print out meow and then 23525 14:49:16,200 --> 14:49:21,680 I can do i+ equals one and I think this 23526 14:49:19,200 --> 14:49:23,840 would do the trick python of meow. and 23527 14:49:21,680 --> 14:49:26,276 we're back in business already well if I 23528 14:49:23,840 --> 14:49:27,796 wanted to change this to a for Loop well 23529 14:49:26,276 --> 14:49:29,240 in Python it would be a little tighter 23530 14:49:27,796 --> 14:49:32,400 but this would not be the best approach 23531 14:49:29,240 --> 14:49:37,040 so for I in uh 23532 14:49:32,400 --> 14:49:39,160 012 I could just do print meow like this 23533 14:49:37,040 --> 14:49:41,680 and that too would get the job done but 23534 14:49:39,160 --> 14:49:43,480 to my to our discussion earlier this 23535 14:49:41,680 --> 14:49:45,080 would get stupid pretty quickly if you 23536 14:49:43,480 --> 14:49:46,636 had to keep enumerating all of these 23537 14:49:45,080 --> 14:49:50,200 values like what did we introduce 23538 14:49:46,636 --> 14:49:51,956 instead the the range function exactly 23539 14:49:50,200 --> 14:49:54,160 so that hands me back way more 23540 14:49:51,956 --> 14:49:55,916 efficiently just the values I want 23541 14:49:54,160 --> 14:49:58,000 indeed one at a time so even this if I 23542 14:49:55,916 --> 14:50:00,480 run it a third a third or fourth time 23543 14:49:58,000 --> 14:50:02,360 we've got the same result but now let's 23544 14:50:00,480 --> 14:50:03,880 transition to where we went with this 23545 14:50:02,360 --> 14:50:05,560 back in the day how can we start to 23546 14:50:03,880 --> 14:50:07,636 modularize this like just like it would 23547 14:50:05,560 --> 14:50:09,880 be nice I claimed if MIT had given us a 23548 14:50:07,636 --> 14:50:11,916 meow function wouldn't it be nice if 23549 14:50:09,880 --> 14:50:14,160 like python had given us a meow function 23550 14:50:11,916 --> 14:50:15,956 maybe less compelling in Python but how 23551 14:50:14,160 --> 14:50:17,560 can I build my own function well I did 23552 14:50:15,956 --> 14:50:19,796 this briefly with the spell checker 23553 14:50:17,560 --> 14:50:21,400 earlier but let me go ahead and propose 23554 14:50:19,796 --> 14:50:25,000 that we could Implement now our own 23555 14:50:21,400 --> 14:50:27,080 version of this in python as follows let 23556 14:50:25,000 --> 14:50:29,880 me go ahead and start fresh here and use 23557 14:50:27,080 --> 14:50:32,400 the keyword defa so this did not exist 23558 14:50:29,880 --> 14:50:34,276 in C you had the return value the 23559 14:50:32,400 --> 14:50:36,040 function name the arguments in Python 23560 14:50:34,276 --> 14:50:39,400 you literally say defa to define a 23561 14:50:36,040 --> 14:50:40,956 function you give it a name like meow 23562 14:50:39,400 --> 14:50:42,720 and now now I'm going to go ahead and in 23563 14:50:40,956 --> 14:50:44,840 this function just print out meow and 23564 14:50:42,720 --> 14:50:47,116 this lets me change it to anything else 23565 14:50:44,840 --> 14:50:51,080 I want in the future but for now it's an 23566 14:50:47,116 --> 14:50:52,796 abstraction and in fact I can uh move it 23567 14:50:51,080 --> 14:50:54,520 out of sight out of mind just going to 23568 14:50:52,796 --> 14:50:56,916 hit enter a bunch of times to pretend 23569 14:50:54,520 --> 14:50:59,276 like now it exists but I don't care how 23570 14:50:56,916 --> 14:51:01,956 it is implemented and up here now I can 23571 14:50:59,276 --> 14:51:04,596 do something like this 4 I in range of 23572 14:51:01,956 --> 14:51:06,720 three let me go ahead and not print meow 23573 14:51:04,596 --> 14:51:08,796 anymore let me just call meow and 23574 14:51:06,720 --> 14:51:11,796 tightening up my code further but I 23575 14:51:08,796 --> 14:51:13,400 think let's see python of meow. py this 23576 14:51:11,796 --> 14:51:15,560 is I think going to be the first time it 23577 14:51:13,400 --> 14:51:19,080 does not work 23578 14:51:15,560 --> 14:51:22,320 correctly okay so here we have sadly our 23579 14:51:19,080 --> 14:51:23,796 first python error and let's see the 23580 14:51:22,320 --> 14:51:25,880 syntax is going to be different from C 23581 14:51:23,796 --> 14:51:27,560 or clangs output traceback is like the 23582 14:51:25,880 --> 14:51:29,436 term of art here this is like a trace 23583 14:51:27,560 --> 14:51:31,360 back of all of the lines of code that 23584 14:51:29,436 --> 14:51:33,240 were just executed or really functions 23585 14:51:31,360 --> 14:51:34,840 you called the file name is 23586 14:51:33,240 --> 14:51:36,796 uninteresting this is like my codes 23587 14:51:34,840 --> 14:51:39,636 space specifically but the file name is 23588 14:51:36,796 --> 14:51:41,520 important here meow. py uh line two is 23589 14:51:39,636 --> 14:51:43,240 is the issue okay I didn't get very far 23590 14:51:41,520 --> 14:51:45,000 before I screwed up and then there's a 23591 14:51:43,240 --> 14:51:47,680 name error and you'll see in Python 23592 14:51:45,000 --> 14:51:49,956 there's typically these capitalized uh 23593 14:51:47,680 --> 14:51:51,400 keywords that hint at what the issue is 23594 14:51:49,956 --> 14:51:54,200 it's something related to names of 23595 14:51:51,400 --> 14:51:55,756 variables meow is not defined all right 23596 14:51:54,200 --> 14:51:57,436 you're programming python for the first 23597 14:51:55,756 --> 14:52:00,720 time you've screwed up you're following 23598 14:51:57,436 --> 14:52:04,116 some online tutorial you're seeing this 23599 14:52:00,720 --> 14:52:08,240 reason through it like why might meow 23600 14:52:04,116 --> 14:52:11,040 not be defined what can weer infer about 23601 14:52:08,240 --> 14:52:14,880 python how to troubleshoot 23602 14:52:11,040 --> 14:52:16,840 logically is it me ised after maybe is 23603 14:52:14,880 --> 14:52:18,756 it because meow is defined after you 23604 14:52:16,840 --> 14:52:21,200 know as smart as python seems to be Visa 23605 14:52:18,756 --> 14:52:22,956 VC they have some similar design 23606 14:52:21,200 --> 14:52:24,560 characteristics so let's try that so let 23607 14:52:22,956 --> 14:52:27,756 me scroll all the way back down to where 23608 14:52:24,560 --> 14:52:30,160 I move this earlier let me uh get rid of 23609 14:52:27,756 --> 14:52:32,360 it way down there I'll copy it to my 23610 14:52:30,160 --> 14:52:34,116 clipboard and let me just kind of hack 23611 14:52:32,360 --> 14:52:36,276 something together let me just put it up 23612 14:52:34,116 --> 14:52:38,160 here and let's see if this works so now 23613 14:52:36,276 --> 14:52:40,840 let me clear my terminal run python of 23614 14:52:38,160 --> 14:52:42,520 meow. okay we're back in business so 23615 14:52:40,840 --> 14:52:44,040 that was actually really good intuition 23616 14:52:42,520 --> 14:52:45,916 good debugging technique to sort of 23617 14:52:44,040 --> 14:52:47,200 reason through it now this is kind of 23618 14:52:45,916 --> 14:52:48,840 contradicting what I claimed back in 23619 14:52:47,200 --> 14:52:50,720 week one which was that you know the 23620 14:52:48,840 --> 14:52:52,080 main part of your program ideally should 23621 14:52:50,720 --> 14:52:53,636 just be at the top of the file like 23622 14:52:52,080 --> 14:52:55,276 don't make me look for it it's not a 23623 14:52:53,636 --> 14:52:57,720 huge deal with like a four-line program 23624 14:52:55,276 --> 14:52:59,480 but if you've got 40 lines 400 lines you 23625 14:52:57,720 --> 14:53:01,400 don't want like the juice juicy part of 23626 14:52:59,480 --> 14:53:03,636 your program to be way down here and all 23627 14:53:01,400 --> 14:53:05,720 of these functions way up here so it 23628 14:53:03,636 --> 14:53:07,956 would be nice maybe if we actually have 23629 14:53:05,720 --> 14:53:10,320 a main function and so it actually turns 23630 14:53:07,956 --> 14:53:12,436 out to be a convention in Python to 23631 14:53:10,320 --> 14:53:14,000 define a main function it's not a 23632 14:53:12,436 --> 14:53:16,756 special function that's automatically 23633 14:53:14,000 --> 14:53:17,956 called like in C but humans realized you 23634 14:53:16,756 --> 14:53:20,360 know what that was a pretty useful 23635 14:53:17,956 --> 14:53:22,596 feature let me Define a function called 23636 14:53:20,360 --> 14:53:24,596 main let me indent these lines 23637 14:53:22,596 --> 14:53:26,400 underneath it let me practice what I'm 23638 14:53:24,596 --> 14:53:29,080 preaching which is put the main code at 23639 14:53:26,400 --> 14:53:32,240 the top of the file and wonderfully in 23640 14:53:29,080 --> 14:53:34,040 Python now you do not need prototypes 23641 14:53:32,240 --> 14:53:35,880 there's none of that hackish copying and 23642 14:53:34,040 --> 14:53:37,276 pasting of the return type the name and 23643 14:53:35,880 --> 14:53:40,320 the arguments to a function like we 23644 14:53:37,276 --> 14:53:43,276 needed in C this is now 23645 14:53:40,320 --> 14:53:45,116 okay instead except for one Minor Detail 23646 14:53:43,276 --> 14:53:48,000 let me go ahead and run python of 23647 14:53:45,116 --> 14:53:50,636 meow. hopefully now I've solve this 23648 14:53:48,000 --> 14:53:53,436 problem by having a main function but 23649 14:53:50,636 --> 14:53:55,880 now nothing has happened all right even 23650 14:53:53,436 --> 14:53:59,116 if you've never programmed in Python 23651 14:53:55,880 --> 14:54:01,840 before What might explain this 23652 14:53:59,116 --> 14:54:03,796 behavior and how do I 23653 14:54:01,840 --> 14:54:05,636 fix again when you're off in the real 23654 14:54:03,796 --> 14:54:09,080 world learning some new language all you 23655 14:54:05,636 --> 14:54:11,400 have is deductive logic to debug yeah I 23656 14:54:09,080 --> 14:54:11,400 remember 23657 14:54:16,840 --> 14:54:20,160 right so the solution to be clear in C 23658 14:54:18,680 --> 14:54:21,680 was that we had to put the Prototype up 23659 14:54:20,160 --> 14:54:23,040 here otherwise we'd get an error message 23660 14:54:21,680 --> 14:54:25,756 in this case I'm actually not getting an 23661 14:54:23,040 --> 14:54:27,636 error message and indeed I'll claim that 23662 14:54:25,756 --> 14:54:28,956 you don't need the prototypes in Python 23663 14:54:27,636 --> 14:54:30,520 just not necessary because that was 23664 14:54:28,956 --> 14:54:33,520 annoying if nothing 23665 14:54:30,520 --> 14:54:35,080 else but what else might explain yeah 23666 14:54:33,520 --> 14:54:37,880 I'm 23667 14:54:35,080 --> 14:54:40,116 back yeah maybe you have to call Main 23668 14:54:37,880 --> 14:54:42,916 itself if main is not some some special 23669 14:54:40,116 --> 14:54:44,880 status in Python maybe just because it 23670 14:54:42,916 --> 14:54:47,240 exists isn't enough and indeed if you 23671 14:54:44,880 --> 14:54:49,000 want to call Maine the new convention is 23672 14:54:47,240 --> 14:54:50,956 actually going to be as the very last 23673 14:54:49,000 --> 14:54:53,240 line of your program typically to 23674 14:54:50,956 --> 14:54:55,116 literally call Main it's a little stupid 23675 14:54:53,240 --> 14:54:56,840 stupid looking but you know they made a 23676 14:54:55,116 --> 14:55:00,596 design decision and this is how now we 23677 14:54:56,840 --> 14:55:02,756 work around it python of meowy now we're 23678 14:55:00,596 --> 14:55:04,796 back in business but now logically why 23679 14:55:02,756 --> 14:55:07,680 does this work the way it does well in 23680 14:55:04,796 --> 14:55:09,596 this case top to bottom line one is 23681 14:55:07,680 --> 14:55:11,276 telling python to define a fun fun 23682 14:55:09,596 --> 14:55:12,956 called Main and then Define it as 23683 14:55:11,276 --> 14:55:15,520 follows lines two and three but it's not 23684 14:55:12,956 --> 14:55:17,240 calling main yet line six is telling 23685 14:55:15,520 --> 14:55:19,560 python how to define a function called 23686 14:55:17,240 --> 14:55:22,436 meow but it's not calling these lines 23687 14:55:19,560 --> 14:55:24,880 yet now line 10 you're telling python 23688 14:55:22,436 --> 14:55:26,520 call Main and at that point python has 23689 14:55:24,880 --> 14:55:28,840 been trained if you will to know what 23690 14:55:26,520 --> 14:55:31,596 main is on line one to know what meow is 23691 14:55:28,840 --> 14:55:34,436 on line six and so it's now perfectly 23692 14:55:31,596 --> 14:55:36,596 okay for Maine to be above meow because 23693 14:55:34,436 --> 14:55:39,040 you never called them yet you defined 23694 14:55:36,596 --> 14:55:41,840 defined and then you called and that's 23695 14:55:39,040 --> 14:55:45,200 the logic behind this any questions now 23696 14:55:41,840 --> 14:55:48,160 on the structure of this technique 23697 14:55:45,200 --> 14:55:49,560 here now let's do one more then recall 23698 14:55:48,160 --> 14:55:52,320 that the last thing we did in scratch 23699 14:55:49,560 --> 14:55:53,560 and in Python uh scratch and in C was to 23700 14:55:52,320 --> 14:55:55,840 actually 23701 14:55:53,560 --> 14:55:57,560 parameterize uh these same function so 23702 14:55:55,840 --> 14:55:59,040 suppose that you don't want Maine to be 23703 14:55:57,560 --> 14:56:00,796 responsible for the loop here you 23704 14:55:59,040 --> 14:56:02,756 instead want to very simply do something 23705 14:56:00,796 --> 14:56:04,956 like meow three times and be done with 23706 14:56:02,756 --> 14:56:06,880 it well in Python it's going to be 23707 14:56:04,956 --> 14:56:08,720 similar in spirit toy but again we don't 23708 14:56:06,880 --> 14:56:10,916 need to keep mentioning data types if 23709 14:56:08,720 --> 14:56:14,276 you want me now to take some argument 23710 14:56:10,916 --> 14:56:15,680 like a number n you can just specify n 23711 14:56:14,276 --> 14:56:17,000 as the name of that argument or you 23712 14:56:15,680 --> 14:56:18,596 could call it anything else of course 23713 14:56:17,000 --> 14:56:21,796 that you want you don't have to specify 23714 14:56:18,596 --> 14:56:23,796 int or anything else in your code now 23715 14:56:21,796 --> 14:56:27,080 inside of meow you can do something like 23716 14:56:23,796 --> 14:56:29,116 for I in let's say I definitely now 23717 14:56:27,080 --> 14:56:31,040 can't do this because like that would be 23718 14:56:29,116 --> 14:56:32,636 weird to start the list and end it with 23719 14:56:31,040 --> 14:56:36,116 n so if I can come back over here what's 23720 14:56:32,636 --> 14:56:40,000 the solution how can I do something n 23721 14:56:36,116 --> 14:56:42,796 times yeah using range so range is nice 23722 14:56:40,000 --> 14:56:45,436 cuz I can pass in now this variable n 23723 14:56:42,796 --> 14:56:48,160 and now I can meow whoops now I can 23724 14:56:45,436 --> 14:56:49,880 print out quote unquote meow so it's 23725 14:56:48,160 --> 14:56:51,360 almost the same as in scratch almost the 23726 14:56:49,880 --> 14:56:54,040 same as in C but it's a little simpler 23727 14:56:51,360 --> 14:56:58,276 and if now I run meow. P I'll have the 23728 14:56:54,040 --> 14:57:01,596 ability now to do this here as well all 23729 14:56:58,276 --> 14:57:03,240 right questions on any of this right now 23730 14:57:01,596 --> 14:57:05,040 we're sort of like taking the stroll 23731 14:57:03,240 --> 14:57:07,636 through week one we're going to 23732 14:57:05,040 --> 14:57:09,796 momentarily escalate things to look not 23733 14:57:07,636 --> 14:57:11,360 only at some of these basic but also 23734 14:57:09,796 --> 14:57:13,320 other features like we saw with face 23735 14:57:11,360 --> 14:57:15,276 recognition with the speller or the like 23736 14:57:13,320 --> 14:57:17,200 um because of how many of us are here we 23737 14:57:15,276 --> 14:57:18,400 have a huge amount of candy out in the 23738 14:57:17,200 --> 14:57:20,200 lobby so why don't we go ahead and take 23739 14:57:18,400 --> 14:57:21,956 a 10-minute break and we come back we'll 23740 14:57:20,200 --> 14:57:26,000 do even fancier more powerful things 23741 14:57:21,956 --> 14:57:27,916 with python in 10 all right so we are 23742 14:57:26,000 --> 14:57:29,636 back among our goals now are to 23743 14:57:27,916 --> 14:57:31,320 introduce a few more building blocks so 23744 14:57:29,636 --> 14:57:33,040 that we can solve more interesting 23745 14:57:31,320 --> 14:57:34,880 problems at the end much like those that 23746 14:57:33,040 --> 14:57:36,276 we began with you recall from a few 23747 14:57:34,880 --> 14:57:38,320 weeks ago we played with this sort of 23748 14:57:36,276 --> 14:57:40,756 two-dimensional Super Mario World and we 23749 14:57:38,320 --> 14:57:42,796 tried to print a vertical column of like 23750 14:57:40,756 --> 14:57:44,520 three or more bricks well let me propose 23751 14:57:42,796 --> 14:57:46,880 that we use this as an opportunity to 23752 14:57:44,520 --> 14:57:48,880 now Tinker with some of Python's more uh 23753 14:57:46,880 --> 14:57:50,796 useful more userfriendly functionality 23754 14:57:48,880 --> 14:57:54,000 as well so let me code a file called 23755 14:57:50,796 --> 14:57:55,436 mario. py and let's just print out like 23756 14:57:54,000 --> 14:57:57,480 that the equivalent of that vertical 23757 14:57:55,436 --> 14:58:00,276 column so it's of height three each one 23758 14:57:57,480 --> 14:58:02,560 is a hash so let's do for I in range of 23759 14:58:00,276 --> 14:58:05,796 three initially and let's just print out 23760 14:58:02,560 --> 14:58:07,956 a single hash and I think now python of 23761 14:58:05,796 --> 14:58:10,040 mario. py voila we're in business 23762 14:58:07,956 --> 14:58:12,320 printing out just that same pyramid 23763 14:58:10,040 --> 14:58:14,436 there or just that same column there 23764 14:58:12,320 --> 14:58:16,200 what if though we want to print a column 23765 14:58:14,436 --> 14:58:18,040 of like some variable height where the 23766 14:58:16,200 --> 14:58:20,436 user tells us how tall they want it to 23767 14:58:18,040 --> 14:58:24,520 be well let me go up here for instance 23768 14:58:20,436 --> 14:58:28,840 and instead how about we'll use um let's 23769 14:58:24,520 --> 14:58:31,720 do this how about uh from cs50 23770 14:58:28,840 --> 14:58:33,596 import how about the get in function as 23771 14:58:31,720 --> 14:58:36,240 before so it will deal with making sure 23772 14:58:33,596 --> 14:58:37,796 the user gives us an integer and now in 23773 14:58:36,240 --> 14:58:40,040 the past whenever we wanted to get a 23774 14:58:37,796 --> 14:58:42,276 number from a user we've actually 23775 14:58:40,040 --> 14:58:46,116 followed a certain Paradigm in fact if I 23776 14:58:42,276 --> 14:58:50,320 open up here for instance uh how about 23777 14:58:46,116 --> 14:58:54,436 Mario in how about Mario 1. C from a 23778 14:58:50,320 --> 14:58:57,240 while back you might recall that we had 23779 14:58:54,436 --> 14:58:59,040 code like this and we specifically use 23780 14:58:57,240 --> 14:59:01,040 the do while loop and see whenever we 23781 14:58:59,040 --> 14:59:02,916 want to like get something from the user 23782 14:59:01,040 --> 14:59:04,480 maybe again and again and again until 23783 14:59:02,916 --> 14:59:06,360 they cooperate at which point we finally 23784 14:59:04,480 --> 14:59:07,956 break out of the loop so it turns out 23785 14:59:06,360 --> 14:59:10,636 python does have while Loops does have 23786 14:59:07,956 --> 14:59:11,916 four Loops does not have do while loops 23787 14:59:10,636 --> 14:59:13,636 and yet pretty much anytime you've 23788 14:59:11,916 --> 14:59:15,916 gotten user input you've probably used 23789 14:59:13,636 --> 14:59:18,480 this Paradigm so it turns out that the 23790 14:59:15,916 --> 14:59:20,880 python equivalent of this is to do 23791 14:59:18,480 --> 14:59:22,680 similar in spirit but using only a while 23792 14:59:20,880 --> 14:59:24,520 loop and a common Paradigm in python as 23793 14:59:22,680 --> 14:59:26,756 I alluded earlier is to actually 23794 14:59:24,520 --> 14:59:29,796 deliberately induce an infinite Loop 23795 14:59:26,756 --> 14:59:32,040 while true capital T and then do what 23796 14:59:29,796 --> 14:59:34,840 you want to do like get an INT from the 23797 14:59:32,040 --> 14:59:38,160 user and prompt them for the height for 23798 14:59:34,840 --> 14:59:40,116 instance in question and then if you're 23799 14:59:38,160 --> 14:59:42,360 sure that the user has given you what 23800 14:59:40,116 --> 14:59:44,000 you want like n is greater than zero 23801 14:59:42,360 --> 14:59:45,956 which is what I want in this case cuz I 23802 14:59:44,000 --> 14:59:47,796 want a positive integer otherwise 23803 14:59:45,956 --> 14:59:49,636 there's nothing to print you literally 23804 14:59:47,796 --> 14:59:51,880 just break out of the loop and so we 23805 14:59:49,636 --> 14:59:54,080 could actually use this technique in C 23806 14:59:51,880 --> 14:59:56,360 it's just not really done in C you could 23807 14:59:54,080 --> 14:59:58,360 absolutely in C have done a while true 23808 14:59:56,360 --> 15:00:00,200 loop with the parentheses lowercase true 23809 14:59:58,360 --> 15:00:02,560 you could break out of it and so forth 23810 15:00:00,200 --> 15:00:04,400 but in Python this is like the python 23811 15:00:02,560 --> 15:00:06,880 way and this is actually a term of art 23812 15:00:04,400 --> 15:00:09,080 this way in Python is pythonic like this 23813 15:00:06,880 --> 15:00:10,840 is the way everyone does it quote 23814 15:00:09,080 --> 15:00:12,360 unquote doesn't mean you have to but 23815 15:00:10,840 --> 15:00:14,116 that's sort of the way like the cool 23816 15:00:12,360 --> 15:00:15,956 python programmers would Implement an 23817 15:00:14,116 --> 15:00:17,636 idea like this trying to do something 23818 15:00:15,956 --> 15:00:19,756 again and again and again until the user 23819 15:00:17,636 --> 15:00:21,756 actually cooperates but all we've done 23820 15:00:19,756 --> 15:00:24,360 is take away the do while loop but still 23821 15:00:21,756 --> 15:00:26,116 logically we can implement the same idea 23822 15:00:24,360 --> 15:00:29,720 now below this let me go ahead and just 23823 15:00:26,116 --> 15:00:31,276 print out for I in range of n this time 23824 15:00:29,720 --> 15:00:33,400 because I want it to be variable and not 23825 15:00:31,276 --> 15:00:35,480 three I can go ahead and print out the 23826 15:00:33,400 --> 15:00:38,080 hash let me go ahead and get rid of the 23827 15:00:35,480 --> 15:00:40,520 C version here open my terminal window 23828 15:00:38,080 --> 15:00:42,796 and I'll run again python of mario. py 23829 15:00:40,520 --> 15:00:44,400 I'll type in three and I get back those 23830 15:00:42,796 --> 15:00:47,560 three hashes but if I instead type in 23831 15:00:44,400 --> 15:00:49,160 four I now get four hashes instead so 23832 15:00:47,560 --> 15:00:50,880 the takeaway here is quite simply that 23833 15:00:49,160 --> 15:00:54,720 this would be the way for instance to 23834 15:00:50,880 --> 15:00:56,596 actually get back a value in Python that 23835 15:00:54,720 --> 15:00:58,596 is consistent with some parameter like 23836 15:00:56,596 --> 15:01:00,560 greater than zero how about this let's 23837 15:00:58,596 --> 15:01:02,636 actually uh practice what we preached a 23838 15:01:00,560 --> 15:01:04,720 moment ago with our meowing examples and 23839 15:01:02,636 --> 15:01:06,916 kind of factoring all this out let me go 23840 15:01:04,720 --> 15:01:09,240 ahead and Define a main function as 23841 15:01:06,916 --> 15:01:11,200 before let me go ahead and assume for 23842 15:01:09,240 --> 15:01:13,360 the moment that a get height function 23843 15:01:11,200 --> 15:01:15,276 exists which is not a thing in Python 23844 15:01:13,360 --> 15:01:16,400 I'm going to invent it in just a moment 23845 15:01:15,276 --> 15:01:19,636 and now I'm going to go ahead and do 23846 15:01:16,400 --> 15:01:21,756 something like this for I in the range 23847 15:01:19,636 --> 15:01:24,880 of that height well let's go ahead and 23848 15:01:21,756 --> 15:01:26,880 print out those hashes so I'm assuming 23849 15:01:24,880 --> 15:01:28,840 that get height exists let me go ahead 23850 15:01:26,880 --> 15:01:30,956 and Implement that abstraction so Define 23851 15:01:28,840 --> 15:01:32,596 a function now called get height it's 23852 15:01:30,956 --> 15:01:36,080 not going to take any arguments in this 23853 15:01:32,596 --> 15:01:38,040 design while true I can go ahead and do 23854 15:01:36,080 --> 15:01:40,720 the same thing as before assign a 23855 15:01:38,040 --> 15:01:43,000 variable n the return value of get int 23856 15:01:40,720 --> 15:01:46,360 prompting the user for that height and 23857 15:01:43,000 --> 15:01:51,040 then if n is greater than zero I can go 23858 15:01:46,360 --> 15:01:53,400 ahead and break but if I break here I 23859 15:01:51,040 --> 15:01:56,000 logically just like can see end up 23860 15:01:53,400 --> 15:01:58,520 executing below the loop in question but 23861 15:01:56,000 --> 15:02:01,276 there's nothing there but if I want get 23862 15:01:58,520 --> 15:02:04,840 height to return the height what should 23863 15:02:01,276 --> 15:02:04,840 I type here on line 14 23864 15:02:05,116 --> 15:02:10,796 logically what do I want to return to be 23865 15:02:07,680 --> 15:02:13,756 clear yeah so I actually want to return 23866 15:02:10,796 --> 15:02:16,480 n and here's another curiosity of python 23867 15:02:13,756 --> 15:02:19,360 Visa BC there doesn't seem to be an 23868 15:02:16,480 --> 15:02:20,916 issue of scope anymore right in C it was 23869 15:02:19,360 --> 15:02:22,360 super important to not only declare your 23870 15:02:20,916 --> 15:02:23,680 variables with the data types you also 23871 15:02:22,360 --> 15:02:26,320 had to be mindful of like where they 23872 15:02:23,680 --> 15:02:28,200 exist inside of those curly braces in 23873 15:02:26,320 --> 15:02:30,680 Python it turns out you can be a little 23874 15:02:28,200 --> 15:02:33,636 looser with things for better for worse 23875 15:02:30,680 --> 15:02:37,956 and so on line 11 if I create a variable 23876 15:02:33,636 --> 15:02:39,360 called n it exists on line 11 12 and 23877 15:02:37,956 --> 15:02:42,560 even 13 23878 15:02:39,360 --> 15:02:45,200 outside of the while loop so to be clear 23879 15:02:42,560 --> 15:02:47,756 in C with a while loop we would have 23880 15:02:45,200 --> 15:02:49,796 ordinarily had not the colon we would 23881 15:02:47,756 --> 15:02:51,560 have had the curly brace like here and 23882 15:02:49,796 --> 15:02:54,320 over here and a week ago I would have 23883 15:02:51,560 --> 15:02:56,116 claimed that in C N does not exist 23884 15:02:54,320 --> 15:02:57,720 outside of the while loop by nature of 23885 15:02:56,116 --> 15:03:00,320 those curly braces even though the curly 23886 15:02:57,720 --> 15:03:03,560 braces are gone python actually allows 23887 15:03:00,320 --> 15:03:06,796 you to use a variable anytime after you 23888 15:03:03,560 --> 15:03:09,160 have assigned it a value so slightly 23889 15:03:06,796 --> 15:03:11,320 more powerful as such how however I can 23890 15:03:09,160 --> 15:03:13,400 tighten this up a little bit logically 23891 15:03:11,320 --> 15:03:15,720 and this is true in C I don't really 23892 15:03:13,400 --> 15:03:18,956 need to break out of the loop by using 23893 15:03:15,720 --> 15:03:21,240 break recall that or know that I can 23894 15:03:18,956 --> 15:03:23,520 actually once I'm ready to go I can just 23895 15:03:21,240 --> 15:03:25,080 return the value I care about even 23896 15:03:23,520 --> 15:03:26,880 inside of the loop and that will have 23897 15:03:25,080 --> 15:03:29,160 the side effect of breaking me out of 23898 15:03:26,880 --> 15:03:31,680 the loop and also breaking me out of and 23899 15:03:29,160 --> 15:03:33,360 returning from the entire function so 23900 15:03:31,680 --> 15:03:35,436 nothing too new here in terms of C 23901 15:03:33,360 --> 15:03:37,956 versus python except for this issue of 23902 15:03:35,436 --> 15:03:39,880 scope and I indeed returned n at the 23903 15:03:37,956 --> 15:03:42,276 bottom there just to make clear that n 23904 15:03:39,880 --> 15:03:45,560 would still exist so either of those are 23905 15:03:42,276 --> 15:03:48,596 correct now I just have a Python program 23906 15:03:45,560 --> 15:03:50,320 that I think is going to allow me to 23907 15:03:48,596 --> 15:03:53,756 implement this same Mario idea so let's 23908 15:03:50,320 --> 15:03:56,956 run python of mario. and okay so nothing 23909 15:03:53,756 --> 15:04:00,480 happened uh python of mario. 23910 15:03:56,956 --> 15:04:02,796 py what did I do wrong yeah I have to 23911 15:04:00,480 --> 15:04:05,000 call Main so at the bottom of my code I 23912 15:04:02,796 --> 15:04:07,436 have to call Main here and this is a 23913 15:04:05,000 --> 15:04:08,916 stylistic detail that's been subtle um 23914 15:04:07,436 --> 15:04:11,956 generally speaking when when you are 23915 15:04:08,916 --> 15:04:13,916 writing in Python um there's not a cs50 23916 15:04:11,956 --> 15:04:16,040 style guide per se there's actually a 23917 15:04:13,916 --> 15:04:19,560 python style guide that most people 23918 15:04:16,040 --> 15:04:21,840 adhere to um it's and in this case 23919 15:04:19,560 --> 15:04:23,796 double blank lines between functions is 23920 15:04:21,840 --> 15:04:25,956 the norm I'm doing that deliberately 23921 15:04:23,796 --> 15:04:27,596 although uh it might otherwise not be 23922 15:04:25,956 --> 15:04:30,720 obvious but now that I've called main on 23923 15:04:27,596 --> 15:04:32,796 line 16 let's run mario. once more aha 23924 15:04:30,720 --> 15:04:34,680 now we get there now we see it type in 23925 15:04:32,796 --> 15:04:37,480 three and I'm back in business printing 23926 15:04:34,680 --> 15:04:40,480 out the values there 23927 15:04:37,480 --> 15:04:40,480 yeah 23928 15:04:41,400 --> 15:04:45,796 sure why do I need the if condition at 23929 15:04:42,880 --> 15:04:48,240 all why can't I just return n here as by 23930 15:04:45,796 --> 15:04:50,596 doing return n or if I really want to be 23931 15:04:48,240 --> 15:04:53,916 succinct I could technically just do 23932 15:04:50,596 --> 15:04:55,200 this the only reason I added the if 23933 15:04:53,916 --> 15:04:57,596 condition is because if the user types 23934 15:04:55,200 --> 15:04:59,240 in negative 1 -2 I wanted to prompt them 23935 15:04:57,596 --> 15:05:00,756 again and again that's all but that 23936 15:04:59,240 --> 15:05:03,320 would be totally acceptable too if you 23937 15:05:00,756 --> 15:05:05,436 were okay with that result instead well 23938 15:05:03,320 --> 15:05:08,000 let me do one other thing here to point 23939 15:05:05,436 --> 15:05:09,916 out why we are using get in so 23940 15:05:08,000 --> 15:05:11,560 frequently this new training wheel all 23941 15:05:09,916 --> 15:05:13,720 be it temporarily so let me go back to 23942 15:05:11,560 --> 15:05:17,360 the way it was a moment ago and let me 23943 15:05:13,720 --> 15:05:20,080 propose now to take away get int I 23944 15:05:17,360 --> 15:05:22,160 claimed earlier that if you're not using 23945 15:05:20,080 --> 15:05:24,240 get int you can just use the input 23946 15:05:22,160 --> 15:05:27,160 function itself from 23947 15:05:24,240 --> 15:05:29,480 python but that always returns a string 23948 15:05:27,160 --> 15:05:31,880 or a stir and so recall that you have to 23949 15:05:29,480 --> 15:05:34,200 pass the output of the input function to 23950 15:05:31,880 --> 15:05:36,956 an INT either on the same line or if you 23951 15:05:34,200 --> 15:05:38,796 prefer on another line instead but it 23952 15:05:36,956 --> 15:05:41,840 turns out what I didn't do was show you 23953 15:05:38,796 --> 15:05:43,956 what happens if you uh don't cooperate 23954 15:05:41,840 --> 15:05:47,116 with the user uh with the program so if 23955 15:05:43,956 --> 15:05:49,240 I run python of mario. now works great 23956 15:05:47,116 --> 15:05:51,480 even without the get int function and I 23957 15:05:49,240 --> 15:05:53,240 can do it with four still works great 23958 15:05:51,480 --> 15:05:54,956 but let me clear my terminal and be 23959 15:05:53,240 --> 15:05:57,840 difficult now as the user and type in C 23960 15:05:54,956 --> 15:05:59,720 for the height instead enter now we see 23961 15:05:57,840 --> 15:06:01,636 one of those tracebacks again this one 23962 15:05:59,720 --> 15:06:03,680 is different this isn't a name error but 23963 15:06:01,636 --> 15:06:05,480 apparently a value error and if I kind 23964 15:06:03,680 --> 15:06:07,720 of ignore the stuff I don't understand I 23965 15:06:05,480 --> 15:06:10,276 can see invalid literal for INT with 23966 15:06:07,720 --> 15:06:12,956 base 10 cat that's a super cryptic way 23967 15:06:10,276 --> 15:06:15,840 of saying that c a is not a number in 23968 15:06:12,956 --> 15:06:17,596 decimal notation and so I would seem to 23969 15:06:15,840 --> 15:06:19,480 have to somehow handle this case and if 23970 15:06:17,596 --> 15:06:22,200 you want to be more Curious you'll see 23971 15:06:19,480 --> 15:06:23,880 that this is indeed a trace back and um 23972 15:06:22,200 --> 15:06:25,560 C tends to do this too or the debugger 23973 15:06:23,880 --> 15:06:26,680 would do this for you too you can see 23974 15:06:25,560 --> 15:06:28,480 all of the functions that have been 23975 15:06:26,680 --> 15:06:31,520 called to get you to this point so 23976 15:06:28,480 --> 15:06:34,320 apparently my problem is initially in 23977 15:06:31,520 --> 15:06:37,080 line 14 but line 14 if I keep scrolling 23978 15:06:34,320 --> 15:06:39,160 is uninteresting it's main but line 14 23979 15:06:37,080 --> 15:06:41,756 leads me to execute line two which is 23980 15:06:39,160 --> 15:06:44,560 indeed in main that leads me to execute 23981 15:06:41,756 --> 15:06:46,480 line nine which is in get height and 23982 15:06:44,560 --> 15:06:48,000 okay here's the issue so the closest 23983 15:06:46,480 --> 15:06:50,276 line number to the error message is the 23984 15:06:48,000 --> 15:06:52,436 one that probably reveals the most line 23985 15:06:50,276 --> 15:06:54,200 nine is where my issue is so I can't 23986 15:06:52,436 --> 15:06:55,916 just blindly ask the user for input and 23987 15:06:54,200 --> 15:06:57,880 then convert it to an INT if they're not 23988 15:06:55,916 --> 15:06:59,480 going to give me an INT now how do we 23989 15:06:57,880 --> 15:07:01,360 deal with this well back in problem set 23990 15:06:59,480 --> 15:07:03,720 two you might recall validating that the 23991 15:07:01,360 --> 15:07:05,520 user typed in a number and using a for 23992 15:07:03,720 --> 15:07:06,796 Loop and the like well it turns out 23993 15:07:05,520 --> 15:07:09,756 there's a better way to do this in 23994 15:07:06,796 --> 15:07:11,880 Python and the are kind of there if you 23995 15:07:09,756 --> 15:07:13,756 want to try to convert something for a 23996 15:07:11,880 --> 15:07:16,116 number to a number that might not 23997 15:07:13,756 --> 15:07:17,796 actually be a number turns out Python 23998 15:07:16,116 --> 15:07:20,480 and certain other languages literally 23999 15:07:17,796 --> 15:07:22,080 have a keyword called try and if only 24000 15:07:20,480 --> 15:07:23,916 this existed for the past few weeks I 24001 15:07:22,080 --> 15:07:25,720 know but like you can try to do the 24002 15:07:23,916 --> 15:07:27,720 following with your code what do I want 24003 15:07:25,720 --> 15:07:30,436 to try to do well I want to try to 24004 15:07:27,720 --> 15:07:32,916 execute those few lines except if 24005 15:07:30,436 --> 15:07:35,116 there's an error so I can say except if 24006 15:07:32,916 --> 15:07:37,240 there's a value error specifically the 24007 15:07:35,116 --> 15:07:39,916 one I screwed up and created a moment 24008 15:07:37,240 --> 15:07:41,520 ago and if there is a value error I can 24009 15:07:39,916 --> 15:07:44,596 print out an informative message to the 24010 15:07:41,520 --> 15:07:47,480 user like not an integer or anything 24011 15:07:44,596 --> 15:07:49,880 else and what's happening here now is 24012 15:07:47,480 --> 15:07:52,636 literally this operative word try the 24013 15:07:49,880 --> 15:07:54,840 pro python is going to try to get input 24014 15:07:52,636 --> 15:07:56,796 and try to convert it to an in and it's 24015 15:07:54,840 --> 15:07:59,360 going to try to check if it's greater 24016 15:07:56,796 --> 15:08:01,116 than zero and then try to return it all 24017 15:07:59,360 --> 15:08:03,040 why all of three of those lines are 24018 15:08:01,116 --> 15:08:05,400 inside of indented underneath the tri 24019 15:08:03,040 --> 15:08:08,596 block except if something goes wrong 24020 15:08:05,400 --> 15:08:10,320 specifically a value error happens then 24021 15:08:08,596 --> 15:08:12,596 it prints this but it doesn't return 24022 15:08:10,320 --> 15:08:14,680 anything and because I'm in a loop that 24023 15:08:12,596 --> 15:08:16,796 means it's going to do it again and 24024 15:08:14,680 --> 15:08:20,040 again and again until the human actually 24025 15:08:16,796 --> 15:08:21,720 cooperates and gives me an actual number 24026 15:08:20,040 --> 15:08:24,240 and so this too is what the world would 24027 15:08:21,720 --> 15:08:26,320 call pythonic in Python you don't 24028 15:08:24,240 --> 15:08:27,796 necessarily rigorously try to validate 24029 15:08:26,320 --> 15:08:29,520 the users's input make sure they haven't 24030 15:08:27,796 --> 15:08:31,796 screwed up you honestly take a more 24031 15:08:29,520 --> 15:08:34,360 laxidasical approach and just try to do 24032 15:08:31,796 --> 15:08:36,360 something but catch an error if it 24033 15:08:34,360 --> 15:08:38,400 happens so catch is also a term of art 24034 15:08:36,360 --> 15:08:40,200 even though it's not a keyword here 24035 15:08:38,400 --> 15:08:42,276 except if something happens you handle 24036 15:08:40,200 --> 15:08:43,956 it so you try and you handle it it's 24037 15:08:42,276 --> 15:08:46,240 sort of best effort programming if you 24038 15:08:43,956 --> 15:08:48,720 will but this is baked into the mindset 24039 15:08:46,240 --> 15:08:51,200 of the Python uh programming community 24040 15:08:48,720 --> 15:08:53,680 so now if I do python of mario. py and I 24041 15:08:51,200 --> 15:08:56,956 cooperate works great as before try and 24042 15:08:53,680 --> 15:09:00,240 succeed three Works four works if though 24043 15:08:56,956 --> 15:09:02,680 I try and fail by typing in cat it 24044 15:09:00,240 --> 15:09:04,080 doesn't crash per se it doesn't show me 24045 15:09:02,680 --> 15:09:05,796 an error it shows me something more user 24046 15:09:04,080 --> 15:09:07,880 friendly like not an integer and then I 24047 15:09:05,796 --> 15:09:10,880 can try again with dog not an integer I 24048 15:09:07,880 --> 15:09:12,400 can try again with five and now it works 24049 15:09:10,880 --> 15:09:14,040 so we won't generally have you write 24050 15:09:12,400 --> 15:09:16,400 much in the way of these try except 24051 15:09:14,040 --> 15:09:18,840 blocks only because they get a little 24052 15:09:16,400 --> 15:09:20,436 sophisticated quickly but that is to 24053 15:09:18,840 --> 15:09:21,720 reveal what the get int function is 24054 15:09:20,436 --> 15:09:23,080 doing this is why we give you the 24055 15:09:21,720 --> 15:09:24,520 training wheels so that when you want to 24056 15:09:23,080 --> 15:09:26,080 get an INT you don't have to jump 24057 15:09:24,520 --> 15:09:28,080 through all these annoying Hoops to do 24058 15:09:26,080 --> 15:09:31,040 so but that's all the library is really 24059 15:09:28,080 --> 15:09:32,680 doing for you is just try and accept you 24060 15:09:31,040 --> 15:09:35,680 won't be left with any training wheels 24061 15:09:32,680 --> 15:09:40,040 ultimately questions now on getting 24062 15:09:35,680 --> 15:09:40,040 inputs and trying in this way 24063 15:09:40,160 --> 15:09:42,840 anything at all 24064 15:09:45,680 --> 15:09:50,756 yeah Tri block it say that oh you could 24065 15:09:49,200 --> 15:09:52,480 you put the condition outside of the tri 24066 15:09:50,756 --> 15:09:53,636 block short answer yes and in fact I 24067 15:09:52,480 --> 15:09:55,000 struggled with this last night when 24068 15:09:53,636 --> 15:09:58,480 tweaking this example to show the 24069 15:09:55,000 --> 15:10:01,636 simplest version I will disclaim that 24070 15:09:58,480 --> 15:10:04,276 really I should only be trying literally 24071 15:10:01,636 --> 15:10:06,320 to do the The Fragile part and then down 24072 15:10:04,276 --> 15:10:08,160 here I should be really doing what 24073 15:10:06,320 --> 15:10:10,116 you're proposing which is do the 24074 15:10:08,160 --> 15:10:12,000 condition out here the problem is though 24075 15:10:10,116 --> 15:10:14,116 that logically this gets messy quickly 24076 15:10:12,000 --> 15:10:16,160 right because except if there's a value 24077 15:10:14,116 --> 15:10:18,040 error I want to print out not an integer 24078 15:10:16,160 --> 15:10:19,596 I can't compare n against zero then 24079 15:10:18,040 --> 15:10:21,680 because n doesn't exist because there 24080 15:10:19,596 --> 15:10:23,040 was an error so it turns out and I'll 24081 15:10:21,680 --> 15:10:24,720 show you this this is now the advanced 24082 15:10:23,040 --> 15:10:27,360 version of python there's actually an 24083 15:10:24,720 --> 15:10:29,636 else keyword you can use in Python that 24084 15:10:27,360 --> 15:10:32,160 does not accompany if or L if it 24085 15:10:29,636 --> 15:10:34,240 accompanies try and accept which I think 24086 15:10:32,160 --> 15:10:36,080 is weirdly confusing a different word 24087 15:10:34,240 --> 15:10:38,000 would have been better but if you really 24088 15:10:36,080 --> 15:10:39,400 prefer I could have done this instead 24089 15:10:38,000 --> 15:10:40,756 dead and this is one of these design 24090 15:10:39,400 --> 15:10:42,596 things where like reasonable people will 24091 15:10:40,756 --> 15:10:44,560 disagree generally speaking you should 24092 15:10:42,596 --> 15:10:46,680 only try to do the one line that might 24093 15:10:44,560 --> 15:10:48,040 very well fail but honestly this looks 24094 15:10:46,680 --> 15:10:50,480 kind of stupid now it's just 24095 15:10:48,040 --> 15:10:52,636 unnecessarily complicated and so my own 24096 15:10:50,480 --> 15:10:54,400 preference was actually the original 24097 15:10:52,636 --> 15:10:56,116 which was yeah I'm trying a few extra 24098 15:10:54,400 --> 15:10:57,756 lines that really aren't going to fail 24099 15:10:56,116 --> 15:10:59,560 mathematically but it's just tighter 24100 15:10:57,756 --> 15:11:01,080 it's cleaner this way and here's again 24101 15:10:59,560 --> 15:11:02,400 the sort of like you know arguments 24102 15:11:01,080 --> 15:11:03,560 you'll start to make yourself as you get 24103 15:11:02,400 --> 15:11:04,796 more comfortable with programming you'll 24104 15:11:03,560 --> 15:11:06,360 have an opinion you'll disagree with 24105 15:11:04,796 --> 15:11:08,916 someone and so long as you can back your 24106 15:11:06,360 --> 15:11:12,720 argument up pretty 24107 15:11:08,916 --> 15:11:15,360 probably all right so how about we now 24108 15:11:12,720 --> 15:11:16,956 take away some piece of magic that's 24109 15:11:15,360 --> 15:11:18,956 been here for a while let me go ahead 24110 15:11:16,956 --> 15:11:21,956 and uh Delete all of this here and let 24111 15:11:18,956 --> 15:11:23,680 me propose that we revisit uh not that 24112 15:11:21,956 --> 15:11:25,040 vertical column and the exceptions that 24113 15:11:23,680 --> 15:11:26,880 might result from getting input but 24114 15:11:25,040 --> 15:11:28,560 these like horizontal question marks 24115 15:11:26,880 --> 15:11:30,680 that we saw a while ago so I want all of 24116 15:11:28,560 --> 15:11:32,160 those question marks on the same line 24117 15:11:30,680 --> 15:11:33,756 and yet I worry we're about to see a 24118 15:11:32,160 --> 15:11:35,436 challenge here because print up until 24119 15:11:33,756 --> 15:11:37,240 now has been putting new lines 24120 15:11:35,436 --> 15:11:39,796 everywhere automatically even without 24121 15:11:37,240 --> 15:11:42,636 those backslash NS well let me propose 24122 15:11:39,796 --> 15:11:44,720 that we do this for I in the range of 24123 15:11:42,636 --> 15:11:47,000 four if I want four question marks let 24124 15:11:44,720 --> 15:11:48,276 me just print four question marks 24125 15:11:47,000 --> 15:11:51,436 unfortunately I don't think this is 24126 15:11:48,276 --> 15:11:53,796 correct yet let me run python of mario. 24127 15:11:51,436 --> 15:11:56,276 and of course this gives me a column 24128 15:11:53,796 --> 15:11:58,560 instead of the row of question marks 24129 15:11:56,276 --> 15:12:00,560 that I want so how do we do this well it 24130 15:11:58,560 --> 15:12:03,116 turns out if you read the documentation 24131 15:12:00,560 --> 15:12:05,000 for the print function it turns out that 24132 15:12:03,116 --> 15:12:06,840 print not surprisingly perhaps takes a 24133 15:12:05,000 --> 15:12:08,796 lot of different arguments as well and 24134 15:12:06,840 --> 15:12:11,320 in fact if you go to the documentation 24135 15:12:08,796 --> 15:12:13,880 for it you'll see that it takes not just 24136 15:12:11,320 --> 15:12:15,916 positional arguments that is from left 24137 15:12:13,880 --> 15:12:17,596 to right separated by commas turns out 24138 15:12:15,916 --> 15:12:19,436 python has supports a fancier feature 24139 15:12:17,596 --> 15:12:21,956 with arguments where you can pass the 24140 15:12:19,436 --> 15:12:24,240 names of arguments to functions too so 24141 15:12:21,956 --> 15:12:27,160 what do I mean by this if I go back to 24142 15:12:24,240 --> 15:12:30,360 vs code here and I've read the 24143 15:12:27,160 --> 15:12:33,000 documentation it turns out that yes as 24144 15:12:30,360 --> 15:12:35,276 before you can pass multiple arguments 24145 15:12:33,000 --> 15:12:37,200 to python like this like hello comma 24146 15:12:35,276 --> 15:12:39,276 David comma me that will just 24147 15:12:37,200 --> 15:12:41,520 automatically concatenate all three of 24148 15:12:39,276 --> 15:12:43,000 those positional arguments together 24149 15:12:41,520 --> 15:12:44,480 they're positional in the sense that 24150 15:12:43,000 --> 15:12:46,480 they literally flow from left to right 24151 15:12:44,480 --> 15:12:48,360 separated by commas but if you don't 24152 15:12:46,480 --> 15:12:50,520 want to just pass in values like that 24153 15:12:48,360 --> 15:12:52,796 you want to actually print out as I did 24154 15:12:50,520 --> 15:12:55,796 before a question mark but you want to 24155 15:12:52,796 --> 15:12:58,080 override the default behavior of print 24156 15:12:55,796 --> 15:13:00,560 by changing the line ending you can 24157 15:12:58,080 --> 15:13:02,240 actually do this you can use the name of 24158 15:13:00,560 --> 15:13:05,636 an argument that you know exists from 24159 15:13:02,240 --> 15:13:08,080 the documentation set it equal to some 24160 15:13:05,636 --> 15:13:10,756 alternative value and in fact even 24161 15:13:08,080 --> 15:13:13,956 though this looks cryptic this is how I 24162 15:13:10,756 --> 15:13:16,400 would override the end of each line to 24163 15:13:13,956 --> 15:13:18,240 be quote unquote that is nothing because 24164 15:13:16,400 --> 15:13:21,240 if you read the documentation the 24165 15:13:18,240 --> 15:13:24,636 default value for this end argument does 24166 15:13:21,240 --> 15:13:26,436 someone want to guess is is back sln so 24167 15:13:24,636 --> 15:13:29,480 if you read the documentation you'll see 24168 15:13:26,436 --> 15:13:31,916 that back sln is the implied default for 24169 15:13:29,480 --> 15:13:33,680 this end argument and so if you want to 24170 15:13:31,916 --> 15:13:37,680 change it you just say end equals 24171 15:13:33,680 --> 15:13:40,400 something else and so here I can change 24172 15:13:37,680 --> 15:13:42,796 change it to nothing and now rerun 24173 15:13:40,400 --> 15:13:44,436 python of mario. and now they're all on 24174 15:13:42,796 --> 15:13:46,360 the same line now looks a little stupid 24175 15:13:44,436 --> 15:13:47,840 cuz I made that sort of week one mistake 24176 15:13:46,360 --> 15:13:49,720 where I still need to move the cursor to 24177 15:13:47,840 --> 15:13:51,000 the next line that's just a different 24178 15:13:49,720 --> 15:13:52,756 problem I'm just going to go over here 24179 15:13:51,000 --> 15:13:54,756 and print nothing I don't even need to 24180 15:13:52,756 --> 15:13:57,080 print back sln because if print 24181 15:13:54,756 --> 15:13:58,796 automatically gives you a backslash n 24182 15:13:57,080 --> 15:14:01,040 just call print with nothing and you'll 24183 15:13:58,796 --> 15:14:03,080 get that for free so let me rerun python 24184 15:14:01,040 --> 15:14:05,360 of mario. Pi and now it looks a little 24185 15:14:03,080 --> 15:14:06,916 prettier at the prompt and to be super 24186 15:14:05,360 --> 15:14:08,840 clear as to what's going on suppose I 24187 15:14:06,916 --> 15:14:11,000 want to sort of make an exclamation here 24188 15:14:08,840 --> 15:14:13,200 I could change the back sln default to 24189 15:14:11,000 --> 15:14:15,840 like an exclamation point Just for kicks 24190 15:14:13,200 --> 15:14:17,320 and if I run python of mario. py again 24191 15:14:15,840 --> 15:14:19,956 now I get this sort of you know 24192 15:14:17,320 --> 15:14:22,116 exclamation with question marks and 24193 15:14:19,956 --> 15:14:23,400 exclamation points as well so that's all 24194 15:14:22,116 --> 15:14:25,916 that's going on here and this is what's 24195 15:14:23,400 --> 15:14:28,000 called a named argument it literally has 24196 15:14:25,916 --> 15:14:29,916 a name that you can specify when calling 24197 15:14:28,000 --> 15:14:31,796 it in and it's different from positional 24198 15:14:29,916 --> 15:14:33,880 in that you're literally using the 24199 15:14:31,796 --> 15:14:35,000 name let me propose something else 24200 15:14:33,880 --> 15:14:36,480 though and this is why people kind of 24201 15:14:35,000 --> 15:14:39,320 like python there's just kind of cool 24202 15:14:36,480 --> 15:14:42,956 ways to do things that's kind of a you 24203 15:14:39,320 --> 15:14:45,240 know ver it's a three line verbose way 24204 15:14:42,956 --> 15:14:47,400 of printing out four question marks you 24205 15:14:45,240 --> 15:14:49,200 know I could certainly take the you know 24206 15:14:47,400 --> 15:14:50,916 shortcut and just do this but that's not 24207 15:14:49,200 --> 15:14:52,320 really that interesting for anyone 24208 15:14:50,916 --> 15:14:54,360 especially if I want to do it a variable 24209 15:14:52,320 --> 15:14:57,636 number of times but python does let you 24210 15:14:54,360 --> 15:15:00,240 do this if you want to uh multiply a 24211 15:14:57,636 --> 15:15:02,880 character some number of times not only 24212 15:15:00,240 --> 15:15:06,276 can you use Plus for concatenation you 24213 15:15:02,880 --> 15:15:08,520 can use star or an asterisk for 24214 15:15:06,276 --> 15:15:10,796 multiplication if you will that is 24215 15:15:08,520 --> 15:15:12,880 concatenation again and again and again 24216 15:15:10,796 --> 15:15:15,636 so if I just print out quote unquote 24217 15:15:12,880 --> 15:15:17,720 question mark Time 4 that's actually 24218 15:15:15,636 --> 15:15:20,160 going to be the tightest way the most 24219 15:15:17,720 --> 15:15:21,796 distinct way I can print four question 24220 15:15:20,160 --> 15:15:24,360 marks instead and if I don't use four I 24221 15:15:21,796 --> 15:15:25,840 use n where I get n from the user bang 24222 15:15:24,360 --> 15:15:29,276 like now I've gotten rid of the four 24223 15:15:25,840 --> 15:15:32,956 Loop entirely and I'm using the the star 24224 15:15:29,276 --> 15:15:34,956 operator to manipulate it instead and to 24225 15:15:32,956 --> 15:15:38,040 be super clear here in so far as python 24226 15:15:34,956 --> 15:15:39,480 does not have milock or free or memory 24227 15:15:38,040 --> 15:15:42,680 management that you have to do guess 24228 15:15:39,480 --> 15:15:42,680 what python also doesn't 24229 15:15:43,400 --> 15:15:46,680 have anything on your minds the past 24230 15:15:45,756 --> 15:15:50,880 couple of 24231 15:15:46,680 --> 15:15:53,080 weeks doesn't have pointers yes so 24232 15:15:50,880 --> 15:15:54,756 python does not have pointers which just 24233 15:15:53,080 --> 15:15:56,796 means that all of that happens for you 24234 15:15:54,756 --> 15:15:58,720 automatically underneath the hood Again 24235 15:15:56,796 --> 15:16:00,840 by way of code that someone else wrote 24236 15:15:58,720 --> 15:16:02,956 how about one more throwback with Mario 24237 15:16:00,840 --> 15:16:05,116 we've talked about in week one this sort 24238 15:16:02,956 --> 15:16:08,596 of two-dimensional structure where it's 24239 15:16:05,116 --> 15:16:10,320 like I claim like 3x3 a grid of bricks 24240 15:16:08,596 --> 15:16:11,796 if you will well how can we do this in 24241 15:16:10,320 --> 15:16:15,000 Python we can do this in a couple of 24242 15:16:11,796 --> 15:16:17,080 ways now let me go back to my mario. py 24243 15:16:15,000 --> 15:16:19,240 and let me do something like for I in 24244 15:16:17,080 --> 15:16:21,360 range of we'll just do three even though 24245 15:16:19,240 --> 15:16:23,796 I know now I could use get int or I 24246 15:16:21,360 --> 15:16:25,436 could use input and int and if I want to 24247 15:16:23,796 --> 15:16:27,480 do something two-dimensionally just like 24248 15:16:25,436 --> 15:16:31,000 in C you can Nest your for Loop so maybe 24249 15:16:27,480 --> 15:16:34,956 I could do 4J in range of three and then 24250 15:16:31,000 --> 15:16:37,116 in here I could print out a uh hash 24251 15:16:34,956 --> 15:16:39,200 symbol and then let's see if that gives 24252 15:16:37,116 --> 15:16:41,636 me nine total so if I've got a nested 24253 15:16:39,200 --> 15:16:44,400 Loop like this python of mario. 24254 15:16:41,636 --> 15:16:45,916 hopefully gives me a grid no it gave me 24255 15:16:44,400 --> 15:16:48,240 a column of 24256 15:16:45,916 --> 15:16:51,200 nine why 24257 15:16:48,240 --> 15:16:53,400 logically even though I've got my row 24258 15:16:51,200 --> 15:16:55,916 and my 24259 15:16:53,400 --> 15:16:59,480 columns 24260 15:16:55,916 --> 15:17:01,080 yeah yeah the line ending so in my row I 24261 15:16:59,480 --> 15:17:02,680 can't let print just keep adding new 24262 15:17:01,080 --> 15:17:04,560 line adding new lines so I just have to 24263 15:17:02,680 --> 15:17:06,320 override this here and let me not screw 24264 15:17:04,560 --> 15:17:08,400 up like before let me print one at the 24265 15:17:06,320 --> 15:17:10,916 end of the whole row just to move the 24266 15:17:08,400 --> 15:17:13,276 cursor down and I think now together now 24267 15:17:10,916 --> 15:17:15,160 we've got our 3x3 of course we could 24268 15:17:13,276 --> 15:17:17,560 tighten this up further like if I don't 24269 15:17:15,160 --> 15:17:20,360 like the nested loop I probably could go 24270 15:17:17,560 --> 15:17:23,040 in here and just print out for instance 24271 15:17:20,360 --> 15:17:24,680 a uh a brick times three or I could 24272 15:17:23,040 --> 15:17:26,520 change the three to a variable if I've 24273 15:17:24,680 --> 15:17:28,276 gotten it from the user so I can tighten 24274 15:17:26,520 --> 15:17:29,956 this up further so again just different 24275 15:17:28,276 --> 15:17:31,680 ways to solve the same problem and again 24276 15:17:29,956 --> 15:17:33,000 sort of evidence of why a lot of people 24277 15:17:31,680 --> 15:17:34,916 like python there's just some more 24278 15:17:33,000 --> 15:17:36,560 pleasant ways to solve problems without 24279 15:17:34,916 --> 15:17:40,040 getting into the weeds constantly of 24280 15:17:36,560 --> 15:17:42,080 doing things like like with um uh for 24281 15:17:40,040 --> 15:17:43,240 loops and wild Loops endlessly all right 24282 15:17:42,080 --> 15:17:44,796 well how about some other building 24283 15:17:43,240 --> 15:17:47,080 blocks lists are going to be so 24284 15:17:44,796 --> 15:17:49,116 incredibly useful in Python just as 24285 15:17:47,080 --> 15:17:50,360 arrays were in C but arrays are annoying 24286 15:17:49,116 --> 15:17:51,880 because you have to manage the memory 24287 15:17:50,360 --> 15:17:53,636 yourself you have to know in advance how 24288 15:17:51,880 --> 15:17:56,116 big they are or you have to use pointers 24289 15:17:53,636 --> 15:17:57,480 and malok or realloc to resize them like 24290 15:17:56,116 --> 15:17:59,596 oh my God like the past two weeks have 24291 15:17:57,480 --> 15:18:02,160 been painful in that sense but python 24292 15:17:59,596 --> 15:18:04,000 does this all for free for you in fact 24293 15:18:02,160 --> 15:18:06,796 there's a whole bunch of functions that 24294 15:18:04,000 --> 15:18:11,080 come with python that involve lists and 24295 15:18:06,796 --> 15:18:14,596 they'll ow us ultimately um to do things 24296 15:18:11,080 --> 15:18:16,116 again and again and again uh with uh 24297 15:18:14,596 --> 15:18:17,480 within the same data structure and for 24298 15:18:16,116 --> 15:18:19,360 instance we'll be able to get the length 24299 15:18:17,480 --> 15:18:21,360 of a list you don't have to remember it 24300 15:18:19,360 --> 15:18:23,400 yourself in a variable you can just ask 24301 15:18:21,360 --> 15:18:25,840 python how many elements are in this 24302 15:18:23,400 --> 15:18:28,200 list and with this I think we can solve 24303 15:18:25,840 --> 15:18:32,040 some some old problems too so let me go 24304 15:18:28,200 --> 15:18:34,956 back here to vs code let me close Mario 24305 15:18:32,040 --> 15:18:37,276 and give us a new program called scores. 24306 15:18:34,956 --> 15:18:39,360 piy and rather than show the C and the 24307 15:18:37,276 --> 15:18:42,240 python now let's just focus on Python 24308 15:18:39,360 --> 15:18:43,916 and in scores. C way back when we just 24309 15:18:42,240 --> 15:18:46,916 averaged like three test scores or 24310 15:18:43,916 --> 15:18:48,956 something like that 72 73 and 33 a few 24311 15:18:46,916 --> 15:18:52,080 weeks ago so if I want to create a list 24312 15:18:48,956 --> 15:18:54,560 in this python version of 72 73 33 I 24313 15:18:52,080 --> 15:18:56,240 just use my square bracket notation C 24314 15:18:54,560 --> 15:18:58,840 let you use curly braces if you know the 24315 15:18:56,240 --> 15:19:01,000 values in advance but Python's just this 24316 15:18:58,840 --> 15:19:03,480 and now if I want to compute the average 24317 15:19:01,000 --> 15:19:05,680 in pi in C recall I did something with a 24318 15:19:03,480 --> 15:19:07,756 loop I added all the values together I 24319 15:19:05,680 --> 15:19:09,360 then divided by the total number values 24320 15:19:07,756 --> 15:19:11,360 just like you would in grade school and 24321 15:19:09,360 --> 15:19:13,320 that gave me the average well python 24322 15:19:11,360 --> 15:19:15,636 comes with a lot of super handy 24323 15:19:13,320 --> 15:19:17,436 functions not just length but others as 24324 15:19:15,636 --> 15:19:20,000 well and so in fact if you want to 24325 15:19:17,436 --> 15:19:22,520 compute the average you can take the sum 24326 15:19:20,000 --> 15:19:24,720 of all of those scores and divide it by 24327 15:19:22,520 --> 15:19:27,040 the length of all of those scores so 24328 15:19:24,720 --> 15:19:29,320 python comes with length comes with sum 24329 15:19:27,040 --> 15:19:31,756 you can just pass in a whole list of any 24330 15:19:29,320 --> 15:19:33,720 size and let it deal with that problem 24331 15:19:31,756 --> 15:19:35,596 for you so if I want to now print out 24332 15:19:33,720 --> 15:19:38,240 this average I can print out average 24333 15:19:35,596 --> 15:19:40,520 colon and then I'll plug in my average 24334 15:19:38,240 --> 15:19:42,436 uh string for variable for interpolation 24335 15:19:40,520 --> 15:19:44,240 let me make this an F string so that it 24336 15:19:42,436 --> 15:19:46,796 gets formatted and let me just run 24337 15:19:44,240 --> 15:19:48,596 python of scores. Pi and there's my 24338 15:19:46,796 --> 15:19:49,956 average it's sort of rounding weird 24339 15:19:48,596 --> 15:19:52,756 because we're still vulnerable to some 24340 15:19:49,956 --> 15:19:54,320 floating point in Precision but at least 24341 15:19:52,756 --> 15:19:56,636 I didn't need loops and I didn't have to 24342 15:19:54,320 --> 15:19:58,400 write all this darn code just to do 24343 15:19:56,636 --> 15:20:00,320 something that you know Excel and Google 24344 15:19:58,400 --> 15:20:02,520 spreadsheets can just do like that well 24345 15:20:00,320 --> 15:20:04,756 python is closer to those kinds of tools 24346 15:20:02,520 --> 15:20:06,840 but more powerful and that you can 24347 15:20:04,756 --> 15:20:08,840 manipulate the data yourself how about 24348 15:20:06,840 --> 15:20:10,680 though if I want to um get a bunch of 24349 15:20:08,840 --> 15:20:12,596 scores manually from the user and then 24350 15:20:10,680 --> 15:20:15,160 sum them together well let's combine a 24351 15:20:12,596 --> 15:20:19,116 few ideas here how about this first let 24352 15:20:15,160 --> 15:20:20,880 me go ahead and uh import um the cs50 LI 24353 15:20:19,116 --> 15:20:22,200 get in function from the cs50 library 24354 15:20:20,880 --> 15:20:24,636 just so we don't have to deal with try 24355 15:20:22,200 --> 15:20:27,040 and accept or all of that and let me go 24356 15:20:24,636 --> 15:20:30,116 ahead and give myself an empty list and 24357 15:20:27,040 --> 15:20:32,200 this is powerful in py in C there's 24358 15:20:30,116 --> 15:20:33,840 really there's no point to an empty 24359 15:20:32,200 --> 15:20:35,596 array because if you create an empty 24360 15:20:33,840 --> 15:20:37,596 array with square bracket notation like 24361 15:20:35,596 --> 15:20:39,916 it's not useful for anything but in 24362 15:20:37,596 --> 15:20:42,116 Python you can create it empty because 24363 15:20:39,916 --> 15:20:44,000 python will grow and shrink the list for 24364 15:20:42,116 --> 15:20:45,956 you automatically as you add things to 24365 15:20:44,000 --> 15:20:47,436 it so if I want to get three scores from 24366 15:20:45,956 --> 15:20:50,276 the user I could do something like this 24367 15:20:47,436 --> 15:20:53,240 for I in range of three and then I can 24368 15:20:50,276 --> 15:20:55,160 grab a variable called score or anything 24369 15:20:53,240 --> 15:20:57,596 I could call get int prompt the human 24370 15:20:55,160 --> 15:20:59,680 for the score that they want to type in 24371 15:20:57,596 --> 15:21:02,040 and then once they do I can do this 24372 15:20:59,680 --> 15:21:04,840 thinking back to our objectoriented 24373 15:21:02,040 --> 15:21:07,596 programming capability now I could do 24374 15:21:04,840 --> 15:21:09,480 scores dot a pen 24375 15:21:07,596 --> 15:21:10,840 and I Canen that score to it and you 24376 15:21:09,480 --> 15:21:12,636 would only know this from having read 24377 15:21:10,840 --> 15:21:14,680 the documentation heard it in class in a 24378 15:21:12,636 --> 15:21:16,560 book or whatnot but it turns out that 24379 15:21:14,680 --> 15:21:19,240 just like strings have functions like 24380 15:21:16,560 --> 15:21:21,200 lower built into them lists have 24381 15:21:19,240 --> 15:21:23,116 functions like append built into them 24382 15:21:21,200 --> 15:21:25,436 that just literally appends to the end 24383 15:21:23,116 --> 15:21:28,040 of the list for you and python will grow 24384 15:21:25,436 --> 15:21:30,320 or Shrink it as needed no more Malo or C 24385 15:21:28,040 --> 15:21:32,840 or Realo or the like so this just 24386 15:21:30,320 --> 15:21:35,720 appends to the scores array the scores 24387 15:21:32,840 --> 15:21:37,596 list that score and then again and again 24388 15:21:35,720 --> 15:21:39,720 and again so the array starts at 24389 15:21:37,596 --> 15:21:42,636 sorry the list starts at size zero then 24390 15:21:39,720 --> 15:21:44,596 grows to one then two then three without 24391 15:21:42,636 --> 15:21:47,636 you having to do anything else and so 24392 15:21:44,596 --> 15:21:50,000 now down here I can compute an average 24393 15:21:47,636 --> 15:21:52,320 with the sum of those scores divided by 24394 15:21:50,000 --> 15:21:54,596 the length of the total number of scores 24395 15:21:52,320 --> 15:21:56,636 and to be clear length is the total 24396 15:21:54,596 --> 15:21:58,880 number of elements in the list doesn't 24397 15:21:56,636 --> 15:22:01,116 matter how big the values themselves are 24398 15:21:58,880 --> 15:22:04,000 now I can go ahead and print out an FST 24399 15:22:01,116 --> 15:22:07,240 string uh with something like average 24400 15:22:04,000 --> 15:22:09,956 colon average and curly braces and and 24401 15:22:07,240 --> 15:22:11,480 if I run python of scores. piy I'll type 24402 15:22:09,956 --> 15:22:14,040 in just for the sake of discussion the 24403 15:22:11,480 --> 15:22:15,680 three values I still get the same answer 24404 15:22:14,040 --> 15:22:18,240 but that would have been painful to do 24405 15:22:15,680 --> 15:22:20,480 in see unless you committ it in advance 24406 15:22:18,240 --> 15:22:24,880 to a fixed size array which we already 24407 15:22:20,480 --> 15:22:27,276 decided weeks ago was annoying or uh you 24408 15:22:24,880 --> 15:22:30,520 uh grew it dynamically using malok or 24409 15:22:27,276 --> 15:22:32,320 realloc or the like all right what else 24410 15:22:30,520 --> 15:22:34,956 can I do well there's some nice things 24411 15:22:32,320 --> 15:22:37,520 you might as well know exist um instead 24412 15:22:34,956 --> 15:22:39,756 of scores. aen you can do slight 24413 15:22:37,520 --> 15:22:42,240 fanciness like this like if you want to 24414 15:22:39,756 --> 15:22:44,880 append something to a list you can 24415 15:22:42,240 --> 15:22:47,400 actually do plus equals and then put 24416 15:22:44,880 --> 15:22:49,916 that thing in a a temporary list of its 24417 15:22:47,400 --> 15:22:51,596 own and just use what is essentially 24418 15:22:49,916 --> 15:22:54,080 concatenation but not concatenation of 24419 15:22:51,596 --> 15:22:57,400 strings but concatination of lists so 24420 15:22:54,080 --> 15:22:59,560 this new line six appends to the scores 24421 15:22:57,400 --> 15:23:00,956 list this tiny little list I'm 24422 15:22:59,560 --> 15:23:03,160 temporarily creating with just the 24423 15:23:00,956 --> 15:23:05,000 current new score so just another piece 24424 15:23:03,160 --> 15:23:07,276 of syntax that's worth seeing that 24425 15:23:05,000 --> 15:23:09,360 allows you to do something like that as 24426 15:23:07,276 --> 15:23:11,200 well all right well how about we go back 24427 15:23:09,360 --> 15:23:12,756 to strings for a moment and all these 24428 15:23:11,200 --> 15:23:14,796 examples as always are on the course's 24429 15:23:12,756 --> 15:23:17,000 website afterward suppose we want to do 24430 15:23:14,796 --> 15:23:19,160 something like converting characters to 24431 15:23:17,000 --> 15:23:20,636 uppercase well to be clear I could do 24432 15:23:19,160 --> 15:23:23,160 something like this let me create a 24433 15:23:20,636 --> 15:23:25,320 program called uppercase dop let me 24434 15:23:23,160 --> 15:23:27,200 prompt the user for a before string as 24435 15:23:25,320 --> 15:23:29,276 by using the input function or get 24436 15:23:27,200 --> 15:23:31,160 string which is almost the same and I'll 24437 15:23:29,276 --> 15:23:33,480 prompt the user for a string 24438 15:23:31,160 --> 15:23:37,480 beforeand then let me go ahead and print 24439 15:23:33,480 --> 15:23:40,840 out uh how about the keyword after 24440 15:23:37,480 --> 15:23:42,276 and then end the new line with nothing 24441 15:23:40,840 --> 15:23:44,880 just so that I can see before on one 24442 15:23:42,276 --> 15:23:46,880 line and after on the next line and then 24443 15:23:44,880 --> 15:23:51,040 let me do this and here's where python 24444 15:23:46,880 --> 15:23:55,240 gets pleasant too with loops for C in 24445 15:23:51,040 --> 15:23:57,400 before print c. uper n equals quote 24446 15:23:55,240 --> 15:23:59,080 unquote and then I'll print this here 24447 15:23:57,400 --> 15:24:01,400 all right that was fast but let's try to 24448 15:23:59,080 --> 15:24:02,956 infer what's going on so line one just 24449 15:24:01,400 --> 15:24:04,720 gets input from the user stores it in a 24450 15:24:02,956 --> 15:24:07,000 variable called before line two 24451 15:24:04,720 --> 15:24:08,636 literally just prints after but doesn't 24452 15:24:07,000 --> 15:24:11,916 move the new line to uh the cursor to 24453 15:24:08,636 --> 15:24:14,560 the next line what it then does is this 24454 15:24:11,916 --> 15:24:16,796 and in C this was a little more annoying 24455 15:24:14,560 --> 15:24:18,956 you needed a for loop with I you needed 24456 15:24:16,796 --> 15:24:22,360 array in uh notation with the square 24457 15:24:18,956 --> 15:24:25,520 brackets but python if you say four 24458 15:24:22,360 --> 15:24:27,560 variable in string so for c for 24459 15:24:25,520 --> 15:24:30,240 character in string Python's going to 24460 15:24:27,560 --> 15:24:31,916 automatically assign C to the first word 24461 15:24:30,240 --> 15:24:33,360 letter that the user types in then on 24462 15:24:31,916 --> 15:24:34,480 the next iteration the second letter the 24463 15:24:33,360 --> 15:24:36,360 third letter and the fourth so you don't 24464 15:24:34,480 --> 15:24:38,480 need any square bracket notation you 24465 15:24:36,360 --> 15:24:40,880 just you see and python will do it for 24466 15:24:38,480 --> 15:24:42,680 you and just hand you back one at a time 24467 15:24:40,880 --> 15:24:45,956 each of the letters that the user has 24468 15:24:42,680 --> 15:24:48,480 typed in so if I go back over here and I 24469 15:24:45,956 --> 15:24:52,080 run for instance python of uppercase 24470 15:24:48,480 --> 15:24:54,560 dopy and I'll type in how about uh David 24471 15:24:52,080 --> 15:24:57,040 in all lowercase and hit enter you'll 24472 15:24:54,560 --> 15:24:58,840 now see that it's all uppercase instead 24473 15:24:57,040 --> 15:25:00,720 by iterating over it indeed one 24474 15:24:58,840 --> 15:25:02,320 character at a time but we already know 24475 15:25:00,720 --> 15:25:04,116 thanks to object-oriented programming 24476 15:25:02,320 --> 15:25:05,520 strings themselves have the 24477 15:25:04,116 --> 15:25:07,320 functionality built in to not just 24478 15:25:05,520 --> 15:25:09,520 uppercase Single Character 24479 15:25:07,320 --> 15:25:11,360 but the whole string so honestly this 24480 15:25:09,520 --> 15:25:15,276 was a bit of a silly exercise I don't 24481 15:25:11,360 --> 15:25:16,720 need to use a loop anymore like in see 24482 15:25:15,276 --> 15:25:18,320 and so some of the habits you've only 24483 15:25:16,720 --> 15:25:19,840 just developed in recent weeks it's time 24484 15:25:18,320 --> 15:25:22,116 to start breaking them when they're not 24485 15:25:19,840 --> 15:25:25,320 necessary I can create a variable called 24486 15:25:22,116 --> 15:25:27,560 after set it equal to before do uper 24487 15:25:25,320 --> 15:25:29,520 which indeed exists just like lower 24488 15:25:27,560 --> 15:25:32,436 exists and then what I can go ahead and 24489 15:25:29,520 --> 15:25:34,160 print out is for instance uh let's get 24490 15:25:32,436 --> 15:25:37,480 rid of this print line here and do it at 24491 15:25:34,160 --> 15:25:40,000 the end after and print the value of 24492 15:25:37,480 --> 15:25:42,080 that variable so now if I rerun 24493 15:25:40,000 --> 15:25:44,796 uppercase Pi type in David and all 24494 15:25:42,080 --> 15:25:47,320 lowercase I can just uppercase the whole 24495 15:25:44,796 --> 15:25:49,796 thing all at once because again in C in 24496 15:25:47,320 --> 15:25:53,436 Python you don't have to operate on 24497 15:25:49,796 --> 15:25:57,400 characters individually questions on any 24498 15:25:53,436 --> 15:25:59,880 of these tricks up until 24499 15:25:57,400 --> 15:26:01,400 now now all right how about a few other 24500 15:25:59,880 --> 15:26:03,720 techniques that we saw and C that we'll 24501 15:26:01,400 --> 15:26:06,956 bring back now in Python so it turns out 24502 15:26:03,720 --> 15:26:08,520 in Python there are other librar you can 24503 15:26:06,956 --> 15:26:11,080 use two that unlock even more 24504 15:26:08,520 --> 15:26:12,956 functionality so in C if you wanted 24505 15:26:11,080 --> 15:26:15,360 command line arguments you just change 24506 15:26:12,956 --> 15:26:18,520 the proo the signature for main to be 24507 15:26:15,360 --> 15:26:21,880 void instead of void to be int argc 24508 15:26:18,520 --> 15:26:24,276 comma string argv Open brackets for an 24509 15:26:21,880 --> 15:26:25,756 array or Char star eventually well it 24510 15:26:24,276 --> 15:26:27,160 turns out in Python that if you want to 24511 15:26:25,756 --> 15:26:28,720 access command line arguments it's a 24512 15:26:27,160 --> 15:26:31,560 little simpler but they're tucked away 24513 15:26:28,720 --> 15:26:34,596 in a library otherwise known as a module 24514 15:26:31,560 --> 15:26:36,240 called CIS the CIS or system module now 24515 15:26:34,596 --> 15:26:37,480 this is similar in spirit to the cs50 24516 15:26:36,240 --> 15:26:38,840 library and that it's got a bunch of 24517 15:26:37,480 --> 15:26:41,200 functionality built in but this one 24518 15:26:38,840 --> 15:26:44,200 comes with python itself so if I want to 24519 15:26:41,200 --> 15:26:46,680 create a program like greet py in VSS 24520 15:26:44,200 --> 15:26:50,560 code here let me go ahead and do this 24521 15:26:46,680 --> 15:26:52,480 from the CIS Library let's import argv 24522 15:26:50,560 --> 15:26:54,000 and that's just a thing that exists it's 24523 15:26:52,480 --> 15:26:56,000 not built into main because there is no 24524 15:26:54,000 --> 15:26:57,756 main per se anymore so it's tucked away 24525 15:26:56,000 --> 15:26:59,560 in that library and now I can do 24526 15:26:57,756 --> 15:27:02,400 something like this if the length of 24527 15:26:59,560 --> 15:27:04,480 argv equals equals 2 well let's go ahead 24528 15:27:02,400 --> 15:27:08,596 and print out something friendly like 24529 15:27:04,480 --> 15:27:11,596 hello comma AR V bracket 1 and then 24530 15:27:08,596 --> 15:27:13,796 close quotes else if the length of RV is 24531 15:27:11,596 --> 15:27:15,840 not equal to two let's just go ahead and 24532 15:27:13,796 --> 15:27:17,200 print out hello world now at a glance 24533 15:27:15,840 --> 15:27:18,756 this might look a little cryptic but 24534 15:27:17,200 --> 15:27:22,400 it's identical to what we did a few 24535 15:27:18,756 --> 15:27:24,680 weeks ago when I run this python of 24536 15:27:22,400 --> 15:27:28,000 greet with no arguments it just says 24537 15:27:24,680 --> 15:27:30,080 hello world but if I instead add a 24538 15:27:28,000 --> 15:27:33,480 command line argument like my first name 24539 15:27:30,080 --> 15:27:36,560 and hit enter now the length of arv is 24540 15:27:33,480 --> 15:27:39,520 no longer one it's going to be two and 24541 15:27:36,560 --> 15:27:42,436 so it prints out hello David instead so 24542 15:27:39,520 --> 15:27:44,680 the takeaway here is that whereas in C 24543 15:27:42,436 --> 15:27:48,756 argv technically contained the name of 24544 15:27:44,680 --> 15:27:50,680 your program like hello or dog greet and 24545 15:27:48,756 --> 15:27:52,840 then everything the human typed Python's 24546 15:27:50,680 --> 15:27:54,916 a little different in that because we're 24547 15:27:52,840 --> 15:27:57,596 using The Interpreter in this way 24548 15:27:54,916 --> 15:28:01,160 Technically when you run python of greet 24549 15:27:57,596 --> 15:28:03,320 py the length of arv is only one it 24550 15:28:01,160 --> 15:28:05,720 contains only greet so the name of the 24551 15:28:03,320 --> 15:28:07,160 file it does not unnecessarily contain 24552 15:28:05,720 --> 15:28:09,520 python itself because what's the point 24553 15:28:07,160 --> 15:28:10,796 of that being there omnes it does 24554 15:28:09,520 --> 15:28:14,000 contain the number of words that the 24555 15:28:10,796 --> 15:28:16,360 human typed after python itself so argv 24556 15:28:14,000 --> 15:28:18,360 is length one here argv is length two 24557 15:28:16,360 --> 15:28:20,916 here and that's why when it did equal to 24558 15:28:18,360 --> 15:28:23,360 I saw hello David instead of the default 24559 15:28:20,916 --> 15:28:25,320 hello world so same ability to access 24560 15:28:23,360 --> 15:28:27,116 command line arguments add these kinds 24561 15:28:25,320 --> 15:28:30,796 of inputs to your functions but you have 24562 15:28:27,116 --> 15:28:33,080 to unlock it by way of using Arvy uh 24563 15:28:30,796 --> 15:28:34,680 instead in this way if you want to see 24564 15:28:33,080 --> 15:28:38,080 all of the words you could do something 24565 15:28:34,680 --> 15:28:42,320 like this uh just as if we combine ideas 24566 15:28:38,080 --> 15:28:43,436 here for I in range of how about length 24567 15:28:42,320 --> 15:28:46,360 of 24568 15:28:43,436 --> 15:28:48,916 arv then I can do this print argv 24569 15:28:46,360 --> 15:28:51,240 bracket I all right a little cryptic but 24570 15:28:48,916 --> 15:28:53,520 line three is just a for Loop iterating 24571 15:28:51,240 --> 15:28:55,400 over the range of length of argv so if 24572 15:28:53,520 --> 15:28:57,916 the human types in two words the length 24573 15:28:55,400 --> 15:28:59,720 of argv will be two so this is just a 24574 15:28:57,916 --> 15:29:02,796 way of saying iterate over all of the 24575 15:28:59,720 --> 15:29:05,720 words in arv printing them one at a time 24576 15:29:02,796 --> 15:29:07,400 so python of greet dopy enter just 24577 15:29:05,720 --> 15:29:10,000 prints out the name of the program 24578 15:29:07,400 --> 15:29:12,360 python of greet dopy with David prints 24579 15:29:10,000 --> 15:29:14,080 out greet dopy and then David I can keep 24580 15:29:12,360 --> 15:29:17,360 running it though with more words and 24581 15:29:14,080 --> 15:29:20,160 they'll each get printed one at a time 24582 15:29:17,360 --> 15:29:21,840 but what's nice too about Python and 24583 15:29:20,160 --> 15:29:23,560 this is the point of this exercise 24584 15:29:21,840 --> 15:29:25,636 honestly this looks pretty cryptic this 24585 15:29:23,560 --> 15:29:28,240 is not very pleasant to look at if you 24586 15:29:25,636 --> 15:29:32,276 just want to iterate over every word in 24587 15:29:28,240 --> 15:29:35,796 a list which argv is watch what I can do 24588 15:29:32,276 --> 15:29:38,480 I can do for ARG or any variable name in 24589 15:29:35,796 --> 15:29:40,436 ARG V let me just now print out that 24590 15:29:38,480 --> 15:29:41,880 argument I could keep calling it I but I 24591 15:29:40,436 --> 15:29:44,480 seems weird when it's not a number so 24592 15:29:41,880 --> 15:29:48,636 I'm changing to AR as a word instead if 24593 15:29:44,480 --> 15:29:50,796 I now do python of greet py it does this 24594 15:29:48,636 --> 15:29:53,240 if I do python of greet David it does 24595 15:29:50,796 --> 15:29:55,200 that again David ma it does that again 24596 15:29:53,240 --> 15:29:56,636 so this is again why Python's just very 24597 15:29:55,200 --> 15:29:58,276 appealing you want to do something this 24598 15:29:56,636 --> 15:29:59,840 many times iterate over a list just say 24599 15:29:58,276 --> 15:30:01,796 it and it reads a little more like 24600 15:29:59,840 --> 15:30:03,756 English and there's even other fanciness 24601 15:30:01,796 --> 15:30:05,240 too if I may it's a little stupid that I 24602 15:30:03,756 --> 15:30:07,956 keep seeing the name of the program 24603 15:30:05,240 --> 15:30:10,756 greet dopy so I it'd be nice if I could 24604 15:30:07,956 --> 15:30:14,636 remove that python also supports what 24605 15:30:10,756 --> 15:30:15,636 are called slices of arrays sorry slices 24606 15:30:14,636 --> 15:30:19,240 of lists even though I get the 24607 15:30:15,636 --> 15:30:20,720 terminology confused if Arvy is a list 24608 15:30:19,240 --> 15:30:22,756 then it's going to print out everything 24609 15:30:20,720 --> 15:30:25,596 in it but if I want a slice of it that 24610 15:30:22,756 --> 15:30:28,560 starts at location one all the way to 24611 15:30:25,596 --> 15:30:30,200 the end you can use this funky syntax in 24612 15:30:28,560 --> 15:30:32,400 between the square brackets which we've 24613 15:30:30,200 --> 15:30:35,080 not seen yet that's going to start at 24614 15:30:32,400 --> 15:30:37,720 item one and go all the way to the end 24615 15:30:35,080 --> 15:30:40,080 and so this is a nice clever way of 24616 15:30:37,720 --> 15:30:42,080 slicing off if you will the very first 24617 15:30:40,080 --> 15:30:45,756 element because now when I run greet doy 24618 15:30:42,080 --> 15:30:48,560 David men I should only see David and 24619 15:30:45,756 --> 15:30:50,916 men if I only want one element I could 24620 15:30:48,560 --> 15:30:53,840 do one to two if I want all of them I 24621 15:30:50,916 --> 15:30:55,756 could do zero onward I could give myself 24622 15:30:53,840 --> 15:30:57,916 just two of one of them in this way so 24623 15:30:55,756 --> 15:30:59,400 you can play with the start value and 24624 15:30:57,916 --> 15:31:01,160 the end value in this way to sort of 24625 15:30:59,400 --> 15:31:04,040 slice and dice these lists in different 24626 15:31:01,160 --> 15:31:06,080 ways that would have been a pain in see 24627 15:31:04,040 --> 15:31:09,360 just because we didn't really have the 24628 15:31:06,080 --> 15:31:12,480 built-in support for manipulating arrays 24629 15:31:09,360 --> 15:31:14,160 as cleanly as this all right just so 24630 15:31:12,480 --> 15:31:16,596 you've seen it too though this one is 24631 15:31:14,160 --> 15:31:18,160 less uh exciting to see live if I go 24632 15:31:16,596 --> 15:31:19,680 ahead and create a quick program here it 24633 15:31:18,160 --> 15:31:22,160 turns out there's something else in the 24634 15:31:19,680 --> 15:31:24,360 CIS Library the ability to exit programs 24635 15:31:22,160 --> 15:31:25,840 either exiting with status code one or 24636 15:31:24,360 --> 15:31:28,080 zero as we've been doing anytime 24637 15:31:25,840 --> 15:31:29,560 something goes right or wrong so for 24638 15:31:28,080 --> 15:31:31,840 instance Let Me Whip up a quick program 24639 15:31:29,560 --> 15:31:36,200 that just says if the length of cy. 24640 15:31:31,840 --> 15:31:37,756 argv uh does not equal two then let's y 24641 15:31:36,200 --> 15:31:40,636 at the user and say you're missing a 24642 15:31:37,756 --> 15:31:44,596 command line argument otherwise command 24643 15:31:40,636 --> 15:31:47,400 line argument and let's then return cy. 24644 15:31:44,596 --> 15:31:49,596 exit one else let's go ahead and 24645 15:31:47,400 --> 15:31:53,320 logically just say print a formatted 24646 15:31:49,596 --> 15:31:55,916 string that says hello as before cy. 24647 15:31:53,320 --> 15:31:57,080 arv1 now things look different all of a 24648 15:31:55,916 --> 15:31:58,796 sudden but I'm doing something 24649 15:31:57,080 --> 15:32:01,916 deliberately first let's see what this 24650 15:31:58,796 --> 15:32:04,080 does so on line one I'm importing not 24651 15:32:01,916 --> 15:32:06,160 argv specifically I'm importing the 24652 15:32:04,080 --> 15:32:08,360 whole CIS library and we'll see why in 24653 15:32:06,160 --> 15:32:11,116 second well it turns out that this Arvy 24654 15:32:08,360 --> 15:32:13,756 the CIS library has not only the Arvy 24655 15:32:11,116 --> 15:32:15,956 list it also has a function called exit 24656 15:32:13,756 --> 15:32:17,840 which I'd like to be able to use as well 24657 15:32:15,956 --> 15:32:20,240 so it turns out that if you import a 24658 15:32:17,840 --> 15:32:21,636 whole library in this way that's fine 24659 15:32:20,240 --> 15:32:23,360 but you have to refer to the things 24660 15:32:21,636 --> 15:32:25,756 inside of it by using that same 24661 15:32:23,360 --> 15:32:28,320 library's name and a DOT to sort of 24662 15:32:25,756 --> 15:32:30,400 namespace it so to speak so here I'm 24663 15:32:28,320 --> 15:32:32,240 just saying if the user types in does 24664 15:32:30,400 --> 15:32:33,796 not type in two words yell at them with 24665 15:32:32,240 --> 15:32:35,520 missing command line argument and then 24666 15:32:33,796 --> 15:32:38,000 exit with one just like in C when you do 24667 15:32:35,520 --> 15:32:39,916 exit one just means something went wrong 24668 15:32:38,000 --> 15:32:41,436 otherwise print out hello to this and 24669 15:32:39,916 --> 15:32:42,916 this is starting to look cryptic but 24670 15:32:41,436 --> 15:32:45,000 it's just a combination of ideas the 24671 15:32:42,916 --> 15:32:48,240 curly braces means interpolate this 24672 15:32:45,000 --> 15:32:49,956 value plug it in here cy. Arvy is just 24673 15:32:48,240 --> 15:32:52,880 the verbose way of saying go into the 24674 15:32:49,956 --> 15:32:55,040 CIS library and get the argv variable 24675 15:32:52,880 --> 15:32:57,956 therein and bracket one of course just 24676 15:32:55,040 --> 15:33:01,080 like arrays and C is just the second 24677 15:32:57,956 --> 15:33:03,680 element at the prompt so when I run this 24678 15:33:01,080 --> 15:33:06,596 version now python of exit. py with no 24679 15:33:03,680 --> 15:33:09,160 arguments I get yelled at in this way if 24680 15:33:06,596 --> 15:33:11,796 however I type in two arguments total 24681 15:33:09,160 --> 15:33:14,200 the name of the file and my own name now 24682 15:33:11,796 --> 15:33:15,636 I get greeted with hello David and it's 24683 15:33:14,200 --> 15:33:17,720 the same idea before this was a very 24684 15:33:15,636 --> 15:33:20,276 low-level technique but same thing here 24685 15:33:17,720 --> 15:33:22,840 if you do Echo dollar sign question mark 24686 15:33:20,276 --> 15:33:25,400 enter you'll see the exit code of your 24687 15:33:22,840 --> 15:33:28,116 program so if I do this incorrectly 24688 15:33:25,400 --> 15:33:30,560 again let me rerun it without my name 24689 15:33:28,116 --> 15:33:32,796 enter I get yelled at but if I do Echo 24690 15:33:30,560 --> 15:33:35,636 dollar sign question mark there's the 24691 15:33:32,796 --> 15:33:38,880 secret one that's returned again just to 24692 15:33:35,636 --> 15:33:42,916 show You parody with C in this case 24693 15:33:38,880 --> 15:33:42,916 questions now on any of these techniques 24694 15:33:43,436 --> 15:33:47,400 here all right how about something 24695 15:33:45,916 --> 15:33:49,360 that's a little more powerful too we 24696 15:33:47,400 --> 15:33:51,000 spent so much time in week zero and one 24697 15:33:49,360 --> 15:33:53,000 doing searching and then eventually 24698 15:33:51,000 --> 15:33:54,596 sorting in week three well it turns out 24699 15:33:53,000 --> 15:33:55,880 python can help with some of this too 24700 15:33:54,596 --> 15:33:57,840 let me go ahead and create a program 24701 15:33:55,880 --> 15:34:00,000 called names. py that's just going to be 24702 15:33:57,840 --> 15:34:02,000 an opportunity to maybe search over a 24703 15:34:00,000 --> 15:34:04,596 whole bunch of names let me go ahead and 24704 15:34:02,000 --> 15:34:06,720 import CIS and then just so I have 24705 15:34:04,596 --> 15:34:08,520 access to exit and let me go ahead and 24706 15:34:06,720 --> 15:34:10,680 create a variable called names that's 24707 15:34:08,520 --> 15:34:14,200 going to be a list with a whole bunch of 24708 15:34:10,680 --> 15:34:18,276 names uh how about Here Charlie and Fred 24709 15:34:14,200 --> 15:34:20,436 and George and Jenny and Percy and 24710 15:34:18,276 --> 15:34:22,000 lastly Ron so a whole bunch of names 24711 15:34:20,436 --> 15:34:23,916 here and you know it'd be a little 24712 15:34:22,000 --> 15:34:25,840 Annoying to implement code that iterates 24713 15:34:23,916 --> 15:34:28,080 over that from left to right and see 24714 15:34:25,840 --> 15:34:29,400 searching for one of those names in fact 24715 15:34:28,080 --> 15:34:31,360 what name well let's go ahead and ask 24716 15:34:29,400 --> 15:34:32,956 the user to input the name that they 24717 15:34:31,360 --> 15:34:35,560 want to search for so that we can tell 24718 15:34:32,956 --> 15:34:37,956 them if the name is there or not and we 24719 15:34:35,560 --> 15:34:40,720 could do this similar to C in Python 24720 15:34:37,956 --> 15:34:43,756 doing something like this so for n in 24721 15:34:40,720 --> 15:34:46,480 names where n is just a variable to 24722 15:34:43,756 --> 15:34:49,000 iterate over each name if how about the 24723 15:34:46,480 --> 15:34:52,360 name I'm looking for equals the current 24724 15:34:49,000 --> 15:34:54,400 name in the list AKA n let's print out 24725 15:34:52,360 --> 15:34:56,916 something friendly like found and then 24726 15:34:54,400 --> 15:34:59,636 let's do cy. Exit 0 to indicate that we 24727 15:34:56,916 --> 15:35:01,320 found whoever that is otherwise if we 24728 15:34:59,636 --> 15:35:02,840 get all the way to the bottom here 24729 15:35:01,320 --> 15:35:04,560 outside of this Loop let's just print 24730 15:35:02,840 --> 15:35:07,436 not found because if we haven't exited 24731 15:35:04,560 --> 15:35:09,520 yet and then let's just exit with one 24732 15:35:07,436 --> 15:35:13,320 just to be clear I can continue 24733 15:35:09,520 --> 15:35:15,880 importing all of CIS or I could do from 24734 15:35:13,320 --> 15:35:18,840 CIS import exit and then I could get rid 24735 15:35:15,880 --> 15:35:20,240 of CIS dot everywhere else but you know 24736 15:35:18,840 --> 15:35:22,000 sometimes it's helpful to know exactly 24737 15:35:20,240 --> 15:35:24,756 where functions came from so this two is 24738 15:35:22,000 --> 15:35:26,116 just a matter of style in this case all 24739 15:35:24,756 --> 15:35:29,200 right so let's go ahead and run this 24740 15:35:26,116 --> 15:35:31,436 python of names. piy and let's look for 24741 15:35:29,200 --> 15:35:33,040 like Ron all the way at the end you know 24742 15:35:31,436 --> 15:35:35,320 all right he's found and let's search 24743 15:35:33,040 --> 15:35:38,040 for someone outside of the family here 24744 15:35:35,320 --> 15:35:39,596 like herion not found okay so it seems 24745 15:35:38,040 --> 15:35:42,560 to be working in this way but I've 24746 15:35:39,596 --> 15:35:44,840 essentially implemented what 24747 15:35:42,560 --> 15:35:49,116 algorithm what algorithm would this seem 24748 15:35:44,840 --> 15:35:50,000 to be per lines seven and eight and nine 24749 15:35:49,116 --> 15:35:52,200 and 24750 15:35:50,000 --> 15:35:53,560 10 yeah so it's just linear search it's 24751 15:35:52,200 --> 15:35:55,360 a loop even though the syntax is a 24752 15:35:53,560 --> 15:35:56,720 little more succinct today and it's just 24753 15:35:55,360 --> 15:35:59,080 iterating over the whole thing well 24754 15:35:56,720 --> 15:36:00,956 honestly we've seen an even more tur way 24755 15:35:59,080 --> 15:36:03,240 to do this in Python and this again is 24756 15:36:00,956 --> 15:36:05,276 what makes it a more pleasant language 24757 15:36:03,240 --> 15:36:07,680 sometimes why don't I just do this 24758 15:36:05,276 --> 15:36:09,680 instead in of iterating one at a time 24759 15:36:07,680 --> 15:36:12,520 why don't I just say this let me go 24760 15:36:09,680 --> 15:36:14,916 ahead and change my condition to just be 24761 15:36:12,520 --> 15:36:18,360 How about if the name we're looking for 24762 15:36:14,916 --> 15:36:20,480 is in the names list we're done we found 24763 15:36:18,360 --> 15:36:22,520 it use the in preposition that we've 24764 15:36:20,480 --> 15:36:24,436 seen a couple of times now that itself 24765 15:36:22,520 --> 15:36:27,116 asks the question is something in 24766 15:36:24,436 --> 15:36:28,720 something else and python will take care 24767 15:36:27,116 --> 15:36:31,240 of linear search for us and it's going 24768 15:36:28,720 --> 15:36:33,160 to work exactly the same if I do python 24769 15:36:31,240 --> 15:36:34,720 of names. search for run it's still 24770 15:36:33,160 --> 15:36:36,840 going to find him and it's still going 24771 15:36:34,720 --> 15:36:38,480 to do it linearly in this case but I 24772 15:36:36,840 --> 15:36:42,276 don't have to write all of the lower 24773 15:36:38,480 --> 15:36:47,000 level code myself in this 24774 15:36:42,276 --> 15:36:49,080 case questions now on any of this the 24775 15:36:47,000 --> 15:36:52,400 code's just getting shorter and 24776 15:36:49,080 --> 15:36:54,756 shorter now what about uh let's see what 24777 15:36:52,400 --> 15:36:56,400 else might we have here how about this 24778 15:36:54,756 --> 15:36:57,796 it turns out let's go ahead and 24779 15:36:56,400 --> 15:36:59,720 Implement that phone book that we 24780 15:36:57,796 --> 15:37:01,080 started metaphorically with in the 24781 15:36:59,720 --> 15:37:03,436 beginning of the course let's code up a 24782 15:37:01,080 --> 15:37:05,756 program called phonebook dopy and in 24783 15:37:03,436 --> 15:37:07,720 this case let's go ahead and let's 24784 15:37:05,756 --> 15:37:09,796 create a dictionary this time recall 24785 15:37:07,720 --> 15:37:11,680 that a dictionary is a little something 24786 15:37:09,796 --> 15:37:14,880 that implements something like this like 24787 15:37:11,680 --> 15:37:17,360 a two column table that's got keys and 24788 15:37:14,880 --> 15:37:18,840 values words and definitions names and 24789 15:37:17,360 --> 15:37:21,240 numbers and let's focus on the last of 24790 15:37:18,840 --> 15:37:22,840 those names and numbers in this case 24791 15:37:21,240 --> 15:37:24,796 well I claimed earlier that python has 24792 15:37:22,840 --> 15:37:26,756 built-in support for dictionaries dict 24793 15:37:24,796 --> 15:37:28,560 objects that you can create with one 24794 15:37:26,756 --> 15:37:29,880 line I didn't need it for speller 24795 15:37:28,560 --> 15:37:31,916 because a set is sufficient when you 24796 15:37:29,880 --> 15:37:33,916 only want one of the keys or the values 24797 15:37:31,916 --> 15:37:35,916 not both but now I want some names and 24798 15:37:33,916 --> 15:37:37,916 numbers so it turns out in pyth python 24799 15:37:35,916 --> 15:37:40,480 you can create an empty dictionary by 24800 15:37:37,916 --> 15:37:42,436 saying dict open parenthesis close and 24801 15:37:40,480 --> 15:37:44,636 that just gives you essentially a chart 24802 15:37:42,436 --> 15:37:46,916 that looks like this with nothing in it 24803 15:37:44,636 --> 15:37:50,596 or there's more succinct syntax you can 24804 15:37:46,916 --> 15:37:52,596 alternatively do uh this with two curly 24805 15:37:50,596 --> 15:37:54,400 braces instead and in fact I've been 24806 15:37:52,596 --> 15:37:58,400 using a shortcut all this time when I 24807 15:37:54,400 --> 15:38:01,200 had a list earlier where my 24808 15:37:58,400 --> 15:38:03,160 variable uh was called scores and I did 24809 15:38:01,200 --> 15:38:05,636 this that was actually the shorthand 24810 15:38:03,160 --> 15:38:07,636 version of this hey python give me an 24811 15:38:05,636 --> 15:38:09,596 empty list so there's different Syntax 24812 15:38:07,636 --> 15:38:12,560 for achieving the same goal in this case 24813 15:38:09,596 --> 15:38:15,480 if I want a dictionary for people I can 24814 15:38:12,560 --> 15:38:17,636 either do this or more commonly just two 24815 15:38:15,480 --> 15:38:19,240 curly braces like that all right well 24816 15:38:17,636 --> 15:38:20,916 what do I want to put in this well let 24817 15:38:19,240 --> 15:38:22,596 me actually put some things in this and 24818 15:38:20,916 --> 15:38:24,560 I'm going to just move my Clos curly 24819 15:38:22,596 --> 15:38:27,040 brace to a new line if I want to 24820 15:38:24,560 --> 15:38:29,756 implement this idea of keys and values 24821 15:38:27,040 --> 15:38:33,040 the way you do this in Python is key 24822 15:38:29,756 --> 15:38:35,116 colon value comma key colon value so 24823 15:38:33,040 --> 15:38:36,796 you'd implement it more in code so for 24824 15:38:35,116 --> 15:38:39,040 instance if I want Carter to be the 24825 15:38:36,796 --> 15:38:43,200 first key in my phone book and I want 24826 15:38:39,040 --> 15:38:45,596 his number to be+ one 617495 1000 I can 24827 15:38:43,200 --> 15:38:48,040 put that as the corresponding value the 24828 15:38:45,596 --> 15:38:50,520 colon is in between both are strings or 24829 15:38:48,040 --> 15:38:52,276 stirs so I've quoted both deliberately 24830 15:38:50,520 --> 15:38:53,680 if I want to add myself I can put a 24831 15:38:52,276 --> 15:38:55,320 comma and then just to keep things 24832 15:38:53,680 --> 15:38:56,956 pretty I'm moving the cursor to the next 24833 15:38:55,320 --> 15:38:58,796 line but that's not strictly required 24834 15:38:56,956 --> 15:39:03,040 aesthetically it's just good style and 24835 15:38:58,796 --> 15:39:06,596 here I might do+ 1 949 468 uh 24836 15:39:03,040 --> 15:39:08,796 2750 and now I have a dictionary that 24837 15:39:06,596 --> 15:39:11,080 essentially has two rows here David uh 24838 15:39:08,796 --> 15:39:12,680 Carter and his number and David and his 24839 15:39:11,080 --> 15:39:14,160 number as well and if I kept adding to 24840 15:39:12,680 --> 15:39:15,956 this this call this chart would just get 24841 15:39:14,160 --> 15:39:17,756 longer and longer suppose I want to 24842 15:39:15,956 --> 15:39:19,720 search for one of our numbers well let's 24843 15:39:17,756 --> 15:39:21,596 prompt the user for the name for whose 24844 15:39:19,720 --> 15:39:23,276 number you want to search by getting 24845 15:39:21,596 --> 15:39:25,320 string or you know what we don't need 24846 15:39:23,276 --> 15:39:28,400 the cs50 library let's just use input 24847 15:39:25,320 --> 15:39:30,720 and prompt the user for a name and now 24848 15:39:28,400 --> 15:39:35,320 we can use this super syntax and just 24849 15:39:30,720 --> 15:39:38,520 say if name in people print the format 24850 15:39:35,320 --> 15:39:42,756 added string number colon and here we 24851 15:39:38,520 --> 15:39:44,636 can do this people bracket name okay so 24852 15:39:42,756 --> 15:39:46,840 this is getting kind of cool kind of 24853 15:39:44,636 --> 15:39:49,080 quickly kind of confusingly so let me 24854 15:39:46,840 --> 15:39:52,000 run this python of phone 24855 15:39:49,080 --> 15:39:54,756 book. let's type in Carter and indeed I 24856 15:39:52,000 --> 15:39:58,160 see his number let's run it again with 24857 15:39:54,756 --> 15:40:00,916 David and I see my number here so what's 24858 15:39:58,160 --> 15:40:03,200 going on well it turns out that a 24859 15:40:00,916 --> 15:40:04,916 dictionary is very similar in spirit to 24860 15:40:03,200 --> 15:40:07,596 a list it's actually very similar in 24861 15:40:04,916 --> 15:40:10,720 spirit to an array in C but instead of 24862 15:40:07,596 --> 15:40:13,276 being limited to keys that are numbers 24863 15:40:10,720 --> 15:40:15,596 like bracket 0o bracket 1 bracket two 24864 15:40:13,276 --> 15:40:18,240 you can actually use words and that's 24865 15:40:15,596 --> 15:40:20,840 all I'm doing here on line eight if I 24866 15:40:18,240 --> 15:40:22,560 want to check for the name Carter which 24867 15:40:20,840 --> 15:40:25,400 is currently in this variable called 24868 15:40:22,560 --> 15:40:28,000 name I can index into my people 24869 15:40:25,400 --> 15:40:30,680 dictionary using not a number but using 24870 15:40:28,000 --> 15:40:33,240 literally a string the name Carter or 24871 15:40:30,680 --> 15:40:35,040 David or anything else to make this 24872 15:40:33,240 --> 15:40:36,796 clear too notice that I'm at the moment 24873 15:40:35,040 --> 15:40:39,276 you using this format string which is 24874 15:40:36,796 --> 15:40:41,160 adding some undue complexity but I could 24875 15:40:39,276 --> 15:40:42,560 clarify this perhaps further as this I 24876 15:40:41,160 --> 15:40:45,040 could give myself another variable 24877 15:40:42,560 --> 15:40:47,720 called number set it equal to the people 24878 15:40:45,040 --> 15:40:49,796 dictionary indexing into it using the 24879 15:40:47,720 --> 15:40:51,840 current name and now I can shorten this 24880 15:40:49,796 --> 15:40:55,040 to make it clear that all I'm doing is 24881 15:40:51,840 --> 15:40:57,436 printing the value of that and in fact I 24882 15:40:55,040 --> 15:40:59,480 can do this even more cryptically if I 24883 15:40:57,436 --> 15:41:01,480 this would be weird to do but if I only 24884 15:40:59,480 --> 15:41:03,916 ever want to show David's phone number 24885 15:41:01,480 --> 15:41:06,956 and never Carter's I can literally quote 24886 15:41:03,916 --> 15:41:08,560 unquote index into the people dictionary 24887 15:41:06,956 --> 15:41:10,596 because now when I run this even if I 24888 15:41:08,560 --> 15:41:12,520 type Carter I'm going to get back my 24889 15:41:10,596 --> 15:41:14,756 number instead but that's all that's 24890 15:41:12,520 --> 15:41:18,520 happening if I undo that because that's 24891 15:41:14,756 --> 15:41:21,240 now a bug but I index into it using the 24892 15:41:18,520 --> 15:41:22,680 value of name dictionaries are just so 24893 15:41:21,240 --> 15:41:24,116 wonderfully convenient because now you 24894 15:41:22,680 --> 15:41:26,596 can associate anything with anything 24895 15:41:24,116 --> 15:41:29,436 else but not using numbers but entire 24896 15:41:26,596 --> 15:41:31,560 keywords instead so here's how if in 24897 15:41:29,436 --> 15:41:34,276 spell we gave you not just words but 24898 15:41:31,560 --> 15:41:36,720 hundreds of thousands of definitions as 24899 15:41:34,276 --> 15:41:38,160 well you could essentially store them as 24900 15:41:36,720 --> 15:41:39,756 this and then when the human wants to 24901 15:41:38,160 --> 15:41:41,480 look up a definition in a proper 24902 15:41:39,756 --> 15:41:43,360 dictionary not just for spellchecking 24903 15:41:41,480 --> 15:41:46,320 you could index into the dictionary 24904 15:41:43,360 --> 15:41:49,756 using square brackets and get back the 24905 15:41:46,320 --> 15:41:53,436 definition in English as well 24906 15:41:49,756 --> 15:41:53,436 questions on this 24907 15:42:04,080 --> 15:42:08,116 yeah 24908 15:42:05,756 --> 15:42:10,436 location a really good question so how 24909 15:42:08,116 --> 15:42:12,276 to summarize how is python finding that 24910 15:42:10,436 --> 15:42:15,040 name within that dictionary this is 24911 15:42:12,276 --> 15:42:17,320 where honestly speller in pet 5 is what 24912 15:42:15,040 --> 15:42:18,596 Python's all about so you have struggled 24913 15:42:17,320 --> 15:42:19,840 are struggling with implementing your 24914 15:42:18,596 --> 15:42:21,560 own spell checker and implementing your 24915 15:42:19,840 --> 15:42:23,796 own hash table and recall that per last 24916 15:42:21,560 --> 15:42:26,320 week the goal of a hash table is to 24917 15:42:23,796 --> 15:42:28,000 ideally get constant time access not 24918 15:42:26,320 --> 15:42:30,436 something linear which is slow and even 24919 15:42:28,000 --> 15:42:33,160 better than something uh uh logarithmic 24920 15:42:30,436 --> 15:42:35,116 like log base 2 of n so Python and the 24921 15:42:33,160 --> 15:42:37,276 really smart people who invented it they 24922 15:42:35,116 --> 15:42:40,276 have written the code that does its best 24923 15:42:37,276 --> 15:42:41,480 to give you constant time searches of 24924 15:42:40,276 --> 15:42:42,880 dictionaries and they're not always 24925 15:42:41,480 --> 15:42:44,320 going to succeed just as you and your 24926 15:42:42,880 --> 15:42:45,756 own problem set probably going to have 24927 15:42:44,320 --> 15:42:47,520 some collisions once in a while and 24928 15:42:45,756 --> 15:42:49,520 start to have chains of length lists of 24929 15:42:47,520 --> 15:42:51,040 words but this is where again you defer 24930 15:42:49,520 --> 15:42:52,880 to someone else someone smarter than you 24931 15:42:51,040 --> 15:42:54,680 someone with more time than you to solve 24932 15:42:52,880 --> 15:42:56,240 these problems for you and if you read 24933 15:42:54,680 --> 15:42:58,480 Python's documentation you'll see that 24934 15:42:56,240 --> 15:43:00,520 it doesn't guarantee constant time but 24935 15:42:58,480 --> 15:43:03,400 it's going to ideally optimize the data 24936 15:43:00,520 --> 15:43:05,040 structure for you to get as fast as 24937 15:43:03,400 --> 15:43:07,160 possible and of all of the data 24938 15:43:05,040 --> 15:43:09,000 structures um like a dictionary a 24939 15:43:07,160 --> 15:43:10,596 hashtable is really like the Swiss army 24940 15:43:09,000 --> 15:43:12,320 knife of computing because it just lets 24941 15:43:10,596 --> 15:43:14,116 you associate something with something 24942 15:43:12,320 --> 15:43:15,756 else and even though we keep focusing on 24943 15:43:14,116 --> 15:43:17,160 names and numbers that's a really 24944 15:43:15,756 --> 15:43:18,796 powerful thing because it's more 24945 15:43:17,160 --> 15:43:20,796 powerful than lists and arrays which are 24946 15:43:18,796 --> 15:43:22,756 only numbers and something else now you 24947 15:43:20,796 --> 15:43:24,756 can have any sorts of relationships 24948 15:43:22,756 --> 15:43:26,560 instead all right let me show a few 24949 15:43:24,756 --> 15:43:28,400 other examples before we culminate with 24950 15:43:26,560 --> 15:43:30,596 some more powerful techniques in pythons 24951 15:43:28,400 --> 15:43:32,596 thanks to libraries how about this 24952 15:43:30,596 --> 15:43:35,080 problem we encountered in week four 24953 15:43:32,596 --> 15:43:38,160 which was this let me code up a program 24954 15:43:35,080 --> 15:43:41,160 called again compare. Pi here but this 24955 15:43:38,160 --> 15:43:43,400 time compare to Strings and not numbers 24956 15:43:41,160 --> 15:43:46,240 so let me for instance do uh get one 24957 15:43:43,400 --> 15:43:47,636 string from the user called s just for 24958 15:43:46,240 --> 15:43:50,520 the sake of discussion let me get 24959 15:43:47,636 --> 15:43:51,680 another string from the user uh called T 24960 15:43:50,520 --> 15:43:54,796 so that we can actually do some 24961 15:43:51,680 --> 15:43:56,040 comparison here and if s equals equals T 24962 15:43:54,796 --> 15:43:58,520 let's go ahead and print out that 24963 15:43:56,040 --> 15:44:00,276 they're the same else let's go ahead and 24964 15:43:58,520 --> 15:44:01,880 print out that they're different so this 24965 15:44:00,276 --> 15:44:04,596 is very similar to what we did in week 24966 15:44:01,880 --> 15:44:06,956 four but in week four recall we did this 24967 15:44:04,596 --> 15:44:10,000 spef specifically because we had 24968 15:44:06,956 --> 15:44:12,360 encountered a problem for instance if I 24969 15:44:10,000 --> 15:44:16,000 run whoops 24970 15:44:12,360 --> 15:44:16,000 uh if I 24971 15:44:16,680 --> 15:44:24,436 run what's going on uh input 24972 15:44:21,320 --> 15:44:24,436 T come 24973 15:44:24,480 --> 15:44:30,756 on oh the okay wow okay long day all 24974 15:44:28,560 --> 15:44:33,400 right if I run the proper command python 24975 15:44:30,756 --> 15:44:36,200 of compare. py then let's go ahead and 24976 15:44:33,400 --> 15:44:38,840 type in something like cat in all 24977 15:44:36,200 --> 15:44:41,720 lowercase cats in all lowercase and 24978 15:44:38,840 --> 15:44:44,160 they're the same uh if though I do this 24979 15:44:41,720 --> 15:44:45,840 again with dog and dog they're the same 24980 15:44:44,160 --> 15:44:47,160 and of course cat and dog they're 24981 15:44:45,840 --> 15:44:50,116 different but does anyone recall from 24982 15:44:47,160 --> 15:44:53,520 two weeks ago when I typed in my name 24983 15:44:50,116 --> 15:44:56,520 twice both identically capitalized what 24984 15:44:53,520 --> 15:44:58,916 did it say that they were in 24985 15:44:56,520 --> 15:45:00,680 fact different and why was that like why 24986 15:44:58,916 --> 15:45:03,560 were two strings in C different even 24987 15:45:00,680 --> 15:45:06,320 though I typed literally the same 24988 15:45:03,560 --> 15:45:07,916 thing two different places in memory so 24989 15:45:06,320 --> 15:45:09,520 each string might look the same 24990 15:45:07,916 --> 15:45:11,796 aesthetically but of course was stored 24991 15:45:09,520 --> 15:45:13,956 elsewhere in memory and yet python 24992 15:45:11,796 --> 15:45:16,360 appears to be using the equality 24993 15:45:13,956 --> 15:45:18,000 operator equals equals like you and I 24994 15:45:16,360 --> 15:45:21,040 would expect as humans actually 24995 15:45:18,000 --> 15:45:23,360 comparing for us Char by Char in each of 24996 15:45:21,040 --> 15:45:25,116 those strings for actual equality so 24997 15:45:23,360 --> 15:45:27,080 this is a feature of python in that it's 24998 15:45:25,116 --> 15:45:28,720 just easier to do and why well this 24999 15:45:27,080 --> 15:45:30,436 derives from the reality that in Python 25000 15:45:28,720 --> 15:45:32,276 there are no pointers anymore there's no 25001 15:45:30,436 --> 15:45:34,080 underlying memory management it's not up 25002 15:45:32,276 --> 15:45:36,276 to you now to worry about those lower 25003 15:45:34,080 --> 15:45:38,636 level details the language itself takes 25004 15:45:36,276 --> 15:45:40,596 care of that for you and so similarly if 25005 15:45:38,636 --> 15:45:42,796 I do this and don't ask the user for two 25006 15:45:40,596 --> 15:45:44,880 strings but just one and then I do 25007 15:45:42,796 --> 15:45:47,080 something like this how about give 25008 15:45:44,880 --> 15:45:48,840 myself a second variable T set it equal 25009 15:45:47,080 --> 15:45:51,080 to s. 25010 15:45:48,840 --> 15:45:53,560 capitalize which note is not the same as 25011 15:45:51,080 --> 15:45:55,320 upper capitalize by Design per Python's 25012 15:45:53,560 --> 15:45:58,160 documentation will only capitalize the 25013 15:45:55,320 --> 15:46:00,680 first letter for you I can now print out 25014 15:45:58,160 --> 15:46:02,880 say two F strings here what the value of 25015 15:46:00,680 --> 15:46:05,240 s is and then let me print out with 25016 15:46:02,880 --> 15:46:07,840 another F string what the value of t is 25017 15:46:05,240 --> 15:46:10,756 and recall that in C this was a problem 25018 15:46:07,840 --> 15:46:13,520 because if you capitalize s and store it 25019 15:46:10,756 --> 15:46:16,436 in t we accidentally capitalized both s 25020 15:46:13,520 --> 15:46:18,756 and t but in this case in Python when I 25021 15:46:16,436 --> 15:46:22,596 actually run this and type in cat in all 25022 15:46:18,756 --> 15:46:25,160 lowercase the original s is unchanged 25023 15:46:22,596 --> 15:46:27,560 because when I use capitalize on line 25024 15:46:25,160 --> 15:46:30,916 three this is indeed capitalizing s but 25025 15:46:27,560 --> 15:46:33,080 it's returning a copy of the result it 25026 15:46:30,916 --> 15:46:35,320 cannot change S itself because again for 25027 15:46:33,080 --> 15:46:37,160 that technical term s is IM mutable 25028 15:46:35,320 --> 15:46:39,480 strings once they exist cannot be 25029 15:46:37,160 --> 15:46:42,680 changed themselves but you can return 25030 15:46:39,480 --> 15:46:44,560 copies and modified mutated copies of 25031 15:46:42,680 --> 15:46:46,040 those same strings so in short all of 25032 15:46:44,560 --> 15:46:48,400 those headaches we encountered in week 25033 15:46:46,040 --> 15:46:50,720 four are now solved really in the way 25034 15:46:48,400 --> 15:46:52,240 you might expect and here's another one 25035 15:46:50,720 --> 15:46:54,560 that we dwell on in week four with the 25036 15:46:52,240 --> 15:46:57,080 colored uh liquid in glasses let me code 25037 15:46:54,560 --> 15:47:00,320 up a program called swap. pi and in 25038 15:46:57,080 --> 15:47:02,956 swap. pi let me set x equal to 1 y equal 25039 15:47:00,320 --> 15:47:06,436 to 2 and then let me just print out an F 25040 15:47:02,956 --> 15:47:10,680 string here so how about X X is this 25041 15:47:06,436 --> 15:47:12,480 comma Y is that and then let me do that 25042 15:47:10,680 --> 15:47:14,756 twice just for the sake of demonstration 25043 15:47:12,480 --> 15:47:16,596 and in here recall that we had to create 25044 15:47:14,756 --> 15:47:18,796 a swap function but then we had to pass 25045 15:47:16,596 --> 15:47:21,240 it in by reference with the Amper sand 25046 15:47:18,796 --> 15:47:23,400 and like oh my God like that was kind of 25047 15:47:21,240 --> 15:47:26,240 peak complexity and see well if you want 25048 15:47:23,400 --> 15:47:29,956 to swap X and Y in Python you could do X 25049 15:47:26,240 --> 15:47:32,680 comma yal y comma X and now python of 25050 15:47:29,956 --> 15:47:35,276 swap whoops python of 25051 15:47:32,680 --> 15:47:37,240 swap. and there we go all of that's 25052 15:47:35,276 --> 15:47:39,680 handled for you sort of like a shell 25053 15:47:37,240 --> 15:47:43,160 game without even a temporary variable 25054 15:47:39,680 --> 15:47:45,680 in mind so what more can we do here how 25055 15:47:43,160 --> 15:47:47,560 about a few final building blocks and 25056 15:47:45,680 --> 15:47:49,200 these related now to files from that 25057 15:47:47,560 --> 15:47:51,796 week four suppose that I want to save 25058 15:47:49,200 --> 15:47:53,560 some names and numbers in like a a CSV 25059 15:47:51,796 --> 15:47:55,756 file comma separated values which is 25060 15:47:53,560 --> 15:47:59,276 like a very lightweight spreadsheet well 25061 15:47:55,756 --> 15:48:02,480 first let me create a uh phonebook CSV 25062 15:47:59,276 --> 15:48:04,796 file that just has name comma number as 25063 15:48:02,480 --> 15:48:06,200 like the first row there but after after 25064 15:48:04,796 --> 15:48:09,160 that I'm going to go ahead now and code 25065 15:48:06,200 --> 15:48:10,880 up a phonebook dopy program that 25066 15:48:09,160 --> 15:48:12,636 actually allows me to add things to this 25067 15:48:10,880 --> 15:48:14,680 phone book so let me split my screen 25068 15:48:12,636 --> 15:48:17,756 here so that we can see the old and the 25069 15:48:14,680 --> 15:48:20,596 new and down here in my code for phone 25070 15:48:17,756 --> 15:48:22,116 book. in this new and improved version 25071 15:48:20,596 --> 15:48:24,796 I'm going to actually import a whole 25072 15:48:22,116 --> 15:48:26,436 other Library this one called CSV and 25073 15:48:24,796 --> 15:48:28,080 here too especially for people in data 25074 15:48:26,436 --> 15:48:30,360 science and the like really like being 25075 15:48:28,080 --> 15:48:31,360 in a manipulate files and data that 25076 15:48:30,360 --> 15:48:33,880 might very well be stored in 25077 15:48:31,360 --> 15:48:35,436 spreadsheets or csvs comma separated 25078 15:48:33,880 --> 15:48:38,240 values which we we saw briefly in week 25079 15:48:35,436 --> 15:48:40,796 four in phonebook then it suffices to 25080 15:48:38,240 --> 15:48:42,756 just import CSV after reading the 25081 15:48:40,796 --> 15:48:45,040 documentation therefore because this is 25082 15:48:42,756 --> 15:48:47,560 going to give me functionality in code 25083 15:48:45,040 --> 15:48:50,040 related to CSV files so here's how I 25084 15:48:47,560 --> 15:48:52,320 might open a file in Python I literally 25085 15:48:50,040 --> 15:48:54,320 call open it's not fop now it's just 25086 15:48:52,320 --> 15:48:55,916 open and I open this file called 25087 15:48:54,320 --> 15:48:58,596 phonebook.com 25088 15:48:55,916 --> 15:49:00,080 going to open it in a pend mode not 25089 15:48:58,596 --> 15:49:02,596 right where it would change the whole 25090 15:49:00,080 --> 15:49:05,200 thing I want to append new line at a 25091 15:49:02,596 --> 15:49:07,040 time uh after this I want to get maybe 25092 15:49:05,200 --> 15:49:09,040 how about a name from the user so let's 25093 15:49:07,040 --> 15:49:11,320 prompt the user for some input for their 25094 15:49:09,040 --> 15:49:14,400 name and then let's prompt the user for 25095 15:49:11,320 --> 15:49:16,956 a number as well using input prompting 25096 15:49:14,400 --> 15:49:18,320 for number all right and now this is a 25097 15:49:16,956 --> 15:49:19,956 little cryptic and you'd only know this 25098 15:49:18,320 --> 15:49:23,276 from the documentation but if you want 25099 15:49:19,956 --> 15:49:25,400 to write line rows to a CSV file that 25100 15:49:23,276 --> 15:49:27,560 you can then view in Excel or the like 25101 15:49:25,400 --> 15:49:28,880 you can do this give me a variable 25102 15:49:27,560 --> 15:49:31,636 called writer but I could call it 25103 15:49:28,880 --> 15:49:34,560 anything I want let me use a CSV do 25104 15:49:31,636 --> 15:49:37,200 writer function that comes with this CSV 25105 15:49:34,560 --> 15:49:39,720 Library passing in the file this is like 25106 15:49:37,200 --> 15:49:41,636 saying hey python treat this open file 25107 15:49:39,720 --> 15:49:43,160 as a CSV file so that things are 25108 15:49:41,636 --> 15:49:45,756 separated with commas and nicely 25109 15:49:43,160 --> 15:49:48,000 formatted in rows and columns now I'm 25110 15:49:45,756 --> 15:49:50,240 going to do this use that writer to 25111 15:49:48,000 --> 15:49:52,520 write a row well what do I want to write 25112 15:49:50,240 --> 15:49:55,040 I want to write a short list namely the 25113 15:49:52,520 --> 15:49:56,956 current name and the current number to 25114 15:49:55,040 --> 15:49:59,080 that file but I don't want to use FR 25115 15:49:56,956 --> 15:50:01,080 printf and percent s and all of that 25116 15:49:59,080 --> 15:50:03,480 stuff that we might have had in the past 25117 15:50:01,080 --> 15:50:05,756 and now I just want to close the file 25118 15:50:03,480 --> 15:50:08,756 let me reopen my ter teral let me run 25119 15:50:05,756 --> 15:50:11,200 python of phone book. and let me type in 25120 15:50:08,756 --> 15:50:15,400 how about David and then +1 25121 15:50:11,200 --> 15:50:18,320 949468 2750 and hold crossing my fingers 25122 15:50:15,400 --> 15:50:20,636 watching the actual CSV at top left my 25123 15:50:18,320 --> 15:50:22,040 code has just added me to the file and 25124 15:50:20,636 --> 15:50:24,840 if I were to run it again for instance 25125 15:50:22,040 --> 15:50:27,240 with Carter in plus1 617495 1000 25126 15:50:24,840 --> 15:50:29,160 crossing my fingers again we've updated 25127 15:50:27,240 --> 15:50:30,916 the file and it turns out there's code 25128 15:50:29,160 --> 15:50:32,880 now via which I can even read that file 25129 15:50:30,916 --> 15:50:34,796 but I can first tighten this up just so 25130 15:50:32,880 --> 15:50:36,796 you've seen it it turns out into python 25131 15:50:34,796 --> 15:50:38,720 it's so common to open files and close 25132 15:50:36,796 --> 15:50:40,720 them you know humans make mistakes and 25133 15:50:38,720 --> 15:50:42,160 they often forget to close files which 25134 15:50:40,720 --> 15:50:44,560 might then end up using more memory than 25135 15:50:42,160 --> 15:50:45,956 you intend so you can alternatively do 25136 15:50:44,560 --> 15:50:48,560 this in Python so that you don't have to 25137 15:50:45,956 --> 15:50:52,160 worry about closing files you can use 25138 15:50:48,560 --> 15:50:54,400 this keyword instead you can say with 25139 15:50:52,160 --> 15:50:57,080 the opening of this file as a variable 25140 15:50:54,400 --> 15:50:59,436 called file do all of the following 25141 15:50:57,080 --> 15:51:01,680 underneath so I'm indenting most of my 25142 15:50:59,436 --> 15:51:03,880 code I'm using this new python specific 25143 15:51:01,680 --> 15:51:05,796 keyword called wi and this is just a 25144 15:51:03,880 --> 15:51:07,720 matter of saying with the following 25145 15:51:05,796 --> 15:51:09,436 opening of the file do those next four 25146 15:51:07,720 --> 15:51:11,520 lines of code and then automatically 25147 15:51:09,436 --> 15:51:13,436 close it for me at the end of the 25148 15:51:11,520 --> 15:51:15,636 indentation it's a minor optimization 25149 15:51:13,436 --> 15:51:18,680 but this again is sort of the pythonic 25150 15:51:15,636 --> 15:51:21,560 way to do things instead how else might 25151 15:51:18,680 --> 15:51:23,596 I do this too well it turns out that the 25152 15:51:21,560 --> 15:51:25,480 code I've written here on line n 25153 15:51:23,596 --> 15:51:27,636 especially is a little fragile right if 25154 15:51:25,480 --> 15:51:30,000 any human opens the spreadsheet the CSV 25155 15:51:27,636 --> 15:51:32,000 file in Excel Google spreadsheets Apple 25156 15:51:30,000 --> 15:51:33,400 numbers and maybe like moves the columns 25157 15:51:32,000 --> 15:51:34,796 around just because maybe they're 25158 15:51:33,400 --> 15:51:36,400 fussing they SA it and they don't 25159 15:51:34,796 --> 15:51:38,680 realize they've now changed my 25160 15:51:36,400 --> 15:51:40,756 assumptions I don't want to necessarily 25161 15:51:38,680 --> 15:51:42,320 write name and number always in that 25162 15:51:40,756 --> 15:51:44,240 order CU what if someone screws up and 25163 15:51:42,320 --> 15:51:46,560 flips those two columns by literally 25164 15:51:44,240 --> 15:51:48,360 dragging and dropping so it turns out 25165 15:51:46,560 --> 15:51:50,636 that instead of using a list here we can 25166 15:51:48,360 --> 15:51:53,756 use another feature of this Library as 25167 15:51:50,636 --> 15:51:55,160 follows instead of using a writer 25168 15:51:53,756 --> 15:51:57,080 there's something called a dictionary 25169 15:51:55,160 --> 15:51:59,400 writer or dict writer that takes the 25170 15:51:57,080 --> 15:52:02,160 same argument as input the file that's 25171 15:51:59,400 --> 15:52:03,840 opened but now the one difference here 25172 15:52:02,160 --> 15:52:04,720 is that you need to tell this dictionary 25173 15:52:03,840 --> 15:52:09,000 writer 25174 15:52:04,720 --> 15:52:11,956 that your field names are name and 25175 15:52:09,000 --> 15:52:13,636 number and let me close the CSV here 25176 15:52:11,956 --> 15:52:16,880 name and number are the names of the 25177 15:52:13,636 --> 15:52:18,916 fields The Columns in this CSV file and 25178 15:52:16,880 --> 15:52:20,276 when it comes time to write a new row 25179 15:52:18,916 --> 15:52:22,636 the syntax here is going to be a little 25180 15:52:20,276 --> 15:52:24,596 uglier but it's just a dictionary the 25181 15:52:22,636 --> 15:52:25,956 name I want to write to the dictionary 25182 15:52:24,596 --> 15:52:28,796 is going to be whatever name the human 25183 15:52:25,956 --> 15:52:30,680 typed in the number that I want to write 25184 15:52:28,796 --> 15:52:33,240 to the the CSV file is going to be 25185 15:52:30,680 --> 15:52:34,916 whatever the number the human typed in 25186 15:52:33,240 --> 15:52:37,636 but what's different next about this 25187 15:52:34,916 --> 15:52:39,756 code is by simply using a dictionary 25188 15:52:37,636 --> 15:52:42,400 writer here instead of the generic 25189 15:52:39,756 --> 15:52:46,000 writer now the columns can be in this 25190 15:52:42,400 --> 15:52:47,720 order or this order or any order and the 25191 15:52:46,000 --> 15:52:49,560 dictionary writer is going to figure out 25192 15:52:47,720 --> 15:52:51,840 based on the first line of text in that 25193 15:52:49,560 --> 15:52:53,480 CSV where to put name where to put 25194 15:52:51,840 --> 15:52:55,080 number so if you flip them no big deal 25195 15:52:53,480 --> 15:52:57,480 it's going to notice oh wait the columns 25196 15:52:55,080 --> 15:52:59,680 changed and it's going to insert the 25197 15:52:57,480 --> 15:53:01,080 columns correctly so just again another 25198 15:52:59,680 --> 15:53:02,160 more powerful feature that lets you 25199 15:53:01,080 --> 15:53:04,956 focus 25200 15:53:02,160 --> 15:53:07,916 on lets you focus on real work as 25201 15:53:04,956 --> 15:53:10,956 opposed to actually uh getting tied up 25202 15:53:07,916 --> 15:53:15,040 in the weeds of writing code like this 25203 15:53:10,956 --> 15:53:17,436 otherwise questions on this one as well 25204 15:53:15,040 --> 15:53:19,956 but what we will do now is come full 25205 15:53:17,436 --> 15:53:21,680 circle to some of the more uh 25206 15:53:19,956 --> 15:53:23,680 sophisticated examples with which we 25207 15:53:21,680 --> 15:53:26,596 began and I'm going to go back over to 25208 15:53:23,680 --> 15:53:28,756 my own Mac laptop here where I've got my 25209 15:53:26,596 --> 15:53:30,040 own terminal window up and running and I 25210 15:53:28,756 --> 15:53:31,840 was just going to introduce a couple of 25211 15:53:30,040 --> 15:53:34,480 final libraries that really speak to 25212 15:53:31,840 --> 15:53:36,160 just how powerful python can can be and 25213 15:53:34,480 --> 15:53:38,240 how quickly you can get up and running 25214 15:53:36,160 --> 15:53:39,840 to be fair can't necessarily do all of 25215 15:53:38,240 --> 15:53:41,276 these things in the cloud like in code 25216 15:53:39,840 --> 15:53:43,160 spaces because you need access to your 25217 15:53:41,276 --> 15:53:45,200 own speakers or microphone or the like 25218 15:53:43,160 --> 15:53:48,040 so that's why I'm doing it on my own Mac 25219 15:53:45,200 --> 15:53:49,480 here but let me go ahead and open up a 25220 15:53:48,040 --> 15:53:51,796 program called 25221 15:53:49,480 --> 15:53:53,480 speech. and I'm not using VSS code here 25222 15:53:51,796 --> 15:53:55,160 I'm using a program called VI that's 25223 15:53:53,480 --> 15:53:57,200 entirely terminal window based but it's 25224 15:53:55,160 --> 15:53:59,796 going to allow me for instance to import 25225 15:53:57,200 --> 15:54:01,596 the python text to speech version three 25226 15:53:59,796 --> 15:54:03,436 Library I'm going to give myself a 25227 15:54:01,596 --> 15:54:05,880 variable called engine that's going to 25228 15:54:03,436 --> 15:54:09,080 be set equal to the python text to 25229 15:54:05,880 --> 15:54:10,756 speech 3 libraries init method which is 25230 15:54:09,080 --> 15:54:12,916 just going to initialize this library 25231 15:54:10,756 --> 15:54:16,360 that relates to text to speech I'm going 25232 15:54:12,916 --> 15:54:19,360 to then use the engines say function to 25233 15:54:16,360 --> 15:54:21,320 say something like how about hello comma 25234 15:54:19,360 --> 15:54:24,680 world and then as my last line I'm going 25235 15:54:21,320 --> 15:54:27,360 to say engine. runand wait capitalized 25236 15:54:24,680 --> 15:54:29,636 as such to tell my program now to run 25237 15:54:27,360 --> 15:54:31,320 that speech and wait until it's done all 25238 15:54:29,636 --> 15:54:34,000 right I'm going to save this file I'm 25239 15:54:31,320 --> 15:54:35,956 going to run python of speech. py and 25240 15:54:34,000 --> 15:54:39,720 I'm going to cross my fingers as always 25241 15:54:35,956 --> 15:54:41,596 and hello world all right so now I have 25242 15:54:39,720 --> 15:54:43,796 a program that's actually synthesizing 25243 15:54:41,596 --> 15:54:45,560 speech using a library like this how can 25244 15:54:43,796 --> 15:54:47,436 I now modify this to be a little more 25245 15:54:45,560 --> 15:54:49,000 interesting well how about this let me 25246 15:54:47,436 --> 15:54:51,160 go ahead and prompt the user for their 25247 15:54:49,000 --> 15:54:53,400 name like we've done several times here 25248 15:54:51,160 --> 15:54:55,240 using Python's built-in name function 25249 15:54:53,400 --> 15:54:57,276 and now let me go ahead and use a format 25250 15:54:55,240 --> 15:54:59,840 string in conjunction with this Library 25251 15:54:57,276 --> 15:55:01,796 interpolating the value of name there 25252 15:54:59,840 --> 15:55:03,320 and at least if my name is somewhat 25253 15:55:01,796 --> 15:55:05,240 phonetically pronounceable let's go 25254 15:55:03,320 --> 15:55:09,840 ahead and run python ofp 25255 15:55:05,240 --> 15:55:11,276 speech. type in my name and hello David 25256 15:55:09,840 --> 15:55:12,560 okay it's a sort of weird choice of 25257 15:55:11,276 --> 15:55:14,436 inflection but we're starting to 25258 15:55:12,560 --> 15:55:16,680 synthesize voice not unlike Siri or 25259 15:55:14,436 --> 15:55:18,520 Google assistant or Alexa or the like 25260 15:55:16,680 --> 15:55:21,400 now we can maybe do something a little 25261 15:55:18,520 --> 15:55:24,320 more advanced too in addition to 25262 15:55:21,400 --> 15:55:27,000 synthesizing speech in this way we could 25263 15:55:24,320 --> 15:55:29,240 synthesize for instance uh an actual 25264 15:55:27,000 --> 15:55:30,840 graphic let me go ahead now and do 25265 15:55:29,240 --> 15:55:32,360 something like this let me create a 25266 15:55:30,840 --> 15:55:35,000 program called 25267 15:55:32,360 --> 15:55:37,000 qr. I'm going to go ahead and import a 25268 15:55:35,000 --> 15:55:38,520 library called OS which gives you access 25269 15:55:37,000 --> 15:55:40,240 to operating system related 25270 15:55:38,520 --> 15:55:41,840 functionality in Python I'm going to 25271 15:55:40,240 --> 15:55:43,160 import a library I've pre-installed 25272 15:55:41,840 --> 15:55:44,480 called QR code which is a 25273 15:55:43,160 --> 15:55:46,160 two-dimensional barcode that you might 25274 15:55:44,480 --> 15:55:48,040 have seen in the real world I'm going to 25275 15:55:46,160 --> 15:55:50,636 go ahead and create an image variable 25276 15:55:48,040 --> 15:55:52,720 using this QR code library's make 25277 15:55:50,636 --> 15:55:54,560 function which per its documentation 25278 15:55:52,720 --> 15:55:58,436 takes a URL like one of cs50's own 25279 15:55:54,560 --> 15:56:02,720 videos so we'll do this with YouTube uh 25280 15:55:58,436 --> 15:56:02,720 /xv f 25281 15:56:02,796 --> 15:56:08,636 zj5 25282 15:56:04,720 --> 15:56:11,200 p g g0 so hopefully that's the right 25283 15:56:08,636 --> 15:56:12,756 lecture and now we've got image. saave 25284 15:56:11,200 --> 15:56:15,560 which is going to allow me to create a 25285 15:56:12,756 --> 15:56:17,276 file called qr. ping think back now on 25286 15:56:15,560 --> 15:56:18,756 problem set 4 and how painful it was to 25287 15:56:17,276 --> 15:56:21,040 save files we'll just use the save 25288 15:56:18,756 --> 15:56:23,276 function now in Python and save this as 25289 15:56:21,040 --> 15:56:25,756 a ping file portable Network graphic and 25290 15:56:23,276 --> 15:56:28,276 then lastly let's just go ahead and open 25291 15:56:25,756 --> 15:56:29,680 uh the with the command open qr. ping on 25292 15:56:28,276 --> 15:56:31,520 my Mac so that hopefully this just 25293 15:56:29,680 --> 15:56:33,320 automatically opens all right I'm going 25294 15:56:31,520 --> 15:56:35,080 to go ahead and just double check my 25295 15:56:33,320 --> 15:56:37,276 syntax here so that I haven't made any 25296 15:56:35,080 --> 15:56:41,680 mistakes I'm going to go ahead and run 25297 15:56:37,276 --> 15:56:43,160 python of qr. enter that opens up this 25298 15:56:41,680 --> 15:56:46,040 let me go ahead and zoom in if you've 25299 15:56:43,160 --> 15:56:47,360 got a phone handy and you'd like to scan 25300 15:56:46,040 --> 15:56:51,080 this code 25301 15:56:47,360 --> 15:56:55,240 here whether in person or 25302 15:56:51,080 --> 15:57:00,116 online I apologize you won't appreciate 25303 15:56:55,240 --> 15:57:03,200 it amazing okay and lastly let me go 25304 15:57:00,116 --> 15:57:05,480 back into our speech example here uh 25305 15:57:03,200 --> 15:57:06,956 create a final ending here on our final 25306 15:57:05,480 --> 15:57:10,596 moments and how about we just say 25307 15:57:06,956 --> 15:57:12,000 something like this was cs50 like this 25308 15:57:10,596 --> 15:57:13,840 let's go ahead here fix my 25309 15:57:12,000 --> 15:57:15,560 capitalization just for tidiness get rid 25310 15:57:13,840 --> 15:57:18,000 of the name and now with our final 25311 15:57:15,560 --> 15:57:21,480 flourish and your introduction to python 25312 15:57:18,000 --> 15:57:22,700 equipped here we go this was cs50 all 25313 15:57:21,480 --> 15:57:27,660 right we'll see you next 25314 15:57:22,700 --> 15:57:27,660 [Applause] 25315 15:57:32,720 --> 15:57:35,720 time 25316 15:57:36,270 --> 15:57:43,030 [Music] 25317 15:58:05,410 --> 15:58:22,629 [Music] 25318 15:58:28,550 --> 15:58:31,619 [Music] 25319 15:58:37,900 --> 15:58:41,010 [Music] 25320 15:58:47,830 --> 15:59:01,200 [Music] 25321 15:59:15,276 --> 15:59:20,276 all right this is cs50 and this is 25322 15:59:18,200 --> 15:59:22,560 already week seven and this is the week 25323 15:59:20,276 --> 15:59:24,116 where we'll continue where we left off 25324 15:59:22,560 --> 15:59:25,636 with python introducing you to a bit 25325 15:59:24,116 --> 15:59:26,880 more syntax and capabilities of the 25326 15:59:25,636 --> 15:59:28,636 language so you can solve like 25327 15:59:26,880 --> 15:59:29,916 interesting problems but a lot of those 25328 15:59:28,636 --> 15:59:32,000 problems increasingly are now going to 25329 15:59:29,916 --> 15:59:34,360 involve data in some form after all if 25330 15:59:32,000 --> 15:59:37,636 you think of most any website or mobile 25331 15:59:34,360 --> 15:59:39,040 app uh or uh process nowadays that 25332 15:59:37,636 --> 15:59:40,720 involves solving problems it almost 25333 15:59:39,040 --> 15:59:42,680 always involves some amount of data and 25334 15:59:40,720 --> 15:59:43,756 often data at scale lots and lots of 25335 15:59:42,680 --> 15:59:45,756 data and so what we're going to see 25336 15:59:43,756 --> 15:59:47,796 first today is that yes you can use 25337 15:59:45,756 --> 15:59:49,560 Python to solve all the problems past 25338 15:59:47,796 --> 15:59:50,916 that we've seen and also some data 25339 15:59:49,560 --> 15:59:52,116 specific ones but sometimes it's just 25340 15:59:50,916 --> 15:59:53,360 going to be annoying it's going to be a 25341 15:59:52,116 --> 15:59:55,200 little painful it's just going to be 25342 15:59:53,360 --> 15:59:57,040 more work than you might like to just 25343 15:59:55,200 --> 15:59:59,276 get to some answer and so today we'll 25344 15:59:57,040 --> 16:00:01,880 too introduce you to a new language 25345 15:59:59,276 --> 16:00:04,400 called SQL structured query language and 25346 16:00:01,880 --> 16:00:06,880 this is a language that wur is actually 25347 16:00:04,400 --> 16:00:09,000 much smaller relatively speaking than C 25348 16:00:06,880 --> 16:00:11,160 and python it sort of does less but it 25349 16:00:09,000 --> 16:00:13,400 does it really well and it's a language 25350 16:00:11,160 --> 16:00:15,276 for querying databases storing data in 25351 16:00:13,400 --> 16:00:17,160 it updating it inserting it deleting it 25352 16:00:15,276 --> 16:00:18,880 and so much more and it's the kind of 25353 16:00:17,160 --> 16:00:21,040 Technology that's used nowadays in 25354 16:00:18,880 --> 16:00:23,160 indeed web apps and mobile apps data 25355 16:00:21,040 --> 16:00:24,956 science analytics and and so much more 25356 16:00:23,160 --> 16:00:27,240 it's really good at storing lots and 25357 16:00:24,956 --> 16:00:29,240 lots of data now this is yet another 25358 16:00:27,240 --> 16:00:31,240 language and believe it or not next week 25359 16:00:29,240 --> 16:00:33,840 we'll introduce you to three more 25360 16:00:31,240 --> 16:00:35,240 languages HTML and CSS which are not 25361 16:00:33,840 --> 16:00:37,000 technically programming languages 25362 16:00:35,240 --> 16:00:39,320 they're all about Aesthetics and markup 25363 16:00:37,000 --> 16:00:41,276 of information but also JavaScript which 25364 16:00:39,320 --> 16:00:43,916 is in fact a programming language but 25365 16:00:41,276 --> 16:00:45,840 the goals here in cs50 really are going 25366 16:00:43,916 --> 16:00:47,116 to be to empower you to program more 25367 16:00:45,840 --> 16:00:48,520 generally and indeed when you're out 25368 16:00:47,116 --> 16:00:50,240 there in the real world some years from 25369 16:00:48,520 --> 16:00:53,000 now invariably there's going to be some 25370 16:00:50,240 --> 16:00:54,916 new other popular language out there and 25371 16:00:53,000 --> 16:00:56,596 hopefully in this week and next week and 25372 16:00:54,916 --> 16:00:58,200 Beyond among the goals is not just to 25373 16:00:56,596 --> 16:01:00,040 teach you these languages specifically 25374 16:00:58,200 --> 16:01:01,680 but again like how to teach yourself the 25375 16:01:00,040 --> 16:01:04,320 future languages that we've not even 25376 16:01:01,680 --> 16:01:06,680 heard about just yet so with that said 25377 16:01:04,320 --> 16:01:09,360 let's begin with a survey of sorts if 25378 16:01:06,680 --> 16:01:13,596 you go to this URL on your phone or 25379 16:01:09,360 --> 16:01:15,840 laptop cs50. l/f favorites a very simple 25380 16:01:13,596 --> 16:01:18,000 Google form awaits you that's just going 25381 16:01:15,840 --> 16:01:21,880 to ask you a couple of multiple choice 25382 16:01:18,000 --> 16:01:25,840 questions so go to cs50. 25383 16:01:21,880 --> 16:01:27,636 l/f favorites and that should lead you 25384 16:01:25,840 --> 16:01:30,116 to a Google form that looks a little 25385 16:01:27,636 --> 16:01:31,840 something like this asking you first as 25386 16:01:30,116 --> 16:01:34,080 of now in week seven what is your 25387 16:01:31,840 --> 16:01:36,240 favorite language among those options 25388 16:01:34,080 --> 16:01:37,796 here and then further down one more 25389 16:01:36,240 --> 16:01:41,880 question if you think back on problem 25390 16:01:37,796 --> 16:01:45,240 sets 0 through six what was if any your 25391 16:01:41,880 --> 16:01:48,680 favorite uh problem set problem be it in 25392 16:01:45,240 --> 16:01:50,360 scratch or C or python so answer those 25393 16:01:48,680 --> 16:01:52,956 two 25394 16:01:50,360 --> 16:01:55,436 questions and in a moment I'll flip over 25395 16:01:52,956 --> 16:01:58,116 to my screen here where you'll see and 25396 16:01:55,436 --> 16:01:59,880 anyone who's used Google forms knows the 25397 16:01:58,116 --> 16:02:02,276 spreadsheet that's collecting now this 25398 16:01:59,880 --> 16:02:03,680 data um Microsoft Office 365 can do the 25399 16:02:02,276 --> 16:02:05,240 same if you use one of those forms and 25400 16:02:03,680 --> 16:02:08,040 what you see here now is a spreadsheet 25401 16:02:05,240 --> 16:02:10,080 in Google Sheets enumerating all of the 25402 16:02:08,040 --> 16:02:12,796 audience's questions language is in 25403 16:02:10,080 --> 16:02:14,560 column B problem is in column C and each 25404 16:02:12,796 --> 16:02:16,796 row represents one student who has 25405 16:02:14,560 --> 16:02:19,000 responded uh a few of you were super 25406 16:02:16,796 --> 16:02:22,080 eager for class today at 8:33 a.m. 25407 16:02:19,000 --> 16:02:23,756 eastern time 10:32 11:10 okay so now 25408 16:02:22,080 --> 16:02:25,956 we're getting into the actual class time 25409 16:02:23,756 --> 16:02:28,720 here and if I scroll down we'll probably 25410 16:02:25,956 --> 16:02:30,916 see few dozen couple hundred Answers by 25411 16:02:28,720 --> 16:02:32,480 now and yeah so we're getting a whole 25412 16:02:30,916 --> 16:02:33,636 lot of answers here and I'm seeing some 25413 16:02:32,480 --> 16:02:35,680 patterns emerge but but it's not 25414 16:02:33,636 --> 16:02:37,116 necessarily obvious to the human eyes 25415 16:02:35,680 --> 16:02:39,040 what those patterns are now of course 25416 16:02:37,116 --> 16:02:40,756 you can use Google spreadsheets you can 25417 16:02:39,040 --> 16:02:42,916 like highlight the data and you can 25418 16:02:40,756 --> 16:02:44,436 create charts magically out of it but 25419 16:02:42,916 --> 16:02:45,560 you can only do what Google lets you do 25420 16:02:44,436 --> 16:02:47,956 with the data and same thing for 25421 16:02:45,560 --> 16:02:49,116 Microsoft Excel or apple numbers but 25422 16:02:47,956 --> 16:02:51,116 wouldn't it be nice to just be able to 25423 16:02:49,116 --> 16:02:53,000 manipulate the raw data relatively 25424 16:02:51,116 --> 16:02:54,796 simple though it is to just answer 25425 16:02:53,000 --> 16:02:56,840 questions about the data Maybe long-term 25426 16:02:54,796 --> 16:02:58,636 create your own charts customize it just 25427 16:02:56,840 --> 16:03:00,840 the way you want rather than be holding 25428 16:02:58,636 --> 16:03:03,436 to like software that's off the shelf 25429 16:03:00,840 --> 16:03:06,040 like this well how could we go about 25430 16:03:03,436 --> 16:03:08,160 doing this well let me propose that we 25431 16:03:06,040 --> 16:03:10,520 treat this data set now as what we're 25432 16:03:08,160 --> 16:03:12,080 going to call for now a flat file 25433 16:03:10,520 --> 16:03:13,796 database we'll see today that there's 25434 16:03:12,080 --> 16:03:15,560 fancier databases but the simplest 25435 16:03:13,796 --> 16:03:18,360 database in the world is really just 25436 16:03:15,560 --> 16:03:20,596 like a CSV file and we saw that a couple 25437 16:03:18,360 --> 16:03:23,680 of weeks ago in C we wrote a bit of C 25438 16:03:20,596 --> 16:03:26,200 code that used F printf to write data to 25439 16:03:23,680 --> 16:03:27,916 a file using commas as the separator we 25440 16:03:26,200 --> 16:03:28,956 didn't really do much more with csvs at 25441 16:03:27,916 --> 16:03:31,400 the time though because it's really 25442 16:03:28,956 --> 16:03:33,520 annoying painful timec consuming not fun 25443 16:03:31,400 --> 16:03:35,480 to use see for something like that 25444 16:03:33,520 --> 16:03:36,956 because of malok and memory and all that 25445 16:03:35,480 --> 16:03:39,560 stuff but with python it's going to be 25446 16:03:36,956 --> 16:03:41,000 much easier and so anytime you have 25447 16:03:39,560 --> 16:03:42,720 access to some data set where you can 25448 16:03:41,000 --> 16:03:44,840 just like download it to your own Mac or 25449 16:03:42,720 --> 16:03:47,560 PC or your Cloud environment it's sort 25450 16:03:44,840 --> 16:03:49,560 of a candidate for now writing code to 25451 16:03:47,560 --> 16:03:51,796 do something with the data Maybe analyze 25452 16:03:49,560 --> 16:03:53,320 it right away if it's been human inputed 25453 16:03:51,796 --> 16:03:55,116 manually maybe you have to clean it up 25454 16:03:53,320 --> 16:03:56,756 by doing a lot of find and replace but 25455 16:03:55,116 --> 16:03:58,596 not with your keyboard but rather with 25456 16:03:56,756 --> 16:04:01,560 code and so let me go ahead and do this 25457 16:03:58,596 --> 16:04:03,320 let me go back to my uh Google sheet 25458 16:04:01,560 --> 16:04:06,436 here that has all of the data that's 25459 16:04:03,320 --> 16:04:10,000 come in now and let me go ahead and 25460 16:04:06,436 --> 16:04:13,040 download this via uh the file menu 25461 16:04:10,000 --> 16:04:14,880 here and let's see download and you can 25462 16:04:13,040 --> 16:04:16,400 see a whole bunch of options of most 25463 16:04:14,880 --> 16:04:18,240 formats might be familiar but today 25464 16:04:16,400 --> 16:04:20,596 we'll focus just on this one comma 25465 16:04:18,240 --> 16:04:22,116 separated values or CSV that's going to 25466 16:04:20,596 --> 16:04:23,796 go ahead and download it on my Mac here 25467 16:04:22,116 --> 16:04:25,360 into my own downloads folder and now I'm 25468 16:04:23,796 --> 16:04:27,360 going to go ahead and do this let me go 25469 16:04:25,360 --> 16:04:28,796 ahead and pull up VSS code in the cloud 25470 16:04:27,360 --> 16:04:30,320 here and if you've never done this 25471 16:04:28,796 --> 16:04:32,200 before there's a couple of ways to do it 25472 16:04:30,320 --> 16:04:33,956 but the simplest way to upload a file to 25473 16:04:32,200 --> 16:04:35,956 your codes space so to speak is just a 25474 16:04:33,956 --> 16:04:38,520 sort of drag and drop that's going to 25475 16:04:35,956 --> 16:04:40,756 magically upload it to the server there 25476 16:04:38,520 --> 16:04:42,680 and we'll see that one it has a very 25477 16:04:40,756 --> 16:04:43,720 long file name which I'm I'm actually 25478 16:04:42,680 --> 16:04:45,796 going to clean this up because that's 25479 16:04:43,720 --> 16:04:48,436 going to be very tedious to type in my 25480 16:04:45,796 --> 16:04:50,116 code so I could either rightclick of of 25481 16:04:48,436 --> 16:04:52,160 course up here but I'm going to use my 25482 16:04:50,116 --> 16:04:54,720 Linux command so let's move this file 25483 16:04:52,160 --> 16:04:55,796 called cs50 2022 something or other and 25484 16:04:54,720 --> 16:04:58,240 let's just name it more simply 25485 16:04:55,796 --> 16:05:01,480 favorites. CSV so all lowercase no 25486 16:04:58,240 --> 16:05:03,720 spaces sort of good Basics and let me go 25487 16:05:01,480 --> 16:05:06,680 ahead now and open up this file with 25488 16:05:03,720 --> 16:05:08,596 code favorites. CSV I'll close my file 25489 16:05:06,680 --> 16:05:10,916 explorer and we'll see exactly the same 25490 16:05:08,596 --> 16:05:12,756 data as before but not quite as pretty 25491 16:05:10,916 --> 16:05:14,956 as Google Sheets makes it be rather we 25492 16:05:12,756 --> 16:05:17,240 see here that I still have three columns 25493 16:05:14,956 --> 16:05:18,880 timestamp language problem and then all 25494 16:05:17,240 --> 16:05:21,080 of the values down below including the 25495 16:05:18,880 --> 16:05:22,956 timestamps and the answers therefore but 25496 16:05:21,080 --> 16:05:25,200 it doesn't have proper columns it just 25497 16:05:22,956 --> 16:05:27,636 has commas separating them now we could 25498 16:05:25,200 --> 16:05:31,040 very easily write python code just like 25499 16:05:27,636 --> 16:05:33,000 we wrote code to manipulate files like 25500 16:05:31,040 --> 16:05:34,436 this either to write or read but in 25501 16:05:33,000 --> 16:05:36,080 instead let's do something that's a 25502 16:05:34,436 --> 16:05:37,880 little more pleasant which is indeed in 25503 16:05:36,080 --> 16:05:40,956 the form of python so python actually 25504 16:05:37,880 --> 16:05:43,400 comes with Native support for csvs it 25505 16:05:40,956 --> 16:05:44,916 has indeed a package called CSV that 25506 16:05:43,400 --> 16:05:46,276 just lets you read and write and do a 25507 16:05:44,916 --> 16:05:48,320 whole bunch of useful stuff when it 25508 16:05:46,276 --> 16:05:50,040 comes to CSV files so let's go ahead and 25509 16:05:48,320 --> 16:05:52,880 do something with this file let me go 25510 16:05:50,040 --> 16:05:55,000 back here to VSS code I'm going to close 25511 16:05:52,880 --> 16:05:56,956 favorites. CSV for now but just remember 25512 16:05:55,000 --> 16:05:59,160 in your mind that timestamp was the 25513 16:05:56,956 --> 16:06:01,240 first column language was the second 25514 16:05:59,160 --> 16:06:02,680 column and problem was the third and 25515 16:06:01,240 --> 16:06:04,480 notice because we're using commas they 25516 16:06:02,680 --> 16:06:06,080 don't again line up perfectly but that's 25517 16:06:04,480 --> 16:06:08,116 not a problem there are two commas in 25518 16:06:06,080 --> 16:06:09,840 every line presumably and I'm going to 25519 16:06:08,116 --> 16:06:12,636 go ahead and now create a file called 25520 16:06:09,840 --> 16:06:13,796 how about uh favorites. piy so that I 25521 16:06:12,636 --> 16:06:15,160 can start writing some code to 25522 16:06:13,796 --> 16:06:16,636 manipulate this data and let's do 25523 16:06:15,160 --> 16:06:18,880 something simple let's just write a 25524 16:06:16,636 --> 16:06:21,636 simple program in Python that opens this 25525 16:06:18,880 --> 16:06:23,240 file reads it and prints something out 25526 16:06:21,636 --> 16:06:24,276 just as like a safety check that I know 25527 16:06:23,240 --> 16:06:26,520 what I'm doing even though it's not 25528 16:06:24,276 --> 16:06:29,560 going to be useful so in Python if you 25529 16:06:26,520 --> 16:06:31,520 want CSV support you import CSV and that 25530 16:06:29,560 --> 16:06:33,756 gives you access to all the magical 25531 16:06:31,520 --> 16:06:36,116 capabilities thereof let me now go ahead 25532 16:06:33,756 --> 16:06:38,916 and use this technique to open a file in 25533 16:06:36,116 --> 16:06:40,720 Python which is similar in C but with 25534 16:06:38,916 --> 16:06:43,240 python I'm going to do this the keyword 25535 16:06:40,720 --> 16:06:45,320 with I'm going to open a file called 25536 16:06:43,240 --> 16:06:47,680 favorites. CSV which was the shorter 25537 16:06:45,320 --> 16:06:49,720 name I gave it this is optional but just 25538 16:06:47,680 --> 16:06:52,436 for explicitness I'm going to open it in 25539 16:06:49,720 --> 16:06:54,320 read mode explicitly just like f open 25540 16:06:52,436 --> 16:06:56,200 took a second argument as well and I'm 25541 16:06:54,320 --> 16:06:57,520 going to name this file once open quite 25542 16:06:56,200 --> 16:07:00,636 simply file though I could call it 25543 16:06:57,520 --> 16:07:02,916 anything I want and now it's just an 25544 16:07:00,636 --> 16:07:05,400 open file so far as python knows at this 25545 16:07:02,916 --> 16:07:07,840 moment it's just text or better yet it's 25546 16:07:05,400 --> 16:07:10,680 just zeros and ones if you want this 25547 16:07:07,840 --> 16:07:12,116 python package called CSV to actually do 25548 16:07:10,680 --> 16:07:14,956 something useful with it you have to 25549 16:07:12,116 --> 16:07:16,520 load this file now into the library and 25550 16:07:14,956 --> 16:07:18,520 the simplest way to do this is to give 25551 16:07:16,520 --> 16:07:20,080 myself like a variable called reader 25552 16:07:18,520 --> 16:07:22,000 because I want to read this file though 25553 16:07:20,080 --> 16:07:23,360 this too I could call anything else I'm 25554 16:07:22,000 --> 16:07:26,436 going to then set that equal to the 25555 16:07:23,360 --> 16:07:28,720 return value of a function called CSV do 25556 16:07:26,436 --> 16:07:31,276 reader and I pass to that per the 25557 16:07:28,720 --> 16:07:32,880 documentation the open file so step one 25558 16:07:31,276 --> 16:07:35,240 I open the file and this just gives me 25559 16:07:32,880 --> 16:07:38,360 me access to the bytes there in step two 25560 16:07:35,240 --> 16:07:40,756 now with csvreader tells the python 25561 16:07:38,360 --> 16:07:42,756 package called CSV to do something 25562 16:07:40,756 --> 16:07:45,520 useful with it and start analyzing the 25563 16:07:42,756 --> 16:07:47,560 commas and uh allow me to uh parse it 25564 16:07:45,520 --> 16:07:50,636 further so let's go ahead and do this 25565 16:07:47,560 --> 16:07:52,916 let me go ahead now and within this Loop 25566 16:07:50,636 --> 16:07:57,560 let's say this with sorry within this 25567 16:07:52,916 --> 16:08:01,276 open file let's do this for every row if 25568 16:07:57,560 --> 16:08:03,756 you will or line in the file AKA reader 25569 16:08:01,276 --> 16:08:07,880 let's go ahead and print out now just 25570 16:08:03,756 --> 16:08:09,240 how about Row Bracket one now what's 25571 16:08:07,880 --> 16:08:10,956 going on here well it turns out if you 25572 16:08:09,240 --> 16:08:13,200 read the documentation for the CSV 25573 16:08:10,956 --> 16:08:16,160 reader function what it hands you back 25574 16:08:13,200 --> 16:08:18,596 is essentially this special object so to 25575 16:08:16,160 --> 16:08:20,720 speak that allows you to treat it as 25576 16:08:18,596 --> 16:08:23,560 though it's just a really big list of 25577 16:08:20,720 --> 16:08:26,636 lines from the file AKA reader so by 25578 16:08:23,560 --> 16:08:29,240 saying for Row in reader this is a way 25579 16:08:26,636 --> 16:08:31,200 more succinct way of saying uh give me 25580 16:08:29,240 --> 16:08:32,840 the first line in the file plus plus 25581 16:08:31,200 --> 16:08:33,956 give me the second line in the file Plus 25582 16:08:32,840 --> 16:08:36,000 plus and so forth that we would have 25583 16:08:33,956 --> 16:08:38,040 done what much more mechanically in C 25584 16:08:36,000 --> 16:08:40,520 this is just much more pythonic and 25585 16:08:38,040 --> 16:08:43,400 English friendly if you will so in every 25586 16:08:40,520 --> 16:08:45,240 iteration of this Loop row is going to 25587 16:08:43,400 --> 16:08:47,360 contain all of the data from the current 25588 16:08:45,240 --> 16:08:49,560 row but better yet what the reader 25589 16:08:47,360 --> 16:08:52,840 function does for me is it hands me each 25590 16:08:49,560 --> 16:08:55,240 row not just as a big string or Stir of 25591 16:08:52,840 --> 16:08:58,000 text in Python it gives me what 25592 16:08:55,240 --> 16:09:01,796 apparently based on the syntax on line 25593 16:08:58,000 --> 16:09:03,840 six any Instinct yeah it's giving me 25594 16:09:01,796 --> 16:09:05,320 back indeed a list and I'm I presume the 25595 16:09:03,840 --> 16:09:06,796 visual clue for you was the fact that 25596 16:09:05,320 --> 16:09:09,520 we're using square brackets here and 25597 16:09:06,796 --> 16:09:11,596 indeed Row Bracket one is going to be 25598 16:09:09,520 --> 16:09:13,956 not the first but the second element in 25599 16:09:11,596 --> 16:09:16,080 that list and so just take a guess when 25600 16:09:13,956 --> 16:09:18,200 I run this code in a moment What's going 25601 16:09:16,080 --> 16:09:21,080 to get printed the timestamp the 25602 16:09:18,200 --> 16:09:21,080 language or the 25603 16:09:21,160 --> 16:09:27,276 problem the yeah oh the language the 25604 16:09:25,116 --> 16:09:29,636 language because it's the second column 25605 16:09:27,276 --> 16:09:31,200 that is in the file delimited by those 25606 16:09:29,636 --> 16:09:32,680 those commas so let me go ahead and do 25607 16:09:31,200 --> 16:09:35,080 this let me clear my terminal down here 25608 16:09:32,680 --> 16:09:37,320 here let me run python of favorites.i 25609 16:09:35,080 --> 16:09:38,916 and enter and there's everything it was 25610 16:09:37,320 --> 16:09:41,080 super fast but there's a really long 25611 16:09:38,916 --> 16:09:43,276 list here and in fact if I increase the 25612 16:09:41,080 --> 16:09:45,400 size of my terminal and start scrolling 25613 16:09:43,276 --> 16:09:47,116 up you'll just see all of the raw data 25614 16:09:45,400 --> 16:09:48,956 now this isn't that useful yet I could 25615 16:09:47,116 --> 16:09:51,880 have just glanced at the CSV but clearly 25616 16:09:48,956 --> 16:09:53,840 now I have the ability to open the file 25617 16:09:51,880 --> 16:09:55,720 parse it so to speak that is break it up 25618 16:09:53,840 --> 16:09:59,000 into its constituent parts and do 25619 16:09:55,720 --> 16:10:00,480 something with specific Parts therein 25620 16:09:59,000 --> 16:10:02,400 all right so if I want to do this a 25621 16:10:00,480 --> 16:10:04,240 little more pleasantly though let me at 25622 16:10:02,400 --> 16:10:05,436 least least make this semantically a 25623 16:10:04,240 --> 16:10:07,560 little cleaner and you know what just 25624 16:10:05,436 --> 16:10:10,240 for clarity let me just give myself a 25625 16:10:07,560 --> 16:10:13,720 variable it's not strictly necessary but 25626 16:10:10,240 --> 16:10:15,880 I know that this is the favorite uh for 25627 16:10:13,720 --> 16:10:18,360 instance uh language so let's just call 25628 16:10:15,880 --> 16:10:20,720 it favorite set it equal to Row Bracket 25629 16:10:18,360 --> 16:10:22,480 1 and now just to be more explicit in my 25630 16:10:20,720 --> 16:10:24,200 code even though again we don't need the 25631 16:10:22,480 --> 16:10:25,400 variable per se this codee's of course 25632 16:10:24,200 --> 16:10:26,880 going to do the same thing it's just 25633 16:10:25,400 --> 16:10:30,000 using an additional variable called 25634 16:10:26,880 --> 16:10:31,796 favorite if I go down here scroll up run 25635 16:10:30,000 --> 16:10:33,840 the program again I get back the exact 25636 16:10:31,796 --> 16:10:36,200 same data but this is a stepping stone 25637 16:10:33,840 --> 16:10:38,520 to something that's even more powerful 25638 16:10:36,200 --> 16:10:40,720 about python support for CSV files is 25639 16:10:38,520 --> 16:10:45,200 that you don't have to just treat the 25640 16:10:40,720 --> 16:10:47,680 return value as a list with zero and one 25641 16:10:45,200 --> 16:10:50,400 and two so just thinking intuitively 25642 16:10:47,680 --> 16:10:52,636 here why is this maybe not the best 25643 16:10:50,400 --> 16:10:55,320 design to hand you the programmer back 25644 16:10:52,636 --> 16:10:57,040 the data in a list that's numerically 25645 16:10:55,320 --> 16:11:02,160 indexed with 25646 16:10:57,040 --> 16:11:04,400 012 it clearly works but critique this 25647 16:11:02,160 --> 16:11:06,636 what could go wrong what's a little 25648 16:11:04,400 --> 16:11:09,240 poorly designed yeah you have to always 25649 16:11:06,636 --> 16:11:12,240 remember what 25650 16:11:09,240 --> 16:11:12,240 are 25651 16:11:12,720 --> 16:11:18,276 yeah exactly so if yeah so it's up to 25652 16:11:15,916 --> 16:11:20,756 you to repeat it's up to you to remember 25653 16:11:18,276 --> 16:11:22,400 like what column the data is actually in 25654 16:11:20,756 --> 16:11:23,636 and you know God forbid you're you're 25655 16:11:22,400 --> 16:11:25,360 collaborating with someone else on the 25656 16:11:23,636 --> 16:11:26,796 spreadsheet you know you've if you've 25657 16:11:25,360 --> 16:11:28,000 used Google spreadsheets you can move 25658 16:11:26,796 --> 16:11:29,560 columns around maybe just because you 25659 16:11:28,000 --> 16:11:31,200 want to visually reorganize things and 25660 16:11:29,560 --> 16:11:33,116 if you do this and then someone else 25661 16:11:31,200 --> 16:11:34,596 downloads that same data all of their 25662 16:11:33,116 --> 16:11:36,796 code is going to break so that's just 25663 16:11:34,596 --> 16:11:38,436 really bad design it's fragile just 25664 16:11:36,796 --> 16:11:40,680 because it's you're sort of on the honor 25665 16:11:38,436 --> 16:11:42,116 System that one means the data that you 25666 16:11:40,680 --> 16:11:44,520 want so wouldn't it be nice if we could 25667 16:11:42,116 --> 16:11:48,116 be a little more explicit well recall 25668 16:11:44,520 --> 16:11:50,956 that the very first line in this file is 25669 16:11:48,116 --> 16:11:52,560 actually this and I paused the output 25670 16:11:50,956 --> 16:11:55,240 this time so that we can see more 25671 16:11:52,560 --> 16:11:56,720 optionally I just reran favorites St pi 25672 16:11:55,240 --> 16:11:58,756 and notice one of these things is not 25673 16:11:56,720 --> 16:12:00,756 like the other every output was either 25674 16:11:58,756 --> 16:12:02,560 scratch or C or python except for this 25675 16:12:00,756 --> 16:12:05,040 first one why am I seeing the word 25676 16:12:02,560 --> 16:12:05,040 language 25677 16:12:05,116 --> 16:12:10,520 here where did language come from you 25678 16:12:08,360 --> 16:12:13,400 didn't have the ability to manual input 25679 16:12:10,520 --> 16:12:16,080 oh no where did it come from 25680 16:12:13,400 --> 16:12:17,680 yeah yeah the header the very first row 25681 16:12:16,080 --> 16:12:19,680 in the file which by human convention 25682 16:12:17,680 --> 16:12:21,400 generally just defines what the columns 25683 16:12:19,680 --> 16:12:23,796 represent so that there's some human 25684 16:12:21,400 --> 16:12:26,080 useful information there now that's not 25685 16:12:23,796 --> 16:12:27,880 really intended to be part of my output 25686 16:12:26,080 --> 16:12:30,200 at the moment so there is a way to skip 25687 16:12:27,880 --> 16:12:31,636 this if you want to skip the first row 25688 16:12:30,200 --> 16:12:33,596 you can actually do something like this 25689 16:12:31,636 --> 16:12:35,756 you can say next row and that will just 25690 16:12:33,596 --> 16:12:38,116 ignore that row so that I'm starting 25691 16:12:35,756 --> 16:12:40,116 really with the every row thereafter but 25692 16:12:38,116 --> 16:12:41,680 there's a better way to handle this than 25693 16:12:40,116 --> 16:12:43,680 that that will get rid of the row in the 25694 16:12:41,680 --> 16:12:45,636 output but let me go ahead and use a 25695 16:12:43,680 --> 16:12:46,680 different feature of the CSV package 25696 16:12:45,636 --> 16:12:48,360 that's just going to make this a little 25697 16:12:46,680 --> 16:12:50,596 cleaner Al together so let me clear my 25698 16:12:48,360 --> 16:12:52,796 terminal window here let me undo this 25699 16:12:50,596 --> 16:12:55,360 next thing that I just added and instead 25700 16:12:52,796 --> 16:12:57,916 of using a reader let me go ahead and 25701 16:12:55,360 --> 16:13:01,520 use a dictionary reader abbreviated dict 25702 16:12:57,916 --> 16:13:04,240 reader that's going to now return me the 25703 16:13:01,520 --> 16:13:06,116 equivalent of all of the rows one at a 25704 16:13:04,240 --> 16:13:09,320 time so I can still call it Reader just 25705 16:13:06,116 --> 16:13:11,320 as before but as the name implies what 25706 16:13:09,320 --> 16:13:13,480 this reader is going to return is not a 25707 16:13:11,320 --> 16:13:15,596 list after list after list but a 25708 16:13:13,480 --> 16:13:16,596 dictionary a dictionary a dictionary and 25709 16:13:15,596 --> 16:13:19,200 remember a dictionary is just a 25710 16:13:16,596 --> 16:13:20,880 collection of key value pairs so what 25711 16:13:19,200 --> 16:13:23,276 does that mean what are the keys what 25712 16:13:20,880 --> 16:13:25,840 are the values well now that I'm using a 25713 16:13:23,276 --> 16:13:27,840 dictionary reader I can actually do this 25714 16:13:25,840 --> 16:13:30,040 instead of sort of on the honor System 25715 16:13:27,840 --> 16:13:32,400 remembering that I want column one I can 25716 16:13:30,040 --> 16:13:34,520 treat row now not as a list but as a 25717 16:13:32,400 --> 16:13:36,880 dictionary and that means I can go in 25718 16:13:34,520 --> 16:13:39,116 here and say quote unquote language and 25719 16:13:36,880 --> 16:13:41,200 we saw that back in week six python 25720 16:13:39,116 --> 16:13:43,320 allows you to index into dictionaries 25721 16:13:41,200 --> 16:13:45,956 using square bracket notation in strings 25722 16:13:43,320 --> 16:13:48,796 or stirs on the inside just like lists 25723 16:13:45,956 --> 16:13:50,796 allow for numbers but this now I think 25724 16:13:48,796 --> 16:13:53,840 is going to be a little more robust if I 25725 16:13:50,796 --> 16:13:55,796 run this again python of favorites. py 25726 16:13:53,840 --> 16:13:57,756 all of that worked out fine and let me 25727 16:13:55,796 --> 16:14:00,200 pause the output to by using this 25728 16:13:57,756 --> 16:14:03,436 program called more now I don't even see 25729 16:14:00,200 --> 16:14:05,636 the header so now whoever works it uh 25730 16:14:03,436 --> 16:14:07,436 with python wrote the code for this 25731 16:14:05,636 --> 16:14:09,436 package to just analyze that first line 25732 16:14:07,436 --> 16:14:12,116 of code use the header as you just 25733 16:14:09,436 --> 16:14:13,756 called it as the keys and then every 25734 16:14:12,116 --> 16:14:15,796 time you iterate through this Loop it 25735 16:14:13,756 --> 16:14:19,160 updates the values the values the values 25736 16:14:15,796 --> 16:14:21,796 but the keys stay the same any questions 25737 16:14:19,160 --> 16:14:24,080 then on this 25738 16:14:21,796 --> 16:14:27,596 technique spiced to say this would be 25739 16:14:24,080 --> 16:14:27,596 painful in C 25740 16:14:31,200 --> 16:14:34,200 yes 25741 16:14:35,200 --> 16:14:39,436 Associated exactly so the keys are 25742 16:14:37,636 --> 16:14:42,520 always going to be quote unquote 25743 16:14:39,436 --> 16:14:45,040 timestamp uh Pro language and problem 25744 16:14:42,520 --> 16:14:46,840 but on each iteration of this Loop here 25745 16:14:45,040 --> 16:14:48,720 uh the row is going to contain a 25746 16:14:46,840 --> 16:14:50,040 different row of values different row 25747 16:14:48,720 --> 16:14:52,200 values different row values so you're 25748 16:14:50,040 --> 16:14:54,080 going to get back one dictionary for 25749 16:14:52,200 --> 16:14:56,000 every student who submitted the Google 25750 16:14:54,080 --> 16:14:58,560 form if you will while iterating through 25751 16:14:56,000 --> 16:15:01,200 it there all right so once we have this 25752 16:14:58,560 --> 16:15:02,880 ability here why don't we go ahead and 25753 16:15:01,200 --> 16:15:05,200 transition 25754 16:15:02,880 --> 16:15:07,080 into how about not just using that 25755 16:15:05,200 --> 16:15:08,916 dictionary reader which makes the code a 25756 16:15:07,080 --> 16:15:10,956 little more robust because now if you 25757 16:15:08,916 --> 16:15:13,000 move the columns around no big deal it 25758 16:15:10,956 --> 16:15:15,400 doesn't matter if the numeric indices 25759 16:15:13,000 --> 16:15:17,400 change you can still use those keywords 25760 16:15:15,400 --> 16:15:18,756 instead well let's actually analyze the 25761 16:15:17,400 --> 16:15:21,000 data now I'm just spitting it out which 25762 16:15:18,756 --> 16:15:23,040 is not solving any problems for anyone 25763 16:15:21,000 --> 16:15:25,276 so let's go ahead and and count the 25764 16:15:23,040 --> 16:15:28,040 popularity of scratch C and Python and 25765 16:15:25,276 --> 16:15:29,796 see what everyone's uh been thinking 25766 16:15:28,040 --> 16:15:32,520 here all right so how might I do this 25767 16:15:29,796 --> 16:15:34,720 well let me go ahead and do this up here 25768 16:15:32,520 --> 16:15:36,480 before I start iterating let me give 25769 16:15:34,720 --> 16:15:38,636 myself let's say three variables and to 25770 16:15:36,480 --> 16:15:41,000 keep things simple I'll say one variable 25771 16:15:38,636 --> 16:15:44,000 called scratch set it equal to zero for 25772 16:15:41,000 --> 16:15:46,596 zero students so far C is going to equal 25773 16:15:44,000 --> 16:15:48,400 zero and python is going to equal zero 25774 16:15:46,596 --> 16:15:49,636 there's a slightly prettier way of doing 25775 16:15:48,400 --> 16:15:51,080 this just because this is like three 25776 16:15:49,636 --> 16:15:54,400 lines of code to do something very 25777 16:15:51,080 --> 16:15:58,240 simple you could alternatively in Python 25778 16:15:54,400 --> 16:16:02,320 but not C do scratch comma C comma 25779 16:15:58,240 --> 16:16:03,840 python equals 0 comma 0 so kind of 25780 16:16:02,320 --> 16:16:05,520 slightly more elegant just to fit it all 25781 16:16:03,840 --> 16:16:07,360 into one line but now let's just do 25782 16:16:05,520 --> 16:16:08,956 something more interesting on line seven 25783 16:16:07,360 --> 16:16:11,276 I'm still going to figure out what the 25784 16:16:08,956 --> 16:16:14,276 current favorite language is and now I'm 25785 16:16:11,276 --> 16:16:17,320 just going to do some conditional checks 25786 16:16:14,276 --> 16:16:20,160 How about if that favorite equals equals 25787 16:16:17,320 --> 16:16:22,916 quote unquote scratch let's go ahead and 25788 16:16:20,160 --> 16:16:25,116 increment Scratch by one we can't do 25789 16:16:22,916 --> 16:16:28,276 plus plus in Python but we can do plus 25790 16:16:25,116 --> 16:16:32,636 equals 1 how about L if favorite equals 25791 16:16:28,276 --> 16:16:35,276 equals c then let's do C+ equal 1 L and 25792 16:16:32,636 --> 16:16:37,240 not we could do else this is actually a 25793 16:16:35,276 --> 16:16:43,916 good design question should I do else 25794 16:16:37,240 --> 16:16:43,916 should I do L if any instincts here 25795 16:16:48,320 --> 16:16:52,360 yeah yeah really good instincts just in 25796 16:16:50,840 --> 16:16:53,880 case someone goes and adds another 25797 16:16:52,360 --> 16:16:54,840 language to the form next week because 25798 16:16:53,880 --> 16:16:56,116 we're obviously going to introduce 25799 16:16:54,840 --> 16:16:57,956 another language today you don't want 25800 16:16:56,116 --> 16:17:00,240 your code to now artificially inflate 25801 16:16:57,956 --> 16:17:02,400 the scores for python just because 25802 16:17:00,240 --> 16:17:04,276 you're conflating multiple languages 25803 16:17:02,400 --> 16:17:06,200 together so the more defensive sort of 25804 16:17:04,276 --> 16:17:09,520 better way to write this code I agree 25805 16:17:06,200 --> 16:17:11,436 would be L if favorite equals equals 25806 16:17:09,520 --> 16:17:14,000 python then let's go ahead and increment 25807 16:17:11,436 --> 16:17:15,480 python plus equals 1 and if there's a 25808 16:17:14,000 --> 16:17:16,756 new language next week we're obviously 25809 16:17:15,480 --> 16:17:18,320 going to have to update the code but at 25810 16:17:16,756 --> 16:17:19,796 least we're not miscounting we're just 25811 16:17:18,320 --> 16:17:21,840 missing the new language so I think 25812 16:17:19,796 --> 16:17:23,480 that's slightly more robust all right 25813 16:17:21,840 --> 16:17:25,160 now at the very bottom of this program 25814 16:17:23,480 --> 16:17:27,756 and outside of the loop when I'm all 25815 16:17:25,160 --> 16:17:30,320 done counting let me go ahead and print 25816 16:17:27,756 --> 16:17:32,436 out using some f strings how about the 25817 16:17:30,320 --> 16:17:35,200 total number of people who uh whose 25818 16:17:32,436 --> 16:17:37,916 favorite is scratch so this is just uh 25819 16:17:35,200 --> 16:17:40,596 week six F string syntax let me go ahead 25820 16:17:37,916 --> 16:17:42,720 and print out another F string for C and 25821 16:17:40,596 --> 16:17:44,480 I'm of course putting the variables in 25822 16:17:42,720 --> 16:17:46,956 curly braces all lowercase but the 25823 16:17:44,480 --> 16:17:49,680 English words I'm doing capitalization 25824 16:17:46,956 --> 16:17:51,956 for Let's do an final one with f uh 25825 16:17:49,680 --> 16:17:54,436 python colon and then in curly braces 25826 16:17:51,956 --> 16:17:56,000 python close quote and I think I'm done 25827 16:17:54,436 --> 16:17:58,080 so let me just hide my terminal for a 25828 16:17:56,000 --> 16:18:01,636 second here's the total program same 25829 16:17:58,080 --> 16:18:04,000 stuff as before open favorites. CSV open 25830 16:18:01,636 --> 16:18:05,880 it further with the dictionary reader to 25831 16:18:04,000 --> 16:18:07,720 do that processing for us initialize 25832 16:18:05,880 --> 16:18:10,000 three variables to zero just so we have 25833 16:18:07,720 --> 16:18:12,040 something to count with and then iterate 25834 16:18:10,000 --> 16:18:13,880 over the file row by row and this is 25835 16:18:12,040 --> 16:18:15,636 just some sort of week one style 25836 16:18:13,880 --> 16:18:18,080 conditional logic albe it in Python 25837 16:18:15,636 --> 16:18:20,276 counting things all right so how can we 25838 16:18:18,080 --> 16:18:23,596 now execute this let me go back to my 25839 16:18:20,276 --> 16:18:26,560 terminal python of favorites.i and here 25840 16:18:23,596 --> 16:18:28,840 we go uh as of today everyone who's 25841 16:18:26,560 --> 16:18:32,796 reporting in live via the Google form 25842 16:18:28,840 --> 16:18:34,680 their favorite languages are 25843 16:18:32,796 --> 16:18:36,360 interesting that's pretty interesting 25844 16:18:34,680 --> 16:18:38,080 too after just one week of python no 25845 16:18:36,360 --> 16:18:39,796 less so but scratch is a healthy 25846 16:18:38,080 --> 16:18:42,400 Contender there a lot of C so a pretty 25847 16:18:39,796 --> 16:18:45,596 good mix here so is this going to be the 25848 16:18:42,400 --> 16:18:46,916 best way to write this program long term 25849 16:18:45,596 --> 16:18:48,596 well as you noted if there's a new 25850 16:18:46,916 --> 16:18:50,880 language next week this week we're going 25851 16:18:48,596 --> 16:18:52,560 to have to constantly update this and 25852 16:18:50,880 --> 16:18:54,160 here's where you should let your mind 25853 16:18:52,560 --> 16:18:56,436 wander to like the future like if we 25854 16:18:54,160 --> 16:18:59,916 have a fourth language fifth language 25855 16:18:56,436 --> 16:19:02,040 6th sth eth which aspect here might kind 25856 16:18:59,916 --> 16:19:04,520 of have some code smell to it like this 25857 16:19:02,040 --> 16:19:05,880 probably isn't the best design to set us 25858 16:19:04,520 --> 16:19:09,956 up for the 25859 16:19:05,880 --> 16:19:12,956 future what might be better than this 25860 16:19:09,956 --> 16:19:14,840 yeah add language line yeah we have to 25861 16:19:12,956 --> 16:19:16,080 keep adding a language to line five and 25862 16:19:14,840 --> 16:19:19,080 okay not a big deal we could add like 25863 16:19:16,080 --> 16:19:21,200 SQL today and maybe JavaScript next week 25864 16:19:19,080 --> 16:19:22,636 but you know anytime a a line of code a 25865 16:19:21,200 --> 16:19:24,400 line of logic is just going to kind of 25866 16:19:22,636 --> 16:19:25,756 grow out of control and we've had this 25867 16:19:24,400 --> 16:19:27,636 chat a couple of times with different 25868 16:19:25,756 --> 16:19:29,436 syntax there's probably a better way 25869 16:19:27,636 --> 16:19:31,360 than that so let's do that instead of 25870 16:19:29,436 --> 16:19:33,720 using these individual variables we 25871 16:19:31,360 --> 16:19:36,276 could make maybe use a list but a list 25872 16:19:33,720 --> 16:19:38,200 would be a little confusing because like 25873 16:19:36,276 --> 16:19:40,320 what does braet zero mean what is 25874 16:19:38,200 --> 16:19:42,520 bracket 1 bracket two but a dictionary 25875 16:19:40,320 --> 16:19:43,840 recall is like this Swiss army knife of 25876 16:19:42,520 --> 16:19:45,680 data structures whereby you can 25877 16:19:43,840 --> 16:19:48,520 associate anything with anything else 25878 16:19:45,680 --> 16:19:50,596 keys with values so I dare say a cleaner 25879 16:19:48,520 --> 16:19:52,956 way to solve this problem that sets us 25880 16:19:50,596 --> 16:19:54,636 up for Less work or confusion later 25881 16:19:52,956 --> 16:19:56,400 would be to create like a new variable 25882 16:19:54,636 --> 16:19:58,560 called counts if that's what we're doing 25883 16:19:56,400 --> 16:20:01,080 counting things up and just set it equal 25884 16:19:58,560 --> 16:20:02,956 to an empty dictionary and you can 25885 16:20:01,080 --> 16:20:04,756 literally say adct with the open 25886 16:20:02,956 --> 16:20:07,520 parenthesis closed parenthesis nothing 25887 16:20:04,756 --> 16:20:09,400 or the more pythonic just use open and 25888 16:20:07,520 --> 16:20:10,880 Clos curly braces with nothing inside 25889 16:20:09,400 --> 16:20:14,080 that gives me an empty dictionary just 25890 16:20:10,880 --> 16:20:15,796 like square brackets gives me a list now 25891 16:20:14,080 --> 16:20:17,916 my logic down here has to change a 25892 16:20:15,796 --> 16:20:21,520 little bit but what's nice is I don't 25893 16:20:17,916 --> 16:20:22,756 need one conditional for every language 25894 16:20:21,520 --> 16:20:25,040 because again if we have a fourth a 25895 16:20:22,756 --> 16:20:27,080 fifth a sixth that chunk of code is also 25896 16:20:25,040 --> 16:20:29,436 going to grow a bit out of control too 25897 16:20:27,080 --> 16:20:31,680 so I can get rid of this here and what I 25898 16:20:29,436 --> 16:20:33,320 think I'm going to do is say this 25899 16:20:31,680 --> 16:20:35,400 whatever the current favorite is from 25900 16:20:33,320 --> 16:20:40,796 the current Row in the file why don't we 25901 16:20:35,400 --> 16:20:42,756 go into our counts variable at that key 25902 16:20:40,796 --> 16:20:44,596 and again favorite is a variable it's 25903 16:20:42,756 --> 16:20:47,680 not quote unquote favorite it's going to 25904 16:20:44,596 --> 16:20:49,360 be scratch or C or Python and then why 25905 16:20:47,680 --> 16:20:52,480 don't we go ahead and just increment 25906 16:20:49,360 --> 16:20:55,880 whatever the value of that count is at 25907 16:20:52,480 --> 16:20:58,596 that key now this is technically bugy 25908 16:20:55,880 --> 16:21:01,240 we're really close but there is a 25909 16:20:58,596 --> 16:21:04,840 bug does anyone want to conjecture what 25910 16:21:01,240 --> 16:21:04,840 the bug is 25911 16:21:09,840 --> 16:21:15,116 yeah a good question that answers my 25912 16:21:12,520 --> 16:21:16,636 question in uh nonetheless so no like 25913 16:21:15,116 --> 16:21:18,796 the magic you describe will not happen 25914 16:21:16,636 --> 16:21:22,840 and to repeat the the hypothesis will 25915 16:21:18,796 --> 16:21:25,360 this automatically create a key for 25916 16:21:22,840 --> 16:21:27,080 every uh language that we try plugging 25917 16:21:25,360 --> 16:21:29,080 into those square brackets short answer 25918 16:21:27,080 --> 16:21:31,000 no odds are this is going to create a 25919 16:21:29,080 --> 16:21:32,400 key error one of those traceback error 25920 16:21:31,000 --> 16:21:34,520 messages that you've probably seen by 25921 16:21:32,400 --> 16:21:37,320 now either in class or in problem sets 25922 16:21:34,520 --> 16:21:39,520 whereby if scratch hasn't appeared in 25923 16:21:37,320 --> 16:21:41,680 the dictionary before or C or python 25924 16:21:39,520 --> 16:21:43,520 like then the dictionary has no clue 25925 16:21:41,680 --> 16:21:44,880 what you're talking about so I think we 25926 16:21:43,520 --> 16:21:46,636 actually still need some conditional 25927 16:21:44,880 --> 16:21:48,320 logic but not that's going to grow 25928 16:21:46,636 --> 16:21:49,756 longer and longer with each language 25929 16:21:48,320 --> 16:21:53,596 what I think we probably want to do is 25930 16:21:49,756 --> 16:21:55,520 this if the current favorite is in the 25931 16:21:53,596 --> 16:21:57,636 counts dictionary and this is the 25932 16:21:55,520 --> 16:22:00,480 pythonic way of just saying is this key 25933 16:21:57,636 --> 16:22:04,560 in this dictionary then go ahead and 25934 16:22:00,480 --> 16:22:07,080 safely do count favorite plus equals 1 25935 16:22:04,560 --> 16:22:09,436 else to your conjecture now else what do 25936 16:22:07,080 --> 16:22:11,080 I want to do counts 25937 16:22:09,436 --> 16:22:14,560 favorite 25938 16:22:11,080 --> 16:22:16,796 equals yeah one so initialize a brand 25939 16:22:14,560 --> 16:22:18,360 new key to a brand new value of one 25940 16:22:16,796 --> 16:22:21,040 because I'm obviously just seen this 25941 16:22:18,360 --> 16:22:22,796 language otherwise increment again and 25942 16:22:21,040 --> 16:22:25,116 again and now down here I just need to 25943 16:22:22,796 --> 16:22:27,360 tweak my syntax a little bit I don't 25944 16:22:25,116 --> 16:22:29,436 need to print out all of these things 25945 16:22:27,360 --> 16:22:31,680 one at a time manually I can actually 25946 16:22:29,436 --> 16:22:33,680 get away I think with another loop at 25947 16:22:31,680 --> 16:22:36,756 the very bottom here so how about I do 25948 16:22:33,680 --> 16:22:38,360 this for each favorite in those counts 25949 16:22:36,756 --> 16:22:40,916 and this is again the pythonic way to 25950 16:22:38,360 --> 16:22:43,320 iterate over all of the keys in a 25951 16:22:40,916 --> 16:22:46,756 dictionary go ahead and print out using 25952 16:22:43,320 --> 16:22:50,200 an F string whatever the current 25953 16:22:46,756 --> 16:22:53,520 favorite is scratch or C or Python and 25954 16:22:50,200 --> 16:22:55,436 then a colon and then figure out what 25955 16:22:53,520 --> 16:22:57,560 its count is and you can do that by 25956 16:22:55,436 --> 16:23:00,720 going into the counts dictionary looking 25957 16:22:57,560 --> 16:23:02,916 at the favorite key and get back its 25958 16:23:00,720 --> 16:23:04,276 value so close my curly braces I close 25959 16:23:02,916 --> 16:23:06,560 my quotes and even though this looks 25960 16:23:04,276 --> 16:23:08,636 ugly at the moment now this is much more 25961 16:23:06,560 --> 16:23:10,480 dynamic because if we go and add SQL to 25962 16:23:08,636 --> 16:23:13,116 the CSV file tomorrow where we add 25963 16:23:10,480 --> 16:23:15,080 JavaScript next week this will just work 25964 16:23:13,116 --> 16:23:16,756 it will keep working now automatically 25965 16:23:15,080 --> 16:23:18,840 all I change is the Google form not my 25966 16:23:16,756 --> 16:23:21,160 actual code all right let's try python 25967 16:23:18,840 --> 16:23:24,636 of favorites.i cross my fingers as 25968 16:23:21,160 --> 16:23:29,796 always and there now is the data as of 25969 16:23:24,636 --> 16:23:29,796 now questions on this code here 25970 16:23:30,000 --> 16:23:34,400 yeah really good good question what if 25971 16:23:32,000 --> 16:23:37,116 you wanted to print it in a particular 25972 16:23:34,400 --> 16:23:38,480 order uh well I could give you a couple 25973 16:23:37,116 --> 16:23:42,040 of solutions like if you want to print 25974 16:23:38,480 --> 16:23:43,916 it out in it's already coincidentally in 25975 16:23:42,040 --> 16:23:46,116 alphabetical order so you got that for 25976 16:23:43,916 --> 16:23:47,916 free although that's just by chance here 25977 16:23:46,116 --> 16:23:49,796 but there is a way to do this and let me 25978 16:23:47,916 --> 16:23:51,360 propose that we go down here to my Loop 25979 16:23:49,796 --> 16:23:52,840 and I explicitly use a function you 25980 16:23:51,360 --> 16:23:54,560 might not have seen in Python yet but 25981 16:23:52,840 --> 16:23:56,276 it's literally called sorted which is 25982 16:23:54,560 --> 16:23:58,840 going to take either a list or in this 25983 16:23:56,276 --> 16:24:01,956 case a dictionary and by default sort it 25984 16:23:58,840 --> 16:24:03,400 by key alphabetically now if my 25985 16:24:01,956 --> 16:24:04,916 intuition is correct this is not going 25986 16:24:03,400 --> 16:24:06,560 to change the output because it's 25987 16:24:04,916 --> 16:24:08,480 already alphabetical but if you read the 25988 16:24:06,560 --> 16:24:10,680 documentation for the sorted function it 25989 16:24:08,480 --> 16:24:12,596 takes multiple parameters potentially 25990 16:24:10,680 --> 16:24:14,560 some of which are named parameters and 25991 16:24:12,596 --> 16:24:16,080 so you can actually do this if you want 25992 16:24:14,560 --> 16:24:18,040 to sort the counts but you want to 25993 16:24:16,080 --> 16:24:19,880 reverse the order for whatever reason 25994 16:24:18,040 --> 16:24:21,916 here so that it's reverse alphabetical 25995 16:24:19,880 --> 16:24:23,240 order now let me go ahead and rerun this 25996 16:24:21,916 --> 16:24:27,160 and I'll keep the previous output on the 25997 16:24:23,240 --> 16:24:29,756 screen enter and now it's backwards uh 25998 16:24:27,160 --> 16:24:33,276 alphabetically if you will other 25999 16:24:29,756 --> 16:24:37,720 questions on this here 26000 16:24:33,276 --> 16:24:39,560 here no how about then how about then we 26001 16:24:37,720 --> 16:24:42,636 transition 26002 16:24:39,560 --> 16:24:44,200 to changing sorting by value and let me 26003 16:24:42,636 --> 16:24:45,840 this is going to escalate a little 26004 16:24:44,200 --> 16:24:48,160 quickly briefly but then we'll we'll 26005 16:24:45,840 --> 16:24:50,560 tone it down again notice that right now 26006 16:24:48,160 --> 16:24:51,916 this is indeed sorting by key what if 26007 16:24:50,560 --> 16:24:53,840 especially if I have lots of data it'd 26008 16:24:51,916 --> 16:24:55,560 be nice to make like a top 10 list or in 26009 16:24:53,840 --> 16:24:59,840 this case a top three list and actually 26010 16:24:55,560 --> 16:25:01,636 see in order of the counts the values uh 26011 16:24:59,840 --> 16:25:03,916 what these popular ones are so it's not 26012 16:25:01,636 --> 16:25:07,240 C python scratch it should ideally be 26013 16:25:03,916 --> 16:25:09,320 python then C then scratch because of 26014 16:25:07,240 --> 16:25:11,116 the values and the magnitude thereof so 26015 16:25:09,320 --> 16:25:14,320 how can I do this well it turns out 26016 16:25:11,116 --> 16:25:17,796 there's another key another uh parameter 26017 16:25:14,320 --> 16:25:19,680 that you can pass to the sorted function 26018 16:25:17,796 --> 16:25:21,240 that is typically implemented as a 26019 16:25:19,680 --> 16:25:22,160 function itself and so I'm going to go 26020 16:25:21,240 --> 16:25:24,680 ahead and do this I'm going to 26021 16:25:22,160 --> 16:25:27,000 temporarily Define a function called get 26022 16:25:24,680 --> 16:25:29,636 value just to make my life easier and 26023 16:25:27,000 --> 16:25:32,116 this get value function is going to take 26024 16:25:29,636 --> 16:25:34,636 I'll say a language par parameter and 26025 16:25:32,116 --> 16:25:37,756 then all I'm going to do is return 26026 16:25:34,636 --> 16:25:39,400 whatever the count is of that language 26027 16:25:37,756 --> 16:25:41,000 so out of context this is just a super 26028 16:25:39,400 --> 16:25:43,680 simple function that you hand it a 26029 16:25:41,000 --> 16:25:44,680 language like scratch or C or python 26030 16:25:43,680 --> 16:25:47,400 it's just going to tell you what the 26031 16:25:44,680 --> 16:25:49,480 count is thereof in that uh in that 26032 16:25:47,400 --> 16:25:51,720 dictionary called counts but what I can 26033 16:25:49,480 --> 16:25:55,596 do now down here in my newly introduced 26034 16:25:51,720 --> 16:25:57,840 call to sorted is I can tell it what to 26035 16:25:55,596 --> 16:26:00,680 use as its key instead of using 26036 16:25:57,840 --> 16:26:02,756 literally the key scratch C python I can 26037 16:26:00,680 --> 16:26:05,436 sort of override that behavior and say 26038 16:26:02,756 --> 16:26:08,200 you know what to figure out what to sort 26039 16:26:05,436 --> 16:26:11,596 by go ahead and call this function 26040 16:26:08,200 --> 16:26:13,796 called get value notice that I have not 26041 16:26:11,596 --> 16:26:16,040 put parentheses after get value because 26042 16:26:13,796 --> 16:26:18,880 I don't want to call get value right 26043 16:26:16,040 --> 16:26:22,636 then and there I want to pass the get 26044 16:26:18,880 --> 16:26:24,320 value function as itself in argument to 26045 16:26:22,636 --> 16:26:25,916 the sorted function so that the sorted 26046 16:26:24,320 --> 16:26:28,520 function written years ago by the people 26047 16:26:25,916 --> 16:26:30,520 at python can call my version of get 26048 16:26:28,520 --> 16:26:32,880 value again and again and again when 26049 16:26:30,520 --> 16:26:36,560 they try to sort sort this actual data 26050 16:26:32,880 --> 16:26:38,840 so now if I add that and I leave reverse 26051 16:26:36,560 --> 16:26:42,116 equals true let's see what happens 26052 16:26:38,840 --> 16:26:44,436 python of favorites. py enter and now I 26053 16:26:42,116 --> 16:26:46,160 get my top 10 or in this case top three 26054 16:26:44,436 --> 16:26:48,200 list and if I had more sophisticated 26055 16:26:46,160 --> 16:26:50,000 data with like more columns Al together 26056 16:26:48,200 --> 16:26:53,560 that I actually care about I could even 26057 16:26:50,000 --> 16:26:54,880 sort this more uh powerfully as well but 26058 16:26:53,560 --> 16:26:56,116 let me clean this up a little bit just 26059 16:26:54,880 --> 16:26:58,840 so you've seen it even though we won't 26060 16:26:56,116 --> 16:27:01,360 use these that often in cs50 until the 26061 16:26:58,840 --> 16:27:03,276 end of the class will they come up again 26062 16:27:01,360 --> 16:27:05,080 technically this is a little bit H this 26063 16:27:03,276 --> 16:27:06,560 isn't necessarily the best design to 26064 16:27:05,080 --> 16:27:08,720 spend all this time implementing a 26065 16:27:06,560 --> 16:27:11,436 function and then only use it in one 26066 16:27:08,720 --> 16:27:12,956 place in general we've argued that H you 26067 16:27:11,436 --> 16:27:14,320 don't necessarily need a variable if 26068 16:27:12,956 --> 16:27:15,400 you're only going to use it in one place 26069 16:27:14,320 --> 16:27:16,796 you don't really need a function if 26070 16:27:15,400 --> 16:27:18,436 you're only going to use it in one place 26071 16:27:16,796 --> 16:27:20,400 and here we kind of have a good 26072 16:27:18,436 --> 16:27:22,720 candidate for that and so it turns out 26073 16:27:20,400 --> 16:27:25,756 in Python if you don't want to bother 26074 16:27:22,720 --> 16:27:27,160 creating a function just to use it once 26075 16:27:25,756 --> 16:27:30,560 you can create what's called an 26076 16:27:27,160 --> 16:27:32,480 anonymous function AKA a Lambda function 26077 16:27:30,560 --> 16:27:34,596 like the lamb Lambda symbol familiar and 26078 16:27:32,480 --> 16:27:37,040 a Lambda function the syntax is a little 26079 16:27:34,596 --> 16:27:39,320 strange looking but you say this you 26080 16:27:37,040 --> 16:27:41,400 literally say Lambda you literally then 26081 16:27:39,320 --> 16:27:43,520 say the name of the argument that you 26082 16:27:41,400 --> 16:27:47,116 want this Anonymous function with no 26083 16:27:43,520 --> 16:27:49,320 name to take then you have a colon and 26084 16:27:47,116 --> 16:27:51,840 then quite simply you write what you 26085 16:27:49,320 --> 16:27:53,480 want the return value of this function 26086 16:27:51,840 --> 16:27:55,320 to be you don't even say return 26087 16:27:53,480 --> 16:27:59,116 literally these Lambda functions are 26088 16:27:55,320 --> 16:28:02,720 meant to be used super turly so that you 26089 16:27:59,116 --> 16:28:04,840 can in one line Express something like 26090 16:28:02,720 --> 16:28:06,200 this and I admit this looks more cryptic 26091 16:28:04,840 --> 16:28:07,636 I think than the previous version but as 26092 16:28:06,200 --> 16:28:08,956 you get more comfortable with python or 26093 16:28:07,636 --> 16:28:11,436 other languages that support this 26094 16:28:08,956 --> 16:28:13,560 feature it allows you to not bother with 26095 16:28:11,436 --> 16:28:15,956 lines of code like that and just tighten 26096 16:28:13,560 --> 16:28:18,436 up your code a little bit so this line 26097 16:28:15,956 --> 16:28:21,596 here lamba language colon counts 26098 16:28:18,436 --> 16:28:22,636 language is the oneline version of this 26099 16:28:21,596 --> 16:28:24,320 and you don't even need to bother 26100 16:28:22,636 --> 16:28:26,436 picking a name for it Lambda tells 26101 16:28:24,320 --> 16:28:27,916 python I didn't waste any time thinking 26102 16:28:26,436 --> 16:28:30,596 of a name for this 26103 16:28:27,916 --> 16:28:33,040 function so questions then on this 26104 16:28:30,596 --> 16:28:36,680 technique technique of using python to 26105 16:28:33,040 --> 16:28:39,080 analyze data like 26106 16:28:36,680 --> 16:28:43,596 this any 26107 16:28:39,080 --> 16:28:46,956 questions we're almost done with python 26108 16:28:43,596 --> 16:28:48,276 questions no okay so why don't we make 26109 16:28:46,956 --> 16:28:50,596 things a little more interesting because 26110 16:28:48,276 --> 16:28:52,080 we had a much juicier data set with the 26111 16:28:50,596 --> 16:28:54,320 problems that we've assigned over the 26112 16:28:52,080 --> 16:28:57,636 past several weeks why don't we go ahead 26113 16:28:54,320 --> 16:28:59,840 and quite simply you know I think we 26114 16:28:57,636 --> 16:29:03,400 wrote pretty darn good code here so I 26115 16:28:59,840 --> 16:29:05,636 think we can pretty much just change a 26116 16:29:03,400 --> 16:29:08,560 bit of it to say let's see if I don't 26117 16:29:05,636 --> 16:29:12,080 want language I want problem and if I 26118 16:29:08,560 --> 16:29:14,000 want to sort by not language but problem 26119 16:29:12,080 --> 16:29:16,240 I think that's it I think if I didn't 26120 16:29:14,000 --> 16:29:18,320 Overlook something here just by changing 26121 16:29:16,240 --> 16:29:20,160 what column I'm reading the data from 26122 16:29:18,320 --> 16:29:21,680 and then just to be consistent renaming 26123 16:29:20,160 --> 16:29:24,636 my variables just so I know what I'm 26124 16:29:21,680 --> 16:29:26,956 looking at what will this program now do 26125 16:29:24,636 --> 16:29:29,840 after those minor 26126 16:29:26,956 --> 16:29:32,276 changes what will I see when I run 26127 16:29:29,840 --> 16:29:33,596 this 26128 16:29:32,276 --> 16:29:36,000 what would be the first thing I see when 26129 16:29:33,596 --> 16:29:36,000 I run 26130 16:29:36,320 --> 16:29:42,840 this tough crowd today yes problem yeah 26131 16:29:40,240 --> 16:29:45,040 the problem this the top problem so the 26132 16:29:42,840 --> 16:29:46,276 most popular problem which I'm a little 26133 16:29:45,040 --> 16:29:47,756 worried it might be hello or just 26134 16:29:46,276 --> 16:29:49,636 scratch but let's go ahead and see so 26135 16:29:47,756 --> 16:29:51,680 let me go ahead and open my terminal 26136 16:29:49,636 --> 16:29:53,560 window I'll even maximize my terminal 26137 16:29:51,680 --> 16:29:56,796 window so we can see a lot let me go 26138 16:29:53,560 --> 16:29:58,400 ahead and run python of favorites. py 26139 16:29:56,796 --> 16:29:59,680 I'm going to go ahead now and cross my 26140 16:29:58,400 --> 16:30:03,756 fingers that I didn't mess up and hit 26141 16:29:59,680 --> 16:30:05,880 enter and okay great we peaked early so 26142 16:30:03,756 --> 16:30:07,956 scratch was the most popular program 26143 16:30:05,880 --> 16:30:10,040 according to the data at the time I 26144 16:30:07,956 --> 16:30:13,200 downloaded it I'm sure other votes have 26145 16:30:10,040 --> 16:30:16,520 come in since filter uh in week four was 26146 16:30:13,200 --> 16:30:19,080 tied then with tan as well Mario is a 26147 16:30:16,520 --> 16:30:21,360 close third there and so forth so this 26148 16:30:19,080 --> 16:30:23,040 is helpful for us on staff that uh not 26149 16:30:21,360 --> 16:30:25,360 so much love down here at the bottom of 26150 16:30:23,040 --> 16:30:26,720 the list so it was a bunch of code to 26151 16:30:25,360 --> 16:30:28,796 write but now that we've written it in 26152 16:30:26,720 --> 16:30:30,400 this very versatile Dynamic way it's 26153 16:30:28,796 --> 16:30:32,636 pretty good for just like crunching data 26154 16:30:30,400 --> 16:30:34,916 and doing some analytics but it's still 26155 16:30:32,636 --> 16:30:37,520 a decent number of lines to have had to 26156 16:30:34,916 --> 16:30:39,796 write manually and this is where 26157 16:30:37,520 --> 16:30:44,160 sometimes it isn't necessarily the right 26158 16:30:39,796 --> 16:30:45,756 tool for the job but rather the job that 26159 16:30:44,160 --> 16:30:47,636 uh but rather a candidate for using some 26160 16:30:45,756 --> 16:30:49,400 other language alog together especially 26161 16:30:47,636 --> 16:30:50,636 when it's not just a one-time program 26162 16:30:49,400 --> 16:30:52,400 that you run and you want to see the 26163 16:30:50,636 --> 16:30:54,360 answer what if you want to take input 26164 16:30:52,400 --> 16:30:56,360 from the user and ask uh answer 26165 16:30:54,360 --> 16:30:57,596 questions dynamically like a mobile app 26166 16:30:56,360 --> 16:30:59,680 would like a website would like 26167 16:30:57,596 --> 16:31:01,320 Microsoft Excel or apple numbers or 26168 16:30:59,680 --> 16:31:03,240 Google Sheets would for for you well 26169 16:31:01,320 --> 16:31:05,680 let's make one final change for now to 26170 16:31:03,240 --> 16:31:08,276 this version of the program and actually 26171 16:31:05,680 --> 16:31:10,680 take in some user input so besides just 26172 16:31:08,276 --> 16:31:14,200 loading all of the data into memory 26173 16:31:10,680 --> 16:31:16,200 let's go ahead and down below here not 26174 16:31:14,200 --> 16:31:18,596 just print out the top 10 list if you 26175 16:31:16,200 --> 16:31:19,956 will but prompt the user for their 26176 16:31:18,596 --> 16:31:21,240 favorite so I'm going to use Python's 26177 16:31:19,956 --> 16:31:23,520 input function and I'm just going to 26178 16:31:21,240 --> 16:31:25,840 prompt them with favorite quote unquote 26179 16:31:23,520 --> 16:31:29,596 like tell me what your favorite problem 26180 16:31:25,840 --> 16:31:32,476 what problem uh rather uh you interested 26181 16:31:29,596 --> 16:31:35,880 in and now let me go ahead and say if 26182 16:31:32,476 --> 16:31:37,360 that favorite is in the counts variable 26183 16:31:35,880 --> 16:31:38,880 so you didn't type in something random 26184 16:31:37,360 --> 16:31:40,880 that we didn't actually assign as a 26185 16:31:38,880 --> 16:31:43,916 problem then let me go ahead and print 26186 16:31:40,880 --> 16:31:46,840 with a format string whatever that 26187 16:31:43,916 --> 16:31:49,360 favorite is of yours and show you the 26188 16:31:46,840 --> 16:31:52,360 actual popularity thereof by indexing 26189 16:31:49,360 --> 16:31:54,320 into counts using that favorite as the 26190 16:31:52,360 --> 16:31:55,840 key and printing this so now it's a 26191 16:31:54,320 --> 16:31:57,636 dynamic program it doesn't dump all of 26192 16:31:55,840 --> 16:31:59,596 the data and all of the summations 26193 16:31:57,636 --> 16:32:01,680 rather it's going to allow me to see 26194 16:31:59,596 --> 16:32:04,680 what my choice of favorite is and I'm 26195 16:32:01,680 --> 16:32:07,636 going to go ahead and say uh let's see 26196 16:32:04,680 --> 16:32:09,160 I'm a fan of Mario here so enter and 26197 16:32:07,636 --> 16:32:11,680 indeed we see the same value we saw a 26198 16:32:09,160 --> 16:32:13,796 moment ago but just for Mario but the 26199 16:32:11,680 --> 16:32:16,436 point now is that one all of this is 26200 16:32:13,796 --> 16:32:18,040 possible two it's way easier and more 26201 16:32:16,436 --> 16:32:19,756 pleasant than this would have been in C 26202 16:32:18,040 --> 16:32:21,756 this is still only 15 lines of code and 26203 16:32:19,756 --> 16:32:23,116 in C again there's the memory management 26204 16:32:21,756 --> 16:32:24,520 there's the iterating over the strings 26205 16:32:23,116 --> 16:32:27,080 trying to find the commas there's just a 26206 16:32:24,520 --> 16:32:28,840 lot more work but honestly even when you 26207 16:32:27,080 --> 16:32:30,956 just want to answer a question like this 26208 16:32:28,840 --> 16:32:31,840 in Excel in Apple Numbers Google sheets 26209 16:32:30,956 --> 16:32:33,240 you know generally you can just 26210 16:32:31,840 --> 16:32:34,596 highlight things you can click a button 26211 16:32:33,240 --> 16:32:37,476 and boom you get your answer for 26212 16:32:34,596 --> 16:32:39,560 summation or Max or Min or any of those 26213 16:32:37,476 --> 16:32:41,040 sort of Basics wouldn't it be nice if we 26214 16:32:39,560 --> 16:32:42,436 weren't taking a step backwards as 26215 16:32:41,040 --> 16:32:44,360 programmers and being sort of more 26216 16:32:42,436 --> 16:32:47,200 powerful and yet we now have to do more 26217 16:32:44,360 --> 16:32:50,000 of the work so maybe sometimes Python's 26218 16:32:47,200 --> 16:32:51,596 not or any language is not the best tool 26219 16:32:50,000 --> 16:32:54,000 for the job and that's going to now 26220 16:32:51,596 --> 16:32:56,080 allow us to introduce more generally 26221 16:32:54,000 --> 16:32:58,756 something called a relational database 26222 16:32:56,080 --> 16:33:00,756 graduating from Mere flat file databases 26223 16:32:58,756 --> 16:33:02,360 like text files or binary files in which 26224 16:33:00,756 --> 16:33:05,720 all of your data is stored to something 26225 16:33:02,360 --> 16:33:05,720 more proper but first 26226 16:33:14,956 --> 16:33:19,520 questions really good question to 26227 16:33:17,276 --> 16:33:21,956 reiterate if I were to is this case 26228 16:33:19,520 --> 16:33:24,160 sensitive so if I were to type in Mario 26229 16:33:21,956 --> 16:33:26,320 in all lowercase and hit enter I 26230 16:33:24,160 --> 16:33:27,720 actually get no such response now that 26231 16:33:26,320 --> 16:33:29,240 might be acceptable because the problem 26232 16:33:27,720 --> 16:33:31,680 technically what is a capital M but 26233 16:33:29,240 --> 16:33:34,720 that's a little uh ridiculous to be that 26234 16:33:31,680 --> 16:33:35,840 pedantic about the input so how could we 26235 16:33:34,720 --> 16:33:38,720 solve 26236 16:33:35,840 --> 16:33:41,476 this any tips for how we can make this a 26237 16:33:38,720 --> 16:33:41,476 little more robust 26238 16:33:47,756 --> 16:33:51,040 yeah okay 26239 16:33:51,116 --> 16:33:54,160 yeah yeah so we could use a few 26240 16:33:53,116 --> 16:33:55,840 different functions one of which is 26241 16:33:54,160 --> 16:33:58,116 called title which will change it to 26242 16:33:55,840 --> 16:33:59,956 title case where it capitalizes like in 26243 16:33:58,116 --> 16:34:02,200 most English sentences the first letter 26244 16:33:59,956 --> 16:34:04,520 of that sentence we could use capitalize 26245 16:34:02,200 --> 16:34:06,360 we could use upper we could use lower 26246 16:34:04,520 --> 16:34:08,956 but indeed we could just decide how we 26247 16:34:06,360 --> 16:34:10,360 want to standardize the capitalization 26248 16:34:08,956 --> 16:34:12,040 either uppercase lowercase or some 26249 16:34:10,360 --> 16:34:14,200 combination thereof and just make sure 26250 16:34:12,040 --> 16:34:15,636 that you change the counts themselves 26251 16:34:14,200 --> 16:34:17,636 make sure that you do the same to 26252 16:34:15,636 --> 16:34:19,240 favorite and make sure that maybe you 26253 16:34:17,636 --> 16:34:21,240 keep a backup of the data if you want to 26254 16:34:19,240 --> 16:34:23,956 show the original version that came from 26255 16:34:21,240 --> 16:34:25,916 the CSV without presuming to just 26256 16:34:23,956 --> 16:34:27,276 capitalize everything for the user but 26257 16:34:25,916 --> 16:34:28,796 indeed that would be the most common 26258 16:34:27,276 --> 16:34:31,080 scenario you just make things case 26259 16:34:28,796 --> 16:34:34,680 insensitive when doing those matches 26260 16:34:31,080 --> 16:34:38,400 other questions now on python before we 26261 16:34:34,680 --> 16:34:40,916 leave it behind for the coming 26262 16:34:38,400 --> 16:34:43,116 week all right well then let's introduce 26263 16:34:40,916 --> 16:34:46,160 these relational databases so relational 26264 16:34:43,116 --> 16:34:48,116 database is what like every is a super 26265 16:34:46,160 --> 16:34:49,596 popular way of storing lots of data like 26266 16:34:48,116 --> 16:34:51,160 this is what the Twitter's of the world 26267 16:34:49,596 --> 16:34:53,796 the Googles of the world the metas of 26268 16:34:51,160 --> 16:34:56,400 the world use to store some of their 26269 16:34:53,796 --> 16:34:58,520 data at scale there are alternatives to 26270 16:34:56,400 --> 16:35:00,040 relational databases um indeed today 26271 16:34:58,520 --> 16:35:01,720 we'll talk about a language called SQL 26272 16:35:00,040 --> 16:35:03,636 there's so a movement if you will or an 26273 16:35:01,720 --> 16:35:05,080 alternative generally called No SQL 26274 16:35:03,636 --> 16:35:06,436 which is just the opposite you don't use 26275 16:35:05,080 --> 16:35:08,436 SQL there are things called 26276 16:35:06,436 --> 16:35:10,400 objectoriented databases and the like 26277 16:35:08,436 --> 16:35:14,160 but if you've ever heard of MySQL or 26278 16:35:10,400 --> 16:35:17,160 postgress SQL or uh Microsoft SQL server 26279 16:35:14,160 --> 16:35:19,796 or Oracle or Maria DB or a bunch of 26280 16:35:17,160 --> 16:35:21,276 other products both free and Commercial 26281 16:35:19,796 --> 16:35:23,080 this is what they're talking about 26282 16:35:21,276 --> 16:35:24,680 databases that are designed to store 26283 16:35:23,080 --> 16:35:26,680 lots of data and what's nice about 26284 16:35:24,680 --> 16:35:28,636 relational databases is that they're 26285 16:35:26,680 --> 16:35:30,436 really similar to the spreadsheets with 26286 16:35:28,636 --> 16:35:33,400 which you were presumably familiar long 26287 16:35:30,436 --> 16:35:35,636 before today's class so a relational 26288 16:35:33,400 --> 16:35:38,080 database is going to store as you'll see 26289 16:35:35,636 --> 16:35:39,636 all of the data in rows and columns now 26290 16:35:38,080 --> 16:35:40,840 the terminology will thereafter be a 26291 16:35:39,636 --> 16:35:43,160 little different instead of having 26292 16:35:40,840 --> 16:35:44,560 sheets you're going to have tables but 26293 16:35:43,160 --> 16:35:45,916 those tables are still going to have 26294 16:35:44,560 --> 16:35:47,956 rows and columns and you're going to 26295 16:35:45,916 --> 16:35:51,116 have even more control over the 26296 16:35:47,956 --> 16:35:53,796 performance of your data when you start 26297 16:35:51,116 --> 16:35:56,320 to access it using this structured query 26298 16:35:53,796 --> 16:35:58,880 language or SQL this is a language you 26299 16:35:56,320 --> 16:36:00,596 can use for web apps mobile apps uh a 26300 16:35:58,880 --> 16:36:02,840 lot of analysts would sit down and their 26301 16:36:00,596 --> 16:36:04,560 Mac or PC and actually ask questions of 26302 16:36:02,840 --> 16:36:06,200 data to get back the answer and 26303 16:36:04,560 --> 16:36:08,400 wonderfully even though there will be 26304 16:36:06,200 --> 16:36:11,000 some new syntax today SQL really just 26305 16:36:08,400 --> 16:36:13,596 does four basic things crud is the sort 26306 16:36:11,000 --> 16:36:16,360 of crude acronym here crud is a way of 26307 16:36:13,596 --> 16:36:18,956 remembering that a relational database 26308 16:36:16,360 --> 16:36:22,276 supports ultimately creating data 26309 16:36:18,956 --> 16:36:24,476 reading data updating data and deleting 26310 16:36:22,276 --> 16:36:25,916 data so even if you're feeling like wow 26311 16:36:24,476 --> 16:36:28,160 this is a lot of new syntax which it 26312 16:36:25,916 --> 16:36:29,636 isn't relative to our past languages the 26313 16:36:28,160 --> 16:36:32,080 only things you're doing really are 26314 16:36:29,636 --> 16:36:34,080 creating dat data reading data updating 26315 16:36:32,080 --> 16:36:37,400 and deleting the same now a little 26316 16:36:34,080 --> 16:36:40,956 confusingly in SQL the corresponding 26317 16:36:37,400 --> 16:36:43,796 functions or commands that exist that 26318 16:36:40,956 --> 16:36:45,240 map to crud are actually this so it's 26319 16:36:43,796 --> 16:36:48,400 still create but there's another one 26320 16:36:45,240 --> 16:36:50,200 called insert uh it's not read which is 26321 16:36:48,400 --> 16:36:52,116 more of the computer scientist way of 26322 16:36:50,200 --> 16:36:53,880 saying it but select which is a little 26323 16:36:52,116 --> 16:36:56,160 more explicit like select data you care 26324 16:36:53,880 --> 16:36:57,636 about update is still update delete is 26325 16:36:56,160 --> 16:36:59,796 still delete but there's another command 26326 16:36:57,636 --> 16:37:03,520 called drop which lets you drop that is 26327 16:36:59,796 --> 16:37:05,720 delete entire tables as well so you can 26328 16:37:03,520 --> 16:37:08,080 create tables using syntax that's 26329 16:37:05,720 --> 16:37:10,680 generally going to look like this you'll 26330 16:37:08,080 --> 16:37:12,596 say create table you'll give the name of 26331 16:37:10,680 --> 16:37:14,320 the table uh which you can call almost 26332 16:37:12,596 --> 16:37:17,116 anything you want but generally all 26333 16:37:14,320 --> 16:37:19,000 lowercase no spaces is best then in 26334 16:37:17,116 --> 16:37:21,240 parentheses you can specify a comma 26335 16:37:19,000 --> 16:37:24,160 separated list of the columns that you 26336 16:37:21,240 --> 16:37:26,636 might want in this table so this is the 26337 16:37:24,160 --> 16:37:28,476 code equivalent in the SQL language of 26338 16:37:26,636 --> 16:37:30,560 like manually opening Google Sheets or 26339 16:37:28,476 --> 16:37:32,320 Excel or numbers and like clicking in 26340 16:37:30,560 --> 16:37:34,276 the top left cell and like typing 26341 16:37:32,320 --> 16:37:36,160 timestamp and then in the next typing 26342 16:37:34,276 --> 16:37:37,596 language and then in the third typing 26343 16:37:36,160 --> 16:37:40,160 problem this is the way to sort of 26344 16:37:37,596 --> 16:37:41,680 Define what your headers are if you will 26345 16:37:40,160 --> 16:37:43,680 in a spreadsheet but now it's called a 26346 16:37:41,680 --> 16:37:45,160 table now we won't use this command 26347 16:37:43,680 --> 16:37:47,400 manually first let's do something a 26348 16:37:45,160 --> 16:37:49,956 little simpler we're going to start off 26349 16:37:47,400 --> 16:37:51,636 by just importing this data ourselves 26350 16:37:49,956 --> 16:37:53,880 and I'm going to go ahead and do this 26351 16:37:51,636 --> 16:37:56,560 let me go back to vs code here I'm going 26352 16:37:53,880 --> 16:37:57,840 to leave behind favorites. py for now 26353 16:37:56,560 --> 16:38:00,636 because now we're going to transition to 26354 16:37:57,840 --> 16:38:04,680 this other language called SQL and to do 26355 16:38:00,636 --> 16:38:06,360 this I am going to create a new database 26356 16:38:04,680 --> 16:38:09,720 file and I'm going to do so using a 26357 16:38:06,360 --> 16:38:11,596 command called SQL light 3 which is just 26358 16:38:09,720 --> 16:38:13,360 the third version thereof and I'm going 26359 16:38:11,596 --> 16:38:15,400 to give the database a name of 26360 16:38:13,360 --> 16:38:16,796 favorites. DB there's different 26361 16:38:15,400 --> 16:38:18,720 conventions but this is one of the most 26362 16:38:16,796 --> 16:38:21,000 common when I hit enter this is going to 26363 16:38:18,720 --> 16:38:22,956 create for me a new empty database just 26364 16:38:21,000 --> 16:38:25,680 like opening an Untitled spreadsheet in 26365 16:38:22,956 --> 16:38:27,160 Excel Google Sheets or Apple Numbers I'm 26366 16:38:25,680 --> 16:38:30,040 being prompted do I want to create 26367 16:38:27,160 --> 16:38:31,636 favorites. DB I'll hit y for yes okay 26368 16:38:30,040 --> 16:38:34,040 we're up and running now you're going to 26369 16:38:31,636 --> 16:38:35,956 notice a different prompt I'm not in my 26370 16:38:34,040 --> 16:38:38,000 Linux prompt per se which is always the 26371 16:38:35,956 --> 16:38:39,796 dollar sign I'm now inside of the 26372 16:38:38,000 --> 16:38:42,596 program called SQL light and we're going 26373 16:38:39,796 --> 16:38:44,796 to use SQL light SQL light 3 as just an 26374 16:38:42,596 --> 16:38:47,520 interactive way for now of playing with 26375 16:38:44,796 --> 16:38:51,116 SQL code at the end of today we'll show 26376 16:38:47,520 --> 16:38:52,636 you how you can use SQL in Python code 26377 16:38:51,116 --> 16:38:54,240 so that you still write python code to 26378 16:38:52,636 --> 16:38:56,000 do whatever you want but you can talk to 26379 16:38:54,240 --> 16:38:58,080 databases using Python and this is 26380 16:38:56,000 --> 16:39:00,476 exactly how web apps mobile apps work 26381 16:38:58,080 --> 16:39:02,320 for instance on iOS uh and an i phone an 26382 16:39:00,476 --> 16:39:04,756 iPad or the like if you want to store 26383 16:39:02,320 --> 16:39:07,116 data it's very often stored in a SQL 26384 16:39:04,756 --> 16:39:09,000 database as we're about to do um but you 26385 16:39:07,116 --> 16:39:10,956 might use a language called Swift or 26386 16:39:09,000 --> 16:39:13,560 objective c and same exists in the world 26387 16:39:10,956 --> 16:39:15,160 of Android using Java or cotlin or 26388 16:39:13,560 --> 16:39:17,400 something else to query the database so 26389 16:39:15,160 --> 16:39:18,916 we're going to see SQL in isolation for 26390 16:39:17,400 --> 16:39:20,596 now like an analyst might just use at 26391 16:39:18,916 --> 16:39:23,796 their Mac or PC but we're going to tie 26392 16:39:20,596 --> 16:39:27,116 it together by Day end so at this 26393 16:39:23,796 --> 16:39:30,720 terminal uh SQL light let me go ahead 26394 16:39:27,116 --> 16:39:32,596 and execute uh this command first I'm 26395 16:39:30,720 --> 16:39:33,520 going to first put SQL light into CSV 26396 16:39:32,596 --> 16:39:35,360 mode because I'm going to cut some 26397 16:39:33,520 --> 16:39:37,796 Corners initially and I'm just going to 26398 16:39:35,360 --> 16:39:39,880 automatically import all of the data 26399 16:39:37,796 --> 16:39:42,680 that was submitted via that Google form 26400 16:39:39,880 --> 16:39:44,520 which I exported as a CSV and uploaded 26401 16:39:42,680 --> 16:39:46,880 to my code space and I'm just going to 26402 16:39:44,520 --> 16:39:49,596 automatically say turn this CSV file 26403 16:39:46,880 --> 16:39:50,680 into a SQL database for me just so I 26404 16:39:49,596 --> 16:39:52,796 don't have to figure out what those 26405 16:39:50,680 --> 16:39:56,116 create table commands are so to do this 26406 16:39:52,796 --> 16:39:58,720 I'm going to say mode CSV so that SQL 26407 16:39:56,116 --> 16:40:00,476 light knows that this is the command uh 26408 16:39:58,720 --> 16:40:02,840 knows that this is a CSV file it's 26409 16:40:00,476 --> 16:40:04,720 literally mode so the dot comes before 26410 16:40:02,840 --> 16:40:07,636 the keyword there and now I'm going to 26411 16:40:04,720 --> 16:40:08,916 say do import and then the name of the 26412 16:40:07,636 --> 16:40:12,720 file I want to import which is 26413 16:40:08,916 --> 16:40:14,720 favorites. CSV and now the name of the 26414 16:40:12,720 --> 16:40:16,436 table that I want to create with that 26415 16:40:14,720 --> 16:40:17,956 data and just for consistency I'm going 26416 16:40:16,436 --> 16:40:19,796 to call it favorites I could change 26417 16:40:17,956 --> 16:40:22,276 these things to be anything I want but 26418 16:40:19,796 --> 16:40:24,200 I'm going to do that and voila nothing 26419 16:40:22,276 --> 16:40:26,436 seems to have happened but just like in 26420 16:40:24,200 --> 16:40:27,756 C and in Python in Linux when nothing 26421 16:40:26,436 --> 16:40:30,520 seems to happen that's usually a good 26422 16:40:27,756 --> 16:40:32,400 thing it means I didn't mess up so if I 26423 16:40:30,520 --> 16:40:34,436 want to see what just happened there's 26424 16:40:32,400 --> 16:40:37,116 this other command and these commands 26425 16:40:34,436 --> 16:40:38,880 that start with dots these are SQL light 26426 16:40:37,116 --> 16:40:41,520 specific which is indeed a lightweight 26427 16:40:38,880 --> 16:40:43,320 version of SQL they're not SQL per se so 26428 16:40:41,520 --> 16:40:44,476 if you're using Oracle or something else 26429 16:40:43,320 --> 16:40:46,276 like that you're not going to use these 26430 16:40:44,476 --> 16:40:48,320 exact commands you'll see the ones we 26431 16:40:46,276 --> 16:40:51,720 use in just a moment and here's the 26432 16:40:48,320 --> 16:40:53,560 first when I type schema the schema of a 26433 16:40:51,720 --> 16:40:55,436 database is the design of the database 26434 16:40:53,560 --> 16:40:57,680 what are the tables what are the columns 26435 16:40:55,436 --> 16:41:00,040 and all of that so when I type schema 26436 16:40:57,680 --> 16:41:02,160 this actually in this case shows me that 26437 16:41:00,040 --> 16:41:05,040 create table command that was 26438 16:41:02,160 --> 16:41:06,756 automatically run for me by just doing 26439 16:41:05,040 --> 16:41:08,680 this import line once I get more 26440 16:41:06,756 --> 16:41:10,596 comfortable with SQL I could literally 26441 16:41:08,680 --> 16:41:12,476 type this out myself or use some program 26442 16:41:10,596 --> 16:41:15,116 to generate that as well but what it's 26443 16:41:12,476 --> 16:41:16,916 creating for me is this create table if 26444 16:41:15,116 --> 16:41:19,400 it doesn't exist even though it's more 26445 16:41:16,916 --> 16:41:21,840 tur than that I want to create a table 26446 16:41:19,400 --> 16:41:24,360 called favorites and then the columns 26447 16:41:21,840 --> 16:41:27,320 for that table are going to be timestamp 26448 16:41:24,360 --> 16:41:29,320 which is going to be text comma language 26449 16:41:27,320 --> 16:41:31,040 which is also going to be text comma 26450 16:41:29,320 --> 16:41:33,916 problem which is also going to be text 26451 16:41:31,040 --> 16:41:36,720 that was just inferred very trivially by 26452 16:41:33,916 --> 16:41:38,520 the do import command to just figure out 26453 16:41:36,720 --> 16:41:41,636 that yes just give me a three column 26454 16:41:38,520 --> 16:41:44,360 database table based on the Google 26455 16:41:41,636 --> 16:41:46,040 form okay questions on this these are 26456 16:41:44,360 --> 16:41:48,080 commands you run once to get up and 26457 16:41:46,040 --> 16:41:50,360 running you don't run these commands 26458 16:41:48,080 --> 16:41:52,596 frequently but we have them on the slide 26459 16:41:50,360 --> 16:41:53,840 just for reference all right so now 26460 16:41:52,596 --> 16:41:55,596 let's do something a little more 26461 16:41:53,840 --> 16:41:57,276 interesting I'm going to clear my SQL 26462 16:41:55,596 --> 16:41:59,360 light terminal here but I'm still in SQL 26463 16:41:57,276 --> 16:42:02,756 light I'm going to now use some of my 26464 16:41:59,360 --> 16:42:06,476 first SQL commands which recall were uh 26465 16:42:02,756 --> 16:42:08,720 were among them uh select so crud c r UD 26466 16:42:06,476 --> 16:42:10,160 D the r was select this is maybe the 26467 16:42:08,720 --> 16:42:12,240 most common the most useful the most 26468 16:42:10,160 --> 16:42:14,160 powerful thing to use with a SQL 26469 16:42:12,240 --> 16:42:15,476 database selecting data to answer 26470 16:42:14,160 --> 16:42:17,840 questions akin to the ones we were 26471 16:42:15,476 --> 16:42:19,360 trying to answer with python this is the 26472 16:42:17,840 --> 16:42:21,040 general syntax anytime you want to 26473 16:42:19,360 --> 16:42:23,360 select data from a SQL database you 26474 16:42:21,040 --> 16:42:25,596 literally say select you then specify 26475 16:42:23,360 --> 16:42:28,200 the column or columns that you want to 26476 16:42:25,596 --> 16:42:30,160 select data from you literally write the 26477 16:42:28,200 --> 16:42:32,160 word from and then you specify the name 26478 16:42:30,160 --> 16:42:34,916 of the table you want to get that data 26479 16:42:32,160 --> 16:42:37,040 from semicolon in this case everything 26480 16:42:34,916 --> 16:42:38,880 that's in capitals here is a sequel 26481 16:42:37,040 --> 16:42:40,756 keyword strictly speaking you don't have 26482 16:42:38,880 --> 16:42:42,756 to capitalize things but we would 26483 16:42:40,756 --> 16:42:44,320 encourage you to do so stylistically and 26484 16:42:42,756 --> 16:42:46,240 especially as you're learning and even 26485 16:42:44,320 --> 16:42:48,840 as you're writing it it just helps to 26486 16:42:46,240 --> 16:42:50,756 distinguish SQL from like words you 26487 16:42:48,840 --> 16:42:53,636 chose like the names of the columns and 26488 16:42:50,756 --> 16:42:55,840 the data there're in so uh do adopt 26489 16:42:53,636 --> 16:42:58,720 early on this convention so let me go 26490 16:42:55,840 --> 16:43:00,360 back now to my code space here I'm 26491 16:42:58,720 --> 16:43:02,320 running my terminal window with SQL 26492 16:43:00,360 --> 16:43:04,080 light 3 inside of it suppose that I just 26493 16:43:02,320 --> 16:43:06,596 want to get all of the data from the 26494 16:43:04,080 --> 16:43:09,240 favorites table which was automatically 26495 16:43:06,596 --> 16:43:13,240 imported let's do this select I want 26496 16:43:09,240 --> 16:43:16,240 everything well I can do timestamp comma 26497 16:43:13,240 --> 16:43:18,520 language comma problem but you know what 26498 16:43:16,240 --> 16:43:20,476 here's a uh convenience already if you 26499 16:43:18,520 --> 16:43:22,360 want everything there's what's called a 26500 16:43:20,476 --> 16:43:24,520 wild card character in SQL which is just 26501 16:43:22,360 --> 16:43:26,560 a star in asterisk which means give me 26502 16:43:24,520 --> 16:43:28,276 every column without my knowing even 26503 16:43:26,560 --> 16:43:31,040 what they're called let me go ahead now 26504 16:43:28,276 --> 16:43:33,840 and say from favorite it's semicolon and 26505 16:43:31,040 --> 16:43:35,916 this is the SQL way of opening the 26506 16:43:33,840 --> 16:43:37,796 database iterating over every row 26507 16:43:35,916 --> 16:43:40,080 they're in printing out every row 26508 16:43:37,796 --> 16:43:42,116 therein done so those three steps which 26509 16:43:40,080 --> 16:43:45,200 was like nine lines of python code give 26510 16:43:42,116 --> 16:43:49,160 or take earlier is now one line of SQL I 26511 16:43:45,200 --> 16:43:51,436 hit enter there is all of the data so I 26512 16:43:49,160 --> 16:43:54,320 see now all of the data just outputed as 26513 16:43:51,436 --> 16:43:55,880 a CSV here but it's not the CSV file 26514 16:43:54,320 --> 16:43:57,916 it's now actually the table and in fact 26515 16:43:55,880 --> 16:43:59,160 just for good measure let me do this CU 26516 16:43:57,916 --> 16:44:01,080 you'll see the behavior a little 26517 16:43:59,160 --> 16:44:03,320 different the next time we open the file 26518 16:44:01,080 --> 16:44:04,880 I've just exited out of SQL light 3 I'm 26519 16:44:03,320 --> 16:44:06,680 going to rerun it but I'm not going to 26520 16:44:04,880 --> 16:44:08,956 reimport the data or do anything like 26521 16:44:06,680 --> 16:44:10,916 that because my file now exists in fact 26522 16:44:08,956 --> 16:44:13,240 let me take one step back if I type LS 26523 16:44:10,916 --> 16:44:15,680 at my Linux prompt there's my favorites. 26524 16:44:13,240 --> 16:44:17,596 py from before there's my favorites. CSV 26525 16:44:15,680 --> 16:44:20,916 from before and here's a third file that 26526 16:44:17,596 --> 16:44:22,916 I did create a moment ago when I first 26527 16:44:20,916 --> 16:44:25,476 ran SQL light 3 so the data is 26528 16:44:22,916 --> 16:44:27,636 persistent it's not using Ram or memory 26529 16:44:25,476 --> 16:44:29,240 anything I do now is save there so let's 26530 16:44:27,636 --> 16:44:31,200 go ahead and rerun SQL light 3 with the 26531 16:44:29,240 --> 16:44:32,400 same file but I'm not going to I don't 26532 16:44:31,200 --> 16:44:34,276 have to reort everything because the 26533 16:44:32,400 --> 16:44:36,956 file already exists let me now do that 26534 16:44:34,276 --> 16:44:39,000 same thing again select star from 26535 16:44:36,956 --> 16:44:40,956 favorites to get all of the data and 26536 16:44:39,000 --> 16:44:42,956 what you'll see now is the same data but 26537 16:44:40,956 --> 16:44:46,240 it's a little prettier now because I 26538 16:44:42,956 --> 16:44:48,240 reran it I effectively disabled CSV mode 26539 16:44:46,240 --> 16:44:52,840 this time and what I'm now seeing is the 26540 16:44:48,240 --> 16:44:54,796 entire contents of this database table 26541 16:44:52,840 --> 16:44:56,560 called favorit now there's nothing new 26542 16:44:54,796 --> 16:44:58,560 here but you're just seeing now like an 26543 16:44:56,560 --> 16:45:00,756 asky or Unicode version of all of the 26544 16:44:58,560 --> 16:45:02,160 same data from that database well 26545 16:45:00,756 --> 16:45:03,596 suppose I want to get a subset of the 26546 16:45:02,160 --> 16:45:05,320 data well let me clear my screen and 26547 16:45:03,596 --> 16:45:07,040 just like in Linux I can hit contrl L 26548 16:45:05,320 --> 16:45:08,840 just to clean things up aesthetically 26549 16:45:07,040 --> 16:45:11,276 suppose I want to get just the languages 26550 16:45:08,840 --> 16:45:13,400 so I could do select language from 26551 16:45:11,276 --> 16:45:16,360 favorites and this will now select not 26552 16:45:13,400 --> 16:45:18,720 all three columns AKA star this will 26553 16:45:16,360 --> 16:45:21,000 only select the language column and all 26554 16:45:18,720 --> 16:45:24,080 of the data they're in if I hit enter 26555 16:45:21,000 --> 16:45:26,520 voila now I just see those there no time 26556 16:45:24,080 --> 16:45:28,596 Stamps no problems it's just a slice of 26557 16:45:26,520 --> 16:45:30,596 the table if you will all right not that 26558 16:45:28,596 --> 16:45:33,116 interesting still still because it's 26559 16:45:30,596 --> 16:45:36,080 just a big column of data but now things 26560 16:45:33,116 --> 16:45:37,720 get more interesting it turns out in SQL 26561 16:45:36,080 --> 16:45:39,636 that there are functions that come with 26562 16:45:37,720 --> 16:45:42,636 this language just like C just like 26563 16:45:39,636 --> 16:45:44,596 python in SQL some of the more useful 26564 16:45:42,636 --> 16:45:47,000 ones some of the simpler ones are these 26565 16:45:44,596 --> 16:45:49,756 here average count distinct lower Max 26566 16:45:47,000 --> 16:45:52,400 Min upper which pretty much uh do what 26567 16:45:49,756 --> 16:45:53,956 they say and count is a particularly 26568 16:45:52,400 --> 16:45:55,756 useful one let's start with that you 26569 16:45:53,956 --> 16:45:57,880 know it's a reasonable question to be 26570 16:45:55,756 --> 16:45:59,680 asked uh how many people submitted the 26571 16:45:57,880 --> 16:46:01,720 Google form by the time I actually 26572 16:45:59,680 --> 16:46:03,720 downloaded the CSV well why don't we go 26573 16:46:01,720 --> 16:46:05,680 ahead and do this let me go back to VSS 26574 16:46:03,720 --> 16:46:09,560 code here in my terminal window let me 26575 16:46:05,680 --> 16:46:12,080 select not star but the count of star so 26576 16:46:09,560 --> 16:46:14,916 give me the count of the rows that are 26577 16:46:12,080 --> 16:46:16,720 being returned from the database called 26578 16:46:14,916 --> 16:46:17,916 uh the database table called favorites 26579 16:46:16,720 --> 16:46:19,680 now when I hit enter I'm not going to 26580 16:46:17,916 --> 16:46:24,436 get all the data I'm just going to get 26581 16:46:19,680 --> 16:46:26,000 simply a number 430 rows came back so 26582 16:46:24,436 --> 16:46:28,000 that's pretty good I now know how much 26583 16:46:26,000 --> 16:46:30,240 data is in there well what languages 26584 16:46:28,000 --> 16:46:33,476 were in there well I could do select 26585 16:46:30,240 --> 16:46:34,796 language from favorit just as before but 26586 16:46:33,476 --> 16:46:36,436 that's not that useful especially if I'm 26587 16:46:34,796 --> 16:46:38,000 inheriting the data like I'm the analyst 26588 16:46:36,436 --> 16:46:39,160 who's been handed a data set by my boss 26589 16:46:38,000 --> 16:46:41,680 and they want me to like crunch some 26590 16:46:39,160 --> 16:46:44,000 numbers okay I could like load this into 26591 16:46:41,680 --> 16:46:46,200 Excel I could sort it but you can use 26592 16:46:44,000 --> 16:46:48,840 SQL now to answer pretty basic questions 26593 16:46:46,200 --> 16:46:50,476 too if you want to select the distinct 26594 16:46:48,840 --> 16:46:52,116 languages in the data set because you 26595 16:46:50,476 --> 16:46:55,276 didn't you weren't privy to the Google 26596 16:46:52,116 --> 16:46:58,756 form let me go ahead and select only the 26597 16:46:55,276 --> 16:47:01,240 distinct languages from the favorites 26598 16:46:58,756 --> 16:47:03,276 table and now I hit enter and I get back 26599 16:47:01,240 --> 16:47:05,116 a much more succinct answer just the 26600 16:47:03,276 --> 16:47:07,040 three languages in question not really 26601 16:47:05,116 --> 16:47:08,880 that useful since I created the Google 26602 16:47:07,040 --> 16:47:10,000 form but certainly if you're inheriting 26603 16:47:08,880 --> 16:47:11,840 data from someone else you've just 26604 16:47:10,000 --> 16:47:13,796 downloaded a data set at least now I'm 26605 16:47:11,840 --> 16:47:16,360 arguably wrapping my mind around what's 26606 16:47:13,796 --> 16:47:18,360 going on now this is not necessary for 26607 16:47:16,360 --> 16:47:20,720 such a small data set but I can combine 26608 16:47:18,360 --> 16:47:23,680 these things select the count of the 26609 16:47:20,720 --> 16:47:25,400 distinct languages in this data set 26610 16:47:23,680 --> 16:47:26,680 called favorites and now I should get 26611 16:47:25,400 --> 16:47:29,320 back what 26612 16:47:26,680 --> 16:47:30,560 answer so hopefully indeed an answer 26613 16:47:29,320 --> 16:47:33,000 called three and what you're getting 26614 16:47:30,560 --> 16:47:36,520 back notice aesthetically too is like a 26615 16:47:33,000 --> 16:47:38,916 mini temporary table when I asked for uh 26616 16:47:36,520 --> 16:47:41,200 just the distinct languages what SQL 26617 16:47:38,916 --> 16:47:43,240 hands me back is this temporary table in 26618 16:47:41,200 --> 16:47:45,796 memory that has one column called 26619 16:47:43,240 --> 16:47:48,116 language and then two row uh three rows 26620 16:47:45,796 --> 16:47:50,200 now this is not saved anywhere it's just 26621 16:47:48,116 --> 16:47:51,956 executed ephemerally like this but 26622 16:47:50,200 --> 16:47:54,000 that's why it's depicted in this way 26623 16:47:51,956 --> 16:47:56,240 what you're getting is subsets of your 26624 16:47:54,000 --> 16:47:58,000 data smaller tables containing some of 26625 16:47:56,240 --> 16:47:59,756 your data and same thing down here this 26626 16:47:58,000 --> 16:48:01,840 is like a crazy long 26627 16:47:59,756 --> 16:48:04,840 uh column name you can rename it if you 26628 16:48:01,840 --> 16:48:06,680 really want uh but uh that's all we're 26629 16:48:04,840 --> 16:48:09,160 seeing there and in fact if that's a 26630 16:48:06,680 --> 16:48:12,040 little ugly we can actually Alias These 26631 16:48:09,160 --> 16:48:14,000 Things N is a common uh name for a 26632 16:48:12,040 --> 16:48:15,796 variable a number in any programming 26633 16:48:14,000 --> 16:48:19,000 language so I can actually alas this to 26634 16:48:15,796 --> 16:48:21,116 be a column called n hit enter and now 26635 16:48:19,000 --> 16:48:23,756 I'm getting a tiny tiny table whose 26636 16:48:21,116 --> 16:48:24,720 column is called n that just has the one 26637 16:48:23,756 --> 16:48:28,116 value 26638 16:48:24,720 --> 16:48:32,636 there all right questions on 26639 16:48:28,116 --> 16:48:32,636 these application of these functions 26640 16:48:32,880 --> 16:48:40,880 here no questions 26641 16:48:36,436 --> 16:48:43,320 yeah say a little louder as oh as as 26642 16:48:40,880 --> 16:48:45,476 literally in English so name this column 26643 16:48:43,320 --> 16:48:47,956 rename this column as this technically 26644 16:48:45,476 --> 16:48:50,436 it creates an alias for the column so 26645 16:48:47,956 --> 16:48:50,436 that's all 26646 16:48:51,680 --> 16:48:56,080 yeah exactly distinct will operate on 26647 16:48:54,320 --> 16:48:57,720 whatever you handed in parentheses and 26648 16:48:56,080 --> 16:48:59,956 get rid of all of the duplicates giving 26649 16:48:57,720 --> 16:49:04,200 you back just the unique 26650 16:48:59,956 --> 16:49:04,200 correct other questions here 26651 16:49:08,840 --> 16:49:13,116 yeah good question when you define an 26652 16:49:11,116 --> 16:49:14,596 alias like n which I just did does it 26653 16:49:13,116 --> 16:49:16,956 become like a variable you can reuse 26654 16:49:14,596 --> 16:49:19,360 short answer no in this case but you can 26655 16:49:16,956 --> 16:49:21,400 reuse it within your same query even 26656 16:49:19,360 --> 16:49:23,880 though these these queries are getting a 26657 16:49:21,400 --> 16:49:27,000 little longer admittedly statements that 26658 16:49:23,880 --> 16:49:29,560 they are uh you can actually reuse n in 26659 16:49:27,000 --> 16:49:30,916 even longer queries so later in your 26660 16:49:29,560 --> 16:49:32,956 query and we'll see a few that are start 26661 16:49:30,916 --> 16:49:35,040 to going to start to grow in length so 26662 16:49:32,956 --> 16:49:36,796 it's a a nice way of nicknaming things 26663 16:49:35,040 --> 16:49:39,240 just to be a little more tur in your 26664 16:49:36,796 --> 16:49:41,080 query so we can transition to some of 26665 16:49:39,240 --> 16:49:43,200 these more sophisticated queries because 26666 16:49:41,080 --> 16:49:45,200 it turns out there's some other uh 26667 16:49:43,200 --> 16:49:47,360 techniques we can introduce as well here 26668 16:49:45,200 --> 16:49:49,080 are some other keywords in SQL and again 26669 16:49:47,360 --> 16:49:50,560 even though like this is another list of 26670 16:49:49,080 --> 16:49:51,956 things there's only four things 26671 16:49:50,560 --> 16:49:54,160 fundamentally we're doing creating 26672 16:49:51,956 --> 16:49:56,276 reading updating and deleting data these 26673 16:49:54,160 --> 16:49:59,240 are just allowing us to like fine-tune 26674 16:49:56,276 --> 16:50:00,916 how we do it exactly so where is going 26675 16:49:59,240 --> 16:50:03,040 to allow us to filter data as we'll do 26676 16:50:00,916 --> 16:50:06,560 in just a moment like select data where 26677 16:50:03,040 --> 16:50:08,276 this conditional is true uh like is 26678 16:50:06,560 --> 16:50:10,436 going to be an alternative to an equal 26679 16:50:08,276 --> 16:50:13,636 sign so instead of looking for exactly 26680 16:50:10,436 --> 16:50:15,956 scratch or exactly python or exactly C 26681 16:50:13,636 --> 16:50:17,276 you can look for something like dot dot 26682 16:50:15,956 --> 16:50:19,560 dot and it can be a little bit of a 26683 16:50:17,276 --> 16:50:21,276 fuzzier match if you will uh with other 26684 16:50:19,560 --> 16:50:23,400 characters as well order by is going to 26685 16:50:21,276 --> 16:50:25,200 deal with sorting limit is going to just 26686 16:50:23,400 --> 16:50:28,116 let me limit the total number of rows 26687 16:50:25,200 --> 16:50:29,956 that come back to one or 10 or finite if 26688 16:50:28,116 --> 16:50:31,080 I don't want to see all 400 plus rows 26689 16:50:29,956 --> 16:50:33,520 all at once because I'm just trying to 26690 16:50:31,080 --> 16:50:35,320 wrap my mind around it and group by is 26691 16:50:33,520 --> 16:50:37,040 best shown by example so let's play with 26692 16:50:35,320 --> 16:50:39,116 just a couple of these as well let me go 26693 16:50:37,040 --> 16:50:41,040 back to vs code here I'll clear my 26694 16:50:39,116 --> 16:50:43,520 screen I'm still in the same SQL light 26695 16:50:41,040 --> 16:50:45,956 instance and let's count how many of you 26696 16:50:43,520 --> 16:50:48,240 likeed C without writing python code as 26697 16:50:45,956 --> 16:50:53,240 before so let me go ahead and select the 26698 16:50:48,240 --> 16:50:57,040 count of the rows from favorites where 26699 16:50:53,240 --> 16:50:58,916 the language in each row equals c and 26700 16:50:57,040 --> 16:51:01,560 the convention in SQL light is to use 26701 16:50:58,916 --> 16:51:03,436 single quotes anytime you're surrounding 26702 16:51:01,560 --> 16:51:06,596 a string that's meant to represent a 26703 16:51:03,436 --> 16:51:08,240 literal piece of text uh as opposed to C 26704 16:51:06,596 --> 16:51:10,360 which was double quotes or python which 26705 16:51:08,240 --> 16:51:13,436 was either so this is selecting the 26706 16:51:10,360 --> 16:51:16,400 count of rows from favorites table where 26707 16:51:13,436 --> 16:51:19,240 the language in question is C enter and 26708 16:51:16,400 --> 16:51:22,000 this gives me 98 notice though if I omit 26709 16:51:19,240 --> 16:51:25,320 that predicate like we did before you'll 26710 16:51:22,000 --> 16:51:27,360 get back the total number of rows that 26711 16:51:25,320 --> 16:51:28,680 were in the table so where is what's 26712 16:51:27,360 --> 16:51:30,720 called a predicate that just allows me 26713 16:51:28,680 --> 16:51:32,520 to filter things just like an if 26714 16:51:30,720 --> 16:51:34,200 condition or the like in a language that 26715 16:51:32,520 --> 16:51:36,040 we've seen before you can be a little 26716 16:51:34,200 --> 16:51:40,160 more specific like how many people 26717 16:51:36,040 --> 16:51:42,636 really liked C and the Mario program uh 26718 16:51:40,160 --> 16:51:44,796 problem specifically well let's do this 26719 16:51:42,636 --> 16:51:48,320 uh let's go ahead and do select the 26720 16:51:44,796 --> 16:51:53,320 number of rows from the favorites table 26721 16:51:48,320 --> 16:51:55,116 where the language is C and so it's uh 26722 16:51:53,320 --> 16:51:58,240 still literally the word ands and or 26723 16:51:55,116 --> 16:51:59,276 just like in Python but not like in C uh 26724 16:51:58,240 --> 16:52:01,116 and 26725 16:51:59,276 --> 16:52:04,040 equals Mario so let's see if there's any 26726 16:52:01,116 --> 16:52:06,520 fans of both C and the Mario problem and 26727 16:52:04,040 --> 16:52:08,520 three of us really like those two things 26728 16:52:06,520 --> 16:52:10,596 together in this case all right what 26729 16:52:08,520 --> 16:52:12,796 else can we do well more compelling 26730 16:52:10,596 --> 16:52:14,596 might be to see kind of like in Python 26731 16:52:12,796 --> 16:52:16,956 for each language what was the 26732 16:52:14,596 --> 16:52:18,720 popularity thereof and at the moment we 26733 16:52:16,956 --> 16:52:20,320 don't really have a way of doing that 26734 16:52:18,720 --> 16:52:21,476 except in Python where we had the loop 26735 16:52:20,320 --> 16:52:22,916 and we had those variables with the 26736 16:52:21,476 --> 16:52:24,880 dictionary that did all that counting 26737 16:52:22,916 --> 16:52:26,400 for us you know totally doable but 26738 16:52:24,880 --> 16:52:28,756 tedious especially if your job is to 26739 16:52:26,400 --> 16:52:30,520 analyze data my God like even writing 15 26740 16:52:28,756 --> 16:52:33,200 lines of code to answer simple questions 26741 16:52:30,520 --> 16:52:36,000 is kind of ridiculous SQL can do better 26742 16:52:33,200 --> 16:52:38,560 for us so let me go ahead and do this 26743 16:52:36,000 --> 16:52:43,436 let me go ahead and select every 26744 16:52:38,560 --> 16:52:47,200 language and the count thereof from the 26745 16:52:43,436 --> 16:52:50,116 favorites table but this time Group by 26746 16:52:47,200 --> 16:52:52,116 language so this was another one of the 26747 16:52:50,116 --> 16:52:54,200 keywords that we can use in this 26748 16:52:52,116 --> 16:52:56,956 abbreviated list of extra features of 26749 16:52:54,200 --> 16:52:58,320 SQL and this one's a little takes a 26750 16:52:56,956 --> 16:53:00,560 moment to wrap your mind around but this 26751 16:52:58,320 --> 16:53:02,476 is going to give me a two column 26752 16:53:00,560 --> 16:53:04,560 temporary table where the First Column 26753 16:53:02,476 --> 16:53:08,080 is a language and the second column is 26754 16:53:04,560 --> 16:53:10,880 the count thereof from this data set and 26755 16:53:08,080 --> 16:53:13,596 group by language just means that only 26756 16:53:10,880 --> 16:53:15,520 show me scratch once only show me c once 26757 16:53:13,596 --> 16:53:18,840 only show me python once that is group 26758 16:53:15,520 --> 16:53:21,796 all of the identical values together but 26759 16:53:18,840 --> 16:53:25,956 keep track of how many of them there are 26760 16:53:21,796 --> 16:53:29,596 and so now if I go over to SQL light and 26761 16:53:25,956 --> 16:53:31,680 I hit enter now I have in SQL version 26762 16:53:29,596 --> 16:53:34,000 the exact same output that I had from 26763 16:53:31,680 --> 16:53:36,436 python that took me what 15 plus lines 26764 16:53:34,000 --> 16:53:38,400 before now we're down to just one 26765 16:53:36,436 --> 16:53:40,560 because SQL structured query language is 26766 16:53:38,400 --> 16:53:43,240 all about constructing queries like this 26767 16:53:40,560 --> 16:53:44,916 to answer questions and get back answers 26768 16:53:43,240 --> 16:53:46,276 quickly if we want to clean this up a 26769 16:53:44,916 --> 16:53:48,520 little bit you asked earlier about 26770 16:53:46,276 --> 16:53:50,916 sorting order well we can do that too 26771 16:53:48,520 --> 16:53:54,476 there's another uh key phrase we can use 26772 16:53:50,916 --> 16:53:57,756 here we can order by the count of those 26773 16:53:54,476 --> 16:53:59,320 rows and then run that query here so now 26774 16:53:57,756 --> 16:54:01,520 unfortunately they're from smallest to 26775 16:53:59,320 --> 16:54:03,000 biggest but we can reverse that it turns 26776 16:54:01,520 --> 16:54:05,040 out and my query is starting to wrap 26777 16:54:03,000 --> 16:54:08,000 here I'll I'll zoom out for a moment if 26778 16:54:05,040 --> 16:54:11,160 you want to order by count the default 26779 16:54:08,000 --> 16:54:13,360 is in ascending order abbreviated ASC if 26780 16:54:11,160 --> 16:54:15,200 you want to reverse the sort in SQL 26781 16:54:13,360 --> 16:54:17,880 instead of using reverse equals true 26782 16:54:15,200 --> 16:54:19,636 like we did in Python you say DC for 26783 16:54:17,880 --> 16:54:22,040 descending order and now we get almost 26784 16:54:19,636 --> 16:54:23,560 the same output but flipped in Reverse 26785 16:54:22,040 --> 16:54:24,916 so it's just a lot faster to answer 26786 16:54:23,560 --> 16:54:27,200 questions once of course you get some 26787 16:54:24,916 --> 16:54:28,956 muscle memory and some comfort with it 26788 16:54:27,200 --> 16:54:30,956 well what else can I do you know what if 26789 16:54:28,956 --> 16:54:32,560 I just care about the most popular 26790 16:54:30,956 --> 16:54:34,116 language I don't care about the second 26791 16:54:32,560 --> 16:54:36,320 place or the third place languages or 26792 16:54:34,116 --> 16:54:39,360 anything else well let me add one more 26793 16:54:36,320 --> 16:54:41,276 Clause here limit the answer to one and 26794 16:54:39,360 --> 16:54:43,636 no matter how many rows should come back 26795 16:54:41,276 --> 16:54:46,320 now I just get the number one language 26796 16:54:43,636 --> 16:54:49,520 as of the data set we collected with 27 26797 16:54:46,320 --> 16:54:51,360 uh 270 votes for 26798 16:54:49,520 --> 16:54:53,436 it 26799 16:54:51,360 --> 16:54:56,880 questions on 26800 16:54:53,436 --> 16:54:59,560 this any questions 26801 16:54:56,880 --> 16:55:01,400 here no well what if uh you know we're 26802 16:54:59,560 --> 16:55:03,436 starting to introduce SQL and it was 26803 16:55:01,400 --> 16:55:05,200 kind of too late to make it into the 26804 16:55:03,436 --> 16:55:07,436 this the Google form so it turns out 26805 16:55:05,200 --> 16:55:08,916 there Syntax for this too you can create 26806 16:55:07,436 --> 16:55:10,720 data of course not just the tables but 26807 16:55:08,916 --> 16:55:13,040 the data they're in and here's like the 26808 16:55:10,720 --> 16:55:15,276 typical Syntax for inserting data into a 26809 16:55:13,040 --> 16:55:17,840 SQL database you literally say insert 26810 16:55:15,276 --> 16:55:20,040 into the name of the table and then in 26811 16:55:17,840 --> 16:55:22,276 parentheses you specify one or more 26812 16:55:20,040 --> 16:55:24,000 columns for which you have values that 26813 16:55:22,276 --> 16:55:26,476 you want to insert this is to say you 26814 16:55:24,000 --> 16:55:28,880 don't have to give values for every 26815 16:55:26,476 --> 16:55:30,560 column in the given row if you only have 26816 16:55:28,880 --> 16:55:32,596 answers to some of those questions you 26817 16:55:30,560 --> 16:55:34,160 can enumerate them here like this but 26818 16:55:32,596 --> 16:55:35,720 the values you insert are going to be 26819 16:55:34,160 --> 16:55:38,520 these so you literally say after the 26820 16:55:35,720 --> 16:55:40,436 closed parenthesis values and then in a 26821 16:55:38,520 --> 16:55:42,916 second set of parentheses with the same 26822 16:55:40,436 --> 16:55:44,756 length comma separated list you specify 26823 16:55:42,916 --> 16:55:47,400 what values do you want to insert so 26824 16:55:44,756 --> 16:55:48,720 it's a little verbose and frankly longer 26825 16:55:47,400 --> 16:55:50,400 term you're going to use like python 26826 16:55:48,720 --> 16:55:52,000 code to automatically do these kinds of 26827 16:55:50,400 --> 16:55:55,040 insertions but let's go ahead and try 26828 16:55:52,000 --> 16:55:59,476 this right now if I do select 26829 16:55:55,040 --> 16:56:01,916 distinct uh language from favorites 26830 16:55:59,476 --> 16:56:03,560 again we see this just these three 26831 16:56:01,916 --> 16:56:06,796 candidates but we've now taught you a 26832 16:56:03,560 --> 16:56:10,240 bit of sequel so let's do insert into 26833 16:56:06,796 --> 16:56:13,080 favorites the column called 26834 16:56:10,240 --> 16:56:15,240 language uh and you know what let's I'm 26835 16:56:13,080 --> 16:56:16,956 going to give a problem here the values 26836 16:56:15,240 --> 16:56:19,040 for which and let me Zoom back out are 26837 16:56:16,956 --> 16:56:21,560 going to be quote unquote SQL and quote 26838 16:56:19,040 --> 16:56:24,520 unquote 50v you'll see soon see what 26839 16:56:21,560 --> 16:56:26,476 that's all about semicolon nothing seems 26840 16:56:24,520 --> 16:56:28,400 to happen but that's usually a good 26841 16:56:26,476 --> 16:56:29,956 thing and now if I scroll back up in my 26842 16:56:28,400 --> 16:56:32,276 my queries in SQL light three you can 26843 16:56:29,956 --> 16:56:35,080 scroll back and forth in time and uh to 26844 16:56:32,276 --> 16:56:38,080 avoid retyping things now I should see 26845 16:56:35,080 --> 16:56:39,636 indeed four candidate languages here now 26846 16:56:38,080 --> 16:56:41,596 suppose that you were never really a fan 26847 16:56:39,636 --> 16:56:43,436 of c and maybe you uh programmed a 26848 16:56:41,596 --> 16:56:46,276 little bit in high school or in the real 26849 16:56:43,436 --> 16:56:50,080 world and you liked C++ well there's a 26850 16:56:46,276 --> 16:56:54,436 whole lot of answers for C so select 26851 16:56:50,080 --> 16:56:56,476 star from favorites where language 26852 16:56:54,436 --> 16:56:59,360 equals quote unquote C so here's 26853 16:56:56,476 --> 16:57:00,840 everyone who submitted the answer for C 26854 16:56:59,360 --> 16:57:02,720 let's presume that no they didn't really 26855 16:57:00,840 --> 16:57:04,320 want C they wanted C++ which is not a 26856 16:57:02,720 --> 16:57:07,040 language we teach in the class but I 26857 16:57:04,320 --> 16:57:09,476 could also now do this you can use the 26858 16:57:07,040 --> 16:57:11,756 update command to set a column or 26859 16:57:09,476 --> 16:57:14,000 columns to different values where some 26860 16:57:11,756 --> 16:57:17,796 condition is met so if I do update table 26861 16:57:14,000 --> 16:57:20,560 name set column name equal to some value 26862 16:57:17,796 --> 16:57:22,796 filtering it perhaps by where some 26863 16:57:20,560 --> 16:57:25,476 condition is true so suppose I've 26864 16:57:22,796 --> 16:57:27,000 changed my mind or you know what let's 26865 16:57:25,476 --> 16:57:31,200 go ahead and do 26866 16:57:27,000 --> 16:57:36,596 update uh favorites set language equal 26867 16:57:31,200 --> 16:57:38,240 to maybe C++ where language equals c now 26868 16:57:36,596 --> 16:57:39,520 this is destructive so you generally 26869 16:57:38,240 --> 16:57:41,476 don't want to do this unless you have a 26870 16:57:39,520 --> 16:57:43,240 backup of your data too overriding what 26871 16:57:41,476 --> 16:57:44,476 people's answers are this seems to have 26872 16:57:43,240 --> 16:57:46,596 been successful because no error 26873 16:57:44,476 --> 16:57:48,276 messages and if I rerun the previous 26874 16:57:46,596 --> 16:57:50,560 select that gives me all of the 26875 16:57:48,276 --> 16:57:53,476 favorites where language equals c now 26876 16:57:50,560 --> 16:57:56,320 indeed I get none but if I search for 26877 16:57:53,476 --> 16:57:57,840 C++ now I get a lot and if I get rid of 26878 16:57:56,320 --> 16:58:00,200 that wear Clause altogether and just 26879 16:57:57,840 --> 16:58:02,560 look at the contents of my database now 26880 16:58:00,200 --> 16:58:04,200 you see that indeed C++ is comingled 26881 16:58:02,560 --> 16:58:06,276 with all the other data this is not what 26882 16:58:04,200 --> 16:58:08,116 you all intended of course so I can undo 26883 16:58:06,276 --> 16:58:10,756 this let me go ahead and undo what I 26884 16:58:08,116 --> 16:58:15,000 just did let me set my favorite language 26885 16:58:10,756 --> 16:58:17,636 to C where language equals C plus plus 26886 16:58:15,000 --> 16:58:20,000 but the predicate is important this I'm 26887 16:58:17,636 --> 16:58:22,840 not going to do what if I accidentally 26888 16:58:20,000 --> 16:58:26,720 omitted this predicate the wear Clause 26889 16:58:22,840 --> 16:58:26,720 how would that screw things up might you 26890 16:58:27,240 --> 16:58:34,000 think uh yeah I'm 26891 16:58:30,596 --> 16:58:36,636 back it would set Every Rose language to 26892 16:58:34,000 --> 16:58:38,360 indeed see and this is dangerous and if 26893 16:58:36,636 --> 16:58:40,796 you start googling around for like 26894 16:58:38,360 --> 16:58:43,000 sequel mistakes or the like people in 26895 16:58:40,796 --> 16:58:44,916 the real world have accidentally run 26896 16:58:43,000 --> 16:58:46,476 commands like this and without naming 26897 16:58:44,916 --> 16:58:48,916 names a former member of our teaching 26898 16:58:46,476 --> 16:58:50,520 staff at one point accidentally re ran a 26899 16:58:48,916 --> 16:58:53,276 command like this and changed every 26900 16:58:50,520 --> 16:58:55,436 student's name in our database to Bobby 26901 16:58:53,276 --> 16:58:57,080 I think it was the same name for every 26902 16:58:55,436 --> 16:58:59,240 row because they simply forgot a 26903 16:58:57,080 --> 16:59:01,360 predicate so here to two like there's 26904 16:58:59,240 --> 16:59:03,636 dangers in code and you should adopt The 26905 16:59:01,360 --> 16:59:05,756 Habit quite quickly of always one 26906 16:59:03,636 --> 16:59:08,000 backing up your data like with CP for 26907 16:59:05,756 --> 16:59:11,240 instance in Linux or any other technique 26908 16:59:08,000 --> 16:59:12,956 or just making sure before you hit enter 26909 16:59:11,240 --> 16:59:14,756 that yes this is indeed the query I want 26910 16:59:12,956 --> 16:59:16,276 to execute and generally speaking in the 26911 16:59:14,756 --> 16:59:18,116 real world there should be process 26912 16:59:16,276 --> 16:59:19,756 controls in place like the intern should 26913 16:59:18,116 --> 16:59:21,400 not have access to the datab the 26914 16:59:19,756 --> 16:59:23,360 production database the live database 26915 16:59:21,400 --> 16:59:24,636 and the like but you have a lot of power 26916 16:59:23,360 --> 16:59:26,956 now with these queries so just be all 26917 16:59:24,636 --> 16:59:29,956 the more careful cuz very easily can you 26918 16:59:26,956 --> 16:59:33,320 do bad things so let me undo this where 26919 16:59:29,956 --> 16:59:35,756 language equals quote unquote C++ and 26920 16:59:33,320 --> 16:59:37,956 I'll Zoom back out enter and now I think 26921 16:59:35,756 --> 16:59:40,680 we're back in business C is among the 26922 16:59:37,956 --> 16:59:40,680 answers 26923 16:59:44,200 --> 16:59:48,680 yeah is essentially doing what at the 26924 16:59:46,436 --> 16:59:50,596 end replace it's essentially find and 26925 16:59:48,680 --> 16:59:52,680 replace yes in like lay person's terms 26926 16:59:50,596 --> 16:59:55,160 this is find and replace implemented 26927 16:59:52,680 --> 16:59:58,200 with SQL and in fact the authors of 26928 16:59:55,160 --> 17:00:00,680 Microsoft Word or Google Docs might very 26929 16:59:58,200 --> 17:00:02,276 well be using language like this sequel 26930 17:00:00,680 --> 17:00:04,520 when you go to the nice graphical 26931 17:00:02,276 --> 17:00:06,080 userfriendly find and replace box this 26932 17:00:04,520 --> 17:00:07,476 may very well be what they're doing 26933 17:00:06,080 --> 17:00:09,276 underneath the hood or of course they 26934 17:00:07,476 --> 17:00:12,240 could be using some other language 26935 17:00:09,276 --> 17:00:13,840 altogether there's one last uh syntax 26936 17:00:12,240 --> 17:00:15,320 that's worth knowing delete which for 26937 17:00:13,840 --> 17:00:17,240 better for worse is even more 26938 17:00:15,320 --> 17:00:19,436 destructive whereby it allows you to 26939 17:00:17,240 --> 17:00:21,880 delete rows from tables it's distinct 26940 17:00:19,436 --> 17:00:24,680 from drop which lets you delete tables 26941 17:00:21,880 --> 17:00:28,200 themselves this focuses on rows so 26942 17:00:24,680 --> 17:00:30,796 suppose that you really really didn't 26943 17:00:28,200 --> 17:00:32,720 like let's say uh tedin was a little 26944 17:00:30,796 --> 17:00:34,756 challenging if you adopt if you tackle 26945 17:00:32,720 --> 17:00:36,560 that more comfortable problem so if you 26946 17:00:34,756 --> 17:00:39,320 really don't want to even think about t 26947 17:00:36,560 --> 17:00:42,320 in anymore so why don't we do uh delete 26948 17:00:39,320 --> 17:00:45,080 from favorites where problem equals and 26949 17:00:42,320 --> 17:00:46,880 I won't execute it for real tiamin this 26950 17:00:45,080 --> 17:00:48,796 would have the effect of deleting every 26951 17:00:46,880 --> 17:00:51,040 row including the language therein and 26952 17:00:48,796 --> 17:00:55,520 the time stamp where the student 26953 17:00:51,040 --> 17:00:59,956 answered tiamin worse than this would be 26954 17:00:55,520 --> 17:00:59,956 this why might this be bad 26955 17:01:01,040 --> 17:01:04,840 okay chuckling because like there's no 26956 17:01:02,840 --> 17:01:07,000 predicate there's no filter which means 26957 17:01:04,840 --> 17:01:08,680 literally this would delete all of the 26958 17:01:07,000 --> 17:01:10,796 data so again with great power here 26959 17:01:08,680 --> 17:01:13,276 comes great responsibility now this has 26960 17:01:10,796 --> 17:01:15,400 just been a data set of what 430 rows by 26961 17:01:13,276 --> 17:01:17,360 us dynamically created there's of course 26962 17:01:15,400 --> 17:01:18,560 some really juicy data sets in the real 26963 17:01:17,360 --> 17:01:20,240 world and one website you might have 26964 17:01:18,560 --> 17:01:22,320 heard or an app you might have used is 26965 17:01:20,240 --> 17:01:24,276 IMDb the internet movie database which 26966 17:01:22,320 --> 17:01:26,796 wonderfully makes some of their data 26967 17:01:24,276 --> 17:01:29,436 available for download as CSV files or 26968 17:01:26,796 --> 17:01:31,000 technically tsv files tab separated 26969 17:01:29,436 --> 17:01:33,596 values but what we did in advance of 26970 17:01:31,000 --> 17:01:35,520 class was download some of that data for 26971 17:01:33,596 --> 17:01:36,840 both TV shows in the real world and 26972 17:01:35,520 --> 17:01:38,360 movies in the real world and what's 26973 17:01:36,840 --> 17:01:41,080 wonderful about this data set is it's 26974 17:01:38,360 --> 17:01:43,320 not just dozens or hundreds or even 26975 17:01:41,080 --> 17:01:46,596 thousands of lines there are millions of 26976 17:01:43,320 --> 17:01:48,000 rows of Juicy data TV shows and movies 26977 17:01:46,596 --> 17:01:49,880 with which most folks are probably 26978 17:01:48,000 --> 17:01:52,360 familiar at least with the subset and 26979 17:01:49,880 --> 17:01:55,160 we'll see in just a little bit that this 26980 17:01:52,360 --> 17:01:56,956 data comes in the form of now six 26981 17:01:55,160 --> 17:01:58,636 different tables that we've given you 26982 17:01:56,956 --> 17:02:00,560 and the tables and question for today 26983 17:01:58,636 --> 17:02:02,796 are going to be the people in the TV 26984 17:02:00,560 --> 17:02:04,360 business the Stars they in the shows 26985 17:02:02,796 --> 17:02:06,916 that people are producing and the like 26986 17:02:04,360 --> 17:02:08,240 this is a picture we'll revisit to let 26987 17:02:06,916 --> 17:02:10,476 enable you to wrap your minds around 26988 17:02:08,240 --> 17:02:12,956 what the actual data is this feels like 26989 17:02:10,476 --> 17:02:14,080 a good opportunity though for a snack in 26990 17:02:12,956 --> 17:02:15,596 fact in just a moment we have a whole 26991 17:02:14,080 --> 17:02:17,956 lot of Rice Krispie treats out in the 26992 17:02:15,596 --> 17:02:20,560 lobby but if folks could perhaps 26993 17:02:17,956 --> 17:02:22,680 acknowledge uh this mini wedding cake 26994 17:02:20,560 --> 17:02:26,880 here cs50 own Carter zeni is getting 26995 17:02:22,680 --> 17:02:28,956 married this week so congratulations to 26996 17:02:26,880 --> 17:02:30,160 Carter 26997 17:02:28,956 --> 17:02:32,000 as 26998 17:02:30,160 --> 17:02:35,520 well 26999 17:02:32,000 --> 17:02:35,520 congrats all right there's 27000 17:02:39,560 --> 17:02:43,476 only okay there's only one piece of cake 27001 17:02:41,956 --> 17:02:44,796 in that box but a lot of Rice Krispy 27002 17:02:43,476 --> 17:02:46,276 Treats in the transip let's take 10 27003 17:02:44,796 --> 17:02:49,320 minutes and we'll be back with Internet 27004 17:02:46,276 --> 17:02:50,636 Movie Database in 10 all right we are 27005 17:02:49,320 --> 17:02:53,320 back so if you've never been like you 27006 17:02:50,636 --> 17:02:55,080 can actually go to I imdb.com right now 27007 17:02:53,320 --> 17:02:57,000 and play around or download the mobile 27008 17:02:55,080 --> 17:03:00,080 app and it's just big database of a lot 27009 17:02:57,000 --> 17:03:02,116 of TV show and movies and actors and the 27010 17:03:00,080 --> 17:03:03,680 like but what indeed is nice is you can 27011 17:03:02,116 --> 17:03:05,360 download some of that data and that's in 27012 17:03:03,680 --> 17:03:07,680 what I've done in advance and what we've 27013 17:03:05,360 --> 17:03:10,116 done is we wrote some python code to 27014 17:03:07,680 --> 17:03:12,040 convert some of the uh flat file 27015 17:03:10,116 --> 17:03:15,276 databases that they let you download and 27016 17:03:12,040 --> 17:03:18,400 we converted it into a SQL database with 27017 17:03:15,276 --> 17:03:20,680 six tables so not just one but six that 27018 17:03:18,400 --> 17:03:22,116 ultimately are these here and let me 27019 17:03:20,680 --> 17:03:24,520 just help you wrap your minds around 27020 17:03:22,116 --> 17:03:26,436 what this picture is which is a entity 27021 17:03:24,520 --> 17:03:28,400 relationship diagram which is just to 27022 17:03:26,436 --> 17:03:30,680 say each of these boxes on the screen 27023 17:03:28,400 --> 17:03:32,756 represents a table and each of the 27024 17:03:30,680 --> 17:03:34,680 arrows or edges represents some kind of 27025 17:03:32,756 --> 17:03:36,720 relationship across the tables because 27026 17:03:34,680 --> 17:03:38,360 up until now the only data we had were 27027 17:03:36,720 --> 17:03:40,040 those three columns in the favorites 27028 17:03:38,360 --> 17:03:42,320 table but what's gets really useful 27029 17:03:40,040 --> 17:03:44,476 about SQL databases just like a Google 27030 17:03:42,320 --> 17:03:46,476 spreadsheet or an Excel file is you can 27031 17:03:44,476 --> 17:03:48,956 have multiple sheets or in a database 27032 17:03:46,476 --> 17:03:51,476 multiple tables and so what we're about 27033 17:03:48,956 --> 17:03:53,560 to see is that in this IMDb database for 27034 17:03:51,476 --> 17:03:55,636 TV shows there's going to be a dedicated 27035 17:03:53,560 --> 17:03:57,840 table for all the people in the TV 27036 17:03:55,636 --> 17:03:59,756 business there's going to be a dedicated 27037 17:03:57,840 --> 17:04:01,636 table for all of the TV shows that are 27038 17:03:59,756 --> 17:04:03,520 in their database as of right now 27039 17:04:01,636 --> 17:04:06,436 there's going to be a dedicated table 27040 17:04:03,520 --> 17:04:09,880 for writers in that industry for the 27041 17:04:06,436 --> 17:04:12,040 ratings of uh shows for the genres to 27042 17:04:09,880 --> 17:04:14,080 which shows belong comedy and the like 27043 17:04:12,040 --> 17:04:15,956 and then lastly there's going to be this 27044 17:04:14,080 --> 17:04:18,916 table which somehow 27045 17:04:15,956 --> 17:04:22,476 Associates people with the TV shows that 27046 17:04:18,916 --> 17:04:24,240 they star in and vice versa and so let's 27047 17:04:22,476 --> 17:04:25,520 consider first what this looks like in 27048 17:04:24,240 --> 17:04:27,840 code and we'll see that it's going to 27049 17:04:25,520 --> 17:04:29,320 overwhelm intentionally at first but I'm 27050 17:04:27,840 --> 17:04:30,756 going to do this I'm going to go back to 27051 17:04:29,320 --> 17:04:32,720 my terminal window and during the break 27052 17:04:30,756 --> 17:04:35,276 I downloaded from the course's website a 27053 17:04:32,720 --> 17:04:37,840 file called shows. DB which we made in 27054 17:04:35,276 --> 17:04:39,400 advance for you and if I type LS I'll 27055 17:04:37,840 --> 17:04:42,000 see all of my favorites files from 27056 17:04:39,400 --> 17:04:45,320 before the CSV the DB and the python 27057 17:04:42,000 --> 17:04:47,000 file but now there's shows. DB so I'm 27058 17:04:45,320 --> 17:04:48,680 going to go ahead in my full screen 27059 17:04:47,000 --> 17:04:50,840 terminal window here I'm not using 27060 17:04:48,680 --> 17:04:53,596 actual tabs or code files now I'm going 27061 17:04:50,840 --> 17:04:55,880 to run SQL light3 on the file called 27062 17:04:53,596 --> 17:04:58,040 shows. DB and I'm just going to see this 27063 17:04:55,880 --> 17:04:59,796 version information here let me clear my 27064 17:04:58,040 --> 17:05:02,080 screen and run the one command I ran 27065 17:04:59,796 --> 17:05:04,520 earlier to show us the schema of the 27066 17:05:02,080 --> 17:05:06,796 favorites database now we'll see the 27067 17:05:04,520 --> 17:05:08,080 schema for the shows database and 27068 17:05:06,796 --> 17:05:09,796 there's a lot going on here but let me 27069 17:05:08,080 --> 17:05:13,796 scroll back up to the very top the 27070 17:05:09,796 --> 17:05:18,596 beginning and we see this here so when I 27071 17:05:13,796 --> 17:05:20,956 run. schema we see a dump really of all 27072 17:05:18,596 --> 17:05:22,720 of the SQL create table commands that 27073 17:05:20,956 --> 17:05:24,636 were run in order to create this 27074 17:05:22,720 --> 17:05:26,840 database for you and one of those tables 27075 17:05:24,636 --> 17:05:30,240 is called genres and another people 27076 17:05:26,840 --> 17:05:31,720 ratings shows stars and so forth and the 27077 17:05:30,240 --> 17:05:33,276 columns therein even though it's 27078 17:05:31,720 --> 17:05:35,276 formatted a little more prettily than 27079 17:05:33,276 --> 17:05:37,360 the automatically generated create table 27080 17:05:35,276 --> 17:05:41,520 statement for favorites whereby we have 27081 17:05:37,360 --> 17:05:43,840 one column per line of output here uh in 27082 17:05:41,520 --> 17:05:45,840 the for instance people table there's 27083 17:05:43,840 --> 17:05:48,596 going to be an ID column like unique 27084 17:05:45,840 --> 17:05:51,200 identifier like a Harvard ID a Yale ID 27085 17:05:48,596 --> 17:05:53,436 or the like uh a name column a birth 27086 17:05:51,200 --> 17:05:55,840 year and then some other stuff if I 27087 17:05:53,436 --> 17:05:58,040 scroll down to shows every show in the 27088 17:05:55,840 --> 17:06:00,636 world is going to have a unique ID as 27089 17:05:58,040 --> 17:06:02,240 well a title of course the year in which 27090 17:06:00,636 --> 17:06:04,116 it debuted and the total number of 27091 17:06:02,240 --> 17:06:06,596 episodes as of the time we downloaded 27092 17:06:04,116 --> 17:06:08,400 the data and then what else is there 27093 17:06:06,596 --> 17:06:12,080 some of these are a little less obvious 27094 17:06:08,400 --> 17:06:14,436 like ratings here so ratings don't have 27095 17:06:12,080 --> 17:06:16,956 an ID column but they have a show ID 27096 17:06:14,436 --> 17:06:18,636 column and a rating like a fivepoint 27097 17:06:16,956 --> 17:06:19,956 scale or 10o scale or the like and then 27098 17:06:18,636 --> 17:06:21,756 the total number of votes that were 27099 17:06:19,956 --> 17:06:23,560 collected to contribute to that rating 27100 17:06:21,756 --> 17:06:25,596 IMDb allows people to like up vote and 27101 17:06:23,560 --> 17:06:28,040 down vote uh shows and movies and the 27102 17:06:25,596 --> 17:06:30,596 like and then similarly is genre 27103 17:06:28,040 --> 17:06:32,116 structured there's a show ID and then 27104 17:06:30,596 --> 17:06:34,000 there's a genre which is going to be 27105 17:06:32,116 --> 17:06:36,360 like an English word like comedy or 27106 17:06:34,000 --> 17:06:37,956 drama or something else and then what 27107 17:06:36,360 --> 17:06:41,160 else let's go a little further at the 27108 17:06:37,956 --> 17:06:43,720 bottom here for stars and writers if we 27109 17:06:41,160 --> 17:06:46,080 go to the very bottom here stars and 27110 17:06:43,720 --> 17:06:49,636 writers are similarly structured too 27111 17:06:46,080 --> 17:06:52,436 they have a show ID and a person ID so 27112 17:06:49,636 --> 17:06:55,040 show and person and then this writer's 27113 17:06:52,436 --> 17:06:56,240 table has a show ID and a person ID and 27114 17:06:55,040 --> 17:06:58,360 there's a whole lot of other words that 27115 17:06:56,240 --> 17:07:00,560 we'll come to in just a moment but what 27116 17:06:58,360 --> 17:07:02,276 are the what is this code hinting at 27117 17:07:00,560 --> 17:07:05,160 well if I go back to the picture from 27118 17:07:02,276 --> 17:07:08,160 earlier here you'll see that this 27119 17:07:05,160 --> 17:07:11,160 picture captures the relationships among 27120 17:07:08,160 --> 17:07:13,720 these various tables so for instance if 27121 17:07:11,160 --> 17:07:16,596 we focus on shows for just a moment a 27122 17:07:13,720 --> 17:07:18,160 show again has a unique ID a title a 27123 17:07:16,596 --> 17:07:20,240 year in which it debuted and a total 27124 17:07:18,160 --> 17:07:25,080 number of episodes if you want to figure 27125 17:07:20,240 --> 17:07:26,840 out what genre or genr a show belongs to 27126 17:07:25,080 --> 17:07:28,720 cuz some shows are just comedies some 27127 17:07:26,840 --> 17:07:30,840 shows are just just dramas but you know 27128 17:07:28,720 --> 17:07:32,636 some shows are arguably comedies and 27129 17:07:30,840 --> 17:07:34,276 dramas depending on the episode or the 27130 17:07:32,636 --> 17:07:36,596 like so you can imagine wanting to 27131 17:07:34,276 --> 17:07:39,720 associate two or three or even more 27132 17:07:36,596 --> 17:07:42,476 genres with a show this line here in 27133 17:07:39,720 --> 17:07:45,560 this second table allows us to do that 27134 17:07:42,476 --> 17:07:49,636 every Row in the genres table we'll see 27135 17:07:45,560 --> 17:07:52,436 has uh two items a show ID which relates 27136 17:07:49,636 --> 17:07:55,040 to the ID of a show and that's why these 27137 17:07:52,436 --> 17:07:58,000 lines literally line up with that 27138 17:07:55,040 --> 17:07:59,360 specific column name and genre which is 27139 17:07:58,000 --> 17:08:01,756 going to be like quote unquote comedy 27140 17:07:59,360 --> 17:08:05,400 quote unquote drama or something else 27141 17:08:01,756 --> 17:08:08,160 now with that said design question why 27142 17:08:05,400 --> 17:08:10,476 have we deliberately not just gotten rid 27143 17:08:08,160 --> 17:08:14,560 of this genre's table and made our lives 27144 17:08:10,476 --> 17:08:16,956 simpler by just adding a genre column to 27145 17:08:14,560 --> 17:08:19,596 this shows table and again a table is 27146 17:08:16,956 --> 17:08:21,360 just like a sheet with rows and columns 27147 17:08:19,596 --> 17:08:24,040 at the moment shows only have four 27148 17:08:21,360 --> 17:08:27,596 columns ID title year episodes why not 27149 17:08:24,040 --> 17:08:30,476 just add a fifth column called genre and 27150 17:08:27,596 --> 17:08:33,680 put the show genre 27151 17:08:30,476 --> 17:08:37,116 there any intuition 27152 17:08:33,680 --> 17:08:39,756 here why not just keep things simple 27153 17:08:37,116 --> 17:08:39,756 like yeah and 27154 17:08:43,680 --> 17:08:50,080 back exactly if you add a fifth column 27155 17:08:47,560 --> 17:08:52,680 here and call it genre then you have to 27156 17:08:50,080 --> 17:08:56,040 pick a genre specifically you have to 27157 17:08:52,680 --> 17:08:59,116 put in that cell presumably comedy or 27158 17:08:56,040 --> 17:09:00,720 drama or music or something else now you 27159 17:08:59,116 --> 17:09:01,956 could write multiple words in the cell 27160 17:09:00,720 --> 17:09:04,320 but generally speaking that would be 27161 17:09:01,956 --> 17:09:05,560 sloppy bad design like every cell just 27162 17:09:04,320 --> 17:09:07,320 like in a spreadsheet should really have 27163 17:09:05,560 --> 17:09:09,160 one value it might have multiple words 27164 17:09:07,320 --> 17:09:10,956 but it shouldn't be like a weirdly comma 27165 17:09:09,160 --> 17:09:12,116 separated list of multiple things it 27166 17:09:10,956 --> 17:09:14,276 should just be in a different cell in 27167 17:09:12,116 --> 17:09:15,880 that case so if you instead were to 27168 17:09:14,276 --> 17:09:18,160 design this with just a single column 27169 17:09:15,880 --> 17:09:20,956 called genre you're imposing what a 27170 17:09:18,160 --> 17:09:24,200 computer scientist would call a on toone 27171 17:09:20,956 --> 17:09:26,116 relationship every show has one genre 27172 17:09:24,200 --> 17:09:28,160 and that's not necessarily a good thing 27173 17:09:26,116 --> 17:09:29,720 or strictly speaking it would be a many 27174 17:09:28,160 --> 17:09:31,756 to one because the same genre could 27175 17:09:29,720 --> 17:09:34,116 belong to multiple shows but each show 27176 17:09:31,756 --> 17:09:35,956 could only have one genre in that case 27177 17:09:34,116 --> 17:09:37,476 what a relational database allows you to 27178 17:09:35,956 --> 17:09:39,560 do and relational is indeed the 27179 17:09:37,476 --> 17:09:42,080 operative word it allows you to factor 27180 17:09:39,560 --> 17:09:45,476 out some of your information and then 27181 17:09:42,080 --> 17:09:47,596 have maybe one show here in one row but 27182 17:09:45,476 --> 17:09:50,040 then in this genres table you could have 27183 17:09:47,596 --> 17:09:52,680 one row for that one show genre or you 27184 17:09:50,040 --> 17:09:55,400 could have two rows in the genres table 27185 17:09:52,680 --> 17:09:56,956 for comedy and for drama or if it has a 27186 17:09:55,400 --> 17:10:00,680 third genre you could just add another 27187 17:09:56,956 --> 17:10:02,840 row here so you still have one row for 27188 17:10:00,680 --> 17:10:05,596 the show itself with all the juiciest 27189 17:10:02,840 --> 17:10:07,636 details but a variable number of rows by 27190 17:10:05,596 --> 17:10:10,720 having this relationship with another 27191 17:10:07,636 --> 17:10:14,040 table meanwhile ratings work the same 27192 17:10:10,720 --> 17:10:15,680 way at least in this case a show has ID 27193 17:10:14,040 --> 17:10:17,360 title year and episodes but if you want 27194 17:10:15,680 --> 17:10:18,916 to figure out its rating you have to 27195 17:10:17,360 --> 17:10:21,240 kind of Follow the arrow here so to 27196 17:10:18,916 --> 17:10:23,596 speak and look up the corresponding show 27197 17:10:21,240 --> 17:10:26,200 ID in this table find the rating of that 27198 17:10:23,596 --> 17:10:27,756 show and the total number of ratings so 27199 17:10:26,200 --> 17:10:30,116 that's been factored out Two For Better 27200 17:10:27,756 --> 17:10:33,276 or For Worse um now let's consider 27201 17:10:30,116 --> 17:10:35,560 people people have just three columns ID 27202 17:10:33,276 --> 17:10:37,796 name and birth but there's no mention of 27203 17:10:35,560 --> 17:10:39,956 the TV show in which people have starred 27204 17:10:37,796 --> 17:10:41,916 or the TV shows that a person has 27205 17:10:39,956 --> 17:10:44,400 written well why is that well if you 27206 17:10:41,916 --> 17:10:46,756 just had a fourth column here called 27207 17:10:44,400 --> 17:10:48,476 show well you would have to decide what 27208 17:10:46,756 --> 17:10:50,080 show is that person in and no one could 27209 17:10:48,476 --> 17:10:51,880 ever act again in another show because 27210 17:10:50,080 --> 17:10:54,200 there's no room to store the data but if 27211 17:10:51,880 --> 17:10:57,200 someone of course a popular actor can St 27212 17:10:54,200 --> 17:10:59,636 star in multiple shows well we could 27213 17:10:57,200 --> 17:11:01,276 have one ID for that person one name one 27214 17:10:59,636 --> 17:11:03,560 birth year obviously like there's only 27215 17:11:01,276 --> 17:11:06,276 one Steve Carell as an actor in the 27216 17:11:03,560 --> 17:11:09,000 world of people but Steve Carell in this 27217 17:11:06,276 --> 17:11:10,680 example could have his person ID 27218 17:11:09,000 --> 17:11:12,956 whatever his Harvard ID equivalent Yale 27219 17:11:10,680 --> 17:11:14,756 ID equivalent is appear in multiple rows 27220 17:11:12,956 --> 17:11:17,240 in this table so that it can be 27221 17:11:14,756 --> 17:11:19,560 associated with multiple shows and this 27222 17:11:17,240 --> 17:11:22,080 allows you to create what's called a one 27223 17:11:19,560 --> 17:11:24,080 to many relationship or technically it's 27224 17:11:22,080 --> 17:11:26,680 bidirectional it's a many to many 27225 17:11:24,080 --> 17:11:28,476 relationship why well one show can 27226 17:11:26,680 --> 17:11:30,320 certain certainly have multiple people 27227 17:11:28,476 --> 17:11:32,796 in it and multiple people writing for it 27228 17:11:30,320 --> 17:11:35,560 just in the real world but conversely 27229 17:11:32,796 --> 17:11:37,636 one uh person could certainly act in 27230 17:11:35,560 --> 17:11:39,916 multiple shows or write multiple shows 27231 17:11:37,636 --> 17:11:42,436 so this is what you get with relational 27232 17:11:39,916 --> 17:11:44,560 databases you put your sort of canonical 27233 17:11:42,436 --> 17:11:46,880 data for people in one place for for 27234 17:11:44,560 --> 17:11:50,040 shows in another place and then you use 27235 17:11:46,880 --> 17:11:52,200 these additional tables to relate one 27236 17:11:50,040 --> 17:11:54,200 thing to another so we won't dwell on 27237 17:11:52,200 --> 17:11:55,756 the pictures that's just if you sort of 27238 17:11:54,200 --> 17:11:57,560 uh can wrap your mind around the data 27239 17:11:55,756 --> 17:11:59,436 set better that way that's one way of 27240 17:11:57,560 --> 17:12:01,796 thinking about it but recall that the 27241 17:11:59,436 --> 17:12:03,200 code we just saw for the schema again 27242 17:12:01,796 --> 17:12:05,040 escalated quickly like there's a lot of 27243 17:12:03,200 --> 17:12:06,720 keywords I haven't mentioned yet but 27244 17:12:05,040 --> 17:12:09,000 some of these are perhaps familiar 27245 17:12:06,720 --> 17:12:11,276 they're capitalized differently here but 27246 17:12:09,000 --> 17:12:13,400 integer is on the list here null is on 27247 17:12:11,276 --> 17:12:14,796 the list albeit technically not null so 27248 17:12:13,400 --> 17:12:16,276 let's tease apart some of these key 27249 17:12:14,796 --> 17:12:17,956 words and consider what they're actually 27250 17:12:16,276 --> 17:12:20,560 doing for your database because now 27251 17:12:17,956 --> 17:12:23,596 we're in exploring features that do not 27252 17:12:20,560 --> 17:12:25,916 exist in the world of spreadsheets alone 27253 17:12:23,596 --> 17:12:27,756 so it turns out in a SQL database 27254 17:12:25,916 --> 17:12:30,000 specifically SQL light which is the 27255 17:12:27,756 --> 17:12:32,080 version of SQL we use in cs50 and which 27256 17:12:30,000 --> 17:12:33,680 is commonly used for things like mobile 27257 17:12:32,080 --> 17:12:35,636 applications nowadays it's like a 27258 17:12:33,680 --> 17:12:37,520 lightweight version of SQL it's when you 27259 17:12:35,636 --> 17:12:39,000 aren't trying to run Twitter and have 27260 17:12:37,520 --> 17:12:40,476 billions and billions of rows 27261 17:12:39,000 --> 17:12:42,276 necessarily you've got hundreds 27262 17:12:40,476 --> 17:12:45,636 thousands tens of thousands maybe even a 27263 17:12:42,276 --> 17:12:47,720 few million but not crazy uh numbers uh 27264 17:12:45,636 --> 17:12:50,080 crazy amounts of data in the world of 27265 17:12:47,720 --> 17:12:52,796 SQL light specifically there's these 27266 17:12:50,080 --> 17:12:55,756 five data types so just like in C we had 27267 17:12:52,796 --> 17:12:57,956 int and Char and the like in SQL we have 27268 17:12:55,756 --> 17:12:59,840 these uh BL which is kind of funny but 27269 17:12:57,956 --> 17:13:01,680 it just means binary large object so 27270 17:12:59,840 --> 17:13:04,040 it's like a binary data type zeros and 27271 17:13:01,680 --> 17:13:06,040 ones that aren't necessarily uh fitting 27272 17:13:04,040 --> 17:13:07,840 into the other categories integer which 27273 17:13:06,040 --> 17:13:10,240 of course is an integer as we know it 27274 17:13:07,840 --> 17:13:12,476 numeric which is kind of a catchall for 27275 17:13:10,240 --> 17:13:15,240 numbers that are formatted specially so 27276 17:13:12,476 --> 17:13:18,880 like a date uh would be like year year 27277 17:13:15,240 --> 17:13:20,436 year year-month month Dash day day um 27278 17:13:18,880 --> 17:13:21,916 and this is actually a wonderful thing 27279 17:13:20,436 --> 17:13:23,400 depending on the country you're from you 27280 17:13:21,916 --> 17:13:25,436 might think your date system in your 27281 17:13:23,400 --> 17:13:28,520 country is great or it's horrible the US 27282 17:13:25,436 --> 17:13:31,116 system is horrible because we have month 27283 17:13:28,520 --> 17:13:33,636 day and then year which is impossible to 27284 17:13:31,116 --> 17:13:35,636 sort it is the wrong way objectively to 27285 17:13:33,636 --> 17:13:37,000 store data and yet here we are using 27286 17:13:35,636 --> 17:13:39,320 this at scale other countries have 27287 17:13:37,000 --> 17:13:41,436 gotten this better numeric and SQL 27288 17:13:39,320 --> 17:13:42,596 itself standardizes that stuff so it 27289 17:13:41,436 --> 17:13:44,116 doesn't matter what country you're from 27290 17:13:42,596 --> 17:13:46,320 you're storing your data in this 27291 17:13:44,116 --> 17:13:48,080 particular way for instance times are 27292 17:13:46,320 --> 17:13:50,560 standardized and other types of numeric 27293 17:13:48,080 --> 17:13:52,276 data as well real is synonymous with 27294 17:13:50,560 --> 17:13:54,956 float so something with a decimal point 27295 17:13:52,276 --> 17:13:57,476 and some number of uh digits thereafter 27296 17:13:54,956 --> 17:13:59,476 and then text is just uh for strings and 27297 17:13:57,476 --> 17:14:02,160 the like with other even fancier 27298 17:13:59,476 --> 17:14:04,240 databases like MySQL postgress SQL 27299 17:14:02,160 --> 17:14:06,200 Oracle and other products you might have 27300 17:14:04,240 --> 17:14:07,680 heard of there's even more data types 27301 17:14:06,200 --> 17:14:09,636 where you have to make even finer grain 27302 17:14:07,680 --> 17:14:11,520 decisions but for SQL light it's indeed 27303 17:14:09,636 --> 17:14:13,276 pretty lightweight and you or we just 27304 17:14:11,520 --> 17:14:15,240 have to decide the data types for each 27305 17:14:13,276 --> 17:14:16,840 column in a table but there's these 27306 17:14:15,240 --> 17:14:20,436 additional constraints in the world of 27307 17:14:16,840 --> 17:14:23,840 SQL you can additionally say that cells 27308 17:14:20,436 --> 17:14:25,756 in this column may or may not be null so 27309 17:14:23,840 --> 17:14:27,200 if you want to protect yourself from 27310 17:14:25,756 --> 17:14:29,476 yourself so you don't screw up and 27311 17:14:27,200 --> 17:14:31,840 insert a null that is a blank value you 27312 17:14:29,476 --> 17:14:34,116 can explicitly design a table to have a 27313 17:14:31,840 --> 17:14:35,636 column that cannot be null and so in 27314 17:14:34,116 --> 17:14:38,080 fact someone came up during the break to 27315 17:14:35,636 --> 17:14:40,880 ask me about my having manually inserted 27316 17:14:38,080 --> 17:14:42,560 SQL quote unquote SQL into our favorites 27317 17:14:40,880 --> 17:14:45,360 database you might recall that I kind of 27318 17:14:42,560 --> 17:14:47,400 cheated I just inserted uh SQL quote 27319 17:14:45,360 --> 17:14:49,240 unquote and 50v the name of a new 27320 17:14:47,400 --> 17:14:50,596 problem quote unquote but what did I not 27321 17:14:49,240 --> 17:14:53,320 insert into the 27322 17:14:50,596 --> 17:14:54,680 database a Tim stamp and I could have I 27323 17:14:53,320 --> 17:14:56,956 could have put like the current day in 27324 17:14:54,680 --> 17:14:58,720 time a few minutes ago but I didn't and 27325 17:14:56,956 --> 17:15:00,400 that's fine if it's uh if it's 27326 17:14:58,720 --> 17:15:02,636 acceptable to you and the product you're 27327 17:15:00,400 --> 17:15:05,160 building but I could have prevented that 27328 17:15:02,636 --> 17:15:07,520 if we had defined the table to have a 27329 17:15:05,160 --> 17:15:10,160 timestamp column that isn't just text 27330 17:15:07,520 --> 17:15:11,756 but it's text that's not null SQL would 27331 17:15:10,160 --> 17:15:13,360 have complained and would not have let 27332 17:15:11,756 --> 17:15:15,000 me complete that insertion so there's 27333 17:15:13,360 --> 17:15:16,276 these kinds of built-in defenses that 27334 17:15:15,000 --> 17:15:18,680 you don't necessarily get with a 27335 17:15:16,276 --> 17:15:20,560 spreadsheet alone and unique means 27336 17:15:18,680 --> 17:15:23,560 exactly that if you want to make sure 27337 17:15:20,560 --> 17:15:25,880 that every Row in that column is unique 27338 17:15:23,560 --> 17:15:27,916 maybe for email addresses or in the US 27339 17:15:25,880 --> 17:15:30,000 Social Security numbers or anything that 27340 17:15:27,916 --> 17:15:31,916 you want to make sure you don't have two 27341 17:15:30,000 --> 17:15:33,476 versions of you can specify that the 27342 17:15:31,916 --> 17:15:35,320 column is unique and there's other such 27343 17:15:33,476 --> 17:15:37,560 constraints as well but again this is 27344 17:15:35,320 --> 17:15:39,720 just a list of features that you get 27345 17:15:37,560 --> 17:15:41,756 from a proper relational database but 27346 17:15:39,720 --> 17:15:43,240 perhaps the most intellectually 27347 17:15:41,756 --> 17:15:46,436 interesting one and the most powerful 27348 17:15:43,240 --> 17:15:48,756 one is what's called here a primary key 27349 17:15:46,436 --> 17:15:52,080 and a foreign key and let me go back now 27350 17:15:48,756 --> 17:15:55,320 to this output if we look at shows 27351 17:15:52,080 --> 17:15:57,680 you'll see that a show again has an ID a 27352 17:15:55,320 --> 17:15:59,596 title a year and a number of episodes 27353 17:15:57,680 --> 17:16:01,320 and now the data types might make sense 27354 17:15:59,596 --> 17:16:03,400 the ID it turns out just like a Harvard 27355 17:16:01,320 --> 17:16:05,436 ID a Yale ID is going to be an integer 27356 17:16:03,400 --> 17:16:07,080 so a simple number the title of course 27357 17:16:05,436 --> 17:16:09,680 is going to be text but not null like it 27358 17:16:07,080 --> 17:16:11,320 would be weird if a TV show had no name 27359 17:16:09,680 --> 17:16:13,000 like that can't be like every the whole 27360 17:16:11,320 --> 17:16:14,636 world would break or your TV gu and 27361 17:16:13,000 --> 17:16:17,400 whatnot so that makes sense there to say 27362 17:16:14,636 --> 17:16:19,840 not null uh year is numeric so it's a 27363 17:16:17,400 --> 17:16:21,040 standardized form episodes is an integer 27364 17:16:19,840 --> 17:16:23,636 like how many episodes have been 27365 17:16:21,040 --> 17:16:26,000 produced and then lastly notice this the 27366 17:16:23,636 --> 17:16:28,680 primary key of the show's table is 27367 17:16:26,000 --> 17:16:30,840 apparently the column called ID 27368 17:16:28,680 --> 17:16:34,880 mentioned a few lines earlier this just 27369 17:16:30,840 --> 17:16:37,360 means that the database will use the ID 27370 17:16:34,880 --> 17:16:39,240 column as the unique identifier so it's 27371 17:16:37,360 --> 17:16:41,116 similar to the unique keyword but 27372 17:16:39,240 --> 17:16:42,756 primary key just means the database is 27373 17:16:41,116 --> 17:16:44,320 going to treat it as special too and 27374 17:16:42,756 --> 17:16:46,436 make sure that it is uniquely 27375 17:16:44,320 --> 17:16:48,840 identifying your data but what's 27376 17:16:46,436 --> 17:16:51,520 interesting is this notice if I scroll 27377 17:16:48,840 --> 17:16:53,200 back up to people people were sort of 27378 17:16:51,520 --> 17:16:56,360 similarly structured but with different 27379 17:16:53,200 --> 17:16:59,956 attributes like up here we had a person 27380 17:16:56,360 --> 17:17:02,916 has an ID a name a birth year and a 27381 17:16:59,956 --> 17:17:05,596 primary key of ID so a ID is again 27382 17:17:02,916 --> 17:17:06,720 integer name is text but not null 27383 17:17:05,596 --> 17:17:09,240 because it'd be weird to have a human 27384 17:17:06,720 --> 17:17:11,000 with absolutely no name textually birth 27385 17:17:09,240 --> 17:17:14,756 is going to be numeric but the primary 27386 17:17:11,000 --> 17:17:16,520 key of people is ID as well so those are 27387 17:17:14,756 --> 17:17:18,840 the unique columns that the database 27388 17:17:16,520 --> 17:17:21,436 will just treat special why well we just 27389 17:17:18,840 --> 17:17:24,000 looked at shows we just looked at people 27390 17:17:21,436 --> 17:17:27,000 let's focus now on this one down here 27391 17:17:24,000 --> 17:17:29,116 Stars how do you determine who star in a 27392 17:17:27,000 --> 17:17:31,560 TV show well we had two columns the show 27393 17:17:29,116 --> 17:17:32,880 ID and the person ID this is the ex the 27394 17:17:31,560 --> 17:17:34,476 Incarnation of a many to- many 27395 17:17:32,880 --> 17:17:36,240 relationship one person could be in many 27396 17:17:34,476 --> 17:17:38,520 shows one show could certainly have many 27397 17:17:36,240 --> 17:17:41,680 people in it or writing for it but 27398 17:17:38,520 --> 17:17:43,880 notice this within this table of two 27399 17:17:41,680 --> 17:17:46,116 columns show ID and person ID there's 27400 17:17:43,880 --> 17:17:50,000 what's going to be called a foreign key 27401 17:17:46,116 --> 17:17:52,436 called show ID that references the shows 27402 17:17:50,000 --> 17:17:54,436 tables ID column and then another 27403 17:17:52,436 --> 17:17:55,796 foreign key called person ID though I 27404 17:17:54,436 --> 17:17:58,080 could call these things in parentheses 27405 17:17:55,796 --> 17:18:00,636 anything I want that references the 27406 17:17:58,080 --> 17:18:02,160 people tables ID column now you're not 27407 17:18:00,636 --> 17:18:03,956 going to often have to type commands 27408 17:18:02,160 --> 17:18:05,840 like this again you set the database up 27409 17:18:03,956 --> 17:18:07,636 once in the beginning typically maybe 27410 17:18:05,840 --> 17:18:09,160 with some help from a TF maybe with help 27411 17:18:07,636 --> 17:18:11,160 of Google or the like but once your 27412 17:18:09,160 --> 17:18:13,116 database is designed it's back to the 27413 17:18:11,160 --> 17:18:14,596 crud like create read update delete the 27414 17:18:13,116 --> 17:18:17,276 selects the inserts the deletions and 27415 17:18:14,596 --> 17:18:19,436 the like but what's this implying these 27416 17:18:17,276 --> 17:18:23,040 keywords like primary key and foreign 27417 17:18:19,436 --> 17:18:26,000 key are what are doing in code what this 27418 17:18:23,040 --> 17:18:28,476 picture was painting a moment ago these 27419 17:18:26,000 --> 17:18:30,080 lines here are drawn literally to line 27420 17:18:28,476 --> 17:18:33,200 up with the corresponding things 27421 17:18:30,080 --> 17:18:37,200 people's ID lines up with person ID 27422 17:18:33,200 --> 17:18:39,116 Show's ID lines up with show ID and so 27423 17:18:37,200 --> 17:18:41,596 you're just seeing graphical version 27424 17:18:39,116 --> 17:18:45,436 code version graphical code that creates 27425 17:18:41,596 --> 17:18:47,436 these relationships now given that let's 27426 17:18:45,436 --> 17:18:49,880 actually see what these things look like 27427 17:18:47,436 --> 17:18:51,680 so let me go back to vs code here let me 27428 17:18:49,880 --> 17:18:54,520 clear my screen I'm still within SQL 27429 17:18:51,680 --> 17:18:56,040 light with shows. DB let me go ahead and 27430 17:18:54,520 --> 17:18:57,400 do what I do with like any new database 27431 17:18:56,040 --> 17:18:59,240 if I ever download something or I'm 27432 17:18:57,400 --> 17:19:00,560 trying to wrap my mind around a problem 27433 17:18:59,240 --> 17:19:02,756 you know usually it doesn't come with a 27434 17:19:00,560 --> 17:19:04,240 pretty picture or uh you know a 27435 17:19:02,756 --> 17:19:06,520 three-hour lecture to explain what the 27436 17:19:04,240 --> 17:19:08,916 data set is rather you just have the 27437 17:19:06,520 --> 17:19:11,320 data set in your own knowledge of SQL so 27438 17:19:08,916 --> 17:19:13,040 let me play around so schema shows me 27439 17:19:11,320 --> 17:19:14,956 all the tables that might be a starting 27440 17:19:13,040 --> 17:19:17,880 point okay this is interesting I know PE 27441 17:19:14,956 --> 17:19:20,840 what people are let's go ahead and show 27442 17:19:17,880 --> 17:19:22,240 me all the people so select star from 27443 17:19:20,840 --> 17:19:23,916 people I'm just trying to wrap my mind 27444 17:19:22,240 --> 17:19:27,476 around what this data set looks like in 27445 17:19:23,916 --> 17:19:29,596 a more userfriendly way okay okay that's 27446 17:19:27,476 --> 17:19:31,160 already a lot of people as you see the 27447 17:19:29,596 --> 17:19:32,756 years flying by there's been a lot of 27448 17:19:31,160 --> 17:19:35,116 people in the TV 27449 17:19:32,756 --> 17:19:36,956 business so this was maybe not the best 27450 17:19:35,116 --> 17:19:40,436 query to run but this is indicative of 27451 17:19:36,956 --> 17:19:43,160 just how large this data set is from 27452 17:19:40,436 --> 17:19:44,476 IMDb okay when in doubt and when 27453 17:19:43,160 --> 17:19:46,796 whenever you lose control over your 27454 17:19:44,476 --> 17:19:48,880 computer control C is your friend to 27455 17:19:46,796 --> 17:19:50,160 interrupt what would have been better 27456 17:19:48,880 --> 17:19:52,756 because I don't think I need to know all 27457 17:19:50,160 --> 17:19:54,880 the million people in the world I could 27458 17:19:52,756 --> 17:19:57,160 do like limit me to 10 people all right 27459 17:19:54,880 --> 17:19:59,840 and that's enough now to get us sense of 27460 17:19:57,160 --> 17:20:02,956 like Fred a stair has an IDE of one 27461 17:19:59,840 --> 17:20:05,680 first person ever um birth year of 1899 27462 17:20:02,956 --> 17:20:07,916 Lauren ball and all of these other uh 27463 17:20:05,680 --> 17:20:09,596 people from yesterday year you see that 27464 17:20:07,916 --> 17:20:11,040 they are the first 10 people in the 27465 17:20:09,596 --> 17:20:12,520 database so there's an example of some 27466 17:20:11,040 --> 17:20:14,360 of the data now if I want to rep my mind 27467 17:20:12,520 --> 17:20:16,240 around what a show is you know I know it 27468 17:20:14,360 --> 17:20:17,956 technically I know it from the picture 27469 17:20:16,240 --> 17:20:19,476 but let's just look at some raw data so 27470 17:20:17,956 --> 17:20:21,240 instead of saying select star from 27471 17:20:19,476 --> 17:20:25,956 people let me go ahead and select star 27472 17:20:21,240 --> 17:20:28,596 from shows limit 10 and okay I've only 27473 17:20:25,956 --> 17:20:30,596 heard heard of or seen a couple of these 27474 17:20:28,596 --> 17:20:32,796 but these are older shows at that but I 27475 17:20:30,596 --> 17:20:34,520 see that every show has an ID a title a 27476 17:20:32,796 --> 17:20:37,000 year in which it debuted and a number of 27477 17:20:34,520 --> 17:20:40,436 episodes but perhaps most opaque is 27478 17:20:37,000 --> 17:20:42,756 going to be this select star from 27479 17:20:40,436 --> 17:20:44,956 Stars where this is the table that 27480 17:20:42,756 --> 17:20:49,840 Associates people with shows am I going 27481 17:20:44,956 --> 17:20:51,360 to see any names or show titles here not 27482 17:20:49,840 --> 17:20:52,840 according to the definition we saw 27483 17:20:51,360 --> 17:20:54,680 earlier oh I should have done my limit 27484 17:20:52,840 --> 17:20:56,840 let me interrupt that let me do that 27485 17:20:54,680 --> 17:20:59,240 again limit 10 27486 17:20:56,840 --> 17:21:00,840 no and this is where now you're 27487 17:20:59,240 --> 17:21:02,080 definitely in the programmer world 27488 17:21:00,840 --> 17:21:03,840 because like this would be the most 27489 17:21:02,080 --> 17:21:06,000 annoying spreadsheet to use on your Mac 27490 17:21:03,840 --> 17:21:07,240 or PC ever if you just had like a sheet 27491 17:21:06,000 --> 17:21:08,596 with all of these numbers that 27492 17:21:07,240 --> 17:21:10,276 Associates one thing with the other like 27493 17:21:08,596 --> 17:21:11,840 my God how do you figure out who this is 27494 17:21:10,276 --> 17:21:14,360 or what this is you have to like 27495 17:21:11,840 --> 17:21:16,720 manually control F or command F looking 27496 17:21:14,360 --> 17:21:19,080 for the data but a database doesn't care 27497 17:21:16,720 --> 17:21:20,880 once you know SQL you can sort of Stitch 27498 17:21:19,080 --> 17:21:23,880 these things back together so what 27499 17:21:20,880 --> 17:21:26,200 you're seeing here are foreign Keys 27500 17:21:23,880 --> 17:21:28,680 foreign Keys why because show ID 27501 17:21:26,200 --> 17:21:31,000 corresponds to the same numbers from 27502 17:21:28,680 --> 17:21:33,880 that other table called shows that has 27503 17:21:31,000 --> 17:21:35,756 an proper primary key called ID person 27504 17:21:33,880 --> 17:21:37,560 ID is a foreign key in this context 27505 17:21:35,756 --> 17:21:41,116 because it refers to numbers that belong 27506 17:21:37,560 --> 17:21:42,720 to really the people table and its ID 27507 17:21:41,116 --> 17:21:44,240 column so this is just a way of somehow 27508 17:21:42,720 --> 17:21:45,756 linking them and so if you think of I 27509 17:21:44,240 --> 17:21:47,436 always like think of um this in my 27510 17:21:45,756 --> 17:21:50,560 mind's eye is this if this is like the 27511 17:21:47,436 --> 17:21:53,000 people table this is the shows table and 27512 17:21:50,560 --> 17:21:55,160 there's this middle table in between the 27513 17:21:53,000 --> 17:21:57,560 Stars table there's some way of like 27514 17:21:55,160 --> 17:21:59,680 stitching those two together by lining 27515 17:21:57,560 --> 17:22:01,680 up the idas of one with the other and 27516 17:21:59,680 --> 17:22:04,320 getting back some more data so let's 27517 17:22:01,680 --> 17:22:06,436 actually play with some of this data how 27518 17:22:04,320 --> 17:22:08,720 about we start where we emphasized 27519 17:22:06,436 --> 17:22:10,116 earlier genres so let me go ahead and 27520 17:22:08,720 --> 17:22:12,400 take a quick look at all of the genres 27521 17:22:10,116 --> 17:22:14,276 in this database so select star from 27522 17:22:12,400 --> 17:22:16,360 genres star is usually going to be a 27523 17:22:14,276 --> 17:22:18,040 little overwhelming but it just gives me 27524 17:22:16,360 --> 17:22:21,080 a sense of what the data is but let's 27525 17:22:18,040 --> 17:22:23,400 actually look at um uh let's go look at 27526 17:22:21,080 --> 17:22:26,360 all of them there okay that's a lot 27527 17:22:23,400 --> 17:22:28,560 these are all official genres from IMDb 27528 17:22:26,360 --> 17:22:31,436 let me oh okay it went okay it wasn't 27529 17:22:28,560 --> 17:22:36,080 terribly long let me filter that down so 27530 17:22:31,436 --> 17:22:37,796 from genres where genre equals comedy uh 27531 17:22:36,080 --> 17:22:40,956 Capital C just based on the data I'm 27532 17:22:37,796 --> 17:22:43,000 seeing okay so what am I seeing now and 27533 17:22:40,956 --> 17:22:44,560 in fact let me limit this arbitrarily to 27534 17:22:43,000 --> 17:22:48,636 10 though I could limit it to anything I 27535 17:22:44,560 --> 17:22:50,360 want here are 10 comedies what are they 27536 17:22:48,636 --> 17:22:52,756 well who the heck knows like all I know 27537 17:22:50,360 --> 17:22:54,360 are the 10 show IDs now I could do 27538 17:22:52,756 --> 17:22:56,756 something like this as we've seen before 27539 17:22:54,360 --> 17:22:58,956 with SQL I could do all right well let's 27540 17:22:56,756 --> 17:23:03,680 figure out what this show idea is Select 27541 17:22:58,956 --> 17:23:08,436 star from shows where the IDE of the 27542 17:23:03,680 --> 17:23:10,476 show I'm looking for equals what 62 614 27543 17:23:08,436 --> 17:23:12,276 semicolon so I could like manually look 27544 17:23:10,476 --> 17:23:14,840 it up by cross referencing the other 27545 17:23:12,276 --> 17:23:16,436 table okay so that was the show in 27546 17:23:14,840 --> 17:23:18,756 question there the first comedy in the 27547 17:23:16,436 --> 17:23:23,276 data set let me look up the second one 27548 17:23:18,756 --> 17:23:25,276 so instead of that let's do 6 3881 enter 27549 17:23:23,276 --> 17:23:27,796 okay so that's that show and let's do 27550 17:23:25,276 --> 17:23:29,880 one more and suffice it to say this is 27551 17:23:27,796 --> 17:23:32,560 just getting tedious and vulnerable to 27552 17:23:29,880 --> 17:23:34,476 mistakes quickly this is not this surely 27553 17:23:32,560 --> 17:23:36,040 can't be the way to do this and indeed 27554 17:23:34,476 --> 17:23:39,200 SQL is going to let us do this a little 27555 17:23:36,040 --> 17:23:40,840 more powerfully instead let's do this 27556 17:23:39,200 --> 17:23:42,636 instead of getting this table 27557 17:23:40,840 --> 17:23:44,916 temporarily with all these show IDs and 27558 17:23:42,636 --> 17:23:49,080 all these genres let's refine the query 27559 17:23:44,916 --> 17:23:52,276 so let's just select the show ID from 27560 17:23:49,080 --> 17:23:56,276 the genres table where the genre equals 27561 17:23:52,276 --> 17:23:59,080 quote unquote comedy now I have a big b 27562 17:23:56,276 --> 17:24:00,840 list of show IDs all of which are comedy 27563 17:23:59,080 --> 17:24:02,520 how many well I can combine ideas from 27564 17:24:00,840 --> 17:24:05,476 earlier I can just count all of those 27565 17:24:02,520 --> 17:24:06,880 show IDs and or Star if I want to just 27566 17:24:05,476 --> 17:24:08,360 do that too but I can count all those 27567 17:24:06,880 --> 17:24:11,680 show IDs 27568 17:24:08,360 --> 17:24:14,276 4876 comedies and IMDB's database for TV 27569 17:24:11,680 --> 17:24:16,360 shows so feels like a lot but how can I 27570 17:24:14,276 --> 17:24:18,916 now use that information and get back 27571 17:24:16,360 --> 17:24:21,636 the titles of comedies in the database 27572 17:24:18,916 --> 17:24:23,636 without doing it manually well let's do 27573 17:24:21,636 --> 17:24:27,240 this I have a moment ago this query 27574 17:24:23,636 --> 17:24:30,240 select the show ID from genres where the 27575 17:24:27,240 --> 17:24:33,000 current genre is quote unquote 27576 17:24:30,240 --> 17:24:34,596 comedy um what if I kind of Nest these 27577 17:24:33,000 --> 17:24:36,840 queries kind of like grade school math 27578 17:24:34,596 --> 17:24:39,000 in parenthesis what if I combine this 27579 17:24:36,840 --> 17:24:40,840 whole thing in parenthesis and now let 27580 17:24:39,000 --> 17:24:45,400 me select what I really want let me go 27581 17:24:40,840 --> 17:24:50,756 ahead and select how about uh the title 27582 17:24:45,400 --> 17:24:53,956 of all shows where the idea of the show 27583 17:24:50,756 --> 17:24:59,756 is in this list of show 27584 17:24:53,956 --> 17:25:01,796 IDs so if you agree that the shows table 27585 17:24:59,756 --> 17:25:03,636 has an ID column which is its otherwise 27586 17:25:01,796 --> 17:25:05,080 known as its primary key the unique ID 27587 17:25:03,636 --> 17:25:07,916 that identifies it just like our Harvard 27588 17:25:05,080 --> 17:25:10,476 IDs our Yale IDs and you agree that per 27589 17:25:07,916 --> 17:25:13,680 a moment ago this shorter query will 27590 17:25:10,476 --> 17:25:16,636 give me back just the show IDs of all of 27591 17:25:13,680 --> 17:25:19,360 the comedies in the database you can 27592 17:25:16,636 --> 17:25:21,476 actually combine or Nest these queries 27593 17:25:19,360 --> 17:25:22,636 together uh it's going to respect SQL 27594 17:25:21,476 --> 17:25:24,200 light order of operations with 27595 17:25:22,636 --> 17:25:25,796 parentheses just like grade school math 27596 17:25:24,200 --> 17:25:27,360 so the thing in parentheses will be 27597 17:25:25,796 --> 17:25:30,360 executed first that gives it back a list 27598 17:25:27,360 --> 17:25:32,880 of IDs like what 48,000 IDs and then 27599 17:25:30,360 --> 17:25:34,520 this query the outer query is going to 27600 17:25:32,880 --> 17:25:37,116 get the title from all of the shows 27601 17:25:34,520 --> 17:25:40,596 where the ID of the show is in that big 27602 17:25:37,116 --> 17:25:42,320 list of 48,000 so if I now execute these 27603 17:25:40,596 --> 17:25:43,916 together I think the list is still going 27604 17:25:42,320 --> 17:25:47,560 to be a little long but let me execute 27605 17:25:43,916 --> 17:25:49,680 it together now I see this long list of 27606 17:25:47,560 --> 17:25:53,360 outputs a little overwhelming let's go 27607 17:25:49,680 --> 17:25:56,116 ahead and maybe limit it to just 10 as 27608 17:25:53,360 --> 17:25:58,796 before for discussion sake and now I see 27609 17:25:56,116 --> 17:26:00,360 10 comedies ordered arbitrarily from 27610 17:25:58,796 --> 17:26:03,320 however they're in the database that 27611 17:26:00,360 --> 17:26:05,160 happen to indeed be have comedy as their 27612 17:26:03,320 --> 17:26:07,916 genre if I want to do this a little more 27613 17:26:05,160 --> 17:26:10,916 cleanly I could do this let's see uh why 27614 17:26:07,916 --> 17:26:12,636 don't I order by title ascending order 27615 17:26:10,916 --> 17:26:16,756 which is alphabetically or the default 27616 17:26:12,636 --> 17:26:19,916 is also uh ascending limit 10 now I see 27617 17:26:16,756 --> 17:26:21,596 the top 10 I mean weirdly named things 27618 17:26:19,916 --> 17:26:23,040 with hash symbols presumably to get 27619 17:26:21,596 --> 17:26:25,360 their titles up to the beginning or 27620 17:26:23,040 --> 17:26:27,436 maybe these are hashtags uh here now we 27621 17:26:25,360 --> 17:26:29,160 have alphabetically the top first 10 27622 17:26:27,436 --> 17:26:33,320 shows that are 27623 17:26:29,160 --> 17:26:35,476 comedies any questions on these kinds of 27624 17:26:33,320 --> 17:26:36,956 queries it's kind of a lot but at the 27625 17:26:35,476 --> 17:26:39,680 same time it's just like composing the 27626 17:26:36,956 --> 17:26:41,436 smaller ideas from before into slightly 27627 17:26:39,680 --> 17:26:44,436 more useful 27628 17:26:41,436 --> 17:26:44,436 queries 27629 17:26:48,276 --> 17:26:52,880 yeah do for Keys have to set the 27630 17:26:50,596 --> 17:26:54,276 relationship when you create the table 27631 17:26:52,880 --> 17:26:56,040 the programmer or the database 27632 17:26:54,276 --> 17:26:57,720 administrator would create that 27633 17:26:56,040 --> 17:26:59,916 relationship by using those keywords 27634 17:26:57,720 --> 17:27:01,560 primary key and foreign key that create 27635 17:26:59,916 --> 17:27:04,040 that teaches the database what is 27636 17:27:01,560 --> 17:27:07,436 related to what per the picture so you 27637 17:27:04,040 --> 17:27:09,000 do that once and now I being the sort of 27638 17:27:07,436 --> 17:27:12,276 programmer who's familiar with the 27639 17:27:09,000 --> 17:27:14,596 database I am just using these foreign 27640 17:27:12,276 --> 17:27:16,680 keys in a way in a manner consistent 27641 17:27:14,596 --> 17:27:18,436 with their design but and this is where 27642 17:27:16,680 --> 17:27:20,400 it's useful at some point even if you no 27643 17:27:18,436 --> 17:27:21,680 one hands you a picture to make sure you 27644 17:27:20,400 --> 17:27:23,796 understand the database because that's 27645 17:27:21,680 --> 17:27:25,680 going to inform literally what you type 27646 17:27:23,796 --> 17:27:27,360 in SQL to get the data you care about 27647 17:27:25,680 --> 17:27:29,756 about well let's do something a little 27648 17:27:27,360 --> 17:27:31,276 more precise how about very reasonable 27649 17:27:29,756 --> 17:27:33,680 question and honestly this is exactly 27650 17:27:31,276 --> 17:27:35,000 what imdb.com in the app are for what if 27651 17:27:33,680 --> 17:27:37,360 you want to find all of the shows that 27652 17:27:35,000 --> 17:27:38,916 Steve Carell is in like kind of a 27653 17:27:37,360 --> 17:27:40,636 reasonable query like literally 27654 17:27:38,916 --> 17:27:43,080 something someone might type into Google 27655 17:27:40,636 --> 17:27:44,916 or more specifically IMDb it's not 27656 17:27:43,080 --> 17:27:46,756 really obvious at first glance how to do 27657 17:27:44,916 --> 17:27:48,636 that though because right like from my 27658 17:27:46,756 --> 17:27:50,160 database if these are my six tables well 27659 17:27:48,636 --> 17:27:52,160 I can pretty easily get Steve Carell 27660 17:27:50,160 --> 17:27:54,000 from here but I can really only get his 27661 17:27:52,160 --> 17:27:55,720 ID number whatever that is his name 27662 17:27:54,000 --> 17:27:57,320 which I know already and his birth year 27663 17:27:55,720 --> 17:27:59,400 okay interesting but has nothing to do 27664 17:27:57,320 --> 17:28:02,000 with the shows that he's in I can look 27665 17:27:59,400 --> 17:28:03,436 at shows over here but there's no 27666 17:28:02,000 --> 17:28:05,796 mention of Steve Carell right because 27667 17:28:03,436 --> 17:28:07,400 there's no person ID here where is that 27668 17:28:05,796 --> 17:28:09,956 relationship implemented well it's 27669 17:28:07,400 --> 17:28:11,916 implemented down here so how do we do 27670 17:28:09,956 --> 17:28:13,320 this well here's the perfect example of 27671 17:28:11,916 --> 17:28:15,360 a a lesson we've trying been trying to 27672 17:28:13,320 --> 17:28:17,436 emphasize for weeks of taking these baby 27673 17:28:15,360 --> 17:28:19,400 steps like break larger problems down 27674 17:28:17,436 --> 17:28:21,240 into smaller ones and let's do something 27675 17:28:19,400 --> 17:28:22,680 like this let's just get everything I 27676 17:28:21,240 --> 17:28:25,240 know about Steve Carell from the 27677 17:28:22,680 --> 17:28:27,476 database let's select star from people 27678 17:28:25,240 --> 17:28:30,040 where the name of the person is quote 27679 17:28:27,476 --> 17:28:31,956 unquote Steve Carell I just want to see 27680 17:28:30,040 --> 17:28:34,160 what data we've got and here's what we 27681 17:28:31,956 --> 17:28:37,360 have okay there's only one Steve Carell 27682 17:28:34,160 --> 17:28:40,080 born in 1962 and his unique ID is 136 27683 17:28:37,360 --> 17:28:43,436 797 according to IMDb this isn't some 27684 17:28:40,080 --> 17:28:45,276 like Global uh actor identifier per se 27685 17:28:43,436 --> 17:28:47,680 all right well how do I get now all of 27686 17:28:45,276 --> 17:28:49,276 the shows that Steve Carell is in well I 27687 17:28:47,680 --> 17:28:53,240 could do this 27688 17:28:49,276 --> 17:28:54,796 select star from Stars not to confuse 27689 17:28:53,240 --> 17:29:00,956 the two one's the symbol one's the table 27690 17:28:54,796 --> 17:29:04,040 name uh where person ID equals 13 27691 17:29:00,956 --> 17:29:05,560 6797 so I think this will now give me 27692 17:29:04,040 --> 17:29:07,756 everything from the Stars table that 27693 17:29:05,560 --> 17:29:09,320 relates to Steve Carell okay and you'll 27694 17:29:07,756 --> 17:29:10,476 see person ID is the same because I'm 27695 17:29:09,320 --> 17:29:13,040 literally searching for just Steve 27696 17:29:10,476 --> 17:29:15,400 Carell but there are what like 20 or so 27697 17:29:13,040 --> 17:29:16,796 shows that he's been in all right well 27698 17:29:15,400 --> 17:29:18,320 here's where things would get tedious 27699 17:29:16,796 --> 17:29:22,916 what are those shows well I could do 27700 17:29:18,320 --> 17:29:25,080 Select Title from shows where the ID of 27701 17:29:22,916 --> 17:29:26,720 the show equals and here's you know 27702 17:29:25,080 --> 17:29:29,080 whenever you copy paste you're probably 27703 17:29:26,720 --> 17:29:31,160 doing something wrong okay he's was in 27704 17:29:29,080 --> 17:29:32,796 The Dana Carvey Show familiar with that 27705 17:29:31,160 --> 17:29:37,080 let's do another one we'll copy paste 27706 17:29:32,796 --> 17:29:38,520 this uh where ID equals this over the 27707 17:29:37,080 --> 17:29:40,000 top another and if we keep digging we'll 27708 17:29:38,520 --> 17:29:41,636 probably find the office but my God like 27709 17:29:40,000 --> 17:29:43,720 that's going to take forever to do 20 27710 17:29:41,636 --> 17:29:45,320 queries manually it's not very Dynamic 27711 17:29:43,720 --> 17:29:47,040 but what if we just Nest these queries a 27712 17:29:45,320 --> 17:29:48,916 little more dynamically so let me start 27713 17:29:47,040 --> 17:29:51,400 from the beginning again what if we go 27714 17:29:48,916 --> 17:29:55,956 ahead and select everything we want we 27715 17:29:51,400 --> 17:29:57,200 know about uh people whose name 27716 17:29:55,956 --> 17:30:00,080 equals 27717 17:29:57,200 --> 17:30:01,956 Steve Carell that gave us earlier this 27718 17:30:00,080 --> 17:30:03,160 data I don't need all of that data I 27719 17:30:01,956 --> 17:30:05,276 know his name I don't care about his 27720 17:30:03,160 --> 17:30:07,796 birth year so let's change this to just 27721 17:30:05,276 --> 17:30:09,796 be give me the ID of Steve Carell and 27722 17:30:07,796 --> 17:30:11,796 that gives me back now this smaller 27723 17:30:09,796 --> 17:30:12,680 temporary data set all right can I now 27724 17:30:11,796 --> 17:30:15,200 use 27725 17:30:12,680 --> 17:30:16,680 this uh inside of another query well let 27726 17:30:15,200 --> 17:30:19,916 me wrap the whole thing with parentheses 27727 17:30:16,680 --> 17:30:24,636 and now let me say select star from the 27728 17:30:19,916 --> 17:30:26,436 Stars table where the person ID equals 27729 17:30:24,636 --> 17:30:27,796 this so I'm Del liely not using in 27730 17:30:26,436 --> 17:30:29,436 because I'm assuming there's indeed only 27731 17:30:27,796 --> 17:30:30,956 one Steve Carell in the world so I'm not 27732 17:30:29,436 --> 17:30:32,720 getting back a list of Steve Carell I'm 27733 17:30:30,956 --> 17:30:34,756 getting back the one and only in this 27734 17:30:32,720 --> 17:30:37,320 case so equal is fine in is when you 27735 17:30:34,756 --> 17:30:39,040 have multiple equal is when you have one 27736 17:30:37,320 --> 17:30:40,320 let me go ahead and hit enter now okay 27737 17:30:39,040 --> 17:30:42,080 that's more data than I need I don't 27738 17:30:40,320 --> 17:30:44,400 need like 20 copies of Steve Carell's 27739 17:30:42,080 --> 17:30:47,840 person ID so let me hit up let me go 27740 17:30:44,400 --> 17:30:50,400 back and let me just get show ID from 27741 17:30:47,840 --> 17:30:53,756 Steve Carell and now I have a list of 27742 17:30:50,400 --> 17:30:56,000 just the 20 or so show IDs that he has 27743 17:30:53,756 --> 17:30:57,956 been in all right how can I now use this 27744 17:30:56,000 --> 17:31:00,200 well let me hit up let me put the whole 27745 17:30:57,956 --> 17:31:02,720 thing in parentheses and now let me 27746 17:31:00,200 --> 17:31:06,680 select what I really want Select Title 27747 17:31:02,720 --> 17:31:10,956 from shows where and here's the final 27748 17:31:06,680 --> 17:31:14,240 flourish the shows table has an ID has a 27749 17:31:10,956 --> 17:31:17,160 title has a year and has an episode and 27750 17:31:14,240 --> 17:31:18,796 what I really want though is to check 27751 17:31:17,160 --> 17:31:24,360 which 27752 17:31:18,796 --> 17:31:26,956 shows have ID that is what anyone want 27753 17:31:24,360 --> 17:31:29,880 to finish the thought 27754 17:31:26,956 --> 17:31:29,880 I just want to 27755 17:31:30,116 --> 17:31:34,796 yeah exactly ID in this and this is 27756 17:31:33,320 --> 17:31:36,400 getting ugly and when you actually write 27757 17:31:34,796 --> 17:31:38,240 your queries in like a text file you can 27758 17:31:36,400 --> 17:31:39,636 format them nicely and indent them my 27759 17:31:38,240 --> 17:31:41,200 font is just getting I don't want to 27760 17:31:39,636 --> 17:31:43,240 make it too small to fit everything but 27761 17:31:41,200 --> 17:31:44,956 now we have three queries one is in 27762 17:31:43,240 --> 17:31:46,560 doubly nested parenthesis then there's 27763 17:31:44,956 --> 17:31:48,320 the middle one then there's the outer 27764 17:31:46,560 --> 17:31:50,400 one so this last query is going to get 27765 17:31:48,320 --> 17:31:53,160 me the title from shows where the ID of 27766 17:31:50,400 --> 17:31:54,916 the show is in this big list of 20 or so 27767 17:31:53,160 --> 17:31:57,080 show IDs that Steve Carell is in and I 27768 17:31:54,916 --> 17:31:58,596 knew that because I looked up his name 27769 17:31:57,080 --> 17:32:00,276 here and notice what I did not do this 27770 17:31:58,596 --> 17:32:02,320 time is I didn't manually hardcode his 27771 17:32:00,276 --> 17:32:04,596 ID number there's no need that would be 27772 17:32:02,320 --> 17:32:06,040 kind of a bad way to implement a website 27773 17:32:04,596 --> 17:32:08,956 uh if you're using a database underneath 27774 17:32:06,040 --> 17:32:10,796 the hood you want the IMDb for real to 27775 17:32:08,956 --> 17:32:12,040 search for whatever the human typed in 27776 17:32:10,796 --> 17:32:14,916 and no one's going to know Steve 27777 17:32:12,040 --> 17:32:17,320 Carell's person ID or anything else so 27778 17:32:14,916 --> 17:32:20,360 here we've done this all dynamically and 27779 17:32:17,320 --> 17:32:22,276 now if I hit enter I think I get all of 27780 17:32:20,360 --> 17:32:24,680 his shows let's go ahead and order this 27781 17:32:22,276 --> 17:32:26,680 by title just to make it tidy and you 27782 17:32:24,680 --> 17:32:28,116 probably will see at least one or more 27783 17:32:26,680 --> 17:32:32,040 shows that you know and probably the 27784 17:32:28,116 --> 17:32:34,200 most popular is dot dot dot the office 27785 17:32:32,040 --> 17:32:36,160 so this is literally the kind of query 27786 17:32:34,200 --> 17:32:38,520 that's being executed underneath the 27787 17:32:36,160 --> 17:32:41,680 hood when you go to uh websites or apps 27788 17:32:38,520 --> 17:32:44,436 like IMDb your textual query is probably 27789 17:32:41,680 --> 17:32:46,840 being plugged into a longer SQL query 27790 17:32:44,436 --> 17:32:48,720 like this where some programmer at IMDb 27791 17:32:46,840 --> 17:32:51,040 probably wrote this whole query in 27792 17:32:48,720 --> 17:32:53,476 advance weeks months years ago and 27793 17:32:51,040 --> 17:32:55,476 they're just somehow plugging in the 27794 17:32:53,476 --> 17:32:59,756 value that you the human type into the 27795 17:32:55,476 --> 17:33:04,596 search box or the like questions 27796 17:32:59,756 --> 17:33:08,840 now on finding this data or any 27797 17:33:04,596 --> 17:33:11,436 other no okay so where else could we go 27798 17:33:08,840 --> 17:33:15,116 with this well let's 27799 17:33:11,436 --> 17:33:17,520 consider how else we might combine data 27800 17:33:15,116 --> 17:33:19,476 suppose that the next question actually 27801 17:33:17,520 --> 17:33:21,596 perhaps appropriately would be focusing 27802 17:33:19,476 --> 17:33:23,636 in on not just like people and shows and 27803 17:33:21,596 --> 17:33:25,720 these stars but how do we kind of like 27804 17:33:23,636 --> 17:33:27,160 gather more information about the shows 27805 17:33:25,720 --> 17:33:28,916 themselves like the genres the ratings 27806 17:33:27,160 --> 17:33:31,040 or the like so indeed let's focus on 27807 17:33:28,916 --> 17:33:33,160 just these two tables here recall that 27808 17:33:31,040 --> 17:33:35,476 every show has an ID a title a year and 27809 17:33:33,160 --> 17:33:37,276 episodes but it also might have one or 27810 17:33:35,476 --> 17:33:39,080 more relationships with rose and this 27811 17:33:37,276 --> 17:33:41,240 other table is called genres and this is 27812 17:33:39,080 --> 17:33:43,520 so that a show can be a comedy can be a 27813 17:33:41,240 --> 17:33:45,796 drama can be any number of other things 27814 17:33:43,520 --> 17:33:48,400 one row per so you would see the same 27815 17:33:45,796 --> 17:33:51,400 show ID again and again and again with a 27816 17:33:48,400 --> 17:33:53,916 different genre written in English like 27817 17:33:51,400 --> 17:33:56,400 comma comedy drama or the like well how 27818 17:33:53,916 --> 17:33:58,080 do I kind of Recon subtitute that data 27819 17:33:56,400 --> 17:34:00,436 Well turns out there's a few different 27820 17:33:58,080 --> 17:34:03,520 ways to do this and let me propose that 27821 17:34:00,436 --> 17:34:05,956 we introduce this keyword here join and 27822 17:34:03,520 --> 17:34:08,360 this is really the most powerful of the 27823 17:34:05,956 --> 17:34:10,636 keywords in SQL itself it doesn't have 27824 17:34:08,360 --> 17:34:12,436 to be used we've seen with uh nested 27825 17:34:10,636 --> 17:34:14,596 queries that you can still select data 27826 17:34:12,436 --> 17:34:17,476 across multiple tables but here is 27827 17:34:14,596 --> 17:34:21,276 another way so let me do this let me go 27828 17:34:17,476 --> 17:34:25,200 back to my SQL light database and let me 27829 17:34:21,276 --> 17:34:29,436 select sort of in one uh breath exactly 27830 17:34:25,200 --> 17:34:31,200 the data I want select star from shows 27831 17:34:29,436 --> 17:34:33,436 and let's just limit this initially to 27832 17:34:31,200 --> 17:34:35,796 10 to see what it looks like all right 27833 17:34:33,436 --> 17:34:38,520 that's again the shows data select star 27834 17:34:35,796 --> 17:34:40,756 from genres let's limit that to 102 just 27835 17:34:38,520 --> 17:34:44,000 to wrap our minds around it and now this 27836 17:34:40,756 --> 17:34:46,796 is not that useful however the data in 27837 17:34:44,000 --> 17:34:48,240 the leftmost column here is the primary 27838 17:34:46,796 --> 17:34:51,636 key in the shows table these are just 27839 17:34:48,240 --> 17:34:53,840 unique IDs the data here in the genres 27840 17:34:51,636 --> 17:34:55,680 table recall show ID is the foreign key 27841 17:34:53,840 --> 17:34:57,360 so it's the same numbers but just copied 27842 17:34:55,680 --> 17:34:59,680 into another table so that we can have 27843 17:34:57,360 --> 17:35:02,040 this relationship across them how do I 27844 17:34:59,680 --> 17:35:04,276 kind of line up these numbers with these 27845 17:35:02,040 --> 17:35:08,000 numbers to get back like a wider table 27846 17:35:04,276 --> 17:35:10,240 that has title and year and episodes and 27847 17:35:08,000 --> 17:35:12,596 genre and heck ratings and all of that 27848 17:35:10,240 --> 17:35:14,276 too if we want well you can join these 27849 17:35:12,596 --> 17:35:17,200 Tables by just telling the database what 27850 17:35:14,276 --> 17:35:22,000 to join on what so let me do this select 27851 17:35:17,200 --> 17:35:24,636 star from shows join that table though 27852 17:35:22,000 --> 17:35:27,116 on the genres table well how do you want 27853 17:35:24,636 --> 17:35:28,796 to join those two tables and again the 27854 17:35:27,116 --> 17:35:30,400 two tables from the picture looked like 27855 17:35:28,796 --> 17:35:32,200 this how do you tell SQL 27856 17:35:30,400 --> 17:35:33,840 programmatically to sort of you know put 27857 17:35:32,200 --> 17:35:35,520 one of them right next to the other line 27858 17:35:33,840 --> 17:35:38,000 up all of the ID so that you just get 27859 17:35:35,520 --> 17:35:40,840 one larger data set well we can use 27860 17:35:38,000 --> 17:35:43,880 indeed this this query this uh syntax 27861 17:35:40,840 --> 17:35:45,840 called join so back to VSS code here and 27862 17:35:43,880 --> 17:35:51,000 let me join these two 27863 17:35:45,840 --> 17:35:54,436 tables sorry typo here join genres on 27864 17:35:51,000 --> 17:35:58,720 the shows tables ID column AKA it's 27865 17:35:54,436 --> 17:36:01,916 primary key equaling the genres tables 27866 17:35:58,720 --> 17:36:03,476 show ID column aka the foreign key so in 27867 17:36:01,916 --> 17:36:05,756 other words it looks a little cryptic 27868 17:36:03,476 --> 17:36:07,720 but I'm just telling SQL how to line up 27869 17:36:05,756 --> 17:36:10,000 these two tables and what column to 27870 17:36:07,720 --> 17:36:12,240 match with the other so that the numbers 27871 17:36:10,000 --> 17:36:15,880 line up and I get essentially a wider 27872 17:36:12,240 --> 17:36:18,116 table let me go ahead and hit uh 27873 17:36:15,880 --> 17:36:19,956 semicolon and enter and this is now 27874 17:36:18,116 --> 17:36:21,880 going to give me a lot of data we might 27875 17:36:19,956 --> 17:36:24,400 have to interrupt it but notice even at 27876 17:36:21,880 --> 17:36:26,436 a glance we're getting the ID the title 27877 17:36:24,400 --> 17:36:28,320 the year the number of episodes the ID 27878 17:36:26,436 --> 17:36:30,436 again redundantly but that's to be 27879 17:36:28,320 --> 17:36:31,956 expected if I'm joining them and the 27880 17:36:30,436 --> 17:36:34,596 genre all the way on the right let me 27881 17:36:31,956 --> 17:36:37,560 hit contrl C to interrupt let me just 27882 17:36:34,596 --> 17:36:39,080 limit this to the office so where title 27883 17:36:37,560 --> 17:36:42,276 equals quote unquote the office so we 27884 17:36:39,080 --> 17:36:45,000 can focus on just one sample uh datum 27885 17:36:42,276 --> 17:36:47,360 and here fun fact there's been more than 27886 17:36:45,000 --> 17:36:49,680 one office the one that you all probably 27887 17:36:47,360 --> 17:36:53,276 like is this one that started in 2005 27888 17:36:49,680 --> 17:36:56,436 with 188 episodes its ID in the shows 27889 17:36:53,276 --> 17:36:58,636 table is 3866 76 that's confirmed over 27890 17:36:56,436 --> 17:37:01,276 here too so again we've just joined the 27891 17:36:58,636 --> 17:37:03,080 two tables how by lining up those fields 27892 17:37:01,276 --> 17:37:04,796 but now that we can see that almost all 27893 17:37:03,080 --> 17:37:07,360 of the offices produced over the decades 27894 17:37:04,796 --> 17:37:09,400 are comedies except for this one there 27895 17:37:07,360 --> 17:37:11,720 was a version of the office produced in 27896 17:37:09,400 --> 17:37:13,880 2001 that was considered more of uh a 27897 17:37:11,720 --> 17:37:16,320 drama no unsure if it's related to the 27898 17:37:13,880 --> 17:37:18,400 other how can we Link in other data well 27899 17:37:16,320 --> 17:37:20,436 let's go ahead and Link in ratings too 27900 17:37:18,400 --> 17:37:23,276 or instead so instead of joining this 27901 17:37:20,436 --> 17:37:28,240 with genres let me go ahead and Rewind 27902 17:37:23,276 --> 17:37:31,320 here and join shows on ratings on shows. 27903 17:37:28,240 --> 17:37:33,040 ID equals ratings. show ID and let's 27904 17:37:31,320 --> 17:37:35,796 limit it to the office 2 for discussion 27905 17:37:33,040 --> 17:37:39,796 sake where uh title equals quote unquote 27906 17:37:35,796 --> 17:37:42,956 the office semicolon and now you can see 27907 17:37:39,796 --> 17:37:44,840 that among the various offices it looks 27908 17:37:42,956 --> 17:37:47,116 like the one that most of us probably 27909 17:37:44,840 --> 17:37:51,160 know and love is the highest rated also 27910 17:37:47,116 --> 17:37:53,000 with a 9.0 with like 585,000 people 27911 17:37:51,160 --> 17:37:54,240 having cast votes for whereas these 27912 17:37:53,000 --> 17:37:55,796 other shows seem to have been less 27913 17:37:54,240 --> 17:37:59,000 popular perhaps that's why indeed you 27914 17:37:55,796 --> 17:38:00,080 see fewer episodes for them as well so 27915 17:37:59,000 --> 17:38:01,756 even though we've put the data in 27916 17:38:00,080 --> 17:38:04,596 multiple places you can still kind of 27917 17:38:01,756 --> 17:38:07,596 reconstitute it by lining things up in 27918 17:38:04,596 --> 17:38:10,400 this way and rejoining the tables 27919 17:38:07,596 --> 17:38:13,160 questions now on 27920 17:38:10,400 --> 17:38:16,040 this like this is the heart of what SQL 27921 17:38:13,160 --> 17:38:18,116 does and what relational databases do 27922 17:38:16,040 --> 17:38:19,636 for 27923 17:38:18,116 --> 17:38:22,596 you 27924 17:38:19,636 --> 17:38:24,840 questions all right a few final features 27925 17:38:22,596 --> 17:38:26,080 like there's not all that much that uh 27926 17:38:24,840 --> 17:38:27,840 you know SQL takes practice like 27927 17:38:26,080 --> 17:38:29,240 anything else but in terms of syntax and 27928 17:38:27,840 --> 17:38:31,956 capabilities let's just introduce you to 27929 17:38:29,240 --> 17:38:33,720 a couple of final features here and how 27930 17:38:31,956 --> 17:38:37,160 and problems that arise and how we might 27931 17:38:33,720 --> 17:38:39,476 solve them let's do this uh as well so 27932 17:38:37,160 --> 17:38:42,520 let me go back into VSS code here and 27933 17:38:39,476 --> 17:38:44,000 let's just find out um Steve Carell's 27934 17:38:42,520 --> 17:38:46,320 information again last time we did it 27935 17:38:44,000 --> 17:38:48,360 with this nested nested query by getting 27936 17:38:46,320 --> 17:38:50,680 his ID and then the show IDs and then 27937 17:38:48,360 --> 17:38:51,956 the titles for those show IDs with join 27938 17:38:50,680 --> 17:38:53,916 you can do it a little differently and 27939 17:38:51,956 --> 17:38:55,560 any of these ways are fine one might 27940 17:38:53,916 --> 17:38:57,160 become EAS easier to you mentally than 27941 17:38:55,560 --> 17:39:01,436 another let's go ahead and select the 27942 17:38:57,160 --> 17:39:04,880 titles from what let's select the title 27943 17:39:01,436 --> 17:39:06,116 from uh the people table but and I'm 27944 17:39:04,880 --> 17:39:08,040 going to hit enter and when you're using 27945 17:39:06,116 --> 17:39:09,916 SQL light 3 interactively if you ever 27946 17:39:08,040 --> 17:39:12,200 find yourself with a prompt that says 27947 17:39:09,916 --> 17:39:13,840 dot dot dot angle bracket it means 27948 17:39:12,200 --> 17:39:15,756 you're continuing your thought onto the 27949 17:39:13,840 --> 17:39:17,840 next line if you didn't intend that you 27950 17:39:15,756 --> 17:39:19,560 can sometimes hit uh semicolon to just 27951 17:39:17,840 --> 17:39:21,320 end the thought and hit enter and just 27952 17:39:19,560 --> 17:39:23,040 even if it triggers an error but this is 27953 17:39:21,320 --> 17:39:24,276 one way of formatting my queries now a 27954 17:39:23,040 --> 17:39:25,720 little more nicely I'm just going to add 27955 17:39:24,276 --> 17:39:28,080 some wh space so that it's a little 27956 17:39:25,720 --> 17:39:30,720 easier to read what do I want to select 27957 17:39:28,080 --> 17:39:34,240 well I want to select the title of shows 27958 17:39:30,720 --> 17:39:37,796 from the people table joined with the 27959 17:39:34,240 --> 17:39:41,916 Stars table on the people tables ID 27960 17:39:37,796 --> 17:39:44,200 column equaling the Stars tables person 27961 17:39:41,916 --> 17:39:46,200 ID column so in other words if you think 27962 17:39:44,200 --> 17:39:48,476 back to what people are and what stars 27963 17:39:46,200 --> 17:39:50,636 are one has an ID one has a person ID 27964 17:39:48,476 --> 17:39:53,000 I'm just now connecting those two tables 27965 17:39:50,636 --> 17:39:55,916 I'm joining those two but I want to do 27966 17:39:53,000 --> 17:39:58,080 this as well with with another table let 27967 17:39:55,916 --> 17:39:59,476 me additionally join in so now I only 27968 17:39:58,080 --> 17:40:02,476 have two hands but now I'm putting a 27969 17:39:59,476 --> 17:40:08,400 third table joined in together here join 27970 17:40:02,476 --> 17:40:10,320 shows on stars. show ID equals shows. ID 27971 17:40:08,400 --> 17:40:12,116 so this is now linking three tables 27972 17:40:10,320 --> 17:40:14,116 together but I only care about this for 27973 17:40:12,116 --> 17:40:17,240 one person so where the name of the 27974 17:40:14,116 --> 17:40:19,880 person equals quote unquote Steve Carell 27975 17:40:17,240 --> 17:40:21,520 so more cryptic to be to be sure but 27976 17:40:19,880 --> 17:40:23,360 what we're doing with this query is just 27977 17:40:21,520 --> 17:40:25,080 taking all three tables that we care 27978 17:40:23,360 --> 17:40:27,000 about and we're joining them them all 27979 17:40:25,080 --> 17:40:29,360 together at once using this new join 27980 17:40:27,000 --> 17:40:31,680 syntax literally telling the database 27981 17:40:29,360 --> 17:40:33,200 what columns to line up with what and 27982 17:40:31,680 --> 17:40:36,360 then we filter at the very end just like 27983 17:40:33,200 --> 17:40:38,240 before to get back if I hit enter the 27984 17:40:36,360 --> 17:40:40,240 answer we want which in this case is a 27985 17:40:38,240 --> 17:40:43,276 little slower at the moment but that 27986 17:40:40,240 --> 17:40:45,160 same list of uh 20 or so shows that he's 27987 17:40:43,276 --> 17:40:47,200 been in there's one other way to do this 27988 17:40:45,160 --> 17:40:48,636 and again uh these are all in the slides 27989 17:40:47,200 --> 17:40:50,000 online so you can repeat them without 27990 17:40:48,636 --> 17:40:51,680 having to jot down everything and we'll 27991 17:40:50,000 --> 17:40:53,956 put them in the notes too but there's 27992 17:40:51,680 --> 17:40:56,320 another way to do this I could also use 27993 17:40:53,956 --> 17:40:57,720 an implicit join so that was an explicit 27994 17:40:56,320 --> 17:41:00,116 join because I literally typed the word 27995 17:40:57,720 --> 17:41:02,560 join multiple times at that but let me 27996 17:41:00,116 --> 17:41:05,560 go ahead and select the title from these 27997 17:41:02,560 --> 17:41:06,796 three tables people stars and shows and 27998 17:41:05,560 --> 17:41:08,116 this might just be nicer because if you 27999 17:41:06,796 --> 17:41:10,080 know what tables you want to select data 28000 17:41:08,116 --> 17:41:11,636 from just enumerate them separated by 28001 17:41:10,080 --> 17:41:16,360 commas which you might prefer in your 28002 17:41:11,636 --> 17:41:18,476 mind where the people ID equals the 28003 17:41:16,360 --> 17:41:23,200 Stars person 28004 17:41:18,476 --> 17:41:26,520 ID and the Stars show ID equals the 28005 17:41:23,200 --> 17:41:28,636 shows ID and the name of the person 28006 17:41:26,520 --> 17:41:30,560 equals Steve Carell so this is an 28007 17:41:28,636 --> 17:41:31,720 implicit joint and honestly I constantly 28008 17:41:30,560 --> 17:41:33,040 reference my notes for some of this 28009 17:41:31,720 --> 17:41:34,160 stuff too it's not the kind of thing 28010 17:41:33,040 --> 17:41:36,276 that's going to come like this to you 28011 17:41:34,160 --> 17:41:37,680 after just one day but it's just a 28012 17:41:36,276 --> 17:41:39,276 different way of expressing the same 28013 17:41:37,680 --> 17:41:42,160 thing I want to select data from three 28014 17:41:39,276 --> 17:41:45,160 different tables and hey SQL here is how 28015 17:41:42,160 --> 17:41:47,116 I want you to line those tables up so 28016 17:41:45,160 --> 17:41:49,756 that I can get like related data for 28017 17:41:47,116 --> 17:41:53,276 Steve Carell and this now will achieve 28018 17:41:49,756 --> 17:41:55,680 the same results ultimately let me hit 28019 17:41:53,276 --> 17:41:58,560 enter 28020 17:41:55,680 --> 17:42:00,276 H and there we go so a little slower and 28021 17:41:58,560 --> 17:42:02,436 performance might vary based on computer 28022 17:42:00,276 --> 17:42:03,880 based on uh implementation of SQL but I 28023 17:42:02,436 --> 17:42:05,840 think I still have the same answers now 28024 17:42:03,880 --> 17:42:07,680 suppose as I'm often do and I had to 28025 17:42:05,840 --> 17:42:09,916 look it up again last time suppose you 28026 17:42:07,680 --> 17:42:12,160 forget what uh how to spell Steve 28027 17:42:09,916 --> 17:42:14,080 Carell's name is it two RS two L's or 28028 17:42:12,160 --> 17:42:16,276 the like well I could also do something 28029 17:42:14,080 --> 17:42:18,596 like this select well let's just keep 28030 17:42:16,276 --> 17:42:20,436 this simple select star from people 28031 17:42:18,596 --> 17:42:22,080 where name equals I've been deliberately 28032 17:42:20,436 --> 17:42:23,720 getting it right so as to not embarrass 28033 17:42:22,080 --> 17:42:26,520 myself that's the Steve Carell I keep 28034 17:42:23,720 --> 17:42:29,436 querying if you forget well you could 28035 17:42:26,520 --> 17:42:30,840 try searching for just Steves but there 28036 17:42:29,436 --> 17:42:33,240 interestingly there's a bunch of Steves 28037 17:42:30,840 --> 17:42:35,436 we don't know when they were born uh but 28038 17:42:33,240 --> 17:42:38,040 that's probably not the Steve Carell we 28039 17:42:35,436 --> 17:42:39,840 want if we don't have his last name so I 28040 17:42:38,040 --> 17:42:42,000 could alternatively do well it's Steve 28041 17:42:39,840 --> 17:42:43,720 and then it's starts with a C I think 28042 17:42:42,000 --> 17:42:45,796 well it turns out there's another wild 28043 17:42:43,720 --> 17:42:48,596 card you can use in SQL we used the 28044 17:42:45,796 --> 17:42:51,560 asterisk to select all of the columns 28045 17:42:48,596 --> 17:42:54,560 you can in quotes use a percent sign to 28046 17:42:51,560 --> 17:42:57,000 say see something so there's some zero 28047 17:42:54,560 --> 17:42:59,756 or more characters after the letter c 28048 17:42:57,000 --> 17:43:01,880 and now this doesn't work cuz now I 28049 17:42:59,756 --> 17:43:05,360 would be literally looking for Steve 28050 17:43:01,880 --> 17:43:07,436 space c something but recall earlier I 28051 17:43:05,360 --> 17:43:09,160 mentioned that one other keyword which 28052 17:43:07,436 --> 17:43:10,756 is for fuzzier matching so to speak 28053 17:43:09,160 --> 17:43:12,276 where it's like not exactly what you're 28054 17:43:10,756 --> 17:43:14,160 looking for but it's like what you're 28055 17:43:12,276 --> 17:43:17,560 looking for if you instead say where his 28056 17:43:14,160 --> 17:43:19,596 name is like Steve space c something now 28057 17:43:17,560 --> 17:43:21,160 we'll get back a whole bunch of Steves 28058 17:43:19,596 --> 17:43:22,520 but I think now I could probably find 28059 17:43:21,160 --> 17:43:23,680 the one I'm actually looking for if I 28060 17:43:22,520 --> 17:43:25,320 don't remember his name you can use 28061 17:43:23,680 --> 17:43:26,840 multiple percent signs if you forget 28062 17:43:25,320 --> 17:43:29,400 what his first name is you could reverse 28063 17:43:26,840 --> 17:43:32,680 the order but that too is a uh very 28064 17:43:29,400 --> 17:43:37,116 powerful sequel feature at that 28065 17:43:32,680 --> 17:43:40,116 questions on these queries here 28066 17:43:37,116 --> 17:43:40,116 yeah 28067 17:43:40,240 --> 17:43:46,840 sorry what about 28068 17:43:42,476 --> 17:43:49,240 it oh yeah sure so the query I used here 28069 17:43:46,840 --> 17:43:52,720 there's a lot of Steves whose last name 28070 17:43:49,240 --> 17:43:55,880 starts with C oops too far uh the last 28071 17:43:52,720 --> 17:43:57,520 query I executed was this one here so 28072 17:43:55,880 --> 17:43:59,956 where the name is like quote unquote 28073 17:43:57,520 --> 17:44:02,680 Steve C percent sign so that's just 28074 17:43:59,956 --> 17:44:03,956 another tool for your toolkit here but 28075 17:44:02,680 --> 17:44:06,200 you'll you'll perhaps have noticed that 28076 17:44:03,956 --> 17:44:07,840 those two the prior to that query the 28077 17:44:06,200 --> 17:44:09,596 joins I did were sort of slow and 28078 17:44:07,840 --> 17:44:11,840 honestly this database isn't even that 28079 17:44:09,596 --> 17:44:13,720 big like yes it has tens of thousands of 28080 17:44:11,840 --> 17:44:15,520 rows in it but like in the real world 28081 17:44:13,720 --> 17:44:17,160 and most of the apps you and I use a lot 28082 17:44:15,520 --> 17:44:19,520 every day or websites like there's 28083 17:44:17,160 --> 17:44:21,840 Millions even billions of rows of data 28084 17:44:19,520 --> 17:44:23,756 and like if I had to wait on like my 28085 17:44:21,840 --> 17:44:25,680 computer here or my code space like a 28086 17:44:23,756 --> 17:44:27,240 second or two to get the data like 28087 17:44:25,680 --> 17:44:29,916 that's not going to work for millions of 28088 17:44:27,240 --> 17:44:32,160 users or customers certainly so how can 28089 17:44:29,916 --> 17:44:35,080 we actually improve things well it turns 28090 17:44:32,160 --> 17:44:38,200 out another upside of a proper 28091 17:44:35,080 --> 17:44:39,880 relational database is that it's not 28092 17:44:38,200 --> 17:44:41,680 just a spreadsheet where the onus is on 28093 17:44:39,880 --> 17:44:43,596 like you to like find the data you're 28094 17:44:41,680 --> 17:44:46,436 looking for you can also tell the 28095 17:44:43,596 --> 17:44:49,840 database to index the data for you an 28096 17:44:46,436 --> 17:44:51,916 index is an an efficient uh cheat sheet 28097 17:44:49,840 --> 17:44:54,240 for Finding data fast like books in the 28098 17:44:51,916 --> 17:44:55,840 real world often have indices at the end 28099 17:44:54,240 --> 17:44:57,160 of the at the end of the book where you 28100 17:44:55,840 --> 17:44:58,476 can look things up alphabetically and 28101 17:44:57,160 --> 17:45:00,596 then you can cross reference it for the 28102 17:44:58,476 --> 17:45:02,200 pages that that topic appears on same 28103 17:45:00,596 --> 17:45:03,956 idea in a database if you tell the 28104 17:45:02,200 --> 17:45:06,636 database in advance that you want to 28105 17:45:03,956 --> 17:45:08,636 search on a certain column frequently 28106 17:45:06,636 --> 17:45:10,080 you can tell it to build a fancy index 28107 17:45:08,636 --> 17:45:12,840 that will just allow you to search that 28108 17:45:10,080 --> 17:45:14,840 column Faster by default these columns 28109 17:45:12,840 --> 17:45:16,596 are going to be searched most likely via 28110 17:45:14,840 --> 17:45:17,720 linear search like not even binary 28111 17:45:16,596 --> 17:45:19,956 search because the data might not be 28112 17:45:17,720 --> 17:45:22,320 sorted because it came in in any order 28113 17:45:19,956 --> 17:45:23,360 but if you create an index you're 28114 17:45:22,320 --> 17:45:25,520 probably going to get something closer 28115 17:45:23,360 --> 17:45:27,796 to logar rmic than linear and that's 28116 17:45:25,520 --> 17:45:29,560 going to be a big plus overall so let me 28117 17:45:27,796 --> 17:45:31,720 do something simple here first let me 28118 17:45:29,560 --> 17:45:33,320 turn on a SQL light specific feature 28119 17:45:31,720 --> 17:45:35,880 that just is going to time all of my 28120 17:45:33,320 --> 17:45:39,000 queries by writing do timer on I just 28121 17:45:35,880 --> 17:45:40,636 want to keep track of uh how long each 28122 17:45:39,000 --> 17:45:42,116 of these commands takes this one is not 28123 17:45:40,636 --> 17:45:43,880 a slow command so this is just going to 28124 17:45:42,116 --> 17:45:45,840 be relative but let's just select 28125 17:45:43,880 --> 17:45:48,520 everything from the shows table where 28126 17:45:45,840 --> 17:45:50,200 the title thereof is the office let's 28127 17:45:48,520 --> 17:45:53,000 see how long this relatively simple 28128 17:45:50,200 --> 17:45:55,160 query takes all right not very long at 28129 17:45:53,000 --> 17:45:59,040 all in real terms like less than a 28130 17:45:55,160 --> 17:46:00,840 second .035 seconds so not slow by any 28131 17:45:59,040 --> 17:46:02,756 means but if you've got hundreds 28132 17:46:00,840 --> 17:46:04,400 thousands millions of users like every 28133 17:46:02,756 --> 17:46:07,436 one of those milliseconds could very 28134 17:46:04,400 --> 17:46:10,320 well add up so can we do better well we 28135 17:46:07,436 --> 17:46:12,520 can if I do this if I use syntax like 28136 17:46:10,320 --> 17:46:16,276 this once in the beginning of the design 28137 17:46:12,520 --> 17:46:19,276 of my database I create not a table but 28138 17:46:16,276 --> 17:46:21,756 an index with some name on a specific 28139 17:46:19,276 --> 17:46:23,720 table on one or more columns I can give 28140 17:46:21,756 --> 17:46:26,560 a clue a hint to the database in advance 28141 17:46:23,720 --> 17:46:29,160 saying please optimize with some Secret 28142 17:46:26,560 --> 17:46:31,160 Sauce searching or selecting on this 28143 17:46:29,160 --> 17:46:33,560 column in this table so that my searches 28144 17:46:31,160 --> 17:46:36,436 are faster so let me do this let me go 28145 17:46:33,560 --> 17:46:38,916 back to vs code here let me create an 28146 17:46:36,436 --> 17:46:40,400 index called how about title index I 28147 17:46:38,916 --> 17:46:42,880 could call it anything I want but I want 28148 17:46:40,400 --> 17:46:46,160 to search faster on titles so I'm going 28149 17:46:42,880 --> 17:46:48,360 to call this a title index where uh 28150 17:46:46,160 --> 17:46:50,720 rather uh title index on the table 28151 17:46:48,360 --> 17:46:53,240 called shows and then in parentheses is 28152 17:46:50,720 --> 17:46:55,080 the syntax the column called title so 28153 17:46:53,240 --> 17:46:57,116 again I've just borrowed this canonical 28154 17:46:55,080 --> 17:47:00,276 syntax and I've just translated into 28155 17:46:57,116 --> 17:47:02,596 something that's TV show specific all 28156 17:47:00,276 --> 17:47:04,636 right what is this going to do for me 28157 17:47:02,596 --> 17:47:06,720 once I hit enter this is going to create 28158 17:47:04,636 --> 17:47:08,360 in the computer's memory the database's 28159 17:47:06,720 --> 17:47:11,956 memory something called a b tree it's 28160 17:47:08,360 --> 17:47:13,636 not a binary tree a b tree is actually a 28161 17:47:11,956 --> 17:47:14,956 potentially more efficient data 28162 17:47:13,636 --> 17:47:16,796 structure that we didn't talk about a 28163 17:47:14,956 --> 17:47:18,200 few weeks back in week five but it looks 28164 17:47:16,796 --> 17:47:20,840 a little something like this which looks 28165 17:47:18,200 --> 17:47:25,796 similar to a binary tree but does anyone 28166 17:47:20,840 --> 17:47:25,796 notice what makes this not a binary tree 28167 17:47:29,116 --> 17:47:34,240 exactly binary tree by implying two has 28168 17:47:31,436 --> 17:47:35,956 no more than two children per node but 28169 17:47:34,240 --> 17:47:37,400 here's a perfect example one two three 28170 17:47:35,956 --> 17:47:39,240 and there could be four children five 28171 17:47:37,400 --> 17:47:41,520 children or more but the effect of that 28172 17:47:39,240 --> 17:47:43,200 if you have a very wide tree the upside 28173 17:47:41,520 --> 17:47:45,400 is that it's like very short like it 28174 17:47:43,200 --> 17:47:47,560 pulls the data higher up closer to the 28175 17:47:45,400 --> 17:47:49,320 node to the root node and recall that 28176 17:47:47,560 --> 17:47:50,880 the root node is where we began our 28177 17:47:49,320 --> 17:47:53,916 searches in the past whether it was a 28178 17:47:50,880 --> 17:47:55,436 BST a binary search tree even a try or 28179 17:47:53,916 --> 17:47:57,636 data structures we always began at the 28180 17:47:55,436 --> 17:47:59,240 top so the higher up you can pull the 28181 17:47:57,636 --> 17:48:01,000 data even if it makes the data structure 28182 17:47:59,240 --> 17:48:03,080 very wide you're going to be able to do 28183 17:48:01,000 --> 17:48:05,276 boom boom boom look up queries look up 28184 17:48:03,080 --> 17:48:07,400 data probably much faster certainly than 28185 17:48:05,276 --> 17:48:10,756 if it's just a very long list like a 28186 17:48:07,400 --> 17:48:13,040 column by default so with that said let 28187 17:48:10,756 --> 17:48:14,436 me go back to vs code I didn't create 28188 17:48:13,040 --> 17:48:16,400 the index yet let me go ahead and hit 28189 17:48:14,436 --> 17:48:18,200 enter and create it all right it took a 28190 17:48:16,400 --> 17:48:20,400 minute a moment it took like half a 28191 17:48:18,200 --> 17:48:22,080 second which obviously is not that slow 28192 17:48:20,400 --> 17:48:24,240 but with more data that could have been 28193 17:48:22,080 --> 17:48:27,796 even slower but it's a onetime operation 28194 17:48:24,240 --> 17:48:30,436 as of now and now let me hit up and let 28195 17:48:27,796 --> 17:48:33,116 me select the same data from shows where 28196 17:48:30,436 --> 17:48:36,956 title equals the office last time just a 28197 17:48:33,116 --> 17:48:39,276 moment ago it took 0.035 seconds no not 28198 17:48:36,956 --> 17:48:41,720 slow but also that's going to add up if 28199 17:48:39,276 --> 17:48:43,520 I have lots of users of IMDb let's go 28200 17:48:41,720 --> 17:48:46,040 ahead now and execute the same query 28201 17:48:43,520 --> 17:48:47,796 again how long did that 28202 17:48:46,040 --> 17:48:50,436 take 28203 17:48:47,796 --> 17:48:52,560 0.01 seconds now I mean practically 28204 17:48:50,436 --> 17:48:54,116 nothing and so that's the sort of 28205 17:48:52,560 --> 17:48:55,240 opportunity now when you've got lots of 28206 17:48:54,116 --> 17:48:56,756 data and you want to really speed up 28207 17:48:55,240 --> 17:48:59,080 these searches these indexes these 28208 17:48:56,756 --> 17:49:01,000 indices that just create for you these 28209 17:48:59,080 --> 17:49:02,796 magical data structures in the databases 28210 17:49:01,000 --> 17:49:04,720 memory it allows you to search on 28211 17:49:02,796 --> 17:49:06,720 columns that you are pretty sure you 28212 17:49:04,720 --> 17:49:08,320 want to search on more effectively Now 28213 17:49:06,720 --> 17:49:09,956 by contrast if you've ever used like 28214 17:49:08,320 --> 17:49:12,276 Google or Bing or some search engine 28215 17:49:09,956 --> 17:49:13,680 that has advanced search some of those 28216 17:49:12,276 --> 17:49:16,200 text boxes that you can search more 28217 17:49:13,680 --> 17:49:18,116 precisely in might very well be slower 28218 17:49:16,200 --> 17:49:20,520 why well probably you don't want to go 28219 17:49:18,116 --> 17:49:26,116 crazy and just index every column on 28220 17:49:20,520 --> 17:49:26,116 every table why well what might be the 28221 17:49:26,276 --> 17:49:30,880 intuition like if logically indexes 28222 17:49:28,680 --> 17:49:32,320 speed things up why not index everything 28223 17:49:30,880 --> 17:49:34,560 there's always going to be a tradeoff 28224 17:49:32,320 --> 17:49:36,956 here what might that 28225 17:49:34,560 --> 17:49:38,840 be 28226 17:49:36,956 --> 17:49:40,596 yeah yeah it's going to take a lot of 28227 17:49:38,840 --> 17:49:42,756 storage right this is just a slide on 28228 17:49:40,596 --> 17:49:44,840 the screen but like this has to go 28229 17:49:42,756 --> 17:49:46,520 somewhere like this needs space in the 28230 17:49:44,840 --> 17:49:48,436 computer's memory or on the hard drive 28231 17:49:46,520 --> 17:49:51,200 or the like and that's fine if you have 28232 17:49:48,436 --> 17:49:52,596 unlimited space but odds are like you 28233 17:49:51,200 --> 17:49:54,476 don't and that's going to get expensive 28234 17:49:52,596 --> 17:49:57,276 for different reasons so maybe you only 28235 17:49:54,476 --> 17:49:59,400 want to index certain columns and 28236 17:49:57,276 --> 17:50:00,916 certain tables and not all of them 28237 17:49:59,400 --> 17:50:02,956 because you know what even if a user 28238 17:50:00,916 --> 17:50:06,116 really wants to search maybe VI advanced 28239 17:50:02,956 --> 17:50:08,476 search on some other column or table 28240 17:50:06,116 --> 17:50:09,916 altogether fine if once in a while a 28241 17:50:08,476 --> 17:50:11,596 query is slow like we're probably 28242 17:50:09,916 --> 17:50:13,880 getting the bigger bang for our buck by 28243 17:50:11,596 --> 17:50:16,320 optimizing the common cases the more 28244 17:50:13,880 --> 17:50:17,596 popular queries that people actually 28245 17:50:16,320 --> 17:50:20,720 care about 28246 17:50:17,596 --> 17:50:24,080 too all right so let's come full circle 28247 17:50:20,720 --> 17:50:26,636 and bring this now back to uh how how we 28248 17:50:24,080 --> 17:50:28,360 actually began which was with some uh 28249 17:50:26,636 --> 17:50:30,560 python code so it turns out these are 28250 17:50:28,360 --> 17:50:32,796 not either or decisions it turns out in 28251 17:50:30,560 --> 17:50:35,880 the real world developers are constantly 28252 17:50:32,796 --> 17:50:38,240 using one two three languages at once in 28253 17:50:35,880 --> 17:50:39,840 fact next week I rattled off HTML CSS 28254 17:50:38,240 --> 17:50:41,596 and JavaScript one of which is a proper 28255 17:50:39,840 --> 17:50:44,320 programming language but those languages 28256 17:50:41,596 --> 17:50:47,956 are often used together totally normal 28257 17:50:44,320 --> 17:50:50,636 and common to use Python and SQL or Java 28258 17:50:47,956 --> 17:50:52,680 in SQL or Swift in SQL or any number of 28259 17:50:50,636 --> 17:50:54,000 different combinations with a database 28260 17:50:52,680 --> 17:50:55,796 language you might use use your 28261 17:50:54,000 --> 17:50:59,080 preferred programming language Java 28262 17:50:55,796 --> 17:51:01,796 python C++ to create the user interface 28263 17:50:59,080 --> 17:51:04,000 and the logic that builds the uh the 28264 17:51:01,796 --> 17:51:05,916 that implements the program itself but 28265 17:51:04,000 --> 17:51:07,680 for your data like SQL is a really good 28266 17:51:05,916 --> 17:51:09,116 candidates and indeed we've seen already 28267 17:51:07,680 --> 17:51:11,276 that SQL can just speed up certain 28268 17:51:09,116 --> 17:51:13,720 operations you can change you can uh 28269 17:51:11,276 --> 17:51:16,000 collapse 15 lines of code into just one 28270 17:51:13,720 --> 17:51:17,636 and you can use these things together so 28271 17:51:16,000 --> 17:51:19,880 let me come back to I'm going to quit 28272 17:51:17,636 --> 17:51:21,840 out of SQL light I'm going to minimize 28273 17:51:19,880 --> 17:51:24,916 my terminal window and here's where we 28274 17:51:21,840 --> 17:51:27,276 left off before with favorite .p with 28275 17:51:24,916 --> 17:51:30,916 favorites. everything was being stored 28276 17:51:27,276 --> 17:51:33,400 in uh favorites. CSV and recall that we 28277 17:51:30,916 --> 17:51:36,360 eventually imported that CSV file into 28278 17:51:33,400 --> 17:51:37,520 favorites. DB automatically with import 28279 17:51:36,360 --> 17:51:40,000 just so we could start playing around 28280 17:51:37,520 --> 17:51:42,040 with SQL but we can now tie these two 28281 17:51:40,000 --> 17:51:45,040 together and a way to do that is as 28282 17:51:42,040 --> 17:51:47,520 follows um cs50 has a library for python 28283 17:51:45,040 --> 17:51:50,116 you might recall having available uh get 28284 17:51:47,520 --> 17:51:51,756 string get int get float you don't 28285 17:51:50,116 --> 17:51:53,436 strictly need to use them in Python 28286 17:51:51,756 --> 17:51:55,840 because it's much easier to just use the 28287 17:51:53,436 --> 17:51:57,560 input function and then try accept and 28288 17:51:55,840 --> 17:52:00,796 convert things to int or float or the 28289 17:51:57,560 --> 17:52:02,880 like but it's a lot more work to use SQL 28290 17:52:00,796 --> 17:52:04,320 in Python without a third-party Library 28291 17:52:02,880 --> 17:52:06,240 a lot of the commercial options or 28292 17:52:04,320 --> 17:52:08,596 popular open source options are actually 28293 17:52:06,240 --> 17:52:10,756 just complicated to use so cs50 does 28294 17:52:08,596 --> 17:52:12,916 have a very useful function inside of 28295 17:52:10,756 --> 17:52:14,880 its library for python that you should 28296 17:52:12,916 --> 17:52:16,560 use and must use for the problem set 28297 17:52:14,880 --> 17:52:19,720 that just makes it easy to execute 28298 17:52:16,560 --> 17:52:22,400 python uh execute SQL inside of your 28299 17:52:19,720 --> 17:52:24,840 python code but it's built on top of a 28300 17:52:22,400 --> 17:52:26,796 very popular open source alternative so 28301 17:52:24,840 --> 17:52:28,476 you can use that too in the real world 28302 17:52:26,796 --> 17:52:30,080 so the documentation for that at this 28303 17:52:28,476 --> 17:52:32,880 URL here but I'll show you what we need 28304 17:52:30,080 --> 17:52:36,000 to know here by focusing back on 28305 17:52:32,880 --> 17:52:38,880 favorites.i so what I'm going to do here 28306 17:52:36,000 --> 17:52:42,636 as follows is this let me delete 28307 17:52:38,880 --> 17:52:47,436 everything from favorites. py except for 28308 17:52:42,636 --> 17:52:49,840 let's say uh this from cs50 import SQL 28309 17:52:47,436 --> 17:52:51,916 in all caps so that's importing a SQL 28310 17:52:49,840 --> 17:52:55,720 feature from cs50's library that's going 28311 17:52:51,916 --> 17:52:57,400 to allow me to open a DB file in code 28312 17:52:55,720 --> 17:52:59,200 how do I do that well let me create a 28313 17:52:57,400 --> 17:53:01,000 variable called DB for database though I 28314 17:52:59,200 --> 17:53:03,840 could call it anything I want let me 28315 17:53:01,000 --> 17:53:06,000 call this SQL function and pass in using 28316 17:53:03,840 --> 17:53:09,720 special syntax that's not cs50 specific 28317 17:53:06,000 --> 17:53:13,636 it's an industry thing SQL light colon 28318 17:53:09,720 --> 17:53:16,320 slash slash slash unlike every other URL 28319 17:53:13,636 --> 17:53:18,200 you type this one literally has three in 28320 17:53:16,320 --> 17:53:19,596 this context here and then the name of 28321 17:53:18,200 --> 17:53:22,116 the database which in this case is 28322 17:53:19,596 --> 17:53:24,880 favorites. DB so this is just a way of 28323 17:53:22,116 --> 17:53:26,596 telling this SQL library that we wrote 28324 17:53:24,880 --> 17:53:29,956 but that works exactly like third party 28325 17:53:26,596 --> 17:53:33,080 Alternatives openen favorites. DB using 28326 17:53:29,956 --> 17:53:34,840 the SQL light technology if you will all 28327 17:53:33,080 --> 17:53:38,680 right let's just ask the user a question 28328 17:53:34,840 --> 17:53:40,240 give me your favorite um uh problem so 28329 17:53:38,680 --> 17:53:41,720 we're going to use input instead of get 28330 17:53:40,240 --> 17:53:43,080 string but we could use get string but 28331 17:53:41,720 --> 17:53:44,916 they're pretty much the same for our 28332 17:53:43,080 --> 17:53:49,360 purposes let's ask the user for their 28333 17:53:44,916 --> 17:53:53,400 favorite and now in Python code let us 28334 17:53:49,360 --> 17:53:57,080 select from favorites. DB all of the 28335 17:53:53,400 --> 17:53:59,360 rows where students specify that problem 28336 17:53:57,080 --> 17:54:02,956 as their favorite so in SQL alone it 28337 17:53:59,360 --> 17:54:07,756 would be this select uh star from 28338 17:54:02,956 --> 17:54:10,400 favorites where problem equals and I'll 28339 17:54:07,756 --> 17:54:12,320 do um well whatever my favorite's going 28340 17:54:10,400 --> 17:54:14,160 to be like problem equals Mario for 28341 17:54:12,320 --> 17:54:15,476 instance so if I were just using SQL I 28342 17:54:14,160 --> 17:54:17,636 would literally write something like 28343 17:54:15,476 --> 17:54:19,916 that but I'm in a piy file now like I 28344 17:54:17,636 --> 17:54:22,520 have to use Python syntax but python 28345 17:54:19,916 --> 17:54:24,596 supports strings SQL is just text it's 28346 17:54:22,520 --> 17:54:27,636 just a string so I could certainly just 28347 17:54:24,596 --> 17:54:29,360 put my SQL code in a string perhaps and 28348 17:54:27,636 --> 17:54:31,520 then pass it to a python function and 28349 17:54:29,360 --> 17:54:33,560 here's the bridge between the two if you 28350 17:54:31,520 --> 17:54:35,560 just treat SQL as any old text we can 28351 17:54:33,560 --> 17:54:38,116 put it in a string and execute it so let 28352 17:54:35,560 --> 17:54:40,000 me actually do this let me go ahead and 28353 17:54:38,116 --> 17:54:41,200 create a variable called rows which is 28354 17:54:40,000 --> 17:54:45,400 eventually going to contain all the rows 28355 17:54:41,200 --> 17:54:48,956 from the database let me go ahead and uh 28356 17:54:45,400 --> 17:54:50,400 select db. execute this is the one 28357 17:54:48,956 --> 17:54:52,080 function you need to know about inside 28358 17:54:50,400 --> 17:54:54,520 of cs50's library and it literally 28359 17:54:52,080 --> 17:54:57,080 executes a SQL statement and then in 28360 17:54:54,520 --> 17:54:59,476 quotes you pass it literally what you 28361 17:54:57,080 --> 17:55:01,560 want to execute and let me go ahead and 28362 17:54:59,476 --> 17:55:03,956 close the parenthesis at the end there 28363 17:55:01,560 --> 17:55:05,840 and now let me just try this so for Row 28364 17:55:03,956 --> 17:55:10,720 in rows let's iterate over all of the 28365 17:55:05,840 --> 17:55:15,916 rows let me go ahead and print out how 28366 17:55:10,720 --> 17:55:19,436 about uh row quote unquote and what do I 28367 17:55:15,916 --> 17:55:22,680 want here uh let's print out the Tim 28368 17:55:19,436 --> 17:55:24,720 stamp of that person for kicks all right 28369 17:55:22,680 --> 17:55:26,916 let me open open my terminal window 28370 17:55:24,720 --> 17:55:29,200 python of favorites. Pi crossing my 28371 17:55:26,916 --> 17:55:31,476 fingers here for 28372 17:55:29,200 --> 17:55:34,840 sure 28373 17:55:31,476 --> 17:55:36,080 enter uh uh there we go favorit so I'll 28374 17:55:34,840 --> 17:55:39,276 type in 28375 17:55:36,080 --> 17:55:40,756 Mario okay so I got back it's not very 28376 17:55:39,276 --> 17:55:42,520 interesting but I got back all of the 28377 17:55:40,756 --> 17:55:44,040 timestamps of students who typed in 28378 17:55:42,520 --> 17:55:46,240 Mario that we imported into this 28379 17:55:44,040 --> 17:55:48,436 database well what I really care about 28380 17:55:46,240 --> 17:55:50,596 is how popular Mario is so let me change 28381 17:55:48,436 --> 17:55:53,956 this a little bit let me change this to 28382 17:55:50,596 --> 17:55:55,680 count the number of rows and let me keep 28383 17:55:53,956 --> 17:55:58,040 it simple let me give an alias like I 28384 17:55:55,680 --> 17:56:00,596 proposed earlier like as n where N is a 28385 17:55:58,040 --> 17:56:03,160 number so that now down here I can 28386 17:56:00,596 --> 17:56:05,956 actually just do this print out the 28387 17:56:03,160 --> 17:56:07,400 value of n all right let me go back to 28388 17:56:05,956 --> 17:56:10,596 my terminal window run python to 28389 17:56:07,400 --> 17:56:13,756 favorites. py let me type in Mario enter 28390 17:56:10,596 --> 17:56:16,240 okay 39 now technically I'm cheating 28391 17:56:13,756 --> 17:56:18,200 like honestly if I'm executing select 28392 17:56:16,240 --> 17:56:20,476 count we've seen before it only ever 28393 17:56:18,200 --> 17:56:22,160 returns one row not multiple so there's 28394 17:56:20,476 --> 17:56:23,560 really nothing to iterate over but it's 28395 17:56:22,160 --> 17:56:25,560 working fine it's just just iterating 28396 17:56:23,560 --> 17:56:27,240 once but I'm getting lucky so 28397 17:56:25,560 --> 17:56:29,916 technically what I should probably just 28398 17:56:27,240 --> 17:56:32,680 do is this I should probably give myself 28399 17:56:29,916 --> 17:56:36,276 like a variable called row set it equal 28400 17:56:32,680 --> 17:56:41,160 to the very first row and only row that 28401 17:56:36,276 --> 17:56:43,400 came back and now print out that rows n 28402 17:56:41,160 --> 17:56:45,636 column let me rerun the program I'll 28403 17:56:43,400 --> 17:56:46,796 type in Mario again enter and I still 28404 17:56:45,636 --> 17:56:49,916 see 28405 17:56:46,796 --> 17:56:51,476 39 so this of course I don't strictly 28406 17:56:49,916 --> 17:56:54,956 need to do this I don't really need a 28407 17:56:51,476 --> 17:56:56,636 variable I can do Row Bracket Z instead 28408 17:56:54,956 --> 17:56:59,520 but let me focus on what this library is 28409 17:56:56,636 --> 17:57:02,276 now doing so per the documentation what 28410 17:56:59,520 --> 17:57:06,756 the C what the cs-50 execute function 28411 17:57:02,276 --> 17:57:10,276 always does for you is it returns a list 28412 17:57:06,756 --> 17:57:12,476 of dictionaries so if your query returns 28413 17:57:10,276 --> 17:57:14,200 nothing like no matches you get back an 28414 17:57:12,476 --> 17:57:15,680 empty list like Open Bracket close 28415 17:57:14,200 --> 17:57:17,080 bracket nothing in it any Loop is not 28416 17:57:15,680 --> 17:57:19,276 going to execute anything useful because 28417 17:57:17,080 --> 17:57:21,560 there's nothing in it if though you get 28418 17:57:19,276 --> 17:57:23,756 back one row you're going to get back a 28419 17:57:21,560 --> 17:57:26,160 list of size one in inside of which is a 28420 17:57:23,756 --> 17:57:28,520 single dictionary that dictionary is 28421 17:57:26,160 --> 17:57:30,636 going to have keys that correspond to 28422 17:57:28,520 --> 17:57:32,796 whatever you selected be it the columns 28423 17:57:30,636 --> 17:57:34,320 or the count so when I selected star 28424 17:57:32,796 --> 17:57:35,596 before I could have like I would have 28425 17:57:34,320 --> 17:57:38,160 gotten all of the columns that's how I 28426 17:57:35,596 --> 17:57:39,796 was able to access timestamp here I'm 28427 17:57:38,160 --> 17:57:41,520 just selecting count and I don't want to 28428 17:57:39,796 --> 17:57:42,916 have to type this down here that would 28429 17:57:41,520 --> 17:57:44,436 just look kind of atrocious it would 28430 17:57:42,916 --> 17:57:47,116 work but it would look weird to just 28431 17:57:44,436 --> 17:57:49,320 keep retyping count peren star close pen 28432 17:57:47,116 --> 17:57:51,240 so I just created an alias called n just 28433 17:57:49,320 --> 17:57:54,916 to like make this my life easier or 28434 17:57:51,240 --> 17:57:57,360 cleaner down here so to be clear the SQL 28435 17:57:54,916 --> 17:58:00,040 the cs50 execute function returns a list 28436 17:57:57,360 --> 17:58:02,400 of dictionaries when you're using select 28437 17:58:00,040 --> 17:58:05,116 and that is how I can now get back the 28438 17:58:02,400 --> 17:58:09,916 first and only row and then print out 28439 17:58:05,116 --> 17:58:12,000 that Row's n value it is identical to 28440 17:58:09,916 --> 17:58:15,116 let me do this let me highlight this 28441 17:58:12,000 --> 17:58:17,160 whole line of text let me in my terminal 28442 17:58:15,116 --> 17:58:19,596 window run SQL light three of favorites. 28443 17:58:17,160 --> 17:58:23,080 DB like we did before break let me just 28444 17:58:19,596 --> 17:58:25,400 copy paste this query enter that that's 28445 17:58:23,080 --> 17:58:27,560 the table I got back earlier when we 28446 17:58:25,400 --> 17:58:29,320 played with SQL manually and so when I 28447 17:58:27,560 --> 17:58:32,200 get back this table here's the key 28448 17:58:29,320 --> 17:58:34,200 here's the value and I only have one row 28449 17:58:32,200 --> 17:58:36,040 which is why I'm just blindly indexing 28450 17:58:34,200 --> 17:58:37,040 into rows bracket zero because I know 28451 17:58:36,040 --> 17:58:38,520 there's always going to be an answer 28452 17:58:37,040 --> 17:58:40,276 there it's going to be zero or one or 28453 17:58:38,520 --> 17:58:43,116 more but I know now it's going to be 28454 17:58:40,276 --> 17:58:45,320 called n because of this here so what 28455 17:58:43,116 --> 17:58:46,796 have I just done well this is SQL down 28456 17:58:45,320 --> 17:58:48,520 here and this is just me being like a 28457 17:58:46,796 --> 17:58:50,400 data scientist asking questions about my 28458 17:58:48,520 --> 17:58:52,796 data just using like black and white SQL 28459 17:58:50,400 --> 17:58:54,360 queries this is me now being a python 28460 17:58:52,796 --> 17:58:56,756 programmer who wants to talk to a SQL 28461 17:58:54,360 --> 17:58:58,720 database using Python and The Bridge 28462 17:58:56,756 --> 17:59:00,360 we're using happens to be the cs-50 28463 17:58:58,720 --> 17:59:02,240 library but again there's third party 28464 17:59:00,360 --> 17:59:04,840 free libraries you can also use as well 28465 17:59:02,240 --> 17:59:06,560 ours is just very simple and indeed the 28466 17:59:04,840 --> 17:59:08,916 documentation will explain how execute 28467 17:59:06,560 --> 17:59:10,636 behaves a little differently for inserts 28468 17:59:08,916 --> 17:59:11,796 updates and deletes you don't get back a 28469 17:59:10,636 --> 17:59:14,680 list because you're not selecting 28470 17:59:11,796 --> 17:59:17,436 anything but you do get back some return 28471 17:59:14,680 --> 17:59:20,400 values questions on 28472 17:59:17,436 --> 17:59:24,956 this that's the last of our python code 28473 17:59:20,400 --> 17:59:27,956 that ties everything together in 28474 17:59:24,956 --> 17:59:27,956 spirit 28475 17:59:30,436 --> 17:59:37,520 yeah uh what this this one 28476 17:59:33,476 --> 17:59:40,720 here yes so db. execute by definition 28477 17:59:37,520 --> 17:59:42,320 returns a list of rows and each of those 28478 17:59:40,720 --> 17:59:45,160 rows happens to be a dictionary because 28479 17:59:42,320 --> 17:59:47,680 it's convenient key value pairs if I'm 28480 17:59:45,160 --> 17:59:49,720 selecting the count of rows I just know 28481 17:59:47,680 --> 17:59:51,520 from Having learned squel an hour ago 28482 17:59:49,720 --> 17:59:54,040 that this is always going to give me a 28483 17:59:51,520 --> 17:59:57,040 single row whose column in this case is 28484 17:59:54,040 --> 17:59:59,040 called n so if I know it's a single row 28485 17:59:57,040 --> 18:00:01,116 I can just blindly just like in C go 28486 17:59:59,040 --> 18:00:03,796 into that list or an array in C and go 28487 18:00:01,116 --> 18:00:05,680 to the first location and then treat 28488 18:00:03,796 --> 18:00:08,116 that as the single row what you don't 28489 18:00:05,680 --> 18:00:11,360 want to do is this even if you the human 28490 18:00:08,116 --> 18:00:13,080 know the query returns one row you can't 28491 18:00:11,360 --> 18:00:15,040 just magically change the variable name 28492 18:00:13,080 --> 18:00:17,560 to be singular and expect to have only 28493 18:00:15,040 --> 18:00:19,116 one value you will always have a list so 28494 18:00:17,560 --> 18:00:21,276 even if there is only one value in it 28495 18:00:19,116 --> 18:00:23,680 it's up to you to do something like this 28496 18:00:21,276 --> 18:00:25,720 to get at it or if you prefer more 28497 18:00:23,680 --> 18:00:28,116 succinctness you can do rose bracket I 28498 18:00:25,720 --> 18:00:31,840 bracket n that'll achieve the same thing 28499 18:00:28,116 --> 18:00:31,840 without a variable 28500 18:00:35,796 --> 18:00:41,276 yeah good so I have been misleading this 28501 18:00:38,680 --> 18:00:43,240 whole time and cheating because this is 28502 18:00:41,276 --> 18:00:45,320 only ever going to return Mario I'm 28503 18:00:43,240 --> 18:00:47,880 ignoring the favorite that the human 28504 18:00:45,320 --> 18:00:49,520 typed in here on line five so let me fix 28505 18:00:47,880 --> 18:00:51,400 that and that's going to lead us to some 28506 18:00:49,520 --> 18:00:53,756 of the problems that arise ultimately 28507 18:00:51,400 --> 18:00:55,400 with SQL the right way to solve that 28508 18:00:53,756 --> 18:00:57,680 problem let me get rid of my terminal 28509 18:00:55,400 --> 18:00:59,916 window here the right way to solve this 28510 18:00:57,680 --> 18:01:02,360 problem is not to use an F string like 28511 18:00:59,916 --> 18:01:04,320 we did in Python generally because SQL 28512 18:01:02,360 --> 18:01:07,840 queries as we'll see in a moment can be 28513 18:01:04,320 --> 18:01:10,116 dangerous when you want to plug in users 28514 18:01:07,840 --> 18:01:13,476 uh data into a query that you've written 28515 18:01:10,116 --> 18:01:16,276 most of in advance you should you must 28516 18:01:13,476 --> 18:01:19,040 you had better use a placeholder namely 28517 18:01:16,276 --> 18:01:20,880 a question mark in this case this is 28518 18:01:19,040 --> 18:01:22,360 somewhat specific to cs50's library but 28519 18:01:20,880 --> 18:01:24,476 we just borrowed the convention that 28520 18:01:22,360 --> 18:01:26,880 like every other Library uses too in the 28521 18:01:24,476 --> 18:01:29,080 world of SQL single question marks are 28522 18:01:26,880 --> 18:01:31,956 used as placeholders and the way you do 28523 18:01:29,080 --> 18:01:34,596 this is as follows if you want to plug 28524 18:01:31,956 --> 18:01:37,880 in a value for that question mark just 28525 18:01:34,596 --> 18:01:39,720 like in print F in C you specify as a 28526 18:01:37,880 --> 18:01:41,756 second or a third or fourth argument all 28527 18:01:39,720 --> 18:01:44,200 of the values you want plugged into this 28528 18:01:41,756 --> 18:01:47,796 so in C weeks ago we were using percent 28529 18:01:44,200 --> 18:01:50,240 s same exact idea in SQL it's a question 28530 18:01:47,796 --> 18:01:53,240 mark that you use instead this now if I 28531 18:01:50,240 --> 18:01:55,720 open back my terminal window and I run 28532 18:01:53,240 --> 18:01:58,840 uh python of favorites. type in Mario I 28533 18:01:55,720 --> 18:02:01,276 should still get 39 but now I can also 28534 18:01:58,840 --> 18:02:03,436 type in scratch perhaps and get 44 for 28535 18:02:01,276 --> 18:02:05,636 that very first piece at zero and that 28536 18:02:03,436 --> 18:02:08,240 one is even more popular here so this 28537 18:02:05,636 --> 18:02:10,880 now is correct it would work to use an F 28538 18:02:08,240 --> 18:02:13,040 string here and then plug in a value 28539 18:02:10,880 --> 18:02:14,560 like favorite here but you'll see in 28540 18:02:13,040 --> 18:02:17,000 just a moment don't do that you will 28541 18:02:14,560 --> 18:02:20,596 expose yourself to potential hack or 28542 18:02:17,000 --> 18:02:21,916 attacks um by trusting the user's input 28543 18:02:20,596 --> 18:02:23,400 and so in fact let's transition from 28544 18:02:21,916 --> 18:02:26,400 that to exact some of these kinds of 28545 18:02:23,400 --> 18:02:28,560 challenges namely two before we wrap up 28546 18:02:26,400 --> 18:02:30,276 so in the world of SQL especially when 28547 18:02:28,560 --> 18:02:32,320 it's used at scale at the Twitter and 28548 18:02:30,276 --> 18:02:34,116 the Googles of the world like a lot of 28549 18:02:32,320 --> 18:02:35,880 lots of data is probably coming into the 28550 18:02:34,116 --> 18:02:37,200 database all at once because multiple 28551 18:02:35,880 --> 18:02:38,756 people are opening their phones at the 28552 18:02:37,200 --> 18:02:40,200 same time around the world they're 28553 18:02:38,756 --> 18:02:41,840 clicking on the same links roughly at 28554 18:02:40,200 --> 18:02:43,720 the same time around the world when you 28555 18:02:41,840 --> 18:02:45,680 have thousands of people all using your 28556 18:02:43,720 --> 18:02:47,240 site at once like order of operations is 28557 18:02:45,680 --> 18:02:49,360 going to be important but unfortunately 28558 18:02:47,240 --> 18:02:51,040 in SQL and in other contexts of 28559 18:02:49,360 --> 18:02:52,796 computing there's this risk of what's 28560 18:02:51,040 --> 18:02:54,880 known as a race condition so for 28561 18:02:52,796 --> 18:02:57,916 instance has anyone ever seen or liked 28562 18:02:54,880 --> 18:02:59,916 this this is like yes the world record 28563 18:02:57,916 --> 18:03:01,320 egg or it's like this thing that was 28564 18:02:59,916 --> 18:03:03,160 very popular while back it's still kind 28565 18:03:01,320 --> 18:03:05,636 of going strong but if you go to the 28566 18:03:03,160 --> 18:03:07,596 Instagram profile for world record egg 28567 18:03:05,636 --> 18:03:09,436 uh the goal was to make the most most 28568 18:03:07,596 --> 18:03:12,000 liked Instagram post ever and they did 28569 18:03:09,436 --> 18:03:14,000 pretty well it's just this it's just a 28570 18:03:12,000 --> 18:03:15,276 picture of an egg now at the height of 28571 18:03:14,000 --> 18:03:17,116 the popularity like there might have 28572 18:03:15,276 --> 18:03:18,720 been hundreds thousands tens of 28573 18:03:17,116 --> 18:03:21,476 thousands of people clicking pretty much 28574 18:03:18,720 --> 18:03:23,200 at the same time on this egg so it 28575 18:03:21,476 --> 18:03:26,000 actually creates a potential problem 28576 18:03:23,200 --> 18:03:27,476 with the Integrity of Instagram's data 28577 18:03:26,000 --> 18:03:28,840 why well if you're have all these 28578 18:03:27,476 --> 18:03:30,636 requests coming in at once how do you 28579 18:03:28,840 --> 18:03:32,840 possibly keep track of all of them and 28580 18:03:30,636 --> 18:03:34,796 update your counter in a way that can 28581 18:03:32,840 --> 18:03:37,240 keep up with all of that traffic why 28582 18:03:34,796 --> 18:03:38,796 well let's just hypothesize what meta 28583 18:03:37,240 --> 18:03:40,680 formerly Facebook was doing underneath 28584 18:03:38,796 --> 18:03:42,360 the hood with Instagram if this were 28585 18:03:40,680 --> 18:03:44,436 their code so suppose for the sake of 28586 18:03:42,360 --> 18:03:47,320 discussion that Instagram servers are 28587 18:03:44,436 --> 18:03:48,880 using a mix of python and SQL probably 28588 18:03:47,320 --> 18:03:50,400 not using the cs50 library but they 28589 18:03:48,880 --> 18:03:53,360 could absolutely be using those two 28590 18:03:50,400 --> 18:03:55,400 languages or two others together um 28591 18:03:53,360 --> 18:03:58,000 suppose they do this in order to update 28592 18:03:55,400 --> 18:04:00,916 the number of likes for that post they 28593 18:03:58,000 --> 18:04:03,360 first execute a SQL query like select 28594 18:04:00,916 --> 18:04:06,000 the current number of likes from a table 28595 18:04:03,360 --> 18:04:08,400 called posts where the ID of the post 28596 18:04:06,000 --> 18:04:11,160 equals whatever the unique identifier is 28597 18:04:08,400 --> 18:04:13,040 for that spec specific egg in the table 28598 18:04:11,160 --> 18:04:15,276 and then they store the result in this 28599 18:04:13,040 --> 18:04:17,720 row variable just like I did and then 28600 18:04:15,276 --> 18:04:19,636 they do this they grab uh they create a 28601 18:04:17,720 --> 18:04:22,436 variable called likes they set it equal 28602 18:04:19,636 --> 18:04:24,680 to rows bracket Z so the very first row 28603 18:04:22,436 --> 18:04:26,520 in the result set and they get the likes 28604 18:04:24,680 --> 18:04:28,680 key so this is literally what I just did 28605 18:04:26,520 --> 18:04:30,436 with the count let me hypothesize that 28606 18:04:28,680 --> 18:04:32,080 Instagram does something similar with 28607 18:04:30,436 --> 18:04:34,116 the total number of likes why are they 28608 18:04:32,080 --> 18:04:36,200 doing this because they then want to 28609 18:04:34,116 --> 18:04:39,400 execute a third line of code that 28610 18:04:36,200 --> 18:04:42,720 executes update the posts table set the 28611 18:04:39,400 --> 18:04:44,916 new number of likes equal to something 28612 18:04:42,720 --> 18:04:46,880 where the ID of the post equals this 28613 18:04:44,916 --> 18:04:48,720 other thing now notice just like in 28614 18:04:46,880 --> 18:04:50,476 printf there's the comma separated list 28615 18:04:48,720 --> 18:04:52,476 of values they want to update the 28616 18:04:50,476 --> 18:04:54,476 current number of likes from the current 28617 18:04:52,476 --> 18:04:56,756 value to the current value plus one so 28618 18:04:54,476 --> 18:04:59,160 it's likes plus one and then we plug in 28619 18:04:56,756 --> 18:05:02,000 the ID for this so suppose this is what 28620 18:04:59,160 --> 18:05:04,400 Instagram's doing unfortunately whenever 28621 18:05:02,000 --> 18:05:06,756 you execute multiple lines of code 28622 18:05:04,400 --> 18:05:08,596 independently and you're so popular like 28623 18:05:06,756 --> 18:05:10,200 Instagram that you have thousands 28624 18:05:08,596 --> 18:05:12,436 hundreds of thousands of servers 28625 18:05:10,200 --> 18:05:13,796 potentially it is quite possible that if 28626 18:05:12,436 --> 18:05:15,796 you and I and everyone else in the room 28627 18:05:13,796 --> 18:05:17,400 clicks that egg at the same time it's 28628 18:05:15,796 --> 18:05:18,956 not going to be the case statistically 28629 18:05:17,400 --> 18:05:20,756 that like three lines of code are 28630 18:05:18,956 --> 18:05:22,000 executed for me and then three lines for 28631 18:05:20,756 --> 18:05:24,080 you and then three lines for you they're 28632 18:05:22,000 --> 18:05:25,796 probably going to get interspersed like 28633 18:05:24,080 --> 18:05:27,520 this gets executed for me and then this 28634 18:05:25,796 --> 18:05:29,400 gets executed for you and then they get 28635 18:05:27,520 --> 18:05:31,040 back to doing work for me and so forth 28636 18:05:29,400 --> 18:05:33,400 just to kind of multitask just like a 28637 18:05:31,040 --> 18:05:35,596 human might but at a super speed here 28638 18:05:33,400 --> 18:05:38,160 the problem though is if these lines of 28639 18:05:35,596 --> 18:05:39,636 code get interrupted what could go wrong 28640 18:05:38,160 --> 18:05:41,116 well suppose that Carter and I both 28641 18:05:39,636 --> 18:05:42,476 click the egg at the same time and 28642 18:05:41,116 --> 18:05:45,756 suppose the current number of likes back 28643 18:05:42,476 --> 18:05:48,116 in the day is 100 that stores in this 28644 18:05:45,756 --> 18:05:50,560 variable the value 100 but if we click 28645 18:05:48,116 --> 18:05:53,116 so close in time we might get back the 28646 18:05:50,560 --> 18:05:54,320 same answer to this select query as of 28647 18:05:53,116 --> 18:05:57,160 that moment in time when David and 28648 18:05:54,320 --> 18:05:58,880 Carter clicked it had a 100 likes but 28649 18:05:57,160 --> 18:06:01,360 then this last line of code is executed 28650 18:05:58,880 --> 18:06:04,116 for me and then maybe Carter because 28651 18:06:01,360 --> 18:06:07,596 that answer the state of the database 28652 18:06:04,116 --> 18:06:11,000 was stored in this variable then both 28653 18:06:07,596 --> 18:06:13,080 Carter and I will result in this line of 28654 18:06:11,000 --> 18:06:16,000 code being executed with the same value 28655 18:06:13,080 --> 18:06:17,520 update the post table setting the likes 28656 18:06:16,000 --> 18:06:21,320 equal to 28657 18:06:17,520 --> 18:06:22,840 101 for that post's ID why because again 28658 18:06:21,320 --> 18:06:25,756 if each of these lines of code running 28659 18:06:22,840 --> 18:06:28,116 on different servers are checking the 28660 18:06:25,756 --> 18:06:29,436 value of the current number of likes but 28661 18:06:28,116 --> 18:06:30,796 then getting interrupted because Carter 28662 18:06:29,436 --> 18:06:33,200 clicked the darn thing too and then 28663 18:06:30,796 --> 18:06:34,916 resuming their work on my behalf we 28664 18:06:33,200 --> 18:06:36,436 might have a race condition where the 28665 18:06:34,916 --> 18:06:38,080 code is sort of racing to finish but 28666 18:06:36,436 --> 18:06:40,636 getting interrupted by other users 28667 18:06:38,080 --> 18:06:42,796 clicks and the problem with that is that 28668 18:06:40,636 --> 18:06:45,276 if you are inspecting the value of some 28669 18:06:42,796 --> 18:06:47,000 variable or in this case a database cell 28670 18:06:45,276 --> 18:06:49,476 and making a decision based on it like 28671 18:06:47,000 --> 18:06:50,956 how to update it you might now lose data 28672 18:06:49,476 --> 18:06:52,680 and Instagram probably not good for 28673 18:06:50,956 --> 18:06:54,956 advertising if they're losing likes and 28674 18:06:52,680 --> 18:06:57,840 so that's probably a problem not to 28675 18:06:54,956 --> 18:07:00,320 retain the value 102 and instead insert 28676 18:06:57,840 --> 18:07:03,040 the number 101 twice it's actually 28677 18:07:00,320 --> 18:07:05,116 similar in spirit to a story that uh was 28678 18:07:03,040 --> 18:07:06,916 told in a databases course I took myself 28679 18:07:05,116 --> 18:07:10,240 years ago 28680 18:07:06,916 --> 18:07:11,596 whereby uh it's somewhat analogous to 28681 18:07:10,240 --> 18:07:12,796 kind of a contrived scenario involving 28682 18:07:11,596 --> 18:07:14,160 like a refrigerator and this is the 28683 18:07:12,796 --> 18:07:15,520 closest thing to a refrigerator we could 28684 18:07:14,160 --> 18:07:16,956 get on stage but imagine you've got like 28685 18:07:15,520 --> 18:07:19,160 one of these little dorm fridges in your 28686 18:07:16,956 --> 18:07:20,560 dorm 2 and your roommate and maybe both 28687 18:07:19,160 --> 18:07:22,560 of you as the story was told to me 28688 18:07:20,560 --> 18:07:23,880 really like milk and one of you is at 28689 18:07:22,560 --> 18:07:25,320 class but the other of you comes home 28690 18:07:23,880 --> 18:07:28,276 and you open your dorm fridge and you're 28691 18:07:25,320 --> 18:07:30,160 like oh darn it we're out of milk and so 28692 18:07:28,276 --> 18:07:32,276 you close the fridge you walk across the 28693 18:07:30,160 --> 18:07:33,840 street to CVS or some other store and 28694 18:07:32,276 --> 18:07:35,080 you get in line to buy some milk 28695 18:07:33,840 --> 18:07:36,560 Meanwhile your roommate gets out of 28696 18:07:35,080 --> 18:07:38,160 class they come back to your dorm room 28697 18:07:36,560 --> 18:07:40,000 they're really thirsty for some milk 28698 18:07:38,160 --> 18:07:42,116 they open up the fridge they say oh 28699 18:07:40,000 --> 18:07:44,276 we're out of milk and then they take a 28700 18:07:42,116 --> 18:07:46,040 different route perhaps to CVS or some 28701 18:07:44,276 --> 18:07:47,880 other store nearby get in line to buy 28702 18:07:46,040 --> 18:07:49,956 some milk fast forward some amount of 28703 18:07:47,880 --> 18:07:51,756 time in this very contrived story and 28704 18:07:49,956 --> 18:07:53,400 what happens oh damn it we now ended up 28705 18:07:51,756 --> 18:07:55,436 with two gallons of milk and there's no 28706 18:07:53,400 --> 18:07:57,680 way we can fit gallons of milk in there 28707 18:07:55,436 --> 18:08:00,840 let alone two of them so that's a 28708 18:07:57,680 --> 18:08:06,080 problem but what's the relationship to 28709 18:08:00,840 --> 18:08:06,080 this here well both of us yeah did 28710 18:08:18,240 --> 18:08:23,916 what 28711 18:08:20,916 --> 18:08:23,916 exactly 28712 18:08:25,756 --> 18:08:29,080 exactly so to summarize both of us had a 28713 18:08:27,720 --> 18:08:30,596 very similar thought process made a 28714 18:08:29,080 --> 18:08:32,680 similar decision based on the same 28715 18:08:30,596 --> 18:08:34,956 information not realizing that the 28716 18:08:32,680 --> 18:08:37,720 information the fridge was in the 28717 18:08:34,956 --> 18:08:39,520 process of being updated and of course 28718 18:08:37,720 --> 18:08:41,116 in the Instagram World happens like this 28719 18:08:39,520 --> 18:08:43,080 in the fridge World it might take a few 28720 18:08:41,116 --> 18:08:45,240 minutes but the problem is ultimately 28721 18:08:43,080 --> 18:08:47,596 the result of our having made a decision 28722 18:08:45,240 --> 18:08:49,756 about the state of the world and the 28723 18:08:47,596 --> 18:08:52,360 state of the world was in the middle of 28724 18:08:49,756 --> 18:08:54,000 being updated the queries got mingled 28725 18:08:52,360 --> 18:08:55,720 with others or in this case someone was 28726 18:08:54,000 --> 18:08:57,320 already on their way to the store so 28727 18:08:55,720 --> 18:08:58,956 what's the solution in the real world 28728 18:08:57,320 --> 18:09:00,720 well you could you know very simply like 28729 18:08:58,956 --> 18:09:02,840 take a Post-It note and put like gone 28730 18:09:00,720 --> 18:09:04,840 for milk so as to communicate to your 28731 18:09:02,840 --> 18:09:06,560 roommate that they should not inspect 28732 18:09:04,840 --> 18:09:08,880 the value of that variable and make a 28733 18:09:06,560 --> 18:09:11,160 decision on it why because it's not yet 28734 18:09:08,880 --> 18:09:12,560 consistent with the outcome that's about 28735 18:09:11,160 --> 18:09:14,320 to happen you could be more traumatic 28736 18:09:12,560 --> 18:09:16,240 and you could actually lock the fridge 28737 18:09:14,320 --> 18:09:18,200 somehow put a pad lock around it or the 28738 18:09:16,240 --> 18:09:19,756 like so they can't even get in there and 28739 18:09:18,200 --> 18:09:22,200 that would achieve the result uh the 28740 18:09:19,756 --> 18:09:23,956 same effect too and that is actually 28741 18:09:22,200 --> 18:09:25,956 pretty much the solution to this problem 28742 18:09:23,956 --> 18:09:28,240 in code too it's not safe it's not 28743 18:09:25,956 --> 18:09:30,636 sufficient to only execute three lines 28744 18:09:28,240 --> 18:09:33,400 of code like this rather what you 28745 18:09:30,636 --> 18:09:35,276 probably want to do is use additional 28746 18:09:33,400 --> 18:09:37,596 SQL keywords that we won't spend much 28747 18:09:35,276 --> 18:09:39,636 time on in the class itself but these 28748 18:09:37,596 --> 18:09:41,880 there are solutions to this problem you 28749 18:09:39,636 --> 18:09:44,796 can begin what's called a transaction 28750 18:09:41,880 --> 18:09:46,596 and you can more explicitly commit to 28751 18:09:44,796 --> 18:09:48,956 making a decision like updating the 28752 18:09:46,596 --> 18:09:51,320 database to 10 1001 or 10 1002 or if you 28753 18:09:48,956 --> 18:09:53,276 realize wait a minute Carter's query 28754 18:09:51,320 --> 18:09:55,916 inist interrupting mine let me roll back 28755 18:09:53,276 --> 18:09:58,756 to the previous state and just uh rewind 28756 18:09:55,916 --> 18:10:00,160 let me undo contrl Z if you will there's 28757 18:09:58,756 --> 18:10:02,080 also another keyword that's not so much 28758 18:10:00,160 --> 18:10:04,160 used anymore in SQL which is locking you 28759 18:10:02,080 --> 18:10:07,080 could literally back in the day lock the 28760 18:10:04,160 --> 18:10:09,200 entire database table preventing anyone 28761 18:10:07,080 --> 18:10:11,080 from updating it or making changes or 28762 18:10:09,200 --> 18:10:12,400 even reading it while someone else was 28763 18:10:11,080 --> 18:10:13,916 accessing it that was a very 28764 18:10:12,400 --> 18:10:15,476 heavy-handed solution because it slowed 28765 18:10:13,916 --> 18:10:18,200 everything down but in short 28766 18:10:15,476 --> 18:10:19,636 transactions are now a feature of SQL 28767 18:10:18,200 --> 18:10:22,000 that you won't necessarily need to use 28768 18:10:19,636 --> 18:10:24,320 yourselves that do solve this problem by 28769 18:10:22,000 --> 18:10:26,200 doing the equivalent of saying while 28770 18:10:24,320 --> 18:10:28,880 David's like counter is in the process 28771 18:10:26,200 --> 18:10:31,240 of being updated keep Carter at Bay 28772 18:10:28,880 --> 18:10:32,840 ideally briefly and then let his data go 28773 18:10:31,240 --> 18:10:34,720 through too it's equivalent too to 28774 18:10:32,840 --> 18:10:36,756 putting a note or a lock on the fridge 28775 18:10:34,720 --> 18:10:38,680 and indeed I mean lock litter they they 28776 18:10:36,756 --> 18:10:41,320 were Once Upon a Time called and still 28777 18:10:38,680 --> 18:10:44,276 are in some texts called locks on 28778 18:10:41,320 --> 18:10:46,160 databases 2 and the last Pro and the 28779 18:10:44,276 --> 18:10:49,400 code for which you might do this is 28780 18:10:46,160 --> 18:10:52,400 almost the same you simply wrap the 28781 18:10:49,400 --> 18:10:54,796 three queries uh with a transaction 28782 18:10:52,400 --> 18:10:57,360 statement and a uh commit and the term 28783 18:10:54,796 --> 18:11:01,040 of art here is that this makes your uh 28784 18:10:57,360 --> 18:11:03,080 your uh statements Atomic so Atomic 28785 18:11:01,040 --> 18:11:04,840 means they're either all executed or not 28786 18:11:03,080 --> 18:11:06,880 at all that is they're all very tightly 28787 18:11:04,840 --> 18:11:08,880 coupled together without interruption 28788 18:11:06,880 --> 18:11:11,520 transactions solves that problem and 28789 18:11:08,880 --> 18:11:13,160 avoid having two gallons of milk and the 28790 18:11:11,520 --> 18:11:15,720 last problem that arises that Is 28791 18:11:13,160 --> 18:11:17,796 tragically so darn common in the real 28792 18:11:15,720 --> 18:11:19,436 world today is what's called a SQL 28793 18:11:17,796 --> 18:11:21,160 injection attack and it's what I alluded 28794 18:11:19,436 --> 18:11:22,476 to earlier with the question mark So 28795 18:11:21,160 --> 18:11:25,000 suppose you're in the habit of logging 28796 18:11:22,476 --> 18:11:26,756 in uh to Yale websites with your net ID 28797 18:11:25,000 --> 18:11:28,916 or password or at Harvard your Harvard 28798 18:11:26,756 --> 18:11:30,680 key and password as well suppose for the 28799 18:11:28,916 --> 18:11:33,276 sake of discussion that the people that 28800 18:11:30,680 --> 18:11:34,840 implemented like Harvard key login allow 28801 18:11:33,276 --> 18:11:36,916 you to type in your email address of 28802 18:11:34,840 --> 18:11:39,160 course and your password but suppose 28803 18:11:36,916 --> 18:11:41,560 that they are using SQL underneath the 28804 18:11:39,160 --> 18:11:43,360 hood to check your username and password 28805 18:11:41,560 --> 18:11:46,040 to make sure that you are David menen or 28806 18:11:43,360 --> 18:11:48,276 Carter zeni or whoever you claim to be I 28807 18:11:46,040 --> 18:11:51,756 haven't shown you the syntax yet but it 28808 18:11:48,276 --> 18:11:53,840 turns out that in SQL Das Dash is a 28809 18:11:51,756 --> 18:11:55,596 special way of indicating a comment it 28810 18:11:53,840 --> 18:11:57,880 means ignore everything to the right so 28811 18:11:55,596 --> 18:12:00,040 it's just like SL slash and C or the 28812 18:11:57,880 --> 18:12:01,916 hash symbol in Python dash dash just 28813 18:12:00,040 --> 18:12:04,200 means ignore everything to the right and 28814 18:12:01,916 --> 18:12:07,160 we've of course seen single quotes So 28815 18:12:04,200 --> 18:12:10,040 one way to wage a SQL injection attack 28816 18:12:07,160 --> 18:12:11,520 is to try to inject malicious SQL code 28817 18:12:10,040 --> 18:12:13,520 into someone else's database without 28818 18:12:11,520 --> 18:12:15,876 them realizing it how do you do this 28819 18:12:13,520 --> 18:12:19,876 well suppose I log in as M harvard.edu 28820 18:12:15,876 --> 18:12:22,000 single quote Das Dash I'm not double 28821 18:12:19,876 --> 18:12:24,876 quoting anything clearly and there's 28822 18:12:22,000 --> 18:12:27,476 nothing to the right of the dash dash 28823 18:12:24,876 --> 18:12:29,244 anyway but it this imbalance is going to 28824 18:12:27,476 --> 18:12:31,196 be useful why because if I'm a hacker 28825 18:12:29,244 --> 18:12:33,360 and I'm presuming you know someone at 28826 18:12:31,196 --> 18:12:36,080 Harvard probably is using SQL uh single 28827 18:12:33,360 --> 18:12:38,680 quotes to wrap the user's email address 28828 18:12:36,080 --> 18:12:40,476 and wrap the user's password what if I 28829 18:12:38,680 --> 18:12:42,400 try to like complete their thought for 28830 18:12:40,476 --> 18:12:45,116 them and close one of those quotes for 28831 18:12:42,400 --> 18:12:46,920 them what might happen well we could do 28832 18:12:45,116 --> 18:12:48,360 this here for instance let me 28833 18:12:46,920 --> 18:12:50,516 hypothesize is the code that Harvard 28834 18:12:48,360 --> 18:12:52,436 wrote hopefully not underneath the hood 28835 18:12:50,516 --> 18:12:54,960 so they're using CS library and Python 28836 18:12:52,436 --> 18:12:57,320 and they're using SQL inside suppose 28837 18:12:54,960 --> 18:13:01,040 that they have a query like this select 28838 18:12:57,320 --> 18:13:03,116 star from users where username equals uh 28839 18:13:01,040 --> 18:13:04,756 question mark and password equals 28840 18:13:03,116 --> 18:13:06,400 question mark and then suppose they just 28841 18:13:04,756 --> 18:13:09,040 plug in whatever username and password 28842 18:13:06,400 --> 18:13:10,720 was typed in and then if they get back 28843 18:13:09,040 --> 18:13:12,720 some number of rows dot dot dot they 28844 18:13:10,720 --> 18:13:14,436 assume I am David they assume Carter is 28845 18:13:12,720 --> 18:13:16,360 Carter if both the username and password 28846 18:13:14,436 --> 18:13:19,080 are in the database just end of story 28847 18:13:16,360 --> 18:13:21,156 there this is good this has the question 28848 18:13:19,080 --> 18:13:23,320 mark placeholders we discussed earlier 28849 18:13:21,156 --> 18:13:24,836 but what if you don't quite remember 28850 18:13:23,320 --> 18:13:26,876 that you don't quite take that to heart 28851 18:13:24,836 --> 18:13:29,600 and you use your more familiar last week 28852 18:13:26,876 --> 18:13:31,836 F strings whereby we use these curly 28853 18:13:29,600 --> 18:13:33,516 braces to plug in values what if you do 28854 18:13:31,836 --> 18:13:36,116 this instead so it's almost the same 28855 18:13:33,516 --> 18:13:37,960 idea it's still DB execute but now it's 28856 18:13:36,116 --> 18:13:39,960 select star from users where username 28857 18:13:37,960 --> 18:13:42,000 equals and now notice I'm doing the 28858 18:13:39,960 --> 18:13:43,556 single quotes which is required by SQL 28859 18:13:42,000 --> 18:13:46,040 but I'm using F strings with the curly 28860 18:13:43,556 --> 18:13:48,476 braces and the password equals single 28861 18:13:46,040 --> 18:13:50,476 quote password and then close single 28862 18:13:48,476 --> 18:13:53,556 quote the problem is if you're just 28863 18:13:50,476 --> 18:13:56,196 blindly pasting equ effectively the 28864 18:13:53,556 --> 18:13:58,476 user's input into that web form into the 28865 18:13:56,196 --> 18:14:00,556 username field and the password field 28866 18:13:58,476 --> 18:14:03,080 there's nothing stopping a malicious 28867 18:14:00,556 --> 18:14:05,476 user student faculty staff from 28868 18:14:03,080 --> 18:14:08,280 including a single quote in their name 28869 18:14:05,476 --> 18:14:09,960 or maybe even an uh uh you know 28870 18:14:08,280 --> 18:14:11,680 benevolently if their name happens to 28871 18:14:09,960 --> 18:14:14,756 have a single quote as some last names 28872 18:14:11,680 --> 18:14:17,000 in particular do so this is very fragile 28873 18:14:14,756 --> 18:14:19,516 why well suppose that if we plug in my 28874 18:14:17,000 --> 18:14:22,156 malicious value Ma at harvard.edu single 28875 18:14:19,516 --> 18:14:25,640 quote-- notice what happens to username 28876 18:14:22,156 --> 18:14:28,960 here the username variable inside of the 28877 18:14:25,640 --> 18:14:30,960 curly quotes will get replaced with this 28878 18:14:28,960 --> 18:14:33,400 and notice single quote which the 28879 18:14:30,960 --> 18:14:37,600 Harvard programmer wrote M an 28880 18:14:33,400 --> 18:14:40,600 harvard.edu single quote which I wrote 28881 18:14:37,600 --> 18:14:42,556 dash dash which I wrote single quote 28882 18:14:40,600 --> 18:14:44,080 which Harvard wrote and whatever else 28883 18:14:42,556 --> 18:14:47,280 they want after that what's the 28884 18:14:44,080 --> 18:14:47,280 implication though of the dash 28885 18:14:47,796 --> 18:14:52,360 dash everything to the right is going to 28886 18:14:50,320 --> 18:14:55,116 be ignored so the password is never even 28887 18:14:52,360 --> 18:14:57,400 checked in this scenario I'm tricking 28888 18:14:55,116 --> 18:14:58,920 the server into ignoring everything 28889 18:14:57,400 --> 18:15:00,640 after the dash dash but I have 28890 18:14:58,920 --> 18:15:03,680 constructed very cleverly very 28891 18:15:00,640 --> 18:15:05,796 maliciously a syntactically valid query 28892 18:15:03,680 --> 18:15:07,516 why because I provided the single quote 28893 18:15:05,796 --> 18:15:09,960 that's going to finish the thought of 28894 18:15:07,516 --> 18:15:11,280 that first single quote and now I would 28895 18:15:09,960 --> 18:15:13,280 only know how to do this if I saw the 28896 18:15:11,280 --> 18:15:15,000 code or if I just randomly try putting 28897 18:15:13,280 --> 18:15:16,196 apostrophes into web forms and see if 28898 18:15:15,000 --> 18:15:18,156 things break that's often how 28899 18:15:16,196 --> 18:15:19,516 adversaries attack systems they type in 28900 18:15:18,156 --> 18:15:21,320 potentially dangerous characters hit 28901 18:15:19,516 --> 18:15:23,280 enter if something breaks they're not 28902 18:15:21,320 --> 18:15:25,080 necessarily into the system but they 28903 18:15:23,280 --> 18:15:26,244 know that there might be a vulnerability 28904 18:15:25,080 --> 18:15:28,244 and then they start trying more 28905 18:15:26,244 --> 18:15:29,756 methodically things like this so This 28906 18:15:28,244 --> 18:15:31,320 Then is going to be bad because it 28907 18:15:29,756 --> 18:15:34,040 effectively Grays out the rest of the 28908 18:15:31,320 --> 18:15:36,516 query and this query is surely going to 28909 18:15:34,040 --> 18:15:38,720 return some rows without even knowing my 28910 18:15:36,516 --> 18:15:41,000 password and so this logic here dot dot 28911 18:15:38,720 --> 18:15:42,876 dot means well if a data came back from 28912 18:15:41,000 --> 18:15:45,796 this query Harvard is presumably going 28913 18:15:42,876 --> 18:15:48,476 to assume that men logged in show them 28914 18:15:45,796 --> 18:15:50,796 uh show him like uh his account or 28915 18:15:48,476 --> 18:15:53,680 whatever is being protected here so in 28916 18:15:50,796 --> 18:15:56,600 short using F strings bad using any 28917 18:15:53,680 --> 18:15:58,556 equivalent like p uh percent s in C bad 28918 18:15:56,600 --> 18:16:00,960 when it comes to SQL using question 28919 18:15:58,556 --> 18:16:03,876 marks or whatever a thirdparty library 28920 18:16:00,960 --> 18:16:06,280 like cs50's prescribed is the way to 28921 18:16:03,876 --> 18:16:08,720 solve this why because libraries like 28922 18:16:06,280 --> 18:16:11,516 ours are designed to at least be smart 28923 18:16:08,720 --> 18:16:13,516 and be paranoid and what we will do is 28924 18:16:11,516 --> 18:16:17,040 this when you use the question marks and 28925 18:16:13,516 --> 18:16:19,116 the values are plugged in we will escape 28926 18:16:17,040 --> 18:16:21,516 any potentially dangerous characters 28927 18:16:19,116 --> 18:16:23,640 inside of those placeholders and so 28928 18:16:21,516 --> 18:16:25,156 effectively the single quote will no 28929 18:16:23,640 --> 18:16:27,080 longer be considered a grammatical 28930 18:16:25,156 --> 18:16:29,836 single quote it will just be literally a 28931 18:16:27,080 --> 18:16:32,476 character in the username or password so 28932 18:16:29,836 --> 18:16:33,836 the library takes care of this for you 28933 18:16:32,476 --> 18:16:36,080 because you're plugging in the username 28934 18:16:33,836 --> 18:16:38,516 and password as separate arguments and 28935 18:16:36,080 --> 18:16:41,000 then we or the third party you're using 28936 18:16:38,516 --> 18:16:43,000 actually sanitize that is uh clean up 28937 18:16:41,000 --> 18:16:44,756 the data and prevent those bad 28938 18:16:43,000 --> 18:16:46,244 characters now this is kind of an 28939 18:16:44,756 --> 18:16:48,876 internet meme that went around for a 28940 18:16:46,244 --> 18:16:50,280 while um if you've ever uh driven a car 28941 18:16:48,876 --> 18:16:52,680 been in a car where there's like the 28942 18:16:50,280 --> 18:16:54,080 automatic reader for tolls uh this 28943 18:16:52,680 --> 18:16:55,680 person thought it might be funny to try 28944 18:16:54,080 --> 18:16:58,320 doing something like this what are they 28945 18:16:55,680 --> 18:17:00,436 presumably doing the presumption here is 28946 18:16:58,320 --> 18:17:02,960 whether or not it worked as unclear is 28947 18:17:00,436 --> 18:17:04,436 that here's like the end of a actual 28948 18:17:02,960 --> 18:17:06,640 license plate number but here's an 28949 18:17:04,436 --> 18:17:07,960 interesting single quote and a semicolon 28950 18:17:06,640 --> 18:17:09,600 that's especially bad because it means 28951 18:17:07,960 --> 18:17:11,476 you can maybe execute a second query on 28952 18:17:09,600 --> 18:17:14,156 the database this is someone having fun 28953 18:17:11,476 --> 18:17:16,600 trying to drop the entire database table 28954 18:17:14,156 --> 18:17:18,756 for whatever municipality is scanning 28955 18:17:16,600 --> 18:17:20,556 through cameras uh their license plate 28956 18:17:18,756 --> 18:17:22,640 code and I would be remiss if we didn't 28957 18:17:20,556 --> 18:17:25,000 end on this note at least in computer 28958 18:17:22,640 --> 18:17:27,756 science circles um there is someone 28959 18:17:25,000 --> 18:17:30,080 named no relation to the TF name we put 28960 18:17:27,756 --> 18:17:33,156 in the database earlier um little Bobby 28961 18:17:30,080 --> 18:17:35,836 tables which ends with this XKCD 28962 18:17:33,156 --> 18:17:39,796 comic and if you chuckle if you laugh 28963 18:17:35,836 --> 18:17:39,796 you're now legit SQL 28964 18:17:40,556 --> 18:17:45,680 programmers nice 28965 18:17:43,244 --> 18:17:47,156 nice like every CS student out there 28966 18:17:45,680 --> 18:17:48,796 knows about little Bobby tables so if 28967 18:17:47,156 --> 18:17:50,876 you name drop little Bobby tables now 28968 18:17:48,796 --> 18:17:54,400 like you're in all right that's it 28969 18:17:50,876 --> 18:17:54,400 though for today we will see you next 28970 18:17:54,820 --> 18:18:02,929 [Music] 28971 18:18:08,920 --> 18:18:17,240 [Music] 28972 18:18:20,040 --> 18:18:23,040 time 28973 18:18:33,539 --> 18:18:50,729 [Music] 28974 18:18:56,679 --> 18:18:59,749 [Music] 28975 18:19:06,030 --> 18:19:09,140 [Music] 28976 18:19:15,960 --> 18:19:29,320 [Music] 28977 18:19:35,240 --> 18:19:57,659 [Music] 28978 18:19:54,570 --> 18:19:57,659 [Applause] 28979 18:20:00,020 --> 18:20:11,440 [Music] 28980 18:20:08,310 --> 18:20:11,440 [Applause] 28981 18:20:12,876 --> 18:20:15,280 all the way to the top and then you're 28982 18:20:14,080 --> 18:20:18,400 passing 28983 18:20:15,280 --> 18:20:22,244 down this is for you Yale we love you 28984 18:20:18,400 --> 18:20:24,436 Yale we're here to Har 28985 18:20:22,244 --> 18:20:24,436 go 28986 18:20:25,836 --> 18:20:28,836 har 28987 18:20:29,539 --> 18:20:32,909 [Applause] 28988 18:20:36,640 --> 18:20:43,000 down it's going to 28989 18:20:39,516 --> 18:20:45,360 happen it's actually going to happen I 28990 18:20:43,000 --> 18:20:49,280 can't believe this what do you think of 28991 18:20:45,360 --> 18:20:49,280 Y they don't think 28992 18:20:49,756 --> 18:20:54,000 good can't does everyone have it does 28993 18:20:52,476 --> 18:20:55,476 everyone have their stuff does everyone 28994 18:20:54,000 --> 18:20:58,640 have their stuff probably that it's 28995 18:20:55,476 --> 18:20:58,640 going to beable very 28996 18:21:00,080 --> 18:21:04,400 small I 28997 18:21:01,920 --> 18:21:08,640 know what 28998 18:21:04,400 --> 18:21:08,640 houses how many exra how many 28999 18:21:11,920 --> 18:21:18,244 EXT no F forer yeah just make sure 29000 18:21:16,920 --> 18:21:22,756 everyone 29001 18:21:18,244 --> 18:21:22,756 has pass all the car distributed 29002 18:21:22,920 --> 18:21:38,289 all right we can do it 29003 18:21:24,300 --> 18:21:41,389 [Applause] 29004 18:21:38,289 --> 18:21:41,389 [Music] 29005 18:21:49,680 --> 18:21:52,680 now 29006 18:21:53,836 --> 18:22:00,600 what do you think of Y 29007 18:21:55,600 --> 18:22:00,600 sir go go one more time one 29008 18:22:03,649 --> 18:22:08,020 [Applause] 29009 18:22:05,836 --> 18:22:19,680 more there goes 29010 18:22:08,020 --> 18:22:20,789 [Applause] 29011 18:22:19,680 --> 18:22:23,999 again 29012 18:22:20,789 --> 18:22:23,999 [Applause] 29013 18:22:25,876 --> 18:22:34,756 all right this is cs50 welcome to week 8 29014 18:22:31,196 --> 18:22:37,796 last week we learned how to create read 29015 18:22:34,756 --> 18:22:38,960 update and delete databases using squl 29016 18:22:37,796 --> 18:22:43,196 but this 29017 18:22:38,960 --> 18:22:43,196 week Adam everyone happy 29018 18:22:48,556 --> 18:22:54,600 Halloween all right so this is cs50 and 29019 18:22:52,244 --> 18:22:57,960 this is week eight already my thanks to 29020 18:22:54,600 --> 18:23:00,244 Adam on today this happy Halloween uh in 29021 18:22:57,960 --> 18:23:02,116 the coming moments we're going to learn 29022 18:23:00,244 --> 18:23:03,516 all about how the internet itself Works 29023 18:23:02,116 --> 18:23:05,556 which of course is a technology that 29024 18:23:03,516 --> 18:23:07,640 like we're all take we all use every day 29025 18:23:05,556 --> 18:23:09,196 probably using in some form right now 29026 18:23:07,640 --> 18:23:10,000 but we'll see that if you start to 29027 18:23:09,196 --> 18:23:12,000 understand some of the underlying 29028 18:23:10,000 --> 18:23:13,920 building blocks that power the internet 29029 18:23:12,000 --> 18:23:15,516 itself we can actually start to build 29030 18:23:13,920 --> 18:23:16,920 interesting things on top of it and a 29031 18:23:15,516 --> 18:23:18,640 lot of the apps the websites that you 29032 18:23:16,920 --> 18:23:20,556 all use every day should become all the 29033 18:23:18,640 --> 18:23:22,116 more familiar things that you yourself 29034 18:23:20,556 --> 18:23:23,476 VES can create and honestly when things 29035 18:23:22,116 --> 18:23:26,196 go wrong you'll have all the more of a 29036 18:23:23,476 --> 18:23:27,836 mental model for how things work or are 29037 18:23:26,196 --> 18:23:30,244 not in fact working so that you can 29038 18:23:27,836 --> 18:23:32,196 ultimately diagnose diagnose all the 29039 18:23:30,244 --> 18:23:34,000 more issues yourselves so if we take a 29040 18:23:32,196 --> 18:23:35,640 look at the internet in the early days 29041 18:23:34,000 --> 18:23:37,320 it pretty much was just this this 29042 18:23:35,640 --> 18:23:38,960 happens to be of course the geography of 29043 18:23:37,320 --> 18:23:41,836 the United States and just some of the 29044 18:23:38,960 --> 18:23:43,876 first uh points on the internet were 29045 18:23:41,836 --> 18:23:46,196 these here this was so-called arpanet 29046 18:23:43,876 --> 18:23:48,280 back in 1969 and indeed the internet had 29047 18:23:46,196 --> 18:23:50,756 its Origins here in the United States 29048 18:23:48,280 --> 18:23:53,320 with just a few computers interconnect 29049 18:23:50,756 --> 18:23:55,080 somehow initially that of course began 29050 18:23:53,320 --> 18:23:56,600 to grow over time such that we 29051 18:23:55,080 --> 18:23:58,640 eventually had the West Coast connected 29052 18:23:56,600 --> 18:24:00,680 to the east coast and nowadays what you 29053 18:23:58,640 --> 18:24:02,360 can think of these dots on the screen is 29054 18:24:00,680 --> 18:24:04,400 representing are these things called 29055 18:24:02,360 --> 18:24:06,796 routers sort of computers or really 29056 18:24:04,400 --> 18:24:08,836 servers that somehow have wires or maybe 29057 18:24:06,796 --> 18:24:11,244 wireless connections between them that 29058 18:24:08,836 --> 18:24:13,040 allow data to flow from point A to B to 29059 18:24:11,244 --> 18:24:15,556 C and then this of course has been now 29060 18:24:13,040 --> 18:24:17,516 magnified across the entire Globe um and 29061 18:24:15,556 --> 18:24:19,960 even above ground as well so that we can 29062 18:24:17,516 --> 18:24:22,836 connect all the more readily uh to 29063 18:24:19,960 --> 18:24:25,876 systems anywhere now in order to Route 29064 18:24:22,836 --> 18:24:27,876 the data from one router to another we 29065 18:24:25,876 --> 18:24:29,516 need to somehow make routing decisions 29066 18:24:27,876 --> 18:24:31,756 and this is the kind of thing that the 29067 18:24:29,516 --> 18:24:33,600 internet service providers the isps of 29068 18:24:31,756 --> 18:24:35,640 the world just handle for us you and I 29069 18:24:33,600 --> 18:24:37,516 plug our Macs our PCS into the network 29070 18:24:35,640 --> 18:24:40,000 here at Harvard or equivalently at Yale 29071 18:24:37,516 --> 18:24:41,556 or we somehow get online via Wi-Fi or 29072 18:24:40,000 --> 18:24:43,640 cellular technology and then some of 29073 18:24:41,556 --> 18:24:45,720 these larger entities these bigger 29074 18:24:43,640 --> 18:24:48,476 companies or countries handle most of 29075 18:24:45,720 --> 18:24:49,796 the data getting from point A to point B 29076 18:24:48,476 --> 18:24:51,436 and if you think about what these 29077 18:24:49,796 --> 18:24:53,556 routers present they're indeed just 29078 18:24:51,436 --> 18:24:55,720 servers somehow interconnected not 29079 18:24:53,556 --> 18:24:57,756 unlike this grid of tiles here for 29080 18:24:55,720 --> 18:24:59,836 instance back in the zoom days and in 29081 18:24:57,756 --> 18:25:01,680 fact here we have I claim a grid of 29082 18:24:59,836 --> 18:25:03,196 routers implemented here by the courses 29083 18:25:01,680 --> 18:25:05,796 teaching fellows and course assistants 29084 18:25:03,196 --> 18:25:08,000 and Tas and if the goal at hand for 29085 18:25:05,796 --> 18:25:10,000 instance is for Phyllis to Route some 29086 18:25:08,000 --> 18:25:11,516 piece of information maybe it's an email 29087 18:25:10,000 --> 18:25:13,360 maybe it's a request for a web page in 29088 18:25:11,516 --> 18:25:15,320 the bottom right hand corner all the way 29089 18:25:13,360 --> 18:25:17,680 up to say Brian here in the top left 29090 18:25:15,320 --> 18:25:19,876 hand corner suffice it to say each each 29091 18:25:17,680 --> 18:25:21,680 of these tiles represents a router a 29092 18:25:19,876 --> 18:25:23,876 server that can move the data back forth 29093 18:25:21,680 --> 18:25:25,476 left and right that packet of 29094 18:25:23,876 --> 18:25:27,876 information so to speak from Phyllis to 29095 18:25:25,476 --> 18:25:30,476 Brian could take any number of different 29096 18:25:27,876 --> 18:25:32,476 possible routes up down left right to go 29097 18:25:30,476 --> 18:25:34,476 from the one corner to another so let me 29098 18:25:32,476 --> 18:25:36,476 go ahead and hit play on this video here 29099 18:25:34,476 --> 18:25:37,830 we're in the teaching fellows play the 29100 18:25:36,476 --> 18:25:49,320 same 29101 18:25:37,830 --> 18:25:52,320 [Music] 29102 18:25:49,320 --> 18:25:52,320 role 29103 18:25:53,000 --> 18:25:56,796 all right so in this particular case the 29104 18:25:54,680 --> 18:25:58,680 data was routed pretty straightforwardly 29105 18:25:56,796 --> 18:26:00,556 up and then to the left but suppose that 29106 18:25:58,680 --> 18:26:02,836 one or more of the staff were a bit busy 29107 18:26:00,556 --> 18:26:04,360 maybe one of the routers is congested 29108 18:26:02,836 --> 18:26:06,080 that is to say just got way more 29109 18:26:04,360 --> 18:26:08,040 envelopes at a moment in time that it 29110 18:26:06,080 --> 18:26:09,836 can handle thankfully the design of the 29111 18:26:08,040 --> 18:26:11,400 internet is such that there's often 29112 18:26:09,836 --> 18:26:13,476 multiple ways that data can get from 29113 18:26:11,400 --> 18:26:15,756 point A to point B maybe going through 29114 18:26:13,476 --> 18:26:17,876 Point C or Point D instead and so 29115 18:26:15,756 --> 18:26:19,640 there's a resilience there even as some 29116 18:26:17,876 --> 18:26:21,476 of these servers themselves might go 29117 18:26:19,640 --> 18:26:23,960 down so allow me to propose that we use 29118 18:26:21,476 --> 18:26:26,040 the same grid of routers now to Route 29119 18:26:23,960 --> 18:26:27,539 the data in a slightly different way 29120 18:26:26,040 --> 18:26:39,710 this 29121 18:26:27,539 --> 18:26:41,090 [Music] 29122 18:26:39,710 --> 18:26:43,950 [Applause] 29123 18:26:41,090 --> 18:26:47,170 [Music] 29124 18:26:43,950 --> 18:26:47,170 [Applause] 29125 18:26:47,479 --> 18:26:52,244 [Music] 29126 18:26:49,244 --> 18:26:52,244 time 29127 18:26:54,796 --> 18:26:58,476 so success you'll see perhaps later just 29128 18:26:56,756 --> 18:27:00,280 how many takes it took us to actually 29129 18:26:58,476 --> 18:27:02,600 get that routing right but it does in 29130 18:27:00,280 --> 18:27:04,244 fact manifest that you can uh travel 29131 18:27:02,600 --> 18:27:06,320 different paths in order to get the data 29132 18:27:04,244 --> 18:27:07,640 from point A to point B so as we talk 29133 18:27:06,320 --> 18:27:09,600 about routers as you think of the 29134 18:27:07,640 --> 18:27:11,640 internet I mean think of those humans as 29135 18:27:09,600 --> 18:27:14,320 just representing these routers points a 29136 18:27:11,640 --> 18:27:16,876 to p and everywhere in between now how 29137 18:27:14,320 --> 18:27:19,360 did the teaching staff know to route 29138 18:27:16,876 --> 18:27:20,556 that packet up and then down or left and 29139 18:27:19,360 --> 18:27:22,280 right in order to get get to the 29140 18:27:20,556 --> 18:27:24,116 destination well all of them were 29141 18:27:22,280 --> 18:27:26,680 programmed so to speak to understand 29142 18:27:24,116 --> 18:27:28,960 protocols it's called TCP and IP 29143 18:27:26,680 --> 18:27:31,196 Otherwise Known together typically as 29144 18:27:28,960 --> 18:27:33,280 tcpip and you've probably seen these 29145 18:27:31,196 --> 18:27:35,116 acronyms at some point in the real world 29146 18:27:33,280 --> 18:27:36,640 on the internet on some kind of 29147 18:27:35,116 --> 18:27:38,360 documentation a text even if you haven't 29148 18:27:36,640 --> 18:27:39,680 really thought hard about it but IP is 29149 18:27:38,360 --> 18:27:42,000 certainly the more common of the two 29150 18:27:39,680 --> 18:27:44,756 perhaps in common culture so what does 29151 18:27:42,000 --> 18:27:46,920 TCP and IP do for us well really two 29152 18:27:44,756 --> 18:27:48,876 primary things any computer or any 29153 18:27:46,920 --> 18:27:51,400 teaching staff member who understands 29154 18:27:48,876 --> 18:27:53,516 tcpip knows how to get data from point A 29155 18:27:51,400 --> 18:27:56,280 to point B but how well let's break down 29156 18:27:53,516 --> 18:27:58,280 what that problem to be solved is IP 29157 18:27:56,280 --> 18:28:00,836 otherwise known as Internet Protocol is 29158 18:27:58,280 --> 18:28:02,756 a protocol that computers speak that 29159 18:28:00,836 --> 18:28:05,040 allow them to know how to address 29160 18:28:02,756 --> 18:28:06,960 computers on the internet and a protocol 29161 18:28:05,040 --> 18:28:08,756 is just a set of conventions that 29162 18:28:06,960 --> 18:28:10,600 computers adhere to so someone wrote 29163 18:28:08,756 --> 18:28:12,116 code that probably has a whole lot of 29164 18:28:10,600 --> 18:28:14,244 conditionals that tells the computer 29165 18:28:12,116 --> 18:28:16,516 what to do if something happens like if 29166 18:28:14,244 --> 18:28:18,280 I receive a packet then send it to the 29167 18:28:16,516 --> 18:28:20,360 next server or something like that in 29168 18:28:18,280 --> 18:28:22,280 the human world we have protocols too 29169 18:28:20,360 --> 18:28:23,796 you know pre in healthier times it was 29170 18:28:22,280 --> 18:28:25,836 quite common to sort of extend your hand 29171 18:28:23,796 --> 18:28:28,156 to another human in order to greet them 29172 18:28:25,836 --> 18:28:29,836 and if they're following human protocol 29173 18:28:28,156 --> 18:28:31,680 they would presumably grab your hand and 29174 18:28:29,836 --> 18:28:34,436 shake it at least in a a culture like 29175 18:28:31,680 --> 18:28:36,156 this one here on campus and now that is 29176 18:28:34,436 --> 18:28:38,244 a human protocol in that someone 29177 18:28:36,156 --> 18:28:39,680 initiates it someone responds to it and 29178 18:28:38,244 --> 18:28:41,244 you both sort of know what to do you're 29179 18:28:39,680 --> 18:28:43,436 programmed to know what to do so same 29180 18:28:41,244 --> 18:28:44,756 idea with Internet Protocol computers 29181 18:28:43,436 --> 18:28:46,720 just know what to do when they've been 29182 18:28:44,756 --> 18:28:50,080 programmed to do so so what does this 29183 18:28:46,720 --> 18:28:52,280 mean IP had decides that every compter 29184 18:28:50,080 --> 18:28:55,796 computer in the world will have a unique 29185 18:28:52,280 --> 18:28:57,360 address just like uh a the science 29186 18:28:55,796 --> 18:28:59,400 center around the corner might have a 29187 18:28:57,360 --> 18:29:03,156 unique address of one Oxford Street 29188 18:28:59,400 --> 18:29:04,600 Cambridge Massachusetts 02138 USA IP 29189 18:29:03,156 --> 18:29:06,640 dictates that every computer on the 29190 18:29:04,600 --> 18:29:08,280 internet have a unique address of this 29191 18:29:06,640 --> 18:29:09,640 form and this too is probably something 29192 18:29:08,280 --> 18:29:11,320 you've seen in the real world even if 29193 18:29:09,640 --> 18:29:13,360 you haven't thought too hard about it 29194 18:29:11,320 --> 18:29:15,244 it's a number and what's called dotted 29195 18:29:13,360 --> 18:29:17,320 decimal notation which means it's a 29196 18:29:15,244 --> 18:29:20,080 decimal number do something do something 29197 18:29:17,320 --> 18:29:21,960 do something so four digits separated by 29198 18:29:20,080 --> 18:29:23,720 convention by decimal points although 29199 18:29:21,960 --> 18:29:26,000 there are newer and bigger versions of 29200 18:29:23,720 --> 18:29:27,920 the same and these so-called IP 29201 18:29:26,000 --> 18:29:32,040 addresses that might be as simple as 29202 18:29:27,920 --> 18:29:34,080 1.2.3.4 uniquely identify a computer on 29203 18:29:32,040 --> 18:29:38,244 the internet uh the numbers have to 29204 18:29:34,080 --> 18:29:39,836 range from 0 to 255 each and that's a 29205 18:29:38,244 --> 18:29:41,516 bit of a hint just as you start to think 29206 18:29:39,836 --> 18:29:44,400 more computationally if each of these is 29207 18:29:41,516 --> 18:29:46,600 a number from 0 to 255 how many bits 29208 18:29:44,400 --> 18:29:48,960 does that suggest each number is 29209 18:29:46,600 --> 18:29:53,476 using feel free to shout it 29210 18:29:48,960 --> 18:29:55,360 out how many bits gives us 255 256 total 29211 18:29:53,476 --> 18:29:57,080 possibilities so eight bits that should 29212 18:29:55,360 --> 18:29:58,600 just be sort of a heuristic in your mind 29213 18:29:57,080 --> 18:30:02,244 anytime you hear something that's in the 29214 18:29:58,600 --> 18:30:03,876 range of 0 to 255 or 256 values total 29215 18:30:02,244 --> 18:30:05,640 think back to week zero which gives us 29216 18:30:03,876 --> 18:30:06,920 eight bits plus another eight bits 29217 18:30:05,640 --> 18:30:08,556 another eight bits and another eight 29218 18:30:06,920 --> 18:30:11,476 bits which is to say an IP address 29219 18:30:08,556 --> 18:30:13,080 typically is 32 bits in total now if we 29220 18:30:11,476 --> 18:30:15,040 do another bit of quick mental math or 29221 18:30:13,080 --> 18:30:17,436 think back to week zero if every IP 29222 18:30:15,040 --> 18:30:18,680 address is 32 bits how many computers 29223 18:30:17,436 --> 18:30:24,244 can we have on the 29224 18:30:18,680 --> 18:30:27,400 internet at once give or take 29225 18:30:24,244 --> 18:30:28,796 roughly 4 billion is the ballpark and we 29226 18:30:27,400 --> 18:30:31,080 don't need to be super precise for 29227 18:30:28,796 --> 18:30:32,876 discussion's sake but roughly 4 billion 29228 18:30:31,080 --> 18:30:35,000 is how high you can account assuming no 29229 18:30:32,876 --> 18:30:37,640 negative numbers if you have 32 bits in 29230 18:30:35,000 --> 18:30:39,756 total now that's not terribly uh many 29231 18:30:37,640 --> 18:30:41,000 number uh numbers of addresses 29232 18:30:39,756 --> 18:30:42,600 especially considering the number of 29233 18:30:41,000 --> 18:30:44,476 humans in the world the number of us 29234 18:30:42,600 --> 18:30:46,476 that do have laptops or desktops or 29235 18:30:44,476 --> 18:30:48,320 devices more generally phones in our 29236 18:30:46,476 --> 18:30:49,920 pockets and the like so let me just 29237 18:30:48,320 --> 18:30:51,720 stipulate for today's purposes that 29238 18:30:49,920 --> 18:30:53,960 there's even a newer and improved 29239 18:30:51,720 --> 18:30:56,556 version of Ip otherwise known as version 29240 18:30:53,960 --> 18:30:59,756 6 this is version four but still super 29241 18:30:56,556 --> 18:31:01,720 popular version six uses 128 bits which 29242 18:30:59,756 --> 18:31:03,556 is a huge number of possible 29243 18:31:01,720 --> 18:31:05,640 premutations I dare say I can't even 29244 18:31:03,556 --> 18:31:07,280 pronounce that number it's so big so 29245 18:31:05,640 --> 18:31:10,000 there are ways around even this 29246 18:31:07,280 --> 18:31:12,360 limitation already so every computer has 29247 18:31:10,000 --> 18:31:14,156 an address like this what does that 29248 18:31:12,360 --> 18:31:16,000 really mean well suppose that I was 29249 18:31:14,156 --> 18:31:17,556 Phyllis in the story told visually 29250 18:31:16,000 --> 18:31:19,836 earlier and I want to send a message to 29251 18:31:17,556 --> 18:31:22,476 Brian well both Phyllis and Brian have I 29252 18:31:19,836 --> 18:31:25,360 IP addresses and suppose that Brian's IP 29253 18:31:22,476 --> 18:31:27,876 address happens to be 1.2.3.4 in that 29254 18:31:25,360 --> 18:31:30,400 top left hand corner well phyllis's Mac 29255 18:31:27,876 --> 18:31:32,796 or PC or phone would essentially do the 29256 18:31:30,400 --> 18:31:35,196 equivalent on this human envelope by 29257 18:31:32,796 --> 18:31:37,080 writing the two address in the middle of 29258 18:31:35,196 --> 18:31:39,320 the envelope as is our human convention 29259 18:31:37,080 --> 18:31:41,280 like this so this is an envelope a piece 29260 18:31:39,320 --> 18:31:43,040 of information an email a text message 29261 18:31:41,280 --> 18:31:45,156 whatever destined for Brian and so she 29262 18:31:43,040 --> 18:31:47,360 would have her computer put Brian's IP 29263 18:31:45,156 --> 18:31:50,796 address in the middle her IP address is 29264 18:31:47,360 --> 18:31:53,080 Maybe 5.6.7 do8 so just like our human 29265 18:31:50,796 --> 18:31:56,680 convention I might write 5. 29266 18:31:53,080 --> 18:31:59,640 6.7.8 at the top of the envelope 29267 18:31:56,680 --> 18:32:01,196 5678 thereby indicating what the return 29268 18:31:59,640 --> 18:32:03,040 address is and this is helpful because 29269 18:32:01,196 --> 18:32:05,400 if Brian's computer needs to acknowledge 29270 18:32:03,040 --> 18:32:07,400 receipt if he needs to reply in some 29271 18:32:05,400 --> 18:32:09,156 form this way the envelope has all the 29272 18:32:07,400 --> 18:32:10,836 information we need but in the real 29273 18:32:09,156 --> 18:32:13,156 world servers do a lot of things 29274 18:32:10,836 --> 18:32:15,360 nowadays not just email but maybe chat 29275 18:32:13,156 --> 18:32:17,640 Maybe video conferencing maybe any 29276 18:32:15,360 --> 18:32:19,960 number of other services as well and so 29277 18:32:17,640 --> 18:32:21,960 it turns out that an address alone might 29278 18:32:19,960 --> 18:32:23,720 not be sufficient because how do how 29279 18:32:21,960 --> 18:32:25,516 does Brian's computer know when he opens 29280 18:32:23,720 --> 18:32:27,476 the envelope so to speak that this 29281 18:32:25,516 --> 18:32:29,116 should be interpreted as an email or 29282 18:32:27,476 --> 18:32:31,116 interpret it as a chat message or 29283 18:32:29,116 --> 18:32:33,040 interpret it as like a video attachment 29284 18:32:31,116 --> 18:32:35,116 that Phyllis has sent well we need some 29285 18:32:33,040 --> 18:32:37,360 other mechanism some other hint on this 29286 18:32:35,116 --> 18:32:39,640 envelope to distinguish one type of 29287 18:32:37,360 --> 18:32:42,436 Internet service from another and so 29288 18:32:39,640 --> 18:32:45,320 that's where the other acronym in TCI 29289 18:32:42,436 --> 18:32:47,876 pip comes in which is TCP so this stands 29290 18:32:45,320 --> 18:32:50,116 for transmission control protocol which 29291 18:32:47,876 --> 18:32:52,000 is just a different set of conventions 29292 18:32:50,116 --> 18:32:54,000 that computers adhere to in order to 29293 18:32:52,000 --> 18:32:56,400 solve a couple of different problems one 29294 18:32:54,000 --> 18:32:58,836 is this problem of distinguishing one 29295 18:32:56,400 --> 18:33:01,400 type of service from another now what 29296 18:32:58,836 --> 18:33:03,476 does that mean well humans decades ago 29297 18:33:01,400 --> 18:33:05,436 decided as they started inventing all of 29298 18:33:03,476 --> 18:33:07,836 these various internet services the web 29299 18:33:05,436 --> 18:33:09,000 being the first one how they might or 29300 18:33:07,836 --> 18:33:11,756 the web now being one of the most 29301 18:33:09,000 --> 18:33:13,720 popular ones they decided to assign 29302 18:33:11,756 --> 18:33:15,920 different services that can be used on 29303 18:33:13,720 --> 18:33:18,516 the internet unique numbers and so two 29304 18:33:15,920 --> 18:33:20,116 of the most common are these 80 is the 29305 18:33:18,516 --> 18:33:21,960 number that a bunch of humans decided 29306 18:33:20,116 --> 18:33:23,960 years ago will represent what you and I 29307 18:33:21,960 --> 18:33:26,400 know as HTTP and we'll talk more 29308 18:33:23,960 --> 18:33:28,000 technically in a bit about what HTTP is 29309 18:33:26,400 --> 18:33:30,640 but obviously it's the thing that's in 29310 18:33:28,000 --> 18:33:33,360 the beginning of every URL nowadays or 29311 18:33:30,640 --> 18:33:35,080 https which of course has the S added to 29312 18:33:33,360 --> 18:33:37,436 it and that has its own unique number 29313 18:33:35,080 --> 18:33:39,600 and for now the S just means secure one 29314 18:33:37,436 --> 18:33:41,080 is encrypted or scrambled somehow for 29315 18:33:39,600 --> 18:33:42,400 privacy sake and the other is 29316 18:33:41,080 --> 18:33:44,640 unencrypted it's a little more 29317 18:33:42,400 --> 18:33:46,796 vulnerable to interception so these two 29318 18:33:44,640 --> 18:33:50,080 numbers are what the world decided when 29319 18:33:46,796 --> 18:33:51,876 implementing TCP shall uniquely identify 29320 18:33:50,080 --> 18:33:53,516 those services so what does this mean 29321 18:33:51,876 --> 18:33:55,600 well this means that if Brian's computer 29322 18:33:53,516 --> 18:33:57,244 in the story from before is hosting not 29323 18:33:55,600 --> 18:33:58,796 like an email server but maybe he has a 29324 18:33:57,244 --> 18:34:01,400 website and Phyllis is requesting 29325 18:33:58,796 --> 18:34:04,320 Brian's homepage or something like that 29326 18:34:01,400 --> 18:34:07,244 she would have her Mac or PC or phone 29327 18:34:04,320 --> 18:34:09,436 not only write Brian's IP address in the 29328 18:34:07,244 --> 18:34:11,876 middle of the envelope but also the 29329 18:34:09,436 --> 18:34:13,920 number otherwise known as a port number 29330 18:34:11,876 --> 18:34:16,876 that she wants this envelope to be 29331 18:34:13,920 --> 18:34:20,556 routed to now 80 would be insecure 29332 18:34:16,876 --> 18:34:24,476 nowadays HTTP col is sort of p and we 29333 18:34:20,556 --> 18:34:26,116 almost always see https colon now so I'm 29334 18:34:24,476 --> 18:34:27,960 just going to go with best practice and 29335 18:34:26,116 --> 18:34:29,196 I'm going to add a colon and then the 29336 18:34:27,960 --> 18:34:32,280 number 29337 18:34:29,196 --> 18:34:34,516 443 at the end of Brian's IP address so 29338 18:34:32,280 --> 18:34:36,116 now I have an IP address for Brian the 29339 18:34:34,516 --> 18:34:37,756 port number for the service that this is 29340 18:34:36,116 --> 18:34:39,000 relevant to and I'm not going to bother 29341 18:34:37,756 --> 18:34:40,720 writing it but it turns out that 29342 18:34:39,000 --> 18:34:42,920 phyllis's computer would also choose a 29343 18:34:40,720 --> 18:34:45,280 port number maybe a random port number 29344 18:34:42,920 --> 18:34:47,640 so that Brian can conversely reply and 29345 18:34:45,280 --> 18:34:50,600 then the computer can know which 29346 18:34:47,640 --> 18:34:52,400 response is in uh coming back for which 29347 18:34:50,600 --> 18:34:54,360 request but the most important one is 29348 18:34:52,400 --> 18:34:56,156 this one in the two field whereby this 29349 18:34:54,360 --> 18:34:57,796 distinguishes this from like an email a 29350 18:34:56,156 --> 18:34:59,756 chat message a video conference session 29351 18:34:57,796 --> 18:35:01,156 or a zoom or whatnot from anything else 29352 18:34:59,756 --> 18:35:03,280 happening on Brian's computer at the 29353 18:35:01,156 --> 18:35:05,796 same time so all this time if you've 29354 18:35:03,280 --> 18:35:08,000 seen these terms TCP and IP those are 29355 18:35:05,796 --> 18:35:10,080 really two of the most important things 29356 18:35:08,000 --> 18:35:12,400 that they do but TCP does one other 29357 18:35:10,080 --> 18:35:15,000 thing that's super useful too it turns 29358 18:35:12,400 --> 18:35:17,720 out that it's 29359 18:35:15,000 --> 18:35:19,436 super common nowadays especially to 29360 18:35:17,720 --> 18:35:21,156 transmit a lot of media on the internet 29361 18:35:19,436 --> 18:35:22,720 internet whether it's an image or maybe 29362 18:35:21,156 --> 18:35:24,960 it's a movie file and it would be a 29363 18:35:22,720 --> 18:35:27,040 little obnoxious to say the least if 29364 18:35:24,960 --> 18:35:28,680 you're downloading a really big file 29365 18:35:27,040 --> 18:35:30,000 meant that no one else in your dorm room 29366 18:35:28,680 --> 18:35:31,680 or your household could actually 29367 18:35:30,000 --> 18:35:33,556 download anything until you're actually 29368 18:35:31,680 --> 18:35:35,516 done so of course multiple people 29369 18:35:33,556 --> 18:35:36,756 nowadays can be on the internet at once 29370 18:35:35,516 --> 18:35:38,436 even if all of the connections are a 29371 18:35:36,756 --> 18:35:40,556 little slower but like one person's 29372 18:35:38,436 --> 18:35:42,756 usage does not block someone else's now 29373 18:35:40,556 --> 18:35:45,040 how does this work well TCP in 29374 18:35:42,756 --> 18:35:46,920 conjunction with IP can also allow you 29375 18:35:45,040 --> 18:35:48,000 to take like a really big image of a cat 29376 18:35:46,920 --> 18:35:50,244 which is the internet of course is 29377 18:35:48,000 --> 18:35:53,000 filled with and take a big image of a 29378 18:35:50,244 --> 18:35:55,196 cat or a big video file of a cat and 29379 18:35:53,000 --> 18:35:57,156 fragment it into multiple pieces so I'm 29380 18:35:55,196 --> 18:35:58,600 just going to sort of roughly tear it 29381 18:35:57,156 --> 18:36:00,556 down the middle and then maybe tear it 29382 18:35:58,600 --> 18:36:02,516 down the middle again so now it's four 29383 18:36:00,556 --> 18:36:04,244 different fragments and I'm I'm sorry 29384 18:36:02,516 --> 18:36:06,876 but the computer will be reassembling 29385 18:36:04,244 --> 18:36:09,680 these for us and what phyllis's computer 29386 18:36:06,876 --> 18:36:11,400 could do now if she's like uploading 29387 18:36:09,680 --> 18:36:14,000 this picture of a cat to Brian's web 29388 18:36:11,400 --> 18:36:16,280 server well she could put one fragment 29389 18:36:14,000 --> 18:36:18,600 in this envelope and then have three 29390 18:36:16,280 --> 18:36:20,680 separate envelopes for the other three 29391 18:36:18,600 --> 18:36:22,680 fragments and what you could then do on 29392 18:36:20,680 --> 18:36:24,360 the outside of this envelope is just 29393 18:36:22,680 --> 18:36:26,836 kind of number them somehow and in fact 29394 18:36:24,360 --> 18:36:28,680 this is something else that TCP and IP 29395 18:36:26,836 --> 18:36:30,556 together would do for us this first 29396 18:36:28,680 --> 18:36:34,244 envelope now might say something like 29397 18:36:30,556 --> 18:36:37,080 one out of four in the memo field so to 29398 18:36:34,244 --> 18:36:38,476 speak of the uh metaphorical envelope 29399 18:36:37,080 --> 18:36:40,556 here now this should be enough 29400 18:36:38,476 --> 18:36:42,000 information because now if Brian gets 29401 18:36:40,556 --> 18:36:43,556 all four of these envelopes he 29402 18:36:42,000 --> 18:36:45,040 presumably knows how to reassemble the 29403 18:36:43,556 --> 18:36:46,720 picture of the cat in order top to 29404 18:36:45,040 --> 18:36:48,836 bottom left to right but more 29405 18:36:46,720 --> 18:36:51,156 importantly suppose that one of the 29406 18:36:48,836 --> 18:36:53,556 routers one of the TFS in the video is 29407 18:36:51,156 --> 18:36:55,556 sort of distracted and they sort of drop 29408 18:36:53,556 --> 18:36:57,080 one of the packets and that's a metaphor 29409 18:36:55,556 --> 18:36:58,876 actually in practice for when a router 29410 18:36:57,080 --> 18:37:00,836 gets really busy it's got way too much 29411 18:36:58,876 --> 18:37:02,516 data coming in it might metaphorically 29412 18:37:00,836 --> 18:37:04,000 drop packets what does that mean in 29413 18:37:02,516 --> 18:37:05,756 practice I mean it literally just 29414 18:37:04,000 --> 18:37:07,360 ignores the zeros and ones it doesn't 29415 18:37:05,756 --> 18:37:09,400 save them to its memory because there's 29416 18:37:07,360 --> 18:37:11,476 just no room left so it's equivalent to 29417 18:37:09,400 --> 18:37:14,360 sort of dropping the packet so suppose 29418 18:37:11,476 --> 18:37:16,516 now that Brian gets one of four three of 29419 18:37:14,360 --> 18:37:18,320 four and four of four what can his 29420 18:37:16,516 --> 18:37:21,796 computer infer 29421 18:37:18,320 --> 18:37:23,960 now after receiving those three packets 29422 18:37:21,796 --> 18:37:26,640 one of four three of four and four of 29423 18:37:23,960 --> 18:37:28,436 four what's the use there yeah I think 29424 18:37:26,640 --> 18:37:30,000 you're you're signaling with your 29425 18:37:28,436 --> 18:37:32,244 fingers which one did which one can I 29426 18:37:30,000 --> 18:37:33,836 call on you yeah so he's missing two out 29427 18:37:32,244 --> 18:37:35,476 of four the second of the packets and 29428 18:37:33,836 --> 18:37:37,040 this is useful now because you could 29429 18:37:35,476 --> 18:37:39,476 imagine he can send some message back to 29430 18:37:37,040 --> 18:37:41,556 Phyllis saying hey please retransmit 29431 18:37:39,476 --> 18:37:43,920 number two of four without having to 29432 18:37:41,556 --> 18:37:46,360 redownload the entirety of the cat so 29433 18:37:43,920 --> 18:37:49,920 there's there's an efficiency there as 29434 18:37:46,360 --> 18:37:52,196 well so tcpip allows data really to go 29435 18:37:49,920 --> 18:37:54,876 from point A to point B while solving a 29436 18:37:52,196 --> 18:37:56,556 bunch of these problems along the way so 29437 18:37:54,876 --> 18:37:59,436 nowadays if you ever see mention on your 29438 18:37:56,556 --> 18:38:02,080 Mac or PC of your so-called IP address 29439 18:37:59,436 --> 18:38:05,244 that is the sort of problem that's being 29440 18:38:02,080 --> 18:38:09,156 solved questions now on these protocols 29441 18:38:05,244 --> 18:38:11,516 these conventions called TCP and 29442 18:38:09,156 --> 18:38:12,756 IP that's the extent to which we'll need 29443 18:38:11,516 --> 18:38:14,244 to understand them won't have to 29444 18:38:12,756 --> 18:38:16,116 implement them per se we'll just take 29445 18:38:14,244 --> 18:38:18,876 them Hereafter for 29446 18:38:16,116 --> 18:38:20,600 granted any questions that you've ever 29447 18:38:18,876 --> 18:38:23,000 been wondering wondering about your home 29448 18:38:20,600 --> 18:38:23,000 network 29449 18:38:27,080 --> 18:38:32,832 yeah a really good question uh how does 29450 18:38:29,872 --> 18:38:35,872 TCP know that a user got a message 29451 18:38:32,832 --> 18:38:38,160 another aspect another feature of TCP is 29452 18:38:35,872 --> 18:38:40,160 that Brian's computer by design of this 29453 18:38:38,160 --> 18:38:41,600 protocol will also acknowledge the 29454 18:38:40,160 --> 18:38:43,480 packets that he's received and it will 29455 18:38:41,600 --> 18:38:45,440 do it efficiently if Brian receives all 29456 18:38:43,480 --> 18:38:47,080 four packets in a pretty narrow window 29457 18:38:45,440 --> 18:38:48,480 of time his computer will send to 29458 18:38:47,080 --> 18:38:50,920 phyllis's computer a quick message 29459 18:38:48,480 --> 18:38:52,440 saying essentially received all four 29460 18:38:50,920 --> 18:38:54,680 otherwise he'll say the opposite which 29461 18:38:52,440 --> 18:38:56,480 is that I'm missing for for instance two 29462 18:38:54,680 --> 18:38:57,960 out of four and that just ensures 29463 18:38:56,480 --> 18:39:00,080 ultimately that all of the data is 29464 18:38:57,960 --> 18:39:03,240 indeed uh arrived so that you're not 29465 18:39:00,080 --> 18:39:05,760 missing like uh a quarter a quadrant of 29466 18:39:03,240 --> 18:39:07,400 the cat in question all right but th 29467 18:39:05,760 --> 18:39:10,920 that's not the only problem that needs 29468 18:39:07,400 --> 18:39:13,552 to be solved ultimately we also need to 29469 18:39:10,920 --> 18:39:16,280 make the internet userfriendly if you 29470 18:39:13,552 --> 18:39:18,600 will and it would be really tedious if 29471 18:39:16,280 --> 18:39:21,160 you had to visit websites for instance 29472 18:39:18,600 --> 18:39:24,040 by way of of their IP addresses right 29473 18:39:21,160 --> 18:39:26,600 1.2.3.4 is pretty memorable but there's 29474 18:39:24,040 --> 18:39:28,512 like 4 billion other possible addresses 29475 18:39:26,600 --> 18:39:30,600 available and it would be super tedious 29476 18:39:28,512 --> 18:39:33,400 to remember those it would be bad 29477 18:39:30,600 --> 18:39:35,160 marketing to advertise those in fact 29478 18:39:33,400 --> 18:39:36,800 most of you probably don't even know the 29479 18:39:35,160 --> 18:39:38,480 phone numbers of your closest friends 29480 18:39:36,800 --> 18:39:40,000 and family members anymore because you 29481 18:39:38,480 --> 18:39:41,872 instead store them in your contacts 29482 18:39:40,000 --> 18:39:44,080 you're in your dress book associating 29483 18:39:41,872 --> 18:39:46,960 with numbers that are completely opaque 29484 18:39:44,080 --> 18:39:49,192 with actual names or strings if you will 29485 18:39:46,960 --> 18:39:50,832 the same is goes for the internet too 29486 18:39:49,192 --> 18:39:52,640 even though every computer does have and 29487 18:39:50,832 --> 18:39:54,960 must have a unique IP address 29488 18:39:52,640 --> 18:39:56,800 numerically why well routers or 29489 18:39:54,960 --> 18:39:58,600 computers computers just crunch numbers 29490 18:39:56,800 --> 18:40:01,240 very readily but we humans work better 29491 18:39:58,600 --> 18:40:03,720 with strings of text we need some system 29492 18:40:01,240 --> 18:40:07,720 for converting userfriendly strings like 29493 18:40:03,720 --> 18:40:09,760 harvard.edu or yale.edu or google.com to 29494 18:40:07,720 --> 18:40:12,160 the underlying IP addresses and that's 29495 18:40:09,760 --> 18:40:15,512 where the next acronym comes in today 29496 18:40:12,160 --> 18:40:17,040 which is DNS domain name system so this 29497 18:40:15,512 --> 18:40:19,320 is just another technology that's been 29498 18:40:17,040 --> 18:40:21,872 in use for some time now and it's a 29499 18:40:19,320 --> 18:40:24,000 collection of servers on the internet 29500 18:40:21,872 --> 18:40:26,640 that whose purpose in life is to convert 29501 18:40:24,000 --> 18:40:29,040 domain names to IP addresses and maybe 29502 18:40:26,640 --> 18:40:31,320 vice versa as well so let me stipulate 29503 18:40:29,040 --> 18:40:33,552 for today's purposes there are some root 29504 18:40:31,320 --> 18:40:36,120 DNS servers in the world that long story 29505 18:40:33,552 --> 18:40:38,640 short know about all of the Dooms all of 29506 18:40:36,120 --> 18:40:41,192 the edus all of the dot dot dot all of 29507 18:40:38,640 --> 18:40:43,440 the other top level domains around the 29508 18:40:41,192 --> 18:40:46,160 world as well as in the US and then 29509 18:40:43,440 --> 18:40:48,720 there are some smaller DNS servers owned 29510 18:40:46,160 --> 18:40:51,160 by companies owned by universities and 29511 18:40:48,720 --> 18:40:53,000 even in your apartments or homes most 29512 18:40:51,160 --> 18:40:54,960 likely indeed if you have a home router 29513 18:40:53,000 --> 18:40:56,800 plugged into the wall somewhere that's 29514 18:40:54,960 --> 18:40:59,240 not only routing your data in and out of 29515 18:40:56,800 --> 18:41:03,040 your apartment or home or dorm room it's 29516 18:40:59,240 --> 18:41:05,800 also typically serving as a local 29517 18:41:03,040 --> 18:41:07,440 therefore faster DNS server a cash if 29518 18:41:05,800 --> 18:41:09,440 you will it's sort of locally saving 29519 18:41:07,440 --> 18:41:11,320 your most frequently accessed websites 29520 18:41:09,440 --> 18:41:13,080 and their IP addresses just to avoid 29521 18:41:11,320 --> 18:41:15,192 bothering the bigger more expensive 29522 18:41:13,080 --> 18:41:17,400 busier servers all day long so there's 29523 18:41:15,192 --> 18:41:20,000 DNS servers all over if you poke around 29524 18:41:17,400 --> 18:41:23,120 your settings in Windows or Mac OS or 29525 18:41:20,000 --> 18:41:25,400 Android or iOS you'll see mention of DNS 29526 18:41:23,120 --> 18:41:27,040 and you'll probably see the IP addresses 29527 18:41:25,400 --> 18:41:28,640 of the servers whose purpose in life is 29528 18:41:27,040 --> 18:41:30,640 to do this conversion for you but this 29529 18:41:28,640 --> 18:41:32,832 is a requisite feature if we just want 29530 18:41:30,640 --> 18:41:35,680 the internet to be user friendly and 29531 18:41:32,832 --> 18:41:38,400 allow us to use words instead of numbers 29532 18:41:35,680 --> 18:41:40,160 alone what's inside of these DNS servers 29533 18:41:38,400 --> 18:41:41,960 you know it's essentially a spreadsheet 29534 18:41:40,160 --> 18:41:43,680 or if we can say it more geeky it's 29535 18:41:41,960 --> 18:41:46,192 essentially like a hash table of some 29536 18:41:43,680 --> 18:41:48,360 sort which it has keys and values like 29537 18:41:46,192 --> 18:41:51,400 the key is the domain name harvard.edu 29538 18:41:48,360 --> 18:41:53,640 gale. edug google.com and the value is 29539 18:41:51,400 --> 18:41:56,600 the corresponding IP address or in many 29540 18:41:53,640 --> 18:41:58,920 cases IP addresses plural of the 29541 18:41:56,600 --> 18:42:00,000 corresponding servers so here already 29542 18:41:58,920 --> 18:42:01,280 even though I've drawn it fairly 29543 18:42:00,000 --> 18:42:03,080 abstractly like you would on a 29544 18:42:01,280 --> 18:42:04,960 chalkboard it's really probably 29545 18:42:03,080 --> 18:42:07,080 implemented as some kind of table maybe 29546 18:42:04,960 --> 18:42:09,280 a hash table maybe a database table 29547 18:42:07,080 --> 18:42:11,280 maybe SQL or something like that or 29548 18:42:09,280 --> 18:42:13,720 maybe it's even just a link list or an 29549 18:42:11,280 --> 18:42:16,600 array we just have to somehow enable 29550 18:42:13,720 --> 18:42:19,400 this computer to convert one to the 29551 18:42:16,600 --> 18:42:21,120 other now just to be super precise 29552 18:42:19,400 --> 18:42:23,240 DNS servers actually convert what are 29553 18:42:21,120 --> 18:42:25,320 called fully qualified domain names 29554 18:42:23,240 --> 18:42:27,280 which is generally not just harvard.edu 29555 18:42:25,320 --> 18:42:29,280 but more verbosely 29556 18:42:27,280 --> 18:42:31,640 www.harvard.edu and 29557 18:42:29,280 --> 18:42:34,440 www.google.com so the whole thing that 29558 18:42:31,640 --> 18:42:36,640 you would see as a substring of the URL 29559 18:42:34,440 --> 18:42:38,832 so that's what DNS does and that's what 29560 18:42:36,640 --> 18:42:41,400 your University your company your home 29561 18:42:38,832 --> 18:42:44,920 router are doing for you let me pause 29562 18:42:41,400 --> 18:42:47,240 here to see if there are any 29563 18:42:44,920 --> 18:42:50,192 questions this to is just a technology 29564 18:42:47,240 --> 18:42:54,800 now we'll take for granted just 29565 18:42:50,192 --> 18:42:57,480 works questions at all all right so 29566 18:42:54,800 --> 18:42:59,080 let's now transition among our protocols 29567 18:42:57,480 --> 18:43:00,720 really to the last for today which will 29568 18:42:59,080 --> 18:43:02,240 set the stage for actually solving 29569 18:43:00,720 --> 18:43:05,120 problems with these and writing some 29570 18:43:02,240 --> 18:43:07,040 code ultimately um HTTP this is 29571 18:43:05,120 --> 18:43:08,280 something that you see or hear all day 29572 18:43:07,040 --> 18:43:10,160 long even though you rarely have to 29573 18:43:08,280 --> 18:43:13,360 bother typing it anymore odds are if you 29574 18:43:10,160 --> 18:43:15,920 go to harvard.edu yale.edu google.com 29575 18:43:13,360 --> 18:43:18,080 you don't bother typing HTTP let alone 29576 18:43:15,920 --> 18:43:19,552 https manually anymore why because your 29577 18:43:18,080 --> 18:43:21,760 browser Auto completes that kind of 29578 18:43:19,552 --> 18:43:24,040 thing just to make life easier but it is 29579 18:43:21,760 --> 18:43:27,192 officially at the beginning of every URL 29580 18:43:24,040 --> 18:43:30,120 you visit either HTTP or the more secure 29581 18:43:27,192 --> 18:43:32,920 https whenever you're using your browser 29582 18:43:30,120 --> 18:43:36,640 to access some website so 29583 18:43:32,920 --> 18:43:39,000 HTTP stands for hyper text transfer 29584 18:43:36,640 --> 18:43:40,872 protocol and it's uh easily one of the 29585 18:43:39,000 --> 18:43:42,320 most popular dare say one of the most 29586 18:43:40,872 --> 18:43:44,280 powerful features of the internet 29587 18:43:42,320 --> 18:43:46,680 nowadays but the mental model to have 29588 18:43:44,280 --> 18:43:48,800 here is that HTTP or the web more 29589 18:43:46,680 --> 18:43:51,760 generally is kind of a service that runs 29590 18:43:48,800 --> 18:43:53,760 on top of the internet and maybe Zoom or 29591 18:43:51,760 --> 18:43:56,600 Microsoft teams is another service that 29592 18:43:53,760 --> 18:43:57,920 runs on top of the internet an iMessage 29593 18:43:56,600 --> 18:43:59,640 and Technologies like it is another 29594 18:43:57,920 --> 18:44:01,320 service that runs on top of the internet 29595 18:43:59,640 --> 18:44:03,832 so the internet is really like the lower 29596 18:44:01,320 --> 18:44:05,192 level Plumbing the tcpip stuff the DNS 29597 18:44:03,832 --> 18:44:07,960 stuff that just gets data from point A 29598 18:44:05,192 --> 18:44:10,000 to point B but now and we're in a a 29599 18:44:07,960 --> 18:44:13,400 software development class ultimately 29600 18:44:10,000 --> 18:44:15,920 here in cs50 HTTP is the application 29601 18:44:13,400 --> 18:44:18,552 Level protocol it's sort of what 29602 18:44:15,920 --> 18:44:21,120 programmers use what companies use what 29603 18:44:18,552 --> 18:44:23,160 uh developers use ultimately to use the 29604 18:44:21,120 --> 18:44:25,832 underlying Plumbing to build interesting 29605 18:44:23,160 --> 18:44:28,480 and Powerful things so what does this 29606 18:44:25,832 --> 18:44:30,872 mean when it comes to accessing Services 29607 18:44:28,480 --> 18:44:34,360 via HTTP or the more secure 29608 18:44:30,872 --> 18:44:35,920 https well here is a representative URL 29609 18:44:34,360 --> 18:44:37,552 even though you might not type the whole 29610 18:44:35,920 --> 18:44:39,192 thing if you poke around your address 29611 18:44:37,552 --> 18:44:41,240 bar this is what's up there with that 29612 18:44:39,192 --> 18:44:43,600 said a lot of browsers nowadays are kind 29613 18:44:41,240 --> 18:44:45,872 of simplifying if not dumbing down what 29614 18:44:43,600 --> 18:44:47,832 you see with your human eyes just to 29615 18:44:45,872 --> 18:44:49,512 shorten the strings especially on mobile 29616 18:44:47,832 --> 18:44:51,800 devices but almost always if you click 29617 18:44:49,512 --> 18:44:53,720 the URL or highlight it then you see the 29618 18:44:51,800 --> 18:44:56,440 whole thing but on many browsers you 29619 18:44:53,720 --> 18:44:58,000 might only ever see example.com but all 29620 18:44:56,440 --> 18:45:00,320 of this information is there it's just 29621 18:44:58,000 --> 18:45:02,760 getting more and more hidden just for 29622 18:45:00,320 --> 18:45:04,960 user interfaces sake well it turns out 29623 18:45:02,760 --> 18:45:06,800 when you visit a URL by default 29624 18:45:04,960 --> 18:45:09,512 especially if you type nothing after the 29625 18:45:06,800 --> 18:45:11,600 do com in this case you're technically 29626 18:45:09,512 --> 18:45:14,360 implicitly adding a single slash so a 29627 18:45:11,600 --> 18:45:16,800 single slash denotes the root of the 29628 18:45:14,360 --> 18:45:19,040 server that is the default page or 29629 18:45:16,800 --> 18:45:20,832 folder in the server and the slash 29630 18:45:19,040 --> 18:45:22,512 whether or not you type it or not is 29631 18:45:20,832 --> 18:45:23,800 implicitly going to be there and that 29632 18:45:22,512 --> 18:45:27,240 just means give me the default whatever 29633 18:45:23,800 --> 18:45:29,640 is at www.example.com give me that page 29634 18:45:27,240 --> 18:45:32,120 or that folder but URLs can be longer 29635 18:45:29,640 --> 18:45:34,192 than this and more generally there can 29636 18:45:32,120 --> 18:45:37,160 be a path so to speak and this is a term 29637 18:45:34,192 --> 18:45:40,480 of art a path is some sequence of folder 29638 18:45:37,160 --> 18:45:42,192 Andor file names after a URL like this 29639 18:45:40,480 --> 18:45:44,800 and so you might see more specifically 29640 18:45:42,192 --> 18:45:47,552 that a URL contains a very specific file 29641 18:45:44,800 --> 18:45:49,360 this isn't as common nowadays anymore 29642 18:45:47,552 --> 18:45:51,480 though we will begin to today by using 29643 18:45:49,360 --> 18:45:54,280 this technique but if there is a file 29644 18:45:51,480 --> 18:45:56,440 called literally file. HTML or something 29645 18:45:54,280 --> 18:45:59,080 else on the server that file is going to 29646 18:45:56,440 --> 18:46:01,512 be what this URL pulls up on the 29647 18:45:59,080 --> 18:46:03,400 computer meanwhile you might have slash 29648 18:46:01,512 --> 18:46:05,240 folder slash which just means show me 29649 18:46:03,400 --> 18:46:08,192 whatever is inside of this folder or you 29650 18:46:05,240 --> 18:46:10,920 might have more verbosely folder file. 29651 18:46:08,192 --> 18:46:13,192 HTML which will show you that file in 29652 18:46:10,920 --> 18:46:14,960 that folder and meanwhile just to give 29653 18:46:13,192 --> 18:46:17,640 some other terms of art this is the 29654 18:46:14,960 --> 18:46:19,240 so-called fully qualified domain name 29655 18:46:17,640 --> 18:46:20,760 and again these vocab don't matter all 29656 18:46:19,240 --> 18:46:23,400 that much but you'll hear or see them 29657 18:46:20,760 --> 18:46:26,480 over time we generally colloquially just 29658 18:46:23,400 --> 18:46:28,120 refer to this as the domain name which 29659 18:46:26,480 --> 18:46:30,552 is a little less precise but gets the 29660 18:46:28,120 --> 18:46:32,960 job done certainly in conversation and 29661 18:46:30,552 --> 18:46:36,400 this part here I described briefly 29662 18:46:32,960 --> 18:46:38,000 earlier what's the name for this suffix 29663 18:46:36,400 --> 18:46:39,120 at the very end of the fully qualified 29664 18:46:38,000 --> 18:46:43,640 domain 29665 18:46:39,120 --> 18:46:45,400 name the yeah yeah top level domain or 29666 18:46:43,640 --> 18:46:47,720 TLD and this is just some form of 29667 18:46:45,400 --> 18:46:49,680 categorization of the URLs now the 29668 18:46:47,720 --> 18:46:51,040 internet in got it start within the 29669 18:46:49,680 --> 18:46:52,680 United States and a lot of the first 29670 18:46:51,040 --> 18:46:54,080 websites of course came from the US and 29671 18:46:52,680 --> 18:46:55,920 so For Better or For Worse the sort of 29672 18:46:54,080 --> 18:46:59,000 steak was planted in the ground so 29673 18:46:55,920 --> 18:47:00,552 generally do indicated at least early on 29674 18:46:59,000 --> 18:47:02,120 that it was some kind of commercial 29675 18:47:00,552 --> 18:47:05,040 Enterprise a business that owned a 29676 18:47:02,120 --> 18:47:06,832 domain name edu is a educational 29677 18:47:05,040 --> 18:47:09,440 institution net was some kind of 29678 18:47:06,832 --> 18:47:13,600 network. goov was the US government now 29679 18:47:09,440 --> 18:47:17,120 there are also country code tlds cctlds 29680 18:47:13,600 --> 18:47:21,552 like UK or JP every country in the world 29681 18:47:17,120 --> 18:47:23,280 has its own two letter uh TLD um that 29682 18:47:21,552 --> 18:47:25,480 might very well be restricted to only 29683 18:47:23,280 --> 18:47:27,552 servers or companies or people in that 29684 18:47:25,480 --> 18:47:29,760 uh in that country many of them can be 29685 18:47:27,552 --> 18:47:32,040 used by anyone you've used a lot of URLs 29686 18:47:29,760 --> 18:47:35,512 in this class ending in 29687 18:47:32,040 --> 18:47:37,960 cs50. that doesn't mean input output um 29688 18:47:35,512 --> 18:47:40,192 it actually is a TLD from another 29689 18:47:37,960 --> 18:47:42,760 country that lets anyone on the internet 29690 18:47:40,192 --> 18:47:45,440 um pay for and on an annual basis using 29691 18:47:42,760 --> 18:47:47,720 that domain. TV for instance you might 29692 18:47:45,440 --> 18:47:50,320 see in some cases like twitch.tv and the 29693 18:47:47,720 --> 18:47:51,960 like um that too is owned by another 29694 18:47:50,320 --> 18:47:53,280 country that allows others in the 29695 18:47:51,960 --> 18:47:55,160 English-speaking World in this case to 29696 18:47:53,280 --> 18:47:57,000 use it as though it connotes TV but 29697 18:47:55,160 --> 18:47:59,640 those are just different types of TLS 29698 18:47:57,000 --> 18:48:01,400 that roughly categorize where the domain 29699 18:47:59,640 --> 18:48:02,832 lives but it doesn't necessarily mean 29700 18:48:01,400 --> 18:48:05,640 it's commercial anymore it doesn't 29701 18:48:02,832 --> 18:48:07,872 necessarily mean it's a Network anymore 29702 18:48:05,640 --> 18:48:10,480 for the most part there are hundreds of 29703 18:48:07,872 --> 18:48:12,512 tlds now for better for worse most of 29704 18:48:10,480 --> 18:48:14,960 which are less common than these big 29705 18:48:12,512 --> 18:48:16,552 ones um but most anyone can buy most of 29706 18:48:14,960 --> 18:48:19,600 them with just some restrictions on 29707 18:48:16,552 --> 18:48:22,120 things like edu and goov that are still 29708 18:48:19,600 --> 18:48:24,832 very much regulated this meanwhile is 29709 18:48:22,120 --> 18:48:27,240 what we might call the host name www 29710 18:48:24,832 --> 18:48:29,872 it's obviously a super common convention 29711 18:48:27,240 --> 18:48:31,512 like almost every website uses www as 29712 18:48:29,872 --> 18:48:33,552 its host name but that's a human 29713 18:48:31,512 --> 18:48:35,320 convention it's not a requirement and 29714 18:48:33,552 --> 18:48:37,240 indeed some websites don't even bother 29715 18:48:35,320 --> 18:48:40,760 having a host name they just use their 29716 18:48:37,240 --> 18:48:42,920 domain to advertise their websites this 29717 18:48:40,760 --> 18:48:44,480 now is going to be the scheme or the 29718 18:48:42,920 --> 18:48:48,440 protocol and this is just going to 29719 18:48:44,480 --> 18:48:50,360 indicate via what protocol the computer 29720 18:48:48,440 --> 18:48:53,760 your Mac your PC your phone should use 29721 18:48:50,360 --> 18:48:55,600 when accessing content at that address 29722 18:48:53,760 --> 18:48:57,120 because indeed there are other protocols 29723 18:48:55,600 --> 18:48:59,512 you can use but for the most part 29724 18:48:57,120 --> 18:49:01,680 will'll only focus on HTTP or 29725 18:48:59,512 --> 18:49:04,160 equivalently 29726 18:49:01,680 --> 18:49:06,192 https all right any questions now on 29727 18:49:04,160 --> 18:49:08,240 those just definitions building blocks 29728 18:49:06,192 --> 18:49:10,440 of URLs just so we all sort of share a 29729 18:49:08,240 --> 18:49:13,440 common 29730 18:49:10,440 --> 18:49:15,640 vocabulary any questions at all yeah 29731 18:49:13,440 --> 18:49:17,720 what is the 29732 18:49:15,640 --> 18:49:19,552 local sure we'll come back to this 29733 18:49:17,720 --> 18:49:21,360 actually later today there's a a 29734 18:49:19,552 --> 18:49:23,552 technical term known as Local Host which 29735 18:49:21,360 --> 18:49:25,440 is a generic name for your computer your 29736 18:49:23,552 --> 18:49:27,480 Mac your PC your phone especially when 29737 18:49:25,440 --> 18:49:29,440 you're doing software development and by 29738 18:49:27,480 --> 18:49:31,192 convention your own computer has not 29739 18:49:29,440 --> 18:49:33,360 only whatever IP address you get from 29740 18:49:31,192 --> 18:49:35,800 your University or your internet service 29741 18:49:33,360 --> 18:49:37,480 provider it also has a reflexive IP 29742 18:49:35,800 --> 18:49:41,280 address one that just always refers to 29743 18:49:37,480 --> 18:49:43,080 itself which is 127.0.0.1 and that's 29744 18:49:41,280 --> 18:49:45,280 just a human convention humans decided 29745 18:49:43,080 --> 18:49:46,280 that shall refer always to your computer 29746 18:49:45,280 --> 18:49:47,872 and it's actually going to be useful 29747 18:49:46,280 --> 18:49:50,320 today and onward because we can use that 29748 18:49:47,872 --> 18:49:53,920 when development on our own computers 29749 18:49:50,320 --> 18:49:57,320 ultimately other questions on URLs IP 29750 18:49:53,920 --> 18:50:00,760 DNS or any of these building 29751 18:49:57,320 --> 18:50:03,552 blocks all right so what do we mean by 29752 18:50:00,760 --> 18:50:05,680 HTTP being a protocol when I extended my 29753 18:50:03,552 --> 18:50:07,280 hand earlier as a human handshake you 29754 18:50:05,680 --> 18:50:09,832 know a typical human in healthy times 29755 18:50:07,280 --> 18:50:12,552 would know to respond in turn well how 29756 18:50:09,832 --> 18:50:14,552 does Brian's computer know to respond to 29757 18:50:12,552 --> 18:50:16,552 phyllis's envelope whatever message is 29758 18:50:14,552 --> 18:50:18,440 they're in well assuming that Brian is 29759 18:50:16,552 --> 18:50:20,280 indeed still a web server in the store 29760 18:50:18,440 --> 18:50:22,960 and Phyllis was trying to upload a cat 29761 18:50:20,280 --> 18:50:25,680 or maybe download a cat from Brian's web 29762 18:50:22,960 --> 18:50:27,960 server inside of phyllis's envelope 29763 18:50:25,680 --> 18:50:30,680 would have been a message literally in 29764 18:50:27,960 --> 18:50:32,192 text and it's English for the most part 29765 18:50:30,680 --> 18:50:33,800 and it would contain at the beginning of 29766 18:50:32,192 --> 18:50:36,080 that message literally one of these 29767 18:50:33,800 --> 18:50:37,960 English verbs either get which means 29768 18:50:36,080 --> 18:50:39,552 just that get me the homepage get me a 29769 18:50:37,960 --> 18:50:43,000 picture of a cat get me a picture of a 29770 18:50:39,552 --> 18:50:46,720 dog or a video or anything else or post 29771 18:50:43,000 --> 18:50:49,080 which often means post that is um put 29772 18:50:46,720 --> 18:50:50,920 that is um upload a picture of a cat or 29773 18:50:49,080 --> 18:50:53,552 a dog or something else to the server 29774 18:50:50,920 --> 18:50:55,160 instead that's not strictly the only use 29775 18:50:53,552 --> 18:50:56,552 cases for these but you can generally 29776 18:50:55,160 --> 18:50:58,400 think them as one is just getting 29777 18:50:56,552 --> 18:51:00,440 information and the other is posting 29778 18:50:58,400 --> 18:51:02,552 from the client from Phyllis to the 29779 18:51:00,440 --> 18:51:04,192 server in this case Brian so those are 29780 18:51:02,552 --> 18:51:06,080 the two keywords that we might see and 29781 18:51:04,192 --> 18:51:08,872 why is this useful well it turns out we 29782 18:51:06,080 --> 18:51:10,872 can start to see in our own Mac or PC 29783 18:51:08,872 --> 18:51:14,400 some of these very same messages for 29784 18:51:10,872 --> 18:51:17,512 instance if Phyllis were visiting not 29785 18:51:14,400 --> 18:51:19,552 Brian's but example.com that web server 29786 18:51:17,512 --> 18:51:21,280 inside of her metaphorical envelope 29787 18:51:19,552 --> 18:51:24,440 there would be a textual message that 29788 18:51:21,280 --> 18:51:27,160 literally starts with get slash then the 29789 18:51:24,440 --> 18:51:29,680 word HTTP then the version she's using 29790 18:51:27,160 --> 18:51:31,760 1.1 is very common two and three are 29791 18:51:29,680 --> 18:51:34,872 becoming more common but HTTP generally 29792 18:51:31,760 --> 18:51:37,512 looks like this the next line of text in 29793 18:51:34,872 --> 18:51:39,800 her envelope would probably say host 29794 18:51:37,512 --> 18:51:41,640 colon then literally the fully qualified 29795 18:51:39,800 --> 18:51:43,360 domain name of the server she's 29796 18:51:41,640 --> 18:51:44,832 accessing just in case and this happens 29797 18:51:43,360 --> 18:51:47,240 super commonly especially on small 29798 18:51:44,832 --> 18:51:50,120 websites if one server is hosting 29799 18:51:47,240 --> 18:51:51,920 multiple domain names multiple websites 29800 18:51:50,120 --> 18:51:53,400 this just distinguishes which one she 29801 18:51:51,920 --> 18:51:55,400 actually wants and then there's usually 29802 18:51:53,400 --> 18:51:58,960 a whole bunch of other lines of text as 29803 18:51:55,400 --> 18:52:01,920 well so where can you actually see this 29804 18:51:58,960 --> 18:52:03,640 well let me actually go ahead and do 29805 18:52:01,920 --> 18:52:06,240 this give me just a moment and I'm going 29806 18:52:03,640 --> 18:52:08,600 to open up on my computer here uh an 29807 18:52:06,240 --> 18:52:10,240 empty Chrome window in incognito mode 29808 18:52:08,600 --> 18:52:12,000 generally speaking incognito mode or 29809 18:52:10,240 --> 18:52:13,320 private mode is used when you don't want 29810 18:52:12,000 --> 18:52:14,832 there to be left remnants of what 29811 18:52:13,320 --> 18:52:16,960 websites you visited and it has the 29812 18:52:14,832 --> 18:52:19,040 effect for software developers of just 29813 18:52:16,960 --> 18:52:20,512 forgetting any things you might have 29814 18:52:19,040 --> 18:52:22,280 tried already within your browser 29815 18:52:20,512 --> 18:52:24,600 including things called cookies more on 29816 18:52:22,280 --> 18:52:26,080 those another time uh your autocomplete 29817 18:52:24,600 --> 18:52:28,080 history and the like so for development 29818 18:52:26,080 --> 18:52:29,320 purposes incognito mode is especially 29819 18:52:28,080 --> 18:52:31,040 helpful because it's sort of like 29820 18:52:29,320 --> 18:52:33,360 starting with a clean slate every time 29821 18:52:31,040 --> 18:52:34,600 you open a new private or incognito mode 29822 18:52:33,360 --> 18:52:36,960 so there's not going to be like any 29823 18:52:34,600 --> 18:52:38,400 remnants of previous testing or code 29824 18:52:36,960 --> 18:52:39,552 that you've been playing with and I'm 29825 18:52:38,400 --> 18:52:42,440 going to go ahead and do this I'm going 29826 18:52:39,552 --> 18:52:44,440 to go ahead and uh right click or 29827 18:52:42,440 --> 18:52:46,600 control click on Chrome I'm going to 29828 18:52:44,440 --> 18:52:48,720 choose inspect and it's going to pull up 29829 18:52:46,600 --> 18:52:49,832 this window sometimes on the side 29830 18:52:48,720 --> 18:52:51,280 sometimes on the bottom I'm going to 29831 18:52:49,832 --> 18:52:52,960 move it to the bottom just so we can see 29832 18:52:51,280 --> 18:52:54,760 it a little more readily and I'm going 29833 18:52:52,960 --> 18:52:57,160 to zoom in and it's going to look a 29834 18:52:54,760 --> 18:52:59,120 little Arcane at first and I'm going to 29835 18:52:57,160 --> 18:53:01,040 just highlight a few of these tabs we'll 29836 18:52:59,120 --> 18:53:03,832 see here along the top that there's 29837 18:53:01,040 --> 18:53:05,400 elements console sources Network and 29838 18:53:03,832 --> 18:53:07,320 whole bunch of other things as well this 29839 18:53:05,400 --> 18:53:09,480 is sort of the advanced mode in Chrome 29840 18:53:07,320 --> 18:53:11,192 and Safari and Firefox and Edge have 29841 18:53:09,480 --> 18:53:12,920 their own equivalent of these features 29842 18:53:11,192 --> 18:53:14,680 they've always been there even if you've 29843 18:53:12,920 --> 18:53:16,160 never clicked the right button to enable 29844 18:53:14,680 --> 18:53:18,400 these features and I'm going to focus 29845 18:53:16,160 --> 18:53:20,120 for a moment on network like this 29846 18:53:18,400 --> 18:53:21,872 this is a feature of the browser that's 29847 18:53:20,120 --> 18:53:24,040 going to allow me the programmer in this 29848 18:53:21,872 --> 18:53:26,160 case so the the engineer to just kind of 29849 18:53:24,040 --> 18:53:28,512 look at what messages my browser is 29850 18:53:26,160 --> 18:53:30,872 actually sending to a server so let me 29851 18:53:28,512 --> 18:53:34,920 go ahead and do something like this let 29852 18:53:30,872 --> 18:53:36,192 me go ahead and visit uh for instance uh 29853 18:53:34,920 --> 18:53:37,640 in my browser here and I'm going to 29854 18:53:36,192 --> 18:53:41,552 shrink the window just a little bit so 29855 18:53:37,640 --> 18:53:44,320 we can see it exactly I'm going to visit 29856 18:53:41,552 --> 18:53:47,280 https uh 29857 18:53:44,320 --> 18:53:48,400 www.harvard.edu and now I'm going to hit 29858 18:53:47,280 --> 18:53:50,720 enter 29859 18:53:48,400 --> 18:53:52,480 and a whole bunch of stuff just happened 29860 18:53:50,720 --> 18:53:54,240 along the bottom of my screen and I'm 29861 18:53:52,480 --> 18:53:56,120 going to try to pull my window up just a 29862 18:53:54,240 --> 18:53:58,400 little bit so we can focus on a subset 29863 18:53:56,120 --> 18:54:00,400 of this let me pull this up covering up 29864 18:53:58,400 --> 18:54:03,640 really the content of the page focusing 29865 18:54:00,400 --> 18:54:06,760 on these lower level details down here 29866 18:54:03,640 --> 18:54:11,480 and what I want to see first 29867 18:54:06,760 --> 18:54:13,512 is let me oh sorry let me go ahead and 29868 18:54:11,480 --> 18:54:15,320 reload this page here after retaining 29869 18:54:13,512 --> 18:54:17,192 the log so that we can see absolutely 29870 18:54:15,320 --> 18:54:19,040 everything on the screen and to be clear 29871 18:54:17,192 --> 18:54:20,720 I just checked because I forgot earlier 29872 18:54:19,040 --> 18:54:22,320 preserve log because I wanted to 29873 18:54:20,720 --> 18:54:24,192 preserve everything on the screen I want 29874 18:54:22,320 --> 18:54:26,480 to see everything all at once and we'll 29875 18:54:24,192 --> 18:54:28,600 see this the very first line of output 29876 18:54:26,480 --> 18:54:30,920 is completely overwhelming with detail 29877 18:54:28,600 --> 18:54:33,640 at first glance but what you'll see here 29878 18:54:30,920 --> 18:54:36,360 if I start to scroll down and down and 29879 18:54:33,640 --> 18:54:38,760 down and down are the so-called request 29880 18:54:36,360 --> 18:54:41,120 headers and let me zoom in here and what 29881 18:54:38,760 --> 18:54:43,400 you're seeing inside of chrome inside of 29882 18:54:41,120 --> 18:54:45,280 its Network tab in its so-called 29883 18:54:43,400 --> 18:54:47,800 developer tools again this is just for 29884 18:54:45,280 --> 18:54:50,080 engineering types you'll see all of the 29885 18:54:47,800 --> 18:54:52,440 headers all of the lines of text that 29886 18:54:50,080 --> 18:54:55,360 magically were sent by my Mac to 29887 18:54:52,440 --> 18:54:58,080 harvard.edu much like from Phyllis to 29888 18:54:55,360 --> 18:55:00,360 Brian server in that story so I can see 29889 18:54:58,080 --> 18:55:01,760 exactly what messages are being sent and 29890 18:55:00,360 --> 18:55:04,192 a lot of this we haven't talked about 29891 18:55:01,760 --> 18:55:06,160 yet but we do see some mention of get 29892 18:55:04,192 --> 18:55:08,680 and we see some mention of Slash and a 29893 18:55:06,160 --> 18:55:10,720 bunch of other Arcane details but notice 29894 18:55:08,680 --> 18:55:12,960 they're all sort of key value pairs with 29895 18:55:10,720 --> 18:55:14,832 the here indicating what the 29896 18:55:12,960 --> 18:55:16,080 corresponding value is now most of this 29897 18:55:14,832 --> 18:55:17,480 is not going to be interesting and we're 29898 18:55:16,080 --> 18:55:19,552 not going to focus too much on the weeds 29899 18:55:17,480 --> 18:55:22,760 of of all of this but it indeed gives us 29900 18:55:19,552 --> 18:55:25,320 a sense of what's inside of that virtual 29901 18:55:22,760 --> 18:55:27,640 envelope now harvard.edu is one thing 29902 18:55:25,320 --> 18:55:30,040 but there's other uh websites we might 29903 18:55:27,640 --> 18:55:32,280 visit as well um and no matter what they 29904 18:55:30,040 --> 18:55:34,960 are we're going to expect ultimately an 29905 18:55:32,280 --> 18:55:36,832 HTTP response so in addition to a 29906 18:55:34,960 --> 18:55:39,192 computer like my Mac or phyllis's 29907 18:55:36,832 --> 18:55:41,960 computer sending a request containing 29908 18:55:39,192 --> 18:55:44,720 gets and host and those details too 29909 18:55:41,960 --> 18:55:47,120 you'll see here in my slide form just 29910 18:55:44,720 --> 18:55:49,040 representative response from the server 29911 18:55:47,120 --> 18:55:50,552 and notice that key here is that the 29912 18:55:49,040 --> 18:55:52,800 server is responding in the same version 29913 18:55:50,552 --> 18:55:54,800 of HTTP in this example it's sending 29914 18:55:52,800 --> 18:55:56,680 back this so-called status code just a 29915 18:55:54,800 --> 18:55:59,360 numeric code that indicates in this case 29916 18:55:56,680 --> 18:56:01,640 that everything's okay and it includes 29917 18:55:59,360 --> 18:56:03,680 this header this HTTP header which again 29918 18:56:01,640 --> 18:56:05,400 is just a key value pair saying that the 29919 18:56:03,680 --> 18:56:08,552 type of this content that's coming back 29920 18:56:05,400 --> 18:56:10,960 from the server is text/html more on 29921 18:56:08,552 --> 18:56:12,872 HTML in just a little bit but for our 29922 18:56:10,960 --> 18:56:14,960 purposes now this just means that 29923 18:56:12,872 --> 18:56:17,280 harvard.edu is sending me back a web 29924 18:56:14,960 --> 18:56:19,512 page and indeed if we hide all of this 29925 18:56:17,280 --> 18:56:20,960 techn techical stuff that's the web page 29926 18:56:19,512 --> 18:56:23,280 that we saw up here with all of the 29927 18:56:20,960 --> 18:56:26,192 usual imagery and the like and in fact I 29928 18:56:23,280 --> 18:56:29,680 can see this if I scroll back up not to 29929 18:56:26,192 --> 18:56:32,040 request headers but response headers 29930 18:56:29,680 --> 18:56:34,120 you'll see up here that we get back 29931 18:56:32,040 --> 18:56:36,720 responses including the date that the 29932 18:56:34,120 --> 18:56:38,800 server responded and a whole bunch of 29933 18:56:36,720 --> 18:56:40,240 other details as well and honestly this 29934 18:56:38,800 --> 18:56:41,872 has always been under your fingertips 29935 18:56:40,240 --> 18:56:44,360 and it will soon be useful as we start 29936 18:56:41,872 --> 18:56:46,280 making web-based applications ultimately 29937 18:56:44,360 --> 18:56:48,440 but this very quickly gets overwhelming 29938 18:56:46,280 --> 18:56:50,120 quickly and so better in this might 29939 18:56:48,440 --> 18:56:52,440 actually be a tool that we can use 29940 18:56:50,120 --> 18:56:54,680 within our code space itself so let me 29941 18:56:52,440 --> 18:56:56,192 go back to vs code here I didn't open 29942 18:56:54,680 --> 18:56:57,832 any code tabs I'm just going to use my 29943 18:56:56,192 --> 18:56:59,280 terminal window for a moment and I'm 29944 18:56:57,832 --> 18:57:01,960 going to run a couple of commands that 29945 18:56:59,280 --> 18:57:04,040 are going to allow me to actually see 29946 18:57:01,960 --> 18:57:05,680 what is going on when I request one 29947 18:57:04,040 --> 18:57:09,000 website let me go ahead and use a 29948 18:57:05,680 --> 18:57:10,832 command called curl for connect URL and 29949 18:57:09,000 --> 18:57:12,680 this is like a command line black and 29950 18:57:10,832 --> 18:57:14,040 white program that's going to pretend to 29951 18:57:12,680 --> 18:57:16,440 be a browser and it's going to connect 29952 18:57:14,040 --> 18:57:17,800 to the URL show me the headers but it's 29953 18:57:16,440 --> 18:57:19,240 not going to show me the images are the 29954 18:57:17,800 --> 18:57:20,872 graphics which might very well be useful 29955 18:57:19,240 --> 18:57:23,080 to the humans but not to me right now as 29956 18:57:20,872 --> 18:57:25,240 the developer so I'm going to do curl 29957 18:57:23,080 --> 18:57:26,920 I'm going to do Dashi and then I'm going 29958 18:57:25,240 --> 18:57:31,040 to do 29959 18:57:26,920 --> 18:57:33,080 https www.harvard.edu as though I'm 29960 18:57:31,040 --> 18:57:35,640 pretending to be a browser requesting 29961 18:57:33,080 --> 18:57:38,480 the home page and what's nice about curl 29962 18:57:35,640 --> 18:57:40,720 is albeit overwhelming too you'll get 29963 18:57:38,480 --> 18:57:43,240 back a whole response from the server 29964 18:57:40,720 --> 18:57:45,552 containing only those header values the 29965 18:57:43,240 --> 18:57:47,640 key value pairs inside of the envelope 29966 18:57:45,552 --> 18:57:49,320 and we'll ignore almost all of these but 29967 18:57:47,640 --> 18:57:50,760 here is the response from the server it 29968 18:57:49,320 --> 18:57:53,600 responded using a new and improved 29969 18:57:50,760 --> 18:57:56,160 version of HTTP in this case version two 29970 18:57:53,600 --> 18:57:58,832 and it gave me back a 200 there's my 29971 18:57:56,160 --> 18:58:00,920 content type text/html and then this 29972 18:57:58,832 --> 18:58:03,192 Char set happens to do with the encoding 29973 18:58:00,920 --> 18:58:04,600 if it's Unicode or asky or something 29974 18:58:03,192 --> 18:58:06,720 else and then there's all this other 29975 18:58:04,600 --> 18:58:08,600 overwhelming detail for now but this is 29976 18:58:06,720 --> 18:58:10,000 the beginnings of my ability to just 29977 18:58:08,600 --> 18:58:13,720 kind of poke around and see how the 29978 18:58:10,000 --> 18:58:16,000 server works and it turns out too that 29979 18:58:13,720 --> 18:58:19,360 we'll be able to see other potential 29980 18:58:16,000 --> 18:58:23,160 responses as well so for instance uh 29981 18:58:19,360 --> 18:58:25,160 HTTP might not only return 100 what if I 29982 18:58:23,160 --> 18:58:27,480 do this instead let me go ahead and 29983 18:58:25,160 --> 18:58:29,360 visit c-i 29984 18:58:27,480 --> 18:58:32,920 HTTP 29985 18:58:29,360 --> 18:58:34,800 colon uh www.harvard.edu so notice I 29986 18:58:32,920 --> 18:58:36,800 deliberately use the insecure version of 29987 18:58:34,800 --> 18:58:38,800 the URL which maybe Harvard's 29988 18:58:36,800 --> 18:58:40,832 Administration system administration 29989 18:58:38,800 --> 18:58:44,000 doesn't like anymore well how can they 29990 18:58:40,832 --> 18:58:45,640 ensure that I the end user the student 29991 18:58:44,000 --> 18:58:48,400 nonetheless use 29992 18:58:45,640 --> 18:58:50,720 https even if I didn't type it myself 29993 18:58:48,400 --> 18:58:53,480 well let me run just that command with 29994 18:58:50,720 --> 18:58:55,480 just HTTP not https and you'll see that 29995 18:58:53,480 --> 18:58:58,400 everything is not okay it didn't come 29996 18:58:55,480 --> 18:58:59,960 back with a 200 it came back with 301 in 29997 18:58:58,400 --> 18:59:01,832 this message saying Harvard moved 29998 18:58:59,960 --> 18:59:04,360 permanently but here's where you can 29999 18:59:01,832 --> 18:59:06,000 look for another clue among all of these 30000 18:59:04,360 --> 18:59:09,192 lines most of which I don't care about 30001 18:59:06,000 --> 18:59:10,680 there's a location header colon that's a 30002 18:59:09,192 --> 18:59:12,680 little hint to me that says where 30003 18:59:10,680 --> 18:59:14,360 Harvard University has apparently moved 30004 18:59:12,680 --> 18:59:16,320 too on the web and what's different 30005 18:59:14,360 --> 18:59:19,552 about this URL just to be 30006 18:59:16,320 --> 18:59:21,640 clear it has the S included and what 30007 18:59:19,552 --> 18:59:23,480 your browser will do by default because 30008 18:59:21,640 --> 18:59:25,872 Google and Microsoft and Mozilla 30009 18:59:23,480 --> 18:59:29,040 programmed it this way whenever it sees 30010 18:59:25,872 --> 18:59:31,000 a 301 response instead of 200 it won't 30011 18:59:29,040 --> 18:59:33,832 show you any web page it will look for a 30012 18:59:31,000 --> 18:59:36,040 location header find that URL and then 30013 18:59:33,832 --> 18:59:38,000 automatically quote unquote redirect you 30014 18:59:36,040 --> 18:59:39,872 there to so this is why it doesn't 30015 18:59:38,000 --> 18:59:41,720 matter what we type in the browser 30016 18:59:39,872 --> 18:59:44,192 Harvard can have its server send these 30017 18:59:41,720 --> 18:59:46,400 semi-secret messages to our browsers and 30018 18:59:44,192 --> 18:59:48,600 then it will just visit a second URL all 30019 18:59:46,400 --> 18:59:51,440 automatically and you can do this with 30020 18:59:48,600 --> 18:59:53,720 host names as well suppose that Harvard 30021 18:59:51,440 --> 18:59:55,872 does not want to standardize on 30022 18:59:53,720 --> 18:59:58,040 harvard.edu why they just want it to 30023 18:59:55,872 --> 18:59:59,760 always be www maybe it's a branding 30024 18:59:58,040 --> 19:00:02,720 thing maybe it's a technical thing we 30025 18:59:59,760 --> 19:00:06,120 can see the exact same response here 30026 19:00:02,720 --> 19:00:08,760 this first tells me when I visit HTTP 30027 19:00:06,120 --> 19:00:10,920 harvard.edu with no www Harvard 30028 19:00:08,760 --> 19:00:14,192 minimally wants me to be using a secure 30029 19:00:10,920 --> 19:00:16,320 connection if I then okay fine cooperate 30030 19:00:14,192 --> 19:00:19,440 let me go ahead and clear my screen let 30031 19:00:16,320 --> 19:00:22,600 me add the s but not the www you can see 30032 19:00:19,440 --> 19:00:26,000 here that it again responded with 301 up 30033 19:00:22,600 --> 19:00:27,600 here and the location now adds the www 30034 19:00:26,000 --> 19:00:30,040 so it's just a way of bouncing users 30035 19:00:27,600 --> 19:00:32,960 from one place to another and this is 30036 19:00:30,040 --> 19:00:35,720 all thanks to http boiling down to 30037 19:00:32,960 --> 19:00:38,120 relatively simple messages inside of the 30038 19:00:35,720 --> 19:00:41,600 envelope that tell the computer the 30039 19:00:38,120 --> 19:00:44,872 browser in this case how to respond now 30040 19:00:41,600 --> 19:00:46,120 odds are you've seen others besides 301 30041 19:00:44,872 --> 19:00:47,832 even though you've probably never seen 30042 19:00:46,120 --> 19:00:49,480 that actual number unless you've done 30043 19:00:47,832 --> 19:00:51,120 this kind of thing before but there 30044 19:00:49,480 --> 19:00:52,680 probably is a number that like everyone 30045 19:00:51,120 --> 19:00:55,192 in this room has seen if if You' never 30046 19:00:52,680 --> 19:00:56,512 really wondered why is it that number I 30047 19:00:55,192 --> 19:01:01,040 think you're smiling what number are you 30048 19:00:56,512 --> 19:01:04,192 thinking of yeah so 404 why is 404 well 30049 19:01:01,040 --> 19:01:06,512 404 indicates by convention not found 30050 19:01:04,192 --> 19:01:08,960 and now why the world decided years ago 30051 19:01:06,512 --> 19:01:12,320 to show us normal humans on the internet 30052 19:01:08,960 --> 19:01:13,680 404 is anything significant is unclear 30053 19:01:12,320 --> 19:01:16,600 that's sort of like bad design like what 30054 19:01:13,680 --> 19:01:18,192 do I care if the status code is 404 but 30055 19:01:16,600 --> 19:01:20,320 it's common enough on the internet that 30056 19:01:18,192 --> 19:01:22,360 probably all of us have seen it but that 30057 19:01:20,320 --> 19:01:24,680 just means that some server when you 30058 19:01:22,360 --> 19:01:27,120 visit a URL that's incorrect maybe it's 30059 19:01:24,680 --> 19:01:30,160 outdated the URL has been changed if you 30060 19:01:27,120 --> 19:01:31,720 see a 404 it just means that the virtual 30061 19:01:30,160 --> 19:01:34,360 envelope that came from the server back 30062 19:01:31,720 --> 19:01:37,552 to your Mac or PC or phone contains not 30063 19:01:34,360 --> 19:01:40,040 200 okay not 301 moved permanently but 30064 19:01:37,552 --> 19:01:42,192 404 not found instead and it's usually 30065 19:01:40,040 --> 19:01:44,120 accompanied by a technical message maybe 30066 19:01:42,192 --> 19:01:45,640 a cute picture of a cat sort of hiding 30067 19:01:44,120 --> 19:01:47,720 because it means not found or something 30068 19:01:45,640 --> 19:01:50,000 like that the Aesthetics are entirely up 30069 19:01:47,720 --> 19:01:52,160 to the server but that's what the 404 30070 19:01:50,000 --> 19:01:53,872 means and there's other codes too a few 30071 19:01:52,160 --> 19:01:55,680 of which you'll use in the coming weeks 30072 19:01:53,872 --> 19:01:58,160 as we transition from commandline 30073 19:01:55,680 --> 19:02:00,040 programs in C and python to web-based 30074 19:01:58,160 --> 19:02:02,192 ones we've seen a few of these already 30075 19:02:00,040 --> 19:02:04,400 and several of them are kind of variants 30076 19:02:02,192 --> 19:02:07,080 of the same thing like these 300 ones 30077 19:02:04,400 --> 19:02:09,552 here but we'll see others like 403 30078 19:02:07,080 --> 19:02:11,720 Forbidden means like you probably forgot 30079 19:02:09,552 --> 19:02:14,160 to log in or you need to log in or 30080 19:02:11,720 --> 19:02:16,080 something like that uh internal server 30081 19:02:14,160 --> 19:02:18,920 error right now no in the coming weeks 30082 19:02:16,080 --> 19:02:20,832 you will absolutely encounter 500 errors 30083 19:02:18,920 --> 19:02:22,192 and they're always your fault it's going 30084 19:02:20,832 --> 19:02:25,040 to be the equivalent of like a 30085 19:02:22,192 --> 19:02:26,760 segmentation fault in C a 500 error 30086 19:02:25,040 --> 19:02:28,480 means like you or I screwed up when 30087 19:02:26,760 --> 19:02:29,512 writing some code so we're going to see 30088 19:02:28,480 --> 19:02:31,320 that but it's just going to be an 30089 19:02:29,512 --> 19:02:34,120 opportunity for us to fix it if a 30090 19:02:31,320 --> 19:02:35,552 server's overloaded you often see 503 30091 19:02:34,120 --> 19:02:38,080 like something's unavailable because 30092 19:02:35,552 --> 19:02:40,600 something's too popular uh or is maybe 30093 19:02:38,080 --> 19:02:43,720 worse getting attacked um this is an old 30094 19:02:40,600 --> 19:02:45,040 uh um April Fool's joke 418 is not 30095 19:02:43,720 --> 19:02:46,480 actually used in practice but someone 30096 19:02:45,040 --> 19:02:48,600 like took the time to write up an entire 30097 19:02:46,480 --> 19:02:50,400 formal text technical proposal so that 30098 19:02:48,600 --> 19:02:52,160 servers can respond saying I'm a teapot 30099 19:02:50,400 --> 19:02:53,920 so it's kind of part of Internet lore 30100 19:02:52,160 --> 19:02:56,440 and there's other ones of these status 30101 19:02:53,920 --> 19:02:59,192 codes as well but this is useful because 30102 19:02:56,440 --> 19:03:01,512 eventually we'll see in code you can use 30103 19:02:59,192 --> 19:03:04,360 this understanding uh high level as it 30104 19:03:01,512 --> 19:03:07,360 might be of HTTP to do some interesting 30105 19:03:04,360 --> 19:03:10,640 and Powerful things so for instance we 30106 19:03:07,360 --> 19:03:12,920 can even send fancier URLs to servers it 30107 19:03:10,640 --> 19:03:16,920 turns out as we'll soon see if you send 30108 19:03:12,920 --> 19:03:19,440 a message like this get/ search question 30109 19:03:16,920 --> 19:03:22,160 question mark Q equals cats and then 30110 19:03:19,440 --> 19:03:25,280 HTTP 1.1 or whatever version and you 30111 19:03:22,160 --> 19:03:27,800 send that message to Google server 30112 19:03:25,280 --> 19:03:30,192 www.google.com this is how you can 30113 19:03:27,800 --> 19:03:32,760 specify not just the path of a web page 30114 19:03:30,192 --> 19:03:34,240 that you want SL search in this case the 30115 19:03:32,760 --> 19:03:36,800 question mark it turns out is going to 30116 19:03:34,240 --> 19:03:40,000 be a convention in the internet in the 30117 19:03:36,800 --> 19:03:43,040 web specifically for passing hum human 30118 19:03:40,000 --> 19:03:44,512 uh user input to the server as well in 30119 19:03:43,040 --> 19:03:46,400 fact you've probably never paid close 30120 19:03:44,512 --> 19:03:48,080 attention to URLs but they very often 30121 19:03:46,400 --> 19:03:50,280 have question question marks they very 30122 19:03:48,080 --> 19:03:53,552 often have equal signs and indeed even 30123 19:03:50,280 --> 19:03:55,480 google.com supports a certain key Q in 30124 19:03:53,552 --> 19:03:57,440 this case for query and you can put 30125 19:03:55,480 --> 19:03:59,872 anything you want after that in order to 30126 19:03:57,440 --> 19:04:02,480 search for actual cats so if I actually 30127 19:03:59,872 --> 19:04:05,440 go back to Chrome itself here for a 30128 19:04:02,480 --> 19:04:08,552 moment let me pull back my uh pull back 30129 19:04:05,440 --> 19:04:10,720 open my Chrome browser here previously I 30130 19:04:08,552 --> 19:04:12,400 was using uh incognito mode for 30131 19:04:10,720 --> 19:04:13,720 harvard.edu I've gone ahead and Clos 30132 19:04:12,400 --> 19:04:15,680 that window and opened a new one so we 30133 19:04:13,720 --> 19:04:16,960 can start fresh by visiting Google 30134 19:04:15,680 --> 19:04:18,872 normally you and I are in the habit of 30135 19:04:16,960 --> 19:04:20,800 going to google.com and searching via 30136 19:04:18,872 --> 19:04:22,920 the form or nowadays you just type like 30137 19:04:20,800 --> 19:04:24,512 your search query in the browser itself 30138 19:04:22,920 --> 19:04:26,640 and it brings you automatically to 30139 19:04:24,512 --> 19:04:28,280 Google or Bing or something else but I 30140 19:04:26,640 --> 19:04:30,192 can really be pedantic here let me go 30141 19:04:28,280 --> 19:04:31,192 ahead and zoom in and I'll manually go 30142 19:04:30,192 --> 19:04:33,040 to 30143 19:04:31,192 --> 19:04:36,960 https 30144 19:04:33,040 --> 19:04:39,280 www.google.com search question mark Q 30145 19:04:36,960 --> 19:04:41,080 equals maybe cats now this would not be 30146 19:04:39,280 --> 19:04:42,600 a very userfriendly experience if all of 30147 19:04:41,080 --> 19:04:45,360 us had to manually type out something 30148 19:04:42,600 --> 19:04:47,760 crazy like that but that's what the form 30149 19:04:45,360 --> 19:04:50,480 is redirecting you to when you type in 30150 19:04:47,760 --> 19:04:53,320 more user friendly cats into like a text 30151 19:04:50,480 --> 19:04:55,080 box if I hit enter here we'll get back 30152 19:04:53,320 --> 19:04:57,920 indeed a whole bunch of search results 30153 19:04:55,080 --> 19:05:00,320 about cats if I zoom back in and maybe I 30154 19:04:57,920 --> 19:05:01,552 change it from cats to dogs that too is 30155 19:05:00,320 --> 19:05:03,720 going to change and notice it's 30156 19:05:01,552 --> 19:05:05,920 pre-populating the text box because 30157 19:05:03,720 --> 19:05:09,800 Google has written its code in order to 30158 19:05:05,920 --> 19:05:12,192 do so as well now appr propo of the 30159 19:05:09,800 --> 19:05:14,552 video with which we began today from 30160 19:05:12,192 --> 19:05:17,192 yester year one of the better uh Yale 30161 19:05:14,552 --> 19:05:19,360 pranks over the years um has anyone one 30162 19:05:17,192 --> 19:05:21,320 actually ever been to uh 30163 19:05:19,360 --> 19:05:23,920 safetyschool.org 30164 19:05:21,320 --> 19:05:27,000 and to our friends at Yale watching live 30165 19:05:23,920 --> 19:05:30,360 hi safetyschool.org so it's kind of fun 30166 19:05:27,000 --> 19:05:32,552 if you actually visit it uh depending on 30167 19:05:30,360 --> 19:05:34,760 who you are so if I open up a new window 30168 19:05:32,552 --> 19:05:36,640 here and I go to 30169 19:05:34,760 --> 19:05:39,120 https 30170 19:05:36,640 --> 19:05:44,920 safetyschool.org zooming 30171 19:05:39,120 --> 19:05:49,240 in enter my oh my look at where it goes 30172 19:05:44,920 --> 19:05:49,240 now how does that okay okay thank 30173 19:05:50,232 --> 19:05:54,960 you this is not a cs50 thing someone out 30174 19:05:53,360 --> 19:05:57,512 there who I don't know who they are for 30175 19:05:54,960 --> 19:06:00,320 like 20 years has been paying annually 30176 19:05:57,512 --> 19:06:03,120 for that domain safetyschool.org for 30177 19:06:00,320 --> 19:06:05,552 just this joke uh but if I now go back 30178 19:06:03,120 --> 19:06:07,080 into VSS code here in my terminal window 30179 19:06:05,552 --> 19:06:10,320 and a little more you know 30180 19:06:07,080 --> 19:06:12,192 sophisticatedly let me do curl dasi HTTP 30181 19:06:10,320 --> 19:06:14,080 col andsafety 30182 19:06:12,192 --> 19:06:15,872 school.org the browser of course 30183 19:06:14,080 --> 19:06:17,120 immediately redirected me to the website 30184 19:06:15,872 --> 19:06:19,120 but what's going on under underneath the 30185 19:06:17,120 --> 19:06:21,080 hood well 20 some years ago someone 30186 19:06:19,120 --> 19:06:24,120 bought the domain configured the server 30187 19:06:21,080 --> 19:06:26,040 quite simply to spit this out safety 30188 19:06:24,120 --> 19:06:28,600 school.org for years has moved 30189 19:06:26,040 --> 19:06:31,360 permanently to 30190 19:06:28,600 --> 19:06:32,920 www.yale.edu so just a little 30191 19:06:31,360 --> 19:06:34,320 demonstration of what you can do with 30192 19:06:32,920 --> 19:06:36,440 just a little bit of understanding of 30193 19:06:34,320 --> 19:06:37,800 HTTP that's been a lot already we've 30194 19:06:36,440 --> 19:06:39,232 laid the foundation for understanding 30195 19:06:37,800 --> 19:06:41,512 how the internet works now we're going 30196 19:06:39,232 --> 19:06:43,920 to use it with a language called HTML 30197 19:06:41,512 --> 19:06:46,320 hypertext markup language CSS cascading 30198 19:06:43,920 --> 19:06:48,400 stylesheets and JavaScript a proper 30199 19:06:46,320 --> 19:06:49,832 program in language but first it feels 30200 19:06:48,400 --> 19:06:51,440 opportune to take a 10-minute break we 30201 19:06:49,832 --> 19:06:52,920 have some wonderful Halloween candy in 30202 19:06:51,440 --> 19:06:55,192 the transcept and we'll be back in 10 30203 19:06:52,920 --> 19:06:58,280 with those 30204 19:06:55,192 --> 19:07:03,080 languages we're headed up to uh 30205 19:06:58,280 --> 19:07:03,080 Austin checking out the stadium for the 30206 19:07:03,192 --> 19:07:07,920 prank a few years ago I was at a math 30207 19:07:06,040 --> 19:07:10,640 conference and I was sitting around at 30208 19:07:07,920 --> 19:07:13,440 the table at dinner with a few other 30209 19:07:10,640 --> 19:07:15,832 mathematicians and uh one of them went 30210 19:07:13,440 --> 19:07:19,000 to Harvard and started telling the story 30211 19:07:15,832 --> 19:07:21,640 of this amazing prank that was you know 30212 19:07:19,000 --> 19:07:23,600 against Harvard and at that point I felt 30213 19:07:21,640 --> 19:07:26,160 I had to interrupt and said well 30214 19:07:23,600 --> 19:07:29,232 actually I can tell you a lot more about 30215 19:07:26,160 --> 19:07:31,160 that okay okay the idea was perfected in 30216 19:07:29,232 --> 19:07:33,480 a dorm room came up with the idea 30217 19:07:31,160 --> 19:07:35,800 actually to prank them with signs at the 30218 19:07:33,480 --> 19:07:37,280 football game we threw some ideas out 30219 19:07:35,800 --> 19:07:39,760 there as far as what what the signs 30220 19:07:37,280 --> 19:07:42,160 would say we uh eventually settled on we 30221 19:07:39,760 --> 19:07:45,232 suck and my immediate reaction was no 30222 19:07:42,160 --> 19:07:47,760 this will never work however the problem 30223 19:07:45,232 --> 19:07:49,400 solver in me started thinking well maybe 30224 19:07:47,760 --> 19:07:52,960 we can make this 30225 19:07:49,400 --> 19:07:55,080 work the problem they had to infiltrate 30226 19:07:52,960 --> 19:07:58,080 Harvard Stadium without getting caught 30227 19:07:55,080 --> 19:08:00,920 sneak in 1,800 placards distribute them 30228 19:07:58,080 --> 19:08:03,360 to unsuspecting Harvard fans and then 30229 19:08:00,920 --> 19:08:06,720 convince those fans to prank themselves 30230 19:08:03,360 --> 19:08:09,120 it's great we thought about basically 30231 19:08:06,720 --> 19:08:10,920 every possible thing that could go wrong 30232 19:08:09,120 --> 19:08:12,832 and tried to come up with a solution for 30233 19:08:10,920 --> 19:08:15,800 it and then you put two Reds on top of 30234 19:08:12,832 --> 19:08:17,600 it they made fake Harvard IDs and fake 30235 19:08:15,800 --> 19:08:20,872 backstories 30236 19:08:17,600 --> 19:08:22,280 fake placard designs and a 28 member 30237 19:08:20,872 --> 19:08:26,120 fake Pep 30238 19:08:22,280 --> 19:08:28,120 Squad on November 20th 2004 a fake 30239 19:08:26,120 --> 19:08:31,960 Harvard student smuggled the placards 30240 19:08:28,120 --> 19:08:31,960 into the game what do you think 30241 19:08:32,900 --> 19:08:36,470 [Music] 30242 19:08:33,280 --> 19:08:36,470 [Applause] 30243 19:08:38,660 --> 19:08:47,640 [Applause] 30244 19:08:41,600 --> 19:08:51,192 of but then trouble what houses 30245 19:08:47,640 --> 19:08:51,192 how many how many extra 30246 19:08:56,160 --> 19:09:00,040 are I you know just showed him the front 30247 19:08:58,480 --> 19:09:02,440 of this ID and all of a sudden he just 30248 19:09:00,040 --> 19:09:04,440 ran away and he felt so 30249 19:09:02,440 --> 19:09:07,512 embarrassed having escaped one 30250 19:09:04,440 --> 19:09:08,552 confrontation they couldn't risk another 30251 19:09:07,512 --> 19:09:11,920 it was 30252 19:09:08,552 --> 19:09:13,920 time this just looks like a total mess 30253 19:09:11,920 --> 19:09:17,080 we have absolutely no idea if this is 30254 19:09:13,920 --> 19:09:19,160 going to work look at the it's going to 30255 19:09:17,080 --> 19:09:22,512 happen it's actually going to 30256 19:09:19,160 --> 19:09:23,640 happen I can't believe this what was 30257 19:09:22,512 --> 19:09:27,120 once a 30258 19:09:23,640 --> 19:09:27,120 prank became a 30259 19:09:31,600 --> 19:09:38,080 legend and immediately we started 30260 19:09:33,832 --> 19:09:38,080 hearing chance from the other side 30261 19:09:38,920 --> 19:09:44,192 you and I think it was at that point in 30262 19:09:41,280 --> 19:09:47,512 time that we knew we had pulled it 30263 19:09:44,192 --> 19:09:48,620 off on 30264 19:09:47,512 --> 19:09:51,192 there it goes 30265 19:09:48,620 --> 19:09:53,400 [Applause] 30266 19:09:51,192 --> 19:09:56,120 again I really think it didn't matter 30267 19:09:53,400 --> 19:09:59,360 that Harvard won because of the prank 30268 19:09:56,120 --> 19:10:00,400 for a lot of Yale students in alumni we 30269 19:09:59,360 --> 19:10:03,840 definitely won that 30270 19:10:00,400 --> 19:10:03,840 [Music] 30271 19:10:14,000 --> 19:10:18,320 year all right 30272 19:10:19,120 --> 19:10:21,552 happy 30273 19:10:22,320 --> 19:10:29,480 Hallen thank you all okay thank you oh 30274 19:10:26,160 --> 19:10:32,480 here oh all right little Halloween candy 30275 19:10:29,480 --> 19:10:34,320 all right thank you all so glad I wore 30276 19:10:32,480 --> 19:10:39,680 the same thing 30277 19:10:34,320 --> 19:10:43,552 today so in just a 30278 19:10:39,680 --> 19:10:45,872 moment thank you okay so in just a 30279 19:10:43,552 --> 19:10:47,680 moment we'll transition to understanding 30280 19:10:45,872 --> 19:10:50,832 all the now what we can do with this 30281 19:10:47,680 --> 19:10:52,920 underlying infrastructure so again HTTP 30282 19:10:50,832 --> 19:10:54,960 and below it TCP IP is all about just 30283 19:10:52,920 --> 19:10:56,800 getting the data from point A to pointb 30284 19:10:54,960 --> 19:10:58,872 in some standardized way but now let's 30285 19:10:56,800 --> 19:11:01,400 talk about HTML this is the language in 30286 19:10:58,872 --> 19:11:03,552 which web pages themselves are written 30287 19:11:01,400 --> 19:11:04,760 hyper text markup language now some of 30288 19:11:03,552 --> 19:11:06,680 you might have used this before to make 30289 19:11:04,760 --> 19:11:08,480 personal homepages some of you might 30290 19:11:06,680 --> 19:11:10,232 have dabbled even if using some website 30291 19:11:08,480 --> 19:11:12,680 to create your own homepage but 30292 19:11:10,232 --> 19:11:15,120 understanding this language is useful 30293 19:11:12,680 --> 19:11:17,280 certainly for creating the Aesthetics of 30294 19:11:15,120 --> 19:11:19,192 a web page can vying data that's of 30295 19:11:17,280 --> 19:11:22,512 interest but at the end of the day it 30296 19:11:19,192 --> 19:11:24,120 and the language we look at next CSS uh 30297 19:11:22,512 --> 19:11:27,040 are not programming languages there's 30298 19:11:24,120 --> 19:11:29,040 going to be no functions no Loops uh no 30299 19:11:27,040 --> 19:11:30,760 programming logic but we will end today 30300 19:11:29,040 --> 19:11:32,600 with a teaser of a proper programming 30301 19:11:30,760 --> 19:11:35,440 language called JavaScript via which you 30302 19:11:32,600 --> 19:11:37,760 can manipulate all of these various uh 30303 19:11:35,440 --> 19:11:39,920 other languages as well so ultimately 30304 19:11:37,760 --> 19:11:42,080 HTML has like two features and this is a 30305 19:11:39,920 --> 19:11:43,720 language that we spend very brief amount 30306 19:11:42,080 --> 19:11:46,280 of time on because it really boils down 30307 19:11:43,720 --> 19:11:47,960 to just a couple of basic ideas and then 30308 19:11:46,280 --> 19:11:49,480 vocabulary that you'll build out over 30309 19:11:47,960 --> 19:11:51,232 time just by Googling looking up 30310 19:11:49,480 --> 19:11:53,480 references looking at other Pages source 30311 19:11:51,232 --> 19:11:55,552 code but tags and attributes are what 30312 19:11:53,480 --> 19:11:58,760 characterize HTML now what do I mean by 30313 19:11:55,552 --> 19:12:00,232 that here for instance is the HTML code 30314 19:11:58,760 --> 19:12:02,440 via which you can make probably the 30315 19:12:00,232 --> 19:12:05,040 simplest of all web pages one that quite 30316 19:12:02,440 --> 19:12:08,120 simply says in the uh browser window 30317 19:12:05,040 --> 19:12:09,920 hello title and hello body for instance 30318 19:12:08,120 --> 19:12:13,040 now what does this actually mean if you 30319 19:12:09,920 --> 19:12:15,320 imagine opening up uh this code in a 30320 19:12:13,040 --> 19:12:16,680 browser be it on a Mac or PC or phone 30321 19:12:15,320 --> 19:12:18,512 you'll see typically like some kind of 30322 19:12:16,680 --> 19:12:20,512 rectangular window and there's usually a 30323 19:12:18,512 --> 19:12:22,280 tab that has the title of that page and 30324 19:12:20,512 --> 19:12:24,192 then most of the rectangular region is 30325 19:12:22,280 --> 19:12:25,960 the web page itself what you're looking 30326 19:12:24,192 --> 19:12:28,232 at then is the code that's going to put 30327 19:12:25,960 --> 19:12:30,280 hello title in the title bar in the tab 30328 19:12:28,232 --> 19:12:32,480 at the very top and down at the bottom 30329 19:12:30,280 --> 19:12:34,320 hello body is going to be all that's in 30330 19:12:32,480 --> 19:12:36,232 the big black and white box that 30331 19:12:34,320 --> 19:12:37,800 composes the rest of the browser window 30332 19:12:36,232 --> 19:12:39,040 itself now what are the Salient 30333 19:12:37,800 --> 19:12:40,832 characteristics here that we'll now 30334 19:12:39,040 --> 19:12:45,000 start to take for granted well first 30335 19:12:40,832 --> 19:12:48,480 whoops uh first let's go ahead and give 30336 19:12:45,000 --> 19:12:50,040 me just a moment here um and actually do 30337 19:12:48,480 --> 19:12:51,872 something with this code so I'm going to 30338 19:12:50,040 --> 19:12:53,872 go ahead and do this back in vs code 30339 19:12:51,872 --> 19:12:57,920 here I'm going to first create a file 30340 19:12:53,872 --> 19:12:59,640 called say hello.html and in this tab 30341 19:12:57,920 --> 19:13:01,800 I'm going to go ahead and really repeat 30342 19:12:59,640 --> 19:13:04,512 exactly that same code now I had this 30343 19:13:01,800 --> 19:13:07,680 line first DOC type HTML then I had this 30344 19:13:04,512 --> 19:13:10,120 line HTML Lang equals quote unquote n 30345 19:13:07,680 --> 19:13:12,000 close quote then I had inside of that 30346 19:13:10,120 --> 19:13:13,800 head then I had inside of that title 30347 19:13:12,000 --> 19:13:14,960 then inside of that I had hello title 30348 19:13:13,800 --> 19:13:16,800 and I'm doing this quickly because we'll 30349 19:13:14,960 --> 19:13:20,280 tease apart in a moment what it actually 30350 19:13:16,800 --> 19:13:23,080 all means and then down here below that 30351 19:13:20,280 --> 19:13:26,160 so-called head I had just the text hello 30352 19:13:23,080 --> 19:13:27,832 body so at the moment I that I claim is 30353 19:13:26,160 --> 19:13:30,080 the entirety of a web page but it 30354 19:13:27,832 --> 19:13:33,120 currently lives in my code space so to 30355 19:13:30,080 --> 19:13:35,480 speak in a file called hello.html that's 30356 19:13:33,120 --> 19:13:36,720 fine if I want to create it but how do I 30357 19:13:35,480 --> 19:13:38,960 how do you how does anyone on the 30358 19:13:36,720 --> 19:13:41,920 internet actually view it well to serve 30359 19:13:38,960 --> 19:13:43,800 a web page you indeed need a web server 30360 19:13:41,920 --> 19:13:45,280 and it turns out that codespaces comes 30361 19:13:43,800 --> 19:13:48,552 with one of these pre-installed because 30362 19:13:45,280 --> 19:13:50,680 we cs50 staff uh did so for you and what 30363 19:13:48,552 --> 19:13:53,400 you can do in a terminal window once you 30364 19:13:50,680 --> 19:13:55,480 have an HTML file ready to go that you 30365 19:13:53,400 --> 19:13:58,040 want the world to see you can literally 30366 19:13:55,480 --> 19:14:00,552 run in your terminal window 30367 19:13:58,040 --> 19:14:02,640 http-server single command and what 30368 19:14:00,552 --> 19:14:04,872 that's going to do for you is start a 30369 19:14:02,640 --> 19:14:07,000 web server that is to say a program 30370 19:14:04,872 --> 19:14:08,920 whose purpose in life is just to serve 30371 19:14:07,000 --> 19:14:10,960 web pages and even though probably up 30372 19:14:08,920 --> 19:14:12,960 until now for years you probably if 30373 19:14:10,960 --> 19:14:15,480 you're like me equate server quote 30374 19:14:12,960 --> 19:14:17,192 unquote with a physical device server is 30375 19:14:15,480 --> 19:14:19,440 really aie piece of software it just 30376 19:14:17,192 --> 19:14:21,480 tends to run on big fancy devices so 30377 19:14:19,440 --> 19:14:23,832 when we say server we often all think of 30378 19:14:21,480 --> 19:14:25,680 in our Mind's Eye you know big expensive 30379 19:14:23,832 --> 19:14:27,600 devices perhaps but a server is just a 30380 19:14:25,680 --> 19:14:30,760 program whose purpose in life is to 30381 19:14:27,600 --> 19:14:32,832 respond to requests with responses and 30382 19:14:30,760 --> 19:14:34,680 that's the vernacular there now once you 30383 19:14:32,832 --> 19:14:35,920 run HTTP server and I'm going to do a 30384 19:14:34,680 --> 19:14:37,400 bit of magic because I set this up 30385 19:14:35,920 --> 19:14:39,512 before class just to make sure it goes 30386 19:14:37,400 --> 19:14:42,552 smoothly you'll see some output like 30387 19:14:39,512 --> 19:14:46,360 this whereby your server is now 30388 19:14:42,552 --> 19:14:48,360 available on a very long URL mine here 30389 19:14:46,360 --> 19:14:50,320 uh uh is a very long URL that will be 30390 19:14:48,360 --> 19:14:51,872 different from yours but what this is is 30391 19:14:50,320 --> 19:14:54,320 a unique identifier that your codes 30392 19:14:51,872 --> 19:14:56,800 space has temporarily generated so that 30393 19:14:54,320 --> 19:15:00,160 you can now access and ideally only you 30394 19:14:56,800 --> 19:15:01,920 can access that file using your browser 30395 19:15:00,160 --> 19:15:03,600 now if I flip the URL or you flip the 30396 19:15:01,920 --> 19:15:05,160 your all to public by right clicking or 30397 19:15:03,600 --> 19:15:07,120 control clicking the right features of 30398 19:15:05,160 --> 19:15:08,680 vs code you can enable anyone in the 30399 19:15:07,120 --> 19:15:10,600 world to visit it but we're not going to 30400 19:15:08,680 --> 19:15:12,400 ultimately host our websites in your 30401 19:15:10,600 --> 19:15:13,920 code space because as soon as you log 30402 19:15:12,400 --> 19:15:15,552 off for the night and the thing shuts 30403 19:15:13,920 --> 19:15:16,800 down like the website will go down but 30404 19:15:15,552 --> 19:15:18,440 at the end end of the semester 30405 19:15:16,800 --> 19:15:20,320 particularly for final projects we'll 30406 19:15:18,440 --> 19:15:22,680 show you ways that you can put your own 30407 19:15:20,320 --> 19:15:24,120 website your own code on the actual 30408 19:15:22,680 --> 19:15:26,360 internet 30409 19:15:24,120 --> 19:15:29,160 247365 even with your own domain name if 30410 19:15:26,360 --> 19:15:31,120 you want to get one so that it lives uh 30411 19:15:29,160 --> 19:15:33,512 independent of your own sleep schedule 30412 19:15:31,120 --> 19:15:36,120 and usage schedule of vs code here so 30413 19:15:33,512 --> 19:15:39,192 I'm going to go ahead now and visit um 30414 19:15:36,120 --> 19:15:41,440 this URL in another tab of my browser 30415 19:15:39,192 --> 19:15:44,080 and what I'll see here is this this is 30416 19:15:41,440 --> 19:15:46,640 the output of that program called HTTP 30417 19:15:44,080 --> 19:15:49,360 server and essentially what it is doing 30418 19:15:46,640 --> 19:15:52,280 is it's using TCP and IP in conjunction 30419 19:15:49,360 --> 19:15:55,552 with HTTP to just run your very own web 30420 19:15:52,280 --> 19:15:57,192 server on gith hubs own servers as well 30421 19:15:55,552 --> 19:15:58,440 and that's because of different ports 30422 19:15:57,192 --> 19:16:00,720 again we won't go too much into the 30423 19:15:58,440 --> 19:16:02,872 weeds of the TCP the IP and all of that 30424 19:16:00,720 --> 19:16:04,920 stuff but recall that different port 30425 19:16:02,872 --> 19:16:06,872 numbers can allow you to distinguish one 30426 19:16:04,920 --> 19:16:08,832 service from another now one of the 30427 19:16:06,872 --> 19:16:10,360 services is of course your code space 30428 19:16:08,832 --> 19:16:12,320 VSS code in the cloud that we've been 30429 19:16:10,360 --> 19:16:14,192 using for weeks but if you want to use 30430 19:16:12,320 --> 19:16:17,120 the same physical server that GitHub 30431 19:16:14,192 --> 19:16:19,192 controls but actually visit your own web 30432 19:16:17,120 --> 19:16:21,280 server that I just ran in my terminal 30433 19:16:19,192 --> 19:16:22,440 window in another tab that's fine 30434 19:16:21,280 --> 19:16:24,120 they're just going to be using different 30435 19:16:22,440 --> 19:16:25,720 TCP ports and you and I don't have to 30436 19:16:24,120 --> 19:16:28,280 care what they are but just that this is 30437 19:16:25,720 --> 19:16:30,080 a feature that TCP supports so what you 30438 19:16:28,280 --> 19:16:32,000 see here is somewhat Arcane this is not 30439 19:16:30,080 --> 19:16:33,760 like a thing that most people on the 30440 19:16:32,000 --> 19:16:35,552 internet should ever see I'm just doing 30441 19:16:33,760 --> 19:16:37,920 this for development purposes but this 30442 19:16:35,552 --> 19:16:40,760 is the index that is the directory the 30443 19:16:37,920 --> 19:16:42,280 the folder contents of my code space and 30444 19:16:40,760 --> 19:16:44,480 because I deleted everything from prior 30445 19:16:42,280 --> 19:16:46,040 weeks already all we see right now is 30446 19:16:44,480 --> 19:16:48,280 hello.html 30447 19:16:46,040 --> 19:16:51,960 which I just created so if I click on 30448 19:16:48,280 --> 19:16:53,360 hello.html within this folder listing 30449 19:16:51,960 --> 19:16:54,680 you'll be a little underwhelmed and I'll 30450 19:16:53,360 --> 19:16:57,872 zoom in just so there's something more 30451 19:16:54,680 --> 19:17:00,192 interesting there but now you see hello 30452 19:16:57,872 --> 19:17:02,320 comma body but what's interesting 30453 19:17:00,192 --> 19:17:05,040 perhaps is that after this long very 30454 19:17:02,320 --> 19:17:06,800 cryptic and uninteresting URL notice at 30455 19:17:05,040 --> 19:17:10,280 the very end of it and I'll zoom in in a 30456 19:17:06,800 --> 19:17:12,640 moment what do you see slh hello.html 30457 19:17:10,280 --> 19:17:15,120 which follows the convention I claimed 30458 19:17:12,640 --> 19:17:17,872 before break is how a browser would 30459 19:17:15,120 --> 19:17:20,400 allow you to to access a specific file 30460 19:17:17,872 --> 19:17:22,360 on a server by doing slash and then the 30461 19:17:20,400 --> 19:17:24,160 name of the file name everything before 30462 19:17:22,360 --> 19:17:26,280 it is very cryptic it would be better if 30463 19:17:24,160 --> 19:17:28,120 I you know uh buy a domain name that's a 30464 19:17:26,280 --> 19:17:29,760 little more easy to remember and set 30465 19:17:28,120 --> 19:17:31,680 that up some other time but for now 30466 19:17:29,760 --> 19:17:34,832 let's just focus on only the file names 30467 19:17:31,680 --> 19:17:37,280 that I'm actually creating all right so 30468 19:17:34,832 --> 19:17:39,400 the code is up and running underwhelming 30469 19:17:37,280 --> 19:17:41,280 though it might be with the body in the 30470 19:17:39,400 --> 19:17:43,760 middle of the page and let me zoom in up 30471 19:17:41,280 --> 19:17:46,120 here too hello comma title is indeed in 30472 19:17:43,760 --> 19:17:48,160 the tab just as promised so what's 30473 19:17:46,120 --> 19:17:49,720 actually going on with code like this 30474 19:17:48,160 --> 19:17:51,720 that we just created well let's go back 30475 19:17:49,720 --> 19:17:53,600 to the slide version of the same and let 30476 19:17:51,720 --> 19:17:55,000 me just highlight a few of these lines 30477 19:17:53,600 --> 19:17:57,512 the very first line is what's called 30478 19:17:55,000 --> 19:17:58,920 your document type declaration doesn't 30479 19:17:57,512 --> 19:18:00,680 really matter to remember that by 30480 19:17:58,920 --> 19:18:02,640 phrasing and this is just something you 30481 19:18:00,680 --> 19:18:04,960 copy paste or do from memory at the top 30482 19:18:02,640 --> 19:18:08,192 of any HTML file that you create when 30483 19:18:04,960 --> 19:18:09,960 making your own web page it's a implicit 30484 19:18:08,192 --> 19:18:12,120 indicator to the browser that you're 30485 19:18:09,960 --> 19:18:13,920 using the very latest version of HTML 30486 19:18:12,120 --> 19:18:15,640 which is version five you don't mention 30487 19:18:13,920 --> 19:18:17,360 the number five just browsers now 30488 19:18:15,640 --> 19:18:19,040 nowadays are program to look for this to 30489 19:18:17,360 --> 19:18:21,160 know that you're using the very latest 30490 19:18:19,040 --> 19:18:23,640 version of the language languages just 30491 19:18:21,160 --> 19:18:25,680 like human languages evolve over time 30492 19:18:23,640 --> 19:18:27,400 we're up to version five of HTML but new 30493 19:18:25,680 --> 19:18:29,192 features get added every few years so 30494 19:18:27,400 --> 19:18:31,360 indeed this lecture this class has been 30495 19:18:29,192 --> 19:18:33,600 evolving over time too so let's now 30496 19:18:31,360 --> 19:18:35,160 focus on the next line as well as the 30497 19:18:33,600 --> 19:18:37,760 bottom line and you'll notice some 30498 19:18:35,160 --> 19:18:39,872 deliberate symmetry here this here is 30499 19:18:37,760 --> 19:18:41,120 what we're going to call a tag and it's 30500 19:18:39,872 --> 19:18:42,600 technically different from this this is 30501 19:18:41,120 --> 19:18:44,192 a document type declaration it's got the 30502 19:18:42,600 --> 19:18:46,680 weird exclamation point that's the only 30503 19:18:44,192 --> 19:18:50,040 anomaly everything else follows pattern 30504 19:18:46,680 --> 19:18:53,720 this is a tag in HTML and it's the HTML 30505 19:18:50,040 --> 19:18:56,960 tag and a tag generally both starts and 30506 19:18:53,720 --> 19:18:59,040 stops or opens and closes at some point 30507 19:18:56,960 --> 19:19:01,232 so this is the so-called start tag or 30508 19:18:59,040 --> 19:19:03,040 open tag and this just means essentially 30509 19:19:01,232 --> 19:19:04,960 to the browser hey browser here comes 30510 19:19:03,040 --> 19:19:07,120 some HTML the language in which web 30511 19:19:04,960 --> 19:19:10,192 pages are written this here with the 30512 19:19:07,120 --> 19:19:12,440 forward slash after the angled bracket 30513 19:19:10,192 --> 19:19:13,832 means hey browser that's it for the HTML 30514 19:19:12,440 --> 19:19:15,720 of this page so that's what I mean by 30515 19:19:13,832 --> 19:19:17,512 symmetry I started a thought here 30516 19:19:15,720 --> 19:19:20,512 finished it down here what's in between 30517 19:19:17,512 --> 19:19:22,360 those two thoughts well here browser or 30518 19:19:20,512 --> 19:19:25,360 rather let me clarify one thing this 30519 19:19:22,360 --> 19:19:27,680 thing here is that other uh keyword an 30520 19:19:25,360 --> 19:19:30,120 attribute an attribute is something that 30521 19:19:27,680 --> 19:19:32,872 modifies the behavior of a tag so it's 30522 19:19:30,120 --> 19:19:34,800 similar to an argument in C or in Python 30523 19:19:32,872 --> 19:19:36,280 like a parameter to a function these 30524 19:19:34,800 --> 19:19:38,400 aren't functions but it's the same idea 30525 19:19:36,280 --> 19:19:40,872 it just modifies the default behavior of 30526 19:19:38,400 --> 19:19:42,360 something instead Lang equals you can 30527 19:19:40,872 --> 19:19:44,680 probably guess just means that hey 30528 19:19:42,360 --> 19:19:46,480 browser assume that everything Hereafter 30529 19:19:44,680 --> 19:19:48,680 is in English and that might be useful 30530 19:19:46,480 --> 19:19:50,360 for like Google translate or just search 30531 19:19:48,680 --> 19:19:52,552 engine optimization so that just the 30532 19:19:50,360 --> 19:19:54,600 server the browser know like what human 30533 19:19:52,552 --> 19:19:56,600 language you have actual content in like 30534 19:19:54,600 --> 19:19:58,320 hello title hello body even though a 30535 19:19:56,600 --> 19:20:00,680 good computer can probably infer from 30536 19:19:58,320 --> 19:20:03,192 Context often all right so that's an 30537 19:20:00,680 --> 19:20:05,232 attribute that's a tag and the whole 30538 19:20:03,192 --> 19:20:07,760 thing here everything in between the 30539 19:20:05,232 --> 19:20:09,832 start tag and end tag we would also call 30540 19:20:07,760 --> 19:20:12,600 an HTML element that just means 30541 19:20:09,832 --> 19:20:15,600 everything related to that open and 30542 19:20:12,600 --> 19:20:18,280 close tag all right now notice indented 30543 19:20:15,600 --> 19:20:20,872 inside of so to speak the HTML open and 30544 19:20:18,280 --> 19:20:23,040 close tag are another pair of tags the 30545 19:20:20,872 --> 19:20:24,920 head tag and the body tag or the head 30546 19:20:23,040 --> 19:20:26,920 element collectively and the body 30547 19:20:24,920 --> 19:20:28,640 element collectively and same idea hey 30548 19:20:26,920 --> 19:20:30,232 browser here comes the head of my page 30549 19:20:28,640 --> 19:20:31,800 hey browser that's it for the head hey 30550 19:20:30,232 --> 19:20:34,080 browser here comes the body of my page 30551 19:20:31,800 --> 19:20:36,160 hey browser that's it for the body the 30552 19:20:34,080 --> 19:20:38,640 head is essentially the tiny little 30553 19:20:36,160 --> 19:20:41,552 strip at the very top including the tab 30554 19:20:38,640 --> 19:20:43,120 itself the body is like 95% of 30555 19:20:41,552 --> 19:20:45,040 everything else the big rectangular 30556 19:20:43,120 --> 19:20:47,480 region what's inside of your title at 30557 19:20:45,040 --> 19:20:49,512 the at of your head of the web page at 30558 19:20:47,480 --> 19:20:51,120 the moment just the title so this 30559 19:20:49,512 --> 19:20:52,920 indicates hey browser Here Comes My 30560 19:20:51,120 --> 19:20:54,920 Title hey browser that's it for the 30561 19:20:52,920 --> 19:20:57,600 title the title of course is literally 30562 19:20:54,920 --> 19:20:59,832 quote unquote hello comma title 30563 19:20:57,600 --> 19:21:03,120 meanwhile if we bounce back out here is 30564 19:20:59,832 --> 19:21:05,232 the uh second element inside of the HTML 30565 19:21:03,120 --> 19:21:06,760 tag uh this says Hey browser here comes 30566 19:21:05,232 --> 19:21:08,480 the body hey browser that's it for the 30567 19:21:06,760 --> 19:21:10,280 body and hey browser this is the 30568 19:21:08,480 --> 19:21:12,640 contents these are the contents of the 30569 19:21:10,280 --> 19:21:14,680 body itself now the indentation is a 30570 19:21:12,640 --> 19:21:17,232 stylistic thing I did it just to be sort 30571 19:21:14,680 --> 19:21:19,440 of neat and en TIY because it suggests 30572 19:21:17,232 --> 19:21:21,960 what is inside of what but it also 30573 19:21:19,440 --> 19:21:23,600 suggests a sort of hierarchy and in fact 30574 19:21:21,960 --> 19:21:26,440 we'll use terminology from like the 30575 19:21:23,600 --> 19:21:29,832 world of family trees if this is like a 30576 19:21:26,440 --> 19:21:33,000 parent so to speak head and body would 30577 19:21:29,832 --> 19:21:35,720 be the child elements of the HTML tag 30578 19:21:33,000 --> 19:21:37,760 meanwhile title is a child of the head 30579 19:21:35,720 --> 19:21:39,872 tag or equivalently tital is a 30580 19:21:37,760 --> 19:21:42,000 grandchild of HTML so you can use the 30581 19:21:39,872 --> 19:21:44,192 same sort of vernacular as in the human 30582 19:21:42,000 --> 19:21:45,872 world when it comes to uh familial 30583 19:21:44,192 --> 19:21:48,872 relationships too and that just H set 30584 19:21:45,872 --> 19:21:52,760 again the same hierarchy so we have tags 30585 19:21:48,872 --> 19:21:55,080 and they include HTML head title body 30586 19:21:52,760 --> 19:21:57,640 and that's it for now we have attributes 30587 19:21:55,080 --> 19:21:59,160 we've seen one example of them Lang but 30588 19:21:57,640 --> 19:22:01,040 we'll see many other examples of the 30589 19:21:59,160 --> 19:22:03,160 same idea but these building blocks are 30590 19:22:01,040 --> 19:22:04,280 exactly the same generally you start a 30591 19:22:03,160 --> 19:22:06,800 thought you finish a thought and you 30592 19:22:04,280 --> 19:22:10,480 might do something in between questions 30593 19:22:06,800 --> 19:22:12,920 on this basic structure of any web 30594 19:22:10,480 --> 19:22:16,232 page any questions at 30595 19:22:12,920 --> 19:22:17,640 all no all right so let's now now allow 30596 19:22:16,232 --> 19:22:19,600 things to ramp up a little more 30597 19:22:17,640 --> 19:22:21,400 interestingly and do something with 30598 19:22:19,600 --> 19:22:22,960 these building blocks but so that you 30599 19:22:21,400 --> 19:22:25,080 have a mental model for everything 30600 19:22:22,960 --> 19:22:28,232 that's going on here on after think of 30601 19:22:25,080 --> 19:22:29,872 this same HTML being related in spirit 30602 19:22:28,232 --> 19:22:32,280 to week five when we talked all about 30603 19:22:29,872 --> 19:22:33,640 data structures if I really wanted to I 30604 19:22:32,280 --> 19:22:36,280 could take to heart this idea of 30605 19:22:33,640 --> 19:22:38,280 children and parents and grandparents 30606 19:22:36,280 --> 19:22:40,680 and really depict this thing graphically 30607 19:22:38,280 --> 19:22:42,360 and in fact this tree here if you will 30608 19:22:40,680 --> 19:22:44,960 and it's not a binary tree it's not a 30609 19:22:42,360 --> 19:22:47,360 binary search tree it's just a tree uh 30610 19:22:44,960 --> 19:22:49,192 used week five's terminology if this 30611 19:22:47,360 --> 19:22:51,512 special node here represents the whole 30612 19:22:49,192 --> 19:22:54,552 document well the root element as I 30613 19:22:51,512 --> 19:22:57,040 called it is HTML HTML has two children 30614 19:22:54,552 --> 19:23:00,320 head and body the head tag has in turn a 30615 19:22:57,040 --> 19:23:02,512 title uh child and in turn has some text 30616 19:23:00,320 --> 19:23:04,440 just as the body has some text and so 30617 19:23:02,512 --> 19:23:06,960 this is what your browser is doing you 30618 19:23:04,440 --> 19:23:09,000 and I the programmers write this stuff 30619 19:23:06,960 --> 19:23:10,400 the browser reads this code top to 30620 19:23:09,000 --> 19:23:12,800 bottom left to right whenever you visit 30621 19:23:10,400 --> 19:23:15,160 a website and inside of the computer's 30622 19:23:12,800 --> 19:23:17,760 memory Chrome Edge Firefox Safari what 30623 19:23:15,160 --> 19:23:20,232 what not they build this data structure 30624 19:23:17,760 --> 19:23:22,280 in the computer's memory so as to know 30625 19:23:20,232 --> 19:23:24,280 what it is you have told them to do and 30626 19:23:22,280 --> 19:23:25,960 we'll see over time at the end of today 30627 19:23:24,280 --> 19:23:28,232 you can write code in an actual 30628 19:23:25,960 --> 19:23:30,192 programming language JavaScript to maybe 30629 19:23:28,232 --> 19:23:32,080 dynamically add or remove things from 30630 19:23:30,192 --> 19:23:33,920 this tree and this is how things like 30631 19:23:32,080 --> 19:23:36,120 Gmail work when you open up your Gmail 30632 19:23:33,920 --> 19:23:37,512 inbox if you're a Gmail user if you just 30633 19:23:36,120 --> 19:23:39,600 stay there long enough you'll probably 30634 19:23:37,512 --> 19:23:41,440 get more and more mail and what happens 30635 19:23:39,600 --> 19:23:43,960 you don't have to like reload the page 30636 19:23:41,440 --> 19:23:45,600 or rebuild the tree per se it just all 30637 19:23:43,960 --> 19:23:47,832 of a sudden appears at the top 30638 19:23:45,600 --> 19:23:49,832 at the top at the top what's happening 30639 19:23:47,832 --> 19:23:52,120 there is that Google wrote some code 30640 19:23:49,832 --> 19:23:54,040 that just keeps adding more nodes to 30641 19:23:52,120 --> 19:23:55,760 this tree every time they realize you've 30642 19:23:54,040 --> 19:23:58,280 got a new message again and again so 30643 19:23:55,760 --> 19:24:00,760 that's the relationship now even with 30644 19:23:58,280 --> 19:24:03,360 this world of HTML with all of the 30645 19:24:00,760 --> 19:24:05,320 programmatic ideas we looked at in the 30646 19:24:03,360 --> 19:24:07,120 past all right so let's go ahead and do 30647 19:24:05,320 --> 19:24:09,120 something with this that's a little more 30648 19:24:07,120 --> 19:24:10,600 interesting than just hello itself I'm 30649 19:24:09,120 --> 19:24:12,760 going to go ahead and hide my terminal 30650 19:24:10,600 --> 19:24:14,440 window because the server is now running 30651 19:24:12,760 --> 19:24:17,400 and all I want to do now is experiment 30652 19:24:14,440 --> 19:24:19,720 with uh hello and other examples as well 30653 19:24:17,400 --> 19:24:23,640 let me go ahead and actually before I do 30654 19:24:19,720 --> 19:24:25,720 that let me go ahead and run uh code of 30655 19:24:23,640 --> 19:24:27,280 paragraphs. HTML just so I can keep my 30656 19:24:25,720 --> 19:24:29,960 code separate and now I'll hide the 30657 19:24:27,280 --> 19:24:32,400 terminal window again um paragraphs. 30658 19:24:29,960 --> 19:24:34,872 HTML I'm going to do almost exactly the 30659 19:24:32,400 --> 19:24:37,000 same let me go ahead and start with 30660 19:24:34,872 --> 19:24:38,720 something familiar and eventually I'll 30661 19:24:37,000 --> 19:24:42,080 start copying and pasting just to save 30662 19:24:38,720 --> 19:24:44,360 time so doc type HTML is always there 30663 19:24:42,080 --> 19:24:46,232 open the HTML tag and now notice I 30664 19:24:44,360 --> 19:24:48,600 didn't type the rest of that just like 30665 19:24:46,232 --> 19:24:50,320 with C just like with python we try to 30666 19:24:48,600 --> 19:24:52,760 save you some keystrokes by closing 30667 19:24:50,320 --> 19:24:54,512 parentheses adding quotes the HTML 30668 19:24:52,760 --> 19:24:56,160 support in VSS code is pretty good too 30669 19:24:54,512 --> 19:24:58,552 and it tries to finish your thought when 30670 19:24:56,160 --> 19:25:00,160 it comes to tags as well it can screw 30671 19:24:58,552 --> 19:25:01,552 things up if you if it does something 30672 19:25:00,160 --> 19:25:02,680 you don't want it to do so sometimes you 30673 19:25:01,552 --> 19:25:05,040 have to delete but it's just 30674 19:25:02,680 --> 19:25:06,872 autocomplete as we've seen before uh 30675 19:25:05,040 --> 19:25:09,360 let's go ahead and let me add Lang 30676 19:25:06,872 --> 19:25:12,400 equals as all of my examples today will 30677 19:25:09,360 --> 19:25:15,080 be let's add the head tag let's go and 30678 19:25:12,400 --> 19:25:17,680 proactively add the body tag and now 30679 19:25:15,080 --> 19:25:19,800 let's go ahead and give this a title tag 30680 19:25:17,680 --> 19:25:21,480 uh which has a I'll just call this 30681 19:25:19,800 --> 19:25:23,640 paragraphs just so I remember which 30682 19:25:21,480 --> 19:25:25,080 example is what now notice all of this 30683 19:25:23,640 --> 19:25:27,512 white space and all of this neat and 30684 19:25:25,080 --> 19:25:29,000 tidy indentation the browser ultimately 30685 19:25:27,512 --> 19:25:31,040 is not going to care about this is just 30686 19:25:29,000 --> 19:25:32,552 for us humans to kind of keep ourselves 30687 19:25:31,040 --> 19:25:34,552 saying when we look at the code it's 30688 19:25:32,552 --> 19:25:37,440 just easier to read but strictly 30689 19:25:34,552 --> 19:25:39,600 speaking I could minimally delete all of 30690 19:25:37,440 --> 19:25:42,232 this white space and I could just move 30691 19:25:39,600 --> 19:25:43,680 all of this tag up to the same line both 30692 19:25:42,232 --> 19:25:45,480 I think are fine I'll just going to 30693 19:25:43,680 --> 19:25:47,600 follow a certain convention but this too 30694 19:25:45,480 --> 19:25:49,360 would have the exact same meaning but 30695 19:25:47,600 --> 19:25:50,832 we'll see where that detail about whites 30696 19:25:49,360 --> 19:25:53,320 space could potentially get us into 30697 19:25:50,832 --> 19:25:56,160 trouble later in my paragraphs tag let's 30698 19:25:53,320 --> 19:25:58,320 do this in advance I've written up some 30699 19:25:56,160 --> 19:26:00,000 Latin like text a really long paragraph 30700 19:25:58,320 --> 19:26:01,552 of Latin like text like this it's 30701 19:26:00,000 --> 19:26:02,920 actually random nonsense it's not real 30702 19:26:01,552 --> 19:26:05,600 Latin even though a couple of the words 30703 19:26:02,920 --> 19:26:07,400 might look familiar and so here we have 30704 19:26:05,600 --> 19:26:09,640 three paragraphs of text and I've 30705 19:26:07,400 --> 19:26:11,320 deliberately hit enter in between them 30706 19:26:09,640 --> 19:26:12,760 so that just like an essay in Google 30707 19:26:11,320 --> 19:26:15,080 Docs or Microsoft Word hopefully I'll 30708 19:26:12,760 --> 19:26:17,640 see three separate paragraphs let me now 30709 19:26:15,080 --> 19:26:20,120 change tabs and I'll close hello.html 30710 19:26:17,640 --> 19:26:22,360 from before I'm going to go back to my 30711 19:26:20,120 --> 19:26:25,040 other tab here I'm going to click back 30712 19:26:22,360 --> 19:26:27,640 to go back to that index of all of my 30713 19:26:25,040 --> 19:26:29,000 files which I started at earlier and 30714 19:26:27,640 --> 19:26:30,232 you'll see now that I have two files 30715 19:26:29,000 --> 19:26:32,720 because I obviously just created a 30716 19:26:30,232 --> 19:26:34,280 second file called paragraphs. HTML so 30717 19:26:32,720 --> 19:26:37,960 let's click on this to see our three 30718 19:26:34,280 --> 19:26:41,280 paragraphs of Latin like text and voila 30719 19:26:37,960 --> 19:26:43,512 I'll zoom out all right first bug if you 30720 19:26:41,280 --> 19:26:47,600 will this just looks like one massive 30721 19:26:43,512 --> 19:26:50,280 blob of text not three Blobs of text and 30722 19:26:47,600 --> 19:26:51,960 why might that be borrowing the the hint 30723 19:26:50,280 --> 19:26:55,640 I offered a moment 30724 19:26:51,960 --> 19:26:57,552 ago why are we not seeing break yeah so 30725 19:26:55,640 --> 19:26:58,920 we need some kind of line breaks here 30726 19:26:57,552 --> 19:27:00,232 because the browser turns out is only 30727 19:26:58,920 --> 19:27:01,680 going to take us literally and if you 30728 19:27:00,232 --> 19:27:03,760 just give it text text text it's just 30729 19:27:01,680 --> 19:27:05,832 going to show you text and anytime 30730 19:27:03,760 --> 19:27:08,960 there's more than a single white space 30731 19:27:05,832 --> 19:27:10,320 whether it's two or 20 or 200 it's going 30732 19:27:08,960 --> 19:27:11,800 to just assume that you did that just to 30733 19:27:10,320 --> 19:27:13,872 be neat and tidy and it's going to 30734 19:27:11,800 --> 19:27:16,120 collapse them into just one space 30735 19:27:13,872 --> 19:27:19,040 visually like this so there are in fact 30736 19:27:16,120 --> 19:27:20,872 a couple of solutions one is this here I 30737 19:27:19,040 --> 19:27:23,832 could add some explicit line breaks and 30738 19:27:20,872 --> 19:27:26,040 it turns out that there's a br tag like 30739 19:27:23,832 --> 19:27:27,872 this and just for uh visibility sake let 30740 19:27:26,040 --> 19:27:30,440 me do two of them so like hitting enter 30741 19:27:27,872 --> 19:27:32,960 enter on my keyboard I'll do it here too 30742 19:27:30,440 --> 19:27:35,120 BR for break break and now let me go 30743 19:27:32,960 --> 19:27:37,080 back to my other tab nothing's changed 30744 19:27:35,120 --> 19:27:38,872 yet but that's because I have to reload 30745 19:27:37,080 --> 19:27:40,320 I've changed it on the server but now I 30746 19:27:38,872 --> 19:27:42,160 need to change it in the browser by 30747 19:27:40,320 --> 19:27:44,480 reloading and now it looks a little 30748 19:27:42,160 --> 19:27:48,040 better albeit nonsensical but you'll 30749 19:27:44,480 --> 19:27:49,800 know is a curiosity per this BR tag this 30750 19:27:48,040 --> 19:27:51,832 is kind of poorly designed it's a little 30751 19:27:49,800 --> 19:27:54,920 hackish to just say enter enter and make 30752 19:27:51,832 --> 19:27:57,800 the browser do this breaks line breaks 30753 19:27:54,920 --> 19:28:00,552 don't actually require Clos tags or end 30754 19:27:57,800 --> 19:28:02,120 tags so not all tags need to be closed 30755 19:28:00,552 --> 19:28:03,760 at least those that it just makes no 30756 19:28:02,120 --> 19:28:06,232 semantic sense to close them right like 30757 19:28:03,760 --> 19:28:08,192 the break is there or it's not you can't 30758 19:28:06,232 --> 19:28:09,680 imagine like starting to move to the 30759 19:28:08,192 --> 19:28:11,080 next line and then eventually getting 30760 19:28:09,680 --> 19:28:13,480 around to finishing like it's either 30761 19:28:11,080 --> 19:28:15,920 there or it's not so some tags do not 30762 19:28:13,480 --> 19:28:18,120 have closed tags as necessary but 30763 19:28:15,920 --> 19:28:20,320 there's a more elegant way here I dare 30764 19:28:18,120 --> 19:28:22,160 say not just sort of hackish putting in 30765 19:28:20,320 --> 19:28:24,600 these line breaks let me do this instead 30766 19:28:22,160 --> 19:28:26,872 I'll delete those and let me go ahead 30767 19:28:24,600 --> 19:28:29,400 and as the name of this file suggests 30768 19:28:26,872 --> 19:28:31,232 let me add a paragraph tag now here I 30769 19:28:29,400 --> 19:28:32,320 need to fight with vs codes autocomplete 30770 19:28:31,232 --> 19:28:34,480 because I don't want to finish the 30771 19:28:32,320 --> 19:28:36,552 sentence uh the thought there let me go 30772 19:28:34,480 --> 19:28:38,232 ahead and open the paragraph tag and 30773 19:28:36,552 --> 19:28:40,832 close a paragraph tag and just to keep 30774 19:28:38,232 --> 19:28:42,192 things tidy I'll go ahead and indent two 30775 19:28:40,832 --> 19:28:43,960 even though the indentation itself 30776 19:28:42,192 --> 19:28:46,320 doesn't matter let me go ahead and 30777 19:28:43,960 --> 19:28:49,400 create another tag for opening this 30778 19:28:46,320 --> 19:28:51,040 paragraph and I'll close this one here 30779 19:28:49,400 --> 19:28:52,512 and now let me see sometimes it's 30780 19:28:51,040 --> 19:28:53,960 fighting with my auto complete but 30781 19:28:52,512 --> 19:28:56,720 that's fine because I did this sort of 30782 19:28:53,960 --> 19:28:59,872 the wrong way at first and now let me go 30783 19:28:56,720 --> 19:29:01,872 ahead and finish this thought by closing 30784 19:28:59,872 --> 19:29:04,480 this paragraph tag here and I've 30785 19:29:01,872 --> 19:29:07,000 manually fixed all of my indentation so 30786 19:29:04,480 --> 19:29:09,040 now on line 10 I have the equivalent of 30787 19:29:07,000 --> 19:29:11,160 hey browser start a paragraph and then 30788 19:29:09,040 --> 19:29:12,920 it does the Latin like text then on line 30789 19:29:11,160 --> 19:29:15,640 12 hey browser that's it for this 30790 19:29:12,920 --> 19:29:19,360 paragraph and repeat repeat repeat if I 30791 19:29:15,640 --> 19:29:21,720 now go back to my other Tab and reload 30792 19:29:19,360 --> 19:29:23,720 again shouldn't be all that different 30793 19:29:21,720 --> 19:29:25,552 but semantically it's a little bit 30794 19:29:23,720 --> 19:29:27,600 better why because just saying break 30795 19:29:25,552 --> 19:29:30,360 break doesn't really mean anything but 30796 19:29:27,600 --> 19:29:31,832 by saying paragraph paragraph paragraph 30797 19:29:30,360 --> 19:29:33,960 now there's some more semantic 30798 19:29:31,832 --> 19:29:36,280 information there now if like Google is 30799 19:29:33,960 --> 19:29:37,760 analyzing your page or if the programmer 30800 19:29:36,280 --> 19:29:39,600 is trying to understand what it is you 30801 19:29:37,760 --> 19:29:41,440 did in the past when writing this code 30802 19:29:39,600 --> 19:29:42,960 you just know semantically oh this is a 30803 19:29:41,440 --> 19:29:44,832 paragraph This is a paragraph This is a 30804 19:29:42,960 --> 19:29:46,512 paragraph just like in a book or or an 30805 19:29:44,832 --> 19:29:49,040 essay so it's a little more clear 30806 19:29:46,512 --> 19:29:52,640 focusing more on what it is not how you 30807 19:29:49,040 --> 19:29:54,832 want to display it any questions then on 30808 19:29:52,640 --> 19:29:57,040 these 30809 19:29:54,832 --> 19:29:58,600 paragraphs now all right so a few more 30810 19:29:57,040 --> 19:30:00,360 tags and indeed these first few examples 30811 19:29:58,600 --> 19:30:01,920 will really just be like sort of bang 30812 19:30:00,360 --> 19:30:03,512 bang bang just a bunch of different 30813 19:30:01,920 --> 19:30:05,000 vocabulary words in the form of these 30814 19:30:03,512 --> 19:30:06,760 new tags but we won't go through the 30815 19:30:05,000 --> 19:30:08,000 entire laundry list of tags this is 30816 19:30:06,760 --> 19:30:09,360 indeed the thing for which web 30817 19:30:08,000 --> 19:30:10,920 references and books and the like are 30818 19:30:09,360 --> 19:30:12,512 ultimately helpful just like a 30819 19:30:10,920 --> 19:30:13,832 dictionary in the real world so I'm 30820 19:30:12,512 --> 19:30:16,000 going to go ahead and do this let me go 30821 19:30:13,832 --> 19:30:18,720 ahead and copy this let me create a new 30822 19:30:16,000 --> 19:30:20,832 file called headings. HTML just so we 30823 19:30:18,720 --> 19:30:22,320 have a new file for this to save time 30824 19:30:20,832 --> 19:30:24,120 I'm just going to paste that exact same 30825 19:30:22,320 --> 19:30:25,960 code just to get me started I'm going to 30826 19:30:24,120 --> 19:30:28,320 change the title of it for clarity for 30827 19:30:25,960 --> 19:30:30,832 the code online to headings and now just 30828 19:30:28,320 --> 19:30:32,832 like a book or an essay or a thesis let 30829 19:30:30,832 --> 19:30:34,960 me actually put some actual headings 30830 19:30:32,832 --> 19:30:37,280 here now if my first heading like 30831 19:30:34,960 --> 19:30:39,080 chapter one I could do something like 30832 19:30:37,280 --> 19:30:40,640 this up here I could have a paragraph 30833 19:30:39,080 --> 19:30:43,080 like I just learned and I could say 30834 19:30:40,640 --> 19:30:44,800 something like chapter one here but 30835 19:30:43,080 --> 19:30:46,680 that's not really a paragraph and so 30836 19:30:44,800 --> 19:30:48,512 it's sort of better designed to tell the 30837 19:30:46,680 --> 19:30:50,320 browser and really tell the world what 30838 19:30:48,512 --> 19:30:53,192 it is so it turns out there's another 30839 19:30:50,320 --> 19:30:55,480 tag I can use like H1 for heading and 30840 19:30:53,192 --> 19:30:56,600 like most important heading and in here 30841 19:30:55,480 --> 19:30:58,232 I'm just going to keep it simple and I'm 30842 19:30:56,600 --> 19:30:59,720 going to say something like one and in 30843 19:30:58,232 --> 19:31:01,232 fact this is so short here's a good 30844 19:30:59,720 --> 19:31:03,512 candidate for just keeping this all on 30845 19:31:01,232 --> 19:31:05,120 the same line but this has no functional 30846 19:31:03,512 --> 19:31:07,360 difference but it'll just make it a 30847 19:31:05,120 --> 19:31:09,720 little tur more tur on the screen now 30848 19:31:07,360 --> 19:31:12,800 let me go ahead down here and I could 30849 19:31:09,720 --> 19:31:14,960 have multiple headings so H1 2 and down 30850 19:31:12,800 --> 19:31:18,360 here I could have another one H1 30851 19:31:14,960 --> 19:31:20,320 three and if I go back to my other tab I 30852 19:31:18,360 --> 19:31:23,552 reload it now we should see just like a 30853 19:31:20,320 --> 19:31:26,000 book or an essay now we have some 30854 19:31:23,552 --> 19:31:27,872 proper now we have some oops I have to 30855 19:31:26,000 --> 19:31:30,232 go to the right file sorry if I go back 30856 19:31:27,872 --> 19:31:32,680 to the index now we see the new third 30857 19:31:30,232 --> 19:31:35,192 file called headings. HTML and now we 30858 19:31:32,680 --> 19:31:37,120 indeed see some fairly pretty if simple 30859 19:31:35,192 --> 19:31:38,960 headings as well now if these aren't 30860 19:31:37,120 --> 19:31:40,680 three chapters 1 two three but maybe 30861 19:31:38,960 --> 19:31:42,280 it's a chapter then a section then a 30862 19:31:40,680 --> 19:31:44,800 subsection such that just visually you 30863 19:31:42,280 --> 19:31:46,400 want things to get smaller and smaller 30864 19:31:44,800 --> 19:31:49,080 well those exist too and in fact you can 30865 19:31:46,400 --> 19:31:51,192 do H1 through H6 H1 a little 30866 19:31:49,080 --> 19:31:54,160 paradoxically is the biggest and boldest 30867 19:31:51,192 --> 19:31:56,960 H6 is the smallest but still bold so it 30868 19:31:54,160 --> 19:31:59,400 might make sense to make this H2 both 30869 19:31:56,960 --> 19:32:01,720 open and close and maybe this H3 open 30870 19:31:59,400 --> 19:32:04,320 and close if again this is a section or 30871 19:32:01,720 --> 19:32:06,512 a subsection inside of that chapter if I 30872 19:32:04,320 --> 19:32:08,552 reload now notice just gets a little 30873 19:32:06,512 --> 19:32:11,160 smaller so it's more similar to what 30874 19:32:08,552 --> 19:32:13,320 you'd see on the printed page but this 30875 19:32:11,160 --> 19:32:15,800 now is just another three tags that I 30876 19:32:13,320 --> 19:32:17,720 might use in my own code all right well 30877 19:32:15,800 --> 19:32:19,640 how about lists of things I have three 30878 19:32:17,720 --> 19:32:21,960 paragraphs here but let's do this let me 30879 19:32:19,640 --> 19:32:23,872 go back to vs code I'm going to copy 30880 19:32:21,960 --> 19:32:26,320 this code so I have a starting point I'm 30881 19:32:23,872 --> 19:32:29,720 going to create a new file called say 30882 19:32:26,320 --> 19:32:31,640 list. HTML here I'm going to copy paste 30883 19:32:29,720 --> 19:32:33,552 I'm going to change my title to be list 30884 19:32:31,640 --> 19:32:34,760 just for clarity and in here I'm going 30885 19:32:33,552 --> 19:32:36,400 to go ahead and get rid of this whole 30886 19:32:34,760 --> 19:32:38,160 body because let's move away from these 30887 19:32:36,400 --> 19:32:40,360 massive paragraphs and keep it simpler 30888 19:32:38,160 --> 19:32:42,640 for now if I want to have a list of 30889 19:32:40,360 --> 19:32:44,360 things uh for instance uh if you haven't 30890 19:32:42,640 --> 19:32:45,400 seen these already a computer scientist 30891 19:32:44,360 --> 19:32:47,600 when they're fishing for just some 30892 19:32:45,400 --> 19:32:50,232 arbitrary meaningless words uh they 30893 19:32:47,600 --> 19:32:51,720 often use Foo bar and baz just as their 30894 19:32:50,232 --> 19:32:54,760 go-to just like a mathematician might 30895 19:32:51,720 --> 19:32:56,480 use XY Z for variables so Fu bar and baz 30896 19:32:54,760 --> 19:32:57,800 are on three separate lines and maybe 30897 19:32:56,480 --> 19:32:59,640 this is like my to-do list or my 30898 19:32:57,800 --> 19:33:02,400 shopping list but you can probably 30899 19:32:59,640 --> 19:33:04,720 imagine if I go back to my other tab go 30900 19:33:02,400 --> 19:33:05,800 back to the index I now see my new file 30901 19:33:04,720 --> 19:33:08,480 list. 30902 19:33:05,800 --> 19:33:10,160 HTML but it's probably going to look 30903 19:33:08,480 --> 19:33:13,160 wrong I think I'm just going to say yeah 30904 19:33:10,160 --> 19:33:15,360 Fubar baz all in one breath if you will 30905 19:33:13,160 --> 19:33:17,080 the not on new line and you can try to 30906 19:33:15,360 --> 19:33:19,552 fight this like you can be like really 30907 19:33:17,080 --> 19:33:21,680 want to put some line breaks there go 30908 19:33:19,552 --> 19:33:24,512 back and reload it's still not going to 30909 19:33:21,680 --> 19:33:25,920 make any change how do I want to fix 30910 19:33:24,512 --> 19:33:27,080 this well I can do this in a few ways I 30911 19:33:25,920 --> 19:33:29,320 could make them paragraphs but they're 30912 19:33:27,080 --> 19:33:31,552 not really paragraphs they're a list so 30913 19:33:29,320 --> 19:33:33,232 I'm going to use a different tag instead 30914 19:33:31,552 --> 19:33:36,760 I'm going to create for instance an 30915 19:33:33,232 --> 19:33:38,760 unordered list using the UL tag open and 30916 19:33:36,760 --> 19:33:41,120 close inside of that I'm going to use 30917 19:33:38,760 --> 19:33:43,440 the list item tag Li I and I'm going to 30918 19:33:41,120 --> 19:33:46,000 say Foo inside of another tag I'm going 30919 19:33:43,440 --> 19:33:48,000 to say bar inside of a third tag open 30920 19:33:46,000 --> 19:33:49,760 and close I'm going to say baz so it's 30921 19:33:48,000 --> 19:33:52,232 getting a little verbose but it's still 30922 19:33:49,760 --> 19:33:54,832 relatively succinct Li is all you need 30923 19:33:52,232 --> 19:33:57,360 type for list item UL is all you need 30924 19:33:54,832 --> 19:33:59,760 type for unordered list so there's some 30925 19:33:57,360 --> 19:34:01,320 shorthand syntax here that's adopted if 30926 19:33:59,760 --> 19:34:04,120 I now reload you're going to see a 30927 19:34:01,320 --> 19:34:05,872 so-called unordered list like not sorted 30928 19:34:04,120 --> 19:34:08,040 which means by convention to show it as 30929 19:34:05,872 --> 19:34:10,480 bullets though it could be displayed in 30930 19:34:08,040 --> 19:34:11,512 different ways visually as well if you 30931 19:34:10,480 --> 19:34:13,040 actually change your mind and you 30932 19:34:11,512 --> 19:34:14,640 realize oh I'd really like to number 30933 19:34:13,040 --> 19:34:17,872 this well you could obviously like just 30934 19:34:14,640 --> 19:34:19,120 add one and two and three but that's 30935 19:34:17,872 --> 19:34:20,720 going to get annoying especially if the 30936 19:34:19,120 --> 19:34:22,040 list grows you want to change something 30937 19:34:20,720 --> 19:34:24,680 insert something in the middle then you 30938 19:34:22,040 --> 19:34:26,832 have to reorder it I mean we using 30939 19:34:24,680 --> 19:34:29,720 computers here they can do this for us 30940 19:34:26,832 --> 19:34:33,120 so we can change the UL to any 30941 19:34:29,720 --> 19:34:34,800 guesses o maybe for ordered list which 30942 19:34:33,120 --> 19:34:37,280 is sort of the opposite here so let's 30943 19:34:34,800 --> 19:34:39,160 try changing that to O let me go back to 30944 19:34:37,280 --> 19:34:42,000 my browser and I'm just hitting command 30945 19:34:39,160 --> 19:34:44,192 r or controlr to reload the page instead 30946 19:34:42,000 --> 19:34:46,872 of clicking the button every time now I 30947 19:34:44,192 --> 19:34:48,720 automatically get 1 2 and three and you 30948 19:34:46,872 --> 19:34:50,320 can even override the Aesthetics using 30949 19:34:48,720 --> 19:34:52,480 different numera bles or symbology 30950 19:34:50,320 --> 19:34:55,440 instead but that would be perhaps the 30951 19:34:52,480 --> 19:34:58,320 most common there as well all right it's 30952 19:34:55,440 --> 19:35:02,000 a lot of tags quickly but any questions 30953 19:34:58,320 --> 19:35:03,680 on lists paragraphs headings or the like 30954 19:35:02,000 --> 19:35:07,160 no all right so let me go ahead and 30955 19:35:03,680 --> 19:35:09,600 propose this here um let's go ahead and 30956 19:35:07,160 --> 19:35:12,192 create what we'll call a table so let me 30957 19:35:09,600 --> 19:35:16,080 copy and paste this into a new file code 30958 19:35:12,192 --> 19:35:18,192 of table. HTML and in table. HTML let me 30959 19:35:16,080 --> 19:35:20,872 again rename the title to table let me 30960 19:35:18,192 --> 19:35:23,400 get rid of that ordered list from before 30961 19:35:20,872 --> 19:35:25,512 and let me now use a table tag open and 30962 19:35:23,400 --> 19:35:28,400 close this one's a little weird but 30963 19:35:25,512 --> 19:35:31,480 inside of a table you typically have a 30964 19:35:28,400 --> 19:35:33,192 head of the table so uh I'll say well 30965 19:35:31,480 --> 19:35:36,832 let's say the first row we'll keep this 30966 19:35:33,192 --> 19:35:39,600 one simple a table row or TR inside of a 30967 19:35:36,832 --> 19:35:41,192 table row you would ideally have columns 30968 19:35:39,600 --> 19:35:43,800 but that's not the nomenclature instead 30969 19:35:41,192 --> 19:35:45,232 you have data so TD for table data and 30970 19:35:43,800 --> 19:35:46,960 let me go ahead and just have the first 30971 19:35:45,232 --> 19:35:48,800 datm be one I'm just going to 30972 19:35:46,960 --> 19:35:50,480 arbitrarily do 1 two three just so we 30973 19:35:48,800 --> 19:35:52,920 have something to play with and you know 30974 19:35:50,480 --> 19:35:54,480 what just for demonstration sake I am 30975 19:35:52,920 --> 19:35:56,232 going to deliberately copy paste this 30976 19:35:54,480 --> 19:35:58,760 twice and I'm just going to manually 30977 19:35:56,232 --> 19:36:02,552 change the numbers just so we can see 30978 19:35:58,760 --> 19:36:04,320 what I'm creating 789 and then maybe 30979 19:36:02,552 --> 19:36:06,192 just for good measure if you're seeing 30980 19:36:04,320 --> 19:36:08,720 where this is going let me copy this one 30981 19:36:06,192 --> 19:36:11,872 more time and give myself a final row 30982 19:36:08,720 --> 19:36:13,800 with an asterisk a zero and a pound sign 30983 19:36:11,872 --> 19:36:16,192 if maybe you see where this is going let 30984 19:36:13,800 --> 19:36:18,680 me go back to my other tab let me go 30985 19:36:16,192 --> 19:36:21,400 back to the index there's my new file 30986 19:36:18,680 --> 19:36:24,280 table. HTML I'll click that and while 30987 19:36:21,400 --> 19:36:27,120 it's not very pretty I'll zoom in it's 30988 19:36:24,280 --> 19:36:29,720 indeed a table of data I happen to mimic 30989 19:36:27,120 --> 19:36:31,232 like a key uh a telephone keypad but you 30990 19:36:29,720 --> 19:36:32,640 could imagine this being much juicier 30991 19:36:31,232 --> 19:36:34,440 much more interesting scientific or 30992 19:36:32,640 --> 19:36:37,720 financial data or the like laid out into 30993 19:36:34,440 --> 19:36:39,872 these rows TRS and these columns AKA 30994 19:36:37,720 --> 19:36:42,400 table data as well so we have that 30995 19:36:39,872 --> 19:36:44,680 ability as well for structured data now 30996 19:36:42,400 --> 19:36:46,440 of course the internet um has lots of 30997 19:36:44,680 --> 19:36:49,000 images on it and in fact this is all 30998 19:36:46,440 --> 19:36:52,360 just text how can we introduce images 30999 19:36:49,000 --> 19:36:55,600 well let me go ahead and do this let me 31000 19:36:52,360 --> 19:36:57,920 first uh sort of semi secretly copy an 31001 19:36:55,600 --> 19:36:59,552 image file that I brought from uh 31002 19:36:57,920 --> 19:37:02,080 earlier just so we have something to 31003 19:36:59,552 --> 19:37:04,920 play with and I have in my account here 31004 19:37:02,080 --> 19:37:06,680 now an image called harbor. JPEG and I 31005 19:37:04,920 --> 19:37:08,320 uploaded this semi- secretly a second 31006 19:37:06,680 --> 19:37:10,920 ago into my account so that I can 31007 19:37:08,320 --> 19:37:13,120 reference a second file let me go ahead 31008 19:37:10,920 --> 19:37:14,440 and copy this HTML just to save myself 31009 19:37:13,120 --> 19:37:17,232 some keystrokes 31010 19:37:14,440 --> 19:37:19,920 let me go ahead and do code of image. 31011 19:37:17,232 --> 19:37:21,600 HTML and let me paste that code and hide 31012 19:37:19,920 --> 19:37:23,680 my terminal window I'm going to get rid 31013 19:37:21,600 --> 19:37:25,480 of all of this table as just 31014 19:37:23,680 --> 19:37:28,280 uninteresting now we're going to make 31015 19:37:25,480 --> 19:37:30,120 this even simpler by changing my title 31016 19:37:28,280 --> 19:37:33,040 to image to keep all these 31017 19:37:30,120 --> 19:37:35,080 demonstrations uh separate and now if I 31018 19:37:33,040 --> 19:37:37,440 want to make a web page that when 31019 19:37:35,080 --> 19:37:39,400 visited shows us a picture of Harvard 31020 19:37:37,440 --> 19:37:42,480 well there's an image tag abbreviated 31021 19:37:39,400 --> 19:37:45,232 IMG for short I can specify what the 31022 19:37:42,480 --> 19:37:47,552 source of that image is and if my file a 31023 19:37:45,232 --> 19:37:50,400 JPEG in this case is literally in the 31024 19:37:47,552 --> 19:37:52,480 same folder I can just say quote unquote 31025 19:37:50,400 --> 19:37:53,760 harvard. jpeg if it's in a folder I 31026 19:37:52,480 --> 19:37:55,872 should mention the folder name in a 31027 19:37:53,760 --> 19:37:57,600 slash or something like that if the 31028 19:37:55,872 --> 19:38:00,192 image is on the internet somewhere with 31029 19:37:57,600 --> 19:38:03,000 a URL I could also have a whole URL 31030 19:38:00,192 --> 19:38:05,760 https colon and then the URL of the 31031 19:38:03,000 --> 19:38:07,120 image but I upload it in in advance now 31032 19:38:05,760 --> 19:38:08,872 this is just going to visually display 31033 19:38:07,120 --> 19:38:10,960 on the screen but not everyone of course 31034 19:38:08,872 --> 19:38:12,960 can see images screen readers might need 31035 19:38:10,960 --> 19:38:14,832 a bit of assistance and even search 31036 19:38:12,960 --> 19:38:17,160 engines might want to analyze the page 31037 19:38:14,832 --> 19:38:18,600 and know what this is an image of now 31038 19:38:17,160 --> 19:38:20,192 machine learning and artificial 31039 19:38:18,600 --> 19:38:22,160 intelligence are maybe getting better to 31040 19:38:20,192 --> 19:38:23,720 be fair at figuring out just by 31041 19:38:22,160 --> 19:38:25,552 analyzing images what they are but 31042 19:38:23,720 --> 19:38:27,400 they're certainly imperfect I am a human 31043 19:38:25,552 --> 19:38:29,480 I know pretty well what I took a photo 31044 19:38:27,400 --> 19:38:31,160 of for instance so maybe what I should 31045 19:38:29,480 --> 19:38:33,640 do proactively which would be good for 31046 19:38:31,160 --> 19:38:35,920 accessibility is have this alt tag for 31047 19:38:33,640 --> 19:38:37,920 alternative text and then literally say 31048 19:38:35,920 --> 19:38:40,600 like Harvard University so that someone 31049 19:38:37,920 --> 19:38:42,280 who can't see or so that a a server can 31050 19:38:40,600 --> 19:38:43,960 actually know with higher probability 31051 19:38:42,280 --> 19:38:45,760 what it is uh they're looking at and I 31052 19:38:43,960 --> 19:38:48,080 could be even more detailed than just a 31053 19:38:45,760 --> 19:38:50,680 phrase I could describe the image as 31054 19:38:48,080 --> 19:38:53,920 well all right let me go back to my 31055 19:38:50,680 --> 19:38:56,400 index in the second tab let me go back 31056 19:38:53,920 --> 19:38:58,640 and zoom back out there's my new file 31057 19:38:56,400 --> 19:39:00,920 and there's my new JPEG that I quickly 31058 19:38:58,640 --> 19:39:03,960 uploaded before I can click now on 31059 19:39:00,920 --> 19:39:06,760 image. HTML and albeit a little 31060 19:39:03,960 --> 19:39:08,720 overwhelming that is a really big image 31061 19:39:06,760 --> 19:39:10,760 of harvor now apparently it's too big to 31062 19:39:08,720 --> 19:39:13,040 fit on the screen so this isn't the best 31063 19:39:10,760 --> 19:39:15,040 user experience to have to scroll up 31064 19:39:13,040 --> 19:39:17,400 okay so there's the image horrible 31065 19:39:15,040 --> 19:39:19,040 horrible design if you will at least in 31066 19:39:17,400 --> 19:39:21,000 terms of my code but there's going to be 31067 19:39:19,040 --> 19:39:23,000 ways where I can sort of rein that in 31068 19:39:21,000 --> 19:39:25,600 and affect the height or the width as 31069 19:39:23,000 --> 19:39:28,040 well but for now it's just deliberately 31070 19:39:25,600 --> 19:39:29,760 a little overwhelming instead now we can 31071 19:39:28,040 --> 19:39:32,192 do something a little more fun and 31072 19:39:29,760 --> 19:39:35,120 topical today uh which might be to use 31073 19:39:32,192 --> 19:39:37,680 an IM a video instead so let me go ahead 31074 19:39:35,120 --> 19:39:40,832 here and very quickly grab another file 31075 19:39:37,680 --> 19:39:42,232 today which is uh I brought in advance 31076 19:39:40,832 --> 19:39:45,440 and that you might have seen briefly 31077 19:39:42,232 --> 19:39:47,400 earlier which is an MP4 an actual video 31078 19:39:45,440 --> 19:39:49,480 file and what I'm going to do here by 31079 19:39:47,400 --> 19:39:51,920 revealing vs code again is I'm going to 31080 19:39:49,480 --> 19:39:53,960 code a file called video. HTML get 31081 19:39:51,920 --> 19:39:56,320 another demonstration here I'm going to 31082 19:39:53,960 --> 19:39:58,160 change my title to video just to keep 31083 19:39:56,320 --> 19:40:00,640 these things straight and instead of the 31084 19:39:58,160 --> 19:40:02,552 image tag you might imagine using now 31085 19:40:00,640 --> 19:40:05,120 indeed a video tag and this is a 31086 19:40:02,552 --> 19:40:06,760 relatively newer tag that has increasing 31087 19:40:05,120 --> 19:40:09,800 support among browsers so it's good now 31088 19:40:06,760 --> 19:40:12,192 to use and inside of this the syntax is 31089 19:40:09,800 --> 19:40:14,920 a little different you specify and this 31090 19:40:12,192 --> 19:40:18,160 is weirdly annoyingly in consistent not 31091 19:40:14,920 --> 19:40:20,480 SRC for Source you literally say source 31092 19:40:18,160 --> 19:40:23,280 and then in Source you use a source 31093 19:40:20,480 --> 19:40:25,040 attribute horrible design semantically 31094 19:40:23,280 --> 19:40:27,680 but like this is what we're stuck with 31095 19:40:25,040 --> 19:40:29,360 halloween. MP4 is the name of the video 31096 19:40:27,680 --> 19:40:31,640 we uploaded in advance made by some of 31097 19:40:29,360 --> 19:40:33,160 harder Harvard's digital artists and the 31098 19:40:31,640 --> 19:40:36,320 type of this video so that the browser 31099 19:40:33,160 --> 19:40:38,000 knows for sure is video/ MP4 that's a 31100 19:40:36,320 --> 19:40:40,360 so-called content type that you just 31101 19:40:38,000 --> 19:40:42,760 know or you look up to figure it out and 31102 19:40:40,360 --> 19:40:45,320 just so that this is as animated as 31103 19:40:42,760 --> 19:40:48,120 possible I'm going to tell the video tag 31104 19:40:45,320 --> 19:40:50,960 with a few attributes to autoplay and it 31105 19:40:48,120 --> 19:40:53,600 turns out that attributes often have key 31106 19:40:50,960 --> 19:40:56,160 value pairs whereby it's the key the 31107 19:40:53,600 --> 19:40:57,872 attribute name equals quote unquote some 31108 19:40:56,160 --> 19:41:00,120 value just like Lang equals quote 31109 19:40:57,872 --> 19:41:02,040 unquote for English but not all 31110 19:41:00,120 --> 19:41:04,232 attributes need values in fact if you 31111 19:41:02,040 --> 19:41:06,232 read the documentation for html's video 31112 19:41:04,232 --> 19:41:07,872 tag there's an autoplay attribute where 31113 19:41:06,232 --> 19:41:09,280 you can literally just say the key and 31114 19:41:07,872 --> 19:41:10,960 it needs no value it's just going to 31115 19:41:09,280 --> 19:41:13,360 mean autoplay and if you don't want to 31116 19:41:10,960 --> 19:41:15,120 autoplay you just omit it alt together 31117 19:41:13,360 --> 19:41:17,512 so so you don't necessarily need a value 31118 19:41:15,120 --> 19:41:19,160 on or off uh I want the thing to Loop 31119 19:41:17,512 --> 19:41:21,320 just so it keeps going I want it to be 31120 19:41:19,160 --> 19:41:23,120 muted so that we don't hear any sound in 31121 19:41:21,320 --> 19:41:25,832 fact there is no sound but browsers 31122 19:41:23,120 --> 19:41:28,800 nowadays for anti-spam and advertising 31123 19:41:25,832 --> 19:41:30,280 reasons often will not play a video if 31124 19:41:28,800 --> 19:41:31,960 it has sound because it's just kind of 31125 19:41:30,280 --> 19:41:34,000 obnoxious if you visit a page and all of 31126 19:41:31,960 --> 19:41:35,480 a sudden your speakers start blaring so 31127 19:41:34,000 --> 19:41:37,160 I know this from having read up on this 31128 19:41:35,480 --> 19:41:39,160 that I should mute it so if I want it to 31129 19:41:37,160 --> 19:41:41,640 actually autoplay for real and then I'll 31130 19:41:39,160 --> 19:41:44,280 set the width manually for now to be 31131 19:41:41,640 --> 19:41:46,440 like 128 pixels across just from some 31132 19:41:44,280 --> 19:41:49,680 trial and error earlier and that with 31133 19:41:46,440 --> 19:41:52,400 attribute does have a value now I'm 31134 19:41:49,680 --> 19:41:54,640 being a little um a little uptight here 31135 19:41:52,400 --> 19:41:56,360 by alphabetizing all of my attributes 31136 19:41:54,640 --> 19:41:57,800 not at all necessary I do it just so I 31137 19:41:56,360 --> 19:41:59,512 can skim things faster and know if 31138 19:41:57,800 --> 19:42:01,872 something is there or not so for me it's 31139 19:41:59,512 --> 19:42:04,400 just a matter of style let me go back to 31140 19:42:01,872 --> 19:42:07,280 my other tab go back to my index and 31141 19:42:04,400 --> 19:42:10,600 you'll see two new files again the mp4 31142 19:42:07,280 --> 19:42:13,000 file and video. HTML I'll click on the 31143 19:42:10,600 --> 19:42:15,320 ladder and if I did this well here we 31144 19:42:13,000 --> 19:42:17,960 have thank thanks to our friends in 31145 19:42:15,320 --> 19:42:20,640 Harvard U our artistic friends at 31146 19:42:17,960 --> 19:42:22,320 Harvard very like an oo would help with 31147 19:42:20,640 --> 19:42:24,512 the drama here but okay but we have a 31148 19:42:22,320 --> 19:42:26,872 very dramatic nice Halloween type view 31149 19:42:24,512 --> 19:42:28,320 here as well so we have videos embedded 31150 19:42:26,872 --> 19:42:30,680 as well and suffice it to say there's 31151 19:42:28,320 --> 19:42:33,400 ways to embed YouTube videos or Vimeo or 31152 19:42:30,680 --> 19:42:35,720 other services as well using yet more 31153 19:42:33,400 --> 19:42:38,512 tags too but the web is of course all 31154 19:42:35,720 --> 19:42:39,872 about hyperlink Hyper text markup 31155 19:42:38,512 --> 19:42:41,120 language where you click on something 31156 19:42:39,872 --> 19:42:42,872 and you end up somewhere else and this 31157 19:42:41,120 --> 19:42:44,000 is how the web is so powerfully 31158 19:42:42,872 --> 19:42:45,920 interconnected 31159 19:42:44,000 --> 19:42:48,640 so how do we start creating links from 31160 19:42:45,920 --> 19:42:50,832 one website or web page to another 31161 19:42:48,640 --> 19:42:53,040 either that I made or someone else well 31162 19:42:50,832 --> 19:42:54,960 let me go ahead and open back up my 31163 19:42:53,040 --> 19:42:57,440 terminal window and let's create a file 31164 19:42:54,960 --> 19:42:59,600 called link. HTML just to demonstrate 31165 19:42:57,440 --> 19:43:01,512 what you and I know as a link I'll hide 31166 19:42:59,600 --> 19:43:03,512 my terminal window now let me copy paste 31167 19:43:01,512 --> 19:43:05,232 just to save myself some keystrokes and 31168 19:43:03,512 --> 19:43:08,192 let me get rid of the video tag so we 31169 19:43:05,232 --> 19:43:10,872 can focus now on links suppose that I 31170 19:43:08,192 --> 19:43:13,480 want you to visit Harvard virtually well 31171 19:43:10,872 --> 19:43:16,080 I could say something like visit uh 31172 19:43:13,480 --> 19:43:17,512 Harvard period this is uninteresting 31173 19:43:16,080 --> 19:43:19,480 because it's just going to be text I 31174 19:43:17,512 --> 19:43:21,832 probably want you to actually visit 31175 19:43:19,480 --> 19:43:23,080 harvard.edu instead more specifically 31176 19:43:21,832 --> 19:43:25,080 and I'll lower case it just to be 31177 19:43:23,080 --> 19:43:27,120 consistent with what browsers do in the 31178 19:43:25,080 --> 19:43:29,080 address bar all right let me go now to 31179 19:43:27,120 --> 19:43:32,080 the video uh back to this video Tab and 31180 19:43:29,080 --> 19:43:34,600 go back where we now see my index I'll 31181 19:43:32,080 --> 19:43:36,360 Zoom back in and there's link. HTML 31182 19:43:34,600 --> 19:43:38,400 unfortunately when I click this and I'll 31183 19:43:36,360 --> 19:43:40,232 zoom in you literally just see the text 31184 19:43:38,400 --> 19:43:42,160 that I wrote and yet on every social 31185 19:43:40,232 --> 19:43:44,320 media platform nowadays except like 31186 19:43:42,160 --> 19:43:45,920 Instagram when you type a URL or what 31187 19:43:44,320 --> 19:43:49,440 looks like a URL even if you didn't 31188 19:43:45,920 --> 19:43:51,320 bother with the HTTP or https it usually 31189 19:43:49,440 --> 19:43:53,080 automatically links it for you on 31190 19:43:51,320 --> 19:43:55,872 Facebook on Twitter and other sites as 31191 19:43:53,080 --> 19:43:57,960 well that's just a convenience Discord 31192 19:43:55,872 --> 19:43:59,320 and slack do that too but they're just 31193 19:43:57,960 --> 19:44:02,440 doing it to make things more 31194 19:43:59,320 --> 19:44:06,120 userfriendly but they have to generate 31195 19:44:02,440 --> 19:44:08,120 HTML with the proper tags and attributes 31196 19:44:06,120 --> 19:44:09,760 so to get this to actually work it's not 31197 19:44:08,120 --> 19:44:13,080 even good enough to say 31198 19:44:09,760 --> 19:44:15,192 https www.harvard.edu because if I go 31199 19:44:13,080 --> 19:44:17,552 back now and reload now you'll literally 31200 19:44:15,192 --> 19:44:19,920 just see all of that as text if you want 31201 19:44:17,552 --> 19:44:22,760 the browser to treat this as a link you 31202 19:44:19,920 --> 19:44:24,192 need to use the anchor tag it'd be great 31203 19:44:22,760 --> 19:44:26,680 if it were called the link tag but it's 31204 19:44:24,192 --> 19:44:29,320 not it's called The Anchor tag or a for 31205 19:44:26,680 --> 19:44:31,832 short and the way you reference the URL 31206 19:44:29,320 --> 19:44:34,512 to which you want to lead the user is 31207 19:44:31,832 --> 19:44:36,120 via href for hyper reference this is one 31208 19:44:34,512 --> 19:44:38,920 of the earliest tags perhaps among the 31209 19:44:36,120 --> 19:44:42,512 most Arcane now but if I then put that 31210 19:44:38,920 --> 19:44:44,552 whole URL in quotes and close my tag I 31211 19:44:42,512 --> 19:44:46,360 now have to opportunity to finish my 31212 19:44:44,552 --> 19:44:49,512 thought in between the start tag and the 31213 19:44:46,360 --> 19:44:51,680 end tag for this anchor and what I put 31214 19:44:49,512 --> 19:44:53,800 in between the start and end tag is 31215 19:44:51,680 --> 19:44:56,512 whatever the human's going to see so 31216 19:44:53,800 --> 19:44:59,640 here I can say Harvard I can go back to 31217 19:44:56,512 --> 19:45:02,360 my other tab I can reload the page and 31218 19:44:59,640 --> 19:45:04,480 now you see the familiar blue underline 31219 19:45:02,360 --> 19:45:05,960 this now is an actual link and if I 31220 19:45:04,480 --> 19:45:09,600 click it I'll be whisked away to the 31221 19:45:05,960 --> 19:45:12,360 actual Harvard websites but there's a 31222 19:45:09,600 --> 19:45:15,192 risk here can anyone imagine pretty 31223 19:45:12,360 --> 19:45:17,160 simply after like what 60 seconds of the 31224 19:45:15,192 --> 19:45:20,440 link tag of the anchor tag how could 31225 19:45:17,160 --> 19:45:23,360 someone an adversary misuse this tag 31226 19:45:20,440 --> 19:45:25,640 alone How could a website run by an 31227 19:45:23,360 --> 19:45:29,920 adversary How could a spammer misuse 31228 19:45:25,640 --> 19:45:29,920 this tag do you think 31229 19:45:31,160 --> 19:45:35,872 yeah yeah absolutely you could have it 31230 19:45:33,552 --> 19:45:38,400 say one thing but lead another lead 31231 19:45:35,872 --> 19:45:40,480 elsewhere so I could say Yale in here 31232 19:45:38,400 --> 19:45:42,160 nothing stopping me as the developer go 31233 19:45:40,480 --> 19:45:44,600 back to the page reload now it says 31234 19:45:42,160 --> 19:45:46,120 visit Yale you click on Yale and voila 31235 19:45:44,600 --> 19:45:48,800 you end up applying to the wrong place 31236 19:45:46,120 --> 19:45:51,320 instead now there's some hints of this I 31237 19:45:48,800 --> 19:45:53,120 could hover over this and super small 31238 19:45:51,320 --> 19:45:55,680 like this isn't very good for your 31239 19:45:53,120 --> 19:45:57,480 anti-hacking techniques but way down 31240 19:45:55,680 --> 19:45:59,320 here you can actually see the URL that 31241 19:45:57,480 --> 19:46:01,080 it's going to go to in most browsers 31242 19:45:59,320 --> 19:46:03,192 indeed do this at least on desktops and 31243 19:46:01,080 --> 19:46:04,360 laptops so it's a little bit of a hint 31244 19:46:03,192 --> 19:46:07,232 but what you're seeing here even though 31245 19:46:04,360 --> 19:46:09,872 this is kind of a silly um uh playful 31246 19:46:07,232 --> 19:46:13,872 example this is exactly how fishing 31247 19:46:09,872 --> 19:46:15,760 attacks work p i sh iing work whereby an 31248 19:46:13,872 --> 19:46:17,512 adversary tells you to log into your 31249 19:46:15,760 --> 19:46:20,280 PayPal account but it doesn't go to 31250 19:46:17,512 --> 19:46:22,480 paypal.com it goes to some other random 31251 19:46:20,280 --> 19:46:24,000 website that they bought and built that 31252 19:46:22,480 --> 19:46:25,552 then tries to collect your username and 31253 19:46:24,000 --> 19:46:27,440 password and stored in their database so 31254 19:46:25,552 --> 19:46:30,440 now they can log into your PayPal 31255 19:46:27,440 --> 19:46:32,400 account as you and it boils down to that 31256 19:46:30,440 --> 19:46:35,280 simple primitive and you can be even 31257 19:46:32,400 --> 19:46:38,400 more manipulative to you can even say 31258 19:46:35,280 --> 19:46:43,480 the whole URL for Yale like yale.edu or 31259 19:46:38,400 --> 19:46:45,832 Worse htps www.yale.edu reload that and 31260 19:46:43,480 --> 19:46:47,480 now who I mean who among you and people 31261 19:46:45,832 --> 19:46:49,760 in your lives are necessarily going to 31262 19:46:47,480 --> 19:46:52,080 be so paranoid as to not just blindly 31263 19:46:49,760 --> 19:46:54,552 click on that URL this is why just being 31264 19:46:52,080 --> 19:46:57,000 a defensive real world person nowadays 31265 19:46:54,552 --> 19:46:58,440 digitally is just ever more so important 31266 19:46:57,000 --> 19:47:01,192 so these same things that can be used 31267 19:46:58,440 --> 19:47:03,960 for good or uh benign use cases can also 31268 19:47:01,192 --> 19:47:07,800 be used for ill purposes too and it is 31269 19:47:03,960 --> 19:47:09,800 literally that simple questions now on 31270 19:47:07,800 --> 19:47:13,760 any of these tags thus 31271 19:47:09,800 --> 19:47:15,800 far just a few more to offer up any 31272 19:47:13,760 --> 19:47:18,960 questions on this 31273 19:47:15,800 --> 19:47:20,360 here no well let me open up a couple 31274 19:47:18,960 --> 19:47:23,080 that I brought in advance just so we 31275 19:47:20,360 --> 19:47:24,920 don't have to type all of them here um 31276 19:47:23,080 --> 19:47:27,000 if you for instance have a web page 31277 19:47:24,920 --> 19:47:29,800 that's got quite a bit of code let me go 31278 19:47:27,000 --> 19:47:33,000 ahead and grab from the website a couple 31279 19:47:29,800 --> 19:47:37,512 of examples real fast here namely one 31280 19:47:33,000 --> 19:47:40,400 that we'll call how about uh meta 31281 19:47:37,512 --> 19:47:42,720 HTML and in this example here give me 31282 19:47:40,400 --> 19:47:43,512 just a moment to full screen it we're 31283 19:47:42,720 --> 19:47:48,480 going to 31284 19:47:43,512 --> 19:47:50,512 have a file So codem Meta HTML I'll open 31285 19:47:48,480 --> 19:47:53,192 this up next no relationship to what we 31286 19:47:50,512 --> 19:47:55,120 now know as meta the company but rather 31287 19:47:53,192 --> 19:47:56,920 this is going to be a page that I copied 31288 19:47:55,120 --> 19:47:58,360 and pasted the same chunk of Latin like 31289 19:47:56,920 --> 19:48:00,400 text from earlier so it's going to be a 31290 19:47:58,360 --> 19:48:01,760 really big paragraph of text and this is 31291 19:48:00,400 --> 19:48:03,400 an example where if you were to open 31292 19:48:01,760 --> 19:48:05,480 this web page not on my own Mac or your 31293 19:48:03,400 --> 19:48:07,280 PC but on your phone the font might 31294 19:48:05,480 --> 19:48:08,440 actually be really annoying and 31295 19:48:07,280 --> 19:48:10,040 difficult to read why because your 31296 19:48:08,440 --> 19:48:12,400 phone's going to try to squeeze all of 31297 19:48:10,040 --> 19:48:14,640 the content onto the tiny viewport the 31298 19:48:12,400 --> 19:48:16,832 rectangular region of your phone instead 31299 19:48:14,640 --> 19:48:19,192 so it turns out there are ways pretty 31300 19:48:16,832 --> 19:48:20,800 easy ways to make your website mobile 31301 19:48:19,192 --> 19:48:22,720 friendly as well Otherwise Known 31302 19:48:20,800 --> 19:48:26,040 technically as responsive and the 31303 19:48:22,720 --> 19:48:29,120 easiest way to do this is to include 31304 19:48:26,040 --> 19:48:30,640 this tag here a meta tag again no 31305 19:48:29,120 --> 19:48:32,920 relationship to Facebook this has been 31306 19:48:30,640 --> 19:48:36,280 here much longer and this case here this 31307 19:48:32,920 --> 19:48:38,080 meta tag online five has its own sort of 31308 19:48:36,280 --> 19:48:39,720 approach to key value pairs this is a 31309 19:48:38,080 --> 19:48:41,400 good example of where it'd be nice if it 31310 19:48:39,720 --> 19:48:43,512 looked just like everything else but 31311 19:48:41,400 --> 19:48:45,080 this is what we have historically 31312 19:48:43,512 --> 19:48:47,160 you can have a meta tag with an 31313 19:48:45,080 --> 19:48:49,040 attribute called name that refers to the 31314 19:48:47,160 --> 19:48:51,120 name of some feature of the browser in 31315 19:48:49,040 --> 19:48:53,040 this case viewport is the technical term 31316 19:48:51,120 --> 19:48:54,872 for like the big rectangular region to 31317 19:48:53,040 --> 19:48:57,440 which I keep referring the body really 31318 19:48:54,872 --> 19:48:59,872 of your page the content for the 31319 19:48:57,440 --> 19:49:01,640 viewport you can say some AR esoteric 31320 19:48:59,872 --> 19:49:03,440 details like this the initial scale 31321 19:49:01,640 --> 19:49:05,080 should be one that is no matter who 31322 19:49:03,440 --> 19:49:07,120 visits your site it shouldn't start 31323 19:49:05,080 --> 19:49:08,680 zoomed in it shouldn't start zoomed out 31324 19:49:07,120 --> 19:49:11,232 it should start at just the default 31325 19:49:08,680 --> 19:49:13,192 sizing and then this here with equals 31326 19:49:11,232 --> 19:49:15,720 device width is a very arcane way of 31327 19:49:13,192 --> 19:49:18,480 saying if the user has a small screen 31328 19:49:15,720 --> 19:49:20,440 show the text proportional to that size 31329 19:49:18,480 --> 19:49:22,800 don't just try to cram it all into a 31330 19:49:20,440 --> 19:49:24,552 tiny little window so it's super simple 31331 19:49:22,800 --> 19:49:26,552 but if for the next problem set or 31332 19:49:24,552 --> 19:49:28,320 future projects as well you find that 31333 19:49:26,552 --> 19:49:31,000 just things look really bad on mobile 31334 19:49:28,320 --> 19:49:33,400 like this kind of tag is the place to 31335 19:49:31,000 --> 19:49:35,192 start meta there aren't terribly many of 31336 19:49:33,400 --> 19:49:37,760 these that you'll use but they're useful 31337 19:49:35,192 --> 19:49:39,872 for other mechanisms as well in fact let 31338 19:49:37,760 --> 19:49:43,040 me go ahead and semi secretly pull up 31339 19:49:39,872 --> 19:49:45,512 one other example as well whereby I'm 31340 19:49:43,040 --> 19:49:49,232 going to grab another example that uses 31341 19:49:45,512 --> 19:49:51,640 more of these tags and in just a moment 31342 19:49:49,232 --> 19:49:56,080 I'll reveal it here give me just one 31343 19:49:51,640 --> 19:49:57,232 second here I'll have I'll propose that 31344 19:49:56,080 --> 19:49:58,960 in this 31345 19:49:57,232 --> 19:50:03,872 example of 31346 19:49:58,960 --> 19:50:05,600 meta we now add these properties instead 31347 19:50:03,872 --> 19:50:07,320 so I copy pasted this from an existing 31348 19:50:05,600 --> 19:50:09,192 file just so as to not waste time typing 31349 19:50:07,320 --> 19:50:12,280 all of these out if you've ever shared a 31350 19:50:09,192 --> 19:50:14,192 URL on Facebook or Twitter or slack or 31351 19:50:12,280 --> 19:50:15,872 Discord order any number of websites 31352 19:50:14,192 --> 19:50:17,552 nowadays that automatically show a nice 31353 19:50:15,872 --> 19:50:19,160 preview where you sudden instantly see 31354 19:50:17,552 --> 19:50:21,480 like the default image of the page maybe 31355 19:50:19,160 --> 19:50:24,080 a few sentences or words of text or 31356 19:50:21,480 --> 19:50:25,640 something like that sometimes those 31357 19:50:24,080 --> 19:50:27,512 applications those websites will just 31358 19:50:25,640 --> 19:50:29,232 choose like the first image it finds on 31359 19:50:27,512 --> 19:50:31,232 the website or the first sentence it 31360 19:50:29,232 --> 19:50:33,440 sees and show that but that might not be 31361 19:50:31,232 --> 19:50:35,832 very user friendly or search engine 31362 19:50:33,440 --> 19:50:38,480 friendly and so a prog a developer might 31363 19:50:35,832 --> 19:50:40,232 want to control what it is that slack 31364 19:50:38,480 --> 19:50:43,120 Discord Facebook Twitter and other such 31365 19:50:40,232 --> 19:50:45,600 sites show by default for that can use 31366 19:50:43,120 --> 19:50:47,800 nowadays what are called open graph tags 31367 19:50:45,600 --> 19:50:49,552 which is to say there's other uses of 31368 19:50:47,800 --> 19:50:50,920 the meta tag and you just look these 31369 19:50:49,552 --> 19:50:52,760 things up even I had to look this up to 31370 19:50:50,920 --> 19:50:54,680 remember what the key value pairs are 31371 19:50:52,760 --> 19:50:56,552 the meta tag can also have a property 31372 19:50:54,680 --> 19:50:59,480 attribute that can be these very 31373 19:50:56,552 --> 19:51:02,080 specific strings OG title OG description 31374 19:50:59,480 --> 19:51:03,872 OG image which denotes open graph which 31375 19:51:02,080 --> 19:51:06,320 again is this standard that's evolved in 31376 19:51:03,872 --> 19:51:09,480 recent years and what you can do here is 31377 19:51:06,320 --> 19:51:11,872 tell browsers and in turn servers what 31378 19:51:09,480 --> 19:51:13,512 you want them to show as the default 31379 19:51:11,872 --> 19:51:15,320 title of the page P the description of 31380 19:51:13,512 --> 19:51:17,192 the page and even the default image just 31381 19:51:15,320 --> 19:51:20,280 so you can exercise more control when 31382 19:51:17,192 --> 19:51:21,720 sharing things socially nowadays as well 31383 19:51:20,280 --> 19:51:23,400 again it just boils down to these key 31384 19:51:21,720 --> 19:51:25,192 value pairs this is absolutely the kind 31385 19:51:23,400 --> 19:51:26,960 of thing you look up as needed to cross 31386 19:51:25,192 --> 19:51:28,400 check but those capabilities are there 31387 19:51:26,960 --> 19:51:30,232 and so literally the next time you paste 31388 19:51:28,400 --> 19:51:32,800 a link into slack or Discord or any 31389 19:51:30,232 --> 19:51:35,360 online site that then displays it in 31390 19:51:32,800 --> 19:51:37,440 embedded fashion just know that all this 31391 19:51:35,360 --> 19:51:40,720 time a little bit of textual code like 31392 19:51:37,440 --> 19:51:43,120 this in HTML has been there by whoever 31393 19:51:40,720 --> 19:51:45,232 authored the site 31394 19:51:43,120 --> 19:51:48,040 all right let's do one final example in 31395 19:51:45,232 --> 19:51:49,872 HTML alone before we transition to just 31396 19:51:48,040 --> 19:51:51,480 cleaning up the Aesthetics and improving 31397 19:51:49,872 --> 19:51:53,760 the visuals of everything we've been 31398 19:51:51,480 --> 19:51:56,640 creating let me go ahead here and close 31399 19:51:53,760 --> 19:52:00,040 meta HTML let me code up a new file 31400 19:51:56,640 --> 19:52:02,480 called how about search. HTML and see if 31401 19:52:00,040 --> 19:52:04,552 we can't draw some inspiration from our 31402 19:52:02,480 --> 19:52:07,120 cursory understanding earlier of how 31403 19:52:04,552 --> 19:52:09,480 URLs work to see if we can't reinvent 31404 19:52:07,120 --> 19:52:11,872 google.com itself so recall that a 31405 19:52:09,480 --> 19:52:13,920 canonical URL might look like this here 31406 19:52:11,872 --> 19:52:17,400 and in particular if you want to pass in 31407 19:52:13,920 --> 19:52:19,360 user input to that URL again you can 31408 19:52:17,400 --> 19:52:21,552 potentially have a question mark and 31409 19:52:19,360 --> 19:52:23,280 then a key equals value pair or for that 31410 19:52:21,552 --> 19:52:25,120 matter you can even have multiple key 31411 19:52:23,280 --> 19:52:26,640 value pairs that by convention are 31412 19:52:25,120 --> 19:52:28,120 separated by ampers sense these things 31413 19:52:26,640 --> 19:52:29,872 are everywhere like later today when you 31414 19:52:28,120 --> 19:52:32,280 pull up almost any website in your 31415 19:52:29,872 --> 19:52:34,320 browser look at the URL and you'll just 31416 19:52:32,280 --> 19:52:35,512 see a lot of this a lot of noise too and 31417 19:52:34,320 --> 19:52:38,040 distractions but there's going to be 31418 19:52:35,512 --> 19:52:39,640 some equal signs most likely Andor some 31419 19:52:38,040 --> 19:52:42,192 ampersands as well and those are just 31420 19:52:39,640 --> 19:52:44,680 separating key value pairs now what can 31421 19:52:42,192 --> 19:52:47,600 I do here well if you think back to how 31422 19:52:44,680 --> 19:52:49,192 we manually searched for cats earlier 31423 19:52:47,600 --> 19:52:52,480 let me quickly do this I'll do this one 31424 19:52:49,192 --> 19:52:55,720 manually doc type HTML as my very first 31425 19:52:52,480 --> 19:52:58,512 line HTML tag with how about my Lang 31426 19:52:55,720 --> 19:53:00,320 attribute for English up here and then 31427 19:52:58,512 --> 19:53:02,160 inside of this I'll have a head tag 31428 19:53:00,320 --> 19:53:05,440 inside of this I'll have a title I'll 31429 19:53:02,160 --> 19:53:07,160 call this example uh search and then 31430 19:53:05,440 --> 19:53:09,360 down here I'll have my beginning of a 31431 19:53:07,160 --> 19:53:12,512 body tag and now let me introduce you to 31432 19:53:09,360 --> 19:53:14,400 really a final tag for now uh a form tag 31433 19:53:12,512 --> 19:53:16,080 which will create a web form the thing 31434 19:53:14,400 --> 19:53:17,920 with text boxes and buttons that you and 31435 19:53:16,080 --> 19:53:20,080 I use every day on any number of 31436 19:53:17,920 --> 19:53:22,720 websites inside of this form I'm going 31437 19:53:20,080 --> 19:53:25,280 to have an input like a text box whose 31438 19:53:22,720 --> 19:53:27,552 name is going to be Q for query because 31439 19:53:25,280 --> 19:53:31,120 I'm trying to re-implement Google here 31440 19:53:27,552 --> 19:53:33,600 uh the uh type of that I want to be a 31441 19:53:31,120 --> 19:53:35,400 text box or if I know I'm using this for 31442 19:53:33,600 --> 19:53:37,192 search I can actually change this to a 31443 19:53:35,400 --> 19:53:38,640 search box and it's going to let me it's 31444 19:53:37,192 --> 19:53:40,280 going to generally put a little X there 31445 19:53:38,640 --> 19:53:42,680 so you can clear it quickly that's a 31446 19:53:40,280 --> 19:53:44,440 nice little enhancement as well and then 31447 19:53:42,680 --> 19:53:47,360 I'm going to give myself a submit button 31448 19:53:44,440 --> 19:53:49,480 by doing input whoops I'm going to give 31449 19:53:47,360 --> 19:53:53,360 myself a submit button by doing input 31450 19:53:49,480 --> 19:53:55,640 type equals submit and then I'll leave 31451 19:53:53,360 --> 19:53:57,232 that as such here all right now I need 31452 19:53:55,640 --> 19:53:58,800 to do a little bit more but let's see 31453 19:53:57,232 --> 19:54:01,800 how this looks let me go over to my 31454 19:53:58,800 --> 19:54:03,040 other tab let me go back to my index and 31455 19:54:01,800 --> 19:54:07,080 if I zoom 31456 19:54:03,040 --> 19:54:08,552 out there is search. HTML I'll click it 31457 19:54:07,080 --> 19:54:10,640 and there's not much going on here even 31458 19:54:08,552 --> 19:54:13,600 if I zoom in but I do indeed have a 31459 19:54:10,640 --> 19:54:16,232 really big text box and a submit button 31460 19:54:13,600 --> 19:54:18,360 but I haven't in my HTML told anyone 31461 19:54:16,232 --> 19:54:20,960 anywhere that I want this input whether 31462 19:54:18,360 --> 19:54:22,080 I type cat or dog to go to google.com so 31463 19:54:20,960 --> 19:54:23,320 for that I need a couple of more 31464 19:54:22,080 --> 19:54:24,552 attributes and I know this from having 31465 19:54:23,320 --> 19:54:26,832 done this before and any online 31466 19:54:24,552 --> 19:54:28,480 reference will say the same you can add 31467 19:54:26,832 --> 19:54:30,440 an action attribute like what do you 31468 19:54:28,480 --> 19:54:32,960 want the action of this form to be and 31469 19:54:30,440 --> 19:54:34,480 you can put the URL to which you want 31470 19:54:32,960 --> 19:54:38,800 this form to be submitted and I know 31471 19:54:34,480 --> 19:54:41,120 from tinkering that it should be hdps 31472 19:54:38,800 --> 19:54:43,552 www.google.com search I don't need to 31473 19:54:41,120 --> 19:54:47,232 put any question marks here myself but I 31474 19:54:43,552 --> 19:54:49,920 do want the uh browser to do that for me 31475 19:54:47,232 --> 19:54:52,680 so let me go back to my other tab let me 31476 19:54:49,920 --> 19:54:55,800 reload and nothing visually has happened 31477 19:54:52,680 --> 19:54:57,832 but watch this when I now type in cats 31478 19:54:55,800 --> 19:55:01,440 but before I hit enter notice that I'm 31479 19:54:57,832 --> 19:55:04,680 currently at some long crazy URL search. 31480 19:55:01,440 --> 19:55:06,360 HTML is expected if I now go down to the 31481 19:55:04,680 --> 19:55:08,400 submit button and click submit watch 31482 19:55:06,360 --> 19:55:11,440 what happens to the URL and the page 31483 19:55:08,400 --> 19:55:13,120 itself I'm whisked away to the actual 31484 19:55:11,440 --> 19:55:15,360 google.com and indeed there are those 31485 19:55:13,120 --> 19:55:17,680 same cats and if I zoom in here you'll 31486 19:55:15,360 --> 19:55:20,440 see that my URL has changed to be indeed 31487 19:55:17,680 --> 19:55:22,040 SL search question mark Q equals cats so 31488 19:55:20,440 --> 19:55:24,480 this is just how web forms work when you 31489 19:55:22,040 --> 19:55:26,552 submit any form on the web in this way 31490 19:55:24,480 --> 19:55:29,160 the browser automatically goes to that 31491 19:55:26,552 --> 19:55:31,000 action URL adds a question mark puts any 31492 19:55:29,160 --> 19:55:33,120 key value pairs that you manually typed 31493 19:55:31,000 --> 19:55:35,080 into the text boxes and lets the server 31494 19:55:33,120 --> 19:55:37,040 do its thing now here's where Chrome is 31495 19:55:35,080 --> 19:55:39,440 starting to simplify things Safari does 31496 19:55:37,040 --> 19:55:41,600 this too if you double click on your url 31497 19:55:39,440 --> 19:55:43,512 now you see the full URL but if any 31498 19:55:41,600 --> 19:55:45,720 parts are missing that's just a UI thing 31499 19:55:43,512 --> 19:55:48,160 to eliminate visual distractions 31500 19:55:45,720 --> 19:55:50,872 nowadays meanwhile if I go back to my 31501 19:55:48,160 --> 19:55:53,640 own form if I search this time for dogs 31502 19:55:50,872 --> 19:55:57,720 and hit enter now again the URL changes 31503 19:55:53,640 --> 19:56:00,360 to be Q equals dogs and it all reduces 31504 19:55:57,720 --> 19:56:02,960 to this basic building block of using an 31505 19:56:00,360 --> 19:56:05,080 a form tag now I can be more explicit if 31506 19:56:02,960 --> 19:56:06,720 I know I want to use get it which is 31507 19:56:05,080 --> 19:56:08,720 actually the default I can literally say 31508 19:56:06,720 --> 19:56:10,552 quote unquote get in all lowercase even 31509 19:56:08,720 --> 19:56:12,360 though the verb earlier was by Design in 31510 19:56:10,552 --> 19:56:14,760 uppercase but here now I'm just being 31511 19:56:12,360 --> 19:56:17,160 ever more explicit if I don't want the 31512 19:56:14,760 --> 19:56:19,232 label of this button to be very 31513 19:56:17,160 --> 19:56:21,480 generically submit maybe I want it to be 31514 19:56:19,232 --> 19:56:23,800 Google search quote unquote well if you 31515 19:56:21,480 --> 19:56:25,720 read the documentation for forms you can 31516 19:56:23,800 --> 19:56:28,400 actually change the value of the button 31517 19:56:25,720 --> 19:56:30,872 to be quote unquote Google search and if 31518 19:56:28,400 --> 19:56:33,280 I now go back here and reload I get a 31519 19:56:30,872 --> 19:56:35,720 fresh form and now I get a button that 31520 19:56:33,280 --> 19:56:37,192 literally says Google Search and if I 31521 19:56:35,720 --> 19:56:38,720 tinker with this further because this 31522 19:56:37,192 --> 19:56:41,872 isn't very user friendly there's even 31523 19:56:38,720 --> 19:56:46,120 more attributes I can do I can add on my 31524 19:56:41,872 --> 19:56:48,680 t uh search input a uh autoc complete 31525 19:56:46,120 --> 19:56:50,400 equals uh off if I don't want to see my 31526 19:56:48,680 --> 19:56:51,512 own history for whatever reason I don't 31527 19:56:50,400 --> 19:56:53,360 want people knowing I'm searching for 31528 19:56:51,512 --> 19:56:55,680 cats and dogs on this page I can 31529 19:56:53,360 --> 19:56:58,680 autofocus on the text box so that it 31530 19:56:55,680 --> 19:57:00,872 shows the cursor blinking in that box by 31531 19:56:58,680 --> 19:57:03,232 default and I can even do something like 31532 19:57:00,872 --> 19:57:05,160 this I can have a placeholder attribute 31533 19:57:03,232 --> 19:57:07,440 that says something like query or some 31534 19:57:05,160 --> 19:57:10,160 other documentation for the user and if 31535 19:57:07,440 --> 19:57:12,480 I now go back and reload you'll see 31536 19:57:10,160 --> 19:57:14,512 notice it says query and it's subtle but 31537 19:57:12,480 --> 19:57:17,280 my cursor is already positioned there it 31538 19:57:14,512 --> 19:57:18,760 gave it focus and I can type cats now 31539 19:57:17,280 --> 19:57:20,760 without having to click in the Box 31540 19:57:18,760 --> 19:57:23,872 manually which is just marginally better 31541 19:57:20,760 --> 19:57:27,232 for the user's experience any questions 31542 19:57:23,872 --> 19:57:29,232 now on all of this 31543 19:57:27,232 --> 19:57:31,160 here any 31544 19:57:29,232 --> 19:57:32,832 questions all right that too was a lot 31545 19:57:31,160 --> 19:57:34,360 why don't we take a casual five minute 31546 19:57:32,832 --> 19:57:36,400 break and when we resume we'll take a 31547 19:57:34,360 --> 19:57:39,800 look at CSS add in some JavaScript and 31548 19:57:36,400 --> 19:57:42,512 then wrap up so 5 minutes only for now 31549 19:57:39,800 --> 19:57:45,400 all right we are back so that's 31550 19:57:42,512 --> 19:57:47,080 technically it for HTML like here on out 31551 19:57:45,400 --> 19:57:48,480 it'll be up to like online resources and 31552 19:57:47,080 --> 19:57:50,280 references we point you to just to fill 31553 19:57:48,480 --> 19:57:52,160 in your vocabulary for more tags and 31554 19:57:50,280 --> 19:57:54,960 attributes but like conceptually that's 31555 19:57:52,160 --> 19:57:57,080 it there's attributes uh there are tags 31556 19:57:54,960 --> 19:57:58,872 and there are attributes and the rest of 31557 19:57:57,080 --> 19:58:01,360 it really is just kind of a laundry list 31558 19:57:58,872 --> 19:58:03,120 of capable uh possible features but it 31559 19:58:01,360 --> 19:58:05,320 turns out too you'll see over time that 31560 19:58:03,120 --> 19:58:08,920 you can even see the HTML for websit so 31561 19:58:05,320 --> 19:58:11,360 for instance if I go over to harvard.edu 31562 19:58:08,920 --> 19:58:13,040 in my browser which I'll go ahead in 31563 19:58:11,360 --> 19:58:13,920 just a moment here here and do here and 31564 19:58:13,040 --> 19:58:15,440 do 31565 19:58:13,920 --> 19:58:17,440 https 31566 19:58:15,440 --> 19:58:20,000 www.harvard.edu 31567 19:58:17,440 --> 19:58:22,000 enter it again will pull up today's 31568 19:58:20,000 --> 19:58:23,832 version of Harvard's website and if I 31569 19:58:22,000 --> 19:58:25,960 rightclick or control click on it again 31570 19:58:23,832 --> 19:58:28,832 and go to inspect you'll see those 31571 19:58:25,960 --> 19:58:30,832 so-called developer tools and earlier we 31572 19:58:28,832 --> 19:58:33,720 focused on the network tab just so we 31573 19:58:30,832 --> 19:58:35,552 could see the HTTP stuff going on but 31574 19:58:33,720 --> 19:58:38,192 what I glossed over earlier was the 31575 19:58:35,552 --> 19:58:41,120 so-called elements tab which actually 31576 19:58:38,192 --> 19:58:43,120 shows you the HTML underlying any web 31577 19:58:41,120 --> 19:58:45,320 page on the internet and so for instance 31578 19:58:43,120 --> 19:58:47,960 here is the underlying HTML for 31579 19:58:45,320 --> 19:58:49,120 Harvard's homepage as of right now and 31580 19:58:47,960 --> 19:58:50,800 aesthetically some of it's been 31581 19:58:49,120 --> 19:58:52,280 collapsed so if I click on these various 31582 19:58:50,800 --> 19:58:54,120 triangles I'll see what's actually 31583 19:58:52,280 --> 19:58:56,360 inside of that is the children of some 31584 19:58:54,120 --> 19:58:58,280 of these HTML tags but here on out if 31585 19:58:56,360 --> 19:59:01,360 you're ever curious as to like how a web 31586 19:58:58,280 --> 19:59:03,160 page uh made some feature visually you 31587 19:59:01,360 --> 19:59:04,832 can just literally use these developer 31588 19:59:03,160 --> 19:59:07,600 tools built into your own browser just 31589 19:59:04,832 --> 19:59:09,832 to see what the uh web developer 31590 19:59:07,600 --> 19:59:13,400 actually did and you can do things too 31591 19:59:09,832 --> 19:59:15,400 like this like if you really like maybe 31592 19:59:13,400 --> 19:59:17,192 uh let's see if you really like this 31593 19:59:15,400 --> 19:59:18,800 menu in the top right hand corner of 31594 19:59:17,192 --> 19:59:21,552 Harvard's website you can even 31595 19:59:18,800 --> 19:59:24,280 rightclick that or control click that 31596 19:59:21,552 --> 19:59:27,480 specifically choose inspect and what 31597 19:59:24,280 --> 19:59:29,680 browsers will do is jump to the HTML 31598 19:59:27,480 --> 19:59:31,512 corresponding to that visual element on 31599 19:59:29,680 --> 19:59:33,440 the page and here you can see though 31600 19:59:31,512 --> 19:59:35,720 we've not talked about this tag before 31601 19:59:33,440 --> 19:59:37,232 there's a button tag there's an ID 31602 19:59:35,720 --> 19:59:39,360 attribute and there's some other 31603 19:59:37,232 --> 19:59:42,232 attributes as well that Define that 31604 19:59:39,360 --> 19:59:44,832 button um you can do other things too in 31605 19:59:42,232 --> 19:59:46,280 the web page let me scroll down for 31606 19:59:44,832 --> 19:59:48,400 instance here and let's go actually 31607 19:59:46,280 --> 19:59:51,800 let's go to another one like yale.edu 31608 19:59:48,400 --> 19:59:55,760 here in today's theme and suppose we 31609 19:59:51,800 --> 19:59:58,232 want to do something like uh change the 31610 19:59:55,760 --> 20:00:00,000 Aesthetics of this website well let's do 31611 19:59:58,232 --> 20:00:02,552 how about this over here life at Yale 31612 20:00:00,000 --> 20:00:04,280 let's rightclick on this choose inspect 31613 20:00:02,552 --> 20:00:05,960 that's going to jump to that part of the 31614 20:00:04,280 --> 20:00:08,600 page and notice what you can do here in 31615 20:00:05,960 --> 20:00:10,872 this elements tab we can be a little a 31616 20:00:08,600 --> 20:00:13,800 little playful in return today life at 31617 20:00:10,872 --> 20:00:17,400 Harvard and voila we've now changed 31618 20:00:13,800 --> 20:00:19,552 Yale's website it would seem so have we 31619 20:00:17,400 --> 20:00:20,720 really like hopefully hacking is not 31620 20:00:19,552 --> 20:00:24,120 actually this 31621 20:00:20,720 --> 20:00:25,440 easy what did we actually do based on 31622 20:00:24,120 --> 20:00:28,640 today's mental 31623 20:00:25,440 --> 20:00:31,080 model like I have changed the page 31624 20:00:28,640 --> 20:00:32,480 but yeah just changed how it is for me 31625 20:00:31,080 --> 20:00:34,192 right because my browser just like with 31626 20:00:32,480 --> 20:00:36,080 Phyllis and Brian from the GetGo 31627 20:00:34,192 --> 20:00:38,512 requested Yale's web page I got back a 31628 20:00:36,080 --> 20:00:40,320 virtual envelope containing that HTML as 31629 20:00:38,512 --> 20:00:42,040 we've now called it my browser has a 31630 20:00:40,320 --> 20:00:43,920 local copy it's got its own tree 31631 20:00:42,040 --> 20:00:46,192 otherwise known as a Dom document object 31632 20:00:43,920 --> 20:00:48,512 model built up in its memory and yeah I 31633 20:00:46,192 --> 20:00:50,512 went to town and changed my copy of it 31634 20:00:48,512 --> 20:00:52,160 but of course hopefully I've not changed 31635 20:00:50,512 --> 20:00:55,800 the actual server and in fact if I 31636 20:00:52,160 --> 20:00:58,600 reload Yale's website now hopefully it 31637 20:00:55,800 --> 20:01:01,280 will revert back to indeed yep what it 31638 20:00:58,600 --> 20:01:03,600 should be instead life at Yale but this 31639 20:01:01,280 --> 20:01:05,512 ability in your own browser be it Chrome 31640 20:01:03,600 --> 20:01:07,000 or Firefox or Edge or Safari to have 31641 20:01:05,512 --> 20:01:08,400 these built-in developer tools are very 31642 20:01:07,000 --> 20:01:10,640 powerful because it's going to enable 31643 20:01:08,400 --> 20:01:12,232 you to not only diagnose problems that 31644 20:01:10,640 --> 20:01:14,080 will invariably arise in the coming 31645 20:01:12,232 --> 20:01:15,480 weeks with your own code but is also 31646 20:01:14,080 --> 20:01:17,232 going to allow you to learn from other 31647 20:01:15,480 --> 20:01:19,512 sites like how you can do things and 31648 20:01:17,232 --> 20:01:21,960 Tinker as well but up until now we 31649 20:01:19,512 --> 20:01:24,640 focused only on tags and attributes and 31650 20:01:21,960 --> 20:01:26,280 on the structure of a web page let's now 31651 20:01:24,640 --> 20:01:28,680 focus more on the Aesthetics and 31652 20:01:26,280 --> 20:01:31,232 fine-tuning that it turns out that HTML 31653 20:01:28,680 --> 20:01:33,800 has very limited support for anything 31654 20:01:31,232 --> 20:01:36,440 aesthetic like font sizes and colors and 31655 20:01:33,800 --> 20:01:38,600 so forth and in recent years people have 31656 20:01:36,440 --> 20:01:40,960 used necessarily a second language 31657 20:01:38,600 --> 20:01:43,280 called CSS not a programming language 31658 20:01:40,960 --> 20:01:45,160 again a markup language if you will to 31659 20:01:43,280 --> 20:01:47,440 just fine-tune the Aesthetics of a page 31660 20:01:45,160 --> 20:01:50,320 font sizes colors margins and all of 31661 20:01:47,440 --> 20:01:51,920 that so CSS is going to allow us to 31662 20:01:50,320 --> 20:01:53,440 define a whole bunch of properties which 31663 20:01:51,920 --> 20:01:56,440 is just another group of people's 31664 20:01:53,440 --> 20:01:57,920 terminology for key value pairs indeed 31665 20:01:56,440 --> 20:02:00,440 ever since week five like key value 31666 20:01:57,920 --> 20:02:02,280 pairs are everywhere in the world not 31667 20:02:00,440 --> 20:02:04,720 just at like sweet green and restaurants 31668 20:02:02,280 --> 20:02:07,120 but indeed under in line code and 31669 20:02:04,720 --> 20:02:09,480 languages and Technologies like these so 31670 20:02:07,120 --> 20:02:11,232 properties is the new word in CSS for 31671 20:02:09,480 --> 20:02:13,120 what a moment ago we called attributes 31672 20:02:11,232 --> 20:02:14,600 in H but it's the same idea just 31673 20:02:13,120 --> 20:02:17,832 different vocabulary that you get used 31674 20:02:14,600 --> 20:02:19,960 to over time a few phrases I might use 31675 20:02:17,832 --> 20:02:21,760 now and you'll hear in the coming days 31676 20:02:19,960 --> 20:02:23,720 would be these type selector class 31677 20:02:21,760 --> 20:02:25,720 selector ID selector attribute selector 31678 20:02:23,720 --> 20:02:27,320 which just refer to different techniques 31679 20:02:25,720 --> 20:02:29,600 we're about to see that are going to 31680 20:02:27,320 --> 20:02:31,552 allow you to control more precisely the 31681 20:02:29,600 --> 20:02:33,320 Aesthetics of specific things on the 31682 20:02:31,552 --> 20:02:35,440 page and the way we're going to do this 31683 20:02:33,320 --> 20:02:37,080 is we're going to take our basic HTML 31684 20:02:35,440 --> 20:02:38,640 like we saw earlier and we're going to 31685 20:02:37,080 --> 20:02:40,720 introduce in the next few minutes just a 31686 20:02:38,640 --> 20:02:43,160 couple of more tags and or attributes 31687 20:02:40,720 --> 20:02:45,920 one we're going to introduce you to a 31688 20:02:43,160 --> 20:02:48,280 tag called style which nicely named 31689 20:02:45,920 --> 20:02:50,480 allows you to um allows you to control 31690 20:02:48,280 --> 20:02:52,600 the style the Aesthetics the visuals of 31691 20:02:50,480 --> 20:02:55,552 the web page or we're going to introduce 31692 20:02:52,600 --> 20:02:57,320 you to a link tag which very confusingly 31693 20:02:55,552 --> 20:02:59,552 does not give you a link that you can 31694 20:02:57,320 --> 20:03:02,232 click on it just links to another file 31695 20:02:59,552 --> 20:03:04,512 that then gets automatically included or 31696 20:03:02,232 --> 20:03:06,760 imported to borrow our language from C 31697 20:03:04,512 --> 20:03:08,920 or in Python but same idea this will 31698 20:03:06,760 --> 20:03:10,720 allow us to include secondary files and 31699 20:03:08,920 --> 20:03:12,080 we're going to ultimately show you how 31700 20:03:10,720 --> 20:03:13,960 you can leverage third third party 31701 20:03:12,080 --> 20:03:15,720 Frameworks libraries that other people 31702 20:03:13,960 --> 20:03:17,280 wrote so as to not get stuck in the 31703 20:03:15,720 --> 20:03:18,800 weeds of all the fine tuning of 31704 20:03:17,280 --> 20:03:20,160 Aesthetics and just make pretty things 31705 20:03:18,800 --> 20:03:21,512 fast so you can focus really on the 31706 20:03:20,160 --> 20:03:23,480 intellectually interesting part if 31707 20:03:21,512 --> 20:03:25,680 that's your choice of building the 31708 20:03:23,480 --> 20:03:27,480 content the site out the application out 31709 20:03:25,680 --> 20:03:30,120 yourself all right so with that said let 31710 20:03:27,480 --> 20:03:32,040 me go back to vs code here and let me go 31711 20:03:30,120 --> 20:03:34,640 ahead and create a simple example called 31712 20:03:32,040 --> 20:03:36,872 home.html like a very simple homepage 31713 20:03:34,640 --> 20:03:39,232 for John Harvard for instance let me 31714 20:03:36,872 --> 20:03:41,000 give myself three paragraphs initially 31715 20:03:39,232 --> 20:03:43,360 the first of which is just going to have 31716 20:03:41,000 --> 20:03:44,720 uh the person name the second paragraph 31717 20:03:43,360 --> 20:03:45,920 is going to say something like welcome 31718 20:03:44,720 --> 20:03:48,320 to my 31719 20:03:45,920 --> 20:03:49,512 homepage uh to greet visitors and the 31720 20:03:48,320 --> 20:03:53,832 third is going to be like a little 31721 20:03:49,512 --> 20:03:57,232 footer like copyright uh how about John 31722 20:03:53,832 --> 20:03:58,872 Harvard 1636 or something like that all 31723 20:03:57,232 --> 20:04:01,320 right let me go back to my other tab as 31724 20:03:58,872 --> 20:04:04,360 before reload my index there's my new 31725 20:04:01,320 --> 20:04:06,160 file home.html and I'll click that and 31726 20:04:04,360 --> 20:04:08,760 you'll see okay I mean this is sort of 31727 20:04:06,160 --> 20:04:11,232 1636 style web page super simple all 31728 20:04:08,760 --> 20:04:12,080 text nothing really interesting going on 31729 20:04:11,232 --> 20:04:13,960 there 31730 20:04:12,080 --> 20:04:15,512 but we can start to style it a little 31731 20:04:13,960 --> 20:04:17,280 differently like if the title of the 31732 20:04:15,512 --> 20:04:18,680 page is John Harvard and then it's 31733 20:04:17,280 --> 20:04:20,480 welcome to my homepage and then this 31734 20:04:18,680 --> 20:04:22,832 less important footer why don't we have 31735 20:04:20,480 --> 20:04:24,440 the text be large then medium then small 31736 20:04:22,832 --> 20:04:27,040 so something arbitrary but a little more 31737 20:04:24,440 --> 20:04:30,872 nuanced so let me go back to vs code 31738 20:04:27,040 --> 20:04:33,720 here and in my home.html file let me 31739 20:04:30,872 --> 20:04:35,512 introduce not yet the style tag but what 31740 20:04:33,720 --> 20:04:37,920 I'm going to call temporarily the style 31741 20:04:35,512 --> 20:04:39,680 attribute both indeed exist this one's 31742 20:04:37,920 --> 20:04:41,480 simpler and it's going to be correct but 31743 20:04:39,680 --> 20:04:43,280 we'll see in a moment not as well 31744 20:04:41,480 --> 20:04:45,760 designed arguably as is often our 31745 20:04:43,280 --> 20:04:48,760 narrative so inside of the style tag you 31746 20:04:45,760 --> 20:04:50,600 can put this language called CSS key 31747 20:04:48,760 --> 20:04:52,160 value pairs otherwise known as 31748 20:04:50,600 --> 20:04:54,680 properties the only way you know what 31749 20:04:52,160 --> 20:04:56,360 properties exist what keys exist is by 31750 20:04:54,680 --> 20:04:57,552 taking a class reading a book looking at 31751 20:04:56,360 --> 20:04:59,192 an online reference and we're going to 31752 20:04:57,552 --> 20:05:01,040 give you just a sampling of what's out 31753 20:04:59,192 --> 20:05:03,720 there so suppose I want to control the 31754 20:05:01,040 --> 20:05:06,360 font size of this first paragraph I can 31755 20:05:03,720 --> 20:05:09,320 literally say font Das size in all lower 31756 20:05:06,360 --> 20:05:12,400 case colon and then a word like large or 31757 20:05:09,320 --> 20:05:13,960 I can specify 12p point or 18 point or 31758 20:05:12,400 --> 20:05:16,440 something more precise like that like 31759 20:05:13,960 --> 20:05:19,000 from Google Docs or Microsoft Word and 31760 20:05:16,440 --> 20:05:22,480 suppose I want to make this text down 31761 20:05:19,000 --> 20:05:25,680 here uh medium well uh I'll do quote 31762 20:05:22,480 --> 20:05:30,120 unquote font size colon medium and down 31763 20:05:25,680 --> 20:05:32,512 here I'll do style equals uh font Das 31764 20:05:30,120 --> 20:05:34,920 size small so I'm going to start with 31765 20:05:32,512 --> 20:05:37,192 just these three key value pairs same 31766 20:05:34,920 --> 20:05:38,800 key but different values I'll go back to 31767 20:05:37,192 --> 20:05:40,320 my page and in a moment I'll reload and 31768 20:05:38,800 --> 20:05:42,080 it's going to be somewhat subtle but 31769 20:05:40,320 --> 20:05:45,040 watch how the font size do change when I 31770 20:05:42,080 --> 20:05:46,920 reload now all right so got a little 31771 20:05:45,040 --> 20:05:49,000 bigger middle one's about the same and 31772 20:05:46,920 --> 20:05:50,640 the last one is a little smaller what if 31773 20:05:49,000 --> 20:05:52,680 I want to center it just like many web 31774 20:05:50,640 --> 20:05:54,400 pages have the text like this centered 31775 20:05:52,680 --> 20:05:56,120 well I can separate these key value 31776 20:05:54,400 --> 20:05:57,920 pairs with semicolons and I'm sorry 31777 20:05:56,120 --> 20:06:02,160 semicolons are kind of sort of back with 31778 20:05:57,920 --> 20:06:03,920 CSS but I can do text-align Colon Center 31779 20:06:02,160 --> 20:06:05,960 strictly speaking I don't need the last 31780 20:06:03,920 --> 20:06:07,552 semicolon if there's no more key value 31781 20:06:05,960 --> 20:06:10,960 pairs but I'll just do it to be 31782 20:06:07,552 --> 20:06:12,640 consistent uh text align Colon Center 31783 20:06:10,960 --> 20:06:16,720 and then down here after another 31784 20:06:12,640 --> 20:06:18,552 semicolon text align Colon Center all 31785 20:06:16,720 --> 20:06:20,760 right let's go back reload now it's 31786 20:06:18,552 --> 20:06:22,480 going to be much more obvious the change 31787 20:06:20,760 --> 20:06:24,320 and we now have the beginnings of a 31788 20:06:22,480 --> 20:06:26,280 homepage still pretty basic but at least 31789 20:06:24,320 --> 20:06:27,720 it's a little more interesting turns out 31790 20:06:26,280 --> 20:06:29,720 we can do a little better with the 31791 20:06:27,720 --> 20:06:31,480 copyright symbol like most computers 31792 20:06:29,720 --> 20:06:34,552 actually have support for a circle with 31793 20:06:31,480 --> 20:06:36,600 a c in it but you can't just do that 31794 20:06:34,552 --> 20:06:38,280 with uh text like this there's different 31795 20:06:36,600 --> 20:06:39,760 ways to do this you could copy paste it 31796 20:06:38,280 --> 20:06:41,232 from like a website that already has it 31797 20:06:39,760 --> 20:06:43,440 so you don't have to figure out the mag 31798 20:06:41,232 --> 20:06:45,040 iCal keystroke on your Mac or PC but 31799 20:06:43,440 --> 20:06:47,552 there's also in HTML what are called 31800 20:06:45,040 --> 20:06:51,040 entities and you can actually specify 31801 20:06:47,552 --> 20:06:54,512 using heximal or decimal codes numbers 31802 20:06:51,040 --> 20:06:57,160 like this H1 169 semicolon after an 31803 20:06:54,512 --> 20:06:58,600 Amper sand and this is a special symbol 31804 20:06:57,160 --> 20:07:00,160 that you can look up in any online 31805 20:06:58,600 --> 20:07:01,872 reference for like special characters 31806 20:07:00,160 --> 20:07:04,872 that are hard or impossible to type 31807 20:07:01,872 --> 20:07:06,680 manually at your keyboard and this let 31808 20:07:04,872 --> 20:07:08,440 me zoom in just so it's obvious if I 31809 20:07:06,680 --> 20:07:11,720 reload now instead of being two 31810 20:07:08,440 --> 20:07:14,192 parentheses and a c character 31811 20:07:11,720 --> 20:07:15,640 now it's a proper Copyright symbol so 31812 20:07:14,192 --> 20:07:17,280 you'll see these out there they're not 31813 20:07:15,640 --> 20:07:19,320 necessarily that frequently used 31814 20:07:17,280 --> 20:07:21,512 nowadays but it's good to know that they 31815 20:07:19,320 --> 20:07:24,600 exist but let me go back now to my code 31816 20:07:21,512 --> 20:07:27,360 and propose that while correct uh this 31817 20:07:24,600 --> 20:07:29,040 is arguably not very welld designed and 31818 20:07:27,360 --> 20:07:31,832 even if you've never seen HTML never 31819 20:07:29,040 --> 20:07:36,832 seen CSS before what Instinct might you 31820 20:07:31,832 --> 20:07:38,760 have for why this is poorly 31821 20:07:36,832 --> 20:07:41,400 designed 31822 20:07:38,760 --> 20:07:43,440 yeah there's repetition right in general 31823 20:07:41,400 --> 20:07:45,680 in the past several weeks see python SQL 31824 20:07:43,440 --> 20:07:47,800 like repetition generally bad and and 31825 20:07:45,680 --> 20:07:48,960 sloppy and it's not going to scale well 31826 20:07:47,800 --> 20:07:50,800 so the repetition I think you're 31827 20:07:48,960 --> 20:07:53,320 probably alluding to is textalign Center 31828 20:07:50,800 --> 20:07:56,640 textalign Center textalign Center well 31829 20:07:53,320 --> 20:07:59,760 we can factor that out in CSS the C in 31830 20:07:56,640 --> 20:08:01,800 CSS means cascading and this means that 31831 20:07:59,760 --> 20:08:04,872 if you move some properties to like a 31832 20:08:01,800 --> 20:08:07,040 parent or a grandparent uh the children 31833 20:08:04,872 --> 20:08:08,872 or grandchildren will inherit those 31834 20:08:07,040 --> 20:08:11,120 properties that is they will Cascade 31835 20:08:08,872 --> 20:08:12,920 down the family tree so to speak and so 31836 20:08:11,120 --> 20:08:15,320 so let me go ahead and remove all of 31837 20:08:12,920 --> 20:08:17,680 these since I claim they're indeed 31838 20:08:15,320 --> 20:08:20,920 redundant and let me preserve just one 31839 20:08:17,680 --> 20:08:22,920 of them by well let me do this it's not 31840 20:08:20,920 --> 20:08:24,720 quite right to put a a paragraph inside 31841 20:08:22,920 --> 20:08:26,480 of a paragraph like that's just not a 31842 20:08:24,720 --> 20:08:28,400 thing in English writing or in writing 31843 20:08:26,480 --> 20:08:30,760 more generally so I'm going to do one 31844 20:08:28,400 --> 20:08:32,440 thing first it turns out that these two 31845 20:08:30,760 --> 20:08:34,872 are arguably not paragraphs right this 31846 20:08:32,440 --> 20:08:37,320 is like a header the body the essence of 31847 20:08:34,872 --> 20:08:39,600 the page and then the footer so if a 31848 20:08:37,320 --> 20:08:41,320 paragraph isn't quite the right English 31849 20:08:39,600 --> 20:08:43,000 semantics you can actually use more 31850 20:08:41,320 --> 20:08:45,440 generically a tag that's all over the 31851 20:08:43,000 --> 20:08:47,760 internet called div for division of the 31852 20:08:45,440 --> 20:08:49,800 page and this is just a very generic 31853 20:08:47,760 --> 20:08:51,760 term for like a big rectangular region 31854 20:08:49,800 --> 20:08:53,192 that divides the page again and again 31855 20:08:51,760 --> 20:08:56,040 just so that you can think about 31856 20:08:53,192 --> 20:08:57,760 different regions now that I have div 31857 20:08:56,040 --> 20:08:59,192 which really has no more meaning than 31858 20:08:57,760 --> 20:09:01,232 that it's a division of the page 31859 20:08:59,192 --> 20:09:02,920 interpret as you will now I can have 31860 20:09:01,232 --> 20:09:05,640 multiple ones of these and let me go 31861 20:09:02,920 --> 20:09:08,160 ahead and open a div tag here let me 31862 20:09:05,640 --> 20:09:10,080 close a new div tag here and then just 31863 20:09:08,160 --> 20:09:11,720 to keep everything tidy I'm going to 31864 20:09:10,080 --> 20:09:13,640 highlight everything in between and hit 31865 20:09:11,720 --> 20:09:16,800 Tab and that just automatically indents 31866 20:09:13,640 --> 20:09:18,600 everything for me now I have a three 31867 20:09:16,800 --> 20:09:21,192 divs inside of another div and that's 31868 20:09:18,600 --> 20:09:23,320 totally fine this is very commonly done 31869 20:09:21,192 --> 20:09:26,760 now I'm going to do this style equals 31870 20:09:23,320 --> 20:09:29,832 quote unquote text align Colon Center 31871 20:09:26,760 --> 20:09:32,832 semicolon or not and now I have some 31872 20:09:29,832 --> 20:09:35,000 cascading capabilities now the parent of 31873 20:09:32,832 --> 20:09:36,680 those three children John Harvard welome 31874 20:09:35,000 --> 20:09:38,600 to my homepage and the copyright will 31875 20:09:36,680 --> 20:09:40,512 now all inherit that property so when I 31876 20:09:38,600 --> 20:09:42,872 hit reload nothing aesthetically has 31877 20:09:40,512 --> 20:09:45,360 changed whoops sorry 31878 20:09:42,872 --> 20:09:47,512 um I should have done reload slightly 31879 20:09:45,360 --> 20:09:49,320 earlier when you use a div instead of a 31880 20:09:47,512 --> 20:09:50,960 paragraph it actually gets rid of the 31881 20:09:49,320 --> 20:09:52,440 par uh the space between those 31882 20:09:50,960 --> 20:09:54,400 paragraphs it just sandwiches them a 31883 20:09:52,440 --> 20:09:57,000 little closer together I can fix this in 31884 20:09:54,400 --> 20:09:58,760 another way but that aside everything is 31885 20:09:57,000 --> 20:10:00,872 still centered and the text is still 31886 20:09:58,760 --> 20:10:02,760 large medium and small but I should have 31887 20:10:00,872 --> 20:10:04,400 called out that change in the paragraph 31888 20:10:02,760 --> 20:10:07,232 spacing but we could bring that back 31889 20:10:04,400 --> 20:10:09,600 before long if we wanted now what more 31890 20:10:07,232 --> 20:10:11,600 could I do to maybe improve this well 31891 20:10:09,600 --> 20:10:13,832 strictly speaking I don't really need 31892 20:10:11,600 --> 20:10:16,832 that parent div right because these 31893 20:10:13,832 --> 20:10:18,400 three divs inside already had a parent 31894 20:10:16,832 --> 20:10:20,872 so let me actually get rid of that new 31895 20:10:18,400 --> 20:10:22,320 div just undo what I did I'll highlight 31896 20:10:20,872 --> 20:10:24,920 this and if you haven't seen this trick 31897 20:10:22,320 --> 20:10:27,552 shift tab will unindent nicely which is 31898 20:10:24,920 --> 20:10:30,040 perhaps helpful I could just put that 31899 20:10:27,552 --> 20:10:34,512 text align Center on the body tag so 31900 20:10:30,040 --> 20:10:36,760 text align Colon Center quote unquote 31901 20:10:34,512 --> 20:10:38,640 this two would work as well so long as 31902 20:10:36,760 --> 20:10:40,920 you go up the family tree so to speak 31903 20:10:38,640 --> 20:10:43,800 reload and now indeed there's nothing 31904 20:10:40,920 --> 20:10:45,320 aesthetically that has changed this time 31905 20:10:43,800 --> 20:10:46,760 but it turns out nowadays the web is 31906 20:10:45,320 --> 20:10:47,960 getting a little more sophisticated and 31907 20:10:46,760 --> 20:10:49,872 even though you will see so many 31908 20:10:47,960 --> 20:10:52,512 examples online and tutorials and books 31909 20:10:49,872 --> 20:10:55,040 using div div div div all over the place 31910 20:10:52,512 --> 20:10:56,400 there are newer semantic tags semantic 31911 20:10:55,040 --> 20:10:58,232 just means they have more meaning than 31912 20:10:56,400 --> 20:11:01,640 this generic notion of a division and if 31913 20:10:58,232 --> 20:11:03,120 you look up the a documentation for HTML 31914 20:11:01,640 --> 20:11:05,760 you'll see that if you want to have a 31915 20:11:03,120 --> 20:11:07,800 header on a page not a heading like H1 31916 20:11:05,760 --> 20:11:10,000 H2 but a header there's literally 31917 20:11:07,800 --> 20:11:12,080 nowadays a header tag and this is 31918 20:11:10,000 --> 20:11:14,040 marginally better because it now says 31919 20:11:12,080 --> 20:11:16,080 what it is search engines like Google 31920 20:11:14,040 --> 20:11:17,600 and Bing can detect oh that's the header 31921 20:11:16,080 --> 20:11:19,000 of the page maybe we should use this and 31922 20:11:17,600 --> 20:11:21,120 give it more prominence in the search 31923 20:11:19,000 --> 20:11:23,232 results you can then have a main part of 31924 20:11:21,120 --> 20:11:25,280 the page so literally a tag called main 31925 20:11:23,232 --> 20:11:27,232 nowadays you can literally have a footer 31926 20:11:25,280 --> 20:11:29,800 of the page and again these are often 31927 20:11:27,232 --> 20:11:31,160 useful for screen readers to help recite 31928 20:11:29,800 --> 20:11:32,920 things verbally for folks who might 31929 20:11:31,160 --> 20:11:35,040 otherwise not be able to read them and 31930 20:11:32,920 --> 20:11:37,000 probably these screen readers might 31931 20:11:35,040 --> 20:11:38,720 highlight the header and the main part 31932 20:11:37,000 --> 20:11:40,400 but maybe not might not spend time for 31933 20:11:38,720 --> 20:11:42,400 the user on the footer which is arguably 31934 20:11:40,400 --> 20:11:45,192 a little less important semantically 31935 20:11:42,400 --> 20:11:46,800 usually um or search engines again now 31936 20:11:45,192 --> 20:11:48,280 know what's the header what's the footer 31937 20:11:46,800 --> 20:11:50,680 what's the main part of the page so they 31938 20:11:48,280 --> 20:11:52,800 know what to search and analyze so this 31939 20:11:50,680 --> 20:11:54,872 would arguably be a a better design 31940 20:11:52,800 --> 20:11:58,680 nowadays as 31941 20:11:54,872 --> 20:12:00,600 well but what else remains as a problem 31942 20:11:58,680 --> 20:12:02,600 well this is now getting a little bit 31943 20:12:00,600 --> 20:12:05,160 more subtle and takes some experience 31944 20:12:02,600 --> 20:12:07,400 but this practice of putting HTML and 31945 20:12:05,160 --> 20:12:09,320 CSS all in the same file it's a little 31946 20:12:07,400 --> 20:12:11,360 sloppy why because it means I'm 31947 20:12:09,320 --> 20:12:13,160 co-mingling my data with the 31948 20:12:11,360 --> 20:12:15,192 presentation thereof like the juicy 31949 20:12:13,160 --> 20:12:17,080 stuff I care about like John Harvard and 31950 20:12:15,192 --> 20:12:18,920 the phrase welcome to my homepage and 31951 20:12:17,080 --> 20:12:21,080 all of the Aesthetics that I might want 31952 20:12:18,920 --> 20:12:23,480 to change over time and honestly because 31953 20:12:21,080 --> 20:12:24,680 everything is currently in one big file 31954 20:12:23,480 --> 20:12:26,760 it's going to make it really hard for me 31955 20:12:24,680 --> 20:12:28,872 to collaborate with a classmate or a 31956 20:12:26,760 --> 20:12:31,120 colleague at work so that maybe I do the 31957 20:12:28,872 --> 20:12:32,640 HTML they do the CSS like uhuh not if 31958 20:12:31,120 --> 20:12:34,760 you're all working in the same file it 31959 20:12:32,640 --> 20:12:36,480 would be a nightmare even if you use vs 31960 20:12:34,760 --> 20:12:38,160 code sharing feature like Google Docs 31961 20:12:36,480 --> 20:12:39,720 and both are typing at the same time 31962 20:12:38,160 --> 20:12:41,552 like you're going to mess up somehow 31963 20:12:39,720 --> 20:12:43,872 it'd be nice if we could separate these 31964 20:12:41,552 --> 20:12:45,832 two languages well one way to do that 31965 20:12:43,872 --> 20:12:48,720 would be as follows let me get rid of 31966 20:12:45,832 --> 20:12:50,872 all of the style tags sorry style 31967 20:12:48,720 --> 20:12:54,512 attributes that I've added up until 31968 20:12:50,872 --> 20:12:57,480 now on all four now of these tags and 31969 20:12:54,512 --> 20:13:00,040 let me introduce the style tag that we 31970 20:12:57,480 --> 20:13:01,920 saw on the slide earlier instead I'm 31971 20:13:00,040 --> 20:13:03,440 going to go up here into the head of the 31972 20:13:01,920 --> 20:13:05,680 page which is where technically these 31973 20:13:03,440 --> 20:13:07,640 style tags must go so that they're 31974 20:13:05,680 --> 20:13:10,120 already loaded into memory before the 31975 20:13:07,640 --> 20:13:12,400 body is even analyzed by the browser and 31976 20:13:10,120 --> 20:13:15,960 inside of the the style tag I'm actually 31977 20:13:12,400 --> 20:13:18,720 going to select the HTML elements that I 31978 20:13:15,960 --> 20:13:20,760 want to stylize if you will so if I want 31979 20:13:18,720 --> 20:13:22,080 to change the body's Aesthetics I'm 31980 20:13:20,760 --> 20:13:25,232 going to literally type the name of that 31981 20:13:22,080 --> 20:13:28,160 tag body and then I'm sorry curly braces 31982 20:13:25,232 --> 20:13:31,440 are back also from c u inside of these 31983 20:13:28,160 --> 20:13:33,232 curly braces I'm going to put text align 31984 20:13:31,440 --> 20:13:35,000 Center so the key value pairs are the 31985 20:13:33,232 --> 20:13:37,160 same the only new thing I've done is 31986 20:13:35,000 --> 20:13:39,760 I've moved some of the syntax up to this 31987 20:13:37,160 --> 20:13:42,512 new style tag in the head if I want to 31988 20:13:39,760 --> 20:13:44,320 now control the header tag as well I can 31989 20:13:42,512 --> 20:13:46,040 use the same curly braces this is 31990 20:13:44,320 --> 20:13:48,080 convention to put the open curly brace 31991 20:13:46,040 --> 20:13:49,960 on the same line the closed curly brace 31992 20:13:48,080 --> 20:13:52,040 on another the browser doesn't really 31993 20:13:49,960 --> 20:13:54,440 care but this is a common CSS style 31994 20:13:52,040 --> 20:13:57,512 convention I'm going to do font size 31995 20:13:54,440 --> 20:14:00,440 large semicolon then for the main tag 31996 20:13:57,512 --> 20:14:02,400 I'm going to do font size medium and 31997 20:14:00,440 --> 20:14:05,960 then for the footer tag I'm going to do 31998 20:14:02,400 --> 20:14:07,440 font size small so same exact thing and 31999 20:14:05,960 --> 20:14:09,120 it's admittedly a little bit more 32000 20:14:07,440 --> 20:14:10,640 verbose it's taking up more lines of 32001 20:14:09,120 --> 20:14:13,320 code it doesn't all quite fit on the 32002 20:14:10,640 --> 20:14:15,280 screen but if you scroll back down now 32003 20:14:13,320 --> 20:14:17,440 and you'll acquire an eye for this this 32004 20:14:15,280 --> 20:14:19,640 is just better like it's just more 32005 20:14:17,440 --> 20:14:21,640 compact it's more readable the the 32006 20:14:19,640 --> 20:14:24,440 content the data jumps out and there's 32007 20:14:21,640 --> 20:14:27,120 no visual distractions like the CSS 32008 20:14:24,440 --> 20:14:29,512 properties as before upside here too is 32009 20:14:27,120 --> 20:14:30,960 that we don't actually need to uh this 32010 20:14:29,512 --> 20:14:33,232 doesn't actually change the Aesthetics 32011 20:14:30,960 --> 20:14:35,680 if I reload the same page it still looks 32012 20:14:33,232 --> 20:14:37,872 the same but I've taken a step toward 32013 20:14:35,680 --> 20:14:39,440 some slightly better design but let me 32014 20:14:37,872 --> 20:14:42,832 propose that there's other ways to do 32015 20:14:39,440 --> 20:14:45,800 this too we just selected things by way 32016 20:14:42,832 --> 20:14:47,320 of their uh type so that was a so-called 32017 20:14:45,800 --> 20:14:50,640 type selector when I literally just 32018 20:14:47,320 --> 20:14:53,360 specifi the type of tag body header main 32019 20:14:50,640 --> 20:14:54,872 footer but there's other ways that now 32020 20:14:53,360 --> 20:14:57,440 we can lay the foundation for making 32021 20:14:54,872 --> 20:15:00,080 reusable CSS that you and colleagues and 32022 20:14:57,440 --> 20:15:02,512 classmates can use and reuse in multiple 32023 20:15:00,080 --> 20:15:04,120 files and even in multiple projects so 32024 20:15:02,512 --> 20:15:06,080 let me actually go ahead and do this 32025 20:15:04,120 --> 20:15:08,232 instead of just very explicitly saying I 32026 20:15:06,080 --> 20:15:10,832 want the body to be centered let me 32027 20:15:08,232 --> 20:15:13,552 invent an adjective if you will and let 32028 20:15:10,832 --> 20:15:16,720 me change this to do centered and this 32029 20:15:13,552 --> 20:15:19,640 new uh this new vocabulary word centered 32030 20:15:16,720 --> 20:15:21,720 will literally mean texal Center let me 32031 20:15:19,640 --> 20:15:24,680 go ahead here and I'm just going to 32032 20:15:21,720 --> 20:15:27,400 create a new adjective called large a 32033 20:15:24,680 --> 20:15:29,640 new adjective called medium and a new 32034 20:15:27,400 --> 20:15:31,800 adjective called small they are 32035 20:15:29,640 --> 20:15:34,040 deliberately consistent with what the 32036 20:15:31,800 --> 20:15:36,680 properties do but these are now my own 32037 20:15:34,040 --> 20:15:39,600 vocabulary words and they are called 32038 20:15:36,680 --> 20:15:41,120 classes so a class is just a collection 32039 20:15:39,600 --> 20:15:42,800 of key value 32040 20:15:41,120 --> 20:15:45,400 a collection of properties that you get 32041 20:15:42,800 --> 20:15:48,832 to invent for yourself and what it lets 32042 20:15:45,400 --> 20:15:51,360 you do now is this now if I want the 32043 20:15:48,832 --> 20:15:52,920 whole body to be centered I can add this 32044 20:15:51,360 --> 20:15:56,600 tag which we actually saw briefly in 32045 20:15:52,920 --> 20:15:57,920 Yale's HTML class equals centered down 32046 20:15:56,600 --> 20:16:00,232 here in the header if I want this to be 32047 20:15:57,920 --> 20:16:02,512 large I can say class equals quote 32048 20:16:00,232 --> 20:16:05,832 unquote large down here on Main I can 32049 20:16:02,512 --> 20:16:07,960 say class equals quote unquote medium 32050 20:16:05,832 --> 20:16:10,080 and down here I can have class equals 32051 20:16:07,960 --> 20:16:12,512 quote unquote small now I have taken one 32052 20:16:10,080 --> 20:16:14,800 step backward by read addding some of 32053 20:16:12,512 --> 20:16:16,720 the Aesthetics to the page but it's not 32054 20:16:14,800 --> 20:16:18,600 the actual properties it's not the key 32055 20:16:16,720 --> 20:16:20,192 value pairs it's now more semantically 32056 20:16:18,600 --> 20:16:22,640 nice because now I just know from 32057 20:16:20,192 --> 20:16:23,920 reading the HTML what these things are 32058 20:16:22,640 --> 20:16:26,080 going to look like whereas the 32059 20:16:23,920 --> 20:16:28,360 implementation details for all four of 32060 20:16:26,080 --> 20:16:30,080 those adjectives is now relegated up 32061 20:16:28,360 --> 20:16:32,192 above and these are literally my words I 32062 20:16:30,080 --> 20:16:34,160 could change it to Fu and use class 32063 20:16:32,192 --> 20:16:35,600 equals quote unquote Foo but obviously 32064 20:16:34,160 --> 20:16:39,320 that would not be the best choice of 32065 20:16:35,600 --> 20:16:42,680 words in this case all right any 32066 20:16:39,320 --> 20:16:46,000 questions on this this now is what we 32067 20:16:42,680 --> 20:16:47,480 would call a class selector by using 32068 20:16:46,000 --> 20:16:49,680 literally the dot even though the dot 32069 20:16:47,480 --> 20:16:51,192 does not appear elsewhere but dot means 32070 20:16:49,680 --> 20:16:53,512 this is a 32071 20:16:51,192 --> 20:16:55,040 class these are not always the best 32072 20:16:53,512 --> 20:16:57,192 syntactic design decisions that the 32073 20:16:55,040 --> 20:16:59,480 world makes all right well one last 32074 20:16:57,192 --> 20:17:01,080 trick then notice that this is a little 32075 20:16:59,480 --> 20:17:02,760 Annoying that I'm still working in the 32076 20:17:01,080 --> 20:17:04,480 same file and if my classmate wants to 32077 20:17:02,760 --> 20:17:06,000 clean up my Aesthetics make my homepage 32078 20:17:04,480 --> 20:17:07,872 look way better if my colleague wants to 32079 20:17:06,000 --> 20:17:09,760 do the same wouldn't it be nice if we 32080 20:17:07,872 --> 20:17:11,720 could actually move all of this code to 32081 20:17:09,760 --> 20:17:14,760 a different function file like a python 32082 20:17:11,720 --> 20:17:17,232 library or a c header file well you can 32083 20:17:14,760 --> 20:17:20,320 let me go ahead and delete that whole 32084 20:17:17,232 --> 20:17:24,232 style tag let me add a confusingly named 32085 20:17:20,320 --> 20:17:28,232 link tag the href of which let's call a 32086 20:17:24,232 --> 20:17:30,440 new file styles.css and let's say that 32087 20:17:28,232 --> 20:17:32,760 the relationship of that file is that of 32088 20:17:30,440 --> 20:17:34,320 stylesheet so this is a term of Art in 32089 20:17:32,760 --> 20:17:36,280 the world of web development a 32090 20:17:34,320 --> 20:17:38,800 stylesheet is a text file that contains 32091 20:17:36,280 --> 20:17:41,320 lots of styles lots of CSS properties 32092 20:17:38,800 --> 20:17:44,760 let me open my terminal real fast 32093 20:17:41,320 --> 20:17:46,872 and let me do code of 32094 20:17:44,760 --> 20:17:49,040 styles.css 32095 20:17:46,872 --> 20:17:51,440 enter and in this file I'm going to 32096 20:17:49,040 --> 20:17:53,320 paste all of those same lines as earlier 32097 20:17:51,440 --> 20:17:55,080 but now they're in a separate file and 32098 20:17:53,320 --> 20:17:56,832 indeed if I hide my terminal window and 32099 20:17:55,080 --> 20:17:58,360 I give this file to a colleague they can 32100 20:17:56,832 --> 20:17:59,800 now work on the Aesthetics of the page 32101 20:17:58,360 --> 20:18:01,552 and make things a lot prettier than this 32102 20:17:59,800 --> 20:18:04,320 maybe use specific font sizes maybe add 32103 20:18:01,552 --> 20:18:06,640 colors and the like whereas I can focus 32104 20:18:04,320 --> 20:18:08,720 entirely on the HTML because this file 32105 20:18:06,640 --> 20:18:10,760 now will reference that other and if I 32106 20:18:08,720 --> 20:18:12,360 go back to my other Tab and reload Cod 32107 20:18:10,760 --> 20:18:15,400 the content's going to be exactly the 32108 20:18:12,360 --> 20:18:17,640 same but now I'm using some separate 32109 20:18:15,400 --> 20:18:20,760 file 32110 20:18:17,640 --> 20:18:22,960 instead any questions now about these 32111 20:18:20,760 --> 20:18:26,920 techniques 32112 20:18:22,960 --> 20:18:29,192 here no all right so with that said let 32113 20:18:26,920 --> 20:18:31,040 me show just one example now of what I 32114 20:18:29,192 --> 20:18:32,480 called a moment ago Frameworks and this 32115 20:18:31,040 --> 20:18:33,920 is where web development gets kind of 32116 20:18:32,480 --> 20:18:35,640 fun at least if you like this especially 32117 20:18:33,920 --> 20:18:37,040 if you like the sort of logical design 32118 20:18:35,640 --> 20:18:38,400 the presentation of information you care 32119 20:18:37,040 --> 20:18:40,192 about but you really don't want to 32120 20:18:38,400 --> 20:18:41,920 struggle with like font sizes and colors 32121 20:18:40,192 --> 20:18:44,280 and getting everything Pixel Perfect so 32122 20:18:41,920 --> 20:18:46,640 to speak let me propose that I open up 32123 20:18:44,280 --> 20:18:49,640 here an example in just a moment in vs 32124 20:18:46,640 --> 20:18:53,600 code that I prepared in advance and this 32125 20:18:49,640 --> 20:18:55,232 one is going to be an opportunity to 32126 20:18:53,600 --> 20:18:58,040 consider how you might take some of the 32127 20:18:55,232 --> 20:19:00,280 data from last week which we had wherein 32128 20:18:58,040 --> 20:19:02,800 we collected everyone's favorites and 32129 20:19:00,280 --> 20:19:03,960 lay it out in a really big HTML table so 32130 20:19:02,800 --> 20:19:05,360 I wrote this out in advance because it 32131 20:19:03,960 --> 20:19:07,160 was a huge amount of data but it's the 32132 20:19:05,360 --> 20:19:09,160 same data from the Google form from last 32133 20:19:07,160 --> 20:19:12,080 week and you'll see already the hints of 32134 20:19:09,160 --> 20:19:14,160 a table tag and these TRS and I added a 32135 20:19:12,080 --> 20:19:15,512 few other tags for Aesthetics it turns 32136 20:19:14,160 --> 20:19:17,552 out when you have a more visually 32137 20:19:15,512 --> 20:19:19,400 interesting header for your table 32138 20:19:17,552 --> 20:19:21,192 there's another tag called T head 32139 20:19:19,400 --> 20:19:22,512 there's another tag called T body these 32140 20:19:21,192 --> 20:19:23,720 are not all that intellectually 32141 20:19:22,512 --> 20:19:25,512 interesting I just read the 32142 20:19:23,720 --> 20:19:27,680 documentation and realized oh to make 32143 20:19:25,512 --> 20:19:29,960 things prettier I need a t head a t body 32144 20:19:27,680 --> 20:19:32,552 and so forth but what's interesting here 32145 20:19:29,960 --> 20:19:35,192 is that if I go to my index here and 32146 20:19:32,552 --> 20:19:37,360 open this file called favorites. HTML 32147 20:19:35,192 --> 20:19:39,600 here is all of the data from last week's 32148 20:19:37,360 --> 20:19:42,480 Google spreadsheet which we exported as 32149 20:19:39,600 --> 20:19:45,480 CS and I manually before class converted 32150 20:19:42,480 --> 20:19:47,160 to just HTML it's indeed a table but 32151 20:19:45,480 --> 20:19:48,960 it's really not pretty like the columns 32152 20:19:47,160 --> 20:19:51,000 are really close together it's kind of 32153 20:19:48,960 --> 20:19:53,680 hard to distinguish one row from another 32154 20:19:51,000 --> 20:19:55,720 but this is just raw HTML written by me 32155 20:19:53,680 --> 20:19:58,000 now I could use CSS and some of the 32156 20:19:55,720 --> 20:19:59,920 tricks we just saw to maybe change font 32157 20:19:58,000 --> 20:20:01,440 size there's ways to change color 32158 20:19:59,920 --> 20:20:04,080 background color and a lot of things 32159 20:20:01,440 --> 20:20:05,800 like that but honestly other surely 32160 20:20:04,080 --> 20:20:07,800 other people in the world have presented 32161 20:20:05,800 --> 20:20:09,400 tabular data in pretty ways right I've 32162 20:20:07,800 --> 20:20:12,160 been to many websites that have prettier 32163 20:20:09,400 --> 20:20:15,640 tables than M can I maybe use someone 32164 20:20:12,160 --> 20:20:17,800 else's framework someone else's CSS 32165 20:20:15,640 --> 20:20:19,720 include it in my page but then stand on 32166 20:20:17,800 --> 20:20:22,080 their shoulders and just make my stuff 32167 20:20:19,720 --> 20:20:24,832 look prettier well I dare say I can let 32168 20:20:22,080 --> 20:20:27,160 me go ahead here and semi secretly open 32169 20:20:24,832 --> 20:20:29,640 up vs code again and let me grab a 32170 20:20:27,160 --> 20:20:32,720 slightly different version of favorites. 32171 20:20:29,640 --> 20:20:37,040 HTML that I also opened in advance 32172 20:20:32,720 --> 20:20:38,760 wherein I add this line of code instead 32173 20:20:37,040 --> 20:20:40,720 give me just a moment to foreground this 32174 20:20:38,760 --> 20:20:42,832 version and the data is is all the same 32175 20:20:40,720 --> 20:20:45,040 as before but I've added one of these 32176 20:20:42,832 --> 20:20:48,512 link tags and I'm not linking to my own 32177 20:20:45,040 --> 20:20:51,000 styles.css I'm using a popular Library 32178 20:20:48,512 --> 20:20:52,920 called bootstrap and bootstrap is just 32179 20:20:51,000 --> 20:20:55,232 one of many popular libraries out there 32180 20:20:52,920 --> 20:20:57,552 free at that that has a whole bunch of 32181 20:20:55,232 --> 20:20:59,640 CSS files and soon JavaScript files that 32182 20:20:57,552 --> 20:21:01,720 you can just use for free in your own 32183 20:20:59,640 --> 20:21:04,400 projects personally or professionally 32184 20:21:01,720 --> 20:21:06,192 that just make things look and behave 32185 20:21:04,400 --> 20:21:09,040 better without you having to reinvent 32186 20:21:06,192 --> 20:21:10,552 Wheels now to access their CSS I had to 32187 20:21:09,040 --> 20:21:12,832 read their documentation and grab this 32188 20:21:10,552 --> 20:21:15,360 very long URL here but it's the same 32189 20:21:12,832 --> 20:21:17,400 idea link a forre equals quote unquote 32190 20:21:15,360 --> 20:21:19,640 something and I read their documentation 32191 20:21:17,400 --> 20:21:21,600 and they told me to add this they told 32192 20:21:19,640 --> 20:21:24,192 me that if I want my tables to be 32193 20:21:21,600 --> 20:21:27,080 prettier I have to add a class attribute 32194 20:21:24,192 --> 20:21:29,000 to my own table tag and specify a little 32195 20:21:27,080 --> 20:21:31,440 weirdly but this is what bootstrap told 32196 20:21:29,000 --> 20:21:33,680 me to do a class called table and that 32197 20:21:31,440 --> 20:21:35,680 will make it a prettier bootstrap table 32198 20:21:33,680 --> 20:21:37,760 and if I want to stripe it like every 32199 20:21:35,680 --> 20:21:39,960 other row is gray instead of white just 32200 20:21:37,760 --> 20:21:41,960 to make it pop a little more visually I 32201 20:21:39,960 --> 20:21:45,320 can also add a second class separated by 32202 20:21:41,960 --> 20:21:47,640 a space called table striped that's all 32203 20:21:45,320 --> 20:21:50,080 I did I added line five and I changed 32204 20:21:47,640 --> 20:21:52,280 line nine and that is it the rest of the 32205 20:21:50,080 --> 20:21:54,720 hundreds of lines in favorites. HTML are 32206 20:21:52,280 --> 20:21:56,760 the same but if I go back here now and 32207 20:21:54,720 --> 20:21:59,960 reload the browser now thanks to 32208 20:21:56,760 --> 20:22:01,552 bootstrap voila like it's much prettier 32209 20:21:59,960 --> 20:22:03,512 now I can zoom out and that changes the 32210 20:22:01,552 --> 20:22:05,720 font size just locally for me and even 32211 20:22:03,512 --> 20:22:08,192 if you don't love their Aesthetics I 32212 20:22:05,720 --> 20:22:10,080 mean this is easily better than my own 32213 20:22:08,192 --> 20:22:12,040 there and it turns out we can can do 32214 20:22:10,080 --> 20:22:14,480 even better by adding interactivity to 32215 20:22:12,040 --> 20:22:16,552 this too but to do that we're going to 32216 20:22:14,480 --> 20:22:18,440 need one final language for today and 32217 20:22:16,552 --> 20:22:20,080 this one is an actual programming 32218 20:22:18,440 --> 20:22:22,120 language and we won't use it all that 32219 20:22:20,080 --> 20:22:24,160 much in cs50 but we introduce it here as 32220 20:22:22,120 --> 20:22:26,192 we begin web stuff because there's just 32221 20:22:24,160 --> 20:22:27,640 so many free libraries and professional 32222 20:22:26,192 --> 20:22:30,120 libraries that you can use just to make 32223 20:22:27,640 --> 20:22:32,552 your web applications fancier and more 32224 20:22:30,120 --> 20:22:36,192 interactive mobile applications as well 32225 20:22:32,552 --> 20:22:38,440 increasingly use HTML CSS and JavaScript 32226 20:22:36,192 --> 20:22:41,000 to power our iPhones and Android devices 32227 20:22:38,440 --> 20:22:42,600 as well so a quick tour some syntax and 32228 20:22:41,000 --> 20:22:44,400 then we'll conclude with just some 32229 20:22:42,600 --> 20:22:46,872 hopefully inspiring examples to give you 32230 20:22:44,400 --> 20:22:49,000 a taste of what JavaScript can do so 32231 20:22:46,872 --> 20:22:51,480 JavaScript supports conditionals just 32232 20:22:49,000 --> 20:22:53,552 like C and python before it if we rewind 32233 20:22:51,480 --> 20:22:55,360 to our scratch days here of course is a 32234 20:22:53,552 --> 20:22:57,440 conditional here is the corresponding 32235 20:22:55,360 --> 20:23:00,040 JavaScript code as of today it's pretty 32236 20:22:57,440 --> 20:23:03,320 much identical to see with the syntax 32237 20:23:00,040 --> 20:23:04,872 here uh if we had an if an if else in 32238 20:23:03,320 --> 20:23:06,280 scratch it looked like this in 32239 20:23:04,872 --> 20:23:08,232 JavaScript it's going to look like this 32240 20:23:06,280 --> 20:23:10,800 instead so it's a bit of a regression V 32241 20:23:08,232 --> 20:23:12,320 ofv python like the the parentheses are 32242 20:23:10,800 --> 20:23:14,320 back the curly braces are back the 32243 20:23:12,320 --> 20:23:16,400 semicolons I mentioned in CSS are also 32244 20:23:14,320 --> 20:23:18,160 back in JavaScript potentially but it's 32245 20:23:16,400 --> 20:23:19,600 familiar is the point here and it's a 32246 20:23:18,160 --> 20:23:22,232 different language that's frequently 32247 20:23:19,600 --> 20:23:23,872 used for the web whereas you can't use 32248 20:23:22,232 --> 20:23:26,000 python in the ways we're about to use 32249 20:23:23,872 --> 20:23:28,512 JavaScript it just wasn't designed for 32250 20:23:26,000 --> 20:23:32,080 that purpose meanwhile if you have an if 32251 20:23:28,512 --> 20:23:33,832 El it's else if else in scratch well in 32252 20:23:32,080 --> 20:23:35,760 JavaScript just like in C it's going to 32253 20:23:33,832 --> 20:23:37,512 look like this instead variables in 32254 20:23:35,760 --> 20:23:39,080 JavaScript of course are a thing too and 32255 20:23:37,512 --> 20:23:42,160 in scratch we might have initialized a 32256 20:23:39,080 --> 20:23:44,280 counter variable to Z in JavaScript a 32257 20:23:42,160 --> 20:23:46,080 few different ways to do this and just 32258 20:23:44,280 --> 20:23:47,960 the for now the keyword is let it's sort 32259 20:23:46,080 --> 20:23:51,192 of a polite way of asking for a variable 32260 20:23:47,960 --> 20:23:52,960 let uh counter equal zero semicolon so 32261 20:23:51,192 --> 20:23:54,680 you don't mention the type but you do 32262 20:23:52,960 --> 20:23:56,800 use a keyword here in this case called 32263 20:23:54,680 --> 20:23:59,040 let if you want to increment counter by 32264 20:23:56,800 --> 20:24:01,400 one few different ways in JavaScript you 32265 20:23:59,040 --> 20:24:03,080 can do this just like in C in JavaScript 32266 20:24:01,400 --> 20:24:05,192 you can do this just like in C and in 32267 20:24:03,080 --> 20:24:07,160 Python in JavaScript you can also get 32268 20:24:05,192 --> 20:24:09,440 this so plus plus is back so maybe that 32269 20:24:07,160 --> 20:24:11,600 counterbalances the other syntax as well 32270 20:24:09,440 --> 20:24:14,040 that was was not the case in Python 32271 20:24:11,600 --> 20:24:15,400 Loops are back of course in JavaScript 32272 20:24:14,040 --> 20:24:17,480 whereas in scratch you could repeat 32273 20:24:15,400 --> 20:24:19,160 three times like this in JavaScript it's 32274 20:24:17,480 --> 20:24:20,832 pretty much just like C the only 32275 20:24:19,160 --> 20:24:23,440 difference here is that you say let 32276 20:24:20,832 --> 20:24:24,600 instead of int for an example like this 32277 20:24:23,440 --> 20:24:26,680 meanwhile if you want to do something 32278 20:24:24,600 --> 20:24:29,480 forever in scratch in JavaScript just 32279 20:24:26,680 --> 20:24:32,000 like in C you say while true in this 32280 20:24:29,480 --> 20:24:33,760 case so this is to say we're sort of 32281 20:24:32,000 --> 20:24:35,400 comfortable spending relatively little 32282 20:24:33,760 --> 20:24:37,800 time on JavaScript at least for today's 32283 20:24:35,400 --> 20:24:40,512 purposes because syntactically it's 32284 20:24:37,800 --> 20:24:42,232 really the same as we've seen before 32285 20:24:40,512 --> 20:24:44,040 with maybe a slight variance here or 32286 20:24:42,232 --> 20:24:45,960 there but what's interesting today 32287 20:24:44,040 --> 20:24:49,160 arguably is just what kinds of things 32288 20:24:45,960 --> 20:24:51,720 you can do with it so with that said 32289 20:24:49,160 --> 20:24:54,280 what kinds of things can we do it's all 32290 20:24:51,720 --> 20:24:56,360 comes back to this picture if this is a 32291 20:24:54,280 --> 20:24:59,360 simple web page on the left and this is 32292 20:24:56,360 --> 20:25:00,800 the corresponding tree or Dom document 32293 20:24:59,360 --> 20:25:02,552 object model on the right that is the 32294 20:25:00,800 --> 20:25:05,512 tree the browser automatically creates 32295 20:25:02,552 --> 20:25:07,680 in memory or RAM for you JavaScript is 32296 20:25:05,512 --> 20:25:10,760 now a proper programming language that 32297 20:25:07,680 --> 20:25:13,192 lets us dynamically manipul ulate like 32298 20:25:10,760 --> 20:25:14,720 read data from this change this and this 32299 20:25:13,192 --> 20:25:16,872 is how Google for instance implements 32300 20:25:14,720 --> 20:25:20,480 your inbox they might have uh in your 32301 20:25:16,872 --> 20:25:21,800 inbox it's like a table so TR TR TR TR 32302 20:25:20,480 --> 20:25:24,552 probably something like that or heck 32303 20:25:21,800 --> 20:25:26,040 maybe div div div div using JavaScript 32304 20:25:24,552 --> 20:25:28,232 anytime they realize someone sent you 32305 20:25:26,040 --> 20:25:30,640 new mail they can create a new node a 32306 20:25:28,232 --> 20:25:33,960 new rectangle in memory and you the 32307 20:25:30,640 --> 20:25:36,280 human see a new div or a new TR again 32308 20:25:33,960 --> 20:25:38,040 and again and again so with JavaScript 32309 20:25:36,280 --> 20:25:40,360 you just have the ability to control the 32310 20:25:38,040 --> 20:25:42,232 user's experience and instead of like 32311 20:25:40,360 --> 20:25:44,080 I've been doing constantly hitting 32312 20:25:42,232 --> 20:25:45,800 reload in the page to see some new 32313 20:25:44,080 --> 20:25:48,280 content to see some new content 32314 20:25:45,800 --> 20:25:50,192 JavaScript can be running 247 so that 32315 20:25:48,280 --> 20:25:52,920 you can actually see all of these 32316 20:25:50,192 --> 20:25:55,040 changes live all right let's go about 32317 20:25:52,920 --> 20:25:56,720 writing some JavaScript code now instead 32318 20:25:55,040 --> 20:25:58,080 of writing it on the server and 32319 20:25:56,720 --> 20:25:59,832 executing it on the server we're going 32320 20:25:58,080 --> 20:26:02,160 to actually use a very common Paradigm 32321 20:25:59,832 --> 20:26:04,600 whereas JavaScript is actually executed 32322 20:26:02,160 --> 20:26:06,080 in the browser client side that is to 32323 20:26:04,600 --> 20:26:08,872 say we can actually start writing some 32324 20:26:06,080 --> 20:26:11,360 JavaScript code inside of our own HTML 32325 20:26:08,872 --> 20:26:13,280 file so that when a user visits that web 32326 20:26:11,360 --> 20:26:15,640 page with their browser not only is the 32327 20:26:13,280 --> 20:26:17,960 HTML and any CSS downloaded to the 32328 20:26:15,640 --> 20:26:20,320 user's browser so is that JavaScript 32329 20:26:17,960 --> 20:26:23,160 code so that it's executed indeed client 32330 20:26:20,320 --> 20:26:25,480 side on the browser rather than server 32331 20:26:23,160 --> 20:26:27,832 side as has been the case with python in 32332 20:26:25,480 --> 20:26:29,480 previous weeks well where do we go about 32333 20:26:27,832 --> 20:26:32,280 writing some JavaScript code let's go 32334 20:26:29,480 --> 20:26:33,832 ahead and revisit hello.html which 32335 20:26:32,280 --> 20:26:35,440 previously was a completely static 32336 20:26:33,832 --> 20:26:38,000 example that literally just said hello 32337 20:26:35,440 --> 20:26:40,192 title hello body indeed if I open this 32338 20:26:38,000 --> 20:26:42,400 up using HTTP server and view it now in 32339 20:26:40,192 --> 20:26:44,600 a separate tab all it said was exactly 32340 20:26:42,400 --> 20:26:46,760 that hello title in the tab and hello 32341 20:26:44,600 --> 20:26:49,400 body in the main part of the viewport so 32342 20:26:46,760 --> 20:26:50,920 to speak well let's make this example a 32343 20:26:49,400 --> 20:26:53,480 little more Dynamic so it doesn't just 32344 20:26:50,920 --> 20:26:55,720 say hello body but maybe says hello to 32345 20:26:53,480 --> 20:26:57,320 an actual person so let's go ahead and 32346 20:26:55,720 --> 20:27:00,120 do this let's go ahead and remove the 32347 20:26:57,320 --> 20:27:03,160 hardcoded hello body and let's actually 32348 20:27:00,120 --> 20:27:05,040 go ahead here and use a form tag but 32349 20:27:03,160 --> 20:27:07,192 we're not going to use this form in the 32350 20:27:05,040 --> 20:27:08,760 usual way whereby the data gets sent all 32351 20:27:07,192 --> 20:27:10,720 the way back to the server we're going 32352 20:27:08,760 --> 20:27:12,960 to Leverage control over this form 32353 20:27:10,720 --> 20:27:14,872 client side instead so I'm going to go 32354 20:27:12,960 --> 20:27:17,040 ahead and create this open form tag 32355 20:27:14,872 --> 20:27:19,192 close form tag inside of that let me 32356 20:27:17,040 --> 20:27:21,280 give myself a text input that's going to 32357 20:27:19,192 --> 20:27:23,040 have autocomplete equals quote unquote 32358 20:27:21,280 --> 20:27:24,760 off just to ensure that what I 32359 20:27:23,040 --> 20:27:26,832 previously type in my examples doesn't 32360 20:27:24,760 --> 20:27:28,960 reappear accidentally we're going to 32361 20:27:26,832 --> 20:27:30,960 autofocus it so that the cursor is 32362 20:27:28,960 --> 20:27:32,872 blinking right there in that text box 32363 20:27:30,960 --> 20:27:37,000 and this time I'm going to go ahead and 32364 20:27:32,872 --> 20:27:39,480 give it how about a uh placeholder of 32365 20:27:37,000 --> 20:27:41,280 quote unquote name to make clear that 32366 20:27:39,480 --> 20:27:42,960 prompting the user for their name and 32367 20:27:41,280 --> 20:27:45,480 then the type of this text box will be 32368 20:27:42,960 --> 20:27:47,040 the default or more explicitly here text 32369 20:27:45,480 --> 20:27:49,400 and then I'm going to have as we've seen 32370 20:27:47,040 --> 20:27:52,360 before a button the type of which is 32371 20:27:49,400 --> 20:27:53,760 submit also our typical default and then 32372 20:27:52,360 --> 20:27:56,512 inside of this button is going to be the 32373 20:27:53,760 --> 20:27:59,000 label how about something like uh how 32374 20:27:56,512 --> 20:28:00,872 about we'll call this greet so that's 32375 20:27:59,000 --> 20:28:02,920 what the button will actually say well 32376 20:28:00,872 --> 20:28:04,760 let me actually go back into my browser 32377 20:28:02,920 --> 20:28:07,320 tab let me reload this page and we 32378 20:28:04,760 --> 20:28:09,440 should now see a relatively simple form 32379 20:28:07,320 --> 20:28:11,320 whereby I have the cursor blinking on a 32380 20:28:09,440 --> 20:28:12,800 text inp input prompting the user for 32381 20:28:11,320 --> 20:28:14,920 their name and then a greet button that 32382 20:28:12,800 --> 20:28:16,552 I can click but if I click this button 32383 20:28:14,920 --> 20:28:18,040 now it's not going to do anything useful 32384 20:28:16,552 --> 20:28:20,440 because I haven't written any code to 32385 20:28:18,040 --> 20:28:22,040 tell the browser what to do when I click 32386 20:28:20,440 --> 20:28:24,000 that button but it turns out there's all 32387 20:28:22,040 --> 20:28:26,480 sorts of events in the world of 32388 20:28:24,000 --> 20:28:28,120 JavaScript that you can listen for so to 32389 20:28:26,480 --> 20:28:30,400 speak in fact here's just a list of some 32390 20:28:28,120 --> 20:28:32,480 of them anytime something changes in a 32391 20:28:30,400 --> 20:28:35,000 form field anytime the user clicks or 32392 20:28:32,480 --> 20:28:36,552 drags on something anytime the user 32393 20:28:35,000 --> 20:28:38,832 presses a key and maybe lifts their 32394 20:28:36,552 --> 20:28:41,280 finger up anytime the mouse goes down or 32395 20:28:38,832 --> 20:28:44,040 over or up on top of something or 32396 20:28:41,280 --> 20:28:45,480 anytime a form is submitted those are 32397 20:28:44,040 --> 20:28:47,400 events in the same way that we talked 32398 20:28:45,480 --> 20:28:50,320 about events back in week zero in 32399 20:28:47,400 --> 20:28:52,160 scratch and in JavaScript just like in 32400 20:28:50,320 --> 20:28:54,160 scratch where you can do something when 32401 20:28:52,160 --> 20:28:56,480 green flag clicked in JavaScript you can 32402 20:28:54,160 --> 20:28:58,680 write code that actually listens for any 32403 20:28:56,480 --> 20:29:00,960 of these events or more so with that 32404 20:28:58,680 --> 20:29:03,360 said let's go back to vs code here and 32405 20:29:00,960 --> 20:29:05,320 let's make a couple of changes instead 32406 20:29:03,360 --> 20:29:07,000 let's go ahead and add to this form a 32407 20:29:05,320 --> 20:29:08,480 new attribute that's not the best way to 32408 20:29:07,000 --> 20:29:11,480 do it but it's perhaps the simplest for 32409 20:29:08,480 --> 20:29:14,280 version one here and let's say onsubmit 32410 20:29:11,480 --> 20:29:16,960 do the following so onsubmit is an HTML 32411 20:29:14,280 --> 20:29:19,400 attribute and curiously its value inside 32412 20:29:16,960 --> 20:29:21,800 of the quotes there can actually be some 32413 20:29:19,400 --> 20:29:23,552 JavaScript code and let's go ahead now 32414 20:29:21,800 --> 20:29:25,552 and let's assume there exists a function 32415 20:29:23,552 --> 20:29:28,000 in the world called greet and what I 32416 20:29:25,552 --> 20:29:29,960 want to do is call that function right 32417 20:29:28,000 --> 20:29:31,720 then and there well now in JavaScript 32418 20:29:29,960 --> 20:29:33,552 how do I go about making that function 32419 20:29:31,720 --> 20:29:36,080 exist it doesn't come out of the box 32420 20:29:33,552 --> 20:29:38,600 just like print might or say might in 32421 20:29:36,080 --> 20:29:40,440 python or scratch respectively but I can 32422 20:29:38,600 --> 20:29:43,040 do this let me go up into the head of 32423 20:29:40,440 --> 20:29:45,000 this page inside of a script tag here 32424 20:29:43,040 --> 20:29:46,600 both open and close let me actually 32425 20:29:45,000 --> 20:29:47,960 write some JavaScript code and just so 32426 20:29:46,600 --> 20:29:49,232 it stands out I'm going to give myself a 32427 20:29:47,960 --> 20:29:51,512 couple of blank lines though not 32428 20:29:49,232 --> 20:29:54,192 strictly necessary and let me Define a 32429 20:29:51,512 --> 20:29:55,960 new function in JavaScript called greet 32430 20:29:54,192 --> 20:29:57,680 and this is the syntax in JavaScript for 32431 20:29:55,960 --> 20:29:59,512 creating your own function similar in 32432 20:29:57,680 --> 20:30:01,512 Python instead of saying defa in 32433 20:29:59,512 --> 20:30:03,440 JavaScript you just say function then 32434 20:30:01,512 --> 20:30:05,080 the name of the function and any 32435 20:30:03,440 --> 20:30:06,640 arguments within the parentheses 32436 20:30:05,080 --> 20:30:09,160 thereafter but I'm not going to pass in 32437 20:30:06,640 --> 20:30:10,640 any here then inside of curly braces 32438 20:30:09,160 --> 20:30:12,160 what I'm going to do is use a built-in 32439 20:30:10,640 --> 20:30:14,232 JavaScript function that comes with any 32440 20:30:12,160 --> 20:30:16,280 browser called alert it's not the best 32441 20:30:14,232 --> 20:30:18,232 or prettiest user interface but for now 32442 20:30:16,280 --> 20:30:20,160 it's going to get the job done what do I 32443 20:30:18,232 --> 20:30:22,360 want to say to the user well let's first 32444 20:30:20,160 --> 20:30:25,600 just say something simple like hello 32445 20:30:22,360 --> 20:30:28,280 comma World close quote semicolon 32446 20:30:25,600 --> 20:30:30,320 thereby alerting the user with precisely 32447 20:30:28,280 --> 20:30:33,400 that message now what I'm going to do 32448 20:30:30,320 --> 20:30:35,040 down here is make one other change I 32449 20:30:33,400 --> 20:30:37,160 don't want this form to actually get 32450 20:30:35,040 --> 20:30:38,680 submitted to the server just like we've 32451 20:30:37,160 --> 20:30:40,440 seen in the past when you submit a form 32452 20:30:38,680 --> 20:30:43,640 it often goes to something like 32453 20:30:40,440 --> 20:30:46,040 google.com/ search I actually want my 32454 20:30:43,640 --> 20:30:48,480 JavaScript code to take control over the 32455 20:30:46,040 --> 20:30:50,680 entire user experience of this form so 32456 20:30:48,480 --> 20:30:53,552 that I'm just using the form as a user 32457 20:30:50,680 --> 20:30:56,160 input mechanism not to actually send via 32458 20:30:53,552 --> 20:30:58,600 get or post this data to some other 32459 20:30:56,160 --> 20:31:00,600 server including my own so this is going 32460 20:30:58,600 --> 20:31:02,192 to look a little ugly but after calling 32461 20:31:00,600 --> 20:31:03,640 greet I'm actually going to do this 32462 20:31:02,192 --> 20:31:05,832 which I've read the documentation about 32463 20:31:03,640 --> 20:31:07,640 and I know that if you actually hardcode 32464 20:31:05,832 --> 20:31:09,640 return false here that just tells the 32465 20:31:07,640 --> 20:31:12,832 browser please don't actually submit the 32466 20:31:09,640 --> 20:31:14,872 form only call the Greet function all 32467 20:31:12,832 --> 20:31:16,872 right well let me go back to my browser 32468 20:31:14,872 --> 20:31:18,360 here let me reload this because I need 32469 20:31:16,872 --> 20:31:20,160 to download the latest version of the 32470 20:31:18,360 --> 20:31:21,600 JavaScript code and I'm just going to go 32471 20:31:20,160 --> 20:31:23,832 ahead without even typing my name I'm 32472 20:31:21,600 --> 20:31:25,320 going to click on the Greet button and 32473 20:31:23,832 --> 20:31:27,552 you'll see that Al be it a little 32474 20:31:25,320 --> 20:31:29,400 cryptically at the top we see an alert 32475 20:31:27,552 --> 20:31:31,440 that says hello world there's my ugly 32476 20:31:29,400 --> 20:31:33,640 URL of my code space there at the moment 32477 20:31:31,440 --> 20:31:35,192 but we do indeed see that string but 32478 20:31:33,640 --> 20:31:37,400 what I haven't of course done is taken 32479 20:31:35,192 --> 20:31:40,040 any actual name from the user so how can 32480 20:31:37,400 --> 20:31:43,040 we go about doing that well ideally I 32481 20:31:40,040 --> 20:31:44,800 want to alert the user with hello comma 32482 20:31:43,040 --> 20:31:47,160 David or hello comma Carter whatever a 32483 20:31:44,800 --> 20:31:49,080 name I type into that box so how can I 32484 20:31:47,160 --> 20:31:51,160 go about doing that well let me create a 32485 20:31:49,080 --> 20:31:54,120 variable called name and let me set it 32486 20:31:51,160 --> 20:31:57,800 equal to this function call 32487 20:31:54,120 --> 20:32:00,160 document. query 32488 20:31:57,800 --> 20:32:02,080 selector that comes with JavaScript 32489 20:32:00,160 --> 20:32:04,400 itself let me then in parenthesis pass 32490 20:32:02,080 --> 20:32:06,480 in an argument that is going to be huh 32491 20:32:04,400 --> 20:32:09,080 the ID the unique I need a unique 32492 20:32:06,480 --> 20:32:10,552 identifier for the thing I want to 32493 20:32:09,080 --> 20:32:12,600 select so let me actually go back to my 32494 20:32:10,552 --> 20:32:16,080 HTML code here and instead of giving 32495 20:32:12,600 --> 20:32:17,920 this form field a name like Q for query 32496 20:32:16,080 --> 20:32:20,320 let me actually use another HTML 32497 20:32:17,920 --> 20:32:22,360 attribute called ID where now I can call 32498 20:32:20,320 --> 20:32:25,320 this anything I want and for clarity I'm 32499 20:32:22,360 --> 20:32:28,320 just going to call this input element 32500 20:32:25,320 --> 20:32:31,400 uniquely name now up here in query 32501 20:32:28,320 --> 20:32:34,120 selector just like in CSS where you can 32502 20:32:31,400 --> 20:32:36,800 use hashes and dots and other symbology 32503 20:32:34,120 --> 20:32:39,232 in order to select certain nodes in your 32504 20:32:36,800 --> 20:32:41,760 Dom that is rectangles in that memory 32505 20:32:39,232 --> 20:32:43,872 tree well I can go ahead and select hash 32506 20:32:41,760 --> 20:32:46,440 name which again is just the Syntax for 32507 20:32:43,872 --> 20:32:49,000 uniquely selecting the element whose ID 32508 20:32:46,440 --> 20:32:51,360 is in this case name so you have the 32509 20:32:49,000 --> 20:32:53,280 hash up here you don't need the hash as 32510 20:32:51,360 --> 20:32:55,192 the value of the attribute down here on 32511 20:32:53,280 --> 20:32:57,232 line 20 and now if I want to actually 32512 20:32:55,192 --> 20:32:59,512 get the value of that text box I 32513 20:32:57,232 --> 20:33:01,232 literally just say do value so document 32514 20:32:59,512 --> 20:33:02,920 refers to the whole web page itself 32515 20:33:01,232 --> 20:33:05,232 query selector is a function that's 32516 20:33:02,920 --> 20:33:08,040 built into that object so to speak and 32517 20:33:05,232 --> 20:33:10,832 the value accessible via value just like 32518 20:33:08,040 --> 20:33:12,832 a c stru or even a python class allows 32519 20:33:10,832 --> 20:33:15,040 me to go inside of that text field and 32520 20:33:12,832 --> 20:33:18,160 get whatever the value the user has 32521 20:33:15,040 --> 20:33:21,000 typed in now as I've uh been able to do 32522 20:33:18,160 --> 20:33:23,280 in languages like python pretty Le 32523 20:33:21,000 --> 20:33:27,000 fairly readily I can concatenate this 32524 20:33:23,280 --> 20:33:28,872 name onto the string hello comma space 32525 20:33:27,000 --> 20:33:30,400 so as to form a complete phrase and 32526 20:33:28,872 --> 20:33:32,440 you'll notice here that I'm actually 32527 20:33:30,400 --> 20:33:34,600 using single quotes in my JavaScript 32528 20:33:32,440 --> 20:33:37,120 double quotes in my HTML this is perhaps 32529 20:33:34,600 --> 20:33:39,040 a common convention in JavaScript the 32530 20:33:37,120 --> 20:33:40,552 language does not care if you use double 32531 20:33:39,040 --> 20:33:42,360 quotes or single quotes but I dare say 32532 20:33:40,552 --> 20:33:44,512 single quotes are just more common and 32533 20:33:42,360 --> 20:33:45,872 so that's what I've done here all right 32534 20:33:44,512 --> 20:33:47,512 now as always I'm going to cross my 32535 20:33:45,872 --> 20:33:49,360 fingers go back to this page I'm going 32536 20:33:47,512 --> 20:33:50,832 to reload because I've changed the 32537 20:33:49,360 --> 20:33:52,800 JavaScript and I need my browser to 32538 20:33:50,832 --> 20:33:56,192 download it and now I'm going to type in 32539 20:33:52,800 --> 20:33:59,720 my name for instance David click greets 32540 20:33:56,192 --> 20:34:02,600 with fingers crossed and voila now I see 32541 20:33:59,720 --> 20:34:04,552 hello comma David all right so it turns 32542 20:34:02,600 --> 20:34:08,080 out that while functional this isn't the 32543 20:34:04,552 --> 20:34:09,760 best design and co-mingling your uh HTML 32544 20:34:08,080 --> 20:34:11,920 with your JavaScript script code AS with 32545 20:34:09,760 --> 20:34:13,960 this onsubmit attribute isn't 32546 20:34:11,920 --> 20:34:17,160 particularly clean it's better as with 32547 20:34:13,960 --> 20:34:19,480 CSS to keep your HTML over here your CSS 32548 20:34:17,160 --> 20:34:21,000 over here and your JavaScript now over 32549 20:34:19,480 --> 20:34:23,232 here so to speak and better still 32550 20:34:21,000 --> 20:34:24,680 perhaps even in some separate files so 32551 20:34:23,232 --> 20:34:26,680 how can I go about changing this a 32552 20:34:24,680 --> 20:34:29,040 little bit well let me go ahead and 32553 20:34:26,680 --> 20:34:31,360 actually let's go ahead and delete all 32554 20:34:29,040 --> 20:34:33,872 of this code for just a moment and let 32555 20:34:31,360 --> 20:34:35,680 me go and get rid of this on submit 32556 20:34:33,872 --> 20:34:39,000 Handler down here and really just 32557 20:34:35,680 --> 20:34:41,192 distill my HTML only into the HTML and 32558 20:34:39,000 --> 20:34:44,400 the attributes therefore and what I'm 32559 20:34:41,192 --> 20:34:47,640 instead going to do now is do this I can 32560 20:34:44,400 --> 20:34:50,960 use JavaScript to achieve the listening 32561 20:34:47,640 --> 20:34:53,360 for that submit event or that onsubmit 32562 20:34:50,960 --> 20:34:56,000 event I don't need to actually use HTML 32563 20:34:53,360 --> 20:34:58,680 for that I can use JavaScript entirely 32564 20:34:56,000 --> 20:35:00,600 so it turns out I can access some other 32565 20:34:58,680 --> 20:35:03,872 uh member of this document by doing 32566 20:35:00,600 --> 20:35:06,440 document. query selector again but this 32567 20:35:03,872 --> 20:35:08,440 time let's select the actual form tag 32568 20:35:06,440 --> 20:35:11,192 and it doesn't have an ID because it has 32569 20:35:08,440 --> 20:35:12,832 no ID in its HTML but it does have a tag 32570 20:35:11,192 --> 20:35:15,320 name so just like in CSS when you can 32571 20:35:12,832 --> 20:35:17,232 Target Elements by way of their name I'm 32572 20:35:15,320 --> 20:35:19,120 just going to select the one and only 32573 20:35:17,232 --> 20:35:21,040 form on this page by using that same 32574 20:35:19,120 --> 20:35:22,640 query selector function and now I'm 32575 20:35:21,040 --> 20:35:24,360 going to use another function that just 32576 20:35:22,640 --> 20:35:26,720 comes with JavaScript in the context of 32577 20:35:24,360 --> 20:35:30,040 browsers whereby once you select an 32578 20:35:26,720 --> 20:35:32,360 element like that form I can call add 32579 20:35:30,040 --> 20:35:34,360 event listener which is similar in 32580 20:35:32,360 --> 20:35:36,832 spirit to scratches when green flag 32581 20:35:34,360 --> 20:35:38,232 clicked or any block like that you can 32582 20:35:36,832 --> 20:35:40,040 then tell the browser what event you 32583 20:35:38,232 --> 20:35:41,800 want to listen for I want to listen for 32584 20:35:40,040 --> 20:35:43,480 the submit event so you don't say 32585 20:35:41,800 --> 20:35:46,000 onsubmit here now that we're in pure 32586 20:35:43,480 --> 20:35:48,480 JavaScript you just say submit and now I 32587 20:35:46,000 --> 20:35:50,760 can do something like this I can go 32588 20:35:48,480 --> 20:35:52,040 ahead and say call the following 32589 20:35:50,760 --> 20:35:53,512 function and I'm not even going to 32590 20:35:52,040 --> 20:35:55,320 bother giving this function a name and 32591 20:35:53,512 --> 20:35:57,440 that is allowed to in JavaScript as we 32592 20:35:55,320 --> 20:36:00,832 saw briefly in Python and what I'm going 32593 20:35:57,440 --> 20:36:02,800 to do now inside of curly braces after 32594 20:36:00,832 --> 20:36:04,832 that keyword function is the same kind 32595 20:36:02,800 --> 20:36:08,920 of code as before I'm going to do let 32596 20:36:04,832 --> 20:36:11,720 name equals document. query selector I'm 32597 20:36:08,920 --> 20:36:14,760 going to select that same IDE uh same 32598 20:36:11,720 --> 20:36:16,960 Name ID as before and get its value and 32599 20:36:14,760 --> 20:36:19,440 then I'm going to do alert and then pass 32600 20:36:16,960 --> 20:36:21,400 in hello comma a single quote again 32601 20:36:19,440 --> 20:36:25,120 after that concatenate with that the 32602 20:36:21,400 --> 20:36:26,920 name and then semicolon but I need to do 32603 20:36:25,120 --> 20:36:28,192 one other thing it turns out that this 32604 20:36:26,920 --> 20:36:30,080 function and if you read the 32605 20:36:28,192 --> 20:36:32,760 documentation for this technique 32606 20:36:30,080 --> 20:36:34,920 actually takes automatically a special 32607 20:36:32,760 --> 20:36:37,000 argument called by convention event and 32608 20:36:34,920 --> 20:36:39,120 this is just an a variable if you will 32609 20:36:37,000 --> 20:36:40,360 that refers to whatever event just 32610 20:36:39,120 --> 20:36:42,360 happened in this case it's of course 32611 20:36:40,360 --> 20:36:44,080 going to be submit but in other contexts 32612 20:36:42,360 --> 20:36:46,360 it might be a click event a mouse down 32613 20:36:44,080 --> 20:36:48,000 event or something else entirely so this 32614 20:36:46,360 --> 20:36:49,552 allows me now to do this which is a 32615 20:36:48,000 --> 20:36:51,720 little cryptic but you get used to these 32616 20:36:49,552 --> 20:36:54,040 conventions I can use that event 32617 20:36:51,720 --> 20:36:56,000 whatever it is and then prevent whatever 32618 20:36:54,040 --> 20:36:58,040 the default behavior is by calling a 32619 20:36:56,000 --> 20:37:00,320 special function called prevent default 32620 20:36:58,040 --> 20:37:03,360 with a capital D this is the alternative 32621 20:37:00,320 --> 20:37:06,512 to that Messier return fall semicolon 32622 20:37:03,360 --> 20:37:08,600 that I had inside of my HTML before so 32623 20:37:06,512 --> 20:37:11,680 all I've done here now is I've left all 32624 20:37:08,600 --> 20:37:13,640 of my HTML as pure HTML down here and 32625 20:37:11,680 --> 20:37:15,600 I've put all of my JavaScript code as 32626 20:37:13,640 --> 20:37:17,232 pure JavaScript up here this sort of 32627 20:37:15,600 --> 20:37:19,640 separation of concerns similar to what 32628 20:37:17,232 --> 20:37:21,320 we started doing with CSS just a bit ago 32629 20:37:19,640 --> 20:37:23,360 in order to keep those two languages 32630 20:37:21,320 --> 20:37:26,160 separate too well let me go back to my 32631 20:37:23,360 --> 20:37:27,832 browser here reload the page and 32632 20:37:26,160 --> 20:37:29,320 unfortunately there's a subtle mistake 32633 20:37:27,832 --> 20:37:32,160 I've made here let me go ahead and type 32634 20:37:29,320 --> 20:37:34,440 in David and click greet and 32635 20:37:32,160 --> 20:37:36,080 unfortunately nothing actually seems to 32636 20:37:34,440 --> 20:37:39,120 happen well maybe it's just my name 32637 20:37:36,080 --> 20:37:41,360 Carter greet and nothing seems to happen 32638 20:37:39,120 --> 20:37:44,360 that alert does not come up well why is 32639 20:37:41,360 --> 20:37:46,040 that well let me go back to vs code here 32640 20:37:44,360 --> 20:37:48,280 and point out that order of operations 32641 20:37:46,040 --> 20:37:51,680 in zscript matters similar in spirit to 32642 20:37:48,280 --> 20:37:53,720 C because on line seven I'm selecting 32643 20:37:51,680 --> 20:37:55,832 the form and trying to add an event 32644 20:37:53,720 --> 20:37:57,920 listener for submitting that form 32645 20:37:55,832 --> 20:37:59,872 unfortunately the form had better exist 32646 20:37:57,920 --> 20:38:02,040 at that moment in time but it doesn't 32647 20:37:59,872 --> 20:38:03,920 because just like in C and in some cases 32648 20:38:02,040 --> 20:38:05,960 python where the compiler or The 32649 20:38:03,920 --> 20:38:07,832 Interpreter reads the code top to bottom 32650 20:38:05,960 --> 20:38:09,552 notice that the form doesn't actually 32651 20:38:07,832 --> 20:38:12,440 exist and therefore get loaded into the 32652 20:38:09,552 --> 20:38:14,720 computer's memory until line 19 so we've 32653 20:38:12,440 --> 20:38:17,120 got to kind of reorder these somehow now 32654 20:38:14,720 --> 20:38:19,512 maybe the simplest way to do this would 32655 20:38:17,120 --> 20:38:21,680 just be to perhaps do something like 32656 20:38:19,512 --> 20:38:24,760 this let me scroll back up to my script 32657 20:38:21,680 --> 20:38:27,320 tag and perhaps a little more explicitly 32658 20:38:24,760 --> 20:38:28,872 move it into the order in which I want 32659 20:38:27,320 --> 20:38:31,120 it to be executed so I'm going to go 32660 20:38:28,872 --> 20:38:32,920 below my form and inside of my body 32661 20:38:31,120 --> 20:38:35,120 which is actually okay for JavaScript 32662 20:38:32,920 --> 20:38:36,832 here and just use that same code and 32663 20:38:35,120 --> 20:38:38,832 assuming I didn't make any typos let's 32664 20:38:36,832 --> 20:38:41,040 go back to the browser click reload 32665 20:38:38,832 --> 20:38:42,832 again to get the latest typee in my name 32666 20:38:41,040 --> 20:38:44,192 again using that purely JavaScript 32667 20:38:42,832 --> 20:38:46,280 solution and the only change I made was 32668 20:38:44,192 --> 20:38:49,440 I move the code from up here to down 32669 20:38:46,280 --> 20:38:52,000 here clicking greet now and wow it's now 32670 20:38:49,440 --> 20:38:53,760 back we get the alert with hello comma 32671 20:38:52,000 --> 20:38:55,440 David so those kinds of things those 32672 20:38:53,760 --> 20:38:56,872 kinds of principles matter at least when 32673 20:38:55,440 --> 20:38:58,440 we're back in this world but there's 32674 20:38:56,872 --> 20:39:00,000 other Solutions too and just so that 32675 20:38:58,440 --> 20:39:02,120 you've seen it because it's a common 32676 20:39:00,000 --> 20:39:05,040 convention in libraries as well let me 32677 20:39:02,120 --> 20:39:07,160 undo that change and put that script tag 32678 20:39:05,040 --> 20:39:08,832 back in the head or really anywhere else 32679 20:39:07,160 --> 20:39:10,360 in the page where it might be 32680 20:39:08,832 --> 20:39:12,600 and let me propose that there's one 32681 20:39:10,360 --> 20:39:15,680 other way to solve this problem to 32682 20:39:12,600 --> 20:39:17,512 postpone that code on line 7 through 11 32683 20:39:15,680 --> 20:39:20,640 getting executed until really the whole 32684 20:39:17,512 --> 20:39:23,080 Dom the tree is ready to go and the 32685 20:39:20,640 --> 20:39:25,640 Syntax for this might be as follows I 32686 20:39:23,080 --> 20:39:28,232 can do document and I can add to the 32687 20:39:25,640 --> 20:39:29,720 document a an event listener that's 32688 20:39:28,232 --> 20:39:30,960 going to listen for something a little 32689 20:39:29,720 --> 20:39:32,400 special and I always have to look this 32690 20:39:30,960 --> 20:39:34,232 up myself to remember the spelling and 32691 20:39:32,400 --> 20:39:35,832 the capitalization but it turns out that 32692 20:39:34,232 --> 20:39:37,920 the browser itself once it's done 32693 20:39:35,832 --> 20:39:41,360 loading all of your HTML top to bottom 32694 20:39:37,920 --> 20:39:45,160 left right it will raise an event called 32695 20:39:41,360 --> 20:39:47,160 Dom content loaded capitalized exactly 32696 20:39:45,160 --> 20:39:49,000 as such and if you want to call some 32697 20:39:47,160 --> 20:39:51,400 function and I don't even need an event 32698 20:39:49,000 --> 20:39:54,040 argument in this case you can open curly 32699 20:39:51,400 --> 20:39:55,760 braces just as before and put inside of 32700 20:39:54,040 --> 20:39:58,192 those curly braces the code that you 32701 20:39:55,760 --> 20:40:00,600 want to execute only once the dumbs 32702 20:39:58,192 --> 20:40:02,080 content has been loaded top to bottom 32703 20:40:00,600 --> 20:40:03,640 and now let me just finish my thought 32704 20:40:02,080 --> 20:40:05,832 with a closed curly brace Clos 32705 20:40:03,640 --> 20:40:07,440 parenthesis and semicolon it gets a 32706 20:40:05,832 --> 20:40:09,400 little Annoying to visually line all of 32707 20:40:07,440 --> 20:40:11,960 this up but I think I'm still good and 32708 20:40:09,400 --> 20:40:15,080 now even though this code is at the top 32709 20:40:11,960 --> 20:40:17,720 of my file or really above the form tag 32710 20:40:15,080 --> 20:40:20,080 itself I think we're okay so let's go 32711 20:40:17,720 --> 20:40:22,832 back to the browser here reload the page 32712 20:40:20,080 --> 20:40:25,040 type in David and click greet and we 32713 20:40:22,832 --> 20:40:26,800 still get the same correct behavior and 32714 20:40:25,040 --> 20:40:29,192 so this is just a very common Paradigm 32715 20:40:26,800 --> 20:40:30,800 to use these kinds of events to listen 32716 20:40:29,192 --> 20:40:33,400 and listen and listen for something to 32717 20:40:30,800 --> 20:40:35,920 happen and then only do something once 32718 20:40:33,400 --> 20:40:37,640 that thing has transpired all right well 32719 20:40:35,920 --> 20:40:39,040 let's take one more step with JavaScript 32720 20:40:37,640 --> 20:40:40,640 code before we take a look at what's 32721 20:40:39,040 --> 20:40:43,000 really fun about this language and what 32722 20:40:40,640 --> 20:40:44,800 you can do with browsers in particular 32723 20:40:43,000 --> 20:40:46,192 by just cleaning things up a little bit 32724 20:40:44,800 --> 20:40:48,160 further I'm going to go back into the 32725 20:40:46,192 --> 20:40:51,440 code here and I'm actually going to 32726 20:40:48,160 --> 20:40:54,552 remove uh or cut all of this code out of 32727 20:40:51,440 --> 20:40:56,640 the hello.html file itself and I'm going 32728 20:40:54,552 --> 20:40:59,192 to change my script tag to have nothing 32729 20:40:56,640 --> 20:41:01,232 in between the open and close tag but I 32730 20:40:59,192 --> 20:41:02,760 am going to give it a source attribute 32731 20:41:01,232 --> 20:41:05,832 and let's go ahead and call this for 32732 20:41:02,760 --> 20:41:07,480 instance hello.js sojs would be the 32733 20:41:05,832 --> 20:41:09,232 convention for the file extension for a 32734 20:41:07,480 --> 20:41:11,040 Javascript file and even though this is 32735 20:41:09,232 --> 20:41:13,440 a little weird that we have the script 32736 20:41:11,040 --> 20:41:15,440 tag and a source attribute then nothing 32737 20:41:13,440 --> 20:41:16,872 in between the open and close tag this 32738 20:41:15,440 --> 20:41:19,120 is indeed the convention when you want 32739 20:41:16,872 --> 20:41:20,920 to put all of your code in a separate 32740 20:41:19,120 --> 20:41:23,040 file and let me go ahead and do that let 32741 20:41:20,920 --> 20:41:25,360 me go ahead and open my terminal window 32742 20:41:23,040 --> 20:41:27,920 create a new file called 32743 20:41:25,360 --> 20:41:29,720 hello.js and then in that file I'm just 32744 20:41:27,920 --> 20:41:32,192 going to paste the very code that I just 32745 20:41:29,720 --> 20:41:34,040 cut from the previous file so no changes 32746 20:41:32,192 --> 20:41:35,680 to the code all I'm doing is factoring 32747 20:41:34,040 --> 20:41:38,320 it out and now I'm doing something just 32748 20:41:35,680 --> 20:41:40,192 like our CSS factorization before which 32749 20:41:38,320 --> 20:41:42,360 confusingly use the link tag this uses 32750 20:41:40,192 --> 20:41:43,960 the script tag this just now allows me 32751 20:41:42,360 --> 20:41:45,512 to collaborate with someone like Carter 32752 20:41:43,960 --> 20:41:47,480 or someone else so that they can do the 32753 20:41:45,512 --> 20:41:49,680 JavaScript code I can do the HTML maybe 32754 20:41:47,480 --> 20:41:52,192 a third person can do the CSS and indeed 32755 20:41:49,680 --> 20:41:55,872 maybe we can build even grander things 32756 20:41:52,192 --> 20:41:57,800 by uh designing things in this way all 32757 20:41:55,872 --> 20:42:00,552 right well let me go back to my browser 32758 20:41:57,800 --> 20:42:02,440 again reload the page I shouldn't see 32759 20:42:00,552 --> 20:42:05,232 any visual changes but if I type in my 32760 20:42:02,440 --> 20:42:07,640 name again David and click greet this 32761 20:42:05,232 --> 20:42:09,120 still now works and what my browser has 32762 20:42:07,640 --> 20:42:12,920 just done underneath the hood is not 32763 20:42:09,120 --> 20:42:15,192 only download the uh hello.html file as 32764 20:42:12,920 --> 20:42:16,800 always because there's now this script 32765 20:42:15,192 --> 20:42:18,280 tag that's referencing the source of 32766 20:42:16,800 --> 20:42:20,360 another file just like an image tag 32767 20:42:18,280 --> 20:42:22,400 might reference the source of an image 32768 20:42:20,360 --> 20:42:25,680 the browser is automatically helping me 32769 20:42:22,400 --> 20:42:29,040 out by loading that into its memory as 32770 20:42:25,680 --> 20:42:30,120 well and now how about one final example 32771 20:42:29,040 --> 20:42:32,232 and for this one I'm going to go ahead 32772 20:42:30,120 --> 20:42:33,800 and not write it live but open it up as 32773 20:42:32,232 --> 20:42:36,040 prepared in advance just to show you 32774 20:42:33,800 --> 20:42:38,192 what you can do by listening for some of 32775 20:42:36,040 --> 20:42:39,832 these other events as well like the key 32776 20:42:38,192 --> 20:42:41,440 up the finger going down the finger 32777 20:42:39,832 --> 20:42:43,280 going up and listening for exactly that 32778 20:42:41,440 --> 20:42:45,512 so as the user is typing something you 32779 20:42:43,280 --> 20:42:47,040 can do something interesting as well I'm 32780 20:42:45,512 --> 20:42:49,232 going to go back into my directory 32781 20:42:47,040 --> 20:42:50,760 listing here and I click on this Source 32782 20:42:49,232 --> 20:42:52,640 a directory which has all of the 32783 20:42:50,760 --> 20:42:54,360 examples that I wrote here in advance 32784 20:42:52,640 --> 20:42:58,400 and I'm going to scroll down to one 32785 20:42:54,360 --> 20:43:00,040 called hello 5. HTML and in hello 5 now 32786 20:42:58,400 --> 20:43:02,640 we've gotten rid of the button and we 32787 20:43:00,040 --> 20:43:04,480 just have this text box but notice now 32788 20:43:02,640 --> 20:43:09,640 what happens if I start typing my name 32789 20:43:04,480 --> 20:43:11,232 as d a v i d D I'm not typing enter at 32790 20:43:09,640 --> 20:43:12,800 all and in fact if I start deleting and 32791 20:43:11,232 --> 20:43:15,040 I change my mind and start typing 32792 20:43:12,800 --> 20:43:17,040 Carter's name notice now that the web 32793 20:43:15,040 --> 20:43:19,600 page the Dom inside of the computer's 32794 20:43:17,040 --> 20:43:21,080 memory is now automatically updating 32795 20:43:19,600 --> 20:43:23,640 itself so it's not even listening it 32796 20:43:21,080 --> 20:43:26,960 would seem for a submit event anymore 32797 20:43:23,640 --> 20:43:29,360 but maybe for a key up event instead so 32798 20:43:26,960 --> 20:43:31,280 let me go back to VSS code here open my 32799 20:43:29,360 --> 20:43:35,680 terminal window and in my source a 32800 20:43:31,280 --> 20:43:37,440 directory let me open up hello 5. HTML 32801 20:43:35,680 --> 20:43:39,512 inside of the script tag you'll see some 32802 20:43:37,440 --> 20:43:41,160 code that's similar in spirit to before 32803 20:43:39,512 --> 20:43:43,280 whereby I'm adding an event listener to 32804 20:43:41,160 --> 20:43:45,960 the document waiting for the whole Dom's 32805 20:43:43,280 --> 20:43:49,360 content to be loaded but then inside of 32806 20:43:45,960 --> 20:43:51,192 that function I'm now doing this I'm 32807 20:43:49,360 --> 20:43:53,280 creating a variable called input and 32808 20:43:51,192 --> 20:43:55,320 selecting from the document the one and 32809 20:43:53,280 --> 20:43:58,600 only input tag that we saw just a moment 32810 20:43:55,320 --> 20:44:01,000 ago I'm then adding on line 11 in event 32811 20:43:58,600 --> 20:44:03,080 listener for key up which is exactly 32812 20:44:01,000 --> 20:44:04,832 that gesture so that I can execute some 32813 20:44:03,080 --> 20:44:06,280 additional code anytime the human lists 32814 20:44:04,832 --> 20:44:08,960 their finger from the keyboard after 32815 20:44:06,280 --> 20:44:10,552 typing a key what do I then do well I'm 32816 20:44:08,960 --> 20:44:12,480 going to go ahead it seems and declare 32817 20:44:10,552 --> 20:44:14,440 another variable called name and I'm 32818 20:44:12,480 --> 20:44:16,400 just going to select some P tag on the 32819 20:44:14,440 --> 20:44:17,720 page and now we didn't really see a P 32820 20:44:16,400 --> 20:44:20,160 tag so I think it's time to look at the 32821 20:44:17,720 --> 20:44:22,720 HTML if I scroll down to the bottom of 32822 20:44:20,160 --> 20:44:24,920 the page where my actual HTML is you'll 32823 20:44:22,720 --> 20:44:26,872 see that there's just a form tag and no 32824 20:44:24,920 --> 20:44:29,400 onsubmit Handler anymore as before 32825 20:44:26,872 --> 20:44:32,280 there's just an input tag and no button 32826 20:44:29,400 --> 20:44:34,192 at all but there is on line 29 here an 32827 20:44:32,280 --> 20:44:35,800 open and close P tag just so I have an 32828 20:44:34,192 --> 20:44:38,040 empty placeholder in which to put 32829 20:44:35,800 --> 20:44:41,040 something like hello David or hello 32830 20:44:38,040 --> 20:44:42,720 Carter so that's why now on line 12 I 32831 20:44:41,040 --> 20:44:45,720 can define a variable called name and I 32832 20:44:42,720 --> 20:44:48,720 can select that P tag so that what do I 32833 20:44:45,720 --> 20:44:50,640 want to do well if inside of the input 32834 20:44:48,720 --> 20:44:52,920 there's a value so this is essentially 32835 20:44:50,640 --> 20:44:56,232 checking for null or the absence of a 32836 20:44:52,920 --> 20:44:58,440 string so if input. value does not equal 32837 20:44:56,232 --> 20:45:00,720 nothing that is there is a value in that 32838 20:44:58,440 --> 20:45:02,960 text box well this syntax here on line 32839 20:45:00,720 --> 20:45:06,480 14 is a very clever way of doing the 32840 20:45:02,960 --> 20:45:09,160 following go into that name tag that is 32841 20:45:06,480 --> 20:45:11,832 to say the empty par paragraph change 32842 20:45:09,160 --> 20:45:15,400 the inner HTML of it the HTML inside of 32843 20:45:11,832 --> 20:45:17,040 it to be literally this hello comma and 32844 20:45:15,400 --> 20:45:18,872 then just so you've seen an additional 32845 20:45:17,040 --> 20:45:20,920 piece of syntax this is similar to 32846 20:45:18,872 --> 20:45:22,440 Python's F strings the syntax is a 32847 20:45:20,920 --> 20:45:24,280 little weird in JavaScript you actually 32848 20:45:22,440 --> 20:45:26,512 use not single quotes not double quotes 32849 20:45:24,280 --> 20:45:28,040 but back ticks which on a US English 32850 20:45:26,512 --> 20:45:30,600 keyboard are typically the top leftand 32851 20:45:28,040 --> 20:45:33,440 key of your keyboard or thereabouts and 32852 20:45:30,600 --> 20:45:36,552 using dollar sign curly braces just like 32853 20:45:33,440 --> 20:45:38,480 the curly braces alone in Python allows 32854 20:45:36,552 --> 20:45:41,040 us to plug in whatever the value is of 32855 20:45:38,480 --> 20:45:42,512 that input however if there's no value 32856 20:45:41,040 --> 20:45:45,080 there it looks like I'm just going to 32857 20:45:42,512 --> 20:45:47,120 say hello whoever you are and in fact we 32858 20:45:45,080 --> 20:45:49,320 can see that behavior now is I delete 32859 20:45:47,120 --> 20:45:51,960 delete delete delete delete delete and 32860 20:45:49,320 --> 20:45:53,960 nothing's there now that if condition is 32861 20:45:51,960 --> 20:45:56,192 no longer true and so we see this 32862 20:45:53,960 --> 20:45:58,680 default value instead so this is only to 32863 20:45:56,192 --> 20:46:00,320 say that by harnessing these various 32864 20:45:58,680 --> 20:46:02,552 events that are constantly happening on 32865 20:46:00,320 --> 20:46:04,280 most any web page we can now register 32866 20:46:02,552 --> 20:46:06,232 code just like we did way back in 32867 20:46:04,280 --> 20:46:09,400 scratch to actually listen for those 32868 20:46:06,232 --> 20:46:11,320 events and do something with them now it 32869 20:46:09,400 --> 20:46:13,192 turns out we can do some interesting 32870 20:46:11,320 --> 20:46:14,872 things even using third party coat and 32871 20:46:13,192 --> 20:46:17,160 just as we used bootstrap a bit ago to 32872 20:46:14,872 --> 20:46:19,920 make our table prettier allow me to 32873 20:46:17,160 --> 20:46:21,920 propose that we also take a look at this 32874 20:46:19,920 --> 20:46:24,160 version of favorites as well let me go 32875 20:46:21,920 --> 20:46:26,192 back into my source a directory and open 32876 20:46:24,160 --> 20:46:27,720 up favorites 2 which I made in advance 32877 20:46:26,192 --> 20:46:29,832 which looks almost the same though I've 32878 20:46:27,720 --> 20:46:34,280 zoomed in here a bit but you'll notice 32879 20:46:29,832 --> 20:46:37,080 somewhat subtly over the leftmost column 32880 20:46:34,280 --> 20:46:39,192 in this table you'll see now this arrow 32881 20:46:37,080 --> 20:46:40,872 in Gray pointing up and pointing down 32882 20:46:39,192 --> 20:46:43,280 previously those were not there all I 32883 20:46:40,872 --> 20:46:45,192 had was a static HTML table with all of 32884 20:46:43,280 --> 20:46:47,832 this data sorted in whatever order it 32885 20:46:45,192 --> 20:46:50,080 was inputed the other day in that form 32886 20:46:47,832 --> 20:46:53,160 but now notice what I can do if I want 32887 20:46:50,080 --> 20:46:55,040 to sort in uh one order I can click this 32888 20:46:53,160 --> 20:46:56,480 Arrow or the other order I can sort in 32889 20:46:55,040 --> 20:46:59,080 this Arrow so essentially doing it 32890 20:46:56,480 --> 20:47:00,552 chronologically forward or backward now 32891 20:46:59,080 --> 20:47:02,120 how is that sorting happening it's 32892 20:47:00,552 --> 20:47:04,360 presumably based on all of the 32893 20:47:02,120 --> 20:47:06,192 timestamps that were registered when we 32894 20:47:04,360 --> 20:47:10,440 submitted that Google form just a bit 32895 20:47:06,192 --> 20:47:12,040 ago um uh when it was live but now using 32896 20:47:10,440 --> 20:47:14,552 JavaScript it turns out that we can use 32897 20:47:12,040 --> 20:47:15,920 some logic somehow and sort this data by 32898 20:47:14,552 --> 20:47:18,512 the same and you don't get that 32899 20:47:15,920 --> 20:47:20,320 automatically just by using HTML alone 32900 20:47:18,512 --> 20:47:22,400 now how did I achieve that well it turns 32901 20:47:20,320 --> 20:47:25,640 out if I go ahead and close these hello 32902 20:47:22,400 --> 20:47:28,800 files and in vs code let's open up uh 32903 20:47:25,640 --> 20:47:30,800 favorites 2. HTML you'll see that all of 32904 20:47:28,800 --> 20:47:32,760 the HTML is actually the same if I 32905 20:47:30,800 --> 20:47:34,280 scroll down and down through this file 32906 20:47:32,760 --> 20:47:36,800 but I added a little something 32907 20:47:34,280 --> 20:47:39,120 interesting at top I copied and pasted 32908 20:47:36,800 --> 20:47:41,832 the appropriate URLs and HTML tags from 32909 20:47:39,120 --> 20:47:43,920 bootstraps documentation and you'll see 32910 20:47:41,832 --> 20:47:46,832 here that I have a file called not only 32911 20:47:43,920 --> 20:47:49,280 bootstrap.css but also 32912 20:47:46,832 --> 20:47:51,400 bootstrap.min.js as well as a couple of 32913 20:47:49,280 --> 20:47:54,232 other things as well that allows me 32914 20:47:51,400 --> 20:47:56,920 ultimately using third-party libraries 32915 20:47:54,232 --> 20:47:59,552 to add some special HTML attributes that 32916 20:47:56,920 --> 20:48:01,160 those libraries told me to add and then 32917 20:47:59,552 --> 20:48:02,920 as soon as those libraries detect the 32918 20:48:01,160 --> 20:48:05,440 presence of these attributes now on my 32919 20:48:02,920 --> 20:48:07,832 own raw data they do their thing and 32920 20:48:05,440 --> 20:48:10,192 javascrip defy not a technical term the 32921 20:48:07,832 --> 20:48:12,400 entire table and now make it interactive 32922 20:48:10,192 --> 20:48:14,000 and not static so you'll see here that 32923 20:48:12,400 --> 20:48:16,192 the Aesthetics of the table are as 32924 20:48:14,000 --> 20:48:18,400 before table and table striped but I'm 32925 20:48:16,192 --> 20:48:20,920 adding now another HTML attribute called 32926 20:48:18,400 --> 20:48:22,760 Data toggle whose value is table and I 32927 20:48:20,920 --> 20:48:24,600 know that only from the documentation of 32928 20:48:22,760 --> 20:48:27,040 these libraries indicating that's that's 32929 20:48:24,600 --> 20:48:29,640 how I can now enable this table to be 32930 20:48:27,040 --> 20:48:32,400 interactive as I can too by adding data 32931 20:48:29,640 --> 20:48:34,800 sortable equals quote unquote true on 32932 20:48:32,400 --> 20:48:36,440 specifically the timestamp column and 32933 20:48:34,800 --> 20:48:39,160 the only thing unfamiliar here perhaps 32934 20:48:36,440 --> 20:48:41,872 is I'm using t P for table heading as 32935 20:48:39,160 --> 20:48:43,920 opposed to TD as I do elsewhere but 32936 20:48:41,872 --> 20:48:45,512 that's all that it takes to now focus on 32937 20:48:43,920 --> 20:48:47,480 the raw data you want to present and let 32938 20:48:45,512 --> 20:48:49,832 someone else do the heavy lifting of 32939 20:48:47,480 --> 20:48:52,552 actually implementing the logic well 32940 20:48:49,832 --> 20:48:54,640 let's end with just a look at what more 32941 20:48:52,552 --> 20:48:56,320 you can do with Java script and just how 32942 20:48:54,640 --> 20:48:59,000 powerful it is when you combine a 32943 20:48:56,320 --> 20:49:01,232 language like this with the data and the 32944 20:48:59,000 --> 20:49:03,512 uh user interface you want to convey 32945 20:49:01,232 --> 20:49:05,640 let's go ahead and open up within Source 32946 20:49:03,512 --> 20:49:08,080 a directory something called background. 32947 20:49:05,640 --> 20:49:10,360 HTML now this interface here is quite 32948 20:49:08,080 --> 20:49:12,232 simple it just has three buttons RG and 32949 20:49:10,360 --> 20:49:14,120 B and the background of course noticed 32950 20:49:12,232 --> 20:49:16,480 by default is just white but when I 32951 20:49:14,120 --> 20:49:18,192 click on the r the background turns red 32952 20:49:16,480 --> 20:49:20,440 when the I click on the G it turns green 32953 20:49:18,192 --> 20:49:23,120 and the blue it turns blue and again and 32954 20:49:20,440 --> 20:49:25,120 again so how is this working well if you 32955 20:49:23,120 --> 20:49:27,120 think back again to the available events 32956 20:49:25,120 --> 20:49:29,600 perhaps I'm just listening for a click 32957 20:49:27,120 --> 20:49:31,512 on those buttons and then doing 32958 20:49:29,600 --> 20:49:33,552 something with maybe the CSS of the page 32959 20:49:31,512 --> 20:49:35,640 to allow me to see those different 32960 20:49:33,552 --> 20:49:38,480 colors so in fact let's go back to vs 32961 20:49:35,640 --> 20:49:41,480 code here and let's open up background. 32962 20:49:38,480 --> 20:49:43,640 HTML and in here you'll see some simple 32963 20:49:41,480 --> 20:49:46,232 HTML at the top just three buttons but 32964 20:49:43,640 --> 20:49:48,512 I've given each a unique ID so that I 32965 20:49:46,232 --> 20:49:50,400 can reference it in code and then inside 32966 20:49:48,512 --> 20:49:52,920 of a script tag here below because I 32967 20:49:50,400 --> 20:49:55,640 didn't bother with the uh Dom content 32968 20:49:52,920 --> 20:49:57,512 loaded event here notice that I'm doing 32969 20:49:55,640 --> 20:49:59,440 the following I'm creating a variable 32970 20:49:57,512 --> 20:50:02,080 called body that lets me select the body 32971 20:49:59,440 --> 20:50:04,512 tag I then have in these three line some 32972 20:50:02,080 --> 20:50:06,640 code that handles red what am I doing 32973 20:50:04,512 --> 20:50:09,720 well I'm selecting from the document 32974 20:50:06,640 --> 20:50:11,232 whatever h HTML tag has unique ID of red 32975 20:50:09,720 --> 20:50:13,320 and then I'm adding an event listener 32976 20:50:11,232 --> 20:50:15,960 for any click on that button and anytime 32977 20:50:13,320 --> 20:50:17,680 someone clicks on that red button I call 32978 20:50:15,960 --> 20:50:20,232 this function anonymously it doesn't 32979 20:50:17,680 --> 20:50:21,640 even have or need a name and this syntax 32980 20:50:20,232 --> 20:50:24,160 here is powerful because now in 32981 20:50:21,640 --> 20:50:26,920 JavaScript I can alter the CSS of my 32982 20:50:24,160 --> 20:50:29,512 page by doing body which is the tag that 32983 20:50:26,920 --> 20:50:32,400 I selected two lines ago accessing its 32984 20:50:29,512 --> 20:50:34,400 style accessing its background color 32985 20:50:32,400 --> 20:50:36,400 property and setting it equal to quote 32986 20:50:34,400 --> 20:50:38,040 unquote red and I do the same down below 32987 20:50:36,400 --> 20:50:39,400 for green I do the same down below for 32988 20:50:38,040 --> 20:50:41,760 blue and the only thing worth noting 32989 20:50:39,400 --> 20:50:44,280 here is that in CSS it turns out it's 32990 20:50:41,760 --> 20:50:46,600 the case that the CSS property for the 32991 20:50:44,280 --> 20:50:49,120 background color of a page is actually 32992 20:50:46,600 --> 20:50:51,440 background Das color in all lower case 32993 20:50:49,120 --> 20:50:53,640 with a hyphen in between unfortunately 32994 20:50:51,440 --> 20:50:55,760 in the world of JavaScript a hyphen 32995 20:50:53,640 --> 20:50:57,360 would be mistaken for subtraction like 32996 20:50:55,760 --> 20:50:59,440 background minus color which would be 32997 20:50:57,360 --> 20:51:01,920 wrong so the convention in JavaScript is 32998 20:50:59,440 --> 20:51:04,640 when you're trying to manipulate CSS you 32999 20:51:01,920 --> 20:51:06,832 take whatever the property name is uh 33000 20:51:04,640 --> 20:51:09,040 font size background color and you 33001 20:51:06,832 --> 20:51:10,320 change it into so-called camel case here 33002 20:51:09,040 --> 20:51:12,552 you get rid of The Hyphen and you 33003 20:51:10,320 --> 20:51:15,000 capitalize the subsequent words like 33004 20:51:12,552 --> 20:51:16,800 color in this case here all right how 33005 20:51:15,000 --> 20:51:19,552 about another well it turns out back in 33006 20:51:16,800 --> 20:51:21,720 the day back in my day there was a HTML 33007 20:51:19,552 --> 20:51:24,440 tag that would actually allow you to do 33008 20:51:21,720 --> 20:51:26,080 this create blinking text on a screen 33009 20:51:24,440 --> 20:51:28,000 it's rather unpleasant at this rate 33010 20:51:26,080 --> 20:51:29,920 certainly but how might this work well 33011 20:51:28,000 --> 20:51:33,680 it turns out in JavaScript if we take a 33012 20:51:29,920 --> 20:51:36,640 look at the blink. HTML file here we'll 33013 20:51:33,680 --> 20:51:38,600 see that you can in your HTML do 33014 20:51:36,640 --> 20:51:41,192 something literally as simple as hello 33015 20:51:38,600 --> 20:51:43,120 world in the body but then you can call 33016 20:51:41,192 --> 20:51:45,120 this function here turns out just like 33017 20:51:43,120 --> 20:51:46,760 document there's another Global special 33018 20:51:45,120 --> 20:51:48,760 variable you can use in JavaScript and 33019 20:51:46,760 --> 20:51:50,960 browsers called window which refers to 33020 20:51:48,760 --> 20:51:52,600 all things related to the window itself 33021 20:51:50,960 --> 20:51:54,640 the window comes with a set interval 33022 20:51:52,600 --> 20:51:57,320 function which lets you do exactly that 33023 20:51:54,640 --> 20:51:59,320 set an interval in milliseconds and 33024 20:51:57,320 --> 20:52:01,760 after every expiration of that interval 33025 20:51:59,320 --> 20:52:03,760 some function will be called for you so 33026 20:52:01,760 --> 20:52:05,280 in this case it's saying every 50 33027 20:52:03,760 --> 20:52:07,232 milliseconds but let's actually slow 33028 20:52:05,280 --> 20:52:09,720 that down now to 500 milliseconds or for 33029 20:52:07,232 --> 20:52:12,400 one half a second call a function called 33030 20:52:09,720 --> 20:52:13,440 blink notice I do not have parentheses 33031 20:52:12,400 --> 20:52:16,080 after the blink name because I don't 33032 20:52:13,440 --> 20:52:17,872 want to call blink now I want to tell 33033 20:52:16,080 --> 20:52:20,192 JavaScript to call the function called 33034 20:52:17,872 --> 20:52:21,552 blink every 500 milliseconds now we'll 33035 20:52:20,192 --> 20:52:22,872 see in a moment what that code looks 33036 20:52:21,552 --> 20:52:24,960 like but let's go back to the page and 33037 20:52:22,872 --> 20:52:26,872 reload and you'll see now that it's a 33038 20:52:24,960 --> 20:52:29,160 more pleasant blinking if that's even 33039 20:52:26,872 --> 20:52:32,080 the case every half second because I'm 33040 20:52:29,160 --> 20:52:34,360 now firing that event that is I'm uh 33041 20:52:32,080 --> 20:52:36,832 calling that function now every 500 33042 20:52:34,360 --> 20:52:39,000 milliseconds instead how am I doing that 33043 20:52:36,832 --> 20:52:41,440 well this same script tag I've invented 33044 20:52:39,000 --> 20:52:43,040 my own blink function this is has the 33045 20:52:41,440 --> 20:52:45,512 distinction back in the day of actually 33046 20:52:43,040 --> 20:52:48,480 being an HTML tag and among the few tags 33047 20:52:45,512 --> 20:52:49,920 that the world removed and got rid of so 33048 20:52:48,480 --> 20:52:52,320 that it's no longer used because it's 33049 20:52:49,920 --> 20:52:54,232 not all that user friendly but down here 33050 20:52:52,320 --> 20:52:57,040 what am I doing I'm getting the body of 33051 20:52:54,232 --> 20:52:59,280 the document itself with this variable 33052 20:52:57,040 --> 20:53:00,680 and then I'm checking two CSS properties 33053 20:52:59,280 --> 20:53:03,000 that we didn't talk about before but it 33054 20:53:00,680 --> 20:53:05,192 turns out that you can check and set the 33055 20:53:03,000 --> 20:53:07,512 visibility of an element in JavaScript 33056 20:53:05,192 --> 20:53:10,232 by going into that tag checking its 33057 20:53:07,512 --> 20:53:12,680 style and getting its visibility uh 33058 20:53:10,232 --> 20:53:15,232 property and if it happens to equal 33059 20:53:12,680 --> 20:53:18,232 hidden the next line of code here 13 33060 20:53:15,232 --> 20:53:19,800 sets it equal to visible instead else if 33061 20:53:18,232 --> 20:53:21,960 it's not hidden it must already be 33062 20:53:19,800 --> 20:53:24,720 visible and so line 17 flips it the 33063 20:53:21,960 --> 20:53:26,552 other way and changes it to Hidden here 33064 20:53:24,720 --> 20:53:28,800 left hand right hand clearly not talking 33065 20:53:26,552 --> 20:53:31,720 no idea why the opposite of visible Is 33066 20:53:28,800 --> 20:53:33,512 Not Invisible it's indeed visible and 33067 20:53:31,720 --> 20:53:35,232 hidden but this just allows you every 33068 20:53:33,512 --> 20:53:37,680 time this function is called to change 33069 20:53:35,232 --> 20:53:39,760 the property from one value to another 33070 20:53:37,680 --> 20:53:41,160 achieving that blinking effect you can 33071 20:53:39,760 --> 20:53:42,960 do even more powerful things that you 33072 20:53:41,160 --> 20:53:45,400 and I take for granted every day let's 33073 20:53:42,960 --> 20:53:47,512 go into Source 8 and go to autocom 33074 20:53:45,400 --> 20:53:50,680 complete. HTML which I wrote in advance 33075 20:53:47,512 --> 20:53:52,960 and this is a page that also loads into 33076 20:53:50,680 --> 20:53:55,192 memory a really big dictionary that you 33077 20:53:52,960 --> 20:53:56,832 might recall from problem set five and 33078 20:53:55,192 --> 20:54:01,192 if I go ahead and type in something like 33079 20:53:56,832 --> 20:54:03,800 c a t you'll notice dynamically an 33080 20:54:01,192 --> 20:54:05,872 unordered bulleted list appearing below 33081 20:54:03,800 --> 20:54:08,480 the text box that shows you all of the 33082 20:54:05,872 --> 20:54:11,512 words in that dictionary from pet 5 that 33083 20:54:08,480 --> 20:54:13,232 start with c and then CA and then T just 33084 20:54:11,512 --> 20:54:15,680 like the autocomplete you see every day 33085 20:54:13,232 --> 20:54:17,920 on your phone in Google or websites like 33086 20:54:15,680 --> 20:54:20,480 it how is that working well probably I'm 33087 20:54:17,920 --> 20:54:22,400 listening for the key press going up as 33088 20:54:20,480 --> 20:54:24,872 soon as that key is pressed I'm probably 33089 20:54:22,400 --> 20:54:26,920 searching through a big array uh really 33090 20:54:24,872 --> 20:54:29,000 of all of those words maybe using linear 33091 20:54:26,920 --> 20:54:30,960 search maybe binary search if uh faster 33092 20:54:29,000 --> 20:54:33,480 than that and then looking for any 33093 20:54:30,960 --> 20:54:36,512 string in that array that starts with C 33094 20:54:33,480 --> 20:54:39,120 or CA or c a t and then I'm generating 33095 20:54:36,512 --> 20:54:41,480 automattic IC Ally the HTML therefore 33096 20:54:39,120 --> 20:54:43,232 but perhaps most familiar nowadays is 33097 20:54:41,480 --> 20:54:46,440 just how much your phone and your laptop 33098 20:54:43,232 --> 20:54:49,160 know about you and let me go into a 33099 20:54:46,440 --> 20:54:51,680 final example here in Source 8 called 33100 20:54:49,160 --> 20:54:53,232 geolocation HTML which is a term of art 33101 20:54:51,680 --> 20:54:54,872 that just refers to figuring out your 33102 20:54:53,232 --> 20:54:57,040 geography for instance your GPS 33103 20:54:54,872 --> 20:54:59,120 coordinates now here we have a third and 33104 20:54:57,040 --> 20:55:01,280 final Global variable that your browser 33105 20:54:59,120 --> 20:55:03,192 provides you with called Navigator and 33106 20:55:01,280 --> 20:55:04,872 in Navigator there's a special object 33107 20:55:03,192 --> 20:55:07,552 called geolocation that comes with a 33108 20:55:04,872 --> 20:55:11,000 function called get current position 33109 20:55:07,552 --> 20:55:13,280 so if you call Navigator geolocation dog 33110 20:55:11,000 --> 20:55:14,960 getcurrent position and then pass in a 33111 20:55:13,280 --> 20:55:16,640 function of your own that doesn't need a 33112 20:55:14,960 --> 20:55:19,192 name but does take an argument in this 33113 20:55:16,640 --> 20:55:20,800 case I called it position as soon as the 33114 20:55:19,192 --> 20:55:23,160 browser or as soon as the phone has 33115 20:55:20,800 --> 20:55:25,480 figured out where in the world that user 33116 20:55:23,160 --> 20:55:27,480 is with that browser at some latitude 33117 20:55:25,480 --> 20:55:30,000 and longitude the browser will 33118 20:55:27,480 --> 20:55:31,512 automatically call that function for you 33119 20:55:30,000 --> 20:55:32,872 and you can do anything you want with 33120 20:55:31,512 --> 20:55:34,872 the position that comes back the 33121 20:55:32,872 --> 20:55:36,160 latitude and longitude respectively so 33122 20:55:34,872 --> 20:55:38,000 I'm going to use a function that's not 33123 20:55:36,160 --> 20:55:39,680 often that help but for our purposes 33124 20:55:38,000 --> 20:55:41,800 today it's just going to write to the 33125 20:55:39,680 --> 20:55:43,640 document itself to my big rectangular 33126 20:55:41,800 --> 20:55:46,680 region whatever the latitude is then a 33127 20:55:43,640 --> 20:55:48,872 comma and then the longitude as well so 33128 20:55:46,680 --> 20:55:53,280 if we go back with this final flourish 33129 20:55:48,872 --> 20:55:54,920 into Source 8 open up geolocation HTML 33130 20:55:53,280 --> 20:55:57,800 you'll see that my browser first wants 33131 20:55:54,920 --> 20:55:59,640 my permission to let this website my own 33132 20:55:57,800 --> 20:56:01,832 know my location I'm going to go ahead 33133 20:55:59,640 --> 20:56:02,920 and click allow crossing my fingers 33134 20:56:01,832 --> 20:56:04,512 because it might take a moment for the 33135 20:56:02,920 --> 20:56:07,280 phone or the laptop to figure it out and 33136 20:56:04,512 --> 20:56:09,640 it looks like according to Google I am 33137 20:56:07,280 --> 20:56:13,192 right this moment with my Mac at 33138 20:56:09,640 --> 20:56:15,552 latitude longitude 42375 comma 33139 20:56:13,192 --> 20:56:17,320 7111 let's go ahead and highlight and 33140 20:56:15,552 --> 20:56:20,232 copy that let's go to a website like 33141 20:56:17,320 --> 20:56:21,280 Googl maps.com crossing our fingers if 33142 20:56:20,232 --> 20:56:24,552 you've never done this you can search 33143 20:56:21,280 --> 20:56:28,512 for GPS coordinates too let's hit enter 33144 20:56:24,552 --> 20:56:30,960 and amazing we are indeed in Sanders 33145 20:56:28,512 --> 20:56:34,040 Theater roughly there standing on this 33146 20:56:30,960 --> 20:56:36,280 stage on Halloween and that then is week 33147 20:56:34,040 --> 20:56:38,160 eight we will see you next time happy 33148 20:56:36,280 --> 20:56:39,080 Halloween 33149 20:56:38,160 --> 20:56:42,000 nothing 33150 20:56:39,080 --> 20:56:45,832 go buffering 33151 20:56:42,000 --> 20:56:45,832 okay Josh 33152 20:56:48,900 --> 20:56:55,360 [Music] 33153 20:56:50,800 --> 20:56:55,360 nice it's Moy no oh 33154 20:56:58,480 --> 20:57:02,192 wait that was amazing 33155 20:57:02,552 --> 20:57:14,360 Josh uh um Sophie 33156 20:57:07,140 --> 20:57:16,290 [Music] 33157 20:57:14,360 --> 20:57:20,040 amazing that was 33158 20:57:16,290 --> 20:57:23,360 [Music] 33159 20:57:20,040 --> 20:57:25,040 perfect I think I 33160 20:57:23,360 --> 20:57:28,040 hey to 33161 20:57:25,040 --> 20:57:28,040 you 33162 20:57:28,290 --> 20:57:35,640 [Music] 33163 20:57:30,640 --> 20:57:38,640 oh that was amazing thank you all so 33164 20:57:35,640 --> 20:57:38,640 good 33165 20:57:39,160 --> 20:57:47,270 [Music] 33166 20:57:53,250 --> 20:58:01,570 [Music] 33167 20:58:17,870 --> 20:58:36,510 [Music] 33168 20:58:50,360 --> 20:58:53,470 [Music] 33169 20:59:00,290 --> 20:59:13,660 [Music] 33170 20:59:28,000 --> 20:59:33,360 all right this is cs50 and this is 33171 20:59:30,832 --> 20:59:35,600 already week nine which is our second to 33172 20:59:33,360 --> 20:59:37,800 last indeed this is really the last week 33173 20:59:35,600 --> 20:59:39,800 where you'll learn in this class how to 33174 20:59:37,800 --> 20:59:41,680 program but indeed it's this week that's 33175 20:59:39,800 --> 20:59:43,280 really meant to be the the pedagogical 33176 20:59:41,680 --> 20:59:44,832 climax of like all of these various 33177 20:59:43,280 --> 20:59:46,360 languages we've been looking at all of 33178 20:59:44,832 --> 20:59:48,720 these various techniques all of this 33179 20:59:46,360 --> 20:59:50,280 syntax so that at the end of cs50 in 33180 20:59:48,720 --> 20:59:52,080 just a few weeks you indeed feel that 33181 20:59:50,280 --> 20:59:53,960 you didn't take a class on C and you 33182 20:59:52,080 --> 20:59:55,512 didn't take a class on python but you 33183 20:59:53,960 --> 20:59:56,872 really more generally took a class on 33184 20:59:55,512 --> 20:59:59,120 programming because indeed we know 33185 20:59:56,872 --> 21:00:00,920 already about half of you uh will go on 33186 20:59:59,120 --> 21:00:02,600 to study computer science further but 33187 21:00:00,920 --> 21:00:04,480 half of you will not and indeed all of 33188 21:00:02,600 --> 21:00:06,080 your programming chops here on out 33189 21:00:04,480 --> 21:00:08,600 theoretically will have a foundation in 33190 21:00:06,080 --> 21:00:09,920 what we been doing these past many weeks 33191 21:00:08,600 --> 21:00:11,920 but here on out it's really going to be 33192 21:00:09,920 --> 21:00:13,640 up to you to learn some new fangled 33193 21:00:11,920 --> 21:00:15,080 language when it comes out or to follow 33194 21:00:13,640 --> 21:00:17,000 some new trend when some language 33195 21:00:15,080 --> 21:00:19,232 eclipses the ones we've been using as 33196 21:00:17,000 --> 21:00:21,040 more popular as more appropriate for 33197 21:00:19,232 --> 21:00:23,000 problems you want to solve and so today 33198 21:00:21,040 --> 21:00:25,000 really is about synthesizing so many of 33199 21:00:23,000 --> 21:00:26,800 the past few weeks but doing it in the 33200 21:00:25,000 --> 21:00:28,400 context of web programming which For 33201 21:00:26,800 --> 21:00:30,800 Better or For Worse is so very much 33202 21:00:28,400 --> 21:00:32,552 invogue nowadays both on our laptops and 33203 21:00:30,800 --> 21:00:34,320 phones and indeed the languages we 33204 21:00:32,552 --> 21:00:36,512 looked at in recent weeks are used not 33205 21:00:34,320 --> 21:00:38,600 only to make websites but also full 33206 21:00:36,512 --> 21:00:40,120 fledged applications and app stores and 33207 21:00:38,600 --> 21:00:42,192 the like so this really will be the 33208 21:00:40,120 --> 21:00:43,680 culmination of those past several weeks 33209 21:00:42,192 --> 21:00:45,720 and indeed we'll even talk about some 33210 21:00:43,680 --> 21:00:47,232 familiar Concepts like shopping carts 33211 21:00:45,720 --> 21:00:48,680 when you're on Amazon and these things 33212 21:00:47,232 --> 21:00:50,720 called cookies when you're visiting 33213 21:00:48,680 --> 21:00:52,040 websites all of those topics too will 33214 21:00:50,720 --> 21:00:53,600 come into play and you'll have an 33215 21:00:52,040 --> 21:00:55,720 understanding of what all that means 33216 21:00:53,600 --> 21:00:58,360 from the ground up so how did we get 33217 21:00:55,720 --> 21:01:00,872 here well just last week we focused on 33218 21:00:58,360 --> 21:01:02,440 HTML and CSS primarily which are not 33219 21:01:00,872 --> 21:01:04,440 programming languages they're just about 33220 21:01:02,440 --> 21:01:06,800 Aesthetics structuring your data 33221 21:01:04,440 --> 21:01:09,800 presenting your data and so forth um and 33222 21:01:06,800 --> 21:01:11,872 we served the web pages we wrote using 33223 21:01:09,800 --> 21:01:13,960 this program HTTP server this is just 33224 21:01:11,872 --> 21:01:16,160 one such program there's dozens hundreds 33225 21:01:13,960 --> 21:01:17,680 of different web servers that you can 33226 21:01:16,160 --> 21:01:19,232 use out there this is just a super 33227 21:01:17,680 --> 21:01:21,232 simple one we pre-installed in your 33228 21:01:19,232 --> 21:01:23,320 codes space for you in vs code so that 33229 21:01:21,232 --> 21:01:25,360 you can just serve up web pages at the 33230 21:01:23,320 --> 21:01:27,280 end of last week two though we teased 33231 21:01:25,360 --> 21:01:30,192 JavaScript a full-fledged programming 33232 21:01:27,280 --> 21:01:32,232 language that you can use to manipulate 33233 21:01:30,192 --> 21:01:34,280 the users experience for the better to 33234 21:01:32,232 --> 21:01:36,280 make things more Dynamic and interactive 33235 21:01:34,280 --> 21:01:38,360 by actually running code in the user 33236 21:01:36,280 --> 21:01:40,680 users's browser on their Mac their PC 33237 21:01:38,360 --> 21:01:42,040 their phone as opposed to server side 33238 21:01:40,680 --> 21:01:44,720 which up until now is where all of our 33239 21:01:42,040 --> 21:01:46,320 code in C and python has been written so 33240 21:01:44,720 --> 21:01:48,600 you're writing code on a server you're 33241 21:01:46,320 --> 21:01:50,920 serving code from a server but now with 33242 21:01:48,600 --> 21:01:53,480 HTML CSS and JavaScript it's getting 33243 21:01:50,920 --> 21:01:55,552 executed in a browser but today we're 33244 21:01:53,480 --> 21:01:57,552 going to give you one final feature of 33245 21:01:55,552 --> 21:01:59,760 python or really languages like it that 33246 21:01:57,552 --> 21:02:02,120 you can also use code on the server to 33247 21:01:59,760 --> 21:02:04,600 generate automatically dynamically the 33248 21:02:02,120 --> 21:02:06,680 HTML uh the JavaScript the CSS that you 33249 21:02:04,600 --> 21:02:08,552 actually want the user to rece receive 33250 21:02:06,680 --> 21:02:10,760 you don't have to hardcode everything as 33251 21:02:08,552 --> 21:02:12,600 you have when making your own homepage 33252 21:02:10,760 --> 21:02:14,000 well let's consider what what some of 33253 21:02:12,600 --> 21:02:17,120 the building blocks were last week so 33254 21:02:14,000 --> 21:02:19,280 here's a sample URL and over here slash 33255 21:02:17,120 --> 21:02:21,640 is sort of the default page on any web 33256 21:02:19,280 --> 21:02:22,760 server it might be index.html it might 33257 21:02:21,640 --> 21:02:24,480 be something else that's just a 33258 21:02:22,760 --> 21:02:26,400 convention but it refers to whatever the 33259 21:02:24,480 --> 21:02:28,232 default actually is you can visit of 33260 21:02:26,400 --> 21:02:31,280 course when any browser like a URL that 33261 21:02:28,232 --> 21:02:33,160 ends in file. HTML or something else. 33262 21:02:31,280 --> 21:02:36,280 HTML and that literally means your 33263 21:02:33,160 --> 21:02:37,920 browser wants this file on this server 33264 21:02:36,280 --> 21:02:40,512 or of course we saw that it can be a 33265 21:02:37,920 --> 21:02:42,512 folder and inside of that folder is 33266 21:02:40,512 --> 21:02:44,760 presumably some default file name like 33267 21:02:42,512 --> 21:02:47,400 again index.html or you can be more 33268 21:02:44,760 --> 21:02:48,760 explicit like folder file. HTML and 33269 21:02:47,400 --> 21:02:51,120 these more generally we just called 33270 21:02:48,760 --> 21:02:53,080 paths and indeed a path is just a 33271 21:02:51,120 --> 21:02:55,872 location on your Mac your PC or on a 33272 21:02:53,080 --> 21:02:57,552 server of some piece of information but 33273 21:02:55,872 --> 21:02:59,680 today we're just going to rename this 33274 21:02:57,552 --> 21:03:01,000 only to use other common terminology but 33275 21:02:59,680 --> 21:03:02,832 they're really just synonyms today we're 33276 21:03:01,000 --> 21:03:04,960 going to refer to those same things as 33277 21:03:02,832 --> 21:03:07,160 routes because now today we're going to 33278 21:03:04,960 --> 21:03:09,040 ultimately replace HT PTP server which 33279 21:03:07,160 --> 21:03:11,832 just serves up static content that you 33280 21:03:09,040 --> 21:03:13,960 all write with your own web server like 33281 21:03:11,832 --> 21:03:15,960 now you will be the ones controlling 33282 21:03:13,960 --> 21:03:18,192 what it is the server does in response 33283 21:03:15,960 --> 21:03:19,640 to the user in uh requests so that you 33284 21:03:18,192 --> 21:03:21,600 can respond interactively and 33285 21:03:19,640 --> 21:03:23,680 dynamically but we're still going to see 33286 21:03:21,600 --> 21:03:26,280 techniques like this these were our 33287 21:03:23,680 --> 21:03:27,552 so-called HTTP parameters they're 33288 21:03:26,280 --> 21:03:29,680 everything that comes after a question 33289 21:03:27,552 --> 21:03:31,832 mark in a URL and it can be like key 33290 21:03:29,680 --> 21:03:34,160 equals value and an example was what 33291 21:03:31,832 --> 21:03:36,832 when we played with Google what was the 33292 21:03:34,160 --> 21:03:40,040 key and what was the value 33293 21:03:36,832 --> 21:03:40,040 that I first tried any 33294 21:03:40,480 --> 21:03:44,832 recollection I was searching for cats 33295 21:03:42,720 --> 21:03:46,552 and so the key I figured that was Q 33296 21:03:44,832 --> 21:03:48,512 because that's what Larry and Sergey who 33297 21:03:46,552 --> 21:03:51,440 created Google years ago decided the 33298 21:03:48,512 --> 21:03:53,640 name would be of the HTML text box that 33299 21:03:51,440 --> 21:03:55,680 you type your query into and if I type 33300 21:03:53,640 --> 21:03:57,640 cat for cat the value of that would end 33301 21:03:55,680 --> 21:03:59,640 up in the URL for Google as being 33302 21:03:57,640 --> 21:04:01,192 question mark cat equals value and I 33303 21:03:59,640 --> 21:04:02,720 mentioned that it's often the case that 33304 21:04:01,192 --> 21:04:04,600 you want to send two different inputs to 33305 21:04:02,720 --> 21:04:05,960 a server and this is why I propose that 33306 21:04:04,600 --> 21:04:07,832 you just keep an eye out for Amper Sands 33307 21:04:05,960 --> 21:04:09,720 and and Ampersand separate these key 33308 21:04:07,832 --> 21:04:11,320 value pairs but again this is the same 33309 21:04:09,720 --> 21:04:13,080 darn Paradigm as before and we've seen 33310 21:04:11,320 --> 21:04:15,960 this so many times right key value pairs 33311 21:04:13,080 --> 21:04:18,680 in dictionaries in Python we've seen uh 33312 21:04:15,960 --> 21:04:21,320 HTML attributes and their values we've 33313 21:04:18,680 --> 21:04:23,360 seen CSS properties and their values 33314 21:04:21,320 --> 21:04:24,832 it's all the same thing associating 33315 21:04:23,360 --> 21:04:26,640 something with something else even 33316 21:04:24,832 --> 21:04:28,960 though every language every person seems 33317 21:04:26,640 --> 21:04:30,872 to have their own uh vernacular for it 33318 21:04:28,960 --> 21:04:32,360 it really is just the same idea this 33319 21:04:30,872 --> 21:04:35,000 associating of something with something 33320 21:04:32,360 --> 21:04:37,192 else we'll continue to see and here to 33321 21:04:35,000 --> 21:04:40,120 be concrete were the 33322 21:04:37,192 --> 21:04:42,080 HTTP lines of text that were in those 33323 21:04:40,120 --> 21:04:44,120 virtual envelopes if you will if I were 33324 21:04:42,080 --> 21:04:45,872 indeed selecting trying to search for 33325 21:04:44,120 --> 21:04:48,232 something like cats on Google this 33326 21:04:45,872 --> 21:04:50,640 recall was the message that got sent to 33327 21:04:48,232 --> 21:04:52,680 the server by my browser in order to 33328 21:04:50,640 --> 21:04:55,552 tell Google to please search for not 33329 21:04:52,680 --> 21:04:57,440 dogs but in this case cats now what is 33330 21:04:55,552 --> 21:04:59,600 HTTP server been doing for us well it's 33331 21:04:57,440 --> 21:05:02,480 just been serving up HTML files CSS 33332 21:04:59,600 --> 21:05:05,160 files maybe some js or JavaScript files 33333 21:05:02,480 --> 21:05:07,232 but it has been ignoring any HTTP 33334 21:05:05,160 --> 21:05:09,360 parameters like HTTP server does not 33335 21:05:07,232 --> 21:05:10,960 take user input why well what's it going 33336 21:05:09,360 --> 21:05:12,832 to do with it because you already wrote 33337 21:05:10,960 --> 21:05:15,360 the HTML you already wrote the CSS like 33338 21:05:12,832 --> 21:05:17,192 there's no decisions to be made until we 33339 21:05:15,360 --> 21:05:18,720 introduce a proper programming language 33340 21:05:17,192 --> 21:05:20,832 on the server and so we're going to move 33341 21:05:18,720 --> 21:05:23,280 away now from this simple HTTP server 33342 21:05:20,832 --> 21:05:25,480 program and introduce you to your own 33343 21:05:23,280 --> 21:05:27,760 server that's going to handle the 33344 21:05:25,480 --> 21:05:29,440 parsing that is the extraction of these 33345 21:05:27,760 --> 21:05:30,832 key value pairs so that you and I don't 33346 21:05:29,440 --> 21:05:32,760 have to write python code all of a 33347 21:05:30,832 --> 21:05:34,480 sudden that like analyzes this stuff 33348 21:05:32,760 --> 21:05:36,400 figures out what pages requested the key 33349 21:05:34,480 --> 21:05:38,960 value pairs all of that were still going 33350 21:05:36,400 --> 21:05:42,280 to get for free by just using the right 33351 21:05:38,960 --> 21:05:44,280 framework and so today we revisit python 33352 21:05:42,280 --> 21:05:45,640 uh which we've now used in some form the 33353 21:05:44,280 --> 21:05:47,512 past few weeks and indeed it's kind of 33354 21:05:45,640 --> 21:05:49,512 been the glue that allows us to stitch 33355 21:05:47,512 --> 21:05:51,440 together some of our own logic we saw it 33356 21:05:49,512 --> 21:05:54,192 with SQL we're going to now see it with 33357 21:05:51,440 --> 21:05:55,480 HTML CSS and even JavaScript if we want 33358 21:05:54,192 --> 21:05:56,832 and we're also going to see another 33359 21:05:55,480 --> 21:05:58,320 language today not a programming 33360 21:05:56,832 --> 21:05:59,960 language called Ginga and this is going 33361 21:05:58,320 --> 21:06:02,680 to be a common Paradigm in the real 33362 21:05:59,960 --> 21:06:04,400 world whereby different languages 33363 21:06:02,680 --> 21:06:06,320 different libraries different Frameworks 33364 21:06:04,400 --> 21:06:08,320 often like borrow from each other or 33365 21:06:06,320 --> 21:06:09,760 they use uh technologies that someone 33366 21:06:08,320 --> 21:06:12,192 else wrote just so they don't have to 33367 21:06:09,760 --> 21:06:14,160 reinvent that wheel so flask is just a 33368 21:06:12,192 --> 21:06:15,720 framework that is a third party Library 33369 21:06:14,160 --> 21:06:17,320 it's pretty popular nowadays it's 33370 21:06:15,720 --> 21:06:19,920 relatively simple which is why we use it 33371 21:06:17,320 --> 21:06:22,320 in cs50 if you've programmed before cs50 33372 21:06:19,920 --> 21:06:24,400 Jango is another popular framework or 33373 21:06:22,320 --> 21:06:26,080 library in the python sta space but it's 33374 21:06:24,400 --> 21:06:28,600 a little more complicated so we focus on 33375 21:06:26,080 --> 21:06:30,040 flask and Ginga we'll see is not a 33376 21:06:28,600 --> 21:06:32,480 programming language it's just going to 33377 21:06:30,040 --> 21:06:34,600 be some syntax thankfully familiar with 33378 21:06:32,480 --> 21:06:37,600 curly braces that allow us to use 33379 21:06:34,600 --> 21:06:39,040 placeholders in our actual web pages so 33380 21:06:37,600 --> 21:06:40,872 again you'll wrap your minds all 33381 21:06:39,040 --> 21:06:42,280 eventually around where the lines are 33382 21:06:40,872 --> 21:06:43,920 among these various Technologies but 33383 21:06:42,280 --> 21:06:45,280 these are not the interesting ideas the 33384 21:06:43,920 --> 21:06:47,920 interesting ideas are the ones we'll 33385 21:06:45,280 --> 21:06:50,600 focus on in code but starting today 33386 21:06:47,920 --> 21:06:52,720 instead of running HTTP server to serve 33387 21:06:50,600 --> 21:06:55,760 up a static website we'll have you start 33388 21:06:52,720 --> 21:06:58,512 running literally flask space run in 33389 21:06:55,760 --> 21:07:01,512 your terminal window to run your own web 33390 21:06:58,512 --> 21:07:04,400 server that's implemented in Python 33391 21:07:01,512 --> 21:07:07,000 using this flask framework so bootstrap 33392 21:07:04,400 --> 21:07:09,512 was a library for making your CSS and 33393 21:07:07,000 --> 21:07:11,872 JavaScript prettier and more interactive 33394 21:07:09,512 --> 21:07:13,800 flask is a framework or library for just 33395 21:07:11,872 --> 21:07:16,040 making your python code more pleasant to 33396 21:07:13,800 --> 21:07:18,160 use since you're borrowing features from 33397 21:07:16,040 --> 21:07:19,760 someone else all right so how can we go 33398 21:07:18,160 --> 21:07:22,080 about doing this well if you are to 33399 21:07:19,760 --> 21:07:25,040 write your very own web application your 33400 21:07:22,080 --> 21:07:27,552 own amazon.com your own google.com in 33401 21:07:25,040 --> 21:07:30,040 Python using flask minimally you need to 33402 21:07:27,552 --> 21:07:31,760 have a file called app.py by convention 33403 21:07:30,040 --> 21:07:33,680 which is where all your python code goes 33404 21:07:31,760 --> 21:07:35,232 and then a folder called templates which 33405 21:07:33,680 --> 21:07:36,680 is where all of your templates go and 33406 21:07:35,232 --> 21:07:38,800 for now your templates are just your 33407 21:07:36,680 --> 21:07:40,320 HTML files so if we're going to now 33408 21:07:38,800 --> 21:07:42,960 start building more interesting 33409 21:07:40,320 --> 21:07:45,192 interactive things like google.com or 33410 21:07:42,960 --> 21:07:46,600 amazon.com we need to be able to execute 33411 21:07:45,192 --> 21:07:48,640 code on the server and so this is the 33412 21:07:46,600 --> 21:07:50,872 convention it's not index.html anymore 33413 21:07:48,640 --> 21:07:53,000 necessarily it's these two things at the 33414 21:07:50,872 --> 21:07:54,680 top level with that said we'll quickly 33415 21:07:53,000 --> 21:07:57,000 see that there's some other conventions 33416 21:07:54,680 --> 21:07:59,000 and in my examples online and in the uh 33417 21:07:57,000 --> 21:08:01,680 problem set nine you'll see another file 33418 21:07:59,000 --> 21:08:03,640 called requirements.txt which is just a 33419 21:08:01,680 --> 21:08:05,800 text file that allows you to enumerate 33420 21:08:03,640 --> 21:08:07,760 all of the thirdparty libraries your 33421 21:08:05,800 --> 21:08:09,280 application might want to use it's a 33422 21:08:07,760 --> 21:08:11,000 convention so that the server can like 33423 21:08:09,280 --> 21:08:12,832 automatically install things for you 33424 21:08:11,000 --> 21:08:14,480 without you having to do it manually and 33425 21:08:12,832 --> 21:08:16,320 then static is going to be where 33426 21:08:14,480 --> 21:08:17,600 literally your static content goes so if 33427 21:08:16,320 --> 21:08:19,640 you've got images for your web 33428 21:08:17,600 --> 21:08:22,120 application if you've got JavaScript 33429 21:08:19,640 --> 21:08:24,000 files CSS files by convention that goes 33430 21:08:22,120 --> 21:08:25,640 in static these are just conventions 33431 21:08:24,000 --> 21:08:27,872 like all of this can be changed but this 33432 21:08:25,640 --> 21:08:30,120 is like the way to do things so we'll 33433 21:08:27,872 --> 21:08:32,360 introduce you to the defaults all right 33434 21:08:30,120 --> 21:08:34,400 so what does this mean how for instance 33435 21:08:32,360 --> 21:08:37,440 could I go about implementing my own web 33436 21:08:34,400 --> 21:08:40,600 application using python that somehow 33437 21:08:37,440 --> 21:08:43,400 spits out a message like Hello World all 33438 21:08:40,600 --> 21:08:45,400 right well turns out just this now we'll 33439 21:08:43,400 --> 21:08:48,360 tease this apart in just a moment but 33440 21:08:45,400 --> 21:08:50,600 this is the content of a sample app.py 33441 21:08:48,360 --> 21:08:52,480 file that apparently uses some Library 33442 21:08:50,600 --> 21:08:54,080 stuff like familiar syntax from 33443 21:08:52,480 --> 21:08:56,192 something import something else we've 33444 21:08:54,080 --> 21:08:58,552 seen that before with csvs and other 33445 21:08:56,192 --> 21:09:00,440 libraries this is somewhat new syntax 33446 21:08:58,552 --> 21:09:01,960 but it's kind of copy paste for now this 33447 21:09:00,440 --> 21:09:03,680 is definitely new syntax and kind of 33448 21:09:01,960 --> 21:09:05,400 weird with the at sign here but we'll 33449 21:09:03,680 --> 21:09:07,160 see this again and again today and it's 33450 21:09:05,400 --> 21:09:08,960 just copy paste initially until you 33451 21:09:07,160 --> 21:09:10,720 understand what it's doing for you but 33452 21:09:08,960 --> 21:09:13,080 at least there's some familiar stuff 33453 21:09:10,720 --> 21:09:15,552 here like index.html is still going to 33454 21:09:13,080 --> 21:09:17,920 be with us but it's going to be up to us 33455 21:09:15,552 --> 21:09:19,552 when and how to show it to the user so 33456 21:09:17,920 --> 21:09:23,400 let's make this more real let me go over 33457 21:09:19,552 --> 21:09:26,280 to vs code here and let me go ahead and 33458 21:09:23,400 --> 21:09:28,800 create a how about we'll do this in 33459 21:09:26,280 --> 21:09:30,232 hello let me do makeer hello to make a 33460 21:09:28,800 --> 21:09:32,160 new folder called hello and I'm going to 33461 21:09:30,232 --> 21:09:33,720 CD into it just to isolate all of these 33462 21:09:32,160 --> 21:09:35,080 files to the same directory so that we 33463 21:09:33,720 --> 21:09:38,512 have different apps today and different 33464 21:09:35,080 --> 21:09:40,440 folders and now I'm going to do code of 33465 21:09:38,512 --> 21:09:43,760 let's do this actually let's do our 33466 21:09:40,440 --> 21:09:45,832 maker templates. HTM ah sorry not 33467 21:09:43,760 --> 21:09:48,080 templates. HTML let me rename that to 33468 21:09:45,832 --> 21:09:49,360 templates using the MV command this has 33469 21:09:48,080 --> 21:09:51,832 nothing to do with web programming this 33470 21:09:49,360 --> 21:09:54,280 is me making typos so if I type LS now 33471 21:09:51,832 --> 21:09:56,600 I've got a folder called templates all 33472 21:09:54,280 --> 21:09:59,080 right in there let's create a file 33473 21:09:56,600 --> 21:10:01,080 called index.html that is going to be 33474 21:09:59,080 --> 21:10:02,760 super simple and pretty much copy paste 33475 21:10:01,080 --> 21:10:05,040 from last week let me hide my terminal 33476 21:10:02,760 --> 21:10:07,232 window and let me just very quickly whip 33477 21:10:05,040 --> 21:10:10,872 up a simp simple hello world page using 33478 21:10:07,232 --> 21:10:13,280 my HTML tag Lang will equal English then 33479 21:10:10,872 --> 21:10:15,360 inside of this I'm going to have a head 33480 21:10:13,280 --> 21:10:16,720 tag inside of this I'm going to have a 33481 21:10:15,360 --> 21:10:18,760 title tag and I'm just going to call 33482 21:10:16,720 --> 21:10:20,760 this thing hello uh I'm going to then 33483 21:10:18,760 --> 21:10:22,680 have a body and in this I'm only going 33484 21:10:20,760 --> 21:10:24,360 to say something simple like hello comma 33485 21:10:22,680 --> 21:10:26,080 world and just so this is mobile 33486 21:10:24,360 --> 21:10:28,040 friendly recall that we touched on these 33487 21:10:26,080 --> 21:10:29,360 meta tags so just in case you after 33488 21:10:28,040 --> 21:10:31,480 class play with your mobile device 33489 21:10:29,360 --> 21:10:33,320 instead of your laptop I'll do name 33490 21:10:31,480 --> 21:10:36,512 equals quote unquote 33491 21:10:33,320 --> 21:10:38,040 viewport uh viewport and and content 33492 21:10:36,512 --> 21:10:39,400 equals and I never remember this I'm 33493 21:10:38,040 --> 21:10:43,280 literally reading it off of a cheat 33494 21:10:39,400 --> 21:10:45,512 sheet initial scale equals 1 width 33495 21:10:43,280 --> 21:10:47,872 equals device width and this is just 33496 21:10:45,512 --> 21:10:49,320 this magical incantation that says to 33497 21:10:47,872 --> 21:10:51,800 the browser like size things 33498 21:10:49,320 --> 21:10:54,000 appropriately for the size of the device 33499 21:10:51,800 --> 21:10:55,232 it blows up the font sizes a bit all 33500 21:10:54,000 --> 21:10:57,120 right so that's what I would have done 33501 21:10:55,232 --> 21:10:59,192 last week and I would have served this 33502 21:10:57,120 --> 21:11:00,832 web page by running HTTP server in the 33503 21:10:59,192 --> 21:11:02,920 same directory and boom I would see that 33504 21:11:00,832 --> 21:11:05,160 HTML but let's now start to take some 33505 21:11:02,920 --> 21:11:06,600 control over the user's experience and 33506 21:11:05,160 --> 21:11:08,000 for now it's going to be underwhelming 33507 21:11:06,600 --> 21:11:10,232 it's just going to always say hello 33508 21:11:08,000 --> 21:11:12,680 world but in a moment version two is 33509 21:11:10,232 --> 21:11:14,552 going to say hello David or hello Carter 33510 21:11:12,680 --> 21:11:16,280 a bit more dynamically and we'll quickly 33511 21:11:14,552 --> 21:11:18,120 escalate from there to just more 33512 21:11:16,280 --> 21:11:19,832 interesting applications as well 33513 21:11:18,120 --> 21:11:22,000 culminating with things like cookies and 33514 21:11:19,832 --> 21:11:24,232 shopping carts and the like so let me go 33515 21:11:22,000 --> 21:11:25,920 back into my terminal window and as 33516 21:11:24,232 --> 21:11:27,160 promised let me create another file 33517 21:11:25,920 --> 21:11:29,760 called 33518 21:11:27,160 --> 21:11:31,760 app.py and this is where now I need to 33519 21:11:29,760 --> 21:11:34,040 implement the web server I'm going to 33520 21:11:31,760 --> 21:11:35,440 run using this flask framework and for 33521 21:11:34,040 --> 21:11:37,832 now I'm just going to kind of do some 33522 21:11:35,440 --> 21:11:40,000 copy paste from uh what we saw on the 33523 21:11:37,832 --> 21:11:41,920 slide a moment ago from the flask 33524 21:11:40,000 --> 21:11:45,720 Library which we've pre-installed for 33525 21:11:41,920 --> 21:11:48,480 you I'm going to import a uh function 33526 21:11:45,720 --> 21:11:50,400 called flask capital F it's subtle but 33527 21:11:48,480 --> 21:11:52,440 it's important there and I'm also going 33528 21:11:50,400 --> 21:11:55,232 to import a few other things a function 33529 21:11:52,440 --> 21:11:57,720 called render template and another 33530 21:11:55,232 --> 21:11:59,640 variable called request and the only way 33531 21:11:57,720 --> 21:12:01,800 I know this is from having taught this 33532 21:11:59,640 --> 21:12:03,400 before read the documentation followed a 33533 21:12:01,800 --> 21:12:04,920 tutorial like you wouldn't know this 33534 21:12:03,400 --> 21:12:06,552 unless someone told you or you read how 33535 21:12:04,920 --> 21:12:09,320 to do this this but what this means is 33536 21:12:06,552 --> 21:12:11,192 that this Library called flask has three 33537 21:12:09,320 --> 21:12:12,872 things in it a function called flask 33538 21:12:11,192 --> 21:12:15,040 capital f a function called render 33539 21:12:12,872 --> 21:12:16,800 template and a variable built into it 33540 21:12:15,040 --> 21:12:18,800 called request and this is going to be 33541 21:12:16,800 --> 21:12:20,640 all the building blocks I need to 33542 21:12:18,800 --> 21:12:21,960 implement my own web server the 33543 21:12:20,640 --> 21:12:24,192 convention in flask when you want to 33544 21:12:21,960 --> 21:12:26,640 create a web app in Python is you create 33545 21:12:24,192 --> 21:12:28,760 a variable by convention called app and 33546 21:12:26,640 --> 21:12:31,640 then you assign it the return value of 33547 21:12:28,760 --> 21:12:34,440 that flask function capital F and pass 33548 21:12:31,640 --> 21:12:36,960 into it underscore uncore name uncore 33549 21:12:34,440 --> 21:12:39,232 underscore which is weird but we have 33550 21:12:36,960 --> 21:12:41,960 seen this before a few weeks ago anyone 33551 21:12:39,232 --> 21:12:43,872 recall when and why we mentioned uncore 33552 21:12:41,960 --> 21:12:48,080 uncore name uncore 33553 21:12:43,872 --> 21:12:48,080 uncore yeah I think was 33554 21:12:48,640 --> 21:12:54,600 it name or something yeah if we wanted 33555 21:12:51,720 --> 21:12:58,080 to check if the name of the file was 33556 21:12:54,600 --> 21:12:59,400 itself main so that we avoided a 33557 21:12:58,080 --> 21:13:01,320 situation where if you're writing your 33558 21:12:59,400 --> 21:13:03,552 own Library code you don't want your 33559 21:13:01,320 --> 21:13:05,720 code to be executed automatically you 33560 21:13:03,552 --> 21:13:07,400 want to potentially execute the main 33561 21:13:05,720 --> 21:13:09,512 function and that was a solution to that 33562 21:13:07,400 --> 21:13:10,832 problem here for today's purposes this 33563 21:13:09,512 --> 21:13:12,480 is just the way you do it underscore 33564 21:13:10,832 --> 21:13:14,120 underscore name underscore uncore refers 33565 21:13:12,480 --> 21:13:16,360 to the current file and so this is just 33566 21:13:14,120 --> 21:13:18,440 a little trick that says turn this file 33567 21:13:16,360 --> 21:13:22,000 into a flask application that's all it 33568 21:13:18,440 --> 21:13:23,600 is and for now uh that line suffices all 33569 21:13:22,000 --> 21:13:25,640 right what do I want to do after that 33570 21:13:23,600 --> 21:13:28,232 well now I'm in charge of the web server 33571 21:13:25,640 --> 21:13:31,000 I need to write the code that decides 33572 21:13:28,232 --> 21:13:33,080 based on the browsers request what file 33573 21:13:31,000 --> 21:13:35,280 or files I'm going to send from the 33574 21:13:33,080 --> 21:13:36,872 server to the browser last week http 33575 21:13:35,280 --> 21:13:38,552 server did all of this for us just based 33576 21:13:36,872 --> 21:13:40,872 on the file name but today I'm going to 33577 21:13:38,552 --> 21:13:42,832 take over control over that process and 33578 21:13:40,872 --> 21:13:46,552 the way I do that is as follows I say 33579 21:13:42,832 --> 21:13:48,800 app. route with weirdly an at sign in 33580 21:13:46,552 --> 21:13:50,872 front of it this is known in python as a 33581 21:13:48,800 --> 21:13:52,360 decorator and it's a feature of python 33582 21:13:50,872 --> 21:13:54,192 not a flask that we just didn't 33583 21:13:52,360 --> 21:13:56,600 introduce in weeks past but it's a 33584 21:13:54,192 --> 21:13:58,552 special it's a handy trick to do what 33585 21:13:56,600 --> 21:14:01,720 we're about to do the route I want to 33586 21:13:58,552 --> 21:14:04,320 Define is quote unquote slash so that is 33587 21:14:01,720 --> 21:14:06,600 here is code I want the server to 33588 21:14:04,320 --> 21:14:09,000 execute whenever I user visits forward 33589 21:14:06,600 --> 21:14:10,800 slash the default page of the website 33590 21:14:09,000 --> 21:14:12,680 well what code do I want them to execute 33591 21:14:10,800 --> 21:14:14,360 well I want them to execute a function 33592 21:14:12,680 --> 21:14:16,040 and I can therefore Define in Python a 33593 21:14:14,360 --> 21:14:18,872 function I can technically call this 33594 21:14:16,040 --> 21:14:20,400 thing anything I want X or Y or Z but 33595 21:14:18,872 --> 21:14:22,400 because they're accessing the default 33596 21:14:20,400 --> 21:14:24,400 page otherwise known as the index of the 33597 21:14:22,400 --> 21:14:26,160 site I'm going to just more reasonably 33598 21:14:24,400 --> 21:14:27,552 call this function index but just a 33599 21:14:26,160 --> 21:14:29,920 convention you could call it anything 33600 21:14:27,552 --> 21:14:32,120 you want but X Y or Z is probably a bad 33601 21:14:29,920 --> 21:14:34,232 stylistic choice it doesn't need to take 33602 21:14:32,120 --> 21:14:35,872 any arguments in this case and the only 33603 21:14:34,232 --> 21:14:40,080 thing this code this function is going 33604 21:14:35,872 --> 21:14:44,120 to do is for now let's go ahead and have 33605 21:14:40,080 --> 21:14:47,680 it return hello world quote unquote and 33606 21:14:44,120 --> 21:14:50,000 that's it all right now let me go into 33607 21:14:47,680 --> 21:14:52,480 my terminal window let me go ahead and 33608 21:14:50,000 --> 21:14:54,760 do flask run in the same directory that 33609 21:14:52,480 --> 21:14:56,800 has appy and hit enter I'm going to see 33610 21:14:54,760 --> 21:14:59,680 some cryptic output but including a URL 33611 21:14:56,800 --> 21:15:01,360 of my code space and if I open that URL 33612 21:14:59,680 --> 21:15:03,832 after hovering over it I'll indeed see 33613 21:15:01,360 --> 21:15:06,080 hello world as you might hope but let me 33614 21:15:03,832 --> 21:15:08,720 do this let me go ahead and rightclick 33615 21:15:06,080 --> 21:15:10,040 on the page and click view page source 33616 21:15:08,720 --> 21:15:12,280 which if you haven't done before shows 33617 21:15:10,040 --> 21:15:14,480 you all of the HTML for a page however 33618 21:15:12,280 --> 21:15:16,600 pretty or messy it is and that's it 33619 21:15:14,480 --> 21:15:19,600 there's no HTML that I've spit out it's 33620 21:15:16,600 --> 21:15:21,480 just quote unquote hello world well if I 33621 21:15:19,600 --> 21:15:23,160 actually want to spit out a full web 33622 21:15:21,480 --> 21:15:24,512 page which is not a big deal here 33623 21:15:23,160 --> 21:15:26,960 because who cares it's just the text 33624 21:15:24,512 --> 21:15:30,600 anyway but if I want to spit out a whole 33625 21:15:26,960 --> 21:15:33,040 file let me do this I want to return 33626 21:15:30,600 --> 21:15:34,552 essentially the contents of index.html 33627 21:15:33,040 --> 21:15:36,832 which have all of the tags I want the 33628 21:15:34,552 --> 21:15:39,512 mobile friend stuff and all of that well 33629 21:15:36,832 --> 21:15:42,552 I can't just return index.html but I can 33630 21:15:39,512 --> 21:15:44,080 return this render template quote 33631 21:15:42,552 --> 21:15:46,320 unquote 33632 21:15:44,080 --> 21:15:48,232 index.html and per the documentation for 33633 21:15:46,320 --> 21:15:50,232 flask this render template function will 33634 21:15:48,232 --> 21:15:52,872 go find that file for me in my templates 33635 21:15:50,232 --> 21:15:54,400 folder by convention it will open it up 33636 21:15:52,872 --> 21:15:56,800 and then it will spit the whole thing 33637 21:15:54,400 --> 21:15:58,640 out to the browser for me so I can keep 33638 21:15:56,800 --> 21:16:01,720 all my HTML in one place and all my 33639 21:15:58,640 --> 21:16:04,512 python code in this one place so now if 33640 21:16:01,720 --> 21:16:06,320 I go back to my browser and reload I 33641 21:16:04,512 --> 21:16:08,512 don't think I'll really see a difference 33642 21:16:06,320 --> 21:16:11,320 because it's the same text ultimately 33643 21:16:08,512 --> 21:16:14,320 but if I view page Source now notice 33644 21:16:11,320 --> 21:16:16,720 that ah there is all of the HTML that 33645 21:16:14,320 --> 21:16:18,720 was just sent to the browser so this is 33646 21:16:16,720 --> 21:16:20,640 only to say we have the building blocks 33647 21:16:18,720 --> 21:16:22,512 the puzzle pieces if you will via which 33648 21:16:20,640 --> 21:16:24,360 to now store all of our HTML in one 33649 21:16:22,512 --> 21:16:27,040 place and presumably CSS JavaScript and 33650 21:16:24,360 --> 21:16:28,832 so forth but then serve up whatever we 33651 21:16:27,040 --> 21:16:30,720 want even though I'm just blindly 33652 21:16:28,832 --> 21:16:33,160 spitting out 33653 21:16:30,720 --> 21:16:35,640 index.html so before we proceed any 33654 21:16:33,160 --> 21:16:38,440 questions on this which again I claim is 33655 21:16:35,640 --> 21:16:40,120 like my manual version of what HTTP 33656 21:16:38,440 --> 21:16:42,120 server was doing for us automatically 33657 21:16:40,120 --> 21:16:44,552 last week but this is how you do it 33658 21:16:42,120 --> 21:16:46,600 yourself any 33659 21:16:44,552 --> 21:16:48,760 questions all right well let's make it 33660 21:16:46,600 --> 21:16:51,920 more interesting which we could not do 33661 21:16:48,760 --> 21:16:55,160 with HTTP server and HTML alone why 33662 21:16:51,920 --> 21:16:56,960 don't we go ahead and do this let me 33663 21:16:55,160 --> 21:16:59,440 visit the same URL and I'm going to zoom 33664 21:16:56,960 --> 21:17:01,120 in and your url will differ from my code 33665 21:16:59,440 --> 21:17:03,872 space but it's going to end similarly 33666 21:17:01,120 --> 21:17:07,120 here I'm going to do slash question mark 33667 21:17:03,872 --> 21:17:09,320 name equals David for instance or Q 33668 21:17:07,120 --> 21:17:11,160 equals cats or name equals Carter any 33669 21:17:09,320 --> 21:17:13,512 key value pair I want I'm going to 33670 21:17:11,160 --> 21:17:15,040 append after a slash and a question mark 33671 21:17:13,512 --> 21:17:17,192 thereby providing user input to the 33672 21:17:15,040 --> 21:17:18,640 server albe it in a very user unfriendly 33673 21:17:17,192 --> 21:17:21,440 way no one's going to normally do this 33674 21:17:18,640 --> 21:17:23,000 in their browser enter nothing changes 33675 21:17:21,440 --> 21:17:24,552 here it just says hello world but 33676 21:17:23,000 --> 21:17:26,872 wouldn't it be nice if it says hello 33677 21:17:24,552 --> 21:17:28,920 David or equivalently if I zoom in here 33678 21:17:26,872 --> 21:17:30,640 again and change David to Carter and hit 33679 21:17:28,920 --> 21:17:33,080 enter wouldn't it be nice if it says 33680 21:17:30,640 --> 21:17:35,960 hello Carter instead so we need some 33681 21:17:33,080 --> 21:17:39,040 dynamism there and here's now python is 33682 21:17:35,960 --> 21:17:43,160 going to be our friend if I want to 33683 21:17:39,040 --> 21:17:45,320 access the HTTP parameters that the user 33684 21:17:43,160 --> 21:17:47,680 has provided via the URL be it Q equals 33685 21:17:45,320 --> 21:17:50,000 cats or name equals David I can use this 33686 21:17:47,680 --> 21:17:52,832 special variable I already preemptively 33687 21:17:50,000 --> 21:17:55,280 imported earlier and I can do this if 33688 21:17:52,832 --> 21:17:59,040 there is an HTTP parameter called name 33689 21:17:55,280 --> 21:18:00,600 in what I'm going to call request. args 33690 21:17:59,040 --> 21:18:02,760 then I'm going to go ahead and create a 33691 21:18:00,600 --> 21:18:05,360 variable called name and I'm going to 33692 21:18:02,760 --> 21:18:08,600 set it equal to request. args bracket 33693 21:18:05,360 --> 21:18:10,960 name else if there is no quote unquote 33694 21:18:08,600 --> 21:18:12,872 name key in this special variable called 33695 21:18:10,960 --> 21:18:15,400 request. args I'm going to just assume 33696 21:18:12,872 --> 21:18:17,040 that the user's name is World by default 33697 21:18:15,400 --> 21:18:18,832 now what's going on here well it turns 33698 21:18:17,040 --> 21:18:21,480 out that flask provides us with this 33699 21:18:18,832 --> 21:18:24,800 special variable called request. ARs and 33700 21:18:21,480 --> 21:18:27,640 in there is all of the key value pairs 33701 21:18:24,800 --> 21:18:31,080 that might have come in via the URL so 33702 21:18:27,640 --> 21:18:34,360 if you had to guess what type of data or 33703 21:18:31,080 --> 21:18:36,832 what data type is request. args that's 33704 21:18:34,360 --> 21:18:40,120 its name and here is in context line n 33705 21:18:36,832 --> 21:18:42,800 might provide a clue in Python what data 33706 21:18:40,120 --> 21:18:44,760 type might request. args 33707 21:18:42,800 --> 21:18:47,192 be 33708 21:18:44,760 --> 21:18:49,000 yeah uh it's not going to be an array or 33709 21:18:47,192 --> 21:18:50,760 a list because those are always in every 33710 21:18:49,000 --> 21:18:52,600 language we've seen numerically indexed 33711 21:18:50,760 --> 21:18:56,320 but you're 33712 21:18:52,600 --> 21:18:58,600 close someone else it's a dictionary so 33713 21:18:56,320 --> 21:19:00,680 a dictionary is similar syntactically to 33714 21:18:58,600 --> 21:19:03,400 a list in Python but instead of numeric 33715 21:19:00,680 --> 21:19:05,512 indices like 012 you can literally use 33716 21:19:03,400 --> 21:19:06,800 strings like quote unquote name now 33717 21:19:05,512 --> 21:19:08,480 that's a bit of a white lie it is a 33718 21:19:06,800 --> 21:19:10,192 dictionary but it's flask's special 33719 21:19:08,480 --> 21:19:12,192 fancy version of a dictionary but the 33720 21:19:10,192 --> 21:19:14,400 syntax via which you can access it is 33721 21:19:12,192 --> 21:19:16,512 exactly the same and I actually this is 33722 21:19:14,400 --> 21:19:18,800 a typo I didn't mean to say names there 33723 21:19:16,512 --> 21:19:21,000 I meant to say name singular but 33724 21:19:18,800 --> 21:19:23,232 otherwise I think the code is correct 33725 21:19:21,000 --> 21:19:25,360 this is going to on line eight check if 33726 21:19:23,232 --> 21:19:27,552 there's a key called name in request. 33727 21:19:25,360 --> 21:19:29,512 ARs and if so it's going to set it equal 33728 21:19:27,552 --> 21:19:32,400 to that value otherwise it's going to 33729 21:19:29,512 --> 21:19:35,680 default to world I deliberately did not 33730 21:19:32,400 --> 21:19:39,040 do this I added this IFL 33731 21:19:35,680 --> 21:19:43,232 and did not do this why what error might 33732 21:19:39,040 --> 21:19:43,232 happen if I just blindly grab 33733 21:19:44,872 --> 21:19:49,400 name exactly if there was nothing at the 33734 21:19:47,360 --> 21:19:51,800 end of the URL that was of the form 33735 21:19:49,400 --> 21:19:54,440 question mark name equals someone then 33736 21:19:51,800 --> 21:19:55,920 there would be no name key and this is 33737 21:19:54,440 --> 21:19:57,440 uh you know a couple weeks back but this 33738 21:19:55,920 --> 21:19:58,960 would give you one of those annoying key 33739 21:19:57,440 --> 21:20:00,512 errors when you get a trace back because 33740 21:19:58,960 --> 21:20:02,192 you screwed up because you used a string 33741 21:20:00,512 --> 21:20:03,760 that doesn't exist that's why I'm just 33742 21:20:02,192 --> 21:20:05,120 proactively trying to avoid that 33743 21:20:03,760 --> 21:20:06,680 situation just like I'm might have a 33744 21:20:05,120 --> 21:20:09,080 couple of weeks ago so even though it's 33745 21:20:06,680 --> 21:20:11,720 more verbose this is just much more 33746 21:20:09,080 --> 21:20:13,440 defensive so that I don't accidentally 33747 21:20:11,720 --> 21:20:15,080 index into a dictionary where there is 33748 21:20:13,440 --> 21:20:17,360 no key but we'll see how we can tighten 33749 21:20:15,080 --> 21:20:19,920 this up to be not four lines but one but 33750 21:20:17,360 --> 21:20:23,760 I think now I can do this wouldn't it be 33751 21:20:19,920 --> 21:20:25,920 nice if now in my index.html file which 33752 21:20:23,760 --> 21:20:27,680 recall is in my templates folder 33753 21:20:25,920 --> 21:20:30,040 wouldn't it be nice if I could do the 33754 21:20:27,680 --> 21:20:33,232 equivalent in C of like a percent s here 33755 21:20:30,040 --> 21:20:35,832 for instance or in Python something like 33756 21:20:33,232 --> 21:20:37,640 this name well it's close and this is 33757 21:20:35,832 --> 21:20:39,832 just because different humans invent 33758 21:20:37,640 --> 21:20:43,552 different languages invent different uh 33759 21:20:39,832 --> 21:20:46,000 Frameworks the Syntax for this in flask 33760 21:20:43,552 --> 21:20:49,480 is to actually do whoops two curly 33761 21:20:46,000 --> 21:20:52,720 braces and then name of the variable 33762 21:20:49,480 --> 21:20:54,320 inside of it why it's just probably 33763 21:20:52,720 --> 21:20:55,680 someone figured what are the odds that a 33764 21:20:54,320 --> 21:20:58,192 normal person is ever going to use two 33765 21:20:55,680 --> 21:20:59,280 curly braces at once versus just one so 33766 21:20:58,192 --> 21:21:00,720 this is probably decreasing the 33767 21:20:59,280 --> 21:21:03,600 probability that people actually want to 33768 21:21:00,720 --> 21:21:05,400 Output literal curly braces like this so 33769 21:21:03,600 --> 21:21:07,000 it's similar in spirit to Python's F 33770 21:21:05,400 --> 21:21:08,600 strings it's similar in spirit to C's 33771 21:21:07,000 --> 21:21:11,192 percent s it's similar in spirit to 33772 21:21:08,600 --> 21:21:13,400 sql's question marks same idea slightly 33773 21:21:11,192 --> 21:21:15,800 different syntax and this there is 33774 21:21:13,400 --> 21:21:18,040 ginger so it's not programming code per 33775 21:21:15,800 --> 21:21:19,480 se it's just a template and indeed 33776 21:21:18,040 --> 21:21:21,480 that's why this folder is called 33777 21:21:19,480 --> 21:21:23,552 templates it is sort of like a a 33778 21:21:21,480 --> 21:21:24,960 blueprint for what I want to be spit out 33779 21:21:23,552 --> 21:21:26,832 to the user but I've got these 33780 21:21:24,960 --> 21:21:29,480 placeholders like this variable that I 33781 21:21:26,832 --> 21:21:32,360 want to plug into that value now this 33782 21:21:29,480 --> 21:21:35,120 alone is not enough watch what happens 33783 21:21:32,360 --> 21:21:37,600 if I go back to my other browser and I 33784 21:21:35,120 --> 21:21:40,320 reload the page after changing up here 33785 21:21:37,600 --> 21:21:44,232 let's do name equals David again enter 33786 21:21:40,320 --> 21:21:46,680 nothing outputs after the hello comma so 33787 21:21:44,232 --> 21:21:49,280 it seems that the name variable doesn't 33788 21:21:46,680 --> 21:21:50,832 exist yet and that's why indeed if I do 33789 21:21:49,280 --> 21:21:52,832 view page Source you can see what was 33790 21:21:50,832 --> 21:21:54,720 sent to the browser something's wrong 33791 21:21:52,832 --> 21:21:56,760 with my placeholder but I just need to 33792 21:21:54,720 --> 21:21:59,160 be a little more explicit as to what I 33793 21:21:56,760 --> 21:22:01,800 want to send where so it turns out that 33794 21:21:59,160 --> 21:22:03,160 the render template function takes not 33795 21:22:01,800 --> 21:22:04,800 just one argument the name of the 33796 21:22:03,160 --> 21:22:07,920 template you want to spit out but it 33797 21:22:04,800 --> 21:22:10,440 takes after that with commas all of the 33798 21:22:07,920 --> 21:22:13,232 placeholders you want to plug in so for 33799 21:22:10,440 --> 21:22:15,800 instance if you want the placeholder to 33800 21:22:13,232 --> 21:22:18,192 be this literally placeholder inside of 33801 21:22:15,800 --> 21:22:20,080 those curly braces you can then specify 33802 21:22:18,192 --> 21:22:23,920 as the second argument to rep uh render 33803 21:22:20,080 --> 21:22:27,600 template a placeholder named argument 33804 21:22:23,920 --> 21:22:29,600 equals whatever the name is so name is 33805 21:22:27,600 --> 21:22:31,720 the variable in the lines above 33806 21:22:29,600 --> 21:22:33,832 placeholder is the name of my literal 33807 21:22:31,720 --> 21:22:36,160 placeholder in the curly braces and so 33808 21:22:33,832 --> 21:22:38,360 now if I go back to my browser and 33809 21:22:36,160 --> 21:22:39,832 reload this with still quote unquote 33810 21:22:38,360 --> 21:22:43,832 with still question mark name equals 33811 21:22:39,832 --> 21:22:46,040 David in the URL now I indeed see hello 33812 21:22:43,832 --> 21:22:47,720 comma David and if I zoom in here and 33813 21:22:46,040 --> 21:22:50,552 let me move over here let me type in 33814 21:22:47,720 --> 21:22:53,120 Carter and hit enter now I see Hello 33815 21:22:50,552 --> 21:22:54,832 Carter instead now this is a little 33816 21:22:53,120 --> 21:22:56,552 unnecessary to explicitly call the 33817 21:22:54,832 --> 21:22:58,320 placeholder placeholder especially if 33818 21:22:56,552 --> 21:22:59,720 you want to have two or three of them so 33819 21:22:58,320 --> 21:23:01,160 you can actually call this anything you 33820 21:22:59,720 --> 21:23:02,400 want and I'm going to change it back to 33821 21:23:01,160 --> 21:23:04,600 name which is a little more 33822 21:23:02,400 --> 21:23:06,640 straightforward the only weird thing 33823 21:23:04,600 --> 21:23:08,192 here is that now you'll see that you're 33824 21:23:06,640 --> 21:23:10,280 writing code like this and this is 33825 21:23:08,192 --> 21:23:12,600 correct and this is the norm it just 33826 21:23:10,280 --> 21:23:15,040 looks weird but the thing on the left of 33827 21:23:12,600 --> 21:23:17,040 the equal sign is the placeholder you're 33828 21:23:15,040 --> 21:23:19,040 using in the template the thing on the 33829 21:23:17,040 --> 21:23:21,480 right can be any value you want 33830 21:23:19,040 --> 21:23:23,232 including a variable so even though I'm 33831 21:23:21,480 --> 21:23:24,800 naming them exactly the same which looks 33832 21:23:23,232 --> 21:23:27,680 stupid admittedly like this is what 33833 21:23:24,800 --> 21:23:29,440 people tend to do just because it's uh 33834 21:23:27,680 --> 21:23:33,280 simpler than introducing another word 33835 21:23:29,440 --> 21:23:34,920 like placeholder any questions now on 33836 21:23:33,280 --> 21:23:36,800 this 33837 21:23:34,920 --> 21:23:38,832 any questions on these 33838 21:23:36,800 --> 21:23:40,872 placeholders no all right well let's 33839 21:23:38,832 --> 21:23:43,040 tighten this up a little bit and see if 33840 21:23:40,872 --> 21:23:46,800 we can't get things to be more Dynamic 33841 21:23:43,040 --> 21:23:48,920 still let me propose now that instead of 33842 21:23:46,800 --> 21:23:50,920 outputting instead of using this 33843 21:23:48,920 --> 21:23:52,552 condition which made a very simple idea 33844 21:23:50,920 --> 21:23:54,512 like very verbose with four different 33845 21:23:52,552 --> 21:23:56,920 lines it turns out there's an easier way 33846 21:23:54,512 --> 21:23:58,960 to do this you can actually still create 33847 21:23:56,920 --> 21:24:02,080 a variable called name and you can set 33848 21:23:58,960 --> 21:24:04,872 it equal to request. args but instead of 33849 21:24:02,080 --> 21:24:08,360 just blindly indexing into to that 33850 21:24:04,872 --> 21:24:11,440 dictionary it turns out that request. 33851 21:24:08,360 --> 21:24:13,720 ARS comes with a function as well called 33852 21:24:11,440 --> 21:24:17,080 get you can pass it an argument that 33853 21:24:13,720 --> 21:24:20,552 tells you what value you want to get and 33854 21:24:17,080 --> 21:24:22,960 by default if there is no key called 33855 21:24:20,552 --> 21:24:24,512 name in that dictionary this function 33856 21:24:22,960 --> 21:24:27,120 will not throw a key error it's just 33857 21:24:24,512 --> 21:24:29,400 going to return none an O the special V 33858 21:24:27,120 --> 21:24:31,120 the Special Value in Python so it avoids 33859 21:24:29,400 --> 21:24:33,160 a bug in your code but it tightens up 33860 21:24:31,120 --> 21:24:34,720 four lines into one but even nicer if 33861 21:24:33,160 --> 21:24:36,512 you read the documentation 33862 21:24:34,720 --> 21:24:38,600 the get function can also take a 33863 21:24:36,512 --> 21:24:40,760 explicit default value so if you don't 33864 21:24:38,600 --> 21:24:42,360 want none to be on the screen like hello 33865 21:24:40,760 --> 21:24:44,872 comma blank or I mean that would be 33866 21:24:42,360 --> 21:24:46,640 weird too you can just put in a default 33867 21:24:44,872 --> 21:24:49,000 value per the documentation of this 33868 21:24:46,640 --> 21:24:51,552 function like world so now we've gone 33869 21:24:49,000 --> 21:24:53,120 from four lines to just one so arguably 33870 21:24:51,552 --> 21:24:55,280 it's better designed and if I go back to 33871 21:24:53,120 --> 21:24:58,280 the browser now still with Carter in the 33872 21:24:55,280 --> 21:25:02,480 URL and hit reload same thing happens 33873 21:24:58,280 --> 21:25:04,552 but we notice this suppose I uh get rid 33874 21:25:02,480 --> 21:25:07,120 of the name parameter altogether and hit 33875 21:25:04,552 --> 21:25:08,680 enter now it goes to the default instead 33876 21:25:07,120 --> 21:25:10,720 world so it's just a little better a 33877 21:25:08,680 --> 21:25:13,232 little better designed than doing it the 33878 21:25:10,720 --> 21:25:15,680 other way instead all right how about we 33879 21:25:13,232 --> 21:25:18,680 take things up one more Notch and how 33880 21:25:15,680 --> 21:25:20,872 about we introduce multiple routes and 33881 21:25:18,680 --> 21:25:23,480 actually introduce perhaps a form to the 33882 21:25:20,872 --> 21:25:25,640 mix because again no normal person is 33883 21:25:23,480 --> 21:25:27,400 going to like visit a URL and add a 33884 21:25:25,640 --> 21:25:29,552 slash and a question mark and their name 33885 21:25:27,400 --> 21:25:30,960 like that's not how browsers work uh 33886 21:25:29,552 --> 21:25:32,920 well that's how browsers work that's not 33887 21:25:30,960 --> 21:25:35,480 how humans interact with browsers you 33888 21:25:32,920 --> 21:25:36,680 and I use a form to quickly instead so 33889 21:25:35,480 --> 21:25:38,720 now things can get a little more 33890 21:25:36,680 --> 21:25:40,920 interesting when making our own web 33891 21:25:38,720 --> 21:25:43,320 application cuz maybe we could do 33892 21:25:40,920 --> 21:25:46,640 something like this let me go and zoom 33893 21:25:43,320 --> 21:25:48,552 out again let me go back to my code here 33894 21:25:46,640 --> 21:25:51,760 and let me move this around and focus 33895 21:25:48,552 --> 21:25:53,872 now on the index.html file instead of 33896 21:25:51,760 --> 21:25:55,760 just this placeholder why don't we go 33897 21:25:53,872 --> 21:25:57,480 ahead and give ourselves a form like 33898 21:25:55,760 --> 21:25:59,872 we've played with a little bit in the 33899 21:25:57,480 --> 21:26:03,120 past be it for Google or something else 33900 21:25:59,872 --> 21:26:05,512 and let's do this uh form and inside of 33901 21:26:03,120 --> 21:26:07,552 this form let's have an input and the 33902 21:26:05,512 --> 21:26:10,512 name of this input will be quote unquote 33903 21:26:07,552 --> 21:26:12,640 name so that too is confusing but inputs 33904 21:26:10,512 --> 21:26:14,640 have name attributes but this is a 33905 21:26:12,640 --> 21:26:17,280 person's name so I'm saying name equals 33906 21:26:14,640 --> 21:26:19,440 name here so just a messy world of 33907 21:26:17,280 --> 21:26:22,360 semantics and let me go ahead and make 33908 21:26:19,440 --> 21:26:24,512 this a text box by default and then let 33909 21:26:22,360 --> 21:26:27,400 me give myself a button whose default 33910 21:26:24,512 --> 21:26:29,872 type will be submit and the name of this 33911 21:26:27,400 --> 21:26:31,280 button will be greet for instance so 33912 21:26:29,872 --> 21:26:33,120 let's see what happens here but let me 33913 21:26:31,280 --> 21:26:34,552 change app.py to just be the original 33914 21:26:33,120 --> 21:26:36,360 simpler I'm not passing in any 33915 21:26:34,552 --> 21:26:37,760 placeholders now and I'm going to even 33916 21:26:36,360 --> 21:26:39,080 get rid of this I'm just going to rewind 33917 21:26:37,760 --> 21:26:42,232 to the first version of this for 33918 21:26:39,080 --> 21:26:44,192 Simplicity let's now change the url to 33919 21:26:42,232 --> 21:26:46,600 get rid of Carter and myself so we just 33920 21:26:44,192 --> 21:26:49,160 go to slash and hit enter and now we 33921 21:26:46,600 --> 21:26:50,872 have a super simple form again all right 33922 21:26:49,160 --> 21:26:52,480 this is not super userfriendly but 33923 21:26:50,872 --> 21:26:54,800 there's some nice enhancements we can 33924 21:26:52,480 --> 21:26:56,600 make for instance like we can uh for 33925 21:26:54,800 --> 21:26:58,120 instance turn off autocomplete 33926 21:26:56,600 --> 21:26:59,360 especially if I want to type David and 33927 21:26:58,120 --> 21:27:01,440 Carter manually and I don't want it 33928 21:26:59,360 --> 21:27:03,600 finishing my thought during class uh we 33929 21:27:01,440 --> 21:27:05,192 can do autofocus which puts the cursor 33930 21:27:03,600 --> 21:27:06,552 there BL linking by default which is 33931 21:27:05,192 --> 21:27:08,192 nice cuz then the human doesn't have to 33932 21:27:06,552 --> 21:27:10,872 deal with that um and then we can even 33933 21:27:08,192 --> 21:27:12,760 have a placeholder attribute placeholder 33934 21:27:10,872 --> 21:27:14,440 equals name so that it's like built in 33935 21:27:12,760 --> 21:27:16,800 instructions for this thing and so now 33936 21:27:14,440 --> 21:27:18,160 if I go back to the other tab nothing's 33937 21:27:16,800 --> 21:27:20,832 changed yet because I have to download 33938 21:27:18,160 --> 21:27:22,360 the HTML again reload okay now it's a 33939 21:27:20,832 --> 21:27:24,720 little more user friendly it says name 33940 21:27:22,360 --> 21:27:26,552 and light gray the cursor is blinking 33941 21:27:24,720 --> 21:27:29,120 and I'm sort of ready to go but this 33942 21:27:26,552 --> 21:27:31,640 form hasn't been wired up to go anywhere 33943 21:27:29,120 --> 21:27:33,640 yet and so let's do this let's for 33944 21:27:31,640 --> 21:27:34,760 instance say that the action of this 33945 21:27:33,640 --> 21:27:35,760 form 33946 21:27:34,760 --> 21:27:37,552 is not going to be something like 33947 21:27:35,760 --> 21:27:40,080 google.com which we did last time with 33948 21:27:37,552 --> 21:27:42,800 for cats I am now going to be both the 33949 21:27:40,080 --> 21:27:44,360 front end and the back end of this 33950 21:27:42,800 --> 21:27:46,720 website the front end is what the human 33951 21:27:44,360 --> 21:27:48,280 sees the web page the graphics the forms 33952 21:27:46,720 --> 21:27:49,960 the back end is the stuff the human 33953 21:27:48,280 --> 21:27:53,000 typically doesn't see the python code 33954 21:27:49,960 --> 21:27:55,120 the SQL code the server itself but now 33955 21:27:53,000 --> 21:27:58,232 I'm in control of both sides of the 33956 21:27:55,120 --> 21:28:00,480 experience the HTML and also the routes 33957 21:27:58,232 --> 21:28:02,640 so let's just propose that we invent our 33958 21:28:00,480 --> 21:28:04,680 own route and instead of calling it SL 33959 21:28:02,640 --> 21:28:07,440 search like Google does let's call it 33960 21:28:04,680 --> 21:28:09,192 SLG greet and let me specify that the 33961 21:28:07,440 --> 21:28:11,480 method this form will use which is 33962 21:28:09,192 --> 21:28:13,680 technically the default will be get and 33963 21:28:11,480 --> 21:28:15,320 confusingly it is lowercase get even 33964 21:28:13,680 --> 21:28:17,360 though in the envelope we keep talking 33965 21:28:15,320 --> 21:28:18,960 about virtually it's actually capitals 33966 21:28:17,360 --> 21:28:21,280 again left hand wasn't talking to right 33967 21:28:18,960 --> 21:28:23,600 hand when these things were decided all 33968 21:28:21,280 --> 21:28:26,120 right so all I've done is create a web 33969 21:28:23,600 --> 21:28:29,040 form that's going to submit whatever the 33970 21:28:26,120 --> 21:28:31,360 text box value is to a route called SLG 33971 21:28:29,040 --> 21:28:35,120 greet by default because there's no HTTP 33972 21:28:31,360 --> 21:28:36,400 or htps or no domain name SLG greet is 33973 21:28:35,120 --> 21:28:38,920 going to be assumed to be not at 33974 21:28:36,400 --> 21:28:42,160 google.com but whatever my own serers 33975 21:28:38,920 --> 21:28:44,440 URL is so whatever my code spaces URL is 33976 21:28:42,160 --> 21:28:47,320 that's going to be the implicit prefix 33977 21:28:44,440 --> 21:28:51,400 this SLG greet is just the route so now 33978 21:28:47,320 --> 21:28:54,192 let's go back to VSS codes app.py file 33979 21:28:51,400 --> 21:28:56,400 how do I now Stitch this together well I 33980 21:28:54,192 --> 21:28:58,480 think we're good to go with index.html 33981 21:28:56,400 --> 21:29:00,920 if index. html's purpose in life is just 33982 21:28:58,480 --> 21:29:03,192 to spit out this form we're done with 33983 21:29:00,920 --> 21:29:06,440 one of my routes but if I want to have a 33984 21:29:03,192 --> 21:29:09,680 second route greet that actually spits 33985 21:29:06,440 --> 21:29:11,512 out some greeting to the user well let's 33986 21:29:09,680 --> 21:29:14,040 prepare that template too let me go 33987 21:29:11,512 --> 21:29:16,872 ahead and highlight all of this HTML let 33988 21:29:14,040 --> 21:29:20,440 me go back into my terminal window and 33989 21:29:16,872 --> 21:29:22,552 into my hello directory and then into my 33990 21:29:20,440 --> 21:29:25,800 templates directory and let me create 33991 21:29:22,552 --> 21:29:27,360 another template called greet HTML whose 33992 21:29:25,800 --> 21:29:29,832 purpose in life will not be to show a 33993 21:29:27,360 --> 21:29:32,280 form but to greet the user with hello so 33994 21:29:29,832 --> 21:29:34,320 and so so in this file I'm going to 33995 21:29:32,280 --> 21:29:36,160 paste all that same HTML but I'm going 33996 21:29:34,320 --> 21:29:38,232 to get rid of the form and essentially 33997 21:29:36,160 --> 21:29:41,192 revert to our previous version hello 33998 21:29:38,232 --> 21:29:44,512 comma and then using the ginger syntax 33999 21:29:41,192 --> 21:29:46,640 name so one template index.html is for 34000 21:29:44,512 --> 21:29:49,080 the form the second template now is for 34001 21:29:46,640 --> 21:29:51,232 the greeting of hello comma so and so 34002 21:29:49,080 --> 21:29:54,480 but otherwise these files notice are 34003 21:29:51,232 --> 21:29:56,600 almost the same except one has the form 34004 21:29:54,480 --> 21:29:58,720 one has just the hello so now let's 34005 21:29:56,600 --> 21:30:00,440 finish this up in app doop High let me 34006 21:29:58,720 --> 21:30:03,720 go down here after a couple of blank 34007 21:30:00,440 --> 21:30:06,600 lines stylistically let me do app. route 34008 21:30:03,720 --> 21:30:07,960 quote unquote SLG greet but I could call 34009 21:30:06,600 --> 21:30:10,192 this route anything I want I'm just 34010 21:30:07,960 --> 21:30:11,720 using a a reasonable verb then let's 34011 21:30:10,192 --> 21:30:13,920 define another function I could call the 34012 21:30:11,720 --> 21:30:16,120 function anything I want X Y or Z I'm 34013 21:30:13,920 --> 21:30:18,512 going to call it more reasonably greet 34014 21:30:16,120 --> 21:30:20,080 no arguments and then now is the code 34015 21:30:18,512 --> 21:30:25,160 where I want to render the template so I 34016 21:30:20,080 --> 21:30:27,480 do return render template greet HTML but 34017 21:30:25,160 --> 21:30:29,872 but I need to do one more 34018 21:30:27,480 --> 21:30:31,800 thing what else do I want to do if I 34019 21:30:29,872 --> 21:30:35,080 want greet HTML to have access to the 34020 21:30:31,800 --> 21:30:37,440 human's name just to 34021 21:30:35,080 --> 21:30:41,000 recap I think we solved this already but 34022 21:30:37,440 --> 21:30:42,800 I deleted it but what do I have to add 34023 21:30:41,000 --> 21:30:45,232 back 34024 21:30:42,800 --> 21:30:47,400 yeahh yeah so I got to pass in the 34025 21:30:45,232 --> 21:30:48,920 placeholder somehow so I can do this a 34026 21:30:47,400 --> 21:30:50,440 couple of different ways I I'm going to 34027 21:30:48,920 --> 21:30:53,192 keep it a little more elegant this time 34028 21:30:50,440 --> 21:30:55,040 I'm just going to put my name uh 34029 21:30:53,192 --> 21:30:58,832 argument there and I'm going to set it 34030 21:30:55,040 --> 21:31:01,160 equal to request. ar. get quote unquote 34031 21:30:58,832 --> 21:31:02,920 name comma world before I used a 34032 21:31:01,160 --> 21:31:04,400 separate variable but I only used it in 34033 21:31:02,920 --> 21:31:06,440 one place so that's not strictly 34034 21:31:04,400 --> 21:31:08,552 necessary so this is fine too but if 34035 21:31:06,440 --> 21:31:10,680 this gets a little overwhelming notice 34036 21:31:08,552 --> 21:31:13,192 that I can alternatively do this I can 34037 21:31:10,680 --> 21:31:16,400 create an actual variable called name 34038 21:31:13,192 --> 21:31:18,760 and then I can pass in an argument 34039 21:31:16,400 --> 21:31:21,080 called name with a value that is that 34040 21:31:18,760 --> 21:31:22,680 variable but again what's really the 34041 21:31:21,080 --> 21:31:24,512 point here it was kind of prettier all 34042 21:31:22,680 --> 21:31:25,800 on one line so these are the exact same 34043 21:31:24,512 --> 21:31:28,552 things I'm just trying to tighten things 34044 21:31:25,800 --> 21:31:30,832 up further here all right so what just 34045 21:31:28,552 --> 21:31:33,280 happened if I go back to my form this is 34046 21:31:30,832 --> 21:31:35,960 still index.html if I reload it nothing 34047 21:31:33,280 --> 21:31:38,680 has changed if I type in my name to this 34048 21:31:35,960 --> 21:31:40,760 form notice again the URL I'm currently 34049 21:31:38,680 --> 21:31:42,720 at this is Chrome hiding things it's 34050 21:31:40,760 --> 21:31:44,320 technically slash by default even though 34051 21:31:42,720 --> 21:31:47,040 many browsers are just hiding 34052 21:31:44,320 --> 21:31:49,440 unnecessary uh characters these days but 34053 21:31:47,040 --> 21:31:52,760 Watch What Happens now if I scroll over 34054 21:31:49,440 --> 21:31:57,192 here and I click greet on this new 34055 21:31:52,760 --> 21:32:00,600 form notice my URL my route changed to 34056 21:31:57,192 --> 21:32:02,280 SL greet question mark name equals David 34057 21:32:00,600 --> 21:32:04,400 and the body of the page at top left 34058 21:32:02,280 --> 21:32:06,000 says hello comma David so this is 34059 21:32:04,400 --> 21:32:07,960 exactly how google.com works and it's 34060 21:32:06,000 --> 21:32:10,832 how we implemented search. HTML last 34061 21:32:07,960 --> 21:32:13,872 time but instead of submitting the form 34062 21:32:10,832 --> 21:32:16,040 to Google via the form I'm submitting it 34063 21:32:13,872 --> 21:32:18,720 to myself my very own route so I'm 34064 21:32:16,040 --> 21:32:21,960 implementing my own backend for this 34065 21:32:18,720 --> 21:32:24,160 same front end all right any questions 34066 21:32:21,960 --> 21:32:26,400 just yet much less interesting than 34067 21:32:24,160 --> 21:32:29,400 Google certainly but we kind of have all 34068 21:32:26,400 --> 21:32:31,920 of the wiring 34069 21:32:29,400 --> 21:32:34,400 now any 34070 21:32:31,920 --> 21:32:37,920 questions no 34071 21:32:34,400 --> 21:32:39,872 all right so what can we do to further 34072 21:32:37,920 --> 21:32:42,760 uh tighten this up and adhere to some 34073 21:32:39,872 --> 21:32:45,440 conventions well let me propose that in 34074 21:32:42,760 --> 21:32:46,832 this version we solve one problem and 34075 21:32:45,440 --> 21:32:48,320 even if you've never done this sort of 34076 21:32:46,832 --> 21:32:50,440 thing before I dare say we have enough 34077 21:32:48,320 --> 21:32:54,680 weeks of cs-50 where if I show you 34078 21:32:50,440 --> 21:32:58,360 index.html again and greet HTML again 34079 21:32:54,680 --> 21:33:00,320 odds are to someone's mind there's a 34080 21:32:58,360 --> 21:33:03,000 opportunity for 34081 21:33:00,320 --> 21:33:05,400 improvement why is this web app super 34082 21:33:03,000 --> 21:33:07,960 simple though it is arguably poorly 34083 21:33:05,400 --> 21:33:10,872 designed at the 34084 21:33:07,960 --> 21:33:13,280 moment and the answer lies somewhere in 34085 21:33:10,872 --> 21:33:15,960 these two templates index.html and 34086 21:33:13,280 --> 21:33:15,960 greet.tolowercase 34087 21:33:33,512 --> 21:33:37,832 probably did necessarily for your 34088 21:33:35,720 --> 21:33:40,232 homepage why because when you have HTML 34089 21:33:37,832 --> 21:33:42,600 only maybe CSS and even JavaScript 34090 21:33:40,232 --> 21:33:44,480 that's all you can do is copy paste copy 34091 21:33:42,600 --> 21:33:45,872 paste and just make sure that you have 34092 21:33:44,480 --> 21:33:47,920 the same structure maybe you have the 34093 21:33:45,872 --> 21:33:49,512 same CSS file the same Javascript file 34094 21:33:47,920 --> 21:33:51,280 the same third party libraries but it 34095 21:33:49,512 --> 21:33:53,040 makes it very very annoying as you might 34096 21:33:51,280 --> 21:33:55,040 have realized already to just m make a 34097 21:33:53,040 --> 21:33:56,832 change that affects everything so 34098 21:33:55,040 --> 21:34:00,000 wouldn't it be nice to like factor out 34099 21:33:56,832 --> 21:34:02,232 all of this and all of this and just let 34100 21:34:00,000 --> 21:34:04,400 the body change so here too is something 34101 21:34:02,232 --> 21:34:07,000 that flask and really other equivalent 34102 21:34:04,400 --> 21:34:08,120 Frameworks let us do it allows us to 34103 21:34:07,000 --> 21:34:10,720 create what we're going to call 34104 21:34:08,120 --> 21:34:11,920 conventionally a layout instead so I'm 34105 21:34:10,720 --> 21:34:14,600 going to go ahead and do this I'm going 34106 21:34:11,920 --> 21:34:17,600 to copy one last time all of the same 34107 21:34:14,600 --> 21:34:19,360 HTML I'm going to go into uh my terminal 34108 21:34:17,600 --> 21:34:22,640 window and I'm going to create by 34109 21:34:19,360 --> 21:34:24,600 convention a file called layout. HTML 34110 21:34:22,640 --> 21:34:27,232 this is truly going to be a blueprint of 34111 21:34:24,600 --> 21:34:29,232 sorts and in layout. HTML I'm going to 34112 21:34:27,232 --> 21:34:32,512 paste all of that same code but I'm 34113 21:34:29,232 --> 21:34:34,800 going to use now some ginger syntax to 34114 21:34:32,512 --> 21:34:37,760 indicate that I don't want to plug in 34115 21:34:34,800 --> 21:34:39,960 just a variable like name here I want to 34116 21:34:37,760 --> 21:34:42,192 actually plug the contents of a whole 34117 21:34:39,960 --> 21:34:44,760 other file so instead of just using 34118 21:34:42,192 --> 21:34:46,440 curly braces two of them left and right 34119 21:34:44,760 --> 21:34:49,160 I have to use slightly different syntax 34120 21:34:46,440 --> 21:34:51,480 to say I want a whole block of HTML here 34121 21:34:49,160 --> 21:34:53,280 from some other file and the way to do 34122 21:34:51,480 --> 21:34:56,280 this even though the syntax is a little 34123 21:34:53,280 --> 21:34:59,720 non-obvious is you use open curly brace 34124 21:34:56,280 --> 21:35:01,512 percent sign block then you can call the 34125 21:34:59,720 --> 21:35:03,440 next word anything you want it just has 34126 21:35:01,512 --> 21:35:06,280 to be a special type of placeholder for 34127 21:35:03,440 --> 21:35:07,960 an actual file not for just a variable 34128 21:35:06,280 --> 21:35:09,552 I'm going to call it body only because 34129 21:35:07,960 --> 21:35:11,720 I'm in the body so I'm want a 34130 21:35:09,552 --> 21:35:13,832 placeholder to be the entire body and 34131 21:35:11,720 --> 21:35:17,320 then outside of this you then say in one 34132 21:35:13,832 --> 21:35:19,040 word no space end block so it looks kind 34133 21:35:17,320 --> 21:35:20,760 of stupid honestly and why do we have 34134 21:35:19,040 --> 21:35:22,512 yet more ugly syntax again just 34135 21:35:20,760 --> 21:35:24,040 different software developers in the 34136 21:35:22,512 --> 21:35:25,832 world are all choosing their own Syntax 34137 21:35:24,040 --> 21:35:27,000 for their own libraries so they all kind 34138 21:35:25,832 --> 21:35:28,720 of look different but are all kind of 34139 21:35:27,000 --> 21:35:31,440 similar in spirit and you just get used 34140 21:35:28,720 --> 21:35:34,040 to seeing the different syntax this now 34141 21:35:31,440 --> 21:35:36,440 is not nearly as pretty as the pair of 34142 21:35:34,040 --> 21:35:38,320 curly braces for variables but this is 34143 21:35:36,440 --> 21:35:41,160 how I can say plug the contents of an 34144 21:35:38,320 --> 21:35:45,832 entire file Here and Now what does this 34145 21:35:41,160 --> 21:35:48,320 let me do I can now go back into my 34146 21:35:45,832 --> 21:35:50,120 index.html file which at the moment 34147 21:35:48,320 --> 21:35:52,552 still looks like this but almost all of 34148 21:35:50,120 --> 21:35:55,232 this is copy paste the only lines that 34149 21:35:52,552 --> 21:35:57,920 are interesting and different are these 34150 21:35:55,232 --> 21:35:59,600 four lines here in the body so what I 34151 21:35:57,920 --> 21:36:01,232 can actually do now is I'm going to 34152 21:35:59,600 --> 21:36:02,320 highlight that and cut it and then I'm 34153 21:36:01,232 --> 21:36:04,360 going to highlight everything else and 34154 21:36:02,320 --> 21:36:06,400 just delete the entire file and I'm 34155 21:36:04,360 --> 21:36:10,680 going to use some of that same syntax 34156 21:36:06,400 --> 21:36:14,440 and say curly brace percent sign extends 34157 21:36:10,680 --> 21:36:16,440 quote unquote layout. HTML and then I 34158 21:36:14,440 --> 21:36:19,160 close my thought with a percent sign and 34159 21:36:16,440 --> 21:36:21,680 close curly brace so this syntax as you 34160 21:36:19,160 --> 21:36:25,040 might just be inferring is now saying 34161 21:36:21,680 --> 21:36:27,280 please extend whatever layout. HTML 34162 21:36:25,040 --> 21:36:28,920 looks like that's the original blueprint 34163 21:36:27,280 --> 21:36:31,600 the mold out of which I want to make 34164 21:36:28,920 --> 21:36:33,512 this web page and now here the syntax is 34165 21:36:31,600 --> 21:36:36,320 a little weird too but similar at least 34166 21:36:33,512 --> 21:36:38,832 from before I can now say the block the 34167 21:36:36,320 --> 21:36:42,600 body block that I want you to plug into 34168 21:36:38,832 --> 21:36:45,000 that layout is going to be everything 34169 21:36:42,600 --> 21:36:48,232 between these two tags which we already 34170 21:36:45,000 --> 21:36:50,720 saw earlier but in layout. HTML they're 34171 21:36:48,232 --> 21:36:52,600 sort of giving a placeholder in 34172 21:36:50,720 --> 21:36:54,832 index.html this is what I'm going to 34173 21:36:52,600 --> 21:36:56,232 plug in to those other placeholders as 34174 21:36:54,832 --> 21:36:58,080 well so I'm just going to give myself 34175 21:36:56,232 --> 21:37:00,360 some extra white space I'm going to 34176 21:36:58,080 --> 21:37:01,920 paste the HTML that was there if I want 34177 21:37:00,360 --> 21:37:03,320 to make clear what's going on I can 34178 21:37:01,920 --> 21:37:05,040 indent it although this has no no 34179 21:37:03,320 --> 21:37:06,872 functional impact but it just makes 34180 21:37:05,040 --> 21:37:09,512 clear that just like in HTML you can 34181 21:37:06,872 --> 21:37:12,872 open a ginger tag and close it but in 34182 21:37:09,512 --> 21:37:15,232 ginger here we have this here hey uh Hey 34183 21:37:12,872 --> 21:37:17,280 python here comes the body of this page 34184 21:37:15,232 --> 21:37:19,280 hey python that's it for the body of 34185 21:37:17,280 --> 21:37:23,512 this page and all of this stuff should 34186 21:37:19,280 --> 21:37:26,480 be plugged into this main parent layout 34187 21:37:23,512 --> 21:37:28,120 if you will so super ugly admittedly but 34188 21:37:26,480 --> 21:37:29,440 now at least things get way less 34189 21:37:28,120 --> 21:37:32,480 redundant because I'm going to do the 34190 21:37:29,440 --> 21:37:34,400 exact same thing over here in greet HTML 34191 21:37:32,480 --> 21:37:39,160 it looks like this but now I'm going to 34192 21:37:34,400 --> 21:37:42,400 do this extends layout. HTML also just 34193 21:37:39,160 --> 21:37:43,960 as before uh the body the uh the body 34194 21:37:42,400 --> 21:37:47,160 that I want to plug in is going to be 34195 21:37:43,960 --> 21:37:48,832 everything inside of these tags here and 34196 21:37:47,160 --> 21:37:53,280 this body is just going to be hello 34197 21:37:48,832 --> 21:37:55,800 comma name in curly braces like that so 34198 21:37:53,280 --> 21:37:57,800 again ugly syntax got really ugly fast 34199 21:37:55,800 --> 21:37:59,232 but it's really just following these 34200 21:37:57,800 --> 21:38:01,192 patterns now and we have two types of 34201 21:37:59,232 --> 21:38:04,280 placeholders two curly braces for 34202 21:38:01,192 --> 21:38:06,040 variables and now this kind of syntax 34203 21:38:04,280 --> 21:38:08,720 with the percent signs and the single 34204 21:38:06,040 --> 21:38:12,232 curly braces for like contents of actual 34205 21:38:08,720 --> 21:38:13,920 files and so now in this world or in the 34206 21:38:12,232 --> 21:38:15,920 world of a homepage if you were using 34207 21:38:13,920 --> 21:38:18,192 flask and python to make your personal 34208 21:38:15,920 --> 21:38:19,960 homepage with all of those various Pages 34209 21:38:18,192 --> 21:38:21,872 you would probably design one main 34210 21:38:19,960 --> 21:38:23,760 layout with all of your pretty logos and 34211 21:38:21,872 --> 21:38:25,400 colors and fonts and like what you want 34212 21:38:23,760 --> 21:38:27,320 the site to look like and then each of 34213 21:38:25,400 --> 21:38:29,440 your smaller Pages would now be 34214 21:38:27,320 --> 21:38:31,232 distilled into just these smaller 34215 21:38:29,440 --> 21:38:33,600 fragments and whether you're using 34216 21:38:31,232 --> 21:38:35,360 python or Java or Javas script or other 34217 21:38:33,600 --> 21:38:37,280 languages too all different programming 34218 21:38:35,360 --> 21:38:40,040 languages have popular Frameworks that 34219 21:38:37,280 --> 21:38:42,160 do things like this the idea is the same 34220 21:38:40,040 --> 21:38:43,720 across all of them all right let's see 34221 21:38:42,160 --> 21:38:47,680 if it works let's go back into the 34222 21:38:43,720 --> 21:38:49,760 browser let me go back to my slash route 34223 21:38:47,680 --> 21:38:52,512 there's that same form let me type in 34224 21:38:49,760 --> 21:38:55,040 David and type and click greet and 34225 21:38:52,512 --> 21:38:57,000 indeed I see hello comma David I see 34226 21:38:55,040 --> 21:38:58,800 that greet was automatically added to 34227 21:38:57,000 --> 21:39:00,960 the URL by the browser when I submitted 34228 21:38:58,800 --> 21:39:02,960 the form followed by the key value Pairs 34229 21:39:00,960 --> 21:39:04,960 and if I view the page source as I did 34230 21:39:02,960 --> 21:39:08,120 earlier you'll see that you have the 34231 21:39:04,960 --> 21:39:10,192 entirety of that layout with hello David 34232 21:39:08,120 --> 21:39:13,320 plugged in meanwhile if I go back to the 34233 21:39:10,192 --> 21:39:15,552 form and view this page Source you'll 34234 21:39:13,320 --> 21:39:17,872 see the exact same layout but with the 34235 21:39:15,552 --> 21:39:20,360 form tag plugged in and here's where you 34236 21:39:17,872 --> 21:39:22,600 can be a little less uh nitpicky with 34237 21:39:20,360 --> 21:39:24,600 styling okay yes this isn't technically 34238 21:39:22,600 --> 21:39:26,360 indented inside of the body but it was 34239 21:39:24,600 --> 21:39:28,040 relative to the original file so at this 34240 21:39:26,360 --> 21:39:30,232 point in the game you don't need to 34241 21:39:28,040 --> 21:39:32,400 worry about your outputed HTML looking 34242 21:39:30,232 --> 21:39:34,760 super pretty you want your source code 34243 21:39:32,400 --> 21:39:37,040 that the see to be pretty not the 34244 21:39:34,760 --> 21:39:40,360 browser this is not a stylistic 34245 21:39:37,040 --> 21:39:42,920 concern okay questions on these 34246 21:39:40,360 --> 21:39:45,832 capabilities then a 34247 21:39:42,920 --> 21:39:48,920 flask or problems that we've just solved 34248 21:39:45,832 --> 21:39:48,920 and why 34249 21:39:51,600 --> 21:39:57,440 yeah uh okay so if the files in question 34250 21:39:54,800 --> 21:40:01,000 are in different folders for instance if 34251 21:39:57,440 --> 21:40:05,320 I go back into my uh index page which 34252 21:40:01,000 --> 21:40:08,360 has the form um the routes here are 34253 21:40:05,320 --> 21:40:11,000 entirely dependent on what is an app.py 34254 21:40:08,360 --> 21:40:12,960 there's no notion of a folder when it 34255 21:40:11,000 --> 21:40:15,320 comes to implementing a web application 34256 21:40:12,960 --> 21:40:17,320 anymore they are more generically routes 34257 21:40:15,320 --> 21:40:19,640 however and we've not done this yet you 34258 21:40:17,320 --> 21:40:21,552 can put your static content your images 34259 21:40:19,640 --> 21:40:23,232 your video files your CSS files in a 34260 21:40:21,552 --> 21:40:25,960 folder called Static and there can be 34261 21:40:23,232 --> 21:40:27,960 subfolders therein and that would affect 34262 21:40:25,960 --> 21:40:30,640 what you use as your Source attributes 34263 21:40:27,960 --> 21:40:33,920 for images or your Source uh tags for 34264 21:40:30,640 --> 21:40:35,232 video or any of those kinds of assets 34265 21:40:33,920 --> 21:40:37,680 and we'll see that eventually in the 34266 21:40:35,232 --> 21:40:39,640 home in the uh the problem set next 34267 21:40:37,680 --> 21:40:41,640 other questions on what we've just done 34268 21:40:39,640 --> 21:40:44,000 here 34269 21:40:41,640 --> 21:40:46,192 yeah good question how do I how did I 34270 21:40:44,000 --> 21:40:49,640 ensure that the web app starts on the 34271 21:40:46,192 --> 21:40:52,832 form and then goes to the hello page so 34272 21:40:49,640 --> 21:40:55,552 whatever you decide your default index 34273 21:40:52,832 --> 21:40:57,120 route is like the implicit slash that is 34274 21:40:55,552 --> 21:40:59,512 what is going to be pulled up when a 34275 21:40:57,120 --> 21:41:01,872 user visits the domain name where your 34276 21:40:59,512 --> 21:41:05,512 website is hosted so if I go back over 34277 21:41:01,872 --> 21:41:08,120 here to app Pi because my slash route is 34278 21:41:05,512 --> 21:41:11,680 designed to return index.html that's 34279 21:41:08,120 --> 21:41:14,360 exactly why that response came back good 34280 21:41:11,680 --> 21:41:16,232 question all right so beyond this let's 34281 21:41:14,360 --> 21:41:17,720 consider now issues of privacy and we'll 34282 21:41:16,232 --> 21:41:19,480 touch on this too as we get to issues 34283 21:41:17,720 --> 21:41:21,920 like shopping carts and cookies if I 34284 21:41:19,480 --> 21:41:23,960 zoom in on the URL here even though the 34285 21:41:21,920 --> 21:41:26,760 URL itself is a little long and ugly and 34286 21:41:23,960 --> 21:41:30,480 cryptic but when I type in my name and 34287 21:41:26,760 --> 21:41:33,360 hit uh greet of course we keep seeing 34288 21:41:30,480 --> 21:41:35,960 name equals David in the URL in what 34289 21:41:33,360 --> 21:41:39,600 sense might this be bad 34290 21:41:35,960 --> 21:41:41,680 design or in what kinds of web apps 34291 21:41:39,600 --> 21:41:43,280 might you not want the name to show up 34292 21:41:41,680 --> 21:41:46,232 in the URL like that because this is 34293 21:41:43,280 --> 21:41:47,800 what Google does this is what my app 34294 21:41:46,232 --> 21:41:50,040 does 34295 21:41:47,800 --> 21:41:52,600 yeah yeah so if I'm logging in with a 34296 21:41:50,040 --> 21:41:54,512 username and password I I could imagine 34297 21:41:52,600 --> 21:41:56,720 that they show up in the URL after the 34298 21:41:54,512 --> 21:41:59,720 question mark where username equals 34299 21:41:56,720 --> 21:42:01,872 mailin and password equals 1 2 3 4 5 but 34300 21:41:59,720 --> 21:42:02,920 then all my you know like nosy siblings 34301 21:42:01,872 --> 21:42:04,920 need to do is go through through my 34302 21:42:02,920 --> 21:42:06,872 browser history and boom like it's right 34303 21:42:04,920 --> 21:42:08,832 there for them to copy paste so that 34304 21:42:06,872 --> 21:42:11,000 doesn't seem particularly uh secure or 34305 21:42:08,832 --> 21:42:12,920 if someone's walking past you in a cafe 34306 21:42:11,000 --> 21:42:14,800 they can just look at your url if it's 34307 21:42:12,920 --> 21:42:17,440 revealed by the browser and they see it 34308 21:42:14,800 --> 21:42:19,400 too so get is not necessarily the best 34309 21:42:17,440 --> 21:42:21,512 verb to use even though it's the default 34310 21:42:19,400 --> 21:42:23,192 when submitting forms typically when 34311 21:42:21,512 --> 21:42:25,760 you've got anything remotely sensitive 34312 21:42:23,192 --> 21:42:28,552 or anything large so be it a password or 34313 21:42:25,760 --> 21:42:30,600 credit card number or like an image or a 34314 21:42:28,552 --> 21:42:33,192 video that you're uploading to uh 34315 21:42:30,600 --> 21:42:35,480 Instagram or to YouTube or any such site 34316 21:42:33,192 --> 21:42:38,400 like that you don't want the data going 34317 21:42:35,480 --> 21:42:40,480 into the URL and thankfully there's 34318 21:42:38,400 --> 21:42:43,160 actually an easy way to fix this I can 34319 21:42:40,480 --> 21:42:45,192 go into my form which is currently in 34320 21:42:43,160 --> 21:42:48,480 index.html and I can just change the 34321 21:42:45,192 --> 21:42:50,400 method from get to post in lower case 34322 21:42:48,480 --> 21:42:51,920 here but the verb in the virtual 34323 21:42:50,400 --> 21:42:55,232 envelope we discussed last week would 34324 21:42:51,920 --> 21:42:58,120 itself be capital p o St now 34325 21:42:55,232 --> 21:43:01,360 unfortunately Watch What Happens here 34326 21:42:58,120 --> 21:43:03,480 let me go over to my original form by 34327 21:43:01,360 --> 21:43:04,920 going back to the Slash rout and I'm 34328 21:43:03,480 --> 21:43:07,920 reloading the page to make sure I get 34329 21:43:04,920 --> 21:43:11,160 the latest freshest HTML and just as a 34330 21:43:07,920 --> 21:43:13,920 um just to confirm here yep if I view 34331 21:43:11,160 --> 21:43:15,800 Source method is now post so let me go 34332 21:43:13,920 --> 21:43:18,440 ahead and type in David now and click 34333 21:43:15,800 --> 21:43:20,720 greet and before we saw hello David but 34334 21:43:18,440 --> 21:43:23,080 now I get method not allowed and this is 34335 21:43:20,720 --> 21:43:26,232 somewhat subtle but in the title of the 34336 21:43:23,080 --> 21:43:28,000 tab notice that it's a 405 error which 34337 21:43:26,232 --> 21:43:30,640 is not familiar probably almost all of 34338 21:43:28,000 --> 21:43:33,920 us have seen 404 file not found turns 34339 21:43:30,640 --> 21:43:36,960 out 405 a little more Arcane is the 34340 21:43:33,920 --> 21:43:39,120 method the HTTP verb is not allowed why 34341 21:43:36,960 --> 21:43:42,552 because by default my app.py only 34342 21:43:39,120 --> 21:43:44,440 currently supports get by default how do 34343 21:43:42,552 --> 21:43:46,872 I support post well I just need a little 34344 21:43:44,440 --> 21:43:50,440 bit more syntax so let me go back into 34345 21:43:46,872 --> 21:43:52,640 vs code here let me go into app.py now 34346 21:43:50,440 --> 21:43:55,080 and after changing the form I just need 34347 21:43:52,640 --> 21:43:57,480 to inform flask that you know what the 34348 21:43:55,080 --> 21:43:59,280 method I want this GRE route to use 34349 21:43:57,480 --> 21:44:02,552 should not be the default which is only 34350 21:43:59,280 --> 21:44:04,600 get I want it to use these methods and 34351 21:44:02,552 --> 21:44:07,160 it takes a second argument called 34352 21:44:04,600 --> 21:44:09,360 Methods the value of which is a list the 34353 21:44:07,160 --> 21:44:11,160 default of which is quote unquote get so 34354 21:44:09,360 --> 21:44:13,360 that's the default this has not made any 34355 21:44:11,160 --> 21:44:16,512 changes but if I want to support post 34356 21:44:13,360 --> 21:44:20,400 instead I can explicitly pass a list 34357 21:44:16,512 --> 21:44:22,192 with one string in it P instead and now 34358 21:44:20,400 --> 21:44:24,512 what does this mean we didn't talk about 34359 21:44:22,192 --> 21:44:26,640 this in any detail last week but inside 34360 21:44:24,512 --> 21:44:30,000 of this virtual envelope typically is 34361 21:44:26,640 --> 21:44:32,760 that line like get slash search Q equals 34362 21:44:30,000 --> 21:44:34,280 cat after the ex after the question mark 34363 21:44:32,760 --> 21:44:35,920 if you want to hide that kind of 34364 21:44:34,280 --> 21:44:37,360 information for privacy sake or because 34365 21:44:35,920 --> 21:44:38,640 you want to upload like an image which 34366 21:44:37,360 --> 21:44:40,872 just doesn't make sense to put in the 34367 21:44:38,640 --> 21:44:43,080 URL essentially the part of the story 34368 21:44:40,872 --> 21:44:44,800 would be well the computer looks deeper 34369 21:44:43,080 --> 21:44:47,360 inside of that virtual envelope and 34370 21:44:44,800 --> 21:44:49,872 anything submitted via post goes below 34371 21:44:47,360 --> 21:44:51,512 the htdp headers like deeper in that 34372 21:44:49,872 --> 21:44:54,120 envelope so they're still there they're 34373 21:44:51,512 --> 21:44:57,160 just not obviously visible uh for prying 34374 21:44:54,120 --> 21:44:59,640 eyes in the user's own browser so just 34375 21:44:57,160 --> 21:45:01,760 by making that change in the HTML 34376 21:44:59,640 --> 21:45:04,232 telling the browser to submit the data 34377 21:45:01,760 --> 21:45:07,440 via post and changing app.py to tell the 34378 21:45:04,232 --> 21:45:10,280 route to expect the data via post I can 34379 21:45:07,440 --> 21:45:12,680 now go back to my other tab let me go 34380 21:45:10,280 --> 21:45:14,440 back to the original page let me reload 34381 21:45:12,680 --> 21:45:16,920 just so I've got the latest HTML and 34382 21:45:14,440 --> 21:45:19,480 indeed view page Source it's still yep 34383 21:45:16,920 --> 21:45:23,320 it's still post but now when I type in 34384 21:45:19,480 --> 21:45:27,192 DAV ID and click greet now it works but 34385 21:45:23,320 --> 21:45:29,280 but but notice the Privacy implication 34386 21:45:27,192 --> 21:45:32,400 I'm at the SLG greet route but where's 34387 21:45:29,280 --> 21:45:34,040 my name it's not actually there it's 34388 21:45:32,400 --> 21:45:36,040 still went to the server but it's not in 34389 21:45:34,040 --> 21:45:40,720 your autocomplete or your history now 34390 21:45:36,040 --> 21:45:44,080 for privacy sake questions now on 34391 21:45:40,720 --> 21:45:46,680 post yeah oh No Just scratching all 34392 21:45:44,080 --> 21:45:47,960 right can you the programmer see this 34393 21:45:46,680 --> 21:45:50,680 well let me show you a couple of other 34394 21:45:47,960 --> 21:45:53,192 features of Chrome's uh Chrome and 34395 21:45:50,680 --> 21:45:54,720 Safari and other browsers as well I keep 34396 21:45:53,192 --> 21:45:56,440 going to view page source which just 34397 21:45:54,720 --> 21:45:58,800 shows you like a readon version of your 34398 21:45:56,440 --> 21:46:00,480 HTML but recall that last time I 34399 21:45:58,800 --> 21:46:02,512 actually right-clicked and went to 34400 21:46:00,480 --> 21:46:04,000 inspect or viewed developer tool tools 34401 21:46:02,512 --> 21:46:06,440 and this brings up a much fancier 34402 21:46:04,000 --> 21:46:08,280 version of the developer tools and under 34403 21:46:06,440 --> 21:46:09,640 elements here you see everything and 34404 21:46:08,280 --> 21:46:11,360 it's nice and pretty printed it's 34405 21:46:09,640 --> 21:46:12,872 hierarchical it collapses things into 34406 21:46:11,360 --> 21:46:14,440 these clickable triangles but it's the 34407 21:46:12,872 --> 21:46:16,440 exact same thing it's just more 34408 21:46:14,440 --> 21:46:19,040 interactive but notice what I can do 34409 21:46:16,440 --> 21:46:21,600 today is this if I go to the network tab 34410 21:46:19,040 --> 21:46:25,360 here and let me zoom out a little bit 34411 21:46:21,600 --> 21:46:29,320 let me go ahead and re uh load the form 34412 21:46:25,360 --> 21:46:33,400 here and type in David again and click 34413 21:46:29,320 --> 21:46:36,080 greet notice now in the network tab of 34414 21:46:33,400 --> 21:46:38,680 Chrome's developer tools I see a few 34415 21:46:36,080 --> 21:46:41,160 things as we saw before one I see that 34416 21:46:38,680 --> 21:46:42,760 the request method is post two I see 34417 21:46:41,160 --> 21:46:44,800 that the server automatically without me 34418 21:46:42,760 --> 21:46:47,000 writing any code for this returns 200 34419 21:46:44,800 --> 21:46:50,000 when it's successful but I can scroll 34420 21:46:47,000 --> 21:46:52,680 down down down down down and you'll see 34421 21:46:50,000 --> 21:46:55,192 that eventually after all these cookies 34422 21:46:52,680 --> 21:46:57,800 more on those later if I click on 34423 21:46:55,192 --> 21:47:00,640 payload the second tab next to headers 34424 21:46:57,800 --> 21:47:02,320 you can see as the developer what was 34425 21:47:00,640 --> 21:47:03,600 actually sent to the server so indeed 34426 21:47:02,320 --> 21:47:05,160 this is going to be super useful like 34427 21:47:03,600 --> 21:47:07,000 when doing problem set 9 maybe your 34428 21:47:05,160 --> 21:47:09,320 final projects if you want to see what's 34429 21:47:07,000 --> 21:47:11,600 going from browser to server you have 34430 21:47:09,320 --> 21:47:13,640 complete control over all of that 34431 21:47:11,600 --> 21:47:14,960 information even if you're using htps 34432 21:47:13,640 --> 21:47:17,440 because your browser and you the 34433 21:47:14,960 --> 21:47:19,360 developer can certainly see all of this 34434 21:47:17,440 --> 21:47:20,640 so again these developer tools even 34435 21:47:19,360 --> 21:47:22,640 though there's a lot of tabs and buttons 34436 21:47:20,640 --> 21:47:24,960 you probably won't need anytime soon 34437 21:47:22,640 --> 21:47:26,832 some of them like elements and network 34438 21:47:24,960 --> 21:47:30,360 and with JavaScript console are going to 34439 21:47:26,832 --> 21:47:35,360 be super useful to start to get familiar 34440 21:47:30,360 --> 21:47:38,920 with all right any questions now on this 34441 21:47:35,360 --> 21:47:42,192 implication of 34442 21:47:38,920 --> 21:47:45,760 post anything at 34443 21:47:42,192 --> 21:47:47,832 all no okay how about one final hello 34444 21:47:45,760 --> 21:47:50,440 example that ties a few of these things 34445 21:47:47,832 --> 21:47:52,920 together how about now we try to tighten 34446 21:47:50,440 --> 21:47:54,512 things up further only in anticipation 34447 21:47:52,920 --> 21:47:57,080 of something like problem set 9 or 34448 21:47:54,512 --> 21:48:00,280 really more complicated web apps where 34449 21:47:57,080 --> 21:48:02,160 you might have not two but 20 or maybe 34450 21:48:00,280 --> 21:48:04,920 even more different routes it might 34451 21:48:02,160 --> 21:48:06,480 might be ideal to just minimize how many 34452 21:48:04,920 --> 21:48:08,512 total routes we have so we don't get a 34453 21:48:06,480 --> 21:48:11,552 little too overwhelmed and I dare say 34454 21:48:08,512 --> 21:48:14,192 that these two routes are so short maybe 34455 21:48:11,552 --> 21:48:16,000 I can combine them into one and maybe I 34456 21:48:14,192 --> 21:48:18,080 can keep the user at what seems to be 34457 21:48:16,000 --> 21:48:20,320 the same URL but just a kind of Tidy 34458 21:48:18,080 --> 21:48:23,600 things up so let me propose that we do 34459 21:48:20,320 --> 21:48:27,760 this instead let me get rid of my greet 34460 21:48:23,600 --> 21:48:30,160 route and let me go into my form in 34461 21:48:27,760 --> 21:48:33,192 index.html and let me go ahead and just 34462 21:48:30,160 --> 21:48:35,400 have the action of this form still slash 34463 21:48:33,192 --> 21:48:37,920 so I want the form to be visible at 34464 21:48:35,400 --> 21:48:40,480 slash the index of the site but I also 34465 21:48:37,920 --> 21:48:42,232 want the form to submit to itself if 34466 21:48:40,480 --> 21:48:44,192 only because I don't want to introduce 34467 21:48:42,232 --> 21:48:45,832 another route like SLG greet which 34468 21:48:44,192 --> 21:48:47,320 eventually indeed will be compelling so 34469 21:48:45,832 --> 21:48:49,512 you don't have one route for everything 34470 21:48:47,320 --> 21:48:51,120 you want your website to do so 34471 21:48:49,512 --> 21:48:53,120 technically this is the default to and 34472 21:48:51,120 --> 21:48:55,120 if I omit action the exact same thing 34473 21:48:53,120 --> 21:48:57,320 would happen as well but let me rewind 34474 21:48:55,120 --> 21:48:59,360 and let me now go into app.py to see how 34475 21:48:57,320 --> 21:49:01,640 we can make this happen well if I want 34476 21:48:59,360 --> 21:49:04,280 my one and now only route to support 34477 21:49:01,640 --> 21:49:07,120 both methods I can say methods equals 34478 21:49:04,280 --> 21:49:08,600 and then a list with both get and post 34479 21:49:07,120 --> 21:49:10,920 in any order but I'll keep them 34480 21:49:08,600 --> 21:49:13,600 alphabetical like this this now tells 34481 21:49:10,920 --> 21:49:17,280 python hey this route should handle both 34482 21:49:13,600 --> 21:49:19,832 get and post requests at the same place 34483 21:49:17,280 --> 21:49:23,280 let's now go into this function I kind 34484 21:49:19,832 --> 21:49:26,760 of want to say the equivalent of this if 34485 21:49:23,280 --> 21:49:29,512 get then I want to return the form else 34486 21:49:26,760 --> 21:49:32,960 if post I want to then return render 34487 21:49:29,512 --> 21:49:35,000 template of greet Doh HTML with the 34488 21:49:32,960 --> 21:49:37,280 user's name but this is not yet complete 34489 21:49:35,000 --> 21:49:39,320 code but I think I can do this I'm going 34490 21:49:37,280 --> 21:49:42,040 to go ahead and say the following I'm 34491 21:49:39,320 --> 21:49:46,040 going to go ahead and say if 34492 21:49:42,040 --> 21:49:47,600 request. method equals equals get then 34493 21:49:46,040 --> 21:49:51,080 indeed return 34494 21:49:47,600 --> 21:49:53,920 index.html L if request. method equals 34495 21:49:51,080 --> 21:49:56,872 equals post then go ahead and return 34496 21:49:53,920 --> 21:49:58,232 greet HTML this isn't quite enough 34497 21:49:56,872 --> 21:50:00,040 though because I still want to pass in 34498 21:49:58,232 --> 21:50:04,280 that placeholder so let me again add 34499 21:50:00,040 --> 21:50:06,680 back name equals request. args doget 34500 21:50:04,280 --> 21:50:10,440 quote unquote name and then a default 34501 21:50:06,680 --> 21:50:13,400 value of world what does this Now do for 34502 21:50:10,440 --> 21:50:15,000 me well let me go back to my other tab 34503 21:50:13,400 --> 21:50:16,600 here let me close the developer tools 34504 21:50:15,000 --> 21:50:18,232 let me go back to the form here let me 34505 21:50:16,600 --> 21:50:19,720 reload to make sure I have the latest 34506 21:50:18,232 --> 21:50:21,360 let me view page Source just to make 34507 21:50:19,720 --> 21:50:23,160 sure I have the latest and yep I have 34508 21:50:21,360 --> 21:50:25,480 the latest because it still says post 34509 21:50:23,160 --> 21:50:27,872 but it now says slash and let's see what 34510 21:50:25,480 --> 21:50:29,920 happens now if I type in my name David 34511 21:50:27,872 --> 21:50:32,160 previously this submitted bya post so I 34512 21:50:29,920 --> 21:50:34,480 didn't see any name or value thereof in 34513 21:50:32,160 --> 21:50:36,360 the URL but I did end up at SLG greet 34514 21:50:34,480 --> 21:50:39,280 but if the action is now slash and I 34515 21:50:36,360 --> 21:50:41,960 click greet notice that it still kind of 34516 21:50:39,280 --> 21:50:43,232 works I see hello comma World although 34517 21:50:41,960 --> 21:50:44,600 that didn't quite work so we'll come 34518 21:50:43,232 --> 21:50:47,400 back to that issue in a moment but 34519 21:50:44,600 --> 21:50:48,832 notice the URL ends in just slash and 34520 21:50:47,400 --> 21:50:50,512 again Chrome is hiding the slash because 34521 21:50:48,832 --> 21:50:54,552 that's all that's there but it does not 34522 21:50:50,512 --> 21:50:56,832 end in name equals David in this case or 34523 21:50:54,552 --> 21:50:59,480 name and equals world now notice this 34524 21:50:56,832 --> 21:51:02,080 too if I reload I'm going to get this 34525 21:50:59,480 --> 21:51:03,680 warning do you want to confirm form 34526 21:51:02,080 --> 21:51:04,920 resubmission the page you're looking for 34527 21:51:03,680 --> 21:51:06,360 used information that you entered 34528 21:51:04,920 --> 21:51:07,720 returning to that page might cause any 34529 21:51:06,360 --> 21:51:09,232 action you took to be repeated do you 34530 21:51:07,720 --> 21:51:10,872 want to continue you might have seen 34531 21:51:09,232 --> 21:51:12,920 this on websites you've actually visited 34532 21:51:10,872 --> 21:51:14,080 where you hit reload and you're prompted 34533 21:51:12,920 --> 21:51:16,080 wait a minute do you want to do that 34534 21:51:14,080 --> 21:51:18,480 odds are you've been prompted to reload 34535 21:51:16,080 --> 21:51:21,832 explicitly because why whatever you just 34536 21:51:18,480 --> 21:51:23,640 did was post instead of get and by 34537 21:51:21,832 --> 21:51:26,080 convention besides post being used for 34538 21:51:23,640 --> 21:51:27,512 privacy to like hide your username your 34539 21:51:26,080 --> 21:51:29,440 password your credit card number or the 34540 21:51:27,512 --> 21:51:32,440 like besides being used to upload bigger 34541 21:51:29,440 --> 21:51:34,680 files like images or videos post is also 34542 21:51:32,440 --> 21:51:36,640 used by convention to make changes to 34543 21:51:34,680 --> 21:51:38,552 the server to add something to your 34544 21:51:36,640 --> 21:51:40,160 shopping cart to add something to the 34545 21:51:38,552 --> 21:51:41,760 database whereas get is the name 34546 21:51:40,160 --> 21:51:44,512 suggests is all about getting 34547 21:51:41,760 --> 21:51:46,720 information not posting or sending 34548 21:51:44,512 --> 21:51:48,552 information instead so this is Chrome 34549 21:51:46,720 --> 21:51:50,080 being a little careful because you know 34550 21:51:48,552 --> 21:51:51,400 if you just checked out on Amazon and 34551 21:51:50,080 --> 21:51:53,232 then you hit reload you don't want to 34552 21:51:51,400 --> 21:51:55,480 accidentally like buy the same book 34553 21:51:53,232 --> 21:51:57,552 again so to speak even though Amazon and 34554 21:51:55,480 --> 21:52:00,360 fancy websites have other defenses for 34555 21:51:57,552 --> 21:52:03,192 this too to avoid this issue now there 34556 21:52:00,360 --> 21:52:05,320 is a bug though here it says hello comma 34557 21:52:03,192 --> 21:52:06,832 World instead of hello comma David and 34558 21:52:05,320 --> 21:52:09,192 it actually would have said the same a 34559 21:52:06,832 --> 21:52:11,160 moment ago and I just didn't retest the 34560 21:52:09,192 --> 21:52:12,920 code and reveal as much to you or if I 34561 21:52:11,160 --> 21:52:15,280 did I didn't even notice it said hello 34562 21:52:12,920 --> 21:52:19,360 world instead of hello David it turns 34563 21:52:15,280 --> 21:52:22,720 out that request. RGS is only used for 34564 21:52:19,360 --> 21:52:24,192 get when using get request. args is a 34565 21:52:22,720 --> 21:52:26,760 dictionary that contains all of your key 34566 21:52:24,192 --> 21:52:29,080 value pairs but somewhat confusingly 34567 21:52:26,760 --> 21:52:32,232 when using post with flask you have to 34568 21:52:29,080 --> 21:52:33,760 go into request. form I have no idea why 34569 21:52:32,232 --> 21:52:36,080 these are not sort of more obvious 34570 21:52:33,760 --> 21:52:39,280 opposites like request.get or request. 34571 21:52:36,080 --> 21:52:41,480 form and sorry request.get and request. 34572 21:52:39,280 --> 21:52:43,640 poost would be sort of sensible names in 34573 21:52:41,480 --> 21:52:47,192 this case though we have request. args 34574 21:52:43,640 --> 21:52:48,552 for get and request. form for post all 34575 21:52:47,192 --> 21:52:50,360 right that's an easy fix though if I go 34576 21:52:48,552 --> 21:52:53,720 back to vs code here let's change 34577 21:52:50,360 --> 21:52:56,440 request. ARS to request. form let's go 34578 21:52:53,720 --> 21:52:58,040 back to my other tab let me just reload 34579 21:52:56,440 --> 21:53:00,120 and you know what I'm going to say okay 34580 21:52:58,040 --> 21:53:01,960 continue to resubmit the same form 34581 21:53:00,120 --> 21:53:04,720 because the form was okay it was my 34582 21:53:01,960 --> 21:53:07,440 python code that was buggy hitting enter 34583 21:53:04,720 --> 21:53:09,480 now it's accessing David okay but watch 34584 21:53:07,440 --> 21:53:11,872 this again if I hit reload command r or 34585 21:53:09,480 --> 21:53:13,832 control r i get the same warning are you 34586 21:53:11,872 --> 21:53:15,800 sure you want to submit the form yes if 34587 21:53:13,832 --> 21:53:18,080 I do it manually with the reload icon I 34588 21:53:15,800 --> 21:53:20,512 get the same warning as before but if I 34589 21:53:18,080 --> 21:53:22,512 want to manually induce a get request 34590 21:53:20,512 --> 21:53:24,512 well that's fine don't hit reload and 34591 21:53:22,512 --> 21:53:27,400 send the same request instead go up to 34592 21:53:24,512 --> 21:53:29,760 your URL and just put the cursor up 34593 21:53:27,400 --> 21:53:32,720 there and hit enter and now notice same 34594 21:53:29,760 --> 21:53:35,280 URL is a get by default so anytime you 34595 21:53:32,720 --> 21:53:37,360 and I have typed URLs into browsers get 34596 21:53:35,280 --> 21:53:39,760 is always the default only when you 34597 21:53:37,360 --> 21:53:42,192 click on a button typically that the 34598 21:53:39,760 --> 21:53:44,040 programmer has configured to use post 34599 21:53:42,192 --> 21:53:47,192 are you actually adding things to your 34600 21:53:44,040 --> 21:53:51,040 shopping cart or the like all right so 34601 21:53:47,192 --> 21:53:53,360 we are back and if I go way back in time 34602 21:53:51,040 --> 21:53:57,600 myself like this is actually like the 34603 21:53:53,360 --> 21:54:01,440 first web application I made back in 34604 21:53:57,600 --> 21:54:03,192 1997 I believe uh so at the time this 34605 21:54:01,440 --> 21:54:05,080 was would have been what my sophomore or 34606 21:54:03,192 --> 21:54:07,120 so year I had taken cs50 I took a 34607 21:54:05,080 --> 21:54:08,680 follow- on class called cs51 which is a 34608 21:54:07,120 --> 21:54:10,400 different type of programming and then I 34609 21:54:08,680 --> 21:54:11,960 pretty much taught myself a language 34610 21:54:10,400 --> 21:54:13,832 called Pearl which is somewhat less 34611 21:54:11,960 --> 21:54:15,832 popular nowadays but it's another 34612 21:54:13,832 --> 21:54:17,320 language like python like Java like 34613 21:54:15,832 --> 21:54:19,320 JavaScript like others that can be used 34614 21:54:17,320 --> 21:54:22,400 to make web-based applications and the 34615 21:54:19,320 --> 21:54:24,320 web was very young at the time and the 34616 21:54:22,400 --> 21:54:26,000 process via which students my classmates 34617 21:54:24,320 --> 21:54:29,920 could register uh for the first year 34618 21:54:26,000 --> 21:54:31,360 intral sports program AKA Frost's was to 34619 21:54:29,920 --> 21:54:32,680 grab a piece of paper and like write 34620 21:54:31,360 --> 21:54:34,760 your name and email address on it and 34621 21:54:32,680 --> 21:54:36,512 walk it across across the yard to 34622 21:54:34,760 --> 21:54:37,960 Wigglesworth I believe where the Proctor 34623 21:54:36,512 --> 21:54:39,720 lived and you'd Slide the piece of paper 34624 21:54:37,960 --> 21:54:42,040 under the door and like that was how we 34625 21:54:39,720 --> 21:54:44,720 submitted forms in my day um so this was 34626 21:54:42,040 --> 21:54:47,040 an opportunity even back in 1997 is to 34627 21:54:44,720 --> 21:54:48,920 like move things online and the website 34628 21:54:47,040 --> 21:54:51,760 went on to live on until I think like 34629 21:54:48,920 --> 21:54:53,760 2007 I found this online and then it's 34630 21:54:51,760 --> 21:54:55,160 become something else since um but this 34631 21:54:53,760 --> 21:54:56,960 was a website via which people could 34632 21:54:55,160 --> 21:54:58,800 register for sports and people could log 34633 21:54:56,960 --> 21:55:00,872 in the scores for various games and 34634 21:54:58,800 --> 21:55:01,872 whatnot and so underneath the hood I 34635 21:55:00,872 --> 21:55:03,872 didn't even know anything anything about 34636 21:55:01,872 --> 21:55:06,640 databases at the time it was just like 34637 21:55:03,872 --> 21:55:09,120 CSV files that I was storing the data in 34638 21:55:06,640 --> 21:55:11,000 but there were HTML forms and there was 34639 21:55:09,120 --> 21:55:12,920 with pearl the language at the time the 34640 21:55:11,000 --> 21:55:15,160 way to do the exact kind of stuff that 34641 21:55:12,920 --> 21:55:17,440 we've just been doing already with flask 34642 21:55:15,160 --> 21:55:19,640 and so what I thought we'd do is 34643 21:55:17,440 --> 21:55:22,760 Implement a slightly less ugly version 34644 21:55:19,640 --> 21:55:25,192 of this um repeating uh graphical 34645 21:55:22,760 --> 21:55:27,400 backgrounds were invogue in like 1997 as 34646 21:55:25,192 --> 21:55:29,120 you can see here um but this is where 34647 21:55:27,400 --> 21:55:31,480 these were the Aesthetics of the day uh 34648 21:55:29,120 --> 21:55:32,552 including the the so-called blink tag so 34649 21:55:31,480 --> 21:55:34,160 let's at least focus on the 34650 21:55:32,552 --> 21:55:35,680 functionality of this website and not so 34651 21:55:34,160 --> 21:55:37,160 much the Aesthetics and see if we can't 34652 21:55:35,680 --> 21:55:38,640 Implement some of the plumbing for 34653 21:55:37,160 --> 21:55:40,040 actually solving like a real world 34654 21:55:38,640 --> 21:55:42,000 representative problem be it for 34655 21:55:40,040 --> 21:55:43,280 freshman inal sports or something else 34656 21:55:42,000 --> 21:55:45,872 like it where you're getting data from 34657 21:55:43,280 --> 21:55:47,872 users and processing it somehow so let 34658 21:55:45,872 --> 21:55:50,040 me go over here to VSS code let me 34659 21:55:47,872 --> 21:55:51,680 create a new directory called fros IMS 34660 21:55:50,040 --> 21:55:54,552 just so we can keep all of this code in 34661 21:55:51,680 --> 21:55:56,680 its own directory let me CD into fros 34662 21:55:54,552 --> 21:55:58,680 IMS let me proactively make another 34663 21:55:56,680 --> 21:56:02,192 directory called templates in which our 34664 21:55:58,680 --> 21:56:04,832 templates our HTML files do need to live 34665 21:56:02,192 --> 21:56:08,640 and eventually I'm going to go ahead and 34666 21:56:04,832 --> 21:56:10,680 create a two files minimally app.py and 34667 21:56:08,640 --> 21:56:13,872 index.html so let's do the first of 34668 21:56:10,680 --> 21:56:15,552 those app.py will live in my Frost im's 34669 21:56:13,872 --> 21:56:17,120 directory and I'm just going to recreate 34670 21:56:15,552 --> 21:56:20,280 something very simple like we have 34671 21:56:17,120 --> 21:56:23,680 previously so from flask in lowercase 34672 21:56:20,280 --> 21:56:25,920 import flask capitalized render template 34673 21:56:23,680 --> 21:56:27,640 and also request so same first line is 34674 21:56:25,920 --> 21:56:29,640 before let me then give myself a 34675 21:56:27,640 --> 21:56:31,960 variable called app set it equal to 34676 21:56:29,640 --> 21:56:33,720 calling the flask function capital f 34677 21:56:31,960 --> 21:56:37,600 with underscore uncore name underscore 34678 21:56:33,720 --> 21:56:41,760 uncore and then let me give myself a 34679 21:56:37,600 --> 21:56:43,160 route for slash as before with an index 34680 21:56:41,760 --> 21:56:45,280 function though again I could call that 34681 21:56:43,160 --> 21:56:47,512 anything I want and just for now let's 34682 21:56:45,280 --> 21:56:50,000 return render template of quote unquote 34683 21:56:47,512 --> 21:56:51,720 index.html as though that exists so this 34684 21:56:50,000 --> 21:56:53,960 is not really a web application as much 34685 21:56:51,720 --> 21:56:57,360 as it is at the moment just a recreation 34686 21:56:53,960 --> 21:57:01,120 of HTTP server for one file let's now in 34687 21:56:57,360 --> 21:57:02,360 another tab create a templates file uh 34688 21:57:01,120 --> 21:57:04,512 called called 34689 21:57:02,360 --> 21:57:06,800 index.html and I'm going to save myself 34690 21:57:04,512 --> 21:57:08,640 a few keystrokes I let me copy paste 34691 21:57:06,800 --> 21:57:10,232 from earlier almost all of the layout 34692 21:57:08,640 --> 21:57:13,040 from before I've change the title in 34693 21:57:10,232 --> 21:57:14,960 advance to frosts instead of hello but 34694 21:57:13,040 --> 21:57:16,720 this is essentially the same template 34695 21:57:14,960 --> 21:57:18,960 and for now though because I'm in an 34696 21:57:16,720 --> 21:57:21,512 index.html I'm not going to use extends 34697 21:57:18,960 --> 21:57:22,960 or any of that fancy block stuff yet I'm 34698 21:57:21,512 --> 21:57:25,000 just going to go ahead and create a 34699 21:57:22,960 --> 21:57:26,552 relatively simple form via which back in 34700 21:57:25,000 --> 21:57:29,552 the day my classmates could have 34701 21:57:26,552 --> 21:57:33,040 registered for interal Sports so let's 34702 21:57:29,552 --> 21:57:36,440 go ahead here and I'll propose that we 34703 21:57:33,040 --> 21:57:39,280 do this um in this page we'll have a 34704 21:57:36,440 --> 21:57:41,512 form the action of which will be a route 34705 21:57:39,280 --> 21:57:43,552 called SL register though I could call 34706 21:57:41,512 --> 21:57:45,320 that anything I want it'll be somewhat 34707 21:57:43,552 --> 21:57:47,280 private so I'm going to use post instead 34708 21:57:45,320 --> 21:57:49,280 of get just so that people don't 34709 21:57:47,280 --> 21:57:51,640 accidentally maybe register twice by 34710 21:57:49,280 --> 21:57:54,040 hitting reload uh without warning uh 34711 21:57:51,640 --> 21:57:57,280 inside of this form let's go ahead and 34712 21:57:54,040 --> 21:57:59,192 give them an input uh where autocomplete 34713 21:57:57,280 --> 21:58:02,000 will be off as always for demonstration 34714 21:57:59,192 --> 21:58:04,280 sake autofocus so the cursor goes there 34715 21:58:02,000 --> 21:58:05,760 initially the name of this field will be 34716 21:58:04,280 --> 21:58:07,000 literally named because I want my 34717 21:58:05,760 --> 21:58:09,192 classmates's name if they want to 34718 21:58:07,000 --> 21:58:11,000 register for some sport the placeholder 34719 21:58:09,192 --> 21:58:13,360 will again be quote unquote Nam just so 34720 21:58:11,000 --> 21:58:16,120 they see some gray instructions and the 34721 21:58:13,360 --> 21:58:18,720 type of this field will indeed be text 34722 21:58:16,120 --> 21:58:20,800 as before and then I need to give them 34723 21:58:18,720 --> 21:58:22,232 the ability to register for a few Sports 34724 21:58:20,800 --> 21:58:24,512 why don't we keep it simple like back in 34725 21:58:22,232 --> 21:58:26,080 the day basketball soccer and Ultimate 34726 21:58:24,512 --> 21:58:28,040 Frisbee were three of the sports that we 34727 21:58:26,080 --> 21:58:30,160 supported and so let me do this and you 34728 21:58:28,040 --> 21:58:31,960 might not have seen this before uh 34729 21:58:30,160 --> 21:58:33,720 unless you dabbled further on with forms 34730 21:58:31,960 --> 21:58:36,192 on your own but I can create a select 34731 21:58:33,720 --> 21:58:38,640 menu otherwise known as a drop-down menu 34732 21:58:36,192 --> 21:58:40,872 in HTML inside of which are a whole 34733 21:58:38,640 --> 21:58:42,640 bunch of options and each option 34734 21:58:40,872 --> 21:58:44,832 typically follows this Paradigm the 34735 21:58:42,640 --> 21:58:46,800 value of the option and then the actual 34736 21:58:44,832 --> 21:58:50,552 text that the human sees so the value of 34737 21:58:46,800 --> 21:58:53,480 these options will be how about we do uh 34738 21:58:50,552 --> 21:58:55,080 basketball as one and I want the human 34739 21:58:53,480 --> 21:58:56,920 to see literally the same thing though 34740 21:58:55,080 --> 21:58:58,480 just like with a link in HTML they could 34741 21:58:56,920 --> 21:59:01,640 be different but I'm going to keep them 34742 21:58:58,480 --> 21:59:03,480 the same another option will be 34743 21:59:01,640 --> 21:59:06,400 uh let's say 34744 21:59:03,480 --> 21:59:08,280 soccer and whoops let me fix my quotes 34745 21:59:06,400 --> 21:59:09,960 and this human will see the exact same 34746 21:59:08,280 --> 21:59:11,872 thing though it could say something else 34747 21:59:09,960 --> 21:59:15,000 and then lastly the value will be quote 34748 21:59:11,872 --> 21:59:18,040 unquote Ultimate Frisbee and the humans 34749 21:59:15,000 --> 21:59:20,232 will see the same thing there ultimate 34750 21:59:18,040 --> 21:59:21,680 frisbe all right so this is going to 34751 21:59:20,232 --> 21:59:23,800 create as we'll soon see just a drop- 34752 21:59:21,680 --> 21:59:25,512 down menu with three separate options if 34753 21:59:23,800 --> 21:59:26,920 I want the students to be able to submit 34754 21:59:25,512 --> 21:59:29,720 this now let me give them a button the 34755 21:59:26,920 --> 21:59:32,360 type of which is submit and this button 34756 21:59:29,720 --> 21:59:33,920 will be like the word register on it so 34757 21:59:32,360 --> 21:59:36,040 I think we're pretty much good to go 34758 21:59:33,920 --> 21:59:38,552 like this is all just HTML no python no 34759 21:59:36,040 --> 21:59:40,760 flask per se except for the rendering of 34760 21:59:38,552 --> 21:59:43,232 the same template so let me go into my 34761 21:59:40,760 --> 21:59:45,000 terminal window let me do flask run 34762 21:59:43,232 --> 21:59:46,800 inside of this directory because I need 34763 21:59:45,000 --> 21:59:48,600 to serve this app instead I'm going to 34764 21:59:46,800 --> 21:59:50,480 see some ugly output including my own 34765 21:59:48,600 --> 21:59:53,160 URL and if I hover over that and then 34766 21:59:50,480 --> 21:59:54,680 open that URL I should now see a more 34767 21:59:53,160 --> 21:59:56,480 interesting form it's got not only a 34768 21:59:54,680 --> 21:59:59,080 field for their name but also this 34769 21:59:56,480 --> 22:00:01,600 drop-down menu with all three Sports now 34770 21:59:59,080 --> 22:00:03,720 this isn't maybe the best user uh 34771 22:00:01,600 --> 22:00:05,720 experience thus far because I feel like 34772 22:00:03,720 --> 22:00:07,920 I'm biasing people to registering for 34773 22:00:05,720 --> 22:00:09,872 basketball maybe because it's checked by 34774 22:00:07,920 --> 22:00:11,680 default I mean a lot of forms nowadays 34775 22:00:09,872 --> 22:00:13,512 have like a blank placeholder for the 34776 22:00:11,680 --> 22:00:15,320 form so this is just an aesthetic thing 34777 22:00:13,512 --> 22:00:18,000 but I can do this let me go back to the 34778 22:00:15,320 --> 22:00:20,120 same form and let me give myself just a 34779 22:00:18,000 --> 22:00:22,440 a blank option at the top that in fact 34780 22:00:20,120 --> 22:00:24,680 I'm going to disable so you technically 34781 22:00:22,440 --> 22:00:26,832 can't select it proactively but I am 34782 22:00:24,680 --> 22:00:28,512 going to select it by default and so we 34783 22:00:26,832 --> 22:00:30,440 probably haven't seen those HTML 34784 22:00:28,512 --> 22:00:33,640 attributes before but if I want to 34785 22:00:30,440 --> 22:00:35,720 create the equivalent of like a a a 34786 22:00:33,640 --> 22:00:37,920 title for this dropdown I'm going to 34787 22:00:35,720 --> 22:00:39,872 literally create a disabled option 34788 22:00:37,920 --> 22:00:41,920 that's automatically selected called 34789 22:00:39,872 --> 22:00:44,120 sport so that you can't select it per se 34790 22:00:41,920 --> 22:00:47,192 but it is there at the top so if I go 34791 22:00:44,120 --> 22:00:48,760 back now to my other tab reload you know 34792 22:00:47,192 --> 22:00:50,480 it's just marginally prettier than 34793 22:00:48,760 --> 22:00:52,160 before and I'm not biasing people toward 34794 22:00:50,480 --> 22:00:54,000 accidentally registering for basketball 34795 22:00:52,160 --> 22:00:55,960 alone and if I click on this you'll see 34796 22:00:54,000 --> 22:00:57,960 that sport is great out and therefore 34797 22:00:55,960 --> 22:01:00,440 not manually selectable but I can select 34798 22:00:57,960 --> 22:01:02,400 any of these other three still all right 34799 22:01:00,440 --> 22:01:04,160 well un unfortunately if I type in David 34800 22:01:02,400 --> 22:01:08,160 and I try registering for instance for 34801 22:01:04,160 --> 22:01:10,832 soccer and click register I do end up 34802 22:01:08,160 --> 22:01:12,872 atreg and there's no question mark or 34803 22:01:10,832 --> 22:01:15,480 name or sport so it's probably indeed 34804 22:01:12,872 --> 22:01:19,920 post instead of get those are hints but 34805 22:01:15,480 --> 22:01:22,800 not found notice the tab here very uh uh 34806 22:01:19,920 --> 22:01:26,280 uh succinctly says 404 not found well 34807 22:01:22,800 --> 22:01:29,120 why is that just to be clear why did SL 34808 22:01:26,280 --> 22:01:29,120 register give me a 34809 22:01:29,872 --> 22:01:37,120 404 what's the logic 34810 22:01:32,960 --> 22:01:39,400 here perhaps just State the obvious 34811 22:01:37,120 --> 22:01:41,040 or it doesn't exist right we haven't 34812 22:01:39,400 --> 22:01:42,600 done that step yet all right so 34813 22:01:41,040 --> 22:01:44,920 something as simple as that and so I 34814 22:01:42,600 --> 22:01:46,360 actually U sort of belabor that point 34815 22:01:44,920 --> 22:01:47,960 because as you're learning like a lot of 34816 22:01:46,360 --> 22:01:49,080 these conventions and some of this new 34817 22:01:47,960 --> 22:01:50,512 syntax like honestly you're just going 34818 22:01:49,080 --> 22:01:52,600 to make stupid mistakes something's not 34819 22:01:50,512 --> 22:01:54,720 going to work but again go back to First 34820 22:01:52,600 --> 22:01:56,680 principles why is it not found all 34821 22:01:54,720 --> 22:01:58,832 right/ register should be a template 34822 22:01:56,680 --> 22:02:00,720 maybe called register. HTML oh I forgot 34823 22:01:58,832 --> 22:02:02,720 my app. route so that should be the kind 34824 22:02:00,720 --> 22:02:04,440 of of thinking as you try to diagnose 34825 22:02:02,720 --> 22:02:07,360 these problems moving forward all right 34826 22:02:04,440 --> 22:02:10,192 so let me go into app. and let me give 34827 22:02:07,360 --> 22:02:13,400 myself a second route here uh so app. 34828 22:02:10,192 --> 22:02:15,232 route quote unquote SL route then let me 34829 22:02:13,400 --> 22:02:17,232 Define a function called anything I want 34830 22:02:15,232 --> 22:02:19,872 but I'm going to call it oh sorry not SL 34831 22:02:17,232 --> 22:02:22,280 route SL register let me call the 34832 22:02:19,872 --> 22:02:24,600 function just to be consistent register 34833 22:02:22,280 --> 22:02:26,440 so but I could call that anything I want 34834 22:02:24,600 --> 22:02:28,760 and just for now let's not do anything 34835 22:02:26,440 --> 22:02:30,872 too interesting let's just return the 34836 22:02:28,760 --> 22:02:32,872 rendering of a template called success 34837 22:02:30,872 --> 22:02:34,512 .html let's just pretend for now that 34838 22:02:32,872 --> 22:02:36,872 registration is successful no matter who 34839 22:02:34,512 --> 22:02:39,720 you are or what you do now I need that 34840 22:02:36,872 --> 22:02:41,480 template and I only have index.html at 34841 22:02:39,720 --> 22:02:44,080 this point so let me actually now do my 34842 22:02:41,480 --> 22:02:47,232 best practices let me copy all of that 34843 22:02:44,080 --> 22:02:49,872 let me in a separate terminal window let 34844 22:02:47,232 --> 22:02:52,040 me do code uh let me go into my Frost 34845 22:02:49,872 --> 22:02:54,360 im's directory and let me create a new 34846 22:02:52,040 --> 22:02:57,280 template called layout. HTML just like 34847 22:02:54,360 --> 22:02:59,680 before let me paste all that same code 34848 22:02:57,280 --> 22:03:03,160 let me delete the form and just put in 34849 22:02:59,680 --> 22:03:06,832 that big placehold folder so block body 34850 22:03:03,160 --> 22:03:08,440 and then end block is all I did earlier 34851 22:03:06,832 --> 22:03:09,832 this is just kind of boiler plate now 34852 22:03:08,440 --> 22:03:11,040 convention everything else I'm going to 34853 22:03:09,832 --> 22:03:13,360 leave the same but if I wanted to make 34854 22:03:11,040 --> 22:03:15,160 it prettier I could add my CSS up top if 34855 22:03:13,360 --> 22:03:16,920 I wanted to add like this crazy 34856 22:03:15,160 --> 22:03:18,832 repeating background I could probably do 34857 22:03:16,920 --> 22:03:20,680 that up top too so I could make every 34858 22:03:18,832 --> 22:03:22,480 page look as ugly as it did back in my 34859 22:03:20,680 --> 22:03:24,720 day but we'll focus just today on the 34860 22:03:22,480 --> 22:03:28,680 text all right so now that I have 34861 22:03:24,720 --> 22:03:30,920 layout. HTML let me clean up index.html 34862 22:03:28,680 --> 22:03:32,872 I don't need all this redundancy I don't 34863 22:03:30,920 --> 22:03:36,160 need all of these tags at the top 34864 22:03:32,872 --> 22:03:40,000 instead recall I think I just need 34865 22:03:36,160 --> 22:03:42,080 extends quote unquote layout. HTML with 34866 22:03:40,000 --> 22:03:44,640 the appropriate percent signs and curly 34867 22:03:42,080 --> 22:03:46,400 braces I then have the appropriate block 34868 22:03:44,640 --> 22:03:47,872 body though I could call Body anything I 34869 22:03:46,400 --> 22:03:49,760 want but I'm going to stick with my 34870 22:03:47,872 --> 22:03:51,720 convention earlier and I'm going to 34871 22:03:49,760 --> 22:03:54,000 delete the tags down here that I no 34872 22:03:51,720 --> 22:03:56,600 longer need why because if I go into 34873 22:03:54,000 --> 22:03:59,040 layout. HTML I already have all my open 34874 22:03:56,600 --> 22:04:01,760 tags all my Clos tags the only stuff I 34875 22:03:59,040 --> 22:04:04,872 want in index.html is is going to be 34876 22:04:01,760 --> 22:04:07,280 which belongs in the body so end block 34877 22:04:04,872 --> 22:04:09,120 down here and just to be pedantic let me 34878 22:04:07,280 --> 22:04:11,680 go ahead and highlight all that hit 34879 22:04:09,120 --> 22:04:14,120 shift Tab and that will like unindent it 34880 22:04:11,680 --> 22:04:15,600 just to line things up just to be tidy 34881 22:04:14,120 --> 22:04:17,400 all right so better even though it looks 34882 22:04:15,600 --> 22:04:19,120 a little cryptic now but now I've laid 34883 22:04:17,400 --> 22:04:21,040 the foundation for making a third page a 34884 22:04:19,120 --> 22:04:23,192 fourth page that don't have all of that 34885 22:04:21,040 --> 22:04:26,400 same copy paste all right so now let's 34886 22:04:23,192 --> 22:04:28,160 go back into app.py success. HTML is 34887 22:04:26,400 --> 22:04:30,680 where I left off so okay let me open my 34888 22:04:28,160 --> 22:04:33,512 terminal window let me code up a 34889 22:04:30,680 --> 22:04:35,000 template called success. HTML whose 34890 22:04:33,512 --> 22:04:37,480 purpose in life is literally just going 34891 22:04:35,000 --> 22:04:39,120 to be like to say you are registered 34892 22:04:37,480 --> 22:04:41,232 just so that we see some informative 34893 22:04:39,120 --> 22:04:44,400 message on the screen so this part I do 34894 22:04:41,232 --> 22:04:46,920 still need extends layout. HTML so 34895 22:04:44,400 --> 22:04:49,192 there's a little bit of copy paste still 34896 22:04:46,920 --> 22:04:51,192 which is a little ugly but so be it 34897 22:04:49,192 --> 22:04:53,232 block body for this template and I'm 34898 22:04:51,192 --> 22:04:56,080 just going to say you are registered 34899 22:04:53,232 --> 22:04:58,400 exclamation point all right and then end 34900 22:04:56,080 --> 22:05:00,192 block so super simple it's just an 34901 22:04:58,400 --> 22:05:02,512 informative message claiming that the 34902 22:05:00,192 --> 22:05:04,720 Stu the student is registered all right 34903 22:05:02,512 --> 22:05:06,760 let's go back to the original form which 34904 22:05:04,720 --> 22:05:09,760 is this let me reload to make sure my 34905 22:05:06,760 --> 22:05:11,760 HTML has reloaded type in David I'm 34906 22:05:09,760 --> 22:05:15,040 going to register again for soccer and 34907 22:05:11,760 --> 22:05:18,080 click register and oh interesting method 34908 22:05:15,040 --> 22:05:20,040 not allowed so I'm not getting a 404 34909 22:05:18,080 --> 22:05:23,040 anymore I'm getting 34910 22:05:20,040 --> 22:05:25,832 405 at SL 34911 22:05:23,040 --> 22:05:28,832 register what's the deduction here how 34912 22:05:25,832 --> 22:05:28,832 did I screw up this 34913 22:05:28,872 --> 22:05:34,080 time 405 is progress yeah the 34914 22:05:31,920 --> 22:05:36,680 placeholder uh so it's not the 34915 22:05:34,080 --> 22:05:38,552 placeholder I think is okay this is now 34916 22:05:36,680 --> 22:05:42,120 about the underlying HTTP stuff the 34917 22:05:38,552 --> 22:05:45,552 method was disallow was not 34918 22:05:42,120 --> 22:05:47,480 allowed say again so get purposes post 34919 22:05:45,552 --> 22:05:49,832 thing too so by default all of these 34920 22:05:47,480 --> 22:05:51,232 routes in flask just by default assume 34921 22:05:49,832 --> 22:05:53,160 get because it's safe it doesn't allow 34922 22:05:51,232 --> 22:05:55,280 you to send information to the server in 34923 22:05:53,160 --> 22:05:57,480 quite the same way but if I do want to 34924 22:05:55,280 --> 22:05:59,872 support post recall that we changed this 34925 22:05:57,480 --> 22:06:01,720 to be methods equals and then a list 34926 22:05:59,872 --> 22:06:04,320 with quote quote Post in it so I just 34927 22:06:01,720 --> 22:06:06,552 need to enable support for that method 34928 22:06:04,320 --> 22:06:08,640 that that is that HTTP verb all right 34929 22:06:06,552 --> 22:06:09,960 let's go back to the form reload just to 34930 22:06:08,640 --> 22:06:12,120 make sure I haven't screwed up type in 34931 22:06:09,960 --> 22:06:15,120 my name David select soccer from the 34932 22:06:12,120 --> 22:06:18,120 dropdown click register and now I'm not 34933 22:06:15,120 --> 22:06:19,680 only at SL register in the URL it claims 34934 22:06:18,120 --> 22:06:20,872 that I am indeed registered now of 34935 22:06:19,680 --> 22:06:22,232 course I'm not I've done nothing 34936 22:06:20,872 --> 22:06:24,480 interesting there's no database there's 34937 22:06:22,232 --> 22:06:26,280 no CSV file we'll get to that in a bit 34938 22:06:24,480 --> 22:06:28,800 but at least I now have the plumbing in 34939 22:06:26,280 --> 22:06:31,552 place to do something Dynamic based on 34940 22:06:28,800 --> 22:06:34,832 that sport all right well how can I now 34941 22:06:31,552 --> 22:06:37,040 improve upon this how about we go ahead 34942 22:06:34,832 --> 22:06:38,800 and implement store the actual 34943 22:06:37,040 --> 22:06:40,160 registrant in a dictionary in the 34944 22:06:38,800 --> 22:06:41,832 computer's memory so instead of just 34945 22:06:40,160 --> 22:06:43,320 claiming that they're registered let's 34946 22:06:41,832 --> 22:06:44,960 actually make a notation and the 34947 22:06:43,320 --> 22:06:46,600 simplest way as we did weeks ago in 34948 22:06:44,960 --> 22:06:48,920 Python is just store things in like a 34949 22:06:46,600 --> 22:06:51,512 variable in memory like a list or a 34950 22:06:48,920 --> 22:06:53,040 dictionary a set anything like that all 34951 22:06:51,512 --> 22:06:55,320 right well let me go back into vs code 34952 22:06:53,040 --> 22:06:57,800 and in app.py and I think what I'm going 34953 22:06:55,320 --> 22:07:00,600 to have to do here is change my register 34954 22:06:57,800 --> 22:07:03,080 route to actually do some useful 34955 22:07:00,600 --> 22:07:05,600 information but before I register the 34956 22:07:03,080 --> 22:07:07,800 user let's consider where I want to 34957 22:07:05,600 --> 22:07:12,440 actually put them and so let me propose 34958 22:07:07,800 --> 22:07:14,832 that how about we do this at the top of 34959 22:07:12,440 --> 22:07:18,040 my file let me go ahead and declare a 34960 22:07:14,832 --> 22:07:21,552 global variable called registrant uh and 34961 22:07:18,040 --> 22:07:22,960 set that equal to an empty dictionary so 34962 22:07:21,552 --> 22:07:24,440 we've done this before when we were 34963 22:07:22,960 --> 22:07:26,280 playing around previously with using 34964 22:07:24,440 --> 22:07:27,400 dictionaries to store key value Pairs 34965 22:07:26,280 --> 22:07:29,600 and I'm going to propose that we store 34966 22:07:27,400 --> 22:07:30,760 the registrant as a dictionary why cuz 34967 22:07:29,600 --> 22:07:33,192 I'm going to keep it simple like the 34968 22:07:30,760 --> 22:07:34,360 name is going to be the student's name 34969 22:07:33,192 --> 22:07:35,800 sorry the key is going to be the 34970 22:07:34,360 --> 22:07:37,832 student's name and the value is going to 34971 22:07:35,800 --> 22:07:39,552 be whatever sport they registered for so 34972 22:07:37,832 --> 22:07:41,512 David and soccer and Carter and 34973 22:07:39,552 --> 22:07:43,512 basketball and so it kind of makes sense 34974 22:07:41,512 --> 22:07:46,080 for like a two column dictionary so to 34975 22:07:43,512 --> 22:07:47,800 speak as we often depict it on screen so 34976 22:07:46,080 --> 22:07:50,232 how can I use this dictionary well let 34977 22:07:47,800 --> 22:07:53,400 me go ahead and do this down here under 34978 22:07:50,232 --> 22:07:55,960 SL register let me go ahead and 34979 22:07:53,400 --> 22:07:59,760 initially do this how about we get the 34980 22:07:55,960 --> 22:08:01,552 user's name from request. form. Get and 34981 22:07:59,760 --> 22:08:02,640 set it equal to whatever the value of 34982 22:08:01,552 --> 22:08:03,872 name is and I'm not going to give a 34983 22:08:02,640 --> 22:08:06,040 default value now because I don't want 34984 22:08:03,872 --> 22:08:07,600 to call the student world or something 34985 22:08:06,040 --> 22:08:09,360 strange like that I'm just going to 34986 22:08:07,600 --> 22:08:10,680 assume for now that it's there let's 34987 22:08:09,360 --> 22:08:13,800 then create another variable called 34988 22:08:10,680 --> 22:08:15,832 Sport and do request. form doget quote 34989 22:08:13,800 --> 22:08:18,320 unquote sport to get these students 34990 22:08:15,832 --> 22:08:19,720 Sport and then let's go ahead and do 34991 22:08:18,320 --> 22:08:22,640 this in the 34992 22:08:19,720 --> 22:08:24,760 registrant dictionary let's index into 34993 22:08:22,640 --> 22:08:27,760 it using the student's name and let's 34994 22:08:24,760 --> 22:08:29,192 set it equal to whatever the sport is so 34995 22:08:27,760 --> 22:08:31,232 I've got these variables just to keep my 34996 22:08:29,192 --> 22:08:35,680 code tidy and I'm now putting a key 34997 22:08:31,232 --> 22:08:38,320 value pair in that uh into that 34998 22:08:35,680 --> 22:08:41,120 dictionary all right well what do I want 34999 22:08:38,320 --> 22:08:43,480 to now do and I'll I'll go ahead and say 35000 22:08:41,120 --> 22:08:45,600 success. HTML sure let's go ahead and do 35001 22:08:43,480 --> 22:08:47,680 that but now I think success. HTML means 35002 22:08:45,600 --> 22:08:52,680 that so let me go back to the form 35003 22:08:47,680 --> 22:08:55,960 reload let me type in David and soccer 35004 22:08:52,680 --> 22:09:01,400 register okay let me go back and say 35005 22:08:55,960 --> 22:09:03,760 Carter and basketball registered okay 35006 22:09:01,400 --> 22:09:05,760 now let's see what I want to do next how 35007 22:09:03,760 --> 22:09:08,552 about I go 35008 22:09:05,760 --> 22:09:11,640 into um Let me give myself another route 35009 22:09:08,552 --> 22:09:14,160 and let's play around here so app. route 35010 22:09:11,640 --> 22:09:16,400 let's give myself another third route 35011 22:09:14,160 --> 22:09:17,832 called registrant whose purpose in life 35012 22:09:16,400 --> 22:09:19,160 is just to show me who all of those 35013 22:09:17,832 --> 22:09:21,192 registrants are just like you would 35014 22:09:19,160 --> 22:09:22,440 expect from a website like this and then 35015 22:09:21,192 --> 22:09:25,040 let me Define a function called 35016 22:09:22,440 --> 22:09:27,480 registrants or anything else and then 35017 22:09:25,040 --> 22:09:29,160 let me return the rendering of a 35018 22:09:27,480 --> 22:09:33,552 template called 35019 22:09:29,160 --> 22:09:35,192 registrant H ML and let me pass in this 35020 22:09:33,552 --> 22:09:37,000 is kind of neat I can do registrant 35021 22:09:35,192 --> 22:09:39,440 equals registrant which again looks 35022 22:09:37,000 --> 22:09:41,320 weird but what am I doing I'm presuming 35023 22:09:39,440 --> 22:09:44,400 to pass in a placeholder called 35024 22:09:41,320 --> 22:09:46,320 registrant the value of which is this 35025 22:09:44,400 --> 22:09:48,552 dictionary that I've been collecting all 35026 22:09:46,320 --> 22:09:50,040 of the registrations in so similar to 35027 22:09:48,552 --> 22:09:51,720 the name placeholder before but it's a 35028 22:09:50,040 --> 22:09:53,320 little more powerful because now it's a 35029 22:09:51,720 --> 22:09:56,872 whole dictionary not just a single 35030 22:09:53,320 --> 22:09:59,160 string so I think now let me be creative 35031 22:09:56,872 --> 22:10:03,360 here let me go into my 35032 22:09:59,160 --> 22:10:06,832 templates under uh my templates folder 35033 22:10:03,360 --> 22:10:08,760 and let's do this uh let's go into uh my 35034 22:10:06,832 --> 22:10:11,760 terminal window let's create another 35035 22:10:08,760 --> 22:10:14,000 template called registrant HTML that's 35036 22:10:11,760 --> 22:10:16,680 actually going to do this displaying of 35037 22:10:14,000 --> 22:10:18,832 all of the registrant for us so extends 35038 22:10:16,680 --> 22:10:20,320 layout. HTML just so I can borrow all of 35039 22:10:18,832 --> 22:10:23,512 the same HTML as 35040 22:10:20,320 --> 22:10:26,800 before and let's define block body just 35041 22:10:23,512 --> 22:10:28,480 like before and inside of this and block 35042 22:10:26,800 --> 22:10:30,512 I want to put I don't know like a 35043 22:10:28,480 --> 22:10:32,360 bulleted list or an ordered list list of 35044 22:10:30,512 --> 22:10:35,720 all of the registrant so how can I do 35045 22:10:32,360 --> 22:10:39,232 this well let's do uh an unordered list 35046 22:10:35,720 --> 22:10:41,552 ul and here's where Ginger and flask 35047 22:10:39,232 --> 22:10:43,720 more generally get kind of interesting 35048 22:10:41,552 --> 22:10:44,832 like I want there to be something like 35049 22:10:43,720 --> 22:10:49,720 this An 35050 22:10:44,832 --> 22:10:53,040 Li uh and then like the students's name 35051 22:10:49,720 --> 22:10:55,480 and then an L uh maybe Li yeah like that 35052 22:10:53,040 --> 22:10:57,920 and then maybe like sport something like 35053 22:10:55,480 --> 22:11:00,120 this but I didn't pass in a name I 35054 22:10:57,920 --> 22:11:02,760 didn't pass in a sport I passed in the 35055 22:11:00,120 --> 22:11:03,960 entire dictionary of registrants now in 35056 22:11:02,760 --> 22:11:06,120 Python if we were just doing something 35057 22:11:03,960 --> 22:11:08,120 at the black and white terminal window 35058 22:11:06,120 --> 22:11:09,360 and doing a command line program you 35059 22:11:08,120 --> 22:11:12,320 know I'd probably have some kind of for 35060 22:11:09,360 --> 22:11:14,440 Loop in Python Ginger does allow you to 35061 22:11:12,320 --> 22:11:16,080 do this so a templating language tends 35062 22:11:14,440 --> 22:11:18,160 to come with very lightweight mechanisms 35063 22:11:16,080 --> 22:11:20,872 for doing placeholders doing Simple 35064 22:11:18,160 --> 22:11:23,440 Loops doing Simple conditions so python 35065 22:11:20,872 --> 22:11:25,360 like syntax and it's almost identical so 35066 22:11:23,440 --> 22:11:27,872 watch what I can do inside of this 35067 22:11:25,360 --> 22:11:30,440 unordered list let me not start to 35068 22:11:27,872 --> 22:11:33,360 manually output a single Li let me use 35069 22:11:30,440 --> 22:11:36,040 this syntax the same Ginger syntax that 35070 22:11:33,360 --> 22:11:38,720 I used for Block so curly brace percent 35071 22:11:36,040 --> 22:11:41,232 sign and I'm going to say this for name 35072 22:11:38,720 --> 22:11:43,360 in registrant so this is just like 35073 22:11:41,232 --> 22:11:45,320 python Syntax for iterating over a 35074 22:11:43,360 --> 22:11:48,000 dictionary and now this is going to look 35075 22:11:45,320 --> 22:11:50,512 stupid but the opposite of that is end4 35076 22:11:48,000 --> 22:11:52,800 so in HTML you use the slash in ginger 35077 22:11:50,512 --> 22:11:54,960 you literally use the word end no space 35078 22:11:52,800 --> 22:11:57,120 and then the name of the keyword so end4 35079 22:11:54,960 --> 22:11:59,640 is how you close this but this is where 35080 22:11:57,120 --> 22:12:03,280 templating gets really cool you can now 35081 22:11:59,640 --> 22:12:06,192 do Li and in here I can do something 35082 22:12:03,280 --> 22:12:08,232 like that student's name and that's it 35083 22:12:06,192 --> 22:12:10,320 I'm going to leave it like that and what 35084 22:12:08,232 --> 22:12:13,160 I'm doing here is using really a 35085 22:12:10,320 --> 22:12:15,400 template as templates are intended I've 35086 22:12:13,160 --> 22:12:17,360 got like the basic building blocks of 35087 22:12:15,400 --> 22:12:19,320 what I want this output to look like but 35088 22:12:17,360 --> 22:12:21,360 thanks to this little for Loop here 35089 22:12:19,320 --> 22:12:23,160 thanks to Ginger syntax the curly brace 35090 22:12:21,360 --> 22:12:24,832 and the percent sign I'm going to 35091 22:12:23,160 --> 22:12:27,160 iterate over every dictionary printing 35092 22:12:24,832 --> 22:12:28,760 out name name name name and so if I've 35093 22:12:27,160 --> 22:12:31,360 got two kids registered now I'm going to 35094 22:12:28,760 --> 22:12:34,320 see two liis David and Carter 35095 22:12:31,360 --> 22:12:39,720 respectively so let's see let me go back 35096 22:12:34,320 --> 22:12:41,160 to uh my Frost IM tab here and I don't 35097 22:12:39,720 --> 22:12:43,800 have a link yet so I got to do this 35098 22:12:41,160 --> 22:12:47,080 manually uh like a developer would let 35099 22:12:43,800 --> 22:12:48,600 me go to slash registrant and I'll zoom 35100 22:12:47,080 --> 22:12:50,512 out and hit 35101 22:12:48,600 --> 22:12:52,480 enter and you'll see what you'll 35102 22:12:50,512 --> 22:12:54,680 probably see two when making mistakes 35103 22:12:52,480 --> 22:12:56,232 for the first time in this world so 35104 22:12:54,680 --> 22:12:58,400 where is the error message unfortunately 35105 22:12:56,232 --> 22:13:00,280 internal Ser server error is not all 35106 22:12:58,400 --> 22:13:02,160 that useful but we do tell you you see 35107 22:13:00,280 --> 22:13:04,280 terminal window so if I go to the 35108 22:13:02,160 --> 22:13:05,760 terminal window I haven't been paying 35109 22:13:04,280 --> 22:13:07,360 attention to this for quite some time 35110 22:13:05,760 --> 22:13:09,120 and in fact I have two terminal windows 35111 22:13:07,360 --> 22:13:11,160 open so that I can still use commands at 35112 22:13:09,120 --> 22:13:13,760 the prompt but if I go back to my first 35113 22:13:11,160 --> 22:13:15,512 terminal window AKA bash there you'll 35114 22:13:13,760 --> 22:13:17,872 see in your terminal window When 35115 22:13:15,512 --> 22:13:20,160 developing web applications like all of 35116 22:13:17,872 --> 22:13:21,400 the mistakes you made in the terminal 35117 22:13:20,160 --> 22:13:23,160 itself this is one of those python 35118 22:13:21,400 --> 22:13:25,800 tracebacks that's related to me screwing 35119 22:13:23,160 --> 22:13:29,040 up here now let me go ahead here and 35120 22:13:25,800 --> 22:13:30,600 let's see uh type error function is not 35121 22:13:29,040 --> 22:13:35,360 iterable 35122 22:13:30,600 --> 22:13:35,360 and block for name function is not 35123 22:13:35,400 --> 22:13:40,360 iterable all right so what mistake did I 35124 22:13:38,280 --> 22:13:41,872 make well this is what happens when I 35125 22:13:40,360 --> 22:13:44,280 don't follow my notes and make changes 35126 22:13:41,872 --> 22:13:46,512 on the fly so I have this variable on 35127 22:13:44,280 --> 22:13:48,720 line five called registrant in all lower 35128 22:13:46,512 --> 22:13:51,600 case but what did I then do on the Fly 35129 22:13:48,720 --> 22:13:51,600 here in line 35130 22:13:52,232 --> 22:13:57,000 22 I defined a function called 35131 22:13:54,720 --> 22:13:58,400 registrant so like newbie mistake like I 35132 22:13:57,000 --> 22:13:59,832 shouldn't have done this I can't have a 35133 22:13:58,400 --> 22:14:01,400 variable and a function of the same name 35134 22:13:59,832 --> 22:14:03,440 name because the symbols are literally 35135 22:14:01,400 --> 22:14:05,280 identical so just to make clear that 35136 22:14:03,440 --> 22:14:07,832 this variable up here is actually Global 35137 22:14:05,280 --> 22:14:10,080 we'll use our convention like we did in 35138 22:14:07,832 --> 22:14:11,800 C often when we had a global variable 35139 22:14:10,080 --> 22:14:14,320 we'll capitalize it all just to make it 35140 22:14:11,800 --> 22:14:16,232 stand out like a a constant value up 35141 22:14:14,320 --> 22:14:19,400 there and so down here what I'm going to 35142 22:14:16,232 --> 22:14:21,040 do is pass in registrants in all cap so 35143 22:14:19,400 --> 22:14:22,832 that was stupid didn't mean to confuse 35144 22:14:21,040 --> 22:14:24,120 there but the reason for that error to 35145 22:14:22,832 --> 22:14:25,760 be clear is that you can't have a 35146 22:14:24,120 --> 22:14:26,920 function that's the same name as a 35147 22:14:25,760 --> 22:14:28,120 variable I could just change the 35148 22:14:26,920 --> 22:14:29,360 variable name alt together I'm going to 35149 22:14:28,120 --> 22:14:31,280 go ahead and just capitalize it to make 35150 22:14:29,360 --> 22:14:33,832 it really stand out that this is in fact 35151 22:14:31,280 --> 22:14:36,160 a global variable up top all right now 35152 22:14:33,832 --> 22:14:39,400 I'm going to go back to my browser let's 35153 22:14:36,160 --> 22:14:39,400 do David and 35154 22:14:40,552 --> 22:14:45,360 socer all right but there's going to be 35155 22:14:42,720 --> 22:14:47,552 some other mistakes here so on line 17 35156 22:14:45,360 --> 22:14:49,400 let me go ahead and change this variable 35157 22:14:47,552 --> 22:14:51,760 to be capitalized there because indeed I 35158 22:14:49,400 --> 22:14:53,800 want to put the key and the value in 35159 22:14:51,760 --> 22:14:55,960 this newly named variable as all 35160 22:14:53,800 --> 22:14:57,832 capitals registrant let me now go back 35161 22:14:55,960 --> 22:15:00,080 to vs code here let me go back to the 35162 22:14:57,832 --> 22:15:03,512 form and let me start adding some data 35163 22:15:00,080 --> 22:15:05,512 fresh let me register David for soccer 35164 22:15:03,512 --> 22:15:07,280 clicking register now and we should see 35165 22:15:05,512 --> 22:15:09,320 you are registered but hopefully now 35166 22:15:07,280 --> 22:15:12,552 it's indeed in the computer's memory let 35167 22:15:09,320 --> 22:15:15,160 me go back and register now Carter for 35168 22:15:12,552 --> 22:15:17,192 basketball clicking register again and 35169 22:15:15,160 --> 22:15:20,120 hopefully it's now registered if I now 35170 22:15:17,192 --> 22:15:22,192 change my route manually to B SL 35171 22:15:20,120 --> 22:15:24,800 registrant which is this newly added 35172 22:15:22,192 --> 22:15:27,760 route that I made and hit enter now I 35173 22:15:24,800 --> 22:15:29,680 see thank God now I see the unordered 35174 22:15:27,760 --> 22:15:31,600 list containing everything in the 35175 22:15:29,680 --> 22:15:33,720 computer's memory so when I say you are 35176 22:15:31,600 --> 22:15:35,552 registered I kind of mean it now because 35177 22:15:33,720 --> 22:15:38,080 the server is still running and in the 35178 22:15:35,552 --> 22:15:40,552 computer's memory is in this registrant 35179 22:15:38,080 --> 22:15:42,232 Global variable a dictionary of key 35180 22:15:40,552 --> 22:15:44,552 value pairs of course we're only seeing 35181 22:15:42,232 --> 22:15:47,040 the keys at the moment so it might be 35182 22:15:44,552 --> 22:15:50,760 nice to actually see the values in as 35183 22:15:47,040 --> 22:15:53,720 well so let me go back to VSS code and 35184 22:15:50,760 --> 22:15:55,160 let me go into registr trans. HTML and 35185 22:15:53,720 --> 22:15:57,872 I'll just do something a little messy 35186 22:15:55,160 --> 22:16:00,320 I'll just say uh how about let just make 35187 22:15:57,872 --> 22:16:03,640 it a sentence is registered 35188 22:16:00,320 --> 22:16:06,832 four and now another placeholder I'm 35189 22:16:03,640 --> 22:16:09,800 going to say registrant bracket name so 35190 22:16:06,832 --> 22:16:12,832 just like in Python if registr is itself 35191 22:16:09,800 --> 22:16:15,120 a dictionary registr bracket and then 35192 22:16:12,832 --> 22:16:17,872 the key you want to index into is 35193 22:16:15,120 --> 22:16:21,160 perfectly valid syntax as well so now 35194 22:16:17,872 --> 22:16:23,800 let me go back to SL registrant let me 35195 22:16:21,160 --> 22:16:26,000 click reload again so why isn't it 35196 22:16:23,800 --> 22:16:28,080 working everyone what's the bug that I 35197 22:16:26,000 --> 22:16:29,600 introduced earlier if David is 35198 22:16:28,080 --> 22:16:31,192 registered for none and Carter is 35199 22:16:29,600 --> 22:16:33,760 registered for none but David and Carter 35200 22:16:31,192 --> 22:16:36,720 are in the dictionary like that's a good 35201 22:16:33,760 --> 22:16:38,872 thing so some of the data is in there so 35202 22:16:36,720 --> 22:16:40,360 why are there no Sports Associated well 35203 22:16:38,872 --> 22:16:42,440 the first thing I literally just did in 35204 22:16:40,360 --> 22:16:45,000 front of you all was I went to app.py 35205 22:16:42,440 --> 22:16:47,080 and I stared at line 17 thinking like 35206 22:16:45,000 --> 22:16:49,400 how did I screw this up I'm putting 35207 22:16:47,080 --> 22:16:51,400 sport as the value of the key which is 35208 22:16:49,400 --> 22:16:53,192 the student's name all right line 17 35209 22:16:51,400 --> 22:16:55,760 looked fine to me a few seconds ago so I 35210 22:16:53,192 --> 22:16:57,400 looked then with my eyes at line 16 and 35211 22:16:55,760 --> 22:16:59,920 this too looked okay my first thought 35212 22:16:57,400 --> 22:17:02,080 was oh did I use request. args instead 35213 22:16:59,920 --> 22:17:04,080 of request. form instead because that 35214 22:17:02,080 --> 22:17:06,600 would have assumed get instead of post 35215 22:17:04,080 --> 22:17:08,872 but no like that looks okay too so then 35216 22:17:06,600 --> 22:17:11,040 my final Instinct was oh my god did I 35217 22:17:08,872 --> 22:17:13,440 screw up the HTML form and so that's why 35218 22:17:11,040 --> 22:17:16,800 I went back over to my tab here I went 35219 22:17:13,440 --> 22:17:19,552 to the original form here I then view 35220 22:17:16,800 --> 22:17:20,960 page source and this might not be as 35221 22:17:19,552 --> 22:17:24,080 obvious to you if you've never seen the 35222 22:17:20,960 --> 22:17:27,360 select menu before what is apparently 35223 22:17:24,080 --> 22:17:30,192 missing here that might explain my 35224 22:17:27,360 --> 22:17:32,960 mistake yeah 35225 22:17:30,192 --> 22:17:34,720 yeah I didn't name this form field quote 35226 22:17:32,960 --> 22:17:36,640 unquote sport now to be fair you haven't 35227 22:17:34,720 --> 22:17:38,400 seen me do this as a select menu before 35228 22:17:36,640 --> 22:17:40,080 and it's different from this input when 35229 22:17:38,400 --> 22:17:43,440 you have an input tag you literally say 35230 22:17:40,080 --> 22:17:44,920 name equals whatever on the input tag it 35231 22:17:43,440 --> 22:17:46,232 turns out I don't know why I skipped 35232 22:17:44,920 --> 22:17:48,320 this earlier I probably meant to come 35233 22:17:46,232 --> 22:17:50,920 back to it the select tag also can take 35234 22:17:48,320 --> 22:17:51,960 a name parameter so if I go back to the 35235 22:17:50,920 --> 22:17:54,160 name 35236 22:17:51,960 --> 22:17:56,512 parameter here and go back and add the 35237 22:17:54,160 --> 22:17:57,960 name parameter let me go into that 35238 22:17:56,512 --> 22:18:00,720 template which is 35239 22:17:57,960 --> 22:18:03,360 index.html let me add name equals quote 35240 22:18:00,720 --> 22:18:05,832 unquote sport in all lowercase which is 35241 22:18:03,360 --> 22:18:08,000 different from the visual aesthetic of 35242 22:18:05,832 --> 22:18:09,360 this temporary disabled option that's 35243 22:18:08,000 --> 22:18:13,320 just there to make things prettier for 35244 22:18:09,360 --> 22:18:15,552 the human now let me go ahead here and 35245 22:18:13,320 --> 22:18:17,400 first I'm going to go into my terminal 35246 22:18:15,552 --> 22:18:19,360 window and I'm actually going to hit 35247 22:18:17,400 --> 22:18:20,832 control C to stop the server alt 35248 22:18:19,360 --> 22:18:22,640 together because I want to throw away 35249 22:18:20,832 --> 22:18:24,160 the contents of memory and therefore get 35250 22:18:22,640 --> 22:18:26,320 rid of that dictionary that had David 35251 22:18:24,160 --> 22:18:27,832 and Carter and those nun values so this 35252 22:18:26,320 --> 22:18:31,192 is sort of me clearing the computer's 35253 22:18:27,832 --> 22:18:33,040 memory I'm going to rerun flask run I 35254 22:18:31,192 --> 22:18:35,040 get that same URL as before so I'm going 35255 22:18:33,040 --> 22:18:36,512 to hover over that and open the new tab 35256 22:18:35,040 --> 22:18:39,552 and just to be sure I'm going to do view 35257 22:18:36,512 --> 22:18:42,552 page source and here I see now okay now 35258 22:18:39,552 --> 22:18:44,232 the form has both a name and a sport in 35259 22:18:42,552 --> 22:18:46,280 it all right now I'm really going to 35260 22:18:44,232 --> 22:18:48,760 cross my fingers because I intend for 35261 22:18:46,280 --> 22:18:51,720 this now to work David will register 35262 22:18:48,760 --> 22:18:53,160 again for soccer register claims we are 35263 22:18:51,720 --> 22:18:55,832 registered I'm going to go back and do 35264 22:18:53,160 --> 22:18:57,872 it again for Carter and basketball 35265 22:18:55,832 --> 22:19:01,040 register we still don't have a link so 35266 22:18:57,872 --> 22:19:03,832 I'm going to manually go up to the URL 35267 22:19:01,040 --> 22:19:06,920 and change/ register to registrant as 35268 22:19:03,832 --> 22:19:09,160 before zooming out and hit enter and 35269 22:19:06,920 --> 22:19:13,320 thank God now I'm actually registered 35270 22:19:09,160 --> 22:19:13,320 properly for this so oh thank 35271 22:19:15,040 --> 22:19:18,800 you so what is it like 20 years later 35272 22:19:17,440 --> 22:19:22,000 I'm still struggling to implement this 35273 22:19:18,800 --> 22:19:24,280 site okay so um so here now we have for 35274 22:19:22,000 --> 22:19:25,512 the first time in Python and web stuff 35275 22:19:24,280 --> 22:19:27,080 like now we have a proper web 35276 22:19:25,512 --> 22:19:29,360 application and it's not just echoing 35277 22:19:27,080 --> 22:19:30,720 back hello David hello Carter this could 35278 22:19:29,360 --> 22:19:32,480 now work for any of you and it's 35279 22:19:30,720 --> 22:19:34,160 currently served privately but if I made 35280 22:19:32,480 --> 22:19:36,400 this URL public I could put this on the 35281 22:19:34,160 --> 22:19:39,280 web now and let anyone in the world 35282 22:19:36,400 --> 22:19:41,800 register but there's kind of some issues 35283 22:19:39,280 --> 22:19:43,640 here there's some security flaws 35284 22:19:41,800 --> 22:19:46,280 potentially and so for instance let me 35285 22:19:43,640 --> 22:19:48,760 go back to the web form here and let me 35286 22:19:46,280 --> 22:19:51,160 open up the inspect tab the developer 35287 22:19:48,760 --> 22:19:52,960 tools and just remind you that anyone on 35288 22:19:51,160 --> 22:19:55,440 the internet not only you the developer 35289 22:19:52,960 --> 22:19:57,760 but a an adversary can see all of your 35290 22:19:55,440 --> 22:19:59,800 HTML see all of your CSS see all of your 35291 22:19:57,760 --> 22:20:02,360 JavaScript but more importantly because 35292 22:19:59,800 --> 22:20:04,360 this is all client side in the browser 35293 22:20:02,360 --> 22:20:07,120 there is literally nothing technically 35294 22:20:04,360 --> 22:20:08,872 stopping them from changing the HTML or 35295 22:20:07,120 --> 22:20:10,600 at least their copy of it and I did that 35296 22:20:08,872 --> 22:20:12,320 last week with Yale I changed their 35297 22:20:10,600 --> 22:20:14,192 website but no I changed my copy of 35298 22:20:12,320 --> 22:20:16,400 their website but when forms get 35299 22:20:14,192 --> 22:20:18,960 involved you could maybe be actually 35300 22:20:16,400 --> 22:20:22,120 malicious now because even though this 35301 22:20:18,960 --> 22:20:24,640 drop-down menu only has basketball 35302 22:20:22,120 --> 22:20:28,832 soccer and Ultimate Frisbee suppose I 35303 22:20:24,640 --> 22:20:32,480 really want to register for how about uh 35304 22:20:28,832 --> 22:20:34,160 let's say uh name your favorite sport 35305 22:20:32,480 --> 22:20:36,480 volleyball we really want to register 35306 22:20:34,160 --> 22:20:38,552 for volleyball but like this website 35307 22:20:36,480 --> 22:20:39,832 won't let me well there's nothing 35308 22:20:38,552 --> 22:20:42,232 stopping me from going under the 35309 22:20:39,832 --> 22:20:44,920 elements tab in my browser going into 35310 22:20:42,232 --> 22:20:47,192 this select menu here and you know what 35311 22:20:44,920 --> 22:20:50,512 no one ultimate for me let's change this 35312 22:20:47,192 --> 22:20:53,552 to volleyball and let's change this to 35313 22:20:50,512 --> 22:20:56,800 volleyball enter I'm going to close the 35314 22:20:53,552 --> 22:21:00,160 inspector now and as requested now we 35315 22:20:56,800 --> 22:21:01,832 support volleyball in the form now now 35316 22:21:00,160 --> 22:21:04,440 it's not changed on the server to be 35317 22:21:01,832 --> 22:21:06,920 fair but think about how HTTP works when 35318 22:21:04,440 --> 22:21:09,160 I fill out this with uh say let's see 35319 22:21:06,920 --> 22:21:11,232 Bernie's name Bernie really wants to 35320 22:21:09,160 --> 22:21:13,360 register for volleyball as well at the 35321 22:21:11,232 --> 22:21:16,440 moment my code is just going to trust 35322 22:21:13,360 --> 22:21:19,120 that what's in request. form is what was 35323 22:21:16,440 --> 22:21:21,320 in the original form itself no matter 35324 22:21:19,120 --> 22:21:23,640 whether the human adversarially actually 35325 22:21:21,320 --> 22:21:25,640 changed it so if I actually submit this 35326 22:21:23,640 --> 22:21:27,800 form and click register for Bernie and 35327 22:21:25,640 --> 22:21:30,360 volleyball even though that's not one of 35328 22:21:27,800 --> 22:21:33,192 the supported available Sports if I now 35329 22:21:30,360 --> 22:21:35,440 go to registrant my website nonetheless 35330 22:21:33,192 --> 22:21:37,512 has trusted that uh Bernie and perhaps 35331 22:21:35,440 --> 22:21:39,120 you are registered for volleyball so 35332 22:21:37,512 --> 22:21:40,920 what's the implication of this this has 35333 22:21:39,120 --> 22:21:43,480 surely happened in the past when like 35334 22:21:40,920 --> 22:21:46,120 really poorly implemented websites um 35335 22:21:43,480 --> 22:21:47,872 allow you to uh specify the price of an 35336 22:21:46,120 --> 22:21:49,360 item for instance in your shopping cart 35337 22:21:47,872 --> 22:21:52,512 and they just trust that when you click 35338 22:21:49,360 --> 22:21:54,600 submit or add to cart it adds the price 35339 22:21:52,512 --> 22:21:56,600 to the backend server if you're not 35340 22:21:54,600 --> 22:21:58,480 validating the price and making sure as 35341 22:21:56,600 --> 22:21:59,832 with a database that wait a minute that 35342 22:21:58,480 --> 22:22:02,192 price is valid valid or wait a minute 35343 22:21:59,832 --> 22:22:03,680 those sports are valid like who knows 35344 22:22:02,192 --> 22:22:06,640 what people are going to do to your site 35345 22:22:03,680 --> 22:22:08,832 and it's that simple to actually hack a 35346 22:22:06,640 --> 22:22:11,320 website accordingly now we can very 35347 22:22:08,832 --> 22:22:13,440 easily fix this with some um some week 35348 22:22:11,320 --> 22:22:15,320 six style python we really just need to 35349 22:22:13,440 --> 22:22:18,480 do a bit of logic here and so let me 35350 22:22:15,320 --> 22:22:21,120 propose this let me go into app.py here 35351 22:22:18,480 --> 22:22:24,480 and at the very top let me also create 35352 22:22:21,120 --> 22:22:26,512 how about a uh Global variable called 35353 22:22:24,480 --> 22:22:28,512 Sports in all caps and I'm going to set 35354 22:22:26,512 --> 22:22:30,192 that equal to in square brackets the 35355 22:22:28,512 --> 22:22:31,800 list of Sports I actually want to 35356 22:22:30,192 --> 22:22:33,600 support so I'm going to put in 35357 22:22:31,800 --> 22:22:36,080 basketball here I'm going to put in 35358 22:22:33,600 --> 22:22:37,872 soccer here and I'm sorry no volleyball 35359 22:22:36,080 --> 22:22:39,872 officially I'm going to put in ultimate 35360 22:22:37,872 --> 22:22:42,640 frisbee here so I've got this Global 35361 22:22:39,872 --> 22:22:44,760 list of supported Sports now think about 35362 22:22:42,640 --> 22:22:46,920 how I made this form a while ago I just 35363 22:22:44,760 --> 22:22:49,320 hardcoded these Sports here well I don't 35364 22:22:46,920 --> 22:22:51,920 have to do that I can sort of draw upon 35365 22:22:49,320 --> 22:22:54,120 my own official list of sports instead 35366 22:22:51,920 --> 22:22:56,872 so let me scroll down to my 35367 22:22:54,120 --> 22:22:58,720 index.html rendering template here let 35368 22:22:56,872 --> 22:23:00,360 me say that the sports I want to support 35369 22:22:58,720 --> 22:23:03,040 are are these so just using the same 35370 22:23:00,360 --> 22:23:04,760 placeholder trick as before but I'm now 35371 22:23:03,040 --> 22:23:08,120 telling the template what sports we 35372 22:23:04,760 --> 22:23:10,600 currently support now if I go back into 35373 22:23:08,120 --> 22:23:12,552 index.html I don't have to manually do 35374 22:23:10,600 --> 22:23:14,280 any of this let me get rid of all three 35375 22:23:12,552 --> 22:23:16,720 of those options which I manually 35376 22:23:14,280 --> 22:23:20,160 inputed earlier let me use my new trick 35377 22:23:16,720 --> 22:23:22,600 with ginger syntax and say for sport in 35378 22:23:20,160 --> 22:23:24,720 sports then let me proactively say end 35379 22:23:22,600 --> 22:23:28,192 for just to finish that thought and then 35380 22:23:24,720 --> 22:23:30,960 in here let me do option value equals in 35381 22:23:28,192 --> 22:23:33,000 curly BRAC is Sport and then so that the 35382 22:23:30,960 --> 22:23:35,832 human also sees the same words I'm going 35383 22:23:33,000 --> 22:23:38,080 to say sport out here so I've completely 35384 22:23:35,832 --> 22:23:39,920 changed what was hardcoded manually 35385 22:23:38,080 --> 22:23:42,232 typed to something now that's completely 35386 22:23:39,920 --> 22:23:44,160 Dynamic so now it's not going to stop 35387 22:23:42,232 --> 22:23:46,832 someone adversar like me from changing 35388 22:23:44,160 --> 22:23:50,480 the HTML but watch this the behavior on 35389 22:23:46,832 --> 22:23:52,320 the form if we go back is still now the 35390 22:23:50,480 --> 22:23:54,232 same drop down as before so 35391 22:23:52,320 --> 22:23:55,872 aesthetically it looks the same but you 35392 22:23:54,232 --> 22:23:58,600 know what why don't we be clever now and 35393 22:23:55,872 --> 22:24:02,320 let's go into app.py and the/ register 35394 22:23:58,600 --> 22:24:07,320 route and why don't we say this uh if 35395 22:24:02,320 --> 22:24:12,320 how about sport not in sports then let's 35396 22:24:07,320 --> 22:24:14,040 return render template uh failure. HTML 35397 22:24:12,320 --> 22:24:15,800 now this template doesn't exist yet so 35398 22:24:14,040 --> 22:24:18,040 let me just quickly make this real fast 35399 22:24:15,800 --> 22:24:22,360 I'm going to copy that code from before 35400 22:24:18,040 --> 22:24:24,440 let me create a code file in uh failure. 35401 22:24:22,360 --> 22:24:26,160 HTML I'm just going to paste this here 35402 22:24:24,440 --> 22:24:27,552 so I have a super simple error message 35403 22:24:26,160 --> 22:24:29,552 and I'm going to say you are not 35404 22:24:27,552 --> 22:24:32,440 registered just to that's what we mean 35405 22:24:29,552 --> 22:24:35,232 by failure and now in app.py consider 35406 22:24:32,440 --> 22:24:38,552 what logic I've added Sports in all caps 35407 22:24:35,232 --> 22:24:41,960 on line 22 is that same Global list as 35408 22:24:38,552 --> 22:24:44,280 before by asking pythonic if sport not 35409 22:24:41,960 --> 22:24:46,192 in sports well then you hacked me like 35410 22:24:44,280 --> 22:24:49,920 you tried to inject volleyball or some 35411 22:24:46,192 --> 22:24:51,960 other sport into request. form so I'm 35412 22:24:49,920 --> 22:24:54,512 just going to say no failure not letting 35413 22:24:51,960 --> 22:24:57,960 you register and I can do this a little 35414 22:24:54,512 --> 22:25:00,280 more uh verbosely too why don't I also 35415 22:24:57,960 --> 22:25:03,080 say this if if not name so if the name 35416 22:25:00,280 --> 22:25:06,552 is blank let's similarly return a render 35417 22:25:03,080 --> 22:25:07,872 template of failure. HTML in other words 35418 22:25:06,552 --> 22:25:09,480 if you didn't give me a name you left it 35419 22:25:07,872 --> 22:25:11,600 blank that's not useful for me running 35420 22:25:09,480 --> 22:25:13,760 the sports program let's also consider 35421 22:25:11,600 --> 22:25:15,832 that to be a failure so if I go back to 35422 22:25:13,760 --> 22:25:17,800 this tab now I'm going to reload just to 35423 22:25:15,832 --> 22:25:20,000 make sure I have the latest client side 35424 22:25:17,800 --> 22:25:22,160 let me be lazy and just click register 35425 22:25:20,000 --> 22:25:24,192 enter you are not registered because I 35426 22:25:22,160 --> 22:25:26,080 didn't give it an actual name all right 35427 22:25:24,192 --> 22:25:27,832 well let's go back how about I now type 35428 22:25:26,080 --> 22:25:31,080 David but no no I'm not going to choose 35429 22:25:27,832 --> 22:25:33,232 a sport I just want register myself nope 35430 22:25:31,080 --> 22:25:37,040 that did not work now let me go ahead 35431 22:25:33,232 --> 22:25:39,640 here and choose soccer this I think does 35432 22:25:37,040 --> 22:25:42,480 work let me go back now and try this 35433 22:25:39,640 --> 22:25:46,160 hacker trick whereby I go into the drop- 35434 22:25:42,480 --> 22:25:48,640 down menu I go into the select menu I 35435 22:25:46,160 --> 22:25:50,720 change as before Ultimate Frisbee to 35436 22:25:48,640 --> 22:25:52,040 volleyball and I'll change this one here 35437 22:25:50,720 --> 22:25:55,512 to 35438 22:25:52,040 --> 22:25:57,920 volleyball let me close the tab now this 35439 22:25:55,512 --> 22:26:00,680 looks like it's available now but when I 35440 22:25:57,920 --> 22:26:03,120 click register this this time it says 35441 22:26:00,680 --> 22:26:05,000 you are not registered and this is much 35442 22:26:03,120 --> 22:26:07,000 better than relying on other techniques 35443 22:26:05,000 --> 22:26:08,920 you might see or have seen online with 35444 22:26:07,000 --> 22:26:11,232 regard to HTML because there's also this 35445 22:26:08,920 --> 22:26:13,600 trick let me go back to the screen here 35446 22:26:11,232 --> 22:26:14,872 let me go back to index.html and you 35447 22:26:13,600 --> 22:26:16,320 might have seen online or you might 35448 22:26:14,872 --> 22:26:18,640 eventually see online that there's other 35449 22:26:16,320 --> 22:26:20,440 attributes you can use like required you 35450 22:26:18,640 --> 22:26:22,400 can literally tell the browser uhuh this 35451 22:26:20,440 --> 22:26:24,640 field is required you cannot leave it 35452 22:26:22,400 --> 22:26:27,320 blank if I go back to the browser now 35453 22:26:24,640 --> 22:26:29,160 reload and I again presume to be lazy 35454 22:26:27,320 --> 22:26:31,160 and I don't type in any name and click 35455 22:26:29,160 --> 22:26:33,192 register okay so that's kind of nice 35456 22:26:31,160 --> 22:26:35,960 like now the browser is being a little 35457 22:26:33,192 --> 22:26:38,120 more helpful for me saying no no no this 35458 22:26:35,960 --> 22:26:39,920 is required you have to fill this out 35459 22:26:38,120 --> 22:26:42,080 but again if you know what you're doing 35460 22:26:39,920 --> 22:26:44,872 okay well I disagree with your requiring 35461 22:26:42,080 --> 22:26:46,960 a name of me let me go in here let me go 35462 22:26:44,872 --> 22:26:51,040 over to this tag let me delete the 35463 22:26:46,960 --> 22:26:52,920 required attribute and now I slip 35464 22:26:51,040 --> 22:26:54,440 through but I didn't slip through on the 35465 22:26:52,920 --> 22:26:56,040 server and so there's a difference here 35466 22:26:54,440 --> 22:26:57,360 and an important distinction and so many 35467 22:26:56,040 --> 22:27:00,080 people in the real world still screw 35468 22:26:57,360 --> 22:27:01,640 this up there's client side validation 35469 22:27:00,080 --> 22:27:03,192 like actually checking that the data is 35470 22:27:01,640 --> 22:27:05,552 as you expect on the client side the 35471 22:27:03,192 --> 22:27:07,280 browser and there server side validation 35472 22:27:05,552 --> 22:27:08,800 and even though client side validation 35473 22:27:07,280 --> 22:27:10,232 like adding that required attribute 35474 22:27:08,800 --> 22:27:11,680 makes things more user friendly right 35475 22:27:10,232 --> 22:27:12,920 like that was a pretty little popup it 35476 22:27:11,680 --> 22:27:15,192 tells me that it's required it just 35477 22:27:12,920 --> 22:27:18,120 looks better than the previous version 35478 22:27:15,192 --> 22:27:20,320 it is not trustable you cannot trust any 35479 22:27:18,120 --> 22:27:22,800 input that ever comes from the user 35480 22:27:20,320 --> 22:27:24,680 because clearly with like a an hour or 35481 22:27:22,800 --> 22:27:26,760 so of cs50 like they can learn how to 35482 22:27:24,680 --> 22:27:28,760 turn all of these defenses off so even 35483 22:27:26,760 --> 22:27:31,000 if you like the user interface better 35484 22:27:28,760 --> 22:27:33,960 client side you have to have to have to 35485 22:27:31,000 --> 22:27:35,832 do server side validation always users 35486 22:27:33,960 --> 22:27:38,080 are not to be trusted and as soon as any 35487 22:27:35,832 --> 22:27:39,832 app or website you make becomes popular 35488 22:27:38,080 --> 22:27:44,360 unfortunately then you have to deal with 35489 22:27:39,832 --> 22:27:44,360 all of the adversarial possibilities as 35490 22:27:48,320 --> 22:27:52,280 well oh good question could the 35491 22:27:50,192 --> 22:27:55,160 adversary potentially access things 35492 22:27:52,280 --> 22:27:57,600 sensitive like app.py theoretically no 35493 22:27:55,160 --> 22:27:58,832 like if flask itself is buggy then sure 35494 22:27:57,600 --> 22:28:00,552 maybe if you're running some other 35495 22:27:58,832 --> 22:28:03,120 software on your server on your laptop 35496 22:28:00,552 --> 22:28:04,760 then sure uh it it's possible however if 35497 22:28:03,120 --> 22:28:06,320 your server is properly configured 35498 22:28:04,760 --> 22:28:08,600 theoretically they should not be able to 35499 22:28:06,320 --> 22:28:10,160 get access to that with that said we'll 35500 22:28:08,600 --> 22:28:11,872 soon see or You might with your final 35501 22:28:10,160 --> 22:28:13,800 project if you do something web based 35502 22:28:11,872 --> 22:28:16,320 you're never going to want to write like 35503 22:28:13,800 --> 22:28:18,160 usernames and passwords in your actual 35504 22:28:16,320 --> 22:28:19,760 code you can put them in what are called 35505 22:28:18,160 --> 22:28:21,640 environment variables so sort of in the 35506 22:28:19,760 --> 22:28:24,080 computer's memory but not in your code 35507 22:28:21,640 --> 22:28:26,512 just in case you or someone screws up 35508 22:28:24,080 --> 22:28:28,640 there are uh still ways to defend 35509 22:28:26,512 --> 22:28:31,680 against those kinds of possibilities 35510 22:28:28,640 --> 22:28:31,680 however slim 35511 22:28:36,232 --> 22:28:40,320 yeah a good question and this comes back 35512 22:28:38,512 --> 22:28:42,552 to First principles just like in C and 35513 22:28:40,320 --> 22:28:45,120 in python as soon as you return from a 35514 22:28:42,552 --> 22:28:47,232 function that's it nothing below that 35515 22:28:45,120 --> 22:28:49,280 line of code executes and so to 35516 22:28:47,232 --> 22:28:51,960 summarize the question even though I'm 35517 22:28:49,280 --> 22:28:53,680 returning this failure. HTML template 35518 22:28:51,960 --> 22:28:55,480 how am I making sure we still don't 35519 22:28:53,680 --> 22:28:57,680 accidentally put volleyball in that 35520 22:28:55,480 --> 22:28:59,552 Global dictionary it's because for 35521 22:28:57,680 --> 22:29:01,480 instance if you don't give me a name on 35522 22:28:59,552 --> 22:29:04,040 line 22 at the moment I'm returning the 35523 22:29:01,480 --> 22:29:07,640 failure template and that's it lines 23 35524 22:29:04,040 --> 22:29:09,920 24 25 26 27 never execute in particular 35525 22:29:07,640 --> 22:29:11,760 26 never executes and that's where I 35526 22:29:09,920 --> 22:29:14,800 would have been saving the name 35527 22:29:11,760 --> 22:29:17,512 similarly if we do we get a invalid 35528 22:29:14,800 --> 22:29:20,040 sport that is either blank or not in the 35529 22:29:17,512 --> 22:29:22,400 original authoritative list we return 35530 22:29:20,040 --> 22:29:25,480 failure. HTML is a template in line 25 35531 22:29:22,400 --> 22:29:27,320 we never get to line 26 so it just boils 35532 22:29:25,480 --> 22:29:30,720 down to return and what that means in 35533 22:29:27,320 --> 22:29:32,080 Python 2 good question other questions 35534 22:29:30,720 --> 22:29:35,080 as 35535 22:29:32,080 --> 22:29:38,920 well no all right how about we make one 35536 22:29:35,080 --> 22:29:40,800 more enhancement here or so because the 35537 22:29:38,920 --> 22:29:45,120 problem with storing everything in this 35538 22:29:40,800 --> 22:29:48,120 Global dictionary might be what we've 35539 22:29:45,120 --> 22:29:50,480 got it all working finally but why is 35540 22:29:48,120 --> 22:29:53,760 probably a global dictionary not the 35541 22:29:50,480 --> 22:29:57,360 place to store Frost's registration 35542 22:29:53,760 --> 22:29:59,760 data what's the implication of 35543 22:29:57,360 --> 22:30:01,360 this okay might slow the whole process 35544 22:29:59,760 --> 22:30:03,192 down but that actually Ram is actually 35545 22:30:01,360 --> 22:30:05,160 good memory is actually generally a good 35546 22:30:03,192 --> 22:30:07,480 thing so not going to be a deal breaker 35547 22:30:05,160 --> 22:30:10,320 here why might I not want to store that 35548 22:30:07,480 --> 22:30:10,320 data though in that 35549 22:30:10,920 --> 22:30:15,512 variable you can perhaps infer how I fix 35550 22:30:13,800 --> 22:30:18,480 something 35551 22:30:15,512 --> 22:30:21,552 earlier yeah and 35552 22:30:18,480 --> 22:30:23,480 back yeah it gets it's the memory gets 35553 22:30:21,552 --> 22:30:25,960 deleted garbage collected if you will as 35554 22:30:23,480 --> 22:30:27,720 soon as flask stops running so if you so 35555 22:30:25,960 --> 22:30:30,120 much as hit control C like you've just 35556 22:30:27,720 --> 22:30:32,360 lost all of your freshmen who registered 35557 22:30:30,120 --> 22:30:33,872 for the sport probably not a good thing 35558 22:30:32,360 --> 22:30:36,040 I did this deliberately a moment ago and 35559 22:30:33,872 --> 22:30:38,400 I hit control C because I did want to 35560 22:30:36,040 --> 22:30:40,232 clear the dictionary but trusting that 35561 22:30:38,400 --> 22:30:41,920 your server will never crash and your 35562 22:30:40,232 --> 22:30:43,640 code will always work and the power will 35563 22:30:41,920 --> 22:30:45,320 never go out like that's not the right 35564 22:30:43,640 --> 22:30:47,040 way to build any kind of web application 35565 22:30:45,320 --> 22:30:49,080 with persistent data so what we probably 35566 22:30:47,040 --> 22:30:51,000 want to do is reintroduce csvs and we've 35567 22:30:49,080 --> 22:30:53,120 played with those in C and in Python 35568 22:30:51,000 --> 22:30:56,400 could totally use csvs but we also now 35569 22:30:53,120 --> 22:30:58,280 have p uh SQL at our disposal and let me 35570 22:30:56,400 --> 22:30:59,800 propose that we do this in SQL in 35571 22:30:58,280 --> 22:31:01,720 instead and for this let me go ahead and 35572 22:30:59,800 --> 22:31:03,440 open up a version of the program that I 35573 22:31:01,720 --> 22:31:05,192 wrote in advance so let me go ahead and 35574 22:31:03,440 --> 22:31:07,280 close these templates which will look 35575 22:31:05,192 --> 22:31:09,552 very similar but a little different from 35576 22:31:07,280 --> 22:31:12,872 the ones I wrote in advance and let me 35577 22:31:09,552 --> 22:31:15,872 go ahead and open up uh in today's let 35578 22:31:12,872 --> 22:31:19,320 me go into Source 9 let me go into Frost 35579 22:31:15,872 --> 22:31:21,232 im's how about version four technically 35580 22:31:19,320 --> 22:31:23,960 in the versions online and let me go 35581 22:31:21,232 --> 22:31:25,552 ahead and open up app.py as follows so 35582 22:31:23,960 --> 22:31:27,232 here is an already made version that 35583 22:31:25,552 --> 22:31:29,760 does just a little something different 35584 22:31:27,232 --> 22:31:31,360 at the very top I'm importing cs50 SQL 35585 22:31:29,760 --> 22:31:32,872 Library which you might recall we used a 35586 22:31:31,360 --> 22:31:34,960 couple of weeks past just to write 35587 22:31:32,872 --> 22:31:36,280 python that talks to a SQL database and 35588 22:31:34,960 --> 22:31:38,360 this feels like an opportune moment to 35589 22:31:36,280 --> 22:31:40,440 bring that idea back down here on line 35590 22:31:38,360 --> 22:31:43,600 eight I'm creating a DB variable that 35591 22:31:40,440 --> 22:31:45,600 opens up a file called frost. DB using 35592 22:31:43,600 --> 22:31:48,232 syntax that we've seen before I did 35593 22:31:45,600 --> 22:31:49,920 create this FRS DB file in advance of 35594 22:31:48,232 --> 22:31:51,440 class just so that we have a couple of 35595 22:31:49,920 --> 22:31:53,960 columns in which to store names and 35596 22:31:51,440 --> 22:31:56,232 sports and such here's that same Global 35597 22:31:53,960 --> 22:31:58,440 array a global list called Sports and 35598 22:31:56,232 --> 22:32:01,680 let's just see what's going on down 35599 22:31:58,440 --> 22:32:04,000 below if I scroll down to Index this is 35600 22:32:01,680 --> 22:32:06,232 the same as before as we wrote together 35601 22:32:04,000 --> 22:32:08,360 on the Fly let's skip deregister for a 35602 22:32:06,232 --> 22:32:10,232 moment and go now into register so this 35603 22:32:08,360 --> 22:32:11,872 one's a little different but let's see 35604 22:32:10,232 --> 22:32:13,360 what I've done I've got some comments in 35605 22:32:11,872 --> 22:32:14,872 here because I wrote it in advance and I 35606 22:32:13,360 --> 22:32:16,512 think this logic is pretty much the same 35607 22:32:14,872 --> 22:32:19,232 though I tightened it up and I'm asking 35608 22:32:16,512 --> 22:32:21,320 two questions at once using on line 38 35609 22:32:19,232 --> 22:32:22,832 the or keyword here just to say if 35610 22:32:21,320 --> 22:32:24,760 there's not a name or the sport is not 35611 22:32:22,832 --> 22:32:27,040 in sports that is what we'll call now a 35612 22:32:24,760 --> 22:32:28,680 failure but what's fun now is that on 35613 22:32:27,040 --> 22:32:31,280 line 42 35614 22:32:28,680 --> 22:32:33,800 I'm using the cs50 SQL library to 35615 22:32:31,280 --> 22:32:36,720 execute some actual SQL and I'm going to 35616 22:32:33,800 --> 22:32:39,232 insert into a table called registrant 35617 22:32:36,720 --> 22:32:41,280 two columns name and Sport what names 35618 22:32:39,232 --> 22:32:43,680 and Sport well these two values with 35619 22:32:41,280 --> 22:32:45,160 placeholders plugging in name and Sport 35620 22:32:43,680 --> 22:32:47,040 notice I'm using the question marks 35621 22:32:45,160 --> 22:32:48,960 absolutely necessary so we don't get one 35622 22:32:47,040 --> 22:32:50,680 of those SQL injection attacks because 35623 22:32:48,960 --> 22:32:52,280 that too could be possible if someone 35624 22:32:50,680 --> 22:32:54,080 typed in some dangerous words or 35625 22:32:52,280 --> 22:32:56,640 keywords like delete or single quotes or 35626 22:32:54,080 --> 22:32:59,000 semicolons in the form here I'm letting 35627 22:32:56,640 --> 22:33:00,680 the library sanitize the data and then 35628 22:32:59,000 --> 22:33:02,320 this is a trick we haven't yet seen and 35629 22:33:00,680 --> 22:33:05,160 it's really going to start tying things 35630 22:33:02,320 --> 22:33:07,480 together I can also use a redirect 35631 22:33:05,160 --> 22:33:09,320 function in flask that has the effect of 35632 22:33:07,480 --> 22:33:12,440 doing the the if you will uh 35633 22:33:09,320 --> 22:33:14,760 safetyschool.org trick again whereby 35634 22:33:12,440 --> 22:33:17,080 after the user registers if I want to 35635 22:33:14,760 --> 22:33:19,920 automatically show them now everyone who 35636 22:33:17,080 --> 22:33:21,320 is registered atreg I don't have to 35637 22:33:19,920 --> 22:33:22,872 manually expect that they'll change the 35638 22:33:21,320 --> 22:33:25,000 url like I've been doing for the past 35639 22:33:22,872 --> 22:33:27,360 few minutes I can just redirect them 35640 22:33:25,000 --> 22:33:29,160 anywhere I want on my app or heck I 35641 22:33:27,360 --> 22:33:31,160 could redirect them to any URL on the 35642 22:33:29,160 --> 22:33:32,720 internet using this function call and 35643 22:33:31,160 --> 22:33:34,600 it's just a nice way to send them to a 35644 22:33:32,720 --> 22:33:36,960 different route if you want them to see 35645 22:33:34,600 --> 22:33:39,440 in this case those registrant so let me 35646 22:33:36,960 --> 22:33:41,600 do this in the same directory let me 35647 22:33:39,440 --> 22:33:45,800 increase my terminal window size let me 35648 22:33:41,600 --> 22:33:48,512 do SQL light three of frost. DB and let 35649 22:33:45,800 --> 22:33:51,192 me type schema and you can indeed see it 35650 22:33:48,512 --> 22:33:53,872 wraps on to two lines here that each 35651 22:33:51,192 --> 22:33:55,720 registrant has an ID which will be 35652 22:33:53,872 --> 22:33:58,320 automatically assigned one two three on 35653 22:33:55,720 --> 22:34:00,192 up a name which is not null text and a 35654 22:33:58,320 --> 22:34:01,480 sport which is also the same and the 35655 22:34:00,192 --> 22:34:03,600 primary key is just going to be this 35656 22:34:01,480 --> 22:34:06,120 unique identifier so that I made in 35657 22:34:03,600 --> 22:34:08,680 advance but if I do select star from 35658 22:34:06,120 --> 22:34:11,600 registrant semicolon there's no one 35659 22:34:08,680 --> 22:34:13,400 currently registered for any sports but 35660 22:34:11,600 --> 22:34:15,760 let's try now running this let me go 35661 22:34:13,400 --> 22:34:18,480 ahead and close my old version which we 35662 22:34:15,760 --> 22:34:21,600 wrote together and I'll close that tab 35663 22:34:18,480 --> 22:34:23,912 let me do flask run in this version four 35664 22:34:21,600 --> 22:34:26,040 here all right I'm going to see some 35665 22:34:23,912 --> 22:34:27,600 similar output I'm going to open the URL 35666 22:34:26,040 --> 22:34:29,800 now and you'll see that I made a couple 35667 22:34:27,600 --> 22:34:32,400 of Chang changes before instead of using 35668 22:34:29,800 --> 22:34:34,192 a select menu I used what are called 35669 22:34:32,400 --> 22:34:35,832 radio buttons now which is a reference 35670 22:34:34,192 --> 22:34:37,720 to Old School radio buttons that were 35671 22:34:35,832 --> 22:34:39,120 mutually exclusive in cars back in the 35672 22:34:37,720 --> 22:34:40,720 day and we'll see how to do this but 35673 22:34:39,120 --> 22:34:42,552 it's just an alternative to a select 35674 22:34:40,720 --> 22:34:46,192 menu and I'm going to go ahead and type 35675 22:34:42,552 --> 22:34:48,512 in my name again here so I'll do David 35676 22:34:46,192 --> 22:34:50,440 I'll do soccer by selecting this radio 35677 22:34:48,512 --> 22:34:52,760 button and I'm going to click register 35678 22:34:50,440 --> 22:34:55,480 now and notice what happened it's a 35679 22:34:52,760 --> 22:34:57,912 little ugly the formatting but so again 35680 22:34:55,480 --> 22:35:00,912 was this 20 years ago here I have now 35681 22:34:57,912 --> 22:35:02,512 now at the slash registrant route 35682 22:35:00,912 --> 22:35:04,680 instead of an unordered list I'm just 35683 22:35:02,512 --> 22:35:06,040 using a simple HTML table so I'll show 35684 22:35:04,680 --> 22:35:07,552 you what this looks like in just a 35685 22:35:06,040 --> 22:35:09,280 moment too and I'll show you this 35686 22:35:07,552 --> 22:35:11,640 deregister button which is sort of 35687 22:35:09,280 --> 22:35:13,320 unnecessarily large I also have 35688 22:35:11,640 --> 22:35:15,720 functionality we'll soon see for how you 35689 22:35:13,320 --> 22:35:18,440 can unregister someone from a sport as 35690 22:35:15,720 --> 22:35:20,040 well so take your name out of contention 35691 22:35:18,440 --> 22:35:21,872 well let me go back to my terminal 35692 22:35:20,040 --> 22:35:24,400 window here and I'm going to click the 35693 22:35:21,872 --> 22:35:26,960 plus to give myself s a second terminal 35694 22:35:24,400 --> 22:35:31,400 so I can go back into Source 9 Frost IMS 35695 22:35:26,960 --> 22:35:33,640 4 I'm going to do SQL light of frost. DB 35696 22:35:31,400 --> 22:35:36,832 I'm going to do select star from 35697 22:35:33,640 --> 22:35:38,912 registrant now and now you'll see that 35698 22:35:36,832 --> 22:35:42,160 indeed there's David registered for 35699 22:35:38,912 --> 22:35:44,040 soccer and in fact if I quit the flask 35700 22:35:42,160 --> 22:35:46,832 program with control C and rerun it 35701 22:35:44,040 --> 22:35:48,280 again no big deal because that next 35702 22:35:46,832 --> 22:35:50,232 version of flask will just use the 35703 22:35:48,280 --> 22:35:53,160 database as well so I'm persisting 35704 22:35:50,232 --> 22:35:55,600 keeping the data in SQL light whereas 35705 22:35:53,160 --> 22:35:57,512 I'm actually grabbing it using my python 35706 22:35:55,600 --> 22:35:59,512 code in flask all right let's put one 35707 22:35:57,512 --> 22:36:03,280 more person in here so we can delete one 35708 22:35:59,512 --> 22:36:05,192 of us too Carter for basketball register 35709 22:36:03,280 --> 22:36:06,760 and now we see both of us here all right 35710 22:36:05,192 --> 22:36:08,872 so let's see how we did this let's go 35711 22:36:06,760 --> 22:36:13,360 back over to VSS code let me shrink down 35712 22:36:08,872 --> 22:36:16,160 my terminal window let me go into the 35713 22:36:13,360 --> 22:36:19,960 actually let's go into the templates 35714 22:36:16,160 --> 22:36:22,480 directory and let's look at for instance 35715 22:36:19,960 --> 22:36:25,480 index.html so previously we were using a 35716 22:36:22,480 --> 22:36:27,960 select menu turns out radio buttons use 35717 22:36:25,480 --> 22:36:30,912 the input tag but instead of having 35718 22:36:27,960 --> 22:36:32,832 input of uh type equals text like for 35719 22:36:30,912 --> 22:36:35,280 the human's name you have type equals 35720 22:36:32,832 --> 22:36:37,280 radio and so long as each of your radio 35721 22:36:35,280 --> 22:36:38,832 buttons has the same name the same name 35722 22:36:37,280 --> 22:36:41,280 the same name that's what makes them 35723 22:36:38,832 --> 22:36:43,160 mutually exclusive so checking one radio 35724 22:36:41,280 --> 22:36:45,480 button turns off the others because they 35725 22:36:43,160 --> 22:36:47,040 have the same name the value I want to 35726 22:36:45,480 --> 22:36:49,320 assign to each of these radio buttons is 35727 22:36:47,040 --> 22:36:51,000 just the sport placeholder this is what 35728 22:36:49,320 --> 22:36:52,480 the human sees on the screen so it's 35729 22:36:51,000 --> 22:36:54,280 almost the same as the select menu it 35730 22:36:52,480 --> 22:36:56,360 just looks aesthetically different but 35731 22:36:54,280 --> 22:36:57,912 there's my same button so that's all the 35732 22:36:56,360 --> 22:37:00,080 difference I made there and I added a 35733 22:36:57,912 --> 22:37:02,120 heading tag H1 just to say register to 35734 22:37:00,080 --> 22:37:06,000 make clear what it is but let's take a 35735 22:37:02,120 --> 22:37:09,872 look at another file uh this one now 35736 22:37:06,000 --> 22:37:13,320 being how about the SL registrant route 35737 22:37:09,872 --> 22:37:15,040 so if I open up registr trans. HTML here 35738 22:37:13,320 --> 22:37:17,000 now it's way more verbose than my 35739 22:37:15,040 --> 22:37:19,800 unordered list but this is just kind of 35740 22:37:17,000 --> 22:37:22,640 boring HTML here's my table tag table 35741 22:37:19,800 --> 22:37:24,832 head table row table heading this makes 35742 22:37:22,640 --> 22:37:27,280 things bold as the first row of the 35743 22:37:24,832 --> 22:37:28,960 table name sport are my two columns I've 35744 22:37:27,280 --> 22:37:31,120 got a third empty column just so I can 35745 22:37:28,960 --> 22:37:33,760 fit that button as we'll soon see again 35746 22:37:31,120 --> 22:37:35,680 T body for table body here's the same 35747 22:37:33,760 --> 22:37:37,640 for Loop trick again so that I can 35748 22:37:35,680 --> 22:37:40,160 output for every registrant a whole 35749 22:37:37,640 --> 22:37:41,760 table row and there's this weird form in 35750 22:37:40,160 --> 22:37:43,600 there but we'll come back to that but 35751 22:37:41,760 --> 22:37:46,552 there's the registrant's name there's 35752 22:37:43,600 --> 22:37:49,760 the registrant sport but notice the 35753 22:37:46,552 --> 22:37:53,192 slightly different syntax here recall 35754 22:37:49,760 --> 22:37:55,912 that cs50's select uh cs50's execute 35755 22:37:53,192 --> 22:37:58,512 function when it returns to you a list 35756 22:37:55,912 --> 22:38:00,912 of dictionaries you can then get at the 35757 22:37:58,512 --> 22:38:03,000 individual columns by way of those keys 35758 22:38:00,912 --> 22:38:05,360 so let's go to the/ registrant Route let 35759 22:38:03,000 --> 22:38:08,320 me go back to app.py scroll down here 35760 22:38:05,360 --> 22:38:11,080 and it's actually super simple here I 35761 22:38:08,320 --> 22:38:13,280 have a SL registr route that first 35762 22:38:11,080 --> 22:38:15,280 executes select star from registrant so 35763 22:38:13,280 --> 22:38:17,552 just old SQL stuff give me everyone from 35764 22:38:15,280 --> 22:38:19,872 the registr table let me then render the 35765 22:38:17,552 --> 22:38:23,080 template called registr trans. HTML and 35766 22:38:19,872 --> 22:38:24,480 just pass in this list of dictionaries 35767 22:38:23,080 --> 22:38:27,680 and we haven't quite done this yet but 35768 22:38:24,480 --> 22:38:30,552 if you go back to register. HTML how do 35769 22:38:27,680 --> 22:38:32,552 you iterate over each dictionary in that 35770 22:38:30,552 --> 22:38:34,832 list well the syntax is just for 35771 22:38:32,552 --> 22:38:37,832 registrant in registrants that makes 35772 22:38:34,832 --> 22:38:40,600 this a dictionary one at a time in the 35773 22:38:37,832 --> 22:38:42,360 list just like in Python so registrant 35774 22:38:40,600 --> 22:38:44,872 name and registrants sport is just 35775 22:38:42,360 --> 22:38:47,120 another Syntax for using the square 35776 22:38:44,872 --> 22:38:49,680 bracket notation it's just a little 35777 22:38:47,120 --> 22:38:51,280 cleaner and slightly more succinct than 35778 22:38:49,680 --> 22:38:53,040 having quotes and square brackets 35779 22:38:51,280 --> 22:38:54,360 everywhere and then the rest of this is 35780 22:38:53,040 --> 22:38:58,600 just 35781 22:38:54,360 --> 22:38:59,912 HTML so what happens now if I want to uh 35782 22:38:58,600 --> 22:39:01,912 like Carter has been cut from the 35783 22:38:59,912 --> 22:39:03,800 basketball team if you will so how do we 35784 22:39:01,912 --> 22:39:06,872 do that well we want to click this 35785 22:39:03,800 --> 22:39:08,800 button deregister next to Carter's name 35786 22:39:06,872 --> 22:39:10,552 but how does this work and think about 35787 22:39:08,800 --> 22:39:12,832 now any website you visited that has 35788 22:39:10,552 --> 22:39:14,552 something like a shopping cart uh where 35789 22:39:12,832 --> 22:39:17,000 you can remove things from your cart or 35790 22:39:14,552 --> 22:39:18,552 update quantities or add more quantities 35791 22:39:17,000 --> 22:39:20,480 to your shopping cart on Amazon or 35792 22:39:18,552 --> 22:39:23,360 anything else well let's actually look 35793 22:39:20,480 --> 22:39:25,280 at the HTML that my app has spit out 35794 22:39:23,360 --> 22:39:27,960 let's actually look at this here and 35795 22:39:25,280 --> 22:39:30,360 we'll see the following we'll see that 35796 22:39:27,960 --> 22:39:33,192 we have here in the HTML that reached 35797 22:39:30,360 --> 22:39:35,720 the user not only is David in the first 35798 22:39:33,192 --> 22:39:38,640 column socer in the second notice that 35799 22:39:35,720 --> 22:39:41,512 my register. HTML form is also spitting 35800 22:39:38,640 --> 22:39:43,512 out a tiny little web form of its own 35801 22:39:41,512 --> 22:39:45,760 it's ugly but I only care about its 35802 22:39:43,512 --> 22:39:48,640 functionality for now and notice what 35803 22:39:45,760 --> 22:39:51,120 I'm doing here every registrant in this 35804 22:39:48,640 --> 22:39:53,832 database gets their very own deregister 35805 22:39:51,120 --> 22:39:56,360 button and that form has a button that 35806 22:39:53,832 --> 22:39:58,232 says deregister but notice what else 35807 22:39:56,360 --> 22:39:59,960 each of those forms have there's no text 35808 22:39:58,232 --> 22:40:04,480 box there's no drop down menu there's no 35809 22:39:59,960 --> 22:40:07,720 radio buttons rather you have a hidden 35810 22:40:04,480 --> 22:40:10,512 input field here so there is a way with 35811 22:40:07,720 --> 22:40:12,280 HTML to have a form that will submit 35812 22:40:10,512 --> 22:40:14,360 information but you don't have to give 35813 22:40:12,280 --> 22:40:16,760 the user the ability to change that 35814 22:40:14,360 --> 22:40:18,872 information you can just go ahead and 35815 22:40:16,760 --> 22:40:21,600 tuck it inside of the form invisibly if 35816 22:40:18,872 --> 22:40:23,232 you will hidden in fashion and so what's 35817 22:40:21,600 --> 22:40:26,040 going to happen is if I click the D 35818 22:40:23,232 --> 22:40:28,120 register button next to Carter his 35819 22:40:26,040 --> 22:40:30,960 primary key is two 35820 22:40:28,120 --> 22:40:33,192 mine is instead one so what's going to 35821 22:40:30,960 --> 22:40:37,400 happen if I click his deregister button 35822 22:40:33,192 --> 22:40:40,912 it submits a form with a 35823 22:40:37,400 --> 22:40:43,120 ID parameter whose value is two and it 35824 22:40:40,912 --> 22:40:44,680 submits it to the deregister route so 35825 22:40:43,120 --> 22:40:47,232 what do that mean well if I go to VSS 35826 22:40:44,680 --> 22:40:50,440 code and I go to app.py let's look at 35827 22:40:47,232 --> 22:40:52,872 the deregister route that I skipped over 35828 22:40:50,440 --> 22:40:56,680 so if you access the deregister route 35829 22:40:52,872 --> 22:40:58,912 via post this code gets called I grab 35830 22:40:56,680 --> 22:41:01,400 from request form the ID that was 35831 22:40:58,912 --> 22:41:03,280 submitted in Hidden fashion if there's 35832 22:41:01,400 --> 22:41:05,232 indeed an ID that is it's not blank it's 35833 22:41:03,280 --> 22:41:08,872 not zero it's an actual number like one 35834 22:41:05,232 --> 22:41:12,280 2 three or more I execute delete from 35835 22:41:08,872 --> 22:41:13,960 registrant where ID equals that value 35836 22:41:12,280 --> 22:41:16,192 with a question mark Place holder and 35837 22:41:13,960 --> 22:41:18,832 then I redirect the user back to 35838 22:41:16,192 --> 22:41:21,080 registrant now if I go back to this form 35839 22:41:18,832 --> 22:41:24,232 here I click deregister we'll see that 35840 22:41:21,080 --> 22:41:26,872 in action gone is now Carter and in fact 35841 22:41:24,232 --> 22:41:30,720 if I go back to my terminal window here 35842 22:41:26,872 --> 22:41:33,040 I open open up uh SQL light 3 of frost. 35843 22:41:30,720 --> 22:41:36,360 DB and rerun select star from registrant 35844 22:41:33,040 --> 22:41:39,232 Carter is now gone so again using very 35845 22:41:36,360 --> 22:41:41,232 simple HTML forms you can get buttons 35846 22:41:39,232 --> 22:41:43,320 and links and other such UI mechanisms 35847 22:41:41,232 --> 22:41:46,280 to like do things on the server that you 35848 22:41:43,320 --> 22:41:48,280 want but there is a danger here this now 35849 22:41:46,280 --> 22:41:50,640 is really meant this example is like an 35850 22:41:48,280 --> 22:41:53,232 administrative website like it was some 35851 22:41:50,640 --> 22:41:56,400 20 years ago just for us internal staff 35852 22:41:53,232 --> 22:41:58,800 to be doing things technically this is 35853 22:41:56,400 --> 22:42:02,552 dangerous what I've just done 35854 22:41:58,800 --> 22:42:05,080 two even though Carter's ID is two and 35855 22:42:02,552 --> 22:42:06,912 hidden and mine is one in Hidden what 35856 22:42:05,080 --> 22:42:10,640 could this allow an adversary to do if 35857 22:42:06,912 --> 22:42:10,640 they had admin access to the same 35858 22:42:11,480 --> 22:42:16,080 site any 35859 22:42:14,232 --> 22:42:18,232 thoughts 35860 22:42:16,080 --> 22:42:20,720 yeah 35861 22:42:18,232 --> 22:42:22,552 yeah yeah they could change the value of 35862 22:42:20,720 --> 22:42:24,360 that hidden attri by opening up chrom's 35863 22:42:22,552 --> 22:42:26,160 like developer tools change the number 35864 22:42:24,360 --> 22:42:28,320 in the HTML they could delete anyone 35865 22:42:26,160 --> 22:42:31,080 deregister anyone want from the database 35866 22:42:28,320 --> 22:42:32,640 now in this case I claim this is fine 35867 22:42:31,080 --> 22:42:34,680 because this is only meant for us staff 35868 22:42:32,640 --> 22:42:36,800 who were running Sports back in the day 35869 22:42:34,680 --> 22:42:39,040 but it's indeed a risk so wouldn't it be 35870 22:42:36,800 --> 22:42:41,912 nice if we could actually ensure that 35871 22:42:39,040 --> 22:42:44,512 only those users who are authorized are 35872 22:42:41,912 --> 22:42:46,912 allowed to execute certain actions I 35873 22:42:44,512 --> 22:42:49,440 think for this capability we're actually 35874 22:42:46,912 --> 22:42:51,912 going to need to introduce something a 35875 22:42:49,440 --> 22:42:53,872 bit more and so here of course is an 35876 22:42:51,912 --> 22:42:55,832 opportunity to talk briefly about really 35877 22:42:53,872 --> 22:42:58,040 what you and I do all day long every day 35878 22:42:55,832 --> 22:43:00,232 we log into one or more websites or apps 35879 22:42:58,040 --> 22:43:01,720 or at least until uh you're logged out 35880 22:43:00,232 --> 22:43:02,912 automatically and you have to do it 35881 22:43:01,720 --> 22:43:04,760 again so here for instance is a 35882 22:43:02,912 --> 22:43:06,320 screenshot of Gmail when you type in 35883 22:43:04,760 --> 22:43:08,320 your username you type in your password 35884 22:43:06,320 --> 22:43:10,192 maybe your to factor code that gets 35885 22:43:08,320 --> 22:43:12,080 texted or sent to your phone then you're 35886 22:43:10,192 --> 22:43:13,832 logged in and thankfully you're not 35887 22:43:12,080 --> 22:43:16,640 prompted to log in again typically for 35888 22:43:13,832 --> 22:43:18,280 number of hours or days or weeks 35889 22:43:16,640 --> 22:43:20,360 depending on the website like Gmail 35890 22:43:18,280 --> 22:43:22,400 keeps you logged in for ages your bank 35891 22:43:20,360 --> 22:43:24,600 probably logs you out within an hour or 35892 22:43:22,400 --> 22:43:26,832 so for safety sake so that is completely 35893 22:43:24,600 --> 22:43:29,512 configurable on the server but how does 35894 22:43:26,832 --> 22:43:31,280 g Emil know how does Google know that 35895 22:43:29,512 --> 22:43:33,000 even as you're checking different mails 35896 22:43:31,280 --> 22:43:34,680 again and again and again how do they 35897 22:43:33,000 --> 22:43:37,192 know that you're still the same person 35898 22:43:34,680 --> 22:43:38,960 who logged in well it turns out that 35899 22:43:37,192 --> 22:43:42,320 using these same building blocks as 35900 22:43:38,960 --> 22:43:44,832 today HTTP and HTML and more you can 35901 22:43:42,320 --> 22:43:47,480 actually implement the notion of a login 35902 22:43:44,832 --> 22:43:49,912 feature by doing the equivalent of 35903 22:43:47,480 --> 22:43:51,320 something with something called cookies 35904 22:43:49,912 --> 22:43:53,160 essentially what happens when you first 35905 22:43:51,320 --> 22:43:55,000 log into a website for the very first 35906 22:43:53,160 --> 22:43:57,360 time successfully with your username and 35907 22:43:55,000 --> 22:43:59,192 password a cookie so to speak is planted 35908 22:43:57,360 --> 22:44:01,160 on your computer and metaphorically this 35909 22:43:59,192 --> 22:44:03,160 is kind of like taking a hand stamp and 35910 22:44:01,160 --> 22:44:05,832 your hand is now stamped in this case a 35911 22:44:03,160 --> 22:44:07,680 smiley face so that every other time you 35912 22:44:05,832 --> 22:44:09,720 click on a link on that same website 35913 22:44:07,680 --> 22:44:11,832 google.com gmail.com whatever 35914 22:44:09,720 --> 22:44:13,640 unbeknownst to you your browser is 35915 22:44:11,832 --> 22:44:15,192 constantly presenting that handstamp 35916 22:44:13,640 --> 22:44:17,000 just like going into a club or an 35917 22:44:15,192 --> 22:44:18,232 amusement park or something like that 35918 22:44:17,000 --> 22:44:19,160 where they don't want to check your ID 35919 22:44:18,232 --> 22:44:20,480 again they don't want to check your 35920 22:44:19,160 --> 22:44:23,320 ticket again they just want to quickly 35921 22:44:20,480 --> 22:44:24,832 see the same hand stamp and so that is 35922 22:44:23,320 --> 22:44:26,552 one of the things that a browser is 35923 22:44:24,832 --> 22:44:29,000 always doing for you once you're logged 35924 22:44:26,552 --> 22:44:31,552 in any cookies that have been planted so 35925 22:44:29,000 --> 22:44:33,912 to speak in your Mac or PC or phone are 35926 22:44:31,552 --> 22:44:35,912 constantly represented to the site every 35927 22:44:33,912 --> 22:44:38,640 time you click a link or make another 35928 22:44:35,912 --> 22:44:40,160 request to that website and mechanically 35929 22:44:38,640 --> 22:44:42,720 how this works not just metaphorically 35930 22:44:40,160 --> 22:44:45,552 and ink essentially this is what happens 35931 22:44:42,720 --> 22:44:47,912 here is a example of an HTTP request to 35932 22:44:45,552 --> 22:44:50,192 something like Gmail and suppose for 35933 22:44:47,912 --> 22:44:52,400 instance that you've logged in typically 35934 22:44:50,192 --> 22:44:54,320 as of last week we said that coming back 35935 22:44:52,400 --> 22:44:56,440 from the server would be another virtual 35936 22:44:54,320 --> 22:44:58,440 envelope containing like a 200 okay 35937 22:44:56,440 --> 22:45:00,232 message and then like the actual web 35938 22:44:58,440 --> 22:45:03,440 page or the picture of a cat or whatever 35939 22:45:00,232 --> 22:45:05,280 it may be but Google can also if they 35940 22:45:03,440 --> 22:45:07,320 verified that you have some username and 35941 22:45:05,280 --> 22:45:09,720 password correctly inputed they can do 35942 22:45:07,320 --> 22:45:11,552 the equivalent of stamping your hand and 35943 22:45:09,720 --> 22:45:13,872 the way they do this is they send an 35944 22:45:11,552 --> 22:45:16,440 additional line of text in that virtual 35945 22:45:13,872 --> 22:45:20,192 envelope from the server to your browser 35946 22:45:16,440 --> 22:45:22,512 literally using another HTTP header not 35947 22:45:20,192 --> 22:45:24,480 content type which just mundanely tells 35948 22:45:22,512 --> 22:45:27,640 you what kind of content has come back 35949 22:45:24,480 --> 22:45:31,000 they literally send an http header 35950 22:45:27,640 --> 22:45:33,720 called set-cookie and then they set a 35951 22:45:31,000 --> 22:45:35,760 key value pair on your Mac or PC this is 35952 22:45:33,720 --> 22:45:38,480 the technical equivalent of this smiley 35953 22:45:35,760 --> 22:45:40,320 face handstamp and what your computer is 35954 22:45:38,480 --> 22:45:43,280 designed to do because your computer and 35955 22:45:40,320 --> 22:45:46,000 intern browser are supposed to implement 35956 22:45:43,280 --> 22:45:48,040 HTTP anytime you click another link on 35957 22:45:46,000 --> 22:45:50,160 Gmail or click on another mail or the 35958 22:45:48,040 --> 22:45:52,160 like your browser unbeknownst to you 35959 22:45:50,160 --> 22:45:54,400 presents that handstamp and how it does 35960 22:45:52,160 --> 22:45:57,120 it technically is in the envelope it 35961 22:45:54,400 --> 22:45:59,360 sends to Google from your browser it 35962 22:45:57,120 --> 22:46:02,760 doesn't send set cookie it just sends 35963 22:45:59,360 --> 22:46:04,640 cookie colon and the exact same thing 35964 22:46:02,760 --> 22:46:07,640 and so long as Google is smart and they 35965 22:46:04,640 --> 22:46:09,680 have a database or something of all of 35966 22:46:07,640 --> 22:46:11,680 the session Valu session is the 35967 22:46:09,680 --> 22:46:14,232 technical term for this maintenance of 35968 22:46:11,680 --> 22:46:15,832 information across HTTP so long as 35969 22:46:14,232 --> 22:46:18,040 Google has a big database that knows 35970 22:46:15,832 --> 22:46:21,600 that my cookie value is one 123 and your 35971 22:46:18,040 --> 22:46:25,192 cookie value is 456 they can infer from 35972 22:46:21,600 --> 22:46:27,680 this virtual handstamp whose emails they 35973 22:46:25,192 --> 22:46:29,320 should be showing mine or yours or the 35974 22:46:27,680 --> 22:46:31,320 like so this is just scratching the 35975 22:46:29,320 --> 22:46:34,872 surface but if I really wanted to enable 35976 22:46:31,320 --> 22:46:36,680 only Carter to deregister himself I just 35977 22:46:34,872 --> 22:46:38,440 have to make sure that I log him in 35978 22:46:36,680 --> 22:46:41,000 somehow username password and all of 35979 22:46:38,440 --> 22:46:43,600 that I stamp his hand or really put a 35980 22:46:41,000 --> 22:46:46,120 cookie on his computer and only if his 35981 22:46:43,600 --> 22:46:48,512 cookie lines up with the user ID he's 35982 22:46:46,120 --> 22:46:51,832 trying to deregister should he be 35983 22:46:48,512 --> 22:46:53,192 allowed to in fact do so so all of this 35984 22:46:51,832 --> 22:46:55,232 is quite possible and indeed the 35985 22:46:53,192 --> 22:46:56,552 technical term for this is session and 35986 22:46:55,232 --> 22:46:59,512 what we thought we'd do in our remaining 35987 22:46:56,552 --> 22:47:01,232 time Today show you some examples of 35988 22:46:59,512 --> 22:47:03,400 exactly how some of the most familiar 35989 22:47:01,232 --> 22:47:05,440 web functionality is implemented today 35990 22:47:03,400 --> 22:47:07,832 some of which you'll use in your own uh 35991 22:47:05,440 --> 22:47:09,640 problem set 9 uh which itself will be a 35992 22:47:07,832 --> 22:47:11,720 web app or perhaps even your final 35993 22:47:09,640 --> 22:47:13,680 project so let me go ahead and do this 35994 22:47:11,720 --> 22:47:16,192 let me close my previous tabs and all 35995 22:47:13,680 --> 22:47:18,912 things for ashs and let's move on to 35996 22:47:16,192 --> 22:47:20,400 implementing some notion of login so in 35997 22:47:18,912 --> 22:47:23,760 just a moment I'll switch over here to 35998 22:47:20,400 --> 22:47:25,512 VSS code and what I'm going to do is 35999 22:47:23,760 --> 22:47:27,680 indeed in my source 9 directory I'm 36000 22:47:25,512 --> 22:47:30,640 going to go into a login directory and 36001 22:47:27,680 --> 22:47:32,800 if I type LS here you'll see app.py 36002 22:47:30,640 --> 22:47:34,232 requirements. text which just refers to 36003 22:47:32,800 --> 22:47:36,960 libraries I want to automatically 36004 22:47:34,232 --> 22:47:38,120 install and a templates folder as well 36005 22:47:36,960 --> 22:47:39,760 I'm going to go ahead and stop the 36006 22:47:38,120 --> 22:47:41,512 previous server and close that terminal 36007 22:47:39,760 --> 22:47:42,600 window and I'm going to open up this 36008 22:47:41,512 --> 22:47:45,400 version of 36009 22:47:42,600 --> 22:47:46,800 app.py so there's a few new lines here 36010 22:47:45,400 --> 22:47:48,640 and we'll give you these lines for 36011 22:47:46,800 --> 22:47:50,400 problems at nine but I've got some of 36012 22:47:48,640 --> 22:47:52,720 the familiar stuff up here including 36013 22:47:50,400 --> 22:47:54,912 this new redirect function we just used 36014 22:47:52,720 --> 22:47:57,120 and I have a session variable that comes 36015 22:47:54,912 --> 22:47:58,960 with flask 2 so what's nice again about 36016 22:47:57,120 --> 22:48:01,280 flask is that it deals with all of this 36017 22:47:58,960 --> 22:48:03,640 cookie stuff for you it sets the cookie 36018 22:48:01,280 --> 22:48:06,160 it checks the cookie and what flask does 36019 22:48:03,640 --> 22:48:09,080 for you is it gives you the abstraction 36020 22:48:06,160 --> 22:48:11,280 of a variable called session so that 36021 22:48:09,080 --> 22:48:13,360 anything you put in the session variable 36022 22:48:11,280 --> 22:48:15,440 which itself is a dictionary will be 36023 22:48:13,360 --> 22:48:17,512 there again and again and again whenever 36024 22:48:15,440 --> 22:48:19,512 that same user comes back a session is 36025 22:48:17,512 --> 22:48:22,800 how you implement essentially like the 36026 22:48:19,512 --> 22:48:24,552 proverbial shopping cart uh if I'm 36027 22:48:22,800 --> 22:48:27,000 logged into Amazon you're logged into 36028 22:48:24,552 --> 22:48:29,760 Amazon Amazon knows which of us is which 36029 22:48:27,000 --> 22:48:31,720 by way of that cookie and Amazon if 36030 22:48:29,760 --> 22:48:33,320 they're using flask provides the 36031 22:48:31,720 --> 22:48:35,512 programmer with a dictionary called 36032 22:48:33,320 --> 22:48:37,760 session and flasks make sure that when 36033 22:48:35,512 --> 22:48:39,720 Carter is visiting the site the code 36034 22:48:37,760 --> 22:48:42,120 uses his session object when I'm 36035 22:48:39,720 --> 22:48:43,680 visiting the site it uses my session 36036 22:48:42,120 --> 22:48:45,552 object but it's all implemented with 36037 22:48:43,680 --> 22:48:47,680 those same cookies this is the same as 36038 22:48:45,552 --> 22:48:49,720 before these lines are new and you'll 36039 22:48:47,680 --> 22:48:52,552 see these in problem Set n this is how 36040 22:48:49,720 --> 22:48:53,912 we enable sessions in a web application 36041 22:48:52,552 --> 22:48:55,720 and I'll just wave my hands at the 36042 22:48:53,912 --> 22:48:57,400 detail there's different ways to 36043 22:48:55,720 --> 22:48:59,160 implement sessions whether you use 36044 22:48:57,400 --> 22:49:01,080 cookies on the server cookies on the 36045 22:48:59,160 --> 22:49:03,000 browser or other things these just 36046 22:49:01,080 --> 22:49:05,480 ensure that we're storing the the 36047 22:49:03,000 --> 22:49:08,232 session information the shopping cart on 36048 22:49:05,480 --> 22:49:10,872 the server itself now down here let's go 36049 22:49:08,232 --> 22:49:14,360 ahead and do this let's go ahead and run 36050 22:49:10,872 --> 22:49:17,192 flask run so I can see what this app 36051 22:49:14,360 --> 22:49:19,552 does if I do this and visit the URL that 36052 22:49:17,192 --> 22:49:21,600 gets outputed you'll see a very simple 36053 22:49:19,552 --> 22:49:23,192 web page here and if I type in for 36054 22:49:21,600 --> 22:49:25,800 instance my name I'm not going to bother 36055 22:49:23,192 --> 22:49:28,320 with a password and click log in you'll 36056 22:49:25,800 --> 22:49:30,232 see that you are logged in as David and 36057 22:49:28,320 --> 22:49:32,680 now I can log out so I'm going to go 36058 22:49:30,232 --> 22:49:35,040 ahead and click log out and now it seems 36059 22:49:32,680 --> 22:49:36,360 to know that I'm not logged in again I 36060 22:49:35,040 --> 22:49:37,832 can log in as Carter because I didn't 36061 22:49:36,360 --> 22:49:39,760 bother implementing passwords for 36062 22:49:37,832 --> 22:49:41,912 Simplicity but now the site knows I'm 36063 22:49:39,760 --> 22:49:44,232 logged in as Carter better yet if I 36064 22:49:41,912 --> 22:49:46,480 reload reload reload or click this 36065 22:49:44,232 --> 22:49:49,760 button again and again notice it still 36066 22:49:46,480 --> 22:49:52,480 knows that I'm Carter uh until such time 36067 22:49:49,760 --> 22:49:54,400 as I log out all right well how is this 36068 22:49:52,480 --> 22:49:57,192 working well let's go back to vs code 36069 22:49:54,400 --> 22:49:59,040 here and let me scroll down to first 36070 22:49:57,192 --> 22:50:01,552 this route this is a very common 36071 22:49:59,040 --> 22:50:05,000 Paradigm here whereby I'm checking for 36072 22:50:01,552 --> 22:50:07,872 the index route if there is not a name 36073 22:50:05,000 --> 22:50:09,912 in the session redirect the user to SL 36074 22:50:07,872 --> 22:50:13,160 login now what does that mean well let 36075 22:50:09,912 --> 22:50:15,800 me go back to vs code here let me go to 36076 22:50:13,160 --> 22:50:17,000 the slash route so again your url will 36077 22:50:15,800 --> 22:50:19,360 be different but I'm just going to go to 36078 22:50:17,000 --> 22:50:21,760 slash and hit enter notice that I got 36079 22:50:19,360 --> 22:50:23,440 automatically redirected to login and so 36080 22:50:21,760 --> 22:50:24,800 many websites do this if you go to a 36081 22:50:23,440 --> 22:50:27,872 website and you're not logged in you're 36082 22:50:24,800 --> 22:50:29,232 very often redirected to SL login or SL 36083 22:50:27,872 --> 22:50:31,832 account or something like that where 36084 22:50:29,232 --> 22:50:33,960 you're prompted the code for doing that 36085 22:50:31,832 --> 22:50:35,232 is right here if there's no name in the 36086 22:50:33,960 --> 22:50:37,720 session if there's no name in the 36087 22:50:35,232 --> 22:50:40,600 shopping cart if you will go ahead and 36088 22:50:37,720 --> 22:50:42,552 return the user to login that route 36089 22:50:40,600 --> 22:50:45,320 otherwise implicitly if they are logged 36090 22:50:42,552 --> 22:50:47,720 in show them index.html so let's go down 36091 22:50:45,320 --> 22:50:49,960 that rabbit hole let me open up in VSS 36092 22:50:47,720 --> 22:50:52,552 code a second terminal window let me go 36093 22:50:49,960 --> 22:50:55,872 into Source 9 and go into this same 36094 22:50:52,552 --> 22:50:58,040 login demonstration and let me open up 36095 22:50:55,872 --> 22:51:00,192 the template called 36096 22:50:58,040 --> 22:51:01,720 index.html and here's all this is 36097 22:51:00,192 --> 22:51:03,192 there's some layout but who cares at 36098 22:51:01,720 --> 22:51:06,440 this point it's just the boiler plate 36099 22:51:03,192 --> 22:51:08,912 generic HTML here's the body block I 36100 22:51:06,440 --> 22:51:12,600 have this and this two is ginger because 36101 22:51:08,912 --> 22:51:14,912 of the curly brace and the percent sign 36102 22:51:12,600 --> 22:51:17,160 if there's a name in the session this is 36103 22:51:14,912 --> 22:51:19,600 just python syntax then say this 36104 22:51:17,160 --> 22:51:21,360 sentence you are logged in as whatever 36105 22:51:19,600 --> 22:51:22,960 name is in the session in the shopping 36106 22:51:21,360 --> 22:51:25,832 cart if you will and then I just have 36107 22:51:22,960 --> 22:51:28,400 this HTML link for logging the user out 36108 22:51:25,832 --> 22:51:30,480 else if there is no name in the session 36109 22:51:28,400 --> 22:51:32,480 logically just say you are not logged in 36110 22:51:30,480 --> 22:51:34,872 and give them a manual link for logging 36111 22:51:32,480 --> 22:51:37,680 in instead so that's all this particular 36112 22:51:34,872 --> 22:51:40,080 template does but how does the slash 36113 22:51:37,680 --> 22:51:42,600 login work well let's go into this other 36114 22:51:40,080 --> 22:51:45,232 template code of login. HTML to which 36115 22:51:42,600 --> 22:51:47,320 I'm redirected super simple this is just 36116 22:51:45,232 --> 22:51:48,912 copy paste from HTML before I've got a 36117 22:51:47,320 --> 22:51:51,080 login form that's going to have an 36118 22:51:48,912 --> 22:51:54,120 action of Slash login submits for 36119 22:51:51,080 --> 22:51:56,040 privacy sake just via post and then the 36120 22:51:54,120 --> 22:51:57,480 rest of this is just a simple form and 36121 22:51:56,040 --> 22:51:59,000 I'm using using an input type equals 36122 22:51:57,480 --> 22:52:01,280 submit instead of button equals type 36123 22:51:59,000 --> 22:52:03,192 equals submit but same idea here too and 36124 22:52:01,280 --> 22:52:06,280 if I go back to app.py well let's see 36125 22:52:03,192 --> 22:52:07,960 how login works all right it's a lot all 36126 22:52:06,280 --> 22:52:10,400 at once but they're relatively simple 36127 22:52:07,960 --> 22:52:14,400 reapplications of the same idea so if 36128 22:52:10,400 --> 22:52:17,512 the user visits SL login via get or post 36129 22:52:14,400 --> 22:52:19,232 call this function login if the user has 36130 22:52:17,512 --> 22:52:21,680 submitted via post and we saw this 36131 22:52:19,232 --> 22:52:25,360 technique before go ahead and do this on 36132 22:52:21,680 --> 22:52:28,720 line 23 store in this special session 36133 22:52:25,360 --> 22:52:32,760 variable that comes with flask a name 36134 22:52:28,720 --> 22:52:34,512 key and store in it the user's own name 36135 22:52:32,760 --> 22:52:36,680 so quote unquote name will have a value 36136 22:52:34,512 --> 22:52:38,800 of David or Carter or the like and as 36137 22:52:36,680 --> 22:52:40,800 soon as you do that redirect the user 36138 22:52:38,800 --> 22:52:42,680 back to slash just so they see the 36139 22:52:40,800 --> 22:52:44,832 homepage again and this is how Amazon 36140 22:52:42,680 --> 22:52:46,760 and all these other websites work too 36141 22:52:44,832 --> 22:52:48,600 otherwise if they visit this page 36142 22:52:46,760 --> 22:52:50,960 implicitly via get and even though I 36143 22:52:48,600 --> 22:52:52,400 didn't say equals equals get anywhere 36144 22:52:50,960 --> 22:52:54,320 that's sort of the implication because 36145 22:52:52,400 --> 22:52:56,400 if you can only get here via get or post 36146 22:52:54,320 --> 22:52:58,400 and we already handled post logically 36147 22:52:56,400 --> 22:53:01,080 All That Remains is get well then just 36148 22:52:58,400 --> 22:53:02,680 show them the login screen instead but 36149 22:53:01,080 --> 22:53:05,360 there's half a dozen ways we could 36150 22:53:02,680 --> 22:53:07,280 express that same logic and then for log 36151 22:53:05,360 --> 22:53:09,232 out this is kind of straightforward if 36152 22:53:07,280 --> 22:53:11,960 the user clicks that log out link and 36153 22:53:09,232 --> 22:53:14,832 ends up at SL logout this route well 36154 22:53:11,960 --> 22:53:18,040 just change the value of that key in the 36155 22:53:14,832 --> 22:53:20,232 session to be none effectively no Carter 36156 22:53:18,040 --> 22:53:23,600 is gone David's gone there's no one 36157 22:53:20,232 --> 22:53:25,000 logged in so that is all that's required 36158 22:53:23,600 --> 22:53:26,720 to actually implement the notion of 36159 22:53:25,000 --> 22:53:27,960 logging in and logging out of website 36160 22:53:26,720 --> 22:53:29,552 plus the password thing which should 36161 22:53:27,960 --> 22:53:31,680 probably involve a database but one 36162 22:53:29,552 --> 22:53:33,160 thing at a time and really session is 36163 22:53:31,680 --> 22:53:35,832 sort of like the code version of a 36164 22:53:33,160 --> 22:53:38,160 shopping cart whereby if I visit the 36165 22:53:35,832 --> 22:53:39,720 same code I get my own session object if 36166 22:53:38,160 --> 22:53:41,720 Carter visits the website he gets his 36167 22:53:39,720 --> 22:53:44,512 own session object and the way flask 36168 22:53:41,720 --> 22:53:46,832 Keeps Us straight is they put one cookie 36169 22:53:44,512 --> 22:53:49,552 on my computer a different cookie on his 36170 22:53:46,832 --> 22:53:51,832 computer and uses those to line up with 36171 22:53:49,552 --> 22:53:54,120 making sure the right session gets shown 36172 22:53:51,832 --> 22:54:00,360 to the right actual 36173 22:53:54,120 --> 22:54:00,360 user questions on this notion of 36174 22:54:00,912 --> 22:54:05,760 sessions no all right how about a couple 36175 22:54:03,600 --> 22:54:08,080 final examples just to tie this all 36176 22:54:05,760 --> 22:54:10,600 together let me go back into vs code 36177 22:54:08,080 --> 22:54:13,512 here let me quit my previous version of 36178 22:54:10,600 --> 22:54:15,600 flask let's go into Source 9 and go into 36179 22:54:13,512 --> 22:54:16,960 store which is a separate app Al 36180 22:54:15,600 --> 22:54:19,040 together and let's start by just running 36181 22:54:16,960 --> 22:54:21,760 flask to see what it does let's hover 36182 22:54:19,040 --> 22:54:23,872 over the URL and open it in another Tab 36183 22:54:21,760 --> 22:54:27,192 and this is pretty ugly too let me zoom 36184 22:54:23,872 --> 22:54:29,232 in but it's a very simple bookstore like 36185 22:54:27,192 --> 22:54:31,440 an early amazon.com for each of these 36186 22:54:29,232 --> 22:54:33,872 seven books here Each of which seems to 36187 22:54:31,440 --> 22:54:36,160 be like maybe this is H1 this is H2 H2 36188 22:54:33,872 --> 22:54:38,232 H2 H2 and then there's a button 36189 22:54:36,160 --> 22:54:40,600 underneath each well now let's use this 36190 22:54:38,232 --> 22:54:42,552 as an opportunity to kind of infer like 36191 22:54:40,600 --> 22:54:44,912 for any website how this thing works let 36192 22:54:42,552 --> 22:54:46,720 me go ahead and do view page source and 36193 22:54:44,912 --> 22:54:49,040 you can do this for any website on the 36194 22:54:46,720 --> 22:54:51,760 internet let's try to figure out how 36195 22:54:49,040 --> 22:54:54,232 this bookstore adds things to a cart 36196 22:54:51,760 --> 22:54:56,600 well here's the H1 tag uninteresting H2 36197 22:54:54,232 --> 22:55:00,192 H2 H2 so the juicy part is in these 36198 22:54:56,600 --> 22:55:02,232 forms each of these forms has an action 36199 22:55:00,192 --> 22:55:03,760 of SL cart so that's the route that's 36200 22:55:02,232 --> 22:55:06,600 going to be interesting in a moment and 36201 22:55:03,760 --> 22:55:08,960 it uses post for privacy sake each of 36202 22:55:06,600 --> 22:55:12,400 these forms like the deregister feature 36203 22:55:08,960 --> 22:55:15,232 for Carter has an ID attribute an ID 36204 22:55:12,400 --> 22:55:18,080 parameter that's hidden visually that 36205 22:55:15,232 --> 22:55:21,080 has a value of one or two or three so 36206 22:55:18,080 --> 22:55:22,760 like the unique uh uh like barcodes for 36207 22:55:21,080 --> 22:55:24,552 the books if you will but super small 36208 22:55:22,760 --> 22:55:26,320 numbers in our case and then each of 36209 22:55:24,552 --> 22:55:28,640 these other forms just had each of these 36210 22:55:26,320 --> 22:55:31,232 other books has an identical form except 36211 22:55:28,640 --> 22:55:33,320 for the value of this year now in this 36212 22:55:31,232 --> 22:55:35,800 case this isn't such a big deal that a 36213 22:55:33,320 --> 22:55:38,080 user could technically hack the HTML of 36214 22:55:35,800 --> 22:55:39,720 this bookstore amazon.com and change the 36215 22:55:38,080 --> 22:55:41,640 IDS because whoa what's the worst 36216 22:55:39,720 --> 22:55:43,600 they're going to do like buy more books 36217 22:55:41,640 --> 22:55:45,280 by adding more IDs to their shopping 36218 22:55:43,600 --> 22:55:47,160 cart like that's not a problem there's 36219 22:55:45,280 --> 22:55:49,512 no prices here it's just the unique 36220 22:55:47,160 --> 22:55:51,192 ideas of books so whereas the deregister 36221 22:55:49,512 --> 22:55:53,512 was maybe worrisome because you're 36222 22:55:51,192 --> 22:55:55,192 changing the server I think it's okay 36223 22:55:53,512 --> 22:55:57,640 because the user can only at worst buy 36224 22:55:55,192 --> 22:56:00,400 more books than then uh they might via 36225 22:55:57,640 --> 22:56:03,600 the buttons alone so how does this now 36226 22:56:00,400 --> 22:56:05,512 work well let's go into vs code again 36227 22:56:03,600 --> 22:56:08,832 here let me give myself another terminal 36228 22:56:05,512 --> 22:56:10,832 window and in Source 9/ store let me 36229 22:56:08,832 --> 22:56:12,960 open up app.py which is where all the 36230 22:56:10,832 --> 22:56:14,232 logic is so I'll flip through most of 36231 22:56:12,960 --> 22:56:16,000 this quickly because we've seen this 36232 22:56:14,232 --> 22:56:18,552 before these Imports are pretty much the 36233 22:56:16,000 --> 22:56:20,040 same as before this line is the same 36234 22:56:18,552 --> 22:56:22,760 this line is almost the same but the 36235 22:56:20,040 --> 22:56:25,600 database now is called store. DB instead 36236 22:56:22,760 --> 22:56:27,720 of frost. DB this is the boilerplate 36237 22:56:25,600 --> 22:56:29,832 code for just enabling sessions this 36238 22:56:27,720 --> 22:56:31,832 notion of a shopping cart and so let's 36239 22:56:29,832 --> 22:56:34,160 see how the index Works how is that I'm 36240 22:56:31,832 --> 22:56:37,232 seeing all seven books at once well in 36241 22:56:34,160 --> 22:56:38,872 this index function I'm using on line 19 36242 22:56:37,232 --> 22:56:40,640 select star from books to get all the 36243 22:56:38,872 --> 22:56:43,280 books from the database and then I'm 36244 22:56:40,640 --> 22:56:45,280 rendering a template called books. HTML 36245 22:56:43,280 --> 22:56:46,720 passing in as a placeholder all of those 36246 22:56:45,280 --> 22:56:48,760 books all right let's go down that 36247 22:56:46,720 --> 22:56:51,800 rabbit hole for a second let me open up 36248 22:56:48,760 --> 22:56:53,640 books. HTML in my templates directory 36249 22:56:51,800 --> 22:56:55,912 and here again even though it's you know 36250 22:56:53,640 --> 22:56:59,160 new today it's probably increasingly 36251 22:56:55,912 --> 22:57:01,800 familiar syntactically here's the H1 36252 22:56:59,160 --> 22:57:03,680 here is my Loop here's the H2 which is 36253 22:57:01,800 --> 22:57:06,440 going to Output the current book's title 36254 22:57:03,680 --> 22:57:07,832 in that Loop here's the form here's the 36255 22:57:06,440 --> 22:57:09,232 ID of that book that's going to be 36256 22:57:07,832 --> 22:57:11,912 outputed in the form so I didn't 36257 22:57:09,232 --> 22:57:14,400 manually type out seven long forms I 36258 22:57:11,912 --> 22:57:16,440 just did one in this template so that it 36259 22:57:14,400 --> 22:57:18,192 gets generated automatically literally 36260 22:57:16,440 --> 22:57:20,912 what a website CL Amazon would do to 36261 22:57:18,192 --> 22:57:23,600 show you 10 books at a time or 10 search 36262 22:57:20,912 --> 22:57:26,400 results or more at a time all right well 36263 22:57:23,600 --> 22:57:28,440 let's go to SLC cart which was the juicy 36264 22:57:26,400 --> 22:57:31,192 one and this one's longer but let's see 36265 22:57:28,440 --> 22:57:34,800 if we can reason through it if the user 36266 22:57:31,192 --> 22:57:36,872 submits via get or post to/ cart well we 36267 22:57:34,800 --> 22:57:39,040 first do this and this is just necessary 36268 22:57:36,872 --> 22:57:41,400 in some boilerplate rewrote we're going 36269 22:57:39,040 --> 22:57:43,600 to use the session object to store a 36270 22:57:41,400 --> 22:57:45,480 variable called cart that in this case 36271 22:57:43,600 --> 22:57:47,192 is going to be a list so session again 36272 22:57:45,480 --> 22:57:49,320 is just a dictionary you can put 36273 22:57:47,192 --> 22:57:53,320 anything in it you want previously we 36274 22:57:49,320 --> 22:57:55,832 put students names and sports now uh 36275 22:57:53,320 --> 22:57:59,160 what I want to do is actually sorry prev 36276 22:57:55,832 --> 22:58:01,552 previously we put uh the student name 36277 22:57:59,160 --> 22:58:05,480 the user's name in it now I'm going to 36278 22:58:01,552 --> 22:58:07,320 actually store a cart key whose value is 36279 22:58:05,480 --> 22:58:09,440 a list why because I want to aggregate 36280 22:58:07,320 --> 22:58:10,760 more and more books in this list all 36281 22:58:09,440 --> 22:58:12,960 right so that just makes sure that I 36282 22:58:10,760 --> 22:58:15,232 have at least an empty shopping cart the 36283 22:58:12,960 --> 22:58:17,480 very first time the user does this if 36284 22:58:15,232 --> 22:58:19,000 they visit this form via post let's go 36285 22:58:17,480 --> 22:58:21,680 ahead and get the ID of the book that 36286 22:58:19,000 --> 22:58:23,872 they posted if it is not empty if there 36287 22:58:21,680 --> 22:58:25,600 is a number like one or two or three 36288 22:58:23,872 --> 22:58:29,192 let's go ahead and go into the shopping 36289 22:58:25,600 --> 22:58:31,872 C cart which is a list per this line and 36290 22:58:29,192 --> 22:58:33,192 just append that ID so this list of 36291 22:58:31,872 --> 22:58:35,760 books in your shopping cart is going to 36292 22:58:33,192 --> 22:58:37,480 contain like one comma 2 comma four 36293 22:58:35,760 --> 22:58:39,800 comma six whatever books you're actually 36294 22:58:37,480 --> 22:58:42,400 buying and then the user gets redirected 36295 22:58:39,800 --> 22:58:44,552 to cart what if though the user got here 36296 22:58:42,400 --> 22:58:46,480 via get and not post well this one's 36297 22:58:44,552 --> 22:58:49,512 relatively straightforward if you just 36298 22:58:46,480 --> 22:58:53,720 visit slart we select star from books 36299 22:58:49,512 --> 22:58:56,000 where ID is in okay so this is 36300 22:58:53,720 --> 22:58:57,552 interesting this list and and this is a 36301 22:58:56,000 --> 22:58:58,912 syntax you might not have seen before 36302 22:58:57,552 --> 22:59:02,440 but if you read the documentation for 36303 22:58:58,912 --> 22:59:04,832 cs50's Library if you select something 36304 22:59:02,440 --> 22:59:07,120 and use a question mark placeholder and 36305 22:59:04,832 --> 22:59:10,552 the placeholder itself is a list we 36306 22:59:07,120 --> 22:59:12,480 output a comma separated list of values 36307 22:59:10,552 --> 22:59:15,400 just like you would use maybe in problem 36308 22:59:12,480 --> 22:59:17,832 Set uh seven for doing SQL queries on 36309 22:59:15,400 --> 22:59:20,000 your own so this just means show me only 36310 22:59:17,832 --> 22:59:21,552 those books in my shopping cart not 36311 22:59:20,000 --> 22:59:24,080 Carters not someone else's not in the 36312 22:59:21,552 --> 22:59:26,320 whole database only show me the books in 36313 22:59:24,080 --> 22:59:28,912 my shopping cart and then 36314 22:59:26,320 --> 22:59:31,872 render it as such so we only saw what 36315 22:59:28,912 --> 22:59:34,440 the catalog here looks like at slash 36316 22:59:31,872 --> 22:59:36,192 books Let's go ahead and in slash let's 36317 22:59:34,440 --> 22:59:40,680 go ahead and add maybe the first book to 36318 22:59:36,192 --> 22:59:43,160 my cart and now I see at SLC cart only 36319 22:59:40,680 --> 22:59:45,360 that first book whose ID is one let me 36320 22:59:43,160 --> 22:59:47,192 now go back to the bookstore here scroll 36321 22:59:45,360 --> 22:59:50,400 down to maybe the seventh book and add 36322 22:59:47,192 --> 22:59:52,872 that to my cart and now I see this here 36323 22:59:50,400 --> 22:59:56,320 too meanwhile all of this information is 36324 22:59:52,872 --> 22:59:58,320 stored in my session and so when I 36325 22:59:56,320 --> 23:00:01,640 reload this cart again and again the 36326 22:59:58,320 --> 23:00:03,640 reason I'm only seeing my two is because 36327 23:00:01,640 --> 23:00:05,960 we're checking only the list in my 36328 23:00:03,640 --> 23:00:07,280 session and flasks make sure again that 36329 23:00:05,960 --> 23:00:08,600 my session is different from your 36330 23:00:07,280 --> 23:00:11,040 session is different from Carter's 36331 23:00:08,600 --> 23:00:13,440 session as well but you write the code 36332 23:00:11,040 --> 23:00:15,912 once and it works for thousands millions 36333 23:00:13,440 --> 23:00:20,480 of people in 36334 23:00:15,912 --> 23:00:23,080 parallel any questions on 36335 23:00:20,480 --> 23:00:27,552 this this yes in the 36336 23:00:23,080 --> 23:00:27,552 back sorry say a little louder 36337 23:00:29,760 --> 23:00:35,232 uh so to recap so users will never have 36338 23:00:33,400 --> 23:00:36,832 the same session values theoretically 36339 23:00:35,232 --> 23:00:38,360 the cookie that gets planted does not 36340 23:00:36,832 --> 23:00:40,800 look like a smiley face for everyone 36341 23:00:38,360 --> 23:00:42,160 each of us gets a big random number 36342 23:00:40,800 --> 23:00:44,120 that's assigned to us so it' be like 36343 23:00:42,160 --> 23:00:45,832 each of us gets a completely unique hand 36344 23:00:44,120 --> 23:00:47,512 stamp that no one else can see the 36345 23:00:45,832 --> 23:00:50,232 reason no one else can see it is because 36346 23:00:47,512 --> 23:00:51,832 if the website's using https every time 36347 23:00:50,232 --> 23:00:53,600 this hand stamp is shown every time this 36348 23:00:51,832 --> 23:00:56,552 cookie is sent back and forth It's all 36349 23:00:53,600 --> 23:00:58,360 encrypted as well so each of us can even 36350 23:00:56,552 --> 23:00:59,832 if we have the same contents by 36351 23:00:58,360 --> 23:01:01,960 coincidence because we like the same 36352 23:00:59,832 --> 23:01:04,912 books they will be separate cookies 36353 23:01:01,960 --> 23:01:07,280 separate memory separate sessions behind 36354 23:01:04,912 --> 23:01:07,280 you 36355 23:01:08,480 --> 23:01:12,552 yeah really good question when does the 36356 23:01:10,600 --> 23:01:14,640 session end totally configurable 36357 23:01:12,552 --> 23:01:16,912 typically it ends when you close the tab 36358 23:01:14,640 --> 23:01:19,000 or when you quit the browser or you can 36359 23:01:16,912 --> 23:01:21,080 also configure cookies to themselves be 36360 23:01:19,000 --> 23:01:24,800 persistent for a day for a week for 36361 23:01:21,080 --> 23:01:27,800 longer so for instance when you log into 36362 23:01:24,800 --> 23:01:29,600 um uh say Gmail they plant a cookie on 36363 23:01:27,800 --> 23:01:31,120 your computer probably for a week a 36364 23:01:29,600 --> 23:01:32,720 month a year something like that because 36365 23:01:31,120 --> 23:01:34,720 it would be annoying and probably drive 36366 23:01:32,720 --> 23:01:36,872 you to like Outlook or something else if 36367 23:01:34,720 --> 23:01:38,552 you kept having to log into your account 36368 23:01:36,872 --> 23:01:40,192 whereas your bank account might actually 36369 23:01:38,552 --> 23:01:42,120 wait for you to just close the tab and 36370 23:01:40,192 --> 23:01:43,680 then for your own Financial safety they 36371 23:01:42,120 --> 23:01:45,912 just automatically delete the session 36372 23:01:43,680 --> 23:01:47,360 far sooner but totally configurable by 36373 23:01:45,912 --> 23:01:48,680 default as I'm using it it will 36374 23:01:47,360 --> 23:01:50,720 typically be thrown away when the 36375 23:01:48,680 --> 23:01:53,400 browser itself quits and here too is 36376 23:01:50,720 --> 23:01:55,000 another reason to develop websites using 36377 23:01:53,400 --> 23:01:56,360 incognito mode because if you want to 36378 23:01:55,000 --> 23:01:58,480 just throw throw away all of your 36379 23:01:56,360 --> 23:01:59,960 cookies you close the incognito window 36380 23:01:58,480 --> 23:02:01,720 mode open a new one and now you're 36381 23:01:59,960 --> 23:02:03,872 starting from scratch you don't have to 36382 23:02:01,720 --> 23:02:05,640 manually delete all your cookies which 36383 23:02:03,872 --> 23:02:09,320 could log you out of websites you 36384 23:02:05,640 --> 23:02:09,320 actually care about 36385 23:02:10,552 --> 23:02:14,832 yeah a good question when using sessions 36386 23:02:13,040 --> 23:02:16,440 if someone maliciously changes the value 36387 23:02:14,832 --> 23:02:19,000 of sub forms could it affect other 36388 23:02:16,440 --> 23:02:20,760 people theoretically no because the 36389 23:02:19,000 --> 23:02:22,400 worst you can do is like add books to 36390 23:02:20,760 --> 23:02:23,912 your own shopping cart that you don't 36391 23:02:22,400 --> 23:02:25,800 want there so at that point even though 36392 23:02:23,912 --> 23:02:29,720 it's on the server it doesn't affect you 36393 23:02:25,800 --> 23:02:32,640 or Carter or anyone else unless there is 36394 23:02:29,720 --> 23:02:34,912 something more globally happening like 36395 23:02:32,640 --> 23:02:36,600 registering or deregistering for a sport 36396 23:02:34,912 --> 23:02:38,480 or removing books from the Amazon 36397 23:02:36,600 --> 23:02:40,600 database that would be problematic but 36398 23:02:38,480 --> 23:02:43,512 in this case we're removing things only 36399 23:02:40,600 --> 23:02:46,280 from my own session that the website is 36400 23:02:43,512 --> 23:02:48,480 giving me all right the last topic for 36401 23:02:46,280 --> 23:02:50,280 today is this thing here which is sort 36402 23:02:48,480 --> 23:02:52,872 of everywhere nowadays these things 36403 23:02:50,280 --> 23:02:54,640 called apis or application programming 36404 23:02:52,872 --> 23:02:56,760 interfaces and this is a very generic 36405 23:02:54,640 --> 23:03:00,720 term in fact because any function you've 36406 23:02:56,760 --> 23:03:03,480 used in C in scratch in Python in SQL 36407 23:03:00,720 --> 23:03:06,440 are all apis like there is a standard 36408 23:03:03,480 --> 23:03:08,320 way of interfacing with those functions 36409 23:03:06,440 --> 23:03:09,832 they all have names they sometimes have 36410 23:03:08,320 --> 23:03:12,832 return values they sometimes have 36411 23:03:09,832 --> 23:03:16,160 arguments and an API is just how you use 36412 23:03:12,832 --> 23:03:18,160 a function or more generally an API just 36413 23:03:16,160 --> 23:03:20,480 specifies how you interact with some 36414 23:03:18,160 --> 23:03:23,000 service and so nowadays there's a lot of 36415 23:03:20,480 --> 23:03:24,912 web-based services that you can use to 36416 23:03:23,000 --> 23:03:27,720 get back data like the weather or the 36417 23:03:24,912 --> 23:03:30,400 current time or the database of Amazon 36418 23:03:27,720 --> 23:03:32,640 books for instance all might have apis 36419 23:03:30,400 --> 23:03:34,872 often web-based that allow you using 36420 23:03:32,640 --> 23:03:36,680 URLs or some other technology to just 36421 23:03:34,872 --> 23:03:38,912 get data from someone else as though 36422 23:03:36,680 --> 23:03:41,400 it's a function you're calling remotely 36423 23:03:38,912 --> 23:03:43,640 but HTTP is very often the mechanism 36424 23:03:41,400 --> 23:03:45,800 that's used to actually get data from 36425 23:03:43,640 --> 23:03:48,000 servers and the way the data can come 36426 23:03:45,800 --> 23:03:50,080 back can be as follows let me end with 36427 23:03:48,000 --> 23:03:53,680 one final example using some of our 36428 23:03:50,080 --> 23:03:55,280 familiar shows from uh weeks past let me 36429 23:03:53,680 --> 23:03:58,160 go ahead and close the old flask version 36430 23:03:55,280 --> 23:04:02,800 version go back into Source 9 and go 36431 23:03:58,160 --> 23:04:04,720 into how about an example called shows 36432 23:04:02,800 --> 23:04:06,080 and the first version of this zero I'm 36433 23:04:04,720 --> 23:04:08,600 just going to go ahead and run with 36434 23:04:06,080 --> 23:04:11,280 flask run I'll hover over my URL and 36435 23:04:08,600 --> 23:04:13,080 open it here and you'll see now that I 36436 23:04:11,280 --> 23:04:15,872 have a very simple form as we keep doing 36437 23:04:13,080 --> 23:04:18,480 today I'm going to type in like o f f i 36438 23:04:15,872 --> 23:04:21,552 c office into this search box and click 36439 23:04:18,480 --> 23:04:24,872 search and you'll see now that I ended 36440 23:04:21,552 --> 23:04:27,000 up at a URL ending in/ search question 36441 23:04:24,872 --> 23:04:29,400 mark Q equals office so this is like my 36442 23:04:27,000 --> 23:04:32,440 own baby version of google.com but I 36443 23:04:29,400 --> 23:04:34,680 implemented it myself and for any title 36444 23:04:32,440 --> 23:04:38,832 of a TV show from a couple of weeks past 36445 23:04:34,680 --> 23:04:41,440 that matches o i I spit it out into an 36446 23:04:38,832 --> 23:04:42,912 unordered list how is this working you 36447 23:04:41,440 --> 23:04:44,640 can maybe imagine even if you might not 36448 23:04:42,912 --> 23:04:46,912 be able to program this off the top of 36449 23:04:44,640 --> 23:04:50,192 your head certainly so soon let me go 36450 23:04:46,912 --> 23:04:52,600 into Source 9 let me go into uh show 36451 23:04:50,192 --> 23:04:55,232 zero let me open up 36452 23:04:52,600 --> 23:04:57,280 app.py and in this file you'll see that 36453 23:04:55,232 --> 23:04:59,480 that I'm grabbing a uh file called 36454 23:04:57,280 --> 23:05:00,720 shows. DB which is like a simpler 36455 23:04:59,480 --> 23:05:03,440 version of the one from a couple of 36456 23:05:00,720 --> 23:05:05,680 weeks past uh here is why I see the web 36457 23:05:03,440 --> 23:05:07,760 form my first route my index is super 36458 23:05:05,680 --> 23:05:09,480 simple it just spits out that form and 36459 23:05:07,760 --> 23:05:11,872 my search route like you can think of 36460 23:05:09,480 --> 23:05:14,512 this as google.com there only like four 36461 23:05:11,872 --> 23:05:17,232 lines of code so if the user sends data 36462 23:05:14,512 --> 23:05:19,400 to SL search this function called search 36463 23:05:17,232 --> 23:05:22,360 is called I declare a variable called 36464 23:05:19,400 --> 23:05:25,760 shows I execute a SQL command that is 36465 23:05:22,360 --> 23:05:27,720 Select star from shows where title like 36466 23:05:25,760 --> 23:05:30,720 question mark and the syntax here is a 36467 23:05:27,720 --> 23:05:34,040 little crazy but I want to prefix to the 36468 23:05:30,720 --> 23:05:36,320 user's input percent sign and suffix it 36469 23:05:34,040 --> 23:05:38,600 with a percent sign as well putting in 36470 23:05:36,320 --> 23:05:41,600 the between those two values the actual 36471 23:05:38,600 --> 23:05:43,040 input why in SQL what is it mean if you 36472 23:05:41,600 --> 23:05:45,480 have a percent sign to the left and to 36473 23:05:43,040 --> 23:05:48,120 the right nothing to do with Ginger 36474 23:05:45,480 --> 23:05:50,192 today yeah it's a it's a wild card so it 36475 23:05:48,120 --> 23:05:52,160 means match zero or more characters on 36476 23:05:50,192 --> 23:05:54,360 the left or match zero or more 36477 23:05:52,160 --> 23:05:55,872 characters on the right you have to do 36478 23:05:54,360 --> 23:05:57,680 the concatenation 36479 23:05:55,872 --> 23:05:59,120 as the second argument to this function 36480 23:05:57,680 --> 23:06:02,280 you can't do something clever like put 36481 23:05:59,120 --> 23:06:03,832 it here around the like the is the 36482 23:06:02,280 --> 23:06:06,552 placeholder that you plug these values 36483 23:06:03,832 --> 23:06:08,960 into but this just means hey SQL show me 36484 23:06:06,552 --> 23:06:11,400 all of the titles that have o f i 36485 23:06:08,960 --> 23:06:14,600 somewhere in them that gives me back an 36486 23:06:11,400 --> 23:06:16,600 a list of dictionaries I pass that in as 36487 23:06:14,600 --> 23:06:19,280 a placeholder for a variable called 36488 23:06:16,600 --> 23:06:20,480 shows and if we look at search. HTML 36489 23:06:19,280 --> 23:06:21,480 let's look at that in my templates 36490 23:06:20,480 --> 23:06:24,440 directory there's something called 36491 23:06:21,480 --> 23:06:26,600 search. HTML super simple I mean this is 36492 23:06:24,440 --> 23:06:29,040 like the essence of google.com search 36493 23:06:26,600 --> 23:06:31,040 results I'm using an unordered list to 36494 23:06:29,040 --> 23:06:33,040 keep things simple but I iterate over 36495 23:06:31,040 --> 23:06:35,320 every show in the shows list that came 36496 23:06:33,040 --> 23:06:36,800 back and I output An Li with each of 36497 23:06:35,320 --> 23:06:38,360 those shows titles and that's it now 36498 23:06:36,800 --> 23:06:40,080 Google has like Blue Links and like 36499 23:06:38,360 --> 23:06:42,120 little previews and other text the first 36500 23:06:40,080 --> 23:06:43,912 sentence or so from each page but like 36501 23:06:42,120 --> 23:06:46,120 that's the idea like this is really 36502 23:06:43,912 --> 23:06:49,640 similar in spirit to what google.com 36503 23:06:46,120 --> 23:06:52,000 search does for you now how is this 36504 23:06:49,640 --> 23:06:54,872 working there's no API involved here yet 36505 23:06:52,000 --> 23:06:56,640 this is just very basic HTTP I submit 36506 23:06:54,872 --> 23:06:58,680 the the form I go to another route and I 36507 23:06:56,640 --> 23:07:01,320 get back the results but check out this 36508 23:06:58,680 --> 23:07:03,960 version Let me close these tabs here and 36509 23:07:01,320 --> 23:07:06,912 open my first terminal window let me go 36510 23:07:03,960 --> 23:07:09,552 into shows one from today's Source 9 36511 23:07:06,912 --> 23:07:11,480 directory and do flask run this time let 36512 23:07:09,552 --> 23:07:13,800 me go ahead and hover over that URL and 36513 23:07:11,480 --> 23:07:16,000 open it here and gone now is the submit 36514 23:07:13,800 --> 23:07:18,232 button now I'm going to make an user 36515 23:07:16,000 --> 23:07:21,280 interface that uses a technique called 36516 23:07:18,232 --> 23:07:23,600 Ajax for asynchronous uh JavaScript and 36517 23:07:21,280 --> 23:07:25,080 XML which is somewhat of a data term 36518 23:07:23,600 --> 23:07:27,960 because we're not using something called 36519 23:07:25,080 --> 23:07:30,400 ml anymore but Ajax is a technique 36520 23:07:27,960 --> 23:07:32,600 whereby you don't have to submit forms 36521 23:07:30,400 --> 23:07:35,440 anymore to get more data from the server 36522 23:07:32,600 --> 23:07:37,512 you can use JavaScript per last week 36523 23:07:35,440 --> 23:07:39,872 listen for an event like the key press 36524 23:07:37,512 --> 23:07:42,120 coming down or up and as soon as you 36525 23:07:39,872 --> 23:07:43,960 hear such an event you can secretly in 36526 23:07:42,120 --> 23:07:46,440 JavaScript code send a request to the 36527 23:07:43,960 --> 23:07:49,280 server to get back more data and then 36528 23:07:46,440 --> 23:07:51,080 plug it into the Dom the tree in the 36529 23:07:49,280 --> 23:07:52,832 computer's memory and this just makes 36530 23:07:51,080 --> 23:07:55,192 for more seamless experiences like 36531 23:07:52,832 --> 23:07:57,760 autocomplete on any website so now let 36532 23:07:55,192 --> 23:08:02,552 me try typing o okay we got auto 36533 23:07:57,760 --> 23:08:05,440 complete super fast f f i c e and you'll 36534 23:08:02,552 --> 23:08:07,440 see every time I add more keys to my 36535 23:08:05,440 --> 23:08:09,440 input I'm doing another search another 36536 23:08:07,440 --> 23:08:11,440 search another search and the data is 36537 23:08:09,440 --> 23:08:13,232 changing now how is this working well 36538 23:08:11,440 --> 23:08:16,600 let me go back to vs code here and in my 36539 23:08:13,232 --> 23:08:20,040 other terminal window let me open up uh 36540 23:08:16,600 --> 23:08:21,512 app.py and in app.py you'll see that 36541 23:08:20,040 --> 23:08:25,440 there's still a 36542 23:08:21,512 --> 23:08:28,440 search route down below that turns a 36543 23:08:25,440 --> 23:08:33,000 search template but watch this let me go 36544 23:08:28,440 --> 23:08:36,400 into templates search. HTML and notice 36545 23:08:33,000 --> 23:08:40,440 here that we're indeed getting back an 36546 23:08:36,400 --> 23:08:44,720 unordered list of shows again and again 36547 23:08:40,440 --> 23:08:48,192 and again and this HTML that's coming 36548 23:08:44,720 --> 23:08:50,120 back let me go here let me open my 36549 23:08:48,192 --> 23:08:52,232 terminal oh sorry this is the wrong 36550 23:08:50,120 --> 23:08:55,640 version uh sorry I was in the wrong 36551 23:08:52,232 --> 23:08:58,232 folder let's fix this and shows one code 36552 23:08:55,640 --> 23:08:59,872 of app.py it's almost the same thing 36553 23:08:58,232 --> 23:09:02,440 inserch 36554 23:08:59,872 --> 23:09:03,872 here okay well I changed this slightly 36555 23:09:02,440 --> 23:09:06,360 let me show you this version of search 36556 23:09:03,872 --> 23:09:09,120 if I open up app.py here's my search 36557 23:09:06,360 --> 23:09:11,040 route I'm getting a variable called Q 36558 23:09:09,120 --> 23:09:13,400 giving it the value of whatever request. 36559 23:09:11,040 --> 23:09:15,440 RX has from the user like Q equals 36560 23:09:13,400 --> 23:09:17,320 office and then I'm checking if the user 36561 23:09:15,440 --> 23:09:19,440 actually typed something in execute this 36562 23:09:17,320 --> 23:09:21,552 SQL query select star from shows where 36563 23:09:19,440 --> 23:09:23,280 title is like that using the same and 36564 23:09:21,552 --> 23:09:25,360 this time just to keep things efficient 36565 23:09:23,280 --> 23:09:27,640 I limited the total results to 50 36566 23:09:25,360 --> 23:09:29,912 instead of an infinite number otherwise 36567 23:09:27,640 --> 23:09:32,120 if the user type nothing just to be 36568 23:09:29,912 --> 23:09:33,832 super safe here I'm setting shows equal 36569 23:09:32,120 --> 23:09:35,600 to an empty list so if you don't type 36570 23:09:33,832 --> 23:09:37,600 anything there's nothing to show and no 36571 23:09:35,600 --> 23:09:40,192 matter what I render this template 36572 23:09:37,600 --> 23:09:43,600 called search. HTML well let's look at 36573 23:09:40,192 --> 23:09:46,512 that if I open up templat search. HTML 36574 23:09:43,600 --> 23:09:49,160 this time there's no layout there's no 36575 23:09:46,512 --> 23:09:51,400 inheritance of that layout. HTML I am 36576 23:09:49,160 --> 23:09:54,760 literally generating just a whole bunch 36577 23:09:51,400 --> 23:09:55,960 of Li fragments why well let's see what 36578 23:09:54,760 --> 23:09:58,552 what's happening in the browser and this 36579 23:09:55,960 --> 23:10:00,640 is the beginning of an API if I wanted 36580 23:09:58,552 --> 23:10:02,360 to make this API available this 36581 23:10:00,640 --> 23:10:03,912 application programming interface I 36582 23:10:02,360 --> 23:10:07,192 could tell the world that if you want to 36583 23:10:03,912 --> 23:10:10,872 search for TV shows in my database go to 36584 23:10:07,192 --> 23:10:15,160 URL that's something something/ search 36585 23:10:10,872 --> 23:10:17,232 question mark Q equals office or cat or 36586 23:10:15,160 --> 23:10:20,120 dog or anything else and what I will 36587 23:10:17,232 --> 23:10:22,120 return to you is this enter I will just 36588 23:10:20,120 --> 23:10:23,640 give you a whole bunch of Li tags which 36589 23:10:22,120 --> 23:10:26,480 almost looks the same but let me view 36590 23:10:23,640 --> 23:10:28,640 the page source only am I going to hand 36591 23:10:26,480 --> 23:10:30,640 you back a fragment of HTML I'm not 36592 23:10:28,640 --> 23:10:32,120 giving you an HTML tag a body tag a 36593 23:10:30,640 --> 23:10:34,760 title tag a head tag I'm not giving you 36594 23:10:32,120 --> 23:10:36,720 a web page I'm giving you a fragment of 36595 23:10:34,760 --> 23:10:40,232 HTML that you can now do whatever you 36596 23:10:36,720 --> 23:10:43,720 want including insert this into your own 36597 23:10:40,232 --> 23:10:46,080 unordered list so notice what happens in 36598 23:10:43,720 --> 23:10:49,192 this actual app if I go back to vs code 36599 23:10:46,080 --> 23:10:51,912 here let me open up my index template 36600 23:10:49,192 --> 23:10:55,760 here and you'll see some JavaScript 36601 23:10:51,912 --> 23:10:58,120 magic so in JavaScript here in my form 36602 23:10:55,760 --> 23:11:01,680 that only had the text box and no button 36603 23:10:58,120 --> 23:11:03,720 what am I doing in a script tag here I 36604 23:11:01,680 --> 23:11:05,512 am creating a variable called input and 36605 23:11:03,720 --> 23:11:08,760 I'm using this function called query 36606 23:11:05,512 --> 23:11:11,552 selector that just gets me a reference 36607 23:11:08,760 --> 23:11:13,600 to the input text box on the form so I 36608 23:11:11,552 --> 23:11:15,552 can see what the human typed this is a 36609 23:11:13,600 --> 23:11:18,640 little different today but I'm using 36610 23:11:15,552 --> 23:11:20,552 input. atevent listener which is a way 36611 23:11:18,640 --> 23:11:23,040 in JavaScript to tell it just like in 36612 23:11:20,552 --> 23:11:24,800 scratch listen for something to happen 36613 23:11:23,040 --> 23:11:27,000 like the green flag being clicked but in 36614 23:11:24,800 --> 23:11:29,872 this case listen for an event that 36615 23:11:27,000 --> 23:11:31,552 involves input that is like typing on 36616 23:11:29,872 --> 23:11:35,160 the keyboard whether it's by a copy 36617 23:11:31,552 --> 23:11:37,192 paste manual input or anything else then 36618 23:11:35,160 --> 23:11:39,120 whenever that happens call this function 36619 23:11:37,192 --> 23:11:41,832 and async stands for asynchronous this 36620 23:11:39,120 --> 23:11:43,360 is a term of art which means that this 36621 23:11:41,832 --> 23:11:44,872 this function might take like a split 36622 23:11:43,360 --> 23:11:47,000 second maybe even a second or two to 36623 23:11:44,872 --> 23:11:48,912 execute so it's going to do it behind 36624 23:11:47,000 --> 23:11:51,760 the scenes like in the background so to 36625 23:11:48,912 --> 23:11:53,512 speak and what is it going to do well 36626 23:11:51,760 --> 23:11:55,192 it's going to call a JavaScript function 36627 23:11:53,512 --> 23:11:58,232 that all browsers now Support called 36628 23:11:55,192 --> 23:12:00,232 Fetch which is a function that uses HTTP 36629 23:11:58,232 --> 23:12:02,192 to go fetch more data via the from the 36630 23:12:00,232 --> 23:12:05,320 server it's going to fetch data from a 36631 23:12:02,192 --> 23:12:07,280 route called SL search question mark Q 36632 23:12:05,320 --> 23:12:09,280 equals and whatever the value is that 36633 23:12:07,280 --> 23:12:11,552 the user typed in so I'm just sort of 36634 23:12:09,280 --> 23:12:14,232 manually creating my own mini URL and 36635 23:12:11,552 --> 23:12:17,280 telling JavaScript go fetch me that HTML 36636 23:12:14,232 --> 23:12:19,192 when it comes back via this line of text 36637 23:12:17,280 --> 23:12:21,280 here called response. text and let me 36638 23:12:19,192 --> 23:12:22,640 wave my hand at await await just means 36639 23:12:21,280 --> 23:12:24,120 this might not come back immediately 36640 23:12:22,640 --> 23:12:25,760 let's await the response and when it 36641 23:12:24,120 --> 23:12:28,000 does come and then let's execute this 36642 23:12:25,760 --> 23:12:30,192 code I'm going to do this I'm going to 36643 23:12:28,000 --> 23:12:33,280 search the document the whole web page 36644 23:12:30,192 --> 23:12:35,040 for this UL tag which is somewhere in 36645 23:12:33,280 --> 23:12:37,760 this page that we'll see in a moment 36646 23:12:35,040 --> 23:12:40,600 change its inner HTML to be that 36647 23:12:37,760 --> 23:12:42,640 fragment of Li Li Li of all of those 36648 23:12:40,600 --> 23:12:44,552 matching shows and where does this all 36649 23:12:42,640 --> 23:12:47,280 go well if we scroll up here you'll 36650 23:12:44,552 --> 23:12:50,320 notice that there's my usual HTML up at 36651 23:12:47,280 --> 23:12:52,080 top head tag body tag and all of that 36652 23:12:50,320 --> 23:12:54,480 there's the text box that we've talked 36653 23:12:52,080 --> 23:12:55,912 about already there's no button for 36654 23:12:54,480 --> 23:12:58,040 submitting cuz it all happens 36655 23:12:55,912 --> 23:13:00,080 automatically but there is by default 36656 23:12:58,040 --> 23:13:03,720 this empty UL that has nothing by 36657 23:13:00,080 --> 23:13:06,512 default until we start using that API 36658 23:13:03,720 --> 23:13:10,552 and the final flourish here is this this 36659 23:13:06,512 --> 23:13:12,280 is kind of a uh ugly sloppy way to get 36660 23:13:10,552 --> 23:13:14,512 data from a server to just get back like 36661 23:13:12,280 --> 23:13:17,120 a fragment of HTML like what if I'm not 36662 23:13:14,512 --> 23:13:19,680 using HTML I want to store these uh TV 36663 23:13:17,120 --> 23:13:21,720 shows in a PDF or in some other wet tag 36664 23:13:19,680 --> 23:13:23,232 in a table or something like that it 36665 23:13:21,720 --> 23:13:25,640 doesn't really make sense for the server 36666 23:13:23,232 --> 23:13:28,120 to be presuming that I want Li tags 36667 23:13:25,640 --> 23:13:30,720 surrounding each of my data better would 36668 23:13:28,120 --> 23:13:32,960 be to get a more generic format back and 36669 23:13:30,720 --> 23:13:36,160 that format is almost always nowadays 36670 23:13:32,960 --> 23:13:38,960 called this our final acronym Json 36671 23:13:36,160 --> 23:13:41,640 JavaScript object notation and let me do 36672 23:13:38,960 --> 23:13:45,440 this let me close these two tabs here 36673 23:13:41,640 --> 23:13:47,600 and open my terminal window and C cancel 36674 23:13:45,440 --> 23:13:49,720 the previous version of flask that was 36675 23:13:47,600 --> 23:13:51,552 running let me close this version and 36676 23:13:49,720 --> 23:13:54,552 look at our final version called shows 36677 23:13:51,552 --> 23:13:56,552 two and do flask run on I'm going to 36678 23:13:54,552 --> 23:13:59,160 hover over that URL and open it in a 36679 23:13:56,552 --> 23:14:03,232 browser and I'm going to manually visit 36680 23:13:59,160 --> 23:14:07,040 after zooming in let's do again slash uh 36681 23:14:03,232 --> 23:14:10,400 search question mark uh Q equals Office 36682 23:14:07,040 --> 23:14:11,912 enter and this is what Json looks like 36683 23:14:10,400 --> 23:14:13,680 now at a glance this does not seem like 36684 23:14:11,912 --> 23:14:15,720 an improvement like this looks crazy 36685 23:14:13,680 --> 23:14:17,512 that it's just this Big Blob of text but 36686 23:14:15,720 --> 23:14:20,440 it's just enough text for the computer 36687 23:14:17,512 --> 23:14:22,960 to be able to process it reliably notice 36688 23:14:20,440 --> 23:14:24,360 that there's a cur a square bracket here 36689 23:14:22,960 --> 23:14:25,832 and if I actually scroll to the Bott 36690 23:14:24,360 --> 23:14:27,872 botom there' be a closed square bracket 36691 23:14:25,832 --> 23:14:31,192 like way down there inside of that 36692 23:14:27,872 --> 23:14:33,872 square bracket is a curly brace then ID 36693 23:14:31,192 --> 23:14:36,480 colon and then a number then a title 36694 23:14:33,872 --> 23:14:38,720 quote unquote colon and then the title 36695 23:14:36,480 --> 23:14:40,400 and then the closed curly brace so what 36696 23:14:38,720 --> 23:14:42,232 you're seeing in JavaScript object 36697 23:14:40,400 --> 23:14:44,480 notation is a very standard super 36698 23:14:42,232 --> 23:14:46,800 popular format that's just text that 36699 23:14:44,480 --> 23:14:49,192 still uses square brackets for lists AKA 36700 23:14:46,800 --> 23:14:51,720 arrays that still uses curly braces for 36701 23:14:49,192 --> 23:14:54,120 dictionaries key value pairs so what you 36702 23:14:51,720 --> 23:14:56,120 see here is a massive list up to 50 I 36703 23:14:54,120 --> 23:14:59,912 think think shows that came back from 36704 23:14:56,120 --> 23:15:02,600 this API Each of which has a dictionary 36705 23:14:59,912 --> 23:15:05,280 if you will an object of key value pairs 36706 23:15:02,600 --> 23:15:07,680 what keys and values an ID key and a 36707 23:15:05,280 --> 23:15:09,512 title key Each of which has a value 36708 23:15:07,680 --> 23:15:11,800 respectively and this is the same data 36709 23:15:09,512 --> 23:15:14,280 from IMDb some of which you might be 36710 23:15:11,800 --> 23:15:16,832 recalling visually this is just a very 36711 23:15:14,280 --> 23:15:18,232 raw computer friendly way of returning a 36712 23:15:16,832 --> 23:15:21,720 whole bunch of data that we humans don't 36713 23:15:18,232 --> 23:15:23,872 need to see but I can use this data by 36714 23:15:21,720 --> 23:15:25,552 going back into vs code let me open 36715 23:15:23,872 --> 23:15:26,640 another terminal window and go into 36716 23:15:25,552 --> 23:15:31,040 Source 36717 23:15:26,640 --> 23:15:35,600 9/ shows 2 and in here let me go ahead 36718 23:15:31,040 --> 23:15:38,960 and open up how about uh templates 36719 23:15:35,600 --> 23:15:41,600 index.html which previously just used 36720 23:15:38,960 --> 23:15:43,000 that inner HTML trick and this is not 36721 23:15:41,600 --> 23:15:45,360 going to impress you're not going to be 36722 23:15:43,000 --> 23:15:47,480 pleased with this syntax but let me just 36723 23:15:45,360 --> 23:15:49,912 at least explain what we're doing it 36724 23:15:47,480 --> 23:15:51,760 turns out that Json is just the better 36725 23:15:49,912 --> 23:15:54,280 way in general the more generic 36726 23:15:51,760 --> 23:15:55,960 multi-purpose user agnostic language 36727 23:15:54,280 --> 23:15:57,680 agnostic way of returning data from a 36728 23:15:55,960 --> 23:15:58,912 server because it's just text so it 36729 23:15:57,680 --> 23:16:01,912 doesn't matter if you're using python or 36730 23:15:58,912 --> 23:16:03,120 C or C++ or JavaScript or Ruby or PHP or 36731 23:16:01,912 --> 23:16:06,360 something else like all of those 36732 23:16:03,120 --> 23:16:08,160 languages can process Json information 36733 23:16:06,360 --> 23:16:10,360 and indeed here is some JavaScript that 36734 23:16:08,160 --> 23:16:12,480 does just that same code as before 36735 23:16:10,360 --> 23:16:14,232 initially I declare a variable called 36736 23:16:12,480 --> 23:16:17,360 input that gives me access to the user's 36737 23:16:14,232 --> 23:16:19,512 text box I listen for input like key 36738 23:16:17,360 --> 23:16:22,760 strokes going up and down and when they 36739 23:16:19,512 --> 23:16:24,720 happen I call this Anonymous function uh 36740 23:16:22,760 --> 23:16:27,160 I fetch data from the server using the 36741 23:16:24,720 --> 23:16:29,440 exact same code as before search 36742 23:16:27,160 --> 23:16:32,440 question mark Q equals office or 36743 23:16:29,440 --> 23:16:35,680 anything else and then this is just now 36744 23:16:32,440 --> 23:16:38,832 new code that I use to convert that Json 36745 23:16:35,680 --> 23:16:41,480 data into my own HTML format be it an 36746 23:16:38,832 --> 23:16:43,720 unordered list an ordered list a table 36747 23:16:41,480 --> 23:16:45,552 or anything else what am I doing I've 36748 23:16:43,720 --> 23:16:48,480 got a variable called HTML initialized 36749 23:16:45,552 --> 23:16:51,440 to nothing so I've got no HTML initially 36750 23:16:48,480 --> 23:16:53,600 I then iterate over every ID in those 36751 23:16:51,440 --> 23:16:55,680 shows so every one of IMDB's unique 36752 23:16:53,600 --> 23:16:59,232 identifiers I iterate over them one at a 36753 23:16:55,680 --> 23:17:03,000 time and then I go into the show at its 36754 23:16:59,232 --> 23:17:05,192 ID location and I grab its title and 36755 23:17:03,000 --> 23:17:08,232 this 36756 23:17:05,192 --> 23:17:11,600 is forget this for just a moment I then 36757 23:17:08,232 --> 23:17:15,480 take this HTML variable concatenate or 36758 23:17:11,600 --> 23:17:18,480 join onto it my own Li tag plus the 36759 23:17:15,480 --> 23:17:20,000 title plus the close Li tag and I 36760 23:17:18,480 --> 23:17:22,000 skipped this because it got scary pretty 36761 23:17:20,000 --> 23:17:24,552 fast but it turns out that if some TV 36762 23:17:22,000 --> 23:17:27,552 shows have actually angled brackets in 36763 23:17:24,552 --> 23:17:29,480 that could break my HTML entirely so it 36764 23:17:27,552 --> 23:17:31,640 turns out you might recall super briefly 36765 23:17:29,480 --> 23:17:34,160 last week we had the copyright symbol 36766 23:17:31,640 --> 23:17:37,160 using an HTML entity using the Ampersand 36767 23:17:34,160 --> 23:17:39,480 and the hash symbol and 169 semicolon it 36768 23:17:37,160 --> 23:17:41,912 turns out there are other such cryptic 36769 23:17:39,480 --> 23:17:44,040 sequences of characters that represent 36770 23:17:41,912 --> 23:17:46,400 otherwise dangerous or untypable 36771 23:17:44,040 --> 23:17:47,720 characters like this which could confuse 36772 23:17:46,400 --> 23:17:49,800 the computer into thinking it's at the 36773 23:17:47,720 --> 23:17:51,320 beginning of a tag and an Amper sand 36774 23:17:49,800 --> 23:17:53,640 which could similarly trick the computer 36775 23:17:51,320 --> 23:17:55,912 into thinking that it's a entity which 36776 23:17:53,640 --> 23:17:57,720 it isn't but long story short there are 36777 23:17:55,912 --> 23:17:59,600 libraries thankfully that handle much of 36778 23:17:57,720 --> 23:18:01,232 this for you for our purposes the 36779 23:17:59,600 --> 23:18:02,160 takeaway is that now that you understand 36780 23:18:01,232 --> 23:18:04,280 a bit of 36781 23:18:02,160 --> 23:18:06,720 HTTP uh now that you understand a bit of 36782 23:18:04,280 --> 23:18:08,760 HTML CSS and JavaScript all of which 36783 23:18:06,720 --> 23:18:10,512 they have their roles you can use them 36784 23:18:08,760 --> 23:18:12,720 ultimately to start assembling your own 36785 23:18:10,512 --> 23:18:14,960 web applications as you will for problem 36786 23:18:12,720 --> 23:18:16,120 set nine um stitching together all of 36787 23:18:14,960 --> 23:18:18,360 those languages and building 36788 23:18:16,120 --> 23:18:21,232 full-fledged web applications mobile 36789 23:18:18,360 --> 23:18:24,000 applications or anything more and for 36790 23:18:21,232 --> 23:18:25,872 that I think we are all set and if the 36791 23:18:24,000 --> 23:18:27,440 first one up here uh can have these 36792 23:18:25,872 --> 23:18:30,832 cookies as well we'll see you next time 36793 23:18:27,440 --> 23:18:30,832 for our very last cs50 36794 23:18:31,870 --> 23:18:39,979 [Music] 36795 23:18:45,960 --> 23:18:55,912 [Music] 36796 23:18:52,912 --> 23:18:55,912 lecture 36797 23:19:10,570 --> 23:19:29,220 [Music] 36798 23:19:43,070 --> 23:19:46,179 [Music] 36799 23:19:53,010 --> 23:20:06,370 [Music] 36800 23:20:12,470 --> 23:20:23,249 [Music] 36801 23:20:26,832 --> 23:20:31,600 good afternoon my name is Sarah and my 36802 23:20:29,232 --> 23:20:34,800 name is Grant and we are the Harvard 36803 23:20:31,600 --> 23:20:35,760 crocodillos and pitches now Sarah and I 36804 23:20:34,800 --> 23:20:37,912 understand that today is the final 36805 23:20:35,760 --> 23:20:40,640 lecture of cs50 it's been a tough 36806 23:20:37,912 --> 23:20:43,080 semester we made it through pets four 36807 23:20:40,640 --> 23:20:44,720 five and even Finance now I know this is 36808 23:20:43,080 --> 23:20:46,280 an unpopular opinion but I particularly 36809 23:20:44,720 --> 23:20:47,232 enjoyed Finance I spent a lot of time 36810 23:20:46,280 --> 23:20:52,192 with my 36811 23:20:47,232 --> 23:20:54,760 flask the P set there was a p 36812 23:20:52,192 --> 23:20:57,800 set well um at least things are looking 36813 23:20:54,760 --> 23:21:00,192 up um today is our last lecture and look 36814 23:20:57,800 --> 23:21:03,040 how far we've come if I were an emoji 36815 23:21:00,192 --> 23:21:05,000 right now I'd be the face with tears of 36816 23:21:03,040 --> 23:21:06,960 joy sorry about that we're just trying 36817 23:21:05,000 --> 23:21:09,512 to work some cs50 references into the uh 36818 23:21:06,960 --> 23:21:13,600 intro I I mean uh boy I sure hope this 36819 23:21:09,512 --> 23:21:17,232 uh tide man doesn't run off my Mario 36820 23:21:13,600 --> 23:21:21,512 filter you could say that for Loop x 36821 23:21:17,232 --> 23:21:23,872 equals Open Bracket one comma 2 close 36822 23:21:21,512 --> 23:21:25,120 bracket boy I sure wish we had checked 36823 23:21:23,872 --> 23:21:26,512 to see if these jokes were funny when we 36824 23:21:25,120 --> 23:21:29,080 wrote 36825 23:21:26,512 --> 23:21:30,480 them any who we hope that you'll enjoy 36826 23:21:29,080 --> 23:21:34,280 this brief 36827 23:21:30,480 --> 23:21:34,280 [Music] 36828 23:21:34,760 --> 23:21:39,832 serenade your 36829 23:21:36,832 --> 23:21:39,832 boot 36830 23:21:41,970 --> 23:21:45,020 [Music] 36831 23:21:46,912 --> 23:21:56,232 KN House of well there's 36832 23:21:50,800 --> 23:21:56,232 friers Andy truck barbecue ribs Tri 36833 23:21:59,360 --> 23:22:02,499 [Music] 36834 23:22:08,720 --> 23:22:16,120 the boy 36835 23:22:10,720 --> 23:22:19,512 boy bo boy bo bo boy boy down at the 36836 23:22:16,120 --> 23:22:21,832 house the house of well your root and 36837 23:22:19,512 --> 23:22:24,232 you walk on down to a knock down Shack 36838 23:22:21,832 --> 23:22:29,040 on the edge of town There's a l 36839 23:22:24,232 --> 23:22:33,232 there just quit you see 36840 23:22:29,040 --> 23:22:33,232 fall down the house the house 36841 23:22:49,120 --> 23:22:55,552 [Applause] 36842 23:22:50,800 --> 23:22:55,552 of up your boots and you walk on down 36843 23:23:03,440 --> 23:23:08,760 at the 36844 23:23:04,232 --> 23:23:08,760 house the House of Blue 36845 23:23:12,640 --> 23:23:19,440 Light to your down at the of 36846 23:23:22,480 --> 23:23:25,480 light 36847 23:23:28,090 --> 23:23:35,160 [Applause] 36848 23:23:33,120 --> 23:23:36,960 good afternoon everyone we are the 36849 23:23:35,160 --> 23:23:38,640 Harvard crocodillos and it is such an 36850 23:23:36,960 --> 23:23:40,760 honor to be here with the Radcliff 36851 23:23:38,640 --> 23:23:42,872 pitches performing for cs50's final 36852 23:23:40,760 --> 23:23:46,912 lecture congratulations to everyone and 36853 23:23:42,872 --> 23:23:46,912 we hope you'll enjoy this our tribute to 36854 23:23:51,040 --> 23:23:56,360 cs50 one two one 1 2 3 36855 23:24:04,232 --> 23:24:17,080 4 C is for the language I once knew O is 36856 23:24:11,040 --> 23:24:21,960 for all notation I must do D is for 36857 23:24:17,080 --> 23:24:26,480 dynamic flask run and finance it is even 36858 23:24:21,960 --> 23:24:31,160 more than David May in canor so code is 36859 23:24:26,480 --> 23:24:34,192 all that I can give to you to youe 36860 23:24:31,160 --> 23:24:37,912 debugging it since 36861 23:24:34,192 --> 23:24:41,040 PE soon deadlines I'll make it hit 36862 23:24:37,912 --> 23:24:42,610 compile and please don't break it code 36863 23:24:41,040 --> 23:24:46,270 was made by 36864 23:24:42,610 --> 23:24:46,270 [Music] 36865 23:24:46,400 --> 23:24:49,400 foru 36866 23:24:49,912 --> 23:24:58,000 I for notation I must do 36867 23:24:55,400 --> 23:24:58,000 is 36868 23:24:59,512 --> 23:25:02,960 Dam even 36869 23:25:03,960 --> 23:25:12,640 more is 36870 23:25:06,780 --> 23:25:12,640 [Music] 36871 23:25:22,320 --> 23:25:35,320 I is for the language 0 1 0 1 0 oh is 36872 23:25:28,232 --> 23:25:39,080 for otation 0 1 1 D is for dynamic flas 36873 23:25:35,320 --> 23:25:43,512 run and finance e is even more than 36874 23:25:39,080 --> 23:25:46,080 David mail in canor so C is all that I 36875 23:25:43,512 --> 23:25:49,912 can give to 36876 23:25:46,080 --> 23:25:53,600 you debugging it since 36877 23:25:49,912 --> 23:25:56,680 PE soon deadlines I'll make it hit 36878 23:25:53,600 --> 23:26:00,160 compile and please don't break it code 36879 23:25:56,680 --> 23:26:02,820 was made by me for COD was made by me 36880 23:26:00,160 --> 23:26:10,809 for COD was 36881 23:26:02,820 --> 23:26:10,809 [Music] 36882 23:26:13,120 --> 23:26:16,480 by all 36883 23:26:22,232 --> 23:26:25,232 right 36884 23:26:26,552 --> 23:26:33,192 all right this is cs50 and cs50 this was 36885 23:26:30,912 --> 23:26:35,040 the Harvard crocodillos and the Radcliff 36886 23:26:33,192 --> 23:26:37,832 pitches if one more time we could thank 36887 23:26:35,040 --> 23:26:37,832 them for joining us 36888 23:26:40,400 --> 23:26:46,320 today so this is already week 10 our 36889 23:26:44,400 --> 23:26:47,912 last and indeed among the goals for 36890 23:26:46,320 --> 23:26:50,000 today are to hopefully give you all the 36891 23:26:47,912 --> 23:26:52,360 more of appreciation of truly just how 36892 23:26:50,000 --> 23:26:54,360 far you've come recall that in week zero 36893 23:26:52,360 --> 23:26:56,832 we began with this this visual here 36894 23:26:54,360 --> 23:26:59,832 whereby it was described this class as a 36895 23:26:56,832 --> 23:27:01,960 bit of a fire hose whereby drinking from 36896 23:26:59,832 --> 23:27:04,360 that fire hose or really a fire hose 36897 23:27:01,960 --> 23:27:06,160 from a water fountain uh is not unlike 36898 23:27:04,360 --> 23:27:07,400 getting an education down the road too 36899 23:27:06,160 --> 23:27:09,232 and so this is to say that if you're 36900 23:27:07,400 --> 23:27:11,160 feeling like you didn't quite get it all 36901 23:27:09,232 --> 23:27:12,552 down like that's actually okay and 36902 23:27:11,160 --> 23:27:15,040 that's to be expected and even if you 36903 23:27:12,552 --> 23:27:17,440 felt that with each passing week 0 1 two 36904 23:27:15,040 --> 23:27:19,552 all the way up now till 10 it never 36905 23:27:17,440 --> 23:27:22,000 really ever got easier perhaps just 36906 23:27:19,552 --> 23:27:24,320 consider that what was once hard like 36907 23:27:22,000 --> 23:27:26,192 Mario and like getting hello world to 36908 23:27:24,320 --> 23:27:28,480 compile is indeed the right measure of 36909 23:27:26,192 --> 23:27:30,192 the Delta between week Zer and now in 36910 23:27:28,480 --> 23:27:32,800 week 10 in fact you might recall that 36911 23:27:30,192 --> 23:27:35,160 again in week zero 2third of your 36912 23:27:32,800 --> 23:27:37,280 classmates had never taken a CS course 36913 23:27:35,160 --> 23:27:38,960 before now of course you all have and 36914 23:27:37,280 --> 23:27:40,800 indeed if you think back too to this 36915 23:27:38,960 --> 23:27:42,160 final sentiment from week zero that 36916 23:27:40,800 --> 23:27:44,080 indeed what ultimately matters in this 36917 23:27:42,160 --> 23:27:45,760 course is not where you end up relative 36918 23:27:44,080 --> 23:27:48,760 to your classmates but where you end up 36919 23:27:45,760 --> 23:27:50,440 relative to where you yourself began so 36920 23:27:48,760 --> 23:27:52,120 I would take some pride take some 36921 23:27:50,440 --> 23:27:53,480 satisfaction take some relief even 36922 23:27:52,120 --> 23:27:55,040 though a little bit more work does 36923 23:27:53,480 --> 23:27:56,760 remain at really just how far you've 36924 23:27:55,040 --> 23:27:58,600 come since that week zero and recall 36925 23:27:56,760 --> 23:28:00,720 that in week zero we literally started 36926 23:27:58,600 --> 23:28:03,120 with just zeros and ones and by now many 36927 23:28:00,720 --> 23:28:04,760 of you might have gleaned that these 64 36928 23:28:03,120 --> 23:28:06,680 zeros and ones have been spelling 36929 23:28:04,760 --> 23:28:09,760 something week by week in fact today is 36930 23:28:06,680 --> 23:28:11,720 our very last message here in binary uh 36931 23:28:09,760 --> 23:28:13,360 encoded on stage but then quickly we 36932 23:28:11,720 --> 23:28:14,512 introduced scratch and we started to 36933 23:28:13,360 --> 23:28:17,600 assemble some building blocks of 36934 23:28:14,512 --> 23:28:19,040 programming Loops conditions uh uh Loops 36935 23:28:17,600 --> 23:28:20,440 conditions functions and the like but 36936 23:28:19,040 --> 23:28:22,360 without all the distractions of 36937 23:28:20,440 --> 23:28:23,832 semicolons and curly braces and all of 36938 23:28:22,360 --> 23:28:26,000 that which admittedly we introduce the 36939 23:28:23,832 --> 23:28:27,512 next week when we introduce you to C but 36940 23:28:26,000 --> 23:28:29,600 even now that we've transitioned to 36941 23:28:27,512 --> 23:28:31,232 python hopefully even those kinds of 36942 23:28:29,600 --> 23:28:32,800 Curiosities or confusions are hopefully 36943 23:28:31,232 --> 23:28:34,512 starting to just get more familiar and 36944 23:28:32,800 --> 23:28:36,680 so you finally start to see the missing 36945 23:28:34,512 --> 23:28:38,640 semicolon as opposed to spending time on 36946 23:28:36,680 --> 23:28:40,280 that kind of struggle recall too that in 36947 23:28:38,640 --> 23:28:42,080 week two we started talking already 36948 23:28:40,280 --> 23:28:44,040 about memory and like how you can manage 36949 23:28:42,080 --> 23:28:46,232 things in arrays that later became of 36950 23:28:44,040 --> 23:28:49,000 course in Python lists uh the week after 36951 23:28:46,232 --> 23:28:51,320 we talked not only about uh debugging uh 36952 23:28:49,000 --> 23:28:52,832 bugs in code but how to debug those same 36953 23:28:51,320 --> 23:28:54,080 programs um thereafter we started 36954 23:28:52,832 --> 23:28:56,160 talking talking about algorithms and we 36955 23:28:54,080 --> 23:28:57,912 took a step back from code and looked at 36956 23:28:56,160 --> 23:28:59,440 the bubble sorts and the selection sorts 36957 23:28:57,912 --> 23:29:01,192 and the merge sorts and all of the 36958 23:28:59,440 --> 23:29:02,912 searches as well that go hand in hand 36959 23:29:01,192 --> 23:29:04,280 with that and indeed this ultimately is 36960 23:29:02,912 --> 23:29:05,360 what a lot of problem solving moving 36961 23:29:04,280 --> 23:29:06,832 forward is going to be about just 36962 23:29:05,360 --> 23:29:08,832 solving problems with some form of 36963 23:29:06,832 --> 23:29:11,080 algorithm and you have so many different 36964 23:29:08,832 --> 23:29:13,120 languages um in your toolkit now with 36965 23:29:11,080 --> 23:29:15,512 which to approach problems like those we 36966 23:29:13,120 --> 23:29:17,360 talk thereafter about pointers which are 36967 23:29:15,512 --> 23:29:18,832 not likely to come back in any modern 36968 23:29:17,360 --> 23:29:20,512 languages that you now use but hopefully 36969 23:29:18,832 --> 23:29:22,160 you have an all the better of a sense 36970 23:29:20,512 --> 23:29:23,680 underneath the hood of like what's going 36971 23:29:22,160 --> 23:29:25,360 on inside of the computer so that when 36972 23:29:23,680 --> 23:29:27,000 you're designing something you're using 36973 23:29:25,360 --> 23:29:28,640 something something crashes you at least 36974 23:29:27,000 --> 23:29:31,040 have a mental model for what's going on 36975 23:29:28,640 --> 23:29:33,232 and it's no longer that week zero black 36976 23:29:31,040 --> 23:29:35,320 box as it once was I mean you built 36977 23:29:33,232 --> 23:29:37,160 things like this think back to week five 36978 23:29:35,320 --> 23:29:39,000 when you built your own hash table and 36979 23:29:37,160 --> 23:29:41,872 those things are everywhere key value 36980 23:29:39,000 --> 23:29:44,552 pairs whether it's in python or in C or 36981 23:29:41,872 --> 23:29:46,320 if it's now in CSS and JavaScript and 36982 23:29:44,552 --> 23:29:48,960 even HTML like that principle of key 36983 23:29:46,320 --> 23:29:50,600 value pairs is really everywhere and so 36984 23:29:48,960 --> 23:29:51,912 of course now code doesn't necessarily 36985 23:29:50,600 --> 23:29:53,232 have to look like this it now 36986 23:29:51,912 --> 23:29:55,080 wonderfully looks a little something 36987 23:29:53,232 --> 23:29:56,872 more like this but eventually you're 36988 23:29:55,080 --> 23:29:58,440 probably not I'm not going to use Python 36989 23:29:56,872 --> 23:30:00,400 anymore something new and better is 36990 23:29:58,440 --> 23:30:01,800 going to come along but odds are like a 36991 23:30:00,400 --> 23:30:04,080 lot of the building blocks from these 36992 23:30:01,800 --> 23:30:05,720 past 11 weeks are still going to be 36993 23:30:04,080 --> 23:30:07,960 useful for wrapping your mind around 36994 23:30:05,720 --> 23:30:09,480 those new worlds and indeed SQL we 36995 23:30:07,960 --> 23:30:11,040 introduced you a little bit too and even 36996 23:30:09,480 --> 23:30:12,720 if you don't feel yourself yet an expert 36997 23:30:11,040 --> 23:30:14,320 hopefully have a sense of like what you 36998 23:30:12,720 --> 23:30:15,960 can do with it and what problems you can 36999 23:30:14,320 --> 23:30:17,680 solve uh it's of course a better 37000 23:30:15,960 --> 23:30:19,400 alternative to something like something 37001 23:30:17,680 --> 23:30:21,080 simple like a spreadsheet and now of 37002 23:30:19,400 --> 23:30:22,640 course like web stuff is everywhere 37003 23:30:21,080 --> 23:30:25,000 whether it's on your laptop or desktop 37004 23:30:22,640 --> 23:30:27,160 or a lot of the mobile apps that you use 37005 23:30:25,000 --> 23:30:28,760 on your phone even though they're native 37006 23:30:27,160 --> 23:30:30,760 applications like you install them from 37007 23:30:28,760 --> 23:30:33,040 Google Play or the Apple App Store like 37008 23:30:30,760 --> 23:30:35,160 they're implemented increasingly with 37009 23:30:33,040 --> 23:30:36,552 HTML CSS and JavaScript but they're put 37010 23:30:35,160 --> 23:30:38,320 in a little rectangular window so you 37011 23:30:36,552 --> 23:30:40,720 don't even notice that that's actually 37012 23:30:38,320 --> 23:30:42,232 really just an embedded browser and then 37013 23:30:40,720 --> 23:30:43,800 of course you can build things as you 37014 23:30:42,232 --> 23:30:45,400 might for your final project that to 37015 23:30:43,800 --> 23:30:47,120 might very well be web based if you go 37016 23:30:45,400 --> 23:30:49,040 that route I mean I'm still clinging to 37017 23:30:47,120 --> 23:30:51,800 like the very first like web app I ever 37018 23:30:49,040 --> 23:30:54,280 made years ago um but honestly I do that 37019 23:30:51,800 --> 23:30:55,760 in part because I was just so darn proud 37020 23:30:54,280 --> 23:30:57,400 that like I taught myself how to do 37021 23:30:55,760 --> 23:30:59,040 something and it actually worked and was 37022 23:30:57,400 --> 23:31:01,192 used by other people so whether it's 37023 23:30:59,040 --> 23:31:02,832 just used by you or your classmates or 37024 23:31:01,192 --> 23:31:04,440 your roommates or your family or your 37025 23:31:02,832 --> 23:31:06,360 company down the line there's a great 37026 23:31:04,440 --> 23:31:08,360 sense of satisfaction that comes despite 37027 23:31:06,360 --> 23:31:10,800 all of the the pain that might be along 37028 23:31:08,360 --> 23:31:13,000 the way when you just can't see or fix 37029 23:31:10,800 --> 23:31:15,912 that bug now of course we'll transition 37030 23:31:13,000 --> 23:31:17,912 as you'll see in the coming days to try 37031 23:31:15,912 --> 23:31:19,600 empowering you to code client side as 37032 23:31:17,912 --> 23:31:21,680 well up until now you've been using our 37033 23:31:19,600 --> 23:31:23,080 own vs code installation in the cloud 37034 23:31:21,680 --> 23:31:25,480 which is nice cuz you got up in running 37035 23:31:23,080 --> 23:31:27,480 super fast in week one focusing only on 37036 23:31:25,480 --> 23:31:29,832 code challenges not on technical 37037 23:31:27,480 --> 23:31:31,872 difficulties but among the goals now if 37038 23:31:29,832 --> 23:31:33,120 you so choose and want to program after 37039 23:31:31,872 --> 23:31:35,120 this class even if you never take 37040 23:31:33,120 --> 23:31:37,720 another CS course before you can use 37041 23:31:35,120 --> 23:31:39,960 these same real world deao standard 37042 23:31:37,720 --> 23:31:41,760 tools on your own Mac or PC and so 37043 23:31:39,960 --> 23:31:43,552 pictured here is a screenshot of like vs 37044 23:31:41,760 --> 23:31:44,552 code on the Mac and even though yes 37045 23:31:43,552 --> 23:31:45,832 you're going to have to like jump 37046 23:31:44,552 --> 23:31:47,512 through a couple of more Hoops to just 37047 23:31:45,832 --> 23:31:49,440 get python or some other language 37048 23:31:47,512 --> 23:31:50,720 working on your own Mac or PC like 37049 23:31:49,440 --> 23:31:52,040 that's what programming is ultimately 37050 23:31:50,720 --> 23:31:54,232 going to be about and we deliberately 37051 23:31:52,040 --> 23:31:56,360 transition you to this at terms end so 37052 23:31:54,232 --> 23:31:58,080 that now you have 11 weeks of more 37053 23:31:56,360 --> 23:32:00,192 Comfort under your belt with which to 37054 23:31:58,080 --> 23:32:01,232 solve sort of silly technical support 37055 23:32:00,192 --> 23:32:03,600 headaches that might have been deal 37056 23:32:01,232 --> 23:32:05,872 breakers so many weeks ago so there's 37057 23:32:03,600 --> 23:32:07,552 still more to be done uh in the coming 37058 23:32:05,872 --> 23:32:09,760 weeks and indeed a support structure 37059 23:32:07,552 --> 23:32:12,872 there for the cs50 hackathon of course 37060 23:32:09,760 --> 23:32:14,640 will be this 700 p.m. till 7 a.m. 37061 23:32:12,872 --> 23:32:16,832 opportunity to dive into your final 37062 23:32:14,640 --> 23:32:19,080 project well really continue diving into 37063 23:32:16,832 --> 23:32:20,760 your final project ideally at that point 37064 23:32:19,080 --> 23:32:22,080 uh alongside classmates perhaps your 37065 23:32:20,760 --> 23:32:23,280 project Partners if you're working 37066 23:32:22,080 --> 23:32:25,320 collaboratively 37067 23:32:23,280 --> 23:32:26,912 uh and awaiting you will be such 37068 23:32:25,320 --> 23:32:28,192 memories and excitement hopefully as 37069 23:32:26,912 --> 23:32:29,912 these even as you then turn your 37070 23:32:28,192 --> 23:32:31,192 attention back to your final project 37071 23:32:29,912 --> 23:32:33,600 there of course will be several meals 37072 23:32:31,192 --> 23:32:36,000 during the day culminating with 500 a.m. 37073 23:32:33,600 --> 23:32:38,360 uh shuttles to IHOP the local Pancake 37074 23:32:36,000 --> 23:32:40,680 Place uh if you are so awake at that 37075 23:32:38,360 --> 23:32:42,280 point or even if you get there uh this 37076 23:32:40,680 --> 23:32:44,400 is not an uncommon site as you might 37077 23:32:42,280 --> 23:32:46,400 recall from week zero and then lastly is 37078 23:32:44,400 --> 23:32:48,120 the cs-50 fair which is finally back 37079 23:32:46,400 --> 23:32:49,640 after a couple of years now of it not 37080 23:32:48,120 --> 23:32:51,480 being on campus and this will be an 37081 23:32:49,640 --> 23:32:53,512 opportunity for everyone to present 37082 23:32:51,480 --> 23:32:55,160 their final projects to passers by 37083 23:32:53,512 --> 23:32:56,640 classmates faculty and staff and really 37084 23:32:55,160 --> 23:32:58,600 just Delight in what it is you created 37085 23:32:56,640 --> 23:33:01,000 on your Mac your PC your phone in the 37086 23:32:58,600 --> 23:33:02,552 cloud or anywhere else and indeed it's 37087 23:33:01,000 --> 23:33:04,160 just going to be an opportunity to bring 37088 23:33:02,552 --> 23:33:06,480 your laptop to a shared space or your 37089 23:33:04,160 --> 23:33:08,680 phone and introduce your project to 37090 23:33:06,480 --> 23:33:10,872 passers by such as might appear and 37091 23:33:08,680 --> 23:33:12,320 ultimately celebrate what you all uh 37092 23:33:10,872 --> 23:33:14,800 accomplished and indeed will you be 37093 23:33:12,320 --> 23:33:17,440 handed at the cs50 fair your very own I 37094 23:33:14,800 --> 23:33:20,192 took cs50 t-shirt which I dare say I'm 37095 23:33:17,440 --> 23:33:21,600 still wearing all of these years later 37096 23:33:20,192 --> 23:33:23,232 and so you 37097 23:33:21,600 --> 23:33:26,760 to 37098 23:33:23,232 --> 23:33:29,120 we'll have that uh ahead of you as well 37099 23:33:26,760 --> 23:33:30,720 so for what's on the agenda today we 37100 23:33:29,120 --> 23:33:32,232 thought we would not only look back but 37101 23:33:30,720 --> 23:33:33,832 look forward but first we wanted to 37102 23:33:32,232 --> 23:33:35,832 thank so many of the team members that 37103 23:33:33,832 --> 23:33:37,360 have been helping both on stage and off 37104 23:33:35,832 --> 23:33:39,320 who've made this course and these 37105 23:33:37,360 --> 23:33:41,440 sections and so much more about cs50 37106 23:33:39,320 --> 23:33:43,000 possible of course um the building that 37107 23:33:41,440 --> 23:33:45,080 we are now in there's a whole team 37108 23:33:43,000 --> 23:33:47,000 downstairs in Memorial Hall who helps us 37109 23:33:45,080 --> 23:33:49,000 get set up and organized each day our 37110 23:33:47,000 --> 23:33:50,360 thanks to them there's the education 37111 23:33:49,000 --> 23:33:52,192 Support Services team who makes 37112 23:33:50,360 --> 23:33:53,720 everything look and sound so well down 37113 23:33:52,192 --> 23:33:55,912 here especially when we have all of the 37114 23:33:53,720 --> 23:33:57,872 more microphones as well our friends the 37115 23:33:55,912 --> 23:33:59,760 harbard crocodillos and the redcliff 37116 23:33:57,872 --> 23:34:04,320 pitches most recently and then of course 37117 23:33:59,760 --> 23:34:06,280 cs50's own team uh but butter cs50's own 37118 23:34:04,320 --> 23:34:08,160 favorite restaurant Chang show down the 37119 23:34:06,280 --> 23:34:09,640 road indeed if you find yourself in 37120 23:34:08,160 --> 23:34:11,760 Cambridge for the next one two 3 four 37121 23:34:09,640 --> 23:34:13,360 years or visiting from out of town uh do 37122 23:34:11,760 --> 23:34:14,720 pay a visit to our friends just down the 37123 23:34:13,360 --> 23:34:16,872 road and in fact we'll have our very 37124 23:34:14,720 --> 23:34:19,480 last cs50 lunch this Friday if you're 37125 23:34:16,872 --> 23:34:22,232 able locally to partake and then there 37126 23:34:19,480 --> 23:34:24,232 cs50's own team um both on stage and off 37127 23:34:22,232 --> 23:34:26,280 in thanks truly because not only do they 37128 23:34:24,232 --> 23:34:28,160 make the everything run so smoothly they 37129 23:34:26,280 --> 23:34:29,760 capture it for students here who might 37130 23:34:28,160 --> 23:34:31,640 not be physically present here for our 37131 23:34:29,760 --> 23:34:33,192 friends down in New Haven at Yale and 37132 23:34:31,640 --> 23:34:36,872 certainly for anyone online who might be 37133 23:34:33,192 --> 23:34:39,512 tuning in as well and then lastly wanted 37134 23:34:36,872 --> 23:34:41,800 to thank of course the huge team of your 37135 23:34:39,512 --> 23:34:43,400 classmates your peers that make cs50 37136 23:34:41,800 --> 23:34:46,680 possible in sections and office hours 37137 23:34:43,400 --> 23:34:49,192 tutorials and more allow me to share 37138 23:34:46,680 --> 23:34:51,480 with you the outtakes so that even we 37139 23:34:49,192 --> 23:34:53,080 the teaching staff sometimes struggle 37140 23:34:51,480 --> 23:34:54,912 with computer science here are some of 37141 23:34:53,080 --> 23:34:57,872 the clips that we captured when just 37142 23:34:54,912 --> 23:34:59,912 passing packets via tcpip a while back 37143 23:34:57,872 --> 23:35:02,232 you saw the finally the the nicely 37144 23:34:59,912 --> 23:35:04,400 polished version but here are if I may 37145 23:35:02,232 --> 23:35:05,160 if we could dim the lights are some of 37146 23:35:04,400 --> 23:35:07,040 the 37147 23:35:05,160 --> 23:35:10,000 outakes nothing 37148 23:35:07,040 --> 23:35:12,680 go buffering 37149 23:35:10,000 --> 23:35:15,680 okay Josh 37150 23:35:12,680 --> 23:35:15,680 nice 37151 23:35:16,830 --> 23:35:24,400 [Music] 37152 23:35:19,680 --> 23:35:24,400 Helen no oh wait 37153 23:35:27,440 --> 23:35:30,160 that was amazing 37154 23:35:30,480 --> 23:35:39,289 Josh uh um 37155 23:35:34,170 --> 23:35:39,289 [Music] 37156 23:35:39,800 --> 23:35:44,360 Sophie 37157 23:35:42,320 --> 23:35:47,360 amazing that was 37158 23:35:44,360 --> 23:35:47,360 perfect 37159 23:35:48,000 --> 23:35:52,960 B I think 37160 23:35:51,000 --> 23:35:56,250 I what 37161 23:35:52,960 --> 23:35:58,600 you oh nice 37162 23:35:56,250 --> 23:36:04,000 [Music] 37163 23:35:58,600 --> 23:36:04,000 guy that was amazing thank you all so 37164 23:36:04,040 --> 23:36:07,872 good indeed and that moment if we could 37165 23:36:06,360 --> 23:36:11,000 just one round of applause for everyone 37166 23:36:07,872 --> 23:36:11,000 who's helped out this 37167 23:36:12,760 --> 23:36:19,760 semester so back in week zero uh we 37168 23:36:17,600 --> 23:36:22,080 introduced you of course to this idea of 37169 23:36:19,760 --> 23:36:23,440 computational thinking which is to think 37170 23:36:22,080 --> 23:36:25,120 a a little more methodically a little 37171 23:36:23,440 --> 23:36:26,512 more algorithmically and by way of these 37172 23:36:25,120 --> 23:36:28,040 various languages hopefully that is 37173 23:36:26,512 --> 23:36:29,720 something you notice maybe not in the 37174 23:36:28,040 --> 23:36:31,160 moment but in the months and the years 37175 23:36:29,720 --> 23:36:32,552 to come that you do find that your 37176 23:36:31,160 --> 23:36:33,832 thoughts are indeed a little more 37177 23:36:32,552 --> 23:36:35,480 cleaned up and you're just able to 37178 23:36:33,832 --> 23:36:37,720 express yourself a little more precisely 37179 23:36:35,480 --> 23:36:39,600 and even spot illogic in someone else's 37180 23:36:37,720 --> 23:36:41,232 document or statements as well but at 37181 23:36:39,600 --> 23:36:43,232 the end of the day really a course like 37182 23:36:41,232 --> 23:36:45,480 this is also about critical thinking and 37183 23:36:43,232 --> 23:36:47,232 indeed rewind again to week zero when we 37184 23:36:45,480 --> 23:36:49,320 frame the entirety of computer science 37185 23:36:47,232 --> 23:36:51,160 is really just this like problem solving 37186 23:36:49,320 --> 23:36:53,280 and any problem in the world be it CS or 37187 23:36:51,160 --> 23:36:55,160 otherwise has some input and we decided 37188 23:36:53,280 --> 23:36:57,120 how to represent those inputs it needs 37189 23:36:55,160 --> 23:36:59,192 some output the solution there too and 37190 23:36:57,120 --> 23:37:00,512 then all of what you focused on doing 37191 23:36:59,192 --> 23:37:02,512 and learning and applying these past 37192 23:37:00,512 --> 23:37:04,600 several weeks is in that proverbial 37193 23:37:02,512 --> 23:37:06,720 black box which hopefully is not such an 37194 23:37:04,600 --> 23:37:08,360 abstraction anymore but is indeed 37195 23:37:06,720 --> 23:37:10,160 something that you know how to harness 37196 23:37:08,360 --> 23:37:11,760 and know what could be going on 37197 23:37:10,160 --> 23:37:13,600 underneath the hood even if it's in some 37198 23:37:11,760 --> 23:37:15,232 technology or some language that maybe 37199 23:37:13,600 --> 23:37:17,232 we ourselves didn't cover because a lot 37200 23:37:15,232 --> 23:37:18,960 of those first principles remain the 37201 23:37:17,232 --> 23:37:20,872 same now along the way we talked about 37202 23:37:18,960 --> 23:37:22,400 the quality of solutions to those 37203 23:37:20,872 --> 23:37:24,440 problems we happen to focus on 37204 23:37:22,400 --> 23:37:26,192 correctness just does it work design 37205 23:37:24,440 --> 23:37:28,000 which is a bit more qualitative and 37206 23:37:26,192 --> 23:37:29,440 subjective and then style the Aesthetics 37207 23:37:28,000 --> 23:37:31,040 of it all and these two are 37208 23:37:29,440 --> 23:37:33,080 characteristic maybe not with these same 37209 23:37:31,040 --> 23:37:35,480 words of just how you might write or 37210 23:37:33,080 --> 23:37:37,440 evaluate other creations in life be it 37211 23:37:35,480 --> 23:37:39,512 physical or written or the like so think 37212 23:37:37,440 --> 23:37:41,080 about too as you solve problems just how 37213 23:37:39,512 --> 23:37:43,512 you can sort of frame for yourself like 37214 23:37:41,080 --> 23:37:45,640 am I doing a good job or not by 37215 23:37:43,512 --> 23:37:47,600 quantizing it along these or perhaps 37216 23:37:45,640 --> 23:37:49,552 other axes as well and we thought we'd 37217 23:37:47,600 --> 23:37:51,160 highlight just two topics from that week 37218 23:37:49,552 --> 23:37:52,680 zero that have really been manifest for 37219 23:37:51,160 --> 23:37:54,552 the past several weeks namely 37220 23:37:52,680 --> 23:37:56,552 abstraction like taking complicated 37221 23:37:54,552 --> 23:37:57,960 things and ideas and trying to simplify 37222 23:37:56,552 --> 23:37:59,440 them so that we can sort of operate at 37223 23:37:57,960 --> 23:38:01,040 this level and like solve problems we 37224 23:37:59,440 --> 23:38:04,000 care about without getting into the 37225 23:38:01,040 --> 23:38:06,120 weeds of implementation detail so to 37226 23:38:04,000 --> 23:38:07,600 speak but there's this tension because 37227 23:38:06,120 --> 23:38:09,440 you know now from all of these different 37228 23:38:07,600 --> 23:38:11,232 languages that code is fairly 37229 23:38:09,440 --> 23:38:12,872 unforgiving I mean even omitting a 37230 23:38:11,232 --> 23:38:14,912 stupid semicolon sometimes breaks 37231 23:38:12,872 --> 23:38:16,912 everything and so Precision is sort of 37232 23:38:14,912 --> 23:38:19,232 at odd sometimes with this idea of 37233 23:38:16,912 --> 23:38:21,480 leveraging abstraction and so we thought 37234 23:38:19,232 --> 23:38:23,912 we would try to tease this apart 37235 23:38:21,480 --> 23:38:26,832 especially all these Weeks Later here uh 37236 23:38:23,912 --> 23:38:28,360 with a bit of a uh live demonstration so 37237 23:38:26,832 --> 23:38:30,600 on the way in you probably all received 37238 23:38:28,360 --> 23:38:32,400 a sheet of paper if not but someone near 37239 23:38:30,600 --> 23:38:34,192 you did just tear theirs in half and 37240 23:38:32,400 --> 23:38:36,000 borrow half a sheet if you can or any 37241 23:38:34,192 --> 23:38:37,800 piece of loose leaf paper or the like 37242 23:38:36,000 --> 23:38:41,000 will suffice so long as you have a pen 37243 23:38:37,800 --> 23:38:43,760 or pencil and for this allow me to 37244 23:38:41,000 --> 23:38:47,232 propose that we invite up maybe two 37245 23:38:43,760 --> 23:38:49,192 final cs50 volunteers this semester and 37246 23:38:47,232 --> 23:38:50,720 like a lot of hands are going up in this 37247 23:38:49,192 --> 23:38:53,320 okay a lot of hands how about I saw the 37248 23:38:50,720 --> 23:38:56,760 first hand there uh yes yes who's yes 37249 23:38:53,320 --> 23:38:58,912 who's pointing at herself now come on 37250 23:38:56,760 --> 23:39:01,192 down we just need the one hand for now 37251 23:38:58,912 --> 23:39:02,872 but oh wait oh wait uh you'll be our 37252 23:39:01,192 --> 23:39:04,552 number two well okay we have way too 37253 23:39:02,872 --> 23:39:07,360 many volunteers now no no please please 37254 23:39:04,552 --> 23:39:10,512 come down yes in the black shirt and if 37255 23:39:07,360 --> 23:39:11,832 you guys we will okay we'll do pair 37256 23:39:10,512 --> 23:39:13,680 programming in just a bit if you want to 37257 23:39:11,832 --> 23:39:15,512 hang out in the wings here we'll have 37258 23:39:13,680 --> 23:39:17,232 our second demonstration as well so okay 37259 23:39:15,512 --> 23:39:19,552 now maybe a round of applause for our 37260 23:39:17,232 --> 23:39:19,552 three 37261 23:39:21,040 --> 23:39:26,600 volunteers oh come on up first oh second 37262 23:39:24,832 --> 23:39:30,000 and third okay you come first we'll do 37263 23:39:26,600 --> 23:39:31,800 order no uh this is a q okay Q here okay 37264 23:39:30,000 --> 23:39:34,400 what's your name I'm Danny Danny okay 37265 23:39:31,800 --> 23:39:36,120 take this mic okay so we will DQ you 37266 23:39:34,400 --> 23:39:38,480 momentarily all right so Danny come on 37267 23:39:36,120 --> 23:39:40,640 over to the middle here and in a moment 37268 23:39:38,480 --> 23:39:42,640 I'm going to hand to Danny a sheet of 37269 23:39:40,640 --> 23:39:43,912 paper that has a picture on it and this 37270 23:39:42,640 --> 23:39:45,680 picture is going to be something that 37271 23:39:43,912 --> 23:39:49,040 I'd like you to verbally program the 37272 23:39:45,680 --> 23:39:50,832 audience to draw you can use any words 37273 23:39:49,040 --> 23:39:52,280 any abstractions any PR level of 37274 23:39:50,832 --> 23:39:54,192 precision that you want but you just 37275 23:39:52,280 --> 23:39:56,000 can't make hand gestures or sort of show 37276 23:39:54,192 --> 23:39:57,280 them what to draw But first you want to 37277 23:39:56,000 --> 23:39:59,760 tell us a little something about 37278 23:39:57,280 --> 23:40:03,400 yourself including everyone here I'm 37279 23:39:59,760 --> 23:40:05,640 daddy and I took ts50 okay wonderful 37280 23:40:03,400 --> 23:40:08,040 wonderful so I'm going to reveal the 37281 23:40:05,640 --> 23:40:09,232 picture only to Danny and if each of you 37282 23:40:08,040 --> 23:40:10,872 would like to take out that sheet of 37283 23:40:09,232 --> 23:40:12,720 paper and just make sure that no one 37284 23:40:10,872 --> 23:40:15,360 else can see this if you want to hold it 37285 23:40:12,720 --> 23:40:17,320 up this way everyone here is now holding 37286 23:40:15,360 --> 23:40:19,440 their pen or pencil and in some number 37287 23:40:17,320 --> 23:40:21,120 of steps give them a verbal algorithm 37288 23:40:19,440 --> 23:40:23,400 for drawing what you see and you can say 37289 23:40:21,120 --> 23:40:25,400 anything you want but no gestures okay 37290 23:40:23,400 --> 23:40:30,280 so you're going to want to 37291 23:40:25,400 --> 23:40:31,600 draw a square in the center of the paper 37292 23:40:30,280 --> 23:40:34,512 with the 37293 23:40:31,600 --> 23:40:38,360 diagonal pointing to 37294 23:40:34,512 --> 23:40:38,360 the center of the 37295 23:40:39,480 --> 23:40:47,800 edge wait no actually I scratched that 37296 23:40:43,232 --> 23:40:47,800 draw a rbus in the center of your 37297 23:40:48,080 --> 23:40:53,280 paper and for those who forget what a 37298 23:40:50,192 --> 23:40:57,800 rhombus is for um a d 37299 23:40:53,280 --> 23:41:02,320 Diamond a a square that's on its 37300 23:40:57,800 --> 23:41:04,912 side and then from the bottom vertex 37301 23:41:02,320 --> 23:41:08,320 draw a straight line down but not all 37302 23:41:04,912 --> 23:41:08,320 the way to the edge of the 37303 23:41:08,720 --> 23:41:14,800 paper okay and then keep your pencil or 37304 23:41:12,512 --> 23:41:18,912 pen at that point and you're going to 37305 23:41:14,800 --> 23:41:23,872 want to draw a line that's parallel to 37306 23:41:18,912 --> 23:41:23,872 the line of the original rumus to the 37307 23:41:24,960 --> 23:41:30,320 right and then keep your pencil or pen 37308 23:41:27,912 --> 23:41:33,912 at that point and draw a line straight 37309 23:41:30,320 --> 23:41:33,912 up connecting to the side 37310 23:41:35,600 --> 23:41:42,160 vertex yes and then go back to the line 37311 23:41:39,760 --> 23:41:45,640 that you drew from the bottom vertex to 37312 23:41:42,160 --> 23:41:50,552 the bottom of the paper and then draw a 37313 23:41:45,640 --> 23:41:53,040 line parallel to the left edge of the 37314 23:41:50,552 --> 23:41:55,720 rumus and then then keep your L your 37315 23:41:53,040 --> 23:42:02,000 paper your pencil at that point and draw 37316 23:41:55,720 --> 23:42:02,000 a line up to the vertex of the rumus 37317 23:42:03,000 --> 23:42:07,960 again the end the end all right well 37318 23:42:05,800 --> 23:42:09,480 thank you to Danny hang on to your paper 37319 23:42:07,960 --> 23:42:11,080 thank you so much and if you want to 37320 23:42:09,480 --> 23:42:13,192 step off to the stage there we will 37321 23:42:11,080 --> 23:42:16,400 reveal thank you a round of applause if 37322 23:42:13,192 --> 23:42:19,160 we could for Danny that is not an easy 37323 23:42:16,400 --> 23:42:21,080 task I'm sure and if Carter wouldn't 37324 23:42:19,160 --> 23:42:22,280 mind just grabbing a few samples here 37325 23:42:21,080 --> 23:42:23,680 let's actually take a look on the 37326 23:42:22,280 --> 23:42:25,280 overhead if we could I'm going to pop 37327 23:42:23,680 --> 23:42:26,600 down over here real fast we don't need 37328 23:42:25,280 --> 23:42:28,912 to collect them all but if you're 37329 23:42:26,600 --> 23:42:32,552 feeling either good or bad with what you 37330 23:42:28,912 --> 23:42:36,040 drew happy to collect a few of them 37331 23:42:32,552 --> 23:42:38,320 okay okay thank you thank 37332 23:42:36,040 --> 23:42:40,760 you okay hope you won't mind if I can't 37333 23:42:38,320 --> 23:42:45,760 reach everyone just a couple more okay 37334 23:42:40,760 --> 23:42:47,832 over here okay all right that's that's 37335 23:42:45,760 --> 23:42:49,120 okay this one's really funny okay I'm 37336 23:42:47,832 --> 23:42:50,760 going to go with this one if I may and 37337 23:42:49,120 --> 23:42:53,760 Carter has some 37338 23:42:50,760 --> 23:42:53,760 too 37339 23:42:54,640 --> 23:43:01,160 okay thank you so much okay so just a 37340 23:42:59,192 --> 23:43:04,360 random assortment here let me turn on a 37341 23:43:01,160 --> 23:43:08,440 camera so I can show you what I see here 37342 23:43:04,360 --> 23:43:10,120 for instance is one classmate drawing 37343 23:43:08,440 --> 23:43:14,232 which might resemble perhaps what you 37344 23:43:10,120 --> 23:43:16,192 drew here uh here is the beginnings of a 37345 23:43:14,232 --> 23:43:19,400 house it 37346 23:43:16,192 --> 23:43:23,680 seems nice this 37347 23:43:19,400 --> 23:43:25,160 one okay this one is larger 37348 23:43:23,680 --> 23:43:28,640 and how about a couple of others that 37349 23:43:25,160 --> 23:43:31,320 were getting closer I think okay so more 37350 23:43:28,640 --> 23:43:33,912 edges and vertices there this one seems 37351 23:43:31,320 --> 23:43:36,960 a little similar in 37352 23:43:33,912 --> 23:43:41,120 spirit if not 37353 23:43:36,960 --> 23:43:43,960 proportional this is uh Zach's the best 37354 23:43:41,120 --> 23:43:46,000 one but it turns out if I may Zach 37355 23:43:43,960 --> 23:43:48,800 you're not all that far off here Denny 37356 23:43:46,000 --> 23:43:51,160 is what you were reciting to everyone 37357 23:43:48,800 --> 23:43:52,640 algorithmically indeed it was this here 37358 23:43:51,160 --> 23:43:54,040 Cube and so Danny can you come on back 37359 23:43:52,640 --> 23:43:56,800 up for a 37360 23:43:54,040 --> 23:43:58,080 moment so if you'd like to share for 37361 23:43:56,800 --> 23:43:59,192 just a moment like what were some of the 37362 23:43:58,080 --> 23:44:01,680 thoughts going through your head and why 37363 23:43:59,192 --> 23:44:03,720 did you choose the words that you did 37364 23:44:01,680 --> 23:44:06,800 okay so what was going through my head 37365 23:44:03,720 --> 23:44:10,040 when I saw the cube um I didn't know if 37366 23:44:06,800 --> 23:44:12,800 I could say draw Cube so I decided to 37367 23:44:10,040 --> 23:44:14,720 start with the top and so draw a Remus 37368 23:44:12,800 --> 23:44:18,192 in the center of your paper and then 37369 23:44:14,720 --> 23:44:21,232 draw a line down and just like do the 37370 23:44:18,192 --> 23:44:23,120 first part then the second part then the 37371 23:44:21,232 --> 23:44:25,872 third part and then you would get a cube 37372 23:44:23,120 --> 23:44:27,912 like Zach yeah and so had you said and 37373 23:44:25,872 --> 23:44:29,512 you could have said draw a cube which 37374 23:44:27,912 --> 23:44:30,960 would be more of an abstraction even 37375 23:44:29,512 --> 23:44:31,960 that's not necessarily sufficiently 37376 23:44:30,960 --> 23:44:33,912 precise right because you don't 37377 23:44:31,960 --> 23:44:35,360 necessarily know what the orientation of 37378 23:44:33,912 --> 23:44:36,912 that cube is the size of it the 37379 23:44:35,360 --> 23:44:38,400 positioning on the paper so you instead 37380 23:44:36,912 --> 23:44:39,720 took a lower level approach which is not 37381 23:44:38,400 --> 23:44:41,400 unlike scratch if you think about 37382 23:44:39,720 --> 23:44:43,720 scratch being able to move up down left 37383 23:44:41,400 --> 23:44:45,232 right turn 90 degrees turn 15 degrees 37384 23:44:43,720 --> 23:44:47,080 and the like I mean that is ultimately 37385 23:44:45,232 --> 23:44:48,800 how a lot of graphical programs and 37386 23:44:47,080 --> 23:44:50,360 games even might be implemented by 37387 23:44:48,800 --> 23:44:51,640 really focusing at the level you do but 37388 23:44:50,360 --> 23:44:53,800 of course there's this tension with us 37389 23:44:51,640 --> 23:44:55,320 humans whereby we prefer often to think 37390 23:44:53,800 --> 23:44:56,800 at this level but even that might not be 37391 23:44:55,320 --> 23:44:59,000 sufficient which is to say here in week 37392 23:44:56,800 --> 23:45:01,360 10 like these are still going to be in 37393 23:44:59,000 --> 23:45:02,600 our hard problems uh but a hand if we 37394 23:45:01,360 --> 23:45:05,040 could one more time for Danny for 37395 23:45:02,600 --> 23:45:07,400 getting us that far along thank you so 37396 23:45:05,040 --> 23:45:09,480 much let me give you a stress ball here 37397 23:45:07,400 --> 23:45:12,680 all right and if we could have both of 37398 23:45:09,480 --> 23:45:16,232 our volunteers come up here we're going 37399 23:45:12,680 --> 23:45:17,872 to have yes come on up come on up and 37400 23:45:16,232 --> 23:45:19,872 let me uh have you guys introduce 37401 23:45:17,872 --> 23:45:22,960 yourselves in the middle 37402 23:45:19,872 --> 23:45:24,680 here hi I'm 37403 23:45:22,960 --> 23:45:27,120 hey there I'm sadik from Turkey nice to 37404 23:45:24,680 --> 23:45:29,760 meet you all wonderful welcome and this 37405 23:45:27,120 --> 23:45:31,960 time we're going to flip it around so as 37406 23:45:29,760 --> 23:45:34,400 to have the audience do what Danny just 37407 23:45:31,960 --> 23:45:35,960 did for us the only catch here is that 37408 23:45:34,400 --> 23:45:37,832 the only means we have for showing the 37409 23:45:35,960 --> 23:45:39,600 audience what they need to tell you to 37410 23:45:37,832 --> 23:45:41,960 draw is like literally right above the 37411 23:45:39,600 --> 23:45:43,680 chalkboard so on our system here that 37412 23:45:41,960 --> 23:45:46,000 your eyes must stay on the chalkboard 37413 23:45:43,680 --> 23:45:47,720 and not look up and in just a moment if 37414 23:45:46,000 --> 23:45:50,552 you guys want to both stand in front of 37415 23:45:47,720 --> 23:45:52,000 the chalkboard back to the audience and 37416 23:45:50,552 --> 23:45:53,400 as you're talking with each other other 37417 23:45:52,000 --> 23:45:55,232 verbalize it through the microphone if 37418 23:45:53,400 --> 23:45:57,160 you will I'm going to show everyone else 37419 23:45:55,232 --> 23:45:59,000 in the room a second and final drawing 37420 23:45:57,160 --> 23:46:00,680 and we'll just go rapid fire around the 37421 23:45:59,000 --> 23:46:02,232 room give us one step at a time 37422 23:46:00,680 --> 23:46:05,800 collectively and we'll see if these guys 37423 23:46:02,232 --> 23:46:07,160 can't draw exactly that same outcome so 37424 23:46:05,800 --> 23:46:08,960 is there another chunk what's that is 37425 23:46:07,160 --> 23:46:10,440 there another chunk or just just the one 37426 23:46:08,960 --> 23:46:15,360 so you'll have to collaborate and let's 37427 23:46:10,440 --> 23:46:18,760 give you a clean slate here 37428 23:46:15,360 --> 23:46:20,720 literally all right so no looking up 37429 23:46:18,760 --> 23:46:23,232 that's the only rule for you guys here 37430 23:46:20,720 --> 23:46:26,832 we go for the audience here is what we'd 37431 23:46:23,232 --> 23:46:30,120 like them ironically to 37432 23:46:26,832 --> 23:46:35,080 draw step one from anyone in the 37433 23:46:30,120 --> 23:46:39,120 audience yes draw a circle draw a circle 37434 23:46:35,080 --> 23:46:39,120 anywhere not anywhere not 37435 23:46:41,440 --> 23:46:47,760 anywhere okay that's step one step two 37436 23:46:44,512 --> 23:46:47,760 someone else yeah in the 37437 23:46:50,400 --> 23:46:54,552 middle 37438 23:46:52,760 --> 23:46:56,512 draw a line down from the bottom of the 37439 23:46:54,552 --> 23:46:59,872 circle about halfway down I think there 37440 23:46:56,512 --> 23:46:59,872 was a hand in front of you too number 37441 23:47:07,912 --> 23:47:10,912 three 37442 23:47:10,920 --> 23:47:15,080 [Music] 37443 23:47:12,760 --> 23:47:18,040 okay okay the overarching goal here for 37444 23:47:15,080 --> 23:47:22,320 those unable here is to draw a person 37445 23:47:18,040 --> 23:47:25,800 fig okay it may be a SI figure draw the 37446 23:47:22,320 --> 23:47:30,680 left leg of the person of this person 37447 23:47:25,800 --> 23:47:32,360 okay good job all right next step four 37448 23:47:30,680 --> 23:47:36,160 yeah 37449 23:47:32,360 --> 23:47:39,832 of line up Circle 37450 23:47:36,160 --> 23:47:42,320 okay the left oh sorry to the right draw 37451 23:47:39,832 --> 23:47:44,720 a v okay to the right of the vertex at 37452 23:47:42,320 --> 23:47:48,552 the bottom of the circle draw a v draw a 37453 23:47:44,720 --> 23:47:53,320 v um like what V nope not interactive 37454 23:47:48,552 --> 23:47:53,320 draw a v um no no 37455 23:47:54,720 --> 23:48:00,640 well yeah it seems Weir 37456 23:47:58,120 --> 23:48:02,280 weird let's get ready for maybe 37457 23:48:00,640 --> 23:48:05,000 something like step 37458 23:48:02,280 --> 23:48:08,192 five okay we'll go with that step five 37459 23:48:05,000 --> 23:48:09,760 someone else step five someone else 37460 23:48:08,192 --> 23:48:13,832 someone else 37461 23:48:09,760 --> 23:48:19,192 yeah draw the right side of the leg okay 37462 23:48:13,832 --> 23:48:23,232 nice step six step six happy face six 37463 23:48:19,192 --> 23:48:23,232 six yes erase the line 37464 23:48:24,040 --> 23:48:29,080 erase the line that you have on the left 37465 23:48:27,552 --> 23:48:33,160 on the 37466 23:48:29,080 --> 23:48:35,512 okay okay step seven yes instead of like 37467 23:48:33,160 --> 23:48:37,280 that line that was before going up make 37468 23:48:35,512 --> 23:48:40,640 it go down instead of that line before 37469 23:48:37,280 --> 23:48:45,800 going up make it go down mhm okay step 37470 23:48:40,640 --> 23:48:47,912 eight step eight step eight yes connect 37471 23:48:45,800 --> 23:48:51,232 that line to the hip connect that line 37472 23:48:47,912 --> 23:48:53,360 to the hip not like not touching not 37473 23:48:51,232 --> 23:48:57,160 touching something like this 37474 23:48:53,360 --> 23:49:00,232 maybe okay compromise not touching okay 37475 23:48:57,160 --> 23:49:03,720 not touching okay all right step nine 37476 23:49:00,232 --> 23:49:03,720 almost there I think step 37477 23:49:05,000 --> 23:49:09,280 nine step nine step nine yes in 37478 23:49:14,120 --> 23:49:19,832 back uh write the word high on the top 37479 23:49:17,000 --> 23:49:22,000 left of the circle 37480 23:49:19,832 --> 23:49:25,680 here okay 37481 23:49:22,000 --> 23:49:28,192 and step 10 almost there line draw a 37482 23:49:25,680 --> 23:49:30,232 line pointing to high so like a spee 37483 23:49:28,192 --> 23:49:34,192 bubble basically 37484 23:49:30,232 --> 23:49:36,912 yeah okay and step uh 10 37485 23:49:34,192 --> 23:49:41,832 11 37486 23:49:36,912 --> 23:49:43,960 yeah erase the exclamation point nice 12 37487 23:49:41,832 --> 23:49:49,832 do we want to give them one 37488 23:49:43,960 --> 23:49:52,912 more 12 or we good yeah last one erase 37489 23:49:49,832 --> 23:49:55,080 the erase erace the right arm okay I 37490 23:49:52,912 --> 23:49:57,960 think we're going to need a 13 37491 23:49:55,080 --> 23:50:01,400 then and 37492 23:49:57,960 --> 23:50:03,872 then yeah 37493 23:50:01,400 --> 23:50:06,040 repat repeat the left arm but rotate it 37494 23:50:03,872 --> 23:50:06,040 by 37495 23:50:07,680 --> 23:50:11,192 90° that feels 37496 23:50:11,320 --> 23:50:16,872 wrong wait how would you like as an 37497 23:50:13,600 --> 23:50:18,760 organic human being how put your 37498 23:50:16,872 --> 23:50:20,120 arms like would you put your would you 37499 23:50:18,760 --> 23:50:22,832 ever structure your arm like that that 37500 23:50:20,120 --> 23:50:25,192 would not be a stick 37501 23:50:22,832 --> 23:50:29,000 figure CU would you do this or would you 37502 23:50:25,192 --> 23:50:32,232 do that or a little hint maybe get a get 37503 23:50:29,000 --> 23:50:34,192 a give me a step 14 step 14 and final 37504 23:50:32,232 --> 23:50:36,192 step 37505 23:50:34,192 --> 23:50:40,192 14 I think we just got to tell them what 37506 23:50:36,192 --> 23:50:40,192 to do step 14 37507 23:50:48,000 --> 23:50:55,720 yes think of a walking man 37508 23:50:51,912 --> 23:50:59,080 and and have the left the right hand 37509 23:50:55,720 --> 23:51:02,080 walking to your right all right so like 37510 23:50:59,080 --> 23:51:04,400 it's like where could the hand go where 37511 23:51:02,080 --> 23:51:09,360 should the hand go on that 37512 23:51:04,400 --> 23:51:12,120 arm but yeah yeah okay yes 37513 23:51:09,360 --> 23:51:14,480 no yeah I mean look look look like right 37514 23:51:12,120 --> 23:51:18,912 here look right here look right I yes 37515 23:51:14,480 --> 23:51:18,912 sorry thank you 14 37516 23:51:19,120 --> 23:51:23,512 St that's pretty close so 37517 23:51:21,360 --> 23:51:26,360 congratulations to you guys and thank 37518 23:51:23,512 --> 23:51:28,280 you as well all right so I mean these 37519 23:51:26,360 --> 23:51:31,480 these things too are not yes Round of 37520 23:51:28,280 --> 23:51:31,480 Applause then sure 37521 23:51:32,280 --> 23:51:38,480 so so this is to say that these ideas of 37522 23:51:36,080 --> 23:51:40,160 abstraction and precision and really 37523 23:51:38,480 --> 23:51:42,000 every other term of art that we explored 37524 23:51:40,160 --> 23:51:43,832 this term are sort of omnipresent and 37525 23:51:42,000 --> 23:51:46,160 can be easier or harder to implement 37526 23:51:43,832 --> 23:51:48,440 depending on exactly what the problem is 37527 23:51:46,160 --> 23:51:50,680 but what we thought we' do now in our uh 37528 23:51:48,440 --> 23:51:53,120 final day is try to now similarly 37529 23:51:50,680 --> 23:51:56,400 prepare prepare you for life after cs50 37530 23:51:53,120 --> 23:51:58,440 and this is really going to be a list of 37531 23:51:56,400 --> 23:52:00,192 really potential to-dos so that you can 37532 23:51:58,440 --> 23:52:02,280 stand on your own after the class after 37533 23:52:00,192 --> 23:52:04,360 the class's infrastructure write actual 37534 23:52:02,280 --> 23:52:06,160 code and then we'll come full circle one 37535 23:52:04,360 --> 23:52:08,120 final time with our friend Jennifer 8 37536 23:52:06,160 --> 23:52:10,000 Lee to look at the world of emojis and 37537 23:52:08,120 --> 23:52:12,040 how they relate to all forms of 37538 23:52:10,000 --> 23:52:14,480 representation that we've talked to uh 37539 23:52:12,040 --> 23:52:16,960 talked about up until now so one how can 37540 23:52:14,480 --> 23:52:18,640 you go about programming after cs50 so 37541 23:52:16,960 --> 23:52:21,280 one you can actually install command 37542 23:52:18,640 --> 23:52:23,040 line tools on your own Mac or PC perhaps 37543 23:52:21,280 --> 23:52:25,232 unbeknownst to you Windows has what's 37544 23:52:23,040 --> 23:52:27,320 generally called a command prompt Mac OS 37545 23:52:25,232 --> 23:52:29,480 literally comes with a terminal program 37546 23:52:27,320 --> 23:52:30,760 in your applications utilities folder 37547 23:52:29,480 --> 23:52:32,512 and so even if you've never run those 37548 23:52:30,760 --> 23:52:34,600 programs you've actually had a sort of 37549 23:52:32,512 --> 23:52:36,232 blinking cursor black and white prompt 37550 23:52:34,600 --> 23:52:38,440 available to you might not have all of 37551 23:52:36,232 --> 23:52:40,160 the same software installed as your code 37552 23:52:38,440 --> 23:52:41,720 space in the cloud but you have that 37553 23:52:40,160 --> 23:52:43,512 command line interface even within 37554 23:52:41,720 --> 23:52:45,400 today's graphical tools and among the 37555 23:52:43,512 --> 23:52:47,040 tools you can install within that 37556 23:52:45,400 --> 23:52:48,800 command line interface would be 37557 23:52:47,040 --> 23:52:51,000 something called xcode on the Mac which 37558 23:52:48,800 --> 23:52:52,440 comes not only with a guey IDE 37559 23:52:51,000 --> 23:52:54,040 integrated development environment but 37560 23:52:52,440 --> 23:52:55,760 also those command line tools and 37561 23:52:54,040 --> 23:52:58,400 Microsoft for Windows has something 37562 23:52:55,760 --> 23:53:00,832 similar as well learning git so we've 37563 23:52:58,400 --> 23:53:02,320 used git only unbeknownst to you 37564 23:53:00,832 --> 23:53:05,232 underneath the hood for the most part 37565 23:53:02,320 --> 23:53:06,600 but git is a very very popular tool if 37566 23:53:05,232 --> 23:53:08,760 challenging to pick up for the first 37567 23:53:06,600 --> 23:53:10,480 time that makes it easy to push code to 37568 23:53:08,760 --> 23:53:12,360 a website called GitHub or any 37569 23:53:10,480 --> 23:53:13,960 equivalent and then collaborate more 37570 23:53:12,360 --> 23:53:15,192 effectively with classmates there's 37571 23:53:13,960 --> 23:53:17,040 definitely a bit of a learning curve but 37572 23:53:15,192 --> 23:53:18,640 thanks to cs50's own Brian youu you can 37573 23:53:17,040 --> 23:53:20,080 start for instance with a video like 37574 23:53:18,640 --> 23:53:21,720 this and this indeed is going to be one 37575 23:53:20,080 --> 23:53:23,080 of these deao standards in the real 37576 23:53:21,720 --> 23:53:25,160 world at least for the next several 37577 23:53:23,080 --> 23:53:27,280 years that you'll probably encounter if 37578 23:53:25,160 --> 23:53:29,232 you work in Tech or really any company 37579 23:53:27,280 --> 23:53:30,720 where you're doing some programming vs 37580 23:53:29,232 --> 23:53:32,120 code itself will walk you through this 37581 23:53:30,720 --> 23:53:34,512 process in the coming days but you can 37582 23:53:32,120 --> 23:53:36,280 indeed install it on your own Mac or PC 37583 23:53:34,512 --> 23:53:37,960 and what can you do when you write code 37584 23:53:36,280 --> 23:53:40,160 well you can certainly write software 37585 23:53:37,960 --> 23:53:42,600 for your Mac for your PC for your phone 37586 23:53:40,160 --> 23:53:44,960 or of course per week 10 uh week nine 37587 23:53:42,600 --> 23:53:48,872 you can host uh your own website be it 37588 23:53:44,960 --> 23:53:50,512 static as in week uh week eight um 37589 23:53:48,872 --> 23:53:51,800 hosting it at websites like these which 37590 23:53:50,512 --> 23:53:53,640 gener generally have free or 37591 23:53:51,800 --> 23:53:55,320 student-friendly accounts via which you 37592 23:53:53,640 --> 23:53:57,192 can put something statically on the web 37593 23:53:55,320 --> 23:53:59,120 at a real domain name that you might 37594 23:53:57,192 --> 23:54:02,480 choose or you can host a full-fledged 37595 23:53:59,120 --> 23:54:04,552 web app and using uh student tiers on 37596 23:54:02,480 --> 23:54:06,872 Amazon and Microsoft and Google's cloud 37597 23:54:04,552 --> 23:54:09,080 services or others you can sign up for 37598 23:54:06,872 --> 23:54:11,440 being a student certainly a whole lot of 37599 23:54:09,080 --> 23:54:13,912 free software free hosting so as to if 37600 23:54:11,440 --> 23:54:15,800 nothing else um experiment and uh 37601 23:54:13,912 --> 23:54:17,480 perhaps maximally get your own app or 37602 23:54:15,800 --> 23:54:19,440 website up and running so know that 37603 23:54:17,480 --> 23:54:22,480 those are resources available to you and 37604 23:54:19,440 --> 23:54:24,480 this is by uh certainly a non-exhaustive 37605 23:54:22,480 --> 23:54:26,120 list if you'd like to geek out in the 37606 23:54:24,480 --> 23:54:28,280 coming months in the coming years these 37607 23:54:26,120 --> 23:54:30,320 are just some of the places that people 37608 23:54:28,280 --> 23:54:32,360 who take computer science classes who 37609 23:54:30,320 --> 23:54:34,552 write code might tend to hang out and 37610 23:54:32,360 --> 23:54:36,480 ask and answer questions of each other 37611 23:54:34,552 --> 23:54:38,080 um so keep an eye for instance on these 37612 23:54:36,480 --> 23:54:39,800 here and then cs50 has its own 37613 23:54:38,080 --> 23:54:41,680 communities as you'll see if you go to 37614 23:54:39,800 --> 23:54:43,232 this URL here via the open coreware 37615 23:54:41,680 --> 23:54:46,160 version of cs50 which is open to the 37616 23:54:43,232 --> 23:54:47,640 world do uh is there a Vibrant Community 37617 23:54:46,160 --> 23:54:50,400 uh thanks to time zones that's pretty 37618 23:54:47,640 --> 23:54:52,480 much active 247 365 talking about not 37619 23:54:50,400 --> 23:54:54,360 only cs50 going on in problem sets and 37620 23:54:52,480 --> 23:54:56,192 projects but really technology more 37621 23:54:54,360 --> 23:54:58,400 generally as well so certainly feel 37622 23:54:56,192 --> 23:55:01,160 welcome to partake either asking or 37623 23:54:58,400 --> 23:55:02,760 answering questions now in speaking of 37624 23:55:01,160 --> 23:55:04,760 asking and answering questions a couple 37625 23:55:02,760 --> 23:55:07,000 of weeks ago you kindly gave us a whole 37626 23:55:04,760 --> 23:55:08,552 bunch of review questions which we 37627 23:55:07,000 --> 23:55:10,832 called through and picked out our 37628 23:55:08,552 --> 23:55:12,600 favorite 20 of them these of course were 37629 23:55:10,832 --> 23:55:14,960 multiple choice questions and in 37630 23:55:12,600 --> 23:55:17,120 preparation for this week uh in 37631 23:55:14,960 --> 23:55:19,680 preparation for life ahead we thought we 37632 23:55:17,120 --> 23:55:21,912 would choreograph a bit of a a quiz show 37633 23:55:19,680 --> 23:55:24,000 here and ceed as you came in at the 37634 23:55:21,912 --> 23:55:26,480 start of class you might recall being 37635 23:55:24,000 --> 23:55:28,960 invited to go to this URL here 37636 23:55:26,480 --> 23:55:32,832 cs50. either here in person or if you're 37637 23:55:28,960 --> 23:55:36,000 watching live from home at this URL here 37638 23:55:32,832 --> 23:55:37,720 you can use a phone or a laptop and if 37639 23:55:36,000 --> 23:55:41,600 it's easier on a phone you can point 37640 23:55:37,720 --> 23:55:43,280 your camera at this 2D barcode here 37641 23:55:41,600 --> 23:55:48,280 we'll give folks a moment to pull that 37642 23:55:43,280 --> 23:55:48,280 up and again that URL was cs50. 37643 23:55:49,600 --> 23:55:54,040 l/p 37644 23:55:51,480 --> 23:55:58,552 and once it looks like most folks have 37645 23:55:54,040 --> 23:56:01,760 it up and running our friend Carter here 37646 23:55:58,552 --> 23:56:04,192 will help us dive into this uh review 37647 23:56:01,760 --> 23:56:05,800 session if you will with a bit of fun 37648 23:56:04,192 --> 23:56:08,600 along the way all right Carter if you'd 37649 23:56:05,800 --> 23:56:10,000 like to take it away what do we have as 37650 23:56:08,600 --> 23:56:11,960 our first question you should see on 37651 23:56:10,000 --> 23:56:13,400 your phone or laptop this same question 37652 23:56:11,960 --> 23:56:16,232 being asked the first question is how do 37653 23:56:13,400 --> 23:56:19,232 you print quote un quote hello world in 37654 23:56:16,232 --> 23:56:22,040 Python so among the possible answers are 37655 23:56:19,232 --> 23:56:25,800 these here 37656 23:56:22,040 --> 23:56:25,800 buzz in on your phone or your 37657 23:56:26,320 --> 23:56:29,680 laptop we've got a few hundred responses 37658 23:56:28,872 --> 23:56:34,120 are 37659 23:56:29,680 --> 23:56:34,120 ready 7 Seconds to make your 37660 23:56:34,360 --> 23:56:38,680 decision this is question one of 20 go 37661 23:56:37,232 --> 23:56:41,080 to it with some confidence I think we're 37662 23:56:38,680 --> 23:56:44,800 down to zero on the clock and Carter it 37663 23:56:41,080 --> 23:56:46,552 looks like 98% of you uh indeed said 37664 23:56:44,800 --> 23:56:48,800 hello world and Carter per the check 37665 23:56:46,552 --> 23:56:50,872 mark That's indeed the correct answer 37666 23:56:48,800 --> 23:56:52,832 here now to make things interesting in 37667 23:56:50,872 --> 23:56:54,552 know that you'll see some number of 37668 23:56:52,832 --> 23:56:56,832 points and we've deliberately anonymized 37669 23:56:54,552 --> 23:56:58,552 it so only you know what number you are 37670 23:56:56,832 --> 23:57:01,280 so a whole lot of guests have a perfect 37671 23:56:58,552 --> 23:57:02,512 score of 1,000 at the moment hopefully 37672 23:57:01,280 --> 23:57:05,232 we'll see over the next several 37673 23:57:02,512 --> 23:57:07,440 questions things start to bridge out uh 37674 23:57:05,232 --> 23:57:09,552 but know that the speed with which you 37675 23:57:07,440 --> 23:57:11,960 buzz in will also factor into how many 37676 23:57:09,552 --> 23:57:13,760 points you now get So the faster you 37677 23:57:11,960 --> 23:57:17,512 move the more points you get question 37678 23:57:13,760 --> 23:57:19,160 two if we could what does DNS stand for 37679 23:57:17,512 --> 23:57:22,000 from just a couple of weeks back domain 37680 23:57:19,160 --> 23:57:24,160 number system domain name system data 37681 23:57:22,000 --> 23:57:26,480 numbering structure or there's no such 37682 23:57:24,160 --> 23:57:29,600 thing as 37683 23:57:26,480 --> 23:57:33,160 DNS few hundred responses are 37684 23:57:29,600 --> 23:57:35,800 in 8 seconds 37685 23:57:33,160 --> 23:57:39,720 remain fewer points now but still a 37686 23:57:35,800 --> 23:57:41,552 chance to buzz in and now as we hit zero 37687 23:57:39,720 --> 23:57:44,040 the responses are these domain name 37688 23:57:41,552 --> 23:57:46,552 system which is indeed correct and 84% 37689 23:57:44,040 --> 23:57:48,320 of you got that one correctly and indeed 37690 23:57:46,552 --> 23:57:50,280 exists we talked about it a couple of 37691 23:57:48,320 --> 23:57:52,160 weeks ago so we're still seeing a whole 37692 23:57:50,280 --> 23:57:54,040 lot lot of ties at 2,000 we'll see if 37693 23:57:52,160 --> 23:57:56,280 someone starts to pull away before long 37694 23:57:54,040 --> 23:57:57,800 question three what is the upper bound 37695 23:57:56,280 --> 23:58:00,192 of merge sorts 37696 23:57:57,800 --> 23:58:05,280 runtime so that escalated 37697 23:58:00,192 --> 23:58:09,832 quickly Big O of n log n Big O of log n 37698 23:58:05,280 --> 23:58:12,760 Theta or Omega of log n or Big O of 37699 23:58:09,832 --> 23:58:14,040 one what is the upper bound of merge 37700 23:58:12,760 --> 23:58:15,960 sorts 37701 23:58:14,040 --> 23:58:18,232 runtime that was the last of the 37702 23:58:15,960 --> 23:58:20,120 algorithms we solve for sorting and in 1 37703 23:58:18,232 --> 23:58:22,720 second we'll see that the correct answer 37704 23:58:20,120 --> 23:58:29,192 is is just edging out everyone else 37705 23:58:22,720 --> 23:58:31,192 indeed 46% it is n I know it's n log now 37706 23:58:29,192 --> 23:58:33,232 if I may as the teacher it can't be 37707 23:58:31,192 --> 23:58:35,480 login because login is strictly less 37708 23:58:33,232 --> 23:58:37,400 than n and you can't possibly sort n 37709 23:58:35,480 --> 23:58:39,232 elements unless you minimally look at or 37710 23:58:37,400 --> 23:58:41,480 touch each of them so it's got to be at 37711 23:58:39,232 --> 23:58:42,760 least greater than n intuitively we 37712 23:58:41,480 --> 23:58:46,120 still have a whole bunch of ties let's 37713 23:58:42,760 --> 23:58:47,512 move on to number four what is stored in 37714 23:58:46,120 --> 23:58:50,080 ARG 37715 23:58:47,512 --> 23:58:52,000 C back to the language C is it in Array 37716 23:58:50,080 --> 23:58:53,720 of arguments the maximum size of an 37717 23:58:52,000 --> 23:58:56,000 array the count of arguments given to a 37718 23:58:53,720 --> 23:58:58,232 program when first run or how much 37719 23:58:56,000 --> 23:59:01,440 memory is allocated to a 37720 23:58:58,232 --> 23:59:05,800 function again you wrote all of these 37721 23:59:01,440 --> 23:59:08,800 questions and we have 5 seconds for the 37722 23:59:05,800 --> 23:59:11,640 reveal ARG C 37723 23:59:08,800 --> 23:59:13,400 is indeed the count of arguments given 37724 23:59:11,640 --> 23:59:15,160 to a program when first run think back 37725 23:59:13,400 --> 23:59:18,480 to C when we did command line arguments 37726 23:59:15,160 --> 23:59:21,080 there was argc and arv argv was the 37727 23:59:18,480 --> 23:59:23,872 array but Arc was indeed the count the 37728 23:59:21,080 --> 23:59:26,600 CN AR save all right we still have a 37729 23:59:23,872 --> 23:59:29,320 whole bunch of ties at the top here but 37730 23:59:26,600 --> 23:59:32,192 let's move on then to number five what 37731 23:59:29,320 --> 23:59:34,160 is the duck debugger favorite hobby 37732 23:59:32,192 --> 23:59:36,280 according to one of your classmates 37733 23:59:34,160 --> 23:59:38,280 dressing up like Dracula swimming across 37734 23:59:36,280 --> 23:59:43,192 the stage filling up the entire bathroom 37735 23:59:38,280 --> 23:59:45,440 of a guy's house and sitting quietly on 37736 23:59:43,192 --> 23:59:46,600 stage the third of course is a reference 37737 23:59:45,440 --> 23:59:49,440 to a YouTube video that was on the 37738 23:59:46,600 --> 23:59:53,832 course's website that week but according 37739 23:59:49,440 --> 23:59:56,280 to your class mate number two seconds 37740 23:59:53,832 --> 23:59:59,552 remaining sitting quietly on stage is 37741 23:59:56,280 --> 00:00:01,280 its favorite pastime so a little harder 37742 23:59:59,552 --> 00:00:04,160 perhaps than the 37743 00:00:01,280 --> 00:00:07,160 others 5,000 now we're starting to see 37744 00:00:04,160 --> 00:00:09,120 some spread so we only have six six 37745 00:00:07,160 --> 00:00:11,960 guests in contention for first place and 37746 00:00:09,120 --> 00:00:14,640 the next question now is six what is the 37747 00:00:11,960 --> 00:00:19,160 function used to open a file in 37748 00:00:14,640 --> 00:00:21,280 C F open open file open 37749 00:00:19,160 --> 00:00:23,680 file 37750 00:00:21,280 --> 00:00:26,080 what is the function used to open a file 37751 00:00:23,680 --> 00:00:26,080 in 37752 00:00:27,192 --> 00:00:32,400 C 7 37753 00:00:29,912 --> 00:00:36,680 Seconds there's some differences between 37754 00:00:32,400 --> 00:00:40,400 C and python here and the reveal it is 37755 00:00:36,680 --> 00:00:42,232 indeed fop it's 77% correct too all 37756 00:00:40,400 --> 00:00:47,872 right let's see the rankings now if you 37757 00:00:42,232 --> 00:00:50,400 are guest 15 9715 6171 3753 or 3273 37758 00:00:47,872 --> 00:00:53,320 you're now in the lead as we move on on 37759 00:00:50,400 --> 00:00:55,280 to se question seven how does Sterling 37760 00:00:53,320 --> 00:00:57,080 compute the average sorry how does 37761 00:00:55,280 --> 00:00:59,800 Sterling compute the length of a string 37762 00:00:57,080 --> 00:01:01,320 in C it looks at how much memory the 37763 00:00:59,800 --> 00:01:03,440 string uses it counts the number of 37764 00:01:01,320 --> 00:01:05,192 characters until it reaches back0 it 37765 00:01:03,440 --> 00:01:06,872 counts the number of bits in the string 37766 00:01:05,192 --> 00:01:08,552 or it creates pointers for each 37767 00:01:06,872 --> 00:01:13,552 character and counts 37768 00:01:08,552 --> 00:01:16,040 them 10 seconds Sterling in 37769 00:01:13,552 --> 00:01:18,400 C recall that we implemented this 37770 00:01:16,040 --> 00:01:20,960 ourselves in class but then we used the 37771 00:01:18,400 --> 00:01:23,280 library thereafter and in indeed with 37772 00:01:20,960 --> 00:01:24,640 85% it simply counts the number of 37773 00:01:23,280 --> 00:01:28,040 characters until it reaches that 37774 00:01:24,640 --> 00:01:30,600 Sentinel back sl0 AKA null and in this 37775 00:01:28,040 --> 00:01:34,440 case we 37776 00:01:30,600 --> 00:01:36,872 have five four four of you tied now for 37777 00:01:34,440 --> 00:01:39,552 first all right question eight where 37778 00:01:36,872 --> 00:01:43,000 does Malo allocate memory 37779 00:01:39,552 --> 00:01:44,040 from the stack the Heap the pointers or 37780 00:01:43,000 --> 00:01:49,080 the 37781 00:01:44,040 --> 00:01:53,800 temp where does malok allocate memory 37782 00:01:49,080 --> 00:01:53,800 from responses are coming in 8 37783 00:01:54,760 --> 00:01:59,760 seconds a good review question at that 37784 00:01:58,160 --> 00:02:02,512 in two seconds we'll see that malok 37785 00:01:59,760 --> 00:02:05,440 allocates memory from woo close one the 37786 00:02:02,512 --> 00:02:07,720 Heap is correct the Heap is correct the 37787 00:02:05,440 --> 00:02:08,912 stack recall is where functions store 37788 00:02:07,720 --> 00:02:10,080 their local variables and their 37789 00:02:08,912 --> 00:02:12,280 arguments and that just happens 37790 00:02:10,080 --> 00:02:14,800 automatically the Heap represented in 37791 00:02:12,280 --> 00:02:18,160 our pictures up top is where malok draws 37792 00:02:14,800 --> 00:02:20,832 from now we have guest 37793 00:02:18,160 --> 00:02:23,512 15 has 37794 00:02:20,832 --> 00:02:25,400 made its way to the top here but others 37795 00:02:23,512 --> 00:02:27,720 can catch up if they don't buzz in fast 37796 00:02:25,400 --> 00:02:30,160 enough so number nine how many people 37797 00:02:27,720 --> 00:02:32,360 flew from 50v to New York on the day of 37798 00:02:30,160 --> 00:02:38,832 the 37799 00:02:32,360 --> 00:02:38,832 crime 16 29 8 or 37800 00:02:39,832 --> 00:02:45,192 three anyone with a laptop perhaps has 37801 00:02:42,440 --> 00:02:45,192 an advantage 37802 00:02:47,280 --> 00:02:57,360 here 5 Seconds 37803 00:02:51,552 --> 00:03:00,760 and the answers are but the answer is 37804 00:02:57,360 --> 00:03:05,192 16 let's see if guest 15 got 37805 00:03:00,760 --> 00:03:07,160 this they did not goodbye to guest 15 at 37806 00:03:05,192 --> 00:03:10,040 the top all right question 10 we're 37807 00:03:07,160 --> 00:03:11,912 about halfway there what are meta tags 37808 00:03:10,040 --> 00:03:15,040 used for in 37809 00:03:11,912 --> 00:03:16,600 HTML to describe a web page to Define 37810 00:03:15,040 --> 00:03:18,552 parameters for an element to group 37811 00:03:16,600 --> 00:03:20,680 elements together to translate content 37812 00:03:18,552 --> 00:03:24,512 into machine readable format 37813 00:03:20,680 --> 00:03:27,080 at what are meta tags used for in HTML 37814 00:03:24,512 --> 00:03:31,000 we saw a few of 37815 00:03:27,080 --> 00:03:34,480 them for different use 37816 00:03:31,000 --> 00:03:36,400 cases and with one second we see that 37817 00:03:34,480 --> 00:03:38,400 indeed the number one answer was to 37818 00:03:36,400 --> 00:03:41,080 describe the web page be it for a mobile 37819 00:03:38,400 --> 00:03:42,800 device be it for screen scrapers like 37820 00:03:41,080 --> 00:03:44,680 Facebook and Twitter and other such apps 37821 00:03:42,800 --> 00:03:46,640 that grab images and descriptions 37822 00:03:44,680 --> 00:03:51,080 thereof all right we're in the second 37823 00:03:46,640 --> 00:03:54,040 half now guest nine guest four 669 has 37824 00:03:51,080 --> 00:03:56,080 edged ahead guess 15 is now in sixth 37825 00:03:54,040 --> 00:03:59,320 place all right number 11 is how do you 37826 00:03:56,080 --> 00:04:02,360 find the address of a variable in C 37827 00:03:59,320 --> 00:04:05,400 think back a few weeks star dollar sign 37828 00:04:02,360 --> 00:04:05,400 Ampersand or 37829 00:04:08,000 --> 00:04:14,832 ask from one of your own classmates how 37830 00:04:10,912 --> 00:04:14,832 do you find the address of a variable in 37831 00:04:18,192 --> 00:04:23,760 C and the number one answer is ersan 37832 00:04:22,080 --> 00:04:26,600 which is indeed the address of operator 37833 00:04:23,760 --> 00:04:30,040 at 62% nicely done let's see who's the 37834 00:04:26,600 --> 00:04:32,760 top of the list now guess 4669 has 37835 00:04:30,040 --> 00:04:36,720 retained their lead so we move on to 12 37836 00:04:32,760 --> 00:04:39,552 what does the arrow operator mean in C a 37837 00:04:36,720 --> 00:04:42,000 hyphen and a greater than sign nothing 37838 00:04:39,552 --> 00:04:44,912 starts a comment replaces a star and Dot 37839 00:04:42,000 --> 00:04:47,440 operator declares a 37840 00:04:44,912 --> 00:04:48,800 pointer what does this Arrow operator 37841 00:04:47,440 --> 00:04:51,000 mean in 37842 00:04:48,800 --> 00:04:55,480 C 37843 00:04:51,000 --> 00:04:59,680 again from a few weeks back 3 seconds 37844 00:04:55,480 --> 00:05:02,080 harder assortment perhaps and it's oh 37845 00:04:59,680 --> 00:05:04,600 replaces a star and Dot operator the 37846 00:05:02,080 --> 00:05:06,800 number two answer was indeed correct 37847 00:05:04,600 --> 00:05:08,600 this was just a cleaner way syntactic 37848 00:05:06,800 --> 00:05:10,232 sugar for collapsing what would be a 37849 00:05:08,600 --> 00:05:12,000 star and then some parentheses and then 37850 00:05:10,232 --> 00:05:13,760 a DOT into quite simply something that 37851 00:05:12,000 --> 00:05:16,872 looks like an arrow itself all right 37852 00:05:13,760 --> 00:05:20,280 Carter who's in the lead now still that 37853 00:05:16,872 --> 00:05:22,400 same guest and let's see what 13 has for 37854 00:05:20,280 --> 00:05:29,360 us which of these is not a data type in 37855 00:05:22,400 --> 00:05:29,360 SQL light blob string integer 37856 00:05:31,120 --> 00:05:36,120 text we used a few of these more 37857 00:05:34,232 --> 00:05:38,280 commonly than others but not all of 37858 00:05:36,120 --> 00:05:42,320 these are for 37859 00:05:38,280 --> 00:05:46,600 real 5 Seconds to make your 37860 00:05:42,320 --> 00:05:49,480 decision and the results are blob is a 37861 00:05:46,600 --> 00:05:53,232 thing string is not in SQL light it's of 37862 00:05:49,480 --> 00:05:55,400 course called text as we've seen it blob 37863 00:05:53,232 --> 00:05:57,600 as goofy as it sounds is just binary 37864 00:05:55,400 --> 00:05:59,320 large object but indeed it's how you 37865 00:05:57,600 --> 00:06:03,080 might store a binary file in your 37866 00:05:59,320 --> 00:06:06,760 database all right the rankings now oh 37867 00:06:03,080 --> 00:06:09,720 guest 8444 has Eed ahead so we move on 37868 00:06:06,760 --> 00:06:11,512 to 14 which of the following is a valid 37869 00:06:09,720 --> 00:06:12,960 way to print Exclamation point 37870 00:06:11,512 --> 00:06:15,872 Exclamation point Exclamation point 37871 00:06:12,960 --> 00:06:18,640 Exclamation point in 37872 00:06:15,872 --> 00:06:20,160 Python I'll let you read these 37873 00:06:18,640 --> 00:06:22,120 yourselves 37874 00:06:20,160 --> 00:06:26,232 which is a valid 37875 00:06:22,120 --> 00:06:26,232 way everyone got quiet in is 37876 00:06:27,000 --> 00:06:34,640 thinking all right 6 seconds few hundred 37877 00:06:31,640 --> 00:06:38,960 responses in so 37878 00:06:34,640 --> 00:06:40,800 far all right and yes nicely done 78% is 37879 00:06:38,960 --> 00:06:42,480 correct you can use the star operator to 37880 00:06:40,800 --> 00:06:45,040 essentially multiply the character at 37881 00:06:42,480 --> 00:06:47,600 left all right who's in the lead Carter 37882 00:06:45,040 --> 00:06:51,080 we now have still G guest 37883 00:06:47,600 --> 00:06:54,600 8444 and so we move on now to 15 what 37884 00:06:51,080 --> 00:06:56,400 does the free function do D allocates 37885 00:06:54,600 --> 00:06:58,640 memory from A Primitive deallocates 37886 00:06:56,400 --> 00:07:00,640 memory at the given pointer terminates a 37887 00:06:58,640 --> 00:07:04,960 loop or returns a value from the 37888 00:07:00,640 --> 00:07:04,960 function what does the free function 37889 00:07:07,400 --> 00:07:12,912 do all right 5 Seconds what does the 37890 00:07:10,960 --> 00:07:15,640 free function do call it's the opposite 37891 00:07:12,912 --> 00:07:18,232 essentially of malok and it deallocates 37892 00:07:15,640 --> 00:07:20,760 the memory at the given pointer as well 37893 00:07:18,232 --> 00:07:22,800 and in C that's on you in Python you 37894 00:07:20,760 --> 00:07:26,120 don't need to worry about allocating or 37895 00:07:22,800 --> 00:07:29,440 freeing aik all right we now have at the 37896 00:07:26,120 --> 00:07:34,080 top still guest 8444 and seeing more and 37897 00:07:29,440 --> 00:07:38,000 more spread 16 which is not a step of 37898 00:07:34,080 --> 00:07:40,552 compiling think back now to week two 37899 00:07:38,000 --> 00:07:44,872 compiling pre-processing linking or 37900 00:07:40,552 --> 00:07:47,640 threading which is not a step of 37901 00:07:44,872 --> 00:07:51,512 compiling we use it as a catchall but it 37902 00:07:47,640 --> 00:07:51,512 technically means a few different things 37903 00:07:54,400 --> 00:07:59,320 collectively all right and threading is 37904 00:07:57,512 --> 00:08:01,960 indeed not on the list that is a 37905 00:07:59,320 --> 00:08:03,872 technical thing generally meaning a 37906 00:08:01,960 --> 00:08:05,960 program can do multiple things at once 37907 00:08:03,872 --> 00:08:09,160 but that is not related here to 37908 00:08:05,960 --> 00:08:11,280 compiling all right guest 8444 is still 37909 00:08:09,160 --> 00:08:14,480 at the top we have just a few questions 37910 00:08:11,280 --> 00:08:16,720 left and so 17 what was the surprise at 37911 00:08:14,480 --> 00:08:18,440 the beginning of the Halloween lecture 37912 00:08:16,720 --> 00:08:21,000 according to your classmates someone 37913 00:08:18,440 --> 00:08:23,440 scared me someone hid candy under every 37914 00:08:21,000 --> 00:08:26,912 chair someone dressed up as me the 37915 00:08:23,440 --> 00:08:26,912 entire staff dressed up as 37916 00:08:28,480 --> 00:08:34,552 Carter interesting litmus test of who 37917 00:08:31,440 --> 00:08:37,512 came to or watch lecture 37918 00:08:34,552 --> 00:08:40,512 perhaps let's see which guests got this 37919 00:08:37,512 --> 00:08:43,280 correct someone someone's really dressed 37920 00:08:40,512 --> 00:08:45,232 up as indeed me at 64% so I think we 37921 00:08:43,280 --> 00:08:47,120 have attendance here essentially from 37922 00:08:45,232 --> 00:08:49,040 that lecture so let's move on now to the 37923 00:08:47,120 --> 00:08:52,360 final few questions after seeing that 37924 00:08:49,040 --> 00:08:54,600 guest a 8444 8444 is still doing well 37925 00:08:52,360 --> 00:08:56,480 can anyone Dethrone them why is it 37926 00:08:54,600 --> 00:08:59,600 incorrect to use the equals equals 37927 00:08:56,480 --> 00:09:02,192 operator in C to compare 37928 00:08:59,600 --> 00:09:03,640 strings it's computationally inefficient 37929 00:09:02,192 --> 00:09:06,192 you comparing the locations of the 37930 00:09:03,640 --> 00:09:09,760 strings strings don't exist using equal 37931 00:09:06,192 --> 00:09:09,760 equals will lead to buffer 37932 00:09:11,400 --> 00:09:16,552 overflow why is it incorrect to use 37933 00:09:13,600 --> 00:09:18,512 equal equals in C to compare strings 3 37934 00:09:16,552 --> 00:09:20,760 seconds we saw this live and it 37935 00:09:18,512 --> 00:09:22,760 motivated like an entire week because 37936 00:09:20,760 --> 00:09:25,280 you're comparing the locations that is 37937 00:09:22,760 --> 00:09:27,080 the addresses in memory of those same 37938 00:09:25,280 --> 00:09:30,192 strings all right let's see if this 37939 00:09:27,080 --> 00:09:32,360 leaves the rankings the same guest 8444 37940 00:09:30,192 --> 00:09:35,080 is still at the top and pulling ahead 37941 00:09:32,360 --> 00:09:39,680 two final questions 19 what is the 37942 00:09:35,080 --> 00:09:42,480 difference between null one L and null 2 37943 00:09:39,680 --> 00:09:45,120 L's they null and null mean the same 37944 00:09:42,480 --> 00:09:47,552 thing nulls refers to back sl0 whereas 37945 00:09:45,120 --> 00:09:49,600 null to L's is the zero address null is 37946 00:09:47,552 --> 00:09:54,400 the zero address whereas null to L's 37947 00:09:49,600 --> 00:09:56,360 refers to back sl0 null is null but 37948 00:09:54,400 --> 00:09:58,872 lazier 5 37949 00:09:56,360 --> 00:10:02,000 Seconds subtle not the best design 37950 00:09:58,872 --> 00:10:05,000 perhaps to have in technical terms but 37951 00:10:02,000 --> 00:10:06,400 indeed 62% of you got that NL is the 37952 00:10:05,000 --> 00:10:09,760 first thing we talked about when we 37953 00:10:06,400 --> 00:10:12,280 talked about back sl0 and N is a pointer 37954 00:10:09,760 --> 00:10:14,440 it's the zero pointer same thing same 37955 00:10:12,280 --> 00:10:18,600 number but different context all right 37956 00:10:14,440 --> 00:10:21,552 Carter guest 8444 is the person to beat 37957 00:10:18,600 --> 00:10:26,280 with our final 20th questions what do 37958 00:10:21,552 --> 00:10:26,280 the binary bulbs on stage spell 37959 00:10:27,600 --> 00:10:33,512 today and these are your four choices 37960 00:10:31,440 --> 00:10:37,760 different from usual we usually use 8 37961 00:10:33,512 --> 00:10:40,080 bit asky today we are using utf8 which 37962 00:10:37,760 --> 00:10:42,872 is a form of Unicode which is the larger 37963 00:10:40,080 --> 00:10:46,512 subset that uses one or two or three or 37964 00:10:42,872 --> 00:10:49,640 even four bites to spell a single 37965 00:10:46,512 --> 00:10:53,600 character and the answer wow close close 37966 00:10:49,640 --> 00:10:55,872 is indeed a cupcake indeed a cupcake 37967 00:10:53,600 --> 00:10:58,872 well done and let's see the final 37968 00:10:55,872 --> 00:11:01,400 results 8444 is the winner are they here 37969 00:10:58,872 --> 00:11:05,410 in person perhaps 37970 00:11:01,400 --> 00:11:08,770 8444 you're 844 come on 37971 00:11:05,410 --> 00:11:08,770 [Applause] 37972 00:11:10,192 --> 00:11:19,480 down thank you here you go 37973 00:11:14,600 --> 00:11:22,552 congratulations oh you're all right so 37974 00:11:19,480 --> 00:11:26,512 today if we 37975 00:11:22,552 --> 00:11:26,512 may give me just one 37976 00:11:31,600 --> 00:11:34,912 moment all 37977 00:11:42,320 --> 00:11:48,280 right all right so today we are so 37978 00:11:45,440 --> 00:11:50,720 pleased to be joined by uh Jennifer 8 37979 00:11:48,280 --> 00:11:52,960 Lee who's an of the college a dear 37980 00:11:50,720 --> 00:11:55,640 friend and is actually really the reason 37981 00:11:52,960 --> 00:11:57,480 why there's evidence of Muppets in cs50 37982 00:11:55,640 --> 00:11:59,640 in fact some years ago I was visiting 37983 00:11:57,480 --> 00:12:01,480 her and she had on her shelf like this 37984 00:11:59,640 --> 00:12:03,512 custom Muppet it wasn't one that appears 37985 00:12:01,480 --> 00:12:05,800 on TV but she had somehow gone on a 37986 00:12:03,512 --> 00:12:07,400 website former toy store called FAO 37987 00:12:05,800 --> 00:12:09,040 Schwarz at the time and you're allowed 37988 00:12:07,400 --> 00:12:10,760 to configure your own Muppet whatnot 37989 00:12:09,040 --> 00:12:12,160 choose the eyes the nose the face and 37990 00:12:10,760 --> 00:12:14,120 the Torso and I just thought this was 37991 00:12:12,160 --> 00:12:15,640 the coolest thing and so in the taxi on 37992 00:12:14,120 --> 00:12:17,640 the way home I was like going on the 37993 00:12:15,640 --> 00:12:19,760 website trying to purchase our very 37994 00:12:17,640 --> 00:12:21,600 first Muppet I then woke up the next 37995 00:12:19,760 --> 00:12:23,160 morning thinking why did I just buy a 37996 00:12:21,600 --> 00:12:25,192 puppet in the back of a taxi and so it 37997 00:12:23,160 --> 00:12:26,960 sat on the shelf for really 2 years and 37998 00:12:25,192 --> 00:12:28,720 then a colleague of mine within cs50's 37999 00:12:26,960 --> 00:12:30,400 team decided after I brought it into the 38000 00:12:28,720 --> 00:12:32,192 office to sit on a shelf there to 38001 00:12:30,400 --> 00:12:34,480 actually bring it to life and indeed if 38002 00:12:32,192 --> 00:12:36,600 you Google around cs50 Muppet and 38003 00:12:34,480 --> 00:12:38,280 puppetry online you'll see in fact these 38004 00:12:36,600 --> 00:12:39,832 as characters not only over the past 38005 00:12:38,280 --> 00:12:41,960 couple of years in coid times when 38006 00:12:39,832 --> 00:12:44,000 really there was next to no one actually 38007 00:12:41,960 --> 00:12:46,232 here and so they were instead um but 38008 00:12:44,000 --> 00:12:47,960 indeed she's brought not only this this 38009 00:12:46,232 --> 00:12:50,160 educational element this pedagogical 38010 00:12:47,960 --> 00:12:52,552 element this playful element El to cs50 38011 00:12:50,160 --> 00:12:54,232 and we have her here today to speak to 38012 00:12:52,552 --> 00:12:56,280 exactly the sorts of encodings that are 38013 00:12:54,232 --> 00:12:58,800 here on stage Jenny is the former Vice 38014 00:12:56,280 --> 00:13:01,080 chair of the Unicode subcommittee on 38015 00:12:58,800 --> 00:13:02,832 emoji which is to say that she and her 38016 00:13:01,080 --> 00:13:04,552 colleagues have been influential in 38017 00:13:02,832 --> 00:13:06,552 taking emoji from what was a very 38018 00:13:04,552 --> 00:13:09,720 limited character set early on and by 38019 00:13:06,552 --> 00:13:11,640 far unrepresentative of much uh human 38020 00:13:09,720 --> 00:13:13,832 emotion in speech into really an 38021 00:13:11,640 --> 00:13:16,280 initiative now to capture digitally all 38022 00:13:13,832 --> 00:13:18,360 of the world's languages past present 38023 00:13:16,280 --> 00:13:20,480 and future as well as the range of 38024 00:13:18,360 --> 00:13:22,552 emotions that might see here in the form 38025 00:13:20,480 --> 00:13:24,960 of that pillow or even in the cake that 38026 00:13:22,552 --> 00:13:27,670 awaits so allow me to introduce Jennifer 38027 00:13:24,960 --> 00:13:29,360 8 Lee thank 38028 00:13:27,670 --> 00:13:33,800 [Music] 38029 00:13:29,360 --> 00:13:36,440 you I much drink okay clicker hi all 38030 00:13:33,800 --> 00:13:39,120 right hold on I have to hide my drinks I 38031 00:13:36,440 --> 00:13:41,320 might need more water all right um I'm 38032 00:13:39,120 --> 00:13:44,040 really excited to speak to speak here 38033 00:13:41,320 --> 00:13:45,232 last time last year I was here uh one I 38034 00:13:44,040 --> 00:13:46,760 was wearing a mask which is like a real 38035 00:13:45,232 --> 00:13:49,232 bummer if you're lecturing and then the 38036 00:13:46,760 --> 00:13:51,960 entire like front part was all Muppet so 38037 00:13:49,232 --> 00:13:54,512 I'm really happy to see humans actually 38038 00:13:51,960 --> 00:13:57,080 um and it's always an honor to speak at 38039 00:13:54,512 --> 00:13:58,720 Sanders and and then Dave and I were 38040 00:13:57,080 --> 00:14:00,872 actually classmates way back when so I 38041 00:13:58,720 --> 00:14:03,600 do remember him when he was an undergrad 38042 00:14:00,872 --> 00:14:05,552 much like you so I um I'm going to give 38043 00:14:03,600 --> 00:14:08,160 a talk on the world of emoji and how I 38044 00:14:05,552 --> 00:14:11,872 kind of became an emoji activist so it 38045 00:14:08,160 --> 00:14:14,000 kind of all starts um with my friend e 38046 00:14:11,872 --> 00:14:16,192 inl who is a designer who's well known 38047 00:14:14,000 --> 00:14:18,680 for doing the Twitter fail whale which 38048 00:14:16,192 --> 00:14:21,080 was kind of laid to rest except now this 38049 00:14:18,680 --> 00:14:24,512 week it seems like it may be like may be 38050 00:14:21,080 --> 00:14:26,720 necessary again um so she and I one day 38051 00:14:24,512 --> 00:14:28,872 were texting this is back in 2015 we're 38052 00:14:26,720 --> 00:14:30,872 texting about dumplings because we are 38053 00:14:28,872 --> 00:14:33,120 Chinese is women and we like like to 38054 00:14:30,872 --> 00:14:34,640 text about food and so I sent her this 38055 00:14:33,120 --> 00:14:38,680 picture of dumplings she was like yum 38056 00:14:34,640 --> 00:14:41,360 yum yum yum yum yum yum yum um and then 38057 00:14:38,680 --> 00:14:43,512 she was like oh Apple doesn't have a 38058 00:14:41,360 --> 00:14:44,872 dumpling emoji and I was like oh that's 38059 00:14:43,512 --> 00:14:47,232 kind of interesting and didn't really 38060 00:14:44,872 --> 00:14:48,600 think anything about it because like you 38061 00:14:47,232 --> 00:14:51,192 know people Point things out to you all 38062 00:14:48,600 --> 00:14:52,640 the time and then you just like forget 38063 00:14:51,192 --> 00:14:56,160 you just like move on but then half an 38064 00:14:52,640 --> 00:14:57,800 hour later um on my phone appears this 38065 00:14:56,160 --> 00:14:59,800 like dumpling with hard eyes and you 38066 00:14:57,800 --> 00:15:01,232 don't see it in in because it's a still 38067 00:14:59,800 --> 00:15:02,552 shot but it actually had like blinking 38068 00:15:01,232 --> 00:15:05,040 eyes so she liked to call it like bling 38069 00:15:02,552 --> 00:15:07,512 bling dumpling so she as a designer had 38070 00:15:05,040 --> 00:15:10,120 decided to like go in and like make her 38071 00:15:07,512 --> 00:15:11,512 own dumpling Emoji um because she was 38072 00:15:10,120 --> 00:15:14,160 like I'm a designer I can fix it but 38073 00:15:11,512 --> 00:15:15,872 that actually got me thinking I was like 38074 00:15:14,160 --> 00:15:18,192 where do Emoji come from and like how is 38075 00:15:15,872 --> 00:15:19,912 there not a dumpling Emoji cuz from my 38076 00:15:18,192 --> 00:15:23,680 perspective dumplings are this kind of 38077 00:15:19,912 --> 00:15:27,192 universal food right so and there are a 38078 00:15:23,680 --> 00:15:30,080 lot of Japanese Foods 38079 00:15:27,192 --> 00:15:31,440 on the emoji keyboard and I I was not 38080 00:15:30,080 --> 00:15:34,160 this was like back in 2015 I was like 38081 00:15:31,440 --> 00:15:36,080 not a big Emoji user like at all so I 38082 00:15:34,160 --> 00:15:38,912 mean you have things like Ramen you have 38083 00:15:36,080 --> 00:15:41,232 Bento boxes you have Curry you have 38084 00:15:38,912 --> 00:15:44,760 tempura you even have like kind of 38085 00:15:41,232 --> 00:15:46,400 obscure um kind of foods like this thing 38086 00:15:44,760 --> 00:15:48,512 things on a stick turns out to be fish 38087 00:15:46,400 --> 00:15:51,192 fish things on a stick then this pink 38088 00:15:48,512 --> 00:15:53,080 and white white swirly thing is also a 38089 00:15:51,192 --> 00:15:54,680 fish thing and there's even like that 38090 00:15:53,080 --> 00:15:57,552 triangle rice ball that looks like it's 38091 00:15:54,680 --> 00:16:00,280 had a bikini wax all well represented on 38092 00:15:57,552 --> 00:16:02,080 the Emoji Keyboard but no dumplings and 38093 00:16:00,280 --> 00:16:03,400 it's very strange cuz like all cultures 38094 00:16:02,080 --> 00:16:06,232 kind of have their dumpling right 38095 00:16:03,400 --> 00:16:08,640 whether or not it's um kinali or ravioli 38096 00:16:06,232 --> 00:16:11,440 or aanas like essentially everyone sort 38097 00:16:08,640 --> 00:16:13,680 of like discover the idea of like um 38098 00:16:11,440 --> 00:16:15,680 yummy goodness inside a carbo hydrate 38099 00:16:13,680 --> 00:16:21,232 carbohydrate shell whether not baked or 38100 00:16:15,680 --> 00:16:23,120 fried or um steamed so I was like okay I 38101 00:16:21,232 --> 00:16:25,552 literally Google I was like who controls 38102 00:16:23,120 --> 00:16:27,760 emoji and you discover that they're 38103 00:16:25,552 --> 00:16:31,600 actually regulated by a nonprofit called 38104 00:16:27,760 --> 00:16:33,040 the Unicode Consortium and um it is you 38105 00:16:31,600 --> 00:16:35,080 know and I just like went on their 38106 00:16:33,040 --> 00:16:37,680 website and I discovered that they had 38107 00:16:35,080 --> 00:16:39,440 12 full voting members as of 2015 so 38108 00:16:37,680 --> 00:16:41,160 this is 2015 and they were like mostly 38109 00:16:39,440 --> 00:16:45,280 us multinational tech companies it was 38110 00:16:41,160 --> 00:16:48,232 Oracle it was IBM Microsoft Adobe Google 38111 00:16:45,280 --> 00:16:51,360 Apple Facebook and Yahoo and of the 38112 00:16:48,232 --> 00:16:54,160 three that were not multinational US 38113 00:16:51,360 --> 00:16:56,360 tech companies they were let's see a 38114 00:16:54,160 --> 00:16:58,552 German company called sap a Chinese 38115 00:16:56,360 --> 00:17:00,440 Telecom company called Huawei and then 38116 00:16:58,552 --> 00:17:02,872 like the government of Oman like those 38117 00:17:00,440 --> 00:17:05,280 were basically the 12 full voting 38118 00:17:02,872 --> 00:17:09,552 members of the US multi-national tech 38119 00:17:05,280 --> 00:17:12,192 companies so they at that point paid um 38120 00:17:09,552 --> 00:17:14,760 $118,000 a year to have full voting 38121 00:17:12,192 --> 00:17:16,800 power on the you know Unicode committee 38122 00:17:14,760 --> 00:17:18,872 and I was like oh that's a that's a lot 38123 00:17:16,800 --> 00:17:21,440 of money and I kind of felt indignant 38124 00:17:18,872 --> 00:17:23,080 about this and uh but then like if you 38125 00:17:21,440 --> 00:17:24,552 kind of keep on digging on their website 38126 00:17:23,080 --> 00:17:26,800 you found like there was this kind of 38127 00:17:24,552 --> 00:17:28,680 interesting loophole which is you could 38128 00:17:26,800 --> 00:17:32,000 join as an individual for 38129 00:17:28,680 --> 00:17:34,600 $75 um you don't get voting power but it 38130 00:17:32,000 --> 00:17:36,600 gave you the right to put yourself on 38131 00:17:34,600 --> 00:17:38,400 the email list and also to like attend 38132 00:17:36,600 --> 00:17:39,912 the quarterly Unicode meeting so I was 38133 00:17:38,400 --> 00:17:41,000 like I'll do that I had no idea what I 38134 00:17:39,912 --> 00:17:43,552 was doing but I'm like I'm going to go 38135 00:17:41,000 --> 00:17:45,800 fight um for this dumpling Emoji because 38136 00:17:43,552 --> 00:17:49,800 from my perspective dumplings are 38137 00:17:45,800 --> 00:17:51,280 Universal Emoji are kind of universal so 38138 00:17:49,800 --> 00:17:52,640 uh the fact there was no dumpling Emoji 38139 00:17:51,280 --> 00:17:54,512 meant like something was wrong in the 38140 00:17:52,640 --> 00:17:58,000 universe and I was I was determined to 38141 00:17:54,512 --> 00:17:59,912 fix this so um you know I was on this 38142 00:17:58,000 --> 00:18:01,832 email list and then a couple of like 38143 00:17:59,912 --> 00:18:03,360 maybe even like a couple weeks later I 38144 00:18:01,832 --> 00:18:04,960 got you know they they they kind of sent 38145 00:18:03,360 --> 00:18:07,040 out this note that's like hey who's 38146 00:18:04,960 --> 00:18:09,040 coming to the quarterly meeting and I 38147 00:18:07,040 --> 00:18:10,720 was like um I looked at the calendar I 38148 00:18:09,040 --> 00:18:13,192 looked at my schedule I was like oh I'll 38149 00:18:10,720 --> 00:18:16,280 be in like you know Silicon Valley that 38150 00:18:13,192 --> 00:18:19,120 time so I basically like rsvpd and I was 38151 00:18:16,280 --> 00:18:21,832 like I will be there and took um cow 38152 00:18:19,120 --> 00:18:23,512 train to an Apple building it's a legal 38153 00:18:21,832 --> 00:18:25,480 building in I think it was sunny Veil so 38154 00:18:23,512 --> 00:18:27,280 I just like show up and I don't know 38155 00:18:25,480 --> 00:18:28,912 what I was sort of expecting like with 38156 00:18:27,280 --> 00:18:29,800 like you know the Unicode I think maybe 38157 00:18:28,912 --> 00:18:31,360 thought it was going to be like a baby 38158 00:18:29,800 --> 00:18:33,000 Congress like you know like with a 38159 00:18:31,360 --> 00:18:35,640 little you know very formal seats people 38160 00:18:33,000 --> 00:18:38,080 with gabbles um that is not what I found 38161 00:18:35,640 --> 00:18:40,120 uh basically it is a it was a conference 38162 00:18:38,080 --> 00:18:42,640 room full of people who skewed wider 38163 00:18:40,120 --> 00:18:43,960 skewed older skewed Mal skewed engineers 38164 00:18:42,640 --> 00:18:46,832 and this is basically the room where it 38165 00:18:43,960 --> 00:18:49,872 happen so this is 2015 these were the 38166 00:18:46,832 --> 00:18:52,440 people who decided your Emoji um all 38167 00:18:49,872 --> 00:18:53,760 very nice and um there was you know one 38168 00:18:52,440 --> 00:18:55,680 one even had a daughter who had a sense 38169 00:18:53,760 --> 00:18:59,872 of humor and made him a shirt that said 38170 00:18:55,680 --> 00:19:01,832 shadowy Emoji Overlord um so I just kind 38171 00:18:59,872 --> 00:19:04,480 of listen to them debate things like 38172 00:19:01,832 --> 00:19:06,680 milk emoji and beans emoji and it just 38173 00:19:04,480 --> 00:19:09,280 seemed like not quite right to me that 38174 00:19:06,680 --> 00:19:11,912 like it would be uh This Global visual 38175 00:19:09,280 --> 00:19:13,720 language that were basically decided 38176 00:19:11,912 --> 00:19:15,640 decided by like a a small group of 38177 00:19:13,720 --> 00:19:18,760 people inside a conference room in 38178 00:19:15,640 --> 00:19:20,400 Silicon Valley so I decided to former 38179 00:19:18,760 --> 00:19:22,440 group called Emoji nation whose motto is 38180 00:19:20,400 --> 00:19:24,232 like Emoji by the people for the people 38181 00:19:22,440 --> 00:19:28,000 and it basically advocates for more kind 38182 00:19:24,232 --> 00:19:29,960 of Representative inclusive Emoji um you 38183 00:19:28,000 --> 00:19:32,720 know we we we we start with a 38184 00:19:29,960 --> 00:19:34,000 Kickstarter campaign uh dumpling Emoji 38185 00:19:32,720 --> 00:19:36,960 process trying to like you know write 38186 00:19:34,000 --> 00:19:38,512 the wrong in this world and uh made this 38187 00:19:36,960 --> 00:19:40,000 little cute video sort of advocating 38188 00:19:38,512 --> 00:19:42,400 dink for one of the most universal 38189 00:19:40,000 --> 00:19:46,000 crosscultural Foods in the world Georgia 38190 00:19:42,400 --> 00:19:48,720 has Kali Japan has giosa Korea has Mandu 38191 00:19:46,000 --> 00:19:50,640 Italy has ravioli Poland has barog 38192 00:19:48,720 --> 00:19:53,440 Russia has pelman Argentina has 38193 00:19:50,640 --> 00:19:56,512 empanadas Jewish people have kler China 38194 00:19:53,440 --> 00:19:58,760 has pot stickers Nepal and Tibet have 38195 00:19:56,512 --> 00:20:00,720 Momos yet somehow despite their 38196 00:19:58,760 --> 00:20:03,720 popularity there is no dumpling Emoji in 38197 00:20:00,720 --> 00:20:06,192 the standard set why is that emoji 38198 00:20:03,720 --> 00:20:08,512 exists for pizza tempora Sushi spaghetti 38199 00:20:06,192 --> 00:20:10,960 hot dog and now tacos which Taco Bell 38200 00:20:08,512 --> 00:20:13,960 takes credit for we need to write this 38201 00:20:10,960 --> 00:20:17,360 disparity dumplings are Global Emoji a 38202 00:20:13,960 --> 00:20:18,960 global isn't it time we brought them 38203 00:20:17,360 --> 00:20:20,640 together 38204 00:20:18,960 --> 00:20:23,600 oh yeah and while we're at it how about 38205 00:20:20,640 --> 00:20:23,600 an emoji for Chinese 38206 00:20:24,600 --> 00:20:29,512 takeout so uh I did put together a 38207 00:20:27,640 --> 00:20:31,800 dumpling Emoji proposal I wrote this uh 38208 00:20:29,512 --> 00:20:35,600 I remember Thanksgiving Day 2015 on a 38209 00:20:31,800 --> 00:20:37,760 plane and uh actually and we got it past 38210 00:20:35,600 --> 00:20:40,360 basically dumpling takeout box 38211 00:20:37,760 --> 00:20:42,680 Chopsticks and uh fortune cookie I have 38212 00:20:40,360 --> 00:20:44,080 to say I don't think fortune cookie 38213 00:20:42,680 --> 00:20:46,160 would have made it on its own merits but 38214 00:20:44,080 --> 00:20:48,080 it kind of like slid in on the clo tails 38215 00:20:46,160 --> 00:20:50,640 of the other ones um and so these were 38216 00:20:48,080 --> 00:20:52,832 the proposals as we submitted them and 38217 00:20:50,640 --> 00:20:54,280 then these are the ones that kind of uh 38218 00:20:52,832 --> 00:20:55,912 exist now on the Apple keyboard and I 38219 00:20:54,280 --> 00:20:59,280 have to say the dumpling looks really 38220 00:20:55,912 --> 00:21:00,912 really realistic um oddly realistic and 38221 00:20:59,280 --> 00:21:02,120 whereas like the fortune cookie is think 38222 00:21:00,912 --> 00:21:04,480 it's like a big fail because first of 38223 00:21:02,120 --> 00:21:06,832 all there's like it has no it has no Gap 38224 00:21:04,480 --> 00:21:07,720 it looks like a dead 3D Pac-Man so I'm 38225 00:21:06,832 --> 00:21:10,872 very 38226 00:21:07,720 --> 00:21:13,832 disappointed in uh the manifestation of 38227 00:21:10,872 --> 00:21:15,120 that but that's okay that's okay um and 38228 00:21:13,832 --> 00:21:18,552 so it's kind of interesting like what is 38229 00:21:15,120 --> 00:21:20,872 the process of getting an emoji um ped 38230 00:21:18,552 --> 00:21:22,080 and I will sort of walk you through it 38231 00:21:20,872 --> 00:21:25,400 so first of all you come up with your 38232 00:21:22,080 --> 00:21:27,320 idea right and then you kind of write 38233 00:21:25,400 --> 00:21:29,280 this proposal and then you submit it to 38234 00:21:27,320 --> 00:21:30,640 the Unicode Emoji subcommittee who then 38235 00:21:29,280 --> 00:21:32,160 kind of gives you comments and then 38236 00:21:30,640 --> 00:21:36,280 sends it back to you and you kind of go 38237 00:21:32,160 --> 00:21:38,320 around and around in the circle um and 38238 00:21:36,280 --> 00:21:39,640 when so these are things that we 38239 00:21:38,320 --> 00:21:41,192 consider so somewhere in there I also 38240 00:21:39,640 --> 00:21:43,280 like fought my way on to the Emoji 38241 00:21:41,192 --> 00:21:45,320 committee and then also became a vice 38242 00:21:43,280 --> 00:21:46,680 chair became a vice chair sort of a 38243 00:21:45,320 --> 00:21:49,680 extracurricular that's like completely 38244 00:21:46,680 --> 00:21:52,400 run a muck in my life um um so things 38245 00:21:49,680 --> 00:21:55,640 that matter uh popular demand is a 38246 00:21:52,400 --> 00:21:58,192 frequently requested Emoji um multiple 38247 00:21:55,640 --> 00:21:59,640 usages usages and meaning so that's 38248 00:21:58,192 --> 00:22:01,640 actually kind of very important for 38249 00:21:59,640 --> 00:22:05,400 something like you know certain animals 38250 00:22:01,640 --> 00:22:07,960 have meaning so we did you know sloth a 38251 00:22:05,400 --> 00:22:10,512 while ago and that also has not only the 38252 00:22:07,960 --> 00:22:13,040 literal meaning but sort of like um like 38253 00:22:10,512 --> 00:22:15,600 um connotations there are visually 38254 00:22:13,040 --> 00:22:17,800 distinctiveness it can be recognized so 38255 00:22:15,600 --> 00:22:20,080 this was a blocker for one of the uh 38256 00:22:17,800 --> 00:22:21,800 emojis that I have worked on before 38257 00:22:20,080 --> 00:22:24,160 which is cave which is kind of hard to 38258 00:22:21,800 --> 00:22:25,480 do I think you know meaning wise is 38259 00:22:24,160 --> 00:22:28,080 pretty good but it's really hard to get 38260 00:22:25,480 --> 00:22:30,400 a cave down in emoji sizes and then it 38261 00:22:28,080 --> 00:22:32,160 filling a gap completeness so a good 38262 00:22:30,400 --> 00:22:34,640 example for that is like for some reason 38263 00:22:32,160 --> 00:22:36,320 for many years we had Red Heart yellow 38264 00:22:34,640 --> 00:22:39,872 heart green heart blue heart purple 38265 00:22:36,320 --> 00:22:41,552 heart and uh no orange heart so so 38266 00:22:39,872 --> 00:22:43,120 somewhat so you know you would do the 38267 00:22:41,552 --> 00:22:44,872 rainbow but people would substitute the 38268 00:22:43,120 --> 00:22:46,192 pumpkin in so You' like you know have a 38269 00:22:44,872 --> 00:22:48,640 have a rainbow heart thing with a little 38270 00:22:46,192 --> 00:22:51,080 pumpkin stuck in the middle so orange 38271 00:22:48,640 --> 00:22:53,512 heart obviously should be added and give 38272 00:22:51,080 --> 00:22:55,192 a sense of completeness um and then 38273 00:22:53,512 --> 00:22:57,552 something else is existing vendor cap 38274 00:22:55,192 --> 00:23:01,872 cap compatibility and so a good example 38275 00:22:57,552 --> 00:23:04,400 for that was um many years ago what's 38276 00:23:01,872 --> 00:23:06,720 app decided to uh add the gender 38277 00:23:04,400 --> 00:23:08,800 non-binary emoji and then once it did 38278 00:23:06,720 --> 00:23:12,680 that then all the other kind of vendors 38279 00:23:08,800 --> 00:23:16,512 um jumped on um so what kind of knocks 38280 00:23:12,680 --> 00:23:19,040 out an emoji so too specific or narrow 38281 00:23:16,512 --> 00:23:21,000 so we'll often see that with like very 38282 00:23:19,040 --> 00:23:23,280 specific animals or a very specific 38283 00:23:21,000 --> 00:23:25,960 group it's redundant so one 38284 00:23:23,280 --> 00:23:27,800 year oh my God who makes that Butterball 38285 00:23:25,960 --> 00:23:30,832 Butterball makes the turkeys Butterball 38286 00:23:27,800 --> 00:23:32,680 submitted um a an emoji proposal that 38287 00:23:30,832 --> 00:23:34,280 was like a cooked turkey but we already 38288 00:23:32,680 --> 00:23:35,720 had a live turkey so it seem kind of 38289 00:23:34,280 --> 00:23:38,720 redundant have both like a cooked turkey 38290 00:23:35,720 --> 00:23:41,440 for Thanksgiving and a live turkey so so 38291 00:23:38,720 --> 00:23:42,912 not visually discernible um this is a 38292 00:23:41,440 --> 00:23:46,680 struggle for things like I don't my 38293 00:23:42,912 --> 00:23:49,040 friends have kind of proposed 38294 00:23:46,680 --> 00:23:51,760 kimchi kimchi is is really hard on emoji 38295 00:23:49,040 --> 00:23:53,000 sizes for many reasons and part of that 38296 00:23:51,760 --> 00:23:54,800 but part of that tension is because it's 38297 00:23:53,000 --> 00:23:57,080 not visually discernable then there are 38298 00:23:54,800 --> 00:24:00,280 no logos Brands deities or celebrities 38299 00:23:57,080 --> 00:24:01,760 so no Nike swish no McDonald's M and 38300 00:24:00,280 --> 00:24:04,040 then this is one that we kind of decided 38301 00:24:01,760 --> 00:24:06,000 in the last uh year or so which is no 38302 00:24:04,040 --> 00:24:09,080 more flags flags are a very complicated 38303 00:24:06,000 --> 00:24:10,360 thing um and as a result Unico does not 38304 00:24:09,080 --> 00:24:12,912 want to be in the business of deciding 38305 00:24:10,360 --> 00:24:13,960 what is a country or not a country uh so 38306 00:24:12,912 --> 00:24:16,800 like you know when you get a proposal 38307 00:24:13,960 --> 00:24:18,600 from like Kurdistan you're like yeah so 38308 00:24:16,800 --> 00:24:20,360 right now the way that the Emoji flags 38309 00:24:18,600 --> 00:24:22,872 are decided is they kind of depend on 38310 00:24:20,360 --> 00:24:24,080 what the UN recognizes and then those 38311 00:24:22,872 --> 00:24:25,832 get passed down to the international 38312 00:24:24,080 --> 00:24:27,800 standards organization and then and then 38313 00:24:25,832 --> 00:24:29,680 Uno just does that like it does not want 38314 00:24:27,800 --> 00:24:31,912 to be in the business of kind of you 38315 00:24:29,680 --> 00:24:34,912 know geopolitical 38316 00:24:31,912 --> 00:24:37,512 Affairs um so once it comes out of the 38317 00:24:34,912 --> 00:24:39,192 subcommittee it goes to the full Unicode 38318 00:24:37,512 --> 00:24:40,320 technical committee UTC those were the 38319 00:24:39,192 --> 00:24:45,160 people that were in the room that I 38320 00:24:40,320 --> 00:24:48,080 showed you um and they vote once a year 38321 00:24:45,160 --> 00:24:49,400 basically to pass all the Emoji and 38322 00:24:48,080 --> 00:24:50,680 takes a while there's a lot of like 38323 00:24:49,400 --> 00:24:53,720 coordinating with the international 38324 00:24:50,680 --> 00:24:54,912 standards organization and like a lot of 38325 00:24:53,720 --> 00:24:56,552 working with the companies and 38326 00:24:54,912 --> 00:24:59,040 eventually it takes a long time but it 38327 00:24:56,552 --> 00:25:00,832 ends up on your on your devices and that 38328 00:24:59,040 --> 00:25:02,280 historically is taken about 18 to 24 38329 00:25:00,832 --> 00:25:03,640 months from when you put in the propose 38330 00:25:02,280 --> 00:25:06,080 to when you get it in it's going to be a 38331 00:25:03,640 --> 00:25:09,320 little bit longer um going forward 38332 00:25:06,080 --> 00:25:11,192 probably is my sense as we were're going 38333 00:25:09,320 --> 00:25:14,320 to Unicode has decided to sort of space 38334 00:25:11,192 --> 00:25:16,872 out some of the Unicode releases so yeah 38335 00:25:14,320 --> 00:25:18,800 Emoji Nation you know kind of has done 38336 00:25:16,872 --> 00:25:20,640 its thing so of the weird things is like 38337 00:25:18,800 --> 00:25:22,400 how did Unicode this like kind of 38338 00:25:20,640 --> 00:25:24,192 nonprofit organization based in Mountain 38339 00:25:22,400 --> 00:25:27,512 View California end up controlling this 38340 00:25:24,192 --> 00:25:29,680 like Global visual language um so a lot 38341 00:25:27,512 --> 00:25:31,120 of it has to start with has has to do 38342 00:25:29,680 --> 00:25:33,872 with the fact that emoji started in 38343 00:25:31,120 --> 00:25:38,192 Japan uh back in the late 1990s one of 38344 00:25:33,872 --> 00:25:39,440 the this set from dok 1999 is considered 38345 00:25:38,192 --> 00:25:41,512 is widely considered sort of like the 38346 00:25:39,440 --> 00:25:44,320 first like color Emoji set it has been 38347 00:25:41,512 --> 00:25:46,760 collected by the Museum of Modern Art 38348 00:25:44,320 --> 00:25:48,600 and um so these the Japanese telecom 38349 00:25:46,760 --> 00:25:50,440 companies would use 38350 00:25:48,600 --> 00:25:52,120 basically would have their own sets of 38351 00:25:50,440 --> 00:25:53,512 emoji and then they were different 38352 00:25:52,120 --> 00:25:55,192 companies so they would have different 38353 00:25:53,512 --> 00:25:56,832 sets so you could basically only send 38354 00:25:55,192 --> 00:25:59,040 these like visual characters with 38355 00:25:56,832 --> 00:26:01,160 someone who is on your same carrier so 38356 00:25:59,040 --> 00:26:02,800 it's like it's like basically equivalent 38357 00:26:01,160 --> 00:26:05,080 of if you were on like Verizon you can 38358 00:26:02,800 --> 00:26:07,600 only text people on Verizon with like 38359 00:26:05,080 --> 00:26:10,760 Emoji or if you were on um T-Mobile you 38360 00:26:07,600 --> 00:26:14,760 could only do that so at a certain point 38361 00:26:10,760 --> 00:26:16,512 they um decided they were they the Apple 38362 00:26:14,760 --> 00:26:18,400 and Google came into Japan they wanted 38363 00:26:16,512 --> 00:26:21,280 to start selling smartphones and they 38364 00:26:18,400 --> 00:26:23,912 realized that it was a hodgepodge of 38365 00:26:21,280 --> 00:26:26,720 systems and they wanted to unify it and 38366 00:26:23,912 --> 00:26:29,280 so in 2007 they went to unic code and 38367 00:26:26,720 --> 00:26:33,232 they're like okay help us unify the 38368 00:26:29,280 --> 00:26:34,960 Emoji um like kind of like basically all 38369 00:26:33,232 --> 00:26:37,912 the Emojis so that we have one standard 38370 00:26:34,960 --> 00:26:39,600 system and part of the reason is why 38371 00:26:37,912 --> 00:26:41,440 Unicode because Unicode basically has 38372 00:26:39,600 --> 00:26:43,120 this mission is to enable everyone 38373 00:26:41,440 --> 00:26:44,912 speaking every language on Earth to be 38374 00:26:43,120 --> 00:26:47,480 able to use your languages on computer 38375 00:26:44,912 --> 00:26:50,080 and smartphone so it basically unifies 38376 00:26:47,480 --> 00:26:52,280 um all written languages into one 38377 00:26:50,080 --> 00:26:54,872 ginormous set and that was not the case 38378 00:26:52,280 --> 00:26:56,912 actually when I was uh growing up there 38379 00:26:54,872 --> 00:26:58,120 was a point where like if you you were 38380 00:26:56,912 --> 00:27:00,800 Japanese on Apple that would be 38381 00:26:58,120 --> 00:27:03,192 different than Japanese on um dos or 38382 00:27:00,800 --> 00:27:04,760 like Chinese or Arabic so it drove 38383 00:27:03,192 --> 00:27:07,832 everyone crazy and they basically 38384 00:27:04,760 --> 00:27:09,760 decided um around the late 80s early 38385 00:27:07,832 --> 00:27:11,320 1990s that they were going to come up 38386 00:27:09,760 --> 00:27:15,440 with one standardized system that sort 38387 00:27:11,320 --> 00:27:18,640 of encoded all characters in in one 38388 00:27:15,440 --> 00:27:20,232 ginormous set so um there's three main 38389 00:27:18,640 --> 00:27:22,480 projects for Unicode if you care so one 38390 00:27:20,232 --> 00:27:24,080 is encoding characters including Emoji 38391 00:27:22,480 --> 00:27:27,000 now they're about 100,000 characters 38392 00:27:24,080 --> 00:27:30,680 aside assigned so that includes like 38393 00:27:27,000 --> 00:27:33,760 like Chinese Japanese Korean uh Arabic 38394 00:27:30,680 --> 00:27:36,400 cilic um actually all the hieroglyphics 38395 00:27:33,760 --> 00:27:38,512 all of the Emoji um a lot of things like 38396 00:27:36,400 --> 00:27:40,960 the Bitcoin symbol or like copy left or 38397 00:27:38,512 --> 00:27:43,320 whatever those all assigned were about 38398 00:27:40,960 --> 00:27:45,760 100,000 uh characters even those 38399 00:27:43,320 --> 00:27:48,280 languages that are basically out of use 38400 00:27:45,760 --> 00:27:51,800 so the other thing it does it creates 38401 00:27:48,280 --> 00:27:53,000 um localization resources so things 38402 00:27:51,800 --> 00:27:55,552 things so that you know like oh if 38403 00:27:53,000 --> 00:27:58,000 you're in this country this is this is 38404 00:27:55,552 --> 00:27:59,760 uh you're using the Euro or you're using 38405 00:27:58,000 --> 00:28:02,320 the pesos or something so there's a lot 38406 00:27:59,760 --> 00:28:05,400 of localization that is that data that 38407 00:28:02,320 --> 00:28:07,120 is needed depending on which um 38408 00:28:05,400 --> 00:28:08,872 geography you're using your device from 38409 00:28:07,120 --> 00:28:10,552 or like that you know you you know that 38410 00:28:08,872 --> 00:28:12,640 the time is used this way or the dates 38411 00:28:10,552 --> 00:28:16,040 are shown that way so that is called um 38412 00:28:12,640 --> 00:28:17,440 the common local data repository or cldr 38413 00:28:16,040 --> 00:28:19,280 as they call it and the other thing they 38414 00:28:17,440 --> 00:28:22,440 do is they kind of maintain libraries 38415 00:28:19,280 --> 00:28:23,760 for developers in order to do all um to 38416 00:28:22,440 --> 00:28:25,000 kind of so they are not building 38417 00:28:23,760 --> 00:28:27,552 everything from scratch and that is 38418 00:28:25,000 --> 00:28:30,832 called ICU so it's very funny cuz like 38419 00:28:27,552 --> 00:28:33,720 it's called cldr and then one day one of 38420 00:28:30,832 --> 00:28:37,040 our friends girlfriends made him this 38421 00:28:33,720 --> 00:28:39,232 thing which she called a seal deer 38422 00:28:37,040 --> 00:28:41,912 because whenever he talked about 38423 00:28:39,232 --> 00:28:44,000 cldr he thought he thought she thought 38424 00:28:41,912 --> 00:28:46,120 he was talking about seal deers so now 38425 00:28:44,000 --> 00:28:49,120 this is his little like mascot for like 38426 00:28:46,120 --> 00:28:50,800 Unicode related things so what is um how 38427 00:28:49,120 --> 00:28:53,640 does Unicode do this so it basically 38428 00:28:50,800 --> 00:28:56,000 asss code points and each code point is 38429 00:28:53,640 --> 00:29:00,192 a unique number assigned to each Unicode 38430 00:28:56,000 --> 00:29:02,400 character so uh face with tears of joy 38431 00:29:00,192 --> 00:29:04,400 can be written like this with a code 38432 00:29:02,400 --> 00:29:05,960 point or it can be written like this in 38433 00:29:04,400 --> 00:29:07,360 terms of decimal numbers and it can also 38434 00:29:05,960 --> 00:29:09,800 be written this in binary numbers so 38435 00:29:07,360 --> 00:29:12,040 these are all basically the same and the 38436 00:29:09,800 --> 00:29:13,800 key thing to know at least about Emoji 38437 00:29:12,040 --> 00:29:16,000 is that when your device if you're 38438 00:29:13,800 --> 00:29:18,600 talking your iPhone with um someone 38439 00:29:16,000 --> 00:29:20,320 who's on a laptop or an iPad or like um 38440 00:29:18,600 --> 00:29:22,040 an Android device it's not sending the 38441 00:29:20,320 --> 00:29:24,040 images back and forth it's sending just 38442 00:29:22,040 --> 00:29:27,120 a number back and forth and then locally 38443 00:29:24,040 --> 00:29:29,640 your phone or like your laptop decides 38444 00:29:27,120 --> 00:29:32,232 like oh this number correlates with 38445 00:29:29,640 --> 00:29:34,232 which image um in terms of our emoji 38446 00:29:32,232 --> 00:29:37,872 font and then pulls it up so this is 38447 00:29:34,232 --> 00:29:39,680 really key to know why different Emoji 38448 00:29:37,872 --> 00:29:43,320 look different on different 38449 00:29:39,680 --> 00:29:45,320 platforms so 2007 to 2010 it took about 38450 00:29:43,320 --> 00:29:46,872 three years but Unicode 6.0 came out 38451 00:29:45,320 --> 00:29:49,040 with a first our first little baby set 38452 00:29:46,872 --> 00:29:50,872 of emoji um and it just kind of hung out 38453 00:29:49,040 --> 00:29:53,872 there for a year like no one it wasn't 38454 00:29:50,872 --> 00:29:56,192 doing anything so 2011 though Apple 38455 00:29:53,872 --> 00:29:58,760 starts adding the emoji keyboard and it 38456 00:29:56,192 --> 00:30:01,040 it just like explodes like I feel like 38457 00:29:58,760 --> 00:30:03,160 in some ways Emoji are were not like 38458 00:30:01,040 --> 00:30:05,040 invented they were discovered they like 38459 00:30:03,160 --> 00:30:09,512 obviously touch something very very 38460 00:30:05,040 --> 00:30:11,120 Primal to um to like our human desire to 38461 00:30:09,512 --> 00:30:14,080 like communicate in like little colorful 38462 00:30:11,120 --> 00:30:15,440 glyphs on electronic devices and you 38463 00:30:14,080 --> 00:30:18,600 kind of have what's kind of really 38464 00:30:15,440 --> 00:30:21,232 interesting is like the ambiguity that 38465 00:30:18,600 --> 00:30:22,800 comes with what emoji kind of mean and 38466 00:30:21,232 --> 00:30:24,232 so one of the you know this one my 38467 00:30:22,800 --> 00:30:26,080 favorite emoji is sort of like an upside 38468 00:30:24,232 --> 00:30:27,872 down smiley face very very ambiguous 38469 00:30:26,080 --> 00:30:29,600 clearly very ambiguous because if you 38470 00:30:27,872 --> 00:30:31,192 start typing into Google like the top 38471 00:30:29,600 --> 00:30:32,872 hits are like what does it mean from a 38472 00:30:31,192 --> 00:30:34,440 guy what does it mean from a girl like 38473 00:30:32,872 --> 00:30:36,640 it's clearly something that a lot of 38474 00:30:34,440 --> 00:30:41,080 people are are using in like complex 38475 00:30:36,640 --> 00:30:42,960 situationships between each other so um 38476 00:30:41,080 --> 00:30:45,120 so so one of the fun things is who can 38477 00:30:42,960 --> 00:30:47,760 propose Emoji technically anyone can 38478 00:30:45,120 --> 00:30:49,832 anyone here can normal normal humans can 38479 00:30:47,760 --> 00:30:51,640 we have basically Google doc uh or 38480 00:30:49,832 --> 00:30:54,000 Google form that we throw up uh 38481 00:30:51,640 --> 00:30:57,960 historically between August sorry April 38482 00:30:54,000 --> 00:31:00,280 and August in the last two years um and 38483 00:30:57,960 --> 00:31:02,552 uh so this is one of our my our favorite 38484 00:31:00,280 --> 00:31:04,800 examples this is Ru aumed she was a 38485 00:31:02,552 --> 00:31:06,912 15-year-old Saudi Arabian girl who was 38486 00:31:04,800 --> 00:31:10,600 living in Vienna at the time that she 38487 00:31:06,912 --> 00:31:12,040 proposed the um hijab emoji and then you 38488 00:31:10,600 --> 00:31:13,680 know then she was like Time Magazine 38489 00:31:12,040 --> 00:31:14,640 like you know coolest teens she got like 38490 00:31:13,680 --> 00:31:16,360 a whole bunch of different things she 38491 00:31:14,640 --> 00:31:20,760 got into Harvard and Stanford and she 38492 00:31:16,360 --> 00:31:22,400 went to Stanford um and uh so this is a 38493 00:31:20,760 --> 00:31:24,552 PR we got then there was a group of 38494 00:31:22,400 --> 00:31:26,872 folks from Argentina who got the mate 38495 00:31:24,552 --> 00:31:29,720 Emoji kind of they you know similar 38496 00:31:26,872 --> 00:31:31,760 their national drink then we worked with 38497 00:31:29,720 --> 00:31:36,360 um there was a a nonprofit that really 38498 00:31:31,760 --> 00:31:38,400 wanted to like get a menstruation emoji 38499 00:31:36,360 --> 00:31:40,680 and so what they proposed to begin with 38500 00:31:38,400 --> 00:31:43,480 was like bloody underwear and I was just 38501 00:31:40,680 --> 00:31:46,320 like no no no so many different reasons 38502 00:31:43,480 --> 00:31:47,680 why this is a terrible Emoji uh but so 38503 00:31:46,320 --> 00:31:49,960 we did get blood drops so then you can 38504 00:31:47,680 --> 00:31:51,320 do Moon and blood drop or underwear and 38505 00:31:49,960 --> 00:31:52,800 blood drop there's a lot of different 38506 00:31:51,320 --> 00:31:55,080 ways actually it's really funny cuz I 38507 00:31:52,800 --> 00:31:57,912 was ping my friends beforehand what they 38508 00:31:55,080 --> 00:31:59,480 would use to indicate menstruation 38509 00:31:57,912 --> 00:32:01,872 before there you know there was a blood 38510 00:31:59,480 --> 00:32:05,080 drop so there was like red wine there 38511 00:32:01,872 --> 00:32:07,400 was like kind of that Rose um with the 38512 00:32:05,080 --> 00:32:09,832 like falling petal and then my favorite 38513 00:32:07,400 --> 00:32:11,720 is actually um my friend who used a 38514 00:32:09,832 --> 00:32:13,480 Japanese flag as a way to indicate that 38515 00:32:11,720 --> 00:32:16,280 she was having her 38516 00:32:13,480 --> 00:32:19,480 period so um one of the biggest 38517 00:32:16,280 --> 00:32:22,160 contributors this uh skin tone Emoji 38518 00:32:19,480 --> 00:32:25,480 appeared I think in 2015 it was amazing 38519 00:32:22,160 --> 00:32:27,872 it is and it was proposed by a mom 38520 00:32:25,480 --> 00:32:29,800 Katrina parrot she is a entrepreneur and 38521 00:32:27,872 --> 00:32:32,400 a mom who is just like at home one day 38522 00:32:29,800 --> 00:32:35,080 and her daughter comes home and is 38523 00:32:32,400 --> 00:32:37,680 like you know I wish there were emoji 38524 00:32:35,080 --> 00:32:40,192 that looked like me and her mom was like 38525 00:32:37,680 --> 00:32:43,000 that's great honey what's an emoji and 38526 00:32:40,192 --> 00:32:45,640 so she like me I guess Googled and just 38527 00:32:43,000 --> 00:32:47,000 figured out that uh Unicode controlled 38528 00:32:45,640 --> 00:32:48,872 emoji and she just came up with a 38529 00:32:47,000 --> 00:32:50,512 proposal saying we should not only have 38530 00:32:48,872 --> 00:32:51,872 the yellow you know skin tones at that 38531 00:32:50,512 --> 00:32:55,552 time everything was sort of Simpsons 38532 00:32:51,872 --> 00:32:59,232 yellow um it's really interesting to see 38533 00:32:55,552 --> 00:33:01,400 how race and like nationality are like 38534 00:32:59,232 --> 00:33:04,232 depicted in different parts of the world 38535 00:33:01,400 --> 00:33:07,232 so originally in Japan everyone was 38536 00:33:04,232 --> 00:33:08,512 yellow um but this these were the non 38537 00:33:07,232 --> 00:33:11,192 you know by default everyone was just 38538 00:33:08,512 --> 00:33:12,512 like like you know human or Japanese but 38539 00:33:11,192 --> 00:33:15,360 they had like a couple things that were 38540 00:33:12,512 --> 00:33:17,280 like not like one was you had a blonde 38541 00:33:15,360 --> 00:33:19,040 person so there's an emoji called like 38542 00:33:17,280 --> 00:33:21,192 blonde blonde woman or whatever that 38543 00:33:19,040 --> 00:33:23,080 represents all westerners okay so that 38544 00:33:21,192 --> 00:33:24,680 was one and then they have one that's 38545 00:33:23,080 --> 00:33:25,912 like an Indian guy with a turban so 38546 00:33:24,680 --> 00:33:28,160 that's supposed to represent Indian 38547 00:33:25,912 --> 00:33:30,480 people and then there's like a like a 38548 00:33:28,160 --> 00:33:31,440 guy with a little like um little hat 38549 00:33:30,480 --> 00:33:33,120 that's supposed to represent like 38550 00:33:31,440 --> 00:33:35,040 Chinese people so that was like that was 38551 00:33:33,120 --> 00:33:37,512 the view Japanese view of race which was 38552 00:33:35,040 --> 00:33:39,600 like default then you were I then you 38553 00:33:37,512 --> 00:33:42,600 were like blonde Western Chinese or 38554 00:33:39,600 --> 00:33:44,640 Indian and and that is all there was and 38555 00:33:42,600 --> 00:33:46,720 um obviously in the United States we 38556 00:33:44,640 --> 00:33:48,360 care a lot about race and then so she 38557 00:33:46,720 --> 00:33:51,400 came up with this system with five skin 38558 00:33:48,360 --> 00:33:52,872 tones um like just like normal people 38559 00:33:51,400 --> 00:33:56,320 some guy in Germany decided that he 38560 00:33:52,872 --> 00:33:59,640 wanted to do a a a face with one eyebrow 38561 00:33:56,320 --> 00:34:04,320 raised or as we call it the coar Emoji 38562 00:33:59,640 --> 00:34:06,552 um and oh this one's fun so woman's flat 38563 00:34:04,320 --> 00:34:08,680 shoe I have to say not highly used 38564 00:34:06,552 --> 00:34:09,912 statistically at this point but I really 38565 00:34:08,680 --> 00:34:11,872 kind of appreciate it because it was a 38566 00:34:09,912 --> 00:34:14,760 mom who was very offended that all 38567 00:34:11,872 --> 00:34:16,600 women's shoes had heels even the sandals 38568 00:34:14,760 --> 00:34:18,480 so this is her she had like three kids 38569 00:34:16,600 --> 00:34:25,280 at the time now is four she was very 38570 00:34:18,480 --> 00:34:30,040 fertile um and she also did um the women 38571 00:34:25,280 --> 00:34:32,680 women's flat okay she also did um um one 38572 00:34:30,040 --> 00:34:34,512 piece bathing suit because she was also 38573 00:34:32,680 --> 00:34:35,832 um offended by the fact that the only 38574 00:34:34,512 --> 00:34:38,000 kind of bathing suit you had was like 38575 00:34:35,832 --> 00:34:39,120 this like little itsy-bitsy polka dot 38576 00:34:38,000 --> 00:34:41,232 bikini thing which is not great if 38577 00:34:39,120 --> 00:34:42,600 you're like taking your six-year-old so 38578 00:34:41,232 --> 00:34:44,232 I have to say that got passed but like 38579 00:34:42,600 --> 00:34:46,832 it didn't go over like super well with 38580 00:34:44,232 --> 00:34:48,800 everyone um so you know unic Cod because 38581 00:34:46,832 --> 00:34:51,800 it's very public submits things for 38582 00:34:48,800 --> 00:34:54,232 comments and um we got this comment back 38583 00:34:51,800 --> 00:34:56,192 so one piece bathing suit why a person 38584 00:34:54,232 --> 00:34:58,872 want to indicate the use of swimwear 38585 00:34:56,192 --> 00:35:01,280 can't use existing bikini is this really 38586 00:34:58,872 --> 00:35:03,512 necessary what about a Victorian bathing 38587 00:35:01,280 --> 00:35:06,232 costume or a wet suit or water rings 38588 00:35:03,512 --> 00:35:10,440 this is like literally in in like uh the 38589 00:35:06,232 --> 00:35:11,680 records and like do not encode um and so 38590 00:35:10,440 --> 00:35:13,872 the person who did it's actually very 38591 00:35:11,680 --> 00:35:16,680 impressive he's actually the person who 38592 00:35:13,872 --> 00:35:18,800 created the middle finger emoji and 38593 00:35:16,680 --> 00:35:20,512 actually you ever seen the Vulcan Emoji 38594 00:35:18,800 --> 00:35:23,192 he he the Vulcan hand emoji he's 38595 00:35:20,512 --> 00:35:24,480 actually very active and I have to say 38596 00:35:23,192 --> 00:35:26,400 this is actually I think one of the more 38597 00:35:24,480 --> 00:35:29,120 impressive Emoji so so obviously 38598 00:35:26,400 --> 00:35:30,552 obviously we have a lot of active debate 38599 00:35:29,120 --> 00:35:32,872 um sometimes you get like whole 38600 00:35:30,552 --> 00:35:34,440 countries submitting so literally the 38601 00:35:32,872 --> 00:35:35,760 government of Finland as in like their 38602 00:35:34,440 --> 00:35:38,680 equivalent in the state department their 38603 00:35:35,760 --> 00:35:42,600 diplomacy kind of thing uh submitted a 38604 00:35:38,680 --> 00:35:46,000 proposal for SAA and um these were so 38605 00:35:42,600 --> 00:35:50,080 creepy and so 38606 00:35:46,000 --> 00:35:52,440 weird so much is wrong with this and but 38607 00:35:50,080 --> 00:35:54,360 I I I felt where they were coming from 38608 00:35:52,440 --> 00:35:57,400 first of all like they're naked they 38609 00:35:54,360 --> 00:35:59,440 have no they have club feet and so we 38610 00:35:57,400 --> 00:36:01,000 decided to help them we're like okay we 38611 00:35:59,440 --> 00:36:03,640 see where you're going with this let's 38612 00:36:01,000 --> 00:36:05,280 see if we can like help you like come up 38613 00:36:03,640 --> 00:36:07,000 you know like we had the spoon like 38614 00:36:05,280 --> 00:36:08,440 should there be like steam around them 38615 00:36:07,000 --> 00:36:10,720 like should they be naked or wearing a 38616 00:36:08,440 --> 00:36:11,872 tow towel it's like super like dicey but 38617 00:36:10,720 --> 00:36:13,192 we wanted to help them because it was 38618 00:36:11,872 --> 00:36:15,320 like literally a foreign government 38619 00:36:13,192 --> 00:36:17,192 coming from an Unicode advocating for 38620 00:36:15,320 --> 00:36:19,600 the sauna emoji on behalf of their ENT 38621 00:36:17,192 --> 00:36:21,160 country so then this is um is sort of 38622 00:36:19,600 --> 00:36:23,192 evolved into just basically person in 38623 00:36:21,160 --> 00:36:25,872 stey room which is which is the most 38624 00:36:23,192 --> 00:36:27,600 sort of like the PG version of sauna no 38625 00:36:25,872 --> 00:36:30,000 and there's no spoon they're all dressed 38626 00:36:27,600 --> 00:36:31,912 it's very odd but um so you can see the 38627 00:36:30,000 --> 00:36:34,640 evolution of what it started out what we 38628 00:36:31,912 --> 00:36:36,680 submitted and what it ended up so 38629 00:36:34,640 --> 00:36:39,000 there's a lot of like Evolution 38630 00:36:36,680 --> 00:36:41,360 throughout the entire process um and 38631 00:36:39,000 --> 00:36:43,080 like companies can submit Emoji 38632 00:36:41,360 --> 00:36:44,800 proposals too so Google actually worked 38633 00:36:43,080 --> 00:36:47,080 on this one I love this one okay so just 38634 00:36:44,800 --> 00:36:49,400 to give you some context as of 2015 38635 00:36:47,080 --> 00:36:52,912 there are many ways you could be or have 38636 00:36:49,400 --> 00:36:55,080 an occupation as a male on the Emoji 38637 00:36:52,912 --> 00:36:57,320 Keyboard right like like for example you 38638 00:36:55,080 --> 00:36:59,120 could be a police officer you could be a 38639 00:36:57,320 --> 00:37:01,440 detective you could be a Buckingham 38640 00:36:59,120 --> 00:37:03,160 Palace guard you could even be Santa 38641 00:37:01,440 --> 00:37:04,760 Claus like these are so many jobs that 38642 00:37:03,160 --> 00:37:06,320 you could have but if you were a woman 38643 00:37:04,760 --> 00:37:08,680 as of 2015 there were four things that 38644 00:37:06,320 --> 00:37:11,360 you could be you could be a princess you 38645 00:37:08,680 --> 00:37:12,720 could be a bride you could be a dancer 38646 00:37:11,360 --> 00:37:14,512 or you could be a Playboy Bunny these 38647 00:37:12,720 --> 00:37:18,440 were the sum total of all the 38648 00:37:14,512 --> 00:37:19,800 occupations that we can have so so we 38649 00:37:18,440 --> 00:37:21,320 there was sort of this movement at that 38650 00:37:19,800 --> 00:37:23,360 time there was like this like video on 38651 00:37:21,320 --> 00:37:25,080 YouTube that like went viral there was 38652 00:37:23,360 --> 00:37:27,800 like a New York Times op that was like 38653 00:37:25,080 --> 00:37:29,720 where are the women with professions so 38654 00:37:27,800 --> 00:37:32,360 um basically they came up with a set of 38655 00:37:29,720 --> 00:37:34,080 emoji for professions and what's nice is 38656 00:37:32,360 --> 00:37:36,552 not only did women have these 38657 00:37:34,080 --> 00:37:38,640 professions now men have them too so of 38658 00:37:36,552 --> 00:37:40,872 emoji Nation Emoji these are some of the 38659 00:37:38,640 --> 00:37:42,832 ones that we've worked on I think about 38660 00:37:40,872 --> 00:37:44,872 130 of the Emoji on your keyboard 38661 00:37:42,832 --> 00:37:48,040 probably came through touched our system 38662 00:37:44,872 --> 00:37:50,640 in in some way including I I have to say 38663 00:37:48,040 --> 00:37:52,552 uh microbe or virus I think I have the 38664 00:37:50,640 --> 00:37:54,960 opinion that every Emoji has its day 38665 00:37:52,552 --> 00:37:56,760 right like it might not be like today it 38666 00:37:54,960 --> 00:37:58,320 might not be next year but I have to say 38667 00:37:56,760 --> 00:38:00,000 virus was not doing anything then came 38668 00:37:58,320 --> 00:38:02,400 2020 and that was like such a good 38669 00:38:00,000 --> 00:38:06,400 moment for it um along with soap we had 38670 00:38:02,400 --> 00:38:07,960 also done soap so um you know among the 38671 00:38:06,400 --> 00:38:10,872 other emoji that we have worked on are 38672 00:38:07,960 --> 00:38:12,512 sari moon cake llama like uh teddy bear 38673 00:38:10,872 --> 00:38:15,960 there were no toys I felt really sad for 38674 00:38:12,512 --> 00:38:18,160 toys we have like giraffe um there was 38675 00:38:15,960 --> 00:38:20,280 Hut bubble te bubble tea was very 38676 00:38:18,160 --> 00:38:22,120 controversial actually I have to say it 38677 00:38:20,280 --> 00:38:24,160 kind we tried to slide it in originally 38678 00:38:22,120 --> 00:38:26,512 with the um takeout box and the 38679 00:38:24,160 --> 00:38:28,360 dumplings and people were not not having 38680 00:38:26,512 --> 00:38:30,872 it I understand that CU there's not a 38681 00:38:28,360 --> 00:38:32,600 lot like compared to like beer or wine 38682 00:38:30,872 --> 00:38:33,960 like bubble tea does not is not long 38683 00:38:32,600 --> 00:38:39,600 does not have a long history on this 38684 00:38:33,960 --> 00:38:41,680 planet um but I will say that um they 38685 00:38:39,600 --> 00:38:43,320 submitted again actually kind of 38686 00:38:41,680 --> 00:38:45,440 originally proposing that it was not 38687 00:38:43,320 --> 00:38:47,232 just bubble tea but but like a black 38688 00:38:45,440 --> 00:38:49,400 ball and milk and tea it was It was kind 38689 00:38:47,232 --> 00:38:51,120 of cool and I I have to say there was 38690 00:38:49,400 --> 00:38:53,192 definitely a generational divide between 38691 00:38:51,120 --> 00:38:54,960 like the Asian women who sit in that 38692 00:38:53,192 --> 00:38:56,800 room and are like this absolutely is a 38693 00:38:54,960 --> 00:38:58,760 thing that we consume like almost like 38694 00:38:56,800 --> 00:39:00,640 every week of our life and people who 38695 00:38:58,760 --> 00:39:02,160 are a little bit older who are like that 38696 00:39:00,640 --> 00:39:03,320 looks like a parfait how do you not know 38697 00:39:02,160 --> 00:39:05,800 that's a parfet and we're like we 38698 00:39:03,320 --> 00:39:07,600 absolutely know it is uh not a parfait 38699 00:39:05,800 --> 00:39:09,600 and so so it got in eventually so it 38700 00:39:07,600 --> 00:39:12,232 does sort of influence it kind of shows 38701 00:39:09,600 --> 00:39:14,912 like who is in the room influences you 38702 00:39:12,232 --> 00:39:16,760 know the decisions um that get made or 38703 00:39:14,912 --> 00:39:18,912 sometimes in the room now sometimes more 38704 00:39:16,760 --> 00:39:21,400 more likely in the zoom um I actually I 38705 00:39:18,912 --> 00:39:22,912 just say beaver Emoji if you see Beaver 38706 00:39:21,400 --> 00:39:24,680 Emoji that's one of one of the ones I'm 38707 00:39:22,912 --> 00:39:26,040 most proud of so that is actually 38708 00:39:24,680 --> 00:39:29,680 co-authored by a professor here at 38709 00:39:26,040 --> 00:39:31,720 Harvard um who is both lesbian and was 38710 00:39:29,680 --> 00:39:33,600 married to a woman from Canada so it was 38711 00:39:31,720 --> 00:39:36,720 very important to her to get um the 38712 00:39:33,600 --> 00:39:38,320 beaver Emoji passed and she promised me 38713 00:39:36,720 --> 00:39:40,680 it would always be the first line of her 38714 00:39:38,320 --> 00:39:43,720 bio so and indeed if you go to her 38715 00:39:40,680 --> 00:39:46,360 Twitter handle uh it's like Joan Donovan 38716 00:39:43,720 --> 00:39:47,872 creator of the Beaver Emoji comma is 38717 00:39:46,360 --> 00:39:49,960 headed research at the shoren scene 38718 00:39:47,872 --> 00:39:52,832 Center at the Harvard Kennedy School 38719 00:39:49,960 --> 00:39:54,600 It's Kind it's pretty impressive um and 38720 00:39:52,832 --> 00:39:57,440 then we did greens actually greens was 38721 00:39:54,600 --> 00:39:58,960 really interesting because people this 38722 00:39:57,440 --> 00:40:00,512 was also like a generational cultural 38723 00:39:58,960 --> 00:40:02,872 thing people were like why do we need 38724 00:40:00,512 --> 00:40:04,760 greens we have salad and I was like 38725 00:40:02,872 --> 00:40:06,160 we're Chinese we don't eat raw greens 38726 00:40:04,760 --> 00:40:08,232 cuz like you don't know where it's been 38727 00:40:06,160 --> 00:40:09,912 or if it's clean so we cook our greens 38728 00:40:08,232 --> 00:40:13,192 so salad is not something that we have 38729 00:40:09,912 --> 00:40:14,680 So eventually I got my greens and um so 38730 00:40:13,192 --> 00:40:16,080 that was kind of fun and then so these 38731 00:40:14,680 --> 00:40:18,280 are some of the people who sort of have 38732 00:40:16,080 --> 00:40:19,872 contributed to our little Emoji Nation 38733 00:40:18,280 --> 00:40:22,040 things including a number of Native 38734 00:40:19,872 --> 00:40:23,760 Americans who help get feather so why do 38735 00:40:22,040 --> 00:40:25,400 I care so part of it is because I'm 38736 00:40:23,760 --> 00:40:26,760 Chinese I grew up speaking Chinese and 38737 00:40:25,400 --> 00:40:28,720 English at the same time and it's really 38738 00:40:26,760 --> 00:40:31,720 interesting to see like Chinese and 38739 00:40:28,720 --> 00:40:33,832 English characters in terms of emoji and 38740 00:40:31,720 --> 00:40:35,440 Chinese together right so this is fire 38741 00:40:33,832 --> 00:40:36,760 you have like Fire characters and now 38742 00:40:35,440 --> 00:40:38,120 you have the Emoji things and it's kind 38743 00:40:36,760 --> 00:40:40,000 of It kind of shows that there is a 38744 00:40:38,120 --> 00:40:41,480 longevity in The Human Experience of 38745 00:40:40,000 --> 00:40:43,680 something that was designed 4,000 years 38746 00:40:41,480 --> 00:40:47,800 ago as the same visual cues in the human 38747 00:40:43,680 --> 00:40:50,872 experiences as now so mouth Tre tree 38748 00:40:47,800 --> 00:40:54,400 Moon Sun you can mix and match them 38749 00:40:50,872 --> 00:40:57,080 which is super fun um so you know two 38750 00:40:54,400 --> 00:41:01,600 trees kind of make a forest oops sorry 38751 00:40:57,080 --> 00:41:07,000 oh well okay then the um Moon and Sun 38752 00:41:01,600 --> 00:41:08,960 together means bright which I like um I 38753 00:41:07,000 --> 00:41:12,000 like this one so if you stop and you 38754 00:41:08,960 --> 00:41:16,120 think about this so this is a basically 38755 00:41:12,000 --> 00:41:17,480 a pig under a roof and what does that 38756 00:41:16,120 --> 00:41:19,440 mean 38757 00:41:17,480 --> 00:41:22,000 it does not mean Farm as you might think 38758 00:41:19,440 --> 00:41:25,232 it um it actually means home or family 38759 00:41:22,000 --> 00:41:26,480 so in the Chinese kind of structure and 38760 00:41:25,232 --> 00:41:29,320 outlook on The View it's like where you 38761 00:41:26,480 --> 00:41:33,600 keep your pigs is actually where your 38762 00:41:29,320 --> 00:41:35,232 home is and what your farm is um so it 38763 00:41:33,600 --> 00:41:37,040 gets kind of weird in all kinds of ways 38764 00:41:35,232 --> 00:41:42,280 so one of my favorite radicals so this 38765 00:41:37,040 --> 00:41:44,640 character means woman uh KN 38766 00:41:42,280 --> 00:41:46,912 and as I was learning Chinese you kind 38767 00:41:44,640 --> 00:41:49,600 of notice like how it shows up so so 38768 00:41:46,912 --> 00:41:52,552 this is a woman underneath a roof and 38769 00:41:49,600 --> 00:41:55,720 you're like oh it means mom or wife or 38770 00:41:52,552 --> 00:41:59,832 or like whatever like home um it does 38771 00:41:55,720 --> 00:42:02,720 not it means peace because things are at 38772 00:41:59,832 --> 00:42:04,960 home when the woman is or things are at 38773 00:42:02,720 --> 00:42:06,280 peace when the woman is at home 38774 00:42:04,960 --> 00:42:09,040 underneath a roof which I always thought 38775 00:42:06,280 --> 00:42:13,232 was a little bit odd um then there is 38776 00:42:09,040 --> 00:42:15,512 also uh woman plus child so you're like 38777 00:42:13,232 --> 00:42:16,512 oh and actually specifically Boy Child 38778 00:42:15,512 --> 00:42:18,192 the connotation there is a little 38779 00:42:16,512 --> 00:42:21,480 unclear so you're like a woman plus 38780 00:42:18,192 --> 00:42:23,720 child family mom you know whatever um it 38781 00:42:21,480 --> 00:42:25,512 is not it means good so the standard for 38782 00:42:23,720 --> 00:42:27,800 goodness in ancient China was a woman 38783 00:42:25,512 --> 00:42:30,872 who had a male child child so that kind 38784 00:42:27,800 --> 00:42:32,600 of kind of just like kind of irked me um 38785 00:42:30,872 --> 00:42:34,680 growing up and then you know three women 38786 00:42:32,600 --> 00:42:36,080 together means evil which is like very 38787 00:42:34,680 --> 00:42:40,192 like 38788 00:42:36,080 --> 00:42:43,120 MCB this character means greed this 38789 00:42:40,192 --> 00:42:45,512 character means slave this marriage 38790 00:42:43,120 --> 00:42:47,872 let's see I think this one is jealousy 38791 00:42:45,512 --> 00:42:51,192 and this means means adultery or 38792 00:42:47,872 --> 00:42:53,720 betrayal so like definitely not loving 38793 00:42:51,192 --> 00:42:56,192 the way women were P you know portrayed 38794 00:42:53,720 --> 00:42:57,512 um on the emoji keyboard so uh in case 38795 00:42:56,192 --> 00:42:58,912 you're wondering we just came out with a 38796 00:42:57,512 --> 00:43:01,832 kids book called had emoji that kind of 38797 00:42:58,912 --> 00:43:03,800 Compares emoji and uh Chinese and I 38798 00:43:01,832 --> 00:43:05,480 think I I think they sent a bunch of 38799 00:43:03,800 --> 00:43:08,912 books so that you guys can do some kind 38800 00:43:05,480 --> 00:43:10,720 of contest some like later on with cs50 38801 00:43:08,912 --> 00:43:13,280 um so but the mixing and matching is 38802 00:43:10,720 --> 00:43:16,440 really interesting right for example the 38803 00:43:13,280 --> 00:43:18,400 skin tones are actually the same yellow 38804 00:43:16,440 --> 00:43:20,512 character plus a layer of skin tone on 38805 00:43:18,400 --> 00:43:22,720 top of this so I kind of took my lessons 38806 00:43:20,512 --> 00:43:23,832 from Chinese in terms of seeing how 38807 00:43:22,720 --> 00:43:25,320 things can be binded so there's 38808 00:43:23,832 --> 00:43:27,760 something you should know about which is 38809 00:43:25,320 --> 00:43:30,000 zge this is also an invisible emoji 38810 00:43:27,760 --> 00:43:31,480 character it stands for zero with Joiner 38811 00:43:30,000 --> 00:43:34,320 and it was actually originally created 38812 00:43:31,480 --> 00:43:36,160 mostly for I think Arabic where you 38813 00:43:34,320 --> 00:43:37,512 would you would basically kind of force 38814 00:43:36,160 --> 00:43:39,320 something to have like be in the 38815 00:43:37,512 --> 00:43:41,400 beginning of of a word or an end of the 38816 00:43:39,320 --> 00:43:43,440 word by kind of having this like 38817 00:43:41,400 --> 00:43:45,600 invisible character so the rainbow flag 38818 00:43:43,440 --> 00:43:47,120 for example is actually a rainbow plus 38819 00:43:45,600 --> 00:43:50,080 the white flag 38820 00:43:47,120 --> 00:43:52,360 um and we we could have all kinds of fun 38821 00:43:50,080 --> 00:43:54,480 combinations if you look at polar bear 38822 00:43:52,360 --> 00:43:56,480 it actually is if you have an older 38823 00:43:54,480 --> 00:43:59,000 device or it breaks apart it is bear 38824 00:43:56,480 --> 00:44:01,832 plus snow which is really cute it was 38825 00:43:59,000 --> 00:44:04,080 originally I had bear plus white and 38826 00:44:01,832 --> 00:44:06,800 then we decided that bear plus snow made 38827 00:44:04,080 --> 00:44:09,480 a lot more sense so uh another one this 38828 00:44:06,800 --> 00:44:11,040 is new I I think if you guys should have 38829 00:44:09,480 --> 00:44:13,120 it if you've updated your phones in the 38830 00:44:11,040 --> 00:44:15,080 last year or so so mending heart is 38831 00:44:13,120 --> 00:44:17,440 heart plus 38832 00:44:15,080 --> 00:44:20,000 Band-Aid um um what is this one oh this 38833 00:44:17,440 --> 00:44:22,192 is interesting um there was a 38834 00:44:20,000 --> 00:44:23,400 breastfeeding woman for a long time and 38835 00:44:22,192 --> 00:44:24,912 people felt like there was not gender 38836 00:44:23,400 --> 00:44:26,640 par was actually really interesting all 38837 00:44:24,912 --> 00:44:28,960 the people who wrote it and were like I 38838 00:44:26,640 --> 00:44:30,552 want to I want to be a dad showing that 38839 00:44:28,960 --> 00:44:32,760 I'm holding my baby why is there only a 38840 00:44:30,552 --> 00:44:36,080 woman holding my baby so so we kind of 38841 00:44:32,760 --> 00:44:39,680 created a whole set so this is man plus 38842 00:44:36,080 --> 00:44:41,800 bottle Tada um and so this is and all of 38843 00:44:39,680 --> 00:44:43,720 these occupations are actually often 38844 00:44:41,800 --> 00:44:47,000 times a woman plus like you know the 38845 00:44:43,720 --> 00:44:48,192 fried egg or like um a school or a 38846 00:44:47,000 --> 00:44:50,320 tractor and that's how you got the 38847 00:44:48,192 --> 00:44:53,120 occupations if you if you if you send an 38848 00:44:50,320 --> 00:44:55,120 emoji over into an older system 38849 00:44:53,120 --> 00:44:56,640 sometimes you'll it'll break apart so 38850 00:44:55,120 --> 00:44:58,640 one of my kind of favorite kind of 38851 00:44:56,640 --> 00:45:00,320 contributions in this in this world is 38852 00:44:58,640 --> 00:45:03,120 interracial couple Emoji which we did 38853 00:45:00,320 --> 00:45:04,512 with um Tinder which is super fun 38854 00:45:03,120 --> 00:45:06,912 because then you could have you know 38855 00:45:04,512 --> 00:45:08,680 different couples that are and and like 38856 00:45:06,912 --> 00:45:10,000 so many combin this is like this is a 38857 00:45:08,680 --> 00:45:12,440 fun if you guys ever have to do a 38858 00:45:10,000 --> 00:45:14,680 combinatoric test um this is really fun 38859 00:45:12,440 --> 00:45:18,080 because you have two genders plus a 38860 00:45:14,680 --> 00:45:20,192 third like gender plus two people plus 38861 00:45:18,080 --> 00:45:22,832 five skin tones plus yellow like how 38862 00:45:20,192 --> 00:45:25,440 many Emoji couples can you come come up 38863 00:45:22,832 --> 00:45:27,720 with when you introduce this Factor so 38864 00:45:25,440 --> 00:45:28,872 this is um and underneath it it's just a 38865 00:45:27,720 --> 00:45:30,600 wige sequence it's like two people 38866 00:45:28,872 --> 00:45:32,440 standing together that are like glued 38867 00:45:30,600 --> 00:45:34,232 together now this actually gets 38868 00:45:32,440 --> 00:45:36,360 interesting from a cs-50 perspective 38869 00:45:34,232 --> 00:45:38,912 because in many cases even though you 38870 00:45:36,360 --> 00:45:41,040 only see one character underneath the 38871 00:45:38,912 --> 00:45:42,832 hood depending on how your system works 38872 00:45:41,040 --> 00:45:44,440 they're counting each one of these as an 38873 00:45:42,832 --> 00:45:46,080 individual character so your string 38874 00:45:44,440 --> 00:45:47,960 length actually might be five and 38875 00:45:46,080 --> 00:45:49,000 instead of one and this this kind of 38876 00:45:47,960 --> 00:45:51,280 became a problem with things like 38877 00:45:49,000 --> 00:45:53,872 Twitter where things had a hard skin um 38878 00:45:51,280 --> 00:45:54,912 length so gender inclusivity um is 38879 00:45:53,872 --> 00:45:58,120 actually one of the things that we've 38880 00:45:54,912 --> 00:46:00,160 been dealing most with um in the last 38881 00:45:58,120 --> 00:46:01,872 couple years so it's kind of interesting 38882 00:46:00,160 --> 00:46:03,512 if you think about both what a pictorial 38883 00:46:01,872 --> 00:46:06,080 language looks like versus the 38884 00:46:03,512 --> 00:46:08,120 abstractness of a of a spoken language 38885 00:46:06,080 --> 00:46:08,912 so because you know we had boy and we 38886 00:46:08,120 --> 00:46:11,000 had 38887 00:46:08,912 --> 00:46:13,160 girl but there was no way to say a 38888 00:46:11,000 --> 00:46:14,440 generic child right like if you were on 38889 00:46:13,160 --> 00:46:16,912 you wanted to say child you had to pick 38890 00:46:14,440 --> 00:46:19,640 a boy or a girl but not a way to say 38891 00:46:16,912 --> 00:46:21,680 like just some little person and that's 38892 00:46:19,640 --> 00:46:24,160 really key cuz in English at least there 38893 00:46:21,680 --> 00:46:27,872 is no gender implied by 38894 00:46:24,160 --> 00:46:29,960 child um so how do we mimic that and it 38895 00:46:27,872 --> 00:46:32,912 also is key for something like doctor 38896 00:46:29,960 --> 00:46:35,080 right doctor and teacher those are those 38897 00:46:32,912 --> 00:46:36,960 have don't have gender implied but when 38898 00:46:35,080 --> 00:46:39,680 we have them on on the Emoji Keyboard 38899 00:46:36,960 --> 00:46:43,280 you to pick a male teacher or um you 38900 00:46:39,680 --> 00:46:45,760 know a a a female doctor or whatnot so 38901 00:46:43,280 --> 00:46:47,800 there was actually uh a guy at Adobe who 38902 00:46:45,760 --> 00:46:50,720 can considers himself non-gender binary 38903 00:46:47,800 --> 00:46:55,192 also The Man Behind the orange um the 38904 00:46:50,720 --> 00:46:58,320 orange heart he fought and got basically 38905 00:46:55,192 --> 00:47:01,512 uh the first three non-gender uh 38906 00:46:58,320 --> 00:47:03,640 non-gender binary emojis so child adult 38907 00:47:01,512 --> 00:47:05,800 and old older adults so those are 38908 00:47:03,640 --> 00:47:07,280 creative and then we started having to 38909 00:47:05,800 --> 00:47:08,680 propagate them through actually all the 38910 00:47:07,280 --> 00:47:11,400 occupations so these are the gender 38911 00:47:08,680 --> 00:47:13,512 neutral versions of many of those um but 38912 00:47:11,400 --> 00:47:15,400 then uh we got into this whole thing 38913 00:47:13,512 --> 00:47:17,160 where every emoji that had a gender 38914 00:47:15,400 --> 00:47:19,160 originally had to be mirrored so 38915 00:47:17,160 --> 00:47:20,640 originally we had bearded man and then 38916 00:47:19,160 --> 00:47:22,512 we're like okay well we actually have to 38917 00:47:20,640 --> 00:47:24,760 get bearded woman so that is on your 38918 00:47:22,512 --> 00:47:28,192 keyboard there is pregnant woman there 38919 00:47:24,760 --> 00:47:31,232 is now pregnant man which is interesting 38920 00:47:28,192 --> 00:47:34,192 um there is you know woman in a bridal 38921 00:47:31,232 --> 00:47:35,512 gown there's now man in a bridal gown um 38922 00:47:34,192 --> 00:47:38,120 and then there were ones that actually 38923 00:47:35,512 --> 00:47:40,232 had to be created that were neither man 38924 00:47:38,120 --> 00:47:41,760 or woman so this is a mer person so 38925 00:47:40,232 --> 00:47:42,800 there was merman there was mermaid and 38926 00:47:41,760 --> 00:47:43,960 there was M person it was really 38927 00:47:42,800 --> 00:47:46,512 interesting to figure out like how do 38928 00:47:43,960 --> 00:47:47,640 you draw a gender neutral M person like 38929 00:47:46,512 --> 00:47:49,280 a bunch of them in the beginning 38930 00:47:47,640 --> 00:47:51,680 actually had the arms crossed around 38931 00:47:49,280 --> 00:47:53,480 sort of the chest um Monarch so there 38932 00:47:51,680 --> 00:47:55,760 was prince and princess and now there is 38933 00:47:53,480 --> 00:47:57,280 monarch and one of my favorite actually 38934 00:47:55,760 --> 00:47:59,720 is there was Santa Claus and there was 38935 00:47:57,280 --> 00:48:01,480 Mrs Claus and now there's MX Claus like 38936 00:47:59,720 --> 00:48:03,400 the name of this character literally in 38937 00:48:01,480 --> 00:48:05,000 Unicode is MX claw so I feel like it's 38938 00:48:03,400 --> 00:48:09,360 sort of like a very official enshrining 38939 00:48:05,000 --> 00:48:10,832 of gender non biner in like the world um 38940 00:48:09,360 --> 00:48:12,912 not everyone loved it New York Post did 38941 00:48:10,832 --> 00:48:14,800 not love this that they like you know 38942 00:48:12,912 --> 00:48:18,280 we're we're cing into like Emoji woke 38943 00:48:14,800 --> 00:48:19,640 Wars um so some Emoji stats for you this 38944 00:48:18,280 --> 00:48:22,400 is very fascinating this is sort of like 38945 00:48:19,640 --> 00:48:24,160 the Gen the General Distribution by far 38946 00:48:22,400 --> 00:48:26,440 the single emoji that used more than 38947 00:48:24,160 --> 00:48:29,480 anything else is the face with tears of 38948 00:48:26,440 --> 00:48:31,600 joy about 10% of all Emoji scent is that 38949 00:48:29,480 --> 00:48:33,832 one character and then number two is 38950 00:48:31,600 --> 00:48:36,400 heart red heart and then it kind of goes 38951 00:48:33,832 --> 00:48:39,280 down so um there's a frequency of emoji 38952 00:48:36,400 --> 00:48:43,440 use these this is sort of done by um 38953 00:48:39,280 --> 00:48:46,000 order magnitude so one is half of two 38954 00:48:43,440 --> 00:48:47,480 two is half of one so it's really 38955 00:48:46,000 --> 00:48:49,680 interesting it's a very very steep drop 38956 00:48:47,480 --> 00:48:52,360 off after the first couple um in case 38957 00:48:49,680 --> 00:48:54,872 you ever want to go onto the the Emoji 38958 00:48:52,360 --> 00:48:57,280 kind of Unicode website you can you too 38959 00:48:54,872 --> 00:48:58,552 can see all the frequency things so I 38960 00:48:57,280 --> 00:49:01,040 think it's really funny so basically 38961 00:48:58,552 --> 00:49:03,800 it's green going this way it increased 38962 00:49:01,040 --> 00:49:06,160 in usage between 2019 and uh 2021 and 38963 00:49:03,800 --> 00:49:08,400 it's red going this way it drop and so 38964 00:49:06,160 --> 00:49:10,680 pleading face which is a relatively new 38965 00:49:08,400 --> 00:49:14,320 emoe you just sort of shut up on the 38966 00:49:10,680 --> 00:49:16,552 charts um and whereas actually like 38967 00:49:14,320 --> 00:49:19,000 smiling face with heart eyes 38968 00:49:16,552 --> 00:49:21,512 like kind of kind of slipped which is 38969 00:49:19,000 --> 00:49:23,512 interesting so we just closed our Emoji 38970 00:49:21,512 --> 00:49:25,360 proposal round for 2022 these were sort 38971 00:49:23,512 --> 00:49:27,120 of the breakdowns people love submitting 38972 00:49:25,360 --> 00:49:29,640 Smileys and food and beverages animals 38973 00:49:27,120 --> 00:49:31,552 and nature da da da I don't I mean these 38974 00:49:29,640 --> 00:49:34,080 are very googly colors um so what is the 38975 00:49:31,552 --> 00:49:35,720 future of emoji I will um I will tell 38976 00:49:34,080 --> 00:49:37,760 you because we just had a meeting two 38977 00:49:35,720 --> 00:49:39,912 weeks ago so I can now publicly talk 38978 00:49:37,760 --> 00:49:41,440 about it so historically there was this 38979 00:49:39,912 --> 00:49:45,512 whole idea like Unico doesn't want to be 38980 00:49:41,440 --> 00:49:47,400 in the world of like encoding glyphs for 38981 00:49:45,512 --> 00:49:48,720 like devices everywhere like there was 38982 00:49:47,400 --> 00:49:50,000 very controversial when it started doing 38983 00:49:48,720 --> 00:49:52,320 that because mostly what Unicode used to 38984 00:49:50,000 --> 00:49:54,280 do was you take an existing language 38985 00:49:52,320 --> 00:49:56,120 could be dead you know and then it would 38986 00:49:54,280 --> 00:49:58,360 just take it and digitize it right it 38987 00:49:56,120 --> 00:50:01,600 took languages that existed and just 38988 00:49:58,360 --> 00:50:03,512 digitize them and uh then when it kind 38989 00:50:01,600 --> 00:50:06,912 of wandered into Emoji World Suddenly 38990 00:50:03,512 --> 00:50:10,360 It's like deciding what deserves to be 38991 00:50:06,912 --> 00:50:12,912 like an emoji decides to be digitized so 38992 00:50:10,360 --> 00:50:14,912 um trying to get kind of get out of it 38993 00:50:12,912 --> 00:50:16,440 and they have proposals over time where 38994 00:50:14,912 --> 00:50:18,512 it's like oh maybe we should like come 38995 00:50:16,440 --> 00:50:21,232 up with a a way to just send pictures 38996 00:50:18,512 --> 00:50:24,600 back and forth where you you it's a 38997 00:50:21,232 --> 00:50:26,640 fixed picture and you like use a hash so 38998 00:50:24,600 --> 00:50:28,640 that you know like we would look at the 38999 00:50:26,640 --> 00:50:30,832 picture and then like go do a lookup 39000 00:50:28,640 --> 00:50:32,080 somewhere like that did not go over well 39001 00:50:30,832 --> 00:50:34,552 then there was actually a really 39002 00:50:32,080 --> 00:50:35,800 interesting proposal I kind of like uh 39003 00:50:34,552 --> 00:50:37,760 didn't go over well which is using 39004 00:50:35,800 --> 00:50:40,000 something called the qid which is in 39005 00:50:37,760 --> 00:50:43,640 Wiki in the Wikipedia world so in 39006 00:50:40,000 --> 00:50:45,680 Wikipedia World items all have uh 39007 00:50:43,640 --> 00:50:49,040 numbers across the different language 39008 00:50:45,680 --> 00:50:51,912 Wikipedia so Obama human Earth they will 39009 00:50:49,040 --> 00:50:53,600 have an ID number so that the page in 39010 00:50:51,912 --> 00:50:55,040 English and the page in German the page 39011 00:50:53,600 --> 00:50:56,832 in Chinese all know that they're 39012 00:50:55,040 --> 00:50:58,400 pointing to the same thing so the 39013 00:50:56,832 --> 00:51:00,120 question so one idea came up like why 39014 00:50:58,400 --> 00:51:02,760 don't we use the numbering system so we 39015 00:51:00,120 --> 00:51:04,872 can use like Eiffel Tower in you know 39016 00:51:02,760 --> 00:51:06,600 see the number and then like oh people 39017 00:51:04,872 --> 00:51:08,400 know like oh you're trying to say Eiffel 39018 00:51:06,600 --> 00:51:09,912 Tower that did not go over well so those 39019 00:51:08,400 --> 00:51:13,320 are both both of those proposals seem 39020 00:51:09,912 --> 00:51:14,640 dead as of yet um and it's too bad cuz 39021 00:51:13,320 --> 00:51:16,680 and you'll see kind of what's happening 39022 00:51:14,640 --> 00:51:19,000 okay so what's coming in 22 so these are 39023 00:51:16,680 --> 00:51:20,280 the Emoji that I actually sort of 39024 00:51:19,000 --> 00:51:21,680 thought they would be on your phones by 39025 00:51:20,280 --> 00:51:23,600 now but cuz we're in mid November and 39026 00:51:21,680 --> 00:51:26,040 they usually update early November so 39027 00:51:23,600 --> 00:51:29,280 three more Hearts people love hearts 39028 00:51:26,040 --> 00:51:31,440 like uh wing blackbird Goose Birds also 39029 00:51:29,280 --> 00:51:33,120 purple flowers jellyfish moose face 39030 00:51:31,440 --> 00:51:36,552 donkey donkey was a little bit late for 39031 00:51:33,120 --> 00:51:40,960 the kind of Elections um Ginger peep pod 39032 00:51:36,552 --> 00:51:42,552 Wireless cond shaking face um folding 39033 00:51:40,960 --> 00:51:44,160 hand pan that one was interesting cuz 39034 00:51:42,552 --> 00:51:46,280 when people first proposed it they 39035 00:51:44,160 --> 00:51:49,360 proposed it as like an electric fan and 39036 00:51:46,280 --> 00:51:50,640 that didn't like who knows what electric 39037 00:51:49,360 --> 00:51:51,872 fans will look like in 100 years because 39038 00:51:50,640 --> 00:51:54,160 the thing is Once An Emoji always an 39039 00:51:51,872 --> 00:51:55,680 emoji they never retire so they're 39040 00:51:54,160 --> 00:51:58,760 always looking for things that have a 39041 00:51:55,680 --> 00:52:00,440 long visual longevity floppy disc did 39042 00:51:58,760 --> 00:52:02,160 not actually do that so there's always 39043 00:52:00,440 --> 00:52:04,120 like we don't want another floppy disc 39044 00:52:02,160 --> 00:52:06,960 um and then hairpick is interesting so 39045 00:52:04,120 --> 00:52:10,872 there was a whole debate about how to 39046 00:52:06,960 --> 00:52:12,232 convey um like afro African hair like 39047 00:52:10,872 --> 00:52:13,720 the the curly hair that they introduced 39048 00:52:12,232 --> 00:52:15,680 a couple years ago was supposed to do 39049 00:52:13,720 --> 00:52:17,552 that and most of the vendors actually 39050 00:52:15,680 --> 00:52:19,512 have it in a sort of Afro way except for 39051 00:52:17,552 --> 00:52:21,280 Apple so there's a lot of complaints but 39052 00:52:19,512 --> 00:52:23,600 um hairpick was sort of a an interesting 39053 00:52:21,280 --> 00:52:25,360 way it means both comb but also has sort 39054 00:52:23,600 --> 00:52:27,080 of an interesting historic connotation 39055 00:52:25,360 --> 00:52:29,512 and it's been around for about 2,000 39056 00:52:27,080 --> 00:52:31,960 years uh a couple music things maracus 39057 00:52:29,512 --> 00:52:34,120 and flute uh Beyond 2022 one of the 39058 00:52:31,960 --> 00:52:36,832 things that's going to die oops can I go 39059 00:52:34,120 --> 00:52:41,320 back no I can't oh well uh we're going 39060 00:52:36,832 --> 00:52:42,720 to retire the the family Emoji um they 39061 00:52:41,320 --> 00:52:44,800 didn't go over so well there were so 39062 00:52:42,720 --> 00:52:48,320 many of them combinatorically if you had 39063 00:52:44,800 --> 00:52:50,320 all the everything in in uh you know all 39064 00:52:48,320 --> 00:52:52,320 the race all the races all because you 39065 00:52:50,320 --> 00:52:53,400 wanted to have skin tones because you 39066 00:52:52,320 --> 00:52:56,000 didn't want to imply that families can 39067 00:52:53,400 --> 00:52:57,872 only be one race it was such an ordeal 39068 00:52:56,000 --> 00:52:59,960 uh essentially we're all like no one 39069 00:52:57,872 --> 00:53:02,160 uses them and there's so many and it's 39070 00:52:59,960 --> 00:53:03,720 like the fonts like in terms of the load 39071 00:53:02,160 --> 00:53:05,440 is like too large so they're just going 39072 00:53:03,720 --> 00:53:08,280 to make them all into like basically 39073 00:53:05,440 --> 00:53:10,232 little like bathroom symbol Type U folks 39074 00:53:08,280 --> 00:53:11,512 um so I think that is those will 39075 00:53:10,232 --> 00:53:13,600 disappear what what's actually really 39076 00:53:11,512 --> 00:53:15,040 interesting about the family Emoji is 39077 00:53:13,600 --> 00:53:17,192 they had gay when they introduced the 39078 00:53:15,040 --> 00:53:19,760 family they had gay family emoji and the 39079 00:53:17,192 --> 00:53:21,080 Russian government went berserk and 39080 00:53:19,760 --> 00:53:22,872 actually you can Google this in 2015 39081 00:53:21,080 --> 00:53:24,680 you'll see a bunch of articles about 39082 00:53:22,872 --> 00:53:26,480 like the Russian government considering 39083 00:53:24,680 --> 00:53:27,760 it homosexual propaganda to Youth and 39084 00:53:26,480 --> 00:53:29,120 there was a big debate about whether or 39085 00:53:27,760 --> 00:53:30,600 not they were going to ban Apple devices 39086 00:53:29,120 --> 00:53:32,760 and what so you can see a lot of the 39087 00:53:30,600 --> 00:53:34,872 media coverage from that time but I 39088 00:53:32,760 --> 00:53:36,400 thought it was really interesting how 39089 00:53:34,872 --> 00:53:39,912 upset a national government can get 39090 00:53:36,400 --> 00:53:41,400 about little pictures on your phone um 39091 00:53:39,912 --> 00:53:43,400 another thing that's going to that's on 39092 00:53:41,400 --> 00:53:46,232 the agenda as of you know a couple of 39093 00:53:43,400 --> 00:53:48,120 weeks ago is directionality in terms of 39094 00:53:46,232 --> 00:53:49,680 emoji so as you know most emojis kind of 39095 00:53:48,120 --> 00:53:52,280 just flip one way or another and the 39096 00:53:49,680 --> 00:53:54,480 reason why it matters is because not all 39097 00:53:52,280 --> 00:53:57,640 languages run in the same direction so 39098 00:53:54,480 --> 00:53:59,480 for example Arabic so we are used to 39099 00:53:57,640 --> 00:54:02,400 left to right but a lot of languages go 39100 00:53:59,480 --> 00:54:04,000 right to left um so and this kind of It 39101 00:54:02,400 --> 00:54:06,440 kind of changes the meaning of emoji for 39102 00:54:04,000 --> 00:54:07,872 example right to left I send this a lot 39103 00:54:06,440 --> 00:54:12,800 to my friends when I'm going flying from 39104 00:54:07,872 --> 00:54:14,832 the Bay Area to New York um if you do it 39105 00:54:12,800 --> 00:54:16,960 from left to right however that is what 39106 00:54:14,832 --> 00:54:18,232 it looks like so it looks like um you 39107 00:54:16,960 --> 00:54:20,480 know you're in NE Bay but the plane is 39108 00:54:18,232 --> 00:54:22,960 still going that you know kind of up and 39109 00:54:20,480 --> 00:54:24,480 to the right and then now it looks like 39110 00:54:22,960 --> 00:54:26,760 you're going from New York to the Bay 39111 00:54:24,480 --> 00:54:28,360 Area the other place is like oh it's 39112 00:54:26,760 --> 00:54:31,320 it's a girl and she's running really 39113 00:54:28,360 --> 00:54:33,720 fast right uh that is left to right to 39114 00:54:31,320 --> 00:54:34,872 left in our world wait left to right 39115 00:54:33,720 --> 00:54:36,440 sorry sorry that was that's that's 39116 00:54:34,872 --> 00:54:39,400 supposed to be left to right and in here 39117 00:54:36,440 --> 00:54:40,600 it would be she's like behind like 39118 00:54:39,400 --> 00:54:42,760 pollution or something like that so 39119 00:54:40,600 --> 00:54:44,280 sorry about so so an example this is 39120 00:54:42,760 --> 00:54:46,160 actually in The Proposal like in one 39121 00:54:44,280 --> 00:54:48,232 case if it's left or right you're 39122 00:54:46,160 --> 00:54:50,280 running away from a line of cars and the 39123 00:54:48,232 --> 00:54:53,232 other one it's a warning to not run 39124 00:54:50,280 --> 00:54:54,680 behind car fumes so they are trying to 39125 00:54:53,232 --> 00:54:57,360 figure out like how do we mirror a bunch 39126 00:54:54,680 --> 00:55:00,440 of the Emoji um but the LA the main 39127 00:54:57,360 --> 00:55:01,600 thing that I think sort of I don't know 39128 00:55:00,440 --> 00:55:03,280 when it's going to happen I'm really 39129 00:55:01,600 --> 00:55:05,000 hopeful is going to happen is trying to 39130 00:55:03,280 --> 00:55:08,480 come up with a system that supports 39131 00:55:05,000 --> 00:55:11,960 little stickers in line that don't need 39132 00:55:08,480 --> 00:55:14,040 Unicode so this is like slack or on um 39133 00:55:11,960 --> 00:55:15,872 twitch you can embed little pictures on 39134 00:55:14,040 --> 00:55:18,512 in line and all all all the vendors have 39135 00:55:15,872 --> 00:55:19,760 to get together and agree to come up 39136 00:55:18,512 --> 00:55:21,800 with a standard way to do that they have 39137 00:55:19,760 --> 00:55:23,552 not yet come up with that but that is 39138 00:55:21,800 --> 00:55:26,440 sort of one of the ways that unic coded 39139 00:55:23,552 --> 00:55:28,320 like want it wants to back away from 39140 00:55:26,440 --> 00:55:31,080 actually being like a global regulator 39141 00:55:28,320 --> 00:55:34,360 for like little colorful glyphs and so 39142 00:55:31,080 --> 00:55:36,960 if you ever need to reach me um on in my 39143 00:55:34,360 --> 00:55:39,160 emoji world you can find me um Jenny 39144 00:55:36,960 --> 00:55:42,640 EmojiNation org there will 39145 00:55:39,160 --> 00:55:44,600 be um the it will be actually a while I 39146 00:55:42,640 --> 00:55:46,552 think before we see a next generation of 39147 00:55:44,600 --> 00:55:47,832 emoji showing up in it used to be like 39148 00:55:46,552 --> 00:55:49,440 every year they would get new code 39149 00:55:47,832 --> 00:55:51,440 points it might be a little bit less 39150 00:55:49,440 --> 00:55:54,160 than every year now as they work on 39151 00:55:51,440 --> 00:55:56,160 things like directionality over time so 39152 00:55:54,160 --> 00:55:58,552 that uh if anyone has questions you can 39153 00:55:56,160 --> 00:56:00,480 ask questions you can find me afterwards 39154 00:55:58,552 --> 00:56:03,120 I think I've I feel like there's 39155 00:56:00,480 --> 00:56:06,872 supposed to be some some hubub right now 39156 00:56:03,120 --> 00:56:09,232 about maybe maybe 39157 00:56:06,872 --> 00:56:11,000 um microphones but maybe not but maybe 39158 00:56:09,232 --> 00:56:12,680 I'm just done and if if there are 39159 00:56:11,000 --> 00:56:14,680 questions or if David is around I'm 39160 00:56:12,680 --> 00:56:17,440 happy you know he can I'm I'm happy to 39161 00:56:14,680 --> 00:56:20,800 to answer any questions that folks have 39162 00:56:17,440 --> 00:56:22,760 yes hi yes I was wondering what were 39163 00:56:20,800 --> 00:56:25,440 your thoughts on the 39164 00:56:22,760 --> 00:56:26,512 emo so the question is what are my 39165 00:56:25,440 --> 00:56:29,552 thoughts on the Emoji Movie you're 39166 00:56:26,512 --> 00:56:32,232 talking about the Sony animated one yes 39167 00:56:29,552 --> 00:56:35,400 okay my thought on that is it is better 39168 00:56:32,232 --> 00:56:37,000 than a 6% rating on um Rotten Tomatoes 39169 00:56:35,400 --> 00:56:39,912 would lead you to believe so that's my 39170 00:56:37,000 --> 00:56:42,600 one thought and my my next thought is 39171 00:56:39,912 --> 00:56:45,160 that um that was a rush job from an 39172 00:56:42,600 --> 00:56:48,720 animation perspective that was that was 39173 00:56:45,160 --> 00:56:51,160 was about 18 months of whereas a typical 39174 00:56:48,720 --> 00:56:52,640 animated movie takes four years so in my 39175 00:56:51,160 --> 00:56:55,040 spare time I also produce movies and 39176 00:56:52,640 --> 00:56:56,400 documentaries so one thing that is key 39177 00:56:55,040 --> 00:57:00,320 to know 39178 00:56:56,400 --> 00:57:01,832 about about movies and animated movies 39179 00:57:00,320 --> 00:57:04,960 and this is very important they take a 39180 00:57:01,832 --> 00:57:07,280 very long time but you can always fix it 39181 00:57:04,960 --> 00:57:08,760 because you haven't shot anything and a 39182 00:57:07,280 --> 00:57:10,872 very good example of that is I assume 39183 00:57:08,760 --> 00:57:12,440 you guys have seen Frozen if you haven't 39184 00:57:10,872 --> 00:57:14,800 seen you seen of the age you would have 39185 00:57:12,440 --> 00:57:17,760 seen Frozen um I do not understand like 39186 00:57:14,800 --> 00:57:21,160 how huge and a phenomenon or why it was 39187 00:57:17,760 --> 00:57:24,120 such a huge phenomenon but 39188 00:57:21,160 --> 00:57:26,680 um they actually did a original cut of 39189 00:57:24,120 --> 00:57:28,600 Frozen and it did so I don't know you 39190 00:57:26,680 --> 00:57:29,960 guys know the the Eye the the sort of 39191 00:57:28,600 --> 00:57:32,640 Snow Queen thing but she's like super 39192 00:57:29,960 --> 00:57:34,280 dark and like not fun and like kind of 39193 00:57:32,640 --> 00:57:36,192 evil and like not someone you want to 39194 00:57:34,280 --> 00:57:38,480 like get behind as a character so they 39195 00:57:36,192 --> 00:57:41,480 actually did sort of a rough cut of that 39196 00:57:38,480 --> 00:57:43,000 of Frozen and they came out of that with 39197 00:57:41,480 --> 00:57:46,360 um it's just storyboarding and they're 39198 00:57:43,000 --> 00:57:48,832 like that is not good and they killed it 39199 00:57:46,360 --> 00:57:50,872 so they were like we can't go with this 39200 00:57:48,832 --> 00:57:53,552 and then started from scratch more or 39201 00:57:50,872 --> 00:57:55,720 less again starting with the song um Let 39202 00:57:53,552 --> 00:57:58,000 It Go which is actually written by a kid 39203 00:57:55,720 --> 00:58:00,360 from my elementary school Bobby Lopez or 39204 00:57:58,000 --> 00:58:02,040 co-written by Bobby Lopez um I also 39205 00:58:00,360 --> 00:58:04,440 actually fun fact I also went I would 39206 00:58:02,040 --> 00:58:06,000 took the school bus with uh Lyn mmel 39207 00:58:04,440 --> 00:58:07,760 Miranda so I was a fourth grader when he 39208 00:58:06,000 --> 00:58:09,440 was like a kindergartener so we had a 39209 00:58:07,760 --> 00:58:11,600 very musical Elementary School in New 39210 00:58:09,440 --> 00:58:13,720 York City but the thing is they could 39211 00:58:11,600 --> 00:58:16,000 fix it because they had enough time and 39212 00:58:13,720 --> 00:58:18,760 have enough money not like movies where 39213 00:58:16,000 --> 00:58:20,360 you shoot humans much harder to fix so 39214 00:58:18,760 --> 00:58:21,832 you have the footage that you have and 39215 00:58:20,360 --> 00:58:23,800 you can do little pickups but you can't 39216 00:58:21,832 --> 00:58:26,480 fix it so essentially what happened in 39217 00:58:23,800 --> 00:58:28,400 that case I think um it's 18 months and 39218 00:58:26,480 --> 00:58:30,280 it could have gotten better and a lot of 39219 00:58:28,400 --> 00:58:32,480 the movies that you see with Pixar like 39220 00:58:30,280 --> 00:58:35,232 it's very it's actually sort emotionally 39221 00:58:32,480 --> 00:58:36,912 similar to the movie called inside out 39222 00:58:35,232 --> 00:58:38,960 and uh but inside they just have more 39223 00:58:36,912 --> 00:58:40,232 time and so it's better so as opposed to 39224 00:58:38,960 --> 00:58:44,160 18 months which is not long enough to 39225 00:58:40,232 --> 00:58:46,760 make a animated movie good but uh the 39226 00:58:44,160 --> 00:58:48,640 other fun thing is is it was the it it 39227 00:58:46,760 --> 00:58:52,192 was so weird cuz they sold sponsorships 39228 00:58:48,640 --> 00:58:55,040 it was like like oh my God here comes 39229 00:58:52,192 --> 00:58:58,040 the Bots and the malware let's go into 39230 00:58:55,040 --> 00:59:00,120 Dropbox and protect ourselves and was so 39231 00:58:58,040 --> 00:59:03,160 I think that it got a lot of like bad 39232 00:59:00,120 --> 00:59:04,552 kind of um kind of uh vibes from the 39233 00:59:03,160 --> 00:59:06,600 from the press for doing things like 39234 00:59:04,552 --> 00:59:09,232 that but from a kids perspective it's 39235 00:59:06,600 --> 00:59:12,280 it's fine I think if um I don't know 39236 00:59:09,232 --> 00:59:13,640 that I would like put into my top 10 of 39237 00:59:12,280 --> 00:59:16,000 animated pictures but it's better than 39238 00:59:13,640 --> 00:59:17,360 6% on Rotten Tomatoes 39239 00:59:16,000 --> 00:59:20,080 and then actually if you guys ever care 39240 00:59:17,360 --> 00:59:22,872 we we have done uh I did a documentary 39241 00:59:20,080 --> 00:59:27,000 about Emoji so and all the people that 39242 00:59:22,872 --> 00:59:30,192 create helped create emoji and uh we did 39243 00:59:27,000 --> 00:59:31,552 a cs50x movie night I think during the 39244 00:59:30,192 --> 00:59:32,960 pandemic was it during the pandemic 39245 00:59:31,552 --> 00:59:35,600 everything sort of like blurred together 39246 00:59:32,960 --> 00:59:40,480 but it was during the pandemic yeah Are 39247 00:59:35,600 --> 00:59:43,040 we more questions yes um I wanted to 39248 00:59:40,480 --> 00:59:46,480 know you mentioned that one of the 39249 00:59:43,040 --> 00:59:51,192 criteria for 39250 00:59:46,480 --> 00:59:51,192 AC do or there's demand 39251 00:59:51,960 --> 00:59:56,192 yeah yeah that's a very good question 39252 00:59:53,960 --> 01:00:00,192 yeah so the question is one of the 39253 00:59:56,192 --> 01:00:01,640 propos one of our criterias of of um 39254 01:00:00,192 --> 01:00:03,912 getting an emoji accepted is to sort of 39255 01:00:01,640 --> 01:00:05,960 to demonstrate demand and how do we 39256 01:00:03,912 --> 01:00:09,360 demonstrate demand and I would say in a 39257 01:00:05,960 --> 01:00:11,480 in a pretty um clumsy way actually at 39258 01:00:09,360 --> 01:00:13,912 this point so the main thing that you 39259 01:00:11,480 --> 01:00:16,720 have in our in our current proposal 39260 01:00:13,912 --> 01:00:18,552 process is we have um a median Emoji 39261 01:00:16,720 --> 01:00:20,160 which is elephant so elephant is like if 39262 01:00:18,552 --> 01:00:21,912 you stack ranked all the emoji for 39263 01:00:20,160 --> 01:00:23,640 popularity elephant is like right there 39264 01:00:21,912 --> 01:00:24,872 in the middle and it's also a concept 39265 01:00:23,640 --> 01:00:25,960 that's like universally understood 39266 01:00:24,872 --> 01:00:29,280 across all 39267 01:00:25,960 --> 01:00:31,720 languages so um elephant shows up 39268 01:00:29,280 --> 01:00:34,080 somewhere between 500 million and 700 39269 01:00:31,720 --> 01:00:36,640 million in Google search results like if 39270 01:00:34,080 --> 01:00:39,000 you type it into a laptop you'll see you 39271 01:00:36,640 --> 01:00:40,680 know elephant 500,000 uh 500 million 39272 01:00:39,000 --> 01:00:42,320 search results and generally you're 39273 01:00:40,680 --> 01:00:46,440 you're trying to when you're comparing 39274 01:00:42,320 --> 01:00:48,512 your term to elephant you want to see 39275 01:00:46,440 --> 01:00:50,160 very roughly how many Google search 39276 01:00:48,512 --> 01:00:52,552 results B search results sometimes 39277 01:00:50,160 --> 01:00:54,232 Instagram so actually something that was 39278 01:00:52,552 --> 01:00:56,512 really surprising to me was someone 39279 01:00:54,232 --> 01:01:01,232 proposed hummingbird I think hummingbird 39280 01:00:56,512 --> 01:01:03,680 is uh a good proposal and um but if you 39281 01:01:01,232 --> 01:01:06,040 look at Hummingbird it's only like 21 39282 01:01:03,680 --> 01:01:08,120 million in terms of the stat so which I 39283 01:01:06,040 --> 01:01:10,552 thought was like very surprisingly low 39284 01:01:08,120 --> 01:01:12,872 so that's one of the main ways that we 39285 01:01:10,552 --> 01:01:16,872 kind of we kind of see like is it also 39286 01:01:12,872 --> 01:01:20,440 visually used and all of that yeah any 39287 01:01:16,872 --> 01:01:22,192 other questions are we good I didn't 39288 01:01:20,440 --> 01:01:23,480 even need my water or anything oh can I 39289 01:01:22,192 --> 01:01:24,912 take I'm going to take a picture I'm 39290 01:01:23,480 --> 01:01:26,192 going to take a picture for because now 39291 01:01:24,912 --> 01:01:28,080 you guys are actually human and not 39292 01:01:26,192 --> 01:01:29,872 Muppets so I'm very very excited about 39293 01:01:28,080 --> 01:01:32,280 this so I will send this to like my 39294 01:01:29,872 --> 01:01:35,320 block mates and be like I just lectured 39295 01:01:32,280 --> 01:01:39,080 at CS you know in Sanders Theater my 39296 01:01:35,320 --> 01:01:41,832 thanks to Jenny Lee thank 39297 01:01:39,080 --> 01:01:43,680 you yeah you can stay up here for give 39298 01:01:41,832 --> 01:01:46,680 me one 39299 01:01:43,680 --> 01:01:46,680 second 39300 01:01:48,680 --> 01:01:53,192 so if up until now thought it would be 39301 01:01:51,320 --> 01:01:54,760 appropriate to toss this up on the board 39302 01:01:53,192 --> 01:01:56,280 if up until now you've not yet gotten 39303 01:01:54,760 --> 01:01:58,192 cs50 stress ball on the way out please 39304 01:01:56,280 --> 01:01:59,800 do grab one we got some extras as well 39305 01:01:58,192 --> 01:02:03,120 off to the side but I would also keep in 39306 01:01:59,800 --> 01:02:04,872 mind back in week zero where again we 39307 01:02:03,120 --> 01:02:06,872 began we asked you to categorize 39308 01:02:04,872 --> 01:02:08,400 yourselves as to whether you are among 39309 01:02:06,872 --> 01:02:09,832 those less comfortable those more 39310 01:02:08,400 --> 01:02:12,080 comfortable or those somewhere in 39311 01:02:09,832 --> 01:02:15,720 between uh please know now that you are 39312 01:02:12,080 --> 01:02:17,480 officially all some uh please know now 39313 01:02:15,720 --> 01:02:19,320 that you are all officially among those 39314 01:02:17,480 --> 01:02:21,000 more comfortable and indeed even though 39315 01:02:19,320 --> 01:02:23,040 a couple of more Milestones await we 39316 01:02:21,000 --> 01:02:26,000 cannot wait to see what you accomplish 39317 01:02:23,040 --> 01:02:31,640 with your final projects in the meantime 39318 01:02:26,000 --> 01:02:31,640 as always this is and now this was 39319 01:02:43,552 --> 01:02:46,552 cs50 39320 01:02:48,960 --> 01:02:57,820 [Laughter] 39321 01:02:54,680 --> 01:02:57,820 [Music] 39322 01:03:01,880 --> 01:03:04,949 [Music] 39323 01:03:07,040 --> 01:03:13,680 Mr when 39324 01:03:08,912 --> 01:03:19,400 I in CS I'll say truth be told I thought 39325 01:03:13,680 --> 01:03:19,400 i' be real coding hero because I did 39326 01:03:20,080 --> 01:03:28,160 so it picked up so fast turn out that 39327 01:03:24,600 --> 01:03:29,850 harder scratch hell world and 39328 01:03:28,160 --> 01:03:31,820 goodby 39329 01:03:29,850 --> 01:03:36,619 [Applause] 39330 01:03:31,820 --> 01:03:36,619 [Music] 39331 01:03:38,232 --> 01:03:42,120 because 39332 01:03:39,872 --> 01:03:46,512 Mr I was on 39333 01:03:42,120 --> 01:03:50,080 the trying so hard not fall behind 39334 01:03:46,512 --> 01:03:53,640 attending section and wanting a stuck in 39335 01:03:50,080 --> 01:03:53,640 the in void of my 39336 01:03:56,280 --> 01:04:05,360 computer to pyth in why on because this 39337 01:04:02,832 --> 01:04:05,360 is 39338 01:04:06,150 --> 01:04:09,850 [Music] 39339 01:04:10,912 --> 01:04:18,232 s Mr s we completed 39340 01:04:15,720 --> 01:04:24,360 JavaScript sequ CSS 39341 01:04:18,232 --> 01:04:24,360 and every language our to reset don't 39342 01:04:25,290 --> 01:04:28,380 [Music] 39343 01:04:29,320 --> 01:04:36,440 get again I repli 39344 01:04:33,232 --> 01:04:39,290 obviously because 39345 01:04:36,440 --> 01:04:42,620 cie 39346 01:04:39,290 --> 01:04:42,620 [Applause] 39347 01:04:43,400 --> 01:04:46,400 because 39348 01:04:48,300 --> 01:04:51,490 [Applause] 39349 01:04:53,120 --> 01:05:00,479 [Music] 39350 01:05:06,470 --> 01:05:14,800 [Music] 39351 01:05:31,080 --> 01:05:49,720 [Music] 39352 01:05:54,240 --> 01:05:57,289 [Music] 39353 01:06:03,570 --> 01:06:06,679 [Music] 39354 01:06:13,510 --> 01:06:26,869 [Music] 39355 01:06:40,832 --> 01:06:45,280 all right so this is cs50 my name is 39356 01:06:43,680 --> 01:06:46,760 David May and this is Harvard 39357 01:06:45,280 --> 01:06:48,400 University's introduction to the 39358 01:06:46,760 --> 01:06:50,832 intellectual Enterprises of computer 39359 01:06:48,400 --> 01:06:52,640 science and the Art of programming and 39360 01:06:50,832 --> 01:06:54,480 this of course is our special family 39361 01:06:52,640 --> 01:06:56,192 weekend wherein not only are cs50's own 39362 01:06:54,480 --> 01:06:58,440 students here in the audience but also 39363 01:06:56,192 --> 01:07:00,192 some family members as well now you're 39364 01:06:58,440 --> 01:07:02,400 showing up in the semester a little bit 39365 01:07:00,192 --> 01:07:04,192 late we've just tackled week eight which 39366 01:07:02,400 --> 01:07:05,872 is really our ninth week since computer 39367 01:07:04,192 --> 01:07:07,360 scientists start counting from zero so 39368 01:07:05,872 --> 01:07:09,000 we've done a whole lot of work over the 39369 01:07:07,360 --> 01:07:11,192 past few weeks as you might have heard 39370 01:07:09,000 --> 01:07:13,872 via emails or text messages home 39371 01:07:11,192 --> 01:07:15,440 including a language known here as binar 39372 01:07:13,872 --> 01:07:17,760 so on the screen here of course is a lot 39373 01:07:15,440 --> 01:07:20,440 of zeros and ones and suffice it to say 39374 01:07:17,760 --> 01:07:22,000 let me sum up the past nine weeks with 39375 01:07:20,440 --> 01:07:23,680 this is what's going on underneath the 39376 01:07:22,000 --> 01:07:25,192 hood but of course today we thought we'd 39377 01:07:23,680 --> 01:07:27,080 make things a little more accessible a 39378 01:07:25,192 --> 01:07:29,280 little more broadly applicable and 39379 01:07:27,080 --> 01:07:31,400 indeed our Focus today will not be on 39380 01:07:29,280 --> 01:07:33,512 what these patterns of zeros and ones 39381 01:07:31,400 --> 01:07:35,600 represent which in astute eye might 39382 01:07:33,512 --> 01:07:37,512 notice are replicated visually with 39383 01:07:35,600 --> 01:07:39,960 these light bulbs being in a pattern on 39384 01:07:37,512 --> 01:07:42,192 and off and as your child might have 39385 01:07:39,960 --> 01:07:44,480 hinted uh before class or perhaps now 39386 01:07:42,192 --> 01:07:46,080 this might very well spell a word up to 39387 01:07:44,480 --> 01:07:48,192 eight characters long because you can 39388 01:07:46,080 --> 01:07:50,040 encode even in the real world things 39389 01:07:48,192 --> 01:07:51,872 digital too but today we'll focus on 39390 01:07:50,040 --> 01:07:54,160 things much more high level this notion 39391 01:07:51,872 --> 01:07:58,000 of cyber security like our the security 39392 01:07:54,160 --> 01:07:59,872 of our data our privacy of our systems 39393 01:07:58,000 --> 01:08:01,192 particularly on the internet nowadays 39394 01:07:59,872 --> 01:08:02,640 because presumably all of us are 39395 01:08:01,192 --> 01:08:04,360 carrying Technologies around in our 39396 01:08:02,640 --> 01:08:07,720 pocket using laptops and desktops every 39397 01:08:04,360 --> 01:08:09,512 day and so the goal today is to 39398 01:08:07,720 --> 01:08:11,320 stipulate that this is what's going on 39399 01:08:09,512 --> 01:08:12,832 underneath the hood but let's solve some 39400 01:08:11,320 --> 01:08:14,680 problems at a higher level so that your 39401 01:08:12,832 --> 01:08:16,760 homework when you go back to wherever 39402 01:08:14,680 --> 01:08:19,080 you're visiting from can actually be to 39403 01:08:16,760 --> 01:08:21,720 apply some of today's Lessons Learned so 39404 01:08:19,080 --> 01:08:24,680 with that said perhaps the most common 39405 01:08:21,720 --> 01:08:26,512 familiar defense uh of one's systems and 39406 01:08:24,680 --> 01:08:28,160 data phone and laptops and desktops 39407 01:08:26,512 --> 01:08:30,120 would just be these simple passwords 39408 01:08:28,160 --> 01:08:32,600 unfortunately you and I are frankly as 39409 01:08:30,120 --> 01:08:34,232 humans not all that good at choosing 39410 01:08:32,600 --> 01:08:36,080 passwords and this is in itself a 39411 01:08:34,232 --> 01:08:39,480 relatively weak form of Defense even 39412 01:08:36,080 --> 01:08:41,552 though each of us has dozens hundreds of 39413 01:08:39,480 --> 01:08:46,040 passwords nowadays or at least dozens or 39414 01:08:41,552 --> 01:08:48,280 hundreds of accounts Maybe fives or tens 39415 01:08:46,040 --> 01:08:50,000 of dozens of passwords indeed if you're 39416 01:08:48,280 --> 01:08:52,280 in the habit of reusing passwords we'll 39417 01:08:50,000 --> 01:08:54,400 see today probably among our first 39418 01:08:52,280 --> 01:08:57,080 Lessons Learned so for instance if we 39419 01:08:54,400 --> 01:08:59,160 look back at the past year 2021 thanks 39420 01:08:57,080 --> 01:09:01,400 to security researchers who take a look 39421 01:08:59,160 --> 01:09:04,080 at data that has been hacked or leaked 39422 01:09:01,400 --> 01:09:06,192 online by way of public databases we 39423 01:09:04,080 --> 01:09:08,832 have a sense as computer scientists of 39424 01:09:06,192 --> 01:09:10,640 what the most popular or equivalently 39425 01:09:08,832 --> 01:09:11,872 what some of the worst passwords are 39426 01:09:10,640 --> 01:09:13,800 that you and I are choosing for our 39427 01:09:11,872 --> 01:09:16,320 system so as of this past year according 39428 01:09:13,800 --> 01:09:19,600 to one measure the most commonly used 39429 01:09:16,320 --> 01:09:23,720 password in systems everywhere was 1 2 3 39430 01:09:19,600 --> 01:09:25,760 4 5 6 all right number two password in 39431 01:09:23,720 --> 01:09:29,832 our top 10 here list was only slightly 39432 01:09:25,760 --> 01:09:32,120 longer 1 2 3 4 5 6 7 8 9 after that we 39433 01:09:29,832 --> 01:09:35,040 took a turn in the other direction 1 2 3 39434 01:09:32,120 --> 01:09:37,360 4 five alone after that got a little 39435 01:09:35,040 --> 01:09:38,960 more interesting quiry which might sound 39436 01:09:37,360 --> 01:09:40,832 pretty cryptic but not if you look down 39437 01:09:38,960 --> 01:09:43,440 at your US keyboard and it's the top 39438 01:09:40,832 --> 01:09:46,480 leftand row of the keys on a an American 39439 01:09:43,440 --> 01:09:49,120 keyboard so also not all that hard uh 39440 01:09:46,480 --> 01:09:52,440 perhaps not surprisingly uh a little 39441 01:09:49,120 --> 01:09:54,960 disconcertingly number five was 39442 01:09:52,440 --> 01:09:58,160 password meanwhile number six returns us 39443 01:09:54,960 --> 01:10:02,120 to digits 1 2 3 4 5 6 7 8 after that 39444 01:09:58,160 --> 01:10:04,040 really less effort 111 111 uh after that 39445 01:10:02,120 --> 01:10:06,480 a little more variation but not all that 39446 01:10:04,040 --> 01:10:08,232 much 1 two 3 one two 3 after that it's 39447 01:10:06,480 --> 01:10:09,120 getting even less interesting 1 2 3 4 5 39448 01:10:08,232 --> 01:10:11,872 6 7 39449 01:10:09,120 --> 01:10:15,000 890 and then lastly topping the list is 39450 01:10:11,872 --> 01:10:17,160 just 1 2 3 4 5 6 7 so this is not a good 39451 01:10:15,000 --> 01:10:18,832 top 10 list to be on so among today's 39452 01:10:17,160 --> 01:10:21,000 first takeaways is if you see your 39453 01:10:18,832 --> 01:10:23,160 password on the screen like you didn't 39454 01:10:21,000 --> 01:10:25,080 make the list in a good way this means 39455 01:10:23,160 --> 01:10:27,080 hundreds thousands millions of other 39456 01:10:25,080 --> 01:10:29,360 people probably have that password of 39457 01:10:27,080 --> 01:10:31,192 yours now in of itself that's not 39458 01:10:29,360 --> 01:10:33,192 necessarily worrisome because I don't 39459 01:10:31,192 --> 01:10:35,280 know who has these passwords in a room 39460 01:10:33,192 --> 01:10:39,280 as large as this but just intuitively 39461 01:10:35,280 --> 01:10:42,552 why is this a bad thing either parent or 39462 01:10:39,280 --> 01:10:44,440 child welcome to raise a hand here why 39463 01:10:42,552 --> 01:10:47,552 might this be 39464 01:10:44,440 --> 01:10:49,232 intuitively yeah access to it so access 39465 01:10:47,552 --> 01:10:51,000 to it like I mean we literally as 39466 01:10:49,232 --> 01:10:53,320 computer scientists now have a database 39467 01:10:51,000 --> 01:10:55,960 of really common passwords and your your 39468 01:10:53,320 --> 01:10:57,872 thoughts password and can just find it 39469 01:10:55,960 --> 01:10:59,960 out quickly yeah you can just find it 39470 01:10:57,872 --> 01:11:01,440 out quickly I mean you could imagine 39471 01:10:59,960 --> 01:11:03,040 trying to guess someone's password by 39472 01:11:01,440 --> 01:11:05,080 just typing in random letters random 39473 01:11:03,040 --> 01:11:07,512 numbers random words but not if you have 39474 01:11:05,080 --> 01:11:09,080 a top 10 list like the the adversaries 39475 01:11:07,512 --> 01:11:11,080 in the world might as well just start 39476 01:11:09,080 --> 01:11:12,552 with this list now you'll notice that 39477 01:11:11,080 --> 01:11:13,832 even absent from this are slight 39478 01:11:12,552 --> 01:11:15,232 variants like some of you might be 39479 01:11:13,832 --> 01:11:16,800 thinking I'm not on the list cuz I do 39480 01:11:15,232 --> 01:11:19,480 something clever like I use an 39481 01:11:16,800 --> 01:11:23,360 exclamation point for the number one or 39482 01:11:19,480 --> 01:11:24,832 a three for an e or a five for an S and 39483 01:11:23,360 --> 01:11:26,800 based on the smiles in the room right 39484 01:11:24,832 --> 01:11:29,192 now you're not all that clever it turns 39485 01:11:26,800 --> 01:11:30,800 out because other people are smiling too 39486 01:11:29,192 --> 01:11:32,640 which is to say that an adversary can 39487 01:11:30,800 --> 01:11:34,552 take those same fistic that you might 39488 01:11:32,640 --> 01:11:36,232 think are making things more secure by 39489 01:11:34,552 --> 01:11:37,960 just tweaking some letters to numbers or 39490 01:11:36,232 --> 01:11:39,832 vice versa but if you're doing it and 39491 01:11:37,960 --> 01:11:41,800 other people are doing it the bad guys 39492 01:11:39,832 --> 01:11:43,800 so to speak are going to be doing it as 39493 01:11:41,800 --> 01:11:47,080 well so unfortunately when it comes to 39494 01:11:43,800 --> 01:11:48,640 passwords better is longer and random 39495 01:11:47,080 --> 01:11:50,280 and really unguessable but that's not 39496 01:11:48,640 --> 01:11:51,720 what most of us have in fact case in 39497 01:11:50,280 --> 01:11:53,720 point on our phones whether you have an 39498 01:11:51,720 --> 01:11:55,232 Android device or an iPhone nowadays you 39499 01:11:53,720 --> 01:11:57,120 know odds are you have something 39500 01:11:55,232 --> 01:11:58,552 relatively simplistic protecting it if 39501 01:11:57,120 --> 01:11:59,872 you have anything at all but at least 39502 01:11:58,552 --> 01:12:01,640 Apple and Google are pretty good at 39503 01:11:59,872 --> 01:12:03,600 least nudging us to choose these kinds 39504 01:12:01,640 --> 01:12:06,832 of passcodes now and a four-digit 39505 01:12:03,600 --> 01:12:08,360 passcode is quite common nowadays and so 39506 01:12:06,832 --> 01:12:10,832 here's where we have an opportunity 39507 01:12:08,360 --> 01:12:13,800 thanks to uh the URL that you saw on the 39508 01:12:10,832 --> 01:12:16,360 screen earlier to conjecture as a group 39509 01:12:13,800 --> 01:12:18,160 just how long might it take an adversary 39510 01:12:16,360 --> 01:12:20,552 someone out there who's out to get us or 39511 01:12:18,160 --> 01:12:23,960 get one of us uh how long might it take 39512 01:12:20,552 --> 01:12:26,640 an adversary to figure out your phone's 39513 01:12:23,960 --> 01:12:27,960 4digit passcode this is a cs50 is on 39514 01:12:26,640 --> 01:12:30,000 Carter Carter if we could switch over 39515 01:12:27,960 --> 01:12:31,720 and poll the audience here if you take 39516 01:12:30,000 --> 01:12:33,680 out your phone or laptop whatever device 39517 01:12:31,720 --> 01:12:38,232 you might have used a few minutes ago to 39518 01:12:33,680 --> 01:12:41,912 scan that QR code or to visit that same 39519 01:12:38,232 --> 01:12:43,960 URL you can see these questions on your 39520 01:12:41,912 --> 01:12:45,440 browser and if you can't that's fine 39521 01:12:43,960 --> 01:12:47,040 we'll share some aggregate data 39522 01:12:45,440 --> 01:12:48,800 nonetheless but you should have an 39523 01:12:47,040 --> 01:12:50,680 opportunity to tap one of your answers 39524 01:12:48,800 --> 01:12:54,192 and we'll give folks a few more 39525 01:12:50,680 --> 01:12:57,832 seconds if you'd like to play along at 39526 01:12:54,192 --> 01:13:00,120 home and here in just a 39527 01:12:57,832 --> 01:13:01,232 moment probably have many people 39528 01:13:00,120 --> 01:13:03,512 reporting but why don't we go ahead and 39529 01:13:01,232 --> 01:13:06,480 take a look at some percentages it looks 39530 01:13:03,512 --> 01:13:08,440 like most of you 67% are proposing just 39531 01:13:06,480 --> 01:13:10,512 a few seconds so that's not all that 39532 01:13:08,440 --> 01:13:11,832 good news if it's a four-digit passcode 39533 01:13:10,512 --> 01:13:14,120 some of you are hoping it's a few 39534 01:13:11,832 --> 01:13:16,832 minutes uh 8% are hoping a few hours 39535 01:13:14,120 --> 01:13:19,000 four more than 4% of you are really 39536 01:13:16,832 --> 01:13:20,440 hoping perhaps it's a few days well 39537 01:13:19,000 --> 01:13:21,960 let's actually consider how we can 39538 01:13:20,440 --> 01:13:23,080 answer this question and make today not 39539 01:13:21,960 --> 01:13:24,512 just conceptual but a little 39540 01:13:23,080 --> 01:13:26,080 quantitative too and see if we can't 39541 01:13:24,512 --> 01:13:27,720 slap some numbers on questions like 39542 01:13:26,080 --> 01:13:29,360 these so ultimately you can make more 39543 01:13:27,720 --> 01:13:31,280 informed decisions with your system 39544 01:13:29,360 --> 01:13:33,960 security so for instance when it comes 39545 01:13:31,280 --> 01:13:36,600 to four digigit passcodes rather than 39546 01:13:33,960 --> 01:13:38,232 just consider how secure it is well 39547 01:13:36,600 --> 01:13:39,720 let's make it a more precise question 39548 01:13:38,232 --> 01:13:41,360 like what are the forms of attack well 39549 01:13:39,720 --> 01:13:42,960 the simplest attack might be just 39550 01:13:41,360 --> 01:13:45,000 someone grabbing your phone be it in 39551 01:13:42,960 --> 01:13:46,760 your family or maybe at Starbucks or the 39552 01:13:45,000 --> 01:13:49,912 airport or the like and just starting 39553 01:13:46,760 --> 01:13:53,400 all possible combinations maybe 00000000 39554 01:13:49,912 --> 01:13:55,120 then 00001 and 00002 we could maybe 39555 01:13:53,400 --> 01:13:57,480 automate this a little bit so for 39556 01:13:55,120 --> 01:14:00,440 instance I might potentially be able to 39557 01:13:57,480 --> 01:14:02,120 do something like uh roboticize this 39558 01:14:00,440 --> 01:14:03,760 here let me go ahead and full screen a 39559 01:14:02,120 --> 01:14:05,640 quick video here that's just going to 39560 01:14:03,760 --> 01:14:08,040 paint a picture in just a moment on the 39561 01:14:05,640 --> 01:14:09,512 screen of how if we're a really clever 39562 01:14:08,040 --> 01:14:11,320 adversary and know how to build things 39563 01:14:09,512 --> 01:14:12,720 well at least maybe we could automate 39564 01:14:11,320 --> 01:14:15,120 some of that process so here's an 39565 01:14:12,720 --> 01:14:17,400 Android phone on a counter here's a very 39566 01:14:15,120 --> 01:14:19,680 simple tripod and a little touch device 39567 01:14:17,400 --> 01:14:22,512 robotically doing all of that hacking 39568 01:14:19,680 --> 01:14:25,192 for you starting at 0000 Z probably all 39569 01:14:22,512 --> 01:14:27,480 the way up to 9999 now that too wasn't 39570 01:14:25,192 --> 01:14:29,552 necessarily all of that all that fast 39571 01:14:27,480 --> 01:14:31,480 but at least you the adversary can step 39572 01:14:29,552 --> 01:14:33,680 away and doesn't actually have to be 39573 01:14:31,480 --> 01:14:35,400 bothered with the time involved the cost 39574 01:14:33,680 --> 01:14:37,120 involved in actually hacking that 39575 01:14:35,400 --> 01:14:39,800 particular device well let's go one 39576 01:14:37,120 --> 01:14:43,872 level deeper a little more interestingly 39577 01:14:39,800 --> 01:14:46,040 and consider here um the how much much 39578 01:14:43,872 --> 01:14:47,680 time really this so-called Brute Force 39579 01:14:46,040 --> 01:14:49,192 attack would take and that's actually a 39580 01:14:47,680 --> 01:14:50,440 term of art much like in yester year 39581 01:14:49,192 --> 01:14:52,360 when maybe there was a battering ram 39582 01:14:50,440 --> 01:14:54,280 trying to brute force their way into a 39583 01:14:52,360 --> 01:14:56,000 castle or something like that a Brute 39584 01:14:54,280 --> 01:14:58,720 Force attack digitally is just someone 39585 01:14:56,000 --> 01:15:00,680 trying manually all possible codes or 39586 01:14:58,720 --> 01:15:02,192 maybe robotically trying all possible 39587 01:15:00,680 --> 01:15:04,600 codes but generally automating the 39588 01:15:02,192 --> 01:15:07,192 process in some way to go through all 39589 01:15:04,600 --> 01:15:11,512 possibilities well if you've got for 39590 01:15:07,192 --> 01:15:13,040 instance um a 4digit passcode let's ask 39591 01:15:11,512 --> 01:15:16,720 maybe a follow-up question here not how 39592 01:15:13,040 --> 01:15:20,000 long will take but how many possible 39593 01:15:16,720 --> 01:15:21,720 4digit passcodes are there because then 39594 01:15:20,000 --> 01:15:24,280 maybe we can do some quick math and if 39595 01:15:21,720 --> 01:15:25,640 every passcode takes me a second or a 39596 01:15:24,280 --> 01:15:27,000 few milliseconds or the like then I 39597 01:15:25,640 --> 01:15:28,552 think we can try to extrapolate from 39598 01:15:27,000 --> 01:15:31,280 that whether the first answer was 39599 01:15:28,552 --> 01:15:33,600 seconds or minutes or days or hours or 39600 01:15:31,280 --> 01:15:35,320 something else so how many four-digit 39601 01:15:33,600 --> 01:15:36,680 passcodes are possible if you take out 39602 01:15:35,320 --> 01:15:38,600 your same device it should have just 39603 01:15:36,680 --> 01:15:40,320 changed automatically if it doesn't seem 39604 01:15:38,600 --> 01:15:43,040 to have maybe reload your browser with 39605 01:15:40,320 --> 01:15:45,232 some menu option and then tap in here 39606 01:15:43,040 --> 01:15:49,280 how many four digigit passcodes are 39607 01:15:45,232 --> 01:15:51,832 possible four total 40 99,999 10,000 or 39608 01:15:49,280 --> 01:15:54,160 unsure is okay 39609 01:15:51,832 --> 01:15:57,000 too so let's see we'll give you a few 39610 01:15:54,160 --> 01:15:59,360 more moments how many four digigit 39611 01:15:57,000 --> 01:16:03,480 passcodes are possible and shall we 39612 01:15:59,360 --> 01:16:06,160 reveal the results so now it looks like 39613 01:16:03,480 --> 01:16:10,552 uh a a few of you uh 2% of you are 39614 01:16:06,160 --> 01:16:12,280 saying just four passcodes 40 99,999 39615 01:16:10,552 --> 01:16:15,760 there's definitely some contention here 39616 01:16:12,280 --> 01:16:17,280 and 6% are un sure well how do we wrap 39617 01:16:15,760 --> 01:16:19,120 our minds around this well let's just 39618 01:16:17,280 --> 01:16:22,192 kind of do this real simply here let me 39619 01:16:19,120 --> 01:16:25,000 switch back over to doing a bit of math 39620 01:16:22,192 --> 01:16:27,000 and if we have here 10 possibilities for 39621 01:16:25,000 --> 01:16:29,280 each digit if there's four digits each 39622 01:16:27,000 --> 01:16:31,192 digit can be 0 1 2 3 4 5 6 7 8 nine so 39623 01:16:29,280 --> 01:16:33,800 that's 10 possibilities so if you think 39624 01:16:31,192 --> 01:16:35,720 about the number of permutations that's 39625 01:16:33,800 --> 01:16:37,760 10 possibilities for the first digit 39626 01:16:35,720 --> 01:16:39,680 times 10 for the next time 10 to the for 39627 01:16:37,760 --> 01:16:42,320 the next times 10 for the next and so if 39628 01:16:39,680 --> 01:16:45,480 we do that out 10 * 10 * 10 * 10 or 10 39629 01:16:42,320 --> 01:16:48,680 the 4th there are indeed as 66% of you 39630 01:16:45,480 --> 01:16:50,232 uh found uh 10,000 possibilities and so 39631 01:16:48,680 --> 01:16:51,912 now we can kind of work backwards and 39632 01:16:50,232 --> 01:16:54,000 decide how long is it going to take for 39633 01:16:51,912 --> 01:16:56,360 an adversary to hack into this phone 39634 01:16:54,000 --> 01:16:58,120 because if it's one attack one guess per 39635 01:16:56,360 --> 01:17:00,440 second well that's going to map out to 39636 01:16:58,120 --> 01:17:02,640 10,000 seconds but maybe not if the 39637 01:17:00,440 --> 01:17:04,192 adversary isn't a roboticist or a human 39638 01:17:02,640 --> 01:17:06,320 what if they're like at a software 39639 01:17:04,192 --> 01:17:08,600 programmer someone who's taken even a 39640 01:17:06,320 --> 01:17:10,320 class introductory like cs50 and learned 39641 01:17:08,600 --> 01:17:11,832 a little bit of programming well a 39642 01:17:10,320 --> 01:17:14,320 little bit frighteningly it's not all 39643 01:17:11,832 --> 01:17:16,192 that hard to hack into systems if you 39644 01:17:14,320 --> 01:17:18,320 just know how to code to and really have 39645 01:17:16,192 --> 01:17:20,000 the computer do your work for you so in 39646 01:17:18,320 --> 01:17:22,040 fact let me go ahead and change over to 39647 01:17:20,000 --> 01:17:23,552 another screen on my computer here um 39648 01:17:22,040 --> 01:17:25,192 this is different for students in the 39649 01:17:23,552 --> 01:17:26,720 group from vs code this is just a black 39650 01:17:25,192 --> 01:17:28,280 and white version of it that we've used 39651 01:17:26,720 --> 01:17:30,120 briefly in the past and I'm just going 39652 01:17:28,280 --> 01:17:32,512 to go ahead and create a program called 39653 01:17:30,120 --> 01:17:34,232 crack.pie to crack something just 39654 01:17:32,512 --> 01:17:36,600 technically means to figure out what it 39655 01:17:34,232 --> 01:17:38,440 is figure out a password in this case 39656 01:17:36,600 --> 01:17:39,800 and dop means I'm going to use a 39657 01:17:38,440 --> 01:17:41,720 programming language that we here in 39658 01:17:39,800 --> 01:17:43,480 cs50 have been dabbling in in the past 39659 01:17:41,720 --> 01:17:46,000 couple of weeks with more to come next 39660 01:17:43,480 --> 01:17:47,480 week as well so it turns out and you 39661 01:17:46,000 --> 01:17:50,600 need not understand each of these lines 39662 01:17:47,480 --> 01:17:53,832 of code if I want to try maybe 39663 01:17:50,600 --> 01:17:55,232 generating all 10,000 possible codes I'm 39664 01:17:53,832 --> 01:17:56,832 not going to bother with a robot I've 39665 01:17:55,232 --> 01:17:58,512 got all these cables coming out of my 39666 01:17:56,832 --> 01:18:01,080 computer and odds are one of them is a 39667 01:17:58,512 --> 01:18:02,440 USB cable or a lightning cable surely we 39668 01:18:01,080 --> 01:18:04,720 could figure out how to connect like 39669 01:18:02,440 --> 01:18:06,552 laptop or desktop to phone and just like 39670 01:18:04,720 --> 01:18:08,192 automate the process nowadays by just 39671 01:18:06,552 --> 01:18:10,600 sending all of the numbers into the 39672 01:18:08,192 --> 01:18:12,800 phone until one uh unlocks the trick 39673 01:18:10,600 --> 01:18:14,552 just like in the movies or TV well in 39674 01:18:12,800 --> 01:18:17,040 Python I could write a program that does 39675 01:18:14,552 --> 01:18:19,600 this as follows I can import so to speak 39676 01:18:17,040 --> 01:18:21,080 all of the decimal digits 0 through n 39677 01:18:19,600 --> 01:18:23,440 and this for students in the room is 39678 01:18:21,080 --> 01:18:26,040 just a slightly better version of typing 39679 01:18:23,440 --> 01:18:28,600 out 10 different numbers manually I can 39680 01:18:26,040 --> 01:18:30,760 also import from a library so to speak 39681 01:18:28,600 --> 01:18:32,120 called iter tools for iteration tools 39682 01:18:30,760 --> 01:18:35,512 which means to do something again and 39683 01:18:32,120 --> 01:18:37,280 again I can import a function called 39684 01:18:35,512 --> 01:18:39,192 Product which means the cross product 39685 01:18:37,280 --> 01:18:40,960 like combine this with this some number 39686 01:18:39,192 --> 01:18:42,512 of times and then it's just two more 39687 01:18:40,960 --> 01:18:44,680 lines of code I can use what's called a 39688 01:18:42,512 --> 01:18:47,760 loop and programming so for every 39689 01:18:44,680 --> 01:18:51,720 passcode in the cross product of all 10 39690 01:18:47,760 --> 01:18:54,400 of those digits repeated a total of four 39691 01:18:51,720 --> 01:18:55,872 times let me go ahead and rather than 39692 01:18:54,400 --> 01:18:57,912 bother connecting my phone and hacking 39693 01:18:55,872 --> 01:18:59,680 my own phone let me just print out every 39694 01:18:57,912 --> 01:19:01,440 one of those 10,000 codes on the screen 39695 01:18:59,680 --> 01:19:03,080 and we'll see how fast the hacker could 39696 01:19:01,440 --> 01:19:04,640 do this let me go ahead and print and 39697 01:19:03,080 --> 01:19:06,480 with an asterisk which is a little trick 39698 01:19:04,640 --> 01:19:08,192 to format it nicely I'm going to print 39699 01:19:06,480 --> 01:19:10,760 out each of those passcodes and that's 39700 01:19:08,192 --> 01:19:12,640 it four lines of code maybe 40 seconds 39701 01:19:10,760 --> 01:19:14,640 of talking but maybe really four seconds 39702 01:19:12,640 --> 01:19:16,552 of code if I actually did this without 39703 01:19:14,640 --> 01:19:18,760 the audience and now let me go ahead and 39704 01:19:16,552 --> 01:19:21,192 save the file and I'm going to run as we 39705 01:19:18,760 --> 01:19:23,680 do every in class of late python of 39706 01:19:21,192 --> 01:19:25,320 crack.pie and when I hit enter I should 39707 01:19:23,680 --> 01:19:28,440 see on the screen all 10,000 39708 01:19:25,320 --> 01:19:30,400 possibilities from 000000 to 9999 so 39709 01:19:28,440 --> 01:19:35,232 let's see is it a few seconds minutes 39710 01:19:30,400 --> 01:19:37,360 hours or days done so barely even 39711 01:19:35,232 --> 01:19:38,872 seconds plural if that so that should be 39712 01:19:37,360 --> 01:19:40,440 a little disconcerting because all that 39713 01:19:38,872 --> 01:19:42,192 adversary needs to do is grab your phone 39714 01:19:40,440 --> 01:19:44,360 off the counter plug in a cable and boom 39715 01:19:42,192 --> 01:19:46,800 they're done like there's no ticking 39716 01:19:44,360 --> 01:19:48,000 clock or worries as in the movies or TV 39717 01:19:46,800 --> 01:19:49,960 that maybe you're going to come into the 39718 01:19:48,000 --> 01:19:52,400 room you don't need that much of a 39719 01:19:49,960 --> 01:19:55,000 window of time so what would be better 39720 01:19:52,400 --> 01:19:56,800 than this well let's consider what our 39721 01:19:55,000 --> 01:19:58,800 options might be if we don't want to 39722 01:19:56,800 --> 01:20:00,600 just use four-digit passcode some of you 39723 01:19:58,800 --> 01:20:02,680 indeed might have better passcodes than 39724 01:20:00,600 --> 01:20:05,320 that and maybe you use four letter 39725 01:20:02,680 --> 01:20:07,480 passcodes instead so A through Z maybe 39726 01:20:05,320 --> 01:20:09,360 uppercase and lowercase that starts to 39727 01:20:07,480 --> 01:20:11,400 make things a little more interesting so 39728 01:20:09,360 --> 01:20:13,832 should we pull this question too if we 39729 01:20:11,400 --> 01:20:15,960 upgrade from four digits to just four 39730 01:20:13,832 --> 01:20:17,720 letters English letters a through z 39731 01:20:15,960 --> 01:20:19,800 uppercase and lowercase why don't we go 39732 01:20:17,720 --> 01:20:25,000 ahead and pull the grip here and ask how 39733 01:20:19,800 --> 01:20:28,000 many four letter passcodes are there 39734 01:20:25,000 --> 01:20:30,080 instead so this time the range starts at 39735 01:20:28,000 --> 01:20:33,232 four still not the right answer though 39736 01:20:30,080 --> 01:20:35,680 this time how many four-letter passcodes 39737 01:20:33,232 --> 01:20:35,680 are 39738 01:20:35,872 --> 01:20:41,960 possible not just just take a couple 39739 01:20:39,680 --> 01:20:44,640 more 39740 01:20:41,960 --> 01:20:47,080 seconds 39741 01:20:44,640 --> 01:20:51,120 all right almost a couple hundred 39742 01:20:47,080 --> 01:20:51,120 responses in already few more 39743 01:20:52,320 --> 01:20:57,360 seconds 39744 01:20:54,360 --> 01:20:59,960 and why don't we go ahead and reveal now 39745 01:20:57,360 --> 01:21:02,480 the answers which are Okay so we've 39746 01:20:59,960 --> 01:21:04,760 solved a couple problems at least so we 39747 01:21:02,480 --> 01:21:06,680 well we well okay someone's just messing 39748 01:21:04,760 --> 01:21:09,360 with us now all right so it looks like 39749 01:21:06,680 --> 01:21:11,912 most of you 76% of you have claimed it's 39750 01:21:09,360 --> 01:21:13,600 7 million plus possibilities so that's 39751 01:21:11,912 --> 01:21:15,480 encouraging cuz that's a whole order of 39752 01:21:13,600 --> 01:21:16,640 magnitude more than before well let's 39753 01:21:15,480 --> 01:21:18,680 figure out how we might do this 39754 01:21:16,640 --> 01:21:20,912 mathematically so if we've got 26 39755 01:21:18,680 --> 01:21:22,480 lowercase 26 uppercase that's 52 39756 01:21:20,912 --> 01:21:25,080 possibilities now for each of those four 39757 01:21:22,480 --> 01:21:27,440 digits so that's 52 times itself four 39758 01:21:25,080 --> 01:21:29,360 times which indeed either off the top of 39759 01:21:27,440 --> 01:21:31,120 your head a good guess a calculator on 39760 01:21:29,360 --> 01:21:34,120 the same device you're using right now 39761 01:21:31,120 --> 01:21:36,320 indeed gives us 7 million instead well 39762 01:21:34,120 --> 01:21:38,160 what might be slightly better than that 39763 01:21:36,320 --> 01:21:40,000 well maybe four characters and this 39764 01:21:38,160 --> 01:21:41,832 indeed is what your Macs PCS and phones 39765 01:21:40,000 --> 01:21:43,872 are urging us to do nowadays not just 39766 01:21:41,832 --> 01:21:46,400 numbers not just letters but like really 39767 01:21:43,872 --> 01:21:48,360 annoying punctuation so it really looks 39768 01:21:46,400 --> 01:21:50,232 cryptic not just to the adversary but 39769 01:21:48,360 --> 01:21:52,360 also to you and me unfortunately and 39770 01:21:50,232 --> 01:21:54,400 that's the downside but here now we have 39771 01:21:52,360 --> 01:21:56,232 a mental model and really a 39772 01:21:54,400 --> 01:21:58,000 computational framework via which we can 39773 01:21:56,232 --> 01:22:00,120 evaluate the security of these and I'll 39774 01:21:58,000 --> 01:22:02,832 go ahead and spoil some of the math here 39775 01:22:00,120 --> 01:22:04,600 if we've got 52 uh letters of the 39776 01:22:02,832 --> 01:22:06,160 alphabet uppercase and lowercase 10 39777 01:22:04,600 --> 01:22:09,040 digits and if I count them out on my 39778 01:22:06,160 --> 01:22:10,872 keyboard about 32 punctuation symbols in 39779 01:22:09,040 --> 01:22:13,912 typical English grammar that actually 39780 01:22:10,872 --> 01:22:17,720 gives us 94 possibilities now which is 39781 01:22:13,912 --> 01:22:19,440 up from 52 which is up from 10 so now 39782 01:22:17,720 --> 01:22:21,800 we're really moving and now that would 39783 01:22:19,440 --> 01:22:23,720 give us 78 million possibility so 39784 01:22:21,800 --> 01:22:25,680 another order of magnitude now it's 39785 01:22:23,720 --> 01:22:26,912 still going to be relatively fast 39786 01:22:25,680 --> 01:22:29,320 because you know what I can actually do 39787 01:22:26,912 --> 01:22:32,080 this let me go back into my code here 39788 01:22:29,320 --> 01:22:33,832 let me reopen the same program and I can 39789 01:22:32,080 --> 01:22:35,800 point out just how easy it is to make 39790 01:22:33,832 --> 01:22:38,800 these changes instead of importing 39791 01:22:35,800 --> 01:22:40,800 digits as before I can import as your uh 39792 01:22:38,800 --> 01:22:43,120 child might know asky letters which are 39793 01:22:40,800 --> 01:22:46,552 a through z uppercase lowercase and I 39794 01:22:43,120 --> 01:22:48,192 can just change this here asky letters 39795 01:22:46,552 --> 01:22:49,800 and so this was that first version where 39796 01:22:48,192 --> 01:22:51,552 we just changed to letters let me now 39797 01:22:49,800 --> 01:22:53,640 rerun the code and instead of seeing 39798 01:22:51,552 --> 01:22:55,440 numbers we'll see letters flying across 39799 01:22:53,640 --> 01:22:57,720 the screen and if I walk over here to 39800 01:22:55,440 --> 01:22:59,640 the screen we'll see that by the time I 39801 01:22:57,720 --> 01:23:01,872 get here we're halfway through the 39802 01:22:59,640 --> 01:23:03,680 entire alphabet lowercase if I now start 39803 01:23:01,872 --> 01:23:06,000 walking away I think yep we're already 39804 01:23:03,680 --> 01:23:08,360 done now with uppercase as well if I 39805 01:23:06,000 --> 01:23:10,280 upgrade this slightly further let's go 39806 01:23:08,360 --> 01:23:13,960 ahead and take it one more level and 39807 01:23:10,280 --> 01:23:16,600 perhaps do let's say ask letters and 39808 01:23:13,960 --> 01:23:18,800 digits and punctuation and this would be 39809 01:23:16,600 --> 01:23:21,040 the pythonic way to say that and I'm 39810 01:23:18,800 --> 01:23:23,912 going to add to those letters those same 39811 01:23:21,040 --> 01:23:25,400 digits those same punctuation symbols 39812 01:23:23,912 --> 01:23:27,600 let me shrink my font just so the code 39813 01:23:25,400 --> 01:23:30,280 still fits on the screen and what we now 39814 01:23:27,600 --> 01:23:33,360 have is with a two seconds of changes a 39815 01:23:30,280 --> 01:23:36,120 program that if I run this version 39816 01:23:33,360 --> 01:23:39,000 whoops without the typographical error 39817 01:23:36,120 --> 01:23:41,600 this is what we call in cs50 a bug so 39818 01:23:39,000 --> 01:23:44,232 now we run the same this is what we call 39819 01:23:41,600 --> 01:23:46,872 in cs50 a second 39820 01:23:44,232 --> 01:23:49,480 bug 39821 01:23:46,872 --> 01:23:51,080 punctuation okay this is where I cross 39822 01:23:49,480 --> 01:23:52,832 my fingers okay so now it's going to be 39823 01:23:51,080 --> 01:23:54,480 a little hard to see as it flies across 39824 01:23:52,832 --> 01:23:56,320 the screen but you probably are seeing 39825 01:23:54,480 --> 01:23:58,120 glimpses of some weird punctuation 39826 01:23:56,320 --> 01:24:00,160 characters as well and I won't waste our 39827 01:23:58,120 --> 01:24:01,912 time trying to talk through this because 39828 01:24:00,160 --> 01:24:03,512 this is going to take longer we're still 39829 01:24:01,912 --> 01:24:06,440 in the lower case I'm still over here 39830 01:24:03,512 --> 01:24:08,912 already we've not even gotten to n now O 39831 01:24:06,440 --> 01:24:11,232 then P so this is going to run longer 39832 01:24:08,912 --> 01:24:12,800 but let's end with one final question on 39833 01:24:11,232 --> 01:24:14,800 the security of all these systems I'm 39834 01:24:12,800 --> 01:24:16,512 going to cancel that by hitting contrl C 39835 01:24:14,800 --> 01:24:19,000 on my keyboard and let's ask the 39836 01:24:16,512 --> 01:24:20,832 question instead if we use eight 39837 01:24:19,000 --> 01:24:23,232 character passwords so twice as many 39838 01:24:20,832 --> 01:24:24,960 characters but even that is not terribly 39839 01:24:23,232 --> 01:24:26,640 long right this is eight characters 39840 01:24:24,960 --> 01:24:28,552 alone on the stage eight characters 39841 01:24:26,640 --> 01:24:30,360 using letters numbers and punctuation 39842 01:24:28,552 --> 01:24:33,120 might be better let's do one final vote 39843 01:24:30,360 --> 01:24:36,000 here if we could on your same device how 39844 01:24:33,120 --> 01:24:38,040 many eight character possibilities are 39845 01:24:36,000 --> 01:24:40,000 there now 39846 01:24:38,040 --> 01:24:42,400 for these 39847 01:24:40,000 --> 01:24:44,120 passcodes and now for didn't even make 39848 01:24:42,400 --> 01:24:46,872 the list this 39849 01:24:44,120 --> 01:24:48,760 time all right few more seconds about 39850 01:24:46,872 --> 01:24:51,320 100 responses so 39851 01:24:48,760 --> 01:24:52,640 far how about we go ahead and Carter if 39852 01:24:51,320 --> 01:24:55,192 you would't mind let's reveal the 39853 01:24:52,640 --> 01:24:56,600 results based on the vote a pretty 39854 01:24:55,192 --> 01:24:58,600 decent spread here although the 39855 01:24:56,600 --> 01:25:00,320 quadrillions are quickly buzzing in and 39856 01:24:58,600 --> 01:25:02,872 they're contending with the others here 39857 01:25:00,320 --> 01:25:05,720 looks like 44% of you said quintilian 39858 01:25:02,872 --> 01:25:08,600 34% said quadrillion and this time for 39859 01:25:05,720 --> 01:25:11,120 the first time uh you overbid so indeed 39860 01:25:08,600 --> 01:25:13,040 if we go back to the math here at least 39861 01:25:11,120 --> 01:25:15,720 the majority overbid if we have eight 39862 01:25:13,040 --> 01:25:18,360 character passcodes that gives us 94 39863 01:25:15,720 --> 01:25:20,552 times itself 8 times or 94 to the eth 39864 01:25:18,360 --> 01:25:26,080 power and in fact that gives us roughly 39865 01:25:20,552 --> 01:25:28,600 6 quadrillion 95 trillion 689 Bill 385 39866 01:25:26,080 --> 01:25:31,680 mil 410,000 and 39867 01:25:28,600 --> 01:25:34,320 86 possible passcodes now what does that 39868 01:25:31,680 --> 01:25:36,680 mean well the adversary's algorithm the 39869 01:25:34,320 --> 01:25:38,800 step-by-step code that they write to try 39870 01:25:36,680 --> 01:25:40,912 to hack into your phone is no different 39871 01:25:38,800 --> 01:25:43,832 and honestly if your passcode is eight 39872 01:25:40,912 --> 01:25:45,872 characters long but there're zeros 39873 01:25:43,832 --> 01:25:47,512 00000000000000 you're no more secure 39874 01:25:45,872 --> 01:25:49,320 fundamentally you really want to be 39875 01:25:47,512 --> 01:25:51,232 somewhere in The Sweet Spot of that 39876 01:25:49,320 --> 01:25:53,040 massive range of values so that if the 39877 01:25:51,232 --> 01:25:55,000 adversary tries this Brute Force attack 39878 01:25:53,040 --> 01:25:58,360 just running through all possibilities 39879 01:25:55,000 --> 01:26:00,680 they will eventually reach your passcode 39880 01:25:58,360 --> 01:26:02,160 just mathematically it will be there 39881 01:26:00,680 --> 01:26:04,160 hopefully though well maybe not 39882 01:26:02,160 --> 01:26:06,600 hopefully you and I and they will be 39883 01:26:04,160 --> 01:26:08,720 gone from this world because that much 39884 01:26:06,600 --> 01:26:11,040 time will have passed and if we fact if 39885 01:26:08,720 --> 01:26:14,160 we do out the math here uh this number 39886 01:26:11,040 --> 01:26:16,912 of seconds for instance is long past uh 39887 01:26:14,160 --> 01:26:18,480 when uh uh we will uh no longer be here 39888 01:26:16,912 --> 01:26:20,000 so that's the sort of measure is we 39889 01:26:18,480 --> 01:26:21,912 don't sort of fundamentally change the 39890 01:26:20,000 --> 01:26:23,720 equation for the adversary it's still 39891 01:26:21,912 --> 01:26:25,360 the same risk it's still the same attack 39892 01:26:23,720 --> 01:26:27,872 but you significantly drive down the 39893 01:26:25,360 --> 01:26:30,120 probability of success on their part or 39894 01:26:27,872 --> 01:26:32,232 conceptually you drive up the cost to 39895 01:26:30,120 --> 01:26:34,440 the adversary and indeed even in the 39896 01:26:32,232 --> 01:26:36,440 physical world this is true you just 39897 01:26:34,440 --> 01:26:38,872 want your passcode in the digital world 39898 01:26:36,440 --> 01:26:40,640 really to be better than someone else's 39899 01:26:38,872 --> 01:26:42,000 because you want someone else's passcode 39900 01:26:40,640 --> 01:26:43,280 to be the one that the adversary does 39901 01:26:42,000 --> 01:26:45,440 something with with just like in the 39902 01:26:43,280 --> 01:26:47,512 physical world even though it's a bit uh 39903 01:26:45,440 --> 01:26:50,120 uncomfortable to consider your house 39904 01:26:47,512 --> 01:26:52,080 doesn't need to be 100% secure and 39905 01:26:50,120 --> 01:26:53,640 indeed it's difficult to make it such 39906 01:26:52,080 --> 01:26:55,552 there's always going to be a a point of 39907 01:26:53,640 --> 01:26:57,872 weakness maybe it's that window the door 39908 01:26:55,552 --> 01:26:59,552 or something like that but if your home 39909 01:26:57,872 --> 01:27:02,280 is more secure than the next door home 39910 01:26:59,552 --> 01:27:04,552 just probabilistically you are more 39911 01:27:02,280 --> 01:27:06,360 secure you're not secure and indeed any 39912 01:27:04,552 --> 01:27:08,440 website you see down the road that says 39913 01:27:06,360 --> 01:27:10,440 we are secure because we do X Y or Z 39914 01:27:08,440 --> 01:27:13,000 like that's nonsense security is really 39915 01:27:10,440 --> 01:27:15,552 about comparisons and Val ating things 39916 01:27:13,000 --> 01:27:18,120 if quantitatively relative to some other 39917 01:27:15,552 --> 01:27:20,040 system relative to some other code so 39918 01:27:18,120 --> 01:27:21,512 what's the takeaway here well hopefully 39919 01:27:20,040 --> 01:27:24,080 a non-trivial number of you we'll go 39920 01:27:21,512 --> 01:27:26,480 home this weekend on Monday and change 39921 01:27:24,080 --> 01:27:27,760 at least one passcode um but there's 39922 01:27:26,480 --> 01:27:29,832 going to be a trade-off here and we talk 39923 01:27:27,760 --> 01:27:32,832 about this all the time in cs50 anytime 39924 01:27:29,832 --> 01:27:34,720 we improve something we pay some price 39925 01:27:32,832 --> 01:27:36,480 in time and performance and cost 39926 01:27:34,720 --> 01:27:38,600 somewhere else so what's the downside 39927 01:27:36,480 --> 01:27:40,912 then of this advice that you should use 39928 01:27:38,600 --> 01:27:43,160 minimally eight character 39929 01:27:40,912 --> 01:27:44,872 passcodes why might you want to say nay 39930 01:27:43,160 --> 01:27:47,552 and not do 39931 01:27:44,872 --> 01:27:49,512 this say again you have to remember you 39932 01:27:47,552 --> 01:27:51,160 have to remember it right and so here 39933 01:27:49,512 --> 01:27:52,960 there's sort of some sociology there's 39934 01:27:51,160 --> 01:27:54,192 some human behavior you know some of you 39935 01:27:52,960 --> 01:27:55,600 might have colleagues if you're working 39936 01:27:54,192 --> 01:27:57,360 in the real world at least back in 39937 01:27:55,600 --> 01:27:58,832 healthier times when you had colleagues 39938 01:27:57,360 --> 01:28:00,080 with desks and cubicles and there's 39939 01:27:58,832 --> 01:28:02,080 probably one person in the office with 39940 01:28:00,080 --> 01:28:03,872 like a Post-It note on their monitor 39941 01:28:02,080 --> 01:28:07,232 with their passcode you know it's a bit 39942 01:28:03,872 --> 01:28:09,440 of a cyber security offense but it's 39943 01:28:07,232 --> 01:28:11,232 also a sort of real world side effect 39944 01:28:09,440 --> 01:28:14,000 maybe of corporate policies that aren't 39945 01:28:11,232 --> 01:28:15,600 really calibrated for human behavior so 39946 01:28:14,000 --> 01:28:17,360 we'll see if there's some other defenses 39947 01:28:15,600 --> 01:28:19,280 and indeed let me propose that we talk 39948 01:28:17,360 --> 01:28:21,120 briefly about one that actually tends to 39949 01:28:19,280 --> 01:28:22,832 kick in automatically even if your 39950 01:28:21,120 --> 01:28:24,800 passcode is not as strong as we've just 39951 01:28:22,832 --> 01:28:26,552 seen one of these six quadrillion 39952 01:28:24,800 --> 01:28:28,680 possibilities well what could we do 39953 01:28:26,552 --> 01:28:31,912 instead well as anyone and I'll zoom in 39954 01:28:28,680 --> 01:28:34,120 on this here accidentally locked themsel 39955 01:28:31,912 --> 01:28:36,512 out of their own phone before like when 39956 01:28:34,120 --> 01:28:38,600 does that happen yeah when you try the 39957 01:28:36,512 --> 01:28:40,760 password too many times yeah so too many 39958 01:28:38,600 --> 01:28:42,552 times maybe your finger's slightly off 39959 01:28:40,760 --> 01:28:45,120 maybe you're slightly off and you just 39960 01:28:42,552 --> 01:28:47,000 don't input the same passcode correctly 39961 01:28:45,120 --> 01:28:48,872 after like five times 10 times there's 39962 01:28:47,000 --> 01:28:50,832 some reasonable threshold and why does 39963 01:28:48,872 --> 01:28:52,600 that happen well Apple and Google 39964 01:28:50,832 --> 01:28:54,640 equivalently figure just 39965 01:28:52,600 --> 01:28:56,232 probabilistically if after 10 guesses 39966 01:28:54,640 --> 01:28:58,400 you still haven't typed in the right 39967 01:28:56,232 --> 01:28:59,872 passcode probably you're not you you're 39968 01:28:58,400 --> 01:29:01,192 someone else who's picked up your phone 39969 01:28:59,872 --> 01:29:03,280 so we're just going to go ahead and lock 39970 01:29:01,192 --> 01:29:05,040 you out now what's the effect of this 39971 01:29:03,280 --> 01:29:06,760 well this means now that each of those 39972 01:29:05,040 --> 01:29:09,480 possible passcodes no longer takes 39973 01:29:06,760 --> 01:29:11,440 roughly 1 second now it takes roughly 1 39974 01:29:09,480 --> 01:29:13,280 minute so the attack is still the same 39975 01:29:11,440 --> 01:29:16,000 but if it's now one passcode or 10 39976 01:29:13,280 --> 01:29:18,360 guesses per minute we have significantly 39977 01:29:16,000 --> 01:29:19,760 by a factor of 60 in this story slowed 39978 01:29:18,360 --> 01:29:21,360 things down and unfortunately does 39979 01:29:19,760 --> 01:29:23,440 anyone know what happens if you screw up 39980 01:29:21,360 --> 01:29:25,512 again after a 39981 01:29:23,440 --> 01:29:27,912 minute yeah it goes longer it's like 39982 01:29:25,512 --> 01:29:29,480 five minutes and then 10 minutes and uh 39983 01:29:27,912 --> 01:29:31,232 Google is kind of obnoxious about it 39984 01:29:29,480 --> 01:29:34,000 they don't even give you a time frame 39985 01:29:31,232 --> 01:29:36,192 they just say try again later and so 39986 01:29:34,000 --> 01:29:38,080 that keeps not only the adversary out 39987 01:29:36,192 --> 01:29:39,832 but also potentially you so there in 39988 01:29:38,080 --> 01:29:41,512 lies that trade-off if you've forgotten 39989 01:29:39,832 --> 01:29:43,120 your code if nowadays your finger is 39990 01:29:41,512 --> 01:29:45,120 slightly wet so the screen isn't 39991 01:29:43,120 --> 01:29:47,552 responding correctly these could be 39992 01:29:45,120 --> 01:29:49,960 usability downsides too so security is 39993 01:29:47,552 --> 01:29:52,552 really just about finding The Sweet Spot 39994 01:29:49,960 --> 01:29:54,232 among these various tradeoffs here but 39995 01:29:52,552 --> 01:29:56,480 there's other mechanisms too and some of 39996 01:29:54,232 --> 01:29:58,960 you might recognize this screen from 39997 01:29:56,480 --> 01:30:01,480 Gmail via which of course you log in but 39998 01:29:58,960 --> 01:30:04,080 after you log into Gmail or similar 39999 01:30:01,480 --> 01:30:06,000 websites or apps or systems at work 40000 01:30:04,080 --> 01:30:08,192 nowadays especially you might be 40001 01:30:06,000 --> 01:30:10,872 presented with uh what's called 40002 01:30:08,192 --> 01:30:12,872 two-factor authentication and what is 40003 01:30:10,872 --> 01:30:15,320 this in a nutshell in Lay person 40004 01:30:12,872 --> 01:30:17,232 terms most many of you if you do 40005 01:30:15,320 --> 01:30:19,440 anything digitally at work might have to 40006 01:30:17,232 --> 01:30:21,872 do this now 40007 01:30:19,440 --> 01:30:24,400 yeah exactly you get texted at your 40008 01:30:21,872 --> 01:30:26,232 phone an additional code that's not your 40009 01:30:24,400 --> 01:30:28,160 same password it's typically a numeric 40010 01:30:26,232 --> 01:30:30,080 code maybe six digits long it expires 40011 01:30:28,160 --> 01:30:32,760 after a minute or 10 minutes but why is 40012 01:30:30,080 --> 01:30:34,400 this a good thing well one it's no 40013 01:30:32,760 --> 01:30:36,320 longer just a piece of information that 40014 01:30:34,400 --> 01:30:38,832 you know or that you might have written 40015 01:30:36,320 --> 01:30:40,640 down it's information that changes every 40016 01:30:38,832 --> 01:30:42,552 time you try to log in but more 40017 01:30:40,640 --> 01:30:44,080 importantly it's a fundamentally second 40018 01:30:42,552 --> 01:30:45,552 factor which means it's not just 40019 01:30:44,080 --> 01:30:47,720 something you know now it's something 40020 01:30:45,552 --> 01:30:49,512 you have so you for instance are the 40021 01:30:47,720 --> 01:30:51,000 only one theoretically that should be 40022 01:30:49,512 --> 01:30:52,640 receiving that code and so now the 40023 01:30:51,000 --> 01:30:54,800 adversary if they want to get into your 40024 01:30:52,640 --> 01:30:56,800 account not only have to guess or brute 40025 01:30:54,800 --> 01:30:58,760 force or maybe read off of a Post-It 40026 01:30:56,800 --> 01:31:00,832 note your password they also have to 40027 01:30:58,760 --> 01:31:02,872 physically have access now to that phone 40028 01:31:00,832 --> 01:31:04,800 so there's still a threat absolutely but 40029 01:31:02,872 --> 01:31:06,912 it's not everyone on the internet with 40030 01:31:04,800 --> 01:31:08,640 an internet connection now it's only the 40031 01:31:06,912 --> 01:31:10,440 people in Starbucks now it's only the 40032 01:31:08,640 --> 01:31:12,600 people at work now it's only the people 40033 01:31:10,440 --> 01:31:14,160 in your home who might have access to 40034 01:31:12,600 --> 01:31:16,232 that second Factor so there too it just 40035 01:31:14,160 --> 01:31:17,832 raises the bar to the adversary making 40036 01:31:16,232 --> 01:31:19,552 it harder more timec consuming more 40037 01:31:17,832 --> 01:31:21,552 geographically impossible for them to 40038 01:31:19,552 --> 01:31:23,400 attack you but what's the downside of 40039 01:31:21,552 --> 01:31:26,512 two-factor authentication whether it's a 40040 01:31:23,400 --> 01:31:27,832 device or even nowadays it's in software 40041 01:31:26,512 --> 01:31:29,320 whether it's on your keychain or on your 40042 01:31:27,832 --> 01:31:31,400 phone where you're prompted for this 40043 01:31:29,320 --> 01:31:33,320 code what's a downside as some of us 40044 01:31:31,400 --> 01:31:36,512 have probably experienced 40045 01:31:33,320 --> 01:31:38,360 too forg you forget your cell phone 40046 01:31:36,512 --> 01:31:40,192 absolutely right the the factor that you 40047 01:31:38,360 --> 01:31:41,360 have you don't have with you or maybe 40048 01:31:40,192 --> 01:31:42,872 you're in a basement somewhere don't 40049 01:31:41,360 --> 01:31:45,192 have reception you're on a plane you 40050 01:31:42,872 --> 01:31:46,960 can't get the code and so there too are 40051 01:31:45,192 --> 01:31:48,320 these tradeoffs and even it departments 40052 01:31:46,960 --> 01:31:50,160 need to keep that in mind because what 40053 01:31:48,320 --> 01:31:51,720 does that mean for them well if you 40054 01:31:50,160 --> 01:31:53,480 don't have your phone with you and you 40055 01:31:51,720 --> 01:31:55,640 are in the habit of calling it to help 40056 01:31:53,480 --> 01:31:57,640 you fix this now there's a a cost a 40057 01:31:55,640 --> 01:32:00,160 human cost maybe even a financial cost 40058 01:31:57,640 --> 01:32:02,192 and so it policy nowadays is really just 40059 01:32:00,160 --> 01:32:03,912 about finding the right balance and 40060 01:32:02,192 --> 01:32:07,120 where we want to spend our resources but 40061 01:32:03,912 --> 01:32:09,760 at least raise the bar to the adversary 40062 01:32:07,120 --> 01:32:11,280 but of course there's other ways too and 40063 01:32:09,760 --> 01:32:12,960 this is going to be one of our homework 40064 01:32:11,280 --> 01:32:14,360 assignments if you will after today 40065 01:32:12,960 --> 01:32:16,600 there's this software called password 40066 01:32:14,360 --> 01:32:18,512 managers and no need to buzz in on your 40067 01:32:16,600 --> 01:32:22,512 phone but maybe with a physical hand how 40068 01:32:18,512 --> 01:32:25,120 many folks here use a password manager 40069 01:32:22,512 --> 01:32:27,120 okay let me ballpark this at 10 20% 40070 01:32:25,120 --> 01:32:29,160 perhaps Okay so we've got 80% upside 40071 01:32:27,120 --> 01:32:31,040 here and a lesson learned potentially so 40072 01:32:29,160 --> 01:32:32,872 a password manager is just a piece of 40073 01:32:31,040 --> 01:32:35,080 software on your Mac your PC or your 40074 01:32:32,872 --> 01:32:36,680 phone nowadays that manages your 40075 01:32:35,080 --> 01:32:38,440 passwords well what is that mean when 40076 01:32:36,680 --> 01:32:39,872 you go to a website for the first time 40077 01:32:38,440 --> 01:32:41,552 or you download an app for the first 40078 01:32:39,872 --> 01:32:43,480 time and you have to create an account 40079 01:32:41,552 --> 01:32:45,552 you can can still use your email address 40080 01:32:43,480 --> 01:32:46,960 or David as your username or whatever 40081 01:32:45,552 --> 01:32:49,080 your name might be so you don't have to 40082 01:32:46,960 --> 01:32:52,480 change that methodology but instead of 40083 01:32:49,080 --> 01:32:54,832 typing in one 2 3 4 5 six as your same 40084 01:32:52,480 --> 01:32:57,000 password for that website or app as well 40085 01:32:54,832 --> 01:32:59,232 as for every other now you use the 40086 01:32:57,000 --> 01:33:01,720 password manager software to generate 40087 01:32:59,232 --> 01:33:03,600 something difficult to guess for you 40088 01:33:01,720 --> 01:33:06,080 that is you tell the password manager 40089 01:33:03,600 --> 01:33:08,640 give me an8 character random passcode 40090 01:33:06,080 --> 01:33:10,800 not 0000 but something with punctuation 40091 01:33:08,640 --> 01:33:12,120 with numbers with letters and better yet 40092 01:33:10,800 --> 01:33:15,280 the password manager is the name 40093 01:33:12,120 --> 01:33:16,400 suggests remembers that password for you 40094 01:33:15,280 --> 01:33:17,512 and the next time you go to another 40095 01:33:16,400 --> 01:33:19,160 website you do it again with a 40096 01:33:17,512 --> 01:33:21,280 completely different password maybe same 40097 01:33:19,160 --> 01:33:22,800 username maybe two-factor authentication 40098 01:33:21,280 --> 01:33:24,320 but different password different 40099 01:33:22,800 --> 01:33:25,680 password different password and it 40100 01:33:24,320 --> 01:33:28,440 doesn't have to be eight I mean I'm in 40101 01:33:25,680 --> 01:33:30,120 the habit of using a dozen two dozen 40102 01:33:28,440 --> 01:33:31,872 characters in total and at that point I 40103 01:33:30,120 --> 01:33:33,800 can't even pronounce the number of 40104 01:33:31,872 --> 01:33:36,232 possibilities because it goes well be 40105 01:33:33,800 --> 01:33:37,640 Beyond uh the quadrillions so the 40106 01:33:36,232 --> 01:33:39,800 probability that someone's going to get 40107 01:33:37,640 --> 01:33:41,512 into one of those accounts for me now is 40108 01:33:39,800 --> 01:33:43,800 very very very low and they're going to 40109 01:33:41,512 --> 01:33:45,280 take less interest in me and maybe more 40110 01:33:43,800 --> 01:33:47,080 interest in someone else that's not 40111 01:33:45,280 --> 01:33:48,720 using as good of a password now what 40112 01:33:47,080 --> 01:33:51,552 does this mean in real terms well when 40113 01:33:48,720 --> 01:33:53,960 you go to log into that managed site you 40114 01:33:51,552 --> 01:33:55,720 don't manually type your password 40115 01:33:53,960 --> 01:33:57,280 anymore in fact you don't generally even 40116 01:33:55,720 --> 01:34:01,120 need to know it nowadays I probably 40117 01:33:57,280 --> 01:34:03,400 don't know 90 plus 99% of my passwords I 40118 01:34:01,120 --> 01:34:05,192 entrust them to this password manager 40119 01:34:03,400 --> 01:34:08,680 now of course you'd like to think that 40120 01:34:05,192 --> 01:34:10,192 the password manager itself is secure so 40121 01:34:08,680 --> 01:34:12,360 what might that mean well those of you 40122 01:34:10,192 --> 01:34:15,080 who do use a password manager how do you 40123 01:34:12,360 --> 01:34:17,600 access that software itself what's 40124 01:34:15,080 --> 01:34:19,832 protecting your data in your 40125 01:34:17,600 --> 01:34:21,512 understanding so maybe Biometrics like 40126 01:34:19,832 --> 01:34:23,232 your your face ID or maybe your 40127 01:34:21,512 --> 01:34:26,120 fingerprint or maybe more simply what 40128 01:34:23,232 --> 01:34:29,080 else password maybe just a password and 40129 01:34:26,120 --> 01:34:31,912 hopefully that password that primary 40130 01:34:29,080 --> 01:34:34,720 password that gatekeeper is not itself 1 40131 01:34:31,912 --> 01:34:36,960 2 3 4 5 6 otherwise it doesn't matter 40132 01:34:34,720 --> 01:34:38,640 how secure all of the others are but if 40133 01:34:36,960 --> 01:34:41,760 you're willing to put in the effort and 40134 01:34:38,640 --> 01:34:43,552 pick one pretty long somewhat random 40135 01:34:41,760 --> 01:34:45,552 very unguessable password that you just 40136 01:34:43,552 --> 01:34:47,120 promise to commit to memory and maybe 40137 01:34:45,552 --> 01:34:48,912 for backup you literally print it out 40138 01:34:47,120 --> 01:34:50,680 and put it in a safe deposit box or a 40139 01:34:48,912 --> 01:34:52,160 safe or just kind of hide it somewhere 40140 01:34:50,680 --> 01:34:53,512 physically that there's very low 40141 01:34:52,160 --> 01:34:55,960 probability someone's going to find the 40142 01:34:53,512 --> 01:34:58,040 backup copy that might be Al loan but of 40143 01:34:55,960 --> 01:35:00,000 course the flip side is now if you 40144 01:34:58,040 --> 01:35:02,080 forget that Ma that primary password 40145 01:35:00,000 --> 01:35:04,160 you've now lost all of the eggs in the 40146 01:35:02,080 --> 01:35:06,160 basket if someone gets that primary 40147 01:35:04,160 --> 01:35:07,832 password now they have access to 40148 01:35:06,160 --> 01:35:10,192 everything so that's rather the 40149 01:35:07,832 --> 01:35:12,600 trade-off but I dare say you're probably 40150 01:35:10,192 --> 01:35:14,800 less threatened depending on your family 40151 01:35:12,600 --> 01:35:17,192 uh uh by the people immediately around 40152 01:35:14,800 --> 01:35:18,720 you than the billions of other people on 40153 01:35:17,192 --> 01:35:20,912 the internet that have access 40154 01:35:18,720 --> 01:35:22,400 potentially to those same systems so 40155 01:35:20,912 --> 01:35:24,480 there to it's a trade-off and it's up to 40156 01:35:22,400 --> 01:35:26,232 you to decide whether or not to manage 40157 01:35:24,480 --> 01:35:28,040 your passwords in this way but if you 40158 01:35:26,232 --> 01:35:30,000 were on that top 10 list or even if 40159 01:35:28,040 --> 01:35:32,232 you're not but you can think of several 40160 01:35:30,000 --> 01:35:33,760 accounts that all have the same password 40161 01:35:32,232 --> 01:35:35,360 you're probably going to benefit from 40162 01:35:33,760 --> 01:35:38,280 something like this and why is it bad to 40163 01:35:35,360 --> 01:35:40,720 be clear to use the same password on 40164 01:35:38,280 --> 01:35:42,760 multiple sites in case that's never sort 40165 01:35:40,720 --> 01:35:45,280 of dawned 40166 01:35:42,760 --> 01:35:47,400 in thought why is that a bad thing to 40167 01:35:45,280 --> 01:35:51,640 reuse a password on different websites 40168 01:35:47,400 --> 01:35:51,640 different apps any intuition yeah and 40169 01:35:52,160 --> 01:35:57,280 back exactly once it's attacked you can 40170 01:35:55,280 --> 01:35:59,720 the adversary presumably by transitivity 40171 01:35:57,280 --> 01:36:01,680 can see oh well if this user's username 40172 01:35:59,720 --> 01:36:03,832 is mailin harvard.edu on this website 40173 01:36:01,680 --> 01:36:06,040 and their password is foolishly 1 2 3 4 40174 01:36:03,832 --> 01:36:07,832 56 or even something way more 40175 01:36:06,040 --> 01:36:09,400 complicated they can probably just 40176 01:36:07,832 --> 01:36:11,760 assume with high probability that if I'm 40177 01:36:09,400 --> 01:36:14,040 being a little reckless let's try access 40178 01:36:11,760 --> 01:36:16,160 mailin at harvard.edu other accounts 40179 01:36:14,040 --> 01:36:17,680 other apps using that exact same 40180 01:36:16,160 --> 01:36:19,480 password and so by transitivity 40181 01:36:17,680 --> 01:36:21,360 essentially you're putting your other uh 40182 01:36:19,480 --> 01:36:23,800 Accounts at risk so what's maybe a 40183 01:36:21,360 --> 01:36:25,280 takeaway minimally here I would start to 40184 01:36:23,800 --> 01:36:26,960 reconsider your passcodes on your most 40185 01:36:25,280 --> 01:36:28,600 important data Maybe it's medical maybe 40186 01:36:26,960 --> 01:36:30,232 it's Financial maybe it's email anything 40187 01:36:28,600 --> 01:36:32,440 remotely personal that you really 40188 01:36:30,232 --> 01:36:34,480 wouldn't want to have access uh do you 40189 01:36:32,440 --> 01:36:37,120 necessarily need the same form level of 40190 01:36:34,480 --> 01:36:38,400 security on eCommerce sites or sites 40191 01:36:37,120 --> 01:36:40,512 that you don't really care about or that 40192 01:36:38,400 --> 01:36:42,600 you signed up for once and after that H 40193 01:36:40,512 --> 01:36:44,760 that's it probably not so you can decide 40194 01:36:42,600 --> 01:36:46,280 for yourself but again software like a 40195 01:36:44,760 --> 01:36:48,912 password manager and these are just some 40196 01:36:46,280 --> 01:36:50,552 of the possibilities out there um are 40197 01:36:48,912 --> 01:36:52,080 probably to be your friend a couple of 40198 01:36:50,552 --> 01:36:53,872 these are free they come with Windows or 40199 01:36:52,080 --> 01:36:56,120 Mac OS a couple are commercial Harvard 40200 01:36:53,872 --> 01:36:57,552 has a site license for students for uh 40201 01:36:56,120 --> 01:36:59,800 one of these as well so there are 40202 01:36:57,552 --> 01:37:01,832 options out there but what else do 40203 01:36:59,800 --> 01:37:03,912 people use what else can people use to 40204 01:37:01,832 --> 01:37:06,000 keep their system secure so most of us 40205 01:37:03,912 --> 01:37:08,000 nowadays have probably heard of 40206 01:37:06,000 --> 01:37:09,600 encryption this technique for just kind 40207 01:37:08,000 --> 01:37:11,640 of scrambling information so when you 40208 01:37:09,600 --> 01:37:14,360 want to send a message an email or 40209 01:37:11,640 --> 01:37:16,280 upload a a photograph or use your credit 40210 01:37:14,360 --> 01:37:17,912 card hopefully it's not just being sent 40211 01:37:16,280 --> 01:37:19,680 out for all to see but there's some kind 40212 01:37:17,912 --> 01:37:21,912 of scrambling going on and some fancy 40213 01:37:19,680 --> 01:37:24,040 mathematics ensure that encryption 40214 01:37:21,912 --> 01:37:25,872 ensures that only you the sender and 40215 01:37:24,040 --> 01:37:28,440 someone else the receiver can 40216 01:37:25,872 --> 01:37:29,912 theoretically see what that credit card 40217 01:37:28,440 --> 01:37:32,160 number is what that message is what that 40218 01:37:29,912 --> 01:37:34,872 photograph is instead so encryption is 40219 01:37:32,160 --> 01:37:37,120 sort of Common Place nowadays both in 40220 01:37:34,872 --> 01:37:38,872 websites and apps and ATMs and other 40221 01:37:37,120 --> 01:37:41,192 such devices but how does it work well 40222 01:37:38,872 --> 01:37:42,720 back in week two of cs50 uh your child 40223 01:37:41,192 --> 01:37:43,912 learned learned a little something about 40224 01:37:42,720 --> 01:37:46,000 encryption otherwise known as 40225 01:37:43,912 --> 01:37:47,872 cryptography and one of the algorithms 40226 01:37:46,000 --> 01:37:50,040 we talked about was quite simply 40227 01:37:47,872 --> 01:37:52,552 something like this this is what we 40228 01:37:50,040 --> 01:37:54,912 might call uh not only cs50 but plain 40229 01:37:52,552 --> 01:37:56,440 text so very plain text that in this 40230 01:37:54,912 --> 01:37:58,232 case is English and obviously everyone 40231 01:37:56,440 --> 01:37:59,680 in the room can read it but what if I 40232 01:37:58,232 --> 01:38:01,232 wanted to send this message out to 40233 01:37:59,680 --> 01:38:03,232 someone in this room or out on the 40234 01:38:01,232 --> 01:38:05,400 Internet or maybe equivalently back in 40235 01:38:03,232 --> 01:38:07,120 the day maybe write a message down on a 40236 01:38:05,400 --> 01:38:08,760 scrap of paper in grade school and pass 40237 01:38:07,120 --> 01:38:11,120 a secret note a secret love note to 40238 01:38:08,760 --> 01:38:13,040 someone in class with hopes that the 40239 01:38:11,120 --> 01:38:14,872 teach or any other students in the class 40240 01:38:13,040 --> 01:38:17,080 can't intercept it and read it well you 40241 01:38:14,872 --> 01:38:19,680 probably don't want to say this is cs50 40242 01:38:17,080 --> 01:38:21,800 or I love you or anything remotely 40243 01:38:19,680 --> 01:38:23,872 sensitive but rather maybe you want to 40244 01:38:21,800 --> 01:38:26,872 encrypt it and let's change the the T to 40245 01:38:23,872 --> 01:38:30,120 a u maybe change the H to an i the I to 40246 01:38:26,872 --> 01:38:32,912 a j the S to a t the I to a j the S to a 40247 01:38:30,120 --> 01:38:34,480 t again the C to a d the S to a T and 40248 01:38:32,912 --> 01:38:36,000 we'll just leave the numbers alone even 40249 01:38:34,480 --> 01:38:38,280 though I worry someone could probably 40250 01:38:36,000 --> 01:38:40,960 guess what this now does say nonetheless 40251 01:38:38,280 --> 01:38:42,600 but what was the algorithm as I rattled 40252 01:38:40,960 --> 01:38:46,232 those ch changes off whether student 40253 01:38:42,600 --> 01:38:49,360 from week two or parent from week now 40254 01:38:46,232 --> 01:38:51,192 yeah a on shift just a onlet shift and 40255 01:38:49,360 --> 01:38:53,960 this is more sophisticatedly called a 40256 01:38:51,192 --> 01:38:55,640 rotational Cipher or a Caesar Cipher 40257 01:38:53,960 --> 01:38:57,512 after Caesar back in the day it's 40258 01:38:55,640 --> 01:38:59,512 relatively simplistic but back in the 40259 01:38:57,512 --> 01:39:01,720 day it's not so simplistic if you're the 40260 01:38:59,512 --> 01:39:03,760 first person in the world to ever use it 40261 01:39:01,720 --> 01:39:05,552 or think of it but nowadays this is not 40262 01:39:03,760 --> 01:39:07,040 actually what we use but it's similarly 40263 01:39:05,552 --> 01:39:09,400 mathematical in nature it's not quite as 40264 01:39:07,040 --> 01:39:12,000 simple as just adding one or subtracting 40265 01:39:09,400 --> 01:39:14,040 one to go from now what we'd call text 40266 01:39:12,000 --> 01:39:15,160 to plain test but it's similarly math 40267 01:39:14,040 --> 01:39:16,960 that's involved and let me just 40268 01:39:15,160 --> 01:39:19,080 stipulate that the way the math works is 40269 01:39:16,960 --> 01:39:21,400 that the sender and the receiver just 40270 01:39:19,080 --> 01:39:22,872 have to have in mind some kind of secret 40271 01:39:21,400 --> 01:39:25,232 and the Secret in this case would very 40272 01:39:22,872 --> 01:39:27,480 trivially be one but it could be a much 40273 01:39:25,232 --> 01:39:29,400 bigger much more unguessable number or 40274 01:39:27,480 --> 01:39:31,320 maybe some other secret we share the 40275 01:39:29,400 --> 01:39:33,160 presumption being that my classmates my 40276 01:39:31,320 --> 01:39:34,512 teacher in that grade school classroom 40277 01:39:33,160 --> 01:39:36,280 if they don't know what that secret is 40278 01:39:34,512 --> 01:39:38,480 that number is yeah they could try to 40279 01:39:36,280 --> 01:39:40,872 brute force it and try all possible 40280 01:39:38,480 --> 01:39:42,120 mathematics plus one plus 2 plus three 40281 01:39:40,872 --> 01:39:43,760 but that's going to take them some time 40282 01:39:42,120 --> 01:39:46,040 and they probably don't care enough and 40283 01:39:43,760 --> 01:39:48,320 so my data might be therefore relatively 40284 01:39:46,040 --> 01:39:50,160 secure but we use encryption all the 40285 01:39:48,320 --> 01:39:52,680 time nowadays and so for instance this 40286 01:39:50,160 --> 01:39:54,640 is at the start of most URLs nowadays 40287 01:39:52,680 --> 01:39:57,192 even if you don't type it yourself with 40288 01:39:54,640 --> 01:39:59,480 that said Safari and even Chrome now or 40289 01:39:57,192 --> 01:40:01,600 kind of simplifying if not dumbing down 40290 01:39:59,480 --> 01:40:03,480 user interfaces to just hide details 40291 01:40:01,600 --> 01:40:06,160 that you and I as sort of normal users 40292 01:40:03,480 --> 01:40:08,080 don't need to see 24/7 but it is there 40293 01:40:06,160 --> 01:40:10,120 and if in fact on your phone or laptop 40294 01:40:08,080 --> 01:40:12,552 you click on the URL even if it's super 40295 01:40:10,120 --> 01:40:14,600 short initially you'll probably see the 40296 01:40:12,552 --> 01:40:16,960 whole thing starting with this and the s 40297 01:40:14,600 --> 01:40:18,600 means secure the s means that encryption 40298 01:40:16,960 --> 01:40:20,800 is being used but there's other forms of 40299 01:40:18,600 --> 01:40:23,160 this not just when you visit websites 40300 01:40:20,800 --> 01:40:25,160 there's this endtoend encryption which 40301 01:40:23,160 --> 01:40:27,192 is being talked about more nowadays 40302 01:40:25,160 --> 01:40:29,120 especially during coid times with so 40303 01:40:27,192 --> 01:40:31,280 many more of us on video and talking 40304 01:40:29,120 --> 01:40:32,912 about uh more sensitive things tele 40305 01:40:31,280 --> 01:40:34,640 medicine talking to doctors things that 40306 01:40:32,912 --> 01:40:36,912 you also wouldn't want to verbally or 40307 01:40:34,640 --> 01:40:39,192 visually get out into the wild just like 40308 01:40:36,912 --> 01:40:42,160 text what's different about endtoend 40309 01:40:39,192 --> 01:40:43,800 encryption versus HTT GPS and the type 40310 01:40:42,160 --> 01:40:45,600 of encryption that most of us use every 40311 01:40:43,800 --> 01:40:48,000 day on websites 40312 01:40:45,600 --> 01:40:49,480 alone end to end encryption is sort of a 40313 01:40:48,000 --> 01:40:52,120 better feature that you want to 40314 01:40:49,480 --> 01:40:55,480 increasingly seek when using services 40315 01:40:52,120 --> 01:40:56,400 like Zoom or Microsoft teams or Whatsapp 40316 01:40:55,480 --> 01:41:02,232 or the 40317 01:40:56,400 --> 01:41:02,232 like any instincts here yeah over on the 40318 01:41:06,280 --> 01:41:09,912 right good so the encryption the 40319 01:41:08,360 --> 01:41:11,960 scrambling of information happens in The 40320 01:41:09,912 --> 01:41:13,800 Source the sender and the destination 40321 01:41:11,960 --> 01:41:15,480 the receiver without a so-called 40322 01:41:13,800 --> 01:41:17,120 middleman in between and this is 40323 01:41:15,480 --> 01:41:19,912 actually very different from most 40324 01:41:17,120 --> 01:41:21,760 contexts nowadays that use just https 40325 01:41:19,912 --> 01:41:23,552 because when you're using https to buy 40326 01:41:21,760 --> 01:41:25,832 something on Amazon securely with your 40327 01:41:23,552 --> 01:41:27,640 credit card well of course Amazon needs 40328 01:41:25,832 --> 01:41:29,480 to be able to decrep the message at the 40329 01:41:27,640 --> 01:41:31,440 end of the day and so that's fine but 40330 01:41:29,480 --> 01:41:33,872 even when you're using services like 40331 01:41:31,440 --> 01:41:35,720 video conferencing or maybe text 40332 01:41:33,872 --> 01:41:37,600 messaging nowadays well if you're using 40333 01:41:35,720 --> 01:41:39,040 Whatsapp that's owned by meta and if 40334 01:41:37,600 --> 01:41:41,600 you're using Instagram that's owned by 40335 01:41:39,040 --> 01:41:43,640 meta there's a lot of middlemen in these 40336 01:41:41,600 --> 01:41:46,040 apps that we're using and if they were 40337 01:41:43,640 --> 01:41:49,040 only using encryption period or only 40338 01:41:46,040 --> 01:41:51,512 using something like https yes your en 40339 01:41:49,040 --> 01:41:53,232 your connection from you to WhatsApp and 40340 01:41:51,512 --> 01:41:56,040 in turn to the recipient might very well 40341 01:41:53,232 --> 01:41:58,360 be secure on each end of that channel 40342 01:41:56,040 --> 01:42:00,080 but meta in the between the company and 40343 01:41:58,360 --> 01:42:01,800 any other company in between could 40344 01:42:00,080 --> 01:42:03,480 theoretically For Better or For Worse be 40345 01:42:01,800 --> 01:42:05,192 looking at that data whether it's to 40346 01:42:03,480 --> 01:42:06,552 mine it for advertising purposes whether 40347 01:42:05,192 --> 01:42:08,360 it's to Snoop on data that you're 40348 01:42:06,552 --> 01:42:10,640 sending that is not end to-end 40349 01:42:08,360 --> 01:42:12,872 encryption if the middleman a company 40350 01:42:10,640 --> 01:42:15,800 typically has technically access to that 40351 01:42:12,872 --> 01:42:17,872 data now zoom and Microsoft teams and 40352 01:42:15,800 --> 01:42:19,512 WhatsApp and iMessage and other services 40353 01:42:17,872 --> 01:42:21,192 with which you're familiar increasingly 40354 01:42:19,512 --> 01:42:23,280 are offering stronger guarantees of 40355 01:42:21,192 --> 01:42:26,040 encryption whereby it's indeed between 40356 01:42:23,280 --> 01:42:27,912 parties A and B and not the one in the 40357 01:42:26,040 --> 01:42:29,440 middle now there's downsides here and 40358 01:42:27,912 --> 01:42:31,120 you can actually see this kind of 40359 01:42:29,440 --> 01:42:34,400 functionality manifest in certain 40360 01:42:31,120 --> 01:42:35,800 settings for instance besides iMessage 40361 01:42:34,400 --> 01:42:38,552 uh which just does this for you on 40362 01:42:35,800 --> 01:42:40,192 iPhones or Macs besides Zoom um you can 40363 01:42:38,552 --> 01:42:41,680 actually fine-tune these settings indeed 40364 01:42:40,192 --> 01:42:43,480 within Zoom itself s so here's a 40365 01:42:41,680 --> 01:42:44,912 screenshot that I took last night of 40366 01:42:43,480 --> 01:42:47,360 just what the user interface looks like 40367 01:42:44,912 --> 01:42:49,400 today to create a new Zoom meeting with 40368 01:42:47,360 --> 01:42:51,120 the latest version of Zoom software and 40369 01:42:49,400 --> 01:42:53,680 maybe unbeknownst to you there's a 40370 01:42:51,120 --> 01:42:56,192 choice of buttons down here and most 40371 01:42:53,680 --> 01:42:58,000 likely yours is by default on enhanced 40372 01:42:56,192 --> 01:42:59,760 encryption which is brilliant marketing 40373 01:42:58,000 --> 01:43:01,600 speak because it's just encryption it's 40374 01:42:59,760 --> 01:43:03,912 not enhanced it actually ironically 40375 01:43:01,600 --> 01:43:06,480 means worse than this um but they want 40376 01:43:03,912 --> 01:43:08,080 you using it most likely why well it's a 40377 01:43:06,480 --> 01:43:10,040 little easier to implement it's a little 40378 01:43:08,080 --> 01:43:12,720 less expensive for them computationally 40379 01:43:10,040 --> 01:43:15,000 and to be fair enhanced encryption does 40380 01:43:12,720 --> 01:43:17,720 scramble the data but not in a way that 40381 01:43:15,000 --> 01:43:19,232 Zoom can't see it Zoom can indeed see it 40382 01:43:17,720 --> 01:43:21,080 but that's actually a plus in some 40383 01:43:19,232 --> 01:43:22,960 context because if you want to do like 40384 01:43:21,080 --> 01:43:24,680 Cloud recordings and you want a meeting 40385 01:43:22,960 --> 01:43:26,360 recorded not on your Mac or PC but like 40386 01:43:24,680 --> 01:43:28,400 let Zoom deal with that if you want 40387 01:43:26,360 --> 01:43:30,280 automatic transcription nowadays so the 40388 01:43:28,400 --> 01:43:31,832 words to appear whether it's English or 40389 01:43:30,280 --> 01:43:33,760 something else on the screen well you 40390 01:43:31,832 --> 01:43:35,552 can't really lock Zoom or any other 40391 01:43:33,760 --> 01:43:37,320 middleman out of that because someone 40392 01:43:35,552 --> 01:43:39,720 needs to save it to the cloud someone 40393 01:43:37,320 --> 01:43:41,832 needs to translate the voice to those 40394 01:43:39,720 --> 01:43:43,320 English or some other language words so 40395 01:43:41,832 --> 01:43:45,640 enhanced encryption enables those 40396 01:43:43,320 --> 01:43:47,232 features but they also allow a bad actor 40397 01:43:45,640 --> 01:43:49,720 malicious employee someone who's just 40398 01:43:47,232 --> 01:43:51,552 nosy at Zoom or the equivalent middleman 40399 01:43:49,720 --> 01:43:53,192 to just kind of poke around your video 40400 01:43:51,552 --> 01:43:55,192 conference and hear what you've said or 40401 01:43:53,192 --> 01:43:58,040 see what you've typed as well unless you 40402 01:43:55,192 --> 01:44:00,440 instead check this box as well so 40403 01:43:58,040 --> 01:44:01,832 increasingly look for mentions of end 40404 01:44:00,440 --> 01:44:03,552 to-end encryption or give that some 40405 01:44:01,832 --> 01:44:05,192 thought when you choose a technology via 40406 01:44:03,552 --> 01:44:07,320 which to communicate with someone 40407 01:44:05,192 --> 01:44:10,912 whether it's within your family or 40408 01:44:07,320 --> 01:44:12,872 without as well now last but not least 40409 01:44:10,912 --> 01:44:14,760 there's other applications of encryption 40410 01:44:12,872 --> 01:44:17,872 too and this too might be a lesson 40411 01:44:14,760 --> 01:44:19,800 learned as well full dis encryption so a 40412 01:44:17,872 --> 01:44:22,040 dis is like where your data stored in 40413 01:44:19,800 --> 01:44:24,040 your Mac or PC or even your phone and 40414 01:44:22,040 --> 01:44:26,080 full dis encryption just means ideally 40415 01:44:24,040 --> 01:44:28,160 that all of your data is encrypted that 40416 01:44:26,080 --> 01:44:30,320 is somehow scrambled now hopefully your 40417 01:44:28,160 --> 01:44:32,832 password for your computer or phone is 40418 01:44:30,320 --> 01:44:34,640 good enough that even though the device 40419 01:44:32,832 --> 01:44:36,080 is encrypted with that password at least 40420 01:44:34,640 --> 01:44:37,832 you'll remember it and your phone or 40421 01:44:36,080 --> 01:44:39,320 your Mac or PC will automatically 40422 01:44:37,832 --> 01:44:40,600 decrypt it for you of course you can't 40423 01:44:39,320 --> 01:44:42,552 scramble the information and hide it 40424 01:44:40,600 --> 01:44:44,640 from from ourselves one of us at least 40425 01:44:42,552 --> 01:44:46,480 for these devices needs to have access 40426 01:44:44,640 --> 01:44:48,000 but full dis encryption typically means 40427 01:44:46,480 --> 01:44:50,552 that at least when you close the laptop 40428 01:44:48,000 --> 01:44:52,800 lid or power down for the night that 40429 01:44:50,552 --> 01:44:55,280 even if someone else steals that device 40430 01:44:52,800 --> 01:44:57,600 opens the lid unless they don't unless 40431 01:44:55,280 --> 01:44:59,680 they have your passcode they can't even 40432 01:44:57,600 --> 01:45:01,912 plug in fancy cables to the device and 40433 01:44:59,680 --> 01:45:03,872 just rip the zeros and ones off of the 40434 01:45:01,912 --> 01:45:05,552 device and see what's actually there 40435 01:45:03,872 --> 01:45:07,360 full dis encryption means they could do 40436 01:45:05,552 --> 01:45:09,512 that but they would just see seemingly 40437 01:45:07,360 --> 01:45:11,360 random zeros and ones now there's a 40438 01:45:09,512 --> 01:45:13,080 downside here too this might slow things 40439 01:45:11,360 --> 01:45:15,000 down potentially but it is a feature 40440 01:45:13,080 --> 01:45:16,640 increasingly that's offered and is 40441 01:45:15,000 --> 01:45:18,960 absolutely something you should consider 40442 01:45:16,640 --> 01:45:20,640 enabling um in general especially if 40443 01:45:18,960 --> 01:45:22,832 your laptop or phone travels with you 40444 01:45:20,640 --> 01:45:25,720 and certainly your phone does or if you 40445 01:45:22,832 --> 01:45:27,192 plan to donate or sell or give away a 40446 01:45:25,720 --> 01:45:28,960 device you don't want to leave all of 40447 01:45:27,192 --> 01:45:30,912 the zeros and ones the remnants of your 40448 01:45:28,960 --> 01:45:32,912 own sensitive data passed on there so 40449 01:45:30,912 --> 01:45:34,800 Windows has a feature called bit Locker 40450 01:45:32,912 --> 01:45:36,640 Mac OS has a feature called file Vault 40451 01:45:34,800 --> 01:45:38,800 there's commercial options as well but 40452 01:45:36,640 --> 01:45:40,720 generally we're at the point now in 2022 40453 01:45:38,800 --> 01:45:43,232 we're clicking a button is suff 40454 01:45:40,720 --> 01:45:45,040 efficient to enable these features with 40455 01:45:43,232 --> 01:45:47,000 that said don't rush into all of these 40456 01:45:45,040 --> 01:45:49,360 decisions I would make backups of your 40457 01:45:47,000 --> 01:45:51,000 data and don't maybe email cs50 if 40458 01:45:49,360 --> 01:45:52,640 something goes wrong with that process 40459 01:45:51,000 --> 01:45:55,000 but I would do your own due diligence 40460 01:45:52,640 --> 01:45:57,600 but this too would be a menu of 40461 01:45:55,000 --> 01:46:00,360 possibilities and now the bad side the 40462 01:45:57,600 --> 01:46:02,160 downside of what seems to be great this 40463 01:46:00,360 --> 01:46:04,800 notion of full dis encryption 40464 01:46:02,160 --> 01:46:06,912 unfortunately just as we can encrypt our 40465 01:46:04,800 --> 01:46:08,832 data to protect it from the adversaries 40466 01:46:06,912 --> 01:46:12,320 so can the adversaries if they get into 40467 01:46:08,832 --> 01:46:14,872 our devices encrypt our data and do what 40468 01:46:12,320 --> 01:46:16,440 not tell us that secret key and so this 40469 01:46:14,872 --> 01:46:17,912 is generally applied in the context of 40470 01:46:16,440 --> 01:46:20,000 ransomware which tragically you 40471 01:46:17,912 --> 01:46:22,160 increasingly hear about in Hospital 40472 01:46:20,000 --> 01:46:23,680 Systems school systems municipalities 40473 01:46:22,160 --> 01:46:25,160 where systems are getting attacked and 40474 01:46:23,680 --> 01:46:26,760 the data is not just getting stolen 40475 01:46:25,160 --> 01:46:28,680 because what is the adversary typically 40476 01:46:26,760 --> 01:46:31,192 need with like local municipal or even 40477 01:46:28,680 --> 01:46:34,192 Hospital data the value to the adversary 40478 01:46:31,192 --> 01:46:37,080 is encrypting all of the hospital all of 40479 01:46:34,192 --> 01:46:38,912 the mpali data preventing them from 40480 01:46:37,080 --> 01:46:40,552 accessing it if they have no backups or 40481 01:46:38,912 --> 01:46:41,720 the like and so ransomware is liter 40482 01:46:40,552 --> 01:46:43,640 Lally about trying to convince someone 40483 01:46:41,720 --> 01:46:45,680 to pay you money or pay you Bitcoin or 40484 01:46:43,640 --> 01:46:47,680 something like that to give you that 40485 01:46:45,680 --> 01:46:49,120 secret key and this key in this case is 40486 01:46:47,680 --> 01:46:51,360 surely more sophisticated than the 40487 01:46:49,120 --> 01:46:53,080 number one but it's really the same idea 40488 01:46:51,360 --> 01:46:55,280 so here too yet again a trade-off just 40489 01:46:53,080 --> 01:46:57,512 as we sort of invent something for good 40490 01:46:55,280 --> 01:46:59,680 it can also be used for evil and so to 40491 01:46:57,512 --> 01:47:01,480 speak as well but it's really the same 40492 01:46:59,680 --> 01:47:03,400 underlying principles even though we 40493 01:47:01,480 --> 01:47:06,960 keep seeing it and hearing about it in 40494 01:47:03,400 --> 01:47:08,512 these different forms and lastly if only 40495 01:47:06,960 --> 01:47:10,760 because folks are generally familiar but 40496 01:47:08,512 --> 01:47:12,800 don't necessarily know what it is that 40497 01:47:10,760 --> 01:47:14,960 it's doing for them browsers nowadays 40498 01:47:12,800 --> 01:47:16,640 have what's often called incognito mode 40499 01:47:14,960 --> 01:47:18,320 or private mode which has nothing to do 40500 01:47:16,640 --> 01:47:20,872 with encryption but does have to do with 40501 01:47:18,320 --> 01:47:23,512 cyber security or really cyber privacy 40502 01:47:20,872 --> 01:47:25,232 keeping your data from prying eyes uh 40503 01:47:23,512 --> 01:47:26,552 incognito mode if you open it in Chrome 40504 01:47:25,232 --> 01:47:28,232 for instance looks a little something 40505 01:47:26,552 --> 01:47:30,080 like this and we use it in cs50 when 40506 01:47:28,232 --> 01:47:32,552 introducing students as we did last week 40507 01:47:30,080 --> 01:47:34,232 to web programming because it in effect 40508 01:47:32,552 --> 01:47:36,080 lets you start with a clean slate like a 40509 01:47:34,232 --> 01:47:37,552 brand new browser that has never visited 40510 01:47:36,080 --> 01:47:39,512 any websites before which is good for 40511 01:47:37,552 --> 01:47:41,512 just diagnosing problems but it's often 40512 01:47:39,512 --> 01:47:42,960 commonly used if you want to log into 40513 01:47:41,512 --> 01:47:44,400 maybe your Gmail account on someone 40514 01:47:42,960 --> 01:47:45,872 else's computer and you don't want your 40515 01:47:44,400 --> 01:47:47,120 password being saved or you want to 40516 01:47:45,872 --> 01:47:48,960 visit some website where you don't want 40517 01:47:47,120 --> 01:47:50,960 the URL or the Search terms ending up in 40518 01:47:48,960 --> 01:47:53,440 your autocomplete history so there's 40519 01:47:50,960 --> 01:47:55,600 multiple uses for incognito mode but 40520 01:47:53,440 --> 01:47:57,720 what does it really do well it doesn't 40521 01:47:55,600 --> 01:47:59,360 stop your company it doesn't stop your 40522 01:47:57,720 --> 01:48:01,360 University your internet service 40523 01:47:59,360 --> 01:48:03,720 provider be it Comcast Verizon or the 40524 01:48:01,360 --> 01:48:05,832 like from knowing what websites you go 40525 01:48:03,720 --> 01:48:07,480 to because ask your student a couple 40526 01:48:05,832 --> 01:48:08,832 weeks ago we talked about actually a 40527 01:48:07,480 --> 01:48:10,640 week ago we talked about how the 40528 01:48:08,832 --> 01:48:12,192 internet works and unfortunately 40529 01:48:10,640 --> 01:48:13,872 computer has an IP address which is 40530 01:48:12,192 --> 01:48:16,720 unique identifier which goes out anytime 40531 01:48:13,872 --> 01:48:18,232 you go anywhere incognito mode or not so 40532 01:48:16,720 --> 01:48:20,400 this isn't really covering your tracks 40533 01:48:18,232 --> 01:48:22,512 outside of your office or outside of 40534 01:48:20,400 --> 01:48:24,320 your home or outside of your company but 40535 01:48:22,512 --> 01:48:26,120 it is at least throwing away local 40536 01:48:24,320 --> 01:48:28,832 information and so we'll talk in fact in 40537 01:48:26,120 --> 01:48:31,192 cs50 is week nine this coming Monday 40538 01:48:28,832 --> 01:48:32,912 about cookies which you might generally 40539 01:48:31,192 --> 01:48:35,280 know about and what are called sessions 40540 01:48:32,912 --> 01:48:37,280 and so long story short what incognito 40541 01:48:35,280 --> 01:48:39,400 mode does is it throws away when you 40542 01:48:37,280 --> 01:48:41,080 close the window any locally stored 40543 01:48:39,400 --> 01:48:42,600 information to the these things called 40544 01:48:41,080 --> 01:48:43,912 cookies which are sort of like virtual 40545 01:48:42,600 --> 01:48:45,760 handstamps that just remember what 40546 01:48:43,912 --> 01:48:48,120 you've logged in as or what's in your 40547 01:48:45,760 --> 01:48:50,192 shopping cart or the like but it doesn't 40548 01:48:48,120 --> 01:48:52,232 hide any information from anyone outside 40549 01:48:50,192 --> 01:48:54,552 of your own Mac or PC it only prevents 40550 01:48:52,232 --> 01:48:56,000 those local prying eyes so there too 40551 01:48:54,552 --> 01:48:57,760 even though we have tools that many of 40552 01:48:56,000 --> 01:49:00,160 you are probably in the habit of using 40553 01:48:57,760 --> 01:49:01,912 or thinking you should use to um be more 40554 01:49:00,160 --> 01:49:04,320 private be more secure on the internet 40555 01:49:01,912 --> 01:49:07,040 what we do really in cs50 boast week's 40556 01:49:04,320 --> 01:49:08,512 uh past and future is talk about how 40557 01:49:07,040 --> 01:49:09,912 these Technologies work so that 40558 01:49:08,512 --> 01:49:12,552 ultimately we have all the more of an 40559 01:49:09,912 --> 01:49:14,960 educated citizenry um here among 40560 01:49:12,552 --> 01:49:16,400 undergrads and here as well as on line 40561 01:49:14,960 --> 01:49:18,160 so that you can apply these same Lessons 40562 01:49:16,400 --> 01:49:20,800 Learned to problems you'll encounter in 40563 01:49:18,160 --> 01:49:23,192 the future so as promised the homework 40564 01:49:20,800 --> 01:49:24,640 one should probably use a password 40565 01:49:23,192 --> 01:49:26,160 manager doesn't have to be one of those 40566 01:49:24,640 --> 01:49:28,320 ones on the list but at least starting 40567 01:49:26,160 --> 01:49:30,320 that conversation maybe with someone who 40568 01:49:28,320 --> 01:49:32,160 does maybe the you know it's often the 40569 01:49:30,320 --> 01:49:33,800 you know the the the student in your 40570 01:49:32,160 --> 01:49:35,280 family perhaps who can advise you on 40571 01:49:33,800 --> 01:49:37,320 some of these Technologies consider 40572 01:49:35,280 --> 01:49:39,080 using a password manager to using 40573 01:49:37,320 --> 01:49:40,600 two-factor authentication whether it's 40574 01:49:39,080 --> 01:49:42,192 your phone or some key fob or or the 40575 01:49:40,600 --> 01:49:43,832 like but at least seeking out that 40576 01:49:42,192 --> 01:49:45,512 feature at least for accounts that you 40577 01:49:43,832 --> 01:49:47,600 really care about your email social 40578 01:49:45,512 --> 01:49:49,832 media Financial medical anything where 40579 01:49:47,600 --> 01:49:51,360 you'd be embarrassed at best or really 40580 01:49:49,832 --> 01:49:52,960 violated at worst if that kind of 40581 01:49:51,360 --> 01:49:54,912 information got out and then 40582 01:49:52,960 --> 01:49:56,640 increasingly using not just encryption 40583 01:49:54,912 --> 01:49:58,912 which you kind of get automatically for 40584 01:49:56,640 --> 01:50:00,640 most Technologies today but increasingly 40585 01:49:58,912 --> 01:50:02,480 choosing technologies that offer 40586 01:50:00,640 --> 01:50:04,512 stronger guarantees that keep those 40587 01:50:02,480 --> 01:50:06,040 middlemen those companies out of the way 40588 01:50:04,512 --> 01:50:08,872 if only so that you can trust with 40589 01:50:06,040 --> 01:50:12,280 higher probability that only party B 40590 01:50:08,872 --> 01:50:13,872 knows what party a has said or sent now 40591 01:50:12,280 --> 01:50:15,680 this of course was a whirlwind tour 40592 01:50:13,872 --> 01:50:17,760 there's so much more that you can do 40593 01:50:15,680 --> 01:50:19,680 online indeed this course cs50 can be 40594 01:50:17,760 --> 01:50:23,120 taken for free online via platforms like 40595 01:50:19,680 --> 01:50:25,832 edex at edx.org cs50 I thought it might 40596 01:50:23,120 --> 01:50:28,832 be appropriate to end on this note if 40597 01:50:25,832 --> 01:50:31,080 anyone would like to conjecture before 40598 01:50:28,832 --> 01:50:34,080 we start playing music and adjourn for 40599 01:50:31,080 --> 01:50:37,080 lunch what our final message here 40600 01:50:34,080 --> 01:50:40,640 is if we reverse the plus one and maybe 40601 01:50:37,080 --> 01:50:43,160 start minus one here minus one here and 40602 01:50:40,640 --> 01:50:44,310 indeed thank you so much for coming this 40603 01:50:43,160 --> 01:50:46,950 was 40604 01:50:44,310 --> 01:50:55,060 [Applause] 40605 01:50:46,950 --> 01:50:55,060 [Music] 40606 01:51:01,040 --> 01:51:12,160 [Music] 40607 01:51:09,160 --> 01:51:12,160 cs503179053

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