0:02 this is a Harvard University course
0:05 taught by Dr David Meen and we are proud
0:07 to bring it to the freeo camp YouTube
0:10 channel throughout a series of lectures
0:12 Dr men will teach you how to think
0:14 algorithmically and solve problems
0:16 efficiently and make sure to check the
0:19 description for a lot of extra resources
0:28 [Music] course
1:34 [Music]
1:38 all right this is cs50 Harvard
1:40 University's introduction to the
1:42 intellectual Enterprises of computer
1:44 science and the Arts of programming my
1:45 name is David man and I actually took
1:49 this course myself back in 1996 I was a
1:50 sophomore at the time I was actually
1:52 concentrating in government because a
1:54 year prior as a first year I'd come into
1:57 Harvard thinking that I liked high stre
1:59 and constitutional law and sort of
2:01 similar classes in high school and so
2:03 when I got here I rather gravitated
2:05 toward that which was familiar I figured
2:07 if I liked and if I were good at that
2:09 particular subject in high school then
2:10 that's presumably who I'm supposed to be
2:13 here but it wasn't until sophomore year
2:15 that I got up the nerve to step foot in
2:17 the cs50 classroom and even then it was
2:19 only out of curiosity like I had no
2:21 intention of studying computer science
2:24 of even taking cs50 when I got to campus
2:26 but people were talking about it and
2:28 there was a lot of like beware and it
2:30 was it perhaps for the initiated only
2:32 and I didn't really know ultimately what
2:35 computer science was but for me the sort
2:37 of light bulb went off and I found that
2:38 contrary to what I had seen in high
2:40 school where I saw friends of mine like
2:42 programming away in the computer lab
2:44 heads down sort of anti-socially just
2:46 doing whatever it was they were doing it
2:48 really wasn't that once I got to this
2:49 particular class in this particular
2:51 place it was much more about problem
2:53 solving more generally and just learning
2:55 how to express yourself in code in
2:56 different languages so that you can
2:57 actually solve problems of interest to
2:59 you even if you have no intention of
3:01 being a computer scientist or an
3:03 engineer but just want to be able to
3:04 solve problems analyze data do
3:07 interesting things in the Arts Humanity
3:09 social sciences physical sciences or
3:11 really any other field and indeed this
3:13 particular path led me to computer
3:14 science but the hope for cs50 more
3:17 generally is that indeed you just find
3:19 your way to applying principles that
3:21 you'll learn over the coming months to
3:23 whatever field is of interest to you
3:25 with that said it was definitely a lot
3:27 of work and not without its frustrations
3:30 for me but there was no better feel
3:31 then like banging your head proverbial
3:33 against the wall for some number of
3:35 hours even days trying to fix a bug a
3:37 mistake in your code and then oh my God
3:40 the rush of emotion of accomplishment of
3:42 pride of exhaustion when you finally
3:43 solve some problem that's really been
3:45 weighing on you it's just so incredibly
3:47 gratifying but also empowering because
3:49 unlike a lot of fields the computer
3:52 science was built by humans themselves
3:54 and so if a human built this surely you
3:56 another human can understand it as well
3:58 and so even though there's going to be
3:59 some distractions along the way you're
4:01 going going to see what looks incredibly
4:02 cryptic if you've never programmed
4:05 before over time and with practice
4:07 everything just starts to make more
4:08 sense and with time and with practice
4:10 you just get better at this particular
4:12 field and indeed really the key to
4:14 success in programming in general is
4:16 just to allow yourself enough time and
4:18 so at least thankfully I quickly got
4:20 into the habit of starting early in the
4:21 week for instance when writing actual
4:23 code why because you're going to like
4:24 run up against a wall you're not going
4:25 to see some bug something's not going to
4:27 jump out at you and that's fine that's
4:29 when you sort of call it a day take a
4:30 break move move on to something else and
4:32 then just come back to it and that's
4:34 what keeps programming fund for me even
4:35 all of these years later whether it's
4:37 teaching or actually applying it but
4:39 there's uh down the road a history of uh
4:41 an MIT hack and it looked a little
4:43 something like this in yester year and
4:44 there was a little sign the MIT students
4:46 when they made this hack uh on the wall
4:48 that says getting an education from MIT
4:51 is like drinking from a fire hose which
4:52 indeed they have connected to what
4:54 should have been otherwise just a water
4:55 fountain and that's going to be what it
4:57 feels like sometimes not just in
4:59 computer science per se but just in UNAM
5:01 familiar field if you're not from stem
5:04 if you're not from CS that's fine but so
5:06 much of it ultimately is going to be
5:08 absorbed by you and going to be uh
5:10 within your grasp by term Zen so just
5:12 keep in mind that's that's very much the
5:14 intent but you'll be amazed what you're
5:16 able to create to accomplish uh just
5:19 three or so months hence indeed 2third
5:21 of you contrary to what you might think
5:24 or assume have never taken a CS class
5:25 before so it's absolutely not the case
5:26 that the person to the left and to the
5:28 right surely must know more than you
5:30 indeed it's quite the opposite and as
5:32 you'll see in the coming weeks as you
5:33 write your own code and solve your own
5:35 problems what ultimately matters in this
5:37 course is not so much where you end up
5:38 relative to your classmates but where
5:41 you end up relative to yourself when you
5:43 began and it really is all about that
5:44 Delta whether you've programmed or not
5:47 just getting something out of a class
5:49 like this and and if it does take time
5:51 and if you do feel those frustrations
5:52 but you simultaneously eventually feel
5:54 that sense of accomplishment that just
5:55 means it's all working and indeed
5:58 hopefully all the more worthwhile and
6:00 gratifying ultimately as a result so
6:02 what are we going to do in the coming
6:04 week so here we are in week zero we'll
6:06 soon see why computers and computer
6:07 scientists start counting if you will
6:10 from zero but week zero is which is one
6:11 in which we explore computational
6:13 thinking sort of thinking like a
6:14 computer and starting to clean up your
6:16 thought processes getting you to think
6:18 to solve problems more methodically and
6:19 then ultimately Translating that into
6:21 code and some of you might recognize
6:23 this environment here AKA scratch
6:25 coincidentally also from MIT you might
6:27 have used it in in grade school we'll
6:29 use it today and a little bit this
6:30 weekend in the course's first homework
6:33 assignment or problem set but not so
6:34 much to kind of play around in a way
6:36 that you might have if you did use it in
6:38 yester year but to explore ideas of
6:39 computer science and programming that
6:42 we're going to use and reuse every week
6:44 Hereafter as well uh thereafter we're
6:46 going to transition just next week to
6:48 week one so to speak whereby we'll
6:49 introduce you to a more traditional
6:51 language a lower level language an older
6:53 language called C and in C you're going
6:55 to use your keyboard not so much your
6:57 mouse and pointing and clicking but
6:58 you're going to write code that soon is
6:59 going to look a little something like
7:01 this and if you programmed before can
7:03 probably glean what this is going to do
7:04 if you've never programmed before which
7:07 is the case for most of you this too
7:08 will soon make sense but this is the
7:10 most canonical program that most any
7:12 programmer ever writes called hello
7:15 world and indeed that and all of the
7:17 surrounding syntax above and below just
7:19 that sentence hello world will soon make
7:20 all the more sense you'll learn how to
7:23 use industry standard tools so to speak
7:24 pictured here something called Visual
7:26 Studio code or vs code you'll use a
7:28 cloud-based version of it initially so
7:29 you don't have to suffer with any
7:31 technical difficulties or headaches like
7:33 that it'll just work right off the bat
7:34 but we'll use that tool and others
7:37 ultimately to then explore ideas in
7:39 computer science principles that you can
7:41 apply and we'll take a look underneath
7:42 the hood so to speak of your computer at
7:45 your memory or Ram Random Access Memory
7:47 where all of the data is ultimately
7:49 going to be stored we'll also take a
7:51 look thereafter at bugs a bug is a
7:53 mistake in a program here is an actual
7:55 bug in an actual computer in yester year
7:57 but we'll teach you how to debug
7:59 programs find your own mistakes find
8:01 others mistakes and improve that code as
8:04 well we'll transition then to algorithm
8:05 step-by-step instructions for solving
8:07 some problems which we'll touch on today
8:10 too and if you picture here this is
8:11 actually a pretty representative
8:12 problems odds are you haven't had to
8:14 deal with something like this but it's
8:16 representative sorting for instance if
8:18 you think of each of these small bars as
8:20 being a small number each of the bigger
8:22 bars as being a bigger number you might
8:24 wonder well how is you could do as a
8:26 human sort all of these bars like get
8:28 all the short bars over here all the big
8:30 bars over there well odds are if you're
8:31 like me you would probably kind of
8:33 eyeball it and if you could physically
8:35 interact you might just start grabbing
8:36 the smallest elements first put them
8:38 over on the left maybe grab the biggest
8:40 elements put them over on the right but
8:42 what's your algorithm there like how
8:43 would you teach someone younger than you
8:45 who's never done that before how to do
8:47 it how would you compel your Mac or PC
8:49 or phone to do something like that you
8:51 can't just kind of wave your hand and
8:52 say oh you know figure it out move
8:54 things around you have to express
8:55 yourself more methodically so we'll
8:57 translate even ideas like this into code
8:59 to and that's what the Googles and
9:01 others of the world are doing constantly
9:02 as they sort and organize the world's
9:05 information we'll use metaphors along
9:06 the way if it helps we'll talk about
9:08 your computer's memory is kind of being
9:10 like a postal address like every mailbox
9:12 in the world has some form of postal
9:14 address Street city state country and
9:16 the like and it turns out that's how
9:18 your Mac your PC and your phone also
9:19 work you've got a whole bunch of memory
9:21 like the picture before but you can
9:22 think of it really as individual
9:24 mailboxes and you can put anything you
9:26 want in those mailboxes and you can go
9:29 to a mailbox to grab information that
9:30 from it so at the end of the day that's
9:32 really all your computer's doing with
9:34 information it's just kind of organizing
9:36 it not into mailboxes per se but a term
9:38 you probably know called bites for
9:40 instance instead we'll talk about
9:43 problems that arise even nowadays in
9:45 fact most of you are familiar with your
9:48 Mac PC even phone like spontaneously
9:50 rebooting sometimes crashing the little
9:52 Annoying spinning beach ball or
9:53 Hourglass Icon that Happ like what is
9:55 with that well those are just bugs in
9:57 programs that humans at Apple and Google
9:59 and Microsoft and others they screwed up
10:01 and they wrote buggy code and your
10:03 computer when it encounters those
10:05 mistakes doesn't know what to do and so
10:07 nine times out of 10 so to speak it just
10:09 crashes or freezes or the like but that
10:10 kind of stuff will make more sense so
10:12 even the real world will make sense and
10:14 pictured here or some lower level terms
10:16 will eventually get to Mid semester but
10:17 generally speaking when something is
10:20 going this way as per this arrow and
10:21 something is going this way as per this
10:23 Arrow like that does not end well and
10:25 that often is what happens when your
10:27 computer crashes someone's using memory
10:28 up here but someone else is using memory
10:30 down here here and then they're not
10:32 really talking left hand and right hand
10:34 so that is just a high level overview of
10:36 some of the problems we'll encounter but
10:38 we'll focus to on data ultimately so
10:39 picture here is something fairly
10:41 technical called a hash table it's an
10:43 amalgam of something we're going to soon
10:45 call an array and also something we call
10:46 a linked list and these are just fancy
10:49 terms for describing how you can
10:51 organize information even more flexibly
10:54 than just putting individual ma values
10:56 in mailboxes like how could you build
10:58 structures like actual data structures
11:00 so to speak speak two-dimensional
11:01 structures at that and so what you're
11:04 seeing here is a glimpse uh as some of
11:05 you might have recognized of some Harry
11:07 Potter Universe names but they're
11:09 organized somewhat alphabetically and
11:10 notice that anytime there's multiple
11:12 people with a name that starts with H
11:14 like hermion Harry and Hagrid well they
11:16 can't all fit in that mailbox if each of
11:18 these squares along the left is that
11:20 same mailbox so you have to kind of
11:21 chain them together well you'll learn
11:23 how to do that in code so that even if
11:26 you get more data than you expect if
11:27 your business is booming and you're some
11:29 web-based business how do you keep
11:31 adding and adding information to your
11:34 software to actually keep up with it but
11:35 this again is what codee's going to soon
11:37 look like as soon as next week in week
11:39 one this here being C but we'll
11:42 transition in a few weeks to a more uh
11:45 modern higher level language so to speak
11:47 called python indeed the course Very
11:48 deliberately back in my day and now this
11:50 introduces you first to C which funny
11:52 enough most many people don't tend to
11:55 program and certainly every day I you
11:57 see generally September October November
11:59 December when teaching C is 50 itself
12:02 but it's everywhere nonetheless in fact
12:03 even today's other languages with which
12:06 you might be familiar like Python and
12:09 Java and uh yet others still you see
12:11 this same primitive language underneath
12:13 the hood because it's so darn fast and
12:15 as you'll learn over the coming weeks it
12:17 really gives you access to and an
12:18 understanding of what's going on
12:20 conceptually down here so that
12:22 thereafter after cs50 when you're
12:23 writing code you can sort of think at a
12:26 very high level what's actually going on
12:28 so in fact in just a few weeks what
12:30 looks like this C is going to look
12:32 instead like this in Python and you'll
12:33 better understand what's going on
12:35 underneath the hood and odds are after
12:37 this class you'll reach for a language
12:40 like python more frequently than C but
12:41 you're going to benefit from that bottom
12:44 up understanding thereof thereafter and
12:46 toward terms end we'll introduce you to
12:48 a few other ideas like where do you put
12:50 large amounts of data in things called
12:52 databases not things like spreadsheets
12:53 like here but actual databases where
12:55 using those same kinds of data
12:56 structures you lay things out in an
12:58 interesting way in memory thereafter
13:00 will transition to a very familiar
13:02 environment that you and I use every day
13:04 the web like the web has become rather
13:06 the user interface or UI that we use
13:08 everywhere on the uh laptops desktops
13:10 and even mobile devices nowadays well
13:13 pictured here is a language called HTML
13:14 it's not a programming language it's a
13:16 markup language and some of you might
13:18 have made homepages or portfolios in the
13:20 past but you'll understand what's going
13:22 on here but more powerfully you'll
13:24 understand how the computer sees that
13:26 same kind of code builds up a
13:29 hierarchical family tree type structure
13:31 in memory and then you can manipulate
13:33 that tree with code to actually add more
13:35 and more information chat messages
13:37 anything on the screen that you like and
13:40 finally we'll tie all of this together
13:41 by introducing what are called
13:44 Frameworks and libraries third-party
13:46 code that makes it a lot easier to solve
13:48 problems of interest to you and so in
13:50 particular here this is the very first
13:53 web app that I myself made back in like
13:56 1997 I was part of the first year
13:58 intramural sports program not as an
14:00 athlete but as the programmer and I was
14:01 teaching myself how to build web
14:03 applications I only knew C and maybe a
14:05 little bit of something else at the time
14:07 but this became for Harvard at least the
14:09 very first website for the first year uh
14:11 intramural sports program and it wasn't
14:13 just a static website with links and
14:15 images and the like it was interactive
14:16 you could register for sports we could
14:18 input exactly who is in a tournament
14:20 bracket or the leg and it could actually
14:22 automatically keep track of this data so
14:24 there two after just three months of a
14:26 class like this you'll go from writing
14:28 quite simply this week in next hello
14:30 world to building things like this for
14:32 whether it's web mobile uh or other
14:34 platforms as well if you so choose but
14:36 we'll get you off of the courses
14:37 infrastructure by the end of the term
14:39 you won't be using any toy environments
14:41 along the way will Empower you
14:43 ultimately to write code after cs50
14:45 especially if this is the only CS class
14:48 you ever take on your own Mac or PC
14:49 using the same software but not the
14:51 cloud-based version thereof but all of
14:53 this software is itself free and can be
14:56 used by you uh powerfully after the
14:58 course's own end but along the way as
15:02 you may know uh is there this tradition
15:04 within the class particularly in healthy
15:05 times of a number of events that really
15:07 brings people get together not just
15:09 collaboratively academically but to just
15:12 solve problems and generally uh engage
15:13 with each other as well coming up first
15:16 cs50 puzzle day which is meant to be not
15:18 jigsaw puzzles but logic puzzles that
15:19 require no prior experience with
15:21 computer science or programming but it's
15:23 just an opportunity to sort of quietly
15:25 work on a packet of puzzles with some
15:27 number of friends for for prizes and
15:30 more later the semester once you tackle
15:32 your final projects the Capstone of the
15:33 course where we don't give you a
15:35 homework to write you yourself come up
15:37 with something to build we'll get
15:39 together generally around 7 a uh 700
15:42 p.m. in the evening uh wrap up around 7
15:44 a.m. if you so choose and it's an
15:46 evening a 12-hour opportunity to
15:48 collaborate with classmates on your very
15:50 own final project in a large space on
15:52 campus uh that ends if you're awake with
15:55 us at 5:00 a.m. uh we can hop on some
15:56 cs50 shuttles and go down the road for
16:00 some pancakes at IHOP around 6 uh of
16:03 course uh of course this is 6 7 a.m. at
16:05 that point but it's an opportunity
16:06 finally to lead into What's called the
16:09 cs50 fair which is an end of semester
16:11 celebration an exhibition of everything
16:12 that you'll accomplish over the coming
16:14 months and in fact pictured here are
16:16 some of your predecessors and healthy
16:18 times um the cs50 fair allows you to
16:20 come with your laptop or phone and
16:22 exhibit a students faculty and staff
16:24 across campus put together something in
16:25 person and on video that people can
16:28 Delight in seeing as you exhibit what it
16:29 is you create in what you learned over
16:31 the course of the several weeks and
16:33 ultimately a chance to just share and
16:36 Inspire others as well um and you'll all
16:38 walk home ultimately with your own I to
16:42 cs50 t-shirt saying as much as well so
16:44 with that highlevel overview of the
16:46 course I propose that we begin to take a
16:48 look at what computer science itself is
16:50 and what it is we're going to be doing
16:52 over the next several weeks at this
16:54 lower level than two so what is computer
16:56 science right if you're maybe like me or
16:58 new people like my friends and High
17:00 School you probably assume that it means
17:02 programming and that's absolutely a big
17:04 part of it for a lot of people because
17:06 with code you can write you can express
17:07 ideas and solve actual problems
17:09 especially involving data but computer
17:11 science itself is really the study of
17:13 information if you will how do you
17:15 represent it and how do you actually
17:16 process it and in that sense
17:18 computational thinking is just the
17:21 application of ideas from computer
17:23 science a course like this to problems
17:25 of interest to you again in the Arts
17:27 Humanities Sciences social sciences
17:30 whatever the domain interest is to you
17:32 so with that if computer science is all
17:34 about information and with it the
17:36 solving of problems well what does it
17:37 actually mean to solve a problem let's
17:39 see if we can't propose a model into
17:42 which all of the Lessons Learned will
17:43 ultimately follow and I'd propose that
17:46 this is problem solving you've got some
17:48 input which is like the problem you want
17:50 to solve the goal is to solve it so
17:51 that's the so-called output and then
17:53 somewhere in here the proverbial black
17:56 box is some kind of Secret Sauce that
17:57 gets the work done and in the coming
17:59 months have to decide well how are we
18:01 going to represent these inputs and
18:03 outputs and really how do we code up how
18:05 do we write solutions for what it is
18:07 that's solving the problem of interest
18:09 to us so when it comes to representation
18:11 of information like there's a lot of
18:13 ways we can do this and for instance if
18:14 the problem at hand quite simply is to
18:16 like take attendance at the beginning of
18:18 class on the first day of school well
18:20 how could we go about doing this well we
18:23 could actually use a system called unary
18:24 well what is that well that's a fancy
18:29 way of saying 1 2 3 4 5 maybe 6 7 8 9 10
18:31 and I can use my digits pun intended on
18:34 my fingers to actually count everyone up
18:35 and eventually you need toes and whatnot
18:38 if you have to count so high but unary
18:40 is a very simple system of using a
18:42 single symbol a human finger in this
18:44 case to just solve some problem like
18:45 counting the number of people in the
18:46 room let's make this slightly more
18:48 technical for a moment a little more
18:50 mathy that's just called base one where
18:52 the base under which you're operating
18:54 has one digit in it like literally a
18:56 human finger and maybe multiple such
18:58 fingers if you need to count higher but
19:00 of course of course all most of you if
19:02 not all of you generally vaguely know
19:04 that computers use something other than
19:07 unary and even you and I probably don't
19:08 use this that often they use what
19:10 language or alphabet
19:14 instead yeah so binary so binary is
19:16 indeed the system that computers somehow
19:19 use so in this case by implying two and
19:21 so computers have two digits it turns
19:23 out at their dispos one in fact if
19:25 you've ever heard the technical term bit
19:27 which is like a smaller version of a
19:29 bite more on that soon well a binary
19:32 digit is the origin of that term bit
19:33 because if you get rid of some of the
19:34 letters and you're left from binary
19:37 digit with just bit thus is a bit a bit
19:39 is just a zero and one it's two more
19:41 digits than you might have on your own
19:43 finger and of course it's fewer though
19:44 than you and I have you and I typically
19:47 use as humans the decimal system deck
19:48 meaning 10 because you and I generally
19:51 use zero through nine so on the one hand
19:54 another pun intended you've got unary
19:56 computers use binary we humans generally
19:58 think and talk in terms of De
20:00 but at the end of the day these are
20:02 fundamentally going to be the same thing
20:04 which is to say that it's all pretty
20:05 accessible to us even if you're not a
20:07 computer person I dare say you're about
20:10 to be so what is a bit well a bit then
20:13 is a zero or a one that is a so-called
20:15 binary digit but how do computers only
20:17 speak in binary how do they solve
20:19 problems represent information using
20:21 only binary well at the end of the day
20:23 if they want to represent zero and one
20:26 we need to do so physically somehow and
20:28 I dare say that maybe the simplest way
20:31 to think about a bit a zero or one is
20:32 like a light bulb and so by human
20:34 convention let's just assume that if you
20:37 are a computer be it a laptop desktop
20:38 phone or the like and you want to
20:41 represent the number zero you know what
20:43 you just keep a light switch off you
20:46 keep a light bulb off if by contrast
20:47 you're that same computer and you want
20:49 to represent the number one you take
20:50 that same switch that same light bulb
20:53 and just turn it on so a light bulb
20:54 that's on represents a one and a light
20:57 bulb that's off represents a zero so why
20:58 is this relevant into computers well at
21:00 the end of the day you and I are
21:02 charging our laptops or phones at night
21:03 so there's some like physical resource
21:05 being replenished there whether you're
21:07 on battery or some power cord and so a
21:09 inside of a computer are just thousands
21:12 millions of tiny little switches
21:13 nowadays you can think of them
21:14 metaphorically as light bulbs but they
21:16 don't yet it's actually shine light but
21:18 there are tiny tiny little switches and
21:20 those switches if you've ever heard the
21:22 term are just called transistors so like
21:24 computers have millions of transistors
21:26 that can either be flipped on to
21:28 represent ones or flipped off to
21:30 represent zeros and from that very
21:33 simple mechanism electricity is there or
21:37 it's not a one or a zero computers can
21:39 actually count obviously from zero to
21:41 one but it turns out even higher if they
21:44 use a little more electricity as well so
21:45 how might I do this well let me go ahead
21:47 and propose that I just grab one of our
21:49 own light bulbs here on stage this one's
21:51 off so for instance If This Were
21:53 miniaturized inside of your Mac PC or
21:54 phone this would be a transistor and
21:56 indeed here's the little switch on the
21:58 bottom and if your computer wants to
21:59 represent a zero it just leaves the
22:01 switch off and the light is not shining
22:03 if you want to represent a one well now
22:05 I've counted as high as one because the
22:07 switch is now on I've grabbed a little
22:08 electricity I'm holding on to it inside
22:10 of the computer and so now I see that
22:12 this is a one all right but
22:13 unfortunately with just one switch one
22:16 light bulb I can only count from zero to
22:19 one how do I count out higher might you think
22:20 think
22:23 intuitively say again yeah so more light
22:25 bulbs so let me do this let me just put
22:27 grab something to put these on so I can
22:29 use a few of them at a time and let me
22:31 propose that here instead of having just
22:33 one light bulb let me give myself maybe
22:35 three in total so all of them are
22:37 initially off and if you think of this
22:39 in miniature form in your mind's eye
22:40 this is like a computer with three
22:43 transistors three switches representing
22:45 now the number you and I know as zero
22:48 why they're just all off so how does a
22:50 computer go about representing the
22:53 number one well it turns on one of these
22:55 light bulbs and how does the computer
22:57 represent the number two well you might
22:59 think if I by may you just turn on a
23:01 second light bul and if you might think
23:03 how does a computer represent three you
23:05 just turn on the third light bulb and so
23:08 as such with three bits a computer would
23:09 seem to be able to count from zero on up
23:12 to 1 two three but it turns out if I'm a
23:15 little smarter here I can actually count
23:17 higher than that why well I'm just
23:19 considering like the combination of
23:20 bulbs being on here what if I do
23:22 something like this this is still zero I
23:24 will claim but what if I propose now
23:26 that this will be how a computer
23:31 represents one on on off off this though
23:33 will be how the computer represents two
23:35 notice I didn't turn on the same two I'm
23:37 just turning on the one in the middle
23:39 this I now claim will be how a computer
23:42 represents three this is going to be in
23:44 just a second how a computer represents
23:48 the number we know as four and yet I'm
23:50 still only using three bulbs this is
23:51 going to be the number the computer
23:54 represents as five this is going to be
23:56 how the computer represents the number
23:59 six and then lastly it turns out with
24:00 three light bulbs if you're smart about
24:02 it you can count it seems as high as
24:05 seven now even if you kind of lost track
24:07 of like what I was turning on and Y I
24:08 claimed there were eight different
24:10 patterns from all of them off to all of
24:12 them on but notice that I started to
24:14 permute them I took into account which
24:16 ones were on and which ones were off why
24:18 though do these represent the numbers we
24:21 know as 0 through 7even well let me go
24:23 ahead and maybe let's let's do this
24:24 instead of just considering there to be
24:27 light bulbs let's assign some special
24:29 significance to each of them based on
24:31 where it is and maybe for this could we
24:35 get uh maybe three volunteers three
24:37 volunteer okay yeah who's you're being
24:39 volunteered okay come on up if you want
24:40 to go over to the stage there yeah you
24:42 want to come on up as well and over here
24:44 as well so there's some stairs on either
24:46 end maybe a round of applause for our
24:56 term all right so you want to be you are
24:58 number one and if you want want to go
25:00 ahead and stand roughly right here how
25:02 about you want to be number two okay
25:03 come on over and right uh to the right
25:06 of here and you'll be number four it
25:07 turns out and if you want to come over
25:10 here on this end let's give you all a
25:12 moment to uh introduce yourselves uh
25:14 briefly to your classmates if you'd like
25:18 hi I'm Ellie I'm a senior nice to meet
25:22 I'm rahana and I'm a first year welcome
25:25 hi I'm Joseph and I am a first year
25:27 welcome all right so so glad to have all
25:34 you let me propose now that we'd like
25:36 you you three to represent how about the
25:38 number zero and I claim now that if each
25:41 of you now represents a switch you have
25:43 sort of fancier light bulbs now one is a
25:44 one one is a two one is a four but each
25:46 of you still just has a switch on the
25:48 bottom in fact of your plastic devices I
25:50 claim these three volunteers are
25:51 representing the number zero let me ask
25:53 you all now how might you represent the
25:57 number one how should you cooperate here
25:59 okay so so we would have on off off
26:01 which I think is matches what I did with
26:02 my three light bulbs as well if you want
26:03 to go ahead and turn yours off how might
26:06 you three represent the number
26:11 two okay so off on off now from right to
26:14 left how would you3 represent the number
26:16 three ah so that's why my two light
26:18 bulbs went on at the end how would you
26:25 four perfect number
26:28 five number six
26:30 and number
26:32 seven all right and give us one more how
26:35 would you represent
26:39 eight okay you can't uh how about then
26:41 one more volunteer one more volunteer
26:48 up all right what's your name my name is
26:49 Moen if you want to say it there my name
26:51 is Moen all right Moen you're GNA be
26:53 number eight and if now you all actually
26:54 let's make this uh how would you
26:57 represent number eight all collectively
27:00 as four bits or four
27:02 switches okay eight and now lastly give me
27:08 15 everyone's awkwardly doing arithmetic
27:15 unar hey yeah is
27:19 everyone yes okay Round of Applause okay
27:20 thank you
27:24 all if you want to leave your numbers
27:26 over here we have a 650 stress ball for
27:28 you but thank you for volunteer you can
27:29 turn those numbers off and leave them
27:31 over here so thank
27:34 you so how do we go about how do we go
27:40 from there to uh creating these patterns
27:43 well even though we still had three bits
27:46 initially and three switches later four
27:48 bits and four switches ultimately we
27:50 still used it used the same approach
27:53 fundamentally to actually representing
27:55 information and now why were they those
27:57 patterns and why did I very deliberately
27:58 have our volum volunteers line up in
28:01 that way well I wanted them using base 2
28:04 AKA binary but with binary there come
28:06 certain rules and even if you're not
28:08 familiar with binary beyond that it
28:10 exists and relates somehow to computers
28:12 it's actually pretty much identical to
28:14 the system you and I use every day known
28:17 as base 10 AKA decimal so let's consider
28:19 if you will by rewinding to primary
28:20 school for just a moment like how
28:22 decimal works and you'll see that even
28:24 if you're not a computer person you
28:25 actually are you just have to tweak your
28:28 mental model ever so slightly so here is
28:29 the number that you're probably viewing
28:32 as 123 but why is that well it's not
28:35 really 123 these are just uh this is
28:37 just a pattern of three symbols on the
28:41 screen 1 2 three and your mind is sort
28:42 of rapidly assigning mathematical
28:45 meaning to them 123 but why is that well
28:46 if you're like me you probably learned
28:47 back in the day when you have a
28:49 three-digit number like this the
28:51 rightmost number is in the ones place
28:53 the middle digit is the 10 place the
28:55 leftmost digit is in the hundreds place
28:57 and why is that relevant well if you
28:58 then quickly do some mental math as you
29:01 and I just do uh instantly nowadays that
29:05 just means 100 * 1 plus 10 * 2 plus 1 *
29:08 3 of course 100 + 20 + 3 gives us the
29:10 number you and I know as
29:13 123 but beyond that how do we get to
29:16 just two digits instead of as many as
29:18 nine in the decimal system well let's
29:20 generalize this in the decimal system
29:22 you and I know if we've got three digits
29:24 represented by these hashes here yes
29:26 it's the ones place T Place hundreds
29:28 place and if we keep going thousands 10
29:30 thousands and so forth but why is that
29:32 well base terminology is now a little
29:34 more gerine that's technically the 10 to
29:37 the zero column the 10 to the 1 10 to
29:40 the two so these are powers of 10 where
29:43 10 is your base computers just simplify
29:45 things a little bit because computers at
29:46 the end of the day only have access to
29:48 electricity on or off they don't have
29:50 access to like 10 different types of
29:53 electricity just two on or off if you
29:55 will well they just use a different base
29:56 and the rightmost digit would be in the
29:59 so-called 2 to the zero then the middle
30:01 digit is 2 to the one the leftmost is 2
30:04 to the 2 AKA one's Place two's Place
30:06 Four's place and as we kept going 8 and
30:10 if we keep going 16 32 64 128 and so
30:12 forth but the idea is fundamentally the
30:15 same so why is this how the computer
30:17 represents the number you and I know is
30:21 zero well off off off from right to left
30:23 or in this case left to right is just
30:27 zero why CU that's 4 * 0 + 2 * 0 + 1
30:32 time 0 is of course 0o this is why 0 01
30:35 represents one this is why 0 1 0
30:38 represents two and three and four and
30:42 five and six and seven on up and why did
30:44 we need a fourth bit to represent eight
30:46 well we kind of needed to carry the one
30:49 so to speak using our human familiar uh
30:51 familiar human terminology but for that
30:53 we need a fourth bit another transistor
30:55 and this now represents the number eight
30:57 and that's why we ended with on from
31:00 left to right off off off so I keep
31:02 saying on and off or the light bulb is
31:05 on or off but really I just mean one or
31:08 zero and so computers and we humans
31:10 think of things digitally as just being
31:11 zeros and ones but mechanically you can
31:13 think of it indeed as these light bulbs
31:15 now a bit not very useful even three
31:17 bits four bits not that useful you can
31:19 count to seven or 15 generally speaking
31:22 bites are a more useful unit of measure
31:23 and anyone familiar how many bits is in
31:27 a bite yeah so eight bits are in a bite
31:29 can think of it as an octet equivalently
31:31 uh in some context there are nuances
31:32 there but think of a bite as just being
31:34 eight bits and that's just a more useful
31:36 measure so what does this mean in real
31:37 terms well if you've ever downloaded
31:41 like a music file or a photograph or a a
31:43 video those are measured in bytes
31:45 probably not small numbers of bytes
31:47 probably kilobytes for thousands of
31:49 bytes megabytes for millions of bytes
31:52 gigabytes for billions of bytes
31:54 especially for video that just means you
31:57 have a lot of patterns of 8 Bits some
31:59 combination of zeros and ones on your
32:02 computer's hard drive here then with a
32:04 bite of bits eight bits is how a
32:06 computer would typically represent the
32:08 number zero and if that same computer
32:11 uses all eight of its bits it's full
32:13 bite to rep uh to change it to one
32:15 anyone who's quick with math or have
32:17 seen this before how high can a computer
32:21 count with eight bits or one yeah 255
32:22 why is that well we're not going to turn
32:24 this into a constant math exercise
32:25 indeed after today we're not really
32:28 going to think about or talk about bits
32:30 at this low level but this is the ones
32:36 place 2os 4S 8 16 32 64 128 and if I do
32:37 all of that math from left to right that
32:39 indeed gives me
32:41 255 it ignores how we might represent
32:43 negative numbers but perhaps more on
32:45 those some other day but computers of
32:47 course do so much more than numbers and
32:50 math and all this lowlevel stuff we send
32:52 text messages right documents emails and
32:54 the like so how might a computer
32:57 represent something like the letter A I
32:59 claim at the end of the day your Mac
33:01 your PC your phone just has lots of
33:03 transistors lots of switches that it can
33:07 use in units of eight in units of bytes
33:09 how though if it's already using those
33:11 patterns of zeros and ones apparently to
33:14 represent numbers from zero on up how do
33:16 you go about representing letters of the
33:20 alphabet might you think yeah okay so we
33:22 could assign a number to every letter
33:24 okay so let me just conjecture well
33:27 let's just call a0 for Simplicity B1 1
33:29 C2 and now let me play Devil's Advocate
33:33 okay how do I now represent zero or one
33:36 or two well we've maybe created a
33:37 problem for oursel if now we have to
33:39 steal some numbers to represent letters
33:41 we kind of have to pick a lane but
33:42 there's a solution to that too that
33:44 we'll see and it turns out the world is
33:46 not quite as simple as a being zero a
33:48 typically is represented by computers
33:50 everywhere phones everywhere with the
33:54 number 65 the decimal number 65 using 8
33:56 Bits if we turn some of the zeros to
33:57 ones let me just stipulate you can
34:00 represent the letter A Using eight bits
34:02 by turning certain ones on and certain
34:04 ones off but we will try not to focus on
34:07 the that binary level too much so if a
34:09 is 65 it turns out that b is going to be
34:13 66 and C is going to be 67 and so forth
34:15 and so where does that get us well it
34:17 turns out there's a whole system that
34:21 Maps uh numbers to letters and here uh
34:23 as I alluded to verbally a moment ago is
34:24 the pattern of zeros and ones via which
34:27 you'd represent 65 and just quick check
34:29 here we won't constantly do math uh
34:32 one's place that's easy 2os Four's 8 16
34:38 32 64's place so 64 + 1 gives us
34:41 65 so once I do that how do I get to all
34:43 of the others well it turns out a bunch
34:46 of Americans years ago came up with this
34:48 asky the American Standard code for
34:50 information interchange now what does
34:52 that mean well it's just an acronym
34:54 describing what really you proposed a
34:56 mapping between numbers and letters not
34:59 not quite as simple as 012 starts at 65
35:03 66 67 for capital letters but here are
35:05 most of the letters in use today at
35:07 least with this system so this is just a
35:09 big chart from online and you'll see it
35:11 in the middle of this chart here here's
35:16 my 65a here's my 66 b c and let's see 72
35:20 is H 73 is I and so forth so there's a
35:21 mapping at least for English between all
35:23 of these numbers and all of these
35:25 letters and if we focus here those are
35:28 the beginning of our up case alphabet so
35:30 suppose then that today tomorrow you
35:34 receive a text message from someone and
35:35 underneath the hood now that you're a
35:37 computer person you figure out a way to
35:39 see what pattern of zeros and ones was
35:41 sent in this case it's wireless as
35:43 opposed to wired but it's still some
35:44 pattern of zeros and ones and your phone
35:46 is turning some switches of its own on
35:48 and off to represent that message from a
35:50 friend suppose that the three patterns
35:53 you received were these three btes from
35:55 left to right spelling out a
35:57 three-letter word well if if we do out
35:59 the math one's Place two's place and so
36:01 forth I'll spoil it for you suppose that
36:03 you received a text message that doesn't
36:06 literally say 72 73 33 but you've
36:09 received a pattern of 8 plus 8 plus 8 24
36:11 bits that if you do out the math
36:15 represent the decimal number 72 73 33
36:17 anyone recall what message you might
36:19 have received from the green and white charts
36:20 charts
36:25 yeah hi but yes hi is the message but
36:29 7273 gives H and I what's 33 any any
36:30 guesses to
36:33 33 yeah over here yeah so it's an
36:35 exclamation point how would you know
36:36 that well you really do need some kind
36:38 of cheat sheet AKA aski in this case and
36:40 if we look elsewhere let me highlight
36:41 the left of the chart you can see that
36:44 next to 33 in decimal is indeed the
36:46 exclamation point so back in the day a
36:48 bunch of humans got in a room decided
36:50 that hey when we start building PCS and
36:52 later Macs and phones we all just have
36:55 to agree on this form of representation
36:58 of letters of the the English alphabet
36:59 in this case we just need to agree on
37:02 this mapping but somewhat curiously
37:05 notice this it turns out that once you
37:07 paint yourself into this corner and
37:12 start using 65 for a 66 for B well how
37:15 do you represent 65 the number and 66
37:16 the number if you want to do math or use
37:19 Excel or something like that does anyone
37:22 kind of see the solution perhaps how do
37:27 you represent the number one in asy yeah
37:29 in the
37:32 middle yeah so this is getting a little
37:34 maybe Inception or something but you
37:35 could represent numbers with other
37:37 numbers and so if you want to represent
37:38 the number you and I know as one like
37:40 when you type it on your keyboard turns
37:42 out the computer stores that as the
37:45 decimal number 49 if you hit two on your
37:47 keyboard the computer is not storing two
37:49 per se it's storing the decimal number
37:53 50 now thankfully the the uh the uh the
37:55 the Paradox kind of stops there we just
37:56 have a mapping now of numbers to to
37:58 numbers but really at the end of the day
37:59 and you're going to learn this when we
38:00 start writing code in that other
38:03 language C next week it's just context
38:04 dependent at the end of the day inside
38:06 of your Mac PC and phone there's just
38:08 all of these permutations of bits all of
38:11 these patterns of zeros and ones and
38:12 generally speaking when you open up a
38:14 text message that you've received from
38:16 someone it's zeros and ones but
38:17 obviously if it's a text message the
38:19 whole point of text messages is to send
38:21 text and so those patterns of zeros and
38:23 ones by default will typically be
38:25 interpreted as letters of the alphabet
38:27 so you won't see zeros and ones you
38:28 won't see decimal numbers you'll see the
38:30 English message that your friend
38:33 intended by contrast if you open up
38:35 something like Excel that same pattern
38:37 of zeros and ones might indeed work out
38:41 to be 6 uh 72 73 33 you might see cells
38:43 in your spreadsheet with literally those
38:45 three numbers why because spreadsheets
38:46 are all about numbers and number
38:49 crunching and math uh in many cases if
38:52 by contrast you open up Photoshop and
38:53 try to look at that same pattern of
38:56 zeros and ones it's not going to be 727
38:59 73 33 it's not going to be zeros and
39:00 ones it's not going to be high it's
39:02 going to be some color of the rainbow
39:04 you're going to use those patterns of
39:06 zeros and ones it turns out too to
39:09 represent colors and indeed so long as
39:12 you and I just agree as humans long have
39:14 what these patterns are going to be all
39:15 of our systems many of our systems
39:17 nowadays are indeed interoperable but
39:19 I'm being very biased here and indeed
39:22 the a in ASI is very American Centric
39:25 what do you not see in this chart if you
39:27 speak any other language in English odds
39:29 are you're not seeing characters you
39:31 know and love and need every day to type
39:33 or send messages well there's a huge
39:35 character set that's not supported here
39:37 whether it's accented characters and a
39:38 lot of Asian alphabets you have many
39:40 more symbols than can fit even on this
39:42 screen here and so humans kind of
39:44 painted themselves into a corner early
39:46 on or really Americans did but on a
39:48 typical keyboard us English keyboard
39:50 yeah you have A's and B's and C's
39:53 uppercase and lowercase but you also
39:55 have accented characters here and
39:57 nowadays not sure if this is maybe
39:59 necessary but nowadays you have other
40:01 characters on your keyboard like these
40:03 and these are kind of a playful
40:04 incarnation of what's actually a
40:07 technical solution to this problem if I
40:08 claim for the moment that ASI
40:11 historically used seven bits to
40:12 represent letters and let's just round
40:14 that up to a bite eight bits to
40:17 represent letters aski can represent as
40:19 many as 255 or really
40:22 256 total characters why 256 well if you
40:25 have them all zero that's zero and the
40:26 highest number I claimed a moment AG go
40:29 was 255 so that's 256 total
40:31 possibilities that's not many letters
40:33 it's fine for English but not a lot of
40:35 human languages so what might the
40:37 intuitive solution be if you want to
40:40 represent accented characters um Asian
40:43 characters Emoji even like these which
40:45 are just keys on a keyboard
40:48 nowadays what's the intuitive solution
40:50 if a bite's too few
40:52 yeah yeah so add another digit just like
40:54 we had a fourth volunteer come on up to
40:56 give us a fourth bid let's just throw
40:57 throw Hardware at the problem and use a
40:59 few more bits so maybe instead of one
41:01 bite let's use two or heck let's use
41:03 three or four bytes even though it's
41:04 getting a little expensive we're going
41:08 from 8 to 16 to 24 or 32 bits that's how
41:10 computers do things these days and
41:11 thankfully we have so much memory inside
41:13 of our computers and phones we can
41:15 certainly spare a few to represent these
41:17 things and the solution then to aski is
41:20 what we'll call Unicode so Unicode is
41:24 also just a mapping of letters to num of
41:26 numbers to letters but in many many
41:27 different languages and indeed the
41:30 Unicode Consortium is a bunch of people
41:32 from all different companies and and uh
41:35 and a lot of different uh companies and
41:37 countries and cultures whose mission as
41:40 an organization is to capture digitally
41:43 all forms of human language in this case
41:45 and to ensure that especially smaller
41:47 demographics of humans speaking lesser
41:49 known languages are nonetheless
41:51 represented and preserve digitally using
41:53 some mapping of these zeros and ones it
41:55 turns out though if you start using 32
41:58 bits as many as 32 bits to represent
42:00 characters on a keyboard that's 4
42:03 billion possible permutations of zeros
42:05 and ones that's way more than we need
42:06 for most human languages so there's a
42:08 little bit of room in there for some of
42:10 those more playful things like those
42:12 Emoji so for instance suppose you got a
42:14 text message with this pattern of zeros and
42:15 and
42:17 ones or if we do out the math suppose
42:19 you receive a text message that if you
42:21 do out the math in decimal is 4 B 36
42:25 m991 106 anyone know what emoji you're looking
42:27 looking
42:30 at this would be weird if you do
42:33 but what is this Well turns out that as
42:34 of this past year this is the most
42:37 popular Emoji to be sent uh by many
42:40 measures face with tears of joy so that
42:42 is the pattern that a bunch of humans in
42:44 the Unicode Consortium decided would
42:46 represent this but you'll notice some
42:48 many of you might have iPhones some of
42:50 you might have Android devices too and
42:52 sometimes these don't actually look
42:53 quite the same this happens to be the
42:55 current version of face with tears of
42:59 joy on iOS on Android it tends to look a
43:01 little something more like this and
43:04 here's kind of a curiosity even though
43:05 you and I look at these things and they
43:07 look like images they're not images
43:09 they're characters at least as we've
43:12 defined them now in Unicode and a IOS
43:15 and Android and Windows and Facebook and
43:18 other companies and uh apps nowadays
43:19 really just have different fonts if you
43:21 will so just like fonts with English and
43:23 other languages can give you different F
43:26 characters with seraps or not emoji are
43:28 themselves yes drawings that someone
43:30 made but they're really just a font and
43:32 so that same pattern of zeros and ones
43:34 might just render slightly differently
43:36 on someone's phone or another if you've
43:37 ever gotten like an icon on your phone
43:39 that's broken and you've been sent an
43:41 emoji but it's like a square or
43:43 something arbitrary and not sensical
43:45 sensible it might just mean that you
43:46 have not updated to the latest version
43:49 of iOS or Android which just updates the
43:51 font of supported Emoji because those
43:53 Folks at Unicode pretty much every year
43:55 nowadays are adding more and more Emoji
43:57 to that that particular character said
43:59 now I went down the rabbit hole figuring
44:01 out the other day just which are the
44:03 most popular Emoji these days on Twitter
44:05 specifically this past year the most
44:08 popular Emoji by contrast uh was loudly
44:11 crying face I don't know if that says
44:13 more about 20121 or about Twitter but
44:15 you'll see different Trends certainly in
44:18 how these are used but even humans
44:20 themselves didn't necessarily think two
44:22 steps ahead and now a lot of the Emoji
44:24 are the sort of default yellow color but
44:26 there's a lot of emoji that aren't sort
44:27 of these cartoon characters but they're
44:29 meant to represent humans in various
44:31 professions or gestures or the like and
44:33 nowadays too you've probably noticed on
44:36 your phone and Macs and PCs there are
44:37 different skin tones that you can assign
44:40 to certain emojis if it's supported by
44:42 the company and by Unicode you can
44:44 actually like touch and hold on a
44:45 certain emoji and then you can choose
44:47 the appropriate skin tone to represent
44:48 yourself or someone else and that then
44:50 modifies the display well let's just
44:53 think for a moment here how did Apple
44:54 and Google and Microsoft and others go
44:58 about implementing support for emoji
45:00 with different skin tones how could you
45:03 do this if you want to represent some
45:06 smiling Emoji but in five in this case
45:07 different skin tones you could come up
45:09 with what five different patterns that
45:12 are identical structurally except for
45:14 the skin tone used in places in that
45:16 image but that's a little inefficient
45:18 right to kind of just do copy paste
45:20 paste paste paste and like change the
45:22 color in Photoshop if you will that's
45:24 going to use more bits more information
45:27 than you might need to how else if you
45:28 now start to think a little bit more
45:30 like a computer scientist if at the end
45:31 of the day all you have are zeros and
45:34 ones how else could you implement skin
45:35 tones might you think
45:38 yeah okay so RGB and we'll come to that
45:39 in just a moment that stands for red
45:42 green blue that's one way in this case
45:44 though I'm seeking an alternative to
45:47 just using five different patterns of
45:48 zeros and ones to represent the same
45:51 Emoji but different skin tones so not
45:52 quite RGB
45:56 yeah okay so store one copy of the Emoji
45:58 and then store different variants of the
46:00 color that you want to assign to that
46:01 emoji yeah so this is actually an
46:04 example of do you want to
46:06 elaborate okay so you can use a loop to
46:08 actually output these things more on
46:09 that in a moment let me go down this
46:11 this road for just a moment this would
46:13 be in some sense sort of a a better
46:16 design if you will uh but why yeah
46:18 filter okay so filter if we think sort
46:20 of in the Instagram sense you can sort
46:21 of change the color of something and
46:24 that could be related here too could it
46:26 be oh interesting so maybe it could be
46:28 just a completely different font and you
46:29 have five different fonts that are
46:31 almost identical except for the various
46:32 interpretations of skin tone for those
46:34 same Emoji let me spoil I think if we go
46:37 down this one particular Road the way
46:39 the uh the Unicode folks decided to do
46:42 this some years ago were the first uh
46:45 bite or bites that you receive via text
46:47 or email just represent like the
46:48 structure of the Emoji the default
46:50 Yellow Version thereof but if it's
46:52 immediately followed by a certain
46:54 pattern of bits that these humans
46:56 standardize to represent each of these
46:59 different shades of skin tone then the
47:01 phone the Mac the PC will change that
47:03 default color yellow in most cases to
47:06 whatever the more apt human tone is so
47:08 you just use twice as many bits but you
47:10 don't use five times as many bits so
47:13 what do I mean you don't have uh five
47:15 completely distinct patterns per se uh
47:19 for each of these possible variants you
47:21 have a representation of just the Emoji
47:25 itself structurally and then uh reusable
47:27 pattern patterns for those five skin
47:30 tones un um unfortunately that wasn't
47:32 quite versatile enough for other
47:33 features that were in the pipeline and
47:35 nowadays too and there's a double
47:38 meaning now to representation emojis had
47:40 tended to focus on certain professions
47:42 and early on too were certain
47:43 professions associated with certain
47:45 genders and vice versa and you couldn't
47:47 necessarily be one gender or another in
47:49 a certain profession or another there
47:50 were these combinatorics that just
47:51 weren't possible but nowadays as you
47:53 might have seen you can have couples in
47:55 love for instance that actually look a a
47:57 little more like three emojis but just
47:58 kind of combined into one and indeed
48:01 this is just one key press on your phone
48:02 and you can combine different emoji on
48:05 the left and in the right with the Emoji
48:07 in the middle and so it turns out how
48:09 computers nowadays represent these
48:12 patterns are one set of bits for the
48:14 character on the left one set of bits
48:16 for a character on the right one set of
48:18 bits for whatever Emoji you want in the
48:20 middle and then you assemble more
48:23 complicated compositions of emoji by
48:25 just reusing those same patterns and
48:27 bits and bits Emoji doesn't the Unicode
48:29 folks don't have to come up with a whole
48:32 new representation for some very
48:34 specific Incarnation they can create one
48:37 for person for male for female for other
48:38 uh characters that you might want to
48:41 display and reuse those same patterns of
48:43 zeros and ones and so here you see sort
48:44 of the imperfection of or lack of
48:46 foresight of humans for building a
48:47 system early on that was entirely
48:49 American Centric no characters Emoji or
48:51 the like that's evolved too and so
48:53 that's an important detail in Computing
48:56 nowadays um it too is evolved in and the
48:57 languages you're about to learn in the
49:00 coming days those twoo are evolving as
49:01 well and new features are getting added
49:03 and even programming languages have
49:05 version numbers you might have a
49:06 different version of an app on your
49:08 phone programming languages too have
49:11 different versions as well questions
49:13 then thus far and how information is
49:15 represented using asy or
49:18 Unicode or anything in between yeah so
49:21 you can use a string of a good question
49:26 so to recap why can't you just um well
49:28 let me summarize that as why can't you
49:30 similarly use different patterns to
49:32 change the context of what these
49:33 patterns of bits represent whether it's
49:36 a number or a letter or a graphic in
49:37 actuality that's kind of what's
49:39 Happening underneath the hood it's not
49:41 standardized in quite the same way but
49:42 starting next week when we transition
49:44 from scratch to C you'll learn about
49:46 types data types where the onus
49:48 initially is going to be on you the
49:50 programmer to tell the program whether
49:51 or not this pattern of bit should be
49:54 interpreted as a number or as a letter
49:57 or as a color or something else nowadays
49:58 though in toward the end of the semester
50:00 you'll use languages like python where
50:02 the computer just figures it out for you
50:04 by context which makes it even easier
50:07 and faster to program as well other
50:10 questions on Unicode asky or the
50:12 like all right well how about just a few
50:14 other forms of information RGB was
50:16 called out earlier red green blue How do
50:18 images get represented in computers well
50:21 in fact it's typically an assembly of
50:22 some amount of red some amount of green
50:24 some amount of blue but there are other
50:25 representations if you're a graphic
50:28 designer you might know them but RGB is
50:29 still pretty common what does this mean
50:31 this means to represent every dot on
50:34 your phone or every dot on your TV or
50:37 your laptop or desktop there is a number
50:38 representing how much red that dot
50:40 should show a number representing how
50:42 much green and a number representing how
50:44 much blue it should show red green blue
50:47 respectively so for instance if a DOT on
50:49 your screen were using these three
50:52 numbers these three values or bytes 72
50:56 73 33 in a text message email that would
50:58 be interpreted as I claimed high but in
51:01 Photoshop or in some graphical program
51:02 that same pattern would be repres would
51:05 be interpreted as let's call it a medium
51:07 amount of red a medium amount of green
51:09 and a little bit of blue and why medium
51:11 and little turns out that each of these
51:13 are bites the smallest value you can
51:15 have in a bite we said is zero the
51:16 largest value you can have in a bite is
51:19 255 so I'm just kind of spitballing here
51:20 this is like medium medium and a low
51:23 amount of red green blue uh specifically
51:25 those three colors um like uh
51:28 wavelengths of light are combined in
51:30 such a way that you would have this dot
51:32 on the screen a sort of murky shade of
51:35 yellow or brown that is how a computer
51:38 would store precisely that color and in
51:40 fact we've seen this color when you type
51:42 in face with tears of joy generally on
51:44 your screen it looks like this typically
51:47 much smaller but let's zoom in or let's
51:49 zoom in a little more what do you
51:52 starting to see if you know the term so
51:54 pixels it's getting very pixelated a
51:56 pixel is just a DOT on the screen and if
51:57 you really zoom in on it you can
51:59 literally see all of the dots that
52:02 compose in emoji in this case on iOS in
52:04 the font that Apple's using to represent
52:06 this particular pattern of zeros and
52:08 ones so one of those yellow dots and
52:09 there's many of them all that kind of
52:12 blend together here each dot on the
52:15 screen I claim is three bytes how much
52:17 red green blue for this dot how much red
52:18 green blue for this dot how much red
52:19 green blue for this Dot and you'll
52:21 notice too that when it gets to be sort
52:23 of brownish here the dots really stand
52:26 out the three values the three bytes AKA
52:29 24 bits are just slightly different and
52:31 so underneath the hood this is why
52:33 images photographs that you take or GIFs
52:35 that you download get so darn big
52:38 potentially because you have a number
52:40 representing every dot on the screen
52:42 well if this I claim is indeed how
52:44 images are typically represented using
52:47 pattern of bits that are assigned to
52:49 some amount of red green or blue how do
52:52 you get video what is a video if at the
52:54 end of the day all we have are zeros and ones
52:55 ones
52:58 what's a video perhaps yeah oh let's go
53:01 here weighing back yeah pixel's really
53:03 changing values over time and do you
53:04 want to confirm or deny the hand that
53:07 went up here yeah or equivalently a
53:09 sequence of images that over time are
53:11 changing on the screen so both of those
53:13 are valid interpretations and you know
53:14 just for fun if you uh grew up with
53:16 these sort of picture books you might
53:18 remember a little something like this if
53:20 we could dim the [Music]
53:24 [Music] lights
53:25 lights [Music]
53:40 [Music]
53:42 so that's sort of the old school analog
53:44 way to implement a video in the sense
53:47 that um that artist wrote out like
53:49 hundreds of pieces of paper with almost
53:51 identical images but where the ink from
53:53 their pencil or pen was slightly moving
53:55 and if you digitize that such that each
53:57 of those Strokes are represented with
53:58 dots instead that's really what you're
54:00 seeing as a sequence of all these images
54:02 flying across the screen and if we dive
54:04 into the the real world if you've ever
54:06 watched a film a Hollywood movie is
54:09 typically 24 FPS frames per second that
54:12 really means you're seeing 24 images per
54:14 second or on TV or in soap operas it's
54:16 often 30 frames per second that makes
54:18 things look a little more smooth so it's
54:21 not actual motion picture if you will it
54:22 sequences of pictures and your brain and
54:25 mind are kind of interpolating that oh
54:26 this is smooth movement even though
54:28 we're just seeing a lot of pictures
54:30 really fast now that gets really big and
54:31 we'll talk later in the semester how you
54:33 can compress information so that you're
54:35 not using way more bits than you
54:36 actually need to and there's fancy
54:38 algorithms that folks have developed but
54:40 at the end of the day that's really all
54:42 a video might be is a sequence of images
54:44 conversely if you want to represent the
54:45 music that accompanies that or something
54:48 else if any of you play an instrument
54:50 and can read sheet music How Could You
54:53 digitize this like how could you
54:56 represent musical notes in a computer
54:58 you and I hear them when we play files
54:59 but what's really going on underneath the
55:01 the
55:05 hood any any musicians piano players
55:09 anyone yeah Val okay so Hertz value so
55:11 some frequency so sound is some
55:12 frequency and it's kind of hitting your
55:14 eardrum and that's what makes it sound
55:16 low or high or somewhere in between so
55:17 maybe we could assign just like there's
55:19 letters a through G here maybe we could
55:21 assign specific frequency values which
55:23 are just going to be numbers measured in
55:24 something called Hertz something per
55:26 second and maybe we could have a few
55:27 other numbers for each of these notes
55:30 not just the note or the frequency maybe
55:32 we could represent the loudness of it
55:34 like how hard or how softly a human
55:36 might equivalently press it maybe a
55:38 third number like duration like how long
55:40 is their finger on the keyboard so you
55:42 could imagine quantizing something like
55:43 music that in the real world is
55:45 perfectly continuous as something more
55:48 discret by representing each note over
55:50 time as just some sequence of values and
55:51 there's so many different ways to do
55:55 this midi if you've heard uh MP3s AAC I
55:57 mean almost all the file extensions you
55:59 see on your Mac or PC if you see them at
56:01 all ultimately just mean there's a
56:03 different form of representation for in
56:07 this case something like sound so let me
56:09 just stipulate there are these and many
56:11 more ways to represent inputs and
56:13 outputs and thankfully humans have
56:14 standardized a lot of this they don't
56:16 always dis agree and this is why we have
56:18 different file formats for Apple numbers
56:20 and Microsoft Excel and Google
56:21 spreadsheets and sort of stupid
56:23 incompatibilities like that but
56:24 generally speaking humans have
56:26 standardized how we represent the inputs
56:28 and outputs to and from problems but
56:31 let's now focus on this black box so to
56:33 speak in the middle this abstraction so
56:35 abstraction is technically a term that
56:37 you'll see all over the place in
56:38 computer science and really problem
56:40 solving that just refers to the
56:43 simplification of something so that you
56:44 don't focus on the lower level
56:46 implementation details you really just
56:49 focus on the high level goals or the
56:52 process itself uh Therefore your car if
56:53 youve uh if you have a license and have
56:55 driven or have been in a car a car so
56:57 far as you're concerned is probably an
56:58 abstraction most of us if you're like me
57:00 probably don't really know or care how
57:02 the engine works and all the parts that
57:04 are moving to you it's just a way of
57:06 getting from point A to point B it's an
57:07 abstraction but someone hopefully the
57:10 mechanic does know those lower level
57:12 implementation details if you had to
57:13 understand how a car works every time
57:15 you want to go to school or to the store
57:17 it's probably going to be a pretty slow
57:18 process you just want to think and
57:20 operate at this higher level of
57:22 abstraction and we're going to do this
57:24 all the time when writing code and
57:25 solving problems
57:27 so what then is in this black box this
57:29 abstraction at the moment well generally
57:31 it's what a computer scientist would
57:33 call an algorithm stepbystep
57:35 instructions for solving some problem
57:37 now let's consider the implementation
57:39 details that is to say how you might
57:41 solve certain problems and let's take a
57:43 a sort of old school example but in
57:46 modern form this icon if you have an
57:48 iPhone is of course for your contacts
57:49 application and if you've got a whole
57:51 bunch of family members or friends or
57:53 colleagues in your phone book you have
57:54 some kind of contacts pictured here here
57:56 and it's alphabetized typically by first
57:58 name and last name and odds are you and
57:59 I are in the habit if they're not
58:02 already a favorite of like clicking on
58:04 search and then using autocomplete and
58:05 what happens when you start typing
58:07 autocomplete well if you type in the
58:09 letter H you'll see only presumably
58:11 Hagrid Harry Hermione and so forth if
58:14 you type in ha that shows you only
58:16 Hagrid and Harry and it all happens
58:18 super fast so how is that happening well
58:21 typically you could just start at the
58:24 top and look to the bottom searching for
58:26 all the hes or all of the ha but for
58:27 larger data sets that's going to get
58:29 slow for the Googles of the world that's
58:31 going to get really slow and even on our
58:33 phones when you have hundreds thousands
58:36 of contacts eventually even that kind of
58:38 approach that algorithm step by step but
58:40 it might be slow so how might we go
58:41 about searching for someone in a phone
58:44 book like this uh like say uh John
58:46 Harvard well here's an old school
58:48 incarnation of this and uh odds are you
58:50 might not have had occasion to even
58:51 physically use this thing nowadays and
58:53 in fact this is a bit of a white lie cuz
58:55 this is the Yellow Pages which means
58:57 this is a book of companies not people
58:59 uh but for this is all you can find and
59:01 at that it's even hard to find this but
59:03 this is the same thing in analog form
59:05 physical form so if I wanted to search
59:07 for someone like John Harvard how could
59:10 I do that well I could start on page one
59:12 and I could start searching for page two
59:16 page three page four page five little
59:18 hard to do physically especially since
59:19 no one's used this phone book in a lot
59:22 of years but uh is this algorithm
59:26 correct Turning Page by Page
59:28 very inelegantly is this correct will I
59:30 find John Harvard if if he's in here all
59:32 right so yes I mean this is a little
59:34 stupidly tedious because if there's like
59:35 a thousand Pages he might be a few
59:37 hundred pages into this but it's correct
59:39 at some point I will find him and if
59:41 he's on the page I'll be able to call
59:42 why because presumably the names are
59:44 alphabetized in here and there's no like
59:46 cheat sheet on the edge so I have to
59:47 search for John Harvard from left to
59:48 right searching for H if it's
59:50 alphabetized by last name well what
59:51 would be marginally better well how
59:53 about two pages at a time it's hard to
59:55 do with a 20-year-old old phone book
59:57 where the pages are kind of uh grown
60:02 together but 2 4 6 8 10 12 this algorithm is this correct all right so
60:04 algorithm is this correct all right so no
60:06 no why yeah so I'm skipping every other
60:08 why yeah so I'm skipping every other page so if I don't consider that and I
60:11 page so if I don't consider that and I find myself in like the I section or the
60:13 find myself in like the I section or the J section well I might accidentally
60:16 J section well I might accidentally conclude nope I haven't found John
60:18 conclude nope I haven't found John Harvard yet just because I skipped them
60:20 Harvard yet just because I skipped them because it was sandwiched between two
60:21 because it was sandwiched between two pages now I can fix this I think if I do
60:24 pages now I can fix this I think if I do hit the I section well let me just
60:26 hit the I section well let me just double back one page just in case he was
60:28 double back one page just in case he was in that last page so it's recoverable
60:30 in that last page so it's recoverable but it's almost twice as fast minus that
60:32 but it's almost twice as fast minus that that hiccup there but what most of us
60:34 that hiccup there but what most of us would do and what your phones are doing
60:35 would do and what your phones are doing albeit digitally is they open up roughly
60:37 albeit digitally is they open up roughly to the middle of the phone book and they
60:39 to the middle of the phone book and they look down and they say oh I'm in roughly
60:41 look down and they say oh I'm in roughly the M section so I'm roughly halfway
60:43 the M section so I'm roughly halfway through this thousand page phone book
60:45 through this thousand page phone book but what do I now know about John
60:46 but what do I now know about John Harvard where is he to my left or to my
60:50 Harvard where is he to my left or to my right all right so alphabetically he's
60:52 right all right so alphabetically he's presumably to my left and so here I get
60:54 presumably to my left and so here I get can both uh met uh metaphorically and
60:57 can both uh met uh metaphorically and physically tear the problem in
61:00 physically tear the problem in half you don't need to be impressed it's
61:02 half you don't need to be impressed it's really easy down the the spine that way
61:04 really easy down the the spine that way but uh I know that John Harvard is to
61:06 but uh I know that John Harvard is to the left here but now I can throw
61:09 the left here but now I can throw unnecessarily dramatically half and page
61:11 unnecessarily dramatically half and page one out of the way and what do I now
61:14 one out of the way and what do I now know I've gone from a thousand pages to
61:16 know I've gone from a thousand pages to like 500 I can kind of repeat roughly
61:17 like 500 I can kind of repeat roughly the same algorithm go to the half of
61:19 the same algorithm go to the half of this and so this time I went back a
61:20 this and so this time I went back a little too far I'm in now the um e
61:23 little too far I'm in now the um e section so what do I know is John
61:25 section so what do I know is John Harvard to my left or to my
61:27 Harvard to my left or to my right to my right so I can again tear
61:29 right to my right so I can again tear the problem in half throw this half away
61:32 the problem in half throw this half away and now I'm really flying I'm toing it
61:34 and now I'm really flying I'm toing it verbally slowly but that went from a
61:36 verbally slowly but that went from a th000 pages to 500 to now 250 and now I
61:39 th000 pages to 500 to now 250 and now I can do it again 125 i' do it again
61:42 can do it again 125 i' do it again roughly like 67 and keep doing it again
61:45 roughly like 67 and keep doing it again and again and again until I get left
61:46 and again and again until I get left with hopefully just one single page or
61:50 with hopefully just one single page or in this case an ad for ironically a
61:52 in this case an ad for ironically a mechanic okay so
61:55 mechanic okay so what is the implication for our
61:56 what is the implication for our performance well let's just do this sort
61:58 performance well let's just do this sort of in the abstract if you will if that
62:00 of in the abstract if you will if that first algorithm were to be plotted just
62:03 first algorithm were to be plotted just quickly on a chart without even numbers
62:05 quickly on a chart without even numbers here's my x-axis size of problem on the
62:07 here's my x-axis size of problem on the x-axis so the bigger the problem the
62:09 x-axis so the bigger the problem the farther out that way time to solve the
62:11 farther out that way time to solve the problem the tire you go up on the y-
62:13 problem the tire you go up on the y- axis the uh more time you're taking to
62:15 axis the uh more time you're taking to solve it how would we draw the running
62:17 solve it how would we draw the running time The amount of time taken to run
62:19 time The amount of time taken to run that first algorithm well it's going to
62:21 that first algorithm well it's going to be a straight line why cuz if you add
62:23 be a straight line why cuz if you add one more page next year because more
62:24 one more page next year because more people move to Cambridge you're going to
62:26 people move to Cambridge you're going to add one more page turn potentially so
62:28 add one more page turn potentially so one more second one more unit of time so
62:30 one more second one more unit of time so it's a straight line and we'll abstract
62:32 it's a straight line and we'll abstract it away as n if there's n pages in the
62:34 it away as n if there's n pages in the phone book the slope of this line is
62:36 phone book the slope of this line is essentially n the second algorithm
62:38 essentially n the second algorithm wherein I was doing two pages at a time
62:41 wherein I was doing two pages at a time was twice as fast but it's still a
62:43 was twice as fast but it's still a straight line and in fact let me just
62:44 straight line and in fact let me just draw some dotted lines here if the phone
62:46 draw some dotted lines here if the phone book is this big with my first algorithm
62:49 book is this big with my first algorithm it might take this many step this many
62:50 it might take this many step this many units of time this many steps this many
62:53 units of time this many steps this many page turns but with that that second
62:54 page turns but with that that second algorithm notice that the intersection
62:57 algorithm notice that the intersection is with much lower on the yellow line
62:59 is with much lower on the yellow line than on the red so n/2 means there's
63:01 than on the red so n/2 means there's half as many pages here if n is the
63:03 half as many pages here if n is the number of pages so indeed that algorithm
63:05 number of pages so indeed that algorithm the second one is twice as fast minus
63:07 the second one is twice as fast minus the little hiccup that I have to double
63:09 the little hiccup that I have to double back one page but that's not a big deal
63:11 back one page but that's not a big deal if I'm still doing things twice as fast
63:14 if I'm still doing things twice as fast but the third algorithm looks
63:15 but the third algorithm looks fundamentally different it looks like
63:16 fundamentally different it looks like this logarithms if you recall from high
63:19 this logarithms if you recall from high school or prior if you don't that's fine
63:20 school or prior if you don't that's fine too it's just a fundamentally different
63:23 too it's just a fundamentally different function a different shape and notice
63:25 function a different shape and notice that the green line is going up and up
63:26 that the green line is going up and up and up but a much slower rate of
63:29 and up but a much slower rate of increase which means crazy things are
63:32 increase which means crazy things are possible if two towns in Massachusetts
63:33 possible if two towns in Massachusetts like Cambridge and Austin across the
63:35 like Cambridge and Austin across the river merge next year for instance in
63:38 river merge next year for instance in terms of their phone book their phone
63:39 terms of their phone book their phone book just got twice as big for the first
63:42 book just got twice as big for the first algorithm that's going to take me twice
63:44 algorithm that's going to take me twice as many steps to go through the second
63:46 as many steps to go through the second algorithm almost Twi it's going to take
63:48 algorithm almost Twi it's going to take me 50% more steps to go through two at a
63:50 me 50% more steps to go through two at a time but the third algorithm that I
63:52 time but the third algorithm that I ended with tearing things again and
63:53 ended with tearing things again and again divid and conquering if you will
63:55 again divid and conquering if you will in half and in half and in half how many
63:57 in half and in half and in half how many more steps will my third algorithm take
64:00 more steps will my third algorithm take if Cambridge and Austin merge into a
64:01 if Cambridge and Austin merge into a phone book that's twice as
64:03 phone book that's twice as big just one more step right no big deal
64:06 big just one more step right no big deal you just take a really big bite out of
64:08 you just take a really big bite out of the problem once you decide if John
64:09 the problem once you decide if John Harvard is to the left or to the right
64:11 Harvard is to the left or to the right and so you've made much faster progress
64:14 and so you've made much faster progress and so this in essence is what your
64:16 and so this in essence is what your computer your phone is probably doing
64:18 computer your phone is probably doing underneath the hood when searching for
64:20 underneath the hood when searching for Harry or Hermione or Hagrid or anyone
64:22 Harry or Hermione or Hagrid or anyone else because it's that much faster
64:24 else because it's that much faster especially when you have large data if
64:26 especially when you have large data if you don't have that many contacts
64:28 you don't have that many contacts probably doesn't matter if you search
64:29 probably doesn't matter if you search from top to bottom or more uh more in
64:32 from top to bottom or more uh more in the form of this divide and conquer
64:33 the form of this divide and conquer algorithm but if you're the Googles of
64:35 algorithm but if you're the Googles of the world or you're analyzing large data
64:37 the world or you're analyzing large data sets indeed this is going to add up
64:38 sets indeed this is going to add up quite quickly so where do we go with
64:41 quite quickly so where do we go with this well we're going to introduce next
64:42 this well we're going to introduce next something called pseudo code how can I
64:44 something called pseudo code how can I translate what I did verbally there sort
64:46 translate what I did verbally there sort of intuitively to actual code well this
64:49 of intuitively to actual code well this won't be scratch this won't be C or
64:50 won't be scratch this won't be C or python just yet it's just going to be an
64:52 python just yet it's just going to be an english-like syntax and this is how many
64:54 english-like syntax and this is how many programmers would start solving a
64:55 programmers would start solving a problem they don't start typing out code
64:57 problem they don't start typing out code in C or python or the like they use
65:00 in C or python or the like they use English or whatever their human language
65:01 English or whatever their human language is to jot down an outline for their
65:03 is to jot down an outline for their ideas my first step really was picking
65:05 ideas my first step really was picking up the phone book my second step was
65:07 up the phone book my second step was opening to the middle of the phone book
65:09 opening to the middle of the phone book my third step was somewhat different
65:12 my third step was somewhat different look at the page because why my fourth
65:13 look at the page because why my fourth step was if person I'm looking for is on
65:16 step was if person I'm looking for is on the page I then do what never happened
65:19 the page I then do what never happened in my example but I call the person so
65:21 in my example but I call the person so I'm done else if the person is earlier
65:25 I'm done else if the person is earlier in the book alphabetically as John
65:27 in the book alphabetically as John Harvard was in the case of my H then I
65:29 Harvard was in the case of my H then I should search to the middle of the left
65:32 should search to the middle of the left of the phone book and then I should go
65:34 of the phone book and then I should go back to step three step three is look at
65:37 back to step three step three is look at the page thereby repeating the same
65:39 the page thereby repeating the same process again and again step nine though
65:41 process again and again step nine though might be else if the person is later in
65:43 might be else if the person is later in the book then let's go ahead and open to
65:45 the book then let's go ahead and open to the middle of the right half of the book
65:47 the middle of the right half of the book and then go back to line three else
65:50 and then go back to line three else there's a fourth scenario we should
65:52 there's a fourth scenario we should probably consider lest my search process
65:54 probably consider lest my search process freeze or crash or give me one of those
65:56 freeze or crash or give me one of those spinning beach balls with a bug
65:59 spinning beach balls with a bug yeah yeah what if John Harvard isn't in
66:01 yeah yeah what if John Harvard isn't in the phone book I'd prefer that my
66:03 the phone book I'd prefer that my algorithm my phone not just reboot or
66:04 algorithm my phone not just reboot or freeze I should handle that with some
66:06 freeze I should handle that with some kind of catchall else so to speak let's
66:09 kind of catchall else so to speak let's just quit the program so there's
66:10 just quit the program so there's welldefined behavior for every possible
66:13 welldefined behavior for every possible scenario of the four now let's call out
66:16 scenario of the four now let's call out a few of these Salient terms it turns
66:17 a few of these Salient terms it turns out if I highlight in yellow here
66:19 out if I highlight in yellow here there's a pattern to what I've been
66:20 there's a pattern to what I've been doing here these are all of my English
66:22 doing here these are all of my English verbs and we're in a moment we're going
66:24 verbs and we're in a moment we're going to start calling those verbs functions
66:26 to start calling those verbs functions when you program or write code and you
66:28 when you program or write code and you want the program or the computer to do
66:30 want the program or the computer to do something for you some action or verb
66:32 something for you some action or verb we're going to refer to those actions or
66:34 we're going to refer to those actions or verbs as these things called functions
66:36 verbs as these things called functions like those here by contrast I've just
66:38 like those here by contrast I've just highlighted instead my if my El if my
66:40 highlighted instead my if my El if my Els if and Els this is going to
66:42 Els if and Els this is going to represent what we're going to start
66:44 represent what we're going to start calling a conditional a proverbial fork
66:46 calling a conditional a proverbial fork in the road where you can either go this
66:48 in the road where you can either go this way or that way do this thing or this
66:50 way or that way do this thing or this other thing and you're going to decide
66:52 other thing and you're going to decide which of those things to do based on
66:54 which of those things to do based on what I've now highlighted here which are
66:56 what I've now highlighted here which are going to be called Boolean Expressions
66:59 going to be called Boolean Expressions bull referring to a mathematician last
67:01 bull referring to a mathematician last named bull a Boolean expression is just
67:03 named bull a Boolean expression is just a question with a yes no a true false a
67:06 a question with a yes no a true false a one or a zero answer if you will and it
67:09 one or a zero answer if you will and it governs whether you do this thing or
67:10 governs whether you do this thing or this thing or this thing or that the
67:12 this thing or this thing or that the indentation in this case is important
67:15 indentation in this case is important the fact that I've indented line five
67:17 the fact that I've indented line five implies by convention in programming
67:20 implies by convention in programming that I should only do line five if the
67:21 that I should only do line five if the answer to line four is a yes or true and
67:24 answer to line four is a yes or true and same for these other indented lines as
67:26 same for these other indented lines as well and the last characteristic here is
67:29 well and the last characteristic here is this here uh someone called this out
67:31 this here uh someone called this out earlier in fact these lines eight and 11
67:33 earlier in fact these lines eight and 11 are now highlighted and represent
67:35 are now highlighted and represent what what might we call these in code if
67:38 what what might we call these in code if you've done that yeah so these are Loops
67:41 you've done that yeah so these are Loops some kind of cycles that result in my
67:43 some kind of cycles that result in my doing the same thing again and again but
67:45 doing the same thing again and again but there's a key detail with this algorithm
67:48 there's a key detail with this algorithm in pseudo code even though it's telling
67:50 in pseudo code even though it's telling me to go back to line three why is this
67:53 me to go back to line three why is this algorithm event going to stop why do I
67:56 algorithm event going to stop why do I not constantly keep looking for John
67:58 not constantly keep looking for John Harvard Forever by nature of these Loops
68:00 Harvard Forever by nature of these Loops telling me to keep going back to line
68:03 telling me to keep going back to line three good eventually he'll be on the
68:05 three good eventually he'll be on the page or or to your point earlier he
68:08 page or or to your point earlier he won't be at all and we're out of pages
68:10 won't be at all and we're out of pages and so we just quit and that's the key
68:12 and so we just quit and that's the key about going to the left half or the
68:14 about going to the left half or the right half it doesn't matter if you do
68:16 right half it doesn't matter if you do the same thing again and again you're
68:17 the same thing again and again you're not going to get stuck in a so-called
68:18 not going to get stuck in a so-called infinite Loop so long as you keep
68:20 infinite Loop so long as you keep dividing the problem and shrinking it
68:23 dividing the problem and shrinking it into something small smaller smaller
68:24 into something small smaller smaller eventually there's going to be no
68:26 eventually there's going to be no problem left to solve so even if you
68:28 problem left to solve so even if you don't think of yourself as a computer
68:29 don't think of yourself as a computer person even if you've never written code
68:31 person even if you've never written code what you'll find in the coming days is
68:33 what you'll find in the coming days is that these ideas that we've just kind of
68:35 that these ideas that we've just kind of harnessed from real life are at your
68:37 harnessed from real life are at your fingertips already and a lot of the
68:39 fingertips already and a lot of the process of learning to code is yes it's
68:41 process of learning to code is yes it's going to be a bumpy Ro uh some bumps in
68:43 going to be a bumpy Ro uh some bumps in the road because you can't quite see the
68:45 the road because you can't quite see the new syntax in a familiar way but you'll
68:47 new syntax in a familiar way but you'll find that the ideas in fact are going to
68:49 find that the ideas in fact are going to be more familiar than you might
68:50 be more familiar than you might otherwise think and so we'll see in a
68:52 otherwise think and so we'll see in a bit and we'll take a break in a moment
68:54 bit and we'll take a break in a moment uh to take a breather that you will see
68:56 uh to take a breather that you will see these same ideas in a moment in the
68:58 these same ideas in a moment in the context of scratch an actual programming
69:00 context of scratch an actual programming language via which we drag and drop
69:01 language via which we drag and drop puzzle pieces to make actual code work
69:03 puzzle pieces to make actual code work we'll see some variant of these ideas
69:05 we'll see some variant of these ideas things called arguments and return
69:06 things called arguments and return values and variables but we'll
69:08 values and variables but we'll ultimately convert it into this somehow
69:11 ultimately convert it into this somehow anyone want to wager what this program
69:14 anyone want to wager what this program will do if fed to your Mac or PC or
69:16 will do if fed to your Mac or PC or phone here's just a massive pattern of
69:19 phone here's just a massive pattern of zeros and
69:21 zeros and ones it will indeed say rather
69:23 ones it will indeed say rather disappoint ly apparently just hello
69:25 disappoint ly apparently just hello world and indeed baked into all of these
69:28 world and indeed baked into all of these zeros and ones are not just the h l l o
69:32 zeros and ones are not just the h l l o but also the verbs the action of
69:34 but also the verbs the action of printing something to the screen and
69:36 printing something to the screen and there's other stuff too so that the
69:38 there's other stuff too so that the program knows how to start and how to
69:40 program knows how to start and how to stop a lot of stuff that we won't have
69:41 stop a lot of stuff that we won't have to worry about that whoever designed the
69:43 to worry about that whoever designed the computer or the language did but at the
69:44 computer or the language did but at the end of the day you're never going to be
69:46 end of the day you're never going to be writing these zeros and ones yourselves
69:48 writing these zeros and ones yourselves though our ancestors Once Upon a Time
69:50 though our ancestors Once Upon a Time did in some form we'll be using a much
69:53 did in some form we'll be using a much higher l level language like this in C
69:56 higher l level language like this in C or better yet in just a moment like in
69:58 or better yet in just a moment like in scratch like this and indeed this is why
70:00 scratch like this and indeed this is why today we focus with focus on and begin
70:03 today we focus with focus on and begin with scratch this graphical programming
70:05 with scratch this graphical programming language so we have a way of expressing
70:07 language so we have a way of expressing ourselves with functions conditionals
70:09 ourselves with functions conditionals loops and more but in a way that doesn't
70:11 loops and more but in a way that doesn't have stupid parentheses and curly braces
70:13 have stupid parentheses and curly braces and all these visual distractions in the
70:15 and all these visual distractions in the way and we'll translate that thereafter
70:17 way and we'll translate that thereafter to this lower level language but for now
70:19 to this lower level language but for now that was a lot that was definitely a
70:20 that was a lot that was definitely a fire hose let's go ahead and take a
70:21 fire hose let's go ahead and take a 10-minute break feel free to get up or
70:23 10-minute break feel free to get up or stay here and we'll resume in a bit with
70:25 stay here and we'll resume in a bit with some actual
70:27 some actual code uh so this then is scratch a
70:30 code uh so this then is scratch a graphical programming language from a
70:31 graphical programming language from a friends down the road at mit's media lab
70:33 friends down the road at mit's media lab that indeed some of you might have used
70:34 that indeed some of you might have used in grade score the like for playing and
70:37 in grade score the like for playing and writing code and the like but you maybe
70:38 writing code and the like but you maybe didn't necessarily think about how some
70:41 didn't necessarily think about how some of these Primitives ultimately worked
70:42 of these Primitives ultimately worked and in fact everything you've done if
70:44 and in fact everything you've done if you've used scratch before and
70:46 you've used scratch before and everything you'll see today is going to
70:47 everything you'll see today is going to apply to all of the weeks to come as we
70:50 apply to all of the weeks to come as we explore these things called functions
70:51 explore these things called functions and loops and conditionals Boolean
70:52 and loops and conditionals Boolean expressions and more with scratch
70:55 expressions and more with scratch because it's so graphical and animated
70:56 because it's so graphical and animated can create can you create animations
70:58 can create can you create animations like this one interactive art and
71:00 like this one interactive art and software more generally but you'll do so
71:03 software more generally but you'll do so by dragging and dropping puzzle pieces
71:05 by dragging and dropping puzzle pieces that only lock together if it makes
71:07 that only lock together if it makes logical sense to do so and what you
71:08 logical sense to do so and what you won't have to deal with in this first
71:10 won't have to deal with in this first week of class is Curly braces
71:12 week of class is Curly braces parentheses all of the weird symbology
71:14 parentheses all of the weird symbology that you might recall seeing when we
71:16 that you might recall seeing when we just wanted to say hello world now this
71:18 just wanted to say hello world now this particular program um riging men was
71:20 particular program um riging men was written by a former cs50 teaching fellow
71:22 written by a former cs50 teaching fellow Andrew bar who's actually now the
71:24 Andrew bar who's actually now the general manager of the Cleveland Browns
71:26 general manager of the Cleveland Browns the American football team and so these
71:28 the American football team and so these are just some of the programs that some
71:30 are just some of the programs that some of your predecessors in the class have
71:32 of your predecessors in the class have created and you'll see in the remainder
71:34 created and you'll see in the remainder of class here a couple of others as well
71:36 of class here a couple of others as well and more in the course's first
71:37 and more in the course's first assignment namely problem set zero so
71:40 assignment namely problem set zero so how do we get there well first a quick
71:42 how do we get there well first a quick tour of what it is we're going to do
71:44 tour of what it is we're going to do this in scratch is perhaps the simplest
71:47 this in scratch is perhaps the simplest program you can write and even if you've
71:48 program you can write and even if you've never seen scratch or any programming
71:50 never seen scratch or any programming language before can probably guess that
71:52 language before can probably guess that this just says on the screen somehow
71:54 this just says on the screen somehow hello world but what you don't have to
71:56 hello world but what you don't have to do is type esoteric commands and weird
71:58 do is type esoteric commands and weird syntax those curly braces and
71:59 syntax those curly braces and parentheses I keep alluding to you just
72:01 parentheses I keep alluding to you just drag this yellow puzzle piece you drag
72:03 drag this yellow puzzle piece you drag this purple puzzle piece let them
72:05 this purple puzzle piece let them magnetically lock together so to speak
72:07 magnetically lock together so to speak click a button and boom with those same
72:09 click a button and boom with those same building blocks and several others can
72:11 building blocks and several others can you make what exactly the sorts of
72:12 you make what exactly the sorts of things that Andrew brought to life as
72:14 things that Andrew brought to life as well so here's what we're about to see
72:17 well so here's what we're about to see at scratch.mit.edu is a cloud-based
72:20 at scratch.mit.edu is a cloud-based programming environment on MIT servers
72:22 programming environment on MIT servers you can also download it offline on your
72:24 you can also download it offline on your own Mac or PC and it gives you an
72:26 own Mac or PC and it gives you an interface like this on the left hand
72:29 interface like this on the left hand side of the screen you'll see a blocks
72:31 side of the screen you'll see a blocks pette these puzzle pieces AKA blocks
72:34 pette these puzzle pieces AKA blocks come in different colors which rather
72:36 come in different colors which rather categorize them so pictured here for
72:37 categorize them so pictured here for instance in blue are a whole bunch of
72:39 instance in blue are a whole bunch of motion related blocks so Andrew used a
72:42 motion related blocks so Andrew used a whole bunch of those to have the singer
72:43 whole bunch of those to have the singer and the men moving around on the screen
72:46 and the men moving around on the screen um in synchronicity with the song that
72:48 um in synchronicity with the song that was playing in the background meanwhile
72:50 was playing in the background meanwhile in the middle of this interface is going
72:52 in the middle of this interface is going to be the code area and this is where
72:54 to be the code area and this is where Andrew and sunu will drag and drop some
72:56 Andrew and sunu will drag and drop some of those puzzle pieces and other colors
72:58 of those puzzle pieces and other colors as well and lock them together to get
73:00 as well and lock them together to get your character soon to be invented to do
73:03 your character soon to be invented to do something on the screen indeed at the
73:05 something on the screen indeed at the bottom right here will you see
73:06 bottom right here will you see ultimately a Sprite area where a Sprite
73:09 ultimately a Sprite area where a Sprite is a technical term for like a character
73:11 is a technical term for like a character in a video game or a programming
73:13 in a video game or a programming environment like this by default
73:14 environment like this by default historically scratch uh is the cat the
73:18 historically scratch uh is the cat the mascot if you will for this programming
73:19 mascot if you will for this programming environment and so here we see by
73:21 environment and so here we see by default just one Sprite selected because
73:23 default just one Sprite selected because on the top right of the screen is the
73:25 on the top right of the screen is the stage for that Sprite and you can click
73:27 stage for that Sprite and you can click in Click and zoom in to make it full
73:29 in Click and zoom in to make it full screen but this is the world in which
73:31 screen but this is the world in which Scratch by default the cat will live but
73:33 Scratch by default the cat will live but you can change scratch's costume so that
73:35 you can change scratch's costume so that it looks like a singer or a man falling
73:37 it looks like a singer or a man falling from the sky or the like or anything
73:39 from the sky or the like or anything else either creating the art yourself or
73:41 else either creating the art yourself or importing some of the things that come
73:42 importing some of the things that come with it or elsewhere online so what is
73:45 with it or elsewhere online so what is this world that scratch rather lives in
73:48 this world that scratch rather lives in well generally speaking we won't have to
73:49 well generally speaking we won't have to care too much about numbers because
73:51 care too much about numbers because we'll be able to ask questions like
73:53 we'll be able to ask questions like intera active ones like is scratch the
73:55 intera active ones like is scratch the cat or any character otherwise touching
73:57 cat or any character otherwise touching the edge of the screen touching
73:59 the edge of the screen touching something else but scratch does exist in
74:01 something else but scratch does exist in this
74:02 this two-dimensional uh coordinate system
74:04 two-dimensional uh coordinate system world so when the cat or any character
74:07 world so when the cat or any character is dead center in the middle that would
74:08 is dead center in the middle that would be XY location 0 comma 0 if you will
74:12 be XY location 0 comma 0 if you will meanwhile over here is 240 pixels or
74:15 meanwhile over here is 240 pixels or dots all the way to the right so this
74:17 dots all the way to the right so this would be 240 comma 0 where Y is z
74:20 would be 240 comma 0 where Y is z because it's right on that midline so
74:22 because it's right on that midline so it's neither up or below
74:23 it's neither up or below over here to the left of course would be
74:26 over here to the left of course would be 240 and 0 above the cat would be xal 0
74:29 240 and 0 above the cat would be xal 0 cuz it's right on that vertical midline
74:32 cuz it's right on that vertical midline and 180 and then down here as you might
74:34 and 180 and then down here as you might guess would be 0 comma netive 180
74:36 guess would be 0 comma netive 180 generally speaking we don't have to care
74:38 generally speaking we don't have to care about those precise pixel coordinates
74:40 about those precise pixel coordinates but it's helpful ultimately if you do
74:41 but it's helpful ultimately if you do want the cat to move up down left or
74:43 want the cat to move up down left or right having some sense of direction
74:45 right having some sense of direction according to the x- axis and y AIS as
74:47 according to the x- axis and y AIS as well can help you express your ideas
74:49 well can help you express your ideas ultimately so what might some of those
74:52 ultimately so what might some of those ideas be well let's do this I'm going to
74:54 ideas be well let's do this I'm going to go ahead and create on
74:57 go ahead and create on scratch.mit.edu just an empty screen
75:01 scratch.mit.edu just an empty screen like this one here and so this is the
75:03 like this one here and so this is the exact same interface but now I'm in my
75:05 exact same interface but now I'm in my browser uh full screen so that I can
75:07 browser uh full screen so that I can start writing some code and let's get
75:09 start writing some code and let's get that cat to say something actually on
75:10 that cat to say something actually on the screen now this takes a little bit
75:12 the screen now this takes a little bit of practice but honestly just by
75:14 of practice but honestly just by scrolling through these puzzle pieces
75:16 scrolling through these puzzle pieces can you quickly get a sense of what's
75:18 can you quickly get a sense of what's possible not just categorically but
75:20 possible not just categorically but specifically and I'll jump around
75:22 specifically and I'll jump around because I've done this of course but I'm
75:23 because I've done this of course but I'm going to go to events in yellow first
75:25 going to go to events in yellow first and I'm going to drag and drop this
75:27 and I'm going to drag and drop this first block called when green flag
75:29 first block called when green flag clicked and I've zoomed in there just to
75:31 clicked and I've zoomed in there just to make it a little more legible and notice
75:33 make it a little more legible and notice that the shape of this green flag just
75:35 that the shape of this green flag just so happens to mirror this green flag
75:38 so happens to mirror this green flag here at top next to this red stop sign
75:40 here at top next to this red stop sign of sorts and the green flag is going to
75:42 of sorts and the green flag is going to mean go and the red stop sign's going to
75:44 mean go and the red stop sign's going to mean stop to start or stop our program
75:47 mean stop to start or stop our program next week you're going to be writing a
75:48 next week you're going to be writing a textual command at your keyboard to do
75:49 textual command at your keyboard to do the exact same idea but for now it's a
75:51 the exact same idea but for now it's a button so when green flag clicked what
75:53 button so when green flag clicked what do I want scratch to do well how about
75:55 do I want scratch to do well how about we have scratch just initially say
75:57 we have scratch just initially say something like Hello World which indeed
75:59 something like Hello World which indeed historically is the first program that
76:01 historically is the first program that most any programmer might write so
76:03 most any programmer might write so anything related to what the cat looks
76:05 anything related to what the cat looks like it's doing is actually going to be
76:07 like it's doing is actually going to be under looks here in purple so I'm going
76:09 under looks here in purple so I'm going to drag over say hello and you'll notice
76:12 to drag over say hello and you'll notice something curious and different about
76:14 something curious and different about this purple block it says of course say
76:16 this purple block it says of course say in purple but then there's this white
76:18 in purple but then there's this white oval and some text that by default is
76:21 oval and some text that by default is hello cuz MIT just decided that by
76:23 hello cuz MIT just decided that by default the placeholder will be hello
76:25 default the placeholder will be hello but anytime you see this white oval it's
76:27 but anytime you see this white oval it's an opportunity to provide an input into
76:30 an opportunity to provide an input into the function called say and so here I'm
76:33 the function called say and so here I'm borrowing terminology from before
76:35 borrowing terminology from before problem solving again is all about
76:36 problem solving again is all about inputs producing outputs and in between
76:39 inputs producing outputs and in between there is some algorithm in a moment
76:41 there is some algorithm in a moment we're going to start referring to
76:42 we're going to start referring to algorithms quite frequently as functions
76:45 algorithms quite frequently as functions why because it's the implementation of
76:46 why because it's the implementation of some algorithm so let me override the
76:48 some algorithm so let me override the default with hello comma World I'll zoom
76:51 default with hello comma World I'll zoom out and now if I go to the top right of
76:53 out and now if I go to the top right of the screen and click the green flag
76:55 the screen and click the green flag we'll see hopefully my very first
76:58 we'll see hopefully my very first program in code now it wasn't a huge
77:00 program in code now it wasn't a huge lift right it only was a matter of
77:02 lift right it only was a matter of dragging and dropping puzzle pieces but
77:04 dragging and dropping puzzle pieces but what has now happened well it turns out
77:06 what has now happened well it turns out that two things have happened when I the
77:08 that two things have happened when I the human clicked on that green flag I
77:11 human clicked on that green flag I triggered what we're going to start
77:12 triggered what we're going to start calling now an event an event is
77:14 calling now an event an event is generally something graphical or
77:16 generally something graphical or interactive that just happens in a
77:17 interactive that just happens in a computer program you and I trigger
77:19 computer program you and I trigger events on our phones all day long
77:22 events on our phones all day long whenever you tap or drag or long press
77:24 whenever you tap or drag or long press or pinch or any of those gestures in
77:26 or pinch or any of those gestures in Vogue nowadays on phones you are
77:28 Vogue nowadays on phones you are triggering events and people at Apple
77:30 triggering events and people at Apple and Google and elsewhere have written
77:32 and Google and elsewhere have written code that listen for those events and do
77:35 code that listen for those events and do something when that event happens that's
77:37 something when that event happens that's what I just did when green flag is
77:39 what I just did when green flag is clicked I want something to happen
77:41 clicked I want something to happen namely I want this purple function this
77:44 namely I want this purple function this verb this action called say to do
77:46 verb this action called say to do something what do I want it to do I want
77:48 something what do I want it to do I want it to say what this input is and I'm
77:50 it to say what this input is and I'm going to introduce another vocabulary
77:51 going to introduce another vocabulary term the white ovals here are yes inputs
77:54 term the white ovals here are yes inputs very generically but in the programmer's
77:56 very generically but in the programmer's terminology they're called arguments
77:59 terminology they're called arguments otherwise known as parameters and that
78:00 otherwise known as parameters and that just means an input to a function that
78:02 just means an input to a function that modifies Its Behavior in some way when I
78:04 modifies Its Behavior in some way when I click stop that's just another event and
78:06 click stop that's just another event and that one is just built into scratch
78:08 that one is just built into scratch scratch knows that when you click the
78:09 scratch knows that when you click the green stop sign uh everything should
78:11 green stop sign uh everything should just stop automatically I don't have to
78:13 just stop automatically I don't have to write code to support that feature so
78:15 write code to support that feature so that's all fine and good hello world but
78:17 that's all fine and good hello world but if I keep doing stop and start and stop
78:20 if I keep doing stop and start and stop and start it's going to do the same
78:22 and start it's going to do the same thing again and again and it's really
78:24 thing again and again and it's really not that interesting at the end of the
78:25 not that interesting at the end of the day maybe gratifying once but it'd be
78:27 day maybe gratifying once but it'd be nice if this were a little more
78:28 nice if this were a little more interactive so it turns out that we can
78:31 interactive so it turns out that we can do that too but we need a different
78:33 do that too but we need a different mental model instead so in this case
78:35 mental model instead so in this case here when we think about this function
78:38 here when we think about this function say in this input hello world this
78:40 say in this input hello world this actually Maps pretty cleanly to this
78:41 actually Maps pretty cleanly to this model earlier that I propose is problem
78:44 model earlier that I propose is problem solving is computer science if you will
78:46 solving is computer science if you will the input to the current problem is
78:48 the input to the current problem is going to be in white here hello world
78:51 going to be in white here hello world the algorithm is the say algorithm now I
78:54 the algorithm is the say algorithm now I don't know how MIT got it to print out
78:56 don't know how MIT got it to print out the little pretty speech bubble on the
78:58 the little pretty speech bubble on the screen but they wrote those underlying
79:00 screen but they wrote those underlying low-level implementation details and
79:02 low-level implementation details and they gave me and you a purple function
79:04 they gave me and you a purple function called say that just does that for you
79:06 called say that just does that for you you and I don't have to reinvent that
79:08 you and I don't have to reinvent that wheel the output of SE is another
79:10 wheel the output of SE is another technical term now called a side effect
79:12 technical term now called a side effect a side effect is usually something
79:14 a side effect is usually something visual that happens like as a side
79:16 visual that happens like as a side effect of you calling a function and so
79:18 effect of you calling a function and so the side effect here is that the cat has
79:20 the side effect here is that the cat has this speech bubble magically appear
79:22 this speech bubble magically appear inside of which is hello world so we
79:24 inside of which is hello world so we have an input we have an output we have
79:27 have an input we have an output we have an algorithm but now we're talking about
79:28 an algorithm but now we're talking about these ideas in the context of
79:30 these ideas in the context of programming so now the input is an
79:33 programming so now the input is an argument the algorithm is a function and
79:35 argument the algorithm is a function and the output in this case is a side effect
79:38 the output in this case is a side effect terminology that you'll just hear more
79:39 terminology that you'll just hear more and more and it'll eventually sink in
79:41 and more and it'll eventually sink in but not to worry if the terminology
79:43 but not to worry if the terminology doesn't come naturally early on so what
79:45 doesn't come naturally early on so what more might I do with this let me go back
79:48 more might I do with this let me go back to scratch here and make this maybe
79:50 to scratch here and make this maybe perhaps more interactive and actually
79:52 perhaps more interactive and actually get the cat to say something a little
79:53 get the cat to say something a little more dynamically so instead of hello
79:55 more dynamically so instead of hello world why don't I get it to say hello to
79:57 world why don't I get it to say hello to me or to you or anyone else so let me do
80:00 me or to you or anyone else so let me do this let me go under say uh let me get
80:03 this let me go under say uh let me get rid of this first and you'll notice this
80:05 rid of this first and you'll notice this neat trick as soon as you start dragging
80:07 neat trick as soon as you start dragging on a block if it gets close to it it
80:09 on a block if it gets close to it it kind of goes gray and it can be
80:11 kind of goes gray and it can be magnetically snapped together you don't
80:13 magnetically snapped together you don't have to do it very precisely conversely
80:15 have to do it very precisely conversely if I want to get rid of a puzzle piece I
80:16 if I want to get rid of a puzzle piece I just drag it anywhere on the left let go
80:19 just drag it anywhere on the left let go and that deletes it or you can right
80:20 and that deletes it or you can right click or control click in a little menu
80:22 click or control click in a little menu will let you delete it as well well let
80:24 will let you delete it as well well let me do this instead under sensing which I
80:26 me do this instead under sensing which I know is there because I've done this
80:28 know is there because I've done this before are a whole bunch of things
80:29 before are a whole bunch of things related to sensing whereby the cat can
80:32 related to sensing whereby the cat can kind of feel out its World in some sense
80:35 kind of feel out its World in some sense it can do things like ask this question
80:37 it can do things like ask this question am I touching the mouse pointer like the
80:38 am I touching the mouse pointer like the user's cursor am I touching a specific
80:41 user's cursor am I touching a specific color that you can override to be
80:42 color that you can override to be anything you want is the distance to the
80:44 anything you want is the distance to the mouse pointer some specific value but
80:46 mouse pointer some specific value but for now I'm going to focus this on this
80:49 for now I'm going to focus this on this this blue puzzle piece that asks a
80:50 this blue puzzle piece that asks a question which itself is this white oval
80:53 question which itself is this white oval that I can apparently change and then
80:54 that I can apparently change and then it's going to wait for a response but
80:56 it's going to wait for a response but this puzzle piece is a little different
80:58 this puzzle piece is a little different it's a little special it comes with a
81:00 it's a little special it comes with a freebie it comes with what we're going
81:01 freebie it comes with what we're going to call technically a return value so
81:04 to call technically a return value so some functions don't just do something
81:05 some functions don't just do something on the screen they hand you back so to
81:08 on the screen they hand you back so to speak a value that you can do anything
81:10 speak a value that you can do anything that you want with nothing happens
81:12 that you want with nothing happens immediately unless you do something with
81:14 immediately unless you do something with that so-called return value so let me go
81:16 that so-called return value so let me go ahead and drag this thing over here ask
81:19 ahead and drag this thing over here ask what's your name and I'll use the
81:20 what's your name and I'll use the default question that seems a reasonable
81:22 default question that seems a reasonable place to start I'm not going to override
81:23 place to start I'm not going to override that default and now let me go ahead and
81:25 that default and now let me go ahead and zoom out let me go back to looks let me
81:28 zoom out let me go back to looks let me go to say and let me just form the
81:31 go to say and let me just form the English sentence I want so let me zoom
81:33 English sentence I want so let me zoom in here and type in hello maybe comma
81:37 in here and type in hello maybe comma space I could do David but that's that's
81:39 space I could do David but that's that's obviously not right because I'm asking
81:41 obviously not right because I'm asking for a name and then I'm like in advance
81:43 for a name and then I'm like in advance hardcoding my name that's not what I
81:45 hardcoding my name that's not what I want I just want hello comma and now let
81:47 want I just want hello comma and now let me zoom out and grab one more say block
81:50 me zoom out and grab one more say block Let Me Maybe say here okay I don't want
81:52 Let Me Maybe say here okay I don't want to say hello hello I I don't want to
81:54 to say hello hello I I don't want to just type in my own name CU again then
81:56 just type in my own name CU again then what's the point of asking the user for
81:57 what's the point of asking the user for their name but notice this if I go back
82:00 their name but notice this if I go back to the sensing block this is where that
82:03 to the sensing block this is where that oval that's blue called answer is useful
82:06 oval that's blue called answer is useful this will be the so-called return value
82:08 this will be the so-called return value of that function so I'm just going to go
82:10 of that function so I'm just going to go ahead and do this and drag and drop even
82:12 ahead and do this and drag and drop even though it's not the right size it is the
82:14 though it's not the right size it is the right shape and so scratch will be smart
82:16 right shape and so scratch will be smart about it and grow to fill that puzzle
82:18 about it and grow to fill that puzzle piece for you let me zoom out now and
82:20 piece for you let me zoom out now and now let me click the green flag you'll
82:22 now let me click the green flag you'll see that scratch is indeed prompting me
82:24 see that scratch is indeed prompting me with the speech bubble what's your name
82:25 with the speech bubble what's your name notice the little text box below the cat
82:27 notice the little text box below the cat is asking what's your name so I'm going
82:28 is asking what's your name so I'm going to type in da a v d and hit enter or I
82:31 to type in da a v d and hit enter or I can click the blue check
82:33 can click the blue check enter okay it's a little weird I wanted
82:36 enter okay it's a little weird I wanted him to say hello not just my name so let
82:38 him to say hello not just my name so let me stop let me start it again all right
82:40 me stop let me start it again all right hello what's your name da a v
82:43 hello what's your name da a v enter huh kind of rude uh why is there
82:48 enter huh kind of rude uh why is there this bug like I wanted to say hello
82:50 this bug like I wanted to say hello David not just David and and yet twice
82:53 David not just David and and yet twice it has failed to do so uh
82:57 it has failed to do so uh yeah yeah the computer is processing my
82:59 yeah yeah the computer is processing my directions my actions really quickly and
83:02 directions my actions really quickly and so it actually is doing it it's just you
83:03 so it actually is doing it it's just you and I in the room are just way too slow
83:05 and I in the room are just way too slow to notice that it said hello David it
83:07 to notice that it said hello David it just seems to have just said David so
83:10 just seems to have just said David so all right how can I fix this well here's
83:11 all right how can I fix this well here's where you start to poke around and think
83:12 where you start to poke around and think about how you might solve this let me go
83:14 about how you might solve this let me go back under looks maybe there's a smarter
83:16 back under looks maybe there's a smarter way to do this maybe I could do okay I
83:19 way to do this maybe I could do okay I could do this how about instead of just
83:20 could do this how about instead of just say hello there's apparently another
83:22 say hello there's apparently another puzzle piece where I can time it so I
83:24 puzzle piece where I can time it so I can maybe slow things down a little bit
83:26 can maybe slow things down a little bit so let me do this let me throw away all
83:29 so let me do this let me throw away all of this let me drag a say hello for two
83:32 of this let me drag a say hello for two seconds let me drag another say hello
83:35 seconds let me drag another say hello for two seconds let me change the first
83:37 for two seconds let me change the first one to indeed hello comma and then let
83:39 one to indeed hello comma and then let me go back to sensing let me grab that
83:42 me go back to sensing let me grab that same answer because I threw it away a
83:44 same answer because I threw it away a second ago and I'll just change it I
83:45 second ago and I'll just change it I don't even have to delete hello I can
83:47 don't even have to delete hello I can just overwrite it like this so now I
83:49 just overwrite it like this so now I think we'll kind of pump the brakes and
83:51 think we'll kind of pump the brakes and see things more slowly let me stop let
83:54 see things more slowly let me stop let me start da ID enter
83:57 me start da ID enter hello David okay so it's better like it
84:01 hello David okay so it's better like it seems to be working I think your
84:02 seems to be working I think your hypothesis was right just looks kind of
84:05 hypothesis was right just looks kind of stupid right like the fact that it's
84:06 stupid right like the fact that it's saying
84:07 saying hello David like we can do better and
84:10 hello David like we can do better and like literally every piece of software
84:11 like literally every piece of software on your phone or Mac or PC is better
84:13 on your phone or Mac or PC is better than that it adds words together in the
84:16 than that it adds words together in the user interfaces you and I are familiar
84:17 user interfaces you and I are familiar with so let's go a little more fishing
84:19 with so let's go a little more fishing here let me throw away these let me go
84:21 here let me throw away these let me go back to look and just get the simpler
84:23 back to look and just get the simpler say I want this to say hello comma name
84:27 say I want this to say hello comma name where name comes from that answer return
84:29 where name comes from that answer return value so how can I do this well let me
84:31 value so how can I do this well let me go under operations which we haven't
84:32 go under operations which we haven't been before there's a lot of stuff in
84:34 been before there's a lot of stuff in here some of it's mathematically related
84:36 here some of it's mathematically related adding subtracting and so forth you can
84:38 adding subtracting and so forth you can Generate random numbers which might be
84:40 Generate random numbers which might be useful and if I keep scrolling down
84:42 useful and if I keep scrolling down there's this join apple and banana but
84:44 there's this join apple and banana but that's just placeholder text you can
84:45 that's just placeholder text you can join one piece of text with another
84:48 join one piece of text with another piece of text by default apple and
84:50 piece of text by default apple and banana but let's change it to hello and
84:52 banana but let's change it to hello and my name name so this to wrong size but
84:54 my name name so this to wrong size but right shape so let me let it snap into
84:56 right shape so let me let it snap into place let me go ahead now and do hello
84:59 place let me go ahead now and do hello comma and now I think I just want to go
85:02 comma and now I think I just want to go grab that answer return value let me
85:05 grab that answer return value let me drag the same oval as before clobber
85:08 drag the same oval as before clobber that is overwrite banana so now I'm kind
85:10 that is overwrite banana so now I'm kind of composing functions the output of one
85:14 of composing functions the output of one function join is going to be the input
85:16 function join is going to be the input of another function say so let's see
85:19 of another function say so let's see what happens now that they're kind of
85:20 what happens now that they're kind of stacked on top of each other or nested
85:22 stacked on top of each other or nested so to speak click the Green Arrow a
85:24 so to speak click the Green Arrow a green flag DAV ID enter hello David all
85:28 green flag DAV ID enter hello David all right that was pretty fast let's just
85:29 right that was pretty fast let's just just do it once more stop start here we
85:31 just do it once more stop start here we go daavid enter okay right it's not the
85:35 go daavid enter okay right it's not the most exciting program in the world but
85:37 most exciting program in the world but it's more correct it's better design
85:38 it's more correct it's better design just because that's what you would kind
85:39 just because that's what you would kind of expect the software to do and not be
85:41 of expect the software to do and not be some kind of lame user interface that's
85:43 some kind of lame user interface that's just inserting random delays to just
85:45 just inserting random delays to just make it kind of work like that's a
85:46 make it kind of work like that's a workaround a hack if you will but
85:48 workaround a hack if you will but there's some cool things you can do with
85:50 there's some cool things you can do with scratch and we won't really go down the
85:51 scratch and we won't really go down the rabbit hole of all of the fun and
85:53 rabbit hole of all of the fun and familyfriendly features that it has but
85:55 familyfriendly features that it has but there is one that's kind of cool here
85:57 there is one that's kind of cool here let me go into the extensions button at
85:59 let me go into the extensions button at the bottom left of my screen and this
86:01 the bottom left of my screen and this one's kind of cool let me go to text to
86:03 one's kind of cool let me go to text to speech and you'll notice that this one
86:04 speech and you'll notice that this one requires internet because it's
86:06 requires internet because it's cloud-based but this just gave me some
86:09 cloud-based but this just gave me some new puzzle pieces in a new category text
86:12 new puzzle pieces in a new category text to speech and these green ones do
86:13 to speech and these green ones do exactly what they say so let me do this
86:16 exactly what they say so let me do this let me zoom out again let me keep the
86:18 let me zoom out again let me keep the join block and I'm just going to
86:19 join block and I'm just going to temporarily toss it over here it's not
86:21 temporarily toss it over here it's not going to delete itself cuz I didn't drag
86:22 going to delete itself cuz I didn't drag it over to the other side but I'm going
86:24 it over to the other side but I'm going to get rid of the say block in purple
86:27 to get rid of the say block in purple I'm going to do the speak block here in
86:29 I'm going to do the speak block here in green and let it snap into place and
86:32 green and let it snap into place and then I'm going to drag and drop this
86:33 then I'm going to drag and drop this onto the input to speak and now perhaps
86:36 onto the input to speak and now perhaps a little more adorably let's try this
86:38 a little more adorably let's try this green flag what's your name d ID enter
86:42 green flag what's your name d ID enter and hello
86:44 and hello David okay it's a little it's a little
86:47 David okay it's a little it's a little robotic but at least now it has
86:49 robotic but at least now it has synthesized speech and I've kind of got
86:50 synthesized speech and I've kind of got my own like Siri or Google assistant or
86:52 my own like Siri or Google assistant or Alexa thing going on here now where it's
86:54 Alexa thing going on here now where it's now recognized whatever text it is and
86:56 now recognized whatever text it is and it's played it well let's make this an
86:59 it's played it well let's make this an actual cat that doesn't talk in that
87:00 actual cat that doesn't talk in that weird human voice let me go ahead and
87:02 weird human voice let me go ahead and get rid of most of this stuff and let's
87:04 get rid of most of this stuff and let's get the cat to actually meow like a cat
87:06 get the cat to actually meow like a cat tends to and let me go under the sounds
87:08 tends to and let me go under the sounds block now MIT gives you a few sounds for
87:10 block now MIT gives you a few sounds for free because it's designed around a cap
87:12 free because it's designed around a cap by default and I'm going to go ahead and
87:14 by default and I'm going to go ahead and grab this one play sound meow until done
87:17 grab this one play sound meow until done and now and we saw I heard a teaser for
87:19 and now and we saw I heard a teaser for this earlier in the
87:21 this earlier in the crowd
87:23 crowd it's a little piercing admittedly we can
87:25 it's a little piercing admittedly we can lower the volume a little bit there but
87:26 lower the volume a little bit there but notice if I want the cat to meow a
87:28 notice if I want the cat to meow a second time I'll just click it
87:30 second time I'll just click it again okay and over there too I
87:33 again okay and over there too I hear okay all right so it's kind of cute
87:37 hear okay all right so it's kind of cute now right so it's just meow okay yes
87:40 now right so it's just meow okay yes echo echo so it's meowing now every time
87:43 echo echo so it's meowing now every time I hit the green flag now that's great
87:45 I hit the green flag now that's great but even a kid is probably going to like
87:47 but even a kid is probably going to like would prefer that it just meow perhaps
87:49 would prefer that it just meow perhaps like again and again without having to
87:51 like again and again without having to keep hitting the button so well how
87:53 keep hitting the button so well how might we do this all right well if I
87:54 might we do this all right well if I want it to meow multiple times why don't
87:57 want it to meow multiple times why don't I just like grab it another time and
87:59 I just like grab it another time and another time alternatively you can right
88:02 another time alternatively you can right click or control click a puzzle piece
88:03 click or control click a puzzle piece and just duplicate it from a little menu
88:05 and just duplicate it from a little menu that drops down so here we go three
88:11 meows all right that's not really a happy cat it sounds maybe hungry so can
88:13 happy cat it sounds maybe hungry so can we slow that down well maybe in fact if
88:17 we slow that down well maybe in fact if I poke around let me go under control
88:19 I poke around let me go under control looks like there's a weight block wait 1
88:21 looks like there's a weight block wait 1 second by default and notice scratch
88:23 second by default and notice scratch will be pretty accommodating if you just
88:25 will be pretty accommodating if you just hover in between blocks it will grow to
88:27 hover in between blocks it will grow to fill that too so I could change it to
88:29 fill that too so I could change it to one or two or anything seconds I'll just
88:31 one or two or anything seconds I'll just leave it at the default for now one and
88:33 leave it at the default for now one and now I'll go ahead and do
88:41 this okay so cuter and less hungry and just more friendly but this isn't the
88:44 just more friendly but this isn't the best design it is correct and let's use
88:46 best design it is correct and let's use that as a term of art correct means the
88:48 that as a term of art correct means the code does what you want it to do I want
88:50 code does what you want it to do I want the cat to meow three times slow slly
88:52 the cat to meow three times slow slly and it did so iager this is correct but
88:55 and it did so iager this is correct but it's not the best design and this is
88:57 it's not the best design and this is where things get more subjective right
88:59 where things get more subjective right like you could write accurate sentences
89:01 like you could write accurate sentences in an essay for an English class but
89:03 in an essay for an English class but otherwise just it's just completely a
89:05 otherwise just it's just completely a mess like your arguments here and there
89:06 mess like your arguments here and there and you don't say anything wrong but you
89:08 and you don't say anything wrong but you don't say it well in the context of code
89:11 don't say it well in the context of code we can do better than this and copy
89:13 we can do better than this and copy paste or repeating yourself again and
89:14 paste or repeating yourself again and again tends to be bad practice why
89:17 again tends to be bad practice why suppose that you want to change the
89:19 suppose that you want to change the weight to two seconds instead of one
89:21 weight to two seconds instead of one it's aditt not a big deal F I click
89:23 it's aditt not a big deal F I click there I change it to two I click there I
89:25 there I change it to two I click there I change it to two but what if you Ma five
89:27 change it to two but what if you Ma five times 10 times now I have to change the
89:29 times 10 times now I have to change the weight like in five 10 different places
89:31 weight like in five 10 different places like that's just stupid it's taking
89:32 like that's just stupid it's taking unnecessary human time and you're going
89:34 unnecessary human time and you're going to screw up eventually especially if
89:36 to screw up eventually especially if your program is getting longer you're
89:37 your program is getting longer you're going to miss one of the inputs you're
89:39 going to miss one of the inputs you're going to leave the number wrong and
89:40 going to leave the number wrong and you're that's a bug so just based on
89:42 you're that's a bug so just based on what you've seen already or if you've
89:43 what you've seen already or if you've programmed before which a few of you
89:45 programmed before which a few of you have what's the term of art here that
89:47 have what's the term of art here that will solve this how can we design this
89:50 will solve this how can we design this better I heard it here here yeah so a
89:53 better I heard it here here yeah so a loop a loop some kind of cycle that says
89:55 loop a loop some kind of cycle that says do that again do that again not
89:57 do that again do that again not infinitely many times necessarily but
89:59 infinitely many times necessarily but some finite number well you can perhaps
90:01 some finite number well you can perhaps see a spoiler on the screen under the
90:03 see a spoiler on the screen under the same uh orange control category is a
90:06 same uh orange control category is a repeat block and by default it's
90:07 repeat block and by default it's proposing 10 but we can change that so
90:09 proposing 10 but we can change that so let me do this I'm going to throw away
90:11 let me do this I'm going to throw away most of this copy paste as redundant I'm
90:14 most of this copy paste as redundant I'm going to detach this temporarily just to
90:16 going to detach this temporarily just to make room for something else and I'm
90:18 make room for something else and I'm going to drag a repeat block over here
90:20 going to drag a repeat block over here and let that snap into place and I'm
90:22 and let that snap into place and I'm going to change it for now just to be
90:23 going to change it for now just to be three for consistency and this is the
90:25 three for consistency and this is the correct shape even though it's too small
90:26 correct shape even though it's too small but scratch will accommodate that for us
90:29 but scratch will accommodate that for us and now same uh same output but arguably
90:33 and now same uh same output but arguably better designed why because if I want to
90:35 better designed why because if I want to change the number of meows I change it
90:37 change the number of meows I change it in one place no copy paste messiness if
90:39 in one place no copy paste messiness if I want to change the waiting one place I
90:41 I want to change the waiting one place I don't have to change it in multiple
90:42 don't have to change it in multiple places and not screw up so let me hit
90:44 places and not screw up so let me hit the green
90:52 flag all right so nice now it would have been nice if MIT had just given us a
90:54 been nice if MIT had just given us a meow block that just automates all of
90:56 meow block that just automates all of this for us let me wager they gave us
90:58 this for us let me wager they gave us the low-level implementation details
91:00 the low-level implementation details they gave us the play sound meow but I
91:03 they gave us the play sound meow but I had to implement like a decent number of
91:05 had to implement like a decent number of blocks just to get a cat to meow again
91:06 blocks just to get a cat to meow again and again I feel like we should have
91:08 and again I feel like we should have gotten that for free from MIT well they
91:11 gotten that for free from MIT well they don't have to be the only ones that
91:12 don't have to be the only ones that invent blocks for us to use you can
91:15 invent blocks for us to use you can write your own functions your own verbs
91:17 write your own functions your own verbs or actions so how can we do this let's
91:19 or actions so how can we do this let's make our own puzzle piece called now
91:22 make our own puzzle piece called now that uses this code but creates it in
91:24 that uses this code but creates it in such a way that it's reusable elsewhere
91:27 such a way that it's reusable elsewhere so let me do this under my blocks in
91:29 so let me do this under my blocks in pink here I'm going to go ahead and
91:30 pink here I'm going to go ahead and click literally make a block now here's
91:33 click literally make a block now here's an interface via which I can give the
91:34 an interface via which I can give the block a name Meo W will be the name of
91:37 block a name Meo W will be the name of this block and I'm just going to go
91:38 this block and I'm just going to go ahead and quickly click okay that just
91:40 ahead and quickly click okay that just gives me a very generic pink puzzle
91:42 gives me a very generic pink puzzle piece that starts with the word Define
91:45 piece that starts with the word Define because scratch is asking me to Define
91:47 because scratch is asking me to Define that is implement or create this new
91:49 that is implement or create this new puzzle piece for me well what does it
91:51 puzzle piece for me well what does it mean to meow I'm going to claim that it
91:53 mean to meow I'm going to claim that it means to do these two steps to play the
91:56 means to do these two steps to play the sound meow and then just wait for one
91:58 sound meow and then just wait for one second but what's powerful about this
92:01 second but what's powerful about this idea is look at this up top now that
92:03 idea is look at this up top now that I've made a block it exists in scratch D
92:06 I've made a block it exists in scratch D MIT didn't need to create this for me I
92:08 MIT didn't need to create this for me I created it for myself and even you if we
92:10 created it for myself and even you if we end up sharing code so I can now drag
92:12 end up sharing code so I can now drag meow up in here and what's nice about
92:15 meow up in here and what's nice about meow is that itself is yes a function
92:18 meow is that itself is yes a function but it's also an abstraction like never
92:20 but it's also an abstraction like never again do I or even you need to worry or
92:23 again do I or even you need to worry or care about what it means to meow or
92:26 care about what it means to meow or implement it I can sort of drag it out
92:27 implement it I can sort of drag it out of the way I didn't delete it drag it
92:29 of the way I didn't delete it drag it out of the way out of sight out of mind
92:31 out of the way out of sight out of mind why because my code is now even better
92:33 why because my code is now even better designed in some sense because it's more
92:34 designed in some sense because it's more readable what is it doing when the green
92:36 readable what is it doing when the green flag is clicked repeat three times meow
92:39 flag is clicked repeat three times meow it just says what it means and so it's a
92:41 it just says what it means and so it's a lot easier to read it and it's a lot
92:42 lot easier to read it and it's a lot easier to think about it especially if
92:44 easier to think about it especially if you're using Meow in other uh projects
92:46 you're using Meow in other uh projects too now let me go ahead and right hit
92:49 too now let me go ahead and right hit click play same thing so it's not really
92:52 click play same thing so it's not really fundamentally any different but I can
92:54 fundamentally any different but I can make this custom puzzle piece this own
92:57 make this custom puzzle piece this own function of M meow even more powerful
92:59 function of M meow even more powerful let me kind of rewind a bit and go to my
93:01 let me kind of rewind a bit and go to my meow puzzle piece and I am going to
93:03 meow puzzle piece and I am going to control click or right click on my pink
93:05 control click or right click on my pink puzzle piece and I'm going to edit it so
93:07 puzzle piece and I'm going to edit it so I kind of regret making meow so simple
93:09 I kind of regret making meow so simple wouldn't it be nice if meow took an
93:11 wouldn't it be nice if meow took an input AKA an argument that tells meow
93:15 input AKA an argument that tells meow how many times to meow then I can get
93:16 how many times to meow then I can get rid of that Loop and just tell meow how
93:19 rid of that Loop and just tell meow how many meows I actually want so I'm going
93:21 many meows I actually want so I'm going to click on another button here called
93:22 to click on another button here called literally add an input and it's going to
93:25 literally add an input and it's going to have placeholder here so I'm just going
93:27 have placeholder here so I'm just going to put a placeholder there I keep using
93:28 to put a placeholder there I keep using n for number which is a go-to in
93:31 n for number which is a go-to in computer scientist terms um and I'm
93:32 computer scientist terms um and I'm going to add some descriptive text just
93:34 going to add some descriptive text just so that it's a little more
93:35 so that it's a little more self-explanatory I'm just going to say
93:37 self-explanatory I'm just going to say meow n times but there's only one oval
93:40 meow n times but there's only one oval times is just going to be explanatory
93:41 times is just going to be explanatory text and now notice what has happened
93:43 text and now notice what has happened now my puzzle piece takes an input AKA
93:47 now my puzzle piece takes an input AKA an argument that will tell that function
93:49 an argument that will tell that function to meow some number of times but it's
93:51 to meow some number of times but it's not just going to going to work
93:52 not just going to going to work magically I need to implement that lower
93:54 magically I need to implement that lower level detail so let me zoom out I have
93:57 level detail so let me zoom out I have to remind myself what this function was
93:59 to remind myself what this function was so I'm going to drag it higher up just
94:00 so I'm going to drag it higher up just so they're on the screen at the same
94:02 so they're on the screen at the same time I'm going to go ahead now and
94:04 time I'm going to go ahead now and temporarily move this over here I'm
94:06 temporarily move this over here I'm going to temporarily detach this over
94:08 going to temporarily detach this over here why because what I think I want to
94:10 here why because what I think I want to do is move my Loop into the function
94:13 do is move my Loop into the function itself move the play and the weight into
94:17 itself move the play and the weight into the loop but I don't want to hardcode
94:19 the loop but I don't want to hardcode three notice that n here is its own oval
94:22 three notice that n here is its own oval I can drag a copy of N and just let it
94:24 I can drag a copy of N and just let it go there so now I have a new version of
94:27 go there so now I have a new version of meow that takes an argument in that
94:30 meow that takes an argument in that tells meow how many times to meow and
94:32 tells meow how many times to meow and now let me again drag this out of sight
94:35 now let me again drag this out of sight out of mind because who cares how I
94:36 out of mind because who cares how I implemented it once it's implemented
94:38 implemented it once it's implemented it's sort of done now my program is even
94:40 it's sort of done now my program is even better designed in some sense why
94:43 better designed in some sense why because now it really just says what it
94:45 because now it really just says what it means there's no Loop there's no repeat
94:46 means there's no Loop there's no repeat no like implementation details when
94:49 no like implementation details when green flag clicked meow three times and
94:51 green flag clicked meow three times and so functions indeed let you implement
94:54 so functions indeed let you implement algorithms like they're just code that
94:56 algorithms like they're just code that do something for you but they're also
94:58 do something for you but they're also themselves abstractions why because once
95:00 themselves abstractions why because once a function exists it has a name and you
95:02 a function exists it has a name and you can think about it in that term and you
95:04 can think about it in that term and you can use it by its name you don't have to
95:06 can use it by its name you don't have to care or remember how the function itself
95:09 care or remember how the function itself was built whether it's by you or even
95:11 was built whether it's by you or even MIT so again here I'll click the green
95:13 MIT so again here I'll click the green flag it's the same
95:21 thing so still correct but better and better designed and so anytime here and
95:23 better designed and so anytime here and out with scratch or soon C and
95:25 out with scratch or soon C and eventually python when you find yourself
95:27 eventually python when you find yourself doing anything resembling copy paste or
95:29 doing anything resembling copy paste or again and again grabbing the same code
95:31 again and again grabbing the same code probably an opportunity to say wait a
95:33 probably an opportunity to say wait a minute let me refactor this so to speak
95:35 minute let me refactor this so to speak that is rip out the code that seems to
95:37 that is rip out the code that seems to be repeated again and again and put it
95:39 be repeated again and again and put it in its own function so you can give it a
95:41 in its own function so you can give it a descriptive name and use and reuse it
95:44 descriptive name and use and reuse it any questions just yet on now saying or
95:47 any questions just yet on now saying or these Loops or these functions that
95:49 these Loops or these functions that we're using yeah
95:52 we're using yeah [Music]
95:54 [Music] how did I make it so it meows three
95:56 how did I make it so it meows three times so I originally only had a puzzle
95:58 times so I originally only had a puzzle piece called meow and I decided to
96:00 piece called meow and I decided to improve it so I held down control and I
96:02 improve it so I held down control and I right clicked or control clicked on the
96:04 right clicked or control clicked on the pink puzzle piece at top left and I
96:06 pink puzzle piece at top left and I clicked edit and that brought back the
96:08 clicked edit and that brought back the original interface that lets me add some
96:10 original interface that lets me add some arguments to the puzzle piece itself and
96:13 arguments to the puzzle piece itself and I clicked add an input on the left here
96:15 I clicked add an input on the left here and then I clicked on add a label over
96:17 and then I clicked on add a label over here so that just lets you customize it
96:20 here so that just lets you customize it even further all right so we've done
96:22 even further all right so we've done this let's add one of those other
96:24 this let's add one of those other Primitives too to do something
96:25 Primitives too to do something optionally so how about we make the cat
96:28 optionally so how about we make the cat meow only if it's being petted by a
96:30 meow only if it's being petted by a human as by moving the mouse to hover
96:32 human as by moving the mouse to hover over the cat like a human would pet a
96:34 over the cat like a human would pet a cat well let me go ahead and throw uh
96:36 cat well let me go ahead and throw uh away the meowing uh for now and let me
96:39 away the meowing uh for now and let me simplify it by just using a sound I'm
96:41 simplify it by just using a sound I'm going to go ahead and do this I'm going
96:42 going to go ahead and do this I'm going to go ahead and have a control block
96:46 to go ahead and have a control block that says if because I want to implement
96:48 that says if because I want to implement the idea of if the cursor is touching
96:50 the idea of if the cursor is touching the Cal
96:51 the Cal then play sound meow or I could use my
96:53 then play sound meow or I could use my same pink puzzle piece but I'm going to
96:55 same pink puzzle piece but I'm going to throw that away and focus only now on
96:57 throw that away and focus only now on the sounds and I'm going to do this uh
97:00 the sounds and I'm going to do this uh if uh touching Mouse pointer so I need
97:02 if uh touching Mouse pointer so I need to sense something about the world and
97:03 to sense something about the world and we saw this earlier so if touching Mouse
97:06 we saw this earlier so if touching Mouse pointer so notice this shape here way
97:08 pointer so notice this shape here way too big but it is the right shape so if
97:10 too big but it is the right shape so if I hover just right it'll snap into place
97:13 I hover just right it'll snap into place and this now in blue is my Boolean
97:15 and this now in blue is my Boolean expression a yes no question true false
97:19 expression a yes no question true false uh if is a conditional and what do I
97:21 uh if is a conditional and what do I want to do well if the Cat is being uh
97:25 want to do well if the Cat is being uh is touching the mouse pointer I want to
97:27 is touching the mouse pointer I want to go ahead and play sound meow until done
97:29 go ahead and play sound meow until done so let's do this I'm going to hit green
97:31 so let's do this I'm going to hit green flag click now nothing's happened yet
97:34 flag click now nothing's happened yet because it's a conditional right it's
97:35 because it's a conditional right it's only supposed to do something if I'm
97:37 only supposed to do something if I'm touching the cat let me move the cursor
97:39 touching the cat let me move the cursor over to the cat and and and wait for
97:42 over to the cat and and and wait for it h another bug why is the cat not
97:48 it h another bug why is the cat not meowing even though I very explicitly
97:50 meowing even though I very explicitly said if touch in Mouse pointer meow yeah
97:53 said if touch in Mouse pointer meow yeah in the
97:58 middle yeah this is again my computers are so darn fast like yours I click the
98:00 are so darn fast like yours I click the green flag it asked the question am I
98:02 green flag it asked the question am I touching the mouse pointer well no cuz
98:03 touching the mouse pointer well no cuz my cursor was up there not touching the
98:05 my cursor was up there not touching the cat it's too late the cat's out of the
98:07 cat it's too late the cat's out of the bag and so we have to instead solve this
98:10 bag and so we have to instead solve this some other mean by some other means how
98:12 some other mean by some other means how can we fix this how do we fix that sort
98:15 can we fix this how do we fix that sort of race yeah yeah so why don't we just
98:18 of race yeah yeah so why don't we just keep asking the question until I
98:20 keep asking the question until I eventually am I'm not actually petting
98:22 eventually am I'm not actually petting the cat so let me detach this
98:24 the cat so let me detach this temporarily let me go under control let
98:26 temporarily let me go under control let me go under instead of repeat some
98:28 me go under instead of repeat some finite number of times let's just do it
98:29 finite number of times let's just do it forever so sometimes Loops that do work
98:31 forever so sometimes Loops that do work forever are a good thing like the clock
98:33 forever are a good thing like the clock on your phone that's in a loop forever
98:36 on your phone that's in a loop forever because you want it to always tell time
98:37 because you want it to always tell time and not stop at the end of the day so
98:39 and not stop at the end of the day so sometimes you do want code to Loop
98:41 sometimes you do want code to Loop forever as in this case so let me go
98:43 forever as in this case so let me go ahead and drag and drop it there let me
98:45 ahead and drag and drop it there let me again click the green flag nothing's
98:47 again click the green flag nothing's happening yet but notice the program's
98:49 happening yet but notice the program's still running and so if if I move my
98:51 still running and so if if I move my cursor move my cursor move my cursor
98:56 cursor move my cursor move my cursor and okay so maybe we could add some
98:58 and okay so maybe we could add some waiting but the cat does not want to be
99:01 waiting but the cat does not want to be pet in this case but it's indeed
99:04 pet in this case but it's indeed conditional so there we have an
99:05 conditional so there we have an incarnation in scratch of doing
99:07 incarnation in scratch of doing something conditionally now we can make
99:09 something conditionally now we can make this really cool really fast if you will
99:11 this really cool really fast if you will let me stop this version let me go ahead
99:13 let me stop this version let me go ahead and do this uh let me go ahead and throw
99:16 and do this uh let me go ahead and throw all of this away let me go into my
99:18 all of this away let me go into my little uh extensions Bucket over here
99:20 little uh extensions Bucket over here and let me do video sensing since most
99:22 and let me do video sensing since most uh laptops or phones these days have
99:24 uh laptops or phones these days have cameras and there indeed I am with
99:27 cameras and there indeed I am with Sanders behind me and let me do this um
99:30 Sanders behind me and let me do this um when video motion and let me get out of
99:32 when video motion and let me get out of the way when video motion is greater
99:35 the way when video motion is greater than some value so 10 is the default
99:37 than some value so 10 is the default this is just a number that measures how
99:39 this is just a number that measures how much motion there is or isn't so small
99:41 much motion there is or isn't so small number is like no motion big number is
99:43 number is like no motion big number is lots of motion so I'm going to choose 50
99:45 lots of motion so I'm going to choose 50 somewhat arbitrarily here so 50 this is
99:47 somewhat arbitrarily here so 50 this is not normal to program off to the side
99:49 not normal to program off to the side but I'm now going to say this when video
99:53 but I'm now going to say this when video motion is 50 go ahead and play sound
99:56 motion is 50 go ahead and play sound meow like this so the cat is still in
100:00 meow like this so the cat is still in that world I'm going to stop the program
100:01 that world I'm going to stop the program and rerun it so here we go green flag
100:05 and rerun it so here we go green flag and
100:06 and now here come all right this is a little
100:08 now here come all right this is a little creepy the way I'm petting the cat
100:10 creepy the way I'm petting the cat but
100:19 and ah okay there we go okay so 50 was too big
100:21 okay there we go okay so 50 was too big of a number I have to pet the cat
100:34 know yeah so okay so you can make things even more
100:37 so okay so you can make things even more interactive in this way by just
100:38 interactive in this way by just assembling different puzzle pieces and
100:39 assembling different puzzle pieces and honestly there are so many different
100:41 honestly there are so many different puzzle pieces in here we're not going to
100:42 puzzle pieces in here we're not going to even scratch the surface of a lot of
100:44 even scratch the surface of a lot of them but they generally just do what
100:46 them but they generally just do what they say and indeed when you see on the
100:48 they say and indeed when you see on the screen here um this pallet of puzzle
100:51 screen here um this pallet of puzzle pieces really a lot of programming
100:52 pieces really a lot of programming especially early on when learning a
100:54 especially early on when learning a language is just trying different things
100:55 language is just trying different things and try and fail and if it doesn't work
100:58 and try and fail and if it doesn't work quite look for doesn't work quite right
100:59 quite look for doesn't work quite right look for an alternative solution there
101:01 look for an alternative solution there too as even I just had to do a moment
101:03 too as even I just had to do a moment ago well let's go ahead and use actually
101:06 ago well let's go ahead and use actually how about another example of something a
101:07 how about another example of something a predecessor of yours made let me go
101:09 predecessor of yours made let me go ahead and grab a program I opened in
101:11 ahead and grab a program I opened in advance here uh called wacka um might we
101:14 advance here uh called wacka um might we get a brave volunteer to come up who is
101:17 get a brave volunteer to come up who is willing to whack a mole with their head
101:22 willing to whack a mole with their head virtually maybe okay let's see how about
101:24 virtually maybe okay let's see how about in way back you want to come on down all
101:26 in way back you want to come on down all right come on down and in just a sure
101:30 right come on down and in just a sure Round of Applause for our
101:47 have come on down there what's your name I'm Josh oh actually say it into the
101:48 I'm Josh oh actually say it into the microphone hi I'm Josh
101:51 microphone hi I'm Josh okay nice welcome Josh come on
101:54 okay nice welcome Josh come on over all right so same idea here I'll
101:57 over all right so same idea here I'll take the mic back you can you'll have to
101:58 take the mic back you can you'll have to stand in front of the camera in just a
101:59 stand in front of the camera in just a moment you're going to have to position
102:01 moment you're going to have to position your head in a box that your classmate
102:03 your head in a box that your classmate from yester year
102:09 created and we'll start with beginner okay so line your head up in the Box in
102:11 okay so line your head up in the Box in a moment all right all
102:14 a moment all right all [Music]
102:14 [Music] [Applause]
102:28 [Music] nice 12
102:39 seconds 5 Seconds notice the score is up to 18 already pretty
102:41 to 18 already pretty good all right a round of applause for
102:44 good all right a round of applause for Josh if you
102:52 can so notice how using some fairly simple Primitives things do get
102:54 simple Primitives things do get interesting pretty fast and how was that
102:56 interesting pretty fast and how was that implemented well there were probably at
102:57 implemented well there were probably at least four Sprites so you're not re
102:59 least four Sprites so you're not re confined to just one cat you can create
103:01 confined to just one cat you can create more and more Sprites change what they
103:02 more and more Sprites change what they look like so they actually look like a
103:04 look like so they actually look like a mole in this case there's probably some
103:06 mole in this case there's probably some conditionals in there Some Loops for 30
103:07 conditionals in there Some Loops for 30 seconds that's checking if Josh's head's
103:10 seconds that's checking if Josh's head's movement is exceeding some value over
103:12 movement is exceeding some value over this way or over this way then increment
103:14 this way or over this way then increment something called a variable we'll see
103:16 something called a variable we'll see those two just like in algebra you might
103:18 those two just like in algebra you might have X and Y and Z storing values like
103:20 have X and Y and Z storing values like numbers so can computer programs have
103:22 numbers so can computer programs have variables called X or Y or Z or more
103:24 variables called X or Y or Z or more descriptively called score as in this
103:27 descriptively called score as in this case at top right or another variable
103:29 case at top right or another variable called countdown typically one word in
103:31 called countdown typically one word in code but in this case two words that
103:32 code but in this case two words that just store some value so there's
103:34 just store some value so there's probably some math going on in there
103:36 probably some math going on in there whereby the author of this program just
103:38 whereby the author of this program just is incrementing that is adding one in
103:40 is incrementing that is adding one in one every time it detected that uh a
103:42 one every time it detected that uh a mole had been whacked in this case with
103:44 mole had been whacked in this case with movement so back in the day I myself
103:46 movement so back in the day I myself actually implemented my very first
103:48 actually implemented my very first program in scratch when I was at a
103:50 program in scratch when I was at a graduate student actually at MIT um cost
103:52 graduate student actually at MIT um cost registered at MIT taking a class from
103:54 registered at MIT taking a class from mit's media lab specifically the
103:56 mit's media lab specifically the lifelong kindergarten Group which is the
103:58 lifelong kindergarten Group which is the group that created scratch itself and
104:00 group that created scratch itself and the program I wrote all those years ago
104:03 the program I wrote all those years ago and still rather cling to is a little
104:05 and still rather cling to is a little something here called Oscar time that I
104:07 something here called Oscar time that I thought I'd play just a quick excerpt of
104:10 thought I'd play just a quick excerpt of my myself here so in this case consider
104:13 my myself here so in this case consider as the music starts playing how this
104:15 as the music starts playing how this program which is much more sophisticated
104:17 program which is much more sophisticated certainly than the earliest say hello
104:19 certainly than the earliest say hello examples we just did might also be
104:21 examples we just did might also be implemented let me go ahead now and
104:23 implemented let me go ahead now and click the green
104:33 flag so some trash is moving presumably in some kind of loop from the
104:35 in some kind of loop from the Top If I'm touching the mouse cursor it
104:38 Top If I'm touching the mouse cursor it follows
104:39 follows me if I hover over the trash can it
104:43 me if I hover over the trash can it responds if I let
104:45 responds if I let go in some kind of loop Oscar pops out
104:48 go in some kind of loop Oscar pops out creates a variable with the current
104:50 creates a variable with the current score and it happens
105:13 but I don't need to keep playing this up on stage in front of everyone so my
105:14 on stage in front of everyone so my score is already now up to some six or
105:16 score is already now up to some six or so but in a moment two you'll see that
105:18 so but in a moment two you'll see that it's going to escalate so I'm taking
105:20 it's going to escalate so I'm taking into account some time apparently so
105:28 now so more and more Sprites are suddenly appearing and notice that each
105:30 suddenly appearing and notice that each time they're appearing from a different
105:31 time they're appearing from a different part of the screen that's an illusion
105:32 part of the screen that's an illusion perhaps do that pick a random number
105:35 perhaps do that pick a random number between X and Y so you can actually pick
105:37 between X and Y so you can actually pick some range of values to have the game
105:39 some range of values to have the game constantly changing and indeed I'm going
105:41 constantly changing and indeed I'm going to go ahead and click stop since i'
105:42 to go ahead and click stop since i' spent like 8 hours plus years ago making
105:44 spent like 8 hours plus years ago making this and I can never listen to the song
105:46 this and I can never listen to the song again not that I should be anyway at
105:48 again not that I should be anyway at this point in my life but this song is
105:51 this point in my life but this song is uh synchronized in with a lot of the
105:52 uh synchronized in with a lot of the actions that's happening and ultimately
105:55 actions that's happening and ultimately there's just a lot of building blocks
105:56 there's just a lot of building blocks but I didn't sit down and Implement
105:57 but I didn't sit down and Implement Oscar time as I called it all at once I
106:00 Oscar time as I called it all at once I really did take baby steps so to speak
106:01 really did take baby steps so to speak and I figured out well how could I
106:03 and I figured out well how could I decompose this Vision I had at the time
106:06 decompose this Vision I had at the time to create this game ultimately and how
106:08 to create this game ultimately and how do I bite off maybe the easiest Parts
106:10 do I bite off maybe the easiest Parts first and honestly the first thing I did
106:12 first and honestly the first thing I did was I found this image and I just like
106:14 was I found this image and I just like dragged and dropped it into scratch okay
106:15 dragged and dropped it into scratch okay done like lamp post is installed it
106:18 done like lamp post is installed it doesn't do anything it's not interactive
106:19 doesn't do anything it's not interactive but I at least set the stage so to speak
106:21 but I at least set the stage so to speak for the program then what else might I
106:23 for the program then what else might I have done well let me do this let me go
106:25 have done well let me do this let me go ahead and open up uh in another editor
106:28 ahead and open up uh in another editor here a early incarnation of Oscar Time
106:32 here a early incarnation of Oscar Time by doing this let me go into Oscar time
106:34 by doing this let me go into Oscar time here let me full screen this and here
106:37 here let me full screen this and here you have let me hide the trash for just
106:39 you have let me hide the trash for just a moment is what I might call the second
106:42 a moment is what I might call the second uh version of my program wherein at the
106:44 uh version of my program wherein at the top right of the stage here I had the
106:46 top right of the stage here I had the lamp post which I just dragged and
106:47 lamp post which I just dragged and dropped and got going but then I added
106:49 dropped and got going but then I added an actual Sprite and it has to be a
106:51 an actual Sprite and it has to be a Sprite if you wanted to do things
106:52 Sprite if you wanted to do things interactively the lamp post not a Sprite
106:54 interactively the lamp post not a Sprite it's just an image a costume if you will
106:57 it's just an image a costume if you will for the whole stage itself a backdrop
106:59 for the whole stage itself a backdrop but this thing is indeed a Sprite
107:00 but this thing is indeed a Sprite because it needs to respond to code and
107:02 because it needs to respond to code and events like dragging and dropping so
107:04 events like dragging and dropping so what might I have done early on with
107:07 what might I have done early on with that code well maybe the first version
107:10 that code well maybe the first version would have been something like this
107:11 would have been something like this where by my very first version of Oscar
107:13 where by my very first version of Oscar time might have said something like oh
107:16 time might have said something like oh this how about let me control the
107:17 this how about let me control the program as before or rather events when
107:20 program as before or rather events when the green flag is clicked what do I want
107:22 the green flag is clicked what do I want to do well I want to go ahead and
107:25 to do well I want to go ahead and forever do something like this uh
107:27 forever do something like this uh forever so I want the lid to open up if
107:30 forever so I want the lid to open up if I touch it so if the cursor gets near
107:32 I touch it so if the cursor gets near the lid I want the lid to open up and
107:33 the lid I want the lid to open up and then if I move away I want it to close
107:35 then if I move away I want it to close so how can I do that I want an if but I
107:38 so how can I do that I want an if but I just don't want one question I really
107:39 just don't want one question I really want two a fork in the road that goes
107:41 want two a fork in the road that goes left or right so to speak and let me
107:44 left or right so to speak and let me grab this puzzle piece here as I did
107:46 grab this puzzle piece here as I did long ago so notice it grows to fill
107:49 long ago so notice it grows to fill what's the question I want to ask well
107:50 what's the question I want to ask well under sensing I'm going to go ahead here
107:53 under sensing I'm going to go ahead here and say if this trash can is touching
107:55 and say if this trash can is touching the mouse pointer what do I want to do
107:58 the mouse pointer what do I want to do well I want to change what the trash can
107:59 well I want to change what the trash can looks like and this part I did in
108:01 looks like and this part I did in advance of class if you go up here to
108:03 advance of class if you go up here to costumes this is where all the graphical
108:04 costumes this is where all the graphical stuff happens and you'll see that I
108:06 stuff happens and you'll see that I imported a whole bunch of different
108:09 imported a whole bunch of different costumes that effectively much like a
108:11 costumes that effectively much like a video when you play them quickly creates
108:13 video when you play them quickly creates the illusion of movement some animation
108:15 the illusion of movement some animation but it's really just dot dot dot dot dot
108:17 but it's really just dot dot dot dot dot different images showing on the screen
108:19 different images showing on the screen well some of these costumes are called
108:21 well some of these costumes are called like Oscar 1 Oscar 2 Oscar 1 is closed
108:25 like Oscar 1 Oscar 2 Oscar 1 is closed Oscar 2 is open so let's just deal with
108:27 Oscar 2 is open so let's just deal with those first so if I'm touching the mouse
108:29 those first so if I'm touching the mouse pointer let me go under how about looks
108:32 pointer let me go under how about looks and we didn't use this before but
108:33 and we didn't use this before but there's this block switch costume to
108:36 there's this block switch costume to something else I'm going to drag and
108:37 something else I'm going to drag and drop this inside of the if and notice
108:39 drop this inside of the if and notice it's a little bit indented I'm going to
108:41 it's a little bit indented I'm going to change it not to Oscar 8 but Oscar 2
108:45 change it not to Oscar 8 but Oscar 2 otherwise if not touching the mouse
108:47 otherwise if not touching the mouse pointer this is the other direction in
108:49 pointer this is the other direction in the fork and the road let's go ahead and
108:50 the fork and the road let's go ahead and switch the costume back to what I
108:53 switch the costume back to what I described as Oscar 1 so let me run this
108:56 described as Oscar 1 so let me run this program and not much of interest is
108:58 program and not much of interest is happening yet but notice if I move the
109:00 happening yet but notice if I move the cursor up down but how is that working
109:04 cursor up down but how is that working it's just changing the costume that's
109:05 it's just changing the costume that's being overlaid on the Sprite so it looks
109:07 being overlaid on the Sprite so it looks like interactivity but you're really
109:09 like interactivity but you're really just changing the Aesthetics and we
109:10 just changing the Aesthetics and we humans are just kind of you know
109:12 humans are just kind of you know assuming oh it's opening up well no it's
109:14 assuming oh it's opening up well no it's just changing a costume so here's the
109:15 just changing a costume so here's the difference the high level abstraction
109:17 difference the high level abstraction trash can opening the lower level
109:19 trash can opening the lower level implementation detail
109:20 implementation detail costume changing creating that illusion
109:22 costume changing creating that illusion and if I wanted to look prettier I could
109:24 and if I wanted to look prettier I could just have many other costumes and go
109:26 just have many other costumes and go boom boom boom boom boom to create more
109:28 boom boom boom boom boom to create more frames per second if you will so I need
109:31 frames per second if you will so I need to do um one other thing maybe if I
109:33 to do um one other thing maybe if I accidentally leave the trash can open
109:35 accidentally leave the trash can open let me make one change here let me make
109:37 let me make one change here let me make sure that the very first thing I do when
109:39 sure that the very first thing I do when the green flag is clicked is always
109:41 the green flag is clicked is always start with the trash can closed because
109:43 start with the trash can closed because otherwise you might accidentally leave
109:45 otherwise you might accidentally leave it open so this gets me into some
109:47 it open so this gets me into some default state so now it's always closed
109:49 default state so now it's always closed until I manually hover over it instead
109:52 until I manually hover over it instead well what might I have done next well if
109:54 well what might I have done next well if I wanted to introduce something like the
109:56 I wanted to introduce something like the trash I need a second Sprite and here in
109:58 trash I need a second Sprite and here in advance I grabb the image already let me
110:00 advance I grabb the image already let me pretend that this never happened let me
110:03 pretend that this never happened let me drag this away here and now I have
110:05 drag this away here and now I have nothing in my code area for this piece
110:07 nothing in my code area for this piece of trash but it is a second Sprite and
110:09 of trash but it is a second Sprite and all I did was I clicked on the little
110:10 all I did was I clicked on the little cat plus icon here created a second
110:13 cat plus icon here created a second Sprite I named it trash I added an
110:15 Sprite I named it trash I added an costume for it sort of the aesthetic
110:17 costume for it sort of the aesthetic stuff I did in advance but here I'll do
110:19 stuff I did in advance but here I'll do now the code how do I want to do this
110:22 now the code how do I want to do this well how about when the green flag is
110:24 well how about when the green flag is clicked for the trash can I want the
110:26 clicked for the trash can I want the trash can in parallel to do or I want
110:29 trash can in parallel to do or I want the trash the piece of trash to do its
110:31 the trash the piece of trash to do its own thing so what I wanted to do is
110:33 own thing so what I wanted to do is maybe let's do motion how about and
110:37 maybe let's do motion how about and let's go to a specific coordinate now
110:38 let's go to a specific coordinate now there's a lot of options here there's
110:40 there's a lot of options here there's turning go to a random position go to x
110:43 turning go to a random position go to x comma y Glide more elegantly there's a
110:46 comma y Glide more elegantly there's a lot of different ways to implement
110:47 lot of different ways to implement movement I just wanted to go to a very
110:49 movement I just wanted to go to a very specific location first so I'm just
110:50 specific location first so I'm just going to go to x comma y first and I'm
110:53 going to go to x comma y first and I'm going to say x how about will be um uh
110:57 going to say x how about will be um uh let's not hardcode this let's just have
110:59 let's not hardcode this let's just have it be well let's do it at zero initially
111:01 it be well let's do it at zero initially and then 240 so whoops let's do 0 comma
111:04 and then 240 so whoops let's do 0 comma 240 so that this piece of trash always
111:06 240 so that this piece of trash always starts at the top middle of the screen
111:07 starts at the top middle of the screen if you think back to that coordinate
111:09 if you think back to that coordinate system 0 0 is in the middle 240 is
111:11 system 0 0 is in the middle 240 is straight above it all right now after I
111:13 straight above it all right now after I do that what do I want to do well how
111:15 do that what do I want to do well how about I control this thing by forever
111:18 about I control this thing by forever falling now how do I make the trash move
111:21 falling now how do I make the trash move we haven't seen this puzzle piece yet
111:22 we haven't seen this puzzle piece yet but under motion the very first thing is
111:24 but under motion the very first thing is called move some number of steps by
111:26 called move some number of steps by default it's 10 but we'll do it more
111:28 default it's 10 but we'll do it more simply let me go ahead and
111:30 simply let me go ahead and move uh oh sorry move is going to move
111:34 move uh oh sorry move is going to move it uh in whatever Direction it's facing
111:36 it uh in whatever Direction it's facing I only want it to move down so here even
111:39 I only want it to move down so here even I'm getting confused as to how many
111:40 I'm getting confused as to how many different ways there are to do things
111:42 different ways there are to do things what I think I want to do is this let me
111:44 what I think I want to do is this let me only change my y AIS as follows so
111:48 only change my y AIS as follows so here's another puzzle piece called
111:49 here's another puzzle piece called change y so again Y is the vertical so
111:52 change y so again Y is the vertical so let me just change y by one pixel
111:55 let me just change y by one pixel downward at a time so Nega one one pixel
111:58 downward at a time so Nega one one pixel at a time so it's kind of slow and I
112:00 at a time so it's kind of slow and I think now I think that's it let me hit
112:02 think now I think that's it let me hit stop notice that my trash can is still
112:05 stop notice that my trash can is still going to be interactive I haven't
112:06 going to be interactive I haven't changed or deleted that code I've just
112:08 changed or deleted that code I've just added now code for my trash piece of
112:10 added now code for my trash piece of trash if I click the green flag notice
112:13 trash if I click the green flag notice that after I enable it let me start that
112:16 that after I enable it let me start that again I had it hidden for before class
112:18 again I had it hidden for before class but let me enable it now green flag
112:20 but let me enable it now green flag notice it starts dead center at xal 0 y
112:23 notice it starts dead center at xal 0 y = 240 and it's dropping one pixel at a
112:26 = 240 and it's dropping one pixel at a time if that seems a little boring we
112:28 time if that seems a little boring we can change it to negative 10 pixels at a
112:30 can change it to negative 10 pixels at a time and boom it's done so that's how
112:32 time and boom it's done so that's how you might change the speed of a program
112:34 you might change the speed of a program but I'm going to leave it more simply as
112:35 but I'm going to leave it more simply as negative 1 and honestly it would be nice
112:37 negative 1 and honestly it would be nice if it doesn't always start from the top
112:39 if it doesn't always start from the top otherwise this game is not going to be
112:40 otherwise this game is not going to be very interactive I'm literally going to
112:42 very interactive I'm literally going to be grabbing the trash from the same
112:43 be grabbing the trash from the same place every time so why don't I instead
112:46 place every time so why don't I instead stop this let me go under operators and
112:49 stop this let me go under operators and let's pick our random number so let me
112:51 let's pick our random number so let me change the hardcoded the manually
112:53 change the hardcoded the manually inputed zero and let's make X be
112:55 inputed zero and let's make X be somewhere between zero so in the middle
112:57 somewhere between zero so in the middle and all the way over to uh what was it
113:00 and all the way over to uh what was it one oh I got my numbers wrong 240 and my
113:04 one oh I got my numbers wrong 240 and my y will be 180 sorry I got my X and my my
113:06 y will be 180 sorry I got my X and my my y confused so let me play this again and
113:10 y confused so let me play this again and now we have a game that's more like
113:11 now we have a game that's more like games you might have played growing up
113:13 games you might have played growing up or even now like there's some Randomness
113:14 or even now like there's some Randomness to it so the CPU so to speak is doing
113:16 to it so the CPU so to speak is doing something more interesting let me run it
113:18 something more interesting let me run it again now it's a little to the left let
113:20 again now it's a little to the left let me run it again now it's a little more
113:21 me run it again now it's a little more to the left again now it's back to the
113:23 to the left again now it's back to the right so Randomness just makes games
113:25 right so Randomness just makes games more interesting and this is why when
113:26 more interesting and this is why when you play any video game if different
113:28 you play any video game if different things are happening there's probably
113:29 things are happening there's probably just some Randomness and it's quantized
113:31 just some Randomness and it's quantized is just a simple number now I think I
113:33 is just a simple number now I think I just need one final flourish here if I
113:35 just need one final flourish here if I may let me go ahead and add this how
113:38 may let me go ahead and add this how about uh events or rather yes events
113:42 about uh events or rather yes events when green flag is clicked I can do
113:44 when green flag is clicked I can do multiple things within the same Sprite
113:47 multiple things within the same Sprite they don't all have to be attached to
113:48 they don't all have to be attached to the same one let me go ahead and forever
113:51 the same one let me go ahead and forever go ahead and do something else how about
113:55 go ahead and do something else how about whenever the trash is how about touching
114:00 whenever the trash is how about touching the trash can so forever if let's see I
114:03 the trash can so forever if let's see I need a sensing block so how about is
114:07 need a sensing block so how about is touching uh not the mouse pointer this
114:09 touching uh not the mouse pointer this time but touching Oscar himself there
114:13 time but touching Oscar himself there now let's see what happens all right so
114:15 now let's see what happens all right so let's go ahead and click the green flag
114:18 let's go ahead and click the green flag now I go down over here and let
114:21 now I go down over here and let go okay that's I kind of want it to go
114:24 go okay that's I kind of want it to go into the trash can how do I make it go
114:26 into the trash can how do I make it go into the trash can how can we take this
114:28 into the trash can how can we take this high level idea put trash into the trash
114:31 high level idea put trash into the trash can and make it seem to
114:33 can and make it seem to disappear logically what could we do
114:37 disappear logically what could we do yeah okay so when it touches it let's
114:39 yeah okay so when it touches it let's have it disappear so I could hide it or
114:41 have it disappear so I could hide it or honestly if the game's going to be
114:42 honestly if the game's going to be ongoing like it was letting me Mo drop
114:44 ongoing like it was letting me Mo drop more and more trash let me just have it
114:46 more and more trash let me just have it go ahead and pick a new random location
114:48 go ahead and pick a new random location so let me do this let me go ahead and
114:51 so let me do this let me go ahead and copy this puzzle piece up here and
114:52 copy this puzzle piece up here and duplicate and I don't want the whole
114:54 duplicate and I don't want the whole thing sorry let me get rid of this let
114:56 thing sorry let me get rid of this let me just do this let me go back to some
114:59 me just do this let me go back to some random location at the top so now notice
115:01 random location at the top so now notice what happens if I click and drag on it
115:03 what happens if I click and drag on it here it goes and I let go it looks like
115:06 here it goes and I let go it looks like it's going into the trash can because it
115:08 it's going into the trash can because it snaps back up to some random location
115:10 snaps back up to some random location now the only thing I'm not doing really
115:12 now the only thing I'm not doing really is keeping track of any kind of score
115:14 is keeping track of any kind of score and it turns out if I full screen this
115:16 and it turns out if I full screen this it's not going to be draggable by
115:17 it's not going to be draggable by default so just as a corner case so to
115:19 default so just as a corner case so to speak something that you might trip over
115:21 speak something that you might trip over otherwise let me go ahead and under uh
115:25 otherwise let me go ahead and under uh let's see uh sensing it turns out I also
115:28 let's see uh sensing it turns out I also need this for the piece of trash there's
115:30 need this for the piece of trash there's this way of setting and scratch a puzzle
115:33 this way of setting and scratch a puzzle a Sprite to be draggable or not
115:35 a Sprite to be draggable or not draggable I need to explicitly make it
115:37 draggable I need to explicitly make it draggable so that when I do full screen
115:39 draggable so that when I do full screen this thing now it Still Remains
115:40 this thing now it Still Remains draggable and someone like myself can
115:43 draggable and someone like myself can play it again and again well how about
115:45 play it again and again well how about we supplement this with one final
115:46 we supplement this with one final flourish why don't we keep track now of
115:49 flourish why don't we keep track now of the user score so how about when the
115:52 the user score so how about when the user actually drags the piece of trash
115:54 user actually drags the piece of trash to the trash can let me go under
115:56 to the trash can let me go under variables here where in advance I've
115:58 variables here where in advance I've already made myself a variable called
116:00 already made myself a variable called score I could have called it X or Y or Z
116:02 score I could have called it X or Y or Z or ABC but that's not very descriptive
116:04 or ABC but that's not very descriptive in programming you typically give things
116:06 in programming you typically give things a more descriptive English or some other
116:08 a more descriptive English or some other language name so I called this one score
116:10 language name so I called this one score so how do I want to do this in my score
116:13 so how do I want to do this in my score well let me go ahead and initially set
116:15 well let me go ahead and initially set this game score to zero at the very top
116:18 this game score to zero at the very top of one of these Scripts
116:20 of one of these Scripts or one of these programs up here and
116:22 or one of these programs up here and then anytime my cursor my piece of trash
116:26 then anytime my cursor my piece of trash is touching Oscar let's not just jump to
116:28 is touching Oscar let's not just jump to the top let's change the score by one up
116:33 the top let's change the score by one up here so now notice if touching Oscar
116:35 here so now notice if touching Oscar change the score that is add one to the
116:37 change the score that is add one to the score and then pick a new random
116:39 score and then pick a new random location and now green flag let's do
116:43 location and now green flag let's do this slowly here it goes it's the trash
116:45 this slowly here it goes it's the trash can opens I let go and now notice at the
116:48 can opens I let go and now notice at the top left of my program notice the score
116:49 top left of my program notice the score is now two notice the score if I do this
116:52 is now two notice the score if I do this again is about to become three and so
116:55 again is about to become three and so here we have building blocks literally
116:57 here we have building blocks literally of making this program better and better
116:59 of making this program better and better and better and so indeed that's how you
117:01 and better and so indeed that's how you generally approach solving any large
117:02 generally approach solving any large program uh any problem with code be it
117:05 program uh any problem with code be it in scratch or C or python or some other
117:07 in scratch or C or python or some other you take this Vision you might have or
117:09 you take this Vision you might have or some Vision you've been assigned in a
117:10 some Vision you've been assigned in a homework assignment and try to break it
117:12 homework assignment and try to break it down into these constituent parts and
117:13 down into these constituent parts and just pluck off the easy ones first put
117:16 just pluck off the easy ones first put the lamp post there first and at least
117:17 the lamp post there first and at least feel like you're making some progress
117:19 feel like you're making some progress then pluck off something like the trash
117:20 then pluck off something like the trash can and just make it do a little thing
117:22 can and just make it do a little thing and it doesn't have to be in some same
117:24 and it doesn't have to be in some same order here I could have done this in a
117:25 order here I could have done this in a million different ways but figure out
117:28 million different ways but figure out what the small pieces are that
117:30 what the small pieces are that ultimately like a few of the problems
117:31 ultimately like a few of the problems we've solved today assemble into a
117:33 we've solved today assemble into a greater solution there too uh so that
117:37 greater solution there too uh so that you have now a mental model for these
117:39 you have now a mental model for these types of blocks and others let's return
117:41 types of blocks and others let's return for a moment to this we saw a moment ago
117:44 for a moment to this we saw a moment ago that when I started saying hello David
117:46 that when I started saying hello David and nesting those puzzle pieces we had a
117:48 and nesting those puzzle pieces we had a whole different Paradigm Al together my
117:51 whole different Paradigm Al together my input for that second version of hello
117:53 input for that second version of hello world was to now pass in for instance
117:55 world was to now pass in for instance what's your name into my function called
117:58 what's your name into my function called ask that gave me not a side effect but
118:01 ask that gave me not a side effect but what I called again a return value
118:03 what I called again a return value called answer by default in scratch and
118:06 called answer by default in scratch and now notice and recall when I had that
118:09 now notice and recall when I had that same output become the input to my next
118:13 same output become the input to my next block it looked a little something like
118:15 block it looked a little something like this say so how does this type of block
118:18 this say so how does this type of block and this nesting the stacking of blocks
118:20 and this nesting the stacking of blocks fit into the same mental model well same
118:22 fit into the same mental model well same idea my input for that part of the story
118:24 idea my input for that part of the story is now taking in not one input but two
118:28 is now taking in not one input but two two arguments hello and the answer from
118:30 two arguments hello and the answer from before the function in this case is that
118:33 before the function in this case is that new block called join the output thereof
118:35 new block called join the output thereof is hello David which itself became if we
118:38 is hello David which itself became if we sort of animate this the input to my
118:42 sort of animate this the input to my final function which indeed was still
118:44 final function which indeed was still say and this is only to say no pun
118:47 say and this is only to say no pun intended that almost everything that
118:49 intended that almost everything that that you do with these puzzle pieces be
118:51 that you do with these puzzle pieces be in the context of Oscar time or the mole
118:53 in the context of Oscar time or the mole whacking or even just something simple
118:55 whacking or even just something simple like hello world will ultimately fit
118:57 like hello world will ultimately fit into that relatively simple mental model
119:00 into that relatively simple mental model there now I thought we'd End by taking a
119:02 there now I thought we'd End by taking a look at just a couple final examples
119:04 look at just a couple final examples these ones two made by some of your
119:06 these ones two made by some of your predecessors and for this I thought we
119:08 predecessors and for this I thought we would not write code together but read
119:11 would not write code together but read it instead and so allow me to open up
119:13 it instead and so allow me to open up one other example here that will show us
119:16 one other example here that will show us a few different versions of a program
119:19 a few different versions of a program that a predecessor made give me just a
119:21 that a predecessor made give me just a moment here and we'll see how we might
119:24 moment here and we'll see how we might build up to something even more
119:26 build up to something even more interactive and in just a moment we'll
119:28 interactive and in just a moment we'll see something they called Ivy's uh
119:30 see something they called Ivy's uh hardest game focused here on these
119:34 hardest game focused here on these particular mechanics so here is version
119:36 particular mechanics so here is version zero so to speak of this program where
119:39 zero so to speak of this program where in the goal is to create a game where
119:40 in the goal is to create a game where you have to like get out of some kind of
119:42 you have to like get out of some kind of Maze and you have to get out in this
119:44 Maze and you have to get out in this case the Harvard Crest from this maze
119:46 case the Harvard Crest from this maze let me go ahead and just hit play on
119:47 let me go ahead and just hit play on this green flag so you can see what what
119:49 this green flag so you can see what what the first building block for this
119:51 the first building block for this program might have been notice that my
119:53 program might have been notice that my hand here is actually on the arrow keys
119:55 hand here is actually on the arrow keys of my keyboard and it seems that by
119:58 of my keyboard and it seems that by moving up down left or right this little
120:01 moving up down left or right this little Crest on the screen responds in exactly
120:04 Crest on the screen responds in exactly that way now let's hypothesize for just
120:06 that way now let's hypothesize for just a moment even though we've not done
120:07 a moment even though we've not done anything quite like this before how
120:09 anything quite like this before how might this code be implemented how do
120:12 might this code be implemented how do you get a Sprite be it a cat or a Crest
120:15 you get a Sprite be it a cat or a Crest to respond to keys on a keyboard might
120:17 to respond to keys on a keyboard might you think intuitively yeah there could
120:19 you think intuitively yeah there could be something that's sensing what key
120:21 be something that's sensing what key you're pressing on yeah there could be
120:22 you're pressing on yeah there could be something sensing what key you're
120:24 something sensing what key you're pressing on and if you do it again in a
120:25 pressing on and if you do it again in a forever Loop you'll just constantly be
120:27 forever Loop you'll just constantly be listening for keystrokes and this is how
120:29 listening for keystrokes and this is how like every piece of software nowadays
120:31 like every piece of software nowadays works it's constantly waiting for your
120:33 works it's constantly waiting for your phone to be tapped or something to be
120:34 phone to be tapped or something to be typed on the screen so let me go ahead
120:36 typed on the screen so let me go ahead and look inside of this existing program
120:39 and look inside of this existing program here and there's more going on but we'll
120:41 here and there's more going on but we'll take a quick glance what's actually
120:43 take a quick glance what's actually going on well up here at top left notice
120:45 going on well up here at top left notice we just have go to x equals 0 and Y
120:48 we just have go to x equals 0 and Y equals 0 that means put the Harvard
120:50 equals 0 that means put the Harvard Crest dead center in the middle of the
120:51 Crest dead center in the middle of the stage then we have forever two functions
120:54 stage then we have forever two functions that we made in advance as custom
120:56 that we made in advance as custom functions uh listen for keyboard feel
120:59 functions uh listen for keyboard feel for walls so it's doing two things at
121:01 for walls so it's doing two things at once it's forever listening for the
121:03 once it's forever listening for the keyboard up down left right and feeling
121:05 keyboard up down left right and feeling for the walls in the sense that if I get
121:06 for the walls in the sense that if I get too far to the left I don't want it to
121:08 too far to the left I don't want it to keep moving past that black wall and if
121:10 keep moving past that black wall and if it moves too far to the right I don't
121:11 it moves too far to the right I don't want it to blow through that wall either
121:13 want it to blow through that wall either so it's going to do two things
121:14 so it's going to do two things constantly listening for keyboard and
121:16 constantly listening for keyboard and feeling for walls so to speak and how
121:19 feeling for walls so to speak and how are those implemented well this one's a
121:20 are those implemented well this one's a bit long but on the left here is listen
121:23 bit long but on the left here is listen for keyboard so this pink puzzle piece
121:26 for keyboard so this pink puzzle piece listen for keyboard first checks if the
121:29 listen for keyboard first checks if the key up arrow is pressed question mark
121:31 key up arrow is pressed question mark Boolean expression in a conditional
121:33 Boolean expression in a conditional change y by one that means move it up
121:35 change y by one that means move it up one else if the key down arrow is
121:38 one else if the key down arrow is pressed then change y by negative one
121:41 pressed then change y by negative one and similar for left Arrow similar for
121:43 and similar for left Arrow similar for right arrow and even though there's not
121:44 right arrow and even though there's not a loop in this pink function there is
121:47 a loop in this pink function there is where I'm using it so it's constantly
121:49 where I'm using it so it's constantly being asked again and again how about
121:51 being asked again and again how about feeling for walls well over here to the
121:53 feeling for walls well over here to the right it's a little cut off but here you
121:56 right it's a little cut off but here you have if touching left wall change X by
121:59 have if touching left wall change X by one so if you hit the wall it's too late
122:01 one so if you hit the wall it's too late you're kind of blowing through it
122:02 you're kind of blowing through it already so I want to move it wh one
122:05 already so I want to move it wh one pixel so it's no longer touching that
122:06 pixel so it's no longer touching that wall similarly if it's touching the
122:08 wall similarly if it's touching the right wall I want to back it up one
122:10 right wall I want to back it up one pixel so it's no longer touching that
122:12 pixel so it's no longer touching that wall so it's kind of like bouncing off
122:13 wall so it's kind of like bouncing off ever so slightly so that it doesn't slip
122:16 ever so slightly so that it doesn't slip through that actual wall and what are
122:17 through that actual wall and what are those walls well noce down here it's
122:20 those walls well noce down here it's just a simple Sprite with a black line
122:23 just a simple Sprite with a black line that I've oriented hor vertically
122:24 that I've oriented hor vertically instead of horizontally and that's just
122:26 instead of horizontally and that's just so that I can ask questions of these
122:28 so that I can ask questions of these other two Sprites now that gives me that
122:30 other two Sprites now that gives me that form of interactivity what more can I
122:32 form of interactivity what more can I now do well what if we make things a
122:34 now do well what if we make things a little more interactive here let me go
122:37 little more interactive here let me go ahead and see inside version one our
122:39 ahead and see inside version one our second and let me propose what's going
122:41 second and let me propose what's going to happen here well how might we add a
122:44 to happen here well how might we add a little something like Yale into the mix
122:47 little something like Yale into the mix well what's Yale going to do when I I
122:49 well what's Yale going to do when I I hit the green flag now based on this
122:51 hit the green flag now based on this code any
122:53 code any hunches here is the code for my Yale
122:56 hunches here is the code for my Yale Sprite yeah yeah it's kind of got to be
122:59 Sprite yeah yeah it's kind of got to be an adversary by blocking my path
123:02 an adversary by blocking my path theoretically if I keep writing more
123:03 theoretically if I keep writing more code so why it to goes to the middle of
123:05 code so why it to goes to the middle of the screen it points in Direction 90 de
123:08 the screen it points in Direction 90 de so similarly there's a whole degree
123:09 so similarly there's a whole degree system as well and it forever asks this
123:12 system as well and it forever asks this if touching the left wall or notice the
123:14 if touching the left wall or notice the green block touching the right wall then
123:16 green block touching the right wall then just turn around 180° and in if you
123:19 just turn around 180° and in if you think this through logically that just
123:20 think this through logically that just means you're bouncing this way and this
123:22 means you're bouncing this way and this way by just flipping yourself around
123:24 way by just flipping yourself around 180° for just this Yale Sprite so if I
123:27 180° for just this Yale Sprite so if I go ahead and zoom in on this and click
123:29 go ahead and zoom in on this and click the green flag I can still move up and
123:32 the green flag I can still move up and down but Yale is just kind of doing this
123:34 down but Yale is just kind of doing this all day long back and forth and back and
123:36 all day long back and forth and back and forth forever nothing bad happens if I
123:38 forth forever nothing bad happens if I try to go through it but we could add
123:41 try to go through it but we could add that certainly to the mix in fact let's
123:42 that certainly to the mix in fact let's add one final feature before we play
123:45 add one final feature before we play this particular game and let me go ahead
123:47 this particular game and let me go ahead and open up the final version of these
123:49 and open up the final version of these building blocks that adds MIT to the mix
123:53 building blocks that adds MIT to the mix so here's
123:54 so here's MIT someone want to explain what this
123:57 MIT someone want to explain what this code does and this is what we're doing
123:58 code does and this is what we're doing this itself is a skill reading someone
124:00 this itself is a skill reading someone else's code and understanding it is half
124:02 else's code and understanding it is half of the part of programming besides
124:04 of the part of programming besides writing yeah yeah it's chasing down the
124:08 writing yeah yeah it's chasing down the Harvard logo outline so this is
124:09 Harvard logo outline so this is apparently the name of the costume that
124:11 apparently the name of the costume that this student made Harvard logo outline
124:13 this student made Harvard logo outline outline and apparently it goes to a
124:15 outline and apparently it goes to a random position first but then it
124:17 random position first but then it forever points to Har so no matter where
124:19 forever points to Har so no matter where I'm moving it up down left or right MIT
124:22 I'm moving it up down left or right MIT is being a little more strategic than
124:23 is being a little more strategic than Yale bouncing back and forth like this
124:25 Yale bouncing back and forth like this so let's go ahead and play this one in
124:26 so let's go ahead and play this one in full screen and here we have a green
124:29 full screen and here we have a green flag so if I move up
124:32 flag so if I move up MIT rather strategically is following me
124:36 MIT rather strategically is following me no matter where I go all right so still
124:38 no matter where I go all right so still nothing bad happens but and now it's
124:40 nothing bad happens but and now it's struggling right it's going up down up
124:42 struggling right it's going up down up down it's trying to uh follow me even
124:44 down it's trying to uh follow me even though I'm not moving so we need some
124:46 though I'm not moving so we need some final flourishes and so I think for this
124:48 final flourishes and so I think for this we need perhaps one final volunteer
124:51 we need perhaps one final volunteer after this cake awaits for everyone
124:53 after this cake awaits for everyone outside as is an end of first lecture
124:55 outside as is an end of first lecture cs50 tradition would you like to come up
124:57 cs50 tradition would you like to come up and be our our vinyl of
125:09 volunteer all right and so this will be the actual version bit written by one of
125:11 the actual version bit written by one of your predecessors that I'll full screen
125:13 your predecessors that I'll full screen here it's going to stitch together all
125:15 here it's going to stitch together all of these same Primitives in more but add
125:17 of these same Primitives in more but add the notion of score and lives so that
125:20 the notion of score and lives so that there's actually a goal which in this
125:22 there's actually a goal which in this case is to move the Harvard Crest to
125:24 case is to move the Harvard Crest to constantly pursue the character on the
125:26 constantly pursue the character on the right hand side so that your Sprite
125:27 right hand side so that your Sprite touches that one would you like to
125:29 touches that one would you like to introduce yourself uh hi my name is
125:31 introduce yourself uh hi my name is Muhammad all right wonderful welcome
125:33 Muhammad all right wonderful welcome aboard and here we come with some
125:35 aboard and here we come with some instructions and final flourish if we
125:37 instructions and final flourish if we want to keep the lights up but perhaps
125:38 want to keep the lights up but perhaps increase the
125:49 music [Music]
127:00 [Music] all right this is cs50 and this is week
127:03 all right this is cs50 and this is week one wherein we continue programming but
127:05 one wherein we continue programming but we do it in a different language because
127:07 we do it in a different language because recall last time we focused on this
127:10 recall last time we focused on this graphical language called scratch but we
127:11 graphical language called scratch but we use scratch uh not only because it's
127:13 use scratch uh not only because it's sort of fun and accessible but because
127:15 sort of fun and accessible but because it allows us to explore a lot of these
127:17 it allows us to explore a lot of these Concepts here Nam functions and
127:19 Concepts here Nam functions and conditionals Boolean Expressions Loops
127:21 conditionals Boolean Expressions Loops variables and more and so indeed even if
127:24 variables and more and so indeed even if today's syntax as we transition to this
127:27 today's syntax as we transition to this new language called C feels a little bit
127:29 new language called C feels a little bit cryptic maybe a little intimidating at
127:31 cryptic maybe a little intimidating at first and you don't quite see all of the
127:33 first and you don't quite see all of the meaning of the symbols beyond the syntax
127:35 meaning of the symbols beyond the syntax itself realized that the ideas are
127:37 itself realized that the ideas are ultimately going to be the same in fact
127:39 ultimately going to be the same in fact as we transition from what was last week
127:42 as we transition from what was last week a hello world program that looked a
127:44 a hello world program that looked a little something like this this week of
127:46 little something like this this week of course it's going to now look a little
127:48 course it's going to now look a little more cryptic it's going to look a little
127:49 more cryptic it's going to look a little something like this and now even if you
127:51 something like this and now even if you can't quite distinguish what all of the
127:53 can't quite distinguish what all of the various symbols mean in this code turns
127:55 various symbols mean in this code turns out that at the end of the day it's
127:56 out that at the end of the day it's indeed going to do what you expect it's
127:58 indeed going to do what you expect it's just going to say hello world on the
128:00 just going to say hello world on the screen just like we did in scratch so
128:02 screen just like we did in scratch so let's start to apply some terminology to
128:04 let's start to apply some terminology to these to these uh tokens first so what
128:06 these to these uh tokens first so what we're about to see what we're about to
128:08 we're about to see what we're about to write henceforth we're going to start
128:09 write henceforth we're going to start calling source code code that you the
128:11 calling source code code that you the human programmer write is just
128:13 human programmer write is just henceforth called source code doesn't
128:14 henceforth called source code doesn't matter if it's scratch doesn't matter if
128:16 matter if it's scratch doesn't matter if it's C doesn't matter if it's python
128:18 it's C doesn't matter if it's python before
128:19 before source code is the general term for
128:20 source code is the general term for really what you and I as human
128:21 really what you and I as human programmers will ultimately write of
128:24 programmers will ultimately write of course computers don't understand source
128:27 course computers don't understand source code it turns out computers don't
128:29 code it turns out computers don't understand scratch and puzzle pieces per
128:31 understand scratch and puzzle pieces per se or C code like we're about to see
128:33 se or C code like we're about to see they only understand this which we
128:35 they only understand this which we called what last week yeah so this is
128:38 called what last week yeah so this is binary zeros and ones but really it's
128:40 binary zeros and ones but really it's just information represented in binary
128:43 just information represented in binary and in fact the technical term now for
128:45 and in fact the technical term now for patterns of zeros and ones that a
128:47 patterns of zeros and ones that a computer not only understands
128:48 computer not only understands understands how to interpret as letters
128:50 understands how to interpret as letters or numbers or colors or images or more
128:53 or numbers or colors or images or more but knows how to execute as well
128:55 but knows how to execute as well henceforth is going to be called machine
128:57 henceforth is going to be called machine code to contrast it with source code so
128:59 code to contrast it with source code so whereas you and I the humans write
129:00 whereas you and I the humans write source code it's the computer that
129:02 source code it's the computer that ultimately only understands machine code
129:05 ultimately only understands machine code and even though we won't get into the
129:06 and even though we won't get into the details of exactly what pattern of
129:08 details of exactly what pattern of symbols means what you'll see that in
129:11 symbols means what you'll see that in this kind of pattern of zeros and ones
129:14 this kind of pattern of zeros and ones there's going to be numbers there's
129:15 there's going to be numbers there's going to be letters but there's also
129:16 going to be letters but there's also going to be instructions because indeed
129:18 going to be instructions because indeed computers are really good at doing
129:19 computers are really good at doing things addition subtraction moving
129:21 things addition subtraction moving things in and out of memory and suffice
129:23 things in and out of memory and suffice it to say that the Macs the PCS the
129:26 it to say that the Macs the PCS the other computers of the world have just
129:27 other computers of the world have just decided as a society what certain
129:30 decided as a society what certain patterns of zeros and ones mean when it
129:32 patterns of zeros and ones mean when it comes to operations as well so not just
129:34 comes to operations as well so not just data but instructions but those patterns
129:36 data but instructions but those patterns are not something we're going to focus
129:37 are not something we're going to focus on in a class like this we're going to
129:39 on in a class like this we're going to focus on the higher level software side
129:41 focus on the higher level software side of things simply assuming that we need
129:43 of things simply assuming that we need to somehow output machine code so it
129:46 to somehow output machine code so it turns out then that this problem we have
129:48 turns out then that this problem we have to solve getting from source code to
129:50 to solve getting from source code to machine code actually fits into the same
129:52 machine code actually fits into the same Paradigm as last time but the input in
129:54 Paradigm as last time but the input in this case is going to be source code on
129:57 this case is going to be source code on the one hand like that's what you and I
129:58 the one hand like that's what you and I ideally will write so that we don't have
130:00 ideally will write so that we don't have to write zeros and ones but we need to
130:02 to write zeros and ones but we need to somehow output machine code because
130:04 somehow output machine code because that's what your Macs PCS phones are
130:06 that's what your Macs PCS phones are actually going to understand well it
130:08 actually going to understand well it turns out there's special programs in
130:10 turns out there's special programs in life whose purpose is to do exactly this
130:13 life whose purpose is to do exactly this conversion convert the source code you
130:15 conversion convert the source code you and I write to the machine code that our
130:17 and I write to the machine code that our phones and computers understand and that
130:19 phones and computers understand and that type of program is going to be called a
130:21 type of program is going to be called a compiler so indeed today we'll introduce
130:24 compiler so indeed today we'll introduce you to another piece of software and
130:26 you to another piece of software and these come in many forms we'll use a
130:28 these come in many forms we'll use a popular one here that allows you to
130:30 popular one here that allows you to convert source code in C to machine code
130:33 convert source code in C to machine code in uh zeros and ones now you didn't have
130:36 in uh zeros and ones now you didn't have to do this with scratch in the world of
130:37 to do this with scratch in the world of scratch it was as simple as clicking the
130:39 scratch it was as simple as clicking the green flag because essentially MIT did
130:41 green flag because essentially MIT did all of the heavy lifting there figuring
130:43 all of the heavy lifting there figuring out how to convert these graphical
130:45 out how to convert these graphical puzzle pieces to the underlying machine
130:47 puzzle pieces to the underlying machine code but now starting today as we begin
130:48 code but now starting today as we begin to study programming and computer
130:50 to study programming and computer science proper now that power moves to
130:52 science proper now that power moves to you and it's up to you now to do that
130:54 you and it's up to you now to do that kind of conversion but thankfully the
130:57 kind of conversion but thankfully the fact that these compilers exist means
130:59 fact that these compilers exist means that you and I don't have to program in
131:01 that you and I don't have to program in machine code like our ancestors Once
131:03 machine code like our ancestors Once Upon a Time did be it virtually or with
131:05 Upon a Time did be it virtually or with physical Punch Cards like pieces of
131:07 physical Punch Cards like pieces of paper with holes in them you and I get
131:08 paper with holes in them you and I get to focus uh on our keyboard as such but
131:12 to focus uh on our keyboard as such but it's not just going to be a matter today
131:14 it's not just going to be a matter today of like writing code it's going to be a
131:16 of like writing code it's going to be a matter ultimately today onward of good
131:18 matter ultimately today onward of good code as well and this is the kind of
131:20 code as well and this is the kind of thing that you don't just learn
131:21 thing that you don't just learn overnight it takes time it takes
131:22 overnight it takes time it takes practice just like writing an essay in
131:24 practice just like writing an essay in any subject might take time and practice
131:26 any subject might take time and practice and iteration over time but in a
131:28 and iteration over time but in a programming class like cs50 we're going
131:30 programming class like cs50 we're going to Aspire to evaluate the quality of
131:33 to Aspire to evaluate the quality of code along these three axes generally is
131:36 code along these three axes generally is it correct first and foremost like does
131:37 it correct first and foremost like does the code do what it's supposed to do
131:39 the code do what it's supposed to do after all if it doesn't well what was
131:40 after all if it doesn't well what was the point of writing it in the first
131:42 the point of writing it in the first place so it sort of goes without saying
131:43 place so it sort of goes without saying that you want code you write to be
131:45 that you want code you write to be correct and it's obviously not always
131:47 correct and it's obviously not always again anytime you're Mac or PC or phone
131:49 again anytime you're Mac or PC or phone has crashed some human somewhere wrote
131:51 has crashed some human somewhere wrote buggy that is code with mistakes but C
131:54 buggy that is code with mistakes but C correctness is going to be the first and
131:55 correctness is going to be the first and foremost goal but then there's a more
131:57 foremost goal but then there's a more subjective goal we'll see in time a
131:59 subjective goal we'll see in time a matter of design and we saw a little bit
132:01 matter of design and we saw a little bit of this last week when I proposed that
132:03 of this last week when I proposed that we could design even scratch programs
132:05 we could design even scratch programs better maybe by using Loops instead of
132:07 better maybe by using Loops instead of just by copying and pasting the same
132:09 just by copying and pasting the same blocks again and again so design is more
132:11 blocks again and again so design is more subjective it's more of a a learned art
132:14 subjective it's more of a a learned art whereby two people might ultimately
132:16 whereby two people might ultimately disagree as to which version of a
132:18 disagree as to which version of a program is better designed but we'll
132:19 program is better designed but we'll give you building blocks and principles
132:21 give you building blocks and principles over the coming weeks so that you can
132:22 over the coming weeks so that you can have a better sense for yourself if your
132:24 have a better sense for yourself if your own code is well designed and why is
132:27 own code is well designed and why is that valuable well the better design
132:28 that valuable well the better design your code is often the faster it's going
132:30 your code is often the faster it's going to run the more maintainable it's going
132:32 to run the more maintainable it's going to be by you or colleagues if you're
132:34 to be by you or colleagues if you're working with others in the real world so
132:36 working with others in the real world so good design is a good thing it helps you
132:37 good design is a good thing it helps you communicate your ideas just like an
132:39 communicate your ideas just like an typical English essay and then lastly
132:41 typical English essay and then lastly we'll talk this week onward about style
132:44 we'll talk this week onward about style and this is really just the Aesthetics
132:45 and this is really just the Aesthetics of your code it turns out that computers
132:47 of your code it turns out that computers often don't care how sloppy your actual
132:50 often don't care how sloppy your actual code is um where uh in the world of code
132:53 code is um where uh in the world of code it turns out that you don't really need
132:55 it turns out that you don't really need to indent things in a beautiful way you
132:57 to indent things in a beautiful way you don't need to paginate things like you
132:58 don't need to paginate things like you might in an essay the computer generally
133:00 might in an essay the computer generally does not care but the human does the
133:03 does not care but the human does the teaching assistant does you will care
133:05 teaching assistant does you will care the next day when you're just trying to
133:06 the next day when you're just trying to understand what your code does so we'll
133:08 understand what your code does so we'll focus lastly on Style the Aesthetics of
133:10 focus lastly on Style the Aesthetics of the code that you're writing so where
133:12 the code that you're writing so where are we going to write code where are we
133:14 are we going to write code where are we going to compile code so for this class
133:16 going to compile code so for this class not only with C but the other languages
133:18 not only with C but the other languages we use later in the term we're going to
133:20 we use later in the term we're going to use a free text editor that is program
133:23 use a free text editor that is program called Visual Studio code AKA vs code
133:25 called Visual Studio code AKA vs code it's super popular nowadays not just for
133:27 it's super popular nowadays not just for C but for C++ and Python and Java and
133:30 C but for C++ and Python and Java and any number of other languages it's a
133:32 any number of other languages it's a text editor in the sense that it lets
133:34 text editor in the sense that it lets you edit text and that all that's all
133:37 you edit text and that all that's all code is going to be now strictly
133:39 code is going to be now strictly speaking you could write code on paper
133:41 speaking you could write code on paper pencil in fact in high school if you
133:42 pencil in fact in high school if you took a class you might have done that
133:43 took a class you might have done that one or more times as sort of an in-class
133:45 one or more times as sort of an in-class exercise you can't run it on paper of
133:47 exercise you can't run it on paper of course but you could write it certainly
133:49 course but you could write it certainly you could use something like Microsoft
133:50 you could use something like Microsoft Word or notepad.exe or text edit on the
133:53 Word or notepad.exe or text edit on the Mac but none of those programs are
133:55 Mac but none of those programs are really designed to format the code in
133:57 really designed to format the code in the best way for you nor are they
133:59 the best way for you nor are they designed to let you compile and run the
134:01 designed to let you compile and run the code so VSS code is going to be a tool
134:04 code so VSS code is going to be a tool via which you can do all that and more
134:06 via which you can do all that and more write the code compile the code run the
134:08 write the code compile the code run the code so that you all don't have to
134:10 code so that you all don't have to wrestle with stupid technical support
134:12 wrestle with stupid technical support headaches at the beginning of the course
134:14 headaches at the beginning of the course by installing this software and that on
134:15 by installing this software and that on your Macs or PCS we'll use a cloud based
134:18 your Macs or PCS we'll use a cloud based version of VSS code at code. cs50. and
134:21 version of VSS code at code. cs50. and that's going to be the exact same tool
134:23 that's going to be the exact same tool and the goal then is by the end of the
134:24 and the goal then is by the end of the semester to sort of uh migrate you off
134:27 semester to sort of uh migrate you off of that cloud-based environment to your
134:28 of that cloud-based environment to your own Mac and PC so that even if cs50 is
134:31 own Mac and PC so that even if cs50 is the only CS class you ever take you're
134:33 the only CS class you ever take you're 100% equipped to continue writing code
134:35 100% equipped to continue writing code after the class using not something
134:37 after the class using not something that's even cs50 specific but a de facto
134:40 that's even cs50 specific but a de facto industry standard at least for some time
134:42 industry standard at least for some time so what's this program VSS code going to
134:44 so what's this program VSS code going to look like be it on your Mac PC or
134:46 look like be it on your Mac PC or initially in your browser and it's going
134:48 initially in your browser and it's going to look a little something like this and
134:49 to look a little something like this and there's going to be several different
134:50 there's going to be several different regions to the screen and picture here
134:52 regions to the screen and picture here is that very same code I keep proposing
134:54 is that very same code I keep proposing is the simplest program you can write in
134:56 is the simplest program you can write in C and what are these different regions
134:58 C and what are these different regions of the screen well there's essentially
135:00 of the screen well there's essentially these four here so first highlighted up
135:03 these four here so first highlighted up top is going to be one or more tabs
135:06 top is going to be one or more tabs where you're going to actually write
135:07 where you're going to actually write code so much like in Google Docs or
135:09 code so much like in Google Docs or Microsoft Word you can have tabs open
135:10 Microsoft Word you can have tabs open with files similarly in VSS code or
135:13 with files similarly in VSS code or really any programming environment do
135:15 really any programming environment do generally nowadays have tabs of some
135:17 generally nowadays have tabs of some sort and this is going to be a tab
135:18 sort and this is going to be a tab containing a file it seems called
135:20 containing a file it seems called hello.c and that's going to be the very
135:22 hello.c and that's going to be the very first file we write in just a moment uh
135:24 first file we write in just a moment uh down here though is going to be an
135:26 down here though is going to be an interface that many of you might not
135:27 interface that many of you might not know this is what's called a terminal
135:30 know this is what's called a terminal window and a terminal window provides
135:32 window and a terminal window provides what's generally called a commandline
135:34 what's generally called a commandline interface or CLI and this is in contrast
135:37 interface or CLI and this is in contrast with a graphical user interface or guey
135:40 with a graphical user interface or guey now you and I every day are using guies
135:42 now you and I every day are using guies on our phones on our PCS and a guy is
135:44 on our phones on our PCS and a guy is literally graphical so menus and buttons
135:47 literally graphical so menus and buttons and icons and you generally use your
135:48 and icons and you generally use your finger or a trackpad or a mouse or
135:51 finger or a trackpad or a mouse or something like that to interact with it
135:53 something like that to interact with it but it turns out that many programmers
135:54 but it turns out that many programmers dare say most programmers at least over
135:56 dare say most programmers at least over time come to prefer not a guey but a CLI
135:59 time come to prefer not a guey but a CLI a command line interface where you
136:01 a command line interface where you actually do everything somewhat uh
136:04 actually do everything somewhat uh somewhat arcely via keyboard alone why
136:07 somewhat arcely via keyboard alone why well it turns out there's just more
136:09 well it turns out there's just more features built in to most computers if
136:11 features built in to most computers if you can access them with a keyboard
136:12 you can access them with a keyboard turns out you can most of us can type
136:14 turns out you can most of us can type faster than you can point and click and
136:16 faster than you can point and click and so that ends up being an efficient gain
136:18 so that ends up being an efficient gain over time so in time will you get
136:20 over time so in time will you get comfortable using this terminal window
136:22 comfortable using this terminal window to do things like compile your code or
136:24 to do things like compile your code or make your program as well as run it so
136:27 make your program as well as run it so you won't be in the habit initially of
136:29 you won't be in the habit initially of just double clicking icons like we do in
136:30 just double clicking icons like we do in our typical real world you'll do it sort
136:32 our typical real world you'll do it sort of the programmer's way but it's not to
136:35 of the programmer's way but it's not to the exclusion of adding icons and
136:36 the exclusion of adding icons and clickability and more on the left hand
136:39 clickability and more on the left hand side of vs code there's going to be a
136:40 side of vs code there's going to be a somewhat familiar file explorer some
136:43 somewhat familiar file explorer some kind of hierarchical tree like on your
136:45 kind of hierarchical tree like on your Mac or PC where you can see all of the
136:47 Mac or PC where you can see all of the files in your account pictured here for
136:49 files in your account pictured here for instance is just hello.c which I'll
136:51 instance is just hello.c which I'll create myself in a moment and then far
136:53 create myself in a moment and then far away on the left is the so-called
136:55 away on the left is the so-called activity bar and this is where you just
136:56 activity bar and this is where you just get a lot of traditional menus and
136:58 get a lot of traditional menus and buttons so VSS code itself gives you
137:00 buttons so VSS code itself gives you both a guey and a CLI but it's within
137:03 both a guey and a CLI but it's within the CLI the terminal window the bottom
137:06 the CLI the terminal window the bottom region of the screen that we're actually
137:07 region of the screen that we're actually going to type most of our commands and
137:08 going to type most of our commands and in general in class I'm going to hide
137:10 in general in class I'm going to hide all of the graphical stuff that's just
137:12 all of the graphical stuff that's just not of all that uh that much interest so
137:15 not of all that uh that much interest so with that said let me actually change
137:16 with that said let me actually change over to a live version of vs code and
137:19 over to a live version of vs code and I've indeed HD in the activity bar I've
137:21 I've indeed HD in the activity bar I've indeed hid in the file explorer so what
137:23 indeed hid in the file explorer so what I have here for visibility sake is a
137:25 I have here for visibility sake is a really big area for writing code and a
137:27 really big area for writing code and a really big terminal window at the bottom
137:29 really big terminal window at the bottom you'll see in the terminal window
137:30 you'll see in the terminal window there's a dollar sign and this doesn't
137:32 there's a dollar sign and this doesn't mean any form of currency this is just
137:34 mean any form of currency this is just the standard symbol that represents type
137:36 the standard symbol that represents type commands here so the fact that there's
137:38 commands here so the fact that there's just a dollar sign in a cursor means
137:40 just a dollar sign in a cursor means eventually that's where I'm going to
137:41 eventually that's where I'm going to type commands but first i'm going to
137:43 type commands but first i'm going to actually create some code so how might I
137:46 actually create some code so how might I program using vs code be it on my Mac PC
137:49 program using vs code be it on my Mac PC or in this cloud-based environment that
137:51 or in this cloud-based environment that you'll get set up for problem set one go
137:54 you'll get set up for problem set one go about writing my first file well perhaps
137:55 about writing my first file well perhaps the easiest way is this literally run
137:57 the easiest way is this literally run the command code and then the name of
138:00 the command code and then the name of the file you want to create notice that
138:02 the file you want to create notice that I deliberately end the file with C in
138:04 I deliberately end the file with C in lowercase notice that I've deliberately
138:06 lowercase notice that I've deliberately lowercased the whole file name and these
138:08 lowercased the whole file name and these are just conventions you could use a
138:10 are just conventions you could use a capital H you kind of could use a
138:12 capital H you kind of could use a Capital C but just don't do that follow
138:14 Capital C but just don't do that follow best practices so that it's consistent
138:15 best practices so that it's consistent with what most everyone else would do
138:17 with what most everyone else would do when I hit enter I just get an empty tab
138:20 when I hit enter I just get an empty tab just like the screenshot a moment ago
138:22 just like the screenshot a moment ago and it's in this tab where I can now
138:24 and it's in this tab where I can now write my very first program in C
138:26 write my very first program in C unfortunately it's not quite as user
138:28 unfortunately it's not quite as user friendly as scratch where you drag and
138:29 friendly as scratch where you drag and drop a couple of puzzle pieces and boom
138:31 drop a couple of puzzle pieces and boom it's done so I'm going to do this for
138:33 it's done so I'm going to do this for memory but this too will become familiar
138:35 memory but this too will become familiar to you over time I'm going to include
138:37 to you over time I'm going to include something called standard io. I'm going
138:40 something called standard io. I'm going to type int main void and parentheses on
138:44 to type int main void and parentheses on a new line I'm going to insert some
138:45 a new line I'm going to insert some curly braces as we'll call them and then
138:48 curly braces as we'll call them and then I'm going to type print F and then some
138:50 I'm going to type print F and then some parentheses and then in quotes hello
138:53 parentheses and then in quotes hello comma world then a backslash then a
138:55 comma world then a backslash then a lowercase n then a close quote and then
138:58 lowercase n then a close quote and then a semicolon at the very end of the line
139:00 a semicolon at the very end of the line so all I've done is recreate just from
139:03 so all I've done is recreate just from memory that very first program in a
139:05 memory that very first program in a little bit we'll make clear what most of
139:07 little bit we'll make clear what most of this does but for now let's just
139:09 this does but for now let's just actually run this thing and just like I
139:11 actually run this thing and just like I click the green flag last week for the
139:12 click the green flag last week for the first time let's actually compile and
139:15 first time let's actually compile and run this program if it were your Mac PC
139:18 run this program if it were your Mac PC and Google or Microsoft or someone else
139:20 and Google or Microsoft or someone else had made the software at this point in
139:22 had made the software at this point in the story we'd be double clicking an
139:23 the story we'd be double clicking an icon but we can't do that yet this is
139:25 icon but we can't do that yet this is still source code so I'm going to click
139:27 still source code so I'm going to click back down in my terminal window notice I
139:29 back down in my terminal window notice I have a second dollar sign below the
139:31 have a second dollar sign below the first which just means it's ready for a
139:33 first which just means it's ready for a second command and now the command via
139:35 second command and now the command via which to make this an actual program to
139:38 which to make this an actual program to compile it from source code to machine
139:41 compile it from source code to machine code is going to be quite simply make
139:43 code is going to be quite simply make and then the name of the program I want
139:45 and then the name of the program I want to make slight subtlety I'm omitting
139:48 to make slight subtlety I'm omitting deliberately. C because the program I
139:50 deliberately. C because the program I want to make I just want to call hello
139:52 want to make I just want to call hello so don't write make hello. C just write
139:54 so don't write make hello. C just write make hello and this program make is
139:58 make hello and this program make is essentially our compiler technically
140:00 essentially our compiler technically speaking it's a program that automates
140:02 speaking it's a program that automates the compilation of my program for me but
140:04 the compilation of my program for me but it is going to see that I've typed the
140:06 it is going to see that I've typed the word hello it's going to automatically
140:08 word hello it's going to automatically look now for a file on the hard drive
140:10 look now for a file on the hard drive called hello.c and convert it from
140:13 called hello.c and convert it from source code in C to machine code in
140:16 source code in C to machine code in zeros and ones so if I didn't make any
140:18 zeros and ones so if I didn't make any typos
140:20 typos enter nothing seems to happen and that's
140:23 enter nothing seems to happen and that's a good thing almost always if nothing
140:26 a good thing almost always if nothing gets outputed on the screen like you did
140:28 gets outputed on the screen like you did good like you didn't make any mistakes
140:29 good like you didn't make any mistakes you didn't get yelled at there's no
140:30 you didn't get yelled at there's no error messages so this is actually a
140:32 error messages so this is actually a good thing how do I now run this program
140:34 good thing how do I now run this program well notice I've got a third dollar sign
140:36 well notice I've got a third dollar sign which just means I'm ready for a third
140:38 which just means I'm ready for a third command and now I'm going to go ahead
140:39 command and now I'm going to go ahead and run dot slash hello and this is
140:42 and run dot slash hello and this is admittedly a little weird that you have
140:44 admittedly a little weird that you have to do dot slash but for now just take on
140:46 to do dot slash but for now just take on faith that this is how you run a program
140:48 faith that this is how you run a program called hello in your current folder in
140:51 called hello in your current folder in your current directory in this
140:52 your current directory in this cloud-based environment all right
140:54 cloud-based environment all right crossing my fingers again hitting enter
140:57 crossing my fingers again hitting enter and voila my very first program in see
141:01 and voila my very first program in see hello world and now let me go ahead and
141:04 hello world and now let me go ahead and reveal the file explorer that I proposed
141:07 reveal the file explorer that I proposed exist earlier I'm just going to use a
141:08 exist earlier I'm just going to use a keyboard shortcut to reveal that and
141:10 keyboard shortcut to reveal that and generally I keep it close because I
141:11 generally I keep it close because I don't really need to know constantly
141:13 don't really need to know constantly what files are in my account but you'll
141:15 what files are in my account but you'll see now in the file explorer similar
141:16 see now in the file explorer similar INSP to a Mac or PC but graphically a
141:19 INSP to a Mac or PC but graphically a little different here's my file hello.c
141:21 little different here's my file hello.c it's highlighted because I have that tab
141:23 it's highlighted because I have that tab open but now there's a second file here
141:26 open but now there's a second file here called just hello that's the name of my
141:28 called just hello that's the name of my program so if you were on a Mac or PC
141:30 program so if you were on a Mac or PC you would ideally double click that
141:32 you would ideally double click that thing you can't do that in a command
141:34 thing you can't do that in a command line environment you have to run it down
141:35 line environment you have to run it down here but that's all we've done we've
141:37 here but that's all we've done we've created a file called hello.c and then
141:39 created a file called hello.c and then my compiler made the program from that
141:42 my compiler made the program from that let me pause here and see if there's any
141:45 let me pause here and see if there's any questions because it's a lot of magical
141:48 questions because it's a lot of magical phrases
141:50 phrases yeah yeah so if you're currently
141:52 yeah yeah so if you're currently following along uh playing along at home
141:55 following along uh playing along at home and you're getting some kind of error
141:56 and you're getting some kind of error message part of today will be for me to
141:58 message part of today will be for me to deliberately induce some of those error
141:59 deliberately induce some of those error messages for now let me just propose
142:02 messages for now let me just propose that if you literally did what I did you
142:03 that if you literally did what I did you must have made a typo somewhere and
142:06 must have made a typo somewhere and notice that it's indeed standard IO
142:09 notice that it's indeed standard IO stdio.h uh maybe you type
142:12 stdio.h uh maybe you type studio. okay super common mistake if I
142:15 studio. okay super common mistake if I don't if I could call you out um like it
142:17 don't if I could call you out um like it is not studio. it is standard io. so
142:20 is not studio. it is standard io. so common but this is exactly
142:22 common but this is exactly representative of like the kind of
142:23 representative of like the kind of stupid headaches you're going to run
142:24 stupid headaches you're going to run into this week probably for a few weeks
142:26 into this week probably for a few weeks probably honestly for a few years but
142:28 probably honestly for a few years but you start to see past these sort of
142:30 you start to see past these sort of stupid mistakes over time and it just
142:32 stupid mistakes over time and it just gets easier and easier because the
142:33 gets easier and easier because the computer is going to be so regimented
142:35 computer is going to be so regimented like you it will only do what you tell
142:36 like you it will only do what you tell it to do and if you say because it's
142:38 it to do and if you say because it's verbally sounds like studio. it's not
142:41 verbally sounds like studio. it's not going to know what the file is so
142:42 going to know what the file is so actually thank you for tripping over
142:44 actually thank you for tripping over that so early that's super common to
142:46 that so early that's super common to happen yeah have two hello so I why do I
142:49 happen yeah have two hello so I why do I have two hello files so why do I have
142:51 have two hello files so why do I have two hello files one is the one I created
142:53 two hello files one is the one I created as the human called hello.c and it's
142:56 as the human called hello.c and it's pictured right here but then when I ran
142:58 pictured right here but then when I ran make hello that process compiled my
143:01 make hello that process compiled my source code into machine code so this
143:04 source code into machine code so this second file just called hello is the
143:06 second file just called hello is the file that contains all of those zeros
143:08 file that contains all of those zeros and ones that the server actually
143:11 and ones that the server actually understands all right so yeah
143:17 question if you try clicking on the Hello file you'll see in this
143:19 Hello file you'll see in this environment of vs code quote unquote the
143:21 environment of vs code quote unquote the file is not displayed in the editor
143:23 file is not displayed in the editor because it is either binary AKA zeros
143:25 because it is either binary AKA zeros and ones or uses an unsupported text
143:27 and ones or uses an unsupported text encoding in this case it's binary it's
143:29 encoding in this case it's binary it's zeros and ones now you could use
143:32 zeros and ones now you could use software to see those zeros and ones it
143:34 software to see those zeros and ones it won't be intellectually enlightening to
143:35 won't be intellectually enlightening to most any human so VSS code just takes
143:38 most any human so VSS code just takes the choice of not showing it to you at
143:39 the choice of not showing it to you at all so that would be a common mistake
143:41 all so that would be a common mistake too clicking on a file you don't intend
143:43 too clicking on a file you don't intend but the source code is indeed going to
143:44 but the source code is indeed going to be editable by us all right all right so
143:47 be editable by us all right all right so I've written this program it seems to
143:48 I've written this program it seems to sort of magically work at least with
143:50 sort of magically work at least with some effort if you get every single
143:52 some effort if you get every single keystroke right well what is it that's
143:54 keystroke right well what is it that's going on and how is this working well
143:56 going on and how is this working well first of all notice that even without my
143:58 first of all notice that even without my highlighting things or choosing buttons
144:01 highlighting things or choosing buttons from menus notice that it's already kind
144:03 from menus notice that it's already kind of colorcoded and yet I wasn't
144:05 of colorcoded and yet I wasn't highlighting along the way in sort of
144:06 highlighting along the way in sort of Google doc style changing the color
144:08 Google doc style changing the color certainly well it turns out what VSS
144:10 certainly well it turns out what VSS code and most programming environments
144:12 code and most programming environments nowadays do for you automatically is
144:14 nowadays do for you automatically is syntax highlighting so syntax
144:17 syntax highlighting so syntax highlighting is just this feature of
144:19 highlighting is just this feature of typical text editors nowadays that
144:21 typical text editors nowadays that analyzes the code that you've typed and
144:23 analyzes the code that you've typed and when it notices certain types of
144:25 when it notices certain types of keystrokes things that represent
144:27 keystrokes things that represent functions or conditionals or Loops or
144:30 functions or conditionals or Loops or variables a lot of the vocab from last
144:31 variables a lot of the vocab from last week it just highlights it ever so
144:33 week it just highlights it ever so differently for you so main for instance
144:35 differently for you so main for instance which we'll soon see is in purple here
144:38 which we'll soon see is in purple here int and void and include are in red
144:40 int and void and include are in red hello world is in blue my parentheses
144:42 hello world is in blue my parentheses are in green this will totally vary by
144:45 are in green this will totally vary by programmer too in fact if you do want to
144:47 programmer too in fact if you do want to change these colors for problem set one
144:49 change these colors for problem set one for your own environment you can poke
144:51 for your own environment you can poke around vs code settings via the gear
144:53 around vs code settings via the gear icon you can change a different a change
144:55 icon you can change a different a change to a different color theme syntax
144:57 to a different color theme syntax highlighting isn't some specific color
144:58 highlighting isn't some specific color scheme like it is in scratch it just
145:00 scheme like it is in scratch it just generally is to each human their own
145:02 generally is to each human their own preference but that's all that's
145:03 preference but that's all that's happening here is this notion of syntax
145:06 happening here is this notion of syntax highlighting at the moment well what
145:07 highlighting at the moment well what more is going on in this code before I
145:10 more is going on in this code before I run it but rather write it well it looks
145:12 run it but rather write it well it looks a little something like this if I take
145:14 a little something like this if I take away all of the colors and then just for
145:15 away all of the colors and then just for discussion sake let me ahead and color
145:17 discussion sake let me ahead and color it a little more like scratch recall
145:19 it a little more like scratch recall that our very first scratch program that
145:21 that our very first scratch program that just said hello world on the screen had
145:23 just said hello world on the screen had a when green flag clicked icon uh puzzle
145:26 a when green flag clicked icon uh puzzle piece roughly an orange and then a
145:29 piece roughly an orange and then a purple uh sa block beneath it so whereas
145:32 purple uh sa block beneath it so whereas this is the C version if we rewind to
145:34 this is the C version if we rewind to last week this was the same program in
145:37 last week this was the same program in scratch but what's happening now is
145:38 scratch but what's happening now is exactly the same so if you think back to
145:40 exactly the same so if you think back to last week and you've got some function
145:42 last week and you've got some function like the say function in purple that
145:45 like the say function in purple that might take one or more arguments like
145:47 might take one or more arguments like inputs that influences what it says on
145:49 inputs that influences what it says on the screen and then functions recall can
145:51 the screen and then functions recall can sometimes have side effects right like
145:53 sometimes have side effects right like the speech bubble appears on the screen
145:55 the speech bubble appears on the screen so last week when we used the sa block
145:57 so last week when we used the sa block and we passed in an argument of hello
145:59 and we passed in an argument of hello world at left we got this visual side
146:01 world at left we got this visual side effect on the screen that says now hello
146:04 effect on the screen that says now hello world in the speech bubble and that's
146:05 world in the speech bubble and that's exactly what just happened in VSS code
146:07 exactly what just happened in VSS code but much much more textually and let's
146:10 but much much more textually and let's look a little closer now at the code
146:11 look a little closer now at the code itself let me wave my hand at the
146:14 itself let me wave my hand at the equivalent of the when green flag
146:16 equivalent of the when green flag clicked part of my code and let's focus
146:18 clicked part of my code and let's focus only on the say block in scratch and the
146:22 only on the say block in scratch and the corresponding function in C so if I step
146:26 corresponding function in C so if I step through this and I wanted to convert
146:28 through this and I wanted to convert what we did last week with the say block
146:30 what we did last week with the say block to C I would first use the print
146:33 to C I would first use the print function although that's actually a bit
146:34 function although that's actually a bit of a white lie it's actually the print F
146:36 of a white lie it's actually the print F function print f means formatted and
146:38 function print f means formatted and it's just a function that allows you to
146:40 it's just a function that allows you to format text on the screen There Is No Sa
146:42 format text on the screen There Is No Sa function in C there's a print F function
146:45 function in C there's a print F function what MIT did down the road years ago go
146:47 what MIT did down the road years ago go was they took what existed historically
146:49 was they took what existed historically as print F and they simplified it for a
146:51 as print F and they simplified it for a broader audience by just calling it
146:52 broader audience by just calling it essentially say instead but notice that
146:55 essentially say instead but notice that now if I want to convert the scratch
146:58 now if I want to convert the scratch code at left to C code at right it's
147:00 code at left to C code at right it's sort of the same shape so MIT
147:03 sort of the same shape so MIT deliberately used this white oval if
147:05 deliberately used this white oval if only because it kind of conjures these
147:08 only because it kind of conjures these uh this idea of having parentheses too
147:10 uh this idea of having parentheses too so on the right if I want to pass an
147:12 so on the right if I want to pass an argument or an input to the print F
147:14 argument or an input to the print F function I use an open parenthesis and a
147:16 function I use an open parenthesis and a CL parenthesis in those parentheses I
147:19 CL parenthesis in those parentheses I then type whatever it is I want to print
147:21 then type whatever it is I want to print on the screen in this case hello comma
147:23 on the screen in this case hello comma world but notice I've deliberately left
147:25 world but notice I've deliberately left some room because you need some extra
147:26 some room because you need some extra keystrokes in the world of C anytime you
147:29 keystrokes in the world of C anytime you type out some text otherwise known as a
147:32 type out some text otherwise known as a string of text to use computer science
147:35 string of text to use computer science jargon you need to quote it in this case
147:37 jargon you need to quote it in this case with double quotes double quote at the
147:39 with double quotes double quote at the left double quote at the right and
147:41 left double quote at the right and notice to I'm going to include some
147:43 notice to I'm going to include some slightly cryptic symbol here too back
147:45 slightly cryptic symbol here too back sln which I also typed and said verbally
147:47 sln which I also typed and said verbally earlier and then one last nuisance at
147:49 earlier and then one last nuisance at the end of this which is a semicolon so
147:52 the end of this which is a semicolon so suffice it to say this is why we start
147:54 suffice it to say this is why we start with scratch this drag and drop you're
147:57 with scratch this drag and drop you're good to go in a language like C print F
147:59 good to go in a language like C print F parenthesis double quotes the text you
148:01 parenthesis double quotes the text you want backs slash and semicolon at the
148:03 want backs slash and semicolon at the end there's just so much syntactic
148:04 end there's just so much syntactic overhead but at the end of the day it's
148:07 overhead but at the end of the day it's just a function and you'll get used to
148:08 just a function and you'll get used to these sort of nuisances like the
148:10 these sort of nuisances like the parentheses the quotes the semicolon and
148:12 parentheses the quotes the semicolon and the like but things can very easily go
148:15 the like but things can very easily go wrong and it's very easy to make
148:17 wrong and it's very easy to make mistakes even with lines of code like
148:19 mistakes even with lines of code like this so let me do this let me go back to
148:21 this so let me do this let me go back to vs code where I have the exact same code
148:23 vs code where I have the exact same code notice that on line five is exactly that
148:26 notice that on line five is exactly that line of code so this is the equivalent
148:28 line of code so this is the equivalent of the sa block and let's consider what
148:30 of the sa block and let's consider what mistakes I may make early on or even now
148:32 mistakes I may make early on or even now 20 years later after learning this that
148:34 20 years later after learning this that are quite common um in general suppose I
148:37 are quite common um in general suppose I forget the semicolon there so easy to do
148:39 forget the semicolon there so easy to do you will do this eventually let's see
148:41 you will do this eventually let's see what happens now when I go back to my
148:43 what happens now when I go back to my terminal window and try to compile my
148:45 terminal window and try to compile my code again just to keep things tidy I'm
148:47 code again just to keep things tidy I'm going to clear my screen but that's just
148:49 going to clear my screen but that's just for lecture sake so that we can focus
148:51 for lecture sake so that we can focus only on the most recent command but I'm
148:53 only on the most recent command but I'm going to go ahead now and rerun make
148:55 going to go ahead now and rerun make Hello this will ensure that my program
148:59 Hello this will ensure that my program is recompiled and this is a manual
149:01 is recompiled and this is a manual process I changed my code the zeros and
149:03 process I changed my code the zeros and ones on the hard drive have not changed
149:05 ones on the hard drive have not changed I need to recompile it to Output the
149:07 I need to recompile it to Output the latest machine code so here we go I'm
149:09 latest machine code so here we go I'm going to hit enter crossing my fingers
149:10 going to hit enter crossing my fingers as before but again I remove the
149:12 as before but again I remove the semicolon by
149:14 semicolon by accident oh my God there's like more
149:17 accident oh my God there's like more lines of Errors now than there are of
149:19 lines of Errors now than there are of actual code and this too takes them
149:21 actual code and this too takes them getting used to um the programs we're
149:23 getting used to um the programs we're using were not necessarily written with
149:25 using were not necessarily written with the least comfortable audience in mind
149:27 the least comfortable audience in mind but really professional programmers back
149:28 but really professional programmers back in the day but through practice and
149:31 in the day but through practice and through experience and through mistakes
149:32 through experience and through mistakes you'll start to notice patterns here too
149:34 you'll start to notice patterns here too so here's what I typed make hello after
149:36 so here's what I typed make hello after the dollar sign prompt now I get yelled
149:38 the dollar sign prompt now I get yelled at with as follows hello.c colon
149:42 at with as follows hello.c colon 529 well what's that referring to I've
149:45 529 well what's that referring to I've screwed up somewhere on line five on the
149:47 screwed up somewhere on line five on the 29th character on that line generally
149:50 29th character on that line generally the specific character is not that
149:51 the specific character is not that useful unless you actually want to count
149:53 useful unless you actually want to count it out but line five is a good clue why
149:55 it out but line five is a good clue why it means I screwed up somewhere on line
149:57 it means I screwed up somewhere on line five here all right well what is the
149:59 five here all right well what is the error expected a semicolon after
150:02 error expected a semicolon after expression and this error is actually
150:03 expression and this error is actually pretty obvious now that I see it and I
150:05 pretty obvious now that I see it and I realize oh wait a minute all right
150:06 realize oh wait a minute all right here's my line of code here in sort of
150:09 here's my line of code here in sort of asky art so to speak textual uh text
150:12 asky art so to speak textual uh text representing Graphics it wants me to put
150:14 representing Graphics it wants me to put in green here this semicolon at the end
150:16 in green here this semicolon at the end of that line one error generated built
150:18 of that line one error generated built in so some esoteric stuff there but my
150:21 in so some esoteric stuff there but my program did not compile when you see an
150:23 program did not compile when you see an error like this it means it did not work
150:26 error like this it means it did not work so what's the fix well obviously the fix
150:28 so what's the fix well obviously the fix is to go back up here put the semicolon
150:31 is to go back up here put the semicolon there and now if I recompile my code
150:34 there and now if I recompile my code with make hello I won't clear my screen
150:36 with make hello I won't clear my screen just yet just to show you the difference
150:37 just yet just to show you the difference now it just worked so we're back in
150:40 now it just worked so we're back in business as before all right let me
150:42 business as before all right let me pause here though and ask if there's any
150:44 pause here though and ask if there's any questions about what I just did
150:46 questions about what I just did [Music]
150:48 [Music] these error messages will become
150:50 these error messages will become frequent initially
150:57 yeah really good question do you need a semicolon after every line or just some
150:59 semicolon after every line or just some it turns out just some uh this is
151:01 it turns out just some uh this is something you'll learn through practice
151:02 something you'll learn through practice through demonstrations and examples
151:04 through demonstrations and examples today generally you put a semicolon
151:06 today generally you put a semicolon after a statement so to speak and this
151:10 after a statement so to speak and this is the technical term for this line of
151:11 is the technical term for this line of code it's a statement and think of it is
151:13 code it's a statement and think of it is it's kind of the code equivalent of like
151:14 it's kind of the code equivalent of like an English sentence so the semi colon
151:16 an English sentence so the semi colon and code is sort of like a period in
151:19 and code is sort of like a period in English when you're done with that
151:20 English when you're done with that particular thought you don't need
151:22 particular thought you don't need semicolons for now anywhere else and
151:24 semicolons for now anywhere else and we'll see examples of where else you put
151:26 we'll see examples of where else you put them but it usually is at the end of a
151:27 them but it usually is at the end of a line of code that hasn't that isn't
151:30 line of code that hasn't that isn't purely syntactic like uh curly braces
151:32 purely syntactic like uh curly braces instead other questions on the mistake I
151:35 instead other questions on the mistake I just fixed and created for myself
151:39 just fixed and created for myself [Music]
151:40 [Music] yeah uh correct so line five is where
151:44 yeah uh correct so line five is where the error is most likely character 29
151:46 the error is most likely character 29 means it's sort of 29 characters that
151:48 means it's sort of 29 characters that way and then it's actually in this case
151:50 way and then it's actually in this case giving me a suggestion the compiler
151:52 giving me a suggestion the compiler won't always know how to advise me
151:53 won't always know how to advise me especially if I've made a real mess of
151:55 especially if I've made a real mess of my code but often it will do its best to
151:58 my code but often it will do its best to give you the answer like this
152:06 yeah ah so how come I first typed code space hello.c and now I'm typing make
152:09 space hello.c and now I'm typing make hello two different processes so when I
152:11 hello two different processes so when I typed code space hello.c that was
152:14 typed code space hello.c that was because I wanted to open vs code and
152:15 because I wanted to open vs code and create a new new file called hello.c
152:17 create a new new file called hello.c it's like going to file new in in a a
152:20 it's like going to file new in in a a Mac or PC thereafter though once the
152:22 Mac or PC thereafter though once the file exists and is actually open here
152:25 file exists and is actually open here and it does autosave you don't need to
152:26 and it does autosave you don't need to hit command s or control s all the time
152:29 hit command s or control s all the time I can now compile it with make hello
152:31 I can now compile it with make hello again and again so theoretically I
152:33 again and again so theoretically I should never need to type code space
152:36 should never need to type code space hello.c again unless I want to create a
152:38 hello.c again unless I want to create a brand new file called the same thing all
152:40 brand new file called the same thing all right so what about this other piece of
152:43 right so what about this other piece of syntax here let me clear my terminal
152:44 syntax here let me clear my terminal window here you can also hit control L
152:46 window here you can also hit control L just to throw everything away just to
152:48 just to throw everything away just to clean it up aesthetically suppose that I
152:50 clean it up aesthetically suppose that I omit whatever this sequence of symbols
152:53 omit whatever this sequence of symbols is back sln since I'm not really sure at
152:55 is back sln since I'm not really sure at first glance why that's even there does
152:58 first glance why that's even there does anyone want to conjecture especially if
152:59 anyone want to conjecture especially if you've never programmed before what
153:01 you've never programmed before what might happen now if I recompile and
153:04 might happen now if I recompile and rerun this version of the program I left
153:07 rerun this version of the program I left the semicolon but I took away the backs
153:09 the semicolon but I took away the backs slash
153:10 slash n any
153:13 n any instincts all right well yeah
153:17 instincts all right well yeah will the next dollar sign appear
153:19 will the next dollar sign appear straight after your it will the next
153:22 straight after your it will the next dollar sign will appear right after my
153:24 dollar sign will appear right after my hello world but what makes you think
153:27 hello world but what makes you think that back creates a line exactly back
153:30 that back creates a line exactly back sln is actually a special sequence of
153:32 sln is actually a special sequence of symbols that creates a new line and so
153:35 symbols that creates a new line and so to your point if I recompile this
153:37 to your point if I recompile this program make hello enter no syntax error
153:40 program make hello enter no syntax error so it did compile this time so you don't
153:42 so it did compile this time so you don't need the backs slash in you do need the
153:44 need the backs slash in you do need the semicolon but if you don't have the back
153:46 semicolon but if you don't have the back sln watch what happens when I do/ hello
153:49 sln watch what happens when I do/ hello this time now indeed I see hello comma
153:52 this time now indeed I see hello comma world and then a weird dollar sign and
153:53 world and then a weird dollar sign and this is still a prompt I can still type
153:55 this is still a prompt I can still type commands at it like clear and everything
153:57 commands at it like clear and everything gets cleaned up but it just looks kind
153:59 gets cleaned up but it just looks kind of stupid if I run it again here with
154:02 of stupid if I run it again here with hello you know it's just not very user
154:04 hello you know it's just not very user friendly it is convention that when
154:06 friendly it is convention that when you're done running your program you
154:07 you're done running your program you should ideally clean things up move the
154:09 should ideally clean things up move the cursor to the next line for the user and
154:11 cursor to the next line for the user and so the backs slash n is simply the
154:13 so the backs slash n is simply the special symbol otherwise known as an
154:15 special symbol otherwise known as an escape
154:16 escape sequence that c knows means move the
154:20 sequence that c knows means move the cursor to the next line in other
154:21 cursor to the next line in other languages python among them uses the
154:24 languages python among them uses the same symbology as well now if I go back
154:28 same symbology as well now if I go back to the code here and for instance I try
154:32 to the code here and for instance I try to do this differently like suppose I
154:34 to do this differently like suppose I don't put the backs slash N I just hit
154:36 don't put the backs slash N I just hit enter like a normal person would in
154:37 enter like a normal person would in Google Docs or Microsoft Word let me go
154:39 Google Docs or Microsoft Word let me go ahead and try compiling this program and
154:42 ahead and try compiling this program and this you would hope would work right you
154:43 this you would hope would work right you would hope this would print out hello
154:45 would hope this would print out hello world and then a blank line because I
154:47 world and then a blank line because I move the cursor to the next line but no
154:49 move the cursor to the next line but no if I run make hello now and try to
154:51 if I run make hello now and try to compile that c does not like this now I
154:54 compile that c does not like this now I get a different error still on line five
154:56 get a different error still on line five this time starting at character 12 uh
154:58 this time starting at character 12 uh error missing terminating double quote
155:01 error missing terminating double quote character and then some other esoteric
155:03 character and then some other esoteric stuff and then this does not sound good
155:04 stuff and then this does not sound good fatal error this time too many errors
155:07 fatal error this time too many errors admitted stopping now so I really
155:08 admitted stopping now so I really screwed up here so why can't I do this
155:12 screwed up here so why can't I do this just because like the humans who
155:13 just because like the humans who designed C decided that if you have a
155:15 designed C decided that if you have a string of text it must stay on the same
155:17 string of text it must stay on the same line it can get really long it can soft
155:20 line it can get really long it can soft wrap that is without you hitting enter
155:22 wrap that is without you hitting enter but you can't hit enter to create a new
155:24 but you can't hit enter to create a new line if you deliberately want a new line
155:26 line if you deliberately want a new line you have to indeed use this back slash
155:29 you have to indeed use this back slash and Escape character so let me go ahead
155:31 and Escape character so let me go ahead and do this let me put it back let me go
155:33 and do this let me put it back let me go back to my terminal window I'll clear
155:35 back to my terminal window I'll clear the screen again let me go ahead now and
155:37 the screen again let me go ahead now and do make Hello to recompile to that
155:39 do make Hello to recompile to that version do/ hello and voila we're back
155:41 version do/ hello and voila we're back in business with uh hello all right so
155:44 in business with uh hello all right so now let's tease apart some some other
155:46 now let's tease apart some some other aspects of this code because there's a
155:48 aspects of this code because there's a lot going on just to get us to say hello
155:50 lot going on just to get us to say hello world on the screen for today we're
155:52 world on the screen for today we're largely going to ignore this int main
155:54 largely going to ignore this int main void and these curly braces here we'll
155:56 void and these curly braces here we'll come back to that before long as to why
155:58 come back to that before long as to why it's there but for now just think of int
156:00 it's there but for now just think of int main void and these curly braces here as
156:03 main void and these curly braces here as really being the C equivalent of when
156:04 really being the C equivalent of when green flag clicked like why you just
156:06 green flag clicked like why you just need it there that's how you get your
156:08 need it there that's how you get your program going and Maine is indeed going
156:10 program going and Maine is indeed going to be some special function but more on
156:11 to be some special function but more on that another time but why do I have this
156:14 that another time but why do I have this line of code here the cor spelling is
156:16 line of code here the cor spelling is indeed standard io.
156:19 indeed standard io. stdio.h and they're angled brackets this
156:22 stdio.h and they're angled brackets this time so that's a little new there's a
156:24 time so that's a little new there's a hash and then an include keyword you
156:27 hash and then an include keyword you know if you don't know what something is
156:28 know if you don't know what something is you know there's not really that much
156:29 you know there's not really that much harm in just getting rid of it and see
156:31 harm in just getting rid of it and see what happens so let me delete that line
156:32 what happens so let me delete that line let me go back to my terminal window
156:34 let me go back to my terminal window clear the screen and then run make hello
156:36 clear the screen and then run make hello again and let's try compiling this
156:38 again and let's try compiling this program now without that first line why
156:40 program now without that first line why I don't understand it so let's see what
156:42 I don't understand it so let's see what happens all right here's yet another
156:44 happens all right here's yet another error but let's see how hello. C line
156:47 error but let's see how hello. C line five character 5 so it's pretty early on
156:50 five character 5 so it's pretty early on error implicitly declaring Library
156:52 error implicitly declaring Library function printf with type int and then
156:54 function printf with type int and then dot dot dot so implicitly declaring
156:57 dot dot dot so implicitly declaring Library function print F so this is very
157:00 Library function print F so this is very cryptic sounding you'll get better at
157:02 cryptic sounding you'll get better at understanding phrases like these but
157:03 understanding phrases like these but apparently I do need the include line
157:07 apparently I do need the include line for standard
157:08 for standard i.h but why based on this symptom what
157:13 i.h but why based on this symptom what might Your Instinct be for what that
157:16 might Your Instinct be for what that first line of code is doing for us in
157:18 first line of code is doing for us in the first place why intuitively must it
157:21 the first place why intuitively must it be
157:23 be there exactly it's like importing a
157:25 there exactly it's like importing a library so that you can do things like
157:27 library so that you can do things like print things out on the screen now in
157:29 print things out on the screen now in scratch you didn't have to do this for
157:30 scratch you didn't have to do this for most of the puzzle pieces but you might
157:32 most of the puzzle pieces but you might recall that partway in through week zero
157:34 recall that partway in through week zero I went to the extensions button at the
157:36 I went to the extensions button at the bottom left of the scratch screen and I
157:38 bottom left of the scratch screen and I imported some extra puzzle pieces for
157:40 imported some extra puzzle pieces for text to speech that gave us the sort of
157:43 text to speech that gave us the sort of creepy uh humanized voice that actually
157:45 creepy uh humanized voice that actually came out of the cat's mouth well that
157:47 came out of the cat's mouth well that was like adding a library code that
157:49 was like adding a library code that someone else wrote in that case it was a
157:51 someone else wrote in that case it was a third party then but I gave myself
157:54 third party then but I gave myself access to it same here turns out that
157:56 access to it same here turns out that you don't really get printf
157:58 you don't really get printf automatically in C you have to include a
158:02 automatically in C you have to include a so-called header file that declares that
158:05 so-called header file that declares that function to exist now the reason for
158:07 function to exist now the reason for this historically is just efficiency
158:09 this historically is just efficiency back in the day when computers were
158:10 back in the day when computers were really slower and resource constrained
158:13 really slower and resource constrained you don't want to just give yourself
158:14 you don't want to just give yourself access to the entire kitchen sync of
158:16 access to the entire kitchen sync of functionality you only want to include
158:18 functionality you only want to include only the functions you actually care
158:20 only the functions you actually care about nowadays it's sort of a sort of a
158:24 about nowadays it's sort of a sort of a a copy paste step because you almost
158:25 a copy paste step because you almost always want to print something out on
158:27 always want to print something out on the screen at least when writing
158:28 the screen at least when writing programs like these but these so-called
158:30 programs like these but these so-called header files contain enough information
158:32 header files contain enough information about all of the functions in What's
158:34 about all of the functions in What's called the standard IO library and
158:37 called the standard IO library and standard IO just means standard input in
158:39 standard IO just means standard input in output and that's appropriate right
158:41 output and that's appropriate right because printing is pretty basic output
158:43 because printing is pretty basic output turns out there's other functions for
158:45 turns out there's other functions for getting input from the human's keyboard
158:46 getting input from the human's keyboard more on that in a bit but anytime you
158:48 more on that in a bit but anytime you want to print something on the screen
158:49 want to print something on the screen and see you indeed need to include this
158:52 and see you indeed need to include this header file at the top of your code and
158:54 header file at the top of your code and that's going to essentially inform the
158:56 that's going to essentially inform the compiler hey compiler I want to use
158:59 compiler hey compiler I want to use functionality from the standard IO
159:01 functionality from the standard IO Library including printf in this case
159:04 Library including printf in this case and if you omit the header file by
159:06 and if you omit the header file by accident it's just not going to work
159:08 accident it's just not going to work because it doesn't know what print f is
159:10 because it doesn't know what print f is it's sort of some unrecognized symbol in
159:13 it's sort of some unrecognized symbol in that case all right questions then about
159:16 that case all right questions then about this line of
159:18 this line of code this line of code here or what
159:21 code this line of code here or what these header files
159:23 these header files are all right you might wonder well how
159:25 are all right you might wonder well how do you know what functions exist how do
159:27 do you know what functions exist how do you know what files you might indeed
159:29 you know what files you might indeed want to include well it turns out that c
159:31 want to include well it turns out that c is a many-year old language and it has
159:33 is a many-year old language and it has ample documentation a caveat is that its
159:36 ample documentation a caveat is that its documentation isn't necessarily all that
159:38 documentation isn't necessarily all that userfriendly but what we have for the
159:40 userfriendly but what we have for the course is a simplified version of the
159:43 course is a simplified version of the official documentation for C at this URL
159:46 official documentation for C at this URL here manual. cs50. so in the world of c
159:49 here manual. cs50. so in the world of c and other languages too there are what
159:51 and other languages too there are what are called manual pages and these are
159:53 are called manual pages and these are just like text-based documentation that
159:55 just like text-based documentation that honestly is typically written in a voice
159:57 honestly is typically written in a voice that you kind of have to be an
159:58 that you kind of have to be an experienced programmer to understand
160:00 experienced programmer to understand some of it so what we've done it this
160:02 some of it so what we've done it this version of the same documentation is
160:04 version of the same documentation is we've imported all of the original
160:06 we've imported all of the original official documentation but we've added
160:08 official documentation but we've added sort of less comfortable translations in
160:10 sort of less comfortable translations in English for a lot of the functionality
160:12 English for a lot of the functionality that you might use in class just to help
160:13 that you might use in class just to help onboard you so at the end of the day you
160:15 onboard you so at the end of the day you don't need this documentation long term
160:17 don't need this documentation long term but just to get started we'll translate
160:19 but just to get started we'll translate it into terminology uh that you might
160:21 it into terminology uh that you might appreciate from a teaching assistant for
160:23 appreciate from a teaching assistant for instance as opposed to the original
160:25 instance as opposed to the original author of these documents and so for
160:27 author of these documents and so for instance if you were interested in
160:28 instance if you were interested in reading up on what functions exist in
160:31 reading up on what functions exist in the standard io. um header file well you
160:34 the standard io. um header file well you could go to a URL like this or you could
160:37 could go to a URL like this or you could search for it at manual. cs50. that
160:39 search for it at manual. cs50. that would show you a list of all of the
160:40 would show you a list of all of the available functions in that library and
160:43 available functions in that library and print F indeed would be one of them and
160:45 print F indeed would be one of them and then you could click further on that
160:46 then you could click further on that reaching a URL like this that's just
160:48 reaching a URL like this that's just going to give you all of the
160:49 going to give you all of the documentation for how to use print def
160:50 documentation for how to use print def it turns out you can do even more than
160:51 it turns out you can do even more than it uh than just printing out hello world
160:54 it uh than just printing out hello world and we'll scratch the surface of that
160:55 and we'll scratch the surface of that today but it turns out that the
160:57 today but it turns out that the documentation will always be your
160:59 documentation will always be your authoritative Source ultimately for
161:01 authoritative Source ultimately for questions like what can I do and how can
161:03 questions like what can I do and how can I do it meanwhile it turns out that cs50
161:07 I do it meanwhile it turns out that cs50 has its own library in A Accessible via
161:10 has its own library in A Accessible via header file called cs50.h it turns out
161:13 header file called cs50.h it turns out in C that output is actually pretty easy
161:16 in C that output is actually pretty easy relatively speaking once you get used to
161:17 relatively speaking once you get used to all the curly braces parentheses quote
161:19 all the curly braces parentheses quote marks and the like but input is a little
161:22 marks and the like but input is a little more difficult and if you have
161:23 more difficult and if you have programmed before input's not that hard
161:26 programmed before input's not that hard to do in Python it's not that hard to do
161:28 to do in Python it's not that hard to do in Java it's more difficult to do in C
161:31 in Java it's more difficult to do in C and we'll see why in a couple of weeks
161:32 and we'll see why in a couple of weeks but for the first couple of weeks of the
161:34 but for the first couple of weeks of the class we actually provide you with some
161:36 class we actually provide you with some training wheels of sorts whereby we have
161:39 training wheels of sorts whereby we have a number of functions that are declared
161:41 a number of functions that are declared in this file cs50.h it lives its
161:43 in this file cs50.h it lives its documentation at a URL like this this
161:45 documentation at a URL like this this and in a moment we'll use a few of these
161:47 and in a moment we'll use a few of these you'll see that cs50 provides you with
161:49 you'll see that cs50 provides you with some functions like get Char forget a
161:52 some functions like get Char forget a single character from the user's
161:54 single character from the user's keyboard uh get int to get an integer
161:56 keyboard uh get int to get an integer from the user's keyboard uh get string
161:59 from the user's keyboard uh get string to get a sequence of text from the
162:00 to get a sequence of text from the user's keyboard and a bunch of others as
162:02 user's keyboard and a bunch of others as well so let's actually use some of these
162:05 well so let's actually use some of these functions how about by revisiting really
162:08 functions how about by revisiting really the second program we wrote in scratch
162:09 the second program we wrote in scratch last time which adds some input to the
162:12 last time which adds some input to the output so first version of scratch was
162:14 output so first version of scratch was just hello world said the same thing
162:16 just hello world said the same thing every time you click the green flag
162:17 every time you click the green flag version two recall though did this it
162:20 version two recall though did this it asked the user what's your name and then
162:22 asked the user what's your name and then that somehow gave it back a a return
162:25 that somehow gave it back a a return variable return value we called it and
162:27 variable return value we called it and we then joined hello in that name to say
162:29 we then joined hello in that name to say something a little more interesting on
162:31 something a little more interesting on the screen so what did that model look
162:34 the screen so what did that model look like same thing as before we've got a
162:36 like same thing as before we've got a function in the Middle where function is
162:38 function in the Middle where function is like the code implementation of our
162:40 like the code implementation of our algorithm that takes in one or more
162:42 algorithm that takes in one or more arguments like what is it you want to uh
162:44 arguments like what is it you want to uh say on the the screen ultimately and
162:47 say on the the screen ultimately and return value in this case is going to be
162:48 return value in this case is going to be actually a value that comes back so in
162:50 actually a value that comes back so in the case of getting input we can
162:52 the case of getting input we can consider this ask block again like last
162:54 consider this ask block again like last week the input to it is whatever words
162:57 week the input to it is whatever words of English you want to ask the user and
162:59 of English you want to ask the user and then it returns a value and this was
163:01 then it returns a value and this was called by default in mit's World answer
163:04 called by default in mit's World answer that we'll see and see you can call
163:05 that we'll see and see you can call these return values anything you want
163:07 these return values anything you want ultimately in variables but this is
163:09 ultimately in variables but this is different from a side effect a side
163:10 different from a side effect a side effect is just something visual often
163:12 effect is just something visual often that happens on the screen like the
163:13 that happens on the screen like the speech bubble or hello world a return
163:16 speech bubble or hello world a return value is actually a value you get back
163:18 value is actually a value you get back from a function that you can use or
163:19 from a function that you can use or reuse so how do we convert this scratch
163:22 reuse so how do we convert this scratch block from last week to C code this week
163:25 block from last week to C code this week well if you want to ask the user for
163:26 well if you want to ask the user for something like their name you can do
163:28 something like their name you can do this you use a cs50 function called get
163:31 this you use a cs50 function called get string and you use the parentheses to
163:34 string and you use the parentheses to represent here comes the inputs there
163:36 represent here comes the inputs there too you can then put the sentence you
163:38 too you can then put the sentence you want to ask the user quote unquote
163:40 want to ask the user quote unquote what's your name but you do indeed need
163:42 what's your name but you do indeed need the quotes literally in C so I'll go
163:44 the quotes literally in C so I'll go ahead and add those as well well subtle
163:46 ahead and add those as well well subtle but I've deliberately included a space
163:49 but I've deliberately included a space after the question mark but before the
163:51 after the question mark but before the double quote just so that the cursor
163:53 double quote just so that the cursor moves one step over because in this case
163:55 moves one step over because in this case we're not going to get a special speech
163:57 we're not going to get a special speech box like we did in scratch it's just
163:58 box like we did in scratch it's just going to leave the cursor where it is so
164:00 going to leave the cursor where it is so we'll see that aesthetically this just
164:01 we'll see that aesthetically this just moves the blinking cursor one space
164:04 moves the blinking cursor one space after the sentence on the screen all
164:06 after the sentence on the screen all right but the catch is with scratch we
164:08 right but the catch is with scratch we just automatically got back the answer
164:11 just automatically got back the answer from the user in a special variable
164:12 from the user in a special variable called answer in C you're going to have
164:14 called answer in C you're going to have to be a little more specific in C if you
164:18 to be a little more specific in C if you want to get back a return value from a
164:20 want to get back a return value from a function like get string you have to use
164:23 function like get string you have to use an equal sign and then the name of a
164:25 an equal sign and then the name of a variable on the left the choice of
164:27 variable on the left the choice of variables is up to you I could have
164:29 variables is up to you I could have called this anything X YZ I'm going to
164:31 called this anything X YZ I'm going to more descriptively call it answer for
164:33 more descriptively call it answer for parody with what MIT did with scratch
164:35 parody with what MIT did with scratch but notice that this doesn't represent
164:37 but notice that this doesn't represent equality per se this is assignment in
164:41 equality per se this is assignment in this case so in C when you use a single
164:43 this case so in C when you use a single equal sign that means means copy the
164:45 equal sign that means means copy the value on the right over to the value on
164:48 value on the right over to the value on the left from right to left so what does
164:50 the left from right to left so what does this do for us well if get string is a
164:53 this do for us well if get string is a function that prompts the user with
164:54 function that prompts the user with quote unquote what's your name and it
164:56 quote unquote what's your name and it has I claim a return value that means it
164:59 has I claim a return value that means it kind of hands me back some value but
165:01 kind of hands me back some value but it's up to me in C to do something with
165:03 it's up to me in C to do something with that value so if I want to copy that
165:05 that value so if I want to copy that value into a variable that I can use and
165:07 value into a variable that I can use and reuse I use an equal sign and I invent
165:10 reuse I use an equal sign and I invent on the left hand side of that equal sign
165:12 on the left hand side of that equal sign any variable name I want there's certain
165:14 any variable name I want there's certain rules certain conventions but generally
165:16 rules certain conventions but generally if you use a single word with all
165:18 if you use a single word with all lowercase you're in good shape but C is
165:21 lowercase you're in good shape but C is a little more pedantic than that and
165:23 a little more pedantic than that and those of you who have have programmed
165:25 those of you who have have programmed before might not be used to this for
165:26 before might not be used to this for instance in Python which is a world
165:28 instance in Python which is a world we'll get to in a few weeks you also
165:30 we'll get to in a few weeks you also have to tell see what type of value
165:34 have to tell see what type of value you're storing so if I do want a string
165:37 you're storing so if I do want a string of text from the user so not an integer
165:40 of text from the user so not an integer not a single character I want a whole
165:42 not a single character I want a whole string of text like a phrase a sentence
165:44 string of text like a phrase a sentence a name in this this case I have to tell
165:46 a name in this this case I have to tell C that this variable is of type string
165:50 C that this variable is of type string so it's a little wordy but you get used
165:52 so it's a little wordy but you get used to it and you just have to be precise
165:54 to it and you just have to be precise you're informing the computer what type
165:56 you're informing the computer what type of value is going in this
165:58 of value is going in this variable all right it's so close to
166:00 variable all right it's so close to being correct but I have omitted
166:03 being correct but I have omitted something that's annoyingly important
166:04 something that's annoyingly important still what's missing still yeah so
166:08 still what's missing still yeah so semicolon this is a statement this is
166:11 semicolon this is a statement this is like a a a full thought if you will in
166:13 like a a a full thought if you will in code I do need to end It ultimately with
166:16 code I do need to end It ultimately with the semicolon at the end there all right
166:18 the semicolon at the end there all right so this was more of a mouthful but let's
166:20 so this was more of a mouthful but let's try using this in now my code let me go
166:22 try using this in now my code let me go back to vs code where I have version
166:25 back to vs code where I have version zero of my code here let me go ahead and
166:27 zero of my code here let me go ahead and include one other file at the top of
166:30 include one other file at the top of hello.c namely include cs50.h so that I
166:34 hello.c namely include cs50.h so that I have access now to get string and
166:36 have access now to get string and anything else I might want now let me go
166:39 anything else I might want now let me go ahead and add a line of cod here inside
166:42 ahead and add a line of cod here inside of these curly braces and let me go
166:43 of these curly braces and let me go ahead and do this string answer equals
166:47 ahead and do this string answer equals get string quote unquote what's your
166:50 get string quote unquote what's your name question mark I'm going to add an
166:52 name question mark I'm going to add an extra space before the double quote I'm
166:53 extra space before the double quote I'm going to indeed end my thought with a
166:56 going to indeed end my thought with a semicolon and now let me deliberately
166:58 semicolon and now let me deliberately make a mistake just to make a point here
167:00 make a mistake just to make a point here let me now try changing hello world to
167:03 let me now try changing hello world to hello comma answer all right now perhaps
167:07 hello comma answer all right now perhaps even though this is some new lines of
167:08 even though this is some new lines of code you can see where I've aired
167:10 code you can see where I've aired already but let me try making this
167:12 already but let me try making this program now so far so good so no error
167:15 program now so far so good so no error messages so that's a good thing let me
167:16 messages so that's a good thing let me go ahead and run /hello and you'll see
167:19 go ahead and run /hello and you'll see the prompt what's your name question
167:21 the prompt what's your name question mark and notice the cursor indeed one
167:23 mark and notice the cursor indeed one space to the right just because I
167:25 space to the right just because I thought it would look prettier to put a
167:26 thought it would look prettier to put a little Blank Space there as opposed to
167:28 little Blank Space there as opposed to leaving it right after the question mark
167:30 leaving it right after the question mark let me type my name but even if you've
167:32 let me type my name but even if you've never programmed before I have screwed
167:34 never programmed before I have screwed up here what are we going to see on the
167:35 up here what are we going to see on the screen when I hit
167:38 screen when I hit enter yeah hello answer most likely why
167:41 enter yeah hello answer most likely why cuz the computer is going to take me
167:42 cuz the computer is going to take me literally and if I say quote unquote
167:44 literally and if I say quote unquote hello answer that is the string of text
167:47 hello answer that is the string of text followed by a new line that's going to
167:48 followed by a new line that's going to be outputed to the screen so we need
167:50 be outputed to the screen so we need some way of actually plugging answer
167:52 some way of actually plugging answer into this line of code it's not quite as
167:55 into this line of code it's not quite as simple as scratch where you could just
167:57 simple as scratch where you could just grab like a second say block and drag
167:59 grab like a second say block and drag and drop the variable there we actually
168:01 and drop the variable there we actually need a new syntax and it's going to look
168:02 need a new syntax and it's going to look weird at first but it is everywhere in
168:05 weird at first but it is everywhere in software nowadays especially in the
168:07 software nowadays especially in the world of c and certain other languages
168:09 world of c and certain other languages so let me go ahead and propose that I
168:12 so let me go ahead and propose that I solve it as follows well back when we
168:15 solve it as follows well back when we did this in scratch remember that the
168:16 did this in scratch remember that the most elegant solution was this here we
168:19 most elegant solution was this here we used the say block still which is going
168:21 used the say block still which is going to be analogous to print F today but I
168:23 to be analogous to print F today but I use the join puzzle piece in scratch to
168:25 use the join puzzle piece in scratch to combine hello comma space and then the
168:28 combine hello comma space and then the name of the human so how do we translate
168:30 name of the human so how do we translate this code to C well it's going to look a
168:33 this code to C well it's going to look a little different now I'm going to start
168:35 little different now I'm going to start with print f with some parth C's and a
168:37 with print f with some parth C's and a semicolon representing the say block but
168:40 semicolon representing the say block but how do I now do this joining this is
168:42 how do I now do this joining this is where the puzzle pieces don't quite
168:43 where the puzzle pieces don't quite translate perfectly this would be the
168:45 translate perfectly this would be the way to do this you put hello comma and
168:48 way to do this you put hello comma and then a placeholder so this is what's
168:50 then a placeholder so this is what's known as a format code in C specifically
168:53 known as a format code in C specifically for printf and it just means this is a
168:56 for printf and it just means this is a placeholder for a string again a string
168:58 placeholder for a string again a string is just text so this means hey computer
169:01 is just text so this means hey computer print out literally hello comma space
169:03 print out literally hello comma space and then not literally percent s percent
169:05 and then not literally percent s percent s is uh treated specially to mean plug
169:08 s is uh treated specially to mean plug in some value here all right so what
169:11 in some value here all right so what else do I still need well this is still
169:12 else do I still need well this is still some text so I'm still going going to
169:15 some text so I'm still going going to surround the whole thing with double
169:18 surround the whole thing with double quotes I'm still going to include my
169:20 quotes I'm still going to include my back sln just to keep things tidy and
169:22 back sln just to keep things tidy and move the cursor to the next line so the
169:25 move the cursor to the next line so the last step here in C is to somehow join
169:28 last step here in C is to somehow join the answer with that word hello and the
169:32 the answer with that word hello and the way you do this is with printf passing
169:35 way you do this is with printf passing it not one argument which is what I keep
169:37 it not one argument which is what I keep doing I keep passing it one string of
169:39 doing I keep passing it one string of text quote unquote I'm going to now add
169:41 text quote unquote I'm going to now add a comma and then the name of the value
169:44 a comma and then the name of the value that I want want printf to go back and
169:46 that I want want printf to go back and plug into that percent s and printf is
169:50 plug into that percent s and printf is just smart about this if you have 1% s
169:52 just smart about this if you have 1% s and one additional argument after a
169:54 and one additional argument after a comma it just does from right to left it
169:56 comma it just does from right to left it plugs it in if you have two percent S's
169:59 plugs it in if you have two percent S's and two variables after the comma that's
170:02 and two variables after the comma that's okay too if you separate them with
170:03 okay too if you separate them with commas it'll plug the first into the
170:05 commas it'll plug the first into the first percent s and the second variable
170:07 first percent s and the second variable into the second percent s so it's just
170:09 into the second percent s so it's just left to right order of operations it's
170:11 left to right order of operations it's not as pretty or as uh simple as this
170:14 not as pretty or as uh simple as this but this is how it's done in C all right
170:17 but this is how it's done in C all right let me pause because this is a lot of
170:18 let me pause because this is a lot of symbology any questions on this
170:21 symbology any questions on this technique here
170:22 technique here [Music]
170:26 [Music] yeah yeah really good question why did I
170:28 yeah yeah really good question why did I exclude the backslash n a moment ago
170:32 exclude the backslash n a moment ago really just my sense of Aesthetics if
170:34 really just my sense of Aesthetics if you will No Good Reason beyond that so
170:36 you will No Good Reason beyond that so if I look back at my code you quite
170:38 if I look back at my code you quite rightly noticed that I didn't have a
170:39 rightly noticed that I didn't have a backslash in there that's just because
170:42 backslash in there that's just because for whatever sense of style that I have
170:44 for whatever sense of style that I have I wanted the name to be typed right
170:46 I wanted the name to be typed right after the question I totally could have
170:48 after the question I totally could have added a back sln there instead of a
170:50 added a back sln there instead of a space that would have just allowed me to
170:52 space that would have just allowed me to type down here totally fine just wanted
170:54 type down here totally fine just wanted to show you something different good
170:56 to show you something different good good catch
170:57 good catch yeah can I show an example with 2% s's
171:01 yeah can I show an example with 2% s's surely so let me uh in vs code do this
171:04 surely so let me uh in vs code do this let me clear my terminal window to clean
171:05 let me clear my terminal window to clean things up and let me do this instead of
171:08 things up and let me do this instead of calling the variable answer all over the
171:09 calling the variable answer all over the place let me call it uh first and I'll
171:13 place let me call it uh first and I'll ask two questions what's your first name
171:15 ask two questions what's your first name and now let me do string last equals get
171:18 and now let me do string last equals get string quot whoops capitalization
171:20 string quot whoops capitalization matters so let me fix my capital S there
171:22 matters so let me fix my capital S there quote unquote what's your last name
171:25 quote unquote what's your last name question mark semicolon and now we'll
171:29 question mark semicolon and now we'll plug in one percent s and a second
171:32 plug in one percent s and a second percent s and now I'm going to plug in
171:35 percent s and now I'm going to plug in first first and last last coincidentally
171:39 first first and last last coincidentally and now I'm going to go back to the
171:40 and now I'm going to go back to the terminal window make hello crossing my
171:44 terminal window make hello crossing my fingers
171:45 fingers all good/ hello here's my first question
171:48 all good/ hello here's my first question David here's my second question me and
171:51 David here's my second question me and again hello David Ma so it just inserts
171:54 again hello David Ma so it just inserts them left to right all I was doing for
171:55 them left to right all I was doing for parody with scratch though and let me go
171:57 parody with scratch though and let me go ahead and undo this again I'll go back
171:59 ahead and undo this again I'll go back to answer like this I'll go back to just
172:03 to answer like this I'll go back to just asking for the person's name I'm going
172:04 asking for the person's name I'm going to delete mention of last I'm going to
172:06 to delete mention of last I'm going to delete mention of the second perc s and
172:08 delete mention of the second perc s and now if I recompile this simpler
172:10 now if I recompile this simpler version I did screw up didn't intend it
172:14 version I did screw up didn't intend it what did I do
172:20 wrong yeah so just newbie mistakes so I changed my variable back to answer just
172:22 changed my variable back to answer just to be consistent with week zero but I
172:24 to be consistent with week zero but I didn't change it here so I have an use
172:27 didn't change it here so I have an use of Undeclared identifier first it's
172:29 of Undeclared identifier first it's Undeclared in the sense that I declared
172:31 Undeclared in the sense that I declared answer align prior I didn't declare
172:34 answer align prior I didn't declare first so indeed intuitively I want to
172:36 first so indeed intuitively I want to just change that to that let me now do
172:38 just change that to that let me now do make hello again /hello type in just my
172:42 make hello again /hello type in just my first name this time and there it is
172:44 first name this time and there it is hello David questions on this then
172:47 hello David questions on this then syntax with print F
172:50 syntax with print F yeah uh the placeholder I'll zoom in is
172:53 yeah uh the placeholder I'll zoom in is just a single percent and then an S so
172:56 just a single percent and then an S so inside of my string here is percent s
173:01 inside of my string here is percent s and then I have a comma outside the
173:03 and then I have a comma outside the quotes and then the name of the variable
173:05 quotes and then the name of the variable whose value I want to plug in for that
173:09 whose value I want to plug in for that percent s and now notice there's
173:11 percent s and now notice there's technically two commas in side of these
173:15 technically two commas in side of these parentheses on line seven and yet I
173:17 parentheses on line seven and yet I claim that printf at the moment is only
173:20 claim that printf at the moment is only taking in two
173:23 taking in two arguments why is there then two commas
173:27 arguments why is there then two commas but only two arguments if there were two
173:29 but only two arguments if there were two commas you would think there would be
173:30 commas you would think there would be three arguments
173:38 right exactly the comma in between the quotes is just an English thing it's
173:39 quotes is just an English thing it's separating the hello from the name so
173:42 separating the hello from the name so that's why indeed it's not only in
173:43 that's why indeed it's not only in quotes that that's also why programs
173:45 quotes that that's also why programs like VSS code tend to syntax highlight
173:48 like VSS code tend to syntax highlight it a little differently just so that it
173:49 it a little differently just so that it sort of jumps out as different to you
173:52 sort of jumps out as different to you even though in this case it's a little
173:53 even though in this case it's a little subtle a light blue versus white but
173:55 subtle a light blue versus white but indeed it's trying its best other
173:57 indeed it's trying its best other questions now on this placeholder
174:10 yeah ah good question if I wanted to add an exclamation point after the name
174:12 an exclamation point after the name would I have to add another placeholder
174:14 would I have to add another placeholder and so forth I could actually do that
174:15 and so forth I could actually do that much more simply I can just put the
174:17 much more simply I can just put the exclamation point right after the
174:19 exclamation point right after the percent sign I don't need an additional
174:21 percent sign I don't need an additional placeholder per se if I zoom out now and
174:24 placeholder per se if I zoom out now and run make hello again/ hello and type in
174:27 run make hello again/ hello and type in just my name no exclamation point now
174:29 just my name no exclamation point now you'll see more excitedly hello comma
174:31 you'll see more excitedly hello comma David So print f is smart it will figure
174:33 David So print f is smart it will figure out where the percent s is and then go
174:37 out where the percent s is and then go and replace it now let me propose that a
174:40 and replace it now let me propose that a common thing in programming is that as
174:41 common thing in programming is that as soon as we make a decision as to how to
174:43 soon as we make a decision as to how to design something we often paint
174:44 design something we often paint ourselves into a corner and sort of
174:46 ourselves into a corner and sort of regret a decision can anyone think of a
174:50 regret a decision can anyone think of a problem that arises from using percent S
174:53 problem that arises from using percent S as a
174:54 as a placeholder in this string to print F
174:58 placeholder in this string to print F what could go wrong if we're using
174:59 what could go wrong if we're using percent in this special
175:05 way yeah if you literally want to say for whatever weird reason percent s on
175:07 for whatever weird reason percent s on the screen or honestly even just a
175:09 the screen or honestly even just a single percent it turns out that a
175:10 single percent it turns out that a percent sign is treated specially inside
175:14 percent sign is treated specially inside of of print F strings so what's the
175:17 of of print F strings so what's the solution here there's different uh
175:19 solution here there's different uh patterns of solutions to problems like
175:21 patterns of solutions to problems like these but suppose you wanted to say uh I
175:25 these but suppose you wanted to say uh I got 100% for instance let me let me go
175:28 got 100% for instance let me let me go ahead and change this completely so I
175:30 ahead and change this completely so I got 100% on your test or whatever all
175:34 got 100% on your test or whatever all right let me go ahead and run make hello
175:36 right let me go ahead and run make hello enter all right so invalid conversion
175:39 enter all right so invalid conversion specifier I mean I have no idea what
175:40 specifier I mean I have no idea what this means but it's underlining the
175:42 this means but it's underlining the percent sign is problematic well it
175:44 percent sign is problematic well it turns out that humans years ago decided
175:46 turns out that humans years ago decided all right damn it we already Ed percent
175:48 all right damn it we already Ed percent well 2% signs will mean 1% literally so
175:52 well 2% signs will mean 1% literally so now if I rerun make hello aha hello I
175:56 now if I rerun make hello aha hello I got 100% so there's going to be things
175:58 got 100% so there's going to be things like that honestly that you have to ask
175:59 like that honestly that you have to ask someone you have to Google you have to
176:00 someone you have to Google you have to look it up in the documentation but
176:01 look it up in the documentation but there's always a solution to those kinds
176:03 there's always a solution to those kinds of problems and thankfully they don't
176:05 of problems and thankfully they don't come up all that often yeah oh just
176:08 come up all that often yeah oh just poting other questions
176:10 poting other questions [Music]
176:13 [Music] yeah if you have multiple variables it
176:16 yeah if you have multiple variables it is in the Left Right order so print F
176:18 is in the Left Right order so print F will analyze the first string of text
176:20 will analyze the first string of text that you pass in between quotes and
176:22 that you pass in between quotes and whatever the first percent is the next
176:24 whatever the first percent is the next the first variable that's passed in
176:26 the first variable that's passed in after a comma gets plugged in there and
176:28 after a comma gets plugged in there and then the second gets plugged into the
176:29 then the second gets plugged into the second third and to the third and so
176:31 second third and to the third and so forth so it's just based on left to
176:33 forth so it's just based on left to right
176:41 yeah it's just a placeholder it's called a format code and it just means
176:43 a format code and it just means colloquially plug in some value here and
176:45 colloquially plug in some value here and printf the like the humans who wrote
176:47 printf the like the humans who wrote printf decades ago decided to treat
176:50 printf decades ago decided to treat percent s special why just because they
176:52 percent s special why just because they needed some placeholder they decided
176:54 needed some placeholder they decided that eh no one's ever going to really
176:55 that eh no one's ever going to really want to type percent s and if they do
176:57 want to type percent s and if they do they can just do percent percent s so
176:59 they can just do percent percent s so they decided to implement printf in such
177:01 they decided to implement printf in such a way that they have code that look
177:03 a way that they have code that look analyzes whatever text comes in looks
177:05 analyzes whatever text comes in looks for percent s and then somehow plugs in
177:08 for percent s and then somehow plugs in the subsequent values into that
177:10 the subsequent values into that placeholder and just this H question
177:14 placeholder and just this H question [Music]
177:17 [Music] sorry ah so what if you wanted to do
177:19 sorry ah so what if you wanted to do single characters like initials like DM
177:21 single characters like initials like DM or djm for first middle last absolutely
177:24 or djm for first middle last absolutely and that too is a perfect segue from the
177:26 and that too is a perfect segue from the two of you to what in general are going
177:28 two of you to what in general are going to be called data types in C so it turns
177:31 to be called data types in C so it turns out in C there's not only strings as
177:34 out in C there's not only strings as text and we'll see in more detail in
177:36 text and we'll see in more detail in over the next couple of weeks what a
177:37 over the next couple of weeks what a string really is underneath the hood but
177:39 string really is underneath the hood but strings of text are not the only thing
177:41 strings of text are not the only thing that programs can output they can indeed
177:42 that programs can output they can indeed output single characters as for initials
177:45 output single characters as for initials they can output integers as well turns
177:47 they can output integers as well turns out that printf has different format
177:49 out that printf has different format codes for all sorts of different data
177:52 codes for all sorts of different data types and just some of the data types
177:53 types and just some of the data types we'll see in the coming weeks will be
177:55 we'll see in the coming weeks will be this list here which you'll notice it
177:57 this list here which you'll notice it almost perfectly lines up with the cs50
177:59 almost perfectly lines up with the cs50 functions that I rattled off earlier
178:01 functions that I rattled off earlier like get Char get int get string the
178:04 like get Char get int get string the reason we called those functions that is
178:06 reason we called those functions that is because each of them is designed to
178:07 because each of them is designed to return to you a different type of value
178:10 return to you a different type of value we've used get string already in this
178:11 we've used get string already in this example here we'll soon see get in and
178:14 example here we'll soon see get in and we'll see opportunities to use others
178:16 we'll see opportunities to use others but these indeed are the menu of
178:19 but these indeed are the menu of available data types plus others dot dot
178:21 available data types plus others dot dot dot that you can use when writing a
178:23 dot that you can use when writing a program in C the onus therefore is on
178:26 program in C the onus therefore is on you to decide in advance do I want to
178:28 you to decide in advance do I want to store an INT in this variable or a
178:30 store an INT in this variable or a string or heck when writing fancier code
178:32 string or heck when writing fancier code an image or a sound or a video even
178:35 an image or a sound or a video even those can all be different data types
178:36 those can all be different data types dot dot dot but for now we'll focus
178:38 dot dot dot but for now we'll focus really on just these Primitives that was
178:41 really on just these Primitives that was a lot let's go ahead and take a 5minute
178:43 a lot let's go ahead and take a 5minute break here no cookies yet but in 5
178:45 break here no cookies yet but in 5 minutes we'll come back dive into more
178:46 minutes we'll come back dive into more detail and our second break today we'll
178:47 detail and our second break today we'll have
178:54 cookies all right we are back and so if you have been playing along at home but
178:55 you have been playing along at home but hitting some bumps in the road that's
178:57 hitting some bumps in the road that's totally normal and indeed the goals of
178:58 totally normal and indeed the goals of lecture generally will be to give you a
179:01 lecture generally will be to give you a sense uh conceptually of where we'll be
179:03 sense uh conceptually of where we'll be going during the course of the week but
179:04 going during the course of the week but it's indeed through the Hands-On labs
179:06 it's indeed through the Hands-On labs and problem sets that you'll really have
179:07 and problem sets that you'll really have an opportunity at your own pace to work
179:09 an opportunity at your own pace to work through uh some of those same bumps in
179:11 through uh some of those same bumps in the road but for today let me give you a
179:14 the road but for today let me give you a few more building blocks and these two
179:15 few more building blocks and these two will translate from scratch initially
179:18 will translate from scratch initially namely like conditionals like how now
179:19 namely like conditionals like how now and see after knowing now how we can use
179:22 and see after knowing now how we can use functions at least uh get string and
179:25 functions at least uh get string and print F and we can use variables like
179:27 print F and we can use variables like the string I created earlier how can I
179:30 the string I created earlier how can I now add to the mix things like
179:31 now add to the mix things like decisionmaking and conditionals at that
179:33 decisionmaking and conditionals at that well with conditionals in scratch we had
179:35 well with conditionals in scratch we had this kind of syntax on the left here in
179:37 this kind of syntax on the left here in scratch is how you might Express if two
179:40 scratch is how you might Express if two variables X and Y have this relationship
179:43 variables X and Y have this relationship if x is less than y then say on the
179:45 if x is less than y then say on the screen X is less than y well let me
179:47 screen X is less than y well let me translate that to the right now in C
179:49 translate that to the right now in C code so in C the corresponding code is
179:52 code so in C the corresponding code is going to look like this assuming X and Y
179:54 going to look like this assuming X and Y already exist more on that later and
179:56 already exist more on that later and notice a pattern we're going to see
179:58 notice a pattern we're going to see again and again there is going to be
180:01 again and again there is going to be parentheses around the X and less than y
180:04 parentheses around the X and less than y so parentheses around the Boolean
180:06 so parentheses around the Boolean expression recall the Boolean expression
180:07 expression recall the Boolean expression is the true false the yes no answer that
180:10 is the true false the yes no answer that a question that you're trying to ask in
180:11 a question that you're trying to ask in order to decide whether or not to do
180:13 order to decide whether or not to do something so you use parentheses there
180:15 something so you use parentheses there so similar in functions where we Ed
180:19 so similar in functions where we Ed parentheses for printf and parentheses
180:21 parentheses for printf and parentheses for get string and this is just a weird
180:23 for get string and this is just a weird inconsistency stylistically when using
180:25 inconsistency stylistically when using the keyword if you should as a matter of
180:28 the keyword if you should as a matter of better best practice put a space after
180:30 better best practice put a space after the word if when using a function like
180:32 the word if when using a function like print F or get string you shouldn't both
180:36 print F or get string you shouldn't both will work but you'll find that these are
180:37 will work but you'll find that these are conventions stylistically that most
180:39 conventions stylistically that most people adhere to so space when using an
180:41 people adhere to so space when using an if here all right now inside of the
180:44 if here all right now inside of the curly braces is where the actual code
180:46 curly braces is where the actual code goes that you want to execute
180:48 goes that you want to execute conditionally so if you want to print
180:50 conditionally so if you want to print out X is less than y only if x is
180:53 out X is less than y only if x is actually less than y in C you use this
180:55 actually less than y in C you use this open curly brace which up until now
180:57 open curly brace which up until now you've probably rarely used on your
180:58 you've probably rarely used on your keyboard and the Clos curly brace down
181:00 keyboard and the Clos curly brace down here and those are kind of hugging if
181:02 here and those are kind of hugging if you will the one or more lines of code
181:05 you will the one or more lines of code underneath the if very similar in spirit
181:07 underneath the if very similar in spirit to how the orange block here kind of
181:09 to how the orange block here kind of hugs the purple puzzle piece here so
181:11 hugs the purple puzzle piece here so there's no Graphics in C it's all text
181:13 there's no Graphics in C it's all text so you can think of those curly braces
181:15 so you can think of those curly braces as really representing the same idea as
181:18 as really representing the same idea as a side note if you only have one line of
181:21 a side note if you only have one line of code inside of the if condition if you
181:24 code inside of the if condition if you will you strictly speaking don't need
181:26 will you strictly speaking don't need the curly braces but as a matter of good
181:28 the curly braces but as a matter of good style do include them it will make more
181:30 style do include them it will make more obvious what your intent is how about in
181:33 obvious what your intent is how about in scratch if you wanted to express this
181:35 scratch if you wanted to express this two ways in the road that you might go
181:37 two ways in the road that you might go left or right so to speak well if x is
181:40 left or right so to speak well if x is less than y I want to say x is less than
181:42 less than y I want to say x is less than y else I want want to say the opposite X
181:45 y else I want want to say the opposite X is not less than y in this case so I'm
181:48 is not less than y in this case so I'm making a decision based on that Boolean
181:49 making a decision based on that Boolean expression in C it's almost the same but
181:52 expression in C it's almost the same but you're adding to the mix the keyword Els
181:54 you're adding to the mix the keyword Els so MIT borrowed for scratch the same
181:57 so MIT borrowed for scratch the same keyword there and a second pair of curly
181:59 keyword there and a second pair of curly braces open and close respectively and
182:01 braces open and close respectively and you might guess now what goes inside of
182:03 you might guess now what goes inside of those well you print out X's less than y
182:06 those well you print out X's less than y or you print out X's not less than y all
182:09 or you print out X's not less than y all right what if there's a three-way fork
182:11 right what if there's a three-way fork in the road in scratch this actually
182:12 in the road in scratch this actually gets a little unwieldy graphically if
182:15 gets a little unwieldy graphically if you will but notice that in scratch this
182:17 you will but notice that in scratch this is how we could express if x is less
182:20 is how we could express if x is less than y say x is less than y else if x is
182:24 than y say x is less than y else if x is greater than y say x is greater than y
182:27 greater than y say x is greater than y else if x equals y then say x is equal
182:31 else if x equals y then say x is equal to Y now minor inconsistency here just a
182:35 to Y now minor inconsistency here just a little bit ago I claimed in C that a
182:37 little bit ago I claimed in C that a equal sign represents what
182:41 equal sign represents what operation assignment from right to left
182:43 operation assignment from right to left left in so far as scratch is really
182:45 left in so far as scratch is really meant for kids and they didn't really
182:46 meant for kids and they didn't really want to get into the weeds of this kind
182:48 want to get into the weeds of this kind of uh semantic equal sign in scratch
182:51 of uh semantic equal sign in scratch means equality however we're going to
182:53 means equality however we're going to need to fix this in C in just a moment
182:55 need to fix this in C in just a moment in c equal sign means assignment right
182:57 in c equal sign means assignment right to left in scratch it literally means
182:59 to left in scratch it literally means what you would expect all right let's
183:01 what you would expect all right let's translate this code then to C on the
183:03 translate this code then to C on the right this code would correspond really
183:06 right this code would correspond really to this and you can perhaps see somewhat
183:09 to this and you can perhaps see somewhat goofy what the solution was not unlike
183:11 goofy what the solution was not unlike the percent percent solution earlier
183:13 the percent percent solution earlier when hum painted themselves into one
183:14 when hum painted themselves into one other corner you say if you say else if
183:17 other corner you say if you say else if and you say else if and how did we
183:20 and you say else if and how did we resolve the use of a single equal sign
183:22 resolve the use of a single equal sign already in C when you want to repr when
183:24 already in C when you want to repr when you want to express equality is the
183:26 you want to express equality is the thing on the left equal to the thing on
183:28 thing on the left equal to the thing on the right you literally use two equal
183:30 the right you literally use two equal signs right next to each other no space
183:32 signs right next to each other no space in between them but now this code would
183:35 in between them but now this code would be correct on both the left and the
183:37 be correct on both the left and the right whether you're doing this in
183:38 right whether you're doing this in scratch or C respectively but now we can
183:41 scratch or C respectively but now we can kind of nitpick our code specifically
183:43 kind of nitpick our code specifically the the design thereof logically can
183:46 the the design thereof logically can anyone critique the design of this code
183:49 anyone critique the design of this code either in scratch or C like I feel like
183:51 either in scratch or C like I feel like we could do better how about in
184:02 back perfect logically it's got to be the case that X is less than y or X is
184:04 the case that X is less than y or X is greater than y or by conclusion it's got
184:08 greater than y or by conclusion it's got to be equal to Y so why are you wasting
184:10 to be equal to Y so why are you wasting my time or the computer's time asking a
184:12 my time or the computer's time asking a third question you don't need to ask
184:13 third question you don't need to ask this final else if because logically as
184:16 this final else if because logically as you knowe it should go without saying so
184:18 you knowe it should go without saying so it's a minor tweak like you're doing
184:20 it's a minor tweak like you're doing extra work potentially in cases where x
184:22 extra work potentially in cases where x equals y so we can just refine that and
184:25 equals y so we can just refine that and just like in scratch you could just use
184:26 just like in scratch you could just use an else block similarly in C could we
184:29 an else block similarly in C could we simplify this code to just an else a
184:32 simplify this code to just an else a sort of catchall logically that just
184:34 sort of catchall logically that just handles the reality that of course
184:35 handles the reality that of course that's going to be the final situation
184:38 that's going to be the final situation instead all right so we have this
184:40 instead all right so we have this ability now to express conditionals with
184:42 ability now to express conditionals with Boolean expressions let's actually do
184:44 Boolean expressions let's actually do something with this next here so let me
184:45 something with this next here so let me go back to vs code I've closed hello.c
184:48 go back to vs code I've closed hello.c and I want to create a second file for
184:50 and I want to create a second file for the sake of some demos now recall that
184:52 the sake of some demos now recall that you can create new files by typing code
184:54 you can create new files by typing code space and then the name of the file you
184:56 space and then the name of the file you want to create uh for instance I might
184:59 want to create uh for instance I might do compare. c I want to write a program
185:01 do compare. c I want to write a program that's going to start comparing some
185:02 that's going to start comparing some values for demonstration sake but before
185:05 values for demonstration sake but before I do that let me just show you by
185:06 I do that let me just show you by opening the file explorer at right this
185:09 opening the file explorer at right this is similar in spirit to a Mac or PC like
185:11 is similar in spirit to a Mac or PC like you can go up here and click on an icon
185:13 you can go up here and click on an icon and you can click on the plus icon and
185:15 and you can click on the plus icon and you'll get a blue box and I can type in
185:17 you'll get a blue box and I can type in compare. C and I can just manually
185:19 compare. C and I can just manually create it that way notice that opens the
185:21 create it that way notice that opens the tab even without my having typed code so
185:24 tab even without my having typed code so again on the left you have a guey a
185:26 again on the left you have a guey a graphical user interface albeit a
185:28 graphical user interface albeit a simplistic one on the right and at the
185:30 simplistic one on the right and at the bottom here you have a command line
185:31 bottom here you have a command line interface but they're one and the same
185:34 interface but they're one and the same what's nice though is that if I close
185:35 what's nice though is that if I close this file accidentally intentionally
185:38 this file accidentally intentionally whatnot I can reopen it without creating
185:41 whatnot I can reopen it without creating a new one by just running that same
185:43 a new one by just running that same command code space compare. c so code is
185:46 command code space compare. c so code is a vs code thing it's just a userfriendly
185:47 a vs code thing it's just a userfriendly shortcut but it's just creating a file
185:50 shortcut but it's just creating a file or opening an existing file like that
185:52 or opening an existing file like that I'm going to hide the file explorer just
185:54 I'm going to hide the file explorer just to make more room for code here and
185:55 to make more room for code here and let's go ahead and do this let's write a
185:57 let's go ahead and do this let's write a program that compares two values that
185:59 program that compares two values that the human inputs but not strings this
186:01 the human inputs but not strings this time let's use some actual integers all
186:03 time let's use some actual integers all right I'm going to go ahead and include
186:05 right I'm going to go ahead and include the cs-50 librar header file at top
186:08 the cs-50 librar header file at top cs50.h I'm going to also include
186:11 cs50.h I'm going to also include standard io. why one gives me
186:14 standard io. why one gives me userfriendly input via get string get
186:16 userfriendly input via get string get int and so forth one gives me user
186:18 int and so forth one gives me user friendly output via printf in the case
186:20 friendly output via printf in the case of standard i.h now I'm just going to
186:23 of standard i.h now I'm just going to kind of blindly type this line of code
186:25 kind of blindly type this line of code which we'll come back to in future weeks
186:26 which we'll come back to in future weeks but for now that's analogous to the when
186:29 but for now that's analogous to the when green flag clicked code in scratch and
186:31 green flag clicked code in scratch and now let's go ahead and do this let me go
186:33 now let's go ahead and do this let me go ahead and get an INT from the user and
186:36 ahead and get an INT from the user and ask the user what's X question mark I'm
186:38 ask the user what's X question mark I'm not going to bother with a new line I
186:40 not going to bother with a new line I want to keep it all in one line just for
186:41 want to keep it all in one line just for Aesthetics sake but when I get back an
186:44 Aesthetics sake but when I get back an INT just like I get back a string I get
186:46 INT just like I get back a string I get back a return value so if I want to
186:49 back a return value so if I want to store the result of get int somewhere I
186:52 store the result of get int somewhere I had better put it in a variable and I
186:54 had better put it in a variable and I can call the variable anything I want
186:56 can call the variable anything I want previously I used answer or first or
186:58 previously I used answer or first or last now I'm going to use x but there's
187:01 last now I'm going to use x but there's still two things left to do here
187:04 still two things left to do here logically even though we haven't
187:05 logically even though we haven't technically done this yet what do I
187:06 technically done this yet what do I still need to
187:08 still need to do so I need the semicolon at the
187:11 do so I need the semicolon at the end and the in at the beginning you the
187:14 end and the in at the beginning you the programmer starting today kind of need
187:17 programmer starting today kind of need to decide what you're going to be
187:18 to decide what you're going to be storing in your variables and you just
187:19 storing in your variables and you just need to tell the computer that so that
187:21 need to tell the computer that so that it knows now as a teaser for languages
187:23 it knows now as a teaser for languages like python more modern languages turns
187:26 like python more modern languages turns out humans realize well gee this is
187:27 out humans realize well gee this is stupid like why can't the computer just
187:29 stupid like why can't the computer just figure out that I'm putting an INT there
187:30 figure out that I'm putting an INT there why do I have to tell it proactively so
187:32 why do I have to tell it proactively so in some languages nowadays like python
187:34 in some languages nowadays like python we'll get rid of some of this syntax
187:36 we'll get rid of some of this syntax we'll get rid of the semicolons but for
187:37 we'll get rid of the semicolons but for now we're looking at really the origins
187:39 now we're looking at really the origins of how this all worked all right so I've
187:41 of how this all worked all right so I've done this one line ending with semicolon
187:44 done this one line ending with semicolon let me do one other and let me get a
187:45 let me do one other and let me get a second int asking the user what's why
187:48 second int asking the user what's why question mark so almost identical but
187:50 question mark so almost identical but different responses from the user
187:52 different responses from the user hopefully and let me just ask simply if
187:54 hopefully and let me just ask simply if x is less than y in parentheses then
187:58 x is less than y in parentheses then some curly braces let me go ahead and
188:00 some curly braces let me go ahead and print out quote unquote X is less than y
188:04 print out quote unquote X is less than y back sln and now just as a side note I I
188:07 back sln and now just as a side note I I seem to be typing kind of fast some of
188:10 seem to be typing kind of fast some of that is because vs code is helping me
188:12 that is because vs code is helping me let me go back to this
188:13 let me go back to this first line with the if hit enter and now
188:16 first line with the if hit enter and now I'm only on my keyboard going to type
188:18 I'm only on my keyboard going to type the open curly brace this is a feature
188:21 the open curly brace this is a feature of many text editors nowadays it
188:23 of many text editors nowadays it finishes part of your thought why just
188:25 finishes part of your thought why just to save yourself a keystroke to make
188:26 to save yourself a keystroke to make sure you don't accidentally forget the
188:28 sure you don't accidentally forget the closing one so you'll notice sometimes
188:30 closing one so you'll notice sometimes that things are happening that you
188:31 that things are happening that you didn't type it's just vs code or future
188:33 didn't type it's just vs code or future programs you use trying to be helpful
188:35 programs you use trying to be helpful for you I'll go ahead and manually type
188:37 for you I'll go ahead and manually type out now printf uh X is less than y back
188:41 out now printf uh X is less than y back slend close quote semicolon so let me go
188:44 slend close quote semicolon so let me go ahead now and try to run this and we'll
188:47 ahead now and try to run this and we'll see let's see so make not hello but make
188:50 see let's see so make not hello but make compare because this file is called
188:52 compare because this file is called compare. C hitting enter okay no output
188:56 compare. C hitting enter okay no output is good because it means I haven't
188:58 is good because it means I haven't messed up let me do dot SL compare
189:00 messed up let me do dot SL compare instead of/ hello enter what's X how
189:04 instead of/ hello enter what's X how about one what's Y how about two x is
189:07 about one what's Y how about two x is less than y well let's try it again and
189:10 less than y well let's try it again and here I'll save you some keystrokes too
189:11 here I'll save you some keystrokes too let me clear my screen instead of
189:13 let me clear my screen instead of constantly typing dot slash this and Dot
189:16 constantly typing dot slash this and Dot slash that you can also use your
189:17 slash that you can also use your keyboard's arrow keys in vs code to
189:20 keyboard's arrow keys in vs code to scroll back through time so if I hit up
189:22 scroll back through time so if I hit up once there's the last command I wrote If
189:24 once there's the last command I wrote If I write uh do it up twice there's the
189:26 I write uh do it up twice there's the second to last command I wrote so
189:28 second to last command I wrote so sometimes if you see me doing things
189:29 sometimes if you see me doing things fast it's just because I'm kind of
189:30 fast it's just because I'm kind of cheating and going through my history
189:32 cheating and going through my history like that all right let me go ahead
189:34 like that all right let me go ahead though and
189:35 though and rerun compare enter let's reverse it
189:38 rerun compare enter let's reverse it this time two for x one for y and now of
189:42 this time two for x one for y and now of course there's no no output all right
189:43 course there's no no output all right well that's logically to be expected
189:45 well that's logically to be expected because we didn't have an else here so
189:46 because we didn't have an else here so let's add that else now let's open my
189:49 let's add that else now let's open my curly braces letting vs code do one of
189:51 curly braces letting vs code do one of them for me print F quote unquote X is
189:54 them for me print F quote unquote X is not less than y back sln semicolon let
189:59 not less than y back sln semicolon let me go ahead and try this again/ compare
190:02 me go ahead and try this again/ compare enter again two for x one for y and we
190:05 enter again two for x one for y and we should see
190:08 should see huh what did I do wrong why am I not
190:11 huh what did I do wrong why am I not seeing any else output
190:18 yeah exactly you got to get into the Habit after you change your code of
190:19 Habit after you change your code of recompiling it otherwise the zeros and
190:21 recompiling it otherwise the zeros and ones in the server are the old ones
190:24 ones in the server are the old ones until you manually compile so let's fix
190:26 until you manually compile so let's fix this make compare enter no error
190:28 this make compare enter no error messages that's good do/ compare 2 1 and
190:32 messages that's good do/ compare 2 1 and now I get back the output so X is not
190:34 now I get back the output so X is not less than y how about if I go and add in
190:37 less than y how about if I go and add in the third condition well we can do this
190:40 the third condition well we can do this either efficiently or inefficiently let
190:42 either efficiently or inefficiently let me go ahead head and refine this so else
190:44 me go ahead head and refine this so else if x is greater than y let's literally
190:47 if x is greater than y let's literally say x is greater than y and now I could
190:51 say x is greater than y and now I could do X else if x equals equals y but I
190:54 do X else if x equals equals y but I think we already claim that that's
190:56 think we already claim that that's unnecessarily inefficient so let's just
190:58 unnecessarily inefficient so let's just have our catchall and here I'm going to
191:00 have our catchall and here I'm going to say quote unquote X is equal 2 y back
191:04 say quote unquote X is equal 2 y back sln close quote there so I think now
191:07 sln close quote there so I think now with this code we've handled all three
191:09 with this code we've handled all three scenarios let me go ahead and recompile
191:11 scenarios let me go ahead and recompile it properly compare do/ compare and now
191:15 it properly compare do/ compare and now 1 and 2 x is less than y let me run it
191:19 1 and 2 x is less than y let me run it again 2 and One X is greater than y and
191:23 again 2 and One X is greater than y and lastly one and one and X is equal to Y
191:27 lastly one and one and X is equal to Y so for the most part our code's getting
191:29 so for the most part our code's getting longer we're up to like 21 lines of code
191:31 longer we're up to like 21 lines of code though some of them are just single
191:33 though some of them are just single characters on the screen almost
191:34 characters on the screen almost everything else is the same I'm using
191:36 everything else is the same I'm using the cs50 libraries header file for my
191:39 the cs50 libraries header file for my get int function standard i.h for my
191:43 get int function standard i.h for my print F function and the rest of this is
191:45 print F function and the rest of this is just now new syntax for conditionals as
191:47 just now new syntax for conditionals as well questions then on this C
191:50 well questions then on this C implementation of just some basic
191:52 implementation of just some basic comparisons like
191:53 comparisons like this any questions
191:57 this any questions [Music]
192:00 [Music] yeah good question do the opening
192:02 yeah good question do the opening brackets need to be on a separate line
192:03 brackets need to be on a separate line in cs50 yes what you'll see is that as
192:06 in cs50 yes what you'll see is that as part of the submission process We
192:08 part of the submission process We compare your code against a style guide
192:10 compare your code against a style guide which is the norm in Industry a company
192:12 which is the norm in Industry a company would have its own sense of style and
192:14 would have its own sense of style and how its code should look and there's
192:16 how its code should look and there's generally automated tools within a
192:17 generally automated tools within a company that help uh give feedback on
192:19 company that help uh give feedback on the code or stylize it as such there are
192:22 the code or stylize it as such there are alternative Styles than what we use in
192:24 alternative Styles than what we use in the class we deliberately keep and ask
192:27 the class we deliberately keep and ask that you keep the curly braces on their
192:29 that you keep the curly braces on their own line if only because it rather
192:31 own line if only because it rather resembles like the hugging nature of
192:34 resembles like the hugging nature of scratches blocks and just makes clear
192:36 scratches blocks and just makes clear that they're balanced open and closed
192:38 that they're balanced open and closed however another common Paradigm in some
192:40 however another common Paradigm in some languages and with some programmers is
192:42 languages and with some programmers is to do something like this uh on each of
192:45 to do something like this uh on each of them so you have the opening curly brace
192:48 them so you have the opening curly brace on the same line as here we do not
192:50 on the same line as here we do not recommend this this is invogue in the
192:52 recommend this this is invogue in the JavaScript world and some others um but
192:54 JavaScript world and some others um but ultimately in the real world it's up to
192:56 ultimately in the real world it's up to each individual programmer Andor the
192:58 each individual programmer Andor the company they're working for if
192:59 company they're working for if applicable to decide on those
193:01 applicable to decide on those things all right so beyond then these
193:04 things all right so beyond then these conditionals what if we want to do
193:05 conditionals what if we want to do something that's maybe pretty common so
193:07 something that's maybe pretty common so almost every piece of software or
193:09 almost every piece of software or website nowadays that you use has you
193:11 website nowadays that you use has you like agree to some terms and conditions
193:13 like agree to some terms and conditions by typing like yes or no or just y for
193:15 by typing like yes or no or just y for yes and N for no so how could we
193:17 yes and N for no so how could we Implement some kind of Agreement System
193:19 Implement some kind of Agreement System well let me do this let me create a new
193:21 well let me do this let me create a new program a third one called agree. C so
193:24 program a third one called agree. C so I'm going to write code agree. C just to
193:26 I'm going to write code agree. C just to give myself a new tab I'm going to start
193:28 give myself a new tab I'm going to start as always now include cs50.h let's
193:31 as always now include cs50.h let's include standard i.h and then let me do
193:34 include standard i.h and then let me do my int main void which again for today's
193:36 my int main void which again for today's purposes we'll take it face value is
193:38 purposes we'll take it face value is just copy paste and if I just want to
193:41 just copy paste and if I just want to get y or n for instance instead of yes
193:43 get y or n for instance instead of yes or no we can just use a simpler variable
193:46 or no we can just use a simpler variable here how about just a Char a character A
193:49 here how about just a Char a character A Single Character so I can use get Char
193:51 Single Character so I can use get Char to ask the user for instance do you
193:54 to ask the user for instance do you agree question mark but before as before
193:58 agree question mark but before as before I need to store this somewhere so I
193:59 I need to store this somewhere so I don't want a string because it's a
194:01 don't want a string because it's a single Char I don't want an INT I just
194:03 single Char I don't want an INT I just want a Char and it's literally CH h a r
194:06 want a Char and it's literally CH h a r and then I could call this thing
194:07 and then I could call this thing anything I want it's conventional if you
194:09 anything I want it's conventional if you have a simple program with just a single
194:11 have a simple program with just a single variable and it's of type Char call it C
194:13 variable and it's of type Char call it C if it's an INT call it I if it's a
194:15 if it's an INT call it I if it's a string call it s for now I'm just going
194:17 string call it s for now I'm just going to keep it simple and call it C and now
194:19 to keep it simple and call it C and now I'm going to ask a question so if C
194:23 I'm going to ask a question so if C equals equals how about quote unquote
194:28 equals equals how about quote unquote y then let me go ahead and print out uh
194:32 y then let me go ahead and print out uh agreed back sln as though they agreed to
194:34 agreed back sln as though they agreed to my terms and conditions uh otherwise
194:38 my terms and conditions uh otherwise let's see else if the character equals
194:40 let's see else if the character equals equals quote unquote n
194:43 equals quote unquote n then let me go ahead and print out uh
194:45 then let me go ahead and print out uh say not agreed as though they didn't
194:49 say not agreed as though they didn't quote unquote and let's leave it at that
194:52 quote unquote and let's leave it at that I think here initially now you'll notice
194:54 I think here initially now you'll notice one curiosity one inconsistency perhaps
194:58 one curiosity one inconsistency perhaps does anyone want to call it out though
195:00 does anyone want to call it out though it's somewhat
195:01 it's somewhat subtle I've done something ever so
195:03 subtle I've done something ever so slightly differently without explaining
195:05 slightly differently without explaining it yet do you see it single single yeah
195:10 it yet do you see it single single yeah so I've suddenly used single quotation
195:12 so I've suddenly used single quotation marks for my single characters and
195:14 marks for my single characters and double quotes for my actual strings of
195:16 double quotes for my actual strings of text this is a necessity in C when
195:18 text this is a necessity in C when you're dealing with strings like strings
195:20 you're dealing with strings like strings of text like someone's name a sentence a
195:23 of text like someone's name a sentence a paragraph anything really more than one
195:24 paragraph anything really more than one character you typically use uh double
195:27 character you typically use uh double quotes and indeed you must when dealing
195:29 quotes and indeed you must when dealing with deliberately single characters like
195:32 with deliberately single characters like I am here for y or n you must use single
195:35 I am here for y or n you must use single quotes instead why because that makes
195:37 quotes instead why because that makes sure that the computer knows that it's
195:39 sure that the computer knows that it's indeed a Char and not a string so double
195:41 indeed a Char and not a string so double quotes are for Strings single quotes are
195:43 quotes are for Strings single quotes are for chars so with that said let me go
195:46 for chars so with that said let me go ahead and zoom out let me go ahead in my
195:48 ahead and zoom out let me go ahead in my terminal window run make agree enter
195:51 terminal window run make agree enter seems to work okay so let me go ahead
195:53 seems to work okay so let me go ahead and do/ agree uh let me go ahead now and
195:57 and do/ agree uh let me go ahead now and type in y here we go
196:01 type in y here we go enter huh uh let me try that again rerun
196:04 enter huh uh let me try that again rerun do/ agree how about no
196:08 do/ agree how about no enter why is it not behaving as I would
196:11 enter why is it not behaving as I would have expected uh because you the capital
196:14 have expected uh because you the capital Y yeah I kind of cheated there and I hit
196:16 Y yeah I kind of cheated there and I hit the caps lock key just as I started
196:17 the caps lock key just as I started typing in input why because I
196:19 typing in input why because I deliberately wanted to type in uppercase
196:21 deliberately wanted to type in uppercase instead of lowercase which is kind of
196:23 instead of lowercase which is kind of reasonable right it's a little obnoxious
196:25 reasonable right it's a little obnoxious if you force the user to toggle their
196:26 if you force the user to toggle their caps lock key on or off when you just
196:28 caps lock key on or off when you just need a simple answer that's not the best
196:30 need a simple answer that's not the best user experience or ux but it would work
196:32 user experience or ux but it would work if I cooperated let me run this again
196:34 if I cooperated let me run this again without caps lock on y lowercase for yes
196:38 without caps lock on y lowercase for yes ah that worked and uh lowercase for no
196:41 ah that worked and uh lowercase for no that work worked but how could I get it
196:43 that work worked but how could I get it to work for both well how about this let
196:45 to work for both well how about this let me go ahead and just add two
196:47 me go ahead and just add two possibilities so else if C equals equals
196:50 possibilities so else if C equals equals quote unquote capital Y then also do
196:54 quote unquote capital Y then also do printf agreed back sln and down here
196:58 printf agreed back sln and down here else if C equals equals uh single quote
197:02 else if C equals equals uh single quote uh capital N then go ahead and print out
197:05 uh capital N then go ahead and print out again not agreed okay this I will claim
197:08 again not agreed okay this I will claim now is correct and I'll do make agree
197:11 now is correct and I'll do make agree real fast do slash agree and I'll use
197:14 real fast do slash agree and I'll use Capital it now works I'll use uh Capital
197:17 Capital it now works I'll use uh Capital it again works but this is perhaps not
197:19 it again works but this is perhaps not the best design let me hide the terminal
197:22 the best design let me hide the terminal window and pull this up on the screen
197:23 window and pull this up on the screen all at
197:25 all at once why might this arguably not be the
197:28 once why might this arguably not be the best design even though it's
197:33 correct there's another term of art we can toss here like something smells kind
197:36 can toss here like something smells kind of funky about this code this is an
197:38 of funky about this code this is an actual term of art like there's code
197:39 actual term of art like there's code smell here like something smells a
197:41 smell here like something smells a little off why what do you
197:44 little off why what do you [Music]
197:53 [Music] think yeah there's the same output again
197:55 think yeah there's the same output again and again I mean I manually typed it but
197:57 and again I mean I manually typed it but honestly I might as well have just
197:58 honestly I might as well have just copied and pasted most of my original
198:00 copied and pasted most of my original code to do it again and again for the C
198:02 code to do it again and again for the C two capital letters so if line 10 and 14
198:06 two capital letters so if line 10 and 14 are the same and line 18 and 22 are the
198:09 are the same and line 18 and 22 are the same and then the rest of these if and
198:11 same and then the rest of these if and Els ifs are almost the same like like
198:13 Els ifs are almost the same like like there's some code smell there like it's
198:15 there's some code smell there like it's not well-designed why because if I want
198:17 not well-designed why because if I want to change things now just like last week
198:18 to change things now just like last week in scratch I might have to change my
198:20 in scratch I might have to change my code in multiple places or copy paste is
198:22 code in multiple places or copy paste is never a good thing and go God forbid I
198:24 never a good thing and go God forbid I want to add support for yes and no as
198:27 want to add support for yes and no as full words it's really going to get long
198:29 full words it's really going to get long so how can we solve this well it turns
198:31 so how can we solve this well it turns out like we can combine some of these
198:33 out like we can combine some of these thoughts so let me try to improve the
198:35 thoughts so let me try to improve the yeses first it turns out if I delete
198:37 yeses first it turns out if I delete that Clause I can actually or things
198:40 that Clause I can actually or things together in scratch there's a couple
198:42 together in scratch there's a couple puzzle pieces if you didn't discover
198:43 puzzle pieces if you didn't discover them that literally have the word or and
198:45 them that literally have the word or and the word and on them which allow you to
198:47 the word and on them which allow you to combine Boolean Expressions so that
198:49 combine Boolean Expressions so that either this or this is true or this and
198:52 either this or this is true or this and this is true in C you can't just say the
198:55 this is true in C you can't just say the word or you instead use two vertical
198:58 word or you instead use two vertical bars and vertical bars together mean or
199:02 bars and vertical bars together mean or logically and so I can say C equals
199:04 logically and so I can say C equals equals quote unquote capital Y agreed
199:08 equals quote unquote capital Y agreed and now I can get rid of this code down
199:10 and now I can get rid of this code down here and let me go ahead and say
199:13 here and let me go ahead and say vertical vertical bar twice C equals uh
199:16 vertical vertical bar twice C equals uh quote unquote n in all caps and now my
199:19 quote unquote n in all caps and now my program's like you know roughly a third
199:21 program's like you know roughly a third uh smaller which is good there's less
199:24 uh smaller which is good there's less redundancy and if I reopen my terminal
199:25 redundancy and if I reopen my terminal window rerun make of agree SL agree now
199:30 window rerun make of agree SL agree now I can type little y or Big Y and same
199:34 I can type little y or Big Y and same thing for lowercase and uppercase n any
199:37 thing for lowercase and uppercase n any questions then on this syntax whereby
199:39 questions then on this syntax whereby now you can combine thoughts and just
199:40 now you can combine thoughts and just kind of tighten things up
199:43 kind of tighten things up and there'll be other such tricks too
199:50 yeah a really good question is there not a function to just ignore the case short
199:53 a function to just ignore the case short answer there is and we'll see how to do
199:55 answer there is and we'll see how to do that in actually just about a week's
199:57 that in actually just about a week's time and in other languages there's even
199:59 time and in other languages there's even more ways to just canonicalize the
200:01 more ways to just canonicalize the user's input throwing away any space
200:03 user's input throwing away any space characters they might have accidentally
200:04 characters they might have accidentally hit forcing everything to lower case in
200:06 hit forcing everything to lower case in C it's going to be a little more work on
200:09 C it's going to be a little more work on our part to do that but in fact as early
200:10 our part to do that but in fact as early as next week we'll see how we can do
200:12 as next week we'll see how we can do that but for now we're comparing indeed
200:14 that but for now we're comparing indeed just these literal values other
200:17 just these literal values other [Music]
200:23 [Music] questions really good question so we are
200:26 questions really good question so we are assuming with this program in all of my
200:28 assuming with this program in all of my last ones that the human's cooperating
200:29 last ones that the human's cooperating when I asked for their name they typed
200:31 when I asked for their name they typed in David and not one two three or in
200:33 in David and not one two three or in this case they typed in a single
200:35 this case they typed in a single character and not a full word so this is
200:37 character and not a full word so this is one of the features often of using a
200:39 one of the features often of using a library so for instance if I run agree
200:41 library so for instance if I run agree again
200:42 again and I say something like sure enter it
200:46 and I say something like sure enter it rejects it alt together why because s e
200:50 rejects it alt together why because s e is a string of characters it's not a
200:52 is a string of characters it's not a single character now I could just say
200:54 single character now I could just say something like X which is neither y nor
200:57 something like X which is neither y nor n of course but it tolerates that
200:59 n of course but it tolerates that because it's a single character but
201:00 because it's a single character but built into cs50's library is some
201:03 built into cs50's library is some built-in rejections of inputs that's not
201:05 built-in rejections of inputs that's not expected so if you use get int and the
201:07 expected so if you use get int and the user types in not the number one or two
201:09 user types in not the number one or two but cat C A it will just prompt them
201:12 but cat C A it will just prompt them again prompt them again and this is
201:14 again prompt them again and this is where too if you were to do this
201:15 where too if you were to do this manually in C you end up writing this
201:18 manually in C you end up writing this much code just to check for all of these
201:20 much code just to check for all of these errors that's why we use these training
201:21 errors that's why we use these training wheels for a few weeks just to make the
201:23 wheels for a few weeks just to make the code more robust but in a few weeks time
201:25 code more robust but in a few weeks time we'll take the library away and you'll
201:27 we'll take the library away and you'll see and understand how it's doing all
201:29 see and understand how it's doing all it's indeed doing all that all right so
201:32 it's indeed doing all that all right so how about this let's now transition to
201:34 how about this let's now transition to something a little more scratch like
201:36 something a little more scratch like literally uh by creating how about
201:38 literally uh by creating how about another program here called meow so
201:40 another program here called meow so meow. C we won't have any audio
201:42 meow. C we won't have any audio capabilities for this one will just rely
201:43 capabilities for this one will just rely on print and suppose that I wanted to
201:46 on print and suppose that I wanted to write a program in C that just simulates
201:48 write a program in C that just simulates like a cat meowing so I don't need any
201:50 like a cat meowing so I don't need any user input just yet so I'm just going to
201:51 user input just yet so I'm just going to use standard i.h I'm going to do my
201:54 use standard i.h I'm going to do my usual int main void up here and then I'm
201:56 usual int main void up here and then I'm just going to go ahead and do printf
201:58 just going to go ahead and do printf meow back sln and let's have this C meow
202:01 meow back sln and let's have this C meow three times like last week so I'm going
202:03 three times like last week so I'm going to do meow meow meow notice as an aside
202:06 to do meow meow meow notice as an aside whenever you highlight the lines you'll
202:07 whenever you highlight the lines you'll see little dots appear this is just a
202:09 see little dots appear this is just a visual cue to you to let you figure out
202:11 visual cue to you to let you figure out how many spaces you've indented vs code
202:13 how many spaces you've indented vs code like a lot of editors will automatically
202:15 like a lot of editors will automatically indent your code for you I've not been
202:17 indent your code for you I've not been hitting the space bar four times every
202:18 hitting the space bar four times every time I've not even been hitting tab
202:20 time I've not even been hitting tab however in C the convention is indeed to
202:23 however in C the convention is indeed to indent lines where appropriate by four
202:26 indent lines where appropriate by four spaces so not three not five and these
202:28 spaces so not three not five and these dots help you see things so that they
202:30 dots help you see things so that they just line up as a matter of good style
202:32 just line up as a matter of good style all right so this program I'm just going
202:34 all right so this program I'm just going to stipulate right now is indeed going
202:35 to stipulate right now is indeed going to work make meow which is kind of cute
202:38 to work make meow which is kind of cute and now meow there three times correct
202:41 and now meow there three times correct it's meowing three times but of course
202:42 it's meowing three times but of course this is not well designed it wasn't well
202:44 this is not well designed it wasn't well designed in scratch last week why what
202:47 designed in scratch last week why what should I be doing
202:48 should I be doing differently
202:51 differently yeah
202:52 yeah yeah it's a perfect like opportunity for
202:55 yeah it's a perfect like opportunity for a loop why because if you wanted to
202:57 a loop why because if you wanted to change maybe the capitalization of these
202:58 change maybe the capitalization of these words you wanted to change the sound to
203:00 words you wanted to change the sound to like woof or a dog or something like
203:02 like woof or a dog or something like you'd have to change it one two three
203:04 you'd have to change it one two three places and that's just kind of stupid
203:06 places and that's just kind of stupid right in code you should ideally change
203:08 right in code you should ideally change things in one place so how might I do
203:11 things in one place so how might I do that well we could introduce a loop yes
203:13 that well we could introduce a loop yes but we're going to need another building
203:15 but we're going to need another building block as well that we had in scratch
203:17 block as well that we had in scratch namely those things called variables so
203:19 namely those things called variables so we're call that a variable like in
203:21 we're call that a variable like in algebra x y z whatever can store a value
203:24 algebra x y z whatever can store a value for you and a variable in scratch might
203:28 for you and a variable in scratch might have looked like this you use this
203:29 have looked like this you use this orange puzzle piece to set a variable of
203:31 orange puzzle piece to set a variable of any name not just X Y or Z but you could
203:33 any name not just X Y or Z but you could call it something more descriptive like
203:35 call it something more descriptive like counter and you can set it equal to some
203:37 counter and you can set it equal to some value in C the way to do this is similar
203:41 value in C the way to do this is similar to to Spirit to some of the syntax we've
203:43 to to Spirit to some of the syntax we've seen thus far you start by saying the
203:45 seen thus far you start by saying the name of the variable you want a single
203:46 name of the variable you want a single equal sign and then the value you want
203:48 equal sign and then the value you want to initialize it to copying therefore
203:51 to initialize it to copying therefore from right to left why because the equal
203:53 from right to left why because the equal sign denotes again assignment from right
203:55 sign denotes again assignment from right to left this isn't enough though you
203:57 to left this isn't enough though you might have the intuition already what's
203:58 might have the intuition already what's missing probably from this line of code
204:01 missing probably from this line of code just to create a variable so we need int
204:04 just to create a variable so we need int to make sure the computer knows that
204:06 to make sure the computer knows that this is indeed an INT and then lastly
204:09 this is indeed an INT and then lastly semicolon as well and that now completes
204:11 semicolon as well and that now completes the thought so a little more annoying
204:12 the thought so a little more annoying than scratch but we're starting to see
204:14 than scratch but we're starting to see patterns here so not every piece of
204:16 patterns here so not every piece of syntax will be new all right if you
204:17 syntax will be new all right if you wanted to increment the counter by one
204:20 wanted to increment the counter by one scratch uses the verb change and they
204:21 scratch uses the verb change and they mean add the value to counter so if I
204:24 mean add the value to counter so if I want to increment an existing variable
204:27 want to increment an existing variable called counter this syntax is a little
204:29 called counter this syntax is a little more interesting it turns out the code
204:31 more interesting it turns out the code looks like this which almost seems like
204:34 looks like this which almost seems like a paradox like how can counter equal
204:37 a paradox like how can counter equal counter plus one like that's not how
204:39 counter plus one like that's not how math works but again a single equal sign
204:41 math works but again a single equal sign is assignment from right to left so this
204:43 is assignment from right to left so this is saying take whatever the value of
204:45 is saying take whatever the value of counter is add one to it and copy that
204:47 counter is add one to it and copy that value from right to left into counter
204:49 value from right to left into counter itself you still need the semicolon but
204:53 itself you still need the semicolon but I claim you do not need to mention the
204:55 I claim you do not need to mention the keyword int when updating an existing
204:59 keyword int when updating an existing variable so only when you create a
205:01 variable so only when you create a variable in C do you use the word string
205:04 variable in C do you use the word string or the word int or any of the others
205:05 or the word int or any of the others will eventually see only when creating
205:07 will eventually see only when creating it or initializing It For the First Time
205:10 it or initializing It For the First Time thereafter if you want to change it it
205:11 thereafter if you want to change it it just exists it's the word you gave it
205:13 just exists it's the word you gave it the computer's smart enough to at least
205:15 the computer's smart enough to at least remember what type it is so this line is
205:17 remember what type it is so this line is now complete turns out in code as we'll
205:20 now complete turns out in code as we'll see it's pretty common to want to add
205:21 see it's pretty common to want to add things together increase increment
205:23 things together increase increment Things by One so there's actually
205:25 Things by One so there's actually different Syntax for the same idea the
205:27 different Syntax for the same idea the term of art here is syntactic sugar like
205:29 term of art here is syntactic sugar like there's often in code many ways to do
205:32 there's often in code many ways to do the same thing even though at the end of
205:33 the same thing even though at the end of the day they do exactly the same
205:35 the day they do exactly the same functionality so for instance if after a
205:38 functionality so for instance if after a few days of cs50 you find this a little
205:40 few days of cs50 you find this a little tedious to keep typing and some program
205:42 tedious to keep typing and some program you can simplify it to just this this is
205:44 you can simplify it to just this this is the syntactic sugar you can use plus
205:47 the syntactic sugar you can use plus equals and only mention the variable
205:49 equals and only mention the variable name once on the left and it just knows
205:51 name once on the left and it just knows that that means the previous thing it's
205:53 that that means the previous thing it's just slightly uh more uh more succinct
205:57 just slightly uh more uh more succinct this to is such a common thing to add
205:58 this to is such a common thing to add one to a value and it doesn't have to be
206:00 one to a value and it doesn't have to be one but in this case it is but if it is
206:01 one but in this case it is but if it is indeed one you can further tighten the
206:03 indeed one you can further tighten the code up to just do this counter Plus+ so
206:07 code up to just do this counter Plus+ so anytime in C you see plus plus it means
206:09 anytime in C you see plus plus it means literally adding one to that particular
206:12 literally adding one to that particular variable there's other ways to do this
206:14 variable there's other ways to do this in the other direction if you want to
206:16 in the other direction if you want to subtract one from a variable you can use
206:18 subtract one from a variable you can use any of the previous syntax using a minus
206:20 any of the previous syntax using a minus sign instead of Plus or you can more
206:22 sign instead of Plus or you can more succinctly do counter minus minus this
206:25 succinctly do counter minus minus this is the way a typical C programmer would
206:27 is the way a typical C programmer would do this all right so if we have now
206:30 do this all right so if we have now variables let's go and solve the meowing
206:33 variables let's go and solve the meowing with loops so in scratch we saw Loops
206:35 with loops so in scratch we saw Loops like this this of course had the cat
206:37 like this this of course had the cat meow three times how do we do this in C
206:41 meow three times how do we do this in C now this this is where things get a
206:42 now this this is where things get a little more uh involved codewise but if
206:45 little more uh involved codewise but if you understand each and every line it
206:47 you understand each and every line it will follow logically what's going on so
206:50 will follow logically what's going on so here I claim is one way to implement a
206:54 here I claim is one way to implement a loop that iterates three times in C and
206:57 loop that iterates three times in C and this is kind of ridiculous right like we
206:59 this is kind of ridiculous right like we went from two super simple puzzle pieces
207:01 went from two super simple puzzle pieces like this to my God like it's 1 2 3 four
207:03 like this to my God like it's 1 2 3 four five six lines of code all of which are
207:05 five six lines of code all of which are pretty involved so like that escalated
207:06 pretty involved so like that escalated quickly but what's each line doing and
207:08 quickly but what's each line doing and we'll see other ways to do this more
207:10 we'll see other ways to do this more simply so we're izing a variable called
207:12 simply so we're izing a variable called counter to three just like before why
207:16 counter to three just like before why well what does it mean to Loop or to
207:17 well what does it mean to Loop or to repeat something three times well it's
207:19 repeat something three times well it's kind of like doing something three times
207:21 kind of like doing something three times and then do it and then count down and
207:23 and then do it and then count down and then do it and then count down and then
207:25 then do it and then count down and then do it until you're all out of counts so
207:28 do it until you're all out of counts so this is declaring a variable called
207:29 this is declaring a variable called counter setting it equal to three then
207:31 counter setting it equal to three then I'm inducing a loop in C which is
207:34 I'm inducing a loop in C which is similar in spirit to repeat three but
207:37 similar in spirit to repeat three but you have to do more of the math yourself
207:39 you have to do more of the math yourself so I'm asking the question in
207:40 so I'm asking the question in parentheses
207:41 parentheses while counter is greater than zero what
207:44 while counter is greater than zero what do I want to do well Pur the indentation
207:47 do I want to do well Pur the indentation inside the curly braces I want to meow
207:49 inside the curly braces I want to meow one time and then to be clear what's
207:51 one time and then to be clear what's this last line of code doing if counter
207:54 this last line of code doing if counter starts off at three this makes it two by
207:57 starts off at three this makes it two by subtracting one from it then what
207:59 subtracting one from it then what happens by nature of a loop just like in
208:01 happens by nature of a loop just like in scratch it kind of knows to go back and
208:03 scratch it kind of knows to go back and forth even though there's nice pretty
208:05 forth even though there's nice pretty arrow in scratch and there isn't here C
208:07 arrow in scratch and there isn't here C knows to do this again and again and
208:10 knows to do this again and again and again con stantly asking this question
208:13 again con stantly asking this question and then updating this value at the end
208:15 and then updating this value at the end so if I highlight just a few of these
208:17 so if I highlight just a few of these steps the variable starts off at three
208:20 steps the variable starts off at three and actually let me simplify two I
208:21 and actually let me simplify two I claimed earlier that uh when using
208:23 claimed earlier that uh when using single variables people very often just
208:25 single variables people very often just call it I for INT or c for Char or s for
208:28 call it I for INT or c for Char or s for string unless you have multiple
208:29 string unless you have multiple variables so let me tighten the code up
208:31 variables so let me tighten the code up and this already makes it look a little
208:32 and this already makes it look a little more tolerable let me actually tighten
208:34 more tolerable let me actually tighten it up further and one more step so now
208:37 it up further and one more step so now this is about as tight as succinct as
208:39 this is about as tight as succinct as you can make this code at the moment so
208:41 you can make this code at the moment so what's actually going to happen here
208:42 what's actually going to happen here well the first line of code executes and
208:44 well the first line of code executes and that initializes I to 3 then we check
208:46 that initializes I to 3 then we check the condition while I is greater than
208:48 the condition while I is greater than zero is I greater than zero we'll per my
208:50 zero is I greater than zero we'll per my three fingers obviously so we print out
208:52 three fingers obviously so we print out meow on the screen then we subtract one
208:55 meow on the screen then we subtract one from I at which point now we have two as
208:58 from I at which point now we have two as the value of I then the code goes back
209:00 the value of I then the code goes back to the condition and notice the
209:02 to the condition and notice the condition there is in parentheses that's
209:04 condition there is in parentheses that's another Boolean expression so Loops can
209:06 another Boolean expression so Loops can use Boolean Expressions just like
209:09 use Boolean Expressions just like conditionals use Boolean Expressions to
209:10 conditionals use Boolean Expressions to make decis ision the loop though is
209:12 make decis ision the loop though is deciding not whether to do this thing or
209:14 deciding not whether to do this thing or that but whether to do the same thing
209:16 that but whether to do the same thing again and again and again and as it
209:18 again and again and again and as it ticks through the code one line after
209:20 ticks through the code one line after the other it's ultimately going to get
209:22 the other it's ultimately going to get down to uh one and then zero and then
209:26 down to uh one and then zero and then stop so put another way came with some
209:28 stop so put another way came with some props here so suppose uh this bowl here
209:31 props here so suppose uh this bowl here is your variable and you initialize it
209:33 is your variable and you initialize it to three with like three stress balls
209:36 to three with like three stress balls you can do something three times right
209:38 you can do something three times right if I want to give out three stress balls
209:39 if I want to give out three stress balls here's your chance for free stress ball
209:41 here's your chance for free stress ball without having to answer any questions
209:42 without having to answer any questions any okay there we go so here we go
209:45 any okay there we go so here we go subtracting one from my variable I'm
209:46 subtracting one from my variable I'm left with two uh oh my God all right uh
209:50 left with two uh oh my God all right uh don't tell
209:52 don't tell Sounders oh I'm sorry oh okay that ended
209:56 Sounders oh I'm sorry oh okay that ended poorly apologies all right but now the
209:59 poorly apologies all right but now the educational point though is that my
210:01 educational point though is that my variable has been uh decremented further
210:03 variable has been uh decremented further to just have I'm not throwing that far
210:04 to just have I'm not throwing that far again I can't do this in here we go all
210:07 again I can't do this in here we go all right here we go and one final
210:08 right here we go and one final subtraction and now our variable is left
210:11 subtraction and now our variable is left so we have three stress balls there and
210:13 so we have three stress balls there and that's all a variable is right it's some
210:15 that's all a variable is right it's some kind of storage it's actually of course
210:17 kind of storage it's actually of course implemented in the computer's memory but
210:19 implemented in the computer's memory but metaphorically it's really just like a
210:21 metaphorically it's really just like a bow with some values and every time you
210:23 bow with some values and every time you add or in this case subtract you're just
210:24 add or in this case subtract you're just changing the value of that variable and
210:26 changing the value of that variable and then the code meanwhile of course in
210:28 then the code meanwhile of course in parenthesis is just checking is the bow
210:30 parenthesis is just checking is the bow empty is the bowl empty is the bowl
210:32 empty is the bowl empty is the bowl empty AKA is I greater than zero or not
210:37 empty AKA is I greater than zero or not any questions on how we've implemented
210:38 any questions on how we've implemented Loops in this way
210:41 Loops in this way and I I'll you a stress ball after class
210:44 and I I'll you a stress ball after class questions on Loops all right so it turns
210:47 questions on Loops all right so it turns out this is kind of ugly and like this
210:50 out this is kind of ugly and like this really starts to take the fun out of
210:52 really starts to take the fun out of programming uh when you have to like
210:54 programming uh when you have to like write out this uh sequence of steps so
210:56 write out this uh sequence of steps so it turns out there's other ways to do
210:57 it turns out there's other ways to do this but first let's see logically how
210:59 this but first let's see logically how else you might Express this because it's
211:00 else you might Express this because it's a little weird that we keep using zero
211:03 a little weird that we keep using zero so the one other way to do this would be
211:05 so the one other way to do this would be to invert the logic you could absolutely
211:07 to invert the logic you could absolutely start with your variable call it I equal
211:09 start with your variable call it I equal to one and then you ask the question is
211:12 to one and then you ask the question is I less than or equal to three and notice
211:15 I less than or equal to three and notice a bit of new syntax here on your uh
211:17 a bit of new syntax here on your uh typical keyboard there is no less than
211:19 typical keyboard there is no less than or equal sign or greater than or equal
211:21 or equal sign or greater than or equal sign like you would write in math class
211:23 sign like you would write in math class with one over the other and so in C you
211:25 with one over the other and so in C you use two characters less than followed by
211:28 use two characters less than followed by an equal sign or if appropriate greater
211:30 an equal sign or if appropriate greater than followed by an equal sign and that
211:32 than followed by an equal sign and that logically captures that idea so notice
211:34 logically captures that idea so notice that I'm kind of changing my questions
211:37 that I'm kind of changing my questions I'm initializing I to one and then I'm
211:39 I'm initializing I to one and then I'm going to increment It ultimately to two
211:41 going to increment It ultimately to two and then three but because I'm doing
211:43 and then three but because I'm doing less than or equal to it's still going
211:44 less than or equal to it's still going to go from 1 to three so that works too
211:48 to go from 1 to three so that works too we could similarly do this yet another
211:50 we could similarly do this yet another way we could initialize I to zero and
211:53 way we could initialize I to zero and then we could say well I is less than
211:56 then we could say well I is less than three and keep incrementing it and I
211:58 three and keep incrementing it and I showed this last form is actually the
212:00 showed this last form is actually the most canonical like it might be the most
212:02 most canonical like it might be the most humanlike to think in terms of 1 to
212:04 humanlike to think in terms of 1 to three it might be the most stress ball
212:06 three it might be the most stress ball like to think in terms of three to zero
212:09 like to think in terms of three to zero counting down but Ty typically the go-to
212:11 counting down but Ty typically the go-to Syntax for most programmers once you get
212:13 Syntax for most programmers once you get comfortable counting from zero is to
212:16 comfortable counting from zero is to always start counting from zero and
212:18 always start counting from zero and count up to less than the value you're
212:21 count up to less than the value you're counting up to so it would be incorrect
212:23 counting up to so it would be incorrect why to change this to less than or equal
212:25 why to change this to less than or equal to three here what would happen if I
212:27 to three here what would happen if I Chang the less than to less than or
212:29 Chang the less than to less than or equal to it'll me out twice yeah it'll
212:31 equal to it'll me out twice yeah it'll out an extra a fourth time in fact total
212:33 out an extra a fourth time in fact total right because you'll start at zero then
212:35 right because you'll start at zero then one then two then three and less than or
212:38 one then two then three and less than or equal to three sorry three will give you
212:40 equal to three sorry three will give you the fourth time so we do want it indeed
212:43 the fourth time so we do want it indeed to be just a single less than all right
212:45 to be just a single less than all right so now that we have those options let me
212:48 so now that we have those options let me just give you one other and this one
212:49 just give you one other and this one takes a little more getting used to as
212:50 takes a little more getting used to as well but it's probably the more common
212:52 well but it's probably the more common way to write this let me go ahead and
212:54 way to write this let me go ahead and propose that we implement this as
212:57 propose that we implement this as follows let me go back to my code here
213:00 follows let me go back to my code here let me go into my several printfs
213:04 let me go into my several printfs getting rid of all but one of them
213:06 getting rid of all but one of them ultimately and let's implement this in
213:08 ultimately and let's implement this in code so let's do in I get that's zero
213:12 code so let's do in I get that's zero how about then while I is less than
213:16 how about then while I is less than three then let's go ahead and say print
213:18 three then let's go ahead and say print F quote unquote meow uh mellow meow back
213:22 F quote unquote meow uh mellow meow back sln and then we have to do I minus minus
213:26 sln and then we have to do I minus minus or plus
213:28 or plus plus so plus plus because we're starting
213:30 plus so plus plus because we're starting at zero and going up two but not through
213:33 at zero and going up two but not through three so let me go ahead now and make
213:35 three so let me go ahead now and make meow after clearing my terminal SL meow
213:38 meow after clearing my terminal SL meow and it's still just as correct but it's
213:40 and it's still just as correct but it's a little more uh it's a little better
213:42 a little more uh it's a little better designed why because now if I want to
213:44 designed why because now if I want to change it from three to 30 times for
213:47 change it from three to 30 times for instance I can change it there I can
213:48 instance I can change it there I can recompile my code I can do/ meow and
213:52 recompile my code I can do/ meow and done I don't have to copy and paste it
213:53 done I don't have to copy and paste it 27 more times to get that effect and I
213:56 27 more times to get that effect and I can even change what the word is by
213:58 can even change what the word is by changing it in just one location but it
214:00 changing it in just one location but it turns out there's other ways to do this
214:02 turns out there's other ways to do this too and let me propose that we introduce
214:04 too and let me propose that we introduce you to what's called a for loop as well
214:07 you to what's called a for loop as well so if you want to repeat something three
214:08 so if you want to repeat something three times you can absolutely take the while
214:10 times you can absolutely take the while Loop approach that we just saw or you
214:13 Loop approach that we just saw or you can do this and this one's a little
214:15 can do this and this one's a little takes a little more getting used to but
214:16 takes a little more getting used to but it kind of consolidates into one line
214:19 it kind of consolidates into one line all of the same logic so notice we have
214:21 all of the same logic so notice we have the keyword four here and four is just a
214:24 the keyword four here and four is just a a preposition in this case that
214:26 a preposition in this case that generally implies here comes a loop
214:28 generally implies here comes a loop inside of parenthesis here is not just a
214:32 inside of parenthesis here is not just a Boolean expression and this where things
214:33 Boolean expression and this where things get a little weird there's three things
214:35 get a little weird there's three things to the left of the semicolon in the
214:37 to the left of the semicolon in the middle of the two semicolons and to the
214:39 middle of the two semicolons and to the right of the semicolon this is really
214:42 right of the semicolon this is really the only other context we'll see
214:43 the only other context we'll see semicolons and it's weird normally it's
214:45 semicolons and it's weird normally it's been at the end of the line now it's two
214:47 been at the end of the line now it's two of them in the middle of the line but
214:48 of them in the middle of the line but this is the way humans decided years ago
214:50 this is the way humans decided years ago to do it so what is this doing almost
214:53 to do it so what is this doing almost the same thing it is going to initialize
214:56 the same thing it is going to initialize a variable called I to zero it's going
214:59 a variable called I to zero it's going to then check if it's less than three
215:03 to then check if it's less than three it's then going to do what's ever in the
215:05 it's then going to do what's ever in the curly braces is and it's lastly going to
215:07 curly braces is and it's lastly going to increment I and repeat so just
215:10 increment I and repeat so just highlighting in turn at first I is
215:12 highlighting in turn at first I is initialized to zero just like before
215:15 initialized to zero just like before then this condition is checked this is a
215:17 then this condition is checked this is a Boolean expression yes or no true or
215:19 Boolean expression yes or no true or false will be its answer and if I is
215:22 false will be its answer and if I is less than three which it should be once
215:23 less than three which it should be once it starts at zero well then we're going
215:25 it starts at zero well then we're going to go ahead and print out meow then I is
215:28 to go ahead and print out meow then I is going to get incremented so it starts at
215:29 going to get incremented so it starts at zero it goes now to one at that point
215:32 zero it goes now to one at that point the Boolean expression is checked again
215:34 the Boolean expression is checked again so you don't keep changing I back to
215:37 so you don't keep changing I back to zero that first step happens only once
215:40 zero that first step happens only once but now you repeat through those three
215:41 but now you repeat through those three other highlights I check if I is less
215:43 other highlights I check if I is less than three it is so I print out meow it
215:46 than three it is so I print out meow it then increments I I check if I now two
215:49 then increments I I check if I now two is less than three it is I print out
215:51 is less than three it is I print out meow I gets incremented I now check is I
215:54 meow I gets incremented I now check is I less than three no it's not because
215:56 less than three no it's not because three is not less than three and so the
215:58 three is not less than three and so the whole thing stops and whatever code is
216:00 whole thing stops and whatever code is below this curly brace if any starts
216:03 below this curly brace if any starts executing instead just like in scratch
216:04 executing instead just like in scratch you break out of the loop and the the
216:07 you break out of the loop and the the puzzle piece being hugged questions then
216:10 puzzle piece being hugged questions then about this alternative Syntax
216:13 about this alternative Syntax for Loops AKA a four
216:23 Loop sorry say again yeah can I explain again why it
216:25 again yeah can I explain again why it doesn't reset to zero honestly just
216:27 doesn't reset to zero honestly just because like this was the syntax they
216:29 because like this was the syntax they chose this first part before the first
216:31 chose this first part before the first semicolon is only executed once just
216:33 semicolon is only executed once just because that's how it's designed
216:35 because that's how it's designed everything else Cycles again and again
216:38 everything else Cycles again and again and this is just an alternative syntax
216:40 and this is just an alternative syntax to to using the slightly more lines of
216:41 to to using the slightly more lines of code it was like six lines of code using
216:43 code it was like six lines of code using the while loop logically it's the same
216:45 the while loop logically it's the same thing programmers once they get more
216:47 thing programmers once they get more comfortable tend to prefer this because
216:48 comfortable tend to prefer this because it just expresses all your same thoughts
216:50 it just expresses all your same thoughts more succinctly that's all yeah okay so
216:55 more succinctly that's all yeah okay so let's just work this into my meow
216:57 let's just work this into my meow example let me go back to the code here
216:59 example let me go back to the code here and notice indeed if I highlight all
217:00 and notice indeed if I highlight all these lines I think we can tighten this
217:02 these lines I think we can tighten this up let me get rid of all of those and
217:04 up let me get rid of all of those and instead do four in I equals 0 and I'm
217:08 instead do four in I equals 0 and I'm saying equals most programmers would say
217:10 saying equals most programmers would say gets so in I gets zero means assignment
217:13 gets so in I gets zero means assignment the word get now I'm going to do I is
217:16 the word get now I'm going to do I is less than three i+ plus now in here I'm
217:19 less than three i+ plus now in here I'm going to do my print F quote unquote
217:22 going to do my print F quote unquote meow back sln and so it's indeed a
217:25 meow back sln and so it's indeed a little tighter I mean two of the lines
217:26 little tighter I mean two of the lines are just curly braces there's really
217:28 are just curly braces there's really only two juicy lines of code now let me
217:30 only two juicy lines of code now let me go ahead and do make
217:32 go ahead and do make meow meow and again we're back in
217:35 meow meow and again we're back in business with three of them printing
217:37 business with three of them printing only all right there's one last
217:39 only all right there's one last structure we should explore just cuz
217:41 structure we should explore just cuz it's sometimes useful this was a forever
217:44 it's sometimes useful this was a forever block and this would be a little weird
217:46 block and this would be a little weird in scratch to just say meow Forever at
217:48 in scratch to just say meow Forever at least without waiting but there is
217:49 least without waiting but there is indeed a forever Block in scratch which
217:51 indeed a forever Block in scratch which means do the following forever and I
217:54 means do the following forever and I proposed I think verbally last week's at
217:56 proposed I think verbally last week's at least one example where this is useful
217:57 least one example where this is useful meowing forever little annoying but can
217:59 meowing forever little annoying but can you think of common cases where you
218:01 you think of common cases where you might want to write code or use a
218:03 might want to write code or use a program that Loops forever
218:07 program that Loops forever yeah yeah playing music like Spotify
218:10 yeah yeah playing music like Spotify playlist just repeating again and again
218:12 playlist just repeating again and again would be some kind of loop for
218:14 would be some kind of loop for collisions checking for collisions and
218:16 collisions checking for collisions and scratch so seeing if something's
218:17 scratch so seeing if something's bouncing off the wall or another Sprite
218:25 yeah checking for input so yeah get string is essentially just waiting there
218:27 string is essentially just waiting there forever for me to type in some input
218:28 forever for me to type in some input until I do the time checking the time
218:31 until I do the time checking the time and actually maintaining like human time
218:33 and actually maintaining like human time like a wall clock behind you was that
218:35 like a wall clock behind you was that the
218:36 the same okay checking the time and one more
218:39 same okay checking the time and one more detecting a key press too like in
218:40 detecting a key press too like in scratch just waiting for some kind of
218:42 scratch just waiting for some kind of event to happen just like on a phone or
218:44 event to happen just like on a phone or a browser and so there's so many
218:45 a browser and so there's so many examples where you might want to do
218:47 examples where you might want to do something forever just so you've seen
218:48 something forever just so you've seen the corresponding C building block it's
218:51 the corresponding C building block it's a little weird but this is probably the
218:53 a little weird but this is probably the most canonical way to do it in C if you
218:56 most canonical way to do it in C if you want to print meow forever which would
218:58 want to print meow forever which would be a little crazy because it literally
219:00 be a little crazy because it literally print and take over your computer
219:02 print and take over your computer printing forever meow you would
219:04 printing forever meow you would generally do it like this why well a
219:06 generally do it like this why well a while loop expects in parentheses a
219:08 while loop expects in parentheses a Boolean expression and a Boolean
219:09 Boolean expression and a Boolean expression is again a yes no a true
219:11 expression is again a yes no a true false question but if you want the
219:13 false question but if you want the answer to that question always to be yes
219:16 answer to that question always to be yes or really always to be true turns out in
219:18 or really always to be true turns out in C in a lot of languages well then just
219:20 C in a lot of languages well then just say true because true T R is never going
219:23 say true because true T R is never going to change magically to false I mean it's
219:25 to change magically to false I mean it's just a special word in the programming
219:27 just a special word in the programming language so by saying while true it just
219:29 language so by saying while true it just means do the following forever another
219:32 means do the following forever another common Paradigm before true and false
219:35 common Paradigm before true and false became common place would be to do uh
219:37 became common place would be to do uh this instead change uh while one you
219:41 this instead change uh while one you might see in online examples and texts
219:43 might see in online examples and texts and the like while one is really the
219:45 and the like while one is really the same thing any value that is zero is
219:48 same thing any value that is zero is generally interpreted as false by a
219:50 generally interpreted as false by a computer any value that is one or any
219:54 computer any value that is one or any other nonzero value is generally
219:56 other nonzero value is generally interpreted as true and so this too
219:58 interpreted as true and so this too would have the same effect saying while
220:00 would have the same effect saying while true or while one generally speaking
220:02 true or while one generally speaking while true is perhaps a little clearer
220:04 while true is perhaps a little clearer these days now meowing forever is not a
220:06 these days now meowing forever is not a good thing but suppose I did that by
220:09 good thing but suppose I did that by intent or by accident well let's try
220:11 intent or by accident well let's try this so here I'll go into my code I'm
220:13 this so here I'll go into my code I'm going to get rid of my for Loop and
220:14 going to get rid of my for Loop and change my while loop to uh how about
220:17 change my while loop to uh how about true um and in this case here well we'll
220:21 true um and in this case here well we'll keep it let's do this make meow enter
220:24 keep it let's do this make meow enter and you'll see this use of Undeclared
220:27 and you'll see this use of Undeclared identifier or true this is actually
220:28 identifier or true this is actually hinting at my mention that the old way
220:31 hinting at my mention that the old way was zero and one nowadays you could say
220:33 was zero and one nowadays you could say true or false but true and false are
220:36 true or false but true and false are themselves special words that you have
220:38 themselves special words that you have to include and it turns out if you want
220:41 to include and it turns out if you want to use special Boolean values like this
220:43 to use special Boolean values like this there's another header file we haven't
220:44 there's another header file we haven't seen called standard bu that essentially
220:47 seen called standard bu that essentially creates true and false as keywords
220:50 creates true and false as keywords alternatively cs50 includes that same
220:52 alternatively cs50 includes that same file so it's more common in cs50 to see
220:54 file so it's more common in cs50 to see it like this now if I clear my terminal
220:56 it like this now if I clear my terminal window and do make meow and then do/
220:59 window and do make meow and then do/ meow and hit enter well unfortunately
221:02 meow and hit enter well unfortunately this isn't the best thing to do uh
221:04 this isn't the best thing to do uh infinitely when you're in the cloud
221:06 infinitely when you're in the cloud using a browser this is indeed a a
221:09 using a browser this is indeed a a browser full screened here um this means
221:12 browser full screened here um this means I'm sending millions of meows over the
221:14 I'm sending millions of meows over the internet to my computer here uh so this
221:16 internet to my computer here uh so this will happen to you at some point
221:17 will happen to you at some point probably not with meow but you'll lose
221:19 probably not with meow but you'll lose control over your terminal window why
221:21 control over your terminal window why because you screwed up and like you have
221:22 because you screwed up and like you have an infinite Loop you didn't really
221:23 an infinite Loop you didn't really intend it or maybe you did you were
221:24 intend it or maybe you did you were curious to see what happens what do you
221:26 curious to see what happens what do you do like when when does the meowing stop
221:30 do like when when does the meowing stop what recourse do we have here all right
221:32 what recourse do we have here all right well control c will be your friend
221:35 well control c will be your friend sometimes you have to hit it a bunch in
221:36 sometimes you have to hit it a bunch in a cloud environment but control C for
221:38 a cloud environment but control C for cancel with will interrupt a program
221:41 cancel with will interrupt a program that's running and I promise that almost
221:43 that's running and I promise that almost all of you will at some point
221:45 all of you will at some point accidentally introduce an infinite Loop
221:47 accidentally introduce an infinite Loop because you're math is slightly off when
221:49 because you're math is slightly off when in doubt click in the terminal window
221:51 in doubt click in the terminal window and hit contrl C sometimes multiple
221:53 and hit contrl C sometimes multiple times and that will indeed cancel
221:55 times and that will indeed cancel whatever is happening there in this case
221:56 whatever is happening there in this case I might have intended it but sometimes
221:58 I might have intended it but sometimes it's not in fact intended all right so
222:01 it's not in fact intended all right so we've been taking for granted this whole
222:03 we've been taking for granted this whole graphical user interface for some time
222:05 graphical user interface for some time and indeed uh the uh commands that I'm
222:09 and indeed uh the uh commands that I'm typing and the buttons I'm clicking and
222:10 typing and the buttons I'm clicking and let me just give you a better sense of
222:11 let me just give you a better sense of what it is we are using underneath the
222:14 what it is we are using underneath the hood this whole time um namely an
222:16 hood this whole time um namely an operating system called Linux so I keep
222:18 operating system called Linux so I keep alluding verbally of course to Macs and
222:20 alluding verbally of course to Macs and PCs because almost all of us are running
222:22 PCs because almost all of us are running Mac OS or Windows on our desktops or
222:24 Mac OS or Windows on our desktops or laptops nowadays but there's lots of
222:26 laptops nowadays but there's lots of other operating systems out there and
222:27 other operating systems out there and one of the most popular one is called
222:29 one of the most popular one is called Linux and Linux is very often used on
222:32 Linux and Linux is very often used on servers nowadays companies that host
222:34 servers nowadays companies that host email companies that host websites or
222:36 email companies that host websites or apps more generally um certain computer
222:38 apps more generally um certain computer scientists or computer science students
222:40 scientists or computer science students often like to brag that they run Linux
222:42 often like to brag that they run Linux just because that's a thing um but it is
222:44 just because that's a thing um but it is really just an alternative to Mac OS or
222:46 really just an alternative to Mac OS or windows that provides you with both a
222:49 windows that provides you with both a guey if you want it but also and
222:51 guey if you want it but also and especially a command line environment
222:54 especially a command line environment now fun fact Windows and Mac OS do have
222:56 now fun fact Windows and Mac OS do have terminal windows or the equivalent
222:58 terminal windows or the equivalent thereof and eventually you might uh use
223:00 thereof and eventually you might uh use it on your own Mac or PC to solve some
223:02 it on your own Mac or PC to solve some problem but Linux is really known for
223:04 problem but Linux is really known for along with other operating systems it's
223:06 along with other operating systems it's command line environment which again I
223:08 command line environment which again I distinguished earlier from Guy as a
223:10 distinguished earlier from Guy as a command line interface or CLI and that
223:13 command line interface or CLI and that refers really to the terminal window so
223:16 refers really to the terminal window so if I go back to VSS code here and let me
223:18 if I go back to VSS code here and let me in fact go ahead and close my tab and
223:20 in fact go ahead and close my tab and focus entirely on the terminal window
223:22 focus entirely on the terminal window this terminal window is really just your
223:24 this terminal window is really just your command line interface to your very own
223:27 command line interface to your very own server in the cloud the term of art here
223:29 server in the cloud the term of art here is you each will have your own container
223:32 is you each will have your own container in the cloud which is like your own
223:33 in the cloud which is like your own computer running somewhere on the
223:35 computer running somewhere on the internet with your own username and
223:37 internet with your own username and password to which you have access and
223:39 password to which you have access and your own hard drive if you will your own
223:41 your own hard drive if you will your own home folder that has all of your files
223:43 home folder that has all of your files for the class and it's only accessible
223:45 for the class and it's only accessible to you unless you enable live sharing
223:47 to you unless you enable live sharing thereof so when you're typing commands
223:49 thereof so when you're typing commands here it looks like you're typing them of
223:51 here it looks like you're typing them of course on your own Mac or PC but they're
223:53 course on your own Mac or PC but they're actually being sent over the browser to
223:56 actually being sent over the browser to uh some server in the cloud where you
223:58 uh some server in the cloud where you are controlling really your own account
224:01 are controlling really your own account therein so it turns out that there are
224:04 therein so it turns out that there are other commands that are worth knowing
224:05 other commands that are worth knowing and we'll give you just a few of these
224:07 and we'll give you just a few of these today and over the coming weeks will you
224:09 today and over the coming weeks will you have opport ities to play with others as
224:11 have opport ities to play with others as well but these are kind of some of the
224:12 well but these are kind of some of the basics and they're all incredibly
224:14 basics and they're all incredibly succinct because indeed for things
224:16 succinct because indeed for things you're typing at the command line humans
224:17 you're typing at the command line humans generally have not wanted to type out
224:19 generally have not wanted to type out long commands so a lot of these are
224:21 long commands so a lot of these are abbreviations here now perhaps the most
224:23 abbreviations here now perhaps the most common one I'll start with first is LS a
224:26 common one I'll start with first is LS a lowercase l and a lowercase s that
224:28 lowercase l and a lowercase s that stands for succinctly list so if I go to
224:31 stands for succinctly list so if I go to my terminal window now where up until
224:33 my terminal window now where up until now I've only typed code which is a vs
224:36 now I've only typed code which is a vs code thing for creating and opening
224:37 code thing for creating and opening files and make which triggers the compil
224:39 files and make which triggers the compil ation of my code what if I now type LS
224:42 ation of my code what if I now type LS this will list all of the files in my
224:45 this will list all of the files in my current folder my hard drive in the
224:47 current folder my hard drive in the cloud if you will so if I hit enter
224:49 cloud if you will so if I hit enter you'll see a whole bunch of results now
224:52 you'll see a whole bunch of results now they're color-coded too the white ones
224:54 they're color-coded too the white ones here and in. c those are the source code
224:57 here and in. c those are the source code files I've written during class today
224:59 files I've written during class today agree. C compare. C hello.c and meow. C
225:02 agree. C compare. C hello.c and meow. C and you can perhaps guess the green ones
225:04 and you can perhaps guess the green ones here that just by convention have an
225:06 here that just by convention have an asterisk on the end to denote that
225:07 asterisk on the end to denote that they're special represent what
225:11 they're special represent what what are the four others
225:13 what are the four others yeah yeah the machine code so those are
225:16 yeah yeah the machine code so those are my actual programs that are identically
225:18 my actual programs that are identically named minus the C extension and the
225:21 named minus the C extension and the asterisk means that they're executable
225:23 asterisk means that they're executable that is in the world of Mac OS or
225:24 that is in the world of Mac OS or Windows you would double click but in
225:26 Windows you would double click but in the world of a command line environment
225:28 the world of a command line environment that means you do dot slash and then the
225:30 that means you do dot slash and then the name without the asterisk to execute or
225:32 name without the asterisk to execute or run the code therein so if I open up my
225:35 run the code therein so if I open up my file explorer and I'm hitting command B
225:37 file explorer and I'm hitting command B on my computer here just as a keyboard
225:39 on my computer here just as a keyboard shortcut you'll see the exact same thing
225:41 shortcut you'll see the exact same thing so LS is the command line interface for
225:44 so LS is the command line interface for listing the files in your account but
225:46 listing the files in your account but here because I'm using vs code or any
225:48 here because I'm using vs code or any program like it I also get a graphical
225:51 program like it I also get a graphical user interface as well so it's just two
225:53 user interface as well so it's just two different places to be you're welcome to
225:55 different places to be you're welcome to use whatever you're comfortable with but
225:56 use whatever you're comfortable with but over time will you naturally get more
225:58 over time will you naturally get more comfortable and capable with the
225:59 comfortable and capable with the terminal window alone well what else is
226:02 terminal window alone well what else is on this this list here well during the
226:04 on this this list here well during the break I saw that at least one of you for
226:05 break I saw that at least one of you for instance had created a file called hello
226:08 instance had created a file called hello instead of hello. so you were in a
226:10 instead of hello. so you were in a situation where you did this
226:12 situation where you did this accidentally and hit enter and then you
226:14 accidentally and hit enter and then you went ahead and typed in all of your code
226:16 went ahead and typed in all of your code like this and then down in your terminal
226:18 like this and then down in your terminal window you were trying to do make hello
226:20 window you were trying to do make hello enter and this now didn't actually do
226:24 enter and this now didn't actually do anything like I can't I'm hitting um I'm
226:27 anything like I can't I'm hitting um I'm trying to run the command I got
226:29 trying to run the command I got permission denied as at least one of you
226:31 permission denied as at least one of you did now why is that well let's just do a
226:32 did now why is that well let's just do a quick check if I do LS I see now hello
226:36 quick check if I do LS I see now hello but hello has no asteris next to it
226:38 but hello has no asteris next to it which means it's not executable that's
226:40 which means it's not executable that's my code why well notice the top of my
226:42 my code why well notice the top of my tab confirms oh I screwed up I didn't
226:44 tab confirms oh I screwed up I didn't name my file hello.c which it just has
226:46 name my file hello.c which it just has to be so what do you do well you could
226:48 to be so what do you do well you could very hackish like copy this create a new
226:51 very hackish like copy this create a new file paste it in or no no no like we
226:53 file paste it in or no no no like we know how to rename things now here
226:55 know how to rename things now here because that's one of our options let me
226:56 because that's one of our options let me do this let me do MV for move hello and
227:01 do this let me do MV for move hello and then hello.c and hit enter you'll see
227:04 then hello.c and hit enter you'll see the tab closes cuz hello no longer
227:06 the tab closes cuz hello no longer exists but if I now now type LS you'll
227:09 exists but if I now now type LS you'll see ah there is hello.c and if I open
227:12 see ah there is hello.c and if I open that file now there's all of my same
227:15 that file now there's all of my same code and now if I do make hello make
227:18 code and now if I do make hello make hello now I do get an executable file
227:21 hello now I do get an executable file wherein the world is restored so MV is
227:24 wherein the world is restored so MV is just a command not just for renaming but
227:26 just a command not just for renaming but it also turns out eventually for moving
227:28 it also turns out eventually for moving files as well you can also create
227:30 files as well you can also create directories or folders so for instance
227:32 directories or folders so for instance if I go into vs code again and suppose I
227:35 if I go into vs code again and suppose I hover over here and click not on the
227:37 hover over here and click not on the plus file icon but plus folder I can
227:39 plus file icon but plus folder I can create a folder called for instance like
227:41 create a folder called for instance like pset one for problem set one in the
227:43 pset one for problem set one in the class and you'll see now that it's empty
227:45 class and you'll see now that it's empty CU all of my other files are in the
227:47 CU all of my other files are in the default folder of my account but I could
227:50 default folder of my account but I could also go in there like this and I could
227:52 also go in there like this and I could click on file and now I can create a new
227:55 click on file and now I can create a new file called like mario.c which is uh one
227:58 file called like mario.c which is uh one of the first problems for instance but
228:00 of the first problems for instance but you'll notice now that
228:02 you'll notice now that mario.c is inside of the pet one folder
228:05 mario.c is inside of the pet one folder so if I zoom out and I type LS at my
228:08 so if I zoom out and I type LS at my terminal window
228:10 terminal window I won't see mario. C anywhere but I do
228:13 I won't see mario. C anywhere but I do see a pet one folder and it's in light
228:16 see a pet one folder and it's in light blue followed by a slash which you don't
228:17 blue followed by a slash which you don't have to type it just indicates that's a
228:19 have to type it just indicates that's a folder now I can visually at top left
228:21 folder now I can visually at top left obviously see pet one contains mario.c
228:24 obviously see pet one contains mario.c but if I try to do something like make
228:26 but if I try to do something like make Mario here no rule to make Target Mario
228:29 Mario here no rule to make Target Mario like it just doesn't seem to exist and
228:31 like it just doesn't seem to exist and that's because you're in the wrong
228:32 that's because you're in the wrong directory so in a command line interface
228:34 directory so in a command line interface it's not quite as simple as just
228:35 it's not quite as simple as just clicking on a folder and voila it opens
228:38 clicking on a folder and voila it opens you have to change into the directory or
228:41 you have to change into the directory or folder and CD is going to be the command
228:43 folder and CD is going to be the command there so if I want to actually change
228:45 there so if I want to actually change into that directory I can do CD space
228:48 into that directory I can do CD space pet one enter and now you'll see my
228:51 pet one enter and now you'll see my prompt changes and this is just a common
228:53 prompt changes and this is just a common convention but it's not the only one out
228:55 convention but it's not the only one out there now I still have a dollar sign
228:57 there now I still have a dollar sign which indicates where I can type
228:58 which indicates where I can type commands but before it I see a reminder
229:01 commands but before it I see a reminder constantly what folder I'm in and we put
229:03 constantly what folder I'm in and we put that there deliberately like a lot of
229:05 that there deliberately like a lot of Linux users do just to remind themselves
229:07 Linux users do just to remind themselves where they are cuz unlike Mac OS where
229:09 where they are cuz unlike Mac OS where you or Windows where you have a nice big
229:11 you or Windows where you have a nice big window telling you where you are at the
229:13 window telling you where you are at the command line you kind of need to be
229:14 command line you kind of need to be reminded textually but now if I type LS
229:17 reminded textually but now if I type LS and hit enter what should I
229:20 and hit enter what should I see yeah mario.c and now if I want to
229:23 see yeah mario.c and now if I want to open it uh if I want to uh actually
229:26 open it uh if I want to uh actually compile it I can run make Mario in this
229:28 compile it I can run make Mario in this directory once I actually type out all
229:29 directory once I actually type out all the code rest assured that in problem
229:32 the code rest assured that in problem sets in Labs we almost always certainly
229:34 sets in Labs we almost always certainly in the first weeks of the class give you
229:35 in the first weeks of the class give you exactly the commands to type odds are
229:38 exactly the commands to type odds are because it's new to many of you view you
229:39 because it's new to many of you view you will accidentally type the wrong
229:41 will accidentally type the wrong commands no big deal just remember that
229:43 commands no big deal just remember that you have different ways to solve these
229:45 you have different ways to solve these problems you've got like the graphical
229:46 problems you've got like the graphical file explorer which should feel a little
229:47 file explorer which should feel a little more familiar but in time you'll start
229:50 more familiar but in time you'll start to know and honestly probably prefer
229:52 to know and honestly probably prefer commands like these so CD for change
229:54 commands like these so CD for change directory CP for copy a file uh LS for
229:58 directory CP for copy a file uh LS for list MK dirt to make a directory create
230:01 list MK dirt to make a directory create a new folder at the command line instead
230:03 a new folder at the command line instead of with the button MV for move or rename
230:06 of with the button MV for move or rename RM
230:08 RM for remove so be careful with that one
230:11 for remove so be careful with that one RM dur remove directory and there's
230:13 RM dur remove directory and there's dozens hundreds of other commands you
230:15 dozens hundreds of other commands you won't need many of them but we'll start
230:17 won't need many of them but we'll start to scratch the surface all the more over
230:19 to scratch the surface all the more over time but ultimately this command line
230:21 time but ultimately this command line interface is going to be a more powerful
230:23 interface is going to be a more powerful mechanism a more capable mechanism and
230:25 mechanism a more capable mechanism and ultimately a more efficient mechanism
230:26 ultimately a more efficient mechanism for writing code running commands uh
230:28 for writing code running commands uh solving problems analyzing data more
230:30 solving problems analyzing data more generally even though no there's going
230:32 generally even though no there's going to be some uh some Growing Pains early
230:35 to be some uh some Growing Pains early on just because it's probably so new for
230:37 on just because it's probably so new for many of you so with that said we have
230:40 many of you so with that said we have some problems still to solve but we
230:41 some problems still to solve but we promised cookies today so let's go ahead
230:43 promised cookies today so let's go ahead and take a 10-minute break cookies are
230:45 and take a 10-minute break cookies are now served in the transcept and we'll be
230:47 now served in the transcept and we'll be back here in
230:48 back here in 10 all right we are back and up until
230:52 10 all right we are back and up until now each of the code examples in C we've
230:54 now each of the code examples in C we've done if been kind of designed to show
230:56 done if been kind of designed to show one specific topic but we thought we'd
230:57 one specific topic but we thought we'd try to take a step back and solve a more
230:59 try to take a step back and solve a more General problem and give you a sense of
231:01 General problem and give you a sense of when given a problem set for instance or
231:04 when given a problem set for instance or just a programming problem more
231:05 just a programming problem more generally like where you even begin and
231:07 generally like where you even begin and how you go about approaching it when
231:08 how you go about approaching it when it's not obvious what the point of the
231:11 it's not obvious what the point of the exercise is so one of my favorite games
231:13 exercise is so one of my favorite games from yester year is this one here Super
231:15 from yester year is this one here Super Mario Brothers that has come in so many
231:17 Mario Brothers that has come in so many different forms since but in this
231:18 different forms since but in this original uh two-dimensional Sid scroller
231:21 original uh two-dimensional Sid scroller game there was a lot of artwork like
231:23 game there was a lot of artwork like this so for instance up here in the sky
231:25 this so for instance up here in the sky were four question marks and we'll find
231:27 were four question marks and we'll find that in C in a lot of programming
231:29 that in C in a lot of programming languages initially it's a lot easier a
231:32 languages initially it's a lot easier a lot more accessible to focus really on
231:33 lot more accessible to focus really on black and white type interactive
231:35 black and white type interactive programs textually as opposed to
231:36 programs textually as opposed to full-fledged graphics and the like but
231:38 full-fledged graphics and the like but more on the more Gra graphical acoustic
231:40 more on the more Gra graphical acoustic uh type of programs before long but for
231:42 uh type of programs before long but for now let me go over and propose that we
231:45 now let me go over and propose that we try to just Implement an asy art aski
231:47 try to just Implement an asy art aski again being the code that Maps numbers
231:49 again being the code that Maps numbers to letters at least for English um into
231:52 to letters at least for English um into a textual version of these four question
231:54 a textual version of these four question marks in the sky so for this let me go
231:56 marks in the sky so for this let me go over to vs code I'll create my own
231:58 over to vs code I'll create my own version of mario.c that will be
232:00 version of mario.c that will be different from the one you're challenged
232:02 different from the one you're challenged with in problem set one indeed in
232:04 with in problem set one indeed in problem set one you'll be challenged to
232:05 problem set one you'll be challenged to build a little something like this
232:07 build a little something like this albeit with hashtags uh for asky art
232:10 albeit with hashtags uh for asky art instead of graphics and in mario.c I
232:12 instead of graphics and in mario.c I want to just solve this simple problem
232:14 want to just solve this simple problem first so it's all involving output so
232:16 first so it's all involving output so I'll do include standard i.h so I can
232:18 I'll do include standard i.h so I can use printf I'll do my int main void more
232:21 use printf I'll do my int main void more on why we keep doing that in future
232:23 on why we keep doing that in future weeks and I'm just going to do something
232:24 weeks and I'm just going to do something simple initially like 1 2 3 4 back sln
232:28 simple initially like 1 2 3 4 back sln this is about the simplest way I can
232:31 this is about the simplest way I can Implement four question marks in the sky
232:34 Implement four question marks in the sky like these here using pure text like
232:36 like these here using pure text like this so let me go ahead and do make
232:38 this so let me go ahead and do make Mario / Mario and voila we have those
232:41 Mario / Mario and voila we have those four question marks but we've seen of
232:43 four question marks but we've seen of course that there are better ways to do
232:45 course that there are better ways to do this and if you wanted to generalize
232:47 this and if you wanted to generalize this to be five question marks six 60
232:50 this to be five question marks six 60 different question marks you know Loop
232:51 different question marks you know Loop was always the answer for not repeating
232:54 was always the answer for not repeating ourselves so maybe I should rewrite this
232:57 ourselves so maybe I should rewrite this a little bit more flexibly and say
232:58 a little bit more flexibly and say something like this four in I gets zero
233:01 something like this four in I gets zero I less than four I plus plus and then
233:04 I less than four I plus plus and then inside of the for Loop now I can just do
233:06 inside of the for Loop now I can just do a single question mark But I don't think
233:10 a single question mark But I don't think what I've just done is correct anyone
233:12 what I've just done is correct anyone spot the aesthetic bug
233:15 spot the aesthetic bug already yeah why is this why is this
233:18 already yeah why is this why is this wrong if I want to print the same thing
233:22 wrong if I want to print the same thing [Music]
233:24 [Music] yeah yeah so I don't think I want to
233:26 yeah yeah so I don't think I want to backs slash in after every question mark
233:28 backs slash in after every question mark because the goal is again this like row
233:29 because the goal is again this like row of question marks in the sky so if I now
233:32 of question marks in the sky so if I now recompile this make
233:34 recompile this make Mario Mario okay it's almost there but
233:37 Mario Mario okay it's almost there but now I have that regression to where the
233:39 now I have that regression to where the dollar sign's not on its own line so I
233:42 dollar sign's not on its own line so I think I need a new line but I don't
233:43 think I need a new line but I don't think I want it here cuz that was not
233:45 think I want it here cuz that was not going to end well where do I want it
233:48 going to end well where do I want it instead any Instinct
233:51 instead any Instinct yeah yeah so outside the for Loop so
233:54 yeah yeah so outside the for Loop so indeed I can just go below line eight
233:56 indeed I can just go below line eight and above line nine creating a new one
233:58 and above line nine creating a new one and now there's it's totally fine to
234:00 and now there's it's totally fine to just print a new line like that you
234:02 just print a new line like that you don't have to print anything else with
234:03 don't have to print anything else with it it's indeed a character unto itself
234:05 it it's indeed a character unto itself so let's do make Mario one last time/
234:08 so let's do make Mario one last time/ Mario okay so now we're back in business
234:10 Mario okay so now we're back in business there well what if we wanted to do some
234:11 there well what if we wanted to do some other scene from Mario uh such as this
234:14 other scene from Mario uh such as this one here where there's a lot of vertical
234:15 one here where there's a lot of vertical obstacles like the These Bricks here if
234:18 obstacles like the These Bricks here if I wanted to print out now a column of
234:20 I wanted to print out now a column of three bricks and I'll use hashtags for
234:22 three bricks and I'll use hashtags for these instead of anything graphical well
234:24 these instead of anything graphical well I think we're almost there right like I
234:27 I think we're almost there right like I think I can now it's almost maybe a
234:29 think I can now it's almost maybe a little easier I can go back here change
234:32 little easier I can go back here change the question mark to something that
234:33 the question mark to something that looks more like a brick like this hash
234:35 looks more like a brick like this hash symbol and I think now I do want the new
234:37 symbol and I think now I do want the new line character because when I now do
234:39 line character because when I now do make Mario Mario okay there's my wall of
234:42 make Mario Mario okay there's my wall of four oh but wait I didn't want four I
234:44 four oh but wait I didn't want four I wanted to be consistent just with this
234:46 wanted to be consistent just with this particular scene here so I just want
234:48 particular scene here so I just want three so I can still change it in one
234:50 three so I can still change it in one place and here again is that Paradigm
234:52 place and here again is that Paradigm even whether you're using four or three
234:55 even whether you're using four or three if you get into the habit of starting
234:57 if you get into the habit of starting counting from zero you go on up to but
234:59 counting from zero you go on up to but not through the value you want to count
235:01 not through the value you want to count up to so that's why I'm using less than
235:04 up to so that's why I'm using less than instead of less than or equals to there
235:06 instead of less than or equals to there so this would be the common Paradigm
235:08 so this would be the common Paradigm though you could certainly count it like
235:09 though you could certainly count it like we saw earlier in different ways but
235:12 we saw earlier in different ways but what if things escalate one level
235:14 what if things escalate one level further and when you're in the
235:15 further and when you're in the underground version of Super Mario
235:17 underground version of Super Mario Brothers there's a lot of these
235:18 Brothers there's a lot of these underground obstructions including like
235:20 underground obstructions including like grids of bricks like this and let me
235:22 grids of bricks like this and let me conjecture that if you slice this up
235:24 conjecture that if you slice this up it's roughly a 3X3 grid of bricks that
235:27 it's roughly a 3X3 grid of bricks that all interlock uh prettily to give us
235:29 all interlock uh prettily to give us just one big large brick like this so if
235:32 just one big large brick like this so if I want to print out a 3X3 grid now
235:35 I want to print out a 3X3 grid now things are getting a little more
235:36 things are getting a little more interesting because up until now I
235:38 interesting because up until now I printed either one row horizontally or
235:41 printed either one row horizontally or one column vertically but we haven't
235:43 one column vertically but we haven't really seen any code where I'm sort of
235:45 really seen any code where I'm sort of printing or living in two different
235:47 printing or living in two different dimensions like the game would imply but
235:49 dimensions like the game would imply but let me propose that we could do this let
235:52 let me propose that we could do this let me go ahead and say all right suppose I
235:54 me go ahead and say all right suppose I want to print a 3X3 grid of bricks it's
235:57 want to print a 3X3 grid of bricks it's really that I want to print what three
236:01 really that I want to print what three rows of bricks like a grid is three rows
236:04 rows of bricks like a grid is three rows so if I take the highle idea and reduce
236:06 so if I take the highle idea and reduce it to something a little simpler how do
236:08 it to something a little simpler how do I do that well let me get rid of the
236:09 I do that well let me get rid of the print for a moment as I did and let me
236:11 print for a moment as I did and let me just stipulate that this for Loop even
236:13 just stipulate that this for Loop even though it doesn't do anything useful yet
236:15 though it doesn't do anything useful yet will do something how many times just by
236:18 will do something how many times just by Design all right three times right this
236:21 Design all right three times right this for Loop is good to go it will do
236:22 for Loop is good to go it will do something three times by just using I to
236:25 something three times by just using I to do the counting all right well if I want
236:26 do the counting all right well if I want to print out now a row of three bricks
236:30 to print out now a row of three bricks all on the same line that's pretty
236:32 all on the same line that's pretty similar to what we did earlier when I
236:34 similar to what we did earlier when I just wanted to print out four question
236:36 just wanted to print out four question marks in the sky so we've kind of seen a
236:38 marks in the sky so we've kind of seen a solution there and I dare say we can
236:40 solution there and I dare say we can compose one into the other so if I want
236:42 compose one into the other so if I want to print out a row of bricks I could
236:45 to print out a row of bricks I could just do this four in I gets zero I less
236:49 just do this four in I gets zero I less than three I ++ and then inside of this
236:53 than three I ++ and then inside of this inner loop if you will let me print out
236:55 inner loop if you will let me print out a single brick like this and then I I
237:00 a single brick like this and then I I don't like where this is going yet but I
237:02 don't like where this is going yet but I think I've taken two ideas and I've
237:03 think I've taken two ideas and I've combined them but what might be
237:05 combined them but what might be problematic about lines five and seven
237:07 problematic about lines five and seven at the moment
237:09 at the moment what might be bad here uh yeah and
237:16 back yeah I'm using the same integer ey which I feel like could get me into
237:18 which I feel like could get me into trouble right if I'm sort of trying to
237:19 trouble right if I'm sort of trying to count three things here but then I'm
237:21 count three things here but then I'm hijacking this variable and using it
237:23 hijacking this variable and using it inside of the loop like I feel like I
237:25 inside of the loop like I feel like I should avoid this this Collision of
237:27 should avoid this this Collision of names and so what's a good alternative
237:30 names and so what's a good alternative to I well a programmer if nesting Loops
237:32 to I well a programmer if nesting Loops in this way would pretty commonly go
237:33 in this way would pretty commonly go with J you could certainly change this
237:35 with J you could certainly change this to be like rows and columns if you want
237:38 to be like rows and columns if you want more descriptive variables but I and J
237:39 more descriptive variables but I and J is pretty canonical so I'm going to go
237:42 is pretty canonical so I'm going to go ahead and do this j++ instead of i++
237:45 ahead and do this j++ instead of i++ everywhere and let me try compiling this
237:46 everywhere and let me try compiling this so make Mario
237:48 so make Mario enter
237:50 enter Mario okay so a couple of things are
237:52 Mario okay so a couple of things are wrong here this is not a 3X3 grid but if
237:55 wrong here this is not a 3X3 grid but if you count these things how many did I
237:56 you count these things how many did I indeed print at
237:58 indeed print at least can probably just guess logically
238:01 least can probably just guess logically yeah there's nine hashes there
238:03 yeah there's nine hashes there unfortunately they're all on the same
238:05 unfortunately they're all on the same line instead of on three different lines
238:08 line instead of on three different lines so where logically can I fix this I'm
238:11 so where logically can I fix this I'm definitely printing all the bricks
238:13 definitely printing all the bricks they're just not on the right levels
238:21 yeah yeah so put a new line after the first Loop this inner loop if you will
238:23 first Loop this inner loop if you will the nested Loop if you will so let me go
238:24 the nested Loop if you will so let me go ahead and print out just a back slash n
238:27 ahead and print out just a back slash n here and what's this doing well I think
238:29 here and what's this doing well I think that's going to solve it by just moving
238:30 that's going to solve it by just moving the cursor to the next line after you've
238:32 the cursor to the next line after you've done one row so let me go ahead and do
238:34 done one row so let me go ahead and do make Mario enter SL Mario and now we're
238:37 make Mario enter SL Mario and now we're in business so it's a very simplistic
238:39 in business so it's a very simplistic version of the same graphic but I'm
238:41 version of the same graphic but I'm leveraging two different ideas now the
238:43 leveraging two different ideas now the same or the same idea twice rather now
238:46 same or the same idea twice rather now I'm using one Loop to kind of control my
238:48 I'm using one Loop to kind of control my cursor going row by row by row but then
238:50 cursor going row by row by row but then within that Loop I'm doing left to right
238:52 within that Loop I'm doing left to right do do dot dot dot with printing out each
238:55 do do dot dot dot with printing out each of these individual bricks like this now
238:57 of these individual bricks like this now there's a little sloppiness here still
239:00 there's a little sloppiness here still like if I want this to always be a
239:01 like if I want this to always be a square just because that's what it looks
239:03 square just because that's what it looks like in the game well I could change it
239:05 like in the game well I could change it to be a 4x4 uh Square by doing this or a
239:09 to be a 4x4 uh Square by doing this or a 5x5 grid whoops by doing this why is
239:13 5x5 grid whoops by doing this why is this perhaps not the best design to just
239:15 this perhaps not the best design to just keep changing the numbers when I want to
239:16 keep changing the numbers when I want to change the
239:19 change the size where could this go AR
239:26 yeah yeah if it's always going to be a square and height is going to be the
239:27 square and height is going to be the same as width I'm just inviting trouble
239:29 same as width I'm just inviting trouble here right eventually I'm going to screw
239:30 here right eventually I'm going to screw up I'm going to change one but not the
239:31 up I'm going to change one but not the other then it's going to come out to be
239:33 other then it's going to come out to be a rectangle instead of a proper Square
239:34 a rectangle instead of a proper Square so I should probably solve this a little
239:36 so I should probably solve this a little differently so let me do that at the top
239:38 differently so let me do that at the top of my main function here let me go ahead
239:40 of my main function here let me go ahead and give myself a variable called maybe
239:43 and give myself a variable called maybe uh n for the number of bricks I want
239:45 uh n for the number of bricks I want horizontally and vertically and I'll
239:48 horizontally and vertically and I'll just initialize that to three initially
239:50 just initialize that to three initially and instead of putting three here I'll
239:52 and instead of putting three here I'll literally just use n but I'll do it in
239:53 literally just use n but I'll do it in both places so that now henceforth if I
239:55 both places so that now henceforth if I ever want to change this and change it
239:57 ever want to change this and change it to four or five or anything else like
239:59 to four or five or anything else like I'm all done like it's better designed
240:01 I'm all done like it's better designed because there's a lower probability of
240:02 because there's a lower probability of mistakes but I could technically SK
240:04 mistakes but I could technically SK still screw up somehow I could
240:06 still screw up somehow I could technically accidentally write line of
240:08 technically accidentally write line of code like n++ or you know I could just
240:12 code like n++ or you know I could just change the value of that variable even
240:13 change the value of that variable even though I don't want it to ever change
240:15 though I don't want it to ever change and maybe it's because I'm a bad
240:16 and maybe it's because I'm a bad programmer I copy pasted wrong I'm
240:18 programmer I copy pasted wrong I'm working with someone who doesn't know
240:19 working with someone who doesn't know what n represents I can defend myself
240:22 what n represents I can defend myself and my code against human error like
240:25 and my code against human error like that by going up here to line five and
240:27 that by going up here to line five and instead of just declaring a simple
240:29 instead of just declaring a simple variable like we did in scratch I can
240:31 variable like we did in scratch I can further Harden my code so to speak by
240:34 further Harden my code so to speak by declaring it to be a constant using the
240:36 declaring it to be a constant using the keyword const now this is just a feature
240:38 keyword const now this is just a feature of c and some other languages to protect
240:41 of c and some other languages to protect you against Yourself by proactively
240:43 you against Yourself by proactively saying N is a constant specifically the
240:46 saying N is a constant specifically the number five or previously the number
240:48 number five or previously the number three you cannot accidentally write code
240:50 three you cannot accidentally write code elsewhere that changes it the computer
240:52 elsewhere that changes it the computer will throw an error and catch that error
240:54 will throw an error and catch that error so it's just a way of programming a
240:56 so it's just a way of programming a little more defensively um some
240:59 little more defensively um some languages have this some languages don't
241:00 languages have this some languages don't but in general it's a good practice it
241:02 but in general it's a good practice it makes your code better designed because
241:04 makes your code better designed because it just is less vulnerable to Mistakes
241:07 it just is less vulnerable to Mistakes by you colleagues or anyone else using
241:09 by you colleagues or anyone else using the code so let me change this back to
241:11 the code so let me change this back to three just to be our default but now I'm
241:13 three just to be our default but now I'm using n in both places and if I do make
241:16 using n in both places and if I do make mario. Mario we're back to where we
241:19 mario. Mario we're back to where we originally started but the code's a
241:20 originally started but the code's a little more better design and let me
241:22 little more better design and let me note this too all this time I've been
241:24 note this too all this time I've been mentioning that uh there's correctness
241:26 mentioning that uh there's correctness is important design is important there
241:28 is important design is important there was also this matter of style I've been
241:30 was also this matter of style I've been very deliberately writing pretty code if
241:33 very deliberately writing pretty code if you will not just the syntax
241:34 you will not just the syntax highlighting which is automatic but
241:35 highlighting which is automatic but notice that I keep indenting everything
241:38 notice that I keep indenting everything nicely anytime I have curly braces like
241:40 nicely anytime I have curly braces like on lines 4 and 14 everything is indented
241:43 on lines 4 and 14 everything is indented one level when I have additional curly
241:45 one level when I have additional curly braces on line 7 and 13 everything is
241:48 braces on line 7 and 13 everything is nicely indented uh as well technically
241:51 nicely indented uh as well technically speaking the computer does not care
241:53 speaking the computer does not care about that kind of white space so to
241:56 about that kind of white space so to speak and you could really make a mess
241:58 speak and you could really make a mess of things like this because you have a
242:00 of things like this because you have a strange sense of style or just because
242:02 strange sense of style or just because you're being a little sloppy but this
242:04 you're being a little sloppy but this code is actually still correct if I
242:07 code is actually still correct if I recompile it let me open up my terminal
242:09 recompile it let me open up my terminal window make uh Mario no errors Mario it
242:13 window make uh Mario no errors Mario it works perfectly fine but you can imagine
242:15 works perfectly fine but you can imagine just how annoying this now is to read
242:18 just how annoying this now is to read like certainly for a TA U but certainly
242:20 like certainly for a TA U but certainly for you the next day certainly for a
242:22 for you the next day certainly for a colleague who has to read your code this
242:23 colleague who has to read your code this is just bad style like it still works
242:25 is just bad style like it still works and it's welld designed in that like
242:27 and it's welld designed in that like you're uh writing code defensively
242:29 you're uh writing code defensively you're using a constant but my God the
242:31 you're using a constant but my God the style is atrocious now you'll often find
242:34 style is atrocious now you'll often find that there's tools that can help you
242:35 that there's tools that can help you format your code for you in a manner
242:37 format your code for you in a manner consistent with a courses or a company's
242:39 consistent with a courses or a company's uh style but this is the kind of muscle
242:41 uh style but this is the kind of muscle memory you'll want to develop over time
242:43 memory you'll want to develop over time to take vs code suggestions as it's
242:45 to take vs code suggestions as it's outputting lines of code for you because
242:47 outputting lines of code for you because it's trying to format your code in a
242:49 it's trying to format your code in a readable way and oh my God if and when
242:52 readable way and oh my God if and when you do have bugs in your code and things
242:53 you do have bugs in your code and things aren't even indented properly there's no
242:55 aren't even indented properly there's no way you the human are going to be able
242:57 way you the human are going to be able to wrap your mind around what's
242:59 to wrap your mind around what's happening and where like you're just
243:00 happening and where like you're just making the problem harder for yourself
243:02 making the problem harder for yourself so do get into this habit too of
243:04 so do get into this habit too of manifesting good style as well all right
243:08 manifesting good style as well all right well let me propose that we don't only
243:09 well let me propose that we don't only want a 3X3 grid we want this to be a
243:12 want a 3X3 grid we want this to be a little more Dynamic so suppose we moved
243:15 little more Dynamic so suppose we moved away from a constant to just using an
243:17 away from a constant to just using an integer called n and let's ask the user
243:20 integer called n and let's ask the user for the size of this grid as by
243:22 for the size of this grid as by prompting them with get int as we've
243:24 prompting them with get int as we've done before and I'll store it in N here
243:27 done before and I'll store it in N here and then I can go ahead and more
243:28 and then I can go ahead and more dynamically run make Mario to compile it
243:32 dynamically run make Mario to compile it whoops oh I screwed up
243:35 whoops oh I screwed up accidentally what is it in suggesting I
243:38 accidentally what is it in suggesting I do albeit
243:44 cryptically yeah I forgot to include the cs50 header file up top and that's why
243:47 cs50 header file up top and that's why it doesn't know that get int is in fact
243:49 it doesn't know that get int is in fact valid so that's an easy fix U I'm just
243:51 valid so that's an easy fix U I'm just going to go up here and include
243:53 going to go up here and include cs50.h now I'm going to clear my
243:55 cs50.h now I'm going to clear my terminal and rerun make Mario now we're
243:57 terminal and rerun make Mario now we're good Mario and now notice I'm prompted
244:00 good Mario and now notice I'm prompted for size so if I type in three it's the
244:02 for size so if I type in three it's the same as before if I type in 10 it's even
244:04 same as before if I type in 10 it's even bigger but it happens all now
244:06 bigger but it happens all now automatically but there are some things
244:08 automatically but there are some things that we're not detecting for instance
244:10 that we're not detecting for instance suppose I type in cat well that's
244:13 suppose I type in cat well that's handled by the get it function as I
244:14 handled by the get it function as I claimed earlier that's one of the
244:15 claimed earlier that's one of the features of using a library you don't
244:16 features of using a library you don't have to deal with erroneous input but we
244:19 have to deal with erroneous input but we only designed a function called get int
244:21 only designed a function called get int to get you an integer we don't know if
244:23 to get you an integer we don't know if you want it to be positive negative zero
244:25 you want it to be positive negative zero or some combination thereof and it's
244:27 or some combination thereof and it's kind of weird to allow the user to type
244:29 kind of weird to allow the user to type in like negative one for the size of the
244:31 in like negative one for the size of the Grid or you know ne3 for the size of the
244:34 Grid or you know ne3 for the size of the grid and indeed your code does nothing
244:36 grid and indeed your code does nothing so at least it's not crashing but that's
244:38 so at least it's not crashing but that's kind of stupid right like it'd be nice
244:39 kind of stupid right like it'd be nice to force the user if they want a grid to
244:41 to force the user if they want a grid to give us a positive value so how could we
244:44 give us a positive value so how could we do this well I could go up here and I
244:47 do this well I could go up here and I could say something like if n is less
244:49 could say something like if n is less than one so if it's zero or negative
244:52 than one so if it's zero or negative which I don't want what could I do well
244:54 which I don't want what could I do well I could say well prompt the user again
244:57 I could say well prompt the user again for the size and now notice I'm not
245:00 for the size and now notice I'm not declaring and again because once it
245:02 declaring and again because once it exists you don't have to mention the
245:04 exists you don't have to mention the data type again we said that earlier but
245:06 data type again we said that earlier but this is kind of stupid why because now
245:09 this is kind of stupid why because now when You' given the user a second chance
245:11 when You' given the user a second chance okay now maybe I'll do all right if this
245:13 okay now maybe I'll do all right if this version of n is less than one well let's
245:16 version of n is less than one well let's just go and prompt the user a third time
245:19 just go and prompt the user a third time I mean you can kind of see where this is
245:20 I mean you can kind of see where this is stupidly going like this can't be the
245:22 stupidly going like this can't be the right solution to keep typing
245:24 right solution to keep typing recursively the same thing again and
245:26 recursively the same thing again and again like where would it stop you'd
245:27 again like where would it stop you'd have to give them a finite number of
245:28 have to give them a finite number of chances or just you know make a mess of
245:30 chances or just you know make a mess of your code so what would be intuitively a
245:33 your code so what would be intuitively a better solution
245:35 better solution here yeah so some kind of loop we've
245:37 here yeah so some kind of loop we've seen a while loop we've seen a four Loop
245:39 seen a while loop we've seen a four Loop so maybe one of those so let me try this
245:41 so maybe one of those so let me try this let me delete this messiness and just go
245:44 let me delete this messiness and just go back to the first question and let me do
245:45 back to the first question and let me do this so while n is less than one so
245:49 this so while n is less than one so while the number is not what we want
245:51 while the number is not what we want let's just prompt the user in a loop
245:53 let's just prompt the user in a loop this time for the size again now here
245:57 this time for the size again now here too this is better because it's only two
246:00 too this is better because it's only two requests for information but clearly
246:02 requests for information but clearly line six and N are pretty much identical
246:05 line six and N are pretty much identical other than the int and if I went in and
246:07 other than the int and if I went in and changed the size you know uh if I add
246:10 changed the size you know uh if I add this if I change the wording here change
246:11 this if I change the wording here change it to a different language like I have
246:13 it to a different language like I have to change it in two places that's bad
246:14 to change it in two places that's bad copy paste bad so what might be better
246:17 copy paste bad so what might be better well it turns out there's another
246:19 well it turns out there's another Paradigm in C that you can use that gets
246:22 Paradigm in C that you can use that gets around this problem this duplication of
246:24 around this problem this duplication of code it would be much nicer if I just
246:25 code it would be much nicer if I just write this code once and I can do that
246:28 write this code once and I can do that using a third type of loop called a do
246:31 using a third type of loop called a do while loop so it turns out in C you can
246:33 while loop so it turns out in C you can do this if you want to get the value of
246:35 do this if you want to get the value of a variable like n first just decree
246:37 a variable like n first just decree create the variable without an initial
246:39 create the variable without an initial value so int n semicolon means it we
246:42 value so int n semicolon means it we don't know what value it has yes but
246:43 don't know what value it has yes but that's okay we're going to add a value
246:45 that's okay we're going to add a value to it eventually then I'm going to say
246:47 to it eventually then I'm going to say this do literally I'm going to open my
246:50 this do literally I'm going to open my curly braces and what do I want to do I
246:52 curly braces and what do I want to do I want to assign to n the return value of
246:55 want to assign to n the return value of get in prompting the user for size well
246:59 get in prompting the user for size well when do you want to do that I want to do
247:01 when do you want to do that I want to do that while n is less than one and this
247:05 that while n is less than one and this code now achieves the exact same goal
247:08 code now achieves the exact same goal but by never repeating myself why well
247:11 but by never repeating myself why well notice on these lines of code now I'm
247:13 notice on these lines of code now I'm literally saying on line six give me a
247:16 literally saying on line six give me a variable called n of type integer it
247:18 variable called n of type integer it doesn't have a value initially but
247:19 doesn't have a value initially but that's fine you can do that line seven
247:21 that's fine you can do that line seven says do the following what do you want
247:23 says do the following what do you want to do get int prompting the user with
247:25 to do get int prompting the user with the word size and just store that value
247:27 the word size and just store that value in N but because code runs top to bottom
247:30 in N but because code runs top to bottom left to right now it's reasonable on
247:33 left to right now it's reasonable on line 11 to ask that question okay is the
247:35 line 11 to ask that question okay is the current value of n which it definitely
247:38 current value of n which it definitely got on line a less than one and if the
247:40 got on line a less than one and if the user didn't cooperate they typed in zero
247:42 user didn't cooperate they typed in zero or Nega 1 or3 what's going to happen
247:45 or Nega 1 or3 what's going to happen it's going to go back up here and repeat
247:47 it's going to go back up here and repeat repeat repeat everything in the do while
247:50 repeat repeat everything in the do while loop so a do while loop in C which is
247:53 loop so a do while loop in C which is not something some other languages have
247:55 not something some other languages have like python if you know it does not have
247:56 like python if you know it does not have a do while loop this is perhaps the
247:58 a do while loop this is perhaps the cleanest way to achieve this even though
248:00 cleanest way to achieve this even though it's a little weird that you have to
248:02 it's a little weird that you have to declare your variable create your
248:03 declare your variable create your variable up top and then check it down
248:06 variable up top and then check it down below but other wise it's similar to a
248:08 below but other wise it's similar to a while loop it just flips the order in
248:11 while loop it just flips the order in which you're asking the question any
248:13 which you're asking the question any questions on this construct and Doh in
248:16 questions on this construct and Doh in general is super useful when you want to
248:17 general is super useful when you want to get input from the user and make sure it
248:19 get input from the user and make sure it meets certain requirements all right so
248:23 meets certain requirements all right so now that we have this building block
248:24 now that we have this building block after that interlude how can I go about
248:27 after that interlude how can I go about cleaning up this code and then let's
248:28 cleaning up this code and then let's conclude by taking a look at things that
248:30 conclude by taking a look at things that our code can't do or can't do very well
248:33 our code can't do or can't do very well or correctly let me propose that in a
248:35 or correctly let me propose that in a final version of Mario let me just add
248:38 final version of Mario let me just add what are called now some comments so it
248:40 what are called now some comments so it turns out in code in C you can Define
248:43 turns out in code in C you can Define what are called comments which are just
248:45 what are called comments which are just notes to self some of you discovered
248:46 notes to self some of you discovered these in scratch there's little yellow
248:48 these in scratch there's little yellow sticky notes you can use to add
248:49 sticky notes you can use to add citations or explanations in C there's a
248:52 citations or explanations in C there's a couple of ways to write comments and in
248:53 couple of ways to write comments and in general comments are notes for yourself
248:55 general comments are notes for yourself for your ta for your colleague as to
248:58 for your ta for your colleague as to what your code is doing and why or how
249:00 what your code is doing and why or how it's a little explanatory note in
249:01 it's a little explanatory note in English or whatever your human language
249:03 English or whatever your human language might be so for instance what I might do
249:06 might be so for instance what I might do here in my implementation of this
249:10 here in my implementation of this version of Mario I might first ask
249:13 version of Mario I might first ask myself a question like I I might first
249:14 myself a question like I I might first make a note to S like this on a new line
249:17 make a note to S like this on a new line above this first block of code uh get
249:19 above this first block of code uh get size of Grid it's just an explanatory
249:22 size of Grid it's just an explanatory remark in any tur English that generally
249:25 remark in any tur English that generally explains the next six or so lines the
249:27 explains the next six or so lines the next chunk or block of code if you will
249:30 next chunk or block of code if you will it would be a little excessive to
249:31 it would be a little excessive to comment every single line at some point
249:33 comment every single line at some point the programmer should know what
249:34 the programmer should know what individual lines of code do but it's
249:36 individual lines of code do but it's nice to be able to kind kind of glance
249:38 nice to be able to kind kind of glance at this comment on line six that starts
249:41 at this comment on line six that starts with two slashes and it gets gray out
249:43 with two slashes and it gets gray out because of syntax highlighting it's not
249:45 because of syntax highlighting it's not logic it's just a note to self it
249:47 logic it's just a note to self it generally gives me a little cheat sheet
249:48 generally gives me a little cheat sheet as to what the following lines of code
249:50 as to what the following lines of code should be doing and or why and then down
249:52 should be doing and or why and then down here well there's a second block of code
249:54 here well there's a second block of code that's a bunch of lines but together
249:56 that's a bunch of lines but together this just what prints uh grid of bricks
250:00 this just what prints uh grid of bricks and so it's another comment to myself
250:02 and so it's another comment to myself that just makes it a little more
250:04 that just makes it a little more understandable what these 20 some odd
250:06 understandable what these 20 some odd lines of code are doing by adding some
250:07 lines of code are doing by adding some English explanations thereof but now
250:10 English explanations thereof but now that I have these you know wouldn't it
250:12 that I have these you know wouldn't it be nice if I could kind of abstract
250:15 be nice if I could kind of abstract these pieces of functionality away this
250:17 these pieces of functionality away this getting of the size and this printing of
250:19 getting of the size and this printing of the Grid in other words suppose that you
250:22 the Grid in other words suppose that you didn't know where to begin with this
250:23 didn't know where to begin with this problem and the problem at hand were
250:25 problem and the problem at hand were literally Implement a program that
250:27 literally Implement a program that prints a grid of bricks of some variable
250:29 prints a grid of bricks of some variable size three or four or five or whatever
250:31 size three or four or five or whatever the human types in if you have really no
250:34 the human types in if you have really no idea where to start comments are
250:36 idea where to start comments are actually a good way of getting started
250:38 actually a good way of getting started because comments can be a approximation
250:41 because comments can be a approximation of what we called last week pseudo code
250:42 of what we called last week pseudo code pseudo code is tur English that kind of
250:44 pseudo code is tur English that kind of gets your point across like for the uh
250:46 gets your point across like for the uh phone book searching like last time so
250:48 phone book searching like last time so if you didn't really know where to begin
250:50 if you didn't really know where to begin you could do something like this I could
250:53 you could do something like this I could for instance just say get size of grid
250:57 for instance just say get size of grid as my first step and then print grid of
250:59 as my first step and then print grid of bricks as my second step and that's it
251:01 bricks as my second step and that's it for my program thus far this is now
251:03 for my program thus far this is now implemented in pseudo code I have some
251:05 implemented in pseudo code I have some massive placeholders there like I still
251:07 massive placeholders there like I still have work to be done but at least I have
251:09 have work to be done but at least I have a highle solution to the problem in
251:11 a highle solution to the problem in comments and now I can even go this far
251:14 comments and now I can even go this far I could say well let's suppose that
251:16 I could say well let's suppose that there's just a function already that
251:18 there's just a function already that exists called get size I could do
251:21 exists called get size I could do something like this I could do Inn
251:24 something like this I could do Inn equals get size and now I just have to
251:26 equals get size and now I just have to assume for the moment that some
251:28 assume for the moment that some abstraction called get size exists it
251:29 abstraction called get size exists it doesn't this does not come with the
251:30 doesn't this does not come with the cs-50 library but I could invent it I
251:33 cs-50 library but I could invent it I bet how else might I proceed well let's
251:36 bet how else might I proceed well let's just assume for the moment that there's
251:38 just assume for the moment that there's also a function called print grid that
251:40 also a function called print grid that just prints that a grid of that size n
251:43 just prints that a grid of that size n so here too is an abstraction these
251:45 so here too is an abstraction these puzzle pieces don't exist these
251:47 puzzle pieces don't exist these functions don't yet exist but in C just
251:49 functions don't yet exist but in C just like in scratch I can create my own
251:52 like in scratch I can create my own functions how do I do that well let me
251:54 functions how do I do that well let me go down later in the file and by
251:56 go down later in the file and by convention you generally want to leave
251:58 convention you generally want to leave main at the top of your code why because
251:59 main at the top of your code why because it's the main function and it's just
252:01 it's the main function and it's just where the human eye is going to look to
252:02 where the human eye is going to look to see what some file of code does and let
252:05 see what some file of code does and let me do this I want to create a function
252:07 me do this I want to create a function of my own called get size whose purpose
252:10 of my own called get size whose purpose in life is to get the size that the user
252:12 in life is to get the size that the user wants I want this function to return an
252:14 wants I want this function to return an integer and the Syntax for doing that is
252:16 integer and the Syntax for doing that is this write similar to a variable the
252:19 this write similar to a variable the data type that this function returns I
252:21 data type that this function returns I don't need this function to take any
252:23 don't need this function to take any inputs and so I'm going to use a new
252:25 inputs and so I'm going to use a new keyword that we've actually been using
252:26 keyword that we've actually been using thus far more on it another time just
252:28 thus far more on it another time just called void which just means this get
252:30 called void which just means this get size function does not take any inputs
252:32 size function does not take any inputs it does have an output it outputs an INT
252:35 it does have an output it outputs an INT and this is just a weird order in which
252:36 and this is just a weird order in which you write it you write the output format
252:38 you write it you write the output format the name of the function and then the
252:40 the name of the function and then the inputs if any inside of parentheses and
252:42 inputs if any inside of parentheses and now I can Implement get size but I've
252:44 now I can Implement get size but I've already implemented get size or at least
252:46 already implemented get size or at least now at this point in the story I at
252:48 now at this point in the story I at least know concretely what to do and I
252:49 least know concretely what to do and I could figure out eventually with some
252:51 could figure out eventually with some trial and error perhaps all right if I
252:53 trial and error perhaps all right if I declare a variable and I do the
252:55 declare a variable and I do the following n equals get in prompting the
252:57 following n equals get in prompting the user for size and I keep doing that
253:00 user for size and I keep doing that while n is less than one once that block
253:04 while n is less than one once that block of code is done here is a new keyword in
253:06 of code is done here is a new keyword in C where you can return that value n so I
253:09 C where you can return that value n so I keep referring to these values that some
253:11 keep referring to these values that some functions return as return values in C
253:14 functions return as return values in C there's literally a keyword called
253:16 there's literally a keyword called return that will hand back to any
253:19 return that will hand back to any function that uses that function the
253:22 function that uses that function the value in question so in a nutshell
253:25 value in question so in a nutshell between lines 15 and 21 now here is some
253:28 between lines 15 and 21 now here is some code identical to our solution earlier
253:30 code identical to our solution earlier that gets a value n from the user that
253:33 that gets a value n from the user that is positive it's one or two or higher
253:35 is positive it's one or two or higher it's not zero or it's not less than one
253:38 it's not zero or it's not less than one and as soon as we've got that value we
253:39 and as soon as we've got that value we hand it back as a return value notice
253:43 hand it back as a return value notice how I'm using this function on line
253:45 how I'm using this function on line 7even just like with get int just like
253:47 7even just like with get int just like with get string I'm calling the function
253:49 with get string I'm calling the function nothing in the parenthesis in this case
253:51 nothing in the parenthesis in this case but then I'm using the assignment
253:52 but then I'm using the assignment operator to copy whatever its return
253:54 operator to copy whatever its return value is into my variable n and so now I
253:58 value is into my variable n and so now I have a function that didn't used to
254:00 have a function that didn't used to exist called get size that gets me a
254:03 exist called get size that gets me a positive integer no matter what and now
254:06 positive integer no matter what and now for the grid how do I do this how do I
254:08 for the grid how do I do this how do I invent a function called print grid that
254:10 invent a function called print grid that takes a single argument a number and
254:12 takes a single argument a number and prints a grid of that size well let's go
254:14 prints a grid of that size well let's go down here I'm going to write the name of
254:17 down here I'm going to write the name of this function print grid this function
254:19 this function print grid this function just needs to print it has a side effect
254:21 just needs to print it has a side effect as we keep saying so I'm just going to
254:22 as we keep saying so I'm just going to say it has no return value it's just
254:25 say it has no return value it's just void it doesn't have an output per se
254:27 void it doesn't have an output per se it's just an aesthetic side effect but
254:29 it's just an aesthetic side effect but it does take an an argument an argument
254:31 it does take an an argument an argument is an input and the Syntax for this in C
254:34 is an input and the Syntax for this in C is to name the type of the input it
254:36 is to name the type of the input it takes
254:37 takes and the name of the variable and I could
254:39 and the name of the variable and I could call this anything I want I'll call it
254:41 call this anything I want I'll call it size I could call it n and it's okay to
254:43 size I could call it n and it's okay to use the same variable in different
254:44 use the same variable in different functions but I'll call it size just to
254:46 functions but I'll call it size just to be distinct and then in this function
254:49 be distinct and then in this function I'm just going to copy from memory the
254:51 I'm just going to copy from memory the same code as before for in I get zero I
254:54 same code as before for in I get zero I less than size instead of three I ++
254:58 less than size instead of three I ++ inside of this four uh int J gets zero J
255:03 inside of this four uh int J gets zero J is less than size j++ and inside of that
255:06 is less than size j++ and inside of that print print out with print f a single
255:09 print print out with print f a single hash print out after that Loop a single
255:13 hash print out after that Loop a single new line and that's it now I did this
255:16 new line and that's it now I did this fast admittedly but it's the same code
255:18 fast admittedly but it's the same code that I wrote earlier but now just like I
255:20 that I wrote earlier but now just like I did with scratch let me just arbitrarily
255:22 did with scratch let me just arbitrarily hit enter a bunch of times to like move
255:23 hit enter a bunch of times to like move the code out of sight out of mind now I
255:26 the code out of sight out of mind now I have abstractions I have puzzle pieces
255:28 have abstractions I have puzzle pieces that now exist called get size and print
255:30 that now exist called get size and print grid Syntax for which takes some getting
255:32 grid Syntax for which takes some getting used to but they now just exist except I
255:35 used to but they now just exist except I do need to do one thing because C is a
255:38 do need to do one thing because C is a little naive if I try to do make Mario
255:41 little naive if I try to do make Mario now and hit enter implicit Declaration
255:44 now and hit enter implicit Declaration of function get size is invalid and
255:46 of function get size is invalid and we've seen that before when I hadn't
255:48 we've seen that before when I hadn't included a file right when I hadn't
255:50 included a file right when I hadn't included cs50 Library get in didn't work
255:53 included cs50 Library get in didn't work but that's not the issue here because
255:54 but that's not the issue here because this is not from a library I just
255:56 this is not from a library I just invented this C kind of takes you
255:58 invented this C kind of takes you literally and if you define these
256:00 literally and if you define these functions at the bottom of your file
256:02 functions at the bottom of your file they don't exist on line seven or 10 so
256:06 they don't exist on line seven or 10 so I could do this I could all right fine
256:08 I could do this I could all right fine well let me just kind of highlight all
256:09 well let me just kind of highlight all of this cut with to my clipboard and
256:12 of this cut with to my clipboard and paste it up here this would solve the
256:13 paste it up here this would solve the problem I could just move all of those
256:15 problem I could just move all of those functions at the top of my file that's
256:17 functions at the top of my file that's kind of annoying because now main is
256:18 kind of annoying because now main is like the bottom of the file you're not
256:19 like the bottom of the file you're not it's going to take longer to find it
256:21 it's going to take longer to find it it's just that's not a clean solution so
256:23 it's just that's not a clean solution so let me put it back where it was at the
256:24 let me put it back where it was at the bottom and let me do this this is the
256:27 bottom and let me do this this is the only time in cs50 and really in C
256:30 only time in cs50 and really in C programming where copy paste is
256:31 programming where copy paste is reasonable if you copy and paste the
256:34 reasonable if you copy and paste the first line of code from each function
256:37 first line of code from each function and then end it with a semicolon you can
256:39 and then end it with a semicolon you can tease the compiler by giving it just
256:42 tease the compiler by giving it just enough of a hint at the top of the file
256:44 enough of a hint at the top of the file that okay these functions don't exist
256:46 that okay these functions don't exist till down later but here's a hint that
256:48 till down later but here's a hint that they will exist this is how you can uh
256:52 they will exist this is how you can uh convince the compiler to trust you so
256:54 convince the compiler to trust you so those other functions can still be lower
256:56 those other functions can still be lower in the file below main but now when I do
256:59 in the file below main but now when I do make Mario oh damn it oh I said print
257:02 make Mario oh damn it oh I said print instead of print F that's my
257:05 instead of print F that's my bad print f
257:07 bad print f so if I do make
257:09 so if I do make Mario Mario now I can type in three and
257:13 Mario Mario now I can type in three and we're back in business now this was a
257:15 we're back in business now this was a very heavy-handed way and long way to
257:16 very heavy-handed way and long way to get to a much more complicated solution
257:19 get to a much more complicated solution but this solution in some sense is
257:21 but this solution in some sense is better designed why because now
257:23 better designed why because now especially without the comments I mean
257:24 especially without the comments I mean look how short my code is my main
257:27 look how short my code is my main function is literally two lines of code
257:29 function is literally two lines of code why well I kind of factored out the
257:30 why well I kind of factored out the juicy stuff into its own functions and
257:33 juicy stuff into its own functions and now especially if I'm working with
257:34 now especially if I'm working with colleagues or others you could imagine
257:36 colleagues or others you could imagine splitting up up large programs into
257:38 splitting up up large programs into smaller parts having different people
257:39 smaller parts having different people Implement different parts so long as you
257:41 Implement different parts so long as you all agree in advance on what those
257:43 all agree in advance on what those inputs and those outputs actually are
257:47 inputs and those outputs actually are all right so let's now consider what
257:48 all right so let's now consider what computers can do well and not so well C
257:51 computers can do well and not so well C indeed supports a whole bunch of
257:52 indeed supports a whole bunch of operators mathematically via which we
257:54 operators mathematically via which we can do addition and subtraction
257:55 can do addition and subtraction multiplication division and even
257:57 multiplication division and even calculate the remainder when you divide
257:59 calculate the remainder when you divide one number by another in fact why don't
258:01 one number by another in fact why don't we go ahead and use these in a very
258:02 we go ahead and use these in a very simple program and make our very own
258:04 simple program and make our very own calculator so let me go over here here
258:06 calculator so let me go over here here to vs code let me go ahead and create a
258:09 to vs code let me go ahead and create a new file called calculator. C and in
258:12 new file called calculator. C and in this file let's go ahead and first
258:13 this file let's go ahead and first include a couple of now familiar header
258:16 include a couple of now familiar header files
258:17 files cs50.h as well as standard i.h let's go
258:20 cs50.h as well as standard i.h let's go ahead then and declare main with int
258:23 ahead then and declare main with int main void and then inside of main let's
258:25 main void and then inside of main let's do something relatively simple let's
258:26 do something relatively simple let's declare an INT and call it X and set it
258:29 declare an INT and call it X and set it equal to whatever the return value is of
258:31 equal to whatever the return value is of get int prompting the user for a value
258:33 get int prompting the user for a value for x let's then give ourselves a second
258:36 for x let's then give ourselves a second variable we'll call it say y set that
258:38 variable we'll call it say y set that equal to the return value of another
258:40 equal to the return value of another call to get int prompting the user this
258:42 call to get int prompting the user this time for that value Y and then let's
258:45 time for that value Y and then let's very simply go ahead at the very end and
258:47 very simply go ahead at the very end and just print out say the sum of X Plus y a
258:50 just print out say the sum of X Plus y a super simple calculator so I'll use
258:52 super simple calculator so I'll use print F quote unquote percent I for
258:54 print F quote unquote percent I for integer back sln to give me the new line
258:57 integer back sln to give me the new line then I'm going to go ahead and do x + y
258:59 then I'm going to go ahead and do x + y to indeed print out the sum let me go
259:01 to indeed print out the sum let me go down to my terminal window now let me do
259:03 down to my terminal window now let me do make calculator in order to compile the
259:06 make calculator in order to compile the C no error messages so that's good let
259:08 C no error messages so that's good let me do/ calulator and let's do something
259:11 me do/ calulator and let's do something like 2 + 2 which of course should equal
259:14 like 2 + 2 which of course should equal 4 and it does but it turns out that
259:17 4 and it does but it turns out that sometimes there are going to be
259:19 sometimes there are going to be limitations that we bump up against and
259:21 limitations that we bump up against and let me get a little more ambitious here
259:23 let me get a little more ambitious here let me clear my terminal window and let
259:24 let me clear my terminal window and let me go ahead and rerun calculator again
259:26 me go ahead and rerun calculator again and this time let's type in
259:33 O2 billion for x and let's type in the same for y and of course now the answer
259:35 same for y and of course now the answer of 2 billion plus 2 billion should have
259:36 of 2 billion plus 2 billion should have of course be 4 billion and yet it's not
259:40 of course be 4 billion and yet it's not so curiously we see of all things a
259:42 so curiously we see of all things a negative number here which suggests that
259:44 negative number here which suggests that somehow the plus operator doesn't quite
259:46 somehow the plus operator doesn't quite work as well as we might like now why
259:49 work as well as we might like now why might this actually be well it turns out
259:51 might this actually be well it turns out that inside of your computer is of
259:52 that inside of your computer is of course memory or Ram random access
259:55 course memory or Ram random access memory and depending on the size of your
259:56 memory and depending on the size of your computer and the type of computer it
259:57 computer and the type of computer it might very well look a little something
259:59 might very well look a little something like this a little circuit board with
260:01 like this a little circuit board with these black little modules on it that
260:02 these black little modules on it that actually contain all of the btes of your
260:04 actually contain all of the btes of your computer's memory unfortunately you and
260:07 computer's memory unfortunately you and I only have a finite amount of this
260:09 I only have a finite amount of this memory inside of our computers which
260:11 memory inside of our computers which means no matter how high we want to
260:13 means no matter how high we want to count there's ultimately going to be a
260:14 count there's ultimately going to be a limitation on high how we can count
260:16 limitation on high how we can count because we only have a finite amount of
260:18 because we only have a finite amount of memory we don't have an infinite number
260:20 memory we don't have an infinite number of zeros and ones to play with we have
260:22 of zeros and ones to play with we have to actually be bounded ultimately so
260:25 to actually be bounded ultimately so what's the implication of this well it
260:26 what's the implication of this well it turns out that computers typically use
260:28 turns out that computers typically use as many as 32 bits zeros or ones to
260:31 as many as 32 bits zeros or ones to represent something like an integer or
260:33 represent something like an integer or in C an INT so for instance the smallest
260:36 in C an INT so for instance the smallest number we could represent using 32 ins
260:38 number we could represent using 32 ins of course using 32 bits of course would
260:40 of course using 32 bits of course would be zero 32 zeros like this here and the
260:44 be zero 32 zeros like this here and the biggest number we could represent is by
260:45 biggest number we could represent is by changing all of those zeros to ones
260:48 changing all of those zeros to ones which in this case will ideally give us
260:50 which in this case will ideally give us a number that equals roughly 4 billion
260:53 a number that equals roughly 4 billion in total it's actually 4 billion 294 mil
260:56 in total it's actually 4 billion 294 mil 967 295 maximally if you set all 32 of
261:00 967 295 maximally if you set all 32 of those bits to ones and then do out the
261:02 those bits to ones and then do out the actual math the catch though is that we
261:05 actual math the catch though is that we humans and Compu in general also
261:07 humans and Compu in general also sometimes want to and need to be able to
261:09 sometimes want to and need to be able to represent negative numbers so if you
261:11 represent negative numbers so if you want to represent negative numbers as
261:13 want to represent negative numbers as well as positive numbers in zero you
261:15 well as positive numbers in zero you can't really just start counting at zero
261:17 can't really just start counting at zero and go all the way up to roughly four
261:19 and go all the way up to roughly four billion you got to kind of split the
261:20 billion you got to kind of split the difference and maybe allocate half of
261:23 difference and maybe allocate half of those patterns of zeros and ones to
261:25 those patterns of zeros and ones to negative numbers and the other half
261:26 negative numbers and the other half roughly to positive numbers so in fact
261:29 roughly to positive numbers so in fact in practice when you're using even as
261:31 in practice when you're using even as many as 32 bits the highest most
261:33 many as 32 bits the highest most computers could count certainly in a
261:35 computers could count certainly in a program like this in C using an INT
261:37 program like this in C using an INT would be roughly 2 billion that is 2 b47
261:40 would be roughly 2 billion that is 2 b47 m
262:08 483,000 flow where you want to use more bits than you have available and as a
262:10 bits than you have available and as a result you sort of overflow the
262:12 result you sort of overflow the available space what does this mean in
262:14 available space what does this mean in fact in real terms well let's suppose
262:15 fact in real terms well let's suppose that you only have three bits but I'm
262:17 that you only have three bits but I'm going to gray out a fourth bit just to
262:19 going to gray out a fourth bit just to convey where we'd like to uh put an
262:22 convey where we'd like to uh put an additional bit ultimately if this of
262:24 additional bit ultimately if this of course is zero per week zero discussion
262:26 course is zero per week zero discussion this is 1 2 3 4 5 6 7 now ideally in
262:32 this is 1 2 3 4 5 6 7 now ideally in binary if you want to add one more to
262:35 binary if you want to add one more to this value seven you're going to have to
262:37 this value seven you're going to have to carry the one mathematically and that
262:39 carry the one mathematically and that would ideally give you 1 0 0 0 but if
262:43 would ideally give you 1 0 0 0 but if you don't have four bits and your
262:44 you don't have four bits and your computer's only sophisticated enough to
262:47 computer's only sophisticated enough to have three bits not even 32 but three
262:50 have three bits not even 32 but three the implication is that you're
262:51 the implication is that you're effectively representing not one0 0 but
262:54 effectively representing not one0 0 but rather 0 z0 there's just no room to
262:58 rather 0 z0 there's just no room to store that fourth bit that I've gray out
263:01 store that fourth bit that I've gray out here which is to say that your integer
263:03 here which is to say that your integer might overflow and as soon as you get to
263:05 might overflow and as soon as you get to seven the next number once you add one
263:08 seven the next number once you add one is actually going to be zero or worse as
263:10 is actually going to be zero or worse as we've seen here in my code a negative
263:12 we've seen here in my code a negative value instead so what could we do to
263:14 value instead so what could we do to perhaps address this kind of concern
263:16 perhaps address this kind of concern well C does not have just integers or
263:18 well C does not have just integers or ins it also has Longs which as the name
263:20 ins it also has Longs which as the name suggest or just longer integers which
263:22 suggest or just longer integers which means they have more bits available to
263:24 means they have more bits available to them so let me go back into my code here
263:26 them so let me go back into my code here I'll clear the terminal window and let
263:28 I'll clear the terminal window and let me go ahead and change my integers to
263:30 me go ahead and change my integers to literally long here long here I'm going
263:33 literally long here long here I'm going to have to change my function in
263:36 to have to change my function in uh cs50's library to be not get in but
263:39 uh cs50's library to be not get in but get long and that's indeed another
263:40 get long and that's indeed another function we provide in the library let
263:42 function we provide in the library let me change this get in to get long as
263:44 me change this get in to get long as well I'll keep my variable names the
263:45 well I'll keep my variable names the same but I do need to make one other
263:47 same but I do need to make one other change it turns out that print F also
263:49 change it turns out that print F also support supports other format codes so
263:51 support supports other format codes so not just percent I for integers or
263:53 not just percent I for integers or percent s for Strings but also for
263:55 percent s for Strings but also for instance percent Li for a long integer
263:59 instance percent Li for a long integer as well as percent f for floating Point
264:01 as well as percent f for floating Point values with decimals so with that said
264:03 values with decimals so with that said let's go ahead and change my print F
264:05 let's go ahead and change my print F line to be not perc I but percent Li I
264:08 line to be not perc I but percent Li I now let me go ahead and do make
264:10 now let me go ahead and do make calculator again enter no apparent
264:13 calculator again enter no apparent errors now do/ calulator and 2 + 2 still
264:16 errors now do/ calulator and 2 + 2 still equals 4 as before but now if I do
264:19 equals 4 as before but now if I do calculator again and let's do
264:22 calculator again and let's do two billion again as well as 2 billion
264:25 two billion again as well as 2 billion for y previously we overflowed the size
264:28 for y previously we overflowed the size of an integer and got some weird
264:30 of an integer and got some weird negative number because the pattern was
264:32 negative number because the pattern was misinterpreted if you will as a negative
264:34 misinterpreted if you will as a negative number instead but along instead of
264:35 number instead but along instead of using using 32 bits conventionally uses
264:38 using using 32 bits conventionally uses 64 bits which means we have more than
264:41 64 bits which means we have more than enough spare bits to go when we add 2
264:43 enough spare bits to go when we add 2 billion plus 2 billion and now in fact
264:45 billion plus 2 billion and now in fact we get the correct answer of four
264:47 we get the correct answer of four billion which does fit inside of the
264:50 billion which does fit inside of the size of a long now along can count up
264:53 size of a long now along can count up quite high and in fact it can count as
264:55 quite high and in fact it can count as high as this nine quintilian and so that
264:59 high as this nine quintilian and so that will give us quite a bit more Runway but
265:01 will give us quite a bit more Runway but of course it too is ultimately going to
265:03 of course it too is ultimately going to be finite so if you have numbers that
265:05 be finite so if you have numbers that need to go bigger than that you might
265:07 need to go bigger than that you might still very well have a problem now
265:09 still very well have a problem now there's another problem that we might
265:11 there's another problem that we might run into as well and we can see it in
265:12 run into as well and we can see it in the context of even this simple
265:14 the context of even this simple calculator computers also suffer from
265:16 calculator computers also suffer from potentially what's called truncation
265:19 potentially what's called truncation where especially when you're doing math
265:20 where especially when you're doing math involving floating Point values that is
265:22 involving floating Point values that is numbers with decimals you might
265:24 numbers with decimals you might accidentally unknowingly truncate the
265:27 accidentally unknowingly truncate the value that is lose everything after the
265:29 value that is lose everything after the decimal point so in fact let me go back
265:31 decimal point so in fact let me go back to vs code here I'll clear my terminal
265:33 to vs code here I'll clear my terminal window and let's still use Longs but
265:35 window and let's still use Longs but let's go ahead and use division instead
265:37 let's go ahead and use division instead of addition here so let me change this
265:39 of addition here so let me change this plus to a divide operator let me go
265:42 plus to a divide operator let me go ahead and recompile the code down here
265:44 ahead and recompile the code down here with make calculator let me go ahead and
265:46 with make calculator let me go ahead and run/ calculator and let me go ahead and
265:48 run/ calculator and let me go ahead and do something like 14x and 3 for y and
265:53 do something like 14x and 3 for y and we'll see that well wait a minute 1
265:55 we'll see that well wait a minute 1 divided 3 I learned should be 13 but in
265:58 divided 3 I learned should be 13 but in a floating point value that should be
266:00 a floating point value that should be point0 it should be
266:03 point0 it should be 0.33333 you know maybe with a little
266:05 0.33333 you know maybe with a little line over in grade school but really an
266:07 line over in grade school but really an infinite number of Threes And yet we
266:08 infinite number of Threes And yet we seem to have lost even one of those
266:11 seem to have lost even one of those threes after the decimal point because
266:13 threes after the decimal point because the answer is coming back here as just
266:15 the answer is coming back here as just zero so why might that be well if I know
266:19 zero so why might that be well if I know that two integers when divided one by
266:21 that two integers when divided one by the other is supposed to give me a
266:23 the other is supposed to give me a fraction a floating point value with a
266:25 fraction a floating point value with a decimal point I can't continue to use
266:28 decimal point I can't continue to use integers or even in this case Longs
266:30 integers or even in this case Longs which do not have support for decimal
266:32 which do not have support for decimal points so let me go ahead and change
266:34 points so let me go ahead and change this format code here from percent Li I
266:36 this format code here from percent Li I to percent F which is again going to
266:39 to percent F which is again going to represent a floating point value instead
266:41 represent a floating point value instead of a long integer or even an integer and
266:43 of a long integer or even an integer and let me go ahead further and Define maybe
266:47 let me go ahead further and Define maybe a third variable Z as a float itself so
266:50 a third variable Z as a float itself so I'll give myself a variable Z equals x /
266:53 I'll give myself a variable Z equals x / Y and now rather than print X ided Y
266:56 Y and now rather than print X ided Y let's just go ahead and print Z so now
266:57 let's just go ahead and print Z so now I'm operating in a world of floating
266:59 I'm operating in a world of floating Point values because I know proactively
267:01 Point values because I know proactively that an long or an INT divided by
267:04 that an long or an INT divided by another such value if it's meant to have
267:06 another such value if it's meant to have a fraction needs to be stored in a
267:08 a fraction needs to be stored in a floating point value something with a
267:10 floating point value something with a decimal point well let me go down to my
267:12 decimal point well let me go down to my terminal window here and rerun make of
267:14 terminal window here and rerun make of calculator seems to work okay/ calulator
267:17 calculator seems to work okay/ calulator and let's do 1 / 3 again and still here
267:22 and let's do 1 / 3 again and still here we see all zeros well so we do at least
267:24 we see all zeros well so we do at least see a decimal point so we've made some
267:25 see a decimal point so we've made some progress thanks to the percent F in the
267:27 progress thanks to the percent F in the float but it seems that we've already
267:29 float but it seems that we've already truncated the value 1 / 3 so how do we
267:33 truncated the value 1 / 3 so how do we actually get around this issue well if
267:35 actually get around this issue well if you the program know that you're dealing
267:36 you the program know that you're dealing in a world that's going to give you
267:38 in a world that's going to give you floating Point values with decimal
267:39 floating Point values with decimal points you might very well need to use
267:42 points you might very well need to use what's called a feature known as typ
267:44 what's called a feature known as typ casting that is convert one data type to
267:47 casting that is convert one data type to another by explicitly telling the
267:49 another by explicitly telling the compiler that you want to do so now how
267:51 compiler that you want to do so now how do I do this well let's go back to my
267:52 do I do this well let's go back to my code here and if the issue fundamentally
267:55 code here and if the issue fundamentally is that c is still treating X and Y as
268:00 is that c is still treating X and Y as integers or technically Longs with no
268:02 integers or technically Longs with no decimal point and dividing one by the
268:04 decimal point and dividing one by the other therefore has no room so to speak
268:06 other therefore has no room so to speak for any numbers after a decimal point
268:09 for any numbers after a decimal point why don't I proactively do this let me
268:11 why don't I proactively do this let me using a slightly new syntax with
268:13 using a slightly new syntax with parenthesis specify that I want to
268:15 parenthesis specify that I want to convert X proactively from a long to a
268:19 convert X proactively from a long to a float let me specify proactively that I
268:22 float let me specify proactively that I want to convert y from a long to a float
268:25 want to convert y from a long to a float as well and now let me go ahead and
268:28 as well and now let me go ahead and trust that in Z should be the result of
268:30 trust that in Z should be the result of dividing not a long by a long or an INT
268:32 dividing not a long by a long or an INT by an INT but rather a float by a float
268:35 by an INT but rather a float by a float Let Me Clear My terminal window run make
268:38 Let Me Clear My terminal window run make calculator again seems to work okay/
268:41 calculator again seems to work okay/ calulator and now 1 3 and hopefully now
268:45 calulator and now 1 3 and hopefully now we actually see that my code has
268:48 we actually see that my code has outputed
268:49 outputed 0.333 333 and I think if we kept showing
268:53 0.333 333 and I think if we kept showing more numbers after the decimal point
268:55 more numbers after the decimal point we'd theoretically see as many of those
268:56 we'd theoretically see as many of those threes as we want but there is still one
268:59 threes as we want but there is still one more catch and especially when we're
269:01 more catch and especially when we're manipulating numbers in this way in a
269:03 manipulating numbers in this way in a computer using a finite amount of memory
269:05 computer using a finite amount of memory another challenge we might run up
269:07 another challenge we might run up against besides integer overflow besides
269:09 against besides integer overflow besides truncation is this known as floating
269:12 truncation is this known as floating point in Precision just as we can't
269:14 point in Precision just as we can't represent as big of an integer as we
269:17 represent as big of an integer as we want using int or long alone because
269:19 want using int or long alone because there is going to be an upper bound
269:20 there is going to be an upper bound they're similarly going to be a boundary
269:22 they're similarly going to be a boundary on just how precise our numbers can be
269:26 on just how precise our numbers can be and indeed let's go back to vs code here
269:28 and indeed let's go back to vs code here I'll clear my terminal window yet again
269:30 I'll clear my terminal window yet again and this time let me use some slightly
269:31 and this time let me use some slightly unley syntax to specify that I don't
269:33 unley syntax to specify that I don't want to see the default number of
269:35 want to see the default number of numbers after the the decimal point
269:36 numbers after the the decimal point which percent F gives us automatically
269:38 which percent F gives us automatically let's go ahead and show me 20 decimal
269:42 let's go ahead and show me 20 decimal point numbers after the decimal point
269:44 point numbers after the decimal point and the weird Syntax for this is to do
269:46 and the weird Syntax for this is to do not percent F but percent period to zero
269:50 not percent F but percent period to zero to indicate to see that I want to see 20
269:52 to indicate to see that I want to see 20 digits not the default after now the
269:54 digits not the default after now the decimal point let me rerun make
269:56 decimal point let me rerun make calculator let me do dot calculat again
269:59 calculator let me do dot calculat again and let's do one let's do three and now
270:02 and let's do one let's do three and now this is even weirder right from grade
270:04 this is even weirder right from grade school you presumably learned that 1
270:06 school you presumably learned that 1 divided 3 is of course 1/3 but that
270:08 divided 3 is of course 1/3 but that should be
270:10 should be 0.33333 infinitely many times or on
270:13 0.33333 infinitely many times or on paper with a little line over it but the
270:15 paper with a little line over it but the computer is just doing some weird
270:17 computer is just doing some weird approximation here it's a whole bunch of
270:18 approximation here it's a whole bunch of Threes And then 43 267 44 079
270:23 Threes And then 43 267 44 079 590 well what's really happening under
270:25 590 well what's really happening under the hood well again is this issue of
270:27 the hood well again is this issue of floating point in Precision if you only
270:29 floating point in Precision if you only have a finite number of bits and in turn
270:31 have a finite number of bits and in turn a finite amount of memory the computer
270:33 a finite amount of memory the computer can really only be so precise
270:36 can really only be so precise intuitively alter Converse or
270:38 intuitively alter Converse or equivalently the computer has decided on
270:41 equivalently the computer has decided on some way of representing floating Point
270:43 some way of representing floating Point values but the catch is per grade school
270:45 values but the catch is per grade school math there's an infinite number of
270:47 math there's an infinite number of numbers out there and an infinite number
270:49 numbers out there and an infinite number of floating Point values because you can
270:51 of floating Point values because you can keep adding more and more digits if you
270:53 keep adding more and more digits if you want so the computer given the way it's
270:55 want so the computer given the way it's implementing these floating Point values
270:57 implementing these floating Point values is essentially giving us the closest
270:59 is essentially giving us the closest approximation that it can now how can we
271:01 approximation that it can now how can we go about improving the situation well
271:04 go about improving the situation well there is one alternative instead of
271:06 there is one alternative instead of using float I can use something called a
271:08 using float I can use something called a double which as the name suggests uses
271:09 double which as the name suggests uses twice as many bits as a float so instead
271:11 twice as many bits as a float so instead of 32 typically it will use 64 and
271:14 of 32 typically it will use 64 and that's just like the difference between
271:16 that's just like the difference between a long and an INT which gave us more
271:17 a long and an INT which gave us more bits but in this case this will be used
271:19 bits but in this case this will be used for more Precision let's go ahead and
271:22 for more Precision let's go ahead and Cast X to a double let's cast y to a
271:24 Cast X to a double let's cast y to a double and now let's go ahead and using
271:26 double and now let's go ahead and using the same format code percent 2f is still
271:30 the same format code percent 2f is still okay for doubles let me do make
271:32 okay for doubles let me do make calculator let me do do slash calculator
271:35 calculator let me do do slash calculator and now let me do 1 / 3 and we still
271:39 and now let me do 1 / 3 and we still have some of that in precision and we'd
271:40 have some of that in precision and we'd see even more of it if we looked at more
271:42 see even more of it if we looked at more than just 20 digits but now we have more
271:44 than just 20 digits but now we have more threes after the decimal point so it's
271:46 threes after the decimal point so it's at least more more more precise but it's
271:49 at least more more more precise but it's not perfect but it it's at least more
271:52 not perfect but it it's at least more precise so these kinds of issues then
271:54 precise so these kinds of issues then are going to be necessary to keep in
271:56 are going to be necessary to keep in mind anytime you do something
271:57 mind anytime you do something numerically scientifically at least with
271:59 numerically scientifically at least with a language like C where you're going to
272:01 a language like C where you're going to bump up against these real world
272:02 bump up against these real world limitations of hardware and intern
272:05 limitations of hardware and intern language now later in the semester we'll
272:06 language now later in the semester we'll transition to a language called Python
272:08 transition to a language called Python and that's actually going to solve at
272:09 and that's actually going to solve at least one of these problems for us but
272:11 least one of these problems for us but just automatically giving us more bits
272:13 just automatically giving us more bits so to speak as we need them at least for
272:15 so to speak as we need them at least for integers but even the issue of floating
272:17 integers but even the issue of floating point in Precision is going to remain
272:19 point in Precision is going to remain now just how real world are these issues
272:21 now just how real world are these issues well back in the year 1999 we got a
272:23 well back in the year 1999 we got a taste of this when the world realized in
272:25 taste of this when the world realized in the Years leading up to that date that
272:28 the Years leading up to that date that it might not have been the best idea to
272:30 it might not have been the best idea to implement computers and software therein
272:33 implement computers and software therein by storing years using just two digits
272:35 by storing years using just two digits like instead of storing 1999 to
272:38 like instead of storing 1999 to represent the year 1999 a lot of
272:40 represent the year 1999 a lot of computers for reasons of space and cost
272:43 computers for reasons of space and cost were in the habit of kind of cutting a
272:44 were in the habit of kind of cutting a corner and just using two digits to keep
272:46 corner and just using two digits to keep track of the year the problem with that
272:49 track of the year the problem with that is that if systems were not updated by
272:51 is that if systems were not updated by the year 1999 to support the year 2000
272:55 the year 1999 to support the year 2000 2001 and so forth is that just like
272:57 2001 and so forth is that just like before with integer overflow some
272:59 before with integer overflow some computers might add one to the year in
273:02 computers might add one to the year in their memory 99 it should be the year
273:04 their memory 99 it should be the year 2000 but if they're only using two
273:06 2000 but if they're only using two digits to represent years they might
273:08 digits to represent years they might mistake the year as some systems may
273:11 mistake the year as some systems may very well have for the year 1900 instead
273:14 very well have for the year 1900 instead taking literally a big step backwards if
273:16 taking literally a big step backwards if you will now you'd like to think that
273:18 you will now you'd like to think that kind of issue is behind us especially as
273:20 kind of issue is behind us especially as we understand all the more about the
273:21 we understand all the more about the limitations of code and Computing but
273:24 limitations of code and Computing but we're actually going to run up against
273:25 we're actually going to run up against this very same type of issue again in
273:27 this very same type of issue again in just a few years on January 19th in the
273:30 just a few years on January 19th in the year
273:31 year 2038 we will have run out of bits in
273:34 2038 we will have run out of bits in most computers right now to keep track
273:36 most computers right now to keep track of time it turns out years ago humans
273:39 of time it turns out years ago humans decided to use a 32-bit integer to keep
273:42 decided to use a 32-bit integer to keep track of how many seconds had elapsed
273:44 track of how many seconds had elapsed over time they chose a somewhat
273:46 over time they chose a somewhat arbitrary date in the past January 1st
273:48 arbitrary date in the past January 1st 1970 and they just started counting
273:51 1970 and they just started counting seconds from there on out and so if a
273:53 seconds from there on out and so if a computer stores some number of seconds
273:55 computer stores some number of seconds that tells the computer how many seconds
273:57 that tells the computer how many seconds have passed since that particular date
273:59 have passed since that particular date January 1st 1970 unfortunately using a
274:02 January 1st 1970 unfortunately using a 32-bit integer as we've seen you can
274:04 32-bit integer as we've seen you can only count so high at which point you
274:07 only count so high at which point you overflow the size of that variable and
274:10 overflow the size of that variable and so potentially if we don't get ahead of
274:12 so potentially if we don't get ahead of this as humans as a society as computer
274:14 this as humans as a society as computer scientist on the date January 19th
274:17 scientist on the date January 19th 2038 that bit might flip over there by
274:21 2038 that bit might flip over there by overflowing the size of those integers
274:23 overflowing the size of those integers bringing us back computationally to
274:25 bringing us back computationally to December 13th
274:28 December 13th 1901 so this is to say now with all of
274:31 1901 so this is to say now with all of this computational ability and code
274:32 this computational ability and code comes a responsibility to actually write
274:34 comes a responsibility to actually write correct code next week we'll peel back
274:36 correct code next week we'll peel back some of these layers but for now this
274:38 some of these layers but for now this was week one and best of luck on problem
274:40 was week one and best of luck on problem set
276:35 all right this is cs50 and this is this is week two wherein we're going to take
276:37 is week two wherein we're going to take a look at a lower level at how things
276:40 a look at a lower level at how things work and indeed among the goals of the
276:41 work and indeed among the goals of the course isn't this bottom up
276:43 course isn't this bottom up understanding so that in a couple of
276:44 understanding so that in a couple of weeks time even a few years time when
276:46 weeks time even a few years time when you encounter some new technology you'll
276:48 you encounter some new technology you'll be able to think back hopefully on some
276:49 be able to think back hopefully on some of this week's and this courses basic
276:51 of this week's and this courses basic building blocks and Primitives and
276:53 building blocks and Primitives and really just deduce how tomorrow's
276:54 really just deduce how tomorrow's Technologies work but along the way it's
276:56 Technologies work but along the way it's going to seem it's going to be a little
276:58 going to seem it's going to be a little hard perhaps to see the forest for the
277:00 hard perhaps to see the forest for the tree so to speak and so the goal at the
277:02 tree so to speak and so the goal at the end of the day still is going to be
277:04 end of the day still is going to be problem solving and so we thought we'd
277:05 problem solving and so we thought we'd begin today with a look at some of the
277:07 begin today with a look at some of the problems we'll talk about or solve this
277:09 problems we'll talk about or solve this coming week uh and for that we have some
277:11 coming week uh and for that we have some Brave volunteers who have already come
277:12 Brave volunteers who have already come up if we could turn on some dramatic
277:14 up if we could turn on some dramatic lighting and meet today's
277:17 lighting and meet today's volunteers so on my left here we have hi
277:20 volunteers so on my left here we have hi my name is Alex I'm a first year at the
277:22 my name is Alex I'm a first year at the college and I'm from Chapo North
277:24 college and I'm from Chapo North Carolina Welcome to Alex and to Alex's
277:28 Carolina Welcome to Alex and to Alex's right um I'm Sarah I'm from Toronto
277:30 right um I'm Sarah I'm from Toronto Canada and I'm also a first year student
277:32 Canada and I'm also a first year student at the college wonderful well welcome to
277:34 at the college wonderful well welcome to both Al and Sarah so one of the problems
277:36 both Al and Sarah so one of the problems you'll perhaps solve this week for
277:38 you'll perhaps solve this week for problem set two is to analyze the
277:40 problem set two is to analyze the reading level of a body of text whether
277:42 reading level of a body of text whether someone reads at a first grade level
277:44 someone reads at a first grade level second grade level third grade level all
277:45 second grade level third grade level all the way up to 12 or 13 or Beyond but
277:48 the way up to 12 or 13 or Beyond but you've perhaps never quite thought about
277:51 you've perhaps never quite thought about certainly in terms of code like how you
277:52 certainly in terms of code like how you would analyze some text some book and
277:54 would analyze some text some book and figure out what reading level is it at
277:56 figure out what reading level is it at and yet surely our teachers growing up
277:58 and yet surely our teachers growing up kind of knew or had an intuitive sense
277:59 kind of knew or had an intuitive sense of this so let's consider some sample
278:01 of this so let's consider some sample text for instance Alex what have you
278:03 text for instance Alex what have you been reading lately um
278:05 been reading lately um One Fish Two Fish Red F Fish Blue Fish
278:12 One Fish Two Fish Red F Fish Blue Fish wonderful so given that what grade level
278:15 wonderful so given that what grade level would you say Alex is currently reading
278:17 would you say Alex is currently reading at feel free to just shout it
278:20 at feel free to just shout it out first first so indeed you'll see
278:23 out first first so indeed you'll see this week if you run your code on Alex's
278:26 this week if you run your code on Alex's text it actually turns out he reads
278:28 text it actually turns out he reads below a first grade reading level but
278:30 below a first grade reading level but but why might that be what might your
278:32 but why might that be what might your intuition be for why we've
278:35 intuition be for why we've uh why we've accused Alex of reading at
278:37 uh why we've accused Alex of reading at this level feel free to shout
278:40 this level feel free to shout out yeah so very few syllables short
278:43 out yeah so very few syllables short words short sentences and so there's
278:44 words short sentences and so there's some puristic perhaps we can infer from
278:47 some puristic perhaps we can infer from that short text that that probably means
278:48 that short text that that probably means that it's best for younger children now
278:50 that it's best for younger children now Sarah by contrast what have you been
278:52 Sarah by contrast what have you been reading Mr and Miss dersley of number
278:55 reading Mr and Miss dersley of number four privet Drive were proud to say that
278:58 four privet Drive were proud to say that they were perfectly normal thank you
279:00 they were perfectly normal thank you very much they were the last people
279:02 very much they were the last people you'd expect to be involved in anything
279:03 you'd expect to be involved in anything strange or mysterious ious because they
279:06 strange or mysterious ious because they just didn't hold with much nonsense all
279:08 just didn't hold with much nonsense all right now irrespective of what grade you
279:10 right now irrespective of what grade you were in when you might have read that
279:11 were in when you might have read that text what grade level does Sarah seem to
279:13 text what grade level does Sarah seem to be reading
279:14 be reading at so eighth grade second grade okay so
279:18 at so eighth grade second grade okay so hearing a bit of everything so that at
279:19 hearing a bit of everything so that at least according to code would actually
279:21 least according to code would actually be seventh grade and what might the
279:23 be seventh grade and what might the intuition there be why is that a higher
279:26 intuition there be why is that a higher grade level even though we might
279:27 grade level even though we might disagree exactly which grade it is comp
279:31 disagree exactly which grade it is comp yeah so complicated sentences longer
279:33 yeah so complicated sentences longer sentences so indeed a lot more more
279:35 sentences so indeed a lot more more words were being spoken by Sarah because
279:36 words were being spoken by Sarah because there was so much more there on the page
279:38 there was so much more there on the page so we'll translate these ideas this
279:40 so we'll translate these ideas this coming week and problem set two if you
279:42 coming week and problem set two if you tackle this one to code so that you can
279:44 tackle this one to code so that you can ultimately infer things of these
279:45 ultimately infer things of these quantitatively but to do so we're going
279:47 quantitatively but to do so we're going to have to understand text so let's
279:49 to have to understand text so let's first thank our volunteers and then
279:50 first thank our volunteers and then we'll dive in to that lower
279:53 we'll dive in to that lower level stress
280:02 balls sure you can keep those yeah all right so besides that let's consider one
280:04 right so besides that let's consider one another body of text perhaps that you
280:06 another body of text perhaps that you might see this week which is namely a
280:08 might see this week which is namely a little something like this what I have
280:10 little something like this what I have here on the screen is what we'll start
280:11 here on the screen is what we'll start calling today Cipher text it's the
280:13 calling today Cipher text it's the result of encrypting some piece of
280:15 result of encrypting some piece of information and encryption or more
280:17 information and encryption or more generally the Art and Science of
280:19 generally the Art and Science of cryptography is all around us it's what
280:21 cryptography is all around us it's what you're using on the web on your phones
280:22 you're using on the web on your phones with your Banks and anything that tries
280:24 with your Banks and anything that tries to keep data secure is using encryption
280:26 to keep data secure is using encryption but there's going to be different levels
280:28 but there's going to be different levels of encryption strong encryption weak
280:30 of encryption strong encryption weak encryption and what you see here on the
280:31 encryption and what you see here on the screen isn't all that strong but we'll
280:34 screen isn't all that strong but we'll see later today how we might decrypt
280:36 see later today how we might decrypt this and actually reveal what the plain
280:39 this and actually reveal what the plain text is that corresponds to that Cipher
280:41 text is that corresponds to that Cipher text but in order to do so we have to
280:43 text but in order to do so we have to start taking off some training wheels so
280:45 start taking off some training wheels so to speak and believe it or not even
280:47 to speak and believe it or not even though your time with C this past week
280:49 though your time with C this past week for the first time probably might have
280:50 for the first time probably might have been rather in the weeds and much more
280:53 been rather in the weeds and much more complicated seemingly than C it turns
280:55 complicated seemingly than C it turns out that along the way we have been
280:56 out that along the way we have been providing and will continue to provide
280:58 providing and will continue to provide certain training wheels for instance the
281:00 certain training wheels for instance the cs50 library is one of them and even
281:02 cs50 library is one of them and even some of the explanations give up topics
281:05 some of the explanations give up topics for now in these early weeks will be
281:07 for now in these early weeks will be somewhat simplified abstracted away if
281:09 somewhat simplified abstracted away if you will but the goal ultimately is for
281:10 you will but the goal ultimately is for you to understand each and every one of
281:12 you to understand each and every one of those details so that after cs50 you
281:14 those details so that after cs50 you really can stand on your own and
281:17 really can stand on your own and understand and wrap your mind around any
281:18 understand and wrap your mind around any future Technologies as well so let's
281:21 future Technologies as well so let's consider first the very first program
281:24 consider first the very first program with which we began last week which was
281:25 with which we began last week which was this one so hello world and C at the end
281:27 this one so hello world and C at the end of the day it was really the print a
281:29 of the day it was really the print a function that was doing the interesting
281:30 function that was doing the interesting part of the work but there was a lot of
281:32 part of the work but there was a lot of technical stuff above and below it the
281:35 technical stuff above and below it the the curly braces the parentheses words
281:37 the curly braces the parentheses words like void and include and then of course
281:39 like void and include and then of course the angled brackets and more but at the
281:42 the angled brackets and more but at the end of the day we needed to convert that
281:43 end of the day we needed to convert that source code in C to machine code the
281:47 source code in C to machine code the zeros and ones in binary that the
281:48 zeros and ones in binary that the computer understood and to do that of
281:51 computer understood and to do that of course we ran we compiled the code we
281:53 course we ran we compiled the code we ran make and then we were able to
281:55 ran make and then we were able to actually run that code there so let me
281:57 actually run that code there so let me actually go over here to VSS code and
281:59 actually go over here to VSS code and really quickly recreate that hello.c
282:02 really quickly recreate that hello.c pretty much by transcribing the same so
282:04 pretty much by transcribing the same so I have here uh include standard
282:08 I have here uh include standard i.h uh int main void and then in here I
282:12 i.h uh int main void and then in here I had quite simply hello comma world with
282:15 had quite simply hello comma world with my back slend quotes and more now last
282:17 my back slend quotes and more now last time to compile this I indeed ran make
282:20 time to compile this I indeed ran make hello followed by enter hopefully you
282:22 hello followed by enter hopefully you see no errors and that's a good thing
282:23 see no errors and that's a good thing and if you do do/ hello you see in fact
282:26 and if you do do/ hello you see in fact the results of that program but it turns
282:28 the results of that program but it turns out that make is not actually a compiler
282:31 out that make is not actually a compiler as I alluded to last week it's a program
282:33 as I alluded to last week it's a program that clearly makes your program but it
282:35 that clearly makes your program but it itself just automates the process of
282:37 itself just automates the process of using an actual compiler and there's
282:39 using an actual compiler and there's lots of different compilers out there
282:41 lots of different compilers out there and the one that it's actually using
282:43 and the one that it's actually using underneath the hood is a little
282:44 underneath the hood is a little something called clang for C language
282:47 something called clang for C language and clang is a pretty popular compiler
282:49 and clang is a pretty popular compiler nowadays there's another one that's been
282:51 nowadays there's another one that's been around for ages called GCC but these are
282:53 around for ages called GCC but these are just specific names for types of
282:56 just specific names for types of compilers that different people
282:57 compilers that different people different companies different groups
282:58 different companies different groups have actually created but if you use in
283:01 have actually created but if you use in week one a compiler yourself manually
283:04 week one a compiler yourself manually you have to know you have to understand
283:06 you have to know you have to understand a little more about what's going on
283:07 a little more about what's going on because it's even more cryptic than with
283:09 because it's even more cryptic than with just make a loan so in fact let me go
283:11 just make a loan so in fact let me go back to my terminal window here let me
283:13 back to my terminal window here let me go ahead and clear the screen a little
283:14 go ahead and clear the screen a little bit and just run really the raw compiler
283:18 bit and just run really the raw compiler command so what make is automating for
283:20 command so what make is automating for me let me actually do this manually for
283:22 me let me actually do this manually for just a moment so if I want to compile uh
283:26 just a moment so if I want to compile uh hello.c into an executable program I can
283:29 hello.c into an executable program I can run I can do this uh clang space hello
283:35 run I can do this uh clang space hello C and then enter and now there's no
283:38 C and then enter and now there's no output which is a good thing in this
283:39 output which is a good thing in this case no errors but notice this if I go
283:42 case no errors but notice this if I go ahead and type LS it turns out there's a
283:46 ahead and type LS it turns out there's a uh a file that's been created suddenly
283:49 uh a file that's been created suddenly in my current folder weirdly called
283:50 in my current folder weirdly called a.out that stands for assembler output
283:53 a.out that stands for assembler output and long story short that's actually the
283:55 and long story short that's actually the default name of a program that's created
283:57 default name of a program that's created when you just run C by itself now that's
283:59 when you just run C by itself now that's a pretty uh bad name for a program
284:01 a pretty uh bad name for a program because it doesn't describe what it said
284:03 because it doesn't describe what it said does so better would be here to perhaps
284:06 does so better would be here to perhaps do well instead of a.out which yes still
284:09 do well instead of a.out which yes still prints hello. world but isn't really a a
284:12 prints hello. world but isn't really a a clearly named program it'd be nice to
284:14 clearly named program it'd be nice to name this hello so what could I do I
284:16 name this hello so what could I do I could do like we learned last week well
284:17 could do like we learned last week well I could rename a.out to Hello by using
284:19 I could rename a.out to Hello by using linux's MV command so I'm going to move
284:22 linux's MV command so I'm going to move a. out to become hello but that too
284:25 a. out to become hello but that too seems kind of tedious now I have three
284:27 seems kind of tedious now I have three steps like write my code compile my code
284:29 steps like write my code compile my code and then rename it before I can even run
284:31 and then rename it before I can even run it like we can do better than that and
284:33 it like we can do better than that and so it turns out that certain commands
284:35 so it turns out that certain commands like clang support what we're going to
284:37 like clang support what we're going to start today calling commandline
284:39 start today calling commandline arguments a commandline argument unlike
284:42 arguments a commandline argument unlike an argument to a function is just an
284:44 an argument to a function is just an additional word or key phrase that you
284:46 additional word or key phrase that you type after a command at your prompt in
284:49 type after a command at your prompt in your terminal window that just modifies
284:51 your terminal window that just modifies the behavior of that command it
284:53 the behavior of that command it configures it a little more specifically
284:55 configures it a little more specifically so what you're seeing here on the screen
284:56 so what you're seeing here on the screen is a summary of a better command with
284:58 is a summary of a better command with which to run clang so that now I can
285:00 which to run clang so that now I can specify the output of this command this-
285:04 specify the output of this command this- o so what do I mean by that well let me
285:06 o so what do I mean by that well let me go ahead and clear my terminal window
285:07 go ahead and clear my terminal window again and more explicitly type clang d o
285:11 again and more explicitly type clang d o hello hello. C and then enter nothing
285:15 hello hello. C and then enter nothing again appears to happen but that's a
285:16 again appears to happen but that's a good thing when you see no errors and
285:18 good thing when you see no errors and now the program I just created is indeed
285:21 now the program I just created is indeed called hello so it achieves really the
285:24 called hello so it achieves really the same exact effect as make did but what I
285:27 same exact effect as make did but what I don't have to do with make is type and
285:28 don't have to do with make is type and remember something as long as this
285:30 remember something as long as this command and this too is a bit of a white
285:32 command and this too is a bit of a white lie it turns out we have preconfigured
285:34 lie it turns out we have preconfigured vs code in the cloud for you to also use
285:37 vs code in the cloud for you to also use some other features of clang that would
285:40 some other features of clang that would be even more tedious for you to write
285:42 be even more tedious for you to write yourselves and so really this is why we
285:44 yourselves and so really this is why we distill this as ultimately just running
285:47 distill this as ultimately just running make so let me pause here to see first
285:49 make so let me pause here to see first if there's any questions on what I've
285:51 if there's any questions on what I've done by taking my very first program in
285:53 done by taking my very first program in C and just now compiling it first with
285:56 C and just now compiling it first with make but then starting over and now
285:57 make but then starting over and now manually compiling it with clang with
286:00 manually compiling it with clang with what we'll call command line arguments -
286:02 what we'll call command line arguments - o space hello and then the name of the
286:06 o space hello and then the name of the file
286:08 file yeah yeah so a.out is a historical name
286:11 yeah yeah so a.out is a historical name it refers to assembler output more on
286:14 it refers to assembler output more on that soon and it's just the default file
286:16 that soon and it's just the default file name that you get automatically if you
286:18 name that you get automatically if you just run the compiler on any file so
286:20 just run the compiler on any file so that you have just a standard name for
286:22 that you have just a standard name for it but it's not a very well-named
286:24 it but it's not a very well-named program instead of running Microsoft
286:26 program instead of running Microsoft Word on your Mac or PC it would like be
286:27 Word on your Mac or PC it would like be like double clicking on a.out so instead
286:30 like double clicking on a.out so instead with these command line arguments you
286:31 with these command line arguments you can customize the output of clang and
286:34 can customize the output of clang and call it hello or anything you want other
286:37 call it hello or anything you want other questions on what I've done here with
286:39 questions on what I've done here with clang itself the
286:42 clang itself the compiler
286:44 compiler yeah so- o and you would only know this
286:47 yeah so- o and you would only know this from reading the manual taking a class
286:49 from reading the manual taking a class means output so- o means change clangs
286:53 means output so- o means change clangs output to be a file called hello instead
286:56 output to be a file called hello instead of the default which is a.out and this
286:58 of the default which is a.out and this too is again a detail you would have to
287:00 too is again a detail you would have to uh look it up on a web page read the
287:02 uh look it up on a web page read the manual hear someone like me tell you
287:03 manual hear someone like me tell you about it and in fact there's even more
287:05 about it and in fact there's even more than these options but we'll just
287:07 than these options but we'll just scratch the surface here all right so if
287:09 scratch the surface here all right so if we now know this what more is actually
287:12 we now know this what more is actually happening underneath the hood well let's
287:13 happening underneath the hood well let's take a a closer look at not just this
287:16 take a a closer look at not just this version of my code but my slightly more
287:18 version of my code but my slightly more complicated version last week which
287:21 complicated version last week which looked a little something like this
287:22 looked a little something like this wherein I added in some Dynamic input
287:25 wherein I added in some Dynamic input from the user so I could say not Hello
287:26 from the user so I could say not Hello World to everyone but hello David or
287:29 World to everyone but hello David or hello to whoever actually runs this
287:30 hello to whoever actually runs this program so in fact let me go ahead and
287:33 program so in fact let me go ahead and change my code here in vs code just to
287:35 change my code here in vs code just to match that same code from last week so
287:37 match that same code from last week so no new code yet I'm just going to in a
287:40 no new code yet I'm just going to in a moment compile it in a slightly
287:41 moment compile it in a slightly different way so I did last week string
287:44 different way so I did last week string uh I think answer equals get string
287:47 uh I think answer equals get string quote unquote what's your name just like
287:50 quote unquote what's your name just like in scratch and then down here instead of
287:53 in scratch and then down here instead of doing world I initially wrote answer but
287:55 doing world I initially wrote answer but that didn't go well what did I
287:58 that didn't go well what did I ultimately do instead to print out hello
288:00 ultimately do instead to print out hello David or hello so and so yeah sorry a
288:04 David or hello so and so yeah sorry a little louder yeah so percent s the
288:07 little louder yeah so percent s the so-called format code that printf just
288:09 so-called format code that printf just knows how to deal with and I had to add
288:11 knows how to deal with and I had to add one other thing someone else besides
288:13 one other thing someone else besides percent F
288:14 percent F yeah the name of the variable that I
288:17 yeah the name of the variable that I want to plug into that placeholder
288:19 want to plug into that placeholder percent s and in this case it's answer
288:21 percent s and in this case it's answer now let me make one refinement only
288:23 now let me make one refinement only because now we're in week two and we're
288:24 because now we're in week two and we're going to start writing more lines of
288:25 going to start writing more lines of code even though scratch called the
288:27 code even though scratch called the return value of the ask puzzle piece
288:30 return value of the ask puzzle piece answer always and see we have full
288:32 answer always and see we have full control over what our variables are
288:33 control over what our variables are called and now it's probably good not to
288:35 called and now it's probably good not to just generically always call my variable
288:37 just generically always call my variable answer if I'm using get string let's
288:40 answer if I'm using get string let's call it what it is so this is now just a
288:41 call it what it is so this is now just a matter of style if you will let me
288:43 matter of style if you will let me change the variable to be name just so
288:46 change the variable to be name just so that it's a little clear to me to you to
288:48 that it's a little clear to me to you to a TF or ta exactly what that variable
288:51 a TF or ta exactly what that variable represents instead of more generically
288:53 represents instead of more generically answer all right so that said let me go
288:55 answer all right so that said let me go down to my terminal window and last week
288:57 down to my terminal window and last week again I ran make to compile this exact
288:59 again I ran make to compile this exact same program now though let me go ahead
289:02 same program now though let me go ahead and just use clang so clang d o I'll
289:05 and just use clang so clang d o I'll still call this version hello space
289:08 still call this version hello space hello.c so exact same command as before
289:10 hello.c so exact same command as before the only thing that's different is I've
289:12 the only thing that's different is I've added a couple of more lines of code to
289:14 added a couple of more lines of code to get the user's input let me hit enter
289:17 get the user's input let me hit enter and now darn it our first error so
289:19 and now darn it our first error so output from clang and make is not a good
289:22 output from clang and make is not a good thing and here we're seeing something
289:23 thing and here we're seeing something particularly cryptic uh so something in
289:26 particularly cryptic uh so something in function main undefined reference to get
289:29 function main undefined reference to get string and then Linker command failed
289:32 string and then Linker command failed with exit code one so there's actually a
289:33 with exit code one so there's actually a lot of jargon in there that will tease
289:35 lot of jargon in there that will tease apart today but my hint is that clearly
289:38 apart today but my hint is that clearly my problem's in Maine although that's
289:39 my problem's in Maine although that's not surprising because there's nothing
289:40 not surprising because there's nothing else going on here get string is an
289:42 else going on here get string is an issue and the uh issue is that it's an
289:45 issue and the uh issue is that it's an undefined reference and yet notice I was
289:48 undefined reference and yet notice I was pretty good I added the cs50 header file
289:51 pretty good I added the cs50 header file and I said last week that that's enough
289:52 and I said last week that that's enough to teach the compiler that functions
289:55 to teach the compiler that functions exist but the problem is that even
289:57 exist but the problem is that even though this does in fact teach clang
290:00 though this does in fact teach clang that get string exists it is not
290:02 that get string exists it is not sufficient information for clang to go
290:05 sufficient information for clang to go find on the hard drive of the computer
290:07 find on the hard drive of the computer the zeros and ones that actually
290:09 the zeros and ones that actually Implement get string itself so in other
290:12 Implement get string itself so in other words this include line per last week is
290:14 words this include line per last week is a little bit of a hint it's a teaser to
290:15 a little bit of a hint it's a teaser to CLA that you're about to see and use
290:18 CLA that you're about to see and use this function somewhere but if you
290:19 this function somewhere but if you actually want to use the zeros and ones
290:23 actually want to use the zeros and ones that cs50 wrote some time ago and bake
290:26 that cs50 wrote some time ago and bake those into your program so your program
290:28 those into your program so your program actually knows how to get input from the
290:29 actually knows how to get input from the user well then I'm going to have to go
290:32 user well then I'm going to have to go ahead and run a slightly different
290:34 ahead and run a slightly different command so let me do this let me clear
290:36 command so let me do this let me clear my terminal window just to get rid of
290:37 my terminal window just to get rid of that distraction and let me propose now
290:40 that distraction and let me propose now that we run this command instead almost
290:43 that we run this command instead almost the same as before clang - o space hello
290:47 the same as before clang - o space hello then hello. C but with one additional
290:50 then hello. C but with one additional command line argument at the end and
290:52 command line argument at the end and this is a-h L not a number one so- L
290:56 this is a-h L not a number one so- L cs50 with no space in between those two
290:59 cs50 with no space in between those two now the L is going to result in all of
291:01 now the L is going to result in all of those zeros and ones that actually were
291:03 those zeros and ones that actually were written by cs50 being linked into your
291:06 written by cs50 being linked into your code your few lines of code or mine here
291:08 code your few lines of code or mine here but that's the second step that the
291:10 but that's the second step that the compiler requires in order to know how
291:13 compiler requires in order to know how to actually execute and rather compile
291:16 to actually execute and rather compile your code and cs50's and cs50 is not the
291:19 your code and cs50's and cs50 is not the only one that does this if you use any
291:21 only one that does this if you use any third-party library in C that doesn't
291:23 third-party library in C that doesn't come with the language you would do- L
291:26 come with the language you would do- L such and such where whoever however
291:28 such and such where whoever however they've named their own library but you
291:30 they've named their own library but you don't have to do it for built-in things
291:31 don't have to do it for built-in things like uh like we've been using thus far
291:34 like uh like we've been using thus far all right so let me go ahead and try
291:35 all right so let me go ahead and try this I'll go back to vs code here and
291:37 this I'll go back to vs code here and let me go ahead now and run clang - o
291:41 let me go ahead now and run clang - o hello then hello. C and now instead of
291:44 hello then hello. C and now instead of just hitting enter - L cs50 with no
291:46 just hitting enter - L cs50 with no space between the L and the cs50 enter
291:49 space between the L and the cs50 enter now nothing bad happens and now I can
291:51 now nothing bad happens and now I can do/ hello what's your name I'll type in
291:54 do/ hello what's your name I'll type in David enter and now we see hello David
291:57 David enter and now we see hello David now honestly this is where we're really
291:59 now honestly this is where we're really getting into the weeds and now this is
292:01 getting into the weeds and now this is taking this is really just adding new to
292:04 taking this is really just adding new to the process of compiling and running
292:05 the process of compiling and running your code and so the reality is even
292:07 your code and so the reality is even though this is indeed what is happening
292:09 though this is indeed what is happening this is why we used last week and we're
292:11 this is why we used last week and we're going to continue using this week onward
292:13 going to continue using this week onward make because it just automates that
292:15 make because it just automates that whole process for you but it's ideal to
292:18 whole process for you but it's ideal to understand what's going wrong because
292:19 understand what's going wrong because any of the error messages you saw for
292:20 any of the error messages you saw for problem set one any of the error
292:22 problem set one any of the error messages you see for the next few weeks
292:24 messages you see for the next few weeks probably aren't coming from make they're
292:26 probably aren't coming from make they're coming from clang underneath the hood
292:28 coming from clang underneath the hood because make is just automating the
292:30 because make is just automating the process but with make you literally just
292:31 process but with make you literally just write make and then the name of the
292:33 write make and then the name of the program you don't have to worry about
292:34 program you don't have to worry about any of those command line arguments
292:37 any of those command line arguments questions then on compiling with- l cs50
292:41 questions then on compiling with- l cs50 or anything else
292:43 or anything else yeah sorry what is the benefit
292:46 yeah sorry what is the benefit of what is the benefit of using clang
292:49 of what is the benefit of using clang manually none really in fact all main is
292:51 manually none really in fact all main is doing is just sa make is doing is saving
292:53 doing is just sa make is doing is saving us some keystrokes um if you prefer
292:55 us some keystrokes um if you prefer though and you just like to be more in
292:57 though and you just like to be more in control you can totally run clang
292:58 control you can totally run clang manually if you remember the various
293:00 manually if you remember the various command line arguments yeah
293:09 exp exactly why did I have to explain that is provide a hint to cs50 with the
293:11 that is provide a hint to cs50 with the cs50.h henter file but I didn't have to
293:13 cs50.h henter file but I didn't have to do that with standard i.h just because
293:16 do that with standard i.h just because standard i.h comes with C just like a
293:19 standard i.h comes with C just like a few other libraries come with C that
293:21 few other libraries come with C that we'll start seeing today um cs50 though
293:23 we'll start seeing today um cs50 though is not built into C everywhere and so
293:25 is not built into C everywhere and so you do have to explicitly add that one
293:27 you do have to explicitly add that one there
293:34 yeah a command line argument is a a word or phrase that you type at the command
293:36 or phrase that you type at the command line AKA your terminal in order to
293:40 line AKA your terminal in order to influence the behavior of a
293:42 influence the behavior of a program for whatever you're doing yeah
293:44 program for whatever you're doing yeah it changes the defaults right in our
293:45 it changes the defaults right in our guey World graphical user interface you
293:47 guey World graphical user interface you and I would probably click some boxes we
293:49 and I would probably click some boxes we would select some menu options to
293:51 would select some menu options to configure a program to behave in the
293:52 configure a program to behave in the same way at a command line interface you
293:54 same way at a command line interface you have to just say everything all at once
293:56 have to just say everything all at once and that's why we have command line
293:58 and that's why we have command line arguments
294:05 yeah no make is not just for cs50 it's used globally in any project really
294:08 used globally in any project really nowadays using C C++ even other
294:10 nowadays using C C++ even other languages as well in fact most every
294:12 languages as well in fact most every command you see in this class unless it
294:14 command you see in this class unless it has 5 zero at the end of it is globally
294:16 has 5 zero at the end of it is globally used only those suffix with 50 are
294:19 used only those suffix with 50 are indeed course specific and even those
294:21 indeed course specific and even those will gradually take training wheels off
294:22 will gradually take training wheels off of so that you know exactly what those
294:24 of so that you know exactly what those commands are doing as well all right so
294:27 commands are doing as well all right so what is it that we've just done
294:29 what is it that we've just done everything we've just done of course I
294:30 everything we've just done of course I keep calling compiling but let's just go
294:32 keep calling compiling but let's just go down one Rabbit Hole so that you
294:33 down one Rabbit Hole so that you understand that when you compile code
294:35 understand that when you compile code there's actually a whole bunch of steps
294:37 there's actually a whole bunch of steps happening and this is going to enable uh
294:39 happening and this is going to enable uh a lot of features like companies can
294:41 a lot of features like companies can write code and then convert it to run it
294:44 write code and then convert it to run it on Macs and PCs alike or phones or the
294:46 on Macs and PCs alike or phones or the like so it's not just a matter of
294:48 like so it's not just a matter of converting source code to machine code
294:50 converting source code to machine code there's actually four steps involved in
294:52 there's actually four steps involved in what you and I as of last week know as
294:54 what you and I as of last week know as compiling and these aren't terms that
294:56 compiling and these aren't terms that you'll have to keep in mind constantly
294:59 you'll have to keep in mind constantly because again we're going to abstract a
295:00 because again we're going to abstract a lot of this away but just so we've gone
295:02 lot of this away but just so we've gone down the rabbit hole once let's consider
295:03 down the rabbit hole once let's consider each of these four steps that have been
295:05 each of these four steps that have been happening for you for a week
295:07 happening for you for a week automatically uh the first of which is
295:09 automatically uh the first of which is called pre-processing so what is this
295:11 called pre-processing so what is this mean well let's consider that same
295:13 mean well let's consider that same program as before notice that a two of
295:15 program as before notice that a two of the lines of code start with a hash mark
295:17 the lines of code start with a hash mark that is a special symbol in C and it's a
295:20 that is a special symbol in C and it's a so-called pre-processor directive you
295:22 so-called pre-processor directive you don't need to memorize terms like that
295:24 don't need to memorize terms like that but it just means that it's a little
295:25 but it just means that it's a little different from every other line and
295:26 different from every other line and anything with a hash symbol here should
295:28 anything with a hash symbol here should be pre-processed that is analyzed
295:30 be pre-processed that is analyzed initially before anything else happens
295:33 initially before anything else happens so let's consider these two lines up top
295:35 so let's consider these two lines up top what exactly is happening well it turns
295:37 what exactly is happening well it turns out with these two lines you have two
295:39 out with these two lines you have two header files of course cs50.h and
295:41 header files of course cs50.h and standard. i.h where are those files
295:44 standard. i.h where are those files because they've never been in VSS code
295:47 because they've never been in VSS code for you seemingly if you type LS if you
295:49 for you seemingly if you type LS if you open up the file explorer in the GUI you
295:51 open up the file explorer in the GUI you have never seen probably cs50.h or
295:54 have never seen probably cs50.h or standard i.h they just work but that's
295:57 standard i.h they just work but that's because there's a folder somewhere on
296:00 because there's a folder somewhere on the uh the hard drive that you're using
296:01 the uh the hard drive that you're using on your Mac or PC or somewhere in the
296:03 on your Mac or PC or somewhere in the cloud as in our case and inside of this
296:06 cloud as in our case and inside of this folder traditionally called sluser SL
296:09 folder traditionally called sluser SL include and user is deliberately
296:11 include and user is deliberately misspelled it's just slightly more
296:12 misspelled it's just slightly more succinct although it's a little weird
296:14 succinct although it's a little weird why we drop that one letter but user SL
296:16 why we drop that one letter but user SL include is just a folder on the server
296:19 include is just a folder on the server that contains cs50.h standard i.h and a
296:21 that contains cs50.h standard i.h and a bunch of other things as well so in fact
296:24 bunch of other things as well so in fact if you type in uh VSS code in your
296:26 if you type in uh VSS code in your terminal window uh when you're using
296:28 terminal window uh when you're using Code spaces in the cloud and type LS
296:31 Code spaces in the cloud and type LS space SL user include you can can see
296:33 space SL user include you can can see all of the files in that folder but
296:35 all of the files in that folder but we've pre-installed all of that stuff
296:36 we've pre-installed all of that stuff for you so let's consider what's
296:38 for you so let's consider what's actually in those files here where if I
296:40 actually in those files here where if I highlight these two lines up top that
296:42 highlight these two lines up top that start with hash include well I kind of
296:46 start with hash include well I kind of hinted last week that what's in that
296:47 hinted last week that what's in that first file is a hint as to what
296:50 first file is a hint as to what functions cs50 wrote for you so you can
296:53 functions cs50 wrote for you so you can kind of think of these include lines as
296:55 kind of think of these include lines as being temporary placeholders for what's
296:58 being temporary placeholders for what's going to become like a global find and
297:00 going to become like a global find and replace that is the first thing clang is
297:02 replace that is the first thing clang is going to do it's pre-process this file
297:04 going to do it's pre-process this file it's going to look for any line that
297:05 it's going to look for any line that starts with hash include and if it sees
297:08 starts with hash include and if it sees that it's going to essentially go into
297:10 that it's going to essentially go into that file like cs50.h and then just copy
297:13 that file like cs50.h and then just copy and paste the contents of that file
297:15 and paste the contents of that file magically there for you you don't see it
297:16 magically there for you you don't see it visually on the screen but it's
297:18 visually on the screen but it's happening behind the scenes and so
297:20 happening behind the scenes and so really what's happening with this first
297:22 really what's happening with this first line is that somewhere in
297:25 line is that somewhere in cs50.h is the Declaration of get string
297:29 cs50.h is the Declaration of get string like we talked last week and it probably
297:31 like we talked last week and it probably looks a little something like this and
297:33 looks a little something like this and we didn't spend much time on this yet
297:34 we didn't spend much time on this yet this past week but we will in time more
297:37 this past week but we will in time more notice that this is how the a function
297:40 notice that this is how the a function is declared that is it is decreed to
297:42 is declared that is it is decreed to exist the name of the function of course
297:44 exist the name of the function of course is get string inside of the parenthesis
297:46 is get string inside of the parenthesis are its arguments in this case there's
297:48 are its arguments in this case there's one argument to get string I claim today
297:51 one argument to get string I claim today but you've known this implicitly and
297:53 but you've known this implicitly and it's a prompt it's the prompt that the
297:54 it's a prompt it's the prompt that the human sees when you use get string what
297:56 human sees when you use get string what is that prompt well it's a string of
297:58 is that prompt well it's a string of text like quote unquote what's your name
298:00 text like quote unquote what's your name or anything else that I asked last week
298:02 or anything else that I asked last week mean meanwhile get string as we know
298:04 mean meanwhile get string as we know from last week has a return value it
298:06 from last week has a return value it returns something to you and that too is
298:08 returns something to you and that too is a string so again this is also called a
298:10 a string so again this is also called a functions prototype it's the thing
298:12 functions prototype it's the thing toward the end of last week that I just
298:14 toward the end of last week that I just copied and pasted from the bottom of my
298:16 copied and pasted from the bottom of my file to the top just so that it was like
298:18 file to the top just so that it was like this teaser for clang as to what would
298:20 this teaser for clang as to what would exist later so you can think then of
298:23 exist later so you can think then of these include lines as just kind of uh
298:26 these include lines as just kind of uh combining all of those function
298:28 combining all of those function declarations in some separate file
298:29 declarations in some separate file called cs50.h so that you yourself don't
298:32 called cs50.h so that you yourself don't have to type them every time you use the
298:34 have to type them every time you use the library or worse so that you yourself
298:35 library or worse so that you yourself don't have to copy and paste those lines
298:38 don't have to copy and paste those lines this is what clang is doing for you in
298:39 this is what clang is doing for you in its first step of pre-processing second
298:42 its first step of pre-processing second and last in this example what happens
298:45 and last in this example what happens when clang pre-processes this second
298:48 when clang pre-processes this second include line well the only other
298:49 include line well the only other function we care about in this story is
298:51 function we care about in this story is printf of course which comes with C so
298:54 printf of course which comes with C so essentially you can think of printf's
298:56 essentially you can think of printf's prototype or Declaration as just being
299:00 prototype or Declaration as just being this print f is the name of the function
299:02 this print f is the name of the function it takes a string that you want to
299:04 it takes a string that you want to format like hello comma world or hello
299:07 format like hello comma world or hello comma percent s and then with dot dot
299:10 comma percent s and then with dot dot dot this actually has technical meaning
299:11 dot this actually has technical meaning it means of course that you can plug in
299:13 it means of course that you can plug in zero variables one variable two or 10 so
299:16 zero variables one variable two or 10 so dot dot dot means some number of
299:17 dot dot dot means some number of variables now we haven't talked about
299:19 variables now we haven't talked about this yet and we won't really in general
299:21 this yet and we won't really in general print F actually returns a value a
299:23 print F actually returns a value a number that is an integer but more on
299:26 number that is an integer but more on that perhaps another time it's generally
299:27 that perhaps another time it's generally not something the programmer tends to
299:30 not something the programmer tends to look at but that's all we mean by
299:31 look at but that's all we mean by pre-processing so that the the end of
299:33 pre-processing so that the the end of this process even though there's more
299:34 this process even though there's more lines of code in cs50.h and standard i.h
299:38 lines of code in cs50.h and standard i.h what's really just happening is that
299:39 what's really just happening is that clang in pre-processing the file copies
299:42 clang in pre-processing the file copies and pastes the contents of those files
299:44 and pastes the contents of those files into your code so that now your code
299:46 into your code so that now your code knows about everything get string printf
299:49 knows about everything get string printf and anything else any questions then on
299:51 and anything else any questions then on that first step
299:54 that first step pre-processing
300:11 good question when you include a file does it only include what you need or
300:13 does it only include what you need or Does it include everything think of it
300:15 Does it include everything think of it as including everything so if it's a big
300:16 as including everything so if it's a big file that's a lot of code at the very
300:18 file that's a lot of code at the very top and that's why if you think back to
300:20 top and that's why if you think back to all of the zeros and ones I showed a
300:22 all of the zeros and ones I showed a little bit ago as well as last week
300:23 little bit ago as well as last week there's a lot of zeros and ones that end
300:25 there's a lot of zeros and ones that end up on the screen as a result of just
300:27 up on the screen as a result of just writing hello world a lot of those zeros
300:29 writing hello world a lot of those zeros and ones are perhaps coming from code
300:31 and ones are perhaps coming from code that you didn't actually necessarily
300:32 that you didn't actually necessarily need but some of it is perhaps there but
300:34 need but some of it is perhaps there but there are ways to optimize that as well
300:37 there are ways to optimize that as well all right so step two of compiling is
300:40 all right so step two of compiling is confusingly called compiling it's just
300:42 confusingly called compiling it's just this is the term that most everyone uses
300:44 this is the term that most everyone uses to describe the whole process instead of
300:46 to describe the whole process instead of just this one step but once a program
300:49 just this one step but once a program has been pre-processed uh behind the
300:51 has been pre-processed uh behind the scenes by the compiler for you it looks
300:54 scenes by the compiler for you it looks now a little something like this and
300:55 now a little something like this and I've put dot dot dots just to imply that
300:57 I've put dot dot dots just to imply that yes to your question there's more stuff
300:59 yes to your question there's more stuff above it there's more stuff below it
301:00 above it there's more stuff below it it's just not interesting right now for
301:02 it's just not interesting right now for us so now we have just C code there's no
301:05 us so now we have just C code there's no more pre-processor directives at this
301:07 more pre-processor directives at this point all of the hash symbols and those
301:08 point all of the hash symbols and those lines of code have been pre-processed
301:10 lines of code have been pre-processed and convert it to something else and so
301:13 and convert it to something else and so now and this is where things get a
301:14 now and this is where things get a little spooky looking uh here now is
301:17 little spooky looking uh here now is what happens when clang or any compiler
301:20 what happens when clang or any compiler literally compiles code like this it
301:23 literally compiles code like this it converts it from this in C to this in
301:27 converts it from this in C to this in assembly code so this is among the
301:29 assembly code so this is among the scarier languages I myself don't really
301:31 scarier languages I myself don't really have fond memories this is not language
301:33 have fond memories this is not language that many people program in if you take
301:34 that many people program in if you take a subsequent class in computer science
301:36 a subsequent class in computer science in systems uh a higher level class you
301:38 in systems uh a higher level class you might actually learn this or some
301:40 might actually learn this or some variant there of but there's at least a
301:42 variant there of but there's at least a few people out there that need to know
301:43 few people out there that need to know this stuff because this is closer to
301:46 this stuff because this is closer to what the computers themselves nowadays
301:48 what the computers themselves nowadays understand like the Intel CPUs or the
301:51 understand like the Intel CPUs or the AMD CPUs the brains of today's computers
301:53 AMD CPUs the brains of today's computers and phones understand stuff that looks
301:55 and phones understand stuff that looks more like this and less like C now it's
301:58 more like this and less like C now it's completely uh esoteric but let me just
302:00 completely uh esoteric but let me just highlight a few phrases there's some
302:02 highlight a few phrases there's some stuff that's a little familiar there is
302:04 stuff that's a little familiar there is mention of Maine at the top there in
302:06 mention of Maine at the top there in yellow there is mention of get string
302:08 yellow there is mention of get string toward the bottom there is mention of
302:10 toward the bottom there is mention of prf down below so this is just another
302:13 prf down below so this is just another programming language called Assembly
302:14 programming language called Assembly Language that decades ago humans myself
302:17 Language that decades ago humans myself included in school did write code in and
302:20 included in school did write code in and absolutely some people still write this
302:22 absolutely some people still write this code especially since you can write very
302:24 code especially since you can write very very efficient code but it's a lot more
302:27 very efficient code but it's a lot more uh Arcane it's a lot more uh lot less
302:30 uh Arcane it's a lot more uh lot less user friendly so you'll see in Yello now
302:32 user friendly so you'll see in Yello now the these are the so-called instructions
302:34 the these are the so-called instructions that a computer's brain or CPU
302:36 that a computer's brain or CPU understands pushing values around moving
302:39 understands pushing values around moving them subtracting values uh calling
302:42 them subtracting values uh calling functions and move move move so really
302:44 functions and move move move so really the low-level operations that computers
302:46 the low-level operations that computers understand tend to be arithmetic
302:49 understand tend to be arithmetic operations subtraction addition and the
302:51 operations subtraction addition and the like moving things in and out of memory
302:54 like moving things in and out of memory it's just a lot more tedious for folks
302:55 it's just a lot more tedious for folks like us to write code like this this is
302:57 like us to write code like this this is why you and I tend to write stuff like
302:59 why you and I tend to write stuff like this and ideally still people like you
303:02 this and ideally still people like you and I tend to drag and drop puzzle
303:03 and I tend to drag and drop puzzle pieces that sort of abstract all of that
303:05 pieces that sort of abstract all of that away further but for now this is again
303:07 away further but for now this is again called Assembly Language it is what
303:09 called Assembly Language it is what happens when the compiler literally
303:12 happens when the compiler literally compiles your code but of course this
303:15 compiles your code but of course this still not zeros and ones so we got two
303:17 still not zeros and ones so we got two steps to go so when a compiler proceeds
303:20 steps to go so when a compiler proceeds to step three this is where things get
303:23 to step three this is where things get converted to machine code and when a
303:25 converted to machine code and when a compiler assembles your code for you it
303:28 compiler assembles your code for you it converts what we just saw on the screen
303:30 converts what we just saw on the screen here to actual zeros and ones the
303:34 here to actual zeros and ones the so-called machine code that your phone
303:36 so-called machine code that your phone or your computer understands but it's
303:38 or your computer understands but it's worth noting that these are not
303:41 worth noting that these are not necessarily all of the zeros and ones of
303:43 necessarily all of the zeros and ones of your program yes they re uh they are the
303:46 your program yes they re uh they are the zeros and ones that correspond to your
303:48 zeros and ones that correspond to your hello program or printf and get string
303:51 hello program or printf and get string and the like but notice that here we
303:54 and the like but notice that here we need one final step in those zeros and
303:57 need one final step in those zeros and ones are only your lines of code but
304:00 ones are only your lines of code but what about cs50's lines of code that we
304:02 what about cs50's lines of code that we wrote to Implement get string what about
304:03 wrote to Implement get string what about the lines of code that humans wrote
304:05 the lines of code that humans wrote decades ago to implement printf those
304:07 decades ago to implement printf those are somewhere on this hard drive like on
304:08 are somewhere on this hard drive like on my Mac my PC or somewhere in the cloud
304:11 my Mac my PC or somewhere in the cloud but we need to combine all of those
304:13 but we need to combine all of those zeros and ones together and Link My code
304:17 zeros and ones together and Link My code with cs50's code with uh standard io's
304:20 with cs50's code with uh standard io's code all together and so what happens in
304:23 code all together and so what happens in the last step ultimately is that if we
304:26 the last step ultimately is that if we have my code here in yellow and then the
304:28 have my code here in yellow and then the code that cs50 wrote and the code that
304:30 code that cs50 wrote and the code that the authors of C itself wrote what
304:32 the authors of C itself wrote what really is happening is that somewhere we
304:35 really is happening is that somewhere we have not only hello.c which obviously I
304:37 have not only hello.c which obviously I wrote and wrote with us live here
304:39 wrote and wrote with us live here there's also let's assume somewhere on
304:42 there's also let's assume somewhere on the computer a cs50.c file that
304:45 the computer a cs50.c file that coincidentally I and cs50 staff wrote
304:47 coincidentally I and cs50 staff wrote years ago and also somewhere on the
304:49 years ago and also somewhere on the computer there's another file Let Me
304:50 computer there's another file Let Me oversimplify by just calling it standard
304:52 oversimplify by just calling it standard io. C in practice it's probably
304:54 io. C in practice it's probably specifically called print F.C but
304:56 specifically called print F.C but there's somewhere these two other files
304:59 there's somewhere these two other files and so this last step called linking
305:02 and so this last step called linking takes my zeros and ones from the code I
305:04 takes my zeros and ones from the code I just wrote namely this code on the
305:06 just wrote namely this code on the screen here it then grabs the zeros and
305:09 screen here it then grabs the zeros and ones that cs50 wrote and it grabs the
305:11 ones that cs50 wrote and it grabs the zeros and ones that the authors of C
305:12 zeros and ones that the authors of C wrote In order to implement the standard
305:14 wrote In order to implement the standard IO library and lastly voila links them
305:19 IO library and lastly voila links them all together and this is the same blob
305:21 all together and this is the same blob of zeros and ones that we saw earlier
305:23 of zeros and ones that we saw earlier it's just now the result of
305:26 it's just now the result of pre-processing your code compiling your
305:28 pre-processing your code compiling your code assembling your code linking your
305:31 code assembling your code linking your code and my God it's at this point like
305:33 code and my God it's at this point like if there were any fun in programming for
305:35 if there were any fun in programming for you yet we've just taken it all away we
305:37 you yet we've just taken it all away we just call this whole process compiling
305:39 just call this whole process compiling why because now that we know those steps
305:41 why because now that we know those steps exist and smart people solve that
305:44 exist and smart people solve that problem for us you and I can kind of
305:45 problem for us you and I can kind of operate at this level of abstraction and
305:47 operate at this level of abstraction and just assume that compiling converts
305:49 just assume that compiling converts source code to machine code questions
305:52 source code to machine code questions though on any of these
305:54 though on any of these intermediate steps
306:11 a good question so where are all of these zeros in one store because you and
306:13 these zeros in one store because you and I we've been using a browser at code.
306:15 I we've been using a browser at code. cs50. of course is this web-based user
306:17 cs50. of course is this web-based user interface but again recall from last
306:19 interface but again recall from last week even though you're using a web
306:20 week even though you're using a web browser to access VSS code that
306:24 browser to access VSS code that web-based version of vs code is
306:25 web-based version of vs code is connected to an actual server somewhere
306:27 connected to an actual server somewhere in the cloud and on that server you have
306:30 in the cloud and on that server you have your own account and your own file and
306:32 your own account and your own file and really your own hard drive virtually in
306:34 really your own hard drive virtually in the cloud think of it a little like
306:36 the cloud think of it a little like Dropbox or box or Google drive or one
306:38 Dropbox or box or Google drive or one drive or something like that so you have
306:40 drive or something like that so you have a hard drive somewhere out there that
306:41 a hard drive somewhere out there that we've provisioned for you and it's on
306:43 we've provisioned for you and it's on that hard drive that we have uh your
306:46 that hard drive that we have uh your code that you just wrote or I just wrote
306:49 code that you just wrote or I just wrote cs50.c standard I.C and all of the other
306:52 cs50.c standard I.C and all of the other code that implements the math functions
306:53 code that implements the math functions and everything else that c
306:55 and everything else that c supports good question
306:58 supports good question yeah
307:01 yeah c
307:14 good question that uh hash includes cs50.h line at the top of my code if I
307:17 cs50.h line at the top of my code if I just replace that with the contents of
307:19 just replace that with the contents of cs50.c would that work short answer yes
307:22 cs50.c would that work short answer yes that would work you could copy all of
307:24 that would work you could copy all of the code there however there's some
307:26 the code there however there's some order of operations that might come into
307:28 order of operations that might come into play and so it's probably not quite as
307:29 play and so it's probably not quite as simple as copy paste but conceptually
307:31 simple as copy paste but conceptually yes that's what what's happening now
307:33 yes that's what what's happening now with that said in
307:35 with that said in cs50.h are only the prototypes of the
307:38 cs50.h are only the prototypes of the functions the hints as to how the
307:41 functions the hints as to how the functions look what their return type is
307:43 functions look what their return type is what their name is and what their
307:44 what their name is and what their arguments are it's in the C file that
307:47 arguments are it's in the C file that actual code tends to be written and this
307:49 actual code tends to be written and this is a little confusing now because you
307:51 is a little confusing now because you and I have only written code in C files
307:53 and I have only written code in C files but in the next few weeks you'll
307:55 but in the next few weeks you'll actually start writing some of your own
307:57 actually start writing some of your own files as well just like cs50 just like
307:59 files as well just like cs50 just like standard iio but in essence that line of
308:01 standard iio but in essence that line of code just makes it easier to use and
308:03 code just makes it easier to use and reuse code that's already been written
308:05 reuse code that's already been written and that's the whole point of a
308:16 louder yes does linking happen when you compile your code yes when you run make
308:19 compile your code yes when you run make as we have been doing the past week now
308:22 as we have been doing the past week now all four of these steps are happening
308:24 all four of these steps are happening pre-processing converts the hash include
308:26 pre-processing converts the hash include lines to something else compiling
308:28 lines to something else compiling technically converts it to assembly code
308:30 technically converts it to assembly code which the Mac the PC the server more
308:32 which the Mac the PC the server more closely understands assembly converts
308:35 closely understands assembly converts that language to Binary machine code
308:37 that language to Binary machine code that this computer actually understands
308:40 that this computer actually understands and then linking combines everything
308:42 and then linking combines everything together and in fact if you think back a
308:43 together and in fact if you think back a few minutes ago to when I did this- L
308:46 few minutes ago to when I did this- L cs50 the reason I had to add that and
308:49 cs50 the reason I had to add that and the reason my code did not compile at
308:51 the reason my code did not compile at first was because I forgot to tell clang
308:54 first was because I forgot to tell clang to link in cs50's zeros and ones per
308:57 to link in cs50's zeros and ones per that last step I don't need to do- L
308:59 that last step I don't need to do- L standard IO because it comes with c so
309:02 standard IO because it comes with c so that would just be tedious for everyone
309:03 that would just be tedious for everyone in the world but cs50 does not come with
309:05 in the world but cs50 does not come with C so we link that in and to be clear too
309:07 C so we link that in and to be clear too we won't always use cs50's Library
309:09 we won't always use cs50's Library that'll be yet another pair of training
309:10 that'll be yet another pair of training wheels we take off in the coming weeks
309:12 wheels we take off in the coming weeks but for now it makes a few things
309:14 but for now it makes a few things simpler
309:30 yeah short answer yes so what do the zeros and ones the machine code
309:32 zeros and ones the machine code translate to yes there is a one toone
309:33 translate to yes there is a one toone relationship between the machine code
309:36 relationship between the machine code and the assembly code assembly code it's
309:38 and the assembly code assembly code it's not really English but at least it's
309:40 not really English but at least it's symbols I recognize it's not zeros and
309:42 symbols I recognize it's not zeros and ones machine code of course is just
309:43 ones machine code of course is just zeros and ones so back in the day before
309:45 zeros and ones so back in the day before c existed people were programming only
309:49 c existed people were programming only in assembly code before assembly code
309:51 in assembly code before assembly code existed people were coding in zeros and
309:53 existed people were coding in zeros and ones and you can imagine just how
309:55 ones and you can imagine just how painful that was and so each of these
309:57 painful that was and so each of these languages makes life for us sort of
309:59 languages makes life for us sort of easier and easier in a few weeks we'll
310:00 easier and easier in a few weeks we'll transition to python which will in turn
310:02 transition to python which will in turn make C even uh simpler or coding in
310:06 make C even uh simpler or coding in general simpler to do to all right so
310:08 general simpler to do to all right so with that said what now can we uh what
310:13 with that said what now can we uh what could go wrong with this well it turns
310:15 could go wrong with this well it turns out that besides compiling technically
310:17 out that besides compiling technically speaking there's decompiling and we've
310:19 speaking there's decompiling and we've not done this and we won't do this but
310:21 not done this and we won't do this but it's worth considering for just a moment
310:23 it's worth considering for just a moment uh if you were to not compile your code
310:26 uh if you were to not compile your code but decompile it as the word suggests
310:28 but decompile it as the word suggests this just means reversing the process
310:30 this just means reversing the process converting it ideally from machine code
310:33 converting it ideally from machine code zeros and ones maybe back to C now this
310:36 zeros and ones maybe back to C now this would be cool perhaps if all you have is
310:39 would be cool perhaps if all you have is a program you can convert it and see the
310:40 a program you can convert it and see the actual source code what might a downside
310:43 actual source code what might a downside be if if anyone on the Internet is able
310:45 be if if anyone on the Internet is able to decompile code on their machine
310:49 to decompile code on their machine yeah okay so it's easier to find bugs in
310:52 yeah okay so it's easier to find bugs in the code that oh to exploit so it might
310:55 the code that oh to exploit so it might be easier to uh hack into the software
310:58 be easier to uh hack into the software by finding mistakes you and I made
310:59 by finding mistakes you and I made because literally they're staring at you
311:01 because literally they're staring at you in code worries the zeros and ones make
311:03 in code worries the zeros and ones make it way less obvious other downsides of
311:06 it way less obvious other downsides of what I call decompiling
311:15 yeah yeah yeah if your code your work is your
311:17 yeah yeah if your code your work is your intellectual property copyrighted or
311:19 intellectual property copyrighted or otherwise you know that's kind of
311:21 otherwise you know that's kind of obnoxious that someone can just like run
311:22 obnoxious that someone can just like run a command and boom they can see the
311:23 a command and boom they can see the original code that you wrote now it
311:25 original code that you wrote now it turns out it's not quite as simple as
311:27 turns out it's not quite as simple as that and so even though yes you could
311:29 that and so even though yes you could take a program like hello or even
311:32 take a program like hello or even Microsoft Word and convert it from zeros
311:34 Microsoft Word and convert it from zeros and ones back to some form of source
311:37 and ones back to some form of source code be it in C or Java or python or
311:40 code be it in C or Java or python or something else whatever it was
311:41 something else whatever it was originally written in odds are it's
311:42 originally written in odds are it's going to be an utter mess to look look
311:45 going to be an utter mess to look look at why because things like variable
311:47 at why because things like variable names are not retained in the zeros and
311:49 names are not retained in the zeros and ones typically function names might not
311:51 ones typically function names might not be retained in the zeros and ones the
311:54 be retained in the zeros and ones the code is the logic is but the computer
311:56 code is the logic is but the computer doesn't care what pretty variables you
311:57 doesn't care what pretty variables you chose and how nicely named your uh
311:59 chose and how nicely named your uh functions were it just needs to know
312:01 functions were it just needs to know them as Z in ones moreover if you think
312:03 them as Z in ones moreover if you think about last week we introduced things
312:04 about last week we introduced things like loops and c and besides four Loops
312:07 like loops and c and besides four Loops there's what other kind of loop for
312:09 there's what other kind of loop for instance so a while loop and even though
312:11 instance so a while loop and even though they look different and you have to
312:13 they look different and you have to write different code they achieve
312:15 write different code they achieve exactly the same functionality which is
312:17 exactly the same functionality which is to say when you compile a for Loop or
312:19 to say when you compile a for Loop or you compile a while loop if they
312:21 you compile a while loop if they logically do the same thing they might
312:24 logically do the same thing they might end up looking identical as zeros and
312:26 end up looking identical as zeros and ones and so therefore it's not
312:28 ones and so therefore it's not necessarily predictable that you'll get
312:30 necessarily predictable that you'll get back the original code why cuz because
312:31 back the original code why cuz because the zeros and ones might not know so to
312:34 the zeros and ones might not know so to speak whether it was a for Loop or a
312:36 speak whether it was a for Loop or a while loop so maybe decompiling will
312:37 while loop so maybe decompiling will show you one or the other and honestly
312:39 show you one or the other and honestly decompiling while possible and it's one
312:41 decompiling while possible and it's one way of reverse engineering someone's
312:43 way of reverse engineering someone's product odds are if you're good enough
312:45 product odds are if you're good enough to start reading code that's been
312:47 to start reading code that's been decompiled and reading through the
312:49 decompiled and reading through the messiness of it odds are you have the
312:50 messiness of it odds are you have the talent probably to just write that same
312:52 talent probably to just write that same program from scratch yourself now that's
312:54 program from scratch yourself now that's an overstatement perhaps but it's not
312:56 an overstatement perhaps but it's not quite as easy or threatening um as you
312:59 quite as easy or threatening um as you might first think so in general once
313:01 might first think so in general once codee is piled it's pretty challenging
313:04 codee is piled it's pretty challenging timec consuming costly to reverse
313:06 timec consuming costly to reverse engineer it um much like it would be in
313:08 engineer it um much like it would be in the real world right like all of us have
313:10 the real world right like all of us have some kind of phone probably nowadays in
313:12 some kind of phone probably nowadays in our pocket there's nothing stopping you
313:13 our pocket there's nothing stopping you from opening it up somehow poking around
313:15 from opening it up somehow poking around recreating what's there that's a huge
313:17 recreating what's there that's a huge amount of effort most likely and at that
313:19 amount of effort most likely and at that point maybe you should just invent the
313:20 point maybe you should just invent the phone instead of trying to reverse
313:22 phone instead of trying to reverse engineer it so same kind of idea in the
313:24 engineer it so same kind of idea in the physical world any questions then on
313:28 physical world any questions then on compiling or even decompiling in these
313:30 compiling or even decompiling in these forms
313:32 forms all right so odds are at this point not
313:34 all right so odds are at this point not only I but you have made mistakes and
313:37 only I but you have made mistakes and you've written buggy code a bug in a
313:39 you've written buggy code a bug in a code is just a mistake a logical error
313:42 code is just a mistake a logical error or otherwise where the code just does
313:43 or otherwise where the code just does not behave correctly as you intend and
313:46 not behave correctly as you intend and up until now odds are your debugging
313:48 up until now odds are your debugging techniques have been to maybe look back
313:50 techniques have been to maybe look back at what I did in class maybe ask a
313:53 at what I did in class maybe ask a question online or in person but
313:55 question online or in person but ultimately it'd be nice if you had some
313:56 ultimately it'd be nice if you had some tools of your own with which to debug
313:59 tools of your own with which to debug code and this honestly is a lifelong
314:00 code and this honestly is a lifelong skill you're going to emerge from cs50
314:02 skill you're going to emerge from cs50 and even 20 years from now you're not
314:04 and even 20 years from now you're not going to be writing if you're writing
314:05 going to be writing if you're writing code at all correct code all of the time
314:07 code at all correct code all of the time like all of us on the staff continue to
314:09 like all of us on the staff continue to write bugs hopefully they get a little
314:11 write bugs hopefully they get a little more sophisticated and not sort of like
314:13 more sophisticated and not sort of like oops I missed a semicolon but even those
314:15 oops I missed a semicolon but even those kinds of mistakes we make too but
314:17 kinds of mistakes we make too but there's tools out there and techniques
314:19 there's tools out there and techniques that can make your life easier when it
314:21 that can make your life easier when it comes to solving those problems now the
314:24 comes to solving those problems now the term bug has actually been around for
314:25 term bug has actually been around for decades but a fun story to tell is that
314:28 decades but a fun story to tell is that the first documented actual bug was
314:31 the first documented actual bug was actually somehow connected to Harvard in
314:33 actually somehow connected to Harvard in fact this is the log book relating to
314:36 fact this is the log book relating to the Harvard Mark 2 computer from 1947
314:40 the Harvard Mark 2 computer from 1947 whereby if you read the notes here and
314:41 whereby if you read the notes here and if I zoom in this was an actual moth
314:44 if I zoom in this was an actual moth discovered inside of this big Mainframe
314:46 discovered inside of this big Mainframe computer that was causing some kind of
314:48 computer that was causing some kind of problems and the engineers at the time
314:50 problems and the engineers at the time actually thought it was funny that wow
314:51 actually thought it was funny that wow physical bug actually explains the issue
314:53 physical bug actually explains the issue and it's been forever uh taped to the
314:55 and it's been forever uh taped to the sheet of paper which I believe now is on
314:57 sheet of paper which I believe now is on display in the Smithsonian uh with that
314:59 display in the Smithsonian uh with that said this is just represented two of a
315:02 said this is just represented two of a logical bug and that story is actually
315:05 logical bug and that story is actually uh that story was often retold by a
315:07 uh that story was often retold by a famous mathematician then computer
315:08 famous mathematician then computer scientist really uh Dr Grace Hopper who
315:11 scientist really uh Dr Grace Hopper who actually worked not only on the Harvard
315:12 actually worked not only on the Harvard Mark 2 computer but its predecessor the
315:15 Mark 2 computer but its predecessor the Harvard Mark 1 and if you ever spent
315:18 Harvard Mark 1 and if you ever spent time yet in the engineering building
315:20 time yet in the engineering building across the river here you can actually
315:21 across the river here you can actually see much of this computer which is along
315:24 see much of this computer which is along the wall when you first walk into the
315:25 the wall when you first walk into the science and engineering complex and
315:27 science and engineering complex and indeed as you've probably heard growing
315:28 indeed as you've probably heard growing up this is a Mainframe computer like
315:30 up this is a Mainframe computer like this is what Macs and PCs so to speak
315:33 this is what Macs and PCs so to speak looked like back in the day with very
315:35 looked like back in the day with very physical things that essentially
315:37 physical things that essentially implemented the zeros and ones that you
315:38 implemented the zeros and ones that you and I take for granted now being
315:40 and I take for granted now being miniaturized in our laptops and phones
315:42 miniaturized in our laptops and phones so there's a piece of history there if
315:43 so there's a piece of history there if you visit campus that side of Campus
315:45 you visit campus that side of Campus sometime do take a look but let's
315:47 sometime do take a look but let's consider then how we solve not of course
315:49 consider then how we solve not of course physical bugs but logical bugs and let's
315:51 physical bugs but logical bugs and let's consider something like this from last
315:53 consider something like this from last week whereby we were trying very simply
315:55 week whereby we were trying very simply to print like this uh column of three
315:58 to print like this uh column of three bricks using hashtags of sorts so let me
316:00 bricks using hashtags of sorts so let me go over here in just a moment to VSS
316:03 go over here in just a moment to VSS code and I'm going to go ahead and open
316:05 code and I'm going to go ahead and open a program I wrote in advance and I'm
316:07 a program I wrote in advance and I'm bringing it to class because there's a
316:08 bringing it to class because there's a bug in it and I'd like to figure out how
316:10 bug in it and I'd like to figure out how to solve this bug so let me open up uh
316:12 to solve this bug so let me open up uh buggy z.c which is version zero of my
316:15 buggy z.c which is version zero of my code and let's just take a quick peek at
316:17 code and let's just take a quick peek at what's here it's pretty short it
316:19 what's here it's pretty short it includes only standard i.h it uses
316:21 includes only standard i.h it uses printf it uses a for Loop and the goal
316:24 printf it uses a for Loop and the goal quite simply is to print out that column
316:26 quite simply is to print out that column of three bricks now it's short enough
316:28 of three bricks now it's short enough that some of you if you're getting comfy
316:30 that some of you if you're getting comfy already with see you might already see
316:31 already with see you might already see The Logical bug it's not a syntax error
316:34 The Logical bug it's not a syntax error like it will compile and run but there's
316:36 like it will compile and run but there's a bug there and suppose that I'm very
316:38 a bug there and suppose that I'm very new to see I'm very uncomfortable with C
316:41 new to see I'm very uncomfortable with C it's 2 a.m. and I just can't see the bug
316:44 it's 2 a.m. and I just can't see the bug what are my recourses here for actually
316:45 what are my recourses here for actually finding a mistake like this well first
316:48 finding a mistake like this well first let's look at the symptom let me go down
316:49 let's look at the symptom let me go down to my terminal window I'm going to use
316:52 to my terminal window I'm going to use make buggy zero because again the file
316:54 make buggy zero because again the file is called buggy zero. C I'm not going to
316:56 is called buggy zero. C I'm not going to use clang in fact I'm never really going
316:58 use clang in fact I'm never really going to use clang manually here and out I'm
316:59 to use clang manually here and out I'm just going to use make because it makes
317:00 just going to use make because it makes our lives easier it does compile no
317:03 our lives easier it does compile no errors so it's not syntax it's not
317:05 errors so it's not syntax it's not something silly like a missing semicolon
317:07 something silly like a missing semicolon but when I runbuggy Z I of course see 1
317:11 but when I runbuggy Z I of course see 1 2 3 4 and this of course does not match
317:14 2 3 4 and this of course does not match the zero the one two three bricks that I
317:17 the zero the one two three bricks that I actually intended for that column and
317:19 actually intended for that column and yet I'm starting counting at zero as I
317:22 yet I'm starting counting at zero as I usually do I've got three I'm going up
317:24 usually do I've got three I'm going up to three so where is my logical error if
317:26 to three so where is my logical error if it hasn't obviously jumped out at you
317:28 it hasn't obviously jumped out at you already well how can I solve this well
317:30 already well how can I solve this well first and foremost perhaps the best
317:32 first and foremost perhaps the best technique for solving bugs at least
317:34 technique for solving bugs at least early on is just use printf like thus
317:36 early on is just use printf like thus far we've used printf to say hello and
317:38 far we've used printf to say hello and other things on the screen but print def
317:40 other things on the screen but print def is just a function for printing anything
317:42 is just a function for printing anything and there's no reason you can't
317:43 and there's no reason you can't temporarily use print def to like print
317:46 temporarily use print def to like print out the contents of variables what's
317:47 out the contents of variables what's going on inside of your program just to
317:49 going on inside of your program just to figure out where your mistake is and
317:51 figure out where your mistake is and then you can delete that line of code
317:52 then you can delete that line of code later it doesn't have to stay there
317:53 later it doesn't have to stay there forever so let me do this instead of
317:56 forever so let me do this instead of just printing out in vs code the hash
317:58 just printing out in vs code the hash symbol let me do a little safety check
318:02 symbol let me do a little safety check here and print out the value of I so let
318:05 here and print out the value of I so let me go ahead and say something like I is
318:09 me go ahead and say something like I is now I want to say I is this but of
318:11 now I want to say I is this but of course this is not how I print out the
318:13 course this is not how I print out the value of I if I want to print out the
318:15 value of I if I want to print out the value of I what should I put
318:17 value of I what should I put here so percent I for integer instead of
318:20 here so percent I for integer instead of percent s for string so they're still
318:22 percent s for string so they're still placeholders but we use percent s for
318:24 placeholders but we use percent s for integers and now if I want to print out
318:25 integers and now if I want to print out I I just need the comma as the second
318:27 I I just need the comma as the second argument and then I all right let me go
318:29 argument and then I all right let me go ahead and back to vs uh to my terminal
318:32 ahead and back to vs uh to my terminal window let me recompile the program
318:34 window let me recompile the program because I've changed it that still works
318:36 because I've changed it that still works fine do/ bugy Z and now let me increase
318:40 fine do/ bugy Z and now let me increase the size of my terminal window here you
318:42 the size of my terminal window here you just see some diagnostic information if
318:44 just see some diagnostic information if you will like this is not the goal this
318:46 you will like this is not the goal this is not what you should be submitting for
318:47 is not what you should be submitting for this homework problem we're at one uh
318:50 this homework problem we're at one uh but it is helping us diagnostically know
318:52 but it is helping us diagnostically know that okay when I is zero here's a hash
318:54 that okay when I is zero here's a hash when I is one here's a hash when I is
318:56 when I is one here's a hash when I is two here's a hash when I is three here's
318:58 two here's a hash when I is three here's a hash well wait a minute that's one two
319:00 a hash well wait a minute that's one two three four so clearly I'm printing it
319:02 three four so clearly I'm printing it one too many times so let me look back
319:04 one too many times so let me look back at the code here by shrinking my
319:06 at the code here by shrinking my terminal window and let me just ask the
319:10 terminal window and let me just ask the group where is in fact the mistake or
319:13 group where is in fact the mistake or what equivalently would be the solution
319:16 what equivalently would be the solution yeah in the
319:21 middle yeah instead of less than or equal to use just less than so you got
319:23 equal to use just less than so you got to kind of pick a lane here like if
319:25 to kind of pick a lane here like if you're going to start counting from zero
319:26 you're going to start counting from zero you generally use less than and go up to
319:29 you generally use less than and go up to but not through the value or if you
319:31 but not through the value or if you prefer like in the human world counting
319:32 prefer like in the human world counting from one on up you can use great less
319:34 from one on up you can use great less than or equal to but you have to be
319:36 than or equal to but you have to be consistent and in general as a
319:38 consistent and in general as a programmer just always start counting
319:39 programmer just always start counting from zero if you're doing something
319:41 from zero if you're doing something canonical like this but the solution is
319:43 canonical like this but the solution is indeed just to change this by changing
319:45 indeed just to change this by changing the greater less than or equal to to
319:47 the greater less than or equal to to less than if I re compile this program
319:50 less than if I re compile this program with make buggy zero and then do buggy
319:52 with make buggy zero and then do buggy zero again and let me increase the size
319:55 zero again and let me increase the size of my terminal window now you see okay
319:57 of my terminal window now you see okay almost the same output but indeed I
320:00 almost the same output but indeed I starts at zero goes up two but not
320:02 starts at zero goes up two but not through three all right so printf in
320:05 through three all right so printf in short should be can be your first uh
320:07 short should be can be your first uh diagnostic tool instead of just staring
320:09 diagnostic tool instead of just staring at the screen or raising your hand I
320:11 at the screen or raising your hand I mean use printf to see literally what's
320:13 mean use printf to see literally what's going on inside of your program by just
320:15 going on inside of your program by just printing out things of interest and then
320:17 printing out things of interest and then once you've solved the problem you can
320:19 once you've solved the problem you can go back into your code AS I'll do here
320:21 go back into your code AS I'll do here by shrinking my terminal window I'll
320:22 by shrinking my terminal window I'll delete the print F line and now I'm
320:24 delete the print F line and now I'm ready to share this program with the
320:26 ready to share this program with the world or submit it as homework or the
320:28 world or submit it as homework or the like it's just meant there to be
320:30 like it's just meant there to be temporary any questions on printf as a
320:34 temporary any questions on printf as a debugging
320:40 tool no all right well that only gets us so far and honestly as your programs
320:41 so far and honestly as your programs grow and grow and grow it's going to
320:43 grow and grow and grow it's going to actually get really annoying to start
320:44 actually get really annoying to start going in and adding printfs then
320:47 going in and adding printfs then removing them and figuring out if you've
320:48 removing them and figuring out if you've got multiple printfs well which one
320:50 got multiple printfs well which one printed what like it just gets messy
320:52 printed what like it just gets messy eventually to rely on printf alone so
320:54 eventually to rely on printf alone so being a computer scientist computer
320:57 being a computer scientist computer scientists have written software to make
320:59 scientists have written software to make it easier to debug code that software is
321:01 it easier to debug code that software is what we would generally call a debugger
321:04 what we would generally call a debugger which would be the second tool of the
321:05 which would be the second tool of the trade that you can use to actually solve
321:06 trade that you can use to actually solve problems in your code now in the world
321:09 problems in your code now in the world of VSS code there's actually a debugger
321:11 of VSS code there's actually a debugger built in so the graphical user interface
321:13 built in so the graphical user interface you're about to see in VSS code isn't
321:15 you're about to see in VSS code isn't specific to cs50 it actually comes with
321:17 specific to cs50 it actually comes with vs code and it supports C and C++ and
321:19 vs code and it supports C and C++ and Java and Python and lots of other
321:21 Java and Python and lots of other languages too but it's admittedly a
321:23 languages too but it's admittedly a little complicated to just start using
321:26 little complicated to just start using the debugger you have to like create a
321:28 the debugger you have to like create a configuration file and do like some
321:30 configuration file and do like some annoying steps that just get in the way
321:31 annoying steps that just get in the way of solving real problems so we have
321:33 of solving real problems so we have automated the process for you of just
321:35 automated the process for you of just starting the debugger and thereafter
321:37 starting the debugger and thereafter it's sort of Industry standard how you
321:39 it's sort of Industry standard how you use it but we save you the headache of
321:40 use it but we save you the headache of having to create those configuration
321:42 having to create those configuration files so suppose I want to do this
321:45 files so suppose I want to do this suppose I want to try to debug this
321:46 suppose I want to try to debug this program step by step using special
321:49 program step by step using special software well how can I do that well let
321:52 software well how can I do that well let me propose that if I revert this back to
321:54 me propose that if I revert this back to the original version where I was less
321:57 the original version where I was less than or equal to three I'm pretty sure
321:59 than or equal to three I'm pretty sure that I was printing to hashes so I'm
322:01 that I was printing to hashes so I'm going to do this and you might have done
322:03 going to do this and you might have done this accidentally or never at all but
322:05 this accidentally or never at all but notice if you hover over the gutter so
322:07 notice if you hover over the gutter so to speak in vs code the part of it all
322:09 to speak in vs code the part of it all the way to the left of the editor you
322:11 the way to the left of the editor you see this sort of grayed out uh Red Dot
322:14 see this sort of grayed out uh Red Dot if you click there it becomes a brighter
322:16 if you click there it becomes a brighter Red Dot and this represents what we're
322:18 Red Dot and this represents what we're going to call a break point and this is
322:20 going to call a break point and this is just a visual indicator that you've put
322:21 just a visual indicator that you've put like a stop sign equivalent there and
322:23 like a stop sign equivalent there and you're telling the debugger in a moment
322:25 you're telling the debugger in a moment stop running my code there why because I
322:28 stop running my code there why because I prefer to step through my code at sort
322:30 prefer to step through my code at sort of a human speed and not as computer
322:32 of a human speed and not as computer speed where it runs all at once so I've
322:34 speed where it runs all at once so I've set my breakpoint which is step one and
322:36 set my breakpoint which is step one and then step two is quite simply this
322:38 then step two is quite simply this instead of running the program itself
322:40 instead of running the program itself run a command called debug 50 and then
322:43 run a command called debug 50 and then do/ bugy Z and now this will start your
322:47 do/ bugy Z and now this will start your program but inside of the debugger which
322:50 program but inside of the debugger which is a special program that smart people
322:51 is a special program that smart people wrote that will Empower you to now step
322:53 wrote that will Empower you to now step through your code line by line at again
322:56 through your code line by line at again at your own Comfort Pace I'm going to
322:59 at your own Comfort Pace I'm going to hit enter some stuff's going to happen
323:01 hit enter some stuff's going to happen on the screen whoops uh notice this is a
323:03 on the screen whoops uh notice this is a common mistake that I made accidentally
323:05 common mistake that I made accidentally here looks like I've changed my code I
323:07 here looks like I've changed my code I did because I went in and changed the
323:08 did because I went in and changed the less than or equal to sign so let me go
323:10 less than or equal to sign so let me go ahead and rerun make buggy zero enter
323:13 ahead and rerun make buggy zero enter good now let me rerun debug 50 enter and
323:17 good now let me rerun debug 50 enter and now some stuff just happened on the
323:19 now some stuff just happened on the screen and it takes a moment to get
323:20 screen and it takes a moment to get started but once it's started you'll see
323:23 started but once it's started you'll see this you'll still see your code but
323:25 this you'll still see your code but you'll see this yellow highlight which
323:27 you'll see this yellow highlight which you've probably not seen before and
323:29 you've probably not seen before and notice that it's specifically
323:30 notice that it's specifically highlighted in the same line that I set
323:32 highlighted in the same line that I set a breakpoint on why that just means the
323:34 a breakpoint on why that just means the program has EXE the debugger has
323:36 program has EXE the debugger has executed all of these lines except for
323:39 executed all of these lines except for line seven it has broken at not in a bad
323:42 line seven it has broken at not in a bad way but it has paused execution on line
323:44 way but it has paused execution on line seven so it hasn't yet printed any
323:46 seven so it hasn't yet printed any hashes and you can see that no hashes in
323:49 hashes and you can see that no hashes in the terminal window yet it's paused
323:51 the terminal window yet it's paused execution but what's interesting with
323:53 execution but what's interesting with the debugger is the stuff over here on
323:56 the debugger is the stuff over here on the left hand side in the debugger here
323:58 the left hand side in the debugger here you'll see under variables all of your
324:00 you'll see under variables all of your so-called local variables and we haven't
324:02 so-called local variables and we haven't really made a distinction between local
324:03 really made a distinction between local and something called Global but for now
324:05 and something called Global but for now local variables just means all of the
324:07 local variables just means all of the variables that exist in your function so
324:09 variables that exist in your function so I currently has a value of zero okay and
324:12 I currently has a value of zero okay and that makes sense so now how do I step
324:14 that makes sense so now how do I step through my code and see what it's doing
324:17 through my code and see what it's doing well at the top of the screen here
324:19 well at the top of the screen here you'll see some playback icons kind of
324:21 you'll see some playback icons kind of like a video player but they have
324:22 like a video player but they have special meaning this first one will just
324:25 special meaning this first one will just play the rest of your program all the
324:27 play the rest of your program all the way to the end so you only click that if
324:28 way to the end so you only click that if you sort of solved a problem and you
324:30 you sort of solved a problem and you just want to run run it to completion
324:32 just want to run run it to completion like before but the next three or next
324:34 like before but the next three or next two really are really the juiciest the
324:37 two really are really the juiciest the second one here if you hover over it
324:39 second one here if you hover over it eventually you'll see that it's called
324:40 eventually you'll see that it's called step over step over means that the
324:43 step over step over means that the debugger will run this currently
324:45 debugger will run this currently highlighted line of code but it's not
324:47 highlighted line of code but it's not going to dive into it so if it's a
324:48 going to dive into it so if it's a function like print F it's not going to
324:50 function like print F it's not going to start stepping through print F line by
324:52 start stepping through print F line by line why because I can pretty much
324:53 line why because I can pretty much assume print F written decades ago is
324:55 assume print F written decades ago is correct problems probably with me but
324:58 correct problems probably with me but this next line if I did really want to
325:00 this next line if I did really want to step in into the printf code to figure
325:02 step in into the printf code to figure out how it works or find some problem in
325:04 out how it works or find some problem in it all these years later you can step
325:06 it all these years later you can step into printf and then the screen would
325:08 into printf and then the screen would change and you'd see each of the lines
325:09 change and you'd see each of the lines for print F line by line at least if you
325:11 for print F line by line at least if you have the source code for print F
325:13 have the source code for print F installed all right I'm going to use the
325:14 installed all right I'm going to use the first one step over and watch as the
325:17 first one step over and watch as the yellow highlight moves and watch as in
325:20 yellow highlight moves and watch as in the terminal window there's a hash
325:21 the terminal window there's a hash symbol here we go there's one hash now
325:25 symbol here we go there's one hash now notice line five is highlighted that
325:27 notice line five is highlighted that means it has paused on line five line
325:29 means it has paused on line five line five has not yet been exec executed so
325:31 five has not yet been exec executed so what does that mean the value of I per
325:34 what does that mean the value of I per the top left hand corner is still zero
325:36 the top left hand corner is still zero but as soon as I click step over again
325:38 but as soon as I click step over again Watch What Happens at the top left where
325:40 Watch What Happens at the top left where I is a variable on the
325:43 I is a variable on the screen now I and it flashed briefly has
325:46 screen now I and it flashed briefly has a value of one and now if I step over
325:48 a value of one and now if I step over again watch the terminal window there's
325:50 again watch the terminal window there's my second hash now let me click step
325:53 my second hash now let me click step over on the for Loop watch the variable
325:55 over on the for Loop watch the variable at top left now one goes to two now let
325:58 at top left now one goes to two now let me click it again third hash and Here's
326:00 me click it again third hash and Here's Where The Logical error is perhaps
326:02 Where The Logical error is perhaps revealed let me go ahead and step over
326:04 revealed let me go ahead and step over the loop now I is three wait a minute
326:06 the loop now I is three wait a minute I'm still going to print out a hash
326:09 I'm still going to print out a hash there it is there's the fourth hash and
326:10 there it is there's the fourth hash and at this point hopefully the light bulb
326:12 at this point hopefully the light bulb proverbially has gone off I realize oh I
326:13 proverbially has gone off I realize oh I screwed up I can either stop the program
326:16 screwed up I can either stop the program Al together with the red square or I can
326:18 Al together with the red square or I can just let it run all the way to the end
326:20 just let it run all the way to the end which just terminates everything at this
326:22 which just terminates everything at this point I just want to get back into my
326:24 point I just want to get back into my code and start fixing things and you can
326:25 code and start fixing things and you can close for instance as I will here the
326:27 close for instance as I will here the file explorer just to hide the panel
326:29 file explorer just to hide the panel that open so that's debug 50 but it's
326:32 that open so that's debug 50 but it's not a cs50 thing that just starts the
326:34 not a cs50 thing that just starts the debugger for you which is something
326:36 debugger for you which is something you'd find in most any programming
326:37 you'd find in most any programming environment nowadays questions on
326:42 environment nowadays questions on debugging questions
326:48 yeah good question where does it tell you where it went wrong so sadly it does
326:51 you where it went wrong so sadly it does not tell you any of that the onus is
326:53 not tell you any of that the onus is still on you the human to use this tool
326:56 still on you the human to use this tool productively to walk through your code
326:57 productively to walk through your code at a a saner pace but your brain is is
327:00 at a a saner pace but your brain is is the one that still needs to solve it and
327:02 the one that still needs to solve it and I don't doubt down the line with
327:03 I don't doubt down the line with artificial intelligence and more
327:05 artificial intelligence and more programs like this will get all the more
327:06 programs like this will get all the more helpful and start answering questions
327:08 helpful and start answering questions like that for us and there are other
327:09 like that for us and there are other tools we'll introduce you this semester
327:11 tools we'll introduce you this semester that are even more powerful than this
327:12 that are even more powerful than this but for now it's just a tool really to
327:14 but for now it's just a tool really to slow things down and not have to change
327:17 slow things down and not have to change your code the fact that I had that panel
327:18 your code the fact that I had that panel on the left that just showed me eyes
327:20 on the left that just showed me eyes changing value is just an alternative to
327:23 changing value is just an alternative to print F and I can step through it a
327:24 print F and I can step through it a little more slowly other questions on
327:29 little more slowly other questions on debugging now let me show you one final
327:32 debugging now let me show you one final example with this debugger here and this
327:35 example with this debugger here and this one too I wrote in advance let me close
327:37 one too I wrote in advance let me close buggy z.c and let me open up buggy1.c my
327:41 buggy z.c and let me open up buggy1.c my second version thereof let me close my
327:42 second version thereof let me close my terminal window for a second and give
327:44 terminal window for a second and give you a quick tour of this program which
327:46 you a quick tour of this program which similarly has a mistake now at the top
327:48 similarly has a mistake now at the top of this program some familiar includes
327:50 of this program some familiar includes cs50.h and standard i.h this is not
327:53 cs50.h and standard i.h this is not something we've seen before it's
327:54 something we've seen before it's specific to this example a function
327:56 specific to this example a function called get Negative int takes no
327:59 called get Negative int takes no arguments and it returns an integer what
328:01 arguments and it returns an integer what does it do it literally gets a negative
328:02 does it do it literally gets a negative integer ideally from the user fun fact
328:05 integer ideally from the user fun fact though it doesn't correctly that's the
328:07 though it doesn't correctly that's the bug get Negative int is broken at the
328:09 bug get Negative int is broken at the moment so what does Maine do well main
328:11 moment so what does Maine do well main just calls this function passing in
328:13 just calls this function passing in nothing in parenthesis no inputs and it
328:15 nothing in parenthesis no inputs and it stores the return value in I and then it
328:18 stores the return value in I and then it just prints out I on the screen so
328:20 just prints out I on the screen so honestly just by eyeballing this you
328:22 honestly just by eyeballing this you know I feel comfortable enough with
328:23 know I feel comfortable enough with programming and see I think main is
328:25 programming and see I think main is correct let me just stipulate main is
328:27 correct let me just stipulate main is correct but there is going to be a bug
328:28 correct but there is going to be a bug down here now what's the bug down here
328:30 down here now what's the bug down here here well let me look at get Negative in
328:33 here well let me look at get Negative in implementation notice this first line 12
328:36 implementation notice this first line 12 is identical to the Prototype up here
328:39 is identical to the Prototype up here the Prototype is sort of stupidly
328:41 the Prototype is sort of stupidly required up here because C reads things
328:43 required up here because C reads things top to bottom left to right the compiler
328:45 top to bottom left to right the compiler technically does so if you reference get
328:47 technically does so if you reference get Negative in here but you don't implement
328:50 Negative in here but you don't implement it until down here and you haven't told
328:52 it until down here and you haven't told C in advance that it will exist again
328:54 C in advance that it will exist again you get the error we saw last week all
328:56 you get the error we saw last week all right so how does get Negative int work
328:58 right so how does get Negative int work we declare a variable called n
329:00 we declare a variable called n we've got a do while loop that does what
329:03 we've got a do while loop that does what it uses get int which comes with the
329:04 it uses get int which comes with the cs50 library per last week it prompts
329:07 cs50 library per last week it prompts the user for negative integer quote
329:08 the user for negative integer quote unquote and stores the value in n i then
329:12 unquote and stores the value in n i then do all of this while n is less than zero
329:16 do all of this while n is less than zero right remember we used a do while loop
329:18 right remember we used a do while loop last week to make sure the human
329:19 last week to make sure the human cooperates and doesn't give us the wrong
329:21 cooperates and doesn't give us the wrong type of value be it positive or negative
329:23 type of value be it positive or negative or something else and then we return n
329:26 or something else and then we return n and there's some subtleties anyone
329:27 and there's some subtleties anyone recall or have an intuition for why I've
329:29 recall or have an intuition for why I've declared clared in on line 14 instead of
329:33 declared clared in on line 14 instead of on line 17 this is a c specific
329:44 thing exactly there's this notion of scope in see and we'll continue to see
329:46 scope in see and we'll continue to see this over time whereby a variable only
329:49 this over time whereby a variable only exists inside of the most recent curly
329:51 exists inside of the most recent curly braces that you've opened so if I've
329:53 braces that you've opened so if I've declared and here on line 14 I can use
329:56 declared and here on line 14 I can use it anywhere between lines 13 and 21
329:59 it anywhere between lines 13 and 21 because those are the nearest curly
330:00 because those are the nearest curly Braes if by contrast as you note if I
330:02 Braes if by contrast as you note if I instead said this int n equals get int
330:05 instead said this int n equals get int and so forth and didn't have the current
330:07 and so forth and didn't have the current line 14 well n would exist inside of
330:10 line 14 well n would exist inside of these curly braces but not here which is
330:13 these curly braces but not here which is too late and definitely not here so you
330:15 too late and definitely not here so you just have to declare it first and then
330:17 just have to declare it first and then use and reuse it as such now let me just
330:19 use and reuse it as such now let me just show you how I can debug this but let me
330:21 show you how I can debug this but let me show you the symptoms first let me open
330:23 show you the symptoms first let me open my terminal window let me run make buggy
330:26 my terminal window let me run make buggy one compiles okay so it's not something
330:28 one compiles okay so it's not something silly like a semicolon dots SL buggy one
330:31 silly like a semicolon dots SL buggy one and I'm asked for a negative integer all
330:33 and I'm asked for a negative integer all right let me give it negative one
330:35 right let me give it negative one enter well the main function supposed to
330:38 enter well the main function supposed to print out what I typed but it clearly
330:40 print out what I typed but it clearly didn't it's prompting me again all right
330:41 didn't it's prompting me again all right so maybe it'll like -2 no maybe
330:45 so maybe it'll like -2 no maybe -3 50 okay so it's definitely broken
330:48 -3 50 okay so it's definitely broken right it kind of seems logically to be
330:50 right it kind of seems logically to be doing the opposite now you can perhaps
330:52 doing the opposite now you can perhaps see why this is happening already these
330:53 see why this is happening already these are deliberately simple programs by uh
330:55 are deliberately simple programs by uh for demonstration sake but let's do this
330:58 for demonstration sake but let's do this let me go ahead and set a break point in
331:00 let me go ahead and set a break point in main even though I'm pretty sure main is
331:02 main even though I'm pretty sure main is correct but it just helps me start my
331:04 correct but it just helps me start my thought process start with Main and then
331:05 thought process start with Main and then take it from there let me run now uh
331:08 take it from there let me run now uh debug
331:09 debug 50 bugy one enter and let's see with
331:13 50 bugy one enter and let's see with that breakpoint now the the goey is
331:15 that breakpoint now the the goey is going to reconfigure itself it's going
331:16 going to reconfigure itself it's going to pause on line eight because that's
331:18 to pause on line eight because that's the first interesting line inside of
331:20 the first interesting line inside of Maine so I could have just put the
331:22 Maine so I could have just put the breakpoint on line eight too it's smart
331:24 breakpoint on line eight too it's smart enough to know that if I set it on six
331:26 enough to know that if I set it on six eh you really mean line eight because
331:27 eh you really mean line eight because that's the first actual line of code and
331:29 that's the first actual line of code and watch when now what happens if I step
331:31 watch when now what happens if I step over this line notice that I which at
331:35 over this line notice that I which at the moment seems to have a default value
331:37 the moment seems to have a default value of zero more on that another time but if
331:39 of zero more on that another time but if I click step over like before I'm
331:43 I click step over like before I'm prompted for a negative integer let me
331:44 prompted for a negative integer let me type negative 1 enter and now
331:48 type negative 1 enter and now notice there's no additional yellow
331:51 notice there's no additional yellow highlight why where am I currently stuck
331:59 logically yeah just logically I must be in that D while Loop and even if you
332:01 in that D while Loop and even if you don't understand it like that's the only
332:02 don't understand it like that's the only explanation if you keep getting prompted
332:04 explanation if you keep getting prompted surely there's a loop going on there's
332:06 surely there's a loop going on there's only one Loop in my code so there's
332:07 only one Loop in my code so there's probably a problem there so okay I can't
332:10 probably a problem there so okay I can't just set a breakpoint in Main and then
332:11 just set a breakpoint in Main and then wait for this to work so let me just uh
332:13 wait for this to work so let me just uh let me stop this with the red square and
332:16 let me stop this with the red square and let me think all right instead of I can
332:18 let me think all right instead of I can still set my break point in main but let
332:20 still set my break point in main but let me rerun the debugger instead and this
332:22 me rerun the debugger instead and this time not step over that line of code let
332:25 time not step over that line of code let me step into that line of code so Watch
332:28 me step into that line of code so Watch What Happens now instead of clicking the
332:30 What Happens now instead of clicking the second icon here let me click the third
332:32 second icon here let me click the third whose name is indeed step into and watch
332:35 whose name is indeed step into and watch as the yellow highlight does not move to
332:36 as the yellow highlight does not move to line n it dives into line8 the function
332:40 line n it dives into line8 the function on line eight thereby bringing me whoosh
332:43 on line eight thereby bringing me whoosh down to line 17 it's kind of going down
332:46 down to line 17 it's kind of going down into that next function now it didn't
332:48 into that next function now it didn't bother pausing on line 12 or 13 or 14
332:51 bother pausing on line 12 or 13 or 14 because there's nothing intellectually
332:52 because there's nothing intellectually interesting there happening yet the
332:54 interesting there happening yet the juicy part really starts it would seem
332:55 juicy part really starts it would seem in line 17 so now notice n is my
332:59 in line 17 so now notice n is my variable at the top left if I click I
333:02 variable at the top left if I click I don't want to click step into now
333:04 don't want to click step into now though what what would go wrong if I
333:06 though what what would go wrong if I click on step into or what would it do
333:09 click on step into or what would it do that I don't think I want to
333:11 that I don't think I want to do
333:13 do yeah yeah it would step into get int but
333:16 yeah yeah it would step into get int but I'd like to think that the staff's
333:18 I'd like to think that the staff's version of get in is correct and that's
333:19 version of get in is correct and that's not our problem today so I want to step
333:21 not our problem today so I want to step over it and watch now at top left that
333:24 over it and watch now at top left that nothing happens yet to the value of n
333:26 nothing happens yet to the value of n until I go to the terminal window now
333:28 until I go to the terminal window now and type in something like Nega 1 now
333:30 and type in something like Nega 1 now notice it jumps to line 19 which is the
333:33 notice it jumps to line 19 which is the next interesting line top left n indeed
333:36 next interesting line top left n indeed is1 and here's where I can now pause as
333:38 is1 and here's where I can now pause as a human and think all right so while n
333:41 a human and think all right so while n is less than zero all right n per the
333:43 is less than zero all right n per the top left corner is negative 1 so all
333:45 top left corner is negative 1 so all right while negative 1 is less than zero
333:47 right while negative 1 is less than zero well obviously that's true
333:48 well obviously that's true mathematically so what's going to happen
333:50 mathematically so what's going to happen it's a do while loop so when I click on
333:52 it's a do while loop so when I click on step over again it's going to go to this
333:56 step over again it's going to go to this line cuz it's at the end of the inside
333:58 line cuz it's at the end of the inside of that Loop and now here it's looping
334:01 of that Loop and now here it's looping through again and again all right let me
334:03 through again and again all right let me do this once more I'm going to step over
334:05 do this once more I'm going to step over all right I'm going to type in -2 and
334:07 all right I'm going to type in -2 and it's the exact same thing now is my
334:09 it's the exact same thing now is my chance on the yellow line okay wait a
334:10 chance on the yellow line okay wait a minute -2 is obviously less than zero
334:13 minute -2 is obviously less than zero let me try this one more time click it
334:16 let me try this one more time click it once here and now all right let me give
334:17 once here and now all right let me give it 50 and now okay while 50 is less than
334:23 it 50 and now okay while 50 is less than zero that's not true so the loop is over
334:26 zero that's not true so the loop is over because it's not going to do it while 50
334:28 because it's not going to do it while 50 is less than zero that's not true so now
334:29 is less than zero that's not true so now watch when I click step over once more
334:32 watch when I click step over once more it then finishes the loop even though
334:34 it then finishes the loop even though there's nothing more to do it's now
334:36 there's nothing more to do it's now about to return n it jumps back up to
334:38 about to return n it jumps back up to main where I left off on line nine it
334:41 main where I left off on line nine it now prints in my terminal window the
334:43 now prints in my terminal window the number 50 and hopefully at this point to
334:44 number 50 and hopefully at this point to your question earlier my human brain has
334:46 your question earlier my human brain has realized oh I'm an idiot like I flipped
334:49 realized oh I'm an idiot like I flipped my my sign there so I probably let me
334:51 my my sign there so I probably let me stop this I probably want to do
334:53 stop this I probably want to do something like this if the goal is to
334:55 something like this if the goal is to get a negative integer I probably want
334:57 get a negative integer I probably want to say well n is for instance greater
335:02 to say well n is for instance greater than or equal to zero would work so
335:05 than or equal to zero would work so while n is greater than or equal to zero
335:07 while n is greater than or equal to zero keep doing this and that's the logic I
335:09 keep doing this and that's the logic I wanted to express so the debugger just
335:11 wanted to express so the debugger just saves me from staring at the screen
335:12 saves me from staring at the screen raising a hand sort of asking someone
335:14 raising a hand sort of asking someone else at least in this case it allows me
335:16 else at least in this case it allows me to go through it at a healthier Pace
335:18 to go through it at a healthier Pace questions now on debug 50 which should
335:20 questions now on debug 50 which should be your new friend even if it's not your
335:22 be your new friend even if it's not your first instinct after
335:29 printf any questions on debug 50 no all right all right well there's one last
335:31 right all right well there's one last Technique we can equip you with here um
335:33 Technique we can equip you with here um and that is in addition to printf and a
335:35 and that is in addition to printf and a debugger no joke um a rubber duck is
335:38 debugger no joke um a rubber duck is actually a reasonably recommended uh
335:40 actually a reasonably recommended uh solution to finding bugs in your code to
335:42 solution to finding bugs in your code to your question earlier the duck two is
335:44 your question earlier the duck two is not going to solve the problem for you
335:46 not going to solve the problem for you but if you've wondered why this little
335:47 but if you've wondered why this little guy has been here for so long um there's
335:49 guy has been here for so long um there's this technique has its own Wikipedia
335:51 this technique has its own Wikipedia article of called rubber duck debugging
335:53 article of called rubber duck debugging the idea of which is that if you're home
335:56 the idea of which is that if you're home in your dorm room wrestling with some
335:57 in your dorm room wrestling with some bug in your code printf didn't quite
335:59 bug in your code printf didn't quite reveal the source to you debugger isn't
336:01 reveal the source to you debugger isn't really helping honestly maybe it would
336:03 really helping honestly maybe it would help to just sound out what problem
336:06 help to just sound out what problem you're having similar to going to office
336:07 you're having similar to going to office hours talking to a a TA or a professor
336:10 hours talking to a a TA or a professor just walking through your problems
336:11 just walking through your problems because in sort of talking to the duck
336:13 because in sort of talking to the duck about you know the fact that you're
336:14 about you know the fact that you're doing this while uh n is less than zero
336:19 doing this while uh n is less than zero and then if it is I wait a minute I'm an
336:21 and then if it is I wait a minute I'm an idiot not just for talking to the rubber
336:23 idiot not just for talking to the rubber duck you realize hopefully in expressing
336:25 duck you realize hopefully in expressing yourself literally verbally you probably
336:27 yourself literally verbally you probably will hear with non-zero probability like
336:29 will hear with non-zero probability like some illogic in your statement and just
336:31 some illogic in your statement and just by sounding things out you'll realize
336:33 by sounding things out you'll realize like oh that's my problem and so frankly
336:37 like oh that's my problem and so frankly if you have roommates you can also use a
336:38 if you have roommates you can also use a roommate for this but the rubber duck is
336:40 roommate for this but the rubber duck is just sort of a go-to when your roommates
336:42 just sort of a go-to when your roommates have no interest in your you know C
336:43 have no interest in your you know C problem set talking something through
336:45 problem set talking something through that um as follow as such and this is an
336:48 that um as follow as such and this is an invaluable technique I admittedly tend
336:50 invaluable technique I admittedly tend not to do it so much with a rubber duck
336:52 not to do it so much with a rubber duck but ideally with colleagues human
336:53 but ideally with colleagues human colleagues but just talking through
336:55 colleagues but just talking through things often will help you just realize
336:58 things often will help you just realize oh I said something logical now I can go
337:00 oh I said something logical now I can go back to the code so don't solve problems
337:03 back to the code so don't solve problems by staring at your screen endlessly for
337:05 by staring at your screen endlessly for minutes for hours at that point it's
337:06 minutes for hours at that point it's time for a break time to walk away time
337:08 time for a break time to walk away time to talk to the duck if you've already
337:10 to talk to the duck if you've already exhausted some of those other tools um
337:12 exhausted some of those other tools um as an aside on your way out today at the
337:14 as an aside on your way out today at the end of class we have H clearly plenty of
337:16 end of class we have H clearly plenty of rubber ducks uh for you um and uh it's
337:19 rubber ducks uh for you um and uh it's become a thing over the years at least
337:21 become a thing over the years at least among some uh to bring the duck with
337:23 among some uh to bring the duck with them when they travel and send us photos
337:25 them when they travel and send us photos here for instance is uh cs50's rubber
337:28 here for instance is uh cs50's rubber duck debugger AKA ddb for duck debugger
337:31 duck debugger AKA ddb for duck debugger which is a pun on a geekier program
337:34 which is a pun on a geekier program called GDB the ganu debugger which is an
337:36 called GDB the ganu debugger which is an actual piece of software for debugging
337:38 actual piece of software for debugging this is cs50's debugger uh in the hills
337:41 this is cs50's debugger uh in the hills of Puerto Rico uh also here on the sea
337:44 of Puerto Rico uh also here on the sea uh he made its way to San Francisco here
337:47 uh he made its way to San Francisco here uh also down by Fisherman's dwarf by the
337:49 uh also down by Fisherman's dwarf by the sea lions if familiar uh here at
337:52 sea lions if familiar uh here at Stanford where there's a William Gates
337:53 Stanford where there's a William Gates computer science building for computer
337:55 computer science building for computer science uh down the road in SF at Google
337:58 science uh down the road in SF at Google uh and this is the Tre fountain in Rome
338:01 uh and this is the Tre fountain in Rome and lastly uh the Coliseum so we'll be
338:04 and lastly uh the Coliseum so we'll be curious to see in the coming years where
338:05 curious to see in the coming years where your duck two travels so that then was
338:08 your duck two travels so that then was quite a bit why don't we go ahead here
338:10 quite a bit why don't we go ahead here and take a short five minute break no
338:11 and take a short five minute break no snacks yet you're welcome to get up or
338:13 snacks yet you're welcome to get up or sit down we'll return in about five all
338:17 sit down we'll return in about five all right so we are back and if the goal
338:20 right so we are back and if the goal ultimately today is to have a better
338:23 ultimately today is to have a better understanding of things like strings so
338:24 understanding of things like strings so that we can solve problems with text
338:26 that we can solve problems with text let's consider some simpler types of
338:28 let's consider some simpler types of data first how we might represent those
338:30 data first how we might represent those and then see if that doesn't lead us to
338:32 and then see if that doesn't lead us to a discovery as to like how strings in
338:34 a discovery as to like how strings in just today's modern software is using
338:36 just today's modern software is using things like that so when we talked on
338:38 things like that so when we talked on week zero about representation of data
338:41 week zero about representation of data we had different ways of doing it in
338:43 we had different ways of doing it in terms of binary and decimal and uh unary
338:46 terms of binary and decimal and uh unary even when we started talking about the
338:48 even when we started talking about the same last week in code we started
338:50 same last week in code we started talking about uh data types instead and
338:54 talking about uh data types instead and these data types were a way of telling
338:56 these data types were a way of telling the computer like do you want an integer
338:58 the computer like do you want an integer do you want a character do you want a
339:00 do you want a character do you want a floating point value like a real number
339:03 floating point value like a real number or even a string as we've seen but it
339:05 or even a string as we've seen but it turns out that computers of course only
339:07 turns out that computers of course only have finite amounts of resources your
339:10 have finite amounts of resources your computer only has a fixed amount of
339:12 computer only has a fixed amount of memory or RAM and that actually has very
339:14 memory or RAM and that actually has very real world implications so for instance
339:16 real world implications so for instance here are some of the data types we've
339:18 here are some of the data types we've seen thus far and it turns out that each
339:21 seen thus far and it turns out that each of these in C has a specific number of
339:24 of these in C has a specific number of bits allocated to it now admittedly this
339:26 bits allocated to it now admittedly this can vary by System it's not so much the
339:28 can vary by System it's not so much the case nowaday days but for many years for
339:30 case nowaday days but for many years for decades computers were getting better
339:32 decades computers were getting better and better the earliest computers might
339:34 and better the earliest computers might have used fewer bits for some of these
339:35 have used fewer bits for some of these data types more modern computers might
339:37 data types more modern computers might use more bits so the numbers you're
339:39 use more bits so the numbers you're about to see are pretty much where we
339:40 about to see are pretty much where we are present day so when it comes to
339:42 are present day so when it comes to these data types A bu which is true or
339:46 these data types A bu which is true or false somewhat curiously uses a whole
339:48 false somewhat curiously uses a whole bite even though that's way Overkill
339:50 bite even though that's way Overkill because for a bull true or false you of
339:52 because for a bull true or false you of course only need one bit but it turns
339:54 course only need one bit but it turns out even though it's wasteful to use
339:56 out even though it's wasteful to use eight bits or one bite just to represent
339:59 eight bits or one bite just to represent true or false it's just easier for
340:00 true or false it's just easier for computers so a bull tends to be one bite
340:02 computers so a bull tends to be one bite an INT which we've been using a lot uses
340:04 an INT which we've been using a lot uses four bytes typically or 32 bits and if I
340:07 four bytes typically or 32 bits and if I do some quick math from week zero with
340:09 do some quick math from week zero with 32 bits you have four billion possible
340:12 32 bits you have four billion possible values roughly but if you want to
340:14 values roughly but if you want to represent positive and negative that
340:16 represent positive and negative that means you can represent roughly -2
340:18 means you can represent roughly -2 billion all the way up to positive2
340:20 billion all the way up to positive2 billion so that's the range typically
340:21 billion so that's the range typically within if that's to few numbers for you
340:25 within if that's to few numbers for you turns out there's things called Longs
340:26 turns out there's things called Longs and longs use 64 bits which allow you to
340:29 and longs use 64 bits which allow you to have like a quintilian number of
340:32 have like a quintilian number of possibilities which is a lot certainly a
340:34 possibilities which is a lot certainly a lot more than 4 billion so sometimes you
340:36 lot more than 4 billion so sometimes you might use a long but even that's finite
340:38 might use a long but even that's finite and so uh as we discussed at the end of
340:40 and so uh as we discussed at the end of last week bad things can happen if you
340:42 last week bad things can happen if you make certain assumptions as the data
340:44 make certain assumptions as the data because of things like Inger overflow or
340:46 because of things like Inger overflow or the like where things wrap around then
340:48 the like where things wrap around then there's a float which is a real number
340:50 there's a float which is a real number something with a decimal point by
340:51 something with a decimal point by convention it's four bytes or 32 bits
340:54 convention it's four bytes or 32 bits which gives you in short only a specific
340:56 which gives you in short only a specific amount of precision it doesn't
340:57 amount of precision it doesn't necessarily dictate how many numbers to
341:00 necessarily dictate how many numbers to the left or to the right in the
341:01 the left or to the right in the aggregate ultimately you have though uh
341:04 aggregate ultimately you have though uh three uh 4 billion possible permutations
341:07 three uh 4 billion possible permutations still if you need more Precision for
341:08 still if you need more Precision for scientific for medical for financial
341:10 scientific for medical for financial applications you might use eight bytes
341:13 applications you might use eight bytes AKA a double which just gives you more
341:16 AKA a double which just gives you more digits of precision they eventually get
341:18 digits of precision they eventually get imprecise per the example we looked at
341:20 imprecise per the example we looked at last week but it at least gets you
341:22 last week but it at least gets you further down the line as an aside in
341:24 further down the line as an aside in really really important applications in
341:27 really really important applications in finance and medicine and Military
341:29 finance and medicine and Military operations and the like where you really
341:30 operations and the like where you really can't have rounding errors long story
341:33 can't have rounding errors long story short humans have developed libraries in
341:35 short humans have developed libraries in C and other languages that use more even
341:38 C and other languages that use more even than 8 bytes so there are solutions to
341:40 than 8 bytes so there are solutions to these problems but they're always finite
341:41 these problems but they're always finite you have to pick uh an upper bound then
341:44 you have to pick uh an upper bound then there's Char which we saw briefly last
341:46 there's Char which we saw briefly last week when I asked the user for y or n
341:48 week when I asked the user for y or n for yes or no and then there's string
341:50 for yes or no and then there's string which I'm going to propose as a question
341:51 which I'm going to propose as a question mark because a string totally depends
341:54 mark because a string totally depends like high h i exclamation point would
341:57 like high h i exclamation point would seem to be three bytes d a ID would seem
342:00 seem to be three bytes d a ID would seem to be five so strings clearly are
342:02 to be five so strings clearly are variable based on what you or the human
342:04 variable based on what you or the human type in so we'll see what this means
342:06 type in so we'll see what this means though in just a bit this though is the
342:09 though in just a bit this though is the thing inside of your Mac your PC your
342:11 thing inside of your Mac your PC your phone might not look exactly like this
342:13 phone might not look exactly like this but this is a a memory module for a
342:15 but this is a a memory module for a modern computer and let's go ahead and
342:16 modern computer and let's go ahead and use this really as just representative
342:18 use this really as just representative of the finite amount of memory that any
342:20 of the finite amount of memory that any computer indeed has let's zoom in on one
342:21 computer indeed has let's zoom in on one of these little black chips on the uh uh
342:24 of these little black chips on the uh uh circuit board here zoom in and let me
342:27 circuit board here zoom in and let me propose that this rectangle really
342:29 propose that this rectangle really represents some number of bytes like
342:31 represents some number of bytes like tucked inside of this little black uh
342:33 tucked inside of this little black uh circuit on the board is maybe I don't
342:35 circuit on the board is maybe I don't know a gigabyte a billion bytes maybe
342:37 know a gigabyte a billion bytes maybe it's a 100 btes some number of bytes it
342:39 it's a 100 btes some number of bytes it totally depends on the computer and how
342:40 totally depends on the computer and how much you paid for the Stick of memory
342:42 much you paid for the Stick of memory but if there's a finite number of bytes
342:45 but if there's a finite number of bytes physically implemented somehow digitally
342:48 physically implemented somehow digitally inside of this Hardware well then it
342:50 inside of this Hardware well then it stands to reason that we could number
342:51 stands to reason that we could number those bytes we can just arbitrarily
342:53 those bytes we can just arbitrarily decide that the top left corner is bite
342:56 decide that the top left corner is bite number one or really bite number zero
342:58 number one or really bite number zero the one next to it is number one then
343:00 the one next to it is number one then number two number three dot dot dot
343:02 number two number three dot dot dot number two billion or whatever it is
343:04 number two billion or whatever it is however big this this memory is so if
343:06 however big this this memory is so if you are use a variable in a c program
343:09 you are use a variable in a c program that's only one bite like a Char it
343:11 that's only one bite like a Char it might literally be stored in that top
343:13 might literally be stored in that top left hand corner of the memory like in
343:15 left hand corner of the memory like in practice you don't care where physically
343:17 practice you don't care where physically it is but really the artist rendition
343:19 it is but really the artist rendition would be this a Char might use one of
343:21 would be this a Char might use one of those single btes somewhere in the
343:23 those single btes somewhere in the computer's memory if you use an INT
343:25 computer's memory if you use an INT which is four bytes it would give you
343:27 which is four bytes it would give you four bytes contiguous that is left to
343:29 four bytes contiguous that is left to right top to bottom but all 32 bits
343:32 right top to bottom but all 32 bits would be next to each other so the
343:33 would be next to each other so the computer knows that those indeed all
343:34 computer knows that those indeed all belong to the same int if you need a
343:36 belong to the same int if you need a long or a double for that matter then
343:38 long or a double for that matter then you might use a full eight bytes in this
343:40 you might use a full eight bytes in this case and you just keep using and using
343:42 case and you just keep using and using this memory kind of like a a canvas you
343:44 this memory kind of like a a canvas you know almost in Photoshop or a
343:46 know almost in Photoshop or a spreadsheet where you can just move uh
343:48 spreadsheet where you can just move uh pixels or you can move data around
343:49 pixels or you can move data around that's really what your computer's
343:51 that's really what your computer's memory is a canvas for storing uh
343:53 memory is a canvas for storing uh information in units of bytes or 8 Bits
343:57 information in units of bytes or 8 Bits now we don't need to keep looking at
343:58 now we don't need to keep looking at these circuit boards can abstract it
343:59 these circuit boards can abstract it away as we often do and let's go ahead
344:01 away as we often do and let's go ahead and zoom in on this grid just to
344:03 and zoom in on this grid just to consider some very specific variables so
344:05 consider some very specific variables so let me zoom in and now I see fewer but
344:07 let me zoom in and now I see fewer but larger uh boxes on the screen Each of
344:09 larger uh boxes on the screen Each of which again represents a bite and now
344:11 which again represents a bite and now let me propose that we play with some
344:13 let me propose that we play with some actual code so here in C albeit without
344:16 actual code so here in C albeit without a full program or three ins score one
344:19 a full program or three ins score one score two score three I have
344:21 score two score three I have coincidentally given myself two uh uh
344:24 coincidentally given myself two uh uh two scores at around 72 and 73 and then
344:27 two scores at around 72 and 73 and then a pretty low score at 33 of course
344:28 a pretty low score at 33 of course course last week or two weeks ago this
344:30 course last week or two weeks ago this would have been high but now we're
344:32 would have been high but now we're dealing with actual integers so these
344:33 dealing with actual integers so these are three so so scores on my quizzes or
344:36 are three so so scores on my quizzes or tests or the like so let me go to vs
344:38 tests or the like so let me go to vs code here and let's make a program
344:40 code here and let's make a program called scores. C so I'm going to write
344:42 called scores. C so I'm going to write code scores. C that's going to give me
344:44 code scores. C that's going to give me my new file and let me go ahead and
344:46 my new file and let me go ahead and Implement something like this include
344:49 Implement something like this include standard i.h int main void and then
344:53 standard i.h int main void and then inside of here let me do int score one
344:55 inside of here let me do int score one will be 72 uh int score two be 73 and
344:59 will be 72 uh int score two be 73 and int score three will be 33 and then let
345:03 int score three will be 33 and then let me just do something like write a
345:04 me just do something like write a program to average my three test scores
345:06 program to average my three test scores together something like that so let me
345:08 together something like that so let me do print F quote unquote my average is
345:12 do print F quote unquote my average is and I'm going to go ahead and do say
345:14 and I'm going to go ahead and do say percent I back sln and now let me plug
345:17 percent I back sln and now let me plug in the results and this is kind of grade
345:19 in the results and this is kind of grade school math now how do I compute the
345:20 school math now how do I compute the average of three values well just like
345:22 average of three values well just like in on paper I can do score one plus
345:26 in on paper I can do score one plus score two plus score three in
345:29 score two plus score three in parentheses because of order of
345:30 parentheses because of order of operations divided by three since
345:32 operations divided by three since there's three total scores all right so
345:34 there's three total scores all right so I think this checks out and indeed you
345:36 I think this checks out and indeed you can use parentheses and operators like
345:38 can use parentheses and operators like plus and your code like this in C let me
345:40 plus and your code like this in C let me go ahead now and do make scores no
345:43 go ahead now and do make scores no syntax error so that's good nothing
345:45 syntax error so that's good nothing missing there and now let me do dot
345:46 missing there and now let me do dot scores and see what my test average is
345:48 scores and see what my test average is all right you know it's not great but I
345:50 all right you know it's not great but I I I think I still passed and indeed my
345:53 I I think I still passed and indeed my average here is is 59 is it precisely 59
345:57 average here is is 59 is it precisely 59 though well let's see let's let's
345:59 though well let's see let's let's actually instead of using an INT how
346:01 actually instead of using an INT how about we go ahead and use something like
346:02 about we go ahead and use something like a floating point value here and let me
346:05 a floating point value here and let me go ahead and do this so let me recompile
346:07 go ahead and do this so let me recompile my code make scores huh all right I've
346:09 my code make scores huh all right I've got an issue let me zoom in on my
346:11 got an issue let me zoom in on my terminal window we've not seen this one
346:13 terminal window we've not seen this one necessarily before but error on line
346:15 necessarily before but error on line nine format specifies type double which
346:19 nine format specifies type double which is a lot of precision but the argument
346:21 is a lot of precision but the argument has Type in so what does this mean well
346:23 has Type in so what does this mean well it's showing me with these green
346:24 it's showing me with these green squigglies that something's bad between
346:26 squigglies that something's bad between the percent F and this thing over here
346:28 the percent F and this thing over here well on the left I'm implying a float or
346:31 well on the left I'm implying a float or a double for that matter on the right
346:33 a double for that matter on the right though what data type are score one
346:34 though what data type are score one score two score three all right so
346:37 score two score three all right so they're ins so Cent does not like this
346:39 they're ins so Cent does not like this the compiler just doesn't like that I'm
346:40 the compiler just doesn't like that I'm using ins on the right but I want floats
346:43 using ins on the right but I want floats on the left so there's going to be
346:44 on the left so there's going to be different ways of solving this one way
346:47 different ways of solving this one way would be to just ignore the problem like
346:48 would be to just ignore the problem like I originally did and just go back to
346:51 I originally did and just go back to percent I or as an aside percent D is
346:55 percent I or as an aside percent D is often an alternative to percent I for a
346:57 often an alternative to percent I for a decimal number but we use perc I cuz it
346:59 decimal number but we use perc I cuz it sounds like int so percent I is fine
347:01 sounds like int so percent I is fine here too but I don't want to just avoid
347:03 here too but I don't want to just avoid the problem I want to actually display a
347:05 the problem I want to actually display a floating point value so how can I fix
347:07 floating point value so how can I fix this well it turns out I can solve this
347:08 this well it turns out I can solve this in a few different ways the simplest is
347:11 in a few different ways the simplest is just to make sure that at least one
347:12 just to make sure that at least one number on the right is a floating point
347:14 number on the right is a floating point value like
347:16 value like 3.0 instead of just three now I think
347:20 3.0 instead of just three now I think clang will be happier let me do make
347:22 clang will be happier let me do make scores enter and indeed it's okay why as
347:25 scores enter and indeed it's okay why as soon as you have at least one more
347:28 soon as you have at least one more precise data data type on the right it
347:30 precise data data type on the right it just treats everything at that point as
347:31 just treats everything at that point as floating point value so that the math
347:33 floating point value so that the math works out so/ scores enter and now there
347:36 works out so/ scores enter and now there we go right you know uh some of us might
347:38 we go right you know uh some of us might really want that third of a point our
347:40 really want that third of a point our average was not 59 it's 59 and a third
347:43 average was not 59 it's 59 and a third as in this case here all right so we've
347:45 as in this case here all right so we've solved that there as an aside though
347:47 solved that there as an aside though there's one other technique to uh to
347:49 there's one other technique to uh to show here if you didn't want to change
347:51 show here if you didn't want to change it to 3.0 because that's a little weird
347:53 it to 3.0 because that's a little weird because I there were literally three
347:55 because I there were literally three scores it's not like that needs to have
347:57 scores it's not like that needs to have a decimal point you could also
347:59 a decimal point you could also explicitly convert the three to a float
348:03 explicitly convert the three to a float by saying in parentheses float this is
348:06 by saying in parentheses float this is what's called type casting and this will
348:08 what's called type casting and this will just convert the thing right after it to
348:10 just convert the thing right after it to that data type if it's possible so if I
348:12 that data type if it's possible so if I do this again make scores no errors now
348:15 do this again make scores no errors now do/ scores and I get in fact the same
348:17 do/ scores and I get in fact the same result there's a bit of a rounding issue
348:19 result there's a bit of a rounding issue here but we know the rounding relates to
348:21 here but we know the rounding relates to the imprecision from last week for now
348:24 the imprecision from last week for now let me just be happy with my 59.3
348:26 let me just be happy with my 59.3 something I'll take that for now but
348:28 something I'll take that for now but this is you know is close to um a good
348:32 this is you know is close to um a good enough correct answer for me now but how
348:34 enough correct answer for me now but how do I think about now what's going on
348:37 do I think about now what's going on inside of the computer's memory well
348:38 inside of the computer's memory well let's consider here's that same grid of
348:40 let's consider here's that same grid of memory each box represents a bite where
348:42 memory each box represents a bite where are score one score two and score three
348:44 are score one score two and score three in my memory well score one let me just
348:47 in my memory well score one let me just propose is at the top left but it's
348:48 propose is at the top left but it's taking up four boxes for four bytes
348:51 taking up four boxes for four bytes score two probably ends up right next to
348:54 score two probably ends up right next to it in memory though this isn't always
348:55 it in memory though this isn't always going to be the case but I've chosen
348:57 going to be the case but I've chosen simple examples 73 is next to it also
348:59 simple examples 73 is next to it also taking up four bytes and then lastly 33
349:02 taking up four bytes and then lastly 33 is in score three uh down there
349:04 is in score three uh down there underneath now if we don't if we really
349:07 underneath now if we don't if we really look at the computer's memory look at it
349:08 look at the computer's memory look at it with some kind of microscope or the like
349:10 with some kind of microscope or the like there's actually 32 bits 32 bits 32 bits
349:13 there's actually 32 bits 32 bits 32 bits in each of those four uh groups of four
349:17 in each of those four uh groups of four bytes representing those values but
349:19 bytes representing those values but again for today's purposes onwards we
349:20 again for today's purposes onwards we don't really need to think again and
349:22 don't really need to think again and again in binary it's just indeed these
349:23 again in binary it's just indeed these decimal numbers being stored there but I
349:26 decimal numbers being stored there but I claim now this isn't the best design
349:28 claim now this isn't the best design even if if you have never programmed
349:29 even if if you have never programmed before cs50 what you're looking at here
349:32 before cs50 what you're looking at here on the screen as an excerpt in what
349:34 on the screen as an excerpt in what sense is this perhaps bad design even
349:36 sense is this perhaps bad design even though it's a correct way of storing
349:37 though it's a correct way of storing three test
349:39 three test scores what's kind of bad here
349:48 yeah yeah always do exactly what you did extrapolate to four scores five scores
349:50 extrapolate to four scores five scores 50 scores this can't be that welld
349:52 50 scores this can't be that welld designed because now you're going to
349:54 designed because now you're going to have four lines of code five lines of
349:55 have four lines of code five lines of code 50 lines of code that are almost
349:57 code 50 lines of code that are almost identical except for this like arbitrary
349:59 identical except for this like arbitrary number that we're updating at the end of
350:01 number that we're updating at the end of the variable so indeed there's probably
350:03 the variable so indeed there's probably going to be a better way even though at
350:05 going to be a better way even though at least in C we haven't yet seen that
350:08 least in C we haven't yet seen that technique but the solution today onward
350:10 technique but the solution today onward is going to be something called an array
350:12 is going to be something called an array an array is a uh way of storing your
350:15 an array is a uh way of storing your data back to back to back in the
350:18 data back to back to back in the computer's memory in such a way that you
350:20 computer's memory in such a way that you can access each individual member easily
350:24 can access each individual member easily put another way with an array you can
350:26 put another way with an array you can instead do something like this instead
350:28 instead do something like this instead of saying int score one int score 2 in
350:31 of saying int score one int score 2 in score three giving each a value you can
350:33 score three giving each a value you can first tell the computer please give me a
350:36 first tell the computer please give me a variable called scores plural that you
350:39 variable called scores plural that you can call it anything you want of size
350:41 can call it anything you want of size three Each of which will be an integer
350:44 three Each of which will be an integer that is to say this is how you declare
350:45 that is to say this is how you declare an array in C that will have enough room
350:49 an array in C that will have enough room to store three integers put another way
350:51 to store three integers put another way this is the technical way of telling the
350:53 this is the technical way of telling the computer please give me uh 12 bytes in
350:57 computer please give me uh 12 bytes in total
350:58 total 3 * 4 each for an inch so give me 12
351:01 3 * 4 each for an inch so give me 12 bytes in total and what the computer
351:03 bytes in total and what the computer will do is guarantee that they're back
351:05 will do is guarantee that they're back to back to back in the computer's memory
351:07 to back to back in the computer's memory and that'll be useful in just a moment
351:09 and that'll be useful in just a moment so let me go ahead and do something
351:10 so let me go ahead and do something useful with this let me store three
351:12 useful with this let me store three actual scores here's how I could now
351:14 actual scores here's how I could now store those same numeric scores in this
351:17 store those same numeric scores in this array syntax is a little different but
351:20 array syntax is a little different but there's one variable called scores but
351:23 there's one variable called scores but if you want to go to its first location
351:24 if you want to go to its first location starting today you use square brackets
351:26 starting today you use square brackets and go to location zero first which
351:29 and go to location zero first which because things in C are zero indexed uh
351:32 because things in C are zero indexed uh so to speak you start counting at zero
351:34 so to speak you start counting at zero the first int is at bracket zero second
351:36 the first int is at bracket zero second int is at bracket 1 third int is at
351:38 int is at bracket 1 third int is at bracket two so it's not 1 two three it's
351:40 bracket two so it's not 1 two three it's literally 0 one2 and this is not
351:42 literally 0 one2 and this is not something you have control over you must
351:44 something you have control over you must start at zero so these lines now create
351:48 start at zero so these lines now create an array of size three and then insert
351:50 an array of size three and then insert one two three values into that array but
351:53 one two three values into that array but the upside now is that you only have one
351:55 the upside now is that you only have one name of the variable to remember it's
351:57 name of the variable to remember it's just called scores yes you need to go
352:00 just called scores yes you need to go into the array to get individual values
352:03 into the array to get individual values you need to index into it using those
352:05 you need to index into it using those square brackets but at least you don't
352:07 square brackets but at least you don't have this hackish approach of declaring
352:08 have this hackish approach of declaring a separate variable for each and every
352:11 a separate variable for each and every one of these values so let me go back to
352:13 one of these values so let me go back to scores do c here and let me propose that
352:16 scores do c here and let me propose that I do this let me just kind of use that
352:18 I do this let me just kind of use that same idea to do the following let me get
352:20 same idea to do the following let me get rid of these three separate integers let
352:22 rid of these three separate integers let me give myself an INT scores array of
352:25 me give myself an INT scores array of size three and then scores bracket Z
352:28 size three and then scores bracket Z will as before be 72 scores bracket 1
352:32 will as before be 72 scores bracket 1 will be 73 and scores bracket 2 will be
352:35 will be 73 and scores bracket 2 will be 33 and let me get rid of the Little Dot
352:38 33 and let me get rid of the Little Dot there all right so now if I go ahead and
352:40 there all right so now if I go ahead and run this again with make uh scores
352:44 run this again with make uh scores enter huh what did I do wrong here I
352:49 enter huh what did I do wrong here I think I got a little too ahead of myself
352:51 think I got a little too ahead of myself let me increase my terminal
352:55 let me increase my terminal window let's focus on line 10 here first
352:58 window let's focus on line 10 here first error use of Undeclared identifier score
353:01 error use of Undeclared identifier score one what did I do here that was dumb
353:09 yeah right so I didn't declare score one I've got old code right so I just kind
353:10 I've got old code right so I just kind of honestly got ahead of myself here not
353:12 of honestly got ahead of myself here not even intentionally so let me go ahead
353:14 even intentionally so let me go ahead and Shrink my terminal window again I
353:15 and Shrink my terminal window again I need to finish my thought here so let me
353:17 need to finish my thought here so let me clear my terminal and let me change this
353:20 clear my terminal and let me change this now to be scores brackets zero plus
353:23 now to be scores brackets zero plus scores bracket 1 plus scores bracket two
353:25 scores bracket 1 plus scores bracket two so it's a little more verbose because
353:27 so it's a little more verbose because I've got these these um square brackets
353:29 I've got these these um square brackets so to speak but I think now my code is
353:31 so to speak but I think now my code is consistent so let me make scores now now
353:34 consistent so let me make scores now now compiles do/ scores gives me indeed the
353:37 compiles do/ scores gives me indeed the same rough average with those same
353:39 same rough average with those same values all right so let me go ahead and
353:41 values all right so let me go ahead and maybe um tidy enhance this a little bit
353:44 maybe um tidy enhance this a little bit it's a little silly to have to write a
353:45 it's a little silly to have to write a special program just to check your
353:47 special program just to check your average of three test scores like 72 73
353:50 average of three test scores like 72 73 33 why don't I actually make the program
353:52 33 why don't I actually make the program Dynamic and ask the human for that
353:55 Dynamic and ask the human for that average uh for those scores so instead
353:57 average uh for those scores so instead let me do do this how about we get rid
353:59 let me do do this how about we get rid of the 72 and change this to get int and
354:03 of the 72 and change this to get int and I'll just prompt the user for a score
354:06 I'll just prompt the user for a score let me get rid of the 33 and change 73
354:08 let me get rid of the 33 and change 73 and get this to be get int score quote
354:11 and get this to be get int score quote unquote and then lastly get rid of the
354:13 unquote and then lastly get rid of the 33 and replace it with get int quote
354:15 33 and replace it with get int quote unquote score get int is a cs50 thing
354:18 unquote score get int is a cs50 thing for now so I need to include cs50.h as
354:22 for now so I need to include cs50.h as always but I think now it's sort of a
354:24 always but I think now it's sort of a better program because now I can compile
354:26 better program because now I can compile it once I can even share it with my
354:28 it once I can even share it with my friends and now any of us can average
354:30 friends and now any of us can average three scores on some classes test they
354:32 three scores on some classes test they don't need to know the code or rewrite
354:34 don't need to know the code or rewrite the code just to T type in their scores
354:36 the code just to T type in their scores so make scores worked scores now I can
354:40 so make scores worked scores now I can type anything I want maybe it's a 72 73
354:44 type anything I want maybe it's a 72 73 33 still get the same answer or maybe
354:46 33 still get the same answer or maybe I'm having a better semester 100 100
354:49 I'm having a better semester 100 100 maybe 99 and now we get still a pretty
354:52 maybe 99 and now we get still a pretty high score there but now it's Dynamic
354:54 high score there but now it's Dynamic now you don't need the source code you
354:55 now you don't need the source code you don't need to recompile the program it's
354:57 don't need to recompile the program it's just going to work work again and again
354:59 just going to work work again and again but this too let me propose that this
355:02 but this too let me propose that this code is correct if I want to get three
355:04 code is correct if I want to get three scores from the user but these
355:06 scores from the user but these highlighted lines now 6 through n are
355:09 highlighted lines now 6 through n are they welld designed would you
355:12 they welld designed would you say
355:14 say yeah yeah right this is we can use a
355:16 yeah yeah right this is we can use a loop is the spoiler here why I mean my
355:19 loop is the spoiler here why I mean my God it's like the same code again and
355:20 God it's like the same code again and again and again the only thing that's
355:21 again and again the only thing that's changing is the number and you it should
355:23 changing is the number and you it should have kind of had some code smell again
355:26 have kind of had some code smell again because if I keep typing the same thing
355:27 because if I keep typing the same thing again again and again like that's
355:29 again again and again like that's clearly an opportunity to better design
355:31 clearly an opportunity to better design something so let me do this let me go
355:33 something so let me do this let me go ahead and still create my ver my array
355:36 ahead and still create my ver my array of size three and but let me use our old
355:39 of size three and but let me use our old friend the for Loop for in I equals z i
355:43 friend the for Loop for in I equals z i Less Than 3 I ++ and then in here let me
355:47 Less Than 3 I ++ and then in here let me do scores bracket we haven't seen this
355:50 do scores bracket we haven't seen this before but any
355:51 before but any intuition scores bracket I because that
355:55 intuition scores bracket I because that will use whatever I is be it Z or one or
355:59 will use whatever I is be it Z or one or two in iteration and then I can get an
356:01 two in iteration and then I can get an INT asking the user for score without
356:04 INT asking the user for score without having to repeat myself again and again
356:06 having to repeat myself again and again so hopefully if I didn't make any typos
356:08 so hopefully if I didn't make any typos make scores all good do/ score 72 73 33
356:13 make scores all good do/ score 72 73 33 and we're back in business but the code
356:14 and we're back in business but the code is arguably now better designed because
356:16 is arguably now better designed because now I haven't actually uh I haven't
356:19 now I haven't actually uh I haven't actually hardcoded the scores and I
356:21 actually hardcoded the scores and I haven't actually copied and pasted any
356:23 haven't actually copied and pasted any of that code well if we consider now
356:26 of that code well if we consider now what's going on inside of the computer's
356:27 what's going on inside of the computer's memory it's pretty much the same in
356:29 memory it's pretty much the same in terms of the values but instead of the
356:31 terms of the values but instead of the variables being literally score one
356:33 variables being literally score one score two score three there's just one
356:36 score two score three there's just one variable it's an array called scores but
356:39 variable it's an array called scores but you can index into its three locations
356:42 you can index into its three locations by using scores bracket Z to get the
356:44 by using scores bracket Z to get the first scores bracket one to get the
356:46 first scores bracket one to get the second scores bracket two to get the
356:48 second scores bracket two to get the third but this is key the memory is
356:50 third but this is key the memory is contiguous it's only the screen is only
356:53 contiguous it's only the screen is only so large so it wraps around but
356:55 so large so it wraps around but physically digitally the memory is
356:57 physically digitally the memory is contiguous top to bottom left to right
357:00 contiguous top to bottom left to right and that's important why because the
357:02 and that's important why because the brackets indicate 0 1 2 that each of
357:04 brackets indicate 0 1 2 that each of these integers is just one integer away
357:07 these integers is just one integer away from the next it can't be randomly down
357:09 from the next it can't be randomly down here all of a sudden it's got to be back
357:12 here all of a sudden it's got to be back to back to back all right now equipped
357:14 to back to back all right now equipped with that Paradigm what more could we
357:18 with that Paradigm what more could we actually do here well it turns out it's
357:21 actually do here well it turns out it's worth knowing that it's possible in code
357:24 worth knowing that it's possible in code to even pass arrays around as arguments
357:26 to even pass arrays around as arguments and let me just whip this program up
357:28 and let me just whip this program up somewhat quickly just so you've seen it
357:30 somewhat quickly just so you've seen it before long but let me go ahead and do
357:32 before long but let me go ahead and do this let me propose that I create a
357:35 this let me propose that I create a function that does this averaging for me
357:38 function that does this averaging for me so I'm going to create a function called
357:39 so I'm going to create a function called average that returns a float uh and the
357:43 average that returns a float uh and the arguments this thing is going to take uh
357:46 arguments this thing is going to take uh let's see it's going to be the array so
357:48 let's see it's going to be the array so it turns out if you want to take in an
357:50 it turns out if you want to take in an array of numbers you can call it
357:51 array of numbers you can call it anything you want this is how you tell C
357:55 anything you want this is how you tell C that a function takes not an integer but
357:58 that a function takes not an integer but in Array of integers and you don't have
358:00 in Array of integers and you don't have to call it array I'm doing that just for
358:01 to call it array I'm doing that just for the sake of discussion it can be called
358:03 the sake of discussion it can be called X it can be numbers it can be anything
358:04 X it can be numbers it can be anything else I'm just calling an array to be
358:06 else I'm just calling an array to be super explicit as to what it is there
358:09 super explicit as to what it is there now how do I change my code down here
358:11 now how do I change my code down here what I think I'm going to do for the
358:12 what I think I'm going to do for the moment is just this I'm going to get rid
358:15 moment is just this I'm going to get rid of this code here where I manually
358:17 of this code here where I manually computed the average and let me just
358:19 computed the average and let me just call the average function here by
358:21 call the average function here by passing in the whole array of scores so
358:25 passing in the whole array of scores so this is just an example of abstraction
358:26 this is just an example of abstraction like now I have a fun function called
358:28 like now I have a fun function called average I don't care I don't have to
358:30 average I don't care I don't have to remember how it works once I implement
358:31 remember how it works once I implement it it just kind of tightens up my main
358:33 it it just kind of tightens up my main code a little bit but I do still have to
358:35 code a little bit but I do still have to implement this so later in my file let
358:38 implement this so later in my file let me repeat myself before the only time
358:39 me repeat myself before the only time it's okay and see to repeat yourself
358:41 it's okay and see to repeat yourself again and again by typing out again
358:43 again and again by typing out again average and then int array Open Bracket
358:46 average and then int array Open Bracket but now not a semicolon now I have to
358:49 but now not a semicolon now I have to implement this thing and I can implement
358:50 implement this thing and I can implement this in a bunch of different ways uh but
358:54 this in a bunch of different ways uh but I don't know huh in advance I can't just
358:58 I don't know huh in advance I can't just do this I can't just do array bracket 0
359:01 do this I can't just do array bracket 0 plus array bracket 1 plus array bracket
359:06 plus array bracket 1 plus array bracket 2 unless unless this program is only
359:09 2 unless unless this program is only ever going to work on three numbers so
359:13 ever going to work on three numbers so huh let me let me go ahead and do this
359:15 huh let me let me go ahead and do this let me first propose that there's a poor
359:17 let me first propose that there's a poor design here in my main function what
359:20 design here in my main function what value have I repeated
359:26 twice among the highlighted lines what jumps out at use twice L of the array
359:28 jumps out at use twice L of the array yeah the length of the array is just
359:30 yeah the length of the array is just three now it's not a huge deal that I
359:32 three now it's not a huge deal that I type the number three on line eight on
359:33 type the number three on line eight on line nine but this is exactly the kind
359:35 line nine but this is exactly the kind of like shortcut that's going to get you
359:37 of like shortcut that's going to get you in trouble eventually why because
359:38 in trouble eventually why because eventually you or someone else is going
359:40 eventually you or someone else is going to go in make the array bigger or
359:41 to go in make the array bigger or smaller and you're not going to realize
359:43 smaller and you're not going to realize that magically that same number is in
359:45 that magically that same number is in two places and indeed this is what a
359:47 two places and indeed this is what a programmer would often call a magic
359:48 programmer would often call a magic number a magic number is one that just
359:50 number a magic number is one that just kind of appears magically and you're on
359:52 kind of appears magically and you're on the honor System to change it here if
359:54 the honor System to change it here if you change it here and then you change
359:55 you change it here and then you change it over here like that's not going to
359:57 it over here like that's not going to end well if it the onus is on the
359:58 end well if it the onus is on the programmer to remember where they
360:00 programmer to remember where they hardcoded that is rote out three
360:02 hardcoded that is rote out three explicitly so anytime you reuse a value
360:05 explicitly so anytime you reuse a value like this you know what we should
360:06 like this you know what we should probably do what we did last week which
360:08 probably do what we did last week which was to declare a variable uh perhaps at
360:11 was to declare a variable uh perhaps at the very top of my program so it's super
360:12 the very top of my program so it's super obvious what it is called maybe n and
360:15 obvious what it is called maybe n and set that equal to three better yet what
360:17 set that equal to three better yet what did I do last week to make sure that I
360:19 did I do last week to make sure that I Can't Screw Up and accidentally change
360:20 Can't Screw Up and accidentally change that value yeah constant and the keyword
360:23 that value yeah constant and the keyword there was just const for short and now I
360:26 there was just const for short and now I have a global variable Global in the
360:28 have a global variable Global in the sense that I can access it anywhere that
360:30 sense that I can access it anywhere that is called n it's an INT and it's always
360:33 is called n it's an INT and it's always going to be three and now I can improve
360:35 going to be three and now I can improve my main function a little bit by just
360:37 my main function a little bit by just changing the threes to n so now if I if
360:40 changing the threes to n so now if I if a colleague realize oh wait a minute
360:42 a colleague realize oh wait a minute there's four tests this year you change
360:44 there's four tests this year you change n to four recompile the code and it just
360:45 n to four recompile the code and it just works everywhere else except in my
360:50 works everywhere else except in my average function let me change it back
360:51 average function let me change it back to three just for consistency this is
360:54 to three just for consistency this is not going to fly now to just uh sum up
360:58 not going to fly now to just uh sum up things like this for instance and then
361:00 things like this for instance and then return this divided three why will this
361:04 return this divided three why will this not work
361:06 not work now as I've defined
361:10 now as I've defined it
361:19 yeah okay I might be in returning an integer value when I intend to return a
361:21 integer value when I intend to return a float per this but I think I'm okay CU I
361:23 float per this but I think I'm okay CU I Ed that little trick where I made sure
361:25 Ed that little trick where I made sure that at least one of the numbers in my
361:27 that at least one of the numbers in my my arithmetic expression is in fact a
361:29 my arithmetic expression is in fact a floating point value and just by adding
361:31 floating point value and just by adding the point zero make sure that everything
361:33 the point zero make sure that everything gets treated as a float so I think
361:35 gets treated as a float so I think that's
361:38 that's okay sorry a little
361:45 ladder exactly so left hand's not talking to the right hand here and that
361:47 talking to the right hand here and that my current implementation of average is
361:50 my current implementation of average is still assuming that there's only going
361:51 still assuming that there's only going to be three tests or whatever but wait a
361:53 to be three tests or whatever but wait a minute I just went through the trouble
361:55 minute I just went through the trouble of modifying this to be n generically
361:59 of modifying this to be n generically and if I change this to four I'm not
362:01 and if I change this to four I'm not going to be happy perhaps with my
362:03 going to be happy perhaps with my average because now I'm going to ignore
362:04 average because now I'm going to ignore one of my test scores Al together so let
362:06 one of my test scores Al together so let me change this back to three and
362:08 me change this back to three and unfortunately if it's a variable now n
362:12 unfortunately if it's a variable now n and therefore I have literally a
362:13 and therefore I have literally a variable number of scores how do I take
362:16 variable number of scores how do I take the average of a variable number of
362:19 the average of a variable number of things I mean what's my building block
362:22 things I mean what's my building block there
362:32 yeah why don't I use a loop that goes through the array and adds things up as
362:34 through the array and adds things up as you go I mean kind of like grade school
362:35 you go I mean kind of like grade school as you take the average on your
362:36 as you take the average on your calculator or paper pencil you just keep
362:38 calculator or paper pencil you just keep adding the numbers together and then you
362:39 adding the numbers together and then you divide at the End by the total number of
362:41 divide at the End by the total number of things so how can I do this well let me
362:43 things so how can I do this well let me change my implementation of average to
362:45 change my implementation of average to First declare a a variable called sum or
362:48 First declare a a variable called sum or whatever set it equal to zero so this is
362:50 whatever set it equal to zero so this is like me on my piece of paper getting
362:52 like me on my piece of paper getting ready to count or my calculator of
362:53 ready to count or my calculator of course when you turn it on typically
362:55 course when you turn it on typically defaults to zero and now let me do four
362:57 defaults to zero and now let me do four in I equals z i is less than a VAR I no
363:02 in I equals z i is less than a VAR I no I didn't do that n i is less than n
363:05 I didn't do that n i is less than n i++ and now in here let me go ahead and
363:09 i++ and now in here let me go ahead and add to the current sum uh whatever is in
363:13 add to the current sum uh whatever is in the arrays location I and then down here
363:16 the arrays location I and then down here I think I can just return sum divided by
363:19 I think I can just return sum divided by 3 nope not 3.0 n perhaps here and
363:25 3 nope not 3.0 n perhaps here and actually I think I'm going to get let's
363:27 actually I think I'm going to get let's make sure it's a float let's use the the
363:29 make sure it's a float let's use the the type casting trick just to make sure I
363:30 type casting trick just to make sure I don't accidentally Short change someone
363:32 don't accidentally Short change someone and uh throw away everything after the
363:34 and uh throw away everything after the decimal point so it just escalated
363:36 decimal point so it just escalated quickly right like average just got a
363:37 quickly right like average just got a lot more involved it's just not just a
363:39 lot more involved it's just not just a single one line of code but now it's
363:41 single one line of code but now it's Dynamic I initialize a variable called
363:43 Dynamic I initialize a variable called sum to zero in this loop I go through
363:46 sum to zero in this loop I go through and just keep adding to sum which is
363:49 and just keep adding to sum which is initially zero whatever is in Array
363:51 initially zero whatever is in Array bracket I or specifically array bracket
363:54 bracket I or specifically array bracket zero array bracket one array bracket two
363:56 zero array bracket one array bracket two that gives me a total sum that I return
363:58 that gives me a total sum that I return divided by the total number of things
364:00 divided by the total number of things now this I can tighten slightly recall
364:02 now this I can tighten slightly recall that this is syntactic sugar for just
364:04 that this is syntactic sugar for just adding things I can't use plus plus
364:06 adding things I can't use plus plus because that only literally adds one but
364:08 because that only literally adds one but I can use here plus
364:11 I can use here plus equals questions on this implementation
364:14 equals questions on this implementation here really the only takeaway or the
364:16 here really the only takeaway or the most important takeaway is that this is
364:18 most important takeaway is that this is the Syntax for how you tell a function
364:21 the Syntax for how you tell a function that to it expects a whole array not a
364:24 that to it expects a whole array not a single variable like an INT or the like
364:26 single variable like an INT or the like you literally use square brackets but
364:27 you literally use square brackets but you don't specify the length inside
364:30 you don't specify the length inside there
364:43 top good question what do I have it defined as at the top this variable n it
364:46 defined as at the top this variable n it must be an integer if you're going to uh
364:49 must be an integer if you're going to uh use it inside of in arrays square
364:52 use it inside of in arrays square brackets here so this line 10 notice no
364:55 brackets here so this line 10 notice no longer says three it says n and so
364:58 longer says three it says n and so whatever n is three or four or something
365:00 whatever n is three or four or something else that's how many integers I will get
365:03 else that's how many integers I will get in that array and it must be by
365:04 in that array and it must be by definition of an array an integer that
365:06 definition of an array an integer that goes in those square brackets and here's
365:08 goes in those square brackets and here's a common source of confusion when you
365:10 a common source of confusion when you create the array that is declare it you
365:12 create the array that is declare it you use square brackets like this where you
365:14 use square brackets like this where you put the total number of elements you
365:15 put the total number of elements you want when you subsequently use the array
365:18 want when you subsequently use the array like I'm doing here you don't mention
365:20 like I'm doing here you don't mention int again just like you don't mention in
365:22 int again just like you don't mention in again and again once a variable exists
365:24 again and again once a variable exists you use the square bracket still but you
365:26 you use the square bracket still but you don't use n you use zero or one or two
365:30 don't use n you use zero or one or two or generically here I so when C was
365:32 or generically here I so when C was designed they sometimes use the same
365:34 designed they sometimes use the same Syntax for two different ideas or
365:36 Syntax for two different ideas or contexts
365:44 yeah good question do I have to include line six short answer yes because of the
365:47 line six short answer yes because of the reason we ran into last week C or clang
365:50 reason we ran into last week C or clang really reads your code top to bottom
365:51 really reads your code top to bottom left to right and so if the compiler
365:54 left to right and so if the compiler sees some mention of this function
365:57 sees some mention of this function average on line 16 but you haven't told
365:59 average on line 16 but you haven't told the compiler that average exists you're
366:01 the compiler that average exists you're going to get an error on the screen so
366:03 going to get an error on the screen so the conventional way to do that is you
366:05 the conventional way to do that is you just copy paste the one first line of
366:07 just copy paste the one first line of code from the function it's so-called
366:09 code from the function it's so-called prototype or Declaration
366:17 yeah really good question uh in a perfect segue is there a library you can
366:19 perfect segue is there a library you can use if you don't know the size of the
366:20 use if you don't know the size of the array no and so if any of you have
366:22 array no and so if any of you have programmed in uh in uh Java or python or
366:26 programmed in uh in uh Java or python or other Lang languages you can actually
366:28 other Lang languages you can actually just ask the array like how big is it in
366:31 just ask the array like how big is it in C you and I the programmers have to
366:33 C you and I the programmers have to remember it and so short answer no
366:34 remember it and so short answer no there's no function that will just
366:35 there's no function that will just automatically do this for us and in fact
366:38 automatically do this for us and in fact let me make a more subtle claim that
366:40 let me make a more subtle claim that it's fine to use Global variables like
366:43 it's fine to use Global variables like this if they're really for configuration
366:44 this if they're really for configuration options why it's just convenient to put
366:46 options why it's just convenient to put them at the very top of the file because
366:48 them at the very top of the file because everyone you your colleagues your Tas
366:50 everyone you your colleagues your Tas are going to see them at the top of the
366:51 are going to see them at the top of the code but you really shouldn't be using
366:54 code but you really shouldn't be using them everywhere throughout your code
366:55 them everywhere throughout your code it'd be better if the average function
366:57 it'd be better if the average function itself we're independent of that special
366:59 itself we're independent of that special variable so by that I mean this you know
367:02 variable so by that I mean this you know what I should really do if I really want
367:03 what I should really do if I really want to be welld designed I should pass in
367:07 to be welld designed I should pass in the length of the array to the average
367:10 the length of the array to the average function I should give the average
367:12 function I should give the average function a second argument I'll call it
367:14 function a second argument I'll call it length for instance but I could call it
367:16 length for instance but I could call it anything I want and so rather than
367:18 anything I want and so rather than putting n all the way down here at the
367:21 putting n all the way down here at the bottom of my file let me just
367:22 bottom of my file let me just dynamically say length instead and this
367:25 dynamically say length instead and this is a subtlety and no need need to get
367:27 is a subtlety and no need need to get too tripped up over this but this now is
367:29 too tripped up over this but this now is just an example of how the same function
367:31 just an example of how the same function can take not one but two arguments but
367:33 can take not one but two arguments but indeed in C you must remember yourself
367:38 indeed in C you must remember yourself what the length of an array is you can't
367:40 what the length of an array is you can't just ask the array via some syntax like
367:42 just ask the array via some syntax like you can those of you who've programmed
367:43 you can those of you who've programmed before in Java or python
367:57 yeah good question would it be better to write a function that computes the size
367:58 write a function that computes the size short answer can't do that in C as soon
368:01 short answer can't do that in C as soon as you pass an array into a function in
368:03 as you pass an array into a function in C you cannot figure out its size if it's
368:06 C you cannot figure out its size if it's a generic array like that of integers
368:07 a generic array like that of integers there are special cases that uh you can
368:10 there are special cases that uh you can do that but in general no it's just not
368:12 do that but in general no it's just not possible in see and if that's some
368:13 possible in see and if that's some frustration honestly this is why more
368:15 frustration honestly this is why more modern languages add that feature why
368:17 modern languages add that feature why because it was really annoying um as I'm
368:19 because it was really annoying um as I'm alluding here to not having that
368:20 alluding here to not having that information now just to make sure I
368:22 information now just to make sure I didn't screw up anywhere let me compile
368:23 didn't screw up anywhere let me compile this final version of
368:25 this final version of scores
368:27 scores suspense all good/ scores 72 73 33 and
368:32 suspense all good/ scores 72 73 33 and we're still back in business so this
368:34 we're still back in business so this version is more complicated and as
368:35 version is more complicated and as always we'll have this version on the
368:36 always we'll have this version on the course's website for reference but the
368:38 course's website for reference but the point really is that arrays not only can
368:40 point really is that arrays not only can be used as containers to store multiple
368:42 be used as containers to store multiple values three or more in this case um you
368:45 values three or more in this case um you can also even pass them around as
368:48 can also even pass them around as arguments as such all right now besides
368:52 arguments as such all right now besides that let's let's simplify for just a
368:53 that let's let's simplify for just a moment and consider now the world of
368:55 moment and consider now the world of chars if we've just got single bites
368:57 chars if we've just got single bites where uh where does this lead us and how
368:59 where uh where does this lead us and how does this get us ultimately to strings
369:00 does this get us ultimately to strings to solve problems like readability and
369:02 to solve problems like readability and cryptography and the like well here for
369:04 cryptography and the like well here for instance are three lines of code out of
369:06 instance are three lines of code out of context that simply store three chars
369:09 context that simply store three chars and you can already see where this is
369:10 and you can already see where this is going having three variables called C1
369:12 going having three variables called C1 C2 C3 is clearly going to end up being
369:14 C2 C3 is clearly going to end up being bad design because of all the silly
369:16 bad design because of all the silly redundancy here but notice I'm using
369:18 redundancy here but notice I'm using single quotes like last week because
369:19 single quotes like last week because these are single chars what does this
369:21 these are single chars what does this look like in the computer's memory well
369:24 look like in the computer's memory well it looks a little something like this if
369:25 it looks a little something like this if we clear out the old memory C1 C2 C3
369:29 we clear out the old memory C1 C2 C3 probably will end up here maybe not
369:30 probably will end up here maybe not literally in the top leftand Corner this
369:32 literally in the top leftand Corner this is just an artist rendition but C1 C2 C3
369:35 is just an artist rendition but C1 C2 C3 will probably end up like that now
369:38 will probably end up like that now what's really there it's really those
369:40 what's really there it's really those same three numbers 72 73 33 but how many
369:44 same three numbers 72 73 33 but how many bits does a bite
369:46 bits does a bite have just eight so if we were to look at
369:50 have just eight so if we were to look at the binary representation of these
369:52 the binary representation of these characters it would only be eight bits
369:54 characters it would only be eight bits each that's enough to store small
369:56 each that's enough to store small numbers like 72 73 33 we're not dealing
369:59 numbers like 72 73 33 we're not dealing with Unicode and emoji and the like but
370:01 with Unicode and emoji and the like but the point is the same you don't have to
370:03 the point is the same you don't have to use four bytes to store these numbers
370:05 use four bytes to store these numbers you can use a different data type like
370:06 you can use a different data type like chars and underneath the hood it's
370:08 chars and underneath the hood it's indeed going to use just single bytes
370:10 indeed going to use just single bytes for each but this is sort of like a this
370:13 for each but this is sort of like a this isn't really how we Implement strings
370:15 isn't really how we Implement strings right when you wanted to say hi last
370:17 right when you wanted to say hi last week or this we use double quotes and we
370:19 week or this we use double quotes and we wrote all of the things together and
370:20 wrote all of the things together and used one variable not three right when I
370:23 used one variable not three right when I typed in David I didn't have a variable
370:24 typed in David I didn't have a variable for d a v i d i had one variable called
370:27 for d a v i d i had one variable called name that stored the whole thing so in C
370:30 name that stored the whole thing so in C we keep talking about these things
370:32 we keep talking about these things called strings we'll see eventually that
370:34 called strings we'll see eventually that strings are not necessarily what they
370:36 strings are not necessarily what they seem to be but for now the key thing
370:38 seem to be but for now the key thing about strings is that they're variable
370:40 about strings is that they're variable length uh so to speak right they might
370:42 length uh so to speak right they might be three characters high or five
370:44 be three characters high or five characters David or anything uh smaller
370:46 characters David or anything uh smaller or larger so how do we go about
370:49 or larger so how do we go about implementing strings if all we have at
370:51 implementing strings if all we have at the end of the day is my memory well
370:53 the end of the day is my memory well here is an example of just creating uh
370:55 here is an example of just creating uh declaring and defining a string called s
370:57 declaring and defining a string called s s because it's just a simple string and
370:59 s because it's just a simple string and quote unquote high in double quotes what
371:02 quote unquote high in double quotes what does this look like in the computer's
371:03 does this look like in the computer's memory well let's clear it again and
371:05 memory well let's clear it again and here now because it's technically stored
371:07 here now because it's technically stored in one variable s here is how I might
371:09 in one variable s here is how I might draw it as an artist it's three bytes in
371:11 draw it as an artist it's three bytes in total H exclamation point but there's no
371:14 total H exclamation point but there's no C1 C2 C3 it's just the whole thing is uh
371:18 C1 C2 C3 it's just the whole thing is uh s but it turns out that a string fun
371:23 s but it turns out that a string fun fact is really just what underneath the
371:25 fact is really just what underneath the hood kind of leading up to this what is
371:29 hood kind of leading up to this what is a string if this is how it's laid out in
371:32 a string if this is how it's laid out in memory literally it's just an array of
371:35 memory literally it's just an array of characters and we didn't have to know
371:36 characters and we didn't have to know about arrays last week to use strings
371:38 about arrays last week to use strings this is where again the training wheels
371:39 this is where again the training wheels are starting to come off but a string is
371:42 are starting to come off but a string is just an array of characters Hi
371:44 just an array of characters Hi exclamation point for instance so
371:46 exclamation point for instance so technically an array a string called s
371:50 technically an array a string called s is really a variable called s that
371:53 is really a variable called s that allows you to get at the first character
371:54 allows you to get at the first character with s bracket zero if you want s
371:56 with s bracket zero if you want s bracket 1 s bracket2 you can literally
371:58 bracket 1 s bracket2 you can literally get individual characters just by
372:00 get individual characters just by treating S as though it's an array which
372:03 treating S as though it's an array which it really is underneath the hood in this
372:05 it really is underneath the hood in this case but there's a catch how do you know
372:09 case but there's a catch how do you know where strings end in the past when I
372:12 where strings end in the past when I drew some integers on the screen I know
372:15 drew some integers on the screen I know I claim they always take up four bytes
372:16 I claim they always take up four bytes if I had drawn a long it always takes up
372:19 if I had drawn a long it always takes up eight bytes if I drawn a a character it
372:21 eight bytes if I drawn a a character it always takes up one bite but how many
372:23 always takes up one bite but how many bites does a string take up yeah I mean
372:27 bites does a string take up yeah I mean that's kind of the right answer in this
372:28 that's kind of the right answer in this case three it would seem but if it's
372:30 case three it would seem but if it's David that's a good five characters but
372:33 David that's a good five characters but where do we put the number three where
372:36 where do we put the number three where do you put the number five right this is
372:37 do you put the number five right this is literally all that's into your computer
372:40 literally all that's into your computer this is all our building blocks in front
372:42 this is all our building blocks in front of us so how can we where does the three
372:45 of us so how can we where does the three go where does the five go well it turns
372:47 go where does the five go well it turns out you can solve this in a couple of
372:48 out you can solve this in a couple of different ways but the way humans
372:50 different ways but the way humans decided to implement strings years ago
372:52 decided to implement strings years ago is indeed an array but they added one
372:55 is indeed an array but they added one extra bite at the end of every such
372:57 extra bite at the end of every such string array just to make clear with a
373:00 string array just to make clear with a so-called Sentinel value that the string
373:03 so-called Sentinel value that the string ends here why so that if you have two
373:05 ends here why so that if you have two strings in the computer's memory like hi
373:07 strings in the computer's memory like hi and by you know where the barrier is
373:10 and by you know where the barrier is between like the exclamation point of
373:12 between like the exclamation point of one and the letter B in the next right
373:14 one and the letter B in the next right you need some kind of delimiter and so
373:16 you need some kind of delimiter and so what really is underneath the hood is
373:19 what really is underneath the hood is this when you store a string in memory
373:21 this when you store a string in memory when you type in a string as the user if
373:24 when you type in a string as the user if you type in three characters it's it's
373:26 you type in three characters it's it's going to use 3 + 1 = 4 bytes in total if
373:30 going to use 3 + 1 = 4 bytes in total if you type in David it's going to use 5 +
373:31 you type in David it's going to use 5 + 1al 6 bytes in total why because C
373:35 1al 6 bytes in total why because C automatically adds this special zero at
373:38 automatically adds this special zero at the end of the string I've drawn it with
373:40 the end of the string I've drawn it with back SL Zer because this is how you
373:42 back SL Zer because this is how you represent zero as a Char as a character
373:45 represent zero as a Char as a character but this is literally just zero as we'll
373:47 but this is literally just zero as we'll soon see so anytime there's a string in
373:49 soon see so anytime there's a string in memory it always takes up one more bite
373:52 memory it always takes up one more bite than you yourself as the programmer or
373:54 than you yourself as the programmer or human typed in in fact if we convert
373:57 human typed in in fact if we convert this again just for discussion sake to
373:58 this again just for discussion sake to those integers what's literally stored
374:00 those integers what's literally stored in the computer's memory is going to be
374:01 in the computer's memory is going to be 72 73 33 and now a zero and the computer
374:06 72 73 33 and now a zero and the computer because of c and how it was invented
374:08 because of c and how it was invented it's just smart enough to know that when
374:09 it's just smart enough to know that when you print out a string it prints out
374:12 you print out a string it prints out every character until it sees a zero and
374:14 every character until it sees a zero and then it just stops printing in
374:16 then it just stops printing in particular printf knows how this works
374:18 particular printf knows how this works and this is why print F knows when to
374:21 and this is why print F knows when to stop printing decimal numbers are not
374:22 stop printing decimal numbers are not that enlightening we'll generally write
374:24 that enlightening we'll generally write the characters like this and again back
374:26 the characters like this and again back back sl0 is just special symbology like
374:29 back sl0 is just special symbology like it's what the programmer types to make
374:30 it's what the programmer types to make clear that you're not saying hi zero
374:33 clear that you're not saying hi zero you're saying hi and then it's a special
374:35 you're saying hi and then it's a special zero specifically it is eight zero bits
374:40 zero specifically it is eight zero bits that indicate that it's the end of the
374:41 that indicate that it's the end of the string technically that back sl0 if you
374:44 string technically that back sl0 if you want to be fancy it's called null
374:46 want to be fancy it's called null NL and it turns out you've seen this
374:48 NL and it turns out you've seen this before that we didn't call it out here's
374:50 before that we didn't call it out here's that same asky chart from the past
374:52 that same asky chart from the past couple of weeks if I highlight this what
374:55 couple of weeks if I highlight this what is decimal number zero mapping to n
374:59 is decimal number zero mapping to n which is just programmer speak for the
375:01 which is just programmer speak for the special null character all zero bits
375:03 special null character all zero bits that means the string ends here this all
375:06 that means the string ends here this all happens automatically for you you do not
375:09 happens automatically for you you do not need to create these null characters or
375:11 need to create these null characters or these
375:12 these zeros any questions
375:15 zeros any questions then on this implementation thus
375:18 then on this implementation thus far any questions here no well let me do
375:22 far any questions here no well let me do this let me go back to vs code in a
375:24 this let me go back to vs code in a second and let's actually corroborate
375:26 second and let's actually corroborate this with some code let me go ahead and
375:28 this with some code let me go ahead and create a small program called high. C
375:30 create a small program called high. C and how about we do this let me include
375:32 and how about we do this let me include standard i.h let me include uh let me
375:36 standard i.h let me include uh let me type out int main void as always and now
375:38 type out int main void as always and now let me do something simple and kind of
375:40 let me do something simple and kind of bad but Char C1 equals quote unquote H
375:43 bad but Char C1 equals quote unquote H in single quotes Char C2 equals quote
375:46 in single quotes Char C2 equals quote unquote I in single quotes and lastly
375:49 unquote I in single quotes and lastly Char C3 equals exclamation point in
375:51 Char C3 equals exclamation point in single quotes and now let me just print
375:53 single quotes and now let me just print this out I can't use percent s cuz that
375:55 this out I can't use percent s cuz that is not a string that's literally three
375:57 is not a string that's literally three chars cuz that's the design decision I
375:59 chars cuz that's the design decision I made but I could do this percent C
376:02 made but I could do this percent C percent C percent C which we haven't
376:04 percent C percent C which we haven't seen before but percent s is string
376:07 seen before but percent s is string percent I is int percent C is indeed uh
376:10 percent I is int percent C is indeed uh Char so let me put a back sln at the end
376:13 Char so let me put a back sln at the end for cleanliness and now do C1 C2 C3 so
376:16 for cleanliness and now do C1 C2 C3 so this is like a Char based version of
376:19 this is like a Char based version of printing string so let me make high and
376:21 printing string so let me make high and then let me do/ high and it looks like I
376:24 then let me do/ high and it looks like I use print f with percent s but I did
376:26 use print f with percent s but I did things very manually by printing out
376:28 things very manually by printing out each individual character what's cool
376:30 each individual character what's cool now though is that once you know that
376:31 now though is that once you know that characters are just numbers and strings
376:33 characters are just numbers and strings are just characters you can kind of poke
376:35 are just characters you can kind of poke around let me change all three
376:37 around let me change all three placeholders to percent i instead and
376:41 placeholders to percent i instead and this is totally fine too let me rerun
376:44 this is totally fine too let me rerun this make
376:46 this make High um actually let me make one change
376:50 High um actually let me make one change just so we can see this let me add
376:51 just so we can see this let me add spaces just for Aesthetics sake let me
376:54 spaces just for Aesthetics sake let me do make high do SL High enter and voila
376:58 do make high do SL High enter and voila like now you can actually see the
376:59 like now you can actually see the numbers that I claimed back in week zero
377:01 numbers that I claimed back in week zero were in fact happening underneath the
377:03 were in fact happening underneath the hood well this is not how you would make
377:05 hood well this is not how you would make strings it'd be incredibly tedious to
377:06 strings it'd be incredibly tedious to have three variables for threel Words
377:09 have three variables for threel Words five variables for five letter words
377:10 five variables for five letter words we've been using of course strings since
377:12 we've been using of course strings since last week so let's do that instead uh
377:14 last week so let's do that instead uh string uh s equals quote unquote double
377:17 string uh s equals quote unquote double quotes high for this no because of these
377:21 quotes high for this no because of these training wheels I need to include the
377:23 training wheels I need to include the cs50 library but we'll come back to that
377:25 cs50 library but we'll come back to that in the coming week but for now I'm going
377:27 in the coming week but for now I'm going to go ahead and create a string s called
377:29 to go ahead and create a string s called quote unquote high and now I'm going to
377:32 quote unquote high and now I'm going to change this to be my familiar percent s
377:34 change this to be my familiar percent s and now just print out s itself this of
377:37 and now just print out s itself this of course is the same thing as last week
377:39 course is the same thing as last week high gives me the exact same thing but
377:41 high gives me the exact same thing but now we're dealing of course with strings
377:44 now we're dealing of course with strings but how can we see a little beyond that
377:47 but how can we see a little beyond that well how about this let's poke around
377:49 well how about this let's poke around further with today's Primitives even
377:51 further with today's Primitives even though s is a string I could technically
377:54 though s is a string I could technically print out its first character with
377:56 print out its first character with percent C by doing s bracket z i could
377:59 percent C by doing s bracket z i could technically print out its second
378:00 technically print out its second character with percent C by doing s
378:02 character with percent C by doing s bracket one I could print out its third
378:04 bracket one I could print out its third character with percent C and printing
378:06 character with percent C and printing out s bracket 2 so again this just
378:08 out s bracket 2 so again this just derives logically from my understanding
378:10 derives logically from my understanding now of that strings or arrays as you
378:12 now of that strings or arrays as you note let me do make let me do make high
378:16 note let me do make let me do make high SL high and no visual change but I'm
378:18 SL high and no visual change but I'm just kind of now tinkering around and in
378:20 just kind of now tinkering around and in fact if you're really curious let me do
378:22 fact if you're really curious let me do this let me change these back to I back
378:25 this let me change these back to I back to I I oops back to I and let me add a
378:28 to I I oops back to I and let me add a fourth one because if I'm really curious
378:30 fourth one because if I'm really curious now let's see it what's S braet 3 this
378:34 now let's see it what's S braet 3 this is the fourth bite and even though the
378:36 is the fourth bite and even though the string itself is hi I think we can
378:39 string itself is hi I think we can corroborate this whole null thing make
378:42 corroborate this whole null thing make high SL High enter and there it is you
378:46 high SL High enter and there it is you could have done this last week if you
378:47 could have done this last week if you really wanted to geek out on strings but
378:49 really wanted to geek out on strings but like for now it's just revealing what's
378:51 like for now it's just revealing what's going on underneath the hood questions
378:53 going on underneath the hood questions then on what these strings are
379:06 bracket uh uh why do you not need brackets good question why do I not need
379:08 brackets good question why do I not need brackets on line six uh to because uh s
379:12 brackets on line six uh to because uh s is a string we'll see in a couple of
379:14 is a string we'll see in a couple of weeks that s is essentially implemented
379:17 weeks that s is essentially implemented underneath the hood indeed as an array
379:20 underneath the hood indeed as an array but that happens automatically for you
379:22 but that happens automatically for you you can treat S as just a variable name
379:25 you can treat S as just a variable name without square brackets you will use
379:27 without square brackets you will use square brackets when you have arrays of
379:28 square brackets when you have arrays of ins or you manually create arrays of
379:31 ins or you manually create arrays of chars or doubles or Floats or anything
379:33 chars or doubles or Floats or anything else but strings are special why I mean
379:35 else but strings are special why I mean every program you write seems to use
379:37 every program you write seems to use strings text in some form we're humans
379:39 strings text in some form we're humans we like text not just numbers and and
379:41 we like text not just numbers and and such so this is just treated a little
379:43 such so this is just treated a little specially in C and many other languages
379:45 specially in C and many other languages as
379:47 as well other questions on this here no
379:51 well other questions on this here no let's add then one other string to the
379:53 let's add then one other string to the mix so instead of just saying hi why
379:55 mix so instead of just saying hi why don't we consider version of the program
379:56 don't we consider version of the program that says both high and by and I claim
379:59 that says both high and by and I claim now that that back sl0 that null
380:01 now that that back sl0 that null character is going to be ever more
380:03 character is going to be ever more important now if we've got two strings
380:05 important now if we've got two strings in memory so that c knows how to
380:06 in memory so that c knows how to distinguish one from the other so let me
380:08 distinguish one from the other so let me go ahead and just get rid of these two
380:09 go ahead and just get rid of these two lines for the moment let me recreate
380:11 lines for the moment let me recreate string s equals quote unquote double
380:14 string s equals quote unquote double quotes High Let me give myself another
380:16 quotes High Let me give myself another one and because I'm just playing around
380:17 one and because I'm just playing around I'll choose very short variable names
380:19 I'll choose very short variable names string T equals quote unquote by
380:22 string T equals quote unquote by exclamation point and then let me just
380:24 exclamation point and then let me just print them both out uh let me go ahead
380:26 print them both out uh let me go ahead and print out percent s back sln comma s
380:31 and print out percent s back sln comma s and then print F percent s uh back sln
380:35 and then print F percent s uh back sln and then T so very simple demonstration
380:38 and then T so very simple demonstration of just these two variables make high do
380:41 of just these two variables make high do SL high and of course it prints out two
380:43 SL high and of course it prints out two lines one after the other what's
380:46 lines one after the other what's actually going on underneath the hood
380:47 actually going on underneath the hood well let's go back to the computer's
380:48 well let's go back to the computer's memory High I think it's going to be I
380:50 memory High I think it's going to be I claim pretty much the same so s I'll
380:52 claim pretty much the same so s I'll claim is in the top left followed by the
380:55 claim is in the top left followed by the back sl0 and that's important now
380:57 back sl0 and that's important now because by probably is going to end up
380:59 because by probably is going to end up there and Visually it wraps just by
381:01 there and Visually it wraps just by nature of how I've drawn this grid of
381:02 nature of how I've drawn this grid of bytes but it's continuous B ye
381:05 bytes but it's continuous B ye exclamation point null AKA back0 this is
381:09 exclamation point null AKA back0 this is Now helpful to print F because now
381:11 Now helpful to print F because now printf knows where one begins and ends
381:15 printf knows where one begins and ends by way of that special null character
381:18 by way of that special null character but we can poke around now too what else
381:20 but we can poke around now too what else can I do here how about this how about I
381:23 can I do here how about this how about I go into my code here back to BS code and
381:27 go into my code here back to BS code and let me go ahead and say something like
381:31 let me go ahead and say something like well if I've got two of these uh strings
381:33 well if I've got two of these uh strings you know let's put them in an array
381:35 you know let's put them in an array let's kind of do this sort of arrays in
381:38 let's kind of do this sort of arrays in arrays sort of inception style here so
381:40 arrays sort of inception style here so string words bracket two so give me an
381:43 string words bracket two so give me an array of two strings is what I'm saying
381:45 array of two strings is what I'm saying here in code even though we've not done
381:47 here in code even though we've not done it with strings yet we only did it with
381:48 it with strings yet we only did it with ins and now let me do this the first
381:51 ins and now let me do this the first word AKA words bracket zero will equal
381:54 word AKA words bracket zero will equal as before high and now words bracket one
381:57 as before high and now words bracket one will equal quote unquote by bite so by
382:00 will equal quote unquote by bite so by and now I've done the exact same thing
382:02 and now I've done the exact same thing but again I'm just avoiding having s t
382:05 but again I'm just avoiding having s t QR and like all these different
382:07 QR and like all these different variables in my code I just now I'm
382:09 variables in my code I just now I'm treating them as one single array of
382:11 treating them as one single array of strings how do I change my code down
382:14 strings how do I change my code down here well if I want to print the first
382:15 here well if I want to print the first word I do words bracket zero and if I
382:17 word I do words bracket zero and if I want to print the second word I do words
382:18 want to print the second word I do words bracket one this is not a useful
382:21 bracket one this is not a useful exercise at the moment because I'm just
382:22 exercise at the moment because I'm just making my code more complicated but
382:24 making my code more complicated but again it allows us to post poke around
382:26 again it allows us to post poke around and see what's going on because there is
382:27 and see what's going on because there is that high and by but watch this if I
382:31 that high and by but watch this if I really want to be cool I can do this
382:34 really want to be cool I can do this let's print out percent C percent C
382:37 let's print out percent C percent C percent C back sln and then here percent
382:41 percent C back sln and then here percent C percent C percent C percent C so four
382:44 C percent C percent C percent C so four of those and now here's where things get
382:47 of those and now here's where things get interesting words is an array of strings
382:50 interesting words is an array of strings but again if I may what's a
382:52 but again if I may what's a string an array of characters so just
382:55 string an array of characters so just use the same logic if words is an array
382:57 use the same logic if words is an array of strings you get at the first string
382:59 of strings you get at the first string with words bracket zero how do you get
383:01 with words bracket zero how do you get at the first character in the first
383:03 at the first character in the first string bracket Zer words bracket Z
383:07 string bracket Zer words bracket Z bracket one and lastly words bracket Z
383:10 bracket one and lastly words bracket Z bracket two and now down here words
383:14 bracket two and now down here words bracket one but the first character is
383:16 bracket one but the first character is there words bracket one the second
383:19 there words bracket one the second character is here words bracket one the
383:21 character is here words bracket one the third character is here whoops third
383:23 third character is here whoops third character is here and words bracket one
383:25 character is here and words bracket one one the fourth character is here like
383:27 one the fourth character is here like this is not how people program this is
383:29 this is not how people program this is only for demonstration sake my God it's
383:31 only for demonstration sake my God it's so tedious and verbose already but if I
383:33 so tedious and verbose already but if I make high now do SL high now I'm like
383:37 make high now do SL high now I'm like manually Reinventing percent s if I
383:40 manually Reinventing percent s if I forgot it existed using percent C alone
383:42 forgot it existed using percent C alone but you can indeed manipulate arrays in
383:45 but you can indeed manipulate arrays in this way but because strings are arrays
383:47 this way but because strings are arrays of characters you can manipulate strings
383:50 of characters you can manipulate strings uh in this way too any question now on
383:53 uh in this way too any question now on this syntax
383:57 this syntax any questions here no no all right well
384:00 any questions here no no all right well let's go ahead and propose that we solve
384:02 let's go ahead and propose that we solve a couple of other problems we might not
384:04 a couple of other problems we might not have as before but first a quick visual
384:06 have as before but first a quick visual of what's been going on underneath the
384:08 of what's been going on underneath the hood here if here again is where we left
384:10 hood here if here again is where we left off on the screen high and by uh back to
384:12 off on the screen high and by uh back to back here is really how I just treated
384:15 back here is really how I just treated these things s bracket 0123 and then t0
384:19 these things s bracket 0123 and then t0 1 2 3 4 but really once I put them in an
384:22 1 2 3 4 but really once I put them in an array the picture becomes this words Z
384:25 array the picture becomes this words Z is the whole High words bracket one is
384:27 is the whole High words bracket one is the whole by but if I really get into
384:29 the whole by but if I really get into the weeds and start indexing into
384:31 the weeds and start indexing into individual characters in those strings
384:33 individual characters in those strings all I'm using is new syntax in order to
384:37 all I'm using is new syntax in order to represent these same values here
384:40 represent these same values here questions then on these
384:43 questions then on these representations before we Forge
384:47 representations before we Forge ahead no
384:55 yeah does the new line character say that one
385:00 more ah really good question does the new line character take up any space uh
385:02 new line character take up any space uh it does when so far as printf is
385:04 it does when so far as printf is concerned but I'm not storing the back
385:06 concerned but I'm not storing the back slash n in my strings printf is being
385:09 slash n in my strings printf is being manually handed that thing
385:12 manually handed that thing instead all right so let's go ahead then
385:14 instead all right so let's go ahead then and consider how we might solve some
385:16 and consider how we might solve some problems that have Arisen now with these
385:18 problems that have Arisen now with these strings as follows here suppose I let's
385:22 strings as follows here suppose I let's do this let me go back to VSS code here
385:24 do this let me go back to VSS code here and let me go ahead and open up a new
385:26 and let me go ahead and open up a new file called how about uh length uh. C
385:29 file called how about uh length uh. C and let's consider for a moment how I
385:31 and let's consider for a moment how I might actually figure out what the
385:32 might actually figure out what the length of a string is which is distinct
385:34 length of a string is which is distinct from the length of an array I claimed
385:36 from the length of an array I claimed earlier you cannot figure out
385:37 earlier you cannot figure out dynamically what the length of an array
385:39 dynamically what the length of an array is but I can figure out the length of a
385:41 is but I can figure out the length of a string specifically because of this
385:44 string specifically because of this implementation detail of that null
385:46 implementation detail of that null character so let me go ahead and do this
385:48 character so let me go ahead and do this let me include cs50.h in this second
385:50 let me include cs50.h in this second program here let me include standard i.h
385:54 program here let me include standard i.h as before and let me do this int main
385:57 as before and let me do this int main void and in the first thing I'll do is
385:59 void and in the first thing I'll do is just get a string from the user I'll ask
386:01 just get a string from the user I'll ask the user as always for their name so
386:03 the user as always for their name so I'll call get string and say what's your
386:05 I'll call get string and say what's your name question mark as always and then
386:08 name question mark as always and then down here if I want to figure out the
386:10 down here if I want to figure out the length of this string and print the name
386:12 length of this string and print the name the print the length out on the screen
386:15 the print the length out on the screen well I can kind of do this similar in
386:17 well I can kind of do this similar in spirit to the average where I'm
386:18 spirit to the average where I'm accumulating something let me go ahead
386:20 accumulating something let me go ahead and initialize n to zero let me give
386:23 and initialize n to zero let me give myself huh it's not not a for Loop
386:26 myself huh it's not not a for Loop because I don't have a I don't know in
386:27 because I don't have a I don't know in advance how long it is but what if I do
386:29 advance how long it is but what if I do this
386:30 this while the value at name bracket n does
386:36 while the value at name bracket n does not equal single quote back
386:39 not equal single quote back sl0 crazy syntax at the moment but it's
386:42 sl0 crazy syntax at the moment but it's just the culmination of these various
386:43 just the culmination of these various building blocks let me just finish the
386:46 building blocks let me just finish the thought here
386:47 thought here n++ and then down here let's just print
386:50 n++ and then down here let's just print out with print F and percent I that
386:53 out with print F and percent I that value of n
386:55 value of n so I claim this is going to show me the
386:57 so I claim this is going to show me the length of any string I type in whether
387:00 length of any string I type in whether it's high or by or David or anything
387:02 it's high or by or David or anything else I initialize a variable to zero and
387:04 else I initialize a variable to zero and that's good because that's where you
387:05 that's good because that's where you start counting in general while name
387:08 start counting in general while name bracket 0 does not equal back0 what is
387:11 bracket 0 does not equal back0 what is this saying well if name is the string
387:12 this saying well if name is the string the user typed in and name is just an
387:14 the user typed in and name is just an array as you noted then name bracket
387:17 array as you noted then name bracket zero is going to be the first character
387:19 zero is going to be the first character and I'm asking the question well does
387:20 and I'm asking the question well does the first character not equal back0 and
387:23 the first character not equal back0 and if I type in David D is it's not so I
387:26 if I type in David D is it's not so I keep going and I add one to n then I'm
387:28 keep going and I add one to n then I'm going to check name bracket one well if
387:31 going to check name bracket one well if I typed in David name bracket one is
387:32 I typed in David name bracket one is going to be a a does not equal back0 and
387:35 going to be a a does not equal back0 and so it's going to go again and again and
387:38 so it's going to go again and again and again but five steps in total later it's
387:40 again but five steps in total later it's going to get to the bite after David
387:43 going to get to the bite after David realize wait a minute that is a back
387:45 realize wait a minute that is a back slash n the Loop finishes and I print
387:48 slash n the Loop finishes and I print out the total length arrays in general
387:50 out the total length arrays in general do not have this null character however
387:53 do not have this null character however strings do again strings are special
387:56 strings do again strings are special versus all of the other data types we've
387:58 versus all of the other data types we've talked about thus far but how could I
388:00 talked about thus far but how could I for instance uh do this differently well
388:03 for instance uh do this differently well let's actually Factor this out as a
388:04 let's actually Factor this out as a function as I've I've commonly done but
388:07 function as I've I've commonly done but rather than implement it myself you know
388:10 rather than implement it myself you know what it turns out what's nice about
388:12 what it turns out what's nice about strings being so common there are many
388:14 strings being so common there are many other people who have solved these
388:16 other people who have solved these problems before and in fact there's a
388:17 problems before and in fact there's a whole string library in C it is used by
388:21 whole string library in C it is used by way of a header file called string.h and
388:24 way of a header file called string.h and what string H is is a library of string
388:27 what string H is is a library of string related functions in fact you can see in
388:29 related functions in fact you can see in cs50's manual pages for C uh the
388:32 cs50's manual pages for C uh the string.h functions at least those that
388:34 string.h functions at least those that we recommend as most useful and in
388:36 we recommend as most useful and in particular if you poke around there
388:37 particular if you poke around there you'll see that there's a function
388:38 you'll see that there's a function called Sterling means string length it
388:42 called Sterling means string length it was named very succinctly just because
388:43 was named very succinctly just because it's a little easier to type than string
388:45 it's a little easier to type than string length but Sterling tells you the length
388:47 length but Sterling tells you the length of a string so how might I use this in
388:49 of a string so how might I use this in my code here well it turns out I can
388:51 my code here well it turns out I can simplify this quite a bit let me get rid
388:54 simplify this quite a bit let me get rid of my Loop get rid of my uh my counting
388:57 of my Loop get rid of my uh my counting manually and do something like this int
389:00 manually and do something like this int n equals Sterling of the human's name
389:04 n equals Sterling of the human's name name and now I'll just use print f as
389:07 name and now I'll just use print f as before with percent I back sln and
389:09 before with percent I back sln and output the value of n but but there's a
389:12 output the value of n but but there's a bug at the moment what have I forgotten
389:15 bug at the moment what have I forgotten to
389:17 to do yeah I have to include the header
389:19 do yeah I have to include the header file at the top of the screen so let me
389:22 file at the top of the screen so let me at the top of the code so let me also
389:23 at the top of the code so let me also include string.h at the top of my file
389:27 include string.h at the top of my file so that c knows that in fact sterl
389:29 so that c knows that in fact sterl exists let me go ahead and make length
389:32 exists let me go ahead and make length as
389:33 as before length uh or actually really for
389:36 before length uh or actually really for the first time what's your name David
389:39 the first time what's your name David and hopefully I'm going to see in fact
389:41 and hopefully I'm going to see in fact five by contrast if I run it again and
389:43 five by contrast if I run it again and type in high exclamation point now I see
389:46 type in high exclamation point now I see three so Sterling is just one of the
389:48 three so Sterling is just one of the functions in that library and there's so
389:50 functions in that library and there's so many more in fact yet another library
389:52 many more in fact yet another library that might be useful moving forward is
389:53 that might be useful moving forward is this one C
389:55 this one C which relates to C data types and lots
389:58 which relates to C data types and lots of functions therein that can be useful
390:00 of functions therein that can be useful for instance if you review its
390:01 for instance if you review its documentation in the manual pages online
390:04 documentation in the manual pages online you'll see that there are functions via
390:06 you'll see that there are functions via which we can solve problems like this
390:09 which we can solve problems like this let me go ahead and propose here let me
390:12 let me go ahead and propose here let me see let's do um example here
390:18 see let's do um example here involving how about uh checking if
390:20 involving how about uh checking if something is uppercase or lowercase and
390:23 something is uppercase or lowercase and converting it uh to uppercase only let
390:26 converting it uh to uppercase only let me go back to vs code and code a program
390:28 me go back to vs code and code a program called uppercase Doc in this file I'm
390:31 called uppercase Doc in this file I'm going to start by including now as
390:33 going to start by including now as always
390:34 always cs50.h I'm going to include standard i.h
390:37 cs50.h I'm going to include standard i.h and I'm going to add one other to the
390:40 and I'm going to add one other to the mix which is
390:42 mix which is string.h now2 so I can access the length
390:44 string.h now2 so I can access the length of things as needed int main void comes
390:47 of things as needed int main void comes next and then within my main function
390:50 next and then within my main function I'm going to go ahead and declare a
390:50 I'm going to go ahead and declare a string called s I'm going to call get
390:52 string called s I'm going to call get string as before and I'm going to go
390:54 string as before and I'm going to go head and just ask the user for a string
390:57 head and just ask the user for a string called before I want to do it before and
390:58 called before I want to do it before and after whatever the user types in is
391:00 after whatever the user types in is before but I want to force everything to
391:02 before but I want to force everything to uppercase thereafter let me now in this
391:06 uppercase thereafter let me now in this Loop here do this let me print F quote
391:09 Loop here do this let me print F quote unquote after just so we can see this on
391:12 unquote after just so we can see this on the screen and let me do four in I gets
391:16 the screen and let me do four in I gets zero I is less than stir Lang of s I ++
391:22 zero I is less than stir Lang of s I ++ what am I about to do I'm about to
391:23 what am I about to do I'm about to iterate over every every character in
391:25 iterate over every every character in the string from left to right from zero
391:27 the string from left to right from zero on up two but not through the length of
391:30 on up two but not through the length of s and how do I check if something is
391:32 s and how do I check if something is lowercase so that I can actually force
391:35 lowercase so that I can actually force it to uppercase well it turns out I
391:38 it to uppercase well it turns out I could do this literally if the character
391:41 could do this literally if the character in s at location I is greater than or
391:45 in s at location I is greater than or equal to Capital a Ampersand Ampersand
391:48 equal to Capital a Ampersand Ampersand which means and instead of or which we
391:51 which means and instead of or which we saw in the past s braet i is less than
391:55 saw in the past s braet i is less than or equal to little Z that means
391:57 or equal to little Z that means logically in English that this is indeed
392:00 logically in English that this is indeed a lowercase how do I now convert it to
392:03 a lowercase how do I now convert it to uppercase this character well I could
392:05 uppercase this character well I could just literally print out the same
392:07 just literally print out the same character but that would not be the
392:09 character but that would not be the answer here because that's not changing
392:10 answer here because that's not changing the value but what could I do instead
392:14 the value but what could I do instead well let me actually pull up here real
392:16 well let me actually pull up here real fast the asky chart as before and let's
392:20 fast the asky chart as before and let's see if we can't glean some insight if I
392:23 see if we can't glean some insight if I pull up the same ask each chart and
392:25 pull up the same ask each chart and suppose the human has typed in a
392:26 suppose the human has typed in a lowercase a that's
392:29 lowercase a that's 97 what letter I want to convert it to
392:32 97 what letter I want to convert it to uppercase a so what number do I want to
392:34 uppercase a so what number do I want to convert the 97 to per week zero so 65 we
392:39 convert the 97 to per week zero so 65 we keep coming back to that one what if the
392:41 keep coming back to that one what if the user types in lowercase B I want to
392:43 user types in lowercase B I want to change the 98 value to 66 and so forth
392:47 change the 98 value to 66 and so forth and any quick math how far part of those
392:49 and any quick math how far part of those so it's always 32 like uppercase to
392:52 so it's always 32 like uppercase to lower case is always wonderfully good
392:54 lower case is always wonderfully good design 32 away one from the other so
392:58 design 32 away one from the other so what does this mean well I think we saw
392:59 what does this mean well I think we saw earlier that underneath the hood a Char
393:01 earlier that underneath the hood a Char is just a number you can certainly do
393:03 is just a number you can certainly do arithmetic on it and here again if you
393:05 arithmetic on it and here again if you understand these lower level Primitives
393:06 understand these lower level Primitives what if I do this whatever s bracket I
393:09 what if I do this whatever s bracket I is if I know on line 13 that it's up
393:12 is if I know on line 13 that it's up that it's lowercase do I want to add or
393:14 that it's lowercase do I want to add or subtract
393:16 subtract 32 so I want to subtract because I want
393:18 32 so I want to subtract because I want to go from like 97 to 65 or 98 to 66 so
393:24 to go from like 97 to 65 or 98 to 66 so indeed if you do some quick math that
393:25 indeed if you do some quick math that gives you 32 so it suffices to just
393:27 gives you 32 so it suffices to just treat chars as numbers subtract the 32
393:30 treat chars as numbers subtract the 32 and printing it with percent C I think
393:33 and printing it with percent C I think will just convert lowercase to uppercase
393:36 will just convert lowercase to uppercase if you now fast forward to like the real
393:37 if you now fast forward to like the real world Microsoft Word or Google Docs if
393:39 world Microsoft Word or Google Docs if you've ever chosen the menu option that
393:41 you've ever chosen the menu option that forces things to uppercase or lowercase
393:43 forces things to uppercase or lowercase on occasion literally that's what
393:44 on occasion literally that's what Microsoft and Google have done they
393:46 Microsoft and Google have done they iterate over every character in the
393:47 iterate over every character in the document check if it's lowercase and if
393:49 document check if it's lowercase and if so they subtract 32 from it and show you
393:52 so they subtract 32 from it and show you the new value what if though it is not a
393:55 the new value what if though it is not a lowercase letter I think I can keep it
393:57 lowercase letter I think I can keep it easy and just print out the current
393:59 easy and just print out the current letter unchanged if my goal is to Simply
394:02 letter unchanged if my goal is to Simply Force things to all uppercase and that
394:04 Force things to all uppercase and that letter then would be S bracket I so let
394:06 letter then would be S bracket I so let me go ahead now and make uppercase
394:09 me go ahead now and make uppercase hopefully no errors do/ uppercase and
394:12 hopefully no errors do/ uppercase and I'll now now type in David with an
394:14 I'll now now type in David with an uppercase D but lowercase everything
394:16 uppercase D but lowercase everything else but now the after version is David
394:19 else but now the after version is David an aesthetic bug notice here I forgot to
394:22 an aesthetic bug notice here I forgot to include just for prettiness sake about
394:24 include just for prettiness sake about back sln at the end no problem I'll add
394:26 back sln at the end no problem I'll add that let me fix my mistake make
394:29 that let me fix my mistake make uppercase SL uppercase enter daid enter
394:33 uppercase SL uppercase enter daid enter and voila and I I deliberately added
394:35 and voila and I I deliberately added another space after the after just so
394:37 another space after the after just so they would line up pretty even though
394:38 they would line up pretty even though before and after have different numbers
394:40 before and after have different numbers of letters questions then on this
394:43 of letters questions then on this implementation of forcing something to
394:45 implementation of forcing something to uppercase which in and of itself is not
394:47 uppercase which in and of itself is not all that enlightening but is
394:48 all that enlightening but is representative now of how you can
394:50 representative now of how you can leverage these low-level
394:52 leverage these low-level Primitives question
394:55 Primitives question now all right well this honestly is
394:58 now all right well this honestly is tedious my God like does Microsoft
394:59 tedious my God like does Microsoft Google everyone you have to literally
395:01 Google everyone you have to literally write out this code just to do something
395:02 write out this code just to do something simple well no that's again why we have
395:05 simple well no that's again why we have things like libraries and increasingly
395:07 things like libraries and increasingly now for problem sets projects and Beyond
395:09 now for problem sets projects and Beyond well you just use libraries more often
395:11 well you just use libraries more often off the shelf so as to solve problems
395:12 off the shelf so as to solve problems that surely other people have had before
395:15 that surely other people have had before you so how can I now use this uh Library
395:18 you so how can I now use this uh Library ctype.h well let me go back into my code
395:21 ctype.h well let me go back into my code let me include this among my header
395:23 let me include this among my header files here I tend just so I can skim
395:25 files here I tend just so I can skim things easily I tend to alphabetize my
395:27 things easily I tend to alphabetize my headers but that's not strictly
395:28 headers but that's not strictly necessary but it allows me at a glance
395:30 necessary but it allows me at a glance to realize did I or did I not include
395:32 to realize did I or did I not include something I need now let me go ahead and
395:34 something I need now let me go ahead and do this it turns out if you read the
395:37 do this it turns out if you read the documentation for the ctype library
395:40 documentation for the ctype library there is a function wonderfully called
395:42 there is a function wonderfully called if is lower that takes in a character as
395:46 if is lower that takes in a character as its argument essentially so s bracket I
395:48 its argument essentially so s bracket I and if that returns true a Boolean value
395:51 and if that returns true a Boolean value if you will well I'm going to force it
395:52 if you will well I'm going to force it to lowercase but I don't have to do this
395:55 to lowercase but I don't have to do this this math anymore turns out in the ctype
395:57 this math anymore turns out in the ctype library there's also a function called
395:59 library there's also a function called two upper that takes a character as
396:01 two upper that takes a character as input like s bracket I and it just does
396:03 input like s bracket I and it just does the math for you so that you can
396:05 the math for you so that you can abstract away the 32 thing and just know
396:07 abstract away the 32 thing and just know that someone else has solved that
396:09 that someone else has solved that problem for you otherwise I can leave my
396:11 problem for you otherwise I can leave my code unchanged down below because I'm
396:13 code unchanged down below because I'm not changing anything else so if I do
396:15 not changing anything else so if I do make uppercase now and then do SL
396:18 make uppercase now and then do SL uppercase daavid with just a capital D
396:21 uppercase daavid with just a capital D and now it still works but if you read
396:24 and now it still works but if you read the documentation further it turns out
396:26 the documentation further it turns out that two upper is smart if you pass in a
396:28 that two upper is smart if you pass in a character to two upper that's lowercase
396:30 character to two upper that's lowercase it obviously converts its uppercase by
396:31 it obviously converts its uppercase by doing that math but if you pass in a
396:33 doing that math but if you pass in a character to to Upper that's already
396:36 character to to Upper that's already uppercase the documentation you would
396:38 uppercase the documentation you would see tells you that it leaves it
396:40 see tells you that it leaves it unchanged so I can tighten all of this
396:43 unchanged so I can tighten all of this up I can get rid of the whole else I can
396:46 up I can get rid of the whole else I can get rid of the whole if and arguably now
396:48 get rid of the whole if and arguably now Implement a program that's just as
396:50 Implement a program that's just as correct but better designed why fewer
396:54 correct but better designed why fewer lines of code easier to read Le lower
396:56 lines of code easier to read Le lower probability of mistakes assuming the
396:58 probability of mistakes assuming the library is correct it just makes it
397:00 library is correct it just makes it easier and faster for me now to write
397:02 easier and faster for me now to write code so if I now do one last time make
397:04 code so if I now do one last time make uppercase enter uppercase and type in my
397:08 uppercase enter uppercase and type in my name still working but now notice we've
397:11 name still working but now notice we've whittel this down to far fewer lines of
397:13 whittel this down to far fewer lines of code albeit using now this additional
397:16 code albeit using now this additional Library questions then on how we did
397:21 Library questions then on how we did this no well even even though this code
397:24 this no well even even though this code I dare say is correct it's not
397:26 I dare say is correct it's not necessarily welld designed just yet in
397:29 necessarily welld designed just yet in fact there's one line of code one
397:31 fact there's one line of code one function call in this current
397:33 function call in this current implementation that's more inefficient
397:36 implementation that's more inefficient than it needs to be and allow me to draw
397:38 than it needs to be and allow me to draw our attention to this here line 10
397:41 our attention to this here line 10 wherein we're calling Sterling but we're
397:44 wherein we're calling Sterling but we're calling it inside of this for Loop
397:46 calling it inside of this for Loop specifically inside of the condition and
397:49 specifically inside of the condition and why might that not necessarily be the
397:51 why might that not necessarily be the best
397:52 best idea well is the length of the string s
397:55 idea well is the length of the string s changing ever I mean certainly not
397:57 changing ever I mean certainly not within the span of this Loop and so here
397:59 within the span of this Loop and so here we are within our for loop on line 10 11
398:01 we are within our for loop on line 10 11 12 and 13 asking on every iteration that
398:04 12 and 13 asking on every iteration that same question what's the length of s
398:06 same question what's the length of s what's the length of s what's the length
398:07 what's the length of s what's the length of s and in turn we're calling Sterling
398:10 of s and in turn we're calling Sterling every time even though we're getting
398:11 every time even though we're getting back the same answer so I dare say a
398:13 back the same answer so I dare say a better solution here would be to maybe
398:15 better solution here would be to maybe figure out the length of s earlier on in
398:17 figure out the length of s earlier on in my code and maybe declare a variable or
398:19 my code and maybe declare a variable or perhaps do something that's
398:20 perhaps do something that's syntactically a little more elegant and
398:22 syntactically a little more elegant and in fact a very common design in Loop
398:24 in fact a very common design in Loop like this would be to declare not just
398:26 like this would be to declare not just one variable like I but to actually
398:28 one variable like I but to actually declare a second variable called n for
398:31 declare a second variable called n for instance where n is just some number Set
398:33 instance where n is just some number Set n equal to the length of s but
398:36 n equal to the length of s but thereafter inside of this condition
398:38 thereafter inside of this condition instead of calling Sterling of s again
398:41 instead of calling Sterling of s again and again and again what might I now do
398:44 and again and again what might I now do I could instead just compare I against n
398:47 I could instead just compare I against n itself because n now will only be
398:49 itself because n now will only be calculated once when it's initialized
398:50 calculated once when it's initialized just as I is initialized to zero and
398:52 just as I is initialized to zero and thereafter we're going to be compar
398:54 thereafter we're going to be compar comparing I which is changing against n
398:56 comparing I which is changing against n which will not be so it's going to be
398:57 which will not be so it's going to be marginally more efficient by Design now
399:00 marginally more efficient by Design now with that said a good compiler could
399:02 with that said a good compiler could also recognize that there was this
399:04 also recognize that there was this optimization possibility and maybe do it
399:06 optimization possibility and maybe do it for us but for now best to get into the
399:08 for us but for now best to get into the Habit best to develop the muscle memory
399:10 Habit best to develop the muscle memory for making those better design decisions
399:12 for making those better design decisions yourselves questions then on how we did
399:17 yourselves questions then on how we did this no all right a few final building
399:21 this no all right a few final building blocks for the day so we started by
399:24 blocks for the day so we started by talking about those command line
399:25 talking about those command line arguments that clang uses whereby
399:28 arguments that clang uses whereby anything after the command that you type
399:30 anything after the command that you type at a prompt be it make or clang or even
399:34 at a prompt be it make or clang or even CD in Linux any word thereafter or
399:37 CD in Linux any word thereafter or something cryptic like- O is a
399:39 something cryptic like- O is a commandline argument it's an input to
399:42 commandline argument it's an input to the command it's different from a
399:43 the command it's different from a function argument because a function
399:44 function argument because a function argument of course is an input to a
399:46 argument of course is an input to a function but it's the same idea it's
399:48 function but it's the same idea it's just different syntax after the dollar
399:49 just different syntax after the dollar sign at the prompt well it turns out
399:52 sign at the prompt well it turns out that command line arguments are
399:53 that command line arguments are something you can now use in your own
399:56 something you can now use in your own programs by
399:58 programs by accessing uh words after the prompt and
400:01 accessing uh words after the prompt and let me propose that we invent we invent
400:04 let me propose that we invent we invent this as follows let me propose that we
400:06 this as follows let me propose that we switch back to vs code here and I'll
400:09 switch back to vs code here and I'll open a new file here called greet doc so
400:13 open a new file here called greet doc so in greet Doc is going to be a program
400:15 in greet Doc is going to be a program that very simply greets the user had we
400:17 that very simply greets the user had we written this last week we would have
400:18 written this last week we would have done this include
400:21 done this include cs50.h and then include uh standard
400:25 cs50.h and then include uh standard i.h and then int main void and then we
400:28 i.h and then int main void and then we might do something simple like string
400:30 might do something simple like string name equals get string quote unquote
400:33 name equals get string quote unquote what's your name question mark and then
400:36 what's your name question mark and then we would have printed out as always
400:38 we would have printed out as always hello comma percent s and then plugging
400:40 hello comma percent s and then plugging in that name so this is the same program
400:43 in that name so this is the same program we've implemented many times just to
400:45 we've implemented many times just to make sure it works although nope that's
400:47 make sure it works although nope that's not quite the same program semicolon in
400:49 not quite the same program semicolon in the wrong place this now is the same
400:52 the wrong place this now is the same program so make greet /g greet and I'll
400:55 program so make greet /g greet and I'll type in my own name hello David so we're
400:57 type in my own name hello David so we're back there now what's arguably a little
401:00 back there now what's arguably a little Annoying about this program if I type in
401:02 Annoying about this program if I type in something else like Carter enter you
401:04 something else like Carter enter you know I have to run the program wait for
401:06 know I have to run the program wait for the prompt type in my name hit enter and
401:09 the prompt type in my name hit enter and that's fine but imagine if every program
401:11 that's fine but imagine if every program worked like this like make suppose you
401:13 worked like this like make suppose you could only type make then you wait for a
401:15 could only type make then you wait for a prompt then you type the name of the
401:16 prompt then you type the name of the program you want to make then you hit
401:17 program you want to make then you hit enter or worse in Linux when you have to
401:20 enter or worse in Linux when you have to change directories as you might have for
401:22 change directories as you might have for problem set one what if you had to type
401:24 problem set one what if you had to type CD enter now type the name of the folder
401:26 CD enter now type the name of the folder you want to change into enter I mean it
401:28 you want to change into enter I mean it just slows life down and so it just gets
401:30 just slows life down and so it just gets annoying quickly so commandline
401:32 annoying quickly so commandline arguments just let you express your
401:33 arguments just let you express your whole thought all at once so how can I
401:36 whole thought all at once so how can I do this well if I want to express the
401:39 do this well if I want to express the notion of command line arguments in my
401:41 notion of command line arguments in my code I could do something like this I
401:45 code I could do something like this I could for the very first time go up and
401:48 could for the very first time go up and get rid of this void which as of today
401:51 get rid of this void which as of today means this program takes no command line
401:53 means this program takes no command line arguments and I can change it to exactly
401:56 arguments and I can change it to exactly this int ARG C string argv with brackets
402:03 this int ARG C string argv with brackets now it's cryptic admittedly and let me
402:05 now it's cryptic admittedly and let me zoom in but I think we can perhaps infer
402:07 zoom in but I think we can perhaps infer now what's going on if main Now does not
402:11 now what's going on if main Now does not have void as its input which means it
402:12 have void as its input which means it takes no arguments surely the spoiler
402:15 takes no arguments surely the spoiler here is that now main will take command
402:17 here is that now main will take command line argument somehow any guesses as to
402:20 line argument somehow any guesses as to what argv is or will be
402:25 what argv is or will be what might this
402:27 what might this represent it's an array of strings right
402:30 represent it's an array of strings right by way of the syntax
402:36 yeah exactly it will be all of the characters or really all of the words
402:38 characters or really all of the words that you type at the prompt argc as an
402:40 that you type at the prompt argc as an INT any
402:43 INT any guess
402:45 guess argument count is what it generally
402:47 argument count is what it generally stands for though technically you could
402:48 stands for though technically you could call these things any anything but this
402:50 call these things any anything but this is the convention because I claimed
402:52 is the convention because I claimed earlier that arrays don't keep track of
402:54 earlier that arrays don't keep track of their own length if you want to know how
402:56 their own length if you want to know how many words the human typed at the prompt
402:58 many words the human typed at the prompt after your program's name you have to be
403:00 after your program's name you have to be told not just the array of the words but
403:03 told not just the array of the words but the length of that array the strings you
403:06 the length of that array the strings you can figure out the length of using
403:07 can figure out the length of using Sterling but you can't figure out the
403:09 Sterling but you can't figure out the length of the array of strings the
403:12 length of the array of strings the collection of words that the human typed
403:14 collection of words that the human typed in so how can I now use this well let me
403:17 in so how can I now use this well let me go ahead and do this let me go ahead and
403:19 go ahead and do this let me go ahead and change this program now just to be print
403:22 change this program now just to be print F quote unquote
403:24 F quote unquote hello comma uh percent s back sln then
403:29 hello comma uh percent s back sln then Arvy bracket 1 so this is not the best
403:32 Arvy bracket 1 so this is not the best version of my code yet but it's my first
403:34 version of my code yet but it's my first make greet and now let me dog greet
403:39 make greet and now let me dog greet David all at once enter hello David now
403:43 David all at once enter hello David now let me run it again dogre Carter enter
403:46 let me run it again dogre Carter enter hello Carter you know it's a marginal
403:48 hello Carter you know it's a marginal Improvement but I don't have to wait for
403:50 Improvement but I don't have to wait for get string to prompt me to hit enter
403:52 get string to prompt me to hit enter it's just speeding things up you know is
403:53 it's just speeding things up you know is fast one less command to type in but I
403:57 fast one less command to type in but I deliberately did bracket one but where
403:59 deliberately did bracket one but where what's the beginning of arv it would be
404:02 what's the beginning of arv it would be bracket zero well what's that this is
404:06 bracket zero well what's that this is sometimes useful though for now it's not
404:08 sometimes useful though for now it's not suppose I recompile my code and run this
404:11 suppose I recompile my code and run this program now greet David anyone want to
404:14 program now greet David anyone want to guess what's in argv
404:20 Z say again greet enter hello. SLG greet so if
404:25 again greet enter hello. SLG greet so if you want sort of inception style your
404:26 you want sort of inception style your program to figure out what its own name
404:28 program to figure out what its own name is or at least how it was executed at
404:30 is or at least how it was executed at the command line at the terminal you can
404:33 the command line at the terminal you can look at ARG v0 in general probably not
404:35 look at ARG v0 in general probably not that useful probably better to start
404:37 that useful probably better to start looking at bracket one which was the
404:39 looking at bracket one which was the first word after the program name and if
404:41 first word after the program name and if there were more I could do this how
404:43 there were more I could do this how about RV bracket 2 let me add in a
404:46 about RV bracket 2 let me add in a second percent s let me recompile greet
404:49 second percent s let me recompile greet let me do greet David maen enter and
404:53 let me do greet David maen enter and that two now works taking in two words
404:56 that two now works taking in two words at the prompt if I really want to be
404:57 at the prompt if I really want to be smart at this now I could do something
404:59 smart at this now I could do something like this though how about if the count
405:02 like this though how about if the count of arguments AKA AR C equals equals 2
405:06 of arguments AKA AR C equals equals 2 then assume that the human typed in only
405:08 then assume that the human typed in only their first name and do print F uh hello
405:12 their first name and do print F uh hello comma percent s back sln and then uh
405:16 comma percent s back sln and then uh argv bracket 1 else if the human did not
405:20 argv bracket 1 else if the human did not provide exactly two arguments the name
405:22 provide exactly two arguments the name of the program and their own name let's
405:24 of the program and their own name let's just print out a default value L they
405:26 just print out a default value L they forgot their name or they typed in two
405:28 forgot their name or they typed in two names or three names let's just do uh
405:31 names or three names let's just do uh hello comma world as a default and we'll
405:33 hello comma world as a default and we'll just ignore what the human typed in if I
405:35 just ignore what the human typed in if I recompile this make greet I can dog
405:38 recompile this make greet I can dog greet and David again enter oops uh
405:42 greet and David again enter oops uh sorry what am I missing yeah so newbie
405:46 sorry what am I missing yeah so newbie mistake else all right make greet again
405:50 mistake else all right make greet again dog greet David enter there's my hello
405:53 dog greet David enter there's my hello David
405:54 David but if I omit my name I just get the
405:56 but if I omit my name I just get the generic like a default value and if I
405:58 generic like a default value and if I get a little curious and I type in both
406:00 get a little curious and I type in both names then I get ignored two why because
406:02 names then I get ignored two why because I just haven't built in support for arxy
406:03 I just haven't built in support for arxy of three I could do anything I want but
406:06 of three I could do anything I want but now we have access to these kinds of
406:08 now we have access to these kinds of building blocks all right what else
406:11 building blocks all right what else might I do here well it turns out there
406:13 might I do here well it turns out there might be some final features for us to
406:16 might be some final features for us to now execute um notice though that in C
406:19 now execute um notice though that in C despite what you might see in books or
406:20 despite what you might see in books or online tutorials nowadays the two
406:23 online tutorials nowadays the two official formats for defining a main
406:25 official formats for defining a main function are either this which we've
406:27 function are either this which we've been using now for Two Plus weeks or now
406:30 been using now for Two Plus weeks or now this whereby you change the void to int
406:33 this whereby you change the void to int argc and then for now string argv and
406:36 argc and then for now string argv and then empty brackets and we'll see that
406:38 then empty brackets and we'll see that this two is an a simplification some
406:40 this two is an a simplification some training wheels if you will but for now
406:42 training wheels if you will but for now those are the two forms even though you
406:43 those are the two forms even though you will see in online tutorials and even
406:45 will see in online tutorials and even books some people use Maine in different
406:47 books some people use Maine in different ways these are the two now to keep in
406:49 ways these are the two now to keep in mind and I'll note that these command
406:51 mind and I'll note that these command line arguments are kind of all over the
406:52 line arguments are kind of all over the place didn't probably expect to see this
406:54 place didn't probably expect to see this word on the screen here and what does it
406:55 word on the screen here and what does it mean well it turns out that for decades
406:57 mean well it turns out that for decades there's actually this program that comes
406:58 there's actually this program that comes with Linux systems in particular called
407:01 with Linux systems in particular called coway why probably because someone had
407:03 coway why probably because someone had too much free time once and decided to
407:05 too much free time once and decided to write a program that creates asy art out
407:07 write a program that creates asy art out of a cow saying something textually on
407:10 of a cow saying something textually on the screen but you use coway Just For
407:13 the screen but you use coway Just For Fun by way of command line arguments so
407:15 Fun by way of command line arguments so for instance let me propose that uh I go
407:18 for instance let me propose that uh I go back to vs code here not because I want
407:22 back to vs code here not because I want to write any code but I just want to use
407:23 to write any code but I just want to use my terminal window and let me uh
407:25 my terminal window and let me uh maximize my terminal window here and let
407:27 maximize my terminal window here and let me go ahead and type in something like
407:30 me go ahead and type in something like how about c space moo so cow is not a
407:34 how about c space moo so cow is not a program I wrote It's been around for
407:35 program I wrote It's been around for decades but we installed it in vs code
407:37 decades but we installed it in vs code for you in the cloud it takes at least
407:39 for you in the cloud it takes at least one command line argument what do you
407:41 one command line argument what do you want the cow to say I can say cow say
407:43 want the cow to say I can say cow say moo and hit enter and voila there's my
407:46 moo and hit enter and voila there's my asky art of a cow saying moo on the
407:48 asky art of a cow saying moo on the screen it can say multiple words so I
407:51 screen it can say multiple words so I can say hello world and enter and now it
407:54 can say hello world and enter and now it says hello world so this is just an
407:56 says hello world so this is just an example of a silly program that uses
407:57 example of a silly program that uses command line arguments but it takes
407:59 command line arguments but it takes others too just like clang use this
408:02 others too just like clang use this convention of hyphens to change the
408:04 convention of hyphens to change the output of the program Dash something is
408:06 output of the program Dash something is just a super common convention with
408:08 just a super common convention with commandline arguments when you want a
408:09 commandline arguments when you want a very tur notation for some option like
408:12 very tur notation for some option like output um in C I read the documentation
408:15 output um in C I read the documentation and it turns out there's a-f command
408:18 and it turns out there's a-f command line argument that allows you to change
408:19 line argument that allows you to change the uh the appearance of the cow if you
408:22 the uh the appearance of the cow if you will so if I do coway dasf duck and then
408:28 will so if I do coway dasf duck and then some other word like quack it's no
408:30 some other word like quack it's no longer a cow that command line argument
408:32 longer a cow that command line argument turns it into a tiny adorable duck
408:35 turns it into a tiny adorable duck instead and then lastly just for fun
408:37 instead and then lastly just for fun because I spent way too much time
408:38 because I spent way too much time playing with command line arguments uh
408:40 playing with command line arguments uh c-f dragon and then how about like raar
408:44 c-f dragon and then how about like raar enter you can even get this on the
408:46 enter you can even get this on the screen here so this too is just an
408:49 screen here so this too is just an example of what you can do with these
408:50 example of what you can do with these command line arguments now that we have
408:52 command line arguments now that we have this building block and there's one
408:54 this building block and there's one final thing we can now do with code
408:56 final thing we can now do with code there's one last feature today that
408:58 there's one last feature today that we'll introduce before we now connect
409:00 we'll introduce before we now connect all of these dots to readability and and
409:03 all of these dots to readability and and encryption by talking lastly about
409:06 encryption by talking lastly about something called exit status it turns
409:08 something called exit status it turns out that whenever your main function
409:11 out that whenever your main function exits it returns a secret integer that
409:14 exits it returns a secret integer that you can figure out as the programmer or
409:16 you can figure out as the programmer or an advanced user what it was and these
409:18 an advanced user what it was and these exit codes exit statuses are typically
409:21 exit codes exit statuses are typically used to indicate errors so for instance
409:22 used to indicate errors so for instance over over the past couple of years if
409:24 over over the past couple of years if you us zoom and you ever got some kind
409:26 you us zoom and you ever got some kind of error you might have seen a screen
409:27 of error you might have seen a screen like this it's usually not that helpful
409:29 like this it's usually not that helpful maybe tells you to click report problem
409:31 maybe tells you to click report problem or contact support but very often in our
409:34 or contact support but very often in our human world on Macs PCS and phones you
409:37 human world on Macs PCS and phones you see cryptic error codes like literally
409:39 see cryptic error codes like literally numbers that probably only Zoom knows or
409:41 numbers that probably only Zoom knows or Microsoft or Google or whatever company
409:43 Microsoft or Google or whatever company wrote the software you're using but that
409:45 wrote the software you're using but that number corresponds to a specific error
409:48 number corresponds to a specific error that some human somewhere knows might
409:50 that some human somewhere knows might very well happen these are used similar
409:53 very well happen these are used similar L although under a different name that
409:54 L although under a different name that we'll talk about later in the term uh on
409:57 we'll talk about later in the term uh on the web as well have you ever seen this
409:59 the web as well have you ever seen this maybe not character but number so 404
410:02 maybe not character but number so 404 means
410:03 means what so error yes but really not found
410:07 what so error yes but really not found so why I mean this is the most Arcane
410:09 so why I mean this is the most Arcane thing and we'll talk in a few weeks
410:10 thing and we'll talk in a few weeks about like what this and other numbers
410:12 about like what this and other numbers mean but numbers are all around us in
410:14 mean but numbers are all around us in technology and they very often mean
410:15 technology and they very often mean something to the technical people who
410:17 something to the technical people who wrote the software less so to humans
410:19 wrote the software less so to humans like you and me why so many of us
410:21 like you and me why so many of us recognize 404 is kind of weird that like
410:23 recognize 404 is kind of weird that like that's been around long enough that we
410:25 that's been around long enough that we all know it but it really is just a
410:27 all know it but it really is just a special number that represents an error
410:29 special number that represents an error of some sort so it turns out the last
410:31 of some sort so it turns out the last thing we'll reveal today about what
410:33 thing we'll reveal today about what we've been taking for granted for two
410:34 we've been taking for granted for two weeks is what the int is in Maine we've
410:38 weeks is what the int is in Maine we've seen just a moment ago that the thing in
410:40 seen just a moment ago that the thing in the parentheses which up until now has
410:42 the parentheses which up until now has been void which means no command line
410:43 been void which means no command line arguments now in Arc string arv brackets
410:47 arguments now in Arc string arv brackets just means yes command line arguments
410:49 just means yes command line arguments and we've seen how to access them so the
410:51 and we've seen how to access them so the last piece of the puzzle honestly of all
410:53 last piece of the puzzle honestly of all the cryptic syntax the past two weeks is
410:56 the cryptic syntax the past two weeks is just what int means int is always there
410:58 just what int means int is always there for Maine and it indicates that Maine
411:01 for Maine and it indicates that Maine will always return an integer even
411:03 will always return an integer even though you and I have never done so
411:05 though you and I have never done so explicitly usually Maine returns Zero by
411:09 explicitly usually Maine returns Zero by default but it would be weird if you saw
411:11 default but it would be weird if you saw an error message saying zero so zero is
411:13 an error message saying zero so zero is just hidden you would never see it on
411:14 just hidden you would never see it on the screen but it's happening
411:15 the screen but it's happening automatically by way of how C is
411:17 automatically by way of how C is designed so let me write one final
411:20 designed so let me write one final program here I'll call it for instance
411:22 program here I'll call it for instance status . C to show you these exit
411:24 status . C to show you these exit statuses code of status. c and then up
411:28 statuses code of status. c and then up here let me do something simple like
411:29 here let me do something simple like include
411:31 include cs50.h then include standard i.h and
411:34 cs50.h then include standard i.h and then int main uh let's do actually let's
411:38 then int main uh let's do actually let's use a command line argument in argc
411:40 use a command line argument in argc string argv so that's copy paste but now
411:43 string argv so that's copy paste but now let's do this if argc does not equal to
411:49 let's do this if argc does not equal to why don't we do something like this
411:50 why don't we do something like this let's not just um default to hello world
411:53 let's not just um default to hello world like last time let's yell at the user so
411:54 like last time let's yell at the user so let's say something like printf missing
411:56 let's say something like printf missing command line argument so that they know
411:59 command line argument so that they know they screwed up and they need to run the
412:01 they screwed up and they need to run the program again correctly else let's go
412:04 program again correctly else let's go ahead and say print out uh as before
412:08 ahead and say print out uh as before hello comma percent s and then plug in
412:12 hello comma percent s and then plug in argv bracket one so the human's name
412:15 argv bracket one so the human's name from The Prompt now at this point let me
412:18 from The Prompt now at this point let me go ahead and run
412:20 go ahead and run statusstatus and I'll will type nothing
412:22 statusstatus and I'll will type nothing first first I get yelled at this time
412:25 first first I get yelled at this time I'll type it again/ status David and it
412:28 I'll type it again/ status David and it works properly but now let me show you a
412:31 works properly but now let me show you a somewhat secret cryptic command you can
412:34 somewhat secret cryptic command you can type this at your prompt and it's just a
412:36 type this at your prompt and it's just a coincidence that there's another dollar
412:38 coincidence that there's another dollar sign Echo dollar sign question mark
412:40 sign Echo dollar sign question mark totally Arcane but it allows you to see
412:42 totally Arcane but it allows you to see what exit status your program has ended
412:44 what exit status your program has ended with so let me run this again the wrong
412:47 with so let me run this again the wrong way/ status okay I get the error message
412:51 way/ status okay I get the error message what was secretly returned I can't can't
412:53 what was secretly returned I can't can't see it there's obviously no error screen
412:54 see it there's obviously no error screen but by typing Echo dollar sign question
412:56 but by typing Echo dollar sign question mark I can see that oh my program
412:59 mark I can see that oh my program automatically by default return zero
413:02 automatically by default return zero however if I run it again correctly
413:04 however if I run it again correctly status David enter this is the correct
413:07 status David enter this is the correct version but if I run Echo question mark
413:10 version but if I run Echo question mark status again it's still entered with
413:12 status again it's still entered with zero and long story short this is just a
413:14 zero and long story short this is just a missed opportunity when something goes
413:16 missed opportunity when something goes wrong why don't I return a value other
413:18 wrong why don't I return a value other than zero zero by default means success
413:20 than zero zero by default means success and it's always there automatically but
413:22 and it's always there automatically but but you can control this I can go into
413:26 but you can control this I can go into my code here and return one else if
413:29 my code here and return one else if something works fine I can return zero
413:32 something works fine I can return zero by default and honestly if I omit the
413:33 by default and honestly if I omit the return zero again zero automatically is
413:36 return zero again zero automatically is returned so let me go ahead and though
413:38 returned so let me go ahead and though be explicit just so I know what's going
413:40 be explicit just so I know what's going on make status
413:42 on make status again/ status and let's do this
413:44 again/ status and let's do this correctly with David enter hello David
413:48 correctly with David enter hello David Echo question mark uh Echo dollar sign
413:50 Echo question mark uh Echo dollar sign question mark zero so all is well
413:53 question mark zero so all is well but now if I do/ status and nothing or
413:56 but now if I do/ status and nothing or multiple things but not just David enter
413:59 multiple things but not just David enter I get the error message but now if I do
414:01 I get the error message but now if I do Echo dollar sign question mark waa there
414:03 Echo dollar sign question mark waa there now is the one so what does this now
414:06 now is the one so what does this now mean this is in the graphical world we
414:08 mean this is in the graphical world we would just show something like this on
414:10 would just show something like this on the screen which is a little more
414:11 the screen which is a little more informative to the user but even in the
414:12 informative to the user but even in the Linux world we don't have a guey
414:14 Linux world we don't have a guey necessarily even for the programs we've
414:16 necessarily even for the programs we've written you can check these exit
414:17 written you can check these exit statuses and in fact more comfortable
414:19 statuses and in fact more comfortable more advanced programmers when they
414:21 more advanced programmers when they write code that calls programs be it
414:24 write code that calls programs be it coway or anything else you can encode
414:27 coway or anything else you can encode check what the exit status is of a
414:29 check what the exit status is of a program and then decide did my program
414:31 program and then decide did my program work or did it not and now let's connect
414:34 work or did it not and now let's connect the final dots uh before we adjourn for
414:36 the final dots uh before we adjourn for some fruit snacks uh cryptography namely
414:40 some fruit snacks uh cryptography namely one of the applications this week via
414:42 one of the applications this week via which you'll be able to send if you will
414:44 which you'll be able to send if you will secret messages and better yet decre
414:46 secret messages and better yet decre secret messages this will be in addition
414:48 secret messages this will be in addition to perhaps analyzing the readability of
414:50 to perhaps analyzing the readability of text using puristic like we identified
414:52 text using puristic like we identified at the start of class 2 so a
414:54 at the start of class 2 so a cryptography is just the art the science
414:56 cryptography is just the art the science of encrypting information scrambling
414:58 of encrypting information scrambling information so that if you have a secret
415:00 information so that if you have a secret message to send in so-called plain text
415:03 message to send in so-called plain text you can run it through some algorithm
415:05 you can run it through some algorithm and turn it into what's called Cipher
415:07 and turn it into what's called Cipher text thereby encrypting it and only
415:10 text thereby encrypting it and only someone who knows what algorithm you've
415:12 someone who knows what algorithm you've used and what input you've used to the
415:14 used and what input you've used to the algorithm theoretically can decrypt that
415:17 algorithm theoretically can decrypt that process and convert it back to the
415:18 process and convert it back to the original message so if we use our mental
415:20 original message so if we use our mental model from last week here is a problem
415:22 model from last week here is a problem here is an input and output the goal I
415:25 here is an input and output the goal I claim here is to take some plain text
415:27 claim here is to take some plain text like the message you want to send think
415:29 like the message you want to send think back to grade school if you ever passed
415:30 back to grade school if you ever passed a note to a friend or to your crush
415:32 a note to a friend or to your crush saying I love you it's a little awkward
415:34 saying I love you it's a little awkward if the teacher or someone else
415:35 if the teacher or someone else intercepts the paper and in English it
415:37 intercepts the paper and in English it just says I love you or whatever it is
415:39 just says I love you or whatever it is it'd be nice if you had at least
415:40 it'd be nice if you had at least encrypted it in some way but the other
415:42 encrypted it in some way but the other person needs to know what algorithm you
415:44 person needs to know what algorithm you used and what inputs you used to that
415:46 used and what inputs you used to that algorithm so that ultimately they can
415:48 algorithm so that ultimately they can decode the so-called Cipher text which
415:50 decode the so-called Cipher text which is the output so what go inside of the
415:53 is the output so what go inside of the box today well an algorithm as it
415:55 box today well an algorithm as it relates to cryptography is called a
415:57 relates to cryptography is called a cipher and a cipher is a fancy name for
415:59 cipher and a cipher is a fancy name for an algorithm that encrypts text from
416:01 an algorithm that encrypts text from plain text to Cipher text the catch is
416:04 plain text to Cipher text the catch is there needs to be not just the algorithm
416:06 there needs to be not just the algorithm there needs to be an input to it and so
416:09 there needs to be an input to it and so for instance you might draw the picture
416:11 for instance you might draw the picture like this for the first time today and
416:12 like this for the first time today and we've seen this in code you can give
416:14 we've seen this in code you can give multiple inputs or arguments to
416:16 multiple inputs or arguments to functions so in this black box can you
416:18 functions so in this black box can you imagine passing in the message you want
416:19 imagine passing in the message you want to send and then some Secret so for
416:23 to send and then some Secret so for instance suppose that the simplest thing
416:25 instance suppose that the simplest thing I could think of as a kid was instead of
416:27 I could think of as a kid was instead of sending the letter A why don't I write
416:29 sending the letter A why don't I write the letter b instead of the letter B why
416:31 the letter b instead of the letter B why don't I write the letter c so I can kind
416:33 don't I write the letter c so I can kind of shift the English alphabet by one
416:35 of shift the English alphabet by one space so a becomes b b becomes C dot dot
416:38 space so a becomes b b becomes C dot dot dot Z becomes a you can wrap around at
416:41 dot Z becomes a you can wrap around at the end and let's assume no punctuation
416:42 the end and let's assume no punctuation in this part of the story so that's a
416:44 in this part of the story so that's a very simple algorithm add a value to
416:48 very simple algorithm add a value to each letter and send the value as the
416:51 each letter and send the value as the cipher text and now the teacher the
416:53 cipher text and now the teacher the classmate they have to know that you use
416:55 classmate they have to know that you use not only this rotational algorithm also
416:57 not only this rotational algorithm also known as a Caesar Cipher they also need
416:59 known as a Caesar Cipher they also need to know what number you use did you add
417:01 to know what number you use did you add one to every letter two to every letter
417:03 one to every letter two to every letter 25 to every letter now if they're super
417:06 25 to every letter now if they're super smart and probably not the the young age
417:08 smart and probably not the the young age in this story they could also just try
417:10 in this story they could also just try all possibilities and that would be an
417:11 all possibilities and that would be an attack on the algorithm this is not a
417:13 attack on the algorithm this is not a sophisticated algorithm but it's enough
417:15 sophisticated algorithm but it's enough to send a message in class so if the two
417:17 to send a message in class so if the two inputs now are high as the plain text
417:19 inputs now are high as the plain text message and one as the so-called key the
417:23 message and one as the so-called key the secret number that only you and the
417:25 secret number that only you and the other person know you might uh be able
417:28 other person know you might uh be able to encrypt a message from one way to the
417:30 to encrypt a message from one way to the other and so in this case for instance
417:32 other and so in this case for instance high would become i j exclamation point
417:36 high would become i j exclamation point in this version of the algorithm we're
417:37 in this version of the algorithm we're not going to bother with numbers or
417:39 not going to bother with numbers or punctuation will only operate on a
417:40 punctuation will only operate on a through z be it uppercase or lowercase
417:43 through z be it uppercase or lowercase so now if you were to receive a a slip
417:45 so now if you were to receive a a slip of paper in class with i j on it you
417:48 of paper in class with i j on it you know you the recipient would know what
417:50 know you the recipient would know what it is so long as you know that the
417:51 it is so long as you know that the sender used one because you just reverse
417:54 sender used one because you just reverse the algorithm and you subtract one
417:55 the algorithm and you subtract one instead the teacher you know they
417:57 instead the teacher you know they probably don't know what this means and
417:58 probably don't know what this means and they're not going to spend time hacking
418:00 they're not going to spend time hacking the message so it just looks scrambled
418:02 the message so it just looks scrambled to them and that's what we get from
418:03 to them and that's what we get from encryption someone who intercepts it be
418:05 encryption someone who intercepts it be it in class or in the real world on the
418:07 it in class or in the real world on the Internet or anywhere else can't actually
418:09 Internet or anywhere else can't actually figure out ideally what it is you have
418:11 figure out ideally what it is you have sent the opposite of course is indeed
418:14 sent the opposite of course is indeed called decryption but the process is the
418:15 called decryption but the process is the same we now pass in negative one and so
418:18 same we now pass in negative one and so how about this why don't we end with a
418:21 how about this why don't we end with a demonstration here
418:22 demonstration here u y JT xbt
418:25 u y JT xbt dt50 there's a bit of a tell there if we
418:28 dt50 there's a bit of a tell there if we pass that in and do Nega -1 well how do
418:31 pass that in and do Nega -1 well how do we get out the plain text originally
418:34 we get out the plain text originally well if this is the cipher text and we
418:35 well if this is the cipher text and we subtract one from each letter I think U
418:39 subtract one from each letter I think U becomes t i becomes h j becomes i t
418:44 becomes t i becomes h j becomes i t becomes s x becomes w b becomes a t
418:49 becomes s x becomes w b becomes a t becomes s d becomes c t becomes s and
418:55 becomes s d becomes c t becomes s and this was indeed cs50 have a duck on your
418:58 this was indeed cs50 have a duck on your way out and some snacks in the
419:00 way out and some snacks in the [Applause]
419:03 [Applause] [Music]
419:22 lobby [Music]
420:54 all right all right this is cs50 and this is week three already wherein we'll
420:57 this is week three already wherein we'll take a look back actually at week zero
420:58 take a look back actually at week zero where we first began and in week zero
421:00 where we first began and in week zero recall that everything was very
421:02 recall that everything was very intuitive in a sense we talked not just
421:04 intuitive in a sense we talked not just about representation of information but
421:06 about representation of information but algorithms and we talked about tearing a
421:08 algorithms and we talked about tearing a phone book again and again and that
421:09 phone book again and again and that somehow got us to a better solution but
421:11 somehow got us to a better solution but today we'll try to start formalizing
421:13 today we'll try to start formalizing some of those ideas and capturing some
421:15 some of those ideas and capturing some of those same ideas not in pseudo code
421:17 of those same ideas not in pseudo code just yet but in uh actual code as well
421:20 just yet but in uh actual code as well but we'll also consider the the
421:22 but we'll also consider the the efficiency of those algorithms like just
421:24 efficiency of those algorithms like just how good how welld designed our
421:26 how good how welld designed our algorithms actually are and if you
421:27 algorithms actually are and if you recall when we did the phone book
421:28 recall when we did the phone book example where in I first had an
421:30 example where in I first had an algorithm searching one page at a time
421:32 algorithm searching one page at a time and then second when two pages at a time
421:34 and then second when two pages at a time and then third started tearing the thing
421:36 and then third started tearing the thing in half recall that we with a wave of
421:38 in half recall that we with a wave of the hand kind of analyzed it as follows
421:40 the hand kind of analyzed it as follows we proposed that if the x-axis here is
421:42 we proposed that if the x-axis here is the size of the problem like number of
421:44 the size of the problem like number of pages in a phone book and the y- axis is
421:47 pages in a phone book and the y- axis is the time required to solve the problem
421:49 the time required to solve the problem in seconds minutes page tears whatever
421:51 in seconds minutes page tears whatever your unit of measuring is recall that
421:52 your unit of measuring is recall that the first algorithm was this straight
421:54 the first algorithm was this straight line such that if you had n pages in the
421:56 line such that if you had n pages in the phone book it might have this slope of N
421:58 phone book it might have this slope of N and there's this one toone relationship
422:00 and there's this one toone relationship between pages and tears two pages at a
422:03 between pages and tears two pages at a time of course was twice as fast but
422:05 time of course was twice as fast but still really the same shape the yellow
422:07 still really the same shape the yellow line here indicating that yeah it's n /
422:09 line here indicating that yeah it's n / two maybe plus one if you have to double
422:11 two maybe plus one if you have to double back as we discussed but it's really
422:12 back as we discussed but it's really still fundamentally the same algorithm
422:15 still fundamentally the same algorithm one or two pages at a time but the third
422:17 one or two pages at a time but the third algorithm recall was this one here in
422:20 algorithm recall was this one here in green where we called it logarithmic in
422:22 green where we called it logarithmic in terms of how fast or how slow it was and
422:24 terms of how fast or how slow it was and indeed the implication of this algorithm
422:27 indeed the implication of this algorithm was that we could even double the size
422:28 was that we could even double the size of the phone book and no big deal one
422:31 of the phone book and no big deal one additional page tear and we take yet
422:32 additional page tear and we take yet another thousand page bite out of the
422:34 another thousand page bite out of the phone book so today we'll revisit some
422:36 phone book so today we'll revisit some of these ideas formalize them a bit but
422:37 of these ideas formalize them a bit but also translate some of them ultimately
422:39 also translate some of them ultimately to code and all of that now is possible
422:42 to code and all of that now is possible because we have this lower level
422:43 because we have this lower level understanding perhaps of like what's
422:44 understanding perhaps of like what's actually inside of your computer this of
422:46 actually inside of your computer this of course is your computer's Ram or memory
422:49 course is your computer's Ram or memory and recall that if we kind of start to
422:50 and recall that if we kind of start to abstract this away your compter
422:51 abstract this away your compter computer's memory is really just a grid
422:53 computer's memory is really just a grid of bites in fact we don't have to look
422:55 of bites in fact we don't have to look at the hardware anymore and we looked at
422:57 at the hardware anymore and we looked at a grid of bites like this whereby each
422:59 a grid of bites like this whereby each of these bites could be used to store a
423:01 of these bites could be used to store a Char an INT a long or even an entire
423:04 Char an INT a long or even an entire string at that but let's focus perhaps
423:07 string at that but let's focus perhaps just on a subset of this because last
423:09 just on a subset of this because last week of course we emphasized really
423:11 week of course we emphasized really arrays storing things in arrays and that
423:13 arrays storing things in arrays and that allowed us to start storing entire
423:16 allowed us to start storing entire strings sequences of characters and even
423:18 strings sequences of characters and even arrays of integers if we want to have
423:20 arrays of integers if we want to have multiple ones and not just multiple
423:22 multiple ones and not just multiple variables as well but the catch is that
423:24 variables as well but the catch is that if you look inside of an array in the
423:26 if you look inside of an array in the computer's memory and for instance
423:28 computer's memory and for instance suppose these integers here are stored
423:30 suppose these integers here are stored it's pretty easy for us humans to glance
423:32 it's pretty easy for us humans to glance at this and immediately find the number
423:34 at this and immediately find the number 50 you sort of have this bird's eye view
423:36 50 you sort of have this bird's eye view from where you're seated of everything
423:37 from where you're seated of everything on the screen and so it's pretty obvious
423:39 on the screen and so it's pretty obvious how you get to the number 50 but in the
423:41 how you get to the number 50 but in the world of computers of course it turns
423:44 world of computers of course it turns out that this is hardware and computers
423:46 out that this is hardware and computers for today's purposes can only do one
423:48 for today's purposes can only do one thing at a time they can't just take it
423:50 thing at a time they can't just take it all in and find instantly some number
423:52 all in and find instantly some number like 50 so perhaps a decent metaphor is
423:55 like 50 so perhaps a decent metaphor is to consider the array of memory inside
423:57 to consider the array of memory inside of your computer really is a sequence of
424:00 of your computer really is a sequence of of closed doors and if the computer
424:02 of closed doors and if the computer wants to find some value in an array it
424:05 wants to find some value in an array it has to do the digital equivalent of
424:06 has to do the digital equivalent of opening each of these doors one at a
424:09 opening each of these doors one at a time now how can code do that well of
424:11 time now how can code do that well of course we introduced indices or indexes
424:13 course we introduced indices or indexes last week whereby we by convention call
424:16 last week whereby we by convention call the first element of an array location
424:18 the first element of an array location zero the second location one the Third
424:20 zero the second location one the Third location two and so forth so-called zero
424:23 location two and so forth so-called zero indexed and this allowed us to now
424:25 indexed and this allowed us to now bridge this conceptual world of like
424:26 bridge this conceptual world of like what's going on in memory with actual
424:28 what's going on in memory with actual code because now we had this square
424:30 code because now we had this square bracket syntax via which we could go
424:31 bracket syntax via which we could go searching for something if we so choose
424:34 searching for something if we so choose and it turns out if I now uh paint these
424:37 and it turns out if I now uh paint these red instead of yellow it would seem that
424:39 red instead of yellow it would seem that we actually have a pretty good physical
424:41 we actually have a pretty good physical metaphor here standing in place for what
424:43 metaphor here standing in place for what would be a computer's uh array of memory
424:46 would be a computer's uh array of memory if for instance you're storing some
424:47 if for instance you're storing some seven numbers like that and so today we
424:49 seven numbers like that and so today we begin with a look of a specific type typ
424:51 begin with a look of a specific type typ of algorithm that is for searching like
424:53 of algorithm that is for searching like searching is all over the place all of
424:55 searching is all over the place all of us have probably gone to google.com or
424:57 us have probably gone to google.com or some equivalent like already multiple
424:59 some equivalent like already multiple times per day and getting back answers
425:01 times per day and getting back answers fast is what companies like Google are
425:03 fast is what companies like Google are really good at so how are they doing
425:05 really good at so how are they doing that how are they storing information in
425:07 that how are they storing information in computers uh memory well let's consider
425:09 computers uh memory well let's consider what this really is it's really just a
425:11 what this really is it's really just a problem as it was back in week zero the
425:13 problem as it was back in week zero the input though to the problem for now
425:15 input though to the problem for now might be this array of seven lockers so
425:17 might be this array of seven lockers so that's the input to the problem inside
425:18 that's the input to the problem inside of which is a number and maybe for
425:20 of which is a number and maybe for Simplicity now we just want a yes no a
425:22 Simplicity now we just want a yes no a true false answer a bull that is to say
425:25 true false answer a bull that is to say of whether or not some number like 50 is
425:28 of whether or not some number like 50 is in that array it's not quite as fancy as
425:31 in that array it's not quite as fancy as Google that doesn't just tell you yes we
425:33 Google that doesn't just tell you yes we have search results it actually gives
425:35 have search results it actually gives you the search results but for now we'll
425:36 you the search results but for now we'll keep it simple and just output as part
425:38 keep it simple and just output as part of this problem yes or no true or false
425:41 of this problem yes or no true or false we have found the number we're looking
425:42 we have found the number we're looking for given an input like that array but
425:45 for given an input like that array but it turns out inside of this black box
425:47 it turns out inside of this black box that we keep coming back to there's all
425:49 that we keep coming back to there's all sorts of possible algorithms and we talk
425:51 sorts of possible algorithms and we talk about this at a high level conceptually
425:53 about this at a high level conceptually in week zero with the phone book but
425:54 in week zero with the phone book but today let's consider it a little more
425:57 today let's consider it a little more concretely uh by way of a game that some
425:59 concretely uh by way of a game that some of you might have grown up with namely
426:00 of you might have grown up with namely Monopoly and so behind these doors it
426:02 Monopoly and so behind these doors it turns out we'll be hidden some
426:04 turns out we'll be hidden some denominations of Monopoly money but for
426:06 denominations of Monopoly money but for this we now have two volunteers if you'd
426:07 this we now have two volunteers if you'd like to greet the world hi I'm
426:12 like to greet the world hi I'm Jackson yay hi my name is Stephanie
426:16 Jackson yay hi my name is Stephanie Steph and you want to say a little
426:16 Steph and you want to say a little something about yourselves years house
426:19 something about yourselves years house first year living in Matthews
426:22 first year living in Matthews nice and I'm a first year in Canada nice
426:24 nice and I'm a first year in Canada nice well Welcome to our two
426:26 well Welcome to our two volunteers so why don't we do this would
426:29 volunteers so why don't we do this would one of you like to volunteer the other
426:31 one of you like to volunteer the other to go
426:32 to go first all go first okay all right so
426:36 first all go first okay all right so Stephanie's up first and behind one of
426:38 Stephanie's up first and behind one of these doors here we've hidden the
426:40 these doors here we've hidden the Monopoly money 50 and so we'd like you
426:42 Monopoly money 50 and so we'd like you to find the 50 we'll tell you nothing
426:44 to find the 50 we'll tell you nothing more about the lockers but we would like
426:45 more about the lockers but we would like you to execute a certain algorithm and
426:47 you to execute a certain algorithm and in fact I'm going to give you some
426:48 in fact I'm going to give you some pseudo code for this and I'm going to
426:50 pseudo code for this and I'm going to give you the name for it it's called
426:51 give you the name for it it's called linear search and as the name implies
426:54 linear search and as the name implies you're pretty much going to end up
426:55 you're pretty much going to end up walking in sort of a straight line but
426:57 walking in sort of a straight line but how are you going to do this well let me
426:58 how are you going to do this well let me propose that in a moment your first step
427:00 propose that in a moment your first step will be to think kind of like a loop for
427:02 will be to think kind of like a loop for each door from left to right what do we
427:04 each door from left to right what do we want you to do on each iteration well if
427:06 want you to do on each iteration well if 50 is behind that door then we want to
427:10 50 is behind that door then we want to go ahead and have you return true and
427:12 go ahead and have you return true and sort of hold up the 50 proudly if you
427:13 sort of hold up the 50 proudly if you will for the group otherwise if you get
427:16 will for the group otherwise if you get through that whole Loop and you haven't
427:18 through that whole Loop and you haven't found the number 50 you can just throw
427:19 found the number 50 you can just throw up your hands and disappointment false
427:22 up your hands and disappointment false you've not found the number 50 so to be
427:24 you've not found the number 50 so to be clear step one is going to be for each
427:26 clear step one is going to be for each uh door from left to right how would you
427:29 uh door from left to right how would you like to
427:37 begin yep oh and then yep there we go yep oh and if you'd like to at least
427:40 yep oh and if you'd like to at least tell oh good good acting here what have
427:43 tell oh good good acting here what have you found instead it's not 50 but 20 oh
427:47 you found instead it's not 50 but 20 oh okay so step one was a fail so let's
427:49 okay so step one was a fail so let's move on to step two inside of the Loop
427:51 move on to step two inside of the Loop what are you going to do next I'm going
427:53 what are you going to do next I'm going to move to the next door
428:03 okay almost okay almost sort of a 500 instead next Locker I would rather
428:15 no okay we're not telling the audience oh okay so keep going this is
428:18 audience oh okay so keep going this is step three
428:20 step three now
428:25 oh [Music]
428:27 [Music] man five okay few more lockers to
428:50 step this one kind of close all right and finally the last clearly you've been
428:52 and finally the last clearly you've been perhaps set up here let's go all right
428:57 perhaps set up here let's go all right so the number
429:03 50 and Stephanie if I may let me ask you a question here so on the screen this is
429:05 a question here so on the screen this is the pseudo code you just executed
429:08 the pseudo code you just executed suppose though I had done what many of
429:10 suppose though I had done what many of us have gotten into the habit of doing
429:12 us have gotten into the habit of doing when you have a if condition you often
429:13 when you have a if condition you often have an else Branch as well suppose that
429:16 have an else Branch as well suppose that I had done this now and I'm marking it
429:18 I had done this now and I'm marking it in red to be clear this is wrong but
429:20 in red to be clear this is wrong but what would have have been bad about this
429:22 what would have have been bad about this code using an if and an else might you
429:25 code using an if and an else might you say any
429:33 instincts um then you would end up like cancelling
429:37 um then you would end up like cancelling canceling the code before you found the
429:38 canceling the code before you found the 50 yeah exactly just be eternally sad
429:41 50 yeah exactly just be eternally sad indeed when Stephanie had opened the
429:42 indeed when Stephanie had opened the first Locker she had found 20 20 of
429:44 first Locker she had found 20 20 of course is not 50 she would have decreed
429:46 course is not 50 she would have decreed false but of course she hadn't checked
429:47 false but of course she hadn't checked all of the rest of the locker so that
429:49 all of the rest of the locker so that would seem to be a key detail that would
429:51 would seem to be a key detail that would this implementation of the SoDo coat we
429:53 this implementation of the SoDo coat we actually do go through as we did and
429:55 actually do go through as we did and only return false not even with an else
429:57 only return false not even with an else but just at the end of the loop such
430:00 but just at the end of the loop such that we only reach that line if we don't
430:02 that we only reach that line if we don't return truer uh earlier than that well
430:04 return truer uh earlier than that well let's go ahead and do this let me take
430:05 let's go ahead and do this let me take the mic from you if you'd like to take a
430:06 the mic from you if you'd like to take a seat next to Jackson Jackson in just a
430:08 seat next to Jackson Jackson in just a moment we'll have you come up Carter if
430:09 moment we'll have you come up Carter if you don't mind reorganizing the uh
430:12 you don't mind reorganizing the uh lockers for us but in the meantime let
430:13 lockers for us but in the meantime let me point out how we might now translate
430:15 me point out how we might now translate that same idea to code pretty high level
430:17 that same idea to code pretty high level pretty English oriented with that pseudo
430:19 pretty English oriented with that pseudo code but really now as of last week we
430:21 code but really now as of last week we have syntax via which Stephanie and soon
430:24 have syntax via which Stephanie and soon Jackson could treat this Locker the set
430:26 Jackson could treat this Locker the set of lockers as really indeed an array
430:28 of lockers as really indeed an array using bracket notation so we can now get
430:30 using bracket notation so we can now get a little closer in our pseudo code to
430:32 a little closer in our pseudo code to actual code and the way a computer
430:34 actual code and the way a computer scientist for instance would translate
430:36 scientist for instance would translate fairly high level English pseudo code
430:38 fairly high level English pseudo code like this to something that's a little
430:40 like this to something that's a little closer to C or any language that
430:42 closer to C or any language that supports arrays would be a little more
430:44 supports arrays would be a little more cryptically like this but you'll see
430:45 cryptically like this but you'll see more of this syntax in the coming days
430:48 more of this syntax in the coming days for I from 0 to n minus one this is
430:51 for I from 0 to n minus one this is still pseudo code but that's kind of
430:52 still pseudo code but that's kind of like the english-like way of expressing
430:54 like the english-like way of expressing what we've known come to know as a for
430:56 what we've known come to know as a for Loop if 50 is behind doors bracket I so
430:59 Loop if 50 is behind doors bracket I so I'm assuming for the sake of discussion
431:01 I'm assuming for the sake of discussion that doors now is the name of my
431:03 that doors now is the name of my variable this array of seven doors but
431:05 variable this array of seven doors but then the rest of the logic the rest of
431:07 then the rest of the logic the rest of the pseudo code really is the same way
431:09 the pseudo code really is the same way and so you'll find in time that
431:10 and so you'll find in time that programmers computer scientists more
431:12 programmers computer scientists more generally when you start expressing
431:13 generally when you start expressing ideas algorithms to someone else instead
431:16 ideas algorithms to someone else instead of maybe operating at this level here
431:19 of maybe operating at this level here you now have in your vocabulary really a
431:21 you now have in your vocabulary really a new new syntax that you can be a little
431:22 new new syntax that you can be a little more specific not getting so into the
431:24 more specific not getting so into the weeds of writing actual C code but at
431:26 weeds of writing actual C code but at least now doing something that's a
431:28 least now doing something that's a little closer to manipulating an array
431:30 little closer to manipulating an array like this so Jackson would you like to
431:32 like this so Jackson would you like to uh stand on up all right yes yes support
431:36 uh stand on up all right yes yes support for Jackson here too
431:38 for Jackson here too nice and here now I'm going to allow you
431:41 nice and here now I'm going to allow you an assumption that Stephanie did not
431:43 an assumption that Stephanie did not have Stephanie clearly was really doing
431:44 have Stephanie clearly was really doing her best searching from left to right
431:46 her best searching from left to right using linear searches we'll Now call it
431:49 using linear searches we'll Now call it but they were pretty much in random
431:50 but they were pretty much in random order right there was a 20 over there
431:52 order right there was a 20 over there there was a one over there and then a 50
431:53 there was a one over there and then a 50 so we deliberately jumbled things up and
431:56 so we deliberately jumbled things up and did not sort the numbers for her but
431:58 did not sort the numbers for her but Carter kindly has just come up to give
432:00 Carter kindly has just come up to give you a leg up Jackson by sorting the
432:02 you a leg up Jackson by sorting the numbers in advance and we'd like you
432:04 numbers in advance and we'd like you this time much like in week zero to do
432:07 this time much like in week zero to do something again and again but this time
432:08 something again and again but this time using what we'll Now call binary search
432:11 using what we'll Now call binary search it's exactly the same algorithm
432:12 it's exactly the same algorithm conceptually as we did in week zero but
432:14 conceptually as we did in week zero but if we translate to the context of this
432:16 if we translate to the context of this array we might now might say something
432:18 array we might now might say something like this the first step for Jackson
432:20 like this the first step for Jackson might be to ask ask the question if 50
432:22 might be to ask ask the question if 50 is behind the middle door where
432:24 is behind the middle door where presumably he's done some mental math to
432:25 presumably he's done some mental math to figure out what the middle is then he's
432:28 figure out what the middle is then he's going to just return true and hopefully
432:29 going to just return true and hopefully we'll get lucky and 50 will be right
432:31 we'll get lucky and 50 will be right there of course there's two other
432:34 there of course there's two other possibilities at least which would be
432:36 possibilities at least which would be what 50 is with respect to these
432:39 what 50 is with respect to these doors yeah so to the left or to the
432:41 doors yeah so to the left or to the right alternatively so if 50 is less
432:44 right alternatively so if 50 is less than the middle door then presumably
432:46 than the middle door then presumably Jackson's going to want to go left else
432:48 Jackson's going to want to go left else if 50 is greater than the middle door
432:50 if 50 is greater than the middle door he's going to want to go right much like
432:52 he's going to want to go right much like I did uh physically last week with the
432:54 I did uh physically last week with the phone book dividing and conquering left
432:56 phone book dividing and conquering left to right but there's actually a a fourth
432:58 to right but there's actually a a fourth case let's put it on the board first
433:00 case let's put it on the board first what else might happen here that Jackson
433:02 what else might happen here that Jackson should
433:03 should consider yeah oh it's not there it's not
433:07 consider yeah oh it's not there it's not there so let me actually go back and
433:09 there so let me actually go back and amend my my pseudo code here and just
433:11 amend my my pseudo code here and just say Jackson if we don't hand you any
433:12 say Jackson if we don't hand you any doors at all or eventually as he's
433:15 doors at all or eventually as he's dividing and conquering if he's left
433:16 dividing and conquering if he's left with no more doors we have to handle
433:18 with no more doors we have to handle that situation so that the def behavior
433:20 that situation so that the def behavior is defined find all right so with that
433:22 is defined find all right so with that said Jackson do you want to go ahead and
433:23 said Jackson do you want to go ahead and find us the number 50 and walk us
433:25 find us the number 50 and walk us through verbally what you're doing and
433:26 through verbally what you're doing and finding all right so it looks like this
433:29 finding all right so it looks like this one is the middle door so I'm going to
433:32 one is the middle door so I'm going to open
433:33 open it but it's 20 not 50 oh sad okay what's
433:38 it but it's 20 not 50 oh sad okay what's going through your head now so now I'm
433:40 going through your head now so now I'm looking because 50 is higher than 20 I
433:43 looking because 50 is higher than 20 I want to look to the right good um and
433:46 want to look to the right good um and look for the new middle door which would
433:48 look for the new middle door which would be here nice and it's 100 sad um but 50
433:53 be here nice and it's 100 sad um but 50 is less than 100 so now we know to look
433:56 is less than 100 so now we know to look left which would be here and Tada nice
434:00 left which would be here and Tada nice very well done this time around
434:03 very well done this time around too
434:05 too so thank you first to our volunteers
434:08 so thank you first to our volunteers here and in fact um since you're fan of
434:10 here and in fact um since you're fan of Monopoly as we're so informed we have
434:12 Monopoly as we're so informed we have the Cambridge edition of Monopoly with
434:14 the Cambridge edition of Monopoly with all your Harvard favorites here you go
434:17 all your Harvard favorites here you go thank you so thank you to our volunteers
434:19 thank you so thank you to our volunteers for finding us 50
434:25 so kind of was more popular than we expected so here we can translate this
434:28 expected so here we can translate this one more time into something a little
434:29 one more time into something a little closer to code and again still pseudo
434:32 closer to code and again still pseudo code but here now might be another
434:34 code but here now might be another formulation of exactly what Jackson just
434:36 formulation of exactly what Jackson just did just using the nomenclature now of
434:37 did just using the nomenclature now of arrays where you can be a little more
434:39 arrays where you can be a little more precise with your instructions and still
434:40 precise with your instructions and still leave it to someone else to translate
434:42 leave it to someone else to translate this finally to code but here we have
434:44 this finally to code but here we have same question at the beginning if no
434:45 same question at the beginning if no doors left return false if 50 is behind
434:48 doors left return false if 50 is behind doors bracket middle so I'm assuming
434:51 doors bracket middle so I'm assuming here because this is pseudo code that
434:52 here because this is pseudo code that somewhere I've done the mental math or
434:54 somewhere I've done the mental math or the actual math to figure out what the
434:56 the actual math to figure out what the index of middle is for instance if these
434:59 index of middle is for instance if these are seven doors in an array this would
435:01 are seven doors in an array this would be location zero 1 2 3 4 5 6 so somehow
435:07 be location zero 1 2 3 4 5 6 so somehow I've taken the total number of doors
435:09 I've taken the total number of doors seven divided by two to find the middle
435:12 seven divided by two to find the middle that's three and a half we have to deal
435:13 that's three and a half we have to deal with rounding but suffice it to say
435:15 with rounding but suffice it to say there's a well- defined formula for
435:17 there's a well- defined formula for finding the middle index given the total
435:19 finding the middle index given the total number of lockers divide by two and then
435:21 number of lockers divide by two and then round accordingly so that's presumably
435:23 round accordingly so that's presumably what Jackson did just by counting in his
435:24 what Jackson did just by counting in his head to find us door number three not
435:27 head to find us door number three not the third door the fourth door but door
435:30 the third door the fourth door but door bracket three so this is just saying if
435:32 bracket three so this is just saying if 50 is behind door is bracket middle
435:34 50 is behind door is bracket middle return true that was not the case he
435:36 return true that was not the case he found a $20 bill instead else if 50 is
435:39 found a $20 bill instead else if 50 is less than the uh doors bracket middle go
435:42 less than the uh doors bracket middle go ahead and now it gets interesting search
435:44 ahead and now it gets interesting search doors zero through doors middle minus
435:48 doors zero through doors middle minus one so it's getting a little more to the
435:50 one so it's getting a little more to the Weeds now but if middle is three this
435:53 Weeds now but if middle is three this one here well we want to now have
435:55 one here well we want to now have Jackson search if 50 had been uh if the
435:58 Jackson search if 50 had been uh if the number had been less we want to start at
436:00 number had been less we want to start at bracket zero and go up through this one
436:03 bracket zero and go up through this one when we deliberately subtract one CU
436:05 when we deliberately subtract one CU what's the point of looking in the same
436:06 what's the point of looking in the same Locker again we might as well do zero
436:08 Locker again we might as well do zero through middle minus one else if 50 is
436:12 through middle minus one else if 50 is greater than doors bracket middle which
436:14 greater than doors bracket middle which it was what did we then do Jackson
436:16 it was what did we then do Jackson intuitively searched for doors middle
436:18 intuitively searched for doors middle plus one through door
436:21 plus one through door n minus one and honestly it gets a
436:23 n minus one and honestly it gets a little Annoying having the pluses and
436:24 little Annoying having the pluses and the minuses here but just think of what
436:25 the minuses here but just think of what it means this is the middle door and
436:28 it means this is the middle door and Jackson then did proceed to search
436:30 Jackson then did proceed to search through doors middle plus one because
436:33 through doors middle plus one because there's no point in searching this one
436:34 there's no point in searching this one again and then the last element in any
436:37 again and then the last element in any array of size n where n is just our
436:39 array of size n where n is just our go-to number uh for the size is always
436:42 go-to number uh for the size is always going to be n minus one it's not going
436:44 going to be n minus one it's not going to be n it's going to be n minus one
436:46 to be n it's going to be n minus one because we always start counting at Rays
436:48 because we always start counting at Rays at zero so here then we have a
436:50 at zero so here then we have a translation into pseudo code that's a
436:52 translation into pseudo code that's a little closer to C of this exact same
436:54 little closer to C of this exact same idea and here we come full circle to
436:56 idea and here we come full circle to week zero like in week zero is pretty
436:58 week zero like in week zero is pretty intuitive to imagine dividing and
437:00 intuitive to imagine dividing and conquering a problem like this but if
437:02 conquering a problem like this but if you now think back to actual your iPhone
437:04 you now think back to actual your iPhone your Android phone or the like when
437:06 your Android phone or the like when you're doing autocomplete and searching
437:08 you're doing autocomplete and searching the list it's possible if you don't have
437:10 the list it's possible if you don't have many friends or family or colleagues in
437:12 many friends or family or colleagues in the phone you know what linear search
437:14 the phone you know what linear search just checking every name for the person
437:16 just checking every name for the person you're searching for might be perfectly
437:19 you're searching for might be perfectly fine but odds are your phones being
437:21 fine but odds are your phones being smarter than that especially if you
437:22 smarter than that especially if you start to have dozens hundreds thousands
437:24 start to have dozens hundreds thousands of people in your contacts over the
437:26 of people in your contacts over the years what would be better than linear
437:27 years what would be better than linear search well perhaps binary search but
437:30 search well perhaps binary search but but but there's an assumption a
437:32 but but there's an assumption a requirement which is what why was
437:34 requirement which is what why was Jackson ultimately able to find the 50
437:37 Jackson ultimately able to find the 50 in just like three steps instead of a
437:40 in just like three steps instead of a full seven like
437:42 full seven like Stephanie because the array was sorted
437:45 Stephanie because the array was sorted and so this is sort of a teaser for what
437:47 and so this is sort of a teaser for what we'll have to come back to later today
437:48 we'll have to come back to later today well you know how much effort did it
437:50 well you know how much effort did it take someone like Carter how much effort
437:52 take someone like Carter how much effort does it take your phone to sort all of
437:54 does it take your phone to sort all of those names and numbers in advance
437:56 those names and numbers in advance because maybe it's not actually worth
437:57 because maybe it's not actually worth the amount of time now someone like
437:59 the amount of time now someone like Google probably somehow keeps the
438:01 Google probably somehow keeps the database of web pages sorted you could
438:03 database of web pages sorted you could imagine it being super slow if when you
438:05 imagine it being super slow if when you type in cats or something else into
438:07 type in cats or something else into google.com if they searched linearly
438:09 google.com if they searched linearly over their entire data set ideally
438:11 over their entire data set ideally they're doing something a little smarter
438:13 they're doing something a little smarter than that so we'll formalize now exactly
438:16 than that so we'll formalize now exactly this kind of analysis and it's not going
438:18 this kind of analysis and it's not going to be so much mathy as it still will be
438:20 to be so much mathy as it still will be in intuitive but we'll introduce you to
438:22 in intuitive but we'll introduce you to some some jargon some terminology that
438:24 some some jargon some terminology that most any programmer or computer
438:26 most any programmer or computer scientist might use when analyzing their
438:28 scientist might use when analyzing their own algorithms let's formalize now what
438:31 own algorithms let's formalize now what this kind of analysis is so right right
438:33 this kind of analysis is so right right now I claim binary search better than
438:35 now I claim binary search better than linear search but how much better and
438:37 linear search but how much better and why exactly well it all comes back to
438:39 why exactly well it all comes back to this kind of graph so this recall is how
438:40 this kind of graph so this recall is how we analyzed the phone book back in week
438:43 we analyzed the phone book back in week zero and recall that indeed we had these
438:45 zero and recall that indeed we had these these formulas rough formulas that
438:47 these formulas rough formulas that describe the running time of those three
438:49 describe the running time of those three algorithms one page at a time two pages
438:51 algorithms one page at a time two pages at a time and then tearing the thing
438:53 at a time and then tearing the thing again and again in half and precisely if
438:55 again and again in half and precisely if you count it up the number of pages I
438:58 you count it up the number of pages I was touching or the number of pages I
438:59 was touching or the number of pages I was tearing it's fair to say that the
439:01 was tearing it's fair to say that the first algorithm in the worst case might
439:03 first algorithm in the worst case might have taken n total Pages it didn't
439:06 have taken n total Pages it didn't because I was searching for John Harvard
439:07 because I was searching for John Harvard at the time which is somewhat early in
439:09 at the time which is somewhat early in the alphabet but if I were searching
439:11 the alphabet but if I were searching with some for someone with the last name
439:12 with some for someone with the last name of Z I would have had to keep going and
439:14 of Z I would have had to keep going and going in the worst case through all n
439:16 going in the worst case through all n Pages not as bad for the second
439:18 Pages not as bad for the second algorithm and that's why we do n divided
439:20 algorithm and that's why we do n divided by two and even that's a bit of a white
439:22 by two and even that's a bit of a white lie right it's probably n / 2 + 1 in
439:25 lie right it's probably n / 2 + 1 in case I have to double back but again I'm
439:27 case I have to double back but again I'm sort of doing this more generally to
439:29 sort of doing this more generally to capture the essence of these things and
439:31 capture the essence of these things and then we really got into the weeds with
439:32 then we really got into the weeds with like log base 2 of n for that third and
439:34 like log base 2 of n for that third and final algorithm and at the time we
439:36 final algorithm and at the time we claimed anytime you're doing dividing
439:38 claimed anytime you're doing dividing something in half in half in half odds
439:41 something in half in half in half odds are there's going to be some kind of
439:42 are there's going to be some kind of logarithm involved and we'll see that
439:43 logarithm involved and we'll see that today but today we're going to actually
439:45 today but today we're going to actually start using computer science terminology
439:47 start using computer science terminology and we're going to sort of formalize
439:49 and we're going to sort of formalize this imprecision if you will we are not
439:52 this imprecision if you will we are not going to care generally about exactly
439:55 going to care generally about exactly how many steps some algorithm takes
439:57 how many steps some algorithm takes because that's not going to be that
439:59 because that's not going to be that enlightening especially if maybe you
440:01 enlightening especially if maybe you have a faster computer tomorrow than you
440:02 have a faster computer tomorrow than you did today it wouldn't really be fair to
440:04 did today it wouldn't really be fair to compare numbers too precisely we really
440:06 compare numbers too precisely we really kind of want to with a wave of the hand
440:08 kind of want to with a wave of the hand just get a sense of roughly how slow or
440:11 just get a sense of roughly how slow or how fast an algorithm is so the notation
440:13 how fast an algorithm is so the notation here is deliberate that is literally a
440:15 here is deliberate that is literally a capital O often italicized referred to
440:18 capital O often italicized referred to as Big O and so the first algorithm is
440:21 as Big O and so the first algorithm is in Big O of n the second algorithm is in
440:24 in Big O of n the second algorithm is in Big O of n / two the third algorithm is
440:27 Big O of n / two the third algorithm is in Big O of log base 2 of n but even
440:30 in Big O of log base 2 of n but even that is kind of unnecessary detail when
440:33 that is kind of unnecessary detail when using Big O notation you really don't
440:36 using Big O notation you really don't care about we'll see the smaller ordered
440:38 care about we'll see the smaller ordered terms right we're not going to care
440:40 terms right we're not going to care about the divided by two because you
440:42 about the divided by two because you know what the shape of these algorithms
440:45 know what the shape of these algorithms is almost the same and really the idea
440:47 is almost the same and really the idea the algorithm itself is sort of
440:49 the algorithm itself is sort of fundamentally the same okay and instead
440:50 fundamentally the same okay and instead of one page at a time I'm doing two but
440:52 of one page at a time I'm doing two but if you throw millions of pages billions
440:54 if you throw millions of pages billions of pages at me those algorithms are
440:57 of pages at me those algorithms are really going to kind of perform the same
440:58 really going to kind of perform the same as n gets really large goes off toward
441:00 as n gets really large goes off toward infinity and the same is true for
441:01 infinity and the same is true for logarithms even if you're a little rusty
441:03 logarithms even if you're a little rusty it turns out that whether you do the
441:05 it turns out that whether you do the math with log base 2 log base 3 log base
441:07 math with log base 2 log base 3 log base 10 you can just multiply one by the
441:10 10 you can just multiply one by the other to really get the same formula
441:11 other to really get the same formula this is only to say a computer scientist
441:13 this is only to say a computer scientist would generally say that the first two
441:15 would generally say that the first two algorithms are on the order of eps the
441:18 algorithms are on the order of eps the third algorithm is on the order of log
441:20 third algorithm is on the order of log end steps and we don't really care
441:23 end steps and we don't really care precisely what we mean beyond that and
441:25 precisely what we mean beyond that and this Big O notation as we'll see and
441:27 this Big O notation as we'll see and actually let me let me zoom out if you
441:28 actually let me let me zoom out if you can imagine suddenly making the x- axis
441:31 can imagine suddenly making the x- axis much longer so more pages on the screen
441:33 much longer so more pages on the screen at once it is indeed going to be the
441:35 at once it is indeed going to be the shapes of these curves that matter
441:37 shapes of these curves that matter because imagine in your mind's eye as
441:39 because imagine in your mind's eye as you zoom out zoom out zoom out zoom out
441:41 you zoom out zoom out zoom out zoom out and as n gets much much much bigger on
441:43 and as n gets much much much bigger on the xaxis the red and the yellow line
441:46 the xaxis the red and the yellow line are essentially going to look the same
441:48 are essentially going to look the same once n is sufficiently large but the
441:50 once n is sufficiently large but the green line is never going to look the
441:53 green line is never going to look the same it's going to be a fundamentally
441:54 same it's going to be a fundamentally different shape and so that's the
441:55 different shape and so that's the intuition of bigo to get a sense of
441:58 intuition of bigo to get a sense of these uh rates of performance like this
442:02 these uh rates of performance like this so here then is Big O here is perhaps a
442:04 so here then is Big O here is perhaps a cheat sheet of like the common formulas
442:06 cheat sheet of like the common formulas that a computer scientist certainly in
442:08 that a computer scientist certainly in an introductory context might use when
442:10 an introductory context might use when analyzing algorithms and let's consider
442:11 analyzing algorithms and let's consider for a moment which of our first two
442:14 for a moment which of our first two algorithms linear search and binary
442:15 algorithms linear search and binary search fall into these categories so
442:17 search fall into these categories so I've ordered them from sort of slowest
442:20 I've ordered them from sort of slowest to fastest so order of n s it's not
442:23 to fastest so order of n s it's not something we've actually seen yet but it
442:25 something we've actually seen yet but it tends to be slow because it's quadratic
442:27 tends to be slow because it's quadratic you're doing n * n that's got to add up
442:29 you're doing n * n that's got to add up to a lot of steps better today is going
442:30 to a lot of steps better today is going to be n Lin even better is going to be n
442:33 to be n Lin even better is going to be n even better than that is login and best
442:35 even better than that is login and best is soall order of one like one step or
442:39 is soall order of one like one step or maybe two steps maybe even a thousand
442:41 maybe two steps maybe even a thousand steps but a fixed finite number of steps
442:43 steps but a fixed finite number of steps that never changes no how matter how big
442:46 that never changes no how matter how big n is so given this chart just to be
442:48 n is so given this chart just to be clear linear search let's consider the
442:51 clear linear search let's consider the worst case in the worst case how many
442:54 worst case in the worst case how many steps did it take someone like Stephanie
442:57 steps did it take someone like Stephanie to find the uh solution to the problem
442:59 to find the uh solution to the problem assuming not seven doors but n doors
443:03 assuming not seven doors but n doors yeah so on the order of N and in this
443:05 yeah so on the order of N and in this case it's exactly n but you know what
443:08 case it's exactly n but you know what you know maybe it's arguably too n right
443:10 you know maybe it's arguably too n right because it took Stephanie a couple of
443:11 because it took Stephanie a couple of steps like she had to lift the latch she
443:13 steps like she had to lift the latch she had to open the door maybe it's three
443:14 had to open the door maybe it's three steps you had to show the money so now
443:16 steps you had to show the money so now it's 3n 2 N but there we don't really
443:19 it's 3n 2 N but there we don't really care about that level of precision we
443:21 care about that level of precision we really just care about the fundamental
443:23 really just care about the fundamental number of operations so we'll say yes on
443:25 number of operations so we'll say yes on the order of n so that might be an upper
443:27 the order of n so that might be an upper bound we'll call this for linear search
443:30 bound we'll call this for linear search and how about binary search in Jackson's
443:32 and how about binary search in Jackson's case or in general me and week zero if
443:34 case or in general me and week zero if there's end doors how many steps did it
443:37 there's end doors how many steps did it take Jackson or me using binary
443:40 take Jackson or me using binary search in this case it was literally
443:43 search in this case it was literally three but that's not a
443:45 three but that's not a formula yeah so it's on the order of log
443:48 formula yeah so it's on the order of log n and indeed if there's seven doors well
443:50 n and indeed if there's seven doors well that's almost eight if you just do a
443:52 that's almost eight if you just do a little bit of rounding and indeed if you
443:53 little bit of rounding and indeed if you take log base 2 of eight okay so that
443:55 take log base 2 of eight okay so that does actually give us three so the math
443:57 does actually give us three so the math actually checks out and if you're not
443:58 actually checks out and if you're not comy with logarithms no big deal just
444:00 comy with logarithms no big deal just think about it intuitively uh logarithm
444:03 think about it intuitively uh logarithm of base two is just dividing something
444:04 of base two is just dividing something again and again so on this chart when we
444:07 again and again so on this chart when we consider Big O which to be clear allows
444:10 consider Big O which to be clear allows you to describe the order of an
444:12 you to describe the order of an algorithm's running time like the
444:14 algorithm's running time like the magnitude of it but it also describes
444:16 magnitude of it but it also describes more specifically an upper bound so in
444:19 more specifically an upper bound so in the worst case for instance these are
444:22 the worst case for instance these are pretty good measures of how good uh or
444:24 pretty good measures of how good uh or rather of how bad linear search and
444:27 rather of how bad linear search and binary search might be why well suppose
444:29 binary search might be why well suppose you're searching a thousand page phone
444:30 you're searching a thousand page phone book and the person's name starts with z
444:32 book and the person's name starts with z the algorithm is still going to be on
444:34 the algorithm is still going to be on the order of n steps why because it
444:36 the order of n steps why because it might take you as many as all end steps
444:38 might take you as many as all end steps to find it now that's not necessarily
444:42 to find it now that's not necessarily going to be the case in practice if I
444:44 going to be the case in practice if I use Big O as an upper bound well it
444:47 use Big O as an upper bound well it would be nice if there's sort of a
444:48 would be nice if there's sort of a corresponding lower bound especially if
444:50 corresponding lower bound especially if you want to consider not just best uh
444:52 you want to consider not just best uh worst cases but maybe best cases so what
444:55 worst cases but maybe best cases so what might we use here well so this is a
444:57 might we use here well so this is a capital Greek omega symbol so Omega is
445:00 capital Greek omega symbol so Omega is the symbol that a computer scientist
445:01 the symbol that a computer scientist uses generally to describe a lower bound
445:03 uses generally to describe a lower bound on an algorithm often in the context of
445:05 on an algorithm often in the context of best case though not necessarily so a
445:08 best case though not necessarily so a lower bound means how few steps might an
445:10 lower bound means how few steps might an algorithm take and here too same
445:12 algorithm take and here too same formulas and we'll fill in these blanks
445:14 formulas and we'll fill in these blanks over time some algorithms might always
445:16 over time some algorithms might always take a minimum of n squ steps or on the
445:19 take a minimum of n squ steps or on the order of n steps some might only take n
445:21 order of n steps some might only take n logn or n or log n or one so something
445:25 logn or n or log n or one so something like uh linear search when Stephanie
445:28 like uh linear search when Stephanie started with linear search she didn't
445:30 started with linear search she didn't get lucky this time on stage but what if
445:32 get lucky this time on stage but what if she had and the first door she opened
445:34 she had and the first door she opened were 50 how much you then describe the
445:38 were 50 how much you then describe the lower bound on linear search in this
445:41 lower bound on linear search in this so-called best
445:43 so-called best case using this list of possible
445:46 case using this list of possible answers yeah yeah so Omega of one so in
445:50 answers yeah yeah so Omega of one so in the best case the lower bound on how
445:53 the best case the lower bound on how many steps it might take uh linear
445:55 many steps it might take uh linear search to find something might just be
445:57 search to find something might just be one step why because maybe Stephanie had
445:59 one step why because maybe Stephanie had gotten lucky and we had pre-filled these
446:02 gotten lucky and we had pre-filled these lockers with the numbers in some other
446:04 lockers with the numbers in some other order such that she might have open the
446:06 order such that she might have open the first locker and waila the number 50
446:08 first locker and waila the number 50 could have been there so a lower bound
446:09 could have been there so a lower bound arguably could indeed be Omega of one
446:12 arguably could indeed be Omega of one for linear search and how about now for
446:14 for linear search and how about now for Jackson he used binary search so he
446:16 Jackson he used binary search so he dived right into the middle of the
446:18 dived right into the middle of the problem but what would be a lower bound
446:21 problem but what would be a lower bound on binary search using this logic yeah
446:25 on binary search using this logic yeah yeah so again Omega of one why because
446:27 yeah so again Omega of one why because maybe he just gets lucky and indeed
446:29 maybe he just gets lucky and indeed right in the middle of the lockers could
446:30 right in the middle of the lockers could have been the number 50 it wasn't and so
446:33 have been the number 50 it wasn't and so more Germain in Jackson's uh actual
446:36 more Germain in Jackson's uh actual practice would have been the Big O
446:38 practice would have been the Big O discussion but Big O and Omega upper
446:41 discussion but Big O and Omega upper bound and lower bound just allow a
446:42 bound and lower bound just allow a computer scientist to kind of wrestle
446:44 computer scientist to kind of wrestle with what could happen maybe in the
446:45 with what could happen maybe in the worst case what can happen in the best
446:47 worst case what can happen in the best case and you can even get even more
446:48 case and you can even get even more precise like the average case or the
446:50 precise like the average case or the like and this is indeed what Engineers
446:52 like and this is indeed what Engineers might do at a whiteboard in a company in
446:54 might do at a whiteboard in a company in a university when designing an algorithm
446:56 a university when designing an algorithm and trying to make arguments as to like
446:58 and trying to make arguments as to like why their algorithm is better than
446:59 why their algorithm is better than someone else's by way of these kinds of
447:02 someone else's by way of these kinds of analyses and just so you've seen it it
447:04 analyses and just so you've seen it it turns out that if some algorithm happens
447:07 turns out that if some algorithm happens to have an identical upper bound and
447:10 to have an identical upper bound and lower bound you can actually use a
447:13 lower bound you can actually use a capital Greek Theta as well and this is
447:14 capital Greek Theta as well and this is the last of the the Greek symbols today
447:17 the last of the the Greek symbols today but a Greek Theta indicates a coinci
447:19 but a Greek Theta indicates a coinci idents of both upper bound and lower
447:21 idents of both upper bound and lower bound that is they are one and the same
447:24 bound that is they are one and the same that was not the case for our discussion
447:25 that was not the case for our discussion a second ago of linear search not the
447:26 a second ago of linear search not the case for binary search but you could use
447:29 case for binary search but you could use the same kinds of formulas if it turns
447:32 the same kinds of formulas if it turns out that your upper bound and lower
447:33 out that your upper bound and lower bound are the same so for instance if I
447:36 bound are the same so for instance if I were to count everyone like literally in
447:38 were to count everyone like literally in this room 1 2 3 4 five six and so forth
447:42 this room 1 2 3 4 five six and so forth you could actually say that counting in
447:44 you could actually say that counting in that way is in Theta of n right because
447:48 that way is in Theta of n right because I in the best case it's going to take me
447:50 I in the best case it's going to take me end points at the uh people in the
447:52 end points at the uh people in the audience in the worst case it's going to
447:53 audience in the worst case it's going to take me n it's always going to take me
447:55 take me n it's always going to take me end steps if I want to count everyone in
447:56 end steps if I want to count everyone in the room you can't really do better than
447:58 the room you can't really do better than that unless you skip people so that
448:00 that unless you skip people so that would be an example off the cuff of
448:02 would be an example off the cuff of something where Theta is instead germine
448:05 something where Theta is instead germine are any questions now on Big O on Omega
448:09 are any questions now on Big O on Omega or Theta which are now just more formal
448:11 or Theta which are now just more formal tools in the toolkit for talking about
448:14 tools in the toolkit for talking about the design of our
448:16 the design of our algorithms any
448:18 algorithms any questions
448:21 questions no seeing none yeah oh is this yes no
448:25 no seeing none yeah oh is this yes no okay so we're good so let's go ahead and
448:28 okay so we're good so let's go ahead and translate this perhaps to some some
448:30 translate this perhaps to some some actual code let me go over to vs code
448:32 actual code let me go over to vs code here and let's see if we can't now
448:34 here and let's see if we can't now translate some of these ideas to some
448:36 translate some of these ideas to some actual code not so much using new syntax
448:38 actual code not so much using new syntax yet we're going to still operate in this
448:40 yet we're going to still operate in this world of arrays like last week so let me
448:42 world of arrays like last week so let me go ahead and create a program called
448:43 go ahead and create a program called search. C by executing code space
448:46 search. C by executing code space search. c in my terminal and then up
448:48 search. c in my terminal and then up here let's go ahead and include our
448:50 here let's go ahead and include our usual so include cs50.h so I can get
448:52 usual so include cs50.h so I can get some input include standard i.h so I can
448:56 some input include standard i.h so I can print some output we'll do int main void
448:59 print some output we'll do int main void which the meaning of which we did start
449:00 which the meaning of which we did start to tease apart last week the fact that
449:02 to tease apart last week the fact that it's void again today just means no
449:04 it's void again today just means no command line arguments and let me go
449:06 command line arguments and let me go ahead and do this let me go ahead and
449:08 ahead and do this let me go ahead and declare just for discussion's sake a
449:11 declare just for discussion's sake a static array like an array that never
449:13 static array like an array that never changes and the Syntax for this is going
449:14 changes and the Syntax for this is going to be give me an array called numbers
449:18 to be give me an array called numbers using the square bracket not ation and
449:20 using the square bracket not ation and I'm going to immediately initialize it
449:22 I'm going to immediately initialize it to 20 500 10 5 100 1 and 50 reminiscent
449:26 to 20 500 10 5 100 1 and 50 reminiscent of those same denominations as before so
449:29 of those same denominations as before so this is a slightly new syntax that we've
449:31 this is a slightly new syntax that we've uh perhaps not seen and the curly braces
449:34 uh perhaps not seen and the curly braces here which are different from for loops
449:35 here which are different from for loops and while loops and functions just tell
449:38 and while loops and functions just tell the compiler please give me an array of
449:40 the compiler please give me an array of whatever size this is containing those
449:42 whatever size this is containing those numbers left to right I could
449:44 numbers left to right I could alternatively use last week's syntax of
449:47 alternatively use last week's syntax of saying something like this let's see one
449:49 saying something like this let's see one 2 3 4 5 six seven denominations I could
449:52 2 3 4 5 six seven denominations I could alternatively do this and then I could
449:55 alternatively do this and then I could say numbers bracket 0 uh numbers bracket
449:59 say numbers bracket 0 uh numbers bracket 0 equals 20 numbers bracket 1 equal 500
450:04 0 equals 20 numbers bracket 1 equal 500 and I could do this five more times
450:06 and I could do this five more times that's just a little tedious if you know
450:08 that's just a little tedious if you know the numbers in advance you don't have to
450:09 the numbers in advance you don't have to tell the compiler how many there are you
450:11 tell the compiler how many there are you can just let it figure it out that your
450:13 can just let it figure it out that your numbers are be will be 20 500 10 5 100 1
450:17 numbers are be will be 20 500 10 5 100 1 and 5050 so this is how you statically
450:20 and 5050 so this is how you statically Define an array all right let me just go
450:22 Define an array all right let me just go ahead and ask the user now for a number
450:24 ahead and ask the user now for a number we'll call it n by using get int and
450:26 we'll call it n by using get int and prompting them for a number so nothing
450:28 prompting them for a number so nothing new there and now let me go ahead and
450:30 new there and now let me go ahead and Implement linear search and the pseudo
450:33 Implement linear search and the pseudo code we had for this before used some
450:34 code we had for this before used some array like notation let me go ahead then
450:37 array like notation let me go ahead then and start similarly four in I and it's
450:40 and start similarly four in I and it's you almost always start counting at I by
450:42 you almost always start counting at I by convention so that's perhaps a good
450:44 convention so that's perhaps a good starting point I'm going to do this so
450:46 starting point I'm going to do this so long as I is less than seven not the
450:48 long as I is less than seven not the best design to hard code the seven but
450:50 best design to hard code the seven but this is just for demonstration sake for
450:52 this is just for demonstration sake for now because I know how many numbers I
450:53 now because I know how many numbers I put in there and then I'm going to do
450:55 put in there and then I'm going to do i++ so now I have the beginnings of a
450:57 i++ so now I have the beginnings of a loop that will just allow me to iterate
450:58 loop that will just allow me to iterate over the entire array and let me ask
451:01 over the entire array and let me ask this if the current number at location I
451:05 this if the current number at location I equals equals n which is the number the
451:08 equals equals n which is the number the human typed in then let's go ahead and
451:10 human typed in then let's go ahead and do something simple like print F quote
451:12 do something simple like print F quote unquote found back sln and then per our
451:16 unquote found back sln and then per our discussion last week to indicate that
451:18 discussion last week to indicate that this is successful I'm going to going to
451:19 this is successful I'm going to going to return zero if I found it and if I don't
451:22 return zero if I found it and if I don't find it I'm just going to go down here
451:24 find it I'm just going to go down here and by default say not found back sln
451:27 and by default say not found back sln and just for convention whoops just for
451:29 and just for convention whoops just for good measure per convention I'll return
451:32 good measure per convention I'll return one or really any value other than zero
451:34 one or really any value other than zero zero recall means success and any other
451:36 zero recall means success and any other integer tends to mean error of some sort
451:39 integer tends to mean error of some sort irrespective of the number I'm looking
451:41 irrespective of the number I'm looking for so just to revisit the only thing
451:44 for so just to revisit the only thing that's new here is the syntax for
451:45 that's new here is the syntax for creating an array of seven numbers these
451:48 creating an array of seven numbers these numbers and then after after that we
451:50 numbers and then after after that we have really highlighted here an
451:52 have really highlighted here an implementation of linear search I mean
451:55 implementation of linear search I mean this is the C version I dare say of what
451:56 this is the C version I dare say of what Stephanie did on the board whereas now
451:59 Stephanie did on the board whereas now the array is called numbers instead of
452:00 the array is called numbers instead of doors but I think it's pretty much the
452:03 doors but I think it's pretty much the same let me go ahead and open my
452:05 same let me go ahead and open my terminal window and run make
452:08 terminal window and run make search seems to compile okay search and
452:11 search seems to compile okay search and let's go ahead and search for a number
452:13 let's go ahead and search for a number we'll start with what we did before 50
452:15 we'll start with what we did before 50 and it's found let's go ahead and run it
452:17 and it's found let's go ahead and run it again/ search let's search for maybe 20
452:20 again/ search let's search for maybe 20 at the beginning that one too is found
452:22 at the beginning that one too is found let's run it one more time searching for
452:24 let's run it one more time searching for like 1,000 which is not uh in among the
452:28 like 1,000 which is not uh in among the uh denominations and that one indeed is
452:31 uh denominations and that one indeed is not found so we've taken an idea from
452:33 not found so we've taken an idea from week zero now formalized in week three
452:35 week zero now formalized in week three and just translated it now to code
452:38 and just translated it now to code questions on this implementation of
452:40 questions on this implementation of linear
452:43 linear search linear
452:45 search linear search nothing oh so successful so far
452:49 search nothing oh so successful so far today okay so let's see if we can't
452:52 today okay so let's see if we can't maybe make this a little more
452:53 maybe make this a little more interesting and see if we can't trip
452:55 interesting and see if we can't trip over a detail that's going to be
452:57 over a detail that's going to be important in C instead of doing numbers
453:00 important in C instead of doing numbers let me go ahead and do this we'll stay
453:02 let me go ahead and do this we'll stay on theme with Monopoly and I went down
453:04 on theme with Monopoly and I went down the rabbit hole of reading the
453:05 the rabbit hole of reading the wikkipedia article on Monopoly and the
453:06 wikkipedia article on Monopoly and the original uh pieces or tokens that came
453:09 original uh pieces or tokens that came with Monopoly and it turns out we can
453:11 with Monopoly and it turns out we can represent those with strings so I'm
453:12 represent those with strings so I'm going to create an array called strings
453:14 going to create an array called strings plural of whatever size I defined here
453:18 plural of whatever size I defined here and the very first first Monopoly pieces
453:20 and the very first first Monopoly pieces back in the day were a battleship that
453:22 back in the day were a battleship that you could play with a boot a cannon an
453:27 you could play with a boot a cannon an iron a
453:29 iron a thimble and a top hat some of which you
453:32 thimble and a top hat some of which you might know from the game nowadays turns
453:33 might know from the game nowadays turns out they've been changing these uh had
453:35 out they've been changing these uh had no idea over the years so here is now an
453:37 no idea over the years so here is now an array of strings let me go ahead and
453:39 array of strings let me go ahead and prompt the user now not for an integer
453:42 prompt the user now not for an integer anymore I want to Now search for one of
453:44 anymore I want to Now search for one of these strings still using linear search
453:46 these strings still using linear search so let me create a string s set it equal
453:48 so let me create a string s set it equal to string prompt the user for a string
453:51 to string prompt the user for a string to search for and then I think my code
453:54 to search for and then I think my code here is almost the same except for one
453:57 here is almost the same except for one detail I now have an array called
453:59 detail I now have an array called strings I now have a variable called s
454:03 strings I now have a variable called s but it turns out for reasons we'll
454:05 but it turns out for reasons we'll explore in more detail next week this
454:07 explore in more detail next week this line of code is not going to work and it
454:10 line of code is not going to work and it turns out the reason has to do with what
454:13 turns out the reason has to do with what we discussed last week of like what a
454:14 we discussed last week of like what a string really is and what is a string
454:17 string really is and what is a string again a string is an array and it turns
454:20 again a string is an array and it turns out though that equals equals is not
454:23 out though that equals equals is not going to generously compare all of the
454:26 going to generously compare all of the characters in an array for you just
454:29 characters in an array for you just because you use equal equals it turns
454:31 because you use equal equals it turns out it's not going to compare every
454:33 out it's not going to compare every letter and so thankfully there is in the
454:37 letter and so thankfully there is in the uh string library that we introduced
454:39 uh string library that we introduced last week a solution to this problem the
454:42 last week a solution to this problem the reason for the problem we'll explore in
454:43 reason for the problem we'll explore in more detail next week but for now just
454:45 more detail next week but for now just know that when you want to compare
454:47 know that when you want to compare strings in C especially if you've come
454:49 strings in C especially if you've come into the class knowing a bit of Java or
454:51 into the class knowing a bit of Java or python or some other language you cannot
454:53 python or some other language you cannot use equals equals even though you could
454:55 use equals equals even though you could in scratch you cannot in C so what I
454:58 in scratch you cannot in C so what I have to actually do here is this I have
455:00 have to actually do here is this I have to ask the question does the return
455:03 to ask the question does the return value of a function called stir compare
455:05 value of a function called stir compare or stir
455:07 or stir comp equal zero when passed in the
455:10 comp equal zero when passed in the current string and that user input so if
455:15 current string and that user input so if you read the documentation for this
455:17 you read the documentation for this function called stir compare you'll see
455:19 function called stir compare you'll see that it takes two strings as input first
455:22 that it takes two strings as input first one and second one it then someone
455:24 one and second one it then someone decades ago wrote the code that probably
455:26 decades ago wrote the code that probably uses a four Loop or a while loop to
455:28 uses a four Loop or a while loop to compare every character in each of those
455:30 compare every character in each of those strings and it turns out it returns zero
455:33 strings and it turns out it returns zero if they are in fact equal turns out too
455:36 if they are in fact equal turns out too it will return a positive number or a
455:38 it will return a positive number or a negative number in other situations any
455:41 negative number in other situations any intuition for why it might actually be
455:43 intuition for why it might actually be useful to have a function that allows
455:46 useful to have a function that allows you to check if two strings are equal
455:49 you to check if two strings are equal if they're not equal what else might be
455:51 if they're not equal what else might be interesting to know when comparing two
456:03 are okay possibly maybe you want to know just how similar they are um and that's
456:06 just how similar they are um and that's indeed an algorithm unto itself but stir
456:07 indeed an algorithm unto itself but stir compare is a little simpler than
456:16 that exactly if you're trying to like alphabetize a whole list of strings just
456:18 alphabetize a whole list of strings just like your phone probably is for your
456:19 like your phone probably is for your context or address book it turns out
456:21 context or address book it turns out that stir compare will actually return a
456:23 that stir compare will actually return a positive number or a negative number or
456:26 positive number or a negative number or a zero based on whether maybe it comes
456:28 a zero based on whether maybe it comes alphabetically first or later or or in
456:31 alphabetically first or later or or in fact equal so that can be a useful thing
456:33 fact equal so that can be a useful thing and that's just a teaser for a lower
456:36 and that's just a teaser for a lower level explanation that we'll see next
456:37 level explanation that we'll see next week so now let me cross my fingers and
456:39 week so now let me cross my fingers and see if I got this right let me go ahead
456:41 see if I got this right let me go ahead and do make
456:43 and do make search did compile okay albeit slowly
456:47 search did compile okay albeit slowly do/ search and let's search for
456:49 do/ search and let's search for something like the thimble and we see
456:51 something like the thimble and we see that that's indeed found otherwise let's
456:53 that that's indeed found otherwise let's search for something that I know isn't
456:55 search for something that I know isn't there like a race car which was there
456:56 there like a race car which was there when I grew up but huh segmentation
457:00 when I grew up but huh segmentation fault core dumped like and actually some
457:02 fault core dumped like and actually some of you have tripped over this error
457:04 of you have tripped over this error before anyone want to admit seeing this
457:06 before anyone want to admit seeing this so yeah not something we've talked about
457:08 so yeah not something we've talked about and um honestly not something I intended
457:10 and um honestly not something I intended just now but that too we'll see next
457:12 just now but that too we'll see next week any intuition for why my program
457:15 week any intuition for why my program just
457:17 just broke I didn't really change the logic
457:20 broke I didn't really change the logic it's still linear search let me hide the
457:23 it's still linear search let me hide the terminal so you can see all of the code
457:24 terminal so you can see all of the code at once the only thing I did was
457:26 at once the only thing I did was switched from integers to Strings and I
457:28 switched from integers to Strings and I switched to stir compare here but
457:31 switched to stir compare here but segmentation fault happened and the
457:33 segmentation fault happened and the teaser is that that somehow relates to
457:35 teaser is that that somehow relates to the computer's memory
457:42 yeah yeah and this is subtle but spot on so 1 2 3 4 5 six elements total in this
457:46 so 1 2 3 4 5 six elements total in this array versus the seven number
457:49 array versus the seven number of Monopoly denominations that we had
457:51 of Monopoly denominations that we had earlier and this is where see sort of
457:53 earlier and this is where see sort of case in point this came back to bite me
457:54 case in point this came back to bite me the fact that I hardcoded this value as
457:57 the fact that I hardcoded this value as to opposed to maybe separating it out as
457:58 to opposed to maybe separating it out as a constant or declaring it higher up
458:00 a constant or declaring it higher up kind of bit me here because now I'm
458:02 kind of bit me here because now I'm iterating over an array of size six but
458:05 iterating over an array of size six but clearly I'm going one step too far
458:07 clearly I'm going one step too far because I'm literally going to iterate
458:09 because I'm literally going to iterate seven times not six so it's as though
458:12 seven times not six so it's as though I'm looking at memory that's over here
458:14 I'm looking at memory that's over here and indeed next week we'll focus on
458:16 and indeed next week we'll focus on memory and that's just a bad thing so
458:17 memory and that's just a bad thing so odds are not even and seeing your code
458:19 odds are not even and seeing your code from this past week if any of you have
458:21 from this past week if any of you have had segmentation faults odds are you
458:23 had segmentation faults odds are you touched memory that you shouldn't have
458:26 touched memory that you shouldn't have you maybe looped too many times you
458:28 you maybe looped too many times you might have uh used a negative number to
458:30 might have uh used a negative number to get into your array in general you
458:32 get into your array in general you touched memory that you shouldn't have
458:34 touched memory that you shouldn't have and you touched a segment of memory that
458:35 and you touched a segment of memory that you shouldn't have the fix though at
458:37 you shouldn't have the fix though at least in my case is simple just don't do
458:39 least in my case is simple just don't do that so let me go ahead and recompile
458:41 that so let me go ahead and recompile this make uh
458:43 this make uh search do/ search and I'll search again
458:46 search do/ search and I'll search again for uh race car and
458:49 for uh race car and and now it does not crash but it does
458:51 and now it does not crash but it does tell me it's not found so subtle but
458:53 tell me it's not found so subtle but something you might yourself have
458:54 something you might yourself have tripped over already questions then on
458:57 tripped over already questions then on what I just did intentionally or
459:01 what I just did intentionally or otherwise yeah in
459:10 front return don't return Z return so what
459:11 what the a really good question so the
459:13 the a really good question so the program will still work even if I don't
459:15 program will still work even if I don't return zero or return one in fact let me
459:18 return zero or return one in fact let me go ahead head and do that and just hide
459:21 go ahead head and do that and just hide my terminal window for a second let's
459:23 my terminal window for a second let's get rid of the return here let's get rid
459:25 get rid of the return here let's get rid of the return here however Watch What
459:28 of the return here however Watch What Happens here uh let me go ahead and
459:30 Happens here uh let me go ahead and recompile this make search Let Me scroll
459:33 recompile this make search Let Me scroll up in my code here let me go ahead and
459:35 up in my code here let me go ahead and do do/ search and let me go ahead and
459:37 do do/ search and let me go ahead and search for the first thing in the list
459:39 search for the first thing in the list Battleship so I know that this should be
459:41 Battleship so I know that this should be found I hit
459:42 found I hit enter huh interesting so it's saying
459:45 enter huh interesting so it's saying found not found but do you see why
459:46 found not found but do you see why logically in this case
459:54 exactly so the loop is still running so there's a couple of solutions to this I
459:56 there's a couple of solutions to this I could for instance somehow break out of
459:58 could for instance somehow break out of the code here but that's going to still
460:00 the code here but that's going to still result in line 18 executing I could then
460:03 result in line 18 executing I could then instead just return here I don't
460:05 instead just return here I don't strictly need to return one down at the
460:07 strictly need to return one down at the bottom but I made this claim last week
460:09 bottom but I made this claim last week that it tends to be helpful as your
460:11 that it tends to be helpful as your programs get more sophisticated to at
460:13 programs get more sophisticated to at least signify just like a real world
460:15 least signify just like a real world programmer error codes when something
460:17 programmer error codes when something goes wrong so returning zero in main is
460:20 goes wrong so returning zero in main is the easiest way to signify my code is
460:22 the easiest way to signify my code is done I'm ready to exit successfully
460:24 done I'm ready to exit successfully that's it but down here I could
460:26 that's it but down here I could absolutely still return zero because
460:28 absolutely still return zero because it's not a huge deal it's not really an
460:29 it's not a huge deal it's not really an error that deserves annoying the user
460:31 error that deserves annoying the user with some kind of popup that something
460:33 with some kind of popup that something went wrong but return one is just a
460:35 went wrong but return one is just a lower level way of signaling H it didn't
460:37 lower level way of signaling H it didn't really find what I was looking for and
460:39 really find what I was looking for and remember from last week you can see this
460:41 remember from last week you can see this as follows if I recompile this again now
460:44 as follows if I recompile this again now that I've reverted those changes so make
460:46 that I've reverted those changes so make search and if I do uh /search and search
460:50 search and if I do uh /search and search for Battleship which is indeed found
460:53 for Battleship which is indeed found recall I can execute this magical
460:55 recall I can execute this magical command Echo dollar sign question mark
460:57 command Echo dollar sign question mark which you're not going to often execute
460:58 which you're not going to often execute but it shows you what main returned if I
461:02 but it shows you what main returned if I run search again and search for race car
461:05 run search again and search for race car which is not found I see not found but I
461:08 which is not found I see not found but I can also run this command again and see
461:09 can also run this command again and see that oh it returned one so now if you
461:12 that oh it returned one so now if you fast forward a few months a few years
461:13 fast forward a few months a few years when you're actually writing code in a
461:15 when you're actually writing code in a company or for larger projects you might
461:17 company or for larger projects you might want to be automating software you might
461:19 want to be automating software you might not want the human to necessarily be
461:20 not want the human to necessarily be running it manually you might want um
461:23 running it manually you might want um code to be automated by some nightly
461:25 code to be automated by some nightly process or something like that using
461:27 process or something like that using these exit codes can a program determine
461:30 these exit codes can a program determine yes or no that other code succeeded or
461:33 yes or no that other code succeeded or failed other questions on linear search
461:38 failed other questions on linear search in this
461:39 in this way no all right well let's translate
461:42 way no all right well let's translate this to one other feature of uh C here
461:47 this to one other feature of uh C here by incorporating these ideas now into
461:49 by incorporating these ideas now into one other program so I'm going to create
461:51 one other program so I'm going to create a phone book in C by doing code space
461:54 a phone book in C by doing code space phone
462:00 book.car a phone book for an actual name and getting back a number so I'm going
462:02 and getting back a number so I'm going to go ahead and quickly include some of
462:03 to go ahead and quickly include some of the same things cs50.h so we can get
462:05 the same things cs50.h so we can get input uh standard io. so we can print
462:09 input uh standard io. so we can print output and I'm going to preemptively
462:10 output and I'm going to preemptively include string.h in case we need that
462:12 include string.h in case we need that one as well uh int main void no need for
462:16 one as well uh int main void no need for uh command line arguments today and let
462:18 uh command line arguments today and let me me give myself now an array of names
462:20 me me give myself now an array of names for this phone book so string names
462:23 for this phone book so string names equals and then in curly braces how
462:25 equals and then in curly braces how about Carter will be one person in the
462:27 about Carter will be one person in the phone book and David myself will be the
462:29 phone book and David myself will be the other so we'll keep it short so we don't
462:30 other so we'll keep it short so we don't have to type too many names but this is
462:32 have to type too many names but this is a phone book with two people thus far
462:34 a phone book with two people thus far suppose now we want to also store
462:36 suppose now we want to also store Carter's phone number in mind so it's
462:38 Carter's phone number in mind so it's not just saying found or not found it's
462:40 not just saying found or not found it's literally looking up our phone numbers
462:42 literally looking up our phone numbers like a proper phone book well at the
462:45 like a proper phone book well at the moment there's really no way to do this
462:48 moment there's really no way to do this I could do something hackish like I
462:49 I could do something hackish like I could put a number like
462:52 could put a number like 617495 1000 after Carter I could maybe
462:55 617495 1000 after Carter I could maybe do something like 949 uh 468 2750 after
463:00 do something like 949 uh 468 2750 after me but now you're kind of doing the
463:02 me but now you're kind of doing the whole apples and oranges thing right
463:04 whole apples and oranges thing right like now it's not strings it's a string
463:05 like now it's not strings it's a string int string int all right so maybe I
463:08 int string int all right so maybe I could just make all of these strings but
463:10 could just make all of these strings but now it's just a conceptual mixing of
463:12 now it's just a conceptual mixing of apples and oranges like yes that's an
463:14 apples and oranges like yes that's an array of four strings but now you're on
463:15 array of four strings but now you're on the honor System to know that the first
463:17 the honor System to know that the first string is a name the second string is a
463:19 string is a name the second string is a number the third string is like you can
463:21 number the third string is like you can do it but it's a bit of a hack so to
463:23 do it but it's a bit of a hack so to speak so what might be cleaner than this
463:26 speak so what might be cleaner than this instead of combining our phone numbers
463:28 instead of combining our phone numbers into the same array as our names what
463:30 into the same array as our names what else might we do that's perhaps a little
463:33 else might we do that's perhaps a little better say a little
463:36 better say a little Lou a 2d array uh possibly something we
463:40 Lou a 2d array uh possibly something we could do I'm going to keep it even
463:41 could do I'm going to keep it even simpler now because we haven't used
463:42 simpler now because we haven't used those by name even though that is we saw
463:44 those by name even though that is we saw last week technically what argv is what
463:47 last week technically what argv is what else could I do if I want to store names
463:48 else could I do if I want to store names and numbers
463:50 and numbers yeahp yeah let me go with this
463:51 yeahp yeah let me go with this suggestion just it's a little simpler
463:53 suggestion just it's a little simpler rather than complicate things in
463:54 rather than complicate things in literally different dimensions let me go
463:56 literally different dimensions let me go ahead and do string well I could do int
463:59 ahead and do string well I could do int numbers but you know what so that we can
464:02 numbers but you know what so that we can support punctuation like dashes or even
464:05 support punctuation like dashes or even parentheses or country codes I'm going
464:07 parentheses or country codes I'm going to do this instead I'm going to do
464:08 to do this instead I'm going to do string numbers so that I can represent
464:11 string numbers so that I can represent Carter's number as quote unquote plus
464:13 Carter's number as quote unquote plus one for the US 617 495 1,000 complete
464:17 one for the US 617 495 1,000 complete with hyphens as is us convention and
464:19 with hyphens as is us convention and then for mine I'll go ahead and do + one
464:21 then for mine I'll go ahead and do + one 949 how about 468
464:24 949 how about 468 275 semi colon and now down below let's
464:28 275 semi colon and now down below let's actually enable the user to search this
464:30 actually enable the user to search this phone book just like in week zero we did
464:32 phone book just like in week zero we did string name equals get string and let's
464:35 string name equals get string and let's ask the user for a name presumably David
464:37 ask the user for a name presumably David or Carter or someone else and now let's
464:39 or Carter or someone else and now let's re-implement linear search so four in I
464:41 re-implement linear search so four in I gets zero I is less than two and do as I
464:45 gets zero I is less than two and do as I say not as I do I think we should beware
464:47 say not as I do I think we should beware this coding but we'll keep it simple for
464:49 this coding but we'll keep it simple for now i++ and then in this for loop I
464:53 now i++ and then in this for loop I think we have all of the ingredients to
464:55 think we have all of the ingredients to solve this so if the return value of
464:58 solve this so if the return value of stir compare of all of the names bracket
465:02 stir compare of all of the names bracket I comparing against the name that the
465:04 I comparing against the name that the human typed in if all of that equals
465:07 human typed in if all of that equals equals zero that is all of the
465:09 equals zero that is all of the characters in those two strings are
465:11 characters in those two strings are equal then I think we can go ahead and
465:14 equal then I think we can go ahead and say found just like last time but you
465:16 say found just like last time but you know what let's actually print Carter or
465:18 know what let's actually print Carter or my phone number so found percent s and
465:20 my phone number so found percent s and we'll plug in numbers bracket I and then
465:24 we'll plug in numbers bracket I and then just for consistency I'll return zero
465:26 just for consistency I'll return zero here and down here how about I'll say
465:28 here and down here how about I'll say something like uh print F not found just
465:31 something like uh print F not found just to be clear and then I'll return one as
465:34 to be clear and then I'll return one as well so just a recap here's all of the
465:36 well so just a recap here's all of the code it's almost the same as before
465:39 code it's almost the same as before except now it's useful I'm not just
465:40 except now it's useful I'm not just saying found or not found I found a
465:42 saying found or not found I found a number in Monopoly or I found a a piece
465:45 number in Monopoly or I found a a piece in Monopoly I'm looking up in one array
465:48 in Monopoly I'm looking up in one array of the strings and then I'm printing
465:49 of the strings and then I'm printing from the other array the answer so let
465:52 from the other array the answer so let me go ahead here and run the compiler
465:56 me go ahead here and run the compiler make phone book enter okay that's
465:58 make phone book enter okay that's promising no errors do/ phonebook now
466:01 promising no errors do/ phonebook now and let's search for for instance Carter
466:04 and let's search for for instance Carter enter all right so we found Carter's
466:06 enter all right so we found Carter's number all right let me do that again
466:07 number all right let me do that again phone book Let's search for David all
466:10 phone book Let's search for David all right we seem to have found David's
466:11 right we seem to have found David's number all right let's do it one last
466:12 number all right let's do it one last time phone book enter and now we'll
466:14 time phone book enter and now we'll search for like John Harvard enter not
466:17 search for like John Harvard enter not found
466:19 found all right so I dare say albeit with
466:21 all right so I dare say albeit with minimal testing this code is
466:23 minimal testing this code is correct would anyone now like to
466:25 correct would anyone now like to critique the design does something rub
466:27 critique the design does something rub you the wrong way perhaps about this
466:30 you the wrong way perhaps about this approach
466:36 here and as always think about how if the program maybe gets longer more
466:37 the program maybe gets longer more complicated how decisions like this
466:39 complicated how decisions like this might unfold
466:42 might unfold yeah okay so if I is less than two so
466:45 yeah okay so if I is less than two so technically I if I change the number of
466:48 technically I if I change the number of people in this phone book I'm going to
466:49 people in this phone book I'm going to have to update I and we've already seen
466:50 have to update I and we've already seen that I get myself into trouble so that's
466:52 that I get myself into trouble so that's bad design
467:05 goodes yeah so again I'm sort of trusting myself not to screw up if I add
467:07 trusting myself not to screw up if I add John or anyone else to the first array
467:09 John or anyone else to the first array but I forget to add their number to the
467:11 but I forget to add their number to the second array you know eventually things
467:13 second array you know eventually things are going to drift and be inconsistent
467:15 are going to drift and be inconsistent and then the just code will be incorrect
467:17 and then the just code will be incorrect at that point so so sort of a poor
467:18 at that point so so sort of a poor design setting me up for future failure
467:21 design setting me up for future failure if you will other thoughts
467:33 yeah yeah really good we're assuming the same order from left to right the names
467:36 same order from left to right the names go and from left to right the numbers go
467:38 go and from left to right the numbers go but that's kind of just the honor System
467:40 but that's kind of just the honor System like there's literally nothing in code
467:41 like there's literally nothing in code preventing me from reversing the order
467:43 preventing me from reversing the order for whatever reason or maybe sorting the
467:45 for whatever reason or maybe sorting the names like they're sorted now and maybe
467:47 names like they're sorted now and maybe that's deliberate but maybe it's not so
467:49 that's deliberate but maybe it's not so this honor System here too is just not
467:51 this honor System here too is just not good right I could put a comment in here
467:52 good right I could put a comment in here to remind myself to uh you know note to
467:55 to remind myself to uh you know note to self always update arrays the same way
467:58 self always update arrays the same way but like that's something's going to
467:59 but like that's something's going to happen eventually especially when we
468:00 happen eventually especially when we have not two but three but 30 300 names
468:04 have not two but three but 30 300 names and numbers it would be nice to keep all
468:06 and numbers it would be nice to keep all of the related data together and so in
468:08 of the related data together and so in fact the one new feature of C we'll
468:10 fact the one new feature of C we'll introduce today is one that actually
468:12 introduce today is one that actually allows us to implement our very own data
468:16 allows us to implement our very own data structures you can think of a raay as a
468:18 structures you can think of a raay as a very lightweight data structure and that
468:21 very lightweight data structure and that allows you to Cluster related data back
468:23 allows you to Cluster related data back to back to back to back and this is how
468:25 to back to back to back and this is how strings are implemented they are a data
468:27 strings are implemented they are a data structure effectively implemented with
468:29 structure effectively implemented with an array but with c and with other
468:31 an array but with c and with other languages it turns out you can invent
468:33 languages it turns out you can invent your own data types whether they're one
468:36 your own data types whether they're one dimensional two-dimensional even or
468:37 dimensional two-dimensional even or Beyond and with uh with C can you
468:42 Beyond and with uh with C can you specifically create your own types that
468:45 specifically create your own types that have their own name so for instance
468:47 have their own name so for instance wouldn't it have been nice if C came
468:49 wouldn't it have been nice if C came with not just Char and int and float and
468:52 with not just Char and int and float and uh uh long and and others wouldn't it be
468:55 uh uh long and and others wouldn't it be nice if C came with a data type called
468:58 nice if C came with a data type called person and ideally a person would have a
469:00 person and ideally a person would have a name and a number now that's a little
469:02 name and a number now that's a little naive and unrealistic like why would
469:04 naive and unrealistic like why would they define a person to have just those
469:06 they define a person to have just those two Fields certainly people could have
469:08 two Fields certainly people could have disagreed what a person is so they leave
469:10 disagreed what a person is so they leave it to us like the authors of C gave us
469:12 it to us like the authors of C gave us all of these Primitives inss and floats
469:14 all of these Primitives inss and floats and strings and so forth but it's up to
469:16 and strings and so forth but it's up to us now to use those in a more
469:17 us now to use those in a more interesting way so that we can create an
469:20 interesting way so that we can create an array of person variables if you will
469:23 array of person variables if you will inside of an array called people just to
469:25 inside of an array called people just to pluralize it here so how are we going to
469:28 pluralize it here so how are we going to do this well for now let's just
469:29 do this well for now let's just stipulate that a person in the world
469:32 stipulate that a person in the world will have a name and a number that we
469:34 will have a name and a number that we could argue all day long what else a
469:36 could argue all day long what else a person should have and that's fine you
469:37 person should have and that's fine you can invent your own person
469:39 can invent your own person eventually at the moment I'm using just
469:42 eventually at the moment I'm using just two variables to define a person's name
469:44 two variables to define a person's name and number but wouldn't it be nice to
469:46 and number but wouldn't it be nice to encapsulate that is combined these two
469:49 encapsulate that is combined these two data types into a new and improved data
469:52 data types into a new and improved data type called person and the Syntax for
469:54 type called person and the Syntax for that is going to be this so it's a bit
469:56 that is going to be this so it's a bit of a mouthful but you can perhaps infer
469:59 of a mouthful but you can perhaps infer what some of this is doing here so it
470:00 what some of this is doing here so it turns out C has a keyword called type
470:02 turns out C has a keyword called type def as the name kind of suggests this
470:04 def as the name kind of suggests this allows you to Define your own type
470:07 allows you to Define your own type struct is an indication that it's a
470:09 struct is an indication that it's a structure it's like a a structure that
470:12 structure it's like a a structure that has multiple values inside of it that
470:14 has multiple values inside of it that you are trying to Define and then at the
470:16 you are trying to Define and then at the very bottom here outside of the Curve
470:17 very bottom here outside of the Curve early braces is the name of the type
470:19 early braces is the name of the type that you want to create so you don't
470:22 that you want to create so you don't have discretion over using type def or
470:24 have discretion over using type def or struct in this particular case but you
470:25 struct in this particular case but you can name the thing whatever you want and
470:27 can name the thing whatever you want and you can put anything in the structure
470:30 you can put anything in the structure that you want as well and as soon as
470:32 that you want as well and as soon as this semicolon is executed at the bottom
470:34 this semicolon is executed at the bottom of the code every line thereafter can
470:37 of the code every line thereafter can now have access to a person data type
470:40 now have access to a person data type whether as a single variable or as an
470:43 whether as a single variable or as an entire array so if I want to build on
470:46 entire array so if I want to build on this then let me go ahead and do this
470:49 this then let me go ahead and do this let me go back to my C code here and I'm
470:51 let me go back to my C code here and I'm going to go ahead and uh change just a
470:55 going to go ahead and uh change just a couple of things let's go ahead and do
470:57 couple of things let's go ahead and do this I'm going to go ahead and first get
470:59 this I'm going to go ahead and first get rid of those two hardcoded arrays and
471:02 rid of those two hardcoded arrays and let me go ahead and at the top of my
471:05 let me go ahead and at the top of my file invent this type so type def struct
471:09 file invent this type so type def struct inside of it will be a string name and
471:11 inside of it will be a string name and then a string number and then the name
471:14 then a string number and then the name of this structure will be person and
471:15 of this structure will be person and best practice would have me Define at
471:17 best practice would have me Define at the very top of my file so that any of
471:19 the very top of my file so that any of my functions in fact could use it even
471:21 my functions in fact could use it even though I just have Main in this case now
471:23 though I just have Main in this case now if I want it I could do this like person
471:26 if I want it I could do this like person P1 and person P2 but we know from last
471:30 P1 and person P2 but we know from last week like that already is bad design if
471:31 week like that already is bad design if you want to have multiple instances of
471:34 you want to have multiple instances of the same type of variable we should
471:36 the same type of variable we should probably use what
471:43 instead and yeah an array so let me not even go down that road let me instead
471:44 even go down that road let me instead just do this person uh will be the type
471:47 just do this person uh will be the type of the array but I'm going to call it I
471:49 of the array but I'm going to call it I could call it persons but in English we
471:51 could call it persons but in English we typically say people so I'll call the
471:53 typically say people so I'll call the array people and I want two people to
471:55 array people and I want two people to exist in this array though I could
471:57 exist in this array though I could certainly change that number to be
471:58 certainly change that number to be anything I want how now do you put a
472:02 anything I want how now do you put a name inside of a person and then put the
472:04 name inside of a person and then put the number inside of that same person well
472:06 number inside of that same person well slightly new syntax today I'm going to
472:08 slightly new syntax today I'm going to go ahead and say this people bracket
472:10 go ahead and say this people bracket zero just gives me the first person in
472:12 zero just gives me the first person in the array that's not new but if you want
472:14 the array that's not new but if you want to go inside of that person in memory
472:18 to go inside of that person in memory you use a DOT and then you just specify
472:21 you use a DOT and then you just specify the name of the attribute therein so if
472:23 the name of the attribute therein so if I want to set the first person's name to
472:25 I want to set the first person's name to Carter I just use that so-called dot
472:27 Carter I just use that so-called dot notation and then if I want to set
472:29 notation and then if I want to set Carter's number using dot notation I
472:31 Carter's number using dot notation I would do this plus one
472:33 would do this plus one 617495 1000 and then if I want to do the
472:36 617495 1000 and then if I want to do the same for myself I would now do people
472:38 same for myself I would now do people bracket
472:40 bracket 1name equals quote unquote David and
472:42 1name equals quote unquote David and then people bracket one still number
472:46 then people bracket one still number equals quote unquote + 1
472:48 equals quote unquote + 1 949468
472:51 949468 2750 and now at the bottom of my file I
472:54 2750 and now at the bottom of my file I think my logic can pretty much stay the
472:56 think my logic can pretty much stay the same I can still on this line here
473:00 same I can still on this line here prompt the user for the name of the
473:01 prompt the user for the name of the person they want to look up for now even
473:04 person they want to look up for now even though I admit it's not the best design
473:05 though I admit it's not the best design I'm just doing this for demonstration
473:06 I'm just doing this for demonstration sake I'm going to leave the two there
473:08 sake I'm going to leave the two there because I know I have two people but
473:10 because I know I have two people but down here this is going to have to
473:12 down here this is going to have to change I don't want to compare names
473:14 change I don't want to compare names bracket I anymore what do I want to type
473:16 bracket I anymore what do I want to type here as the first first argument to stir
473:20 here as the first first argument to stir compare what do I want to do here
473:24 compare what do I want to do here yeah so people i. name yeah so I want to
473:28 yeah so people i. name yeah so I want to go into the people array at the I
473:31 go into the people array at the I location because that's what my Loop is
473:32 location because that's what my Loop is doing it's updating I again and again
473:34 doing it's updating I again and again and then look at name and that's good I
473:37 and then look at name and that's good I think now I need to change this too what
473:38 think now I need to change this too what do I want to print if the person is
473:40 do I want to print if the person is found someone
473:42 found someone else what do I want to print here if I
473:45 else what do I want to print here if I found the person's name
473:52 yeah say a little louder perfect so people bracket. number
473:55 louder perfect so people bracket. number if indeed I want to print the
473:56 if indeed I want to print the corresponding number to this person and
473:59 corresponding number to this person and then I think the rest of my code can
474:00 then I think the rest of my code can stay the same so let me go ahead and run
474:03 stay the same so let me go ahead and run make phonebook to recompile this version
474:06 make phonebook to recompile this version so far so good/ phonebook let's go ahead
474:08 so far so good/ phonebook let's go ahead and type in Carter's name found all
474:11 and type in Carter's name found all right let's go ahead and run it again
474:12 right let's go ahead and run it again David's name found let's go ahead and
474:14 David's name found let's go ahead and run it one more time type in John
474:16 run it one more time type in John Harvard for instance not found in this
474:18 Harvard for instance not found in this case so fundamentally the code isn't all
474:22 case so fundamentally the code isn't all that different linear search is still
474:23 that different linear search is still behaving the same way and I admit this
474:25 behaving the same way and I admit this is kind of ugly looking like we've kind
474:27 is kind of ugly looking like we've kind of made a two-line solution like five
474:30 of made a two-line solution like five lines of code now but if we fast forward
474:32 lines of code now but if we fast forward a week or two when we start saving
474:34 a week or two when we start saving information to files uh we'll introduce
474:37 information to files uh we'll introduce you to files like CSV value CSV files
474:40 you to files like CSV value CSV files comma separated values or spreadsheet
474:42 comma separated values or spreadsheet files which you've surely opened on your
474:43 files which you've surely opened on your Mac or PC at some point in the past
474:45 Mac or PC at some point in the past suffice it to say We'll soon learn
474:46 suffice it to say We'll soon learn Techni for storing information like
474:48 Techni for storing information like names and numbers in files and at that
474:51 names and numbers in files and at that point we're not going to do any of this
474:52 point we're not going to do any of this hackish sort of hardcoding of the number
474:54 hackish sort of hardcoding of the number two and manually typing my name and
474:56 two and manually typing my name and Carter's name and number into our
474:57 Carter's name and number into our program we'll read the information
474:59 program we'll read the information dynamically from a file and in a few
475:01 dynamically from a file and in a few weeks we'll read it dynamically from a
475:02 weeks we'll read it dynamically from a database instead but this is for now
475:05 database instead but this is for now just syntactically how we can create an
475:07 just syntactically how we can create an array of size two containing one person
475:10 array of size two containing one person each we can update the name and number
475:12 each we can update the name and number of the first person update the name and
475:14 of the first person update the name and the number of the second person and then
475:15 the number of the second person and then later search across those names and
475:18 later search across those names and print out the corresponding numbers and
475:20 print out the corresponding numbers and in this sense this is a better design
475:23 in this sense this is a better design why because my person data type
475:26 why because my person data type encapsulates now everything that it
475:29 encapsulates now everything that it means to be a person at least in this
475:31 means to be a person at least in this narrow world and if I want to add
475:33 narrow world and if I want to add something to the notion of a person for
475:36 something to the notion of a person for instance I could go up to my typed Def
475:37 instance I could go up to my typed Def and tomorrow add an address to every
475:40 and tomorrow add an address to every person and start reading that in as well
475:42 person and start reading that in as well and now it's not the honor System it's
475:44 and now it's not the honor System it's not a names array a numbers array and a
475:47 not a names array a numbers array and a addesses array and everything else you
475:49 addesses array and everything else you might imagine related to a person it's
475:51 might imagine related to a person it's all encapsulated which is a a term of
475:53 all encapsulated which is a a term of art inside of the same type reminiscent
475:57 art inside of the same type reminiscent if some of you have programmed before of
475:58 if some of you have programmed before of something called objectoriented
475:59 something called objectoriented programming but we're not there yet C is
476:02 programming but we're not there yet C is not that questions on this use of struct
476:06 not that questions on this use of struct or this new syntax the dot operator
476:09 or this new syntax the dot operator being really the juicy part
476:12 being really the juicy part here any questions
476:16 here any questions yeah
476:24 on what line number 16 so yes so syntactically we
476:28 number 16 so yes so syntactically we introduced the square brackets last week
476:30 introduced the square brackets last week so doing people bracket zero just means
476:32 so doing people bracket zero just means go to the first person in the array that
476:34 go to the first person in the array that was like when Stephanie literally opened
476:36 was like when Stephanie literally opened this door that's uh that's doors bracket
476:38 this door that's uh that's doors bracket zero but this is of course people
476:39 zero but this is of course people bracket zero instead today the dot is a
476:42 bracket zero instead today the dot is a new piece of syntax it means go inside
476:44 new piece of syntax it means go inside of that person in memory
476:47 of that person in memory and look at the name therein and set it
476:50 and look at the name therein and set it equal to Carter and do the same for
476:51 equal to Carter and do the same for number so that's all it's like open the
476:53 number so that's all it's like open the locker door go inside of it and check or
476:55 locker door go inside of it and check or set the name and the number
477:10 yeah attributes is fine uh good question in the struct can you set default values
477:12 in the struct can you set default values short answer no and this is where C
477:15 short answer no and this is where C becomes less featurable than more more
477:17 becomes less featurable than more more modern languages like Python and Java
477:19 modern languages like Python and Java and others where you can in fact do that
477:21 and others where you can in fact do that so when we transition to python in a few
477:23 so when we transition to python in a few weeks time we'll see how we can start
477:24 weeks time we'll see how we can start solving problems like that but for now
477:26 solving problems like that but for now it's up to you to initialize name and
477:28 it's up to you to initialize name and number to something
477:44 yeah really good question how can we adjust or critique the design of what
477:46 adjust or critique the design of what I'm doing
477:47 I'm doing this is one of the few situations where
477:49 this is one of the few situations where I would say hypocritically do as I say
477:51 I would say hypocritically do as I say not as I do I am using pretty ugly lines
477:54 not as I do I am using pretty ugly lines like this just to introduce the syntax
477:56 like this just to introduce the syntax but my claim pedagogically today is that
477:58 but my claim pedagogically today is that eventually when we start storing names
478:00 eventually when we start storing names and numbers or other things in files or
478:02 and numbers or other things in files or in databases you won't have this
478:04 in databases you won't have this redundancy you'll have one line of code
478:06 redundancy you'll have one line of code or two lines of code that read the
478:08 or two lines of code that read the information from the file or database
478:10 information from the file or database and then fill the entire array with that
478:13 and then fill the entire array with that data for now I'm just doing it manually
478:14 data for now I'm just doing it manually so is to keep our Focus only on the new
478:16 so is to keep our Focus only on the new syntax but that's it so forgive the bad
478:19 syntax but that's it so forgive the bad Design By Design today other questions
478:22 Design By Design today other questions on
478:23 on this all right that's been a lot already
478:26 this all right that's been a lot already why don't we go ahead and take our
478:27 why don't we go ahead and take our 10-minute break with snacks first we
478:29 10-minute break with snacks first we have some delightful brownies in the
478:31 have some delightful brownies in the lobby all right we are back and up until
478:35 lobby all right we are back and up until now it clearly seems to be a good thing
478:37 now it clearly seems to be a good thing if your data is sorted because you can
478:39 if your data is sorted because you can use binary search you know a little more
478:42 use binary search you know a little more some little something more about the
478:43 some little something more about the data uh but it turns out that sorting of
478:46 data uh but it turns out that sorting of itself is kind of a problem to solve too
478:49 itself is kind of a problem to solve too and you might think well if sorting is
478:53 and you might think well if sorting is going to be pretty fast we absolutely
478:55 going to be pretty fast we absolutely should do it before we start searching
478:56 should do it before we start searching because that'll just speed up all of our
478:58 because that'll just speed up all of our searches but if sorting is slow that
479:00 searches but if sorting is slow that kind of invites the question well should
479:02 kind of invites the question well should we bother sorting our data if we're only
479:04 we bother sorting our data if we're only going to search the data maybe once
479:06 going to search the data maybe once maybe twice and so here is going to be
479:08 maybe twice and so here is going to be potentially a trade-off so let's
479:10 potentially a trade-off so let's consider what it means really to sort
479:11 consider what it means really to sort data in our case it's just going to be
479:13 data in our case it's just going to be simple and use numbers but it might in
479:15 simple and use numbers but it might in the case of the Googles of the World be
479:17 the case of the Googles of the World be actual web pages or persons or the like
479:19 actual web pages or persons or the like so here is our typical picture for
479:21 so here is our typical picture for sorting input uh for solving any problem
479:25 sorting input uh for solving any problem input at left and output at right the
479:27 input at left and output at right the input to our sort problem is going to be
479:30 input to our sort problem is going to be uh some unsorted set of values and the
479:33 uh some unsorted set of values and the output ideally will be the same set of
479:35 output ideally will be the same set of values sorted and if we do this
479:37 values sorted and if we do this concretely let's suppose that we want to
479:39 concretely let's suppose that we want to go about sorting this list of numbers 7
479:41 go about sorting this list of numbers 7 2 5 4 1 6 03 so it's all of the numbers
479:45 2 5 4 1 6 03 so it's all of the numbers from 0 to 7 but they're somehow jumbled
479:47 from 0 to 7 but they're somehow jumbled up randomly that's going to be the input
479:49 up randomly that's going to be the input to the problem and the goal is now to
479:51 to the problem and the goal is now to sort those so that you indeed get out 0
479:53 sort those so that you indeed get out 0 1 2 3 4 5 67 instead so it turns out
479:57 1 2 3 4 5 67 instead so it turns out there's lots of different ways we can
479:59 there's lots of different ways we can actually sort numbers like these here um
480:02 actually sort numbers like these here um and in fact just to complement our
480:04 and in fact just to complement our search example earlier could we perhaps
480:06 search example earlier could we perhaps quickly get some eight volunteers to
480:08 quickly get some eight volunteers to come up if you're comfortable appearing
480:10 come up if you're comfortable appearing on the internet if you want to do one
480:12 on the internet if you want to do one okay uh two three four five 6 7 eight
480:17 okay uh two three four five 6 7 eight how about all right come on
480:25 down all right come on over here and I'll give
480:27 right come on over here and I'll give you each a number and if you want to
480:30 you each a number and if you want to start to organize yourselves in the same
480:32 start to organize yourselves in the same order you see the numbers on the
480:35 order you see the numbers on the board okay so look up on the overhead
480:38 board okay so look up on the overhead and organize yourselves from left to
480:40 and organize yourselves from left to right in that same
480:42 right in that same order and let's have the first of you
480:44 order and let's have the first of you perfect if you want to come uh right
480:46 perfect if you want to come uh right over here
480:47 over here how about right in line with this all
480:49 how about right in line with this all right and a few more
480:51 right and a few more numbers all right number
480:54 numbers all right number two six
480:57 two six and perfect just the right number all
480:59 and perfect just the right number all right no uhoh all right there we go
481:02 right no uhoh all right there we go number three all right so let's just do
481:04 number three all right so let's just do a quick check we have 7 2 5 4 1 603 very
481:09 a quick check we have 7 2 5 4 1 603 very good so far do you want to just scooch a
481:11 good so far do you want to just scooch a little this way just to make a little
481:12 little this way just to make a little more of room all right and let's
481:14 more of room all right and let's consider now who we have here on stage
481:16 consider now who we have here on stage you want to each say a quick hello to
481:18 you want to each say a quick hello to the audience hi my name is Ryan uh I'm a
481:21 the audience hi my name is Ryan uh I'm a first year from Penny
481:23 first year from Penny Packer hi my name is Cel I'm a first
481:26 Packer hi my name is Cel I'm a first year at Strauss um hi my name is Lucy
481:29 year at Strauss um hi my name is Lucy I'm a first year from
481:30 I'm a first year from greo hi my name is Shiloh I'm a first
481:32 greo hi my name is Shiloh I'm a first year in
481:33 year in Wigglesworth hi my name is Jack and I'm
481:36 Wigglesworth hi my name is Jack and I'm a first year in
481:38 a first year in St hi my name is Katherine I'm a first
481:40 St hi my name is Katherine I'm a first year in stra hi my name is Michael and
481:43 year in stra hi my name is Michael and my first year at Penny Packer hi my name
481:45 my first year at Penny Packer hi my name is Muhammad at my first here in massuse
481:48 is Muhammad at my first here in massuse nice welcome aboard all right so let's
481:51 nice welcome aboard all right so let's consider now how we might go about
481:53 consider now how we might go about sorting Our Kind volunteers here the
481:55 sorting Our Kind volunteers here the goal being to get them into order from
481:58 goal being to get them into order from uh smallest to largest so that
482:00 uh smallest to largest so that presumably then we can use something
482:01 presumably then we can use something smarter than just linear search we could
482:03 smarter than just linear search we could actually use binary search assuming that
482:05 actually use binary search assuming that they are already then sorted so let me
482:07 they are already then sorted so let me propose that we first consider an
482:08 propose that we first consider an algorithm that actually has a name
482:10 algorithm that actually has a name called selection sort and selection sort
482:12 called selection sort and selection sort is going to be one that literally has me
482:15 is going to be one that literally has me or really you as the programmer
482:16 or really you as the programmer selecting the smallest element again and
482:19 selecting the smallest element again and again and then putting them into the
482:21 again and then putting them into the appropriate place so let me go ahead and
482:23 appropriate place so let me go ahead and start this here uh starting with the
482:25 start this here uh starting with the number seven at the moment seven is the
482:27 number seven at the moment seven is the smallest number I found so I'm going to
482:28 smallest number I found so I'm going to make mental note of that with a mental
482:30 make mental note of that with a mental variable if you will I'm going to move
482:31 variable if you will I'm going to move on now oh number two is obviously
482:33 on now oh number two is obviously smaller so I'm just going to update my
482:34 smaller so I'm just going to update my mental reminder that two is now the
482:36 mental reminder that two is now the smallest effectively forgetting for now
482:38 smallest effectively forgetting for now number seven uh five not smaller four
482:41 number seven uh five not smaller four not smaller one smaller and I'm going to
482:43 not smaller one smaller and I'm going to make mental note of that six not smaller
482:45 make mental note of that six not smaller zero smaller I'll make mental note of
482:47 zero smaller I'll make mental note of that having forgotten now everything
482:49 that having forgotten now everything else and now number three is not smaller
482:52 else and now number three is not smaller so what's your name again Michael so
482:53 so what's your name again Michael so Michael is number zero he belongs of
482:56 Michael is number zero he belongs of course way down there but unfortunately
482:57 course way down there but unfortunately you are Ryan Ryan Ryan is in the way so
483:02 you are Ryan Ryan Ryan is in the way so what should we do how should we start to
483:04 what should we do how should we start to sort this
483:05 sort this list where should number zero
483:08 list where should number zero go yeah do you want to say the
483:15 L yeah so let's just go ahead and swap so if you want to go ahead and zero go
483:16 so if you want to go ahead and zero go on where seven is we need to make room
483:19 on where seven is we need to make room for number seven it would kind of be
483:20 for number seven it would kind of be cheating if maybe everyone kind of
483:22 cheating if maybe everyone kind of politely stepped over to the side why
483:24 politely stepped over to the side why because if we imagine all of our
483:26 because if we imagine all of our volunteers here to be in Array like
483:27 volunteers here to be in Array like that's a crazy amount of work to have
483:29 that's a crazy amount of work to have every element in the array shift to the
483:31 every element in the array shift to the left just to make room so we're going to
483:33 left just to make room so we're going to keep it simple and just evict whoever is
483:35 keep it simple and just evict whoever is there now maybe we get lucky and number
483:37 there now maybe we get lucky and number seven is actually closer to its
483:39 seven is actually closer to its destination maybe we get unlucky and it
483:41 destination maybe we get unlucky and it goes farther away but we've at least
483:42 goes farther away but we've at least solved one problem if we had n problems
483:45 solved one problem if we had n problems at first now we have n minus one because
483:47 at first now we have n minus one because number zero is indeed in the right place
483:49 number zero is indeed in the right place so if I continue to act this out let me
483:51 so if I continue to act this out let me go ahead and say two okay currently the
483:53 go ahead and say two okay currently the smallest five no four no one currently
483:56 smallest five no four no one currently the smallest I'll make mental note 6 7 3
484:00 the smallest I'll make mental note 6 7 3 and now let me pause one is obviously
484:02 and now let me pause one is obviously the now smallest element so did I need
484:05 the now smallest element so did I need to keep going well it turns out at least
484:07 to keep going well it turns out at least as I've defined selection sort I do need
484:10 as I've defined selection sort I do need to keep going because I only claim that
484:12 to keep going because I only claim that I'm using one variable in my mind to
484:13 I'm using one variable in my mind to remember the then smallest element I'm
484:15 remember the then smallest element I'm not smart enough like us humans to
484:17 not smart enough like us humans to remember oh wait a minute one is
484:19 remember oh wait a minute one is definitely the smallest now I don't have
484:20 definitely the smallest now I don't have that whole recollection so I just am
484:22 that whole recollection so I just am keeping track of the now smallest so
484:24 keeping track of the now smallest so number one what your name was Jack Jack
484:26 number one what your name was Jack Jack where should jack go probably there and
484:29 where should jack go probably there and what's your name itel itel okay so Jack
484:31 what's your name itel itel okay so Jack and itel if you want to swap places
484:33 and itel if you want to swap places we've now solved two of the end total
484:35 we've now solved two of the end total problems and now we'll do it a little
484:37 problems and now we'll do it a little faster if each of you want to sort of
484:39 faster if each of you want to sort of start to swap as I find the right person
484:41 start to swap as I find the right person so five smallest four is smaller two is
484:44 so five smallest four is smaller two is smaller got to keep checking okay two
484:47 smaller got to keep checking okay two was smaller all right now I'm going to
484:49 was smaller all right now I'm going to go back to the beginning all right four
484:51 go back to the beginning all right four is small five is not six is not seven is
484:53 is small five is not six is not seven is oh three is small where do you want to
484:55 oh three is small where do you want to go okay good I'm going to go back here
484:58 go okay good I'm going to go back here and I can be a little smart I don't have
485:00 and I can be a little smart I don't have to go all the way to the end because I
485:02 to go all the way to the end because I know these folks are already sorted so I
485:03 know these folks are already sorted so I can at least optimize slightly so now
485:05 can at least optimize slightly so now five is small six is small seven is four
485:08 five is small six is small seven is four four is smaller if you want to go in
485:09 four is smaller if you want to go in place
485:11 place there and now here things get
485:13 there and now here things get interesting I can optimize by not
485:15 interesting I can optimize by not looking at these folks anymore more cuz
485:16 looking at these folks anymore more cuz they're obviously problem solved but now
485:18 they're obviously problem solved but now five is small six is not seven is not
485:21 five is small six is not seven is not okay five you can stay where you are now
485:24 okay five you can stay where you are now a human in the room is obviously going
485:25 a human in the room is obviously going to question why I'm wasting any more
485:27 to question why I'm wasting any more time but with selection sort as I've
485:29 time but with selection sort as I've defined it thus far I still have to now
485:32 defined it thus far I still have to now check six is smallest not seven and now
485:35 check six is smallest not seven and now my final step okay they're all in place
485:37 my final step okay they're all in place so here too is this dichotomy between
485:39 so here too is this dichotomy between what we all have is this bird's eye view
485:40 what we all have is this bird's eye view of the whole problem where it's obvious
485:42 of the whole problem where it's obvious where everyone needs to go but a
485:44 where everyone needs to go but a computer implementing this with an array
485:46 computer implementing this with an array really has to be more methodical and
485:48 really has to be more methodical and we're actually saving a step here if we
485:49 we're actually saving a step here if we were really doing this none of these
485:51 were really doing this none of these numbers would be visible all eight of
485:53 numbers would be visible all eight of our volunteers would be inside of a
485:54 our volunteers would be inside of a locked door and only then could we see
485:56 locked door and only then could we see them one at a time but we're focusing
485:58 them one at a time but we're focusing now just on the Sorting aspect so let me
486:01 now just on the Sorting aspect so let me just before we do one other
486:02 just before we do one other demonstration here proposed that what I
486:04 demonstration here proposed that what I really just did here in pseudo code was
486:07 really just did here in pseudo code was something like this 4 I from 0er to n
486:10 something like this 4 I from 0er to n minus one keeping in mind that zero is
486:13 minus one keeping in mind that zero is always the left of the array n minus one
486:15 always the left of the array n minus one is always the right end of of the array
486:17 is always the right end of of the array for I from 0 to N minus1 I found the
486:19 for I from 0 to N minus1 I found the smallest number between numbers bracket
486:21 smallest number between numbers bracket I and numbers bracket n minus one and
486:24 I and numbers bracket n minus one and that's the very geeky way of expressing
486:26 that's the very geeky way of expressing this optimization I'm always starting
486:28 this optimization I'm always starting from numbers bracket I wherever I am and
486:30 from numbers bracket I wherever I am and then everything else to the right and
486:32 then everything else to the right and that's what was allowing me to ignore
486:33 that's what was allowing me to ignore the already sorted volunteers if though
486:36 the already sorted volunteers if though my last line says swap smallest number
486:38 my last line says swap smallest number with numbers I I think that implements
486:41 with numbers I I think that implements what our humans were doing by physically
486:42 what our humans were doing by physically walking to another spot all right so
486:45 walking to another spot all right so that then would what we'll call
486:46 that then would what we'll call selection sort let's go ahead and take a
486:49 selection sort let's go ahead and take a second approach here using an algorithm
486:51 second approach here using an algorithm that I'm going to call bubble sort but
486:52 that I'm going to call bubble sort but to do this we need you all to reset to
486:53 to do this we need you all to reset to your original locations we have a little
486:55 your original locations we have a little cheat sheet on the board if you'd like
486:56 cheat sheet on the board if you'd like to go back to this position here and let
486:59 to go back to this position here and let me take a fundamentally different
487:00 me take a fundamentally different approach because I'm not really liking
487:02 approach because I'm not really liking selection sort as is because it's kind
487:03 selection sort as is because it's kind of a lot of walking back and forth and a
487:05 of a lot of walking back and forth and a lot of walking suggests like a lot of
487:07 lot of walking suggests like a lot of lot of steps again and again so what
487:09 lot of steps again and again so what might I do instead well bubble sort is
487:12 might I do instead well bubble sort is going to have me focus a little more
487:13 going to have me focus a little more intuitively on just smaller problems and
487:15 intuitively on just smaller problems and let's see if this gets me somewhere else
487:17 let's see if this gets me somewhere else so if I just look at this list without
487:19 so if I just look at this list without looking at everyone else seven and two
487:21 looking at everyone else seven and two this is obviously a problem why because
487:23 this is obviously a problem why because you're out of order so let's just solve
487:25 you're out of order so let's just solve one tiny problem first so seven and two
487:27 one tiny problem first so seven and two why don't you swap I know two is in a
487:29 why don't you swap I know two is in a better place now because she's
487:31 better place now because she's definitely lower uh less than seven so I
487:33 definitely lower uh less than seven so I think I can now move on seven and five
487:35 think I can now move on seven and five problem so let's solve that seven and
487:37 problem so let's solve that seven and four problem let's solve that s and one
487:40 four problem let's solve that s and one let's solve that 7 and six let's solve
487:42 let's solve that 7 and six let's solve that 7 and zero solve that 7 and three
487:44 that 7 and zero solve that 7 and three solve that okay done sorted right all
487:48 solve that okay done sorted right all obviously not if you just glance at
487:49 obviously not if you just glance at these numbers here but we have
487:51 these numbers here but we have fundamentally taken a bite out of the
487:52 fundamentally taken a bite out of the problem seven is indeed in the right
487:55 problem seven is indeed in the right place so we maximally have n minus one
487:58 place so we maximally have n minus one other problems to solve so how do I do
488:02 other problems to solve so how do I do this I think I can just repeat the same
488:03 this I think I can just repeat the same logic let me go over here two and five
488:06 logic let me go over here two and five good five and four no five and one no
488:10 good five and four no five and one no five and six yes 6 and zero no six and
488:13 five and six yes 6 and zero no six and three no so so now we've solved two of
488:17 three no so so now we've solved two of the problems and what's nice about
488:18 the problems and what's nice about Bubble sword at least as this glance
488:20 Bubble sword at least as this glance it's nice and simple it's nice and local
488:22 it's nice and simple it's nice and local and you just keep incrementally solving
488:24 and you just keep incrementally solving more and more problems so let's go ahead
488:26 more and more problems so let's go ahead and do this again and I'll do it we can
488:27 and do this again and I'll do it we can do it faster two and four we know are
488:30 do it faster two and four we know are good four and one four and five five and
488:33 good four and one four and five five and zero five and three five and six six and
488:37 zero five and three five and six six and seven good so we go back two and one ah
488:40 seven good so we go back two and one ah now another problem solve two and four
488:43 now another problem solve two and four four and zero four and three
488:46 four and zero four and three four and five five and six six and seven
488:48 four and five five and six six and seven and so notice too as per its name the
488:51 and so notice too as per its name the largest elements have bubbled their way
488:53 largest elements have bubbled their way up to the top and that's what seems to
488:54 up to the top and that's what seems to be happening just as we're fixing some
488:56 be happening just as we're fixing some remaining problems so almost done one
488:58 remaining problems so almost done one and two two and zero two and three three
489:01 and two two and zero two and three three and four four and five five and six six
489:04 and four four and five five and six six and seven almost done obviously to us
489:07 and seven almost done obviously to us humans it looks done how do I know as
489:09 humans it looks done how do I know as the computer for sure what would be the
489:12 the computer for sure what would be the most Surefire way for me to now oh it's
489:14 most Surefire way for me to now oh it's not done sorry uh that's a bug
489:17 not done sorry uh that's a bug okay one and zero okay one and two two
489:19 okay one and zero okay one and two two and three three and four three four and
489:21 and three three and four three four and five five and six six and seven okay so
489:22 five five and six six and seven okay so now it's obviously sorted to the rest of
489:25 now it's obviously sorted to the rest of us on stage how could I confirm as much
489:28 us on stage how could I confirm as much as code right you're doing it with your
489:29 as code right you're doing it with your mind just glancing at this how would the
489:31 mind just glancing at this how would the computer the code know for sure that
489:33 computer the code know for sure that this list is now sorted
489:36 this list is now sorted yeah let's do one more time and look uh
489:39 yeah let's do one more time and look uh draw what
489:45 conclusion yeah let's do it one more time even though it's a little wasteful
489:47 time even though it's a little wasteful but logically if I go through the whole
489:50 but logically if I go through the whole list comparing pairs again again and
489:52 list comparing pairs again again and again and I don't do any work that time
489:54 again and I don't do any work that time now it's obviously logically safe to
489:56 now it's obviously logically safe to just stop because otherwise I'm wasting
489:58 just stop because otherwise I'm wasting my time doing the same thing again and
489:59 my time doing the same thing again and again if no one's actually moving so I'm
490:02 again if no one's actually moving so I'm afraid we don't have Monopoly games for
490:03 afraid we don't have Monopoly games for all of you but we do have eight stress
490:05 all of you but we do have eight stress balls and a round of applause if we
490:06 balls and a round of applause if we could for our
490:07 could for our volunteers if you want to put your
490:09 volunteers if you want to put your numbers on the Shelf
490:11 numbers on the Shelf there so if we consider for a moment
490:15 there so if we consider for a moment thank you thank you so
490:17 thank you thank you so much
490:20 much sure thank you thanks sure so if we
490:24 sure thank you thanks sure so if we consider now these two algorithms which
490:26 consider now these two algorithms which one is better any intuition for whether
490:29 one is better any intuition for whether selection sort the first is better or
490:31 selection sort the first is better or worse than bubble sort the
490:33 worse than bubble sort the second any
490:36 second any thoughts
490:44 yeah okay so bubbl swort seemed like less work especially since I was
490:45 less work especially since I was focusing on those localized problems
490:47 focusing on those localized problems other
490:49 other intuition selection sort versus bubble
490:52 intuition selection sort versus bubble sort well let me propose that we try to
490:55 sort well let me propose that we try to like quantize this so we can actually
490:56 like quantize this so we can actually analyze it in some way and this is not
490:58 analyze it in some way and this is not an exercise we'll do constantly for lots
491:00 an exercise we'll do constantly for lots of algorithms but these are pretty
491:02 of algorithms but these are pretty representative of algorithms so we can
491:04 representative of algorithms so we can wrap our minds around indeed the
491:06 wrap our minds around indeed the performance or the design of these
491:07 performance or the design of these things so here is my pseudo code for
491:09 things so here is my pseudo code for selection sort whereby as it's as per
491:12 selection sort whereby as it's as per its name I just iteratively select the
491:14 its name I just iteratively select the next smallest element El again and again
491:17 next smallest element El again and again so how can we go about analyzing
491:19 so how can we go about analyzing something like this well we could just
491:21 something like this well we could just do it on paper pencil and count up the
491:23 do it on paper pencil and count up the number of steps that seem to be implied
491:25 number of steps that seem to be implied logically by the code we could literally
491:28 logically by the code we could literally comp count like the number of steps I
491:29 comp count like the number of steps I was taking again and again left to right
491:31 was taking again and again left to right we could also just com uh count the
491:33 we could also just com uh count the number of comparisons I was making with
491:36 number of comparisons I was making with each of the persons involved and I was
491:37 each of the persons involved and I was doing it kind of quickly in selection
491:39 doing it kind of quickly in selection sort but every time I was looking at a
491:41 sort but every time I was looking at a person trying to decide do I want to
491:42 person trying to decide do I want to remember that number is small as that
491:44 remember that number is small as that number I was comparing two values with
491:46 number I was comparing two values with an equals equals or less than or greater
491:48 an equals equals or less than or greater than sign at least if we had done this
491:49 than sign at least if we had done this in code so that tends to be the norm
491:52 in code so that tends to be the norm when analyzing algorithms like these
491:54 when analyzing algorithms like these counting the number of comparisons
491:55 counting the number of comparisons because it's kind of a global uh it's
491:58 because it's kind of a global uh it's kind of a global unit of measure we can
492:00 kind of a global unit of measure we can use to compare different algorithms
492:01 use to compare different algorithms entirely so think too that in the
492:05 entirely so think too that in the general case when we have more than
492:07 general case when we have more than eight volunteers more than seven doors
492:09 eight volunteers more than seven doors we can generalize our our array in
492:11 we can generalize our our array in general as this is the first element at
492:13 general as this is the first element at bracket zero and the end of it is always
492:15 bracket zero and the end of it is always n minus one so arrays uh or doors in
492:18 n minus one so arrays uh or doors in this case or volunteers are always
492:21 this case or volunteers are always numerically indexed from zero on up to n
492:23 numerically indexed from zero on up to n minus one if there's n of them in total
492:26 minus one if there's n of them in total so how do we analyze the code of
492:28 so how do we analyze the code of selection sort well how many steps did
492:31 selection sort well how many steps did it take me to find the first smallest
492:34 it take me to find the first smallest element or more precisely how many
492:36 element or more precisely how many comparisons did I need to make when I
492:38 comparisons did I need to make when I walked left to right to find our first
492:40 walked left to right to find our first smallest person which ended up being
492:44 smallest person which ended up being zero how many comparisons did I do when
492:47 zero how many comparisons did I do when walking left to right if there were
492:48 walking left to right if there were eight people on
492:50 eight people on stage how many total comparisons did I
492:53 stage how many total comparisons did I do like if there's eight people I
492:55 do like if there's eight people I compared these folks then then like this
492:59 compared these folks then then like this person this person yeah yeah so seven
493:02 person this person yeah yeah so seven total right because if there's eight
493:03 total right because if there's eight people on stage you can only do seven
493:04 people on stage you can only do seven comparisons total because otherwise
493:07 comparisons total because otherwise you'd be comparing one number to itself
493:08 you'd be comparing one number to itself so it seems like in the general case if
493:10 so it seems like in the general case if you've got n numbers that you're trying
493:12 you've got n numbers that you're trying to sort finding the smallest element
493:14 to sort finding the smallest element first takes n minus one comparisons
493:17 first takes n minus one comparisons maybe n it's total steps left or right
493:20 maybe n it's total steps left or right but the number of comparisons which I
493:21 but the number of comparisons which I claim is just a useful unit of measure
493:23 claim is just a useful unit of measure is n minus one how about finding the
493:25 is n minus one how about finding the next smallest person how many steps did
493:28 next smallest person how many steps did it take me to find the next smallest
493:30 it take me to find the next smallest number which ended up being the number
493:36 one yeah yeah so just n minus two why
493:38 yeah yeah so just n minus two why because I'd already solved one problem
493:40 because I'd already solved one problem someone was already in the right
493:41 someone was already in the right position it would be silly to keep
493:42 position it would be silly to keep counting them again and again so I can
493:44 counting them again and again so I can Whittle down my number of comparisons
493:46 Whittle down my number of comparisons for the next past n minus 2 the third
493:48 for the next past n minus 2 the third past to find the third smallest number
493:50 past to find the third smallest number would be n minus 3 and then dot dot dot
493:52 would be n minus 3 and then dot dot dot presumably this story this formula ends
493:54 presumably this story this formula ends when you have just one final pair the
493:56 when you have just one final pair the people at the end to compare so if this
493:59 people at the end to compare so if this is looking a little reminiscent of some
494:00 is looking a little reminiscent of some kind of recurrence from high school or
494:02 kind of recurrence from high school or high school math or physics or the like
494:04 high school math or physics or the like let me just stipulate that if you
494:05 let me just stipulate that if you actually do out this math and generalize
494:07 actually do out this math and generalize it that is the same thing as n * nus1 /
494:12 it that is the same thing as n * nus1 / 2 and if you're Rusty on that no big
494:13 2 and if you're Rusty on that no big deal just kind of commit to memory that
494:15 deal just kind of commit to memory that anytime you add up this kind of series
494:17 anytime you add up this kind of series something plus something slightly
494:18 something plus something slightly smaller plus something slightly smaller
494:20 smaller plus something slightly smaller Each of which differs by one you're
494:22 Each of which differs by one you're going to get this formula n * n -1/ 2 if
494:25 going to get this formula n * n -1/ 2 if we of course uh uh multiply that out
494:27 we of course uh uh multiply that out that's really n^ 2 minus n all divided
494:30 that's really n^ 2 minus n all divided by two if we keep multiplying it out
494:32 by two if we keep multiplying it out that's n^2 / 2 minus n /2 and now we
494:36 that's n^2 / 2 minus n /2 and now we have kind of a vocabulary with which we
494:38 have kind of a vocabulary with which we can talk about the efficiency the design
494:40 can talk about the efficiency the design of this algorithm but honestly I don't
494:42 of this algorithm but honestly I don't really care about this level of
494:44 really care about this level of precision like n^2 / 2us n / 2 as n gets
494:49 precision like n^2 / 2us n / 2 as n gets really large which of these symbols
494:52 really large which of these symbols which of these terms is really going to
494:54 which of these terms is really going to dominate become the biggest influencer
494:56 dominate become the biggest influencer on the total value of
494:58 on the total value of steps right it's the square right like
495:00 steps right it's the square right like it's definitely not n divided by two
495:02 it's definitely not n divided by two that's shaving some time off but N
495:03 that's shaving some time off but N squared as n gets big is going to get
495:06 squared as n gets big is going to get really big if n is 100 then n squar is
495:08 really big if n is 100 then n squar is bigger if N is a million n squ is really
495:10 bigger if N is a million n squ is really bigger and so at the end of the day when
495:12 bigger and so at the end of the day when we really just talking about sort of a
495:15 we really just talking about sort of a wave of of the hand analysis and upper
495:17 wave of of the hand analysis and upper bound if you will let's just say that
495:19 bound if you will let's just say that selection sort as analyzed here it's on
495:22 selection sort as analyzed here it's on the order of n s steps it's not
495:25 the order of n s steps it's not precisely n squar steps but you know
495:27 precisely n squar steps but you know what n^2 divided two the intuition here
495:30 what n^2 divided two the intuition here might be that well it's half of that you
495:34 might be that well it's half of that you n squ is what really matters as n gets
495:36 n squ is what really matters as n gets really really large and that's when you
495:38 really really large and that's when you start thinking about and trying to solve
495:39 start thinking about and trying to solve the Google problems of the world when n
495:41 the Google problems of the world when n gets large that's when you have to be
495:42 gets large that's when you have to be smarter than just sort of naive
495:44 smarter than just sort of naive implementations of any algorithm so
495:47 implementations of any algorithm so where then does this algorithm fall into
495:49 where then does this algorithm fall into this categorization here well n^2 it
495:52 this categorization here well n^2 it turns out is on the order of n squ steps
495:55 turns out is on the order of n squ steps in the worst case whether it's sorted or
495:57 in the worst case whether it's sorted or not it turns out though lower bound if
496:01 not it turns out though lower bound if we consider this same code suppose the
496:04 we consider this same code suppose the best case scenario like our eight
496:06 best case scenario like our eight volunteers came up on stage and just
496:07 volunteers came up on stage and just because they already sorted themselves 0
496:10 because they already sorted themselves 0 through seven suppose they just happen
496:12 through seven suppose they just happen to be in that state how many steps would
496:14 to be in that state how many steps would selection store
496:15 selection store take to sort an already sorted list of
496:20 take to sort an already sorted list of volunteers any intuition
496:29 yeah would it still be n so for the first pass it would still
496:31 n so for the first pass it would still be seven for the first per uh pass
496:33 be seven for the first per uh pass across the humans because even though
496:35 across the humans because even though yeah I'm claiming zero is here I don't
496:37 yeah I'm claiming zero is here I don't know that zero is the smallest until I
496:39 know that zero is the smallest until I make my way all the way over there doing
496:41 make my way all the way over there doing all seven comparisons okay fine first
496:44 all seven comparisons okay fine first pass took seven more generally n minus
496:46 pass took seven more generally n minus one steps what if I look for the next
496:48 one steps what if I look for the next smallest element and the humans in this
496:50 smallest element and the humans in this story are already sorted 0 through 7
496:53 story are already sorted 0 through 7 well yes the number one's here and I see
496:55 well yes the number one's here and I see them first but I don't know they're the
496:57 them first but I don't know they're the smallest until I compare against
496:59 smallest until I compare against everyone else get to the end of the list
497:01 everyone else get to the end of the list oh well that was stupid I already had
497:03 oh well that was stupid I already had the smallest person in hand then and so
497:05 the smallest person in hand then and so this pseudo code this implementation of
497:07 this pseudo code this implementation of selection sort is sort of fixed like
497:10 selection sort is sort of fixed like this there's no special case that says
497:12 this there's no special case that says if already sorted quit early it's always
497:15 if already sorted quit early it's always going to take n squ steps and so in this
497:17 going to take n squ steps and so in this case if we borrow our uh jargon from
497:19 case if we borrow our uh jargon from earlier using Omega notation just to be
497:23 earlier using Omega notation just to be clear selection sword is also going to
497:26 clear selection sword is also going to be in this Incarnation on an Omega of n
497:29 be in this Incarnation on an Omega of n s because even in the best case where
497:31 s because even in the best case where the list is already sorted you're going
497:32 the list is already sorted you're going to waste a huge amount of time
497:34 to waste a huge amount of time essentially verifying as much or
497:37 essentially verifying as much or discovering as much even though we
497:38 discovering as much even though we humans of course could see it right away
497:40 humans of course could see it right away so selection sort would seem to take
497:42 so selection sort would seem to take both N squared steps in the uh worst
497:45 both N squared steps in the uh worst case n s steps in the best case and so
497:47 case n s steps in the best case and so you know what we can use our Theta
497:49 you know what we can use our Theta terminology for that here would be an
497:50 terminology for that here would be an algorithm just like counting earlier
497:52 algorithm just like counting earlier that always takes N squared steps no
497:55 that always takes N squared steps no matter whether the array is sorted or
497:57 matter whether the array is sorted or not from the get-go all right so
497:59 not from the get-go all right so hopefully we can do better and someone
498:00 hopefully we can do better and someone proposed earlier that bubble sort felt
498:02 proposed earlier that bubble sort felt like it was using fewer steps well let's
498:04 like it was using fewer steps well let's consider that next with bubble sort we
498:07 consider that next with bubble sort we had this pseudo code I claim whereby
498:10 had this pseudo code I claim whereby let's focus on the inside of the code
498:12 let's focus on the inside of the code first down here what was I doing for I
498:16 first down here what was I doing for I from 0 to n minus 2 that's curious we've
498:19 from 0 to n minus 2 that's curious we've never seen n minus 2 before but I asked
498:22 never seen n minus 2 before but I asked this question if numbers bracket I and
498:24 this question if numbers bracket I and numbers bracket I + 1 are out of order
498:28 numbers bracket I + 1 are out of order swap them so that was when I was
498:30 swap them so that was when I was pointing at our first two volunteers
498:32 pointing at our first two volunteers here I saw that they were out of order
498:34 here I saw that they were out of order so I swapped them how come I'm doing
498:37 so I swapped them how come I'm doing that again and again up to nus 2 though
498:40 that again and again up to nus 2 though instead of n
498:42 instead of n minus1 which we've always used up until
498:44 minus1 which we've always used up until now as our rightmost
498:47 now as our rightmost boundary any intuition for why I'm doing
498:50 boundary any intuition for why I'm doing this from 0 to n minus 2
499:02 yeah exactly because I'm looking at the E person per this pseudo code here and
499:05 E person per this pseudo code here and the E plus one person I better make sure
499:07 the E plus one person I better make sure I don't go Step Beyond the at boundaries
499:09 I don't go Step Beyond the at boundaries of my array so if you think of like my
499:11 of my array so if you think of like my left hand when my back was two here
499:13 left hand when my back was two here pointing at the current person at the
499:15 pointing at the current person at the first position my right hand for this if
499:17 first position my right hand for this if conditioner is essentially pointing at
499:19 conditioner is essentially pointing at the person next to them and you want to
499:20 the person next to them and you want to iterate with your left hand all through
499:23 iterate with your left hand all through these people but you don't want your
499:24 these people but you don't want your left hand to point at the last person
499:26 left hand to point at the last person you want it to point at the second to
499:28 you want it to point at the second to last person but we know that the last
499:30 last person but we know that the last person is always at n minus one so the
499:33 person is always at n minus one so the second to last person just
499:34 second to last person just mathematically is at n minus 2 so it's a
499:37 mathematically is at n minus 2 so it's a subtlety but this is like a Segal
499:39 subtlety but this is like a Segal waiting to happen if you implemented
499:40 waiting to happen if you implemented bubble sort using n minus one you will
499:43 bubble sort using n minus one you will my right hand would go beyond the
499:45 my right hand would go beyond the boundaries of the array so just bad all
499:47 boundaries of the array so just bad all right so why am I saying this end times
499:49 right so why am I saying this end times Well we did it very organically with
499:51 Well we did it very organically with humans but each time someone uh each
499:55 humans but each time someone uh each pass I did through the array someone
499:57 pass I did through the array someone bubbled their way up to the end number
499:59 bubbled their way up to the end number seven then number six then number five
500:01 seven then number six then number five so if on each pass through the array of
500:04 so if on each pass through the array of volunteers I was solving at Mo at least
500:07 volunteers I was solving at Mo at least one problem it seems like bubble sort
500:09 one problem it seems like bubble sort can just run n times total to solve all
500:12 can just run n times total to solve all n problems cuz the first pass will get
500:14 n problems cuz the first pass will get at least one one number into place
500:15 at least one one number into place second pass second number into place you
500:17 second pass second number into place you might get lucky and it would do more but
500:19 might get lucky and it would do more but worst case this feels like enough just
500:20 worst case this feels like enough just do this blindly end times and they'll
500:23 do this blindly end times and they'll all line up together well technically
500:27 all line up together well technically all right now we're getting into the
500:28 all right now we're getting into the weeds technically you can just repeat it
500:29 weeds technically you can just repeat it n minus one times because if you solve
500:31 n minus one times because if you solve all n minus one other problems and
500:33 all n minus one other problems and you're left with one like literally that
500:35 you're left with one like literally that person's where they need to be just
500:37 person's where they need to be just logically if you've already sorted
500:38 logically if you've already sorted everything else and you've got just the
500:39 everything else and you've got just the one left it's already bubbled up so how
500:41 one left it's already bubbled up so how do we analyze this well in bubble sore
500:43 do we analyze this well in bubble sore we might do something like this I'm
500:45 we might do something like this I'm essentially doing n minus one Things N
500:49 essentially doing n minus one Things N minus one times now let me back up to
500:51 minus one times now let me back up to the pseudo code because this one's a
500:52 the pseudo code because this one's a little less obvious this is where you
500:54 little less obvious this is where you can actually mathematically infer from
500:57 can actually mathematically infer from your Loop uh how many steps you're
500:59 your Loop uh how many steps you're taking so this first line literally says
501:01 taking so this first line literally says repeat the following n minus one times
501:03 repeat the following n minus one times so that's going to translate very
501:04 so that's going to translate very straightforwardly to our mathematical
501:06 straightforwardly to our mathematical formula do something n minus one times
501:09 formula do something n minus one times this Loop just because I'm using four
501:11 this Loop just because I'm using four Loop terminology it's framed a little
501:13 Loop terminology it's framed a little differently but but if you're iterating
501:15 differently but but if you're iterating from 0 to nus 2 you're iterating a total
501:20 from 0 to nus 2 you're iterating a total of nus1 times and again the arithmetic
501:23 of nus1 times and again the arithmetic is getting a little annoying but this
501:24 is getting a little annoying but this just means do the following n minus one
501:26 just means do the following n minus one times so do n minus one Things N minus
501:29 times so do n minus one Things N minus one times we can now uh run out the math
501:32 one times we can now uh run out the math as follows we have the formula n minus1
501:35 as follows we have the formula n minus1 * n minus1 we do our little foil method
501:38 * n minus1 we do our little foil method here N2 -1 * N - 1 * n + 1 we can
501:42 here N2 -1 * N - 1 * n + 1 we can combine like terms n^ 2 - 2 n plus one
501:45 combine like terms n^ 2 - 2 n plus one but at this point when n gets really
501:47 but at this point when n gets really large which term are we really going to
501:49 large which term are we really going to care about this is on the order
501:51 care about this is on the order of yeah n s so at least ASM totically
501:55 of yeah n s so at least ASM totically ASM totically means as n approaches
501:58 ASM totically means as n approaches Infinity gets really large turns out
502:00 Infinity gets really large turns out that the upper bounds on selection sort
502:02 that the upper bounds on selection sort and bubble sort are essentially the same
502:04 and bubble sort are essentially the same now if we really nitpicked and compared
502:06 now if we really nitpicked and compared the total number of comparisons they
502:07 the total number of comparisons they might differ slightly but as n gets
502:09 might differ slightly but as n gets large honestly you're barely going to
502:10 large honestly you're barely going to notice the difference it would seem
502:12 notice the difference it would seem between these two algorithms
502:15 between these two algorithms but what about um the lower bound if the
502:18 but what about um the lower bound if the upper bound on Bubble sword is also Big
502:20 upper bound on Bubble sword is also Big O of n what about the lower bound here
502:24 O of n what about the lower bound here well with this pseudo code what would
502:26 well with this pseudo code what would the lower bound be on bubble sort even
502:29 the lower bound be on bubble sort even in the best case when all of the
502:30 in the best case when all of the volunteers are
502:31 volunteers are sorted any
502:34 sorted any intuition in this pseudo code yeah in
502:36 intuition in this pseudo code yeah in the
502:45 middle good question isn't bubble sorts
502:46 good question isn't bubble sorts designed such that you wouldn't need to
502:48 designed such that you wouldn't need to compare numbers that have already uh
502:51 compare numbers that have already uh bubbled up that's what's happening here
502:54 bubbled up that's what's happening here in the middle implicitly I'm always
502:56 in the middle implicitly I'm always going from left to right but remember
502:59 going from left to right but remember that even when I screwed up at the end
503:00 that even when I screwed up at the end and the last two people were out of
503:01 and the last two people were out of order I do always need to restart at the
503:04 order I do always need to restart at the beginning because the big numbers are
503:05 beginning because the big numbers are going that way and the small numbers are
503:07 going that way and the small numbers are coming this
503:13 way so that is true there are some slight optimizations that I'm kind of
503:15 slight optimizations that I'm kind of glossing over here let me stipulate that
503:17 glossing over here let me stipulate that it would still end up being on the order
503:18 it would still end up being on the order of n squ but that would definitely shave
503:20 of n squ but that would definitely shave off some actual running time here but
503:23 off some actual running time here but what if the list is already sorted our
503:25 what if the list is already sorted our our pseudo code at the moment has no
503:26 our pseudo code at the moment has no allowance for if list is already sorted
503:29 allowance for if list is already sorted quit early so we're going to blindly do
503:31 quit early so we're going to blindly do n minus one Things N minus one times
503:33 n minus one Things N minus one times unless we modify our pseudo code as I
503:36 unless we modify our pseudo code as I did verbally earlier I propose this
503:39 did verbally earlier I propose this inside of that outer loop if you make a
503:41 inside of that outer loop if you make a pass across all of the volunteers and
503:43 pass across all of the volunteers and your mental counter has made no swaps
503:45 your mental counter has made no swaps you have to keep track with some kind of
503:47 you have to keep track with some kind of variable well then you might as well
503:48 variable well then you might as well stop because if you do a whole pass and
503:50 stop because if you do a whole pass and make no swaps why would you waste time
503:52 make no swaps why would you waste time doing it again expecting different
503:55 doing it again expecting different Behavior so to help visualize these
503:59 Behavior so to help visualize these whereby now bubble sort can be
504:01 whereby now bubble sort can be advantageous if the data is already
504:03 advantageous if the data is already sorted or mostly sorted why because it
504:06 sorted or mostly sorted why because it does have this short circuit detail at
504:08 does have this short circuit detail at least if we implement it like that how
504:10 least if we implement it like that how can we go about um visualizing these
504:13 can we go about um visualizing these things a little more clearly well let me
504:15 things a little more clearly well let me go ahead and do this let me pull up here
504:17 go ahead and do this let me pull up here a visualization of exactly these
504:20 a visualization of exactly these algorithms thanks to a third party tool
504:22 algorithms thanks to a third party tool here that's going to help us visualize
504:24 here that's going to help us visualize these sorting algorithms as follows
504:25 these sorting algorithms as follows small bars represent small numbers big
504:27 small bars represent small numbers big bars represent big numbers and so the
504:29 bars represent big numbers and so the idea now is when I hit a button here to
504:32 idea now is when I hit a button here to get all of the small bars this way all
504:34 get all of the small bars this way all of the big bars this way so just like
504:36 of the big bars this way so just like our volunteers but instead of holding
504:37 our volunteers but instead of holding lighted numbers its bars representing
504:40 lighted numbers its bars representing their magnitude so let's go ahead and
504:42 their magnitude so let's go ahead and start with for instance select
504:45 start with for instance select sort and you'll see in pink is being
504:48 sort and you'll see in pink is being highlighted the current number that is
504:50 highlighted the current number that is being selected and then pulled all the
504:52 being selected and then pulled all the way to the left so this is selection
504:54 way to the left so this is selection sort and again it's selecting the next
504:58 sort and again it's selecting the next smallest element but you can see here
505:00 smallest element but you can see here all the more visibly that just like my
505:03 all the more visibly that just like my human feet were taking a lot of steps so
505:06 human feet were taking a lot of steps so is this algorithm touching these
505:08 is this algorithm touching these elements again and again and again and
505:11 elements again and again and again and this is why the N squ is really a thing
505:13 this is why the N squ is really a thing there's got to be some inherent
505:15 there's got to be some inherent redundancy here like why do we keep
505:17 redundancy here like why do we keep looking at the same darn elements again
505:18 looking at the same darn elements again and again we do in terms of our pseudo
505:20 and again we do in terms of our pseudo code need to do so but it's this
505:22 code need to do so but it's this redundant comparisons that kind of
505:24 redundant comparisons that kind of explains why n s is indeed the case so
505:27 explains why n s is indeed the case so now it's done small bars here big bars
505:29 now it's done small bars here big bars there and I had to just keep talking
505:31 there and I had to just keep talking there to kill time because it's
505:32 there to kill time because it's relatively slow well let me randomize
505:34 relatively slow well let me randomize the array just so we start with a
505:36 the array just so we start with a different order and now let me click on
505:38 different order and now let me click on bubbl sore and you'll see similar idea
505:40 bubbl sore and you'll see similar idea but different algorithm so now the two
505:43 but different algorithm so now the two bars in pink are the two that are being
505:45 bars in pink are the two that are being compared and fixed potentially if
505:47 compared and fixed potentially if they're out of order and you can see
505:49 they're out of order and you can see already that the biggest bars are
505:50 already that the biggest bars are bubbling their way up to the top but now
505:54 bubbling their way up to the top but now you can also see like this redundancy
505:56 you can also see like this redundancy like we keep swooping through the list
505:58 like we keep swooping through the list again and again just like I kept walking
506:00 again and again just like I kept walking back and forth and this is n squar this
506:02 back and forth and this is n squar this is not that many bars what 10 20 there's
506:04 is not that many bars what 10 20 there's like 40 or something bars I'm guessing
506:06 like 40 or something bars I'm guessing that's pretty slow already just to sort
506:09 that's pretty slow already just to sort 40 numbers and I think it's going to get
506:11 40 numbers and I think it's going to get tedious if I keep talking over this so
506:13 tedious if I keep talking over this so let's just assume that this two is
506:15 let's just assume that this two is relatively slow had I gotten lucky and
506:18 relatively slow had I gotten lucky and the list were almost sorted already
506:20 the list were almost sorted already bubble sort would have been pretty fast
506:22 bubble sort would have been pretty fast but this was a truly random array so we
506:24 but this was a truly random array so we did not get lucky so indeed the worst
506:26 did not get lucky so indeed the worst case might be what's kicking in here so
506:29 case might be what's kicking in here so I don't I feel like it'll be
506:31 I don't I feel like it'll be anticlimactic like holding in a sneeze
506:33 anticlimactic like holding in a sneeze if I don't let you see the end of this
506:34 if I don't let you see the end of this so here we go nothing interesting is
506:37 so here we go nothing interesting is about to happen almost
506:39 about to happen almost done ah okay done all right so thank you
506:45 done ah okay done all right so thank you thank
506:47 thank you so still somewhat slow though how
506:51 you so still somewhat slow though how though can we perhaps do a little better
506:53 though can we perhaps do a little better fundamentally so we can do so if we
506:56 fundamentally so we can do so if we introduce yet another technique and this
506:58 introduce yet another technique and this one isn't so much a function of code as
507:00 one isn't so much a function of code as it is concept and it's something that
507:01 it is concept and it's something that you might have seen in the real world um
507:04 you might have seen in the real world um but perhaps not so obviously so so it
507:06 but perhaps not so obviously so so it turns out in programming recursion
507:10 turns out in programming recursion refers to the ability of a function to
507:12 refers to the ability of a function to call itself in the world of mathematics
507:15 call itself in the world of mathematics if you have a function f if F appears on
507:17 if you have a function f if F appears on both the left side and the right side of
507:19 both the left side and the right side of a formula that would be a recursive
507:21 a formula that would be a recursive function in the math world too whenever
507:23 function in the math world too whenever f is defined in terms of itself or in
507:24 f is defined in terms of itself or in our case in Compu in programming anytime
507:28 our case in Compu in programming anytime a function calls itself that function is
507:31 a function calls itself that function is said to be recursive and this is
507:32 said to be recursive and this is actually something we've seen already in
507:34 actually something we've seen already in class even though we didn't call it as
507:35 class even though we didn't call it as much so for instance consider this
507:38 much so for instance consider this pseudo code um from earlier whereby this
507:41 pseudo code um from earlier whereby this was the pseudo code for searching via AR
507:44 was the pseudo code for searching via AR search a whole bunch of doors if no
507:47 search a whole bunch of doors if no doors are left return false that was the
507:49 doors are left return false that was the the additional conditional we added but
507:51 the additional conditional we added but then if number behind middle door return
507:53 then if number behind middle door return true and here's the interesting part if
507:55 true and here's the interesting part if number is less than middle door search
507:58 number is less than middle door search the left half else if number is greater
508:00 the left half else if number is greater than middle door search the right half
508:03 than middle door search the right half this pseudo code earlier was itself
508:05 this pseudo code earlier was itself recursive why because here is an
508:08 recursive why because here is an algorithm for searching but what's the
508:10 algorithm for searching but what's the algorithm telling us well on this line
508:12 algorithm telling us well on this line and this line it's telling us to search
508:15 and this line it's telling us to search something else so even though it's not
508:17 something else so even though it's not explicitly defined in code as having a
508:19 explicitly defined in code as having a name if this is a search algorithm and
508:21 name if this is a search algorithm and yet the search algorithm is using a
508:22 yet the search algorithm is using a search algorithm this pseudo code is
508:25 search algorithm this pseudo code is recursive now that could quickly get you
508:27 recursive now that could quickly get you into trouble if a function just calls
508:29 into trouble if a function just calls itself again and again and again but why
508:33 itself again and again and again but why intuitively is it not problematic that
508:36 intuitively is it not problematic that this code this pseudo code calls
508:39 this code this pseudo code calls itself why will the algorithm still stop
508:43 itself why will the algorithm still stop yeah
508:48 exactly it has some exit condition like if no do is less and more more
508:50 if no do is less and more more importantly anytime you search the left
508:53 importantly anytime you search the left half you're searching a smaller version
508:55 half you're searching a smaller version of the problem anytime you search the
508:56 of the problem anytime you search the right half you're searching a smaller
508:58 right half you're searching a smaller version of the problem literally half
509:00 version of the problem literally half the size so this is why in the phone
509:02 the size so this is why in the phone book obviously I couldn't tear the phone
509:03 book obviously I couldn't tear the phone book in half uh infinitely many times
509:06 book in half uh infinitely many times because it was literally getting smaller
509:07 because it was literally getting smaller each time so recursion is this ability
509:09 each time so recursion is this ability to call yourself if you will it's but
509:12 to call yourself if you will it's but what's important is that you do it on a
509:13 what's important is that you do it on a small smaller smaller problem so that
509:15 small smaller smaller problem so that eventually you have no more problems to
509:18 eventually you have no more problems to solve or no more data no more doors at
509:20 solve or no more data no more doors at all so these two lines here would be the
509:23 all so these two lines here would be the recursive elements here but if we go
509:25 recursive elements here but if we go back to week zero we could have used
509:27 back to week zero we could have used recursion in some other way so this was
509:28 recursion in some other way so this was our Cudo code for the phone book back in
509:31 our Cudo code for the phone book back in week zero and recall that we described
509:33 week zero and recall that we described these yellow lines as really
509:34 these yellow lines as really representing a loop some kind of cycle
509:36 representing a loop some kind of cycle again and again but there was a missed
509:38 again and again but there was a missed opportunity here what if I had reinvent
509:41 opportunity here what if I had reinvent reimplemented this code to do this
509:43 reimplemented this code to do this instead
509:44 instead instead of saying open to middle of left
509:47 instead of saying open to middle of left half of book and then go back to line
509:49 half of book and then go back to line three like literally inducing a loop or
509:51 three like literally inducing a loop or open to Middle right half a book and go
509:53 open to Middle right half a book and go back to line three inducing another loop
509:55 back to line three inducing another loop why don't I just recognize that what I'm
509:57 why don't I just recognize that what I'm staring at now is a algorithm for
510:00 staring at now is a algorithm for searching a phone book and if you want
510:03 searching a phone book and if you want to search a smaller phone book like a
510:05 to search a smaller phone book like a through M or n through Z we'll just use
510:08 through M or n through Z we'll just use the same algorithm so I can replace
510:10 the same algorithm so I can replace these yellow lines with just this
510:13 these yellow lines with just this casually speaking search left half a
510:14 casually speaking search left half a book search right half a book this would
510:16 book search right half a book this would be implicitly and now I can shorten the
510:18 be implicitly and now I can shorten the whole thing a recursive implementation
510:20 whole thing a recursive implementation of the phone book pseudo code from week
510:22 of the phone book pseudo code from week zero and it's recursive because if this
510:23 zero and it's recursive because if this is a search algorithm and you're saying
510:25 is a search algorithm and you're saying go search something else that's fine
510:27 go search something else that's fine that's recursive but because you're
510:29 that's recursive but because you're searching half of the phone book it's
510:31 searching half of the phone book it's indeed going to get smaller and smaller
510:34 indeed going to get smaller and smaller even in the real world or the real
510:37 even in the real world or the real virtual world you can see recursive data
510:39 virtual world you can see recursive data structures in the wild or at least in
510:41 structures in the wild or at least in Super Mario Brothers like this let me
510:42 Super Mario Brothers like this let me get rid of all the distractions here and
510:44 get rid of all the distractions here and focus on this pyramid where you have one
510:47 focus on this pyramid where you have one block then two then three then four well
510:50 block then two then three then four well this itself is technically recursively
510:52 this itself is technically recursively defined in the sense that well what is a
510:54 defined in the sense that well what is a pyramid of height four well it's really
510:57 pyramid of height four well it's really what how would you describe a pyramid of
510:59 what how would you describe a pyramid of height four is actually the same thing
511:01 height four is actually the same thing as a pyramid
511:08 of of of height three plus one additional layer well what's a pyramid
511:10 additional layer well what's a pyramid of height three well it's technically a
511:11 of height three well it's technically a pyramid of height two plus one
511:14 pyramid of height two plus one additional layer and so even physical
511:16 additional layer and so even physical structures can be recursive if you can
511:17 structures can be recursive if you can Define them in terms of itself now at
511:19 Define them in terms of itself now at some point you have to say that if the
511:21 some point you have to say that if the pyramid is of height one there's just
511:23 pyramid is of height one there's just one block you can't forever say it's
511:26 one block you can't forever say it's defined in terms of a height negative 1
511:27 defined in terms of a height negative 1 negative2 you would never stop so you
511:29 negative2 you would never stop so you have to kind of have a special case
511:31 have to kind of have a special case there but let's go ahead and translate
511:32 there but let's go ahead and translate something like this in fact to code let
511:34 something like this in fact to code let me go back to uh vs code here and let me
511:37 me go back to uh vs code here and let me Implement a program called iteration
511:39 Implement a program called iteration that refers to a loop iterating and let
511:42 that refers to a loop iterating and let me Implement a very simple pyramid like
511:44 me Implement a very simple pyramid like that so let me go ahead and include the
511:45 that so let me go ahead and include the cs50 library I'll include our standard
511:49 cs50 library I'll include our standard i.h in main void no command line
511:52 i.h in main void no command line arguments today and let's go ahead and
511:54 arguments today and let's go ahead and do this let's declare a variable called
511:56 do this let's declare a variable called height ask the human for the height of
511:58 height ask the human for the height of this pyramid and then let's go ahead and
512:02 this pyramid and then let's go ahead and draw a pyramid of that height now of
512:04 draw a pyramid of that height now of course draw does not yet exist so I'm
512:06 course draw does not yet exist so I'm going to need to invent the draw a
512:08 going to need to invent the draw a function let me go ahead and Define a
512:10 function let me go ahead and Define a function that doesn't have a return
512:11 function that doesn't have a return value it's just going to have side
512:12 value it's just going to have side effects it's just going to print bricks
512:14 effects it's just going to print bricks on the screen call draw and it takes in
512:16 on the screen call draw and it takes in an integer n as its input and how am I
512:19 an integer n as its input and how am I going to implement this well again I
512:21 going to implement this well again I want to print one block then two then
512:24 want to print one block then two then three then four that's pretty
512:26 three then four that's pretty straightforward at least once you're
512:27 straightforward at least once you're comfortable with loops let me go back to
512:29 comfortable with loops let me go back to the code here let me go ahead and say 4
512:32 the code here let me go ahead and say 4 into I gets zero I is less than n i ++
512:37 into I gets zero I is less than n i ++ and that's going to iterate essentially
512:38 and that's going to iterate essentially row by row and on each row I want to
512:41 row by row and on each row I want to print out one then two then three then
512:44 print out one then two then three then four bricks but I'm iterating from Z to
512:46 four bricks but I'm iterating from Z to 1 to two to three so I think that's okay
512:48 1 to two to three so I think that's okay I can just say something like 4 in J
512:50 I can just say something like 4 in J gets zero J Let's Be Clever about this
512:54 gets zero J Let's Be Clever about this is less than I
512:57 is less than I j++ and now let me go ahead and inside
513:00 j++ and now let me go ahead and inside of this loop I think I can get away with
513:02 of this loop I think I can get away with just printing out a single hash sign but
513:06 just printing out a single hash sign but then outside of that Loop similar to
513:08 then outside of that Loop similar to last week I'm going to print my new line
513:10 last week I'm going to print my new line separately so a little non-obvious at
513:13 separately so a little non-obvious at first but this outer loop iterates row
513:15 first but this outer loop iterates row by row line by line if you will and then
513:18 by row line by line if you will and then the inner loop just Mak sure that when I
513:21 the inner loop just Mak sure that when I equals zero um let's see oh nope there's
513:26 equals zero um let's see oh nope there's a bug I need to make sure that it's j is
513:29 a bug I need to make sure that it's j is less than I + 1 so when I is zero on my
513:33 less than I + 1 so when I is zero on my first line of output I'm going to print
513:35 first line of output I'm going to print out one brick when I is one I'm going to
513:38 out one brick when I is one I'm going to print out two bricks and so forth so let
513:41 print out two bricks and so forth so let me go ahead and run make iteration
513:44 me go ahead and run make iteration all right and now seems to compile
513:48 all right and now seems to compile uhoh huh implicit Declaration of
513:50 uhoh huh implicit Declaration of function draw so I'm making week one
513:52 function draw so I'm making week one mistakes
513:54 mistakes again what say
513:56 again what say again yeah the the Prototype is missing
513:59 again yeah the the Prototype is missing I didn't declare it at the top that's an
514:00 I didn't declare it at the top that's an easy fix and the only time really it's
514:02 easy fix and the only time really it's okay and necessary to copy paste let me
514:04 okay and necessary to copy paste let me copy the functions declaration there end
514:06 copy the functions declaration there end it with a semicolon so that clang now
514:09 it with a semicolon so that clang now knows that draw will exist make
514:11 knows that draw will exist make iteration now it works thank you /
514:14 iteration now it works thank you / iteration we'll type in something like
514:16 iteration we'll type in something like four and there we have it our pyramid of
514:18 four and there we have it our pyramid of height 1 2 3 4 that looks pretty similar
514:21 height 1 2 3 4 that looks pretty similar to this albeit using hashes so that's
514:23 to this albeit using hashes so that's how we would have implemented this like
514:24 how we would have implemented this like two weeks ago in week one maybe last
514:26 two weeks ago in week one maybe last week but just using arrays but let me
514:28 week but just using arrays but let me propose that we could do something
514:31 propose that we could do something recursively instead let me close this
514:33 recursively instead let me close this version of the code and let me go back
514:35 version of the code and let me go back to VSS code and open up recursion doc
514:38 to VSS code and open up recursion doc just to demonstrate something
514:39 just to demonstrate something recursively and I'll do it incorrectly
514:42 recursively and I'll do it incorrectly deliberately the first time so let me
514:43 deliberately the first time so let me include cs50.h let me include standard
514:46 include cs50.h let me include standard i.h let me do uh int main void and let
514:51 i.h let me do uh int main void and let me just blindly draw a pyramid initially
514:55 me just blindly draw a pyramid initially of height one but now in my draw
514:58 of height one but now in my draw function let me reimplement it a little
514:59 function let me reimplement it a little differently so my draw function this
515:02 differently so my draw function this time is still going to take a number n
515:03 time is still going to take a number n but that's how many hashes it's going to
515:05 but that's how many hashes it's going to print so let's do four into I gets zero
515:09 print so let's do four into I gets zero I is less than n i
515:12 I is less than n i ++ then let's go ahead and print out a
515:14 ++ then let's go ahead and print out a single hash mark here and then after
515:18 single hash mark here and then after that let's print out the end of the line
515:22 that let's print out the end of the line just as before but now this of course is
515:24 just as before but now this of course is only going to draw a single um row it's
515:28 only going to draw a single um row it's going to print out one hash or two
515:30 going to print out one hash or two hashes or three hashes but only on one
515:33 hashes or three hashes but only on one line let me now incorrectly but just
515:35 line let me now incorrectly but just kind of curiously say all right well if
515:38 kind of curiously say all right well if this draws a pyramid of height one let's
515:40 this draws a pyramid of height one let's just use ourself to draw a pyramid of
515:42 just use ourself to draw a pyramid of height n plus one so the first time I
515:44 height n plus one so the first time I call draw it will print out one hash
515:47 call draw it will print out one hash then the second time I call draw it will
515:49 then the second time I call draw it will print out two hashes then three then
515:52 print out two hashes then three then four so we're kind of laying These
515:53 four so we're kind of laying These Bricks down from top to bottom uh make
515:58 Bricks down from top to bottom uh make recursion uh oops I screwed up again so
516:01 recursion uh oops I screwed up again so let's copy the Prototype here let's put
516:03 let's copy the Prototype here let's put this down over here semicolon let's do
516:06 this down over here semicolon let's do this again uh make recursion all right
516:09 this again uh make recursion all right all good/ recursion and now let me
516:12 all good/ recursion and now let me increase the size of my terminal window
516:13 increase the size of my terminal window just so you can see more of the output
516:16 just so you can see more of the output and here we
516:17 and here we have okay bad but thank you so we have
516:20 have okay bad but thank you so we have an infinitely tall pyramid and it's just
516:23 an infinitely tall pyramid and it's just flying across the screen which is why it
516:24 flying across the screen which is why it looks kind of like a mess but I printed
516:26 looks kind of like a mess but I printed out a pyramid of height one and then two
516:28 out a pyramid of height one and then two and then three and then four and
516:30 and then three and then four and unfortunately what am I lacking any sort
516:32 unfortunately what am I lacking any sort of quick condition any kind of condition
516:35 of quick condition any kind of condition that says wait a minute when it's too
516:36 that says wait a minute when it's too tall stop all together so this is an
516:38 tall stop all together so this is an infinite Loop but it's not a loop it's a
516:40 infinite Loop but it's not a loop it's a recursive call and actually doing this
516:43 recursive call and actually doing this in en is very bad we'll see next week
516:45 in en is very bad we'll see next week that if you call a function too many
516:46 that if you call a function too many times you can actually trigger yet one
516:48 times you can actually trigger yet one yet another of those segmentation faults
516:51 yet another of those segmentation faults cuz you're using too much memory
516:52 cuz you're using too much memory essentially but for now I haven't
516:54 essentially but for now I haven't triggered that yet control C is your
516:56 triggered that yet control C is your friend to cancel and as an aside if
516:58 friend to cancel and as an aside if you're playing along at home or play
516:59 you're playing along at home or play with this code later I actually cheated
517:01 with this code later I actually cheated here we have a special clang
517:02 here we have a special clang configuration feature that prevents you
517:05 configuration feature that prevents you from calling a function like that and
517:07 from calling a function like that and creating a problem I overrode it just to
517:09 creating a problem I overrode it just to for demonstration sake but odds are at
517:11 for demonstration sake but odds are at home you wouldn't be able to compile
517:12 home you wouldn't be able to compile this code yourself but let me do a
517:14 this code yourself but let me do a proper version recursively of this code
517:17 proper version recursively of this code as follows let me go back into the code
517:20 as follows let me go back into the code here let me go ahead and not just
517:21 here let me go ahead and not just blindly start drawing one then two then
517:24 blindly start drawing one then two then three layers of bricks let me prompt the
517:26 three layers of bricks let me prompt the human as before for the height of the
517:28 human as before for the height of the pyramid they want using our get in
517:31 pyramid they want using our get in function and now let me call draw of
517:33 function and now let me call draw of height again so now I'm going back to
517:35 height again so now I'm going back to the looplike version but instead of
517:38 the looplike version but instead of using a loop now this is where recursion
517:40 using a loop now this is where recursion gets rather elegant if you will let me
517:43 gets rather elegant if you will let me go ahead and execute and code uh the
517:46 go ahead and execute and code uh the draw function as
517:48 draw function as follows per your definition if a pyramid
517:52 follows per your definition if a pyramid of height four is really just a pyramid
517:53 of height four is really just a pyramid of height three plus another row well
517:56 of height three plus another row well let's take that literally let me go back
517:57 let's take that literally let me go back to my code and if you want to draw a
518:00 to my code and if you want to draw a pyramid of height four well go right
518:02 pyramid of height four well go right ahead and draw a pyramid of height uh
518:05 ahead and draw a pyramid of height uh three first or more generally n minus
518:07 three first or more generally n minus one but what's the second step well once
518:09 one but what's the second step well once you've drawn a pyramid of height three
518:11 you've drawn a pyramid of height three draw an extra row so I at least have to
518:14 draw an extra row so I at least have to bite off that part of the problem myself
518:15 bite off that part of the problem myself so let me just do for in I gets zero I
518:18 so let me just do for in I gets zero I is less than n i++ and let me the
518:21 is less than n i++ and let me the programmer of this function print out my
518:24 programmer of this function print out my hashes and then at the very bottom print
518:26 hashes and then at the very bottom print out a new line so the cursor moves to
518:28 out a new line so the cursor moves to the next line but this is kind of
518:30 the next line but this is kind of elegant now I dare say in that draw is
518:33 elegant now I dare say in that draw is recursive because I'm literally
518:35 recursive because I'm literally translating from English to code this
518:37 translating from English to code this idea that a pyramid of height four is
518:39 idea that a pyramid of height four is really just a pyramid of height three so
518:41 really just a pyramid of height three so I do that first and I'm sort of trusting
518:44 I do that first and I'm sort of trusting that this will work then I just have to
518:46 that this will work then I just have to lay one more layer of bricks four of
518:48 lay one more layer of bricks four of them so if n is four this is just a
518:50 them so if n is four this is just a simple for Loop Allah week one that will
518:52 simple for Loop Allah week one that will print out an additional layer but this
518:54 print out an additional layer but this of course is going to be problematic
518:56 of course is going to be problematic eventually why it's not done yet this
519:00 eventually why it's not done yet this program how many times will draw call
519:03 program how many times will draw call itself in this
519:14 why yeah there's no there's no equivalent of quit like if you've
519:16 equivalent of quit like if you've printed enough already then quit well
519:18 printed enough already then quit well how do we capture that well I don't
519:20 how do we capture that well I don't think we want this to go negative right
519:22 think we want this to go negative right it would make no sense to draw a
519:23 it would make no sense to draw a negative height pyramid so I think we
519:26 negative height pyramid so I think we can just pluck off as the programmer a
519:28 can just pluck off as the programmer a an easy case an easy answer a so-called
519:32 an easy case an easy answer a so-called base case and I'm just going to do this
519:33 base case and I'm just going to do this at the top of my draw function let me
519:35 at the top of my draw function let me just say if n is less than or heck less
519:39 just say if n is less than or heck less than or equal to zero that's it go ahead
519:42 than or equal to zero that's it go ahead and just return there's nothing more to
519:44 and just return there's nothing more to do and that simple condition technically
519:47 do and that simple condition technically known as a base case will ensure that
519:50 known as a base case will ensure that the code doesn't run forever why well
519:52 the code doesn't run forever why well suppose that draw uh is called with an
519:55 suppose that draw uh is called with an argument of four four is of course not
519:57 argument of four four is of course not less than zero so we don't return but we
519:59 less than zero so we don't return but we do draw a pyramid of height three and
520:01 do draw a pyramid of height three and here's where things get a little
520:02 here's where things get a little mentally tricky you don't move on to
520:04 mentally tricky you don't move on to line 20 until draw has been called so
520:07 line 20 until draw has been called so when draw is called with an argument of
520:09 when draw is called with an argument of three it's as though you're executing
520:11 three it's as though you're executing from the top of this function again
520:13 from the top of this function again three is not less than zero so what do
520:14 three is not less than zero so what do you do you draw two okay how do you draw
520:18 you do you draw two okay how do you draw two well two is not less than zero so
520:19 two well two is not less than zero so you don't return so you draw one got to
520:22 you don't return so you draw one got to be careful here draw one and now we go
520:25 be careful here draw one and now we go ahead back to the beginning how do you
520:26 ahead back to the beginning how do you draw One well one is not less than zero
520:28 draw One well one is not less than zero so you don't return you draw height zero
520:32 so you don't return you draw height zero how do you draw height zero wait a
520:33 how do you draw height zero wait a minute 0o is less than or equal to zero
520:36 minute 0o is less than or equal to zero and you return and so it's kind of like
520:39 and you return and so it's kind of like this mental stack this to-do list you
520:41 this mental stack this to-do list you keep postponing executing these lower
520:43 keep postponing executing these lower lines of code because you keep
520:45 lines of code because you keep restarting restarting restarting the
520:46 restarting restarting restarting the draw function until finally one of those
520:49 draw function until finally one of those function calls says there's nothing to
520:51 function calls says there's nothing to do return and now the whole thing starts
520:53 do return and now the whole thing starts to unravel if you will and you pick back
520:56 to unravel if you will and you pick back up where you left off and this is
520:57 up where you left off and this is perhaps the best uh scenario we won't do
520:59 perhaps the best uh scenario we won't do it in class but if you'd like to wrestle
521:00 it in class but if you'd like to wrestle through this on your own using debug 50
521:03 through this on your own using debug 50 to keep stepping into step into step
521:05 to keep stepping into step into step into each of those lines logically
521:07 into each of those lines logically you'll see exactly what's actually
521:09 you'll see exactly what's actually happening so let me go to my terminal
521:11 happening so let me go to my terminal and do make recursion which is now this
521:14 and do make recursion which is now this correct version of the code do/
521:15 correct version of the code do/ recursion let's type in a height of four
521:18 recursion let's type in a height of four and voila now we have that same pyramid
521:21 and voila now we have that same pyramid not using iteration per se though
521:23 not using iteration per se though admittedly we're using iteration to
521:25 admittedly we're using iteration to print the additional layer we're now
521:27 print the additional layer we're now using draw recursively to print all of
521:31 using draw recursively to print all of the smaller pyramids that need come
521:32 the smaller pyramids that need come before
521:38 it no question is can you only use recursion with a void function no not at
521:40 recursion with a void function no not at all in fact it's very common to have a
521:42 all in fact it's very common to have a return value like an integer or
521:44 return value like an integer or something else so that uh you can
521:46 something else so that uh you can actually do something constructively
521:48 actually do something constructively with that actual value other questions
521:50 with that actual value other questions on
522:00 lad when is line 21 getting executed so if you continue to UNR let me uh scroll
522:03 if you continue to UNR let me uh scroll down a bit more so you can see the top
522:04 down a bit more so you can see the top of the code so line 21 will be executed
522:09 of the code so line 21 will be executed once line 19 is done executing itself
522:14 once line 19 is done executing itself now in the story I told we kept calling
522:16 now in the story I told we kept calling draw again again again but as soon as
522:20 draw again again again but as soon as one of those function calls where n
522:22 one of those function calls where n equals z returns immediately then we
522:25 equals z returns immediately then we don't keep drawing again and again so
522:27 don't keep drawing again and again so now if you kind of think of the process
522:28 now if you kind of think of the process as reversing then you continue to line
522:31 as reversing then you continue to line 21 then a line 21 again then line 21
522:35 21 then a line 21 again then line 21 again and as the sort of logic unravels
522:38 again and as the sort of logic unravels and next week we'll actually paint a
522:39 and next week we'll actually paint a picture of what's actually happening in
522:41 picture of what's actually happening in the computer's memory but for now it's
522:43 the computer's memory but for now it's just it's very similar to the pseudo
522:45 just it's very similar to the pseudo code for the phone book you're just
522:47 code for the phone book you're just searching again and again but you're
522:48 searching again and again but you're waiting until the very end to get back
522:51 waiting until the very end to get back the final result uh can Google Now whom
522:54 the final result uh can Google Now whom I keep mentioning by coincidence today
522:55 I keep mentioning by coincidence today is full of programmers of course um
522:57 is full of programmers of course um here's a fun exercise let me uh go back
523:01 here's a fun exercise let me uh go back to a browser I'm going to go ahead and
523:03 to a browser I'm going to go ahead and search for recursion because I want to
523:04 search for recursion because I want to learn a little something about recursion
523:06 learn a little something about recursion uh here is kind of an internet meme or
523:08 uh here is kind of an internet meme or joke if I zoom in here the engineers at
523:11 joke if I zoom in here the engineers at Google are kind of funny funny see
523:21 why ah there we go yes yes this is recursion and there's going to be so
523:22 recursion and there's going to be so many memes you'll come across now where
523:24 many memes you'll come across now where recursion like if you've ever pointed a
523:25 recursion like if you've ever pointed a camera at the TV that's showing the
523:27 camera at the TV that's showing the camera and you sort of see yourself for
523:29 camera and you sort of see yourself for the image again and again that's really
523:30 the image again and again that's really recursion and in that case it only stops
523:32 recursion and in that case it only stops once you hit the base case of a single
523:34 once you hit the base case of a single Pixel um but this is a very funny joke
523:37 Pixel um but this is a very funny joke in some circles uh when it comes to
523:38 in some circles uh when it comes to recursion uh and Google so how can we
523:41 recursion uh and Google so how can we actually use Google or rather how can we
523:43 actually use Google or rather how can we actually use recursion constructively
523:47 actually use recursion constructively well let me propose that we actually
523:49 well let me propose that we actually introduce a third and final algorithm
523:51 introduce a third and final algorithm for sorting that hopefully does better
523:53 for sorting that hopefully does better than the two sorts thus far we've done
523:56 than the two sorts thus far we've done selection sort and bubble sort bubble
523:58 selection sort and bubble sort bubble sort we liked a little better at least
524:00 sort we liked a little better at least in so far is in the best case where the
524:02 in so far is in the best case where the list is already sorted bubble sort is at
524:03 list is already sorted bubble sort is at least smarter and it will actually
524:04 least smarter and it will actually terminate early giving us a better lower
524:07 terminate early giving us a better lower Bound in terms of our Omega notation but
524:09 Bound in terms of our Omega notation but it turns out that recursion and this is
524:11 it turns out that recursion and this is not necessarily a feature of recursion
524:13 not necessarily a feature of recursion but something we can now leverage it
524:15 but something we can now leverage it turns out using recursion we can take a
524:17 turns out using recursion we can take a fundamentally different approach to
524:18 fundamentally different approach to sorting a whole bunch of numbers in such
524:21 sorting a whole bunch of numbers in such a way that we can do far fewer
524:24 a way that we can do far fewer comparisons and ideally speed up our
524:26 comparisons and ideally speed up our final results so here is the pseudo code
524:29 final results so here is the pseudo code for what we're about to see for
524:31 for what we're about to see for something called merge sort and it
524:33 something called merge sort and it really is this tur sort the left half of
524:36 really is this tur sort the left half of numbers sort the right half of numbers
524:39 numbers sort the right half of numbers merge the sorted halves right this is
524:42 merge the sorted halves right this is almost sort of non sensical because I uh
524:44 almost sort of non sensical because I uh if you're asked for an algorithm to sort
524:46 if you're asked for an algorithm to sort and you respond with well sort the left
524:48 and you respond with well sort the left half sort the right half like that's
524:50 half sort the right half like that's being sort of difficult because well I'm
524:51 being sort of difficult because well I'm asking you for a sorting algorithm
524:53 asking you for a sorting algorithm you're just telling me to sort the left
524:54 you're just telling me to sort the left half and the right half but implicit in
524:56 half and the right half but implicit in that last line merging is a pretty
524:58 that last line merging is a pretty powerful feature of this sort now we do
525:01 powerful feature of this sort now we do need another base case at the top so let
525:03 need another base case at the top so let me add this if we find ourselves with a
525:04 me add this if we find ourselves with a list an array of size one well that
525:07 list an array of size one well that array is obviously sorted if there's
525:08 array is obviously sorted if there's only one element in it there's no work
525:10 only one element in it there's no work to be done so that's going to be our
525:12 to be done so that's going to be our base case but allowing us now in just
525:14 base case but allowing us now in just these what uh four six lines of pseudo
525:17 these what uh four six lines of pseudo code to actually sort some elements but
525:19 code to actually sort some elements but let's focus first on just a subset of
525:21 let's focus first on just a subset of this let's consider for a moment what it
525:23 this let's consider for a moment what it means to merge sorted have so Carter has
525:25 means to merge sorted have so Carter has wonderfully come up to volunteer here
525:27 wonderfully come up to volunteer here just to help us reset these numbers
525:29 just to help us reset these numbers suppose that in the middle of the story
525:31 suppose that in the middle of the story we're about to tell we have two sorted
525:34 we're about to tell we have two sorted halves I've already sorted the left half
525:36 halves I've already sorted the left half of these numbers and indeed 2 4 57 is
525:38 of these numbers and indeed 2 4 57 is sorted from smallest to largest and the
525:40 sorted from smallest to largest and the right half appears to be already sorted
525:43 right half appears to be already sorted 0136 already sorted so in my pseudo code
525:46 0136 already sorted so in my pseudo code we're already done sorting the left half
525:48 we're already done sorting the left half and the right half somehow but we'll see
525:49 and the right half somehow but we'll see how in a moment well how do I go about
525:52 how in a moment well how do I go about merging these two halves well because
525:54 merging these two halves well because they're sorted already and you want to
525:56 they're sorted already and you want to merge them in order I think we can flip
525:58 merge them in order I think we can flip down we can hide all but the first
526:01 down we can hide all but the first numbers in each of these subl lists so
526:04 numbers in each of these subl lists so here we have a half that starts with two
526:07 here we have a half that starts with two and I don't really care what the other
526:08 and I don't really care what the other numbers are because they're clearly
526:09 numbers are because they're clearly larger than two I can focus only on Two
526:12 larger than two I can focus only on Two And Z too zero also we know that zero is
526:15 And Z too zero also we know that zero is the smallest there so let's just ignore
526:16 the smallest there so let's just ignore the numbers that Carter kindly flipped
526:17 the numbers that Carter kindly flipped down so how do I merge these two lists
526:21 down so how do I merge these two lists into a new sorted larger list well I
526:23 into a new sorted larger list well I compare the two on my left with the zero
526:26 compare the two on my left with the zero on my right obviously which comes first
526:28 on my right obviously which comes first the zero so let me put this down here
526:30 the zero so let me put this down here and Carter if you want to give us the
526:32 and Carter if you want to give us the next element now I have two sorted
526:34 next element now I have two sorted halves but I've already plucked one off
526:36 halves but I've already plucked one off so now I compare the two against the one
526:38 so now I compare the two against the one one obviously comes next so I'm going to
526:40 one obviously comes next so I'm going to take out the one and put it in place
526:43 take out the one and put it in place here now I'm going to compare the two
526:45 here now I'm going to compare the two halves again two and three which do I
526:46 halves again two and three which do I merge first obviously the two comes next
526:49 merge first obviously the two comes next and now notice each time I do this my
526:51 and now notice each time I do this my hands are theoretically making forward
526:53 hands are theoretically making forward progress I'm not doubling back like I
526:55 progress I'm not doubling back like I kept doing with selection sort or bubble
526:57 kept doing with selection sort or bubble sort back and forth back and forth my
526:59 sort back and forth back and forth my fingers are constantly advancing forward
527:01 fingers are constantly advancing forward and that's going to be a key detail so I
527:03 and that's going to be a key detail so I compare four and three three obviously I
527:06 compare four and three three obviously I compare three and uh I compare four and
527:09 compare three and uh I compare four and six four obviously I compare five five
527:12 six four obviously I compare five five and six five obviously and then I
527:16 and six five obviously and then I compare seven and six six of course and
527:20 compare seven and six six of course and then lastly we have just one element
527:21 then lastly we have just one element left and even though I'm kind of moving
527:23 left and even though I'm kind of moving awkwardly as a human my hands
527:25 awkwardly as a human my hands technically were only moving to the
527:26 technically were only moving to the right I was never looping back doing
527:28 right I was never looping back doing something again and again and that's
527:30 something again and again and that's perhaps the intuition and just enough
527:32 perhaps the intuition and just enough room for the seven so that then is how
527:34 room for the seven so that then is how you would merge two sorted halves we
527:37 you would merge two sorted halves we started with left half sorted right half
527:39 started with left half sorted right half sorted and merging is just like what you
527:40 sorted and merging is just like what you would do as a human and Carter just
527:42 would do as a human and Carter just flips the numbers down so our Focus was
527:43 flips the numbers down so our Focus was only on the smallest elements in each
527:47 only on the smallest elements in each any questions before we Forge ahead with
527:50 any questions before we Forge ahead with what it means then to be merged in this
527:54 what it means then to be merged in this way so now here is an original list we
527:58 way so now here is an original list we deliberately put it at the top because
527:59 deliberately put it at the top because there's one detail of Mer sort that's
528:00 there's one detail of Mer sort that's key mer sort is technically going to use
528:03 key mer sort is technically going to use a little more space and so whereas
528:04 a little more space and so whereas previously we just kept moving our
528:06 previously we just kept moving our humans around and swapping people and
528:08 humans around and swapping people and making sure they stayed ultimately in
528:09 making sure they stayed ultimately in the original positions with mer sort
528:12 the original positions with mer sort pretend ends that here's our original
528:13 pretend ends that here's our original array of uh memory I'm going to need at
528:16 array of uh memory I'm going to need at least one other array of memory and I'm
528:17 least one other array of memory and I'm going to cheat and I'm going to use even
528:19 going to cheat and I'm going to use even more memory but technically I could
528:21 more memory but technically I could actually go back and forth between one
528:22 actually go back and forth between one array and a secondary array but it isn't
528:24 array and a secondary array but it isn't going to it is going to take me more
528:26 going to it is going to take me more space so how do I go about implementing
528:29 space so how do I go about implementing merge sort on this Al on this code well
528:32 merge sort on this Al on this code well let's consider this here's a array of
528:35 let's consider this here's a array of size eight if only one number quit
528:37 size eight if only one number quit obviously not applicable so let's focus
528:38 obviously not applicable so let's focus on the juicy part there sort the left
528:40 on the juicy part there sort the left half of the numbers all right how do I
528:42 half of the numbers all right how do I sort the left half of the numbers I'm
528:44 sort the left half of the numbers I'm going to just nudge them over just to be
528:45 going to just nudge them over just to be clear which is the left half how here is
528:48 clear which is the left half how here is now a sub list of size four how do I
528:51 now a sub list of size four how do I sort the left
528:52 sort the left half well do I have an algorithm for
528:55 half well do I have an algorithm for sorting yeah what do I do here's a list
528:57 sorting yeah what do I do here's a list of size four how do I sort it what's
528:59 of size four how do I sort it what's step one sort the left half so I now
529:02 step one sort the left half so I now sort of conceptually in my mind take
529:04 sort of conceptually in my mind take this sublist of size four and I sort it
529:07 this sublist of size four and I sort it by first sorting the left half focusing
529:10 by first sorting the left half focusing now on the seven and two all right
529:11 now on the seven and two all right here's a list of size two how do I sort
529:14 here's a list of size two how do I sort a list of size
529:16 a list of size two sorry I think we just keep following
529:20 two sorry I think we just keep following our instruction sort the left half all
529:22 our instruction sort the left half all right here is a list of size one how do
529:24 right here is a list of size one how do I sort a list of size
529:27 I sort a list of size one I'm done like it's done so I leave
529:30 one I'm done like it's done so I leave this alone what was the next step in the
529:33 this alone what was the next step in the story I've just sorted the left half of
529:35 story I've just sorted the left half of the left half of the left half what
529:37 the left half of the left half what comes next I sort the right half of the
529:40 comes next I sort the right half of the left half of the left half and I'm done
529:43 left half of the left half and I'm done cuz it's just a list of size one what
529:45 cuz it's just a list of size one what comes after this merge so this is where
529:49 comes after this merge so this is where it gets a little trippy because you have
529:50 it gets a little trippy because you have to remember where we're pausing the
529:52 to remember where we're pausing the story to do things recursively again and
529:54 story to do things recursively again and again but if I've just sorted the left
529:56 again but if I've just sorted the left half and I've just sorted the right half
529:58 half and I've just sorted the right half now I merge them together this is a
530:00 now I merge them together this is a super short list so we don't need um uh
530:02 super short list so we don't need um uh Carter's help here as before but I think
530:04 Carter's help here as before but I think the first number I take here is the two
530:06 the first number I take here is the two and then the second number I take
530:07 and then the second number I take because it's the only option is the
530:09 because it's the only option is the seven but what's nice now is that notice
530:12 seven but what's nice now is that notice the left half of the left half is indeed
530:15 the left half of the left half is indeed sorted because I trivially sorted the
530:17 sorted because I trivially sorted the left half of it and the right half of it
530:19 left half of it and the right half of it but then merging is really where the
530:20 but then merging is really where the magic happens all right again if you rem
530:23 magic happens all right again if you rem if you rewind now in your mind if I've
530:24 if you rewind now in your mind if I've just sorted the left half of the left
530:27 just sorted the left half of the left half what happens
530:29 half what happens next sort the right half of the left
530:33 next sort the right half of the left half so again you kind of rewind in time
530:35 half so again you kind of rewind in time so how do I do this I've got a list of
530:37 so how do I do this I've got a list of size two I sort the left half the just
530:39 size two I sort the left half the just the five done sort the right half four
530:42 the five done sort the right half four done now the interesting part I merge
530:45 done now the interesting part I merge the left half and the right half of the
530:48 the left half and the right half of the right half of the left half so what do I
530:50 right half of the left half so what do I do four comes down here five comes down
530:54 do four comes down here five comes down here and
530:56 here and now notice what I have left half is
530:59 now notice what I have left half is sorted right half is sorted if you
531:02 sorted right half is sorted if you rewind in time where is my next step
531:04 rewind in time where is my next step three merge the two halves and so this
531:07 three merge the two halves and so this is what Carter helped me do before let's
531:08 is what Carter helped me do before let's focus only on the smallest elements just
531:10 focus only on the smallest elements just so there's less distraction I compare
531:11 so there's less distraction I compare the two and the four two comes first so
531:13 the two and the four two comes first so let's obviously put that here now I
531:16 let's obviously put that here now I compare the new beginning of this list
531:18 compare the new beginning of this list and the old beginning of this list four
531:20 and the old beginning of this list four obviously comes next and now I compare
531:23 obviously comes next and now I compare the seven against the five five
531:25 the seven against the five five obviously comes next and now lastly I'm
531:27 obviously comes next and now lastly I'm left with one number so now I'm down to
531:29 left with one number so now I'm down to the seven so even if you've kind of lost
531:31 the seven so even if you've kind of lost track of some of the nuances here if you
531:32 track of some of the nuances here if you just kind of take a step back we have
531:34 just kind of take a step back we have the original right half here still
531:36 the original right half here still untouched but the left half of the
531:38 untouched but the left half of the original input is now indeed sorted all
531:42 original input is now indeed sorted all by way of doing sorting left half right
531:44 by way of doing sorting left half right half left half right half but with those
531:46 half left half right half but with those merges in between all right so if we've
531:48 merges in between all right so if we've just sorted the left half we rewind all
531:51 just sorted the left half we rewind all the way to the beginning what do I now
531:53 the way to the beginning what do I now do all right so sort the right half so
531:56 do all right so sort the right half so sort the right half how do I sort a list
531:58 sort the right half how do I sort a list of size four well I first sort the left
532:00 of size four well I first sort the left half the one and the six how do I sort a
532:02 half the one and the six how do I sort a list of size two you sort the left half
532:04 list of size two you sort the left half just the number one obviously there's no
532:06 just the number one obviously there's no work to be done done sorting the left
532:08 work to be done done sorting the left half six done sorting the right half now
532:12 half six done sorting the right half now what do I do I merge the left half here
532:17 what do I do I merge the left half here with the right half here and that one's
532:20 with the right half here and that one's pretty straightforward now what do I do
532:21 pretty straightforward now what do I do I've just merged so now I sorted I've
532:24 I've just merged so now I sorted I've just sorted the left half of the right
532:25 just sorted the left half of the right half so now I sort the right half of the
532:27 half so now I sort the right half of the right half so I consider the zero done I
532:30 right half so I consider the zero done I consider the three done I now merge
532:32 consider the three done I now merge these two together zero of course comes
532:35 these two together zero of course comes first then comes the three and now I'm
532:37 first then comes the three and now I'm at the point of the story where I've
532:39 at the point of the story where I've sorted the left half of the right half
532:42 sorted the left half of the right half and the right half of the right half so
532:44 and the right half of the right half so step three is merge and I'll do it again
532:47 step three is merge and I'll do it again like we did with Carter all right one
532:48 like we did with Carter all right one and zero obviously the zero comes first
532:51 and zero obviously the zero comes first now compare the one and the three
532:53 now compare the one and the three obviously the one comes first compare
532:55 obviously the one comes first compare the six and the three obviously the
532:56 the six and the three obviously the three and then lastly the sixth so now
532:59 three and then lastly the sixth so now where are we we've taken the left half
533:03 where are we we've taken the left half of the whole thing and sorted the left
533:04 of the whole thing and sorted the left and sorted it we then took the right
533:06 and sorted it we then took the right half of the whole thing and sorted it so
533:09 half of the whole thing and sorted it so now we're at lastly step three for the
533:11 now we're at lastly step three for the last last time what do we do merge and
533:14 last last time what do we do merge and so just to be consistent let me push
533:16 so just to be consistent let me push these down and let's compare left hand
533:19 these down and let's compare left hand to right hand noticing that they only
533:21 to right hand noticing that they only make forward progress none of this back
533:22 make forward progress none of this back and forth comparisons two and zero of
533:25 and forth comparisons two and zero of course the zero so we'll put that in
533:27 course the zero so we'll put that in place two and one of course the one so
533:30 place two and one of course the one so we put that in place two and three we
533:33 we put that in place two and three we merge in of course the two in this case
533:35 merge in of course the two in this case four and three we now merge in the three
533:38 four and three we now merge in the three in this case four and six we now merge
533:42 in this case four and six we now merge of course the four in place and now we
533:45 of course the four in place and now we compare five and six we keep the
533:53 five bug okay well pretend that the five is
533:57 bug okay well pretend that the five is on uh oh this is why all right so now we
534:01 on uh oh this is why all right so now we compare the seven and the six sixth is
534:04 compare the seven and the six sixth is gone and lastly seven is the last one in
534:07 gone and lastly seven is the last one in place and even though I grant that of
534:10 place and even though I grant that of all the algorithms this is probably the
534:11 all the algorithms this is probably the hardest one to stay on top of especially
534:13 hardest one to stay on top of especially when I'm doing it as a voiceover realize
534:15 when I'm doing it as a voiceover realize that what we've just done is only those
534:18 that what we've just done is only those three steps recursively we started with
534:20 three steps recursively we started with a list of size eight we sorted the left
534:22 a list of size eight we sorted the left half we sorted the right half and then
534:23 half we sorted the right half and then we merged the two together but if you go
534:25 we merged the two together but if you go down each of those rabbit holes so to
534:27 down each of those rabbit holes so to speak sorting the left half involves
534:29 speak sorting the left half involves sorting the left half of the left half
534:31 sorting the left half of the left half and the right half of the left half and
534:33 and the right half of the left half and so forth but this germ of an idea of
534:35 so forth but this germ of an idea of really dividing and conquering the
534:37 really dividing and conquering the problem not such that you're having the
534:39 problem not such that you're having the problem and only dealing with one half
534:41 problem and only dealing with one half clearly we're sorting one half and the
534:44 clearly we're sorting one half and the other half and merging them together
534:45 other half and merging them together ultimately it does still lead us to the
534:48 ultimately it does still lead us to the same solution and if we visualize the
534:50 same solution and if we visualize the remnants of this now if I depict this as
534:53 remnants of this now if I depict this as follows where on the screen here you see
534:55 follows where on the screen here you see where the numbers originally started in
534:57 where the numbers originally started in the top row from left to right
534:59 the top row from left to right essentially even though this is in a
535:01 essentially even though this is in a different order I divided that list of
535:03 different order I divided that list of size eight ultimately into eight lists
535:06 size eight ultimately into eight lists of size one and that's where the base
535:08 of size one and that's where the base case kicked in and just said okay we're
535:10 case kicked in and just said okay we're done sorting that and after that
535:12 done sorting that and after that logically I then sorted I merged two
535:15 logically I then sorted I merged two lists of size one into many lists of
535:17 lists of size one into many lists of size two and those lists of size two
535:19 size two and those lists of size two into lists of size four and then finally
535:21 into lists of size four and then finally the list of size four into one big list
535:23 the list of size four into one big list sorted of size eight and so I put forth
535:27 sorted of size eight and so I put forth this picture with the little line
535:28 this picture with the little line indicators here because how many times
535:31 indicators here because how many times did I divide divide divide in half or
535:34 did I divide divide divide in half or really double double double so exponent
535:36 really double double double so exponent is the opposite oh spoiler uh how many
535:40 is the opposite oh spoiler uh how many times did I divide so three concretely
535:43 times did I divide so three concretely but if there's eight elements total and
535:45 but if there's eight elements total and there's N More generally it really is a
535:48 there's N More generally it really is a matter of dividing and conquering login
535:50 matter of dividing and conquering login times you start this and you can divide
535:52 times you start this and you can divide one two three times login times or
535:54 one two three times login times or conversely you can start here and
535:55 conversely you can start here and exponentially uh double double double
535:58 exponentially uh double double double three times which is log n but on every
536:01 three times which is log n but on every row every shelf literally I made a fuss
536:04 row every shelf literally I made a fuss about pointing my hands only from the
536:06 about pointing my hands only from the left to the right constantly advancing
536:08 left to the right constantly advancing them such that every time I did those
536:09 them such that every time I did those merges I touched every element once and
536:12 merges I touched every element once and only once there was none of this back
536:14 only once there was none of this back and forth back and forth on stage so if
536:16 and forth back and forth on stage so if I'm doing something log n times if I'm
536:21 I'm doing something log n times if I'm doing rather N Things log n times what
536:25 doing rather N Things log n times what would be our Big O formula perhaps N
536:28 would be our Big O formula perhaps N Things log n
536:30 Things log n times yeah so n log n the order of n log
536:33 times yeah so n log n the order of n log n is indeed how we would describe the
536:35 n is indeed how we would describe the running time of merge sort and so of all
536:38 running time of merge sort and so of all of the sorts thus far we've seen that
536:40 of the sorts thus far we've seen that merge sort here actually is n log n
536:44 merge sort here actually is n log n which is strictly better than n squ
536:45 which is strictly better than n squ which is where both merge uh both
536:47 which is where both merge uh both selection sort and bubble sort landed
536:49 selection sort and bubble sort landed but it's also slower than linear search
536:52 but it's also slower than linear search for instance but you would rather expect
536:54 for instance but you would rather expect that if you have to do a lot of work up
536:55 that if you have to do a lot of work up front sorting some elements versus just
536:58 front sorting some elements versus just searching them you're going to have to
536:59 searching them you're going to have to put in more effort and so the question
537:01 put in more effort and so the question of whether or not you should just search
537:02 of whether or not you should just search something blindly with linear search and
537:04 something blindly with linear search and not bother sorting it really boils down
537:06 not bother sorting it really boils down to can you afford to spend this amount
537:08 to can you afford to spend this amount of time and if you're the Googles of the
537:10 of time and if you're the Googles of the world odds are you don't want to be
537:11 world odds are you don't want to be searching their database linearly every
537:13 searching their database linearly every time why because you can sort it once
537:16 time why because you can sort it once and then benefit millions billions of
537:18 and then benefit millions billions of people subsequently using something like
537:20 people subsequently using something like binary search or frankly in practice
537:22 binary search or frankly in practice something even fancier and faster than
537:24 something even fancier and faster than binary search but there's always going
537:26 binary search but there's always going to be this tradeoff you can achieve
537:28 to be this tradeoff you can achieve binary search only if the elements are
537:30 binary search only if the elements are sorted how much does it cost you to sort
537:32 sorted how much does it cost you to sort them well maybe N squared if you use
537:34 them well maybe N squared if you use some of the earlier algorithms but it
537:36 some of the earlier algorithms but it turns out n log n is pretty fast as well
537:39 turns out n log n is pretty fast as well so at the end of the day
537:42 so at the end of the day these running times involve tradeoffs
537:45 these running times involve tradeoffs and indeed in merge sort 2 I should note
537:47 and indeed in merge sort 2 I should note that the lower bound on merge sort is
537:48 that the lower bound on merge sort is also going to be Omega of n log n as
537:51 also going to be Omega of n log n as such we can describe it in terms of our
537:52 such we can describe it in terms of our Theta notation saying that merge short
537:54 Theta notation saying that merge short is indeed in Theta of n log n so
537:57 is indeed in Theta of n log n so generally speaking probably better to
537:59 generally speaking probably better to use something like merge sort or some
538:01 use something like merge sort or some other algorithm that's in N log n in
538:03 other algorithm that's in N log n in practice most programmers are not
538:04 practice most programmers are not implementing these sorting algorithms
538:06 implementing these sorting algorithms themselves odds are they're using a
538:07 themselves odds are they're using a library off the shelf that themselves
538:10 library off the shelf that themselves have made the decision as to which of
538:11 have made the decision as to which of these algorithms to do but generally
538:13 these algorithms to do but generally speaking and we're seeing now this for
538:15 speaking and we're seeing now this for the first time if you want to improve
538:17 the first time if you want to improve time like use less time write faster
538:19 time like use less time write faster code you got to pay a price and that
538:21 code you got to pay a price and that might be your human time just takes you
538:24 might be your human time just takes you more time to code up something more
538:25 more time to code up something more sophisticated more difficult to
538:27 sophisticated more difficult to implement or you need to spend something
538:29 implement or you need to spend something like space and as these shelves suggest
538:32 like space and as these shelves suggest that too is one of the key details of
538:33 that too is one of the key details of merge sort you can't just have the
538:35 merge sort you can't just have the elements swapping in place you need at
538:38 elements swapping in place you need at least an auxiliary array so that when
538:40 least an auxiliary array so that when you do the merging you have a place to
538:42 you do the merging you have a place to put them and this is excessive this
538:44 put them and this is excessive this amount of memory I could have just gone
538:45 amount of memory I could have just gone back and forth between top shelf and
538:47 back and forth between top shelf and bottom shelf but it's a little more
538:48 bottom shelf but it's a little more interesting to go top down but you do
538:50 interesting to go top down but you do need more space back in the day decades
538:52 need more space back in the day decades ago space was really expensive and so
538:54 ago space was really expensive and so you know what it might have been better
538:56 you know what it might have been better to not use merge sort use bubble sort or
538:59 to not use merge sort use bubble sort or uh selection sort even or some other
539:00 uh selection sort even or some other algorithm alog together nowadays space
539:03 algorithm alog together nowadays space is relatively cheap and so these are
539:04 is relatively cheap and so these are more acceptable trade-offs but it
539:06 more acceptable trade-offs but it totally depends on the application the
539:08 totally depends on the application the very last thing we thought we'd do is
539:10 very last thing we thought we'd do is show you an actual compar comparison of
539:12 show you an actual compar comparison of some of these sorting algorithms it's
539:13 some of these sorting algorithms it's about 60 seconds long and it will
539:15 about 60 seconds long and it will compare for you uh selection sort bubble
539:18 compare for you uh selection sort bubble sort and merge sort in parallel
539:21 sort and merge sort in parallel simultaneously uh with some fun sorting
539:23 simultaneously uh with some fun sorting music showing you ultimately what it
539:25 music showing you ultimately what it really means to be an O of n s or better
539:27 really means to be an O of n s or better yet Big O of n
539:30 yet Big O of n Logan selection on the top bubble on the
539:35 Logan selection on the top bubble on the bottom merge in the
539:38 bottom merge in the [Music]
539:40 [Music] middle
539:42 middle [Music]
540:34 all right that's it for cs50 we'll see you next
540:40 [Music] time
540:42 time [Music]
542:27 [Music] all right this is cs50 and this is week
542:30 all right this is cs50 and this is week four and if you think back a few weeks
542:32 four and if you think back a few weeks ago already in week zero we started
542:34 ago already in week zero we started talking about like what images are and
542:35 talking about like what images are and we talked about representation of images
542:37 we talked about representation of images as this grid of pixels and each pixel
542:40 as this grid of pixels and each pixel has like some pattern of bits that
542:41 has like some pattern of bits that defines its color well it turns out
542:43 defines its color well it turns out today we'll take a deeper look
542:45 today we'll take a deeper look underneath the hood at how things like
542:46 underneath the hood at how things like images and so much more is actually
542:49 images and so much more is actually implemented using just these zeros and
542:50 implemented using just these zeros and ones and how now as a programmer you can
542:53 ones and how now as a programmer you can actually harness that for better for
542:55 actually harness that for better for worse to better understand and better
542:57 worse to better understand and better manipulate what's going on inside of a
542:59 manipulate what's going on inside of a computer's memory using a language like
543:01 computer's memory using a language like C in fact even this bowl of stress balls
543:03 C in fact even this bowl of stress balls that we keep happening is just a
543:04 that we keep happening is just a photograph of course but if you think
543:06 photograph of course but if you think back to week zero if you sort of enhance
543:08 back to week zero if you sort of enhance enhance enhance this image like they do
543:10 enhance enhance this image like they do in the movies it actually doesn't work
543:12 in the movies it actually doesn't work out the way you would think from
543:13 out the way you would think from Hollywood as I keep continue to zoom in
543:16 Hollywood as I keep continue to zoom in and zoom in and zoom in on a screen like
543:18 and zoom in and zoom in on a screen like this you'll see that yes it gets bigger
543:20 this you'll see that yes it gets bigger but if it gets too big what do you start
543:22 but if it gets too big what do you start to notice the so-called pixelation and
543:24 to notice the so-called pixelation and indeed you can see the individual dots
543:26 indeed you can see the individual dots so next time you watch some uh show or
543:29 so next time you watch some uh show or movie on uh TV that has this sort of
543:31 movie on uh TV that has this sort of notion of enhancing you know there's
543:33 notion of enhancing you know there's actually a finite limit there you can
543:34 actually a finite limit there you can only enhance so far as there's actually
543:36 only enhance so far as there's actually information there but once you zoom in
543:38 information there but once you zoom in to a certain level like this like that's
543:40 to a certain level like this like that's all that there you're not going to see
543:42 all that there you're not going to see the glint of the suspect in some crime
543:43 the glint of the suspect in some crime drama in their eye just because you've
543:46 drama in their eye just because you've enhanced the image there's only a finite
543:47 enhanced the image there's only a finite amount of information actually there but
543:50 amount of information actually there but we'll see today too that by
543:52 we'll see today too that by understanding what's going on inside of
543:54 understanding what's going on inside of a computer's memory we can start to
543:55 a computer's memory we can start to represent and even create and code more
543:57 represent and even create and code more interesting things so for instance here
543:59 interesting things so for instance here is a bit map if you will which is a term
544:02 is a bit map if you will which is a term of art a bit map is a type of image and
544:05 of art a bit map is a type of image and it's a map of bits in the sense that you
544:06 it's a map of bits in the sense that you have this coordinate system of up top
544:08 have this coordinate system of up top down left right at least in this artist
544:10 down left right at least in this artist representation here and suppose that
544:12 representation here and suppose that maybe we all defi decide as a as as the
544:15 maybe we all defi decide as a as as the world that one shall represent the color
544:18 world that one shall represent the color white and zero shall represent the color
544:20 white and zero shall represent the color black what might this map of bits this
544:23 black what might this map of bits this bit map actually be can you see through
544:27 bit map actually be can you see through it yeah it is indeed a smiley face so an
544:31 it yeah it is indeed a smiley face so an amazing eye if I actually turn all of
544:33 amazing eye if I actually turn all of the ones to White just to visualize this
544:35 the ones to White just to visualize this you'll see indeed this is what was
544:36 you'll see indeed this is what was embedded there but of course on our
544:38 embedded there but of course on our computer monitors and phones we have
544:40 computer monitors and phones we have this grid of square is this grid of
544:41 this grid of square is this grid of pixels so indeed if you were to actually
544:44 pixels so indeed if you were to actually see on your screen a smiley face like a
544:45 see on your screen a smiley face like a black and white one at that what's
544:47 black and white one at that what's probably going on underneath the hood is
544:49 probably going on underneath the hood is just some pattern of zeros and ones and
544:50 just some pattern of zeros and ones and maybe single bits one bit color if you
544:53 maybe single bits one bit color if you will where one here represents white and
544:55 will where one here represents white and zero represents black so if you kind of
544:58 zero represents black so if you kind of like this thing it turns out you can do
544:59 like this thing it turns out you can do pretty uh pretty beautiful pretty
545:01 pretty uh pretty beautiful pretty interesting pretty artistically inclined
545:03 interesting pretty artistically inclined things if you go to this URL at your
545:05 things if you go to this URL at your leisure cs50. lart it'll actually
545:08 leisure cs50. lart it'll actually redirect you to a Google spreadsheet
545:09 redirect you to a Google spreadsheet that we've made in advance and we've
545:11 that we've made in advance and we've kind of Shrunk the rows and columns to
545:12 kind of Shrunk the rows and columns to resemble a grid of pixels tiny little
545:15 resemble a grid of pixels tiny little squares all of which are white by
545:16 squares all of which are white by default not unlike this easel here that
545:20 default not unlike this easel here that we have a couple of volunteers working
545:21 we have a couple of volunteers working away at in fact would you guys like to
545:23 away at in fact would you guys like to come forward for a moment and say a
545:24 come forward for a moment and say a quick hello before we come back to you
545:27 quick hello before we come back to you uh hello my name is Daniel I'm from
545:29 uh hello my name is Daniel I'm from Chicago welcome to Daniel and hi
545:31 Chicago welcome to Daniel and hi everyone I'm Adam and I'm from chindon
545:33 everyone I'm Adam and I'm from chindon Tobago nice well welcome to you both
545:36 Tobago nice well welcome to you both thank you you'll see that in their hands
545:38 thank you you'll see that in their hands are actually a whole bunch of pixels uh
545:40 are actually a whole bunch of pixels uh Post-it notes that we've handed them in
545:41 Post-it notes that we've handed them in in advance so if you don't mind we'll
545:42 in advance so if you don't mind we'll come back to you in a couple of minutes
545:44 come back to you in a couple of minutes and see what they've created if you will
545:45 and see what they've created if you will on this grid of white paper much like
545:48 on this grid of white paper much like you could create on this Google
545:49 you could create on this Google spreadsheet in fact feel free to send us
545:51 spreadsheet in fact feel free to send us your Creations if so inclined uh via the
545:53 your Creations if so inclined uh via the URL you'll get at cs50. lart now let's
545:57 URL you'll get at cs50. lart now let's come back to week zero where we Define
545:59 come back to week zero where we Define some of the building blocks for images
546:00 some of the building blocks for images we talked about RGB which is just red
546:02 we talked about RGB which is just red green blue and it's just one of the
546:04 green blue and it's just one of the systems a popular system via which you
546:05 systems a popular system via which you can represent any color of the rainbow
546:07 can represent any color of the rainbow using some combination of red and green
546:11 using some combination of red and green and blue and if any of you are
546:13 and blue and if any of you are artistically inclined or have used
546:14 artistically inclined or have used Photoshop or similar programs you might
546:17 Photoshop or similar programs you might typically have some means of selecting a
546:19 typically have some means of selecting a color by some grid like this but down
546:21 color by some grid like this but down here notice there's explicit mentions of
546:23 here notice there's explicit mentions of the types of color systems in use R GB
546:27 the types of color systems in use R GB and in fact here you see 0000 0 and up
546:30 and in fact here you see 0000 0 and up here under new you see the color black
546:32 here under new you see the color black and that implies that if you have no red
546:33 and that implies that if you have no red no green no blue well that indeed would
546:35 no green no blue well that indeed would represent by convention the color black
546:38 represent by convention the color black by contrast if we play around with
546:40 by contrast if we play around with Photoshop or similar program if you have
546:42 Photoshop or similar program if you have a lot of red a lot of green and a lot of
546:45 a lot of red a lot of green and a lot of blue for instance 255 255 255 like
546:48 blue for instance 255 255 255 like really crank it up to the max value you
546:50 really crank it up to the max value you can represent with eight bits per week
546:53 can represent with eight bits per week zero well then it turns out you get the
546:55 zero well then it turns out you get the color white here and we can play with
546:57 color white here and we can play with these numbers endlessly for instance if
546:59 these numbers endlessly for instance if we use 255 of red but zero green and
547:03 we use 255 of red but zero green and zero blue not surprisingly the square at
547:06 zero blue not surprisingly the square at the top of the screen Becomes Of course
547:07 the top of the screen Becomes Of course red entirely because it's all red and no
547:09 red entirely because it's all red and no green no blue if we change it instead to
547:12 green no blue if we change it instead to 255 for green but zero for red and blue
547:15 255 for green but zero for red and blue of course we get green and then lastly
547:17 of course we get green and then lastly if we crank up the blue but leave red
547:19 if we crank up the blue but leave red and green as zero we of course get blue
547:21 and green as zero we of course get blue but all this while down here highlighted
547:23 but all this while down here highlighted is something that maybe some of you have
547:25 is something that maybe some of you have seen before like some combination of
547:27 seen before like some combination of numbers and letters if any of you have
547:28 numbers and letters if any of you have made personal web pages or use programs
547:31 made personal web pages or use programs like Photoshop you might have used these
547:32 like Photoshop you might have used these so-called color codes so indeed the
547:34 so-called color codes so indeed the world has this convention whereby using
547:37 world has this convention whereby using six digits or sometimes three you can
547:40 six digits or sometimes three you can represent a little more succinctly some
547:42 represent a little more succinctly some amount of red green blue and you'll see
547:45 amount of red green blue and you'll see here maybe by inference that if RGB is0
547:48 here maybe by inference that if RGB is0 0 255 respectively perhaps where we're
547:51 0 255 respectively perhaps where we're going with this is that
547:53 going with this is that 0000 FF is just an alternative way of
547:56 0000 FF is just an alternative way of expressing the exact same idea no red no
547:58 expressing the exact same idea no red no green and a lot of blue but why is that
548:02 green and a lot of blue but why is that and in fact we'll come full circle here
548:03 and in fact we'll come full circle here to introducing something that we could
548:05 to introducing something that we could have done in week zero but it doesn't
548:06 have done in week zero but it doesn't really solve a problem then but today as
548:08 really solve a problem then but today as we focus more on images and on memory
548:11 we focus more on images and on memory itself turns out understanding these
548:13 itself turns out understanding these patterns is pretty useful so back in
548:15 patterns is pretty useful so back in week zero we talked of course about
548:16 week zero we talked of course about binary and binary by implying two only
548:19 binary and binary by implying two only gives you two digits zero and one you
548:21 gives you two digits zero and one you and I as humans almost always use the
548:23 and I as humans almost always use the decimal system in normal conversation
548:25 decimal system in normal conversation deck meaning 10 so we have 0 through n
548:28 deck meaning 10 so we have 0 through n instead if a human like us wants to
548:30 instead if a human like us wants to count up as high as 10 or 11 or 12 we
548:34 count up as high as 10 or 11 or 12 we don't have a digit per se for 10 11 and
548:36 don't have a digit per se for 10 11 and 12 we start reusing digits so it's 1 0 1
548:39 12 we start reusing digits so it's 1 0 1 1 1 2 and so forth but in other systems
548:43 1 1 2 and so forth but in other systems not binary not decimal but systems
548:45 not binary not decimal but systems called heximal hex implying 16 there are
548:49 called heximal hex implying 16 there are actually more digits than these which
548:51 actually more digits than these which might come as a surprise um it's not
548:53 might come as a surprise um it's not pairs of digits like in decimal single
548:55 pairs of digits like in decimal single digits and frankly it doesn't really
548:57 digits and frankly it doesn't really matter what the digits are because at
548:58 matter what the digits are because at the end of the day these are just
548:59 the end of the day these are just symbols that you and I immediately
549:01 symbols that you and I immediately associate with some notion of math but
549:03 associate with some notion of math but just Strokes on the screen that
549:05 just Strokes on the screen that represent some have represent some
549:07 represent some have represent some actual value so it turns out that by
549:10 actual value so it turns out that by convention
549:11 convention when you want more than n 10 digits 0
549:13 when you want more than n 10 digits 0 through 9 you start using letters of the
549:15 through 9 you start using letters of the English alphabet A B C D E and F and you
549:20 English alphabet A B C D E and F and you can represent them in lower case it's
549:21 can represent them in lower case it's case insensitive so it doesn't really
549:23 case insensitive so it doesn't really matter you might see it in upper case or
549:24 matter you might see it in upper case or lower case but this is how you can count
549:26 lower case but this is how you can count Beyond N9 not using decimal but using
549:29 Beyond N9 not using decimal but using indeed something called hexadecimal if
549:31 indeed something called hexadecimal if we get really technical this is also
549:33 we get really technical this is also known as base 16 and it's the same idea
549:35 known as base 16 and it's the same idea as week zero where instead of using base
549:37 as week zero where instead of using base 2 for binary base 10 for decimal use 16
549:40 2 for binary base 10 for decimal use 16 as the base for heximal and so if we run
549:43 as the base for heximal and so if we run through just some simple examples here
549:45 through just some simple examples here in the world of heximal your columns are
549:48 in the world of heximal your columns are just powers of 16 16 to the 0er 16 to
549:51 just powers of 16 16 to the 0er 16 to the 1 16 to the 2 and so forth but in
549:54 the 1 16 to the 2 and so forth but in the world of hex we usually at least
549:56 the world of hex we usually at least thus far and today we'll see just pairs
549:58 thus far and today we'll see just pairs of digits like this so here for instance
550:00 of digits like this so here for instance is the ones column and the 16's column
550:03 is the ones column and the 16's column if we multiply that out so if you wanted
550:05 if we multiply that out so if you wanted to represent the number you and I know
550:06 to represent the number you and I know in in uh the real world as zero in
550:09 in in uh the real world as zero in heximal it would just be 0 0 if you want
550:12 heximal it would just be 0 0 if you want to represent the number one it would be
550:14 to represent the number one it would be 01 and from there we get 02 03 04 0 five
550:19 01 and from there we get 02 03 04 0 five 06 07 08 09 now things get potentially
550:23 06 07 08 09 now things get potentially interesting in decimal it would
550:25 interesting in decimal it would obviously become 10 but in heximal it
550:27 obviously become 10 but in heximal it just becomes 0 a and then 0 B which is
550:31 just becomes 0 a and then 0 B which is to say if I rewind after nine comes in
550:34 to say if I rewind after nine comes in heximal if I pronounce it in in decimal
550:37 heximal if I pronounce it in in decimal this is how you'd represent 10 this is
550:39 this is how you'd represent 10 this is how you'd represent 11 12 13 14 and then
550:43 how you'd represent 11 12 13 14 and then lastly in heximal the 16th value is f
550:46 lastly in heximal the 16th value is f which is just always going to represent
550:49 which is just always going to represent 15 so where to how do we connect this to
550:52 15 so where to how do we connect this to some of the past math well once you get
550:53 some of the past math well once you get to Zer F in heximal if f is the highest
550:57 to Zer F in heximal if f is the highest you can count just like in decimal nine
550:59 you can count just like in decimal nine is the highest you can count what comes
551:02 is the highest you can count what comes next if this is 15 I claim how do I
551:05 next if this is 15 I claim how do I represent 16 in heximal with what
551:07 represent 16 in heximal with what pattern of
551:09 pattern of symbols what pattern of symbols for
551:12 symbols what pattern of symbols for heximal yeah so one Z not 10 even though
551:15 heximal yeah so one Z not 10 even though you might read it like that as a typical
551:17 you might read it like that as a typical human but one zero because why well even
551:20 human but one zero because why well even if this is completely new to you the
551:22 if this is completely new to you the whole column system the places are
551:23 whole column system the places are exactly the same intuitively so you need
551:25 exactly the same intuitively so you need a one in the 16's place and a zero in
551:28 a one in the 16's place and a zero in the ones place and we won't count all
551:30 the ones place and we won't count all the way up to 255 but we count if we
551:32 the way up to 255 but we count if we count a little higher this would be 1 Z
551:34 count a little higher this would be 1 Z AKA 16 in decimal this would be 1 one
551:38 AKA 16 in decimal this would be 1 one AKA 17 in decimal and then 18 19 20 and
551:43 AKA 17 in decimal and then 18 19 20 and so forth dot dot dot and we can count
551:45 so forth dot dot dot and we can count all the way up to FF CU if f is the
551:48 all the way up to FF CU if f is the biggest digit in hexadecimal FF is
551:50 biggest digit in hexadecimal FF is indeed as high as we can count and if
551:52 indeed as high as we can count and if each F represents 15 well let's just do
551:55 each F represents 15 well let's just do the math like in week zero so 16 * F + 1
551:58 the math like in week zero so 16 * F + 1 * f is how all of us learned to do uh
552:01 * f is how all of us learned to do uh math in grade school even though not in
552:03 math in grade school even though not in heximal that's of course 16 * 15 + 1 *
552:07 heximal that's of course 16 * 15 + 1 * 15 multiply that out you get 240 plus 15
552:11 15 multiply that out you get 240 plus 15 and air go you can count as high as
552:14 and air go you can count as high as 255 using two heximal digits now this is
552:19 255 using two heximal digits now this is not the kind of thing where like this is
552:20 not the kind of thing where like this is going to be an interesting exercise
552:22 going to be an interesting exercise mentally to ever convert in your head
552:23 mentally to ever convert in your head generally you'll get used to the fact
552:25 generally you'll get used to the fact that after nine comes a and the biggest
552:27 that after nine comes a and the biggest digit is f and you'll just start to see
552:29 digit is f and you'll just start to see patterns like this in the worlds of
552:30 patterns like this in the worlds of Photoshop web pages in a few weeks and
552:32 Photoshop web pages in a few weeks and Beyond but like why is heximal useful
552:36 Beyond but like why is heximal useful like why are we complicating the world
552:39 like why are we complicating the world and adding on top of decimal something
552:41 and adding on top of decimal something else well it turns out that a single
552:42 else well it turns out that a single decimal digit like f the biggest one for
552:45 decimal digit like f the biggest one for instance is 15 and here let me just
552:47 instance is 15 and here let me just propose a bit of Mental Math how many
552:49 propose a bit of Mental Math how many bits do you need to represent the number
552:53 bits do you need to represent the number 15 in binary if you've got the ones
552:56 15 in binary if you've got the ones place two's Place fours and so forth how
552:59 place two's Place fours and so forth how many bits
553:01 many bits total so fewer than five to count this
553:03 total so fewer than five to count this highest 15 I think but
553:06 highest 15 I think but close someone
553:09 close someone else sing in hand yeah so four bits I
553:13 else sing in hand yeah so four bits I think suffice because if you want to
553:15 think suffice because if you want to count as high as F that is to say 15 I
553:18 count as high as F that is to say 15 I think if you have four bits you can do
553:20 think if you have four bits you can do that because if over here is the one's
553:22 that because if over here is the one's place from week zero for binary this is
553:24 place from week zero for binary this is the two's place this is The Four's place
553:26 the two's place this is The Four's place this is the e place do out some quick
553:28 this is the e place do out some quick math so 8 + 4 is 12 + 2 is 14 + 1 is 15
553:33 math so 8 + 4 is 12 + 2 is 14 + 1 is 15 so it turns out that by convenience
553:35 so it turns out that by convenience heximal digits can just be represented
553:37 heximal digits can just be represented consistently with four bits or fewer but
553:40 consistently with four bits or fewer but four and four of course is half of eight
553:43 four and four of course is half of eight and eight is like everywhere like eight
553:44 and eight is like everywhere like eight bits is a bite which is again just a
553:46 bits is a bite which is again just a convention we've seen and so the reason
553:48 convention we've seen and so the reason that you see hexad desmal in the world
553:50 that you see hexad desmal in the world of Photoshop and eventually web pages is
553:52 of Photoshop and eventually web pages is it actually just Maps really nicely to
553:55 it actually just Maps really nicely to expressing binary numbers more
553:57 expressing binary numbers more succinctly with a fixed number of digits
553:59 succinctly with a fixed number of digits so for instance anytime you see 11 one
554:03 so for instance anytime you see 11 one one one 1111 in the world as binary you
554:06 one one 1111 in the world as binary you know what that's a little tedious to
554:08 know what that's a little tedious to both say and write you can represent
554:10 both say and write you can represent more succinctly
554:12 more succinctly any uh group of four one bits more
554:15 any uh group of four one bits more succinctly in heximal as just F so 111
554:20 succinctly in heximal as just F so 111 1111 in binary more succinctly and more
554:23 1111 in binary more succinctly and more commonly now in the world of Photoshop
554:25 commonly now in the world of Photoshop memory images and the like is
554:27 memory images and the like is represented more succinctly as FF and
554:29 represented more succinctly as FF and that's why because it just Maps really
554:30 that's why because it just Maps really nicely to four bits and so we can be a
554:32 nicely to four bits and so we can be a little more succinct so any questions on
554:36 little more succinct so any questions on heximal which is just another way of
554:38 heximal which is just another way of representing information but using the
554:40 representing information but using the same grade school approach yeah good
554:43 same grade school approach yeah good question if you represent 15 with f it
554:45 question if you represent 15 with f it would use four bits so base systems are
554:48 would use four bits so base systems are really just a way for us humans on paper
554:50 really just a way for us humans on paper or on screens to represent information
554:52 or on screens to represent information if F represents the decimal number 15
554:55 if F represents the decimal number 15 the computer underneath the hood has to
554:57 the computer underneath the hood has to use four bits to represent it so one
555:00 use four bits to represent it so one heximal digit by convention always
555:02 heximal digit by convention always implies four bits underneath the hood so
555:05 implies four bits underneath the hood so therefore if you have two heximal digits
555:07 therefore if you have two heximal digits like 0 0 that means eight zero bits
555:10 like 0 0 that means eight zero bits underneath the hood like for red or for
555:12 underneath the hood like for red or for green if you see FF now we know that's
555:15 green if you see FF now we know that's four one bits and another four one Bits
555:17 four one bits and another four one Bits And if we do out the math that's 255
555:19 And if we do out the math that's 255 that's why in Photoshop
555:22 that's why in Photoshop 000000 FF means no red no green and
555:26 000000 FF means no red no green and 255 of blue and it's just way more
555:29 255 of blue and it's just way more succinct than writing out like what 8
555:30 succinct than writing out like what 8 plus 8 plus 8 24 zeros and ones and it's
555:33 plus 8 plus 8 24 zeros and ones and it's just cleaner than even using decimal
555:35 just cleaner than even using decimal when you're using units of eight which
555:37 when you're using units of eight which again computers just use everywhere so
555:39 again computers just use everywhere so it's just another system it's not one
555:40 it's just another system it's not one you need to dwell on very much but again
555:42 you need to dwell on very much but again it's fundamentally no different from
555:44 it's fundamentally no different from binary or decimal we're just using a
555:46 binary or decimal we're just using a slightly different base no all right
555:49 slightly different base no all right well we had this blank canvas here and I
555:52 well we had this blank canvas here and I think uh are you two perhaps ready to
555:54 think uh are you two perhaps ready to reveal for the world what you've created
555:55 reveal for the world what you've created do you want to go ahead and I'll I'll
555:57 do you want to go ahead and I'll I'll swivel it around for you all right here
555:59 swivel it around for you all right here we go big reveal and today's pixel
556:03 we go big reveal and today's pixel art a round of applause if we
556:07 art a round of applause if we could very nicely done well thank you
556:09 could very nicely done well thank you both if you want to come up after and
556:11 both if you want to come up after and tear this off and bring it home you're
556:12 tear this off and bring it home you're welcome to and keep the Post-it notes
556:14 welcome to and keep the Post-it notes too well thank you to our volunteers
556:15 too well thank you to our volunteers there let's now translate this to really
556:18 there let's now translate this to really more technical worlds where we're going
556:20 more technical worlds where we're going to see and consider it more often
556:21 to see and consider it more often because in fact sometimes when you've
556:23 because in fact sometimes when you've had error messages over the past few
556:25 had error messages over the past few weeks from clang the compiler you might
556:27 weeks from clang the compiler you might have even seen evidence of heximal we
556:29 have even seen evidence of heximal we didn't call it out it wasn't useful to
556:31 didn't call it out it wasn't useful to know at the time but it turns out a lot
556:32 know at the time but it turns out a lot of programs use and a lot of code uses
556:36 of programs use and a lot of code uses heximal for those reasons of more prec
556:39 heximal for those reasons of more prec more representation so for instance
556:41 more representation so for instance where else might we see it well here's
556:43 where else might we see it well here's that picture we keep pulling up of our
556:44 that picture we keep pulling up of our computer's memory and each of these
556:46 computer's memory and each of these squares in this grid represents a bite
556:48 squares in this grid represents a bite sort of top left to bottom right in the
556:50 sort of top left to bottom right in the computer's memory but again just an
556:52 computer's memory but again just an artist's representation a few weeks ago
556:54 artist's representation a few weeks ago I claimed that each of these bytes can
556:56 I claimed that each of these bytes can be numbered of course like this is bite
556:58 be numbered of course like this is bite zero at top left then bite one then bite
557:00 zero at top left then bite one then bite two then bite two billion if you have
557:02 two then bite two billion if you have two gigabytes of memory and so we could
557:04 two gigabytes of memory and so we could just number them like this 0 through 15
557:07 just number them like this 0 through 15 on up 16 17 18 and so forth but per the
557:11 on up 16 17 18 and so forth but per the reasons earlier it's just more common in
557:13 reasons earlier it's just more common in computer systems and in software to
557:15 computer systems and in software to actually use hexadecimal just to
557:17 actually use hexadecimal just to describe the locations of the addresses
557:19 describe the locations of the addresses of things in memory so instead a typical
557:22 of things in memory so instead a typical programmer or computer scientist would
557:24 programmer or computer scientist would call these first 16 bytes 0 through F
557:27 call these first 16 bytes 0 through F just because but that's because it's a
557:29 just because but that's because it's a predictable number of bits so if we keep
557:32 predictable number of bits so if we keep going beyond that you would get not 10
557:34 going beyond that you would get not 10 not 11 not 12 but in heximal 1 0 1 1 1 2
557:39 not 11 not 12 but in heximal 1 0 1 1 1 2 and so forth all the way down on the
557:40 and so forth all the way down on the screen to 1 F and if I you know shrunk
557:43 screen to 1 F and if I you know shrunk this down or had a bigger monitor we
557:45 this down or had a bigger monitor we would see eventually 255 bytes later
557:48 would see eventually 255 bytes later from the start 255 as well but there's a
557:51 from the start 255 as well but there's a potential problem here with using
557:53 potential problem here with using heximal in this way there's an
557:55 heximal in this way there's an ambiguity can anyone imagine like what
557:58 ambiguity can anyone imagine like what can go wrong if we use hex to just
558:01 can go wrong if we use hex to just simply describe locations in memory like
558:06 simply describe locations in memory like this
558:08 this yeah yeah yeah like one zero might also
558:11 yeah yeah yeah like one zero might also be 10 and you know maybe if you're you
558:13 be 10 and you know maybe if you're you know really thorough okay wait a minute
558:14 know really thorough okay wait a minute it can't be 10 cuz here's F over here so
558:17 it can't be 10 cuz here's F over here so it's obviously not decimal but why
558:18 it's obviously not decimal but why create potential confusion especially
558:20 create potential confusion especially when you're collaborating building
558:21 when you're collaborating building something with someone we want to avoid
558:22 something with someone we want to avoid that ambiguity and so the convention
558:24 that ambiguity and so the convention humans decided on years ago is that if
558:27 humans decided on years ago is that if you want to make clear that a number is
558:28 you want to make clear that a number is in hexadecimal just by convention you
558:31 in hexadecimal just by convention you prefix all of the digits with Zer X the
558:35 prefix all of the digits with Zer X the X is not like another character it's not
558:37 X is not like another character it's not like a 17th character it's just a human
558:39 like a 17th character it's just a human conven
558:40 conven of putting 0x to imply here comes
558:42 of putting 0x to imply here comes heximal and now it's unambiguous so now
558:45 heximal and now it's unambiguous so now we see 0x10 obviously is not 10 as we
558:48 we see 0x10 obviously is not 10 as we know it in decimal but rather it's the
558:50 know it in decimal but rather it's the number that comes after a single F so
558:53 number that comes after a single F so it's really the number in decimal 16 so
558:55 it's really the number in decimal 16 so 0x anytime you see it that's just a
558:57 0x anytime you see it that's just a visual cue that what is ahead is
559:00 visual cue that what is ahead is actually heximal so let's now start
559:04 actually heximal so let's now start playing around with this information so
559:06 playing around with this information so here's a super simple line of code from
559:07 here's a super simple line of code from like week one where I'm just declaring
559:09 like week one where I'm just declaring ing a variable n and I'm defining it to
559:12 ing a variable n and I'm defining it to be the value 50 and this is out of
559:14 be the value 50 and this is out of context we probably need a main function
559:15 context we probably need a main function and all of that but let's just rewind to
559:17 and all of that but let's just rewind to week one where we actually saw code like
559:19 week one where we actually saw code like this and do something useful with a line
559:21 this and do something useful with a line of code like this so let me go over here
559:23 of code like this so let me go over here to VSS code and in vs code I'll create a
559:25 to VSS code and in vs code I'll create a program called how about addresses since
559:28 program called how about addresses since the goal of this uh the goal here is to
559:30 the goal of this uh the goal here is to just play around ultimately with a
559:32 just play around ultimately with a variable like n and let me go ahead and
559:35 variable like n and let me go ahead and do this I'll include how about standard
559:37 do this I'll include how about standard i.h I'll do int main void so no command
559:40 i.h I'll do int main void so no command line Arguments for now in N gets 50 and
559:44 line Arguments for now in N gets 50 and now so that we can do something mildly
559:46 now so that we can do something mildly useful with it let's just go use print F
559:48 useful with it let's just go use print F and print out with percent I and then a
559:50 and print out with percent I and then a new line whatever that value of n is so
559:53 new line whatever that value of n is so this is not going to be interesting per
559:54 this is not going to be interesting per se it's just week one stuff where I'm
559:56 se it's just week one stuff where I'm defining a variable and printing it out
559:58 defining a variable and printing it out to the screen so let me go down to my
560:00 to the screen so let me go down to my terminal window and do make
560:02 terminal window and do make addresses no errors so that's good I'll
560:05 addresses no errors so that's good I'll do do/ addresses and of course I should
560:07 do do/ addresses and of course I should see the number 50 here now what's going
560:10 see the number 50 here now what's going on underneath the hood let's translate
560:12 on underneath the hood let's translate now code to really what's going on under
560:15 now code to really what's going on under under underneath the hood of the
560:16 under underneath the hood of the computer so if this is our grid of
560:18 computer so if this is our grid of memory I don't necessarily know as the
560:20 memory I don't necessarily know as the programmer and I definitely don't care
560:21 programmer and I definitely don't care as the programmer where exactly it's
560:23 as the programmer where exactly it's ending up in memory that's the whole
560:24 ending up in memory that's the whole point of using Code let the computer
560:26 point of using Code let the computer figure this out but at least
560:27 figure this out but at least conceptually I know that by declaring a
560:30 conceptually I know that by declaring a line of code like that the number 50
560:32 line of code like that the number 50 ends up somewhere in the computer's
560:34 ends up somewhere in the computer's memory and it's assigned the name n a
560:37 memory and it's assigned the name n a symbol n via which I the programmer can
560:40 symbol n via which I the programmer can refer to it and I very deliberately used
560:42 refer to it and I very deliberately used four of these squares for what
560:46 four of these squares for what reason what might be the reason for
560:48 reason what might be the reason for using four squares specifically yeah
560:52 using four squares specifically yeah yeah so an integer is four bytes at
560:53 yeah so an integer is four bytes at least most of the time on Modern systems
560:55 least most of the time on Modern systems an integer is four bytes on an older
560:57 an integer is four bytes on an older computer it might just use one or maybe
560:59 computer it might just use one or maybe even uh two bytes But Here by convention
561:02 even uh two bytes But Here by convention we're almost always going to see four
561:03 we're almost always going to see four bytes I don't know if it's going to end
561:04 bytes I don't know if it's going to end up here it might end up over here but
561:06 up here it might end up over here but for now who cares I just know that the
561:08 for now who cares I just know that the computer can store the the information
561:10 computer can store the the information in this way underneath the hood so let's
561:13 in this way underneath the hood so let's now introduce another feature of C that
561:15 now introduce another feature of C that we haven't had occasion to use just yet
561:18 we haven't had occasion to use just yet that's going to allow us to start poking
561:20 that's going to allow us to start poking around the computer's memory For Better
561:21 around the computer's memory For Better or For Worse and this is one of those
561:23 or For Worse and this is one of those situations where you're about to learn
561:25 situations where you're about to learn acquire a a skill a power that can
561:27 acquire a a skill a power that can actually come back to bite you because
561:29 actually come back to bite you because once you know how to start poking around
561:31 once you know how to start poking around a computer's memory you can do very
561:33 a computer's memory you can do very powerful things and next week we'll see
561:34 powerful things and next week we'll see what you can build in a computer's
561:36 what you can build in a computer's memory but you can also screw up pretty
561:38 memory but you can also screw up pretty easily and cause more of those
561:39 easily and cause more of those segmentation faults that a few of you
561:41 segmentation faults that a few of you have already suffered so with that said
561:43 have already suffered so with that said let's just stipulate that you know what
561:45 let's just stipulate that you know what I don't care necessarily where the 50 is
561:47 I don't care necessarily where the 50 is in memory but I know it exists at some
561:49 in memory but I know it exists at some address in memory and just so I have an
561:52 address in memory and just so I have an easy address to pronounce let's just
561:53 easy address to pronounce let's just suppose it lives at ox123 so that's the
561:56 suppose it lives at ox123 so that's the address in memory in heximal by
561:58 address in memory in heximal by convention and that just happens to be
562:00 convention and that just happens to be where it ends up when I write that line
562:01 where it ends up when I write that line of code but it turns out C has some
562:05 of code but it turns out C has some other operators we can use when we've
562:06 other operators we can use when we've seen the asterisk before the star and
562:09 seen the asterisk before the star and we've used it for multiplication but
562:10 we've used it for multiplication but today we're going to use it for
562:11 today we're going to use it for something more powerful and we're also
562:13 something more powerful and we're also going to introduce an Amper sand which
562:15 going to introduce an Amper sand which allows us to do something as well the
562:17 allows us to do something as well the Amper sand operator is going to allow us
562:20 Amper sand operator is going to allow us to get the address of a piece of data in
562:23 to get the address of a piece of data in memory like by literally putting
562:25 memory like by literally putting Ampersand before the name of a variable
562:28 Ampersand before the name of a variable C will tell us tell you what address
562:31 C will tell us tell you what address that variable lives at maybe it's ox123
562:33 that variable lives at maybe it's ox123 maybe it's Ox 456 who knows but that
562:35 maybe it's Ox 456 who knows but that will give you back the answer the Star
562:38 will give you back the answer the Star does the opposite it's sort of means go
562:40 does the opposite it's sort of means go there so using the star otherwise known
562:42 there so using the star otherwise known as the D reference operator I can
562:44 as the D reference operator I can actually go to a specific address if I
562:46 actually go to a specific address if I want and we'll see what this means in
562:48 want and we'll see what this means in code so how can I leverage this in some
562:52 code so how can I leverage this in some mildly interesting way to start poking
562:54 mildly interesting way to start poking around but eventually we'll use this
562:55 around but eventually we'll use this primitive to build more interesting
562:58 primitive to build more interesting things so let me go back to save vs code
563:00 things so let me go back to save vs code here and let me go ahead and do this
563:03 here and let me go ahead and do this I'll clear my terminal to start fresh
563:05 I'll clear my terminal to start fresh and I'll introduce another format code
563:06 and I'll introduce another format code for printf percent p and for now just
563:10 for printf percent p and for now just take on faith that this it is percent P
563:13 take on faith that this it is percent P because but percent p is going to allow
563:15 because but percent p is going to allow me to print the address of a variable if
563:18 me to print the address of a variable if I additionally tell C get the address of
563:21 I additionally tell C get the address of n so I'm changing percent I to percent p
563:24 n so I'm changing percent I to percent p and that's just something you have to do
563:25 and that's just something you have to do when printing addresses for now but I
563:28 when printing addresses for now but I need to change an ampersand in front of
563:30 need to change an ampersand in front of the variable name so I don't print in
563:32 the variable name so I don't print in the number 50 I print out something like
563:34 the number 50 I print out something like ox123 and it's not going to be as simple
563:36 ox123 and it's not going to be as simple as that we'll see on the screen though
563:37 as that we'll see on the screen though where it actually ended up in my code
563:39 where it actually ended up in my code spaces memory so here we go uh dot uh
563:43 spaces memory so here we go uh dot uh down in my terminal make addresses again
563:45 down in my terminal make addresses again to recompile and now dot SL addresses
563:49 to recompile and now dot SL addresses should reveal not the value of 50 but
563:51 should reveal not the value of 50 but the address of 50 and there it is it's
563:55 the address of 50 and there it is it's pretty long it's not quite as simple and
563:57 pretty long it's not quite as simple and pretty as ox123 but there's the ox
563:59 pretty as ox123 but there's the ox meaning here's a hexadecimal address and
564:01 meaning here's a hexadecimal address and it's 7f FC c784 a04 C suffice it to say
564:06 it's 7f FC c784 a04 C suffice it to say your codes space and even your Macs and
564:08 your codes space and even your Macs and PCs nowadays have a lot of memory that's
564:10 PCs nowadays have a lot of memory that's why in part this address is so big not
564:13 why in part this address is so big not as small as the thing on my slide so
564:16 as small as the thing on my slide so this at the moment isn't that useful yet
564:19 this at the moment isn't that useful yet but it introduces us to a concept that
564:21 but it introduces us to a concept that we'll Now call pointers and pointers are
564:24 we'll Now call pointers and pointers are admittedly one of the more challenging
564:27 admittedly one of the more challenging aspects of c and if in future life you
564:29 aspects of c and if in future life you tell friends that oh I took a class
564:31 tell friends that oh I took a class called cs50 and we learned C like you'll
564:33 called cs50 and we learned C like you'll probably get kind of a look at people
564:35 probably get kind of a look at people like why did you learn C or like oh c
564:37 like why did you learn C or like oh c was hard and it's largely because of
564:40 was hard and it's largely because of this topic which isn't to say that it's
564:42 this topic which isn't to say that it's that hard to wrap your mind around but
564:45 that hard to wrap your mind around but it's definitely very different and it's
564:46 it's definitely very different and it's not a feature that you can harness in
564:48 not a feature that you can harness in higher level languages that we'll see in
564:50 higher level languages that we'll see in class two like Python and Java and the
564:53 class two like Python and Java and the like C is about as close to the
564:55 like C is about as close to the computer's Hardware so to speak that you
564:56 computer's Hardware so to speak that you can get before things get actually scary
564:59 can get before things get actually scary the so-called Assembly Language we saw
565:00 the so-called Assembly Language we saw in week two when I had a link and
565:02 in week two when I had a link and compile and assemble and all of that
565:04 compile and assemble and all of that like that gets really lowlevel and you
565:06 like that gets really lowlevel and you really have to be an expert with the
565:07 really have to be an expert with the computer's CPU or brain to understand
565:09 computer's CPU or brain to understand that but with C you can actually poke
565:12 that but with C you can actually poke around the computer's memory and do
565:13 around the computer's memory and do powerful things with that but again with
565:15 powerful things with that but again with great power comes responsibility it's
565:17 great power comes responsibility it's very easy to break programs by misusing
565:20 very easy to break programs by misusing memory or just having a bug that touches
565:22 memory or just having a bug that touches memory in some way that you don't intend
565:25 memory in some way that you don't intend so pointers at the end of the day are
565:28 so pointers at the end of the day are pretty much what we just saw a pointer
565:31 pretty much what we just saw a pointer is really just a variable that contains
565:33 is really just a variable that contains the address of some value a pointer is a
565:37 the address of some value a pointer is a variable that contains the address of
565:39 variable that contains the address of some value or more simply it's fine to
565:41 some value or more simply it's fine to think of it as an address a pointer is
565:42 think of it as an address a pointer is an address of something in the
565:44 an address of something in the computer's memory now what might we do
565:46 computer's memory now what might we do to uh to uh actualize this well here's
565:50 to uh to uh actualize this well here's two lines of code it turns out by using
565:52 two lines of code it turns out by using our two new operators today I can
565:54 our two new operators today I can declare an INT call it n and assign it a
565:57 declare an INT call it n and assign it a value like 50 just like before if I want
566:00 value like 50 just like before if I want to store the address of n in a variable
566:03 to store the address of n in a variable and not just print it immediately via
566:04 and not just print it immediately via printf I can declare a variable for
566:07 printf I can declare a variable for instance called P but I could call it
566:09 instance called P but I could call it anything I want like any variable but
566:11 anything I want like any variable but because it's an address it's not int P
566:14 because it's an address it's not int P it has to be int star P so to speak and
566:17 it has to be int star P so to speak and the star here on the left hand side of
566:19 the star here on the left hand side of the equal sign is just a clue to C that
566:21 the equal sign is just a clue to C that means p is going to be a pointer that is
566:25 means p is going to be a pointer that is p is going to be the address of what the
566:27 p is going to be the address of what the address of an integer now technically
566:30 address of an integer now technically it's still an integer itself right
566:31 it's still an integer itself right because an address is just a number
566:33 because an address is just a number whether it's 1 2 3 or ox123 so this is
566:35 whether it's 1 2 3 or ox123 so this is really just a semantic difference so in
566:38 really just a semantic difference so in Star p just means that this variable
566:40 Star p just means that this variable doesn't contain any old number like 50
566:42 doesn't contain any old number like 50 it specifically contains a number that
566:44 it specifically contains a number that is the address of something
566:46 is the address of something else so how can I now use this well let
566:49 else so how can I now use this well let me go back to VSS code and let me
566:52 me go back to VSS code and let me propose that we add a line of code like
566:55 propose that we add a line of code like that so instead of just directly
566:56 that so instead of just directly printing out that value let's go ahead
566:58 printing out that value let's go ahead and Define a second variable called uh P
567:01 and Define a second variable called uh P that's of type in Star P set it equal to
567:04 that's of type in Star P set it equal to Ampersand n and then this time let's not
567:08 Ampersand n and then this time let's not just print out Amper sand n let's
567:09 just print out Amper sand n let's actually print out the value of P so the
567:11 actually print out the value of P so the only two new things here if I zoom in
567:13 only two new things here if I zoom in are I've used not only the Amper sand on
567:16 are I've used not only the Amper sand on the right to get the address of n I'm
567:18 the right to get the address of n I'm now using the star on the Left To Tell C
567:21 now using the star on the Left To Tell C that P is still a still a variable as
567:23 that P is still a still a variable as always but it's a pointer it is the
567:26 always but it's a pointer it is the address of some other value like this
567:29 address of some other value like this and I'm still going to print it with the
567:30 and I'm still going to print it with the same format code percent P so that
567:31 same format code percent P so that doesn't change so let me go ahead and
567:33 doesn't change so let me go ahead and zoom out and do make addresses and slash
567:37 zoom out and do make addresses and slash addresses and there it is exactly the
567:40 addresses and there it is exactly the same thing now in and of itself not that
567:43 same thing now in and of itself not that useful yet but the fact that you can now
567:46 useful yet but the fact that you can now access the addresses of things in memory
567:48 access the addresses of things in memory means that we'll be able to build things
567:50 means that we'll be able to build things and construct things and Link things
567:52 and construct things and Link things together by knowing where they live so
567:55 together by knowing where they live so to speak so any questions on this
567:57 to speak so any questions on this technique thus far
567:58 technique thus far [Music]
568:05 yeah a good question on line six must it be starp and Ampersand and in this case
568:07 be starp and Ampersand and in this case yes because what am I doing on the left
568:10 yes because what am I doing on the left and I'll get rid of the equal sign for
568:11 and I'll get rid of the equal sign for now this would give me a variable called
568:14 now this would give me a variable called P that's not an integer per se but
568:16 P that's not an integer per se but that's the address of an integer but
568:18 that's the address of an integer but without the equal sign I'm not storing
568:20 without the equal sign I'm not storing anything in that variable so by adding
568:22 anything in that variable so by adding the equal sign and then Ampersand N I am
568:27 the equal sign and then Ampersand N I am explicitly figuring out with Ampersand
568:29 explicitly figuring out with Ampersand what the address of n is which already
568:31 what the address of n is which already exists per line five and tucking it away
568:34 exists per line five and tucking it away in this new variable called
568:36 in this new variable called P other questions yeah every time you
568:40 P other questions yeah every time you run good question every time I run the
568:42 run good question every time I run the program it uses up a different piece of
568:44 program it uses up a different piece of memory short answer yes computers though
568:46 memory short answer yes computers though long story short also have something
568:47 long story short also have something called virtual memory so if you run it
568:49 called virtual memory so if you run it again and again you might actually see
568:51 again and again you might actually see the same addresses on the same Mac or PC
568:53 the same addresses on the same Mac or PC or cloud-based server but we'll see in a
568:55 or cloud-based server but we'll see in a bit where uh at a high level it's laid
568:58 bit where uh at a high level it's laid out but it will always exist at some
569:00 out but it will always exist at some address good question yeah some correct
569:05 address good question yeah some correct Ampersand n is the address of N and in
569:07 Ampersand n is the address of N and in Star p
569:09 Star p is a pointer called p and honestly in an
569:13 is a pointer called p and honestly in an Ideal World if C were made today and not
569:15 Ideal World if C were made today and not decades ago when humans were first
569:17 decades ago when humans were first creating languages you know ideally we
569:19 creating languages you know ideally we would just have a data type called
569:21 would just have a data type called pointer and then this would be a little
569:23 pointer and then this would be a little less complicated because it would
569:24 less complicated because it would literally be what it says you know the
569:26 literally be what it says you know the humans who invented SE didn't do this
569:28 humans who invented SE didn't do this but this is the idea so pointer is not a
569:30 but this is the idea so pointer is not a legitimate word in the code it is a term
569:32 legitimate word in the code it is a term of Art in English but this is really
569:34 of Art in English but this is really just the idea but the way you express
569:37 just the idea but the way you express pointer as a data type type is a little
569:39 pointer as a data type type is a little more cryptic as int star P here but
569:43 more cryptic as int star P here but notice in line seven when I print out p
569:47 notice in line seven when I print out p i don't use a star I don't use an
569:49 i don't use a star I don't use an ampersand y I literally just want to
569:50 ampersand y I literally just want to print the value of p and we've been
569:52 print the value of p and we've been doing that since week one if you want to
569:53 doing that since week one if you want to print a variable just describe the
569:55 print a variable just describe the variable by its name no special syntax
569:58 variable by its name no special syntax any other questions on this thus
570:01 any other questions on this thus far uh what's the advantage of using
570:03 far uh what's the advantage of using pointers with pointers we'll see today
570:06 pointers with pointers we'll see today some applications of them really the
570:08 some applications of them really the idea is going to come to fruition next
570:09 idea is going to come to fruition next week when we're going to create what are
570:11 week when we're going to create what are called uh data structures in memory
570:13 called uh data structures in memory where we can build not just uh for
570:15 where we can build not just uh for instance uh one-dimensional data
570:17 instance uh one-dimensional data structures like an array we'll see next
570:19 structures like an array we'll see next week we can actually create the
570:20 week we can actually create the equivalent of two-dimensional data
570:22 equivalent of two-dimensional data structures or even threedimensional data
570:23 structures or even threedimensional data structures by using these addresses and
570:25 structures by using these addresses and sort of linking things together and
570:27 sort of linking things together and we'll see the beginnings of that this
570:28 we'll see the beginnings of that this week but for now focus at least for now
570:30 week but for now focus at least for now on just really the syntax and what these
570:32 on just really the syntax and what these building blocks can do for us um does
570:34 building blocks can do for us um does the poin have to be does the p uh in
570:39 the poin have to be does the p uh in does the pop pointer have to be an point
570:41 does the pop pointer have to be an point to an integer short answer no and we'll
570:42 to an integer short answer no and we'll come back to this for now for the sake
570:44 come back to this for now for the sake of discussion we're only dealing with
570:45 of discussion we're only dealing with integers like the number 50 uh you
570:47 integers like the number 50 uh you mentioned strings or characters
570:49 mentioned strings or characters absolutely we're about to go there soon
570:51 absolutely we're about to go there soon so you can use the address of anything
570:53 so you can use the address of anything you want in the computer's memory so in
570:55 you want in the computer's memory so in fact let's translate this now to just
570:57 fact let's translate this now to just the same picture just to help you wrap
570:59 the same picture just to help you wrap your minds around what these two lines
571:00 your minds around what these two lines of code really fundamentally are doing
571:03 of code really fundamentally are doing so if I come back to my grid of memory
571:04 so if I come back to my grid of memory here let's plop the number 50 in the
571:07 here let's plop the number 50 in the variable n at the bottom right like it
571:09 variable n at the bottom right like it was before so this is that first line of
571:10 was before so this is that first line of code as before but with the new second
571:13 code as before but with the new second line of code as soon as I create P what
571:16 line of code as soon as I create P what do I do well first remember that n lives
571:19 do I do well first remember that n lives somewhere in the computer's memory
571:21 somewhere in the computer's memory usually I don't care precisely where it
571:22 usually I don't care precisely where it is but for the sake of discussion let's
571:24 is but for the sake of discussion let's suppose it's at ox123 which is easier to
571:26 suppose it's at ox123 which is easier to say than where it actually ended up and
571:28 say than where it actually ended up and now what is p well p is just another
571:30 now what is p well p is just another variable and variables live in memory
571:32 variable and variables live in memory too so let me just hypothesize that P
571:35 too so let me just hypothesize that P lives up here and it turns out that P
571:39 lives up here and it turns out that P once you assign it the value of
571:41 once you assign it the value of Ampersand N means that c will take a
571:44 Ampersand N means that c will take a look at the variable n realize oh it
571:46 look at the variable n realize oh it lives at ox123 and what goes in the
571:49 lives at ox123 and what goes in the value of p is literally
571:52 value of p is literally ox123 so again it's still an integer
571:55 ox123 so again it's still an integer which is confusing but it's technically
571:57 which is confusing but it's technically an integer being used as an address and
571:59 an integer being used as an address and now just a a prompt here notice that
572:02 now just a a prompt here notice that this pointer is pretty darn big it's
572:04 this pointer is pretty darn big it's like eight squares what's the
572:06 like eight squares what's the implication of that because I did that
572:07 implication of that because I did that deliberately
572:09 deliberately how big must a pointer apparently be in
572:11 how big must a pointer apparently be in most modern systems would you say okay
572:14 most modern systems would you say okay good computers today are very big you
572:15 good computers today are very big you have gigabytes of RAM in your computer
572:17 have gigabytes of RAM in your computer you therefore need big pointers to be
572:18 you therefore need big pointers to be able to point at memory that's
572:20 able to point at memory that's conceptually pretty far away so to be
572:21 conceptually pretty far away so to be clear how many bytes does a pointer
572:23 clear how many bytes does a pointer apparently take up well it seems to take
572:25 apparently take up well it seems to take up eight in total integers by convention
572:27 up eight in total integers by convention nowadays are usually four pointers
572:29 nowadays are usually four pointers though nowadays are typically eight in
572:32 though nowadays are typically eight in this case so I'm drawing it in a manner
572:33 this case so I'm drawing it in a manner consistent with the reality even though
572:34 consistent with the reality even though at the end of the day it's not really
572:36 at the end of the day it's not really that interesting what values are in here
572:38 that interesting what values are in here in fact let's emerge from these weeds I
572:41 in fact let's emerge from these weeds I don't really care what else is going on
572:42 don't really care what else is going on in my computer's memory at the moment
572:44 in my computer's memory at the moment because I've only got those two lines of
572:45 because I've only got those two lines of Juicy code defining n and defining P so
572:48 Juicy code defining n and defining P so let's hide all of the other squares and
572:50 let's hide all of the other squares and honestly I mean it when I say that
572:52 honestly I mean it when I say that programmers need to know that a variable
572:55 programmers need to know that a variable exists somewhere in memory and needs to
572:57 exists somewhere in memory and needs to be able to get that address using like
572:59 be able to get that address using like the Ampersand but you're never going to
573:01 the Ampersand but you're never going to print F like I did the actual address
573:04 print F like I did the actual address like it's not generally interesting
573:06 like it's not generally interesting unless you're debugging your code but
573:07 unless you're debugging your code but you're not going to like start typing
573:09 you're not going to like start typing out crazy Ox numbers in your code to
573:11 out crazy Ox numbers in your code to move things around you just need to know
573:13 move things around you just need to know that the computer can figure out where
573:15 that the computer can figure out where things are so frankly by that logic who
573:17 things are so frankly by that logic who cares that it's ox123 right tomorrow it
573:20 cares that it's ox123 right tomorrow it could be Ox 456 or something else so one
573:23 could be Ox 456 or something else so one of the ways to think of a pointer is
573:25 of the ways to think of a pointer is literally as a variable that points at
573:28 literally as a variable that points at something else and indeed in this case P
573:32 something else and indeed in this case P yeah technically it has an address and
573:33 yeah technically it has an address and yeah technically it's ox123 in the story
573:36 yeah technically it's ox123 in the story but honestly who cares I just need to
573:37 but honestly who cares I just need to know that you using p i can get to the
573:41 know that you using p i can get to the value n and so what are these addresses
573:44 value n and so what are these addresses and in fact if Carter wouldn't mind
573:45 and in fact if Carter wouldn't mind joining me up here for a moment what are
573:47 joining me up here for a moment what are these addresses well just like in our
573:49 these addresses well just like in our human world we have mailboxes even
573:50 human world we have mailboxes even though you might not check it very
573:51 though you might not check it very frequently nowadays but to get physical
573:53 frequently nowadays but to get physical mail every uh home every business has a
573:55 mail every uh home every business has a unique address the uh science and
573:58 unique address the uh science and engineering complex is 150 Western
574:00 engineering complex is 150 Western Avenue Austin Massachusetts 02134 USA
574:03 Avenue Austin Massachusetts 02134 USA and theoretically that uniquely
574:05 and theoretically that uniquely identifies that building in the world
574:07 identifies that building in the world well here we we have two mailboxes um
574:10 well here we we have two mailboxes um over here we have a value n that happens
574:13 over here we have a value n that happens to live I'll claim it address ox123 and
574:16 to live I'll claim it address ox123 and then over here I claim there's another
574:18 then over here I claim there's another address uh called by name p i don't
574:21 address uh called by name p i don't actually care where it is even though it
574:23 actually care where it is even though it definitely exists somewhere in the
574:24 definitely exists somewhere in the computer's memory but if this is p which
574:26 computer's memory but if this is p which is a variable and that's n another
574:29 is a variable and that's n another variable ideally this mailbox would be
574:31 variable ideally this mailbox would be twice as big because of the number of
574:33 twice as big because of the number of btes using but Home Depot only had
574:34 btes using but Home Depot only had identical size mailboxes but here is p
574:36 identical size mailboxes but here is p one variable there is in another
574:39 one variable there is in another variable if I open up this mailbox what
574:43 variable if I open up this mailbox what should I find inside of it based on our
574:45 should I find inside of it based on our story thus far like what value will I
574:48 story thus far like what value will I pull out dramatically in just a
574:50 pull out dramatically in just a moment yeah I think ox1 123 Now using
574:54 moment yeah I think ox1 123 Now using this you can kind of think of this as
574:55 this you can kind of think of this as like x marks the spot no pun intended
574:57 like x marks the spot no pun intended where I can now like walk around the
574:59 where I can now like walk around the computer's memory and find my way to
575:02 computer's memory and find my way to that location by sort of following the
575:03 that location by sort of following the treasure map or if I want it more
575:05 treasure map or if I want it more dramatically uh thanks to our little
575:07 dramatically uh thanks to our little Yale foam finger here here you can think
575:08 Yale foam finger here here you can think of it more abstractly as p is just
575:10 of it more abstractly as p is just pointing at n okay that's not going over
575:13 pointing at n okay that's not going over well so let's switch over to the Harvard
575:14 well so let's switch over to the Harvard one so p is
575:22 pointing so p is pointing at n and so it turns out we will be able to write code
575:24 turns out we will be able to write code now that will do the equivalent of me
575:26 now that will do the equivalent of me walking over to n but for now Carter if
575:28 walking over to n but for now Carter if you want to reveal what's in the mailbox
575:29 you want to reveal what's in the mailbox we should see indeed the number 50 so
575:33 we should see indeed the number 50 so that's really all that's oh I feel
575:35 that's really all that's oh I feel Carter's waiting for Applause so like
575:36 Carter's waiting for Applause so like really well nicely done
575:44 thank you so that's just like a physical metaphor of what's going on here in one
575:46 metaphor of what's going on here in one variable we have an address and that
575:47 variable we have an address and that variable by convention is called a
575:49 variable by convention is called a pointer in the other variable per week
575:51 pointer in the other variable per week one we just have a value like n and you
575:54 one we just have a value like n and you can yes follow the map and walk yourself
575:57 can yes follow the map and walk yourself to that particular address and we'll see
575:58 to that particular address and we'll see how to do that in code but what's really
576:00 how to do that in code but what's really interesting is this abstraction that
576:02 interesting is this abstraction that pointers literally or really I guess
576:04 pointers literally or really I guess figuratively point at some other value
576:07 figuratively point at some other value in memory all right questions then on
576:09 in memory all right questions then on pointers in this form pointers point to
576:11 pointers in this form pointers point to each other can pointers point to each
576:13 each other can pointers point to each other so yes there's things called
576:15 other so yes there's things called double pointers we're not going to see
576:17 double pointers we're not going to see them anytime soon but using star star
576:19 them anytime soon but using star star you can express an address of an address
576:22 you can express an address of an address um but we won't see that just yet other
576:24 um but we won't see that just yet other questions on
576:27 questions on pointers yeah in
576:34 front our array so to summarize our arrays then pointers so short answer
576:37 arrays then pointers so short answer there's a relationship and we'll come
576:38 there's a relationship and we'll come back to that in a little bit but arrays
576:40 back to that in a little bit but arrays are technically different from pointers
576:42 are technically different from pointers but we we're going to be able to blur
576:43 but we we're going to be able to blur the lines a little bit by using one like
576:45 the lines a little bit by using one like the other but let me come back to that
576:46 the other but let me come back to that in just a bit of time all right so if we
576:49 in just a bit of time all right so if we have now this mental model if you will
576:52 have now this mental model if you will of like what a pointer is in memory I
576:54 of like what a pointer is in memory I think we can start to peel back a layer
576:57 think we can start to peel back a layer of uh simplification that we've been
576:59 of uh simplification that we've been assuming for the past few weeks since
577:00 assuming for the past few weeks since week one so a string recall is a
577:03 week one so a string recall is a sequence of characters and so if you
577:05 sequence of characters and so if you want to create a string that says Hi in
577:07 want to create a string that says Hi in all caps and an next exclamation point
577:08 all caps and an next exclamation point we do string s equals quote unquote high
577:11 we do string s equals quote unquote high and we can hard code it like this or we
577:12 and we can hard code it like this or we could use get string but for now just
577:14 could use get string but for now just assume that I hardcoded it into my code
577:16 assume that I hardcoded it into my code to always say hi in all caps with an
577:19 to always say hi in all caps with an exclamation point well what does that
577:20 exclamation point well what does that look like in the computer's memory well
577:22 look like in the computer's memory well let's stop looking at the entire memory
577:24 let's stop looking at the entire memory let's just focus on really what's going
577:26 let's just focus on really what's going on once you create a string called s and
577:29 on once you create a string called s and store in it hi you know that a couple of
577:32 store in it hi you know that a couple of things are happening H and I and the
577:35 things are happening H and I and the exclamation point are ending up in the
577:36 exclamation point are ending up in the computer's memory we know from week two
577:39 computer's memory we know from week two that this thing the so-called null
577:41 that this thing the so-called null character n AKA back slz is also being
577:45 character n AKA back slz is also being added for you and it's somewhere in
577:47 added for you and it's somewhere in memory at the moment I don't really care
577:48 memory at the moment I don't really care where I drew it at the bottom right yes
577:50 where I drew it at the bottom right yes it has an address but for now it just
577:51 it has an address but for now it just ends up somewhere and in fact here's a
577:53 ends up somewhere and in fact here's a little visual cue as to how this happens
577:57 little visual cue as to how this happens in C anytime you use double quotes to
578:00 in C anytime you use double quotes to give you a string you can imagine that
578:02 give you a string you can imagine that the double quotes are like a a clue to
578:04 the double quotes are like a a clue to not only store Hi exclamation point but
578:07 not only store Hi exclamation point but also put the null character there for
578:09 also put the null character there for you and this is in contrast to what
578:11 you and this is in contrast to what chars if you want individual characters
578:13 chars if you want individual characters what syntax did we use
578:15 what syntax did we use instead so single quotes single quotes
578:17 instead so single quotes single quotes do not add magically a back SL zero they
578:20 do not add magically a back SL zero they literally just store one character so
578:22 literally just store one character so again strings have always been a little
578:23 again strings have always been a little special you get some extra an extra bite
578:25 special you get some extra an extra bite for free so that you know where the
578:27 for free so that you know where the string ends and functions like stir
578:29 string ends and functions like stir compare can then find their way there so
578:31 compare can then find their way there so in memory it might indeed look a little
578:33 in memory it might indeed look a little like this and if we assume that there's
578:36 like this and if we assume that there's going to be somewhere in memory these
578:38 going to be somewhere in memory these things are going to be somewhere in
578:39 things are going to be somewhere in memory we can address them per week two
578:41 memory we can address them per week two by way of the name of the variable so if
578:43 by way of the name of the variable so if s is the name of the variable S braet 0
578:45 s is the name of the variable S braet 0 is how you would refer to the first
578:46 is how you would refer to the first letter s bracket 1 s braet two and if
578:48 letter s bracket 1 s braet two and if you really want s bracket 3 would get
578:50 you really want s bracket 3 would get you at the uh null character at the very
578:53 you at the uh null character at the very end but what is s so technically in this
578:57 end but what is s so technically in this line of code here not only is the
578:59 line of code here not only is the computer giving you memory for h i
579:02 computer giving you memory for h i exclamation point back sl0 we turns out
579:05 exclamation point back sl0 we turns out that s itself must take up some amount
579:07 that s itself must take up some amount of space right because s is the variable
579:09 of space right because s is the variable and every time we' talked about
579:10 and every time we' talked about variables thus far I've given you a
579:11 variables thus far I've given you a rectangle on the screen in which to
579:13 rectangle on the screen in which to store its value so let's assume for the
579:16 store its value so let's assume for the sake of discussion that the H is at
579:18 sake of discussion that the H is at ox123 and I is at o x124 exclamation
579:22 ox123 and I is at o x124 exclamation points at o x125 and the null characters
579:24 points at o x125 and the null characters at ox1 126 well what then is s well s is
579:28 at ox1 126 well what then is s well s is just going to be some other variable and
579:30 just going to be some other variable and I'll draw it somewhat abstractly without
579:31 I'll draw it somewhat abstractly without all the other boxes up here and I'll
579:33 all the other boxes up here and I'll claim that the name of this variable is
579:35 claim that the name of this variable is s but it turns out what is s really how
579:39 s but it turns out what is s really how do strings Really Work Well s is a
579:42 do strings Really Work Well s is a variable and has been since week one but
579:44 variable and has been since week one but when you define it what the computer is
579:46 when you define it what the computer is doing for you automatically is when it
579:48 doing for you automatically is when it knows you want to store Hi exclamation
579:50 knows you want to store Hi exclamation point it puts that somewhere in memory
579:52 point it puts that somewhere in memory the computer then figures out for you
579:54 the computer then figures out for you what's the address of the very first
579:56 what's the address of the very first character and it stores that address and
579:59 character and it stores that address and only that address in the variable you
580:01 only that address in the variable you created on the left hand side of the
580:03 created on the left hand side of the equal sign and that's enough like to
580:05 equal sign and that's enough like to represent a string with three letters of
580:07 represent a string with three letters of the alph alphabet or punctuation you
580:09 the alph alphabet or punctuation you don't need three variables you just need
580:12 don't need three variables you just need one you just need to know the beginning
580:14 one you just need to know the beginning of the string why why is it sufficient
580:17 of the string why why is it sufficient for a variable to Only Store the first
580:19 for a variable to Only Store the first byes address and not all of the bytes
580:27 addresses exactly because of the design of strings per week two we always null
580:30 of strings per week two we always null terminate them so it suffices to only
580:32 terminate them so it suffices to only remember the first bites address because
580:34 remember the first bites address because from there you can sort of follow the
580:35 from there you can sort of follow the breadcrumbs bite after bite after bite
580:37 breadcrumbs bite after bite after bite and until you see the new line Sorry the
580:40 and until you see the new line Sorry the the null character you know that all of
580:41 the null character you know that all of those characters are apparently part of
580:44 those characters are apparently part of the same string so this is what's been
580:46 the same string so this is what's been going on in the me computer's memory all
580:48 going on in the me computer's memory all since week one and in fact if we
580:50 since week one and in fact if we abstract this away you can really think
580:52 abstract this away you can really think of s as being just this really a pointer
580:56 of s as being just this really a pointer to that chunk of memory so in fact what
580:59 to that chunk of memory so in fact what do we have here well in the left to
581:02 do we have here well in the left to recap on the code here on the left hand
581:04 recap on the code here on the left hand side string that's what ensures that
581:06 side string that's what ensures that we'll actually be able to store a string
581:08 we'll actually be able to store a string in a variable called s we're going to
581:10 in a variable called s we're going to have on the uh right hand side though
581:12 have on the uh right hand side though the actual value so let me switch back
581:14 the actual value so let me switch back to VSS code here and let me change my
581:16 to VSS code here and let me change my code to no longer involve integers alone
581:18 code to no longer involve integers alone so I'm going to add the uh cs50 Library
581:22 so I'm going to add the uh cs50 Library just so that I can use some shortcuts in
581:24 just so that I can use some shortcuts in there cs50.h and then in my main
581:26 there cs50.h and then in my main function I'm going to go ahead and do
581:28 function I'm going to go ahead and do this string s equals quote unquote high
581:30 this string s equals quote unquote high in all caps exclamation point and then
581:32 in all caps exclamation point and then I'm going to go ahead and print out
581:33 I'm going to go ahead and print out using percent S as always back sln the
581:36 using percent S as always back sln the value of s so this program at the moment
581:38 value of s so this program at the moment not interesting at all it's just week
581:40 not interesting at all it's just week one stuff again/ addresses indeed prints
581:42 one stuff again/ addresses indeed prints out high but it turns out that now that
581:46 out high but it turns out that now that I know this what's really been going on
581:48 I know this what's really been going on underneath the hood all this time well
581:50 underneath the hood all this time well here's that same line of code that
581:52 here's that same line of code that defines the variable called s and it
581:55 defines the variable called s and it turns out anyone want to guess what
581:57 turns out anyone want to guess what string is actually a synonym
581:59 string is actually a synonym for string it turns out is kind of a
582:02 for string it turns out is kind of a white lie we've been telling since week
582:04 white lie we've been telling since week one there is no such thing as string as
582:06 one there is no such thing as string as a keyword in in C it's technically a
582:10 a keyword in in C it's technically a cs50 thing
582:11 cs50 thing yeah it's a pointer to a character so
582:15 yeah it's a pointer to a character so really all this time we've kind of been
582:16 really all this time we've kind of been lying to you there is no string quote
582:18 lying to you there is no string quote unquote it's actually Char star and if I
582:21 unquote it's actually Char star and if I may it dramatically here go the training
582:25 may it dramatically here go the training wheels like okay that didn't land very
582:27 wheels like okay that didn't land very well so uh what have we been doing well
582:30 well so uh what have we been doing well it turns out that string is a much
582:32 it turns out that string is a much easier way conceptually to think about
582:33 easier way conceptually to think about what a string of characters is like my
582:35 what a string of characters is like my God if we had to start in week one by
582:36 God if we had to start in week one by having you type char
582:38 having you type char like yeah you might get past it but like
582:39 like yeah you might get past it but like this is just way too much ugly syntax
582:41 this is just way too much ugly syntax not intellectually interesting at all so
582:42 not intellectually interesting at all so we abstract it away what a Char star was
582:45 we abstract it away what a Char star was in the first week of C by telling you
582:47 in the first week of C by telling you it's actually called string now string
582:49 it's actually called string now string is a term of art like C programmers
582:51 is a term of art like C programmers programmers are in any language we'll
582:52 programmers are in any language we'll use the word string to mean a sequence
582:54 use the word string to mean a sequence of characters but in C it's not
582:56 of characters but in C it's not technically a word unto itself it's
582:59 technically a word unto itself it's rather a synonym that we ourselves
583:01 rather a synonym that we ourselves created in some form so in fact how did
583:04 created in some form so in fact how did we do this well think back to just last
583:06 we do this well think back to just last week last week I proposed that it'd be
583:08 week last week I proposed that it'd be really nice if we had a person data type
583:10 really nice if we had a person data type which the creators of C did not think of
583:12 which the creators of C did not think of decades ago but that's okay we can
583:14 decades ago but that's okay we can Define it ourselves what did we do here
583:16 Define it ourselves what did we do here well using syntax like this recall that
583:20 well using syntax like this recall that we defined a person to be what to be
583:22 we defined a person to be what to be this structure this structure using the
583:24 this structure this structure using the new keyword last week struct means that
583:27 new keyword last week struct means that a person is just a name and a number and
583:29 a person is just a name and a number and it could have been other things we just
583:30 it could have been other things we just kept it simple but how did I associate
583:34 kept it simple but how did I associate person with that structure well we
583:36 person with that structure well we claimed that it was this value here type
583:39 claimed that it was this value here type def which as you might expect defines a
583:41 def which as you might expect defines a data type so what did we do as cs50 back
583:45 data type so what did we do as cs50 back in week one without telling you well we
583:48 in week one without telling you well we could have done something like this like
583:50 could have done something like this like int itself is a little cryptic and maybe
583:52 int itself is a little cryptic and maybe we should have to keep things even
583:53 we should have to keep things even simpler said hey everyone turns out you
583:55 simpler said hey everyone turns out you can Define integers in C and if you
583:57 can Define integers in C and if you wanted to do this well if you want to
583:59 wanted to do this well if you want to create the keyword integer as a data
584:01 create the keyword integer as a data type you can just typ def it to int so
584:04 type you can just typ def it to int so typed def creates the word on the far
584:06 typed def creates the word on the far right integer and U creates a synonym
584:09 right integer and U creates a synonym for it in this case called int so what
584:11 for it in this case called int so what did we do in week one without telling
584:14 did we do in week one without telling you we have a line of code like this in
584:16 you we have a line of code like this in the cs-50 library that Associates quote
584:20 the cs-50 library that Associates quote unquote string with more cryptically
584:23 unquote string with more cryptically charar and this is why in week one
584:26 charar and this is why in week one onward anytime you use the cs50 library
584:29 onward anytime you use the cs50 library you can write the word string as though
584:31 you can write the word string as though it's a real C data type and that's just
584:33 it's a real C data type and that's just because we wanted to have this
584:34 because we wanted to have this abstraction these training wheels on for
584:35 abstraction these training wheels on for the first weeks so we don't have to get
584:37 the first weeks so we don't have to get weeds of all this crazy memory stuff we
584:39 weeds of all this crazy memory stuff we can sort of talk about strings at a
584:40 can sort of talk about strings at a higher level but that's all they are
584:43 higher level but that's all they are strings are the address of the first
584:45 strings are the address of the first character in that sequence of characters
584:48 character in that sequence of characters questions now on any of these details
584:52 questions now on any of these details yeah strings
584:55 yeah strings liar good question what about the
584:56 liar good question what about the strings Library which we have used um
584:59 strings Library which we have used um unrelated so it does not define the word
585:01 unrelated so it does not define the word string everything in there actually
585:03 string everything in there actually relates to char stars and so in fact if
585:06 relates to char stars and so in fact if you've used the CS5
585:08 you've used the CS5 uh manual uh which is just our
585:10 uh manual uh which is just our userfriendly version of the actual
585:12 userfriendly version of the actual manual pages for the official language C
585:14 manual pages for the official language C you'll see throughout that now if you
585:16 you'll see throughout that now if you start poking around or turning off less
585:17 start poking around or turning off less comfortable mode you'll actually see
585:19 comfortable mode you'll actually see that we've changed any mentions of
585:21 that we've changed any mentions of charar in the official documentation for
585:23 charar in the official documentation for these first weeks to just string to
585:25 these first weeks to just string to simplify it but underneath the hood C
585:27 simplify it but underneath the hood C does not know the word string per se as
585:30 does not know the word string per se as a keyword but it's absolutely a concept
585:32 a keyword but it's absolutely a concept that like every program in the world
585:34 that like every program in the world knows about and in fact in other
585:36 knows about and in fact in other languages in Python for instance there
585:37 languages in Python for instance there will actually be a proper string
585:39 will actually be a proper string although it's not going to be called
585:40 although it's not going to be called string it's going to be called stir St
585:42 string it's going to be called stir St Str for short questions on these strings
585:46 Str for short questions on these strings here well let me propose there's one
585:48 here well let me propose there's one other feature of this syntax that we can
585:50 other feature of this syntax that we can now leverage as follows let me propose
585:53 now leverage as follows let me propose that if we go back to the previous
585:55 that if we go back to the previous version of my code here wherein let me
585:58 version of my code here wherein let me switch back to vs code in just a moment
586:00 switch back to vs code in just a moment I'm going to rewind in vs code to the
586:02 I'm going to rewind in vs code to the integer version of my code from before
586:05 integer version of my code from before and most recently it looked looked like
586:07 and most recently it looked looked like this before when we were using integers
586:09 this before when we were using integers only and not in fact strings at all let
586:12 only and not in fact strings at all let me propose that there's this other
586:14 me propose that there's this other feature of C that we can use that
586:16 feature of C that we can use that actually allows us to go to an address
586:18 actually allows us to go to an address so at the moment let me just rewind and
586:20 so at the moment let me just rewind and do make addresses to remind you what
586:21 do make addresses to remind you what this program do did when it was using
586:23 this program do did when it was using integers alone and there's that address
586:26 integers alone and there's that address why because on line seven notice I'm
586:28 why because on line seven notice I'm printing out the value of P which is a
586:31 printing out the value of P which is a pointer so of course it's going to look
586:32 pointer so of course it's going to look like an address but let me zoom out now
586:35 like an address but let me zoom out now and make one change and instead of
586:37 and make one change and instead of printing out P how can I use today's
586:40 printing out P how can I use today's second new operator not the Ampersand
586:42 second new operator not the Ampersand but the star to actually go to that
586:45 but the star to actually go to that address well what I can actually do on
586:47 address well what I can actually do on this line of code is this if I want to
586:49 this line of code is this if I want to print out the actual integer 50 that's
586:51 print out the actual integer 50 that's in that variable or equivalently at that
586:54 in that variable or equivalently at that address I can go to P here and not print
586:58 address I can go to P here and not print P literally because that's just an
586:59 P literally because that's just an address I can now say star p and star p
587:03 address I can now say star p and star p means go there more technically
587:05 means go there more technically dreference p that is follow the treasure
587:08 dreference p that is follow the treasure map to the actual address and do what
587:09 map to the actual address and do what Carter did open the mailbox and print
587:12 Carter did open the mailbox and print whatever was in the mailbox which recall
587:14 whatever was in the mailbox which recall was the actual number 50 so let me try
587:17 was the actual number 50 so let me try this let me recompile the code so make
587:21 this let me recompile the code so make addresses okay let me clear my terminal
587:23 addresses okay let me clear my terminal window do/ addresses this time I
587:24 window do/ addresses this time I shouldn't see the ox anything I should
587:26 shouldn't see the ox anything I should see just the number 50 in this case and
587:30 see just the number 50 in this case and here to is kind of a unfortunate design
587:33 here to is kind of a unfortunate design decision certainly pedagogically I would
587:34 decision certainly pedagogically I would say in C if I zoom in on this code star
587:38 say in C if I zoom in on this code star is unfortunately being used in two
587:39 is unfortunately being used in two different ways in an Ideal World they
587:41 different ways in an Ideal World they would have used three different symbols
587:42 would have used three different symbols to make this more semantically clear but
587:44 to make this more semantically clear but this is what we're stuck with so in line
587:46 this is what we're stuck with so in line six when you declare a pointer that is a
587:49 six when you declare a pointer that is a variable that stores an address you put
587:51 variable that stores an address you put the type of variable that you want to
587:53 the type of variable that you want to point at then a star just because and
587:55 point at then a star just because and then the name of the variable and on the
587:57 then the name of the variable and on the right hand side you actually get the
587:59 right hand side you actually get the address of whatever using Ampersand but
588:01 address of whatever using Ampersand but when you want to go to an address you
588:04 when you want to go to an address you want to dreference a pointer you don't
588:06 want to dreference a pointer you don't use int again and we've never done that
588:08 use int again and we've never done that right once you declare a variable you
588:09 right once you declare a variable you never again mention the data type but in
588:11 never again mention the data type but in the world of pointers now if you want to
588:12 the world of pointers now if you want to not print out P but go to whatever
588:15 not print out P but go to whatever address p is storing you use star P here
588:19 address p is storing you use star P here so a good visual indicator would be when
588:21 so a good visual indicator would be when you declare a pointer that is make it
588:23 you declare a pointer that is make it exist in your program you have to
588:25 exist in your program you have to declare the data type with the star but
588:28 declare the data type with the star but when you use a pointer you just use the
588:30 when you use a pointer you just use the star in an Ideal World this would be a
588:32 star in an Ideal World this would be a completely different symbol but again
588:33 completely different symbol but again this is what we have questions now on
588:36 this is what we have questions now on that syntax
588:38 that syntax [Music]
588:40 [Music] yeah uh why can't we just do the Amper
588:43 yeah uh why can't we just do the Amper sand here are you saying it was still a
588:45 sand here are you saying it was still a little quiet so strictly speaking we do
588:47 little quiet so strictly speaking we do not need line six so this is really for
588:49 not need line six so this is really for pedagogical sake that I am um defining a
588:52 pedagogical sake that I am um defining a separate variable p and then printing it
588:54 separate variable p and then printing it out at this point though I'm just kind
588:56 out at this point though I'm just kind of you know going in circles if you will
588:58 of you know going in circles if you will because more simple would have been what
589:00 because more simple would have been what I would have done in week one which
589:02 I would have done in week one which would be get rid of P alt together get
589:04 would be get rid of P alt together get rid of P here and just print out n right
589:06 rid of P here and just print out n right but today we're just giving you this new
589:08 but today we're just giving you this new building block these this new syntax via
589:11 building block these this new syntax via which you can figure out the address of
589:12 which you can figure out the address of something and then reverse the process
589:14 something and then reverse the process later and actually go to it as
589:17 later and actually go to it as well other questions on what we've done
589:19 well other questions on what we've done here with these
589:21 here with these pointers all right well let's context
589:23 pointers all right well let's context switch back to the string now and see
589:25 switch back to the string now and see what more we can do with this here in
589:28 what more we can do with this here in the case of our strings here let me uh
589:31 the case of our strings here let me uh refine this to zoom out let me delete
589:34 refine this to zoom out let me delete the integer related code here let me do
589:37 the integer related code here let me do string s equals quote unquote high in
589:39 string s equals quote unquote high in all caps let me go ahead and for the
589:41 all caps let me go ahead and for the moment include cs50.h at the top so that
589:45 moment include cs50.h at the top so that indeed I can use the keyword s or string
589:48 indeed I can use the keyword s or string rather and let me go ahead now and do
589:50 rather and let me go ahead now and do something more than I did last time last
589:52 something more than I did last time last time I did print F of percent s back sln
589:55 time I did print F of percent s back sln and then I printed out s and again I'll
589:57 and then I printed out s and again I'll recompile this just for clarity make
589:59 recompile this just for clarity make addresses addresses that just prints out
590:01 addresses addresses that just prints out high so that's again week one stuff but
590:03 high so that's again week one stuff but now that we have this other bit of
590:05 now that we have this other bit of syntax we can do some interesting things
590:08 syntax we can do some interesting things too so for instance suppose I want to
590:10 too so for instance suppose I want to print out not s itself but what if I
590:12 print out not s itself but what if I want to print out the address of s like
590:15 want to print out the address of s like at what memory location is s well I can
590:17 at what memory location is s well I can change my percent s to percent P which
590:19 change my percent s to percent P which now we know P is for pointer so percent
590:21 now we know P is for pointer so percent p means print out the value of a pointer
590:24 p means print out the value of a pointer that is an address and here I can
590:26 that is an address and here I can actually print out s itself but why that
590:29 actually print out s itself but why that is we'll see in a moment let me do this
590:33 is we'll see in a moment let me do this here go the training wheels string does
590:36 here go the training wheels string does not technically EX exist but it does if
590:37 not technically EX exist but it does if I'm using the cs-50 library but if I get
590:39 I'm using the cs-50 library but if I get rid of the cs50 library as I'm
590:41 rid of the cs50 library as I'm metaphorically doing by taking off the
590:43 metaphorically doing by taking off the training wheels I can't use the word
590:44 training wheels I can't use the word string anymore and in fact let me make
590:46 string anymore and in fact let me make this mistake deliberately as you might
590:48 this mistake deliberately as you might have accidentally in past weeks here is
590:50 have accidentally in past weeks here is the error message I get if I forget the
590:52 the error message I get if I forget the cs50 library use of Undeclared
590:55 cs50 library use of Undeclared identifier string did you mean standard
590:57 identifier string did you mean standard in it's trying to be helpful but it's
590:58 in it's trying to be helpful but it's not because I didn't mean standard in so
591:00 not because I didn't mean standard in so indeed this is confirmation that c does
591:02 indeed this is confirmation that c does not know the word string exists at least
591:05 not know the word string exists at least as a keyword exists as a concept but not
591:07 as a keyword exists as a concept but not a keyword so I could fix this by adding
591:10 a keyword so I could fix this by adding back the cs50 library but that's kind of
591:12 back the cs50 library but that's kind of a step backwards educationally is
591:13 a step backwards educationally is instead of a step forward what could I
591:15 instead of a step forward what could I do instead to fix this now if the
591:17 do instead to fix this now if the training wheels are now off
591:25 yeah exactly replace string quote unquote with Char star instead so I'm
591:27 unquote with Char star instead so I'm going to go ahead and change this to
591:29 going to go ahead and change this to char technically you can put the literal
591:31 char technically you can put the literal star here the asterisk or you can put it
591:34 star here the asterisk or you can put it there or you can put it here by
591:35 there or you can put it here by convention is to do what I done from the
591:37 convention is to do what I done from the beginning put the star next to the name
591:39 beginning put the star next to the name of the variable as opposed to anywhere
591:41 of the variable as opposed to anywhere else uh let me go ahead now and or sorry
591:44 else uh let me go ahead now and or sorry I meant to add the spaces there you
591:45 I meant to add the spaces there you could do this too but this would be the
591:47 could do this too but this would be the most normal convention so now let's do
591:49 most normal convention so now let's do this make addresses compile is okay now
591:52 this make addresses compile is okay now do slash addresses what should I see
591:55 do slash addresses what should I see high or something else feel free to just
591:59 high or something else feel free to just call it
592:00 call it out so still high you say someone
592:03 out so still high you say someone else memory location a memory location
592:06 else memory location a memory location all right so could be one of the two
592:07 all right so could be one of the two options right either I'm going to see
592:09 options right either I'm going to see the string or I'm going to see a memory
592:10 the string or I'm going to see a memory address though I do in fact see a memory
592:12 address though I do in fact see a memory address and this one's quite different
592:14 address and this one's quite different from the integer one but does anyone now
592:16 from the integer one but does anyone now want to explain why you were correct why
592:19 want to explain why you were correct why am I seeing the address down here and
592:21 am I seeing the address down here and not hi it's subtle
592:29 yeah exactly because I left my percent P there which means hey print F show me a
592:30 there which means hey print F show me a pointer but this is where printf is
592:32 pointer but this is where printf is smart and has been smart since week zero
592:35 smart and has been smart since week zero humans who invented printf decades ago
592:38 humans who invented printf decades ago uh wrote code that notices that okay
592:40 uh wrote code that notices that okay percent s means to treat the following
592:42 percent s means to treat the following value not as just an address per se that
592:45 value not as just an address per se that gets printed literally but printed as
592:47 gets printed literally but printed as with the mailbox demo is sort of a
592:49 with the mailbox demo is sort of a treasure map that leads you to the
592:51 treasure map that leads you to the address of a character so simply by
592:54 address of a character so simply by changing one character percent P to
592:55 changing one character percent P to percent s and if I now do make addresses
592:58 percent s and if I now do make addresses again and slash addresses this now is
593:00 again and slash addresses this now is identical to week one but hopefully
593:02 identical to week one but hopefully makes sense because percent s is just a
593:05 makes sense because percent s is just a clue to print f that means go to this
593:08 clue to print f that means go to this address in s print out every character
593:11 address in s print out every character there and thereafter until you see what
593:14 there and thereafter until you see what the null character and then stop
593:16 the null character and then stop printing anything more and this is why
593:18 printing anything more and this is why hi has printed since week one today we
593:21 hi has printed since week one today we can see the address percent P but this
593:24 can see the address percent P but this combination of having access to
593:25 combination of having access to addresses and the null Terminator is all
593:28 addresses and the null Terminator is all the information printf needs to actually
593:30 the information printf needs to actually do something more useful by like
593:32 do something more useful by like printing the actual
593:34 printing the actual strings any questions now on this
593:37 strings any questions now on this approach to percent s yeah in
593:42 approach to percent s yeah in back oh so why is it traditionally being
593:45 back oh so why is it traditionally being used in this way honestly like the word
593:46 used in this way honestly like the word string has been around for decades it's
593:48 string has been around for decades it's not a keyword you should be able to type
593:50 not a keyword you should be able to type in C unless you're using a library like
593:52 in C unless you're using a library like cs50 um and so percent s just means
593:54 cs50 um and so percent s just means string so even though it doesn't exist
593:56 string so even though it doesn't exist as a keyword percent s connotes string
593:59 as a keyword percent s connotes string and humans decades ago like today just
594:01 and humans decades ago like today just kind of know what that means so they
594:02 kind of know what that means so they could have chosen any letter of the
594:03 could have chosen any letter of the alphabet but s sort of makes the most
594:05 alphabet but s sort of makes the most sense all right well let's in back other
594:09 sense all right well let's in back other question good question before let me
594:11 question good question before let me zoom in I did not use a star before the
594:14 zoom in I did not use a star before the S why well it's subtle here but printf
594:18 S why well it's subtle here but printf is was invented years ago to know given
594:21 is was invented years ago to know given an address like in the variable s printf
594:24 an address like in the variable s printf knows to go there so if we looked at the
594:25 knows to go there so if we looked at the source code that some human wrote years
594:27 source code that some human wrote years ago for C we would likely see the actual
594:30 ago for C we would likely see the actual uh asterisk that you're referring to
594:32 uh asterisk that you're referring to printf is taking on the responsibility
594:34 printf is taking on the responsibility for going to S if you were to do uh star
594:37 for going to S if you were to do uh star s here instead an asterisk and then s
594:40 s here instead an asterisk and then s that would now be literally a character
594:43 that would now be literally a character because if I say p uh star s that means
594:46 because if I say p uh star s that means go to the address in s and all you're
594:47 go to the address in s and all you're going to find there is a single
594:49 going to find there is a single character what printf wants to know is
594:51 character what printf wants to know is not what is the character there what is
594:53 not what is the character there what is the address of that character why
594:55 the address of that character why because printf needs to walk through the
594:58 because printf needs to walk through the rest of those characters looking for the
595:01 rest of those characters looking for the final null character and in fact let me
595:03 final null character and in fact let me see with a bit more syntax if we can
595:05 see with a bit more syntax if we can highlight this a bit more let me do this
595:07 highlight this a bit more let me do this in addition to printing s let's try out
595:09 in addition to printing s let's try out our syntax in another way let me print
595:11 our syntax in another way let me print out with percent s how about uh not s
595:14 out with percent s how about uh not s here but let's print out some addresses
595:16 here but let's print out some addresses percent s back sln close quote and then
595:20 percent s back sln close quote and then let's print out how about this the first
595:22 let's print out how about this the first character in the string s would be
595:24 character in the string s would be called s bracket Z but how do I get the
595:27 called s bracket Z but how do I get the address of the first character in s well
595:30 address of the first character in s well I could technically just use today's new
595:32 I could technically just use today's new primitive I can just add an ampon that
595:33 primitive I can just add an ampon that always gives me the address of some
595:35 always gives me the address of some value so when I end this thought and
595:40 value so when I end this thought and clear my terminal window and run make
595:41 clear my terminal window and run make addresses still compiles when I run
595:44 addresses still compiles when I run addresses in just a moment any guesses
595:46 addresses in just a moment any guesses as to what I will see line by line this
595:48 as to what I will see line by line this will print out two
595:50 will print out two things and you don't have to remember
595:52 things and you don't have to remember what the actual number was but at a high
595:54 what the actual number was but at a high level what will be printed now the same
595:57 level what will be printed now the same thing twice why well when I run this
595:59 thing twice why well when I run this what I'm printing here and let me zoom
596:01 what I'm printing here and let me zoom in at the bottom I ined see two really
596:03 in at the bottom I ined see two really long addresses but they're in fact the
596:05 long addresses but they're in fact the same why well that's because again if s
596:08 same why well that's because again if s is the address of a character as implied
596:11 is the address of a character as implied Now by either the cs50 word string or
596:14 Now by either the cs50 word string or the actual phrase Char star well then s
596:16 the actual phrase Char star well then s is just an address by contrast per week
596:19 is just an address by contrast per week two s bracket 0 is a Char always has
596:23 two s bracket 0 is a Char always has been a Char a specific Char but if you
596:25 been a Char a specific Char but if you want the address of that Char you just
596:27 want the address of that Char you just add the Ampersand well it turns out that
596:29 add the Ampersand well it turns out that strings per the definition we keep
596:31 strings per the definition we keep emphasizing is just the address of the
596:33 emphasizing is just the address of the first character in a string so of course
596:34 first character in a string so of course if you do this you're going to see the
596:36 if you do this you're going to see the exact same thing and if I do this a bit
596:38 exact same thing and if I do this a bit more generally you don't want to copy
596:40 more generally you don't want to copy paste but this is just for uh
596:41 paste but this is just for uh visualization sake let me print out all
596:43 visualization sake let me print out all the characters so another another
596:45 the characters so another another another and let me change this to print
596:47 another and let me change this to print out the address of bracket 1 bracket two
596:50 out the address of bracket 1 bracket two and bracket three so all four characters
596:52 and bracket three so all four characters Hi exclamation point and the null
596:54 Hi exclamation point and the null character notice I'm using percent P for
596:57 character notice I'm using percent P for all of them so if I now do make
596:59 all of them so if I now do make addresses and do/ addresses now notice
597:01 addresses and do/ addresses now notice and this is kind of cool the first two
597:04 and this is kind of cool the first two are indeed still the same but what's no
597:06 are indeed still the same but what's no noteworthy about the other values on the
597:12 screen yeah they're consecutive each of these is just one bite away even if
597:14 these is just one bite away even if you're not good at hex yet and there's a
597:15 you're not good at hex yet and there's a crazy number of digits here who cares
597:17 crazy number of digits here who cares they're all the same except for the last
597:18 they're all the same except for the last ones four four and then 5 six seven and
597:21 ones four four and then 5 six seven and this confirms what I've been claiming
597:23 this confirms what I've been claiming for weeks is that in an array all of the
597:25 for weeks is that in an array all of the characters are back to back to back
597:27 characters are back to back to back contiguous One Bites away so with just
597:31 contiguous One Bites away so with just this Ampersand with just this star like
597:32 this Ampersand with just this star like it's actually a pretty cool tool in the
597:34 it's actually a pretty cool tool in the toolkit to have because you can start to
597:36 toolkit to have because you can start to poke around what's actually going on
597:39 poke around what's actually going on inside of the computer's memory and in
597:41 inside of the computer's memory and in fact if we do this I can introduce one
597:44 fact if we do this I can introduce one other cool trick here if you will let me
597:46 other cool trick here if you will let me propose that we can actually now do
597:50 propose that we can actually now do arithmetic on pointers and you don't
597:52 arithmetic on pointers and you don't have to you'll see a simpler way to do
597:54 have to you'll see a simpler way to do this but now that you have perhaps this
597:56 this but now that you have perhaps this underlying understanding of where things
597:58 underlying understanding of where things are in memory and it's just addresses we
598:00 are in memory and it's just addresses we can actually do something kind of neat
598:02 can actually do something kind of neat we can do something like this uh let me
598:05 we can do something like this uh let me go back to how about uh the string
598:07 go back to how about uh the string version of this with high and let me do
598:10 version of this with high and let me do this instead let me um clean this up a
598:12 this instead let me um clean this up a bit get rid of some of these lines of
598:14 bit get rid of some of these lines of code and let me do this let me print out
598:16 code and let me do this let me print out percent C percent C percent C let me get
598:19 percent C percent C percent C let me get rid of all these Amper Sands we're going
598:20 rid of all these Amper Sands we're going to roll back to like week two stuff just
598:23 to roll back to like week two stuff just to be clear when I compile and run this
598:25 to be clear when I compile and run this version of the program and I'll zoom in
598:28 version of the program and I'll zoom in what should get printed on the screen
598:31 what should get printed on the screen this is just week two stuff now no
598:33 this is just week two stuff now no pointers per se yeah
598:36 pointers per se yeah mhm just Hi exclamation point one per
598:40 mhm just Hi exclamation point one per line because I have all of these back
598:41 line because I have all of these back slash ends so let me do that let me go
598:43 slash ends so let me do that let me go down here make addresses enter okay
598:46 down here make addresses enter okay pretty good/ addresses and indeed Hi
598:49 pretty good/ addresses and indeed Hi exclamation point But now if you're
598:51 exclamation point But now if you're getting a little more comfortable and
598:52 getting a little more comfortable and it's fine if you're not yet today but
598:54 it's fine if you're not yet today but over the coming week or weeks as you get
598:55 over the coming week or weeks as you get a little more comfortable with the
598:57 a little more comfortable with the equivalence of addresses with our
598:59 equivalence of addresses with our definition in the past of arrays and
599:01 definition in the past of arrays and strings and all of this you can start to
599:03 strings and all of this you can start to play around and I can do this instead if
599:05 play around and I can do this instead if I I want to print out the first
599:08 I I want to print out the first character in the string I could do like
599:10 character in the string I could do like week two s bracket zero like that will
599:12 week two s bracket zero like that will always work and you can keep using that
599:13 always work and you can keep using that that's not a cs50 thing it's just a
599:15 that's not a cs50 thing it's just a convenience in C but I could technically
599:18 convenience in C but I could technically print out not s because s is an address
599:21 print out not s because s is an address but what would be the syntax I could use
599:23 but what would be the syntax I could use to say print out the character at
599:26 to say print out the character at s any
599:28 s any Instinct how can I say go to the address
599:30 Instinct how can I say go to the address in
599:31 in s it's one of two possible answers
599:34 s it's one of two possible answers today so of our two new uh uh of our two
599:38 today so of our two new uh uh of our two new operators today we have the
599:39 new operators today we have the Ampersand and the star which one will
599:42 Ampersand and the star which one will lead us to what is that an address so
599:45 lead us to what is that an address so the star so in fact if I want to print
599:47 the star so in fact if I want to print out what is that address zero at the
599:50 out what is that address zero at the address S I can just do star s and if
599:53 address S I can just do star s and if you really want to get fancy how do you
599:55 you really want to get fancy how do you print out the second character that's
599:57 print out the second character that's immediately to the right of it so to
599:58 immediately to the right of it so to speak well you can go to with the D
600:01 speak well you can go to with the D reference operator and do you want to
600:02 reference operator and do you want to answer this
600:03 answer this one s+ one Argo pointer arithmetic like
600:08 one s+ one Argo pointer arithmetic like you can do math simple addition
600:09 you can do math simple addition subtraction whatever on pointers if you
600:11 subtraction whatever on pointers if you want and you can do this here too so
600:14 want and you can do this here too so star you want to pluck this one off too
600:15 star you want to pluck this one off too how do I print out the last character
600:17 how do I print out the last character the
600:18 the third s plus2 right because if you know
600:21 third s plus2 right because if you know and understand that like a string is
600:23 and understand that like a string is just a sequence of characters every
600:24 just a sequence of characters every character is just a bite and these bites
600:26 character is just a bite and these bites are back to back to back you can just go
600:28 are back to back to back you can just go wherever you want in the computer's
600:30 wherever you want in the computer's memory and here I can do make addresses
600:32 memory and here I can do make addresses again/ addresses and voila we now have
600:35 again/ addresses and voila we now have high exclamation point so we haven't
600:37 high exclamation point so we haven't printed out anything new but again just
600:39 printed out anything new but again just by using these two new operators the
600:41 by using these two new operators the erson and the star you can figure out
600:43 erson and the star you can figure out the address of something and you can go
600:45 the address of something and you can go to the address of something okay
600:47 to the address of something okay question in
600:48 question in back indeed it ends up being the exact
600:51 back indeed it ends up being the exact same and so I might have used this term
600:52 same and so I might have used this term before the Amper sand technique where
600:55 before the Amper sand technique where sorry the square bracket technique where
600:57 sorry the square bracket technique where you do s bracket 0o s bracket One S
600:59 you do s bracket 0o s bracket One S bracket 2 that's actually what we would
601:01 bracket 2 that's actually what we would really call syntactic Sugar like it
601:03 really call syntactic Sugar like it works and you can use it you should use
601:04 works and you can use it you should use it it's nice and simple
601:06 it it's nice and simple but the square bracket notation
601:07 but the square bracket notation underneath the hood is essentially being
601:10 underneath the hood is essentially being converted to this which this is not fun
601:12 converted to this which this is not fun right like this is when you want to show
601:13 right like this is when you want to show off to your friends like you know how to
601:14 off to your friends like you know how to do cool stuff in code but this is not as
601:16 do cool stuff in code but this is not as readable as just s bracket Z and one and
601:19 readable as just s bracket Z and one and two but that's all that's happening
601:20 two but that's all that's happening underneath the hood and so again this is
601:22 underneath the hood and so again this is why in cs50 we spend time on some of
601:24 why in cs50 we spend time on some of these lower level building blocks
601:25 these lower level building blocks because if you assume that indeed your
601:27 because if you assume that indeed your computer's memory is just this grid of
601:28 computer's memory is just this grid of btes and you have now the code ability
601:31 btes and you have now the code ability in code to get an address and go to an
601:34 in code to get an address and go to an address you can start doing any anything
601:36 address you can start doing any anything you want and you can poke around a
601:37 you want and you can poke around a computer's memory at any location and
601:39 computer's memory at any location and here in lies the danger like I'm kind of
601:41 here in lies the danger like I'm kind of on the honor System right now that if my
601:42 on the honor System right now that if my string is high exclamation point it's
601:45 string is high exclamation point it's kind of up to me to go to the first bite
601:47 kind of up to me to go to the first bite the second and the third but I could get
601:49 the second and the third but I could get kind of crazy now and if I want to see
601:51 kind of crazy now and if I want to see what's going on in the computer's memory
601:53 what's going on in the computer's memory I mean there's nothing stopping me from
601:55 I mean there's nothing stopping me from doing like s+ 50 and let's see what's
601:58 doing like s+ 50 and let's see what's there so make addresses do/ addresses Hi
602:01 there so make addresses do/ addresses Hi and then okay nothing it seems well how
602:03 and then okay nothing it seems well how about 5,000 bytes away let's poke around
602:06 about 5,000 bytes away let's poke around what's inside of the computer's memory
602:07 what's inside of the computer's memory so make addresses again uh make
602:10 so make addresses again uh make addresses addresses enter okay still
602:13 addresses addresses enter okay still nothing there let's try
602:15 nothing there let's try 50,000 all right do make addresses do SL
602:19 50,000 all right do make addresses do SL addresses okay there we see it so you've
602:22 addresses okay there we see it so you've probably done this some of you by
602:23 probably done this some of you by accident because you probably went too
602:25 accident because you probably went too far to the left or to the right in an
602:26 far to the left or to the right in an array touching memory that you shouldn't
602:28 array touching memory that you shouldn't suffice it to say I should not go
602:30 suffice it to say I should not go blindly touching 50,000 bytes away cuz
602:33 blindly touching 50,000 bytes away cuz who knows what's there and indeed in
602:35 who knows what's there and indeed in your computer computer when a program is
602:36 your computer computer when a program is running the computer uh segments it into
602:39 running the computer uh segments it into different segments of memory and if you
602:41 different segments of memory and if you get a little too greedy and you touch
602:43 get a little too greedy and you touch another segment of memory that
602:44 another segment of memory that technically was not allocated to you by
602:46 technically was not allocated to you by Mac OS or Windows or Linux or the
602:48 Mac OS or Windows or Linux or the operating system bad things happen and
602:50 operating system bad things happen and you get a segmentation fault and that
602:52 you get a segmentation fault and that means it's a bug in your code so you can
602:54 means it's a bug in your code so you can now do this and this means hackers too
602:56 now do this and this means hackers too can do things like this if they can
602:58 can do things like this if they can somehow inject code into your C program
603:01 somehow inject code into your C program maybe they can poke around the
603:03 maybe they can poke around the computer's memory and indeed this is
603:05 computer's memory and indeed this is kind of the technique whereby maybe a
603:06 kind of the technique whereby maybe a really sophisticated hacker can jump to
603:08 really sophisticated hacker can jump to this memory this memory this memory
603:10 this memory this memory this memory looking for something like your password
603:12 looking for something like your password or your financial information or
603:14 or your financial information or anything that's in the program but at
603:16 anything that's in the program but at some other address there's nothing
603:17 some other address there's nothing stopping an adversary at least right now
603:20 stopping an adversary at least right now from poking around if they can execute
603:22 from poking around if they can execute code on your computer from doing this
603:24 code on your computer from doing this kind of thing so there and again is the
603:25 kind of thing so there and again is the power of C but also the danger and
603:27 power of C but also the danger and you'll absolutely suffer more segals in
603:30 you'll absolutely suffer more segals in the coming days but ultimately the goal
603:31 the coming days but ultimately the goal is going to be to help you solve them
603:34 is going to be to help you solve them ultimately uh and fix things um but for
603:37 ultimately uh and fix things um but for now I think that was that was quite a
603:40 now I think that was that was quite a bit so let me propose that we go ahead
603:42 bit so let me propose that we go ahead and take our longer break here maybe 10
603:44 and take our longer break here maybe 10 minutes and have ourselves some whoopy
603:46 minutes and have ourselves some whoopy pies in the transcept we'll be back in
603:48 pies in the transcept we'll be back in 10 all right so we're back and to recap
603:53 10 all right so we're back and to recap where we left off you now have this new
603:55 where we left off you now have this new capability in code to do pointer
603:57 capability in code to do pointer arithmetic like treat addresses as
603:59 arithmetic like treat addresses as numbers which they really are in heximal
604:01 numbers which they really are in heximal or otherwise and like add them together
604:03 or otherwise and like add them together and kind of poke around a computer's
604:04 and kind of poke around a computer's memory and it was asked during break
604:07 memory and it was asked during break actually how we might further harness
604:09 actually how we might further harness this in the context of string so I
604:11 this in the context of string so I didn't change the code we wrote just
604:12 didn't change the code we wrote just before break recall that we last broke
604:15 before break recall that we last broke the program by checking out bytes 50,000
604:18 the program by checking out bytes 50,000 bytes away but let's not do that and
604:20 bytes away but let's not do that and let's actually try printing out not
604:21 let's actually try printing out not individual characters like I did per the
604:24 individual characters like I did per the percent C but why don't we try printing
604:26 percent C but why don't we try printing out strings and substrings if you will
604:29 out strings and substrings if you will so let me clear my terminal window let
604:30 so let me clear my terminal window let me change all of these percent C's to
604:33 me change all of these percent C's to percent s percent s percent s and then
604:35 percent s percent s percent s and then let me rewind to what we've been doing
604:37 let me rewind to what we've been doing since week one with strings which is
604:39 since week one with strings which is just print them out for instance with
604:41 just print them out for instance with that first line and the only difference
604:43 that first line and the only difference at the moment is that now I took off the
604:46 at the moment is that now I took off the training wheels I got rid of cs50.h
604:48 training wheels I got rid of cs50.h wherein string is typ Def to char star
604:52 wherein string is typ Def to char star for you got rid of that so now on line
604:54 for you got rid of that so now on line five I'm declaring S as being a Char
604:56 five I'm declaring S as being a Char star which just means the address of a
604:57 star which just means the address of a character and print f is smart enough to
604:59 character and print f is smart enough to know that the end of a string is
605:00 know that the end of a string is wherever that null character is but now
605:02 wherever that null character is but now that I can do pointer arithmetic notice
605:05 that I can do pointer arithmetic notice that I could do something like this if I
605:07 that I could do something like this if I want to print out s i just print out s
605:09 want to print out s i just print out s suppose I do s+ one here and s+2 here
605:13 suppose I do s+ one here and s+2 here again after changing percent C to
605:15 again after changing percent C to percent s any intuition around what this
605:19 percent s any intuition around what this code will now print on the screen line
605:21 code will now print on the screen line by
605:23 by line yeah
605:25 line yeah thoughts okay reasonable conjecture
605:27 thoughts okay reasonable conjecture maybe the memory address of H that of I
605:30 maybe the memory address of H that of I that of exclamation point but other
605:32 that of exclamation point but other [Music]
605:33 [Music] thoughts yeah I think it's going to do
605:35 thoughts yeah I think it's going to do the latter it's going to print high in
605:37 the latter it's going to print high in the usual way because honestly line five
605:39 the usual way because honestly line five is this rather line six is the same as
605:42 is this rather line six is the same as like week one stuff except we took off
605:44 like week one stuff except we took off the training wheel of string and we're
605:45 the training wheel of string and we're calling it Char star but I think line
605:47 calling it Char star but I think line seven is indeed going to print out I and
605:50 seven is indeed going to print out I and line eight is just going to print out
605:52 line eight is just going to print out because it'll be just the exclamation
605:54 because it'll be just the exclamation point prti will still be smart enough to
605:56 point prti will still be smart enough to know where each of those substrings a
605:59 know where each of those substrings a portions of the string End by the same
606:01 portions of the string End by the same logic as always but let me go ahead and
606:02 logic as always but let me go ahead and zoom out run make addresses enter
606:05 zoom out run make addresses enter compiles okay/ addresses and now indeed
606:09 compiles okay/ addresses and now indeed this is all a string is it's a sequence
606:11 this is all a string is it's a sequence of characters identified by its first
606:13 of characters identified by its first bite if you then start poking around and
606:16 bite if you then start poking around and tell printf to print at what's at the
606:18 tell printf to print at what's at the next bite or the next next bite it's
606:20 next bite or the next next bite it's going to do its same thing printing out
606:22 going to do its same thing printing out that character and everything after it
606:24 that character and everything after it up until that null character so again
606:26 up until that null character so again even though there's like a lot going on
606:28 even though there's like a lot going on we've introduced these two new operators
606:30 we've introduced these two new operators like there's nothing that's happening
606:32 like there's nothing that's happening today that hasn't been happening for
606:33 today that hasn't been happening for weeks but hopefully through this week uh
606:35 weeks but hopefully through this week uh this week's lecture this week's problem
606:37 this week's lecture this week's problem set and Beyond you'll start to realize
606:39 set and Beyond you'll start to realize that now you just have more tools via
606:40 that now you just have more tools via which to harness those lower level
606:42 which to harness those lower level implementation details so last week too
606:46 implementation details so last week too recall one other implementation detail I
606:48 recall one other implementation detail I claimed that you could not compare two
606:51 claimed that you could not compare two strings quite as easily as you could
606:53 strings quite as easily as you could compare to uh integers for instance and
606:57 compare to uh integers for instance and I told you to use a different function
606:59 I told you to use a different function instead that you probably used one or
607:00 instead that you probably used one or more times with the past problem set how
607:03 more times with the past problem set how are you supposed to compare strings
607:06 are you supposed to compare strings apparently yeah so string compare stir
607:08 apparently yeah so string compare stir comp that additional function that we
607:10 comp that additional function that we said H you just have to use it for now
607:12 said H you just have to use it for now but you might have a little intuition
607:14 but you might have a little intuition already as to like why we have to use
607:16 already as to like why we have to use Stir compare and we can't just use
607:17 Stir compare and we can't just use equals equals to compare strings like
607:20 equals equals to compare strings like any intuition for this already why was
607:22 any intuition for this already why was Stir compare necessary last week
607:24 Stir compare necessary last week equ perfect equals equals would compare
607:27 equ perfect equals equals would compare literally the two memory addresses
607:29 literally the two memory addresses instead of the actual strings character
607:31 instead of the actual strings character by character and unless the memory
607:33 by character and unless the memory addresses are literally the same so you
607:35 addresses are literally the same so you compare the that exact same memory
607:37 compare the that exact same memory address two different strings probably
607:39 address two different strings probably are not going to be considered equal
607:41 are not going to be considered equal even if to us humans they indeed look
607:43 even if to us humans they indeed look equal so let's see this let me go ahead
607:44 equal so let's see this let me go ahead and close addresses. C and actually
607:47 and close addresses. C and actually before I do one last mention one of the
607:49 before I do one last mention one of the powerful things about Pointer arithmetic
607:51 powerful things about Pointer arithmetic as an aside is that c and really the
607:54 as an aside is that c and really the compiler is smart enough to know how
607:57 compiler is smart enough to know how many bytes to keep adding and adding and
608:00 many bytes to keep adding and adding and by that I mean this right now we got
608:01 by that I mean this right now we got lucky because a string is a sequence of
608:03 lucky because a string is a sequence of characters and by definition every
608:04 characters and by definition every character is is a single bite you can
608:06 character is is a single bite you can poke around and do s+ one to get the
608:08 poke around and do s+ one to get the next bite S Plus 2 to get the uh the
608:11 next bite S Plus 2 to get the uh the third bite however if we weren't dealing
608:13 third bite however if we weren't dealing with strings suppose we were dealing
608:14 with strings suppose we were dealing with integers that were in an array back
608:17 with integers that were in an array back to back to back if you wanted to get at
608:19 to back to back if you wanted to get at the next integer you could still do plus
608:22 the next integer you could still do plus one or plus two to get at the next or
608:24 one or plus two to get at the next or the next next integer you would not
608:26 the next next integer you would not start to get into the weeds of doing
608:28 start to get into the weeds of doing plus four and then plus eight you don't
608:30 plus four and then plus eight you don't have to know or care how big the data
608:32 have to know or care how big the data types are in the computer C and the
608:34 types are in the computer C and the compiler will figure that out for you
608:35 compiler will figure that out for you based on the data type in question so
608:37 based on the data type in question so keep that in mind if ever doing this on
608:39 keep that in mind if ever doing this on a different data type uh than chars all
608:41 a different data type uh than chars all right so let me go ahead and open up a
608:43 right so let me go ahead and open up a file that I wrote in advance most of and
608:46 file that I wrote in advance most of and let me hide my terminal window and show
608:47 let me hide my terminal window and show you this so here is a program called
608:49 you this so here is a program called compare. C whose purpose in life is to
608:51 compare. C whose purpose in life is to compare two strings I'm back to using
608:53 compare two strings I'm back to using the cs50 library because at least for
608:55 the cs50 library because at least for now in probably a couple more weeks it
608:57 now in probably a couple more weeks it is so much easier to get input from the
608:59 is so much easier to get input from the user using cs50's function get in but
609:02 user using cs50's function get in but we'll conclude today by taking off those
609:03 we'll conclude today by taking off those training wheels as well so you can see
609:05 training wheels as well so you can see how you can actually get user input with
609:07 how you can actually get user input with nothing cs50 specific so line six and
609:10 nothing cs50 specific so line six and seven pretty boring week one stuff get
609:12 seven pretty boring week one stuff get an INT called I get an INT called J and
609:15 an INT called I get an INT called J and store them in two variables I and J
609:16 store them in two variables I and J respectively if I equals equals J print
609:19 respectively if I equals equals J print out the same else print out that they're
609:21 out the same else print out that they're different let me just stipulate for time
609:23 different let me just stipulate for time sake I'm pretty sure this code is
609:25 sake I'm pretty sure this code is correct this will get two integers from
609:26 correct this will get two integers from the human it will compare them and tell
609:28 the human it will compare them and tell me correctly if they're the same or
609:30 me correctly if they're the same or different and I'll prove as much by
609:33 different and I'll prove as much by running make compare
609:36 running make compare slash compare and I'll type in 50 for I
609:38 slash compare and I'll type in 50 for I 50 for J and they're the same and now
609:41 50 for J and they're the same and now I'll do how about 50 and say 13 and
609:45 I'll do how about 50 and say 13 and those are different so let me just
609:46 those are different so let me just stipulate this code is indeed correct
609:48 stipulate this code is indeed correct would have worked in week one also works
609:49 would have worked in week one also works now in week four but let me now change
609:52 now in week four but let me now change it to compare not two integers but as I
609:55 it to compare not two integers but as I hinted maybe two strings instead so let
609:57 hinted maybe two strings instead so let me go ahead and change this line of code
610:00 me go ahead and change this line of code to maybe be string s equals get string
610:03 to maybe be string s equals get string uh asking the user for s s uh then let's
610:06 uh asking the user for s s uh then let's change this second line here to be
610:08 change this second line here to be string T just to keep the variable name
610:10 string T just to keep the variable name short for now and T is a good uh Choice
610:13 short for now and T is a good uh Choice after s for something like this get
610:14 after s for something like this get string prompt the human for T and then
610:17 string prompt the human for T and then let's change our I and J here to do the
610:20 let's change our I and J here to do the wrong thing per the intuition earlier if
610:22 wrong thing per the intuition earlier if s equals equals T then print out the
610:25 s equals equals T then print out the same else print out that they're
610:26 same else print out that they're different now if I want I could take off
610:28 different now if I want I could take off at least some of the training wheels I
610:30 at least some of the training wheels I could change this to char star I could
610:31 could change this to char star I could change this to char star either is fine
610:34 change this to char star either is fine I still need the Cs library though
610:35 I still need the Cs library though because I'm using get string because
610:36 because I'm using get string because it's actually hard as we'll see today to
610:38 it's actually hard as we'll see today to get strings manually without using a
610:40 get strings manually without using a library but I'll keep it using string
610:42 library but I'll keep it using string just for now with the library all right
610:44 just for now with the library all right make compare
610:46 make compare again compare and now let me go ahead
610:48 again compare and now let me go ahead and type in for instance Hi exclamation
610:51 and type in for instance Hi exclamation point Enter and Hi exclamation point
610:53 point Enter and Hi exclamation point Enter and they're different all they're
610:57 Enter and they're different all they're obviously not visually but they are
610:58 obviously not visually but they are underneath the hood and you probably do
611:00 underneath the hood and you probably do have the intuition for this already
611:02 have the intuition for this already whereby what's going on underneath the
611:04 whereby what's going on underneath the hood is that we're comparing
611:05 hood is that we're comparing accidentally the two memory addresses so
611:08 accidentally the two memory addresses so in fact let's go there let's consider
611:10 in fact let's go there let's consider the memory and let me zoom out now so I
611:11 the memory and let me zoom out now so I can just have more btes to play with so
611:13 can just have more btes to play with so the squares are a little smaller than
611:14 the squares are a little smaller than before just so we can fit more in them
611:17 before just so we can fit more in them and let me propose that when I declare s
611:19 and let me propose that when I declare s on what was line six a moment ago it
611:23 on what was line six a moment ago it ends up somewhere in memory like the top
611:24 ends up somewhere in memory like the top leftand corner of my picture for
611:26 leftand corner of my picture for discussion sake and when I uh execute
611:29 discussion sake and when I uh execute that same line of code and get string is
611:32 that same line of code and get string is called and I type in high exclamation
611:34 called and I type in high exclamation point we know from week one that get
611:36 point we know from week one that get string puts it somewhere in the
611:37 string puts it somewhere in the computer's memory and I'll propose that
611:39 computer's memory and I'll propose that it's in like the bottom left hand corner
611:41 it's in like the bottom left hand corner of the screen here what happens after
611:43 of the screen here what happens after that well I know even though I don't
611:45 that well I know even though I don't generally care that Hi exclamation point
611:47 generally care that Hi exclamation point in the null character exist at some
611:49 in the null character exist at some address like ox123 12 4 125 126 for
611:53 address like ox123 12 4 125 126 for discussion sake and what's in s same as
611:56 discussion sake and what's in s same as before break 0x1 23 so that's all that's
611:59 before break 0x1 23 so that's all that's happening again on line six which is
612:02 happening again on line six which is pretty much the same as when we were
612:03 pretty much the same as when we were getting an S earlier but notice now with
612:06 getting an S earlier but notice now with line seven when I get a second variable
612:08 line seven when I get a second variable called T and I call get string again and
612:11 called T and I call get string again and by coincidence as the human I type the
612:13 by coincidence as the human I type the same thing well what happens here T gets
612:15 same thing well what happens here T gets its own chunk of memory maybe at the top
612:17 its own chunk of memory maybe at the top right uh that second version of high
612:20 right uh that second version of high gets somewhere else in memory you know
612:21 gets somewhere else in memory you know the computer could be smart and notice
612:23 the computer could be smart and notice it's the same but C doesn't generally do
612:25 it's the same but C doesn't generally do that for you it just plops it somewhere
612:26 that for you it just plops it somewhere else in memory and maybe it's at address
612:28 else in memory and maybe it's at address Ox 456 457 458 459 or wherever but you
612:33 Ox 456 457 458 459 or wherever but you can perhaps see where this is going
612:34 can perhaps see where this is going already T now of course contains the
612:36 already T now of course contains the address of that first bite and so in my
612:38 address of that first bite and so in my code on line nine when I compare s and t
612:42 code on line nine when I compare s and t for equality suffice it to say they are
612:45 for equality suffice it to say they are not equal because of the way the uh
612:48 not equal because of the way the uh strings are laid out in the computer's
612:49 strings are laid out in the computer's memory it's indeed looks the same the
612:51 memory it's indeed looks the same the same values are there but if we abstract
612:53 same values are there but if we abstract away further you can really see that s
612:55 away further you can really see that s and t are not the same themselves and so
612:59 and t are not the same themselves and so how did we fix this or really how did we
613:01 how did we fix this or really how did we avoid this last week without spilling
613:03 avoid this last week without spilling the beans and going down this Rabbit
613:05 the beans and going down this Rabbit Hole explaining like why you have to use
613:06 Hole explaining like why you have to use Stir compare well if I go back to my
613:08 Stir compare well if I go back to my code here let's do it now the right way
613:11 code here let's do it now the right way let me go ahead and include uh a line of
613:14 let me go ahead and include uh a line of code that says string compare of s comma
613:16 code that says string compare of s comma T both as inputs and then if you recall
613:20 T both as inputs and then if you recall what does stir compare return when two
613:22 what does stir compare return when two strings are equal there's three possible
613:24 strings are equal there's three possible return values so zero so one is for if
613:27 return values so zero so one is for if it comes alphabetically or atically
613:29 it comes alphabetically or atically first or second but for now I just want
613:32 first or second but for now I just want zero if I want to use S compare I do
613:34 zero if I want to use S compare I do need
613:35 need string.h so string.h does exist that's
613:38 string.h so string.h does exist that's not a cs50 thing there's no keyword
613:40 not a cs50 thing there's no keyword string as a data type that's a cs50
613:42 string as a data type that's a cs50 thing but string.h does exist so I think
613:45 thing but string.h does exist so I think now with that change on line 10 if I do
613:47 now with that change on line 10 if I do make compare and slash compare and then
613:51 make compare and slash compare and then run again uh type again High exclamation
613:53 run again uh type again High exclamation point High exclamation point I think now
613:56 point High exclamation point I think now they're the same and just as a uh second
613:59 they're the same and just as a uh second check high in L caps maybe high in
614:01 check high in L caps maybe high in lowercase those are in fact different
614:04 lowercase those are in fact different why well stir compare which was written
614:06 why well stir compare which was written by some other human decades ago is just
614:09 by some other human decades ago is just smart enough to know that it should go
614:11 smart enough to know that it should go to S and go to T start comparing them
614:15 to S and go to T start comparing them left to right stopping once it hits one
614:17 left to right stopping once it hits one or both null characters and return zero
614:20 or both null characters and return zero only if everything in s and and T are
614:23 only if everything in s and and T are exactly the same are any questions then
614:27 exactly the same are any questions then on this
614:28 on this here any questions on why we're using
614:30 here any questions on why we're using stir compare all right if no yeah oh in
614:34 stir compare all right if no yeah oh in the
614:41 middle yes so so why does why is it not the case with integers so it turns out
614:43 the case with integers so it turns out it's not the case with integers with
614:45 it's not the case with integers with floats with bulls with uh doubles with
614:48 floats with bulls with uh doubles with Longs like literally every other data
614:50 Longs like literally every other data type works correctly strings though are
614:52 type works correctly strings though are special they are useful enough in
614:54 special they are useful enough in programming and have been for decades
614:56 programming and have been for decades that the authors of printf and the
614:58 that the authors of printf and the authors of stir compare and bunches of
614:59 authors of stir compare and bunches of other functions Sterling for that matter
615:01 other functions Sterling for that matter just kind of treat strings special
615:04 just kind of treat strings special because they're just useful right we
615:05 because they're just useful right we humans interact using language be it
615:07 humans interact using language be it English or anything else and so it's
615:08 English or anything else and so it's just useful to have into the language C
615:12 just useful to have into the language C just sort of uh first class support for
615:14 just sort of uh first class support for this notion of strings of human text so
615:17 this notion of strings of human text so the short answer is just because like it
615:19 the short answer is just because like it just uh is necessar strings are
615:21 just uh is necessar strings are different they're implemented with this
615:23 different they're implemented with this address and the null character
615:25 address and the null character everything else though is just a value
615:28 everything else though is just a value but a string again is a white lie it's
615:29 but a string again is a white lie it's an address it's not a thing unto itself
615:33 an address it's not a thing unto itself good question yeah in front
615:39 oh really good question so in my code here in vs code what if I do this
615:44 here in vs code what if I do this instead of stir compare and instead of
615:46 instead of stir compare and instead of if s equals equals T what if I start
615:49 if s equals equals T what if I start playing around using star s and star T
615:53 playing around using star s and star T really interesting case to consider
615:54 really interesting case to consider let's go back to our sort of deductive
615:56 let's go back to our sort of deductive logic here so star the asteris operator
615:58 logic here so star the asteris operator today means go there so when I've typed
616:01 today means go there so when I've typed in high once and then high again both
616:04 in high once and then high again both uppercase for instance what is at the
616:06 uppercase for instance what is at the address s literally someone
616:09 address s literally someone else what is at the address s yeah uh so
616:13 else what is at the address s yeah uh so not quite at the address not so not what
616:16 not quite at the address not so not what is the address what is at the address
616:18 is the address what is at the address ox123 H and what is at the address Ox
616:22 ox123 H and what is at the address Ox 456 H also and so here you're kind of
616:26 456 H also and so here you're kind of cheating like you're comparing the first
616:28 cheating like you're comparing the first character of both strings but not every
616:31 character of both strings but not every other one now you could be really
616:32 other one now you could be really pedantic and here again this is like a
616:34 pedantic and here again this is like a good use of uh codee but you could do
616:37 good use of uh codee but you could do this if that and how about this
616:39 this if that and how about this craziness so star
616:41 craziness so star s+1 equals equals star t + one and and
616:46 s+1 equals equals star t + one and and you could do this for every character
616:47 you could do this for every character manually but that's why stir compare
616:49 manually but that's why stir compare exists it does all of this for you but
616:51 exists it does all of this for you but that's why and that's the intuition so I
616:52 that's why and that's the intuition so I would encourage you too anytime there's
616:54 would encourage you too anytime there's something kind of weird going on there's
616:56 something kind of weird going on there's I realize we might be um straining
616:58 I realize we might be um straining credibility now we haven't told you that
616:59 credibility now we haven't told you that many white lies and so most everything
617:02 many white lies and so most everything that we've seen thus far can explain
617:05 that we've seen thus far can explain pretty much all of the behavior up until
617:07 pretty much all of the behavior up until now from week one onward in C so let me
617:09 now from week one onward in C so let me revert this back to the right way if s
617:11 revert this back to the right way if s compare of S&T equals equals z this now
617:14 compare of S&T equals equals z this now is the right version of the code and now
617:16 is the right version of the code and now here is again where you can play so let
617:17 here is again where you can play so let me do this let me um clear my terminal
617:19 me do this let me um clear my terminal window just to tidy things up let me get
617:22 window just to tidy things up let me get rid of all of this comparison stuff and
617:23 rid of all of this comparison stuff and let's just see what's going on as you
617:25 let's just see what's going on as you are welcome to in your own code let's
617:27 are welcome to in your own code let's print out for instance as we might have
617:28 print out for instance as we might have in week one the value of s itself in a
617:31 in week one the value of s itself in a new line comma s and then let's just
617:33 new line comma s and then let's just print out T just to make sure it
617:34 print out T just to make sure it compiles and I'm not doing anything
617:36 compiles and I'm not doing anything wrong but this is not going to be that
617:37 wrong but this is not going to be that interesting and frankly I don't need
617:40 interesting and frankly I don't need string.h anymore because I'm not using
617:42 string.h anymore because I'm not using stir compare so make addresses addresses
617:45 stir compare so make addresses addresses there's my um oh sorry that's fun okay
617:50 there's my um oh sorry that's fun okay not percent T percent s here too ignore
617:53 not percent T percent s here too ignore that let's do this again make a oh and
617:55 that let's do this again make a oh and that's the wrong program okay dot SL
617:58 that's the wrong program okay dot SL let's do make
617:59 let's do make compare compare and let's type in high
618:02 compare compare and let's type in high again and high again and and now we just
618:04 again and high again and and now we just see the two strings I'm not comparing
618:06 see the two strings I'm not comparing but now we can kind of play around right
618:08 but now we can kind of play around right instead of printing out percent s which
618:10 instead of printing out percent s which prints the string how do I print the
618:13 prints the string how do I print the address in S I just need to make a
618:16 address in S I just need to make a slight change if I want to see not
618:19 slight change if I want to see not what's at s but I want to see s the
618:22 what's at s but I want to see s the address
618:29 yeah perfect so change percent s in both places here to percent P so now printf
618:32 places here to percent P so now printf will treat it literally as an dress it's
618:34 will treat it literally as an dress it's not going to do any fanciness this with
618:36 not going to do any fanciness this with a loop from left to right looking for
618:37 a loop from left to right looking for the null character it's just going to
618:38 the null character it's just going to print out S&T so let me clear my
618:40 print out S&T so let me clear my terminal run make compare whoops let's
618:43 terminal run make compare whoops let's do make comparecompare enter type in
618:46 do make comparecompare enter type in high type in high again and now you see
618:49 high type in high again and now you see oh so this is interesting it's not quite
618:52 oh so this is interesting it's not quite as straightforward as the other values
618:53 as straightforward as the other values which were slight one bite away they're
618:56 which were slight one bite away they're almost the same but this one ends in b0
618:59 almost the same but this one ends in b0 this one ends in F0 so they're indeed
619:01 this one ends in F0 so they're indeed separated by some number of bytes not
619:04 separated by some number of bytes not just one but a few because these strings
619:06 just one but a few because these strings are indeed longer all right so once
619:08 are indeed longer all right so once you've seen this here how can we now
619:11 you've seen this here how can we now maybe leverage this to solve other
619:13 maybe leverage this to solve other problems well let me propose that we do
619:15 problems well let me propose that we do this let me uh zoom out here let me
619:17 this let me uh zoom out here let me close compare and let me open up another
619:20 close compare and let me open up another program I wrote part of in advance
619:22 program I wrote part of in advance called
619:23 called copy.c so copy. C in theory makes a copy
619:27 copy.c so copy. C in theory makes a copy of a string how on line eight I'm using
619:31 of a string how on line eight I'm using the same thing as before get string
619:32 the same thing as before get string storing in a string or char star and
619:35 storing in a string or char star and asking the user for it then I'm not
619:37 asking the user for it then I'm not asking get string again I'm just making
619:39 asking get string again I'm just making a copy super simply with line 10 here
619:42 a copy super simply with line 10 here string T equals s now intuitively I
619:46 string T equals s now intuitively I think that's how I would copy a variable
619:47 think that's how I would copy a variable right that's how we've copied variables
619:49 right that's how we've copied variables every week thus far and see but
619:51 every week thus far and see but something's going to go wrong in line 12
619:54 something's going to go wrong in line 12 in English does someone want to explain
619:56 in English does someone want to explain what you think line 12 does don't worry
619:58 what you think line 12 does don't worry about finding any bugs or mistakes but
620:00 about finding any bugs or mistakes but what does line 12 seem to be doing using
620:02 what does line 12 seem to be doing using two upper which which is thanks to the
620:04 two upper which which is thanks to the ctype library which I've included the
620:06 ctype library which I've included the header file for
620:10 header file for yeah yeah right it's kind of like ugly
620:12 yeah yeah right it's kind of like ugly syntax but this would seem to be
620:14 syntax but this would seem to be capitalizing the first letter of T
620:16 capitalizing the first letter of T specifically and just changing it so we
620:19 specifically and just changing it so we have t bracket Zer here because we want
620:20 have t bracket Zer here because we want to save the change and we're passing to
620:22 to save the change and we're passing to two upper the first character here so
620:24 two upper the first character here so this is how we did uppercase in the past
620:26 this is how we did uppercase in the past and now I print out s and t respectively
620:29 and now I print out s and t respectively using percent s so this feels like it
620:31 using percent s so this feels like it should work I copied s and stored it in
620:34 should work I copied s and stored it in t on line 10 and then I change T and
620:37 t on line 10 and then I change T and only t on line 12 but you can perhaps if
620:40 only t on line 12 but you can perhaps if you're comfy thus far see where this is
620:42 you're comfy thus far see where this is going if I do make
620:45 going if I do make copy copy and let me type in lowercase
620:48 copy copy and let me type in lowercase Hi exclamation point this time just once
620:51 Hi exclamation point this time just once so I'm going to hit enter and watch what
620:53 so I'm going to hit enter and watch what we see for the value of s and
620:55 we see for the value of s and t huh the new value of SN T at the end
620:59 t huh the new value of SN T at the end of my program seems to be what it seems
621:03 of my program seems to be what it seems to be
621:04 to be the same high is capitalized both times
621:08 the same high is capitalized both times so what's the intuition then for this
621:10 so what's the intuition then for this why did this just
621:13 why did this just happen yeah and
621:15 happen yeah and back yeah I assigned S&T the same memory
621:18 back yeah I assigned S&T the same memory address so it did copy s into T but C
621:22 address so it did copy s into T but C takes this very literally what is s it's
621:24 takes this very literally what is s it's an address what is T it's a copy of that
621:26 an address what is T it's a copy of that address if you want to copy the whole
621:28 address if you want to copy the whole string like a normal human would expect
621:30 string like a normal human would expect hey you or someone has to do a lot more
621:31 hey you or someone has to do a lot more work you have to go to that address copy
621:34 work you have to go to that address copy this character this one this one this
621:35 this character this one this one this one and copy it to a new location in
621:37 one and copy it to a new location in memory that does not happen
621:39 memory that does not happen automatically here for you in C it does
621:41 automatically here for you in C it does in some other languages those of you
621:42 in some other languages those of you who've programmed in certain higher
621:43 who've programmed in certain higher level languages this just works as you
621:45 level languages this just works as you would hope and that's one of the
621:46 would hope and that's one of the benefits of python and other languages
621:48 benefits of python and other languages that we'll soon see but for now it
621:50 that we'll soon see but for now it literally takes at face value what this
621:52 literally takes at face value what this is copy the address into this address
621:54 is copy the address into this address and I'll make that more clear by getting
621:56 and I'll make that more clear by getting rid of the string keyword which again is
621:58 rid of the string keyword which again is just a type def this is technically an
622:00 just a type def this is technically an address here this is technically an
622:02 address here this is technically an address here so what's being being
622:04 address here so what's being being copied is the value of that address not
622:06 copied is the value of that address not all of the characters that might very
622:08 all of the characters that might very well follow it so I should make one note
622:11 well follow it so I should make one note to here I'm going to start getting more
622:13 to here I'm going to start getting more in the habit of trying to avoid
622:15 in the habit of trying to avoid segmentation faults because things could
622:18 segmentation faults because things could go wrong here for instance online 12
622:20 go wrong here for instance online 12 previously I was kind of blindly naively
622:23 previously I was kind of blindly naively dangerously assuming that there will be
622:25 dangerously assuming that there will be at least one character in s or t that
622:28 at least one character in s or t that might not be the case if the user Just
622:29 might not be the case if the user Just Hits enter there's no characters to
622:31 Hits enter there's no characters to uppercase and so this is reckless of me
622:33 uppercase and so this is reckless of me and could theoretically create a sigall
622:35 and could theoretically create a sigall so I should probably start to be smarter
622:38 so I should probably start to be smarter and say something like this if the
622:39 and say something like this if the length of T is greater than zero okay
622:43 length of T is greater than zero okay now it's safe to actually capitalize the
622:45 now it's safe to actually capitalize the first letter and that will decrease the
622:47 first letter and that will decrease the probability now of those segmentation
622:49 probability now of those segmentation faults by just not making any
622:52 faults by just not making any assumptions about what the human does
622:54 assumptions about what the human does almost always the your programs will
622:56 almost always the your programs will crash when you've made a mistake yes but
622:58 crash when you've made a mistake yes but the user gives you an input that you
623:00 the user gives you an input that you yourself did not
623:02 yourself did not expect so what does this all look like
623:04 expect so what does this all look like in memory well let's go back to the Big
623:06 in memory well let's go back to the Big Grid this time focusing on the copying
623:08 Grid this time focusing on the copying of values and let's do this here's S as
623:11 of values and let's do this here's S as in this new program just declared to be
623:13 in this new program just declared to be a Char star uh here is where my
623:15 a Char star uh here is where my lowercase High maybe ended up in the
623:17 lowercase High maybe ended up in the computer's memory that's probably at
623:19 computer's memory that's probably at ox123 12 4 125 whatever something like
623:22 ox123 12 4 125 whatever something like that and that's of course what ends up
623:24 that and that's of course what ends up in s as a value when I declare T I do
623:27 in s as a value when I declare T I do get a second variable called T just like
623:30 get a second variable called T just like before but when I copy s into T what
623:34 before but when I copy s into T what happens it's really just literally ox1 2
623:37 happens it's really just literally ox1 2 3 whatever the value of s is is now also
623:40 3 whatever the value of s is is now also the value of T and so if we abstract
623:42 the value of T and so if we abstract this away at a high level get rid of all
623:43 this away at a high level get rid of all of those extra squares this is what s
623:46 of those extra squares this is what s and t now are they're indeed copies but
623:48 and t now are they're indeed copies but copies of each other not copies of the
623:50 copies of each other not copies of the underlying characters and so if you
623:52 underlying characters and so if you follow those arrows um and try to print
623:55 follow those arrows um and try to print them both out after capitalizing one or
623:57 them both out after capitalizing one or the other you're going to unfortunately
623:58 the other you're going to unfortunately end up capitalizing not just one of them
624:01 end up capitalizing not just one of them s but both of them s and T because
624:04 s but both of them s and T because literally it's the same address any
624:07 literally it's the same address any questions then on this
624:09 questions then on this visualization
624:12 visualization yeah good question is this pass by
624:14 yeah good question is this pass by reference we haven't um we have not seen
624:18 reference we haven't um we have not seen in detail an example like that um right
624:21 in detail an example like that um right now you're copying by value but
624:23 now you're copying by value but references will come into play and
624:24 references will come into play and remind me in a bit if I haven't used
624:25 remind me in a bit if I haven't used that term yet but this is just copying
624:27 that term yet but this is just copying things by could have ended poorly value
624:31 things by could have ended poorly value other
624:32 other questions no all right so with this in
624:36 questions no all right so with this in mind like how do we actually copy things
624:39 mind like how do we actually copy things properly for this we actually need
624:41 properly for this we actually need another building block so today we give
624:42 another building block so today we give you two functions one of which is called
624:44 you two functions one of which is called Malo one of which is called free and
624:47 Malo one of which is called free and these are used all of the time by like
624:49 these are used all of the time by like every piece of software you and I use on
624:50 every piece of software you and I use on our Macs PCS and phones whether it's
624:52 our Macs PCS and phones whether it's written in C or some equivalent other
624:54 written in C or some equivalent other language Malo is for memory allocation
624:57 language Malo is for memory allocation it's a function that you can use to ask
624:59 it's a function that you can use to ask the operating system Mac OS Linux
625:01 the operating system Mac OS Linux Windows anything for some number of of
625:03 Windows anything for some number of of bytes one bite 100 bytes a gigabyte of
625:06 bytes one bite 100 bytes a gigabyte of memory you can ask malog for however
625:08 memory you can ask malog for however much memory you want in advance it will
625:10 much memory you want in advance it will return to you the address of the first
625:13 return to you the address of the first bite of memory that it found free for
625:15 bite of memory that it found free for you unlike a string it is not null
625:18 you unlike a string it is not null terminated and so the danger with malok
625:20 terminated and so the danger with malok is that it's on the honor System if you
625:22 is that it's on the honor System if you ask it for one bite or 10 bytes you the
625:24 ask it for one bite or 10 bytes you the programmer in like a variable have to
625:26 programmer in like a variable have to remember how many byes you requested one
625:28 remember how many byes you requested one or 10 or the like strings do that for
625:31 or 10 or the like strings do that for you not when we're getting now to this
625:32 you not when we're getting now to this low level Mal just going to give you
625:34 low level Mal just going to give you some memory and it's up to you to manage
625:35 some memory and it's up to you to manage it free does the opposite when you're
625:37 it free does the opposite when you're done with some chunk of memory you can
625:38 done with some chunk of memory you can free it by passing in that same address
625:40 free it by passing in that same address and just hand it back to Mac OS windows
625:42 and just hand it back to Mac OS windows or Linux and say I'm done with this you
625:44 or Linux and say I'm done with this you can let me use this for something else
625:46 can let me use this for something else later um as an aside if your computer
625:49 later um as an aside if your computer has ever like frozen or hung like the
625:52 has ever like frozen or hung like the whole thing maybe just spontaneously
625:53 whole thing maybe just spontaneously reboots yet another reason for a bug
625:55 reboots yet another reason for a bug like that might be if you write a
625:57 like that might be if you write a program with a bug that keeps Mal
625:59 program with a bug that keeps Mal loocking Mal loocking Mal loocking that
626:00 loocking Mal loocking Mal loocking that is asking for more and more and more
626:02 is asking for more and more and more memory but you make a mistake and you
626:04 memory but you make a mistake and you never free it well eventually the
626:06 never free it well eventually the computer is going to literally run out
626:08 computer is going to literally run out of memory and something's going to go
626:09 of memory and something's going to go wrong and that's often when computers
626:12 wrong and that's often when computers freeze like they're just out of memory
626:13 freeze like they're just out of memory it has the memory there but the program
626:15 it has the memory there but the program was trying to use too much of it
626:17 was trying to use too much of it endlessly so this too will be a mistake
626:19 endlessly so this too will be a mistake that some of us will surely make in the
626:21 that some of us will surely make in the coming weeks but hopefully you'll now
626:22 coming weeks but hopefully you'll now see the solution so let me go back to uh
626:25 see the solution so let me go back to uh vs code here and let me propose that we
626:28 vs code here and let me propose that we do the following I'll hide my terminal
626:29 do the following I'll hide my terminal window for a moment and I'm going to
626:31 window for a moment and I'm going to introduce another header file up here
626:33 introduce another header file up here and I promise there's not going to be
626:35 and I promise there's not going to be too many more of these but this one is
626:36 too many more of these but this one is called standard li. for standard library
626:41 called standard li. for standard library and in this file are the Declarations
626:43 and in this file are the Declarations the prototypes for malok and free and a
626:45 the prototypes for malok and free and a bunch of other stuff as well it lets me
626:47 bunch of other stuff as well it lets me now manage my own memory so let's focus
626:50 now manage my own memory so let's focus now on line 11 line 11 is where I went
626:52 now on line 11 line 11 is where I went wrong before because conceptually I want
626:54 wrong before because conceptually I want to copy the whole string but of course
626:56 to copy the whole string but of course I'm only copying modestly the individual
626:58 I'm only copying modestly the individual address so how do I copy the whole darn
627:01 address so how do I copy the whole darn thing well what I need to do is this
627:03 thing well what I need to do is this when when I declare T to be the address
627:05 when when I declare T to be the address of something in memory why don't I set T
627:08 of something in memory why don't I set T to be the address of a free chunk of
627:10 to be the address of a free chunk of memory so let me ask the operating
627:11 memory so let me ask the operating system give me this many bytes tell me
627:14 system give me this many bytes tell me what the address is and I'm going to
627:15 what the address is and I'm going to store that in t initially just so I know
627:17 store that in t initially just so I know where there's free space for me so how
627:19 where there's free space for me so how do I do that well quite simply I call
627:20 do I do that well quite simply I call malok and then I pass in the number of
627:22 malok and then I pass in the number of bytes that I need now for Hi exclamation
627:25 bytes that I need now for Hi exclamation point I think I need three although wait
627:27 point I think I need three although wait no I really need four because of the
627:29 no I really need four because of the null character but I don't think I
627:31 null character but I don't think I should be hardcoding numbers like this
627:33 should be hardcoding numbers like this cuz who knows what the human's going to
627:34 cuz who knows what the human's going to type in so I can actually use Sterling
627:37 type in so I can actually use Sterling of s and then plus one this will ask
627:40 of s and then plus one this will ask malok then for however many btes
627:43 malok then for however many btes corresponds to the number of characters
627:44 corresponds to the number of characters the human typed in plus one for again
627:46 the human typed in plus one for again the null character so it's just being
627:48 the null character so it's just being smart and defensive rather than choosing
627:50 smart and defensive rather than choosing a number myself but now all T is is a
627:54 a number myself but now all T is is a pointer if you will to some random chunk
627:56 pointer if you will to some random chunk of free space so there's nothing there
627:58 of free space so there's nothing there yet or there's you know bits there but
628:00 yet or there's you know bits there but who knows what value they are they're
628:01 who knows what value they are they're certainly not identical to what the
628:02 certainly not identical to what the human type did in I now have to do this
628:05 human type did in I now have to do this so how can I copy one string into the
628:08 so how can I copy one string into the other well let me do this instead of uh
628:12 other well let me do this instead of uh capitalizing something just yet let me
628:14 capitalizing something just yet let me do this how about four in I gets zero I
628:18 do this how about four in I gets zero I is less than the length of
628:20 is less than the length of s uh and then i++ so I'm going to
628:23 s uh and then i++ so I'm going to iterate for the whole length of the
628:24 iterate for the whole length of the string and in here I'm just going to do
628:27 string and in here I'm just going to do this the E character in t should be
628:29 this the E character in t should be identical to the E character in s so I'm
628:32 identical to the E character in s so I'm just literally copying from right to
628:34 just literally copying from right to left each and every character in s and I
628:37 left each and every character in s and I can trust that there's enough memory in
628:38 can trust that there's enough memory in t why cuz I asked for that many bytes
628:40 t why cuz I asked for that many bytes plus one now there's technically a bug
628:42 plus one now there's technically a bug here I actually should probably do this
628:45 here I actually should probably do this I should do plus one here or if you
628:49 I should do plus one here or if you prefer I should do less than or equal to
628:51 prefer I should do less than or equal to the Sterling but I think it's a little
628:53 the Sterling but I think it's a little clear to do the plus one why do I for
628:56 clear to do the plus one why do I for the first time want to go just beyond
628:58 the first time want to go just beyond the boundary of s and copy one more
629:02 the boundary of s and copy one more bite yeah yeah I need the null character
629:04 bite yeah yeah I need the null character like I could technically manually add it
629:06 like I could technically manually add it with some additional line of code but I
629:08 with some additional line of code but I might as well just copy it because back
629:09 might as well just copy it because back sl0 is back sl0 so this time and
629:12 sl0 is back sl0 so this time and probably only this time it's reasonable
629:14 probably only this time it's reasonable and correct to go just beyond the
629:16 and correct to go just beyond the boundary of your string so you copy the
629:18 boundary of your string so you copy the null terminating character so that the
629:19 null terminating character so that the computer also knows where T ends and now
629:22 computer also knows where T ends and now I think what I can do a little more
629:24 I think what I can do a little more safely is this let me go down here and
629:28 safely is this let me go down here and say t braet 0 equals 2 upper of t uh two
629:33 say t braet 0 equals 2 upper of t uh two upper of T bracket Z so same line of
629:35 upper of T bracket Z so same line of code as before if I actually want to be
629:37 code as before if I actually want to be really safe I should probably do this so
629:39 really safe I should probably do this so if the stir L of T is greater than zero
629:42 if the stir L of T is greater than zero so there's at least one bite there okay
629:44 so there's at least one bite there okay now it's safe to blindly capitalize the
629:46 now it's safe to blindly capitalize the first character and I think that now
629:48 first character and I think that now puts me in better shape so let me try
629:50 puts me in better shape so let me try this now let me uh open up my terminal
629:53 this now let me uh open up my terminal make uh copy do slash copy I'm going to
629:57 make uh copy do slash copy I'm going to type in Hi exclamation point in all
629:59 type in Hi exclamation point in all lowercase crossing my fingers this time
630:02 lowercase crossing my fingers this time and now if I zoom in it indeed
630:05 and now if I zoom in it indeed capitalized only T and not s in this
630:09 capitalized only T and not s in this case so pictorially let me switch over
630:11 case so pictorially let me switch over here here is as before the variable s
630:14 here here is as before the variable s pointing at high in all lowercase when I
630:17 pointing at high in all lowercase when I call malok though that gives me a chunk
630:20 call malok though that gives me a chunk of memory that I'm going to store the
630:21 of memory that I'm going to store the address in t of so if T is some other
630:24 address in t of so if T is some other variable as it is in my code and there's
630:27 variable as it is in my code and there's some other available chunk of memory I
630:28 some other available chunk of memory I don't know where it is but let's assume
630:29 don't know where it is but let's assume as always it's at Ox 456 4557 4558 N9 so
630:33 as always it's at Ox 456 4557 4558 N9 so four bytes total what is now happening
630:36 four bytes total what is now happening well T is defined as pointing to that
630:38 well T is defined as pointing to that because that's what Malo gives us the
630:40 because that's what Malo gives us the address of the first bite of the free
630:42 address of the first bite of the free memory and now with my for Loop I'm just
630:45 memory and now with my for Loop I'm just iterating over it copying the H then the
630:47 iterating over it copying the H then the I then the exclamation point and then
630:49 I then the exclamation point and then for good measure the back slash Zer
630:52 for good measure the back slash Zer instead questions then on this process
630:57 instead questions then on this process here a really good question um if I
631:00 here a really good question um if I omitted in my code the
631:03 omitted in my code the uh plus one and I didn't do less than or
631:06 uh plus one and I didn't do less than or equal to so that I'm copying the fourth
631:08 equal to so that I'm copying the fourth bite odds are in this program because
631:10 bite odds are in this program because it's so short you wouldn't notice that
631:11 it's so short you wouldn't notice that there's an actual error but what could
631:14 there's an actual error but what could happen is When I Call printf On T if
631:18 happen is When I Call printf On T if there's no null bite there it might
631:20 there's no null bite there it might print h i exclamation point some random
631:24 print h i exclamation point some random value some random value some random
631:25 value some random value some random value some random value until it gets
631:27 value some random value until it gets lucky and there happens to be a zero
631:29 lucky and there happens to be a zero bite a null bite by chance for instance
631:32 bite a null bite by chance for instance so if you don't include the back sl0
631:34 so if you don't include the back sl0 some way that's going to happen and I
631:37 some way that's going to happen and I say some way I could even do this I
631:39 say some way I could even do this I could technically just copy the length
631:41 could technically just copy the length of the string s and at the very bottom
631:43 of the string s and at the very bottom here I could do something like T bracket
631:46 here I could do something like T bracket I uh sorry T bracket um
631:49 I uh sorry T bracket um Sterling of T I could do this but this
631:53 Sterling of T I could do this but this is just not necessary like I could
631:55 is just not necessary like I could manually add it at the end of the string
631:57 manually add it at the end of the string but again i' claim that it's just
631:58 but again i' claim that it's just simpler to borrow that is copy the one
632:01 simpler to borrow that is copy the one that's already in s because it's the
632:02 that's already in s because it's the same thing at the end of the day good
632:04 same thing at the end of the day good question other questions on this copying
632:06 question other questions on this copying correctly
632:08 correctly [Music]
632:10 [Music] now all right is there any room for
632:12 now all right is there any room for improvement here well let me propose a
632:15 improvement here well let me propose a slight optimization this is kind of a
632:16 slight optimization this is kind of a throwback now to week one turns out that
632:19 throwback now to week one turns out that arguably my line 13 here wherein I have
632:22 arguably my line 13 here wherein I have this for Loop now that I'm doing things
632:25 this for Loop now that I'm doing things in Loops again and again and using a
632:27 in Loops again and again and using a function like Sterling this is correct
632:29 function like Sterling this is correct it will iterate from zero on up to the
632:32 it will iterate from zero on up to the length of I a length of uh s plus one
632:35 length of I a length of uh s plus one but it's kind of stupid of me to write
632:39 but it's kind of stupid of me to write this for Loop in this way why well
632:41 this for Loop in this way why well here's my initialization on the left
632:43 here's my initialization on the left here's my condition in the middle and in
632:45 here's my condition in the middle and in general calling a function inside of
632:48 general calling a function inside of your condition is probably not very good
632:51 your condition is probably not very good design like why why is it bad for me to
632:54 design like why why is it bad for me to be calling a function like Sterling in
632:56 be calling a function like Sterling in this condition in the middle of my for
632:58 this condition in the middle of my for Loop
633:04 yeah yeah you're just calling it again and again for no reason like the length
633:05 and again for no reason like the length of s never changes so like why are you
633:07 of s never changes so like why are you wasting everyone's Time by calling
633:08 wasting everyone's Time by calling Sterling of s again again again again
633:11 Sterling of s again again again again just to check this inequality whether I
633:14 just to check this inequality whether I is less than that value so it turns out
633:15 is less than that value so it turns out if you haven't discovered this already
633:17 if you haven't discovered this already there's a slight optimization we can do
633:18 there's a slight optimization we can do here that has nothing to do
633:19 here that has nothing to do fundamentally with strings or pointers
633:22 fundamentally with strings or pointers just with better design I can actually
633:24 just with better design I can actually Define two variables at once I could do
633:26 Define two variables at once I could do this let me remove this whole condition
633:29 this let me remove this whole condition and let me add a comma after I equals 0
633:32 and let me add a comma after I equals 0 set n or any variable equal to the stirl
633:36 set n or any variable equal to the stirl of s + 1 and then after the semicolon
633:41 of s + 1 and then after the semicolon just ask the question while I is less
633:43 just ask the question while I is less than n so it's almost the same but
633:45 than n so it's almost the same but notice now my condition in the very
633:47 notice now my condition in the very middle of this Loop is at least
633:49 middle of this Loop is at least comparing two static values n never
633:52 comparing two static values n never change sorry one static value n never
633:54 change sorry one static value n never changes all that changes is I but I'm
633:56 changes all that changes is I but I'm not foolishly calling sterling sterling
633:58 not foolishly calling sterling sterling sterling again and again why well how
634:00 sterling again and again why well how does Sterling work similar in spirit to
634:02 does Sterling work similar in spirit to PR print F Sterling given the name of a
634:05 PR print F Sterling given the name of a string looks at the first character and
634:07 string looks at the first character and then starts looking through the entire
634:09 then starts looking through the entire string looking for the null character
634:11 string looking for the null character and we saw this in week two counting up
634:13 and we saw this in week two counting up how many characters are there so it's
634:15 how many characters are there so it's just a waste of time again and
634:17 just a waste of time again and [Music]
634:25 again totally if you wanted to use n multiple times you could absolutely take
634:27 multiple times you could absolutely take it out of the for Loop put it right
634:28 it out of the for Loop put it right after s is defined and reuse n and again
634:31 after s is defined and reuse n and again and again absolutely but in General
634:33 and again absolutely but in General consider this when designing your for
634:34 consider this when designing your for Loops even though modern compilers like
634:37 Loops even though modern compilers like cang can actually fix this problem this
634:38 cang can actually fix this problem this inefficiency for you good practice would
634:41 inefficiency for you good practice would be don't call functions unnecessarily
634:43 be don't call functions unnecessarily especially if the answer is always going
634:45 especially if the answer is always going to be the same all right so what else
634:48 to be the same all right so what else should I perhaps refine here well how
634:51 should I perhaps refine here well how about I do one last thing and just
634:53 about I do one last thing and just comment on what exactly could go wrong
634:56 comment on what exactly could go wrong here well a couple of things well
634:58 here well a couple of things well actually this is just silly too like
635:00 actually this is just silly too like surely someone before me in the world
635:02 surely someone before me in the world has had to copy a string before surely
635:04 has had to copy a string before surely there's a function like called stir copy
635:07 there's a function like called stir copy maybe like stir compare like stir Ling
635:08 maybe like stir compare like stir Ling and indeed there is so let me propose
635:10 and indeed there is so let me propose that we actually get rid of this whole
635:12 that we actually get rid of this whole for Loop and we actually just call a
635:14 for Loop and we actually just call a function called stir copy no o just St
635:17 function called stir copy no o just St cpy and pass in the destination which is
635:19 cpy and pass in the destination which is T first and then the source that you
635:22 T first and then the source that you want to copy into the destination and
635:24 want to copy into the destination and that takes the place entirely of that
635:27 that takes the place entirely of that whole Loop so again I demonstrated the
635:29 whole Loop so again I demonstrated the loop first just to be very pedantic
635:30 loop first just to be very pedantic about it but that's wasting time you're
635:32 about it but that's wasting time you're wasting time writing lines of code you
635:33 wasting time writing lines of code you don't need to stir copy is what you can
635:36 don't need to stir copy is what you can use here instead and so this has now
635:38 use here instead and so this has now always existed and what more can I do
635:41 always existed and what more can I do well as one final point it turns out
635:43 well as one final point it turns out that there's actually things that can go
635:45 that there's actually things that can go wrong in this code even besides the
635:47 wrong in this code even besides the string being too short like if the human
635:49 string being too short like if the human Just Hits enter and there are no
635:50 Just Hits enter and there are no characters I don't want to blindly
635:51 characters I don't want to blindly capitalize the first character that
635:53 capitalize the first character that doesn't exist that's why I added that if
635:55 doesn't exist that's why I added that if condition but there's other things that
635:57 condition but there's other things that can go wrong and we introduced those to
635:58 can go wrong and we introduced those to you today it turns out that functions
636:01 you today it turns out that functions like get string and functions like Malo
636:05 like get string and functions like Malo return potentially a special value and
636:08 return potentially a special value and wonderfully confusingly it's also called
636:10 wonderfully confusingly it's also called null but with two L's all right so left
636:13 null but with two L's all right so left hand and right hand weren't talking so
636:14 hand and right hand weren't talking so well like decades ago NL is a back SL
636:18 well like decades ago NL is a back SL zero it's a single character as it
636:20 zero it's a single character as it always has been for a couple of weeks
636:22 always has been for a couple of weeks now
636:23 now n is technically a pointer it's an
636:26 n is technically a pointer it's an address but it's address zero it's like
636:28 address but it's address zero it's like the top left hand corner if you will of
636:31 the top left hand corner if you will of your computer's memory that just nothing
636:33 your computer's memory that just nothing is ever supposed to go in by convention
636:35 is ever supposed to go in by convention so null is a synonym for zero but it's
636:39 so null is a synonym for zero but it's specifically an address now why is this
636:40 specifically an address now why is this useful well suppose that in my code here
636:43 useful well suppose that in my code here something goes wrong with get string
636:44 something goes wrong with get string suppose you're being a little crazy and
636:45 suppose you're being a little crazy and you type in way too long of a string
636:47 you type in way too long of a string it's not just high but it's like an
636:49 it's not just high but it's like an entire essay of text and there's not
636:50 entire essay of text and there's not enough memory in the computer how does
636:52 enough memory in the computer how does get string signal to the programmer whoa
636:55 get string signal to the programmer whoa like that's way too big of a string I
636:56 like that's way too big of a string I can't fit it in memory well we never
636:58 can't fit it in memory well we never told you this but all of this time it
637:01 told you this but all of this time it turns out that that get string will
637:04 turns out that that get string will return this special value called null if
637:06 return this special value called null if something goes wrong so to be really
637:09 something goes wrong so to be really careful now you should do something like
637:10 careful now you should do something like this if s equals equals literally null
637:14 this if s equals equals literally null then you better um exit the program
637:16 then you better um exit the program entirely and return like one or two or
637:18 entirely and return like one or two or three to signify that something went
637:20 three to signify that something went wrong don't uh go any further similarly
637:23 wrong don't uh go any further similarly with malok it's possible if you ask for
637:25 with malok it's possible if you ask for way too much memory that could fail
637:26 way too much memory that could fail especially if you're asking now for
637:27 especially if you're asking now for double the memory after the human typed
637:29 double the memory after the human typed something in so if T equals equals null
637:32 something in so if T equals equals null then you know what but let's also return
637:33 then you know what but let's also return one or some other value to just get out
637:35 one or some other value to just get out before something crashes or freezes on
637:37 before something crashes or freezes on the human as well so honestly I tend not
637:40 the human as well so honestly I tend not to do this always in class because the
637:41 to do this always in class because the code just gets so bloated and
637:43 code just gets so bloated and complicated but you absolutely in
637:44 complicated but you absolutely in practice need to start doing this
637:46 practice need to start doing this otherwise you will be responsible for
637:48 otherwise you will be responsible for the freezes and the crashes and the
637:50 the freezes and the crashes and the reboots that users in the real world
637:52 reboots that users in the real world might actually encounter otherwise of
637:54 might actually encounter otherwise of course if we get to the bottom of this
637:55 course if we get to the bottom of this program now I should probably return
637:58 program now I should probably return zero explicitly or implicitly to just
638:00 zero explicitly or implicitly to just signify that everything is is
638:03 signify that everything is is successful but there's one other thing I
638:05 successful but there's one other thing I haven't done we introduced malok but
638:07 haven't done we introduced malok but what did I claim also
638:09 what did I claim also existed so free I'm also being a little
638:12 existed so free I'm also being a little reckless now here I am not practicing
638:14 reckless now here I am not practicing what I'm preaching I'm asking the
638:15 what I'm preaching I'm asking the computer for memory via get string I'm
638:17 computer for memory via get string I'm asking the computer for more memory my
638:19 asking the computer for more memory my via malok and I'm never technically
638:21 via malok and I'm never technically handing it back so really what I should
638:23 handing it back so really what I should be doing at the very bottom of my
638:25 be doing at the very bottom of my program to is freeing the memory I've
638:29 program to is freeing the memory I've asked for so henceforth it is a rule a
638:32 asked for so henceforth it is a rule a law if you will in see whenever you
638:34 law if you will in see whenever you allocate memory with malok or certain
638:36 allocate memory with malok or certain other functions as well you the
638:38 other functions as well you the programmer must free it when you're all
638:40 programmer must free it when you're all done with it now this is a bit of an
638:42 done with it now this is a bit of an overstatement because technically when
638:44 overstatement because technically when programs quit they'll free the memory
638:46 programs quit they'll free the memory automatically so you're not going to
638:47 automatically so you're not going to break someone's Mac or PC because you
638:48 break someone's Mac or PC because you necessarily have this bug but for
638:50 necessarily have this bug but for programs that are running all the time
638:51 programs that are running all the time like someone keeps a chrome their
638:53 like someone keeps a chrome their browser open Microsoft Word or the like
638:55 browser open Microsoft Word or the like bad things will happen if over time you
638:57 bad things will happen if over time you never never never call free and the
638:59 never never never call free and the program keeps running so always get into
639:01 program keeps running so always get into this habit here you you do not need to
639:03 this habit here you you do not need to free memory that comes from get string
639:05 free memory that comes from get string because the cs50 library automatically
639:07 because the cs50 library automatically frees it for you but you anytime you use
639:09 frees it for you but you anytime you use malok henceforth as you did or I did
639:12 malok henceforth as you did or I did here you must free that by just passing
639:15 here you must free that by just passing in the same address you got back
639:18 in the same address you got back questions now on malok and
639:22 questions now on malok and free questions
639:34 yeah oh really good question so free just so what does free do so free um
639:37 just so what does free do so free um just uh lets the computer know that you
639:40 just uh lets the computer know that you are done with that chunk of memory which
639:41 are done with that chunk of memory which means that if you have a another line of
639:43 means that if you have a another line of code elsewhere that same memory might be
639:45 code elsewhere that same memory might be reused and can be used again and again
639:47 reused and can be used again and again and that's going to be necessary
639:48 and that's going to be necessary certainly for any long running program
639:50 certainly for any long running program you can't ask for memory constantly
639:51 you can't ask for memory constantly you'll eventually run out so you need to
639:53 you'll eventually run out so you need to free it in this way other languages as
639:55 free it in this way other languages as an aside python you get another
639:56 an aside python you get another motivation in a couple of weeks for it
639:57 motivation in a couple of weeks for it is going to be Python and certain other
639:59 is going to be Python and certain other languages manage all this headache for
640:01 languages manage all this headache for you but in C the goal here is to really
640:03 you but in C the goal here is to really harness these capabilities ourselves all
640:06 harness these capabilities ourselves all right so it turns out like almost
640:08 right so it turns out like almost everyone in the room everyone in the
640:09 everyone in the room everyone in the room myself included you're going to
640:10 room myself included you're going to screw up when it comes to anything
640:12 screw up when it comes to anything memory related if you haven't already
640:13 memory related if you haven't already segals are in your future but hopefully
640:16 segals are in your future but hopefully there's tools via which you can detect
640:17 there's tools via which you can detect these things and fix them proactively
640:19 these things and fix them proactively and not just use print def uh or debug
640:22 and not just use print def uh or debug 50 or rubber duck we actually have
640:24 50 or rubber duck we actually have another tool we can equip you with now
640:26 another tool we can equip you with now that help you find some mistakes so let
640:28 that help you find some mistakes so let me do this let me close copy.c let me
640:31 me do this let me close copy.c let me open a program I wrote In Advance called
640:32 open a program I wrote In Advance called memory. C that doesn't do anything
640:34 memory. C that doesn't do anything really interesting but it's going to
640:36 really interesting but it's going to have two bugs in it notice that I've
640:38 have two bugs in it notice that I've included standard i.h as always I've
640:41 included standard i.h as always I've also included standard li. which is
640:43 also included standard li. which is necessary now for anything related to
640:45 necessary now for anything related to malok Andor free and the like line six
640:48 malok Andor free and the like line six it's a little weird what I've done here
640:50 it's a little weird what I've done here but this is like the manual way of
640:53 but this is like the manual way of asking for enough memory for an array in
640:56 asking for enough memory for an array in week two how do we ask for memory for an
640:59 week two how do we ask for memory for an array you very simply say int X3 and
641:03 array you very simply say int X3 and that gives you an array called X of size
641:05 that gives you an array called X of size three but if you do it manually Now
641:07 three but if you do it manually Now using Malo what you have to do is use
641:10 using Malo what you have to do is use syntax like this you call malok you ask
641:13 syntax like this you call malok you ask for three things times however big an
641:15 for three things times however big an INT is now we know it's four so you
641:17 INT is now we know it's four so you could literally write 12 here but this
641:19 could literally write 12 here but this is more generic so three times the size
641:21 is more generic so three times the size of an integer will give you 12
641:23 of an integer will give you 12 dynamically and what do Malo return the
641:25 dynamically and what do Malo return the address of the first bite you get back
641:28 address of the first bite you get back what do I want to put that well I want
641:30 what do I want to put that well I want to put it in a variable now the variable
641:32 to put it in a variable now the variable can't just be int X because that's a
641:34 can't just be int X because that's a number it's not an address per se if I
641:36 number it's not an address per se if I want to store this address in a variable
641:38 want to store this address in a variable I could call it X I could call it P but
641:40 I could call it X I could call it P but in Star X just means that X is now the
641:43 in Star X just means that X is now the address of a chunk of memory
641:45 address of a chunk of memory specifically a chunk of memory that's
641:46 specifically a chunk of memory that's big enough not for one but for three ins
641:49 big enough not for one but for three ins in total all right now I'm just sort of
641:52 in total all right now I'm just sort of naively putting our old friends 72 73
641:55 naively putting our old friends 72 73 and 33 at the first second and third
641:57 and 33 at the first second and third locations in memory but perhaps based on
642:00 locations in memory but perhaps based on week two or week four I'm clearly
642:01 week two or week four I'm clearly screwing up up here in a couple of ways
642:03 screwing up up here in a couple of ways someone want to identify at least one
642:05 someone want to identify at least one bug what did I do
642:08 bug what did I do wrong yeah like this is now you know
642:10 wrong yeah like this is now you know amateur uh stuff like I should be zero
642:13 amateur uh stuff like I should be zero indexing not one indexing so this has
642:15 indexing not one indexing so this has got to be 0 one2 ultimately and other
642:17 got to be 0 one2 ultimately and other bugs that are maybe more week four
642:21 bugs that are maybe more week four specific other bugs it's more subtle
642:24 specific other bugs it's more subtle yeah I'm not freeing the memory right so
642:26 yeah I'm not freeing the memory right so I'm not practicing what I'm preaching by
642:28 I'm not practicing what I'm preaching by freeing this memory now suppose these
642:29 freeing this memory now suppose these are non obvious and honestly after like
642:31 are non obvious and honestly after like an hour or two of this like this
642:32 an hour or two of this like this shouldn't be obvious yet it will be over
642:34 shouldn't be obvious yet it will be over time how could I find these bugs with uh
642:38 time how could I find these bugs with uh software as opposed to just staring at
642:40 software as opposed to just staring at the thing or asking someone for help
642:42 the thing or asking someone for help well let me propose this let me first go
642:44 well let me propose this let me first go ahead and run make memory to compile the
642:46 ahead and run make memory to compile the program and it seems to work look fine
642:49 program and it seems to work look fine there's no syntax errors at least
642:51 there's no syntax errors at least dotmemory notice seems to work fine too
642:55 dotmemory notice seems to work fine too now this program doesn't do anything
642:56 now this program doesn't do anything interesting there's no printf or
642:57 interesting there's no printf or anything like that but it didn't crash
642:59 anything like that but it didn't crash there's no segmentation fault but that
643:01 there's no segmentation fault but that doesn't mean there aren't bugs latent in
643:04 doesn't mean there aren't bugs latent in the software and this is true sadly of
643:06 the software and this is true sadly of all of today's software like Chrome and
643:08 all of today's software like Chrome and Microsoft Word and other programs surely
643:11 Microsoft Word and other programs surely have memory related bugs that people at
643:13 have memory related bugs that people at Google and Microsoft haven't yet found
643:15 Google and Microsoft haven't yet found but there are tools at least to find the
643:16 but there are tools at least to find the most obvious of those bugs and we're
643:18 most obvious of those bugs and we're going to introduce you now to a program
643:20 going to introduce you now to a program called valren so valren it's a fairly
643:23 called valren so valren it's a fairly fancy program but we'll use it for very
643:24 fancy program but we'll use it for very simple ways we'll look at your code and
643:27 simple ways we'll look at your code and find memory errors as it's executing and
643:29 find memory errors as it's executing and try to help you understand where they
643:31 try to help you understand where they are so let me go back to vs code here
643:34 are so let me go back to vs code here memory seems to be fine you know I feel
643:36 memory seems to be fine you know I feel like okay I'm going to submit this
643:37 like okay I'm going to submit this homework all is good no error messages
643:39 homework all is good no error messages that's no longer the case now you need
643:40 that's no longer the case now you need to poke a little more at your code to
643:42 to poke a little more at your code to see if maybe there's still some bug
643:44 see if maybe there's still some bug there so let me do this
643:46 there so let me do this valren and then space dotmemory so just
643:49 valren and then space dotmemory so just like debug 50 you run it on a program
643:51 like debug 50 you run it on a program you already compiled valren I'm going to
643:53 you already compiled valren I'm going to run it on a program I already compiled
643:55 run it on a program I already compiled let me uh zoom in on my terminal window
643:57 let me uh zoom in on my terminal window so we can see more at once and enter all
644:00 so we can see more at once and enter all right the output is crazy cryp IC for no
644:02 right the output is crazy cryp IC for no good reason there's lots of numbers and
644:04 good reason there's lots of numbers and equal signs it's a lot of clutter but
644:06 equal signs it's a lot of clutter but there is some juicy information here and
644:08 there is some juicy information here and let me start from the top down invalid
644:10 let me start from the top down invalid write of size four so write means to
644:13 write of size four so write means to change a value read means to like access
644:15 change a value read means to like access a value and this is again esoteric like
644:18 a value and this is again esoteric like a lot of our error messages are but it
644:20 a lot of our error messages are but it looks like uh after a block of size 12
644:24 looks like uh after a block of size 12 allock and then there's these weird hex
644:26 allock and then there's these weird hex notation there's some mention of malok
644:28 notation there's some mention of malok but honestly the juicy part here is
644:30 but honestly the juicy part here is memory. C line six that's probably my
644:32 memory. C line six that's probably my fault so let's look at line six per that
644:34 fault so let's look at line six per that output let me shrink the terminal window
644:37 output let me shrink the terminal window look at line six okay 12 is now Germain
644:39 look at line six okay 12 is now Germain right if you did the mental math of the
644:41 right if you did the mental math of the size of an INT times three 12 is somehow
644:43 size of an INT times three 12 is somehow involved here but line six is now uh
644:48 involved here but line six is now uh happening next year that's where the
644:50 happening next year that's where the memory came from what is this let me
644:52 memory came from what is this let me Zoom back in where is there invalid
644:56 Zoom back in where is there invalid right of size four like what's perhaps
644:59 right of size four like what's perhaps going wrong here invalid right of size
645:03 going wrong here invalid right of size four what does that mean it's like a
645:05 four what does that mean it's like a very technical way of explaining the bug
645:08 very technical way of explaining the bug is actually one line later on line seven
645:10 is actually one line later on line seven as we already identified
645:11 as we already identified yeah indeed and I I misspoke a moment
645:14 yeah indeed and I I misspoke a moment ago the bug actually arises here with
645:15 ago the bug actually arises here with line nine so after the allocation of
645:17 line nine so after the allocation of memory I'm somehow writing four bytes
645:20 memory I'm somehow writing four bytes incorrectly and unfortunately the onus
645:21 incorrectly and unfortunately the onus is kind of on you to sort of think
645:23 is kind of on you to sort of think through deductively like what could that
645:24 through deductively like what could that mean but I'm clearly touching four bytes
645:27 mean but I'm clearly touching four bytes of memory in these few lines of code
645:28 of memory in these few lines of code that I shouldn't be and hopefully here
645:30 that I shouldn't be and hopefully here is the light bulb already went off ear
645:32 is the light bulb already went off ear oh I'm not zero indexing okay that must
645:34 oh I'm not zero indexing okay that must mean that X bracket 3 as you note is
645:36 mean that X bracket 3 as you note is just too far past the chunk of memory so
645:39 just too far past the chunk of memory so I'm invalidly writing to four bytes that
645:41 I'm invalidly writing to four bytes that I shouldn't be so again it's not super
645:43 I shouldn't be so again it's not super obvious this is not super userfriendly
645:45 obvious this is not super userfriendly but at least it does give you a clue as
645:48 but at least it does give you a clue as to where that bug is so the fix there is
645:50 to where that bug is so the fix there is going to be quite simply to change the
645:52 going to be quite simply to change the one to a zero the two to a one and the
645:54 one to a zero the two to a one and the three to a two that'll fix that but
645:57 three to a two that'll fix that but there's still a second error and let me
645:58 there's still a second error and let me look at the cryptic output again Heap
646:00 look at the cryptic output again Heap summary some stuff there okay this does
646:02 summary some stuff there okay this does not sound good down here 12 bytes in one
646:05 not sound good down here 12 bytes in one blocks are definitely lost in Lost
646:07 blocks are definitely lost in Lost record one of one very Arcane output two
646:10 record one of one very Arcane output two but clearly related to line six again
646:12 but clearly related to line six again our allocation of memory now here too
646:14 our allocation of memory now here too it's not obvious what the solution is
646:16 it's not obvious what the solution is but memory is lost AKA this is a memory
646:19 but memory is lost AKA this is a memory leak and now the deductions kind of up
646:21 leak and now the deductions kind of up to you why what is leak oh wait I didn't
646:24 to you why what is leak oh wait I didn't call free and so the second solution
646:26 call free and so the second solution here is probably to free x at the very
646:29 here is probably to free x at the very end of the program and if you really
646:31 end of the program and if you really want to be you should probably check
646:32 want to be you should probably check like I proposed earlier if x is null
646:35 like I proposed earlier if x is null just get out now while you still can and
646:37 just get out now while you still can and don't even touch those other lines of
646:39 don't even touch those other lines of code but if you get to the bottom return
646:40 code but if you get to the bottom return zero but really the takeaways are I
646:43 zero but really the takeaways are I fixed my zero indexing of the array to
646:45 fixed my zero indexing of the array to avoid the invalid right of size four and
646:48 avoid the invalid right of size four and now I'm freeing the memory that I asked
646:50 now I'm freeing the memory that I asked for so there should be no leak lost all
646:52 for so there should be no leak lost all right let's try this again make
646:54 right let's try this again make memorymemory no visible errors yet but
646:57 memorymemory no visible errors yet but let me now increase my terminal window
646:59 let me now increase my terminal window again do valren of dotmemory crossing my
647:02 again do valren of dotmemory crossing my fingers and now all Heap blocks were
647:05 fingers and now all Heap blocks were freed no leaks are possible I don't see
647:07 freed no leaks are possible I don't see any invalid rights there's still a crazy
647:09 any invalid rights there's still a crazy amount of output but none of it is
647:10 amount of output but none of it is erroneous it's not bad now I fixed my
647:13 erroneous it's not bad now I fixed my memory bugs and so now my my ta my TF
647:15 memory bugs and so now my my ta my TF they're not going to find them either
647:16 they're not going to find them either because at least valr has proactively
647:18 because at least valr has proactively done that for me questions then on
647:21 done that for me questions then on valgren generally it's those two types
647:24 valgren generally it's those two types of Errors you might trip over there's
647:25 of Errors you might trip over there's not twoo much else in the way of Arcane
647:27 not twoo much else in the way of Arcane output questions then on
647:30 output questions then on this no all right well what else might
647:33 this no all right well what else might be going on so someone alluded to this
647:35 be going on so someone alluded to this earlier what happens when you for
647:38 earlier what happens when you for instance forget the null Terminator or
647:40 instance forget the null Terminator or you generally start poking around memory
647:42 you generally start poking around memory that you yourself didn't ask for or uh
647:45 that you yourself didn't ask for or uh looking at values you didn't put there
647:47 looking at values you didn't put there well let me go ahead and open this code
647:49 well let me go ahead and open this code of garbage. C in honor of Oscar the
647:51 of garbage. C in honor of Oscar the Grouch here of sorts and here is a
647:54 Grouch here of sorts and here is a simple program if I hide my terminal
647:56 simple program if I hide my terminal window that just does something kind of
647:57 window that just does something kind of arbitrary I first declare an array uh
648:00 arbitrary I first declare an array uh called scores uh but I made it crazy big
648:03 called scores uh but I made it crazy big like 1024 like that's a lot of uh
648:05 like 1024 like that's a lot of uh integers but so be it and then I iterate
648:08 integers but so be it and then I iterate over those integers and I print each of
648:10 over those integers and I print each of those scores out so I'm using week two
648:12 those scores out so I'm using week two syntax here but based on this program
648:14 syntax here but based on this program what have I clearly not done that I did
648:16 what have I clearly not done that I did do back in week two I've allocated the
648:19 do back in week two I've allocated the array I'm printing the array but but
648:22 array I'm printing the array but but Bute yeah I didn't initialize any values
648:25 Bute yeah I didn't initialize any values for that array back in week two we
648:27 for that array back in week two we didn't do 1024 we did like three and I
648:28 didn't do 1024 we did like three and I typed in like three test scores or
648:30 typed in like three test scores or something like that he here I'm
648:31 something like that he here I'm allocating memory even more than that
648:33 allocating memory even more than that just because I really want to be
648:34 just because I really want to be dramatic with what I'm demonstrating but
648:36 dramatic with what I'm demonstrating but I'm not initializing those values to
648:38 I'm not initializing those values to anything and so here it turns out in C
648:41 anything and so here it turns out in C generally if you do not initialize a
648:43 generally if you do not initialize a variable or you do not initialize an
648:44 variable or you do not initialize an array with explicit values there are
648:46 array with explicit values there are going to be garbage values there so to
648:48 going to be garbage values there so to speak remnants of that memory having
648:51 speak remnants of that memory having been used before probably by some other
648:53 been used before probably by some other function of yours some Library function
648:55 function of yours some Library function or something else while your program is
648:57 or something else while your program is running not a huge deal with a super
648:59 running not a huge deal with a super small program like this but for anything
649:01 small program like this but for anything sizable memory is going to be used and
649:03 sizable memory is going to be used and unused and used and unused that is
649:04 unused and used and unused that is macked and freed again and again there's
649:06 macked and freed again and again there's going to be lots of garbage values in
649:08 going to be lots of garbage values in the computer's memory by default so if I
649:11 the computer's memory by default so if I open my terminal window here uh let me
649:13 open my terminal window here uh let me do make garbage uh let me zoom in on my
649:16 do make garbage uh let me zoom in on my terminal so we can see the output when I
649:18 terminal so we can see the output when I runt garbage theoretically I should see
649:21 runt garbage theoretically I should see 1,24 integers but none of which have
649:24 1,24 integers but none of which have been initialized now I'm going to get
649:26 been initialized now I'm going to get lucky with some of them and it looks
649:27 lucky with some of them and it looks like wow okay a lot of them are
649:29 like wow okay a lot of them are initialized to zero and C does in some
649:31 initialized to zero and C does in some context initialize memory for you to
649:33 context initialize memory for you to zero at least at the beginning but not
649:35 zero at least at the beginning but not again and again typically but if I start
649:37 again and again typically but if I start scrolling backwards in time at this
649:39 scrolling backwards in time at this array of size
649:41 array of size 1024 where did these values come from so
649:45 1024 where did these values come from so just random positive and negative
649:47 just random positive and negative numbers interspersed among the zeros
649:48 numbers interspersed among the zeros well that's because I'm literally poking
649:50 well that's because I'm literally poking around a random 1,24 bytes of the
649:53 around a random 1,24 bytes of the computer's memory who knows what's there
649:55 computer's memory who knows what's there so the lesson here is that garbage
649:57 so the lesson here is that garbage values are indeed this like term of art
649:58 values are indeed this like term of art it means that a variable that you might
650:01 it means that a variable that you might have uh defined that you might have
650:03 have uh defined that you might have declared if you don't give it an
650:04 declared if you don't give it an explicit value who knows what's going to
650:07 explicit value who knows what's going to be there and the lesson here is just
650:08 be there and the lesson here is just don't do that always initialize
650:10 don't do that always initialize variables to something either yourself
650:13 variables to something either yourself or prompting the human for
650:15 or prompting the human for it questions about garbage
650:18 it questions about garbage values you'll see them sometimes if you
650:20 values you'll see them sometimes if you print things you shouldn't or touch
650:22 print things you shouldn't or touch arrays beyond their
650:24 arrays beyond their boundaries all right so maybe to make
650:26 boundaries all right so maybe to make this a little visual too it turns out
650:28 this a little visual too it turns out that a lot of things can go wrong
650:29 that a lot of things can go wrong unfortunately with poers and we've seen
650:31 unfortunately with poers and we've seen some of them and here's another program
650:32 some of them and here's another program that's a little contrived it's very
650:34 that's a little contrived it's very simple and it just is about manipulating
650:37 simple and it just is about manipulating values it doesn't do anything useful per
650:38 values it doesn't do anything useful per se except demonstrate some of today's
650:40 se except demonstrate some of today's Concepts so in main here let me propose
650:42 Concepts so in main here let me propose that we declare a pointer called X
650:44 that we declare a pointer called X that's going to store eventually the
650:45 that's going to store eventually the address of an integer apparently here's
650:47 address of an integer apparently here's another one called y that's going to
650:49 another one called y that's going to store the address of an integer as well
650:51 store the address of an integer as well here now I'm allocating enough memory to
650:53 here now I'm allocating enough memory to fit one integer now technically it's
650:55 fit one integer now technically it's four we know that but size of in just
650:57 four we know that but size of in just gives me that answer dynamically so it
651:00 gives me that answer dynamically so it will work on all systems
651:01 will work on all systems and I'm going to store the address that
651:03 and I'm going to store the address that Malo finds for me in X then I go to x
651:08 Malo finds for me in X then I go to x and put the number 42 there all right
651:11 and put the number 42 there all right why it's the sort of meaning of life the
651:13 why it's the sort of meaning of life the universe and everything here but star X
651:15 universe and everything here but star X again just means go to that address and
651:18 again just means go to that address and put a value there so why I don't know
651:20 put a value there so why I don't know but it's just uh correct at this point
651:22 but it's just uh correct at this point but what about this line here star y
651:25 but what about this line here star y equals 13 unlucky in this case what's
651:28 equals 13 unlucky in this case what's bad about this line here star y It's a
651:31 bad about this line here star y It's a combination now of today's Primitives
651:33 combination now of today's Primitives and that point here
651:35 and that point here yeah yeah we didn't ask the computer to
651:37 yeah yeah we didn't ask the computer to allocate any space so why was not
651:39 allocate any space so why was not initialized with an equal sign at any
651:41 initialized with an equal sign at any point to anything and so what is inside
651:43 point to anything and so what is inside y so to speak like a garbage value maybe
651:46 y so to speak like a garbage value maybe it's zero which isn't bad because at
651:47 it's zero which isn't bad because at least it's nice and simple but maybe
651:49 least it's nice and simple but maybe it's some crazy large uh positive number
651:51 it's some crazy large uh positive number some crazy large negative number either
651:52 some crazy large negative number either way odds are if I go to this address or
651:54 way odds are if I go to this address or that address randomly with star y bad
651:57 that address randomly with star y bad things are going to happen and so let me
651:59 things are going to happen and so let me go ahead and propose well let's not do
652:00 go ahead and propose well let's not do that let's let's actually do this
652:02 that let's let's actually do this instead assign y equal to X and we've
652:04 instead assign y equal to X and we've done that before and then I can go to Y
652:07 done that before and then I can go to Y now and change what was a 42 to a 13
652:10 now and change what was a 42 to a 13 again why this is just for educational
652:12 again why this is just for educational sake but for now this does not crash
652:15 sake but for now this does not crash because I only dreference y with star y
652:18 because I only dreference y with star y after actually giving it a value albe it
652:19 after actually giving it a value albe it a duplicate value similar to our copy
652:22 a duplicate value similar to our copy example earlier so our friends at
652:24 example earlier so our friends at Stanford have put together a wonderful
652:25 Stanford have put together a wonderful visual it's about 2 minutes long allow
652:27 visual it's about 2 minutes long allow me to dramatically dim the lights if we
652:29 me to dramatically dim the lights if we could and play with what happens with
652:32 could and play with what happens with memory when you do bad things like
652:35 memory when you do bad things like [Music]
652:37 [Music] this hey Binky wake up it's time for
652:40 this hey Binky wake up it's time for pointer fun what's that learn about
652:44 pointer fun what's that learn about pointers Oh goodie well to get started I
652:47 pointers Oh goodie well to get started I guess we're going to need a couple
652:48 guess we're going to need a couple pointers okay this code allocates two
652:51 pointers okay this code allocates two pointers which can point to integers
652:54 pointers which can point to integers okay well I see the two pointers but
652:56 okay well I see the two pointers but they don't seem to be pointing to
652:57 they don't seem to be pointing to anything that's right initially pointers
653:00 anything that's right initially pointers don't point to anything the things they
653:01 don't point to anything the things they point to are called Pointes and setting
653:03 point to are called Pointes and setting them up is a separate step oh right
653:06 them up is a separate step oh right right I knew that the Pointes are
653:07 right I knew that the Pointes are separate or so how do you allocate a
653:10 separate or so how do you allocate a point e okay well this code allocates a
653:13 point e okay well this code allocates a new integer point and this part sets X
653:16 new integer point and this part sets X to point to it hey that looks better so
653:19 to point to it hey that looks better so make it do something okay I'll
653:22 make it do something okay I'll dereference the pointer X to store the
653:24 dereference the pointer X to store the number 42 into its Point e for this
653:27 number 42 into its Point e for this trick I'll need my magic wand of
653:29 trick I'll need my magic wand of dereferencing your magic wand of D
653:32 dereferencing your magic wand of D referencing uh that that's great this is
653:35 referencing uh that that's great this is what the code looks like I'll just set
653:37 what the code looks like I'll just set up the number
653:39 up the number and hey look there it goes so doing a d
653:43 and hey look there it goes so doing a d reference on X follows the arrow to
653:45 reference on X follows the arrow to access its Point T in this case to store
653:47 access its Point T in this case to store 42 in there hey try using it to store
653:50 42 in there hey try using it to store the number 13 through the other pointer
653:53 the number 13 through the other pointer why okay I'll just go over here to Y and
653:57 why okay I'll just go over here to Y and get the number 13 set up and then take
654:00 get the number 13 set up and then take the wand of d referencing and
654:04 the wand of d referencing and just oh hey that didn't work say uh
654:08 just oh hey that didn't work say uh Binky I don't think de referencing Y is
654:10 Binky I don't think de referencing Y is a good idea cuz uh you know setting up
654:12 a good idea cuz uh you know setting up the point T is a separate step and uh I
654:14 the point T is a separate step and uh I don't think we ever did it good point
654:17 don't think we ever did it good point yeah we we allocated the pointer y but
654:20 yeah we we allocated the pointer y but we never set it to point to a point
654:22 we never set it to point to a point T very observant hey you're looking good
654:25 T very observant hey you're looking good there Binky can you fix it so that y
654:27 there Binky can you fix it so that y points to the same point as X sure I'll
654:30 points to the same point as X sure I'll use my magic wand of pointer assignment
654:32 use my magic wand of pointer assignment is that going to be a problem like
654:34 is that going to be a problem like before no this doesn't touch the Pointes
654:36 before no this doesn't touch the Pointes it just changes one pointer to point to
654:38 it just changes one pointer to point to the same thing is another oh I see now y
654:42 the same thing is another oh I see now y points to the same place as X so so wait
654:45 points to the same place as X so so wait now Y is fixed it has a point e so you
654:47 now Y is fixed it has a point e so you can try the wand of D referencing again
654:49 can try the wand of D referencing again to send the 13 over uh okay here it
654:53 to send the 13 over uh okay here it goes hey look at that now D referencing
654:56 goes hey look at that now D referencing works on why and because the pointers
654:58 works on why and because the pointers are sharing that one point e they both
654:59 are sharing that one point e they both see the 13 yeah sharing uh whatever so
655:03 see the 13 yeah sharing uh whatever so are we going to switch places now oh
655:05 are we going to switch places now oh look we're out of time but L thanks to
655:07 look we're out of time but L thanks to Professor Nick parlante of Stanford for
655:09 Professor Nick parlante of Stanford for spending a huge amount of time doing
655:11 spending a huge amount of time doing stop motion animation for that but
655:13 stop motion animation for that but hopefully now you have a sense of what
655:14 hopefully now you have a sense of what two can go wrong when you misuse memory
655:17 two can go wrong when you misuse memory in this way but at the end of the day we
655:19 in this way but at the end of the day we really only have these four new building
655:21 really only have these four new building blocks today like the star operator the
655:22 blocks today like the star operator the Ampersand operator malok and free and
655:25 Ampersand operator malok and free and really with that and the underlying
655:26 really with that and the underlying understanding of what your computer is
655:28 understanding of what your computer is doing underneath the hood we have this
655:30 doing underneath the hood we have this way now to really manipulate things in
655:32 way now to really manipulate things in memory For Better or For Worse and
655:33 memory For Better or For Worse and eventually we'll see how we can build
655:35 eventually we'll see how we can build things but we can also now use today's
655:37 things but we can also now use today's Primitives to better explain some things
655:39 Primitives to better explain some things that we've been asking you to take for
655:41 that we've been asking you to take for granted over the past several weeks so
655:43 granted over the past several weeks so for instance let me propose that we uh
655:46 for instance let me propose that we uh do one volunteer up here if we could
655:49 do one volunteer up here if we could could we get one volunteer who's you
655:51 could we get one volunteer who's you want to come straight up yep right in
655:53 want to come straight up yep right in the middle come on you'll have to take a
655:54 the middle come on you'll have to take a left right
656:01 there all right so we have two empty glasses
656:04 all right so we have two empty glasses here and two colors of liquid and we
656:06 here and two colors of liquid and we have let me give you the mic if you'd
656:08 have let me give you the mic if you'd like to say hello to the
656:10 like to say hello to the group hello um I'm Moen uh I'm in the
656:13 group hello um I'm Moen uh I'm in the and first year all right welcome well
656:15 and first year all right welcome well well welcome here I'm going to go ahead
656:17 well welcome here I'm going to go ahead and fill these two glasses with this
656:18 and fill these two glasses with this colored liquid um purple here on my
656:21 colored liquid um purple here on my right let's fill up a glass
656:29 here yeah don't drink uh and now we'll put some orange in here
656:32 put some orange in here and what we'd like you to do for the
656:33 and what we'd like you to do for the audience if you don't mind is swap the
656:36 audience if you don't mind is swap the two values we've got a purple value and
656:38 two values we've got a purple value and orange value and I'd like the purple
656:40 orange value and I'd like the purple liquid in this glass and the orange
656:42 liquid in this glass and the orange liquid in that glass
656:48 please can I have another glass oh okay good intuition but for the microphone uh
656:51 good intuition but for the microphone uh can I have another glass so you can and
656:53 can I have another glass so you can and just in fact I brought one here for you
656:55 just in fact I brought one here for you why are you asking for this though cuz
656:57 why are you asking for this though cuz if I just pour this into this and it'll
656:58 if I just pour this into this and it'll get mixed up right so obviously we need
657:00 get mixed up right so obviously we need like a temporary variable if you will so
657:03 like a temporary variable if you will so here is your temporary
657:05 here is your temporary variable and you want yeah there's
657:09 variable and you want yeah there's yeah all right so pouring the value of
657:12 yeah all right so pouring the value of the orange glass into this temporary
657:14 the orange glass into this temporary variable if you
657:16 variable if you will all right and now pouring the value
657:20 will all right and now pouring the value of the purple glass into the former
657:22 of the purple glass into the former orange
657:28 glass and now the temporary value goes back into
657:31 now the temporary value goes back into the original purple glass and now I
657:34 the original purple glass and now I think we give you a round of applause
657:35 think we give you a round of applause for having done that very well
657:38 for having done that very well okay thank you all right so so it should
657:42 okay thank you all right so so it should go without saying that in the real world
657:44 go without saying that in the real world like that's how you do this and in fact
657:45 like that's how you do this and in fact in code that's pretty much how you have
657:47 in code that's pretty much how you have to do this although ask us some time for
657:50 to do this although ask us some time for a super fancy way of doing it without a
657:52 a super fancy way of doing it without a temporary variable turns out that is
657:53 temporary variable turns out that is possible using bits but for now let's
657:56 possible using bits but for now let's suppose that indeed this demonstrates
657:57 suppose that indeed this demonstrates what is the reality in code if you want
657:59 what is the reality in code if you want to swap two values you need to have have
658:00 to swap two values you need to have have something like a temporary variable so
658:03 something like a temporary variable so for instance on the screen here is a uh
658:05 for instance on the screen here is a uh the beginning of a function called swap
658:07 the beginning of a function called swap whose purpose in life is to as you just
658:09 whose purpose in life is to as you just did swap two values call it a and b so
658:11 did swap two values call it a and b so orange and purple respectively are now
658:13 orange and purple respectively are now just a and b and integers to keep things
658:15 just a and b and integers to keep things simple well here is the corresponding
658:16 simple well here is the corresponding code if I may to what you just enacted
658:18 code if I may to what you just enacted as a human you declared a temporary
658:20 as a human you declared a temporary variable a called temp in this case
658:22 variable a called temp in this case which was like me handing you The Empty
658:24 which was like me handing you The Empty Glass and you stored the orange liquid
658:26 Glass and you stored the orange liquid in it akaa you then changed the value of
658:29 in it akaa you then changed the value of the formerly orange glass to be equal to
658:32 the formerly orange glass to be equal to the Purple by pouring one into the other
658:34 the Purple by pouring one into the other and then you did the opposite there now
658:36 and then you did the opposite there now at the end of this you still have a
658:37 at the end of this you still have a temporary variable that's now empty so
658:39 temporary variable that's now empty so it's temporary in literally that sense
658:41 it's temporary in literally that sense like you just don't need it anymore but
658:42 like you just don't need it anymore but it was necessary along the way so I dare
658:45 it was necessary along the way so I dare say this code is correct logically like
658:47 say this code is correct logically like this will swap two values A and B thanks
658:51 this will swap two values A and B thanks to the use of that temporary variable
658:53 to the use of that temporary variable unfortunately though if I actually do
658:55 unfortunately though if I actually do this in practice let me go over to VSS
658:57 this in practice let me go over to VSS code here and open a program I wrote in
658:58 code here and open a program I wrote in advance called swap. C which does this
659:01 advance called swap. C which does this as follows in here notice I have my
659:04 as follows in here notice I have my prototype for a swap function at the
659:06 prototype for a swap function at the very top and let me scroll down to the
659:07 very top and let me scroll down to the very bottom there is that exact same
659:09 very bottom there is that exact same code so I'm uh the same code for
659:12 code so I'm uh the same code for swapping two values A and B which I'm
659:14 swapping two values A and B which I'm claiming for now is correct now if I go
659:16 claiming for now is correct now if I go back up here what is main going to do
659:18 back up here what is main going to do for us main is really just meant to be a
659:19 for us main is really just meant to be a demonstration of the correctness of your
659:21 demonstration of the correctness of your algorithm so here I declare on line
659:23 algorithm so here I declare on line seven and eight two variables X and Y
659:26 seven and eight two variables X and Y being 1 and two arbitrarily respectively
659:28 being 1 and two arbitrarily respectively I then on line 10 just print out what
659:30 I then on line 10 just print out what the value of x is and Y is just so I can
659:32 the value of x is and Y is just so I can see it on the screen I then call the
659:35 see it on the screen I then call the swap function on line 11 and then I
659:37 swap function on line 11 and then I literally print the exact same thing
659:39 literally print the exact same thing again I print X and Y hopefully it'll
659:41 again I print X and Y hopefully it'll obviously be the opposite so I think
659:43 obviously be the opposite so I think logically swap is indeed correct let me
659:45 logically swap is indeed correct let me do make Swap and then do/ Swap and I
659:48 do make Swap and then do/ Swap and I should see X is one y is 2 and then
659:51 should see X is one y is 2 and then hopefully X is 2 Y is 1 enter but I
659:56 hopefully X is 2 Y is 1 enter but I don't and it did work in the sense that
659:58 don't and it did work in the sense that the code compiled the code ran so it's
660:00 the code compiled the code ran so it's not like some bug in that sense but
660:03 not like some bug in that sense but because I don't quite understand what's
660:05 because I don't quite understand what's going on underneath the hood at least as
660:07 going on underneath the hood at least as of right now or prior weeks this code
660:10 of right now or prior weeks this code here is indeed buggy in some way but
660:13 here is indeed buggy in some way but does anyone have an intuition perhaps
660:15 does anyone have an intuition perhaps based on today's discussion is to like
660:17 based on today's discussion is to like why this code while logically correct
660:18 why this code while logically correct clearly works in reality apparently does
660:21 clearly works in reality apparently does not work in
660:22 not work in C any intuition
660:29 yeah perfect and to summarize here's that term of art I prom
660:31 that term of art I prom when you call a function and pass in two
660:33 when you call a function and pass in two arguments Like A and B you're passing
660:35 arguments Like A and B you're passing those arguments by value so copies of
660:38 those arguments by value so copies of those values effectively and so when
660:40 those values effectively and so when swap is actually called here sorry when
660:43 swap is actually called here sorry when you pass in X and Y we call them A and B
660:45 you pass in X and Y we call them A and B but that's just a convention we could
660:47 but that's just a convention we could call the parameters anything we want
660:49 call the parameters anything we want what A and B are are indeed the values
660:52 what A and B are are indeed the values of X and Y respectively but copies of
660:55 of X and Y respectively but copies of the values so this code here is very
660:57 the values so this code here is very successfully in vs code 2 swapping the
660:59 successfully in vs code 2 swapping the values of a A and B but as you note
661:02 values of a A and B but as you note because I'm passing them in by value
661:04 because I'm passing them in by value literally one literally two and not by
661:06 literally one literally two and not by another term of art by reference AKA by
661:09 another term of art by reference AKA by their addresses swap has no capability
661:12 their addresses swap has no capability in C to go to those locations swap the
661:15 in C to go to those locations swap the actual locations just like we did
661:17 actual locations just like we did successfully in reality but I think we
661:19 successfully in reality but I think we really have the syntax already for
661:21 really have the syntax already for solving this if we consider that really
661:23 solving this if we consider that really this is just an issue of scope and we've
661:24 this is just an issue of scope and we've talked a bit about scope in the past
661:26 talked a bit about scope in the past whereby scope refers to the context in
661:28 whereby scope refers to the context in which a variable lives and generally
661:30 which a variable lives and generally I've claimed that a variable exists
661:32 I've claimed that a variable exists between the most recent curly braces and
661:34 between the most recent curly braces and that's pretty much true for the swap
661:36 that's pretty much true for the swap function because a and b i now claim
661:38 function because a and b i now claim again exist only in the context of these
661:40 again exist only in the context of these curly braces they have no effect on Main
661:43 curly braces they have no effect on Main up top which has different variables X
661:45 up top which has different variables X and Y but we can consider now what's
661:47 and Y but we can consider now what's really going on underneath the hood and
661:48 really going on underneath the hood and here's that same picture of memory as
661:50 here's that same picture of memory as we've seen in the past if we zoom in and
661:52 we've seen in the past if we zoom in and see on these little black chips this is
661:53 see on these little black chips this is a bunch of bites of memory if I create a
661:55 a bunch of bites of memory if I create a grid out of it just to kind of highlight
661:57 grid out of it just to kind of highlight that we can address each of these bites
661:59 that we can address each of these bites throw away the plastic circuit board and
662:01 throw away the plastic circuit board and focus only on those bites what's going
662:04 focus only on those bites what's going on underneath the hood when functions
662:06 on underneath the hood when functions are called in C which you've been doing
662:08 are called in C which you've been doing for weeks now well this rectangle of
662:10 for weeks now well this rectangle of memory if we kind of abstract it away
662:12 memory if we kind of abstract it away further is generally broken up into
662:15 further is generally broken up into different regions or segments like I
662:16 different regions or segments like I called them earlier and different things
662:19 called them earlier and different things get put in different parts of the
662:20 get put in different parts of the computer's memory and without getting
662:22 computer's memory and without getting too into the weeds when you double click
662:24 too into the weeds when you double click a program on your macro PC or when you
662:26 a program on your macro PC or when you do/ something on a Linux you are loading
662:31 do/ something on a Linux you are loading your machine code into the computer's
662:33 your machine code into the computer's memory from the computer's hard drive so
662:35 memory from the computer's hard drive so all the zeros and ones that compose
662:37 all the zeros and ones that compose Microsoft Word or Chrome or whatever are
662:39 Microsoft Word or Chrome or whatever are loaded into the computer's memory or RAM
662:42 loaded into the computer's memory or RAM and by convention it's put up top in the
662:43 and by convention it's put up top in the so-called machine code area and that's
662:45 so-called machine code area and that's how the CPU has access to them quickly
662:47 how the CPU has access to them quickly at that below that are what are be going
662:50 at that below that are what are be going to be our globals so Global variables
662:52 to be our globals so Global variables which we haven't used very much in C but
662:54 which we haven't used very much in C but you can declare them outside of main at
662:56 you can declare them outside of main at the very top of your files if you have
662:58 the very top of your files if you have globals they end up up there as well
663:00 globals they end up up there as well just FYI and then there's this big chunk
663:02 just FYI and then there's this big chunk of memory that we saw valgren mention
663:04 of memory that we saw valgren mention indirectly earlier called the Heap and
663:06 indirectly earlier called the Heap and it's kind of like Heap literally like
663:08 it's kind of like Heap literally like it's a heap of memory that you can use
663:10 it's a heap of memory that you can use as you see fit and the Heap is where
663:13 as you see fit and the Heap is where malok grabs memory from so initially
663:15 malok grabs memory from so initially there's nothing in the Heap it's just a
663:17 there's nothing in the Heap it's just a big chunk of free space anytime you call
663:19 big chunk of free space anytime you call malok Malo kind of carves out from the
663:21 malok Malo kind of carves out from the Heap area more and more bites and Malo
663:24 Heap area more and more bites and Malo keeps track of essentially which bites
663:26 keeps track of essentially which bites have already been allocated so initially
663:28 have already been allocated so initially it looks empty but different bites
663:30 it looks empty but different bites squares if you will keep getting
663:31 squares if you will keep getting requested again and again as a program
663:33 requested again and again as a program runs thanks to functions like malok and
663:35 runs thanks to functions like malok and it grows if you will conceptually down
663:37 it grows if you will conceptually down so the more and more memory you request
663:39 so the more and more memory you request from malok it starts up here but then
663:41 from malok it starts up here but then the next chunk you get is down here
663:42 the next chunk you get is down here conceptually the next chunk is down here
663:44 conceptually the next chunk is down here down here so it kind of fills the
663:46 down here so it kind of fills the available space in the computer's
663:48 available space in the computer's overall memory but there's this other
663:50 overall memory but there's this other chunk of memory called the stack and
663:53 chunk of memory called the stack and just like a stack of trays in like
663:54 just like a stack of trays in like annenburg or a cafeteria kind of grow
663:57 annenburg or a cafeteria kind of grow upward so does the stack of memory and
663:59 upward so does the stack of memory and it turns out the stack is where
664:02 it turns out the stack is where functions have variables and have
664:05 functions have variables and have arguments stored temporarily so whenever
664:07 arguments stored temporarily so whenever you call a function and it has variables
664:10 you call a function and it has variables inside of it or it has arguments there
664:12 inside of it or it has arguments there too this is the chunk of memory and the
664:14 too this is the chunk of memory and the computer's overall block of memory that
664:16 computer's overall block of memory that are used for functions but anytime you
664:18 are used for functions but anytime you call malok it's memory up here right at
664:20 call malok it's memory up here right at the end of the day they just had to pick
664:22 the end of the day they just had to pick a direction top bottom and technically
664:23 a direction top bottom and technically it's an artist rendition you could
664:24 it's an artist rendition you could Circle this thing around any orientation
664:26 Circle this thing around any orientation you want but you're just using a finite
664:28 you want but you're just using a finite amount of memory in this conventional
664:30 amount of memory in this conventional way Malo Starts Here functions start
664:32 way Malo Starts Here functions start here now you can kind of see where like
664:34 here now you can kind of see where like bad things can happen and indeed one of
664:37 bad things can happen and indeed one of the other reasons programs computers can
664:38 the other reasons programs computers can crash is if you ask for way too much
664:40 crash is if you ask for way too much memory from the Heap by calling malok
664:42 memory from the Heap by calling malok many many many times or if you call way
664:45 many many many times or if you call way too many functions or accidentally per
664:47 too many functions or accidentally per last week you recurse infinitely many
664:49 last week you recurse infinitely many times you might have a segmentation
664:51 times you might have a segmentation fault and that's because you're using
664:52 fault and that's because you're using too much stack memory so this is bound
664:54 too much stack memory so this is bound to be a problem eventually and the onus
664:57 to be a problem eventually and the onus is on the programmer to just minimize
664:59 is on the programmer to just minimize the probability of doing that and really
665:01 the probability of doing that and really avoid the possibility of doing that by
665:03 avoid the possibility of doing that by just checking return values like
665:05 just checking return values like checking if malok or get string return
665:07 checking if malok or get string return null because you can proactively with
665:09 null because you can proactively with conditionals make sure that these two
665:11 conditionals make sure that these two things do not Collide by just making
665:13 things do not Collide by just making sure that you get back non-null values
665:16 sure that you get back non-null values so let's consider the stack in the
665:17 so let's consider the stack in the context of Swap and what's really
665:19 context of Swap and what's really happening here and Carter if you
665:20 happening here and Carter if you wouldn't mind helping me animate the
665:21 wouldn't mind helping me animate the screen here when I call the main
665:24 screen here when I call the main function of any program it is allocated
665:28 function of any program it is allocated a slice of memory called a frame at the
665:30 a slice of memory called a frame at the bottom of this stack so if Carter you
665:31 bottom of this stack so if Carter you want to go ahead and uh Advance here
665:33 want to go ahead and uh Advance here here's like the First Slice of memory
665:35 here's like the First Slice of memory that will always be used by main whether
665:37 that will always be used by main whether it has commandline arguments or local
665:39 it has commandline arguments or local variables it just ends up here in memory
665:42 variables it just ends up here in memory suppose now per our swap. C program that
665:45 suppose now per our swap. C program that main calls swap well where does the
665:47 main calls swap well where does the memory for swap end up like right up
665:49 memory for swap end up like right up here so swap had two variable two
665:51 here so swap had two variable two arguments uh A and B and it also had a
665:54 arguments uh A and B and it also had a temporary variable so all of those end
665:55 temporary variable so all of those end up in here in memory and if you want to
665:57 up in here in memory and if you want to go ahead and Advance again Carter once
665:59 go ahead and Advance again Carter once swap is done executing whether it just
666:02 swap is done executing whether it just returns because there's no more lines of
666:03 returns because there's no more lines of code or you explicitly return this
666:06 code or you explicitly return this memory is just freed up automatically
666:08 memory is just freed up automatically you don't call free you don't undo malok
666:10 you don't call free you don't undo malok this just all happens automatically and
666:12 this just all happens automatically and has been since week one now technically
666:14 has been since week one now technically it's still there even though we've
666:16 it's still there even though we've removed it from the picture and there's
666:18 removed it from the picture and there's your first hint of garbage values right
666:20 your first hint of garbage values right like there's still zeros and ones there
666:22 like there's still zeros and ones there and they're left in the original the
666:23 and they're left in the original the previous configuration and so the reason
666:26 previous configuration and so the reason you get random values in the memory is
666:28 you get random values in the memory is because even though we hav't drawn swap
666:29 because even though we hav't drawn swap here there was stuff there a moment ago
666:31 here there was stuff there a moment ago it's going to be there the next time you
666:33 it's going to be there the next time you use that same memory now let's go ahead
666:35 use that same memory now let's go ahead and step through this a little more
666:36 and step through this a little more methodically main has two variables
666:39 methodically main has two variables called X and Y one and two so let's
666:41 called X and Y one and two so let's advance and represent X is one y is two
666:44 advance and represent X is one y is two taking up these two chunks of memory
666:46 taking up these two chunks of memory when we call swamp now swamp gets a new
666:48 when we call swamp now swamp gets a new slice of memory that then gives us three
666:51 slice of memory that then gives us three variables A and B technically the
666:53 variables A and B technically the arguments and temp so what happens well
666:56 arguments and temp so what happens well because functions automatically pass in
666:58 because functions automatically pass in values by value or rather pass in
667:00 values by value or rather pass in arguments by value X gets copied into a
667:04 arguments by value X gets copied into a y gets copied into B and then once we
667:07 y gets copied into B and then once we start executing the algorithm all the
667:08 start executing the algorithm all the watered glasses well what happens here
667:11 watered glasses well what happens here so if I execute the first line of code
667:12 so if I execute the first line of code temp equals a temp gets a copy of a what
667:16 temp equals a temp gets a copy of a what happens next a equals B so a takes on a
667:18 happens next a equals B so a takes on a copy of B and now we do the final swap
667:21 copy of B and now we do the final swap in the glasses b equals temp b gets a
667:23 in the glasses b equals temp b gets a copy of temp now we don't have to change
667:25 copy of temp now we don't have to change temp because it's essentially empty
667:27 temp because it's essentially empty although there's the garbage value one
667:29 although there's the garbage value one is always now going to be there until we
667:31 is always now going to be there until we reuse that memory the important thing
667:33 reuse that memory the important thing though is that a and b have been swapped
667:35 though is that a and b have been swapped but what obviously has not been swapped
667:37 but what obviously has not been swapped as is Manifest is when swap returns X
667:40 as is Manifest is when swap returns X and Y are untouched because copies
667:42 and Y are untouched because copies thereof were passed in so we need a
667:44 thereof were passed in so we need a solution to this problem and if we
667:45 solution to this problem and if we advance one more time if you don't mind
667:47 advance one more time if you don't mind let me step over here but then call you
667:48 let me step over here but then call you back in a second this code here is
667:51 back in a second this code here is logically correct this is what you did
667:53 logically correct this is what you did but this is now a detail of C you can't
667:55 but this is now a detail of C you can't just swap the things by value because
667:57 just swap the things by value because you're only changing it in the scope of
667:59 you're only changing it in the scope of the swap function function but I think
668:01 the swap function function but I think if we change it to this and add some
668:03 if we change it to this and add some annoying syntax we can solve the problem
668:06 annoying syntax we can solve the problem just like you can declare variables as
668:08 just like you can declare variables as storing addresses you can declare
668:10 storing addresses you can declare arguments to functions AKA parameters as
668:13 arguments to functions AKA parameters as taking addresses this new version of
668:15 taking addresses this new version of swap means that a shall be the address
668:17 swap means that a shall be the address of an integer B shall be the address of
668:20 of an integer B shall be the address of an integer and now it gets a little
668:22 an integer and now it gets a little cryptic here temp is the same because
668:24 cryptic here temp is the same because it's just an integer like it was in week
668:25 it's just an integer like it was in week one nothing special about temp but if
668:28 one nothing special about temp but if you want to get the value at a you do
668:31 you want to get the value at a you do star a and that goes to the address
668:33 star a and that goes to the address grabs the number one presumably if you
668:35 grabs the number one presumably if you want to change the value of a you go to
668:37 want to change the value of a you go to that address you follow the treasure map
668:39 that address you follow the treasure map to the other mailbox and you set it
668:41 to the other mailbox and you set it equal to whatever is at the value of B
668:43 equal to whatever is at the value of B you go to B as well last line you go to
668:46 you go to B as well last line you go to B now and change it to be whatever the
668:47 B now and change it to be whatever the temporary variable was which happened to
668:49 temporary variable was which happened to be the same as a so that's where the
668:51 be the same as a so that's where the final value gets swapped but here
668:53 final value gets swapped but here there's a lot more like crisscrossing
668:55 there's a lot more like crisscrossing metaphorically across the stage where
668:56 metaphorically across the stage where you're going to all of these different
668:58 you're going to all of these different addresses in the swap function to make
669:00 addresses in the swap function to make these changes so if we advance now to
669:01 these changes so if we advance now to the pictoral version of this here's the
669:03 the pictoral version of this here's the same story as before with Maine and X
669:05 same story as before with Maine and X and Y are 1 and two respectively when
669:07 and Y are 1 and two respectively when swap gets called now notice and I'll do
669:10 swap gets called now notice and I'll do it with arrows here a is effectively
669:12 it with arrows here a is effectively pointing to X B is effectively pointing
669:14 pointing to X B is effectively pointing to Y if we really get into the weeds
669:16 to Y if we really get into the weeds these are actually like addresses but
669:18 these are actually like addresses but who cares about the specifics it's
669:20 who cares about the specifics it's really just the concept here so now what
669:22 really just the concept here so now what happens in temp gets star a star a means
669:25 happens in temp gets star a star a means start at a and go there Follow the arrow
669:27 start at a and go there Follow the arrow if you will sort of shoots in ladder
669:29 if you will sort of shoots in ladder Style and then that's one so we put one
669:31 Style and then that's one so we put one and 10th all right star a equals star B
669:34 and 10th all right star a equals star B so let's do it from right to left star b
669:35 so let's do it from right to left star b means Follow the arrow it's two and then
669:38 means Follow the arrow it's two and then what do you do Follow the arrow it's now
669:40 what do you do Follow the arrow it's now two because you copy one to the other
669:42 two because you copy one to the other from right to left and then lastly star
669:45 from right to left and then lastly star b gets temp so start at B go to B and
669:47 b gets temp so start at B go to B and now store whatever the value is in temp
669:50 now store whatever the value is in temp so just by having this basic new syntax
669:52 so just by having this basic new syntax of like ampersands and stars and so
669:54 of like ampersands and stars and so forth we can actually now go to places
669:56 forth we can actually now go to places and circumvent what is otherwise a
669:57 and circumvent what is otherwise a feature of C that these variables are
670:00 feature of C that these variables are locally scoped but you can still access
670:02 locally scoped but you can still access things in other functions as well so
670:04 things in other functions as well so thank you so much for helping step
670:05 thank you so much for helping step through this so we now have a
670:08 through this so we now have a application of this that explains why
670:10 application of this that explains why now in this version of the C code this
670:12 now in this version of the C code this would actually now work so in fact let
670:14 would actually now work so in fact let me go back to my swap code here and let
670:17 me go back to my swap code here and let me change the function ever so slightly
670:19 me change the function ever so slightly in vs code so let me scroll down leaving
670:21 in vs code so let me scroll down leaving main the same and let me change swaps
670:24 main the same and let me change swaps prototype to taking in addresses let me
670:27 prototype to taking in addresses let me go to a here let me go to a here let me
670:30 go to a here let me go to a here let me go to B here and let me go to B here as
670:33 go to B here and let me go to B here as well but nothing else changes this
670:35 well but nothing else changes this change here in particular is enough of a
670:37 change here in particular is enough of a clue to see that means when you call
670:39 clue to see that means when you call Swap and pass into values I'm expecting
670:42 Swap and pass into values I'm expecting addresses now not integers but now that
670:45 addresses now not integers but now that I've made this change I do need to go up
670:47 I've made this change I do need to go up to Main and make one change does anyone
670:51 to Main and make one change does anyone have the intuition for what now need
670:53 have the intuition for what now need change in main so that I pass in X and Y
670:57 change in main so that I pass in X and Y by reference that is by address rather
670:59 by reference that is by address rather than than by value or copy oh yeah and
671:02 than than by value or copy oh yeah and [Music]
671:05 [Music] back so close so on the swap line it's
671:08 back so close so on the swap line it's not star that I want in front of the X
671:10 not star that I want in front of the X and the Y it's
671:16 instead what's the other one it's the Amper sand why because if I want to
671:18 Amper sand why because if I want to enable swap to go somewhere just like
671:20 enable swap to go somewhere just like Carter and I played this game with the
671:21 Carter and I played this game with the mailboxes I need to inform swap of the
671:24 mailboxes I need to inform swap of the address of X and the address of Y and
671:26 address of X and the address of Y and again per the beginning of today's class
671:28 again per the beginning of today's class Ampersand is the text via which we do
671:30 Ampersand is the text via which we do that so I add an ampersand here to get
671:32 that so I add an ampersand here to get the address of X Ampersand here to get
671:33 the address of X Ampersand here to get the address of Y and now this code lines
671:36 the address of Y and now this code lines up with the picture that Carter just
671:38 up with the picture that Carter just helped us walk through and so when I run
671:40 helped us walk through and so when I run make swap here I have a mistake oh what
671:43 make swap here I have a mistake oh what did I do wrong not intentional but I
671:46 did I do wrong not intentional but I guess worth pointing out I screwed up
671:48 guess worth pointing out I screwed up here it doesn't like Ampersand
671:52 here it doesn't like Ampersand X because of something on line three
671:55 X because of something on line three which is way early in the code what did
671:57 which is way early in the code what did I screw up yeah in the middle
672:04 yeah so this is why we you should not copy paste even though it's necessary
672:05 copy paste even though it's necessary for things like function prototypes if I
672:06 for things like function prototypes if I change swap at the bottom I need to
672:08 change swap at the bottom I need to change its prototype so let me add the
672:10 change its prototype so let me add the star there add the star there or just
672:12 star there add the star there or just recopy paste it at the top of the file
672:14 recopy paste it at the top of the file now let me do make swap again let me now
672:16 now let me do make swap again let me now do do/ Swap and I should now see X is
672:19 do do/ Swap and I should now see X is one y is 2 and hopefully X is 2 Y is 1
672:23 one y is 2 and hopefully X is 2 Y is 1 which I now do so the logic is the same
672:25 which I now do so the logic is the same the algorithm is the same all the weak
672:27 the algorithm is the same all the weak zero stuff is the same except now and
672:29 zero stuff is the same except now and four you just have a bit more
672:31 four you just have a bit more expressiveness via which you can tell
672:33 expressiveness via which you can tell the computer exactly what you want to
672:35 the computer exactly what you want to manipulate and how any questions then on
672:39 manipulate and how any questions then on this technique
672:42 this technique here no all right well when we fix this
672:45 here no all right well when we fix this there's still going to be problems and
672:47 there's still going to be problems and just so you've seen some terms of art
672:48 just so you've seen some terms of art here this is bad whenever you have two
672:50 here this is bad whenever you have two arrows pointing at one another certainly
672:51 arrows pointing at one another certainly if you might use and reuse more and more
672:53 if you might use and reuse more and more memory and it turns out there's some
672:55 memory and it turns out there's some terms of art that might suddenly now
672:56 terms of art that might suddenly now make sense especially if you've
672:57 make sense especially if you've programmed before bad things Can Happen
673:00 programmed before bad things Can Happen by this design but there's really only
673:02 by this design but there's really only this kind of design because it's a
673:03 this kind of design because it's a finite amount of memory so at some point
673:05 finite amount of memory so at some point bad things are going to happen no matter
673:06 bad things are going to happen no matter what if a computer runs out of memory so
673:08 what if a computer runs out of memory so it's not that this was a poor decision
673:09 it's not that this was a poor decision it's just sort of a necessary one given
673:11 it's just sort of a necessary one given finite amounts of memory in a computer
673:13 finite amounts of memory in a computer but a heap overflow so to speak is when
673:15 but a heap overflow so to speak is when you actually overflow the Heap and touch
673:17 you actually overflow the Heap and touch memory that you shouldn't up there stack
673:19 memory that you shouldn't up there stack Overflow is when you somehow overflow
673:21 Overflow is when you somehow overflow the stack and touch memory that you
673:22 the stack and touch memory that you shouldn't down there so with that said
673:24 shouldn't down there so with that said these are really just problems that can
673:26 these are really just problems that can happen and there specific incarnations
673:28 happen and there specific incarnations of what are generally called buffer
673:29 of what are generally called buffer overflows a buffer like in the YouTube
673:32 overflows a buffer like in the YouTube sense is just like a chunk of memory
673:33 sense is just like a chunk of memory that in the case of YouTube stores like
673:35 that in the case of YouTube stores like the next few seconds or minutes of video
673:37 the next few seconds or minutes of video but generally speaking a buffer is just
673:38 but generally speaking a buffer is just a chunk of memory that the computer is
673:40 a chunk of memory that the computer is using for some purpose be it the stack
673:42 using for some purpose be it the stack be it the Heap be it an array in the
673:44 be it the Heap be it an array in the computer and so buffer overflows are
673:46 computer and so buffer overflows are what happens when you just have logical
673:48 what happens when you just have logical bugs in your code but with these
673:52 bugs in your code but with these Primitives now in mind we wanted to
673:53 Primitives now in mind we wanted to conclude with a final Revelations and
673:56 conclude with a final Revelations and that's how some functions like these
673:58 that's how some functions like these here work the other thing in the cs50
673:59 here work the other thing in the cs50 library besides the type def for quote
674:01 library besides the type def for quote unquote string is of course all of these
674:03 unquote string is of course all of these functions and we give you these
674:04 functions and we give you these functions because honestly in C it is
674:07 functions because honestly in C it is hard it's annoying it's painful it's
674:09 hard it's annoying it's painful it's difficult to get user input correctly
674:12 difficult to get user input correctly it's very easy when you don't know how
674:14 it's very easy when you don't know how much how much the humans going to type
674:16 much how much the humans going to type to write buggy code when it comes to it
674:18 to write buggy code when it comes to it and indeed it's really hard to store it
674:20 and indeed it's really hard to store it correctly without accidentally having
674:21 correctly without accidentally having some kind of buffer overflow so for
674:24 some kind of buffer overflow so for instance let me show you a program here
674:26 instance let me show you a program here I'm going to go ahead and write this one
674:27 I'm going to go ahead and write this one from scratch so let me go ahead and open
674:29 from scratch so let me go ahead and open a file called get. C where I'm going to
674:31 a file called get. C where I'm going to go ahead and mimic the idea of getting
674:33 go ahead and mimic the idea of getting integers manually without the cs50
674:35 integers manually without the cs50 library so I'm going to include standard
674:37 library so I'm going to include standard io. only I'm going to Define main as not
674:39 io. only I'm going to Define main as not taking any command line arguments and
674:41 taking any command line arguments and then I'm going to do something like this
674:43 then I'm going to do something like this give me a variable x with no value yet
674:46 give me a variable x with no value yet and normally I would do something like
674:47 and normally I would do something like get int but let me take that away no
674:49 get int but let me take that away no more training wheels for get int either
674:51 more training wheels for get int either so let me just Define the int X let me
674:54 so let me just Define the int X let me then just print out something like uh a
674:57 then just print out something like uh a prompt and I'll just do x c just to make
674:59 prompt and I'll just do x c just to make it obvious to the human what we're
675:01 it obvious to the human what we're waiting for and now I'm going to use a
675:03 waiting for and now I'm going to use a builtin c function to get user input I'm
675:05 builtin c function to get user input I'm going to call a function called scan F
675:07 going to call a function called scan F which sort of scans the user's keyboard
675:09 which sort of scans the user's keyboard for input I'm going to scan it for an
675:11 for input I'm going to scan it for an integer so just like print F I'm going
675:13 integer so just like print F I'm going to use percent I because I expect an INT
675:16 to use percent I because I expect an INT and then I want to tell scanf where to
675:18 and then I want to tell scanf where to put the human's integer from the
675:20 put the human's integer from the keyboard it is not correct though to say
675:23 keyboard it is not correct though to say x because if I say x I run into the same
675:25 x because if I say x I run into the same swap problem scanf no function can
675:28 swap problem scanf no function can change the value of X unless I pass it
675:30 change the value of X unless I pass it not by value but
675:32 not by value but by reference so we're back to our
675:34 by reference so we're back to our Ampersand friend and now it has like a a
675:37 Ampersand friend and now it has like a a treasure map to the actual location of X
675:40 treasure map to the actual location of X and can therefore change it and so now
675:42 and can therefore change it and so now at the very end of this program let me
675:43 at the very end of this program let me do something simple like let's just go
675:45 do something simple like let's just go ahead and print out with print F uh the
675:49 ahead and print out with print F uh the value of x using percent I as always
675:52 value of x using percent I as always plugging in X not Ampersand X this is
675:54 plugging in X not Ampersand X this is now week one stuff I want to print the
675:56 now week one stuff I want to print the actual integer value of x so the only
675:59 actual integer value of x so the only change here is that instead of using get
676:00 change here is that instead of using get in I'm now using this new function that
676:03 in I'm now using this new function that as of today exists called scanf so let
676:06 as of today exists called scanf so let me go ahead and run get make get to
676:09 me go ahead and run get make get to create this program doget and let's go
676:11 create this program doget and let's go ahead and type in a value for x 50 enter
676:15 ahead and type in a value for x 50 enter and it just works so it turns out get in
676:18 and it just works so it turns out get in is pretty simple to implement however
676:20 is pretty simple to implement however notice what does not work if I type in
676:21 notice what does not work if I type in cat for instance cat gets converted to
676:24 cat for instance cat gets converted to zero and meanwhile get in recall will
676:27 zero and meanwhile get in recall will reprompt the user if a human not type an
676:29 reprompt the user if a human not type an actual integer you get automatically
676:31 actual integer you get automatically reprompt so that's one of the features
676:33 reprompt so that's one of the features we for cs50 added to get in just to make
676:35 we for cs50 added to get in just to make your programs more user friendly but
676:37 your programs more user friendly but otherwise get in is pretty
676:38 otherwise get in is pretty straightforward to re-implement using
676:40 straightforward to re-implement using scanf unfortunately that's not true for
676:43 scanf unfortunately that's not true for Strings because how do you know when you
676:45 Strings because how do you know when you write your code what word the human's
676:47 write your code what word the human's going to eventually type in how long
676:49 going to eventually type in how long they're greeting like high is if it's uh
676:51 they're greeting like high is if it's uh if their name is David or Carter or
676:53 if their name is David or Carter or anything else you just don't know in
676:54 anything else you just don't know in advance how much memory you need so how
676:57 advance how much memory you need so how might we do this in with string well let
676:59 might we do this in with string well let me go ahead and declare a string s
677:02 me go ahead and declare a string s although you know what there's no cs50
677:03 although you know what there's no cs50 Library so we do Char star s today
677:06 Library so we do Char star s today instead and that gives me not a string
677:09 instead and that gives me not a string per se but a pointer that will Point
677:12 per se but a pointer that will Point presumably to a string ideally I would
677:14 presumably to a string ideally I would use this get string but again we've
677:16 use this get string but again we've taken that training wheel away so now
677:18 taken that training wheel away so now that I have a pointer s suppose I prompt
677:20 that I have a pointer s suppose I prompt the human for a value for S just like
677:22 the human for a value for S just like before let me use scan F now and tell
677:24 before let me use scan F now and tell the user that I expect to read a string
677:27 the user that I expect to read a string percent s from the keyboard
677:29 percent s from the keyboard uh and store it in s now this is subtle
677:33 uh and store it in s now this is subtle I don't technically need an Amper sand
677:35 I don't technically need an Amper sand here even though I did for an INT and I
677:38 here even though I did for an INT and I would for a float and a double and a
677:40 would for a float and a double and a long and a bull and a Char why do I not
677:42 long and a bull and a Char why do I not need an Amper sand in this story to pass
677:44 need an Amper sand in this story to pass by
677:47 by reference because s is it's already an
677:50 reference because s is it's already an address again strings are just special
677:52 address again strings are just special strings now are always addresses so you
677:55 strings now are always addresses so you don't need to additionally add an Amper
677:57 don't need to additionally add an Amper sand here that's the only subtle
677:58 sand here that's the only subtle difference here but now if I go ahead
678:00 difference here but now if I go ahead and print out at the very end what the
678:01 and print out at the very end what the value of s is using percent S as before
678:04 value of s is using percent S as before this program looks like it's almost the
678:07 this program looks like it's almost the same as the int version but let's do
678:09 same as the int version but let's do make get and okay so this is not good
678:13 make get and okay so this is not good all right so it doesn't like an
678:14 all right so it doesn't like an initialized value so let me make it
678:16 initialized value so let me make it happy I said earlier to always
678:18 happy I said earlier to always initialize my variable so let's
678:19 initialize my variable so let's initialize it to null so that at least
678:21 initialize it to null so that at least something is there that's your good
678:22 something is there that's your good default value nowadays now if I do doget
678:25 default value nowadays now if I do doget now we're good and let me type in
678:27 now we're good and let me type in something like cat
678:30 something like cat okay cat is not X well let me try
678:32 okay cat is not X well let me try another word maybe it's just cat is
678:33 another word maybe it's just cat is wrong dog okay let me try David it just
678:37 wrong dog okay let me try David it just doesn't seem to be working moreover it's
678:38 doesn't seem to be working moreover it's printing it as a zero what logically
678:42 printing it as a zero what logically though is the bug here scanf worked a
678:45 though is the bug here scanf worked a moment ago for integers but it's not
678:47 moment ago for integers but it's not working for Strings and it seems to be
678:48 working for Strings and it seems to be forgetting C A it's forgetting dog it's
678:51 forgetting C A it's forgetting dog it's forgetting
678:52 forgetting David
678:53 David why what's Happening
678:56 why what's Happening Here think back to our yellow pictures
678:59 Here think back to our yellow pictures of
679:00 of memory when I
679:03 memory when I yeah it might be reading just the null
679:06 yeah it might be reading just the null itself because s is being initialized to
679:08 itself because s is being initialized to null and what step have I forgotten from
679:10 null and what step have I forgotten from just like a few minutes ago what did I
679:12 just like a few minutes ago what did I not actually request of the computer
679:15 not actually request of the computer like actual memory to store the C A the
679:18 like actual memory to store the C A the dog the d v d right there's nowhere have
679:21 dog the d v d right there's nowhere have I asked the computer for some amount of
679:23 I asked the computer for some amount of memory and so technically it might be
679:26 memory and so technically it might be reading it into some garbage location
679:28 reading it into some garbage location and that's really the problem here S is
679:30 and that's really the problem here S is initialized to null now and so in fact
679:32 initialized to null now and so in fact it is printing zero as null but I'm not
679:34 it is printing zero as null but I'm not seeing any of the other letters because
679:36 seeing any of the other letters because there was nowhere to put them C A do D
679:38 there was nowhere to put them C A do D ID because I didn't ask for three bytes
679:40 ID because I didn't ask for three bytes four bytes five bytes 100 bytes there's
679:42 four bytes five bytes 100 bytes there's no use of malok there's no use of an
679:44 no use of malok there's no use of an array there's no memory allocated for
679:46 array there's no memory allocated for anything other than the pointer itself
679:49 anything other than the pointer itself and this is where honestly life gets
679:51 and this is where honestly life gets hard with scanf like I could solve this
679:53 hard with scanf like I could solve this problem in a couple of ways let me go
679:54 problem in a couple of ways let me go ahead and do this instead of declaring s
679:56 ahead and do this instead of declaring s to be a pointer let me declare s to
679:59 to be a pointer let me declare s to actually be a an array of four chars and
680:02 actually be a an array of four chars and now let me go ahead and recompile the
680:04 now let me go ahead and recompile the code so make get doget and I'll type in
680:08 code so make get doget and I'll type in cat now that now works why well I'm
680:12 cat now that now works why well I'm allocating an explicit array of size
680:14 allocating an explicit array of size four enough for a one two three letters
680:16 four enough for a one two three letters plus a null character here's where to
680:18 plus a null character here's where to someone's question earlier it turns out
680:20 someone's question earlier it turns out then in some context you can treat
680:22 then in some context you can treat arrays as though they are Pointers
680:25 arrays as though they are Pointers themselves see we'll sort of do the
680:26 themselves see we'll sort of do the conversion for you but for now just
680:28 conversion for you but for now just assume that s is just an array of size
680:30 assume that s is just an array of size four and if you pass it into to scanf
680:33 four and if you pass it into to scanf that's like a treasure map that leads to
680:35 that's like a treasure map that leads to those four bytes so scanf can now
680:37 those four bytes so scanf can now successfully fill it with c a d o but
680:41 successfully fill it with c a d o but but let's try this again let's type in
680:43 but let's try this again let's type in David and here okay we got lucky but I
680:47 David and here okay we got lucky but I technically touched memory that I should
680:49 technically touched memory that I should not and in fact if I typed in a long
680:51 not and in fact if I typed in a long enough string and I don't think I could
680:52 enough string and I don't think I could do it very easily like by without typing
680:54 do it very easily like by without typing this thousands or hundreds of times
680:57 this thousands or hundreds of times still okay but you'll notice that it's
680:59 still okay but you'll notice that it's forgotten the rest of it now right so
681:01 forgotten the rest of it now right so somewhere we went beyond the boundary of
681:03 somewhere we went beyond the boundary of the array and we just don't have enough
681:04 the array and we just don't have enough storage space for that entire thing so
681:06 storage space for that entire thing so what do you do in your program if you
681:07 what do you do in your program if you don't know how long the person's name or
681:09 don't know how long the person's name or the the the animal name is going to be
681:10 the the the animal name is going to be what do you do 40 400 4,000 40,000 like
681:14 what do you do 40 400 4,000 40,000 like at some point you have to draw a line in
681:16 at some point you have to draw a line in the sand and that's why like getting
681:17 the sand and that's why like getting user input is so annoying in a language
681:20 user input is so annoying in a language like C and that's why get string exists
681:22 like C and that's why get string exists what we do if you're curious is we look
681:25 what we do if you're curious is we look at the user's input and we take baby
681:27 at the user's input and we take baby steps we look at it one character at a
681:29 steps we look at it one character at a time and every time we see another
681:31 time and every time we see another character we actually call malok again
681:33 character we actually call malok again and say no I need more than one bite I
681:34 and say no I need more than one bite I need two oh wait they typed in three
681:36 need two oh wait they typed in three letters I need three instead of two oh I
681:38 letters I need three instead of two oh I need four instead of two and we have
681:39 need four instead of two and we have this crazy loop essentially that keeps
681:41 this crazy loop essentially that keeps asking for more and more memory but by
681:43 asking for more and more memory but by taking baby steps and honestly if you
681:45 taking baby steps and honestly if you all had to do that in week one my God
681:47 all had to do that in week one my God like we couldn't even write hello world
681:48 like we couldn't even write hello world anymore and so that's why these training
681:50 anymore and so that's why these training wheels exist at least early on and
681:52 wheels exist at least early on and that's why in higher level languages
681:54 that's why in higher level languages like in uh python you don't have to do
681:56 like in uh python you don't have to do this at all it just works as you'd
681:59 this at all it just works as you'd expect so what more can we do well
682:01 expect so what more can we do well you'll see in problems set for this
682:03 you'll see in problems set for this coming week if I open up an example like
682:05 coming week if I open up an example like this phone book. C you'll see that you
682:08 this phone book. C you'll see that you can manipulate files now that you have a
682:10 can manipulate files now that you have a vocabulary for pointers it's going to be
682:12 vocabulary for pointers it's going to be new quickly but here we have an example
682:15 new quickly but here we have an example of how I have a program using some
682:17 of how I have a program using some familiar libraries here but as I claim
682:19 familiar libraries here but as I claim in my comment this saves names and
682:20 in my comment this saves names and numbers to a CSV file all of my examples
682:22 numbers to a CSV file all of my examples thus far I type in some words I type in
682:24 thus far I type in some words I type in some names and some phone numbers and
682:26 some names and some phone numbers and they disappear because we only store
682:27 they disappear because we only store them in memory but if you want to store
682:29 them in memory but if you want to store data in like a CSV file comma separated
682:31 data in like a CSV file comma separated values which is like a simple
682:32 values which is like a simple spreadsheet like Excel and apple numbers
682:35 spreadsheet like Excel and apple numbers and Google Sheets can open you can
682:36 and Google Sheets can open you can actually do this yourself so just as a
682:38 actually do this yourself so just as a teaser for this week here on line n I'm
682:40 teaser for this week here on line n I'm using a new data type not a cs50 thing
682:42 using a new data type not a cs50 thing this is a c thing called file but if you
682:45 this is a c thing called file but if you want to manipulate files you need to use
682:46 want to manipulate files you need to use addresses that is pointers so here is me
682:49 addresses that is pointers so here is me creating a variable called file that's
682:51 creating a variable called file that's going to point to an actual file on the
682:53 going to point to an actual file on the hard drive on the server or your Mac or
682:55 hard drive on the server or your Mac or PC fop is going to be a new function
682:57 PC fop is going to be a new function you'll use that will open open a file
682:59 you'll use that will open open a file and it will return effectively a pointer
683:01 and it will return effectively a pointer there to in memory the file name I want
683:04 there to in memory the file name I want to open is phonebook .csv and in this
683:05 to open is phonebook .csv and in this example it's going to be uh a pen mode
683:08 example it's going to be uh a pen mode it will keep allowing me to add more and
683:09 it will keep allowing me to add more and more names and numbers to this file
683:11 more names and numbers to this file here's some old get string stuff because
683:13 here's some old get string stuff because I'm not going to reinvent get string
683:14 I'm not going to reinvent get string with scan F but down here is a slightly
683:16 with scan F but down here is a slightly new function it's not print F but
683:18 new function it's not print F but fprintf and it turns out it's very easy
683:20 fprintf and it turns out it's very easy to print things not to the screen but to
683:22 to print things not to the screen but to a file with fprintf and it takes an
683:24 a file with fprintf and it takes an additional argument instead of starting
683:26 additional argument instead of starting with the quoted string you'll have to
683:28 with the quoted string you'll have to like say what file you want to write to
683:30 like say what file you want to write to and fr print F we'll figure out how to
683:32 and fr print F we'll figure out how to get the uh the bits into that file
683:34 get the uh the bits into that file passing in something like name comma
683:36 passing in something like name comma number so if I run this somewhat quickly
683:38 number so if I run this somewhat quickly here let me do this let me pre-create a
683:41 here let me do this let me pre-create a file called uh phonebook do CSV and in
683:45 file called uh phonebook do CSV and in phone book. CSV I'm going to create a
683:47 phone book. CSV I'm going to create a temporary row here name comma number
683:49 temporary row here name comma number just so that there's something in this
683:51 just so that there's something in this file and now let me go ahead and do this
683:53 file and now let me go ahead and do this and split my screen here if I have
683:56 and split my screen here if I have phonebook.com
683:58 phonebook.com and phone book. C on the left let me
684:00 and phone book. C on the left let me compile make phonebook which is the C
684:03 compile make phonebook which is the C version phonebook and now I'm prompted
684:06 version phonebook and now I'm prompted for a name and a number so I'll type in
684:07 for a name and a number so I'll type in David and then for instance + one
684:10 David and then for instance + one 949 uh what is it 468 275 o enter oh
684:16 949 uh what is it 468 275 o enter oh damn it bug uh pretend that didn't
684:18 damn it bug uh pretend that didn't happen I forgot to hit enter in the file
684:20 happen I forgot to hit enter in the file so let's do this again if I run the
684:23 so let's do this again if I run the program Again David and plus one 94 9
684:29 program Again David and plus one 94 9 468 2750 enter it's been saved now to
684:34 468 2750 enter it's been saved now to the file and if I close this file and I
684:37 the file and if I close this file and I reopen code of phonebook.com
684:51 or the like and I've actually created an actual CSV file uh if you're smiling
684:53 actual CSV file uh if you're smiling because I keep repeating my phone number
684:54 because I keep repeating my phone number out loud I would encourage you to call
684:56 out loud I would encourage you to call or text that number sometime it might
684:58 or text that number sometime it might very well well be a an Easter egg of
684:59 very well well be a an Easter egg of source but via these functions here do
685:02 source but via these functions here do we have now the ability to write files
685:04 we have now the ability to write files uh input and output and among the goals
685:07 uh input and output and among the goals then for this week as we'll see are to
685:08 then for this week as we'll see are to actually play with images in the spirit
685:10 actually play with images in the spirit of something like Instagram filters or
685:12 of something like Instagram filters or the like and we'll introduce you for
685:13 the like and we'll introduce you for instance to a file format called bmps
685:16 instance to a file format called bmps which to come full circle to the start
685:17 which to come full circle to the start of class are just maps of bits but more
685:20 of class are just maps of bits but more than just single bits for white and
685:21 than just single bits for white and black but rather colorful patterns as
685:23 black but rather colorful patterns as well and we'll give you images like this
685:25 well and we'll give you images like this of the week's Bridge here across the
685:26 of the week's Bridge here across the river at Harvard and you've run
685:28 river at Harvard and you've run after writing your own code in C and
685:30 after writing your own code in C and understanding how the data stored in the
685:33 understanding how the data stored in the computer's memory you'll be able to
685:34 computer's memory you'll be able to apply your own Instagram like filters to
685:36 apply your own Instagram like filters to make things uh grayscale instead or
685:38 make things uh grayscale instead or sepia in this case you can even flip the
685:40 sepia in this case you can even flip the bits around so that the thing is a
685:41 bits around so that the thing is a mirror image you can blur things further
685:44 mirror image you can blur things further or if you really are feeling more
685:45 or if you really are feeling more comfortable you can even write code that
685:47 comfortable you can even write code that finds the edges of the image and creates
685:50 finds the edges of the image and creates works of art like these so all that in
685:52 works of art like these so all that in more in problem set four we will see you
685:54 more in problem set four we will see you next
687:49 all right this is cs50 and this is week five which is going to be our last week
687:51 five which is going to be our last week in see uh but what that means is that
687:54 in see uh but what that means is that we'll have
687:57 we'll have okay
687:58 okay but with this week with last week and
688:00 but with this week with last week and really all of the weeks prior have you
688:02 really all of the weeks prior have you been hopefully acquiring if slowly and
688:04 been hopefully acquiring if slowly and with some challenge like some
688:05 with some challenge like some fundamental building blocks that are
688:07 fundamental building blocks that are still going to underly everything we
688:08 still going to underly everything we continue doing in the semester even as
688:10 continue doing in the semester even as we transition to so-called higher level
688:12 we transition to so-called higher level languages next week indeed we'll
688:14 languages next week indeed we'll introduce python a very popular language
688:16 introduce python a very popular language that does not have pointers does not
688:18 that does not have pointers does not have memory management at this very low
688:20 have memory management at this very low level but that's really just because
688:21 level but that's really just because someone else wrote the code that will do
688:23 someone else wrote the code that will do that for you and it's going to make our
688:24 that for you and it's going to make our lives easier because it means when you
688:26 lives easier because it means when you want to solve a problem concept up here
688:28 want to solve a problem concept up here to just get real work done or build
688:30 to just get real work done or build something amazing you don't have to
688:31 something amazing you don't have to really get into the same weeds as we
688:33 really get into the same weeds as we have been deliberately this week and now
688:35 have been deliberately this week and now last but the goal ultimately is that you
688:38 last but the goal ultimately is that you better understand and can better harness
688:40 better understand and can better harness than all that a computer can do even in
688:42 than all that a computer can do even in those higher level languages So today
688:44 those higher level languages So today we're going to focus particularly on
688:46 we're going to focus particularly on data structures how you might structure
688:47 data structures how you might structure your data in memory which really amounts
688:49 your data in memory which really amounts to building things digitally stitching
688:52 to building things digitally stitching together ideas and Concepts in memory
688:54 together ideas and Concepts in memory using this new building block from last
688:56 using this new building block from last week which of course are these pointers
688:58 week which of course are these pointers pointers allow you to store addresses in
689:00 pointers allow you to store addresses in memory like in variables but with those
689:02 memory like in variables but with those simple addresses we can sort of leave
689:04 simple addresses we can sort of leave these breadcrumbs we can point from here
689:06 these breadcrumbs we can point from here to there and we can conceptually stitch
689:08 to there and we can conceptually stitch pieces of data together but there's
689:10 pieces of data together but there's going to be different ways of doing that
689:12 going to be different ways of doing that and today we'll focus first on what's
689:13 and today we'll focus first on what's generally known as an abstract data type
689:16 generally known as an abstract data type and similar to a type in C more
689:17 and similar to a type in C more generally it does actually have some
689:19 generally it does actually have some properties in it but the underlying
689:21 properties in it but the underlying implementation details of an abstract
689:23 implementation details of an abstract data type are ultimately up to you that
689:25 data type are ultimately up to you that is to say an abstract data type can
689:27 is to say an abstract data type can represent one thing and can do something
689:29 represent one thing and can do something but how you implement it allows you some
689:31 but how you implement it allows you some discretion underneath the hood so for
689:34 discretion underneath the hood so for instance in the world of computer
689:35 instance in the world of computer science a q is actually a technical term
689:38 science a q is actually a technical term this is a type of data structure that we
689:40 this is a type of data structure that we could theoretically build in code in C
689:42 could theoretically build in code in C or really any other language but a q has
689:45 or really any other language but a q has properties just like cu's in the real
689:47 properties just like cu's in the real world for instance if you've ever lined
689:49 world for instance if you've ever lined up for something to get food in a uh and
689:51 up for something to get food in a uh and get food in a restaurant or go into a
689:53 get food in a restaurant or go into a store or wait for the airport to clear
689:55 store or wait for the airport to clear you well you've lined up in a queue Q
689:58 you well you've lined up in a queue Q being some sort of line but what's
690:00 being some sort of line but what's noteworthy about Q's are specific
690:02 noteworthy about Q's are specific properties they are first in first out
690:06 properties they are first in first out data structures either virtually or in
690:08 data structures either virtually or in the human world which is to say the
690:10 the human world which is to say the first person in the line should ideally
690:12 first person in the line should ideally be served first at the restaurant or the
690:14 be served first at the restaurant or the first person in the line should get
690:15 first person in the line should get through airport security first by
690:17 through airport security first by contrast if it weren't first in first
690:19 contrast if it weren't first in first out you can imagine how frustrated you
690:21 out you can imagine how frustrated you would be if you have this sort of
690:23 would be if you have this sort of inherent unfairness in fact if you've
690:24 inherent unfairness in fact if you've ever been in line at a store a
690:26 ever been in line at a store a supermarket or the like and all of a
690:28 supermarket or the like and all of a sudden they maybe open a new line and
690:29 sudden they maybe open a new line and now the person behind you gets to kind
690:31 now the person behind you gets to kind of cut ahead and go forward that's
690:33 of cut ahead and go forward that's because they've broken the concept of
690:35 because they've broken the concept of the cube so it has this inherent
690:37 the cube so it has this inherent potential for unfairness unless you
690:39 potential for unfairness unless you maintain this first in first out
690:41 maintain this first in first out property this would be true too for like
690:43 property this would be true too for like a to-do list just for productivity sake
690:45 a to-do list just for productivity sake if you're in the habit on paper or
690:47 if you're in the habit on paper or virtually making a to-do list ideally
690:50 virtually making a to-do list ideally you probably want to go through that
690:51 you probably want to go through that list top to bottom so that you actually
690:53 list top to bottom so that you actually get the first stuff you thought of done
690:55 get the first stuff you thought of done first rather than always focusing on
690:57 first rather than always focusing on your most recent thought now within the
690:59 your most recent thought now within the world of Q's there's generally two
691:02 world of Q's there's generally two operations two functions if you will
691:04 operations two functions if you will that any Q would have either in the real
691:06 that any Q would have either in the real world or in the virtual NQ is usually
691:08 world or in the virtual NQ is usually the technical term to mean adding
691:10 the technical term to mean adding something to a q but specifically it
691:12 something to a q but specifically it means adding it to the end of the que so
691:14 means adding it to the end of the que so that someone isn't cutting in line for
691:16 that someone isn't cutting in line for instance to go up front and then DQ is
691:18 instance to go up front and then DQ is just the opposite when it's time for the
691:20 just the opposite when it's time for the first person in line to be served the
691:22 first person in line to be served the time for the first person in line to go
691:24 time for the first person in line to go through security they are dced so to
691:26 through security they are dced so to speak so technical concept ultimately as
691:29 speak so technical concept ultimately as it's implemented in code but it's really
691:31 it's implemented in code but it's really just a real world concept and these are
691:33 just a real world concept and these are in contrast to another abstract data
691:35 in contrast to another abstract data type that we might call a stack and a
691:38 type that we might call a stack and a stack much like a stack of trays in the
691:40 stack much like a stack of trays in the cafeteria has sort of fundamentally
691:42 cafeteria has sort of fundamentally different properties you can still add
691:44 different properties you can still add and remove things from them but consider
691:46 and remove things from them but consider what happens whenever they clean all the
691:47 what happens whenever they clean all the trays in the cafeteria or the dining
691:49 trays in the cafeteria or the dining hall they put one of the trays down here
691:52 hall they put one of the trays down here and then the next one on top of it and
691:53 and then the next one on top of it and then the next one on top of it it it and
691:55 then the next one on top of it it it and so forth but of course which tray do you
691:57 so forth but of course which tray do you presume presumably take as a user of
691:59 presume presumably take as a user of that physical
692:01 that physical stack the top one presumably right like
692:04 stack the top one presumably right like you're not going to fuss down here and
692:05 you're not going to fuss down here and try to pull one out and so that would
692:06 try to pull one out and so that would seem to have an opposite property lifo
692:09 seem to have an opposite property lifo last in first out is what characterizes
692:12 last in first out is what characterizes something like a stack and that just
692:14 something like a stack and that just makes sense certainly in the physical
692:15 makes sense certainly in the physical world of stacking all of those cafeteria
692:17 world of stacking all of those cafeteria trays because it just makes more sense
692:18 trays because it just makes more sense to grab the most recently added one the
692:21 to grab the most recently added one the last added one first and at least in
692:24 last added one first and at least in that context the trays don't necessarily
692:26 that context the trays don't necessarily care what order they're using used in
692:28 care what order they're using used in but even then you could imagine that
692:29 but even then you could imagine that maybe there's some old dirty nasty tray
692:31 maybe there's some old dirty nasty tray at the very bottom that like never gets
692:33 at the very bottom that like never gets used because you're constantly
692:35 used because you're constantly replenishing the stack so there might
692:36 replenishing the stack so there might very well be side effects of these kinds
692:38 very well be side effects of these kinds of features um you might be familiar
692:41 of features um you might be familiar with using Gmail for instance or really
692:43 with using Gmail for instance or really any email program what you're looking at
692:45 any email program what you're looking at in your inbox is technically a stack at
692:48 in your inbox is technically a stack at least if you've left the defaults
692:49 least if you've left the defaults configured why you get a new email where
692:51 configured why you get a new email where does it end up not like five pages of
692:54 does it end up not like five pages of emails later presumably right at the top
692:55 emails later presumably right at the top and the next one's right at the top
692:57 and the next one's right at the top right at the the top right at the top
692:58 right at the the top right at the top and so if you're like me you're guilty
693:00 and so if you're like me you're guilty of eventually losing track of some
693:01 of eventually losing track of some emails why because you've pushed so many
693:04 emails why because you've pushed so many more emails onto the stack that you sort
693:06 more emails onto the stack that you sort of lose track of the things you got
693:08 of lose track of the things you got earliest last in first out though is
693:10 earliest last in first out though is maintained the most recent email you get
693:11 maintained the most recent email you get might very well be the first one you
693:13 might very well be the first one you reply to but that's not necessarily good
693:15 reply to but that's not necessarily good for responsiveness to everyone else out
693:17 for responsiveness to everyone else out there uh similarly if you store like all
693:19 there uh similarly if you store like all of your sweaters in a stack like this uh
693:23 of your sweaters in a stack like this uh the uh like a pile of black ones below
693:25 the uh like a pile of black ones below which is a red and then a blue stack
693:27 which is a red and then a blue stack might be perfectly fine for keeping
693:28 might be perfectly fine for keeping things organized it's sort of the sane
693:30 things organized it's sort of the sane way to do it if you just have a shelf in
693:31 way to do it if you just have a shelf in your dorm room or home but what's going
693:33 your dorm room or home but what's going to be a side effect of using a stack to
693:35 to be a side effect of using a stack to store your sweaters if they're these in
693:37 store your sweaters if they're these in this way as opposed to a queue
693:42 this way as opposed to a queue yeah it's harder to get the red and blue
693:44 yeah it's harder to get the red and blue one so presumably you're going to much
693:46 one so presumably you're going to much more often wear for instance if you will
693:48 more often wear for instance if you will black instead there now the operations
693:51 black instead there now the operations for adding things to a stack are similar
693:54 for adding things to a stack are similar in spirit but just different vocabulary
693:56 in spirit but just different vocabulary you push something on top of a stack um
693:59 you push something on top of a stack um even though it's more like in the tray
694:00 even though it's more like in the tray world you sort of place it or rest it
694:02 world you sort of place it or rest it but pushing means adding something to
694:03 but pushing means adding something to the top of the stack and popping means
694:05 the top of the stack and popping means some removing something also from the
694:08 some removing something also from the top of the stack so it's not a matter of
694:10 top of the stack so it's not a matter of enqing at the end and deqing at the
694:13 enqing at the end and deqing at the beginning with a stack everything's
694:15 beginning with a stack everything's happening on top you're pushing onto the
694:17 happening on top you're pushing onto the top and then popping off of the top now
694:19 top and then popping off of the top now when it comes to actual code how might
694:21 when it comes to actual code how might we Implement something like this well
694:22 we Implement something like this well let's just focus on really how you might
694:24 let's just focus on really how you might implement the data structure itself and
694:26 implement the data structure itself and we won't Implement any functions you
694:28 we won't Implement any functions you might implement the notion of a stack
694:30 might implement the notion of a stack like this we've seen type def before it
694:31 like this we've seen type def before it just means Define a new type of my own
694:34 just means Define a new type of my own struct means here comes a structure AKA
694:36 struct means here comes a structure AKA a data structure of one or more
694:38 a data structure of one or more variables within and let's suppose like
694:41 variables within and let's suppose like last time we've had we defined already
694:43 last time we've had we defined already like a person data type using a separate
694:45 like a person data type using a separate type Def and every person has like a
694:47 type Def and every person has like a name and a number or whatever let me
694:49 name and a number or whatever let me just stipulate that person exists
694:50 just stipulate that person exists already so here you might have to
694:52 already so here you might have to implement a stack an array called people
694:56 implement a stack an array called people of some capacity maybe it's an an array
694:58 of some capacity maybe it's an an array of size 10 or 100 or whatever this is a
695:02 of size 10 or 100 or whatever this is a constant here in this context capacity
695:04 constant here in this context capacity and every element in that array is a
695:06 and every element in that array is a person structure and I now have this to
695:09 person structure and I now have this to size now this almost seems like a
695:11 size now this almost seems like a synonym for capacity but maybe
695:13 synonym for capacity but maybe intuitively anyone want to propose why
695:16 intuitively anyone want to propose why I'm apparently maintaining separately
695:18 I'm apparently maintaining separately the capacity of the stack but also the
695:21 the capacity of the stack but also the size of the stack why these two
695:23 size of the stack why these two distinctions
695:25 distinctions here yeah
695:37 perfect the capacity is how high the stack could be like how much room is
695:39 stack could be like how much room is there for those sweatshirts in my closet
695:42 there for those sweatshirts in my closet for instance whereas size is just
695:44 for instance whereas size is just literally at this moment in time how
695:45 literally at this moment in time how many sweatshirts are in the stack it's
695:47 many sweatshirts are in the stack it's either capacity or fewer presumably in
695:50 either capacity or fewer presumably in total there so what is capacity well we
695:52 total there so what is capacity well we could implement this in you know perhaps
695:54 could implement this in you know perhaps a familiar way I might just Define a
695:56 a familiar way I might just Define a const somewhere else in my code of type
695:58 const somewhere else in my code of type int that just defines it to be capacity
696:01 int that just defines it to be capacity 50 but what perhaps is going to be the
696:03 50 but what perhaps is going to be the downside of implementing a stack in this
696:06 downside of implementing a stack in this way of how using an array inside here
696:10 way of how using an array inside here like what's a downside now of
696:11 like what's a downside now of implementing a stack using an array and
696:14 implementing a stack using an array and this size variable
696:17 this size variable within what's a caveat here
696:20 within what's a caveat here perhaps any hands
696:26 yeah okay so it's going to be harder to reach elements that aren't the last one
696:28 reach elements that aren't the last one that is the most recently added one so
696:30 that is the most recently added one so there could be some sweatshirts so to
696:31 there could be some sweatshirts so to speak way down below so we've seen that
696:33 speak way down below so we've seen that before too but at some point too a
696:35 before too but at some point too a limitation of this design is is what
696:37 limitation of this design is is what it's going to be finite right I can
696:39 it's going to be finite right I can maximally fit in this example 50
696:41 maximally fit in this example 50 sweatshirts or 50 emails or 50 cafeteria
696:43 sweatshirts or 50 emails or 50 cafeteria Trace which is fine but at least it's
696:45 Trace which is fine but at least it's indeed finite and at least in the
696:47 indeed finite and at least in the computer's memory it might be nice to
696:49 computer's memory it might be nice to use more and more and more maybe as more
696:51 use more and more and more maybe as more things are getting added to the computer
696:53 things are getting added to the computer so maybe I make this 500 or heck why
696:55 so maybe I make this 500 or heck why don't I make it 5,000 or 50,000 well
696:58 don't I make it 5,000 or 50,000 well what's the tradeoff there if I want to
697:00 what's the tradeoff there if I want to have enough room to grow seems like I
697:03 have enough room to grow seems like I should just crank up the value of
697:04 should just crank up the value of capacity endlessly but why might I not
697:07 capacity endlessly but why might I not want to change the 50 to 500 or 5,000 or
697:11 want to change the 50 to 500 or 5,000 or 50,000 what's the trade-off there
697:14 50,000 what's the trade-off there perhaps just intuitively
697:21 yeah okay you don't want to touch memory that you're not supposed to be touching
697:22 that you're not supposed to be touching and in this case it wouldn't be that
697:24 and in this case it wouldn't be that wouldn't be a risk per se unless you
697:26 wouldn't be a risk per se unless you indeed overflow the stack but there's a
697:28 indeed overflow the stack but there's a related issue in asking for that much
697:30 related issue in asking for that much memory what would another downside
697:41 be yeah okay exactly so if you've got a capacity of 5,000 but you're only using
697:43 capacity of 5,000 but you're only using one of those elements it's it's awkward
697:46 one of those elements it's it's awkward to say it non-technically which is just
697:47 to say it non-technically which is just to say very very wasteful right that's
697:49 to say very very wasteful right that's just bad design it's correct it will
697:51 just bad design it's correct it will work for up to 5,000 elements but my
697:53 work for up to 5,000 elements but my gosh you're wasting
697:54 gosh you're wasting 4,999 extra spots and that's not going
697:57 4,999 extra spots and that's not going to end well especially if you're using
697:58 to end well especially if you're using more data structures in memory like your
698:00 more data structures in memory like your Mac your PC your phone is surely going
698:02 Mac your PC your phone is surely going to run out of memory if you ask for that
698:04 to run out of memory if you ask for that much so it' be nice if there is a bit
698:05 much so it' be nice if there is a bit more dynamism there whether it's a stack
698:08 more dynamism there whether it's a stack or a CU both of which might be
698:10 or a CU both of which might be implemented a little similarly in spirit
698:12 implemented a little similarly in spirit but let's conclude this abstraction by
698:15 but let's conclude this abstraction by comparing thanks to a friend of ours
698:16 comparing thanks to a friend of ours Professor Shannon Duval of Elon
698:18 Professor Shannon Duval of Elon University who kindly put together this
698:20 University who kindly put together this graphical animation that's just under 2
698:22 graphical animation that's just under 2 minutes long that paints a picture of
698:23 minutes long that paints a picture of these two types of abstract data
698:25 these two types of abstract data structures and then we'll dive in
698:26 structures and then we'll dive in underneath to how we might Implement uh
698:29 underneath to how we might Implement uh problems like these if we could dim the
698:30 problems like these if we could dim the lights
698:32 lights [Music]
698:38 dramatically once upon a time there was a guy named Jack when it came to making
698:41 a guy named Jack when it came to making friends Jack did not have the Knack so
698:43 friends Jack did not have the Knack so Jack went to talk to the most popular
698:45 Jack went to talk to the most popular guy he knew he went up to Lou and asked
698:48 guy he knew he went up to Lou and asked what do I do Lou saw that his friend was
698:50 what do I do Lou saw that his friend was really distressed well Lou began just
698:53 really distressed well Lou began just look how you're dressed don't you have
698:54 look how you're dressed don't you have any clothes with a different look yeah
698:57 any clothes with a different look yeah yes said Jack I sure do come to my house
698:59 yes said Jack I sure do come to my house and I'll show them to you so they went
699:02 and I'll show them to you so they went off to Jacks and Jack showed Lou the box
699:04 off to Jacks and Jack showed Lou the box where he kept all his shirts and his
699:06 where he kept all his shirts and his pants and his socks L said I see you
699:08 pants and his socks L said I see you have all your clothes in a pile why
699:10 have all your clothes in a pile why don't you wear some others once in a
699:12 don't you wear some others once in a while Jack said well when I remove
699:14 while Jack said well when I remove clothes and socks I wash them and put
699:17 clothes and socks I wash them and put them away in the box then comes the next
699:19 them away in the box then comes the next morning and up I hop I go to the box and
699:22 morning and up I hop I go to the box and get my clothes off the top Lou quickly
699:25 get my clothes off the top Lou quickly realized the problem with Jack he kept
699:27 realized the problem with Jack he kept clothes CDs and books in a stack when he
699:30 clothes CDs and books in a stack when he reached for something to read or to wear
699:32 reached for something to read or to wear he chose the top book or underwear then
699:35 he chose the top book or underwear then when he was done he would put it right
699:37 when he was done he would put it right back back it would go on top of the
699:39 back back it would go on top of the stack I know the solution said a
699:41 stack I know the solution said a triumphant Lou you need to learn to
699:43 triumphant Lou you need to learn to start using a queue Lou took Jack's
699:46 start using a queue Lou took Jack's clothes and hung them in a closet and
699:48 clothes and hung them in a closet and when he had emptied the box he just
699:49 when he had emptied the box he just tossed it then he said now Jack at the
699:52 tossed it then he said now Jack at the end of the day put your clothes on the
699:54 end of the day put your clothes on the left when you put them away then
699:56 left when you put them away then tomorrow morning morning when you see
699:57 tomorrow morning morning when you see the sun shine get your clothes from the
699:59 the sun shine get your clothes from the right from the end of the line don't you
700:02 right from the end of the line don't you see said Lou it will be so nice you'll
700:04 see said Lou it will be so nice you'll wear everything once before you wear
700:06 wear everything once before you wear something twice and with everything in
700:08 something twice and with everything in cues in his closet and shelf Jack
700:11 cues in his closet and shelf Jack started to feel quite sure of himself
700:13 started to feel quite sure of himself all thanks to Lou and his wonderful
700:24 Q all right so sure so that paints a picture of these two abstract data
700:25 picture of these two abstract data structures but if we really were to dive
700:27 structures but if we really were to dive underneath the hood we could Implement
700:28 underneath the hood we could Implement them in a number of different ways but
700:30 them in a number of different ways but we really I think need some building
700:32 we really I think need some building blocks via which we could solve problems
700:33 blocks via which we could solve problems like those and we'll see today to some
700:35 like those and we'll see today to some others as well so let's rewind back to
700:37 others as well so let's rewind back to week two where we imple we introduced
700:40 week two where we imple we introduced you to your very first data structure
700:41 you to your very first data structure that is an array and an array of recall
700:43 that is an array and an array of recall was just a chunk of memory whereby
700:46 was just a chunk of memory whereby elements were stored by Design back to
700:48 elements were stored by Design back to back to back it's an array of contiguous
700:51 back to back it's an array of contiguous memory specifically so with an array we
700:54 memory specifically so with an array we could certainly store not just one thing
700:56 could certainly store not just one thing but two or three or even more and so for
700:58 but two or three or even more and so for instance if we treat uh my computer's
701:01 instance if we treat uh my computer's memory as this sort of abstraction here
701:02 memory as this sort of abstraction here and pictured here are three bytes or
701:04 and pictured here are three bytes or some uh uh multiplication thereof
701:06 some uh uh multiplication thereof suppose we're storing in the computer's
701:08 suppose we're storing in the computer's memory an array of size three storing
701:10 memory an array of size three storing the digits one two and three well
701:13 the digits one two and three well remember that if we zoom out per last
701:15 remember that if we zoom out per last week there's other stuff going on in
701:17 week there's other stuff going on in memory so even if we want to add another
701:19 memory so even if we want to add another number to this array that we didn't
701:21 number to this array that we didn't think of when we first started the
701:23 think of when we first started the program like the number four ideally we
701:25 program like the number four ideally we would put it right here next to it
701:27 would put it right here next to it otherwise it's no longer an array so by
701:29 otherwise it's no longer an array so by definition if we're using an array it's
701:30 definition if we're using an array it's got to end up right there after the
701:32 got to end up right there after the three but what else is going on inside
701:34 three but what else is going on inside of your computer's memory well assuming
701:36 of your computer's memory well assuming your program is of any length and you've
701:38 your program is of any length and you've got other variables other functions
701:40 got other variables other functions you've been running it for a while
701:41 you've been running it for a while there's a lot going on in your memory is
701:43 there's a lot going on in your memory is being used and reused so for instance
701:45 being used and reused so for instance somewhere in memory might be immediately
701:47 somewhere in memory might be immediately adjacent to this like hello comma world
701:50 adjacent to this like hello comma world back0 the null character just because
701:52 back0 the null character just because maybe you have another variable
701:54 maybe you have another variable somewhere in there that is storing that
701:56 somewhere in there that is storing that particular string alongside your
701:57 particular string alongside your existing array of size three and all of
701:59 existing array of size three and all of these Oscar the grouches here really
702:01 these Oscar the grouches here really just represent what we called last week
702:03 just represent what we called last week garbage values like there's obviously
702:05 garbage values like there's obviously bits there because they don't disappear
702:06 bits there because they don't disappear they're always going to be inside of the
702:08 they're always going to be inside of the computer somehow implemented but we
702:10 computer somehow implemented but we don't really know or care what they are
702:11 don't really know or care what they are they're the remnants of those bytes
702:13 they're the remnants of those bytes having been used for other older
702:14 having been used for other older variables previous function calls or the
702:17 variables previous function calls or the like but the problem clearly here is
702:19 like but the problem clearly here is that okay 1 two 3 is there but the H is
702:21 that okay 1 two 3 is there but the H is here and unless I want to start uh
702:23 here and unless I want to start uh taking a bite out of my string by
702:25 taking a bite out of my string by overriding the H with a four like we
702:28 overriding the H with a four like we just can't fit it right there and yet
702:30 just can't fit it right there and yet even though there's Oscars all over the
702:32 even though there's Oscars all over the place those are indeed garbage values
702:33 place those are indeed garbage values and therefore we could use that space
702:35 and therefore we could use that space because it's technically unused we just
702:37 because it's technically unused we just don't know or care what the values are
702:39 don't know or care what the values are so where could I put 1 2 3 4 well my
702:42 so where could I put 1 2 3 4 well my gosh like I have all this memory down
702:44 gosh like I have all this memory down here that's unused I could certainly
702:46 here that's unused I could certainly change those garbage values to be 1 2 3
702:49 change those garbage values to be 1 2 3 4 but to do that I might need to do a
702:53 4 but to do that I might need to do a bit of work here right it's not just a
702:55 bit of work here right it's not just a matter of just saying boom and it
702:57 matter of just saying boom and it happens now with c and with code I'd
703:00 happens now with c and with code I'd have to do this a little more
703:01 have to do this a little more methodically so let me abstract away
703:02 methodically so let me abstract away everything else that's a distraction let
703:04 everything else that's a distraction let me assume that there is indeed at least
703:06 me assume that there is indeed at least four bytes available for four numbers
703:08 four bytes available for four numbers just down here though we could have put
703:09 just down here though we could have put them in a bunch of different spots
703:11 them in a bunch of different spots what's involved now in moving the one
703:14 what's involved now in moving the one two three to this new chunk of memory so
703:17 two three to this new chunk of memory so we can add the four well I think
703:19 we can add the four well I think conceptually we're going to have to copy
703:21 conceptually we're going to have to copy the one from old to new copy the two
703:23 the one from old to new copy the two from old to new copy the three from old
703:25 from old to new copy the three from old to new and then ultimately we can get
703:28 to new and then ultimately we can get rid of the old memory those three
703:29 rid of the old memory those three original bites could now look like Oscar
703:31 original bites could now look like Oscar the Grouch and just be garbage values
703:33 the Grouch and just be garbage values for all intents and purposes but now I
703:35 for all intents and purposes but now I have room for a fourth bite wherein I
703:38 have room for a fourth bite wherein I can put the number four so this is nice
703:41 can put the number four so this is nice but what's a downside of this
703:44 but what's a downside of this approach what's a downside of solving
703:47 approach what's a downside of solving the problem in this way where the
703:48 the problem in this way where the problem at hand is just to grow the
703:50 problem at hand is just to grow the array so to speak to increase its size
703:53 array so to speak to increase its size to fit one or more
703:55 to fit one or more numbers seems pretty
703:58 numbers seems pretty straightforward but
704:06 yeah okay maybe it's out of order but I think that's okay because the order is
704:08 think that's okay because the order is just matters that it's relative so so
704:09 just matters that it's relative so so long it's it's still contiguous back to
704:11 long it's it's still contiguous back to back to back in a different chunk of
704:12 back to back in a different chunk of memory I think we're okay there it's not
704:14 memory I think we're okay there it's not like I changed it for 4 3 2 one but a
704:17 like I changed it for 4 3 2 one but a reasonable hunch
704:24 yeah yeah like I don't really plan ahead here like if I have to add another
704:25 here like if I have to add another number like five or anything else well I
704:27 number like five or anything else well I might have to jump through these hoops
704:29 might have to jump through these hoops again maybe I get lucky and maybe
704:30 again maybe I get lucky and maybe there's space there but not if I have
704:32 there's space there but not if I have other variables and other things going
704:33 other variables and other things going on that too might be used at some point
704:36 on that too might be used at some point other thoughts yeah slow efficiency slow
704:39 other thoughts yeah slow efficiency slow effic in slow efficiency why have
704:43 effic in slow efficiency why have to again yeah I mean it's just in it's
704:45 to again yeah I mean it's just in it's just inefficient it's sort of bad design
704:48 just inefficient it's sort of bad design arguably why because I had to copy all
704:51 arguably why because I had to copy all of my original work down here and as you
704:53 of my original work down here and as you note if I want to add a fifth number I'm
704:55 note if I want to add a fifth number I'm going to have to copy it again and again
704:57 going to have to copy it again and again and again and do things end times again
705:00 and again and do things end times again and again now maybe that's necessary
705:02 and again now maybe that's necessary we'll soon see for sure but it feels
705:04 we'll soon see for sure but it feels like this is not going to end well
705:06 like this is not going to end well especially if the array isn't of size
705:07 especially if the array isn't of size three or four but 300 400 your computer
705:10 three or four but 300 400 your computer ends up spending so much time just
705:12 ends up spending so much time just spinning its Wheels I mean honestly
705:14 spinning its Wheels I mean honestly better might be this like if this is my
705:16 better might be this like if this is my same array physically incarnated Now 1
705:18 same array physically incarnated Now 1 two 3 it's literally on the edge of the
705:20 two 3 it's literally on the edge of the shelf so there's no room for the number
705:23 shelf so there's no room for the number four you know maybe where we could take
705:25 four you know maybe where we could take this story is well let's just find room
705:27 this story is well let's just find room for the four like let's just put the
705:29 for the four like let's just put the four for instance over here replacing
705:31 four for instance over here replacing some available garbage value some spare
705:33 some available garbage value some spare bite over here but now wait a minute
705:36 bite over here but now wait a minute I've broken the definition of an array
705:38 I've broken the definition of an array right it's I can't have one two three
705:40 right it's I can't have one two three and then four over here so maybe there
705:42 and then four over here so maybe there maybe there could be a mechanism if I
705:44 maybe there could be a mechanism if I put this thing on again where when you
705:46 put this thing on again where when you get to the end of the existing elements
705:48 get to the end of the existing elements maybe I just somehow digitally point to
705:51 maybe I just somehow digitally point to the fourth array and maybe we can kind
705:53 the fourth array and maybe we can kind of stitch together all of these
705:54 of stitch together all of these different values in memory so that if
705:56 different values in memory so that if you follow the arrows so to speak we can
705:58 you follow the arrows so to speak we can reconstruct exactly what the order is
706:01 reconstruct exactly what the order is even without having to find or make room
706:03 even without having to find or make room here or pick up all of these numbers and
706:05 here or pick up all of these numbers and move all of them over there so that's
706:07 move all of them over there so that's perhaps the direction in which we'll go
706:09 perhaps the direction in which we'll go here so let's see how we might get to
706:12 here so let's see how we might get to that spot as follows let me go ahead and
706:15 that spot as follows let me go ahead and open up say VSS code here let me open up
706:17 open up say VSS code here let me open up a program called list. C in my terminal
706:21 a program called list. C in my terminal and let me go ahead and whip up a
706:23 and let me go ahead and whip up a relatively simple program that just
706:25 relatively simple program that just demonstrates what we did back in week
706:27 demonstrates what we did back in week two when we introduced arrays as follows
706:29 two when we introduced arrays as follows let me include uh standard io. so we can
706:32 let me include uh standard io. so we can print stuff out let me do int main void
706:35 print stuff out let me do int main void no command line Arguments for now let me
706:37 no command line Arguments for now let me give myself a an array uh called list of
706:42 give myself a an array uh called list of size three and I'll just hardcode it to
706:44 size three and I'll just hardcode it to keep it simple for lecture sake Each of
706:46 keep it simple for lecture sake Each of which is going to be an integer and now
706:48 which is going to be an integer and now just so we have some specifics to talk
706:49 just so we have some specifics to talk about let me put it list bracket zero
706:51 about let me put it list bracket zero the number one list bracket one the
706:53 the number one list bracket one the number two and list bracket two the
706:55 number two and list bracket two the number three so I'm just translating
706:57 number three so I'm just translating into codee what we just had uh
706:59 into codee what we just had uh pictorially on the screen and also
707:01 pictorially on the screen and also physically here with these numbers on
707:03 physically here with these numbers on the desk now let's just do something
707:05 the desk now let's just do something mildly useful for this how about we do
707:07 mildly useful for this how about we do four in I gets zero I is less than three
707:10 four in I gets zero I is less than three I ++ let's just print each of these
707:13 I ++ let's just print each of these numbers out just to make sure they're
707:14 numbers out just to make sure they're indeed in memory as I intended so
707:16 indeed in memory as I intended so percent I back sln comma I and then a
707:19 percent I back sln comma I and then a semicolon and I think that's it for now
707:21 semicolon and I think that's it for now so nothing interesting no problem solved
707:24 so nothing interesting no problem solved just yet just a proof of concept so that
707:25 just yet just a proof of concept so that now when I clear my terminal and run
707:27 now when I clear my terminal and run make
707:28 make list no apparent errors at the terminal
707:32 list no apparent errors at the terminal and so when I now do do/ list I should
707:34 and so when I now do do/ list I should see hopefully from left to right 1 two 3
707:37 see hopefully from left to right 1 two 3 but of course if I want to add a fourth
707:39 but of course if I want to add a fourth number now there's no mechanism for such
707:42 number now there's no mechanism for such certainly in the code that I just wrote
707:44 certainly in the code that I just wrote I could go back in here and change this
707:46 I could go back in here and change this to a four I could go down here and
707:48 to a four I could go down here and change lists bracket 3 equals 4 I could
707:51 change lists bracket 3 equals 4 I could just manually change the code recompile
707:53 just manually change the code recompile the code but of course that doesn't give
707:55 the code but of course that doesn't give me any additional run way for the fifth
707:56 me any additional run way for the fifth or sixth number so let me try to take a
707:59 or sixth number so let me try to take a different approach drawing some
708:00 different approach drawing some inspiration from last week if I want to
708:04 inspiration from last week if I want to allocate memory dynamically maybe
708:06 allocate memory dynamically maybe because I don't know when I wrote the
708:08 because I don't know when I wrote the program how many bytes I want we have
708:10 program how many bytes I want we have another function as of last week that
708:12 another function as of last week that does not require that you commit in
708:14 does not require that you commit in advance to a certain number of bytes via
708:16 advance to a certain number of bytes via what function can you just ask the
708:17 what function can you just ask the operating system for a chunk of memory
708:20 operating system for a chunk of memory okay so Malo to allocate memory now an
708:22 okay so Malo to allocate memory now an array is just a chunk of memory and even
708:24 array is just a chunk of memory and even though since week two 've been using
708:26 though since week two 've been using this syntactic sugar this convenience of
708:29 this syntactic sugar this convenience of just using square brackets and indexing
708:31 just using square brackets and indexing into it it's just making it easier to
708:33 into it it's just making it easier to manipulate a chunk of memory that's
708:35 manipulate a chunk of memory that's contiguous all together back to back to
708:37 contiguous all together back to back to back so today just like last week we can
708:39 back so today just like last week we can take those sort of training wheels off
708:41 take those sort of training wheels off and maybe be a little more deliberate in
708:43 and maybe be a little more deliberate in how we allocate memory let me go for
708:45 how we allocate memory let me go for instance and do this let me delete my
708:48 instance and do this let me delete my contents of my main function here go
708:51 contents of my main function here go back into Main and let me propose now
708:53 back into Main and let me propose now that I declare for instance how about my
708:57 that I declare for instance how about my list no longer as an array but as a
708:59 list no longer as an array but as a pointer so int star list and I'm going
709:02 pointer so int star list and I'm going to go ahead and initialize this to be
709:05 to go ahead and initialize this to be how about a chunk of three integers for
709:07 how about a chunk of three integers for now so I'm still going to hardcode it
709:09 now so I'm still going to hardcode it but I'm taking a step toward more
709:10 but I'm taking a step toward more dynamism for now so let me allocate
709:13 dynamism for now so let me allocate three times whatever the size is of an
709:15 three times whatever the size is of an INT but it's usually going to be four
709:17 INT but it's usually going to be four bytes as we know so this is really going
709:19 bytes as we know so this is really going to be 3 * 4al 12 but it's a little more
709:21 to be 3 * 4al 12 but it's a little more Dynamic and now what can I do down here
709:24 Dynamic and now what can I do down here well this is just a chunk of memory
709:26 well this is just a chunk of memory so I can do literally list bracket 0 uh
709:30 so I can do literally list bracket 0 uh equal 1 list bracket 1 = 2 list bracket
709:35 equal 1 list bracket 1 = 2 list bracket 2 equals 3 and voila achieve the exact
709:39 2 equals 3 and voila achieve the exact same effect because again an array is
709:41 same effect because again an array is just a chunk of contiguous memory but
709:43 just a chunk of contiguous memory but malok gives you any old chunk of
709:46 malok gives you any old chunk of contiguous memory so you can rather
709:48 contiguous memory so you can rather treat one like the other here now if you
709:50 treat one like the other here now if you really want to be cool you could do
709:52 really want to be cool you could do something like this instead you could
709:54 something like this instead you could dreference the address in list and go
709:57 dreference the address in list and go there you could go down here and
709:58 there you could go down here and dreference list + one and go there but
710:02 dreference list + one and go there but honestly no one really writes code like
710:04 honestly no one really writes code like this it's just too cryptic it's a little
710:06 this it's just too cryptic it's a little too far over the line at least for most
710:07 too far over the line at least for most people and so I think the syntactic
710:09 people and so I think the syntactic sugar as I keep describing it just the
710:11 sugar as I keep describing it just the more user friendly square bracket
710:13 more user friendly square bracket notation does the exact same thing
710:14 notation does the exact same thing figures out the pointer arithmetic and
710:16 figures out the pointer arithmetic and puts each of these integers in the right
710:18 puts each of these integers in the right chunks therein now just to be super
710:21 chunks therein now just to be super pedantic let me make sure if something
710:23 pedantic let me make sure if something went wrong so if list equals equals n
710:26 went wrong so if list equals equals n that means that something went wrong
710:28 that means that something went wrong like my computer is out of memory which
710:29 like my computer is out of memory which we should check for typically so let me
710:31 we should check for typically so let me just immediately return one signaling
710:34 just immediately return one signaling anything other than zero which means
710:36 anything other than zero which means success typically just to get out of
710:37 success typically just to get out of this program because something's wrong
710:39 this program because something's wrong but now let me propose that I've had a
710:42 but now let me propose that I've had a uh well let's do this for in I gets zero
710:45 uh well let's do this for in I gets zero I less than three i++ though a better
710:48 I less than three i++ though a better design would always be to use a con but
710:50 design would always be to use a con but I'm just doing this for demonstration
710:52 I'm just doing this for demonstration sake let's print out each of these ins
710:54 sake let's print out each of these ins too and just make sure I didn't mess
710:55 too and just make sure I didn't mess anything up and let me open my terminal
710:58 anything up and let me open my terminal window again let me do make list
711:01 window again let me do make list again okay huh implicitly declaring
711:04 again okay huh implicitly declaring Library function Malo with type void
711:07 Library function Malo with type void star something something implicitly de
711:10 star something something implicitly de declaring is the operative words there
711:12 declaring is the operative words there what did I mess up
711:16 what did I mess up yeah yeah I forgot the header file in
711:18 yeah yeah I forgot the header file in which malok is declared I remember now
711:21 which malok is declared I remember now okay that's in standard li. and it's
711:23 okay that's in standard li. and it's fine to look stuff like that up if you
711:25 fine to look stuff like that up if you forget so let me include standard libh
711:28 forget so let me include standard libh now let me clear my terminal run make
711:30 now let me clear my terminal run make list again okay now we're good /list and
711:34 list again okay now we're good /list and now what did I do
711:38 now what did I do [Music]
711:40 [Music] wrong oh okay not intended but teachable
711:43 wrong oh okay not intended but teachable moment what did I do wrong
711:47 moment what did I do wrong yeah yeah I'm printing the values of i
711:49 yeah yeah I'm printing the values of i instead of what is at location I in the
711:52 instead of what is at location I in the array so what I actually meant to do was
711:54 array so what I actually meant to do was print this out thank you you so now let
711:56 print this out thank you you so now let me recompile make
711:58 me recompile make listlist and now okay those are the
712:00 listlist and now okay those are the three values I was expecting not the
712:02 three values I was expecting not the indices thereof now let me propose that
712:05 indices thereof now let me propose that for the sake of discussion that I I
712:07 for the sake of discussion that I I regret having only allocated space for
712:09 regret having only allocated space for three integers and maybe I really should
712:12 three integers and maybe I really should have allocated enough space for four now
712:14 have allocated enough space for four now this is not how you would do this in
712:15 this is not how you would do this in practice because presumably if you have
712:17 practice because presumably if you have a change of thought just go back in and
712:19 a change of thought just go back in and correct the code but let me propose that
712:21 correct the code but let me propose that somewhere in here is a more complicated
712:23 somewhere in here is a more complicated program and time passes dot dot dot
712:25 program and time passes dot dot dot there's a lot of other interesting code
712:26 there's a lot of other interesting code there but at some point I might want to
712:29 there but at some point I might want to give myself more memory so how can I do
712:31 give myself more memory so how can I do this well let me just ask the operating
712:33 this well let me just ask the operating system now for four new bytes of memory
712:35 system now for four new bytes of memory so that we can at least in version one
712:37 so that we can at least in version one implement the ID on the board where I
712:38 implement the ID on the board where I just copied the three bytes into the new
712:41 just copied the three bytes into the new four bytes and then added a fourth value
712:43 four bytes and then added a fourth value so I'm going to use malok again and I'm
712:45 so I'm going to use malok again and I'm going to say here's a new pointer I'll
712:47 going to say here's a new pointer I'll call it temp TMP for short which is
712:49 call it temp TMP for short which is quite common when you just need it
712:51 quite common when you just need it briefly I'm going to then call malok
712:53 briefly I'm going to then call malok again I'm going to say give me four
712:55 again I'm going to say give me four integers using size of let me again make
712:58 integers using size of let me again make sure so if temp equals equals null
713:02 sure so if temp equals equals null something went wrong so let me just
713:03 something went wrong so let me just immediately return one and for good
713:06 immediately return one and for good measure before I return one one let me
713:09 measure before I return one one let me free the original list so that I don't
713:11 free the original list so that I don't leak memory so I'm not just immediately
713:13 leak memory so I'm not just immediately returning one I'm being a good citizen
713:15 returning one I'm being a good citizen and remembering well if this Malo call
713:18 and remembering well if this Malo call did succeed and indeed I got as far as
713:21 did succeed and indeed I got as far as line 18 but then line 18 failed I should
713:25 line 18 but then line 18 failed I should free the memory that I previously macked
713:27 free the memory that I previously macked so again that's the rule of thumb if you
713:29 so again that's the rule of thumb if you allocate it you should be the one to
713:30 allocate it you should be the one to free it even before you're about to quit
713:33 free it even before you're about to quit now once I've done that I think I need
713:34 now once I've done that I think I need to do what we did pictorially on the
713:36 to do what we did pictorially on the screen where I need to copy the One the
713:38 screen where I need to copy the One the two the three from the old array into
713:41 two the three from the old array into the new so how might I do this well let
713:43 the new so how might I do this well let me give myself a loop so for in I gets
713:46 me give myself a loop so for in I gets zero I less than three i++ because the
713:49 zero I less than three i++ because the size of the original is still the same
713:51 size of the original is still the same let me go ahead and treat the new chunk
713:53 let me go ahead and treat the new chunk of memory called 10 temp as an array
713:57 of memory called 10 temp as an array itself and so I can absolutely use these
713:59 itself and so I can absolutely use these square brackets just like before it's
714:00 square brackets just like before it's just a chunk of memory I'm treating it
714:02 just a chunk of memory I'm treating it like an array and let me add to that
714:04 like an array and let me add to that value whatever is at the original list
714:06 value whatever is at the original list at location I as well so this again is
714:09 at location I as well so this again is just this exercise of copying from right
714:12 just this exercise of copying from right uh from old to new step by step the one
714:15 uh from old to new step by step the one the two and the three but I still need
714:18 the two and the three but I still need one additional step if my goal at hand
714:20 one additional step if my goal at hand now is to have ultimately a fourth value
714:23 now is to have ultimately a fourth value here well I'm just going to hard coose
714:25 here well I'm just going to hard coose this for demonstration sake and I'm
714:26 this for demonstration sake and I'm going to go to the very last location of
714:29 going to go to the very last location of temp which is of size four which means
714:32 temp which is of size four which means the last element in temp is temp
714:35 the last element in temp is temp bracket three because it's zero indexed
714:38 bracket three because it's zero indexed but there's four total spaces there and
714:40 but there's four total spaces there and I'm just going to arbitrarily for the
714:42 I'm just going to arbitrarily for the sake of discussion put the number four
714:43 sake of discussion put the number four there and that is what happened when we
714:47 there and that is what happened when we proposed uh changing the final garbage
714:50 proposed uh changing the final garbage value there to that four but now I need
714:52 value there to that four but now I need to do what the slide did for us sort of
714:54 to do what the slide did for us sort of magically on the screen I should now do
714:56 magically on the screen I should now do a couple of final things I should free
714:58 a couple of final things I should free the original list which I've not done
715:00 the original list which I've not done yet CU I only called free earlier in
715:02 yet CU I only called free earlier in cases of error and that was just to be
715:04 cases of error and that was just to be safe I can now free the list and now if
715:08 safe I can now free the list and now if I want to inform the computer that I
715:10 I want to inform the computer that I want list quote unquote my variable
715:14 want list quote unquote my variable called list to point at not the old
715:16 called list to point at not the old chunk like it originally did but the new
715:18 chunk like it originally did but the new chunk I think I can just do this list
715:21 chunk I think I can just do this list equals
715:22 equals TMP and again that's just saying that if
715:25 TMP and again that's just saying that if list is a pointer which it was cuz look
715:27 list is a pointer which it was cuz look at the very top line here on line eight
715:29 at the very top line here on line eight on line six I declared list to be a
715:33 on line six I declared list to be a pointer uh to a chunk of memory temp
715:36 pointer uh to a chunk of memory temp meanwhile is a separate pointer to a
715:38 meanwhile is a separate pointer to a chunk of memory so down here this line
715:41 chunk of memory so down here this line 33 is just a matter of my saying okay
715:44 33 is just a matter of my saying okay now henceforth because I've already
715:45 now henceforth because I've already freed the old chunk of memory my list
715:47 freed the old chunk of memory my list variable should Point not at this chunk
715:49 variable should Point not at this chunk of three bytes but this chunk of four
715:52 of three bytes but this chunk of four bytes or really 12 in total now uh or
715:55 bytes or really 12 in total now uh or rather 16 now because we have four such
715:58 rather 16 now because we have four such bytes questions now on this code the
716:02 bytes questions now on this code the point of which was quite simply to
716:05 point of which was quite simply to demonstrate how we could Implement and
716:06 demonstrate how we could Implement and code this idea of Fairly correctly but
716:09 code this idea of Fairly correctly but inefficiently allocating a new array of
716:12 inefficiently allocating a new array of sufficient size and then populating it
716:14 sufficient size and then populating it with a new fourth
716:16 with a new fourth value questions on what we've just done
716:21 value questions on what we've just done here no
716:24 here no yeah
716:33 [Music] good question at this point in the story
716:35 good question at this point in the story with line 33 do I not have two different
716:38 with line 33 do I not have two different variables pointing at the same chunk of
716:40 variables pointing at the same chunk of memory short answer yes but here's where
716:42 memory short answer yes but here's where the semantics are perhaps compelling
716:44 the semantics are perhaps compelling list is the variable that I intend to
716:46 list is the variable that I intend to use longer term and keep around in
716:47 use longer term and keep around in memory and again assume that there's
716:49 memory and again assume that there's even more code going on here that we
716:51 even more code going on here that we just didn't write yet so it's useful to
716:52 just didn't write yet so it's useful to have that that variable temp was just
716:54 have that that variable temp was just kind of a necessary evil because up here
716:58 kind of a necessary evil because up here it would not have been correct to do
717:00 it would not have been correct to do this it would not have been correct to
717:02 this it would not have been correct to say list on line 18 equals the new chunk
717:05 say list on line 18 equals the new chunk of memory because this would have
717:07 of memory because this would have represented a memory leak if I pre uh
717:10 represented a memory leak if I pre uh prematurely change temp to point not at
717:13 prematurely change temp to point not at the old chunk but the new chunk at that
717:15 the old chunk but the new chunk at that point no one's pointing at the old chunk
717:17 point no one's pointing at the old chunk and so I've lost those three bites vren
717:19 and so I've lost those three bites vren for instance would yell at you for
717:21 for instance would yell at you for having lost as many btes in memory so in
717:24 having lost as many btes in memory so in this case here I do leave this as temp
717:27 this case here I do leave this as temp yes it's duplicative at this point but
717:29 yes it's duplicative at this point but it's it's not a huge deal if it was just
717:30 it's it's not a huge deal if it was just meant semantically to be a temporary
717:32 meant semantically to be a temporary value but down here at the risk of one
717:35 value but down here at the risk of one more line of code I still want to to be
717:38 more line of code I still want to to be a good citizen free list and maybe just
717:40 a good citizen free list and maybe just for good measure return zero explicitly
717:43 for good measure return zero explicitly but notice it's not doing it twice per
717:46 but notice it's not doing it twice per se on line 31 what am I freeing the
717:50 se on line 31 what am I freeing the original address of list the three
717:53 original address of list the three integer version then I change what list
717:56 integer version then I change what list points at so it's pointing at a
717:57 points at so it's pointing at a completely different chunk of memory
717:59 completely different chunk of memory this one of size four So eventually when
718:01 this one of size four So eventually when I'm all done using this memory for this
718:03 I'm all done using this memory for this demonstration I still need to free list
718:05 demonstration I still need to free list but at this point in the story line 40
718:07 but at this point in the story line 40 it's pointing at the new chunk of memory
718:09 it's pointing at the new chunk of memory which I similarly need to hand back to
718:11 which I similarly need to hand back to the operating system by free
718:19 yeah when would temp equal null so let me scroll back up slightly this is being
718:22 me scroll back up slightly this is being a good citizen and a good programmer
718:24 a good citizen and a good programmer whenever it comes to using malok malok
718:27 whenever it comes to using malok malok can return null if the computer's out of
718:30 can return null if the computer's out of memory so this is maybe a much bigger
718:32 memory so this is maybe a much bigger program you've got other things going on
718:33 program you've got other things going on in it and so you just don't have enough
718:35 in it and so you just don't have enough memory available to be handed malok
718:38 memory available to be handed malok needs to signal to you that there's some
718:39 needs to signal to you that there's some error and so it will by convention per
718:42 error and so it will by convention per the documentation per the manual pages
718:44 the documentation per the manual pages return null so this is just me being a
718:46 return null so this is just me being a good citizen otherwise here's another
718:48 good citizen otherwise here's another error that might cause your program to
718:50 error that might cause your program to crash with a segmentation fault if you
718:52 crash with a segmentation fault if you get back null but you assume that it's
718:54 get back null but you assume that it's good memory uh going to address zero AKA
718:57 good memory uh going to address zero AKA null will crash your program
719:00 null will crash your program intentionally
719:02 intentionally [Music]
719:10 yeah correct if I were to change my final line 40 here to be free temp this
719:15 final line 40 here to be free temp this would also work as well and here this is
719:18 would also work as well and here this is really a matter of design it's a very
719:20 really a matter of design it's a very nitpicky thing we could probably debate
719:22 nitpicky thing we could probably debate it but because at this point in the
719:24 it but because at this point in the story my main variable for remembering
719:27 story my main variable for remembering where the list is is called list this is
719:29 where the list is is called list this is sort of the more responsible way to do
719:31 sort of the more responsible way to do it freeing the list just so that my
719:32 it freeing the list just so that my colleagues my ta doesn't sort of wonder
719:35 colleagues my ta doesn't sort of wonder why are you freeing temporary ver memory
719:37 why are you freeing temporary ver memory that you already freed like it just is a
719:38 that you already freed like it just is a semantic thing at this point but good
719:40 semantic thing at this point but good Instinct it would also work correct
719:42 Instinct it would also work correct maybe just not good design all right so
719:45 maybe just not good design all right so it turns out that this gets annoying
719:46 it turns out that this gets annoying quickly as it did in the picture of
719:48 quickly as it did in the picture of doing all of this duplication and even
719:50 doing all of this duplication and even though technically it's necessary to
719:52 though technically it's necessary to copy those values if you need a newer
719:54 copy those values if you need a newer bigger chunk of memory
719:55 bigger chunk of memory there is at least a function in C that
719:58 there is at least a function in C that simplifies a lot of this for us and in
720:00 simplifies a lot of this for us and in fact let me go ahead and do this instead
720:03 fact let me go ahead and do this instead of using malok this second time on line
720:06 of using malok this second time on line 18 in addition to the first time I used
720:09 18 in addition to the first time I used it on line six I'm actually going to try
720:11 it on line six I'm actually going to try and introduce another function called
720:13 and introduce another function called realloc which as the name suggests tries
720:15 realloc which as the name suggests tries to reallocate memory for you and it
720:18 to reallocate memory for you and it works a little differently from malok
720:20 works a little differently from malok realloc expects two arguments the first
720:23 realloc expects two arguments the first one is what is the chunk of memory that
720:25 one is what is the chunk of memory that you want to try to grow or Shrink that
720:27 you want to try to grow or Shrink that is reallocate to be a different size and
720:30 is reallocate to be a different size and then you specify what size you would
720:32 then you specify what size you would want and indeed in this case I want four
720:35 want and indeed in this case I want four times size of int and that will now give
720:39 times size of int and that will now give me hopefully a new address of a chunk of
720:42 me hopefully a new address of a chunk of memory that's big enough to fit all four
720:45 memory that's big enough to fit all four numbers but what's wonderful about
720:48 numbers but what's wonderful about realloc is that it will handle all of
720:51 realloc is that it will handle all of the copying for me so in fact I'm going
720:53 the copying for me so in fact I'm going to go down here I'm going to get rid of
720:56 to go down here I'm going to get rid of all of this this extra for Loop and what
720:59 all of this this extra for Loop and what I'm simply going to do instead is this
721:02 I'm simply going to do instead is this once I can
721:03 once I can trust after lines 18 through 23 that
721:07 trust after lines 18 through 23 that rioc worked and it didn't return null
721:09 rioc worked and it didn't return null because I'm out of memory I can just say
721:11 because I'm out of memory I can just say okay just immediately remember that the
721:13 okay just immediately remember that the new list points at this new chunk of
721:16 new list points at this new chunk of memory instead and then I can still now
721:18 memory instead and then I can still now do this line but I can tweak the
721:20 do this line but I can tweak the semantics here and just say list bracket
721:22 semantics here and just say list bracket 3 the new Final a in uh um the new list
721:28 3 the new Final a in uh um the new list is for I don't need to free this here I
721:31 is for I don't need to free this here I don't need to do this all I need now at
721:33 don't need to do this all I need now at the bottom is the final for Loop to just
721:35 the bottom is the final for Loop to just print out these values so in short even
721:37 print out these values so in short even though that was somewhat quick using
721:39 though that was somewhat quick using realloc just moves the entire copying
721:43 realloc just moves the entire copying process that I implemented myself a
721:44 process that I implemented myself a moment ago using a for Loop it just
721:46 moment ago using a for Loop it just moves it to realloc and lets it deal
721:48 moves it to realloc and lets it deal with the copying for me it's no more
721:50 with the copying for me it's no more efficient but at least means uh I'm
721:52 efficient but at least means uh I'm writing less code which is more pleasant
721:53 writing less code which is more pleasant and hopefully the people who wrote aloc
721:55 and hopefully the people who wrote aloc or realloc are smarter than me and they
721:57 or realloc are smarter than me and they just will introduce bugs with lower
721:59 just will introduce bugs with lower probability
722:00 probability too all right that was a lot any
722:04 too all right that was a lot any [Music]
722:09 questions good question why do you still need to make list equal temp as I did on
722:12 need to make list equal temp as I did on line 24 so ideally I would do this
722:17 line 24 so ideally I would do this ideally I would just change this line 18
722:21 ideally I would just change this line 18 to be list that is to say call re or
722:23 to be list that is to say call re or actually even better ideally I would
722:25 actually even better ideally I would just say realloc this list to be of this
722:27 just say realloc this list to be of this new size but again things can go wrong
722:29 new size but again things can go wrong when allocating memory you need to check
722:31 when allocating memory you need to check a return value to see if it was
722:33 a return value to see if it was successful or not and so we need to use
722:36 successful or not and so we need to use a return value okay so let's not
722:37 a return value okay so let's not introduce temp let's just use list but
722:40 introduce temp let's just use list but here's where a memory leak might happen
722:42 here's where a memory leak might happen in the off chance realloc fails and
722:46 in the off chance realloc fails and doesn't have enough memory for your four
722:48 doesn't have enough memory for your four bytes therefore it returns by definition
722:50 bytes therefore it returns by definition null you can't overwrite the original
722:53 null you can't overwrite the original value of list with null
722:55 value of list with null to then check it why because now who
722:58 to then check it why because now who remembers where the original Three byes
722:59 remembers where the original Three byes were if you prematurely change the value
723:02 were if you prematurely change the value of list you've lost you've uh leaked
723:04 of list you've lost you've uh leaked memory in that sense and so that's why
723:06 memory in that sense and so that's why let me undo this change I declare a
723:09 let me undo this change I declare a temporary pointer for the sole purpose
723:12 temporary pointer for the sole purpose of making sure I can check the return
723:13 of making sure I can check the return value and then once it's good now I'll
723:16 value and then once it's good now I'll update the value of list so it's sort of
723:18 update the value of list so it's sort of doing a Switcheroo by making sure first
723:20 doing a Switcheroo by making sure first that you have a new value to swap with
723:23 that you have a new value to swap with the old other question
723:26 the old other question on this code
723:35 yeah indeed realloc automatically frees the previous memory for you and better
723:37 the previous memory for you and better yet it's even smarter than that if you
723:39 yet it's even smarter than that if you get lucky and they happens to be space
723:42 get lucky and they happens to be space right after your existing chunk of
723:43 right after your existing chunk of memory so one two three garbage value
723:46 memory so one two three garbage value instead of one two three hello world
723:48 instead of one two three hello world realloc won't even bother copying things
723:51 realloc won't even bother copying things from old to new it will just say okay
723:53 from old to new it will just say okay I'm going to now reserve for you more
723:55 I'm going to now reserve for you more bites than you originally asked for so
723:57 bites than you originally asked for so it doesn't have to waste time doing that
723:59 it doesn't have to waste time doing that copying and so in that sense this
724:00 copying and so in that sense this version is now not only still correct
724:02 version is now not only still correct it's even better designed because we're
724:04 it's even better designed because we're not wasting time with that for Loop we
724:06 not wasting time with that for Loop we might have to resort to it if there is
724:07 might have to resort to it if there is in fact hello world or something else in
724:09 in fact hello world or something else in the way but hopefully we'll get lucky
724:11 the way but hopefully we'll get lucky and save those steps other questions on
724:14 and save those steps other questions on this this manipulation of code
724:18 this this manipulation of code here yeah in the
724:26 middle what if you want to resize a
724:27 what if you want to resize a two-dimensional array so very similar in
724:30 two-dimensional array so very similar in Spirit uh whereby you can use the same
724:33 Spirit uh whereby you can use the same trickery let me wave my hand at that for
724:35 trickery let me wave my hand at that for now just because I think that's going to
724:37 now just because I think that's going to sort of significantly increase the
724:38 sort of significantly increase the complexity but very same Primitives
724:40 complexity but very same Primitives ultimately a two-dimensional array is
724:42 ultimately a two-dimensional array is essentially just a doubly long or
724:45 essentially just a doubly long or quadratically longer list of memory that
724:47 quadratically longer list of memory that using square bracket notation is doing
724:49 using square bracket notation is doing some of that mental math for you but
724:51 some of that mental math for you but it's fundamentally no different of
724:53 it's fundamentally no different of what's going on underneath the
724:55 what's going on underneath the hood all right so with that said and
724:59 hood all right so with that said and that code under our belt even though
725:01 that code under our belt even though that's not going to be something you'll
725:02 that's not going to be something you'll frequently need to code yourself let's
725:04 frequently need to code yourself let's propose now how we might go about
725:07 propose now how we might go about building some actual data structures
725:09 building some actual data structures ourselves the new ingredient here being
725:12 ourselves the new ingredient here being this reality that if you want to resize
725:15 this reality that if you want to resize a chunk of memory so is to make room for
725:17 a chunk of memory so is to make room for things we now have that ability memory
725:19 things we now have that ability memory addresses and pointers just give us the
725:21 addresses and pointers just give us the ability to like Point around at things
725:23 ability to like Point around at things and move things around in memory but now
725:25 and move things around in memory but now that we have malok and even realloc you
725:27 that we have malok and even realloc you can imagine maybe rewinding and you
725:29 can imagine maybe rewinding and you could Implement that stack that Q using
725:32 could Implement that stack that Q using not an array per se because you have to
725:34 not an array per se because you have to commit to an array size in advance but
725:36 commit to an array size in advance but if you implement your stack or your que
725:38 if you implement your stack or your que using a pointer and then malok and
725:40 using a pointer and then malok and realloc and maybe someone else writes
725:42 realloc and maybe someone else writes all that code for you perhaps now you
725:44 all that code for you perhaps now you can imagine that okay now the stack can
725:47 can imagine that okay now the stack can grow or Shrink by using realloc
725:50 grow or Shrink by using realloc accordingly you don't have to preact pre
725:52 accordingly you don't have to preact pre um preemptively say give me five bytes
725:54 um preemptively say give me five bytes or 50 or 500 or 5,000 you can say just
725:57 or 50 or 500 or 5,000 you can say just give me one initially and if I need more
725:59 give me one initially and if I need more I'll realloc realloc reloc and if you
726:01 I'll realloc realloc reloc and if you keep popping things off the stack you
726:03 keep popping things off the stack you can realloc in the other direction and
726:05 can realloc in the other direction and ask for fewer and fewer bytes and the
726:07 ask for fewer and fewer bytes and the operating system can take that memory
726:08 operating system can take that memory back as well so we now have this
726:10 back as well so we now have this building block let's see what we can do
726:12 building block let's see what we can do with it so we've had a few pieces of
726:14 with it so we've had a few pieces of syntax in recent weeks all of which
726:16 syntax in recent weeks all of which we're going to combine now in just a
726:17 we're going to combine now in just a slightly more clever way so struct is
726:20 slightly more clever way so struct is this keyword in C that lets us build our
726:22 this keyword in C that lets us build our own structure in memory like a
726:23 own structure in memory like a collection of two or three or more
726:25 collection of two or three or more variables like a person that we've seen
726:27 variables like a person that we've seen before the dot operator recall we've
726:31 before the dot operator recall we've used when you do have a struct like a a
726:33 used when you do have a struct like a a person and you want to go inside of it
726:36 person and you want to go inside of it so like uh person. name or person.
726:39 so like uh person. name or person. number we did this a few weeks ago now
726:42 number we did this a few weeks ago now but the dot Operator just allows you to
726:43 but the dot Operator just allows you to go inside of a structure and get the
726:45 go inside of a structure and get the individual variables within and then the
726:47 individual variables within and then the star operator unfortunately has a lot of
726:50 star operator unfortunately has a lot of uses now one was multiplication like my
726:52 uses now one was multiplication like my God that was easy back in the day now
726:54 God that was easy back in the day now it's used to declare pointers it's also
726:57 it's used to declare pointers it's also used to dreference pointers so to make
726:59 used to dreference pointers so to make one exist and then go to that address
727:02 one exist and then go to that address unfortunately it's the same symbol for
727:04 unfortunately it's the same symbol for all of those but it's all related but
727:06 all of those but it's all related but with these three symbols it turns out
727:08 with these three symbols it turns out you're going to get one last one today
727:10 you're going to get one last one today and my God it finally looks like the
727:12 and my God it finally looks like the concept it turns out there's a clever
727:15 concept it turns out there's a clever way anytime you want to use the dot and
727:17 way anytime you want to use the dot and the star together that is to go
727:19 the star together that is to go somewhere and go to an address and then
727:21 somewhere and go to an address and then look inside of a structure you can
727:23 look inside of a structure you can actually literally use an arrow symbol
727:25 actually literally use an arrow symbol on your keyboard it's not a single
727:27 on your keyboard it's not a single keystroke it's a hyphen and then an open
727:30 keystroke it's a hyphen and then an open angle bracket but at least it looks like
727:32 angle bracket but at least it looks like an arrow and we'll see indeed in code
727:33 an arrow and we'll see indeed in code today the things I was drawing
727:35 today the things I was drawing pictorially on the screen last time with
727:36 pictorially on the screen last time with yellow arrows you can actually now
727:38 yellow arrows you can actually now Express as well in code and so here we
727:41 Express as well in code and so here we have our next data structure called a
727:44 have our next data structure called a linked list and this is one of the most
727:46 linked list and this is one of the most useful powerful Concepts in C it's the
727:48 useful powerful Concepts in C it's the kind of thing that you can take for
727:50 kind of thing that you can take for granted in Java and Python and higher
727:52 granted in Java and Python and higher level languages but today we'll see how
727:54 level languages but today we'll see how we or others can actually build these
727:56 we or others can actually build these things just using these same Primitives
727:59 things just using these same Primitives so a linked list is going to allow us to
728:01 so a linked list is going to allow us to actually do what we you know used a foam
728:04 actually do what we you know used a foam finger for last week allow us to link
728:06 finger for last week allow us to link together for instance these three values
728:07 together for instance these three values maybe with that fourth value over there
728:09 maybe with that fourth value over there and then if there's a fifth you know
728:11 and then if there's a fifth you know maybe this other foam finger points even
728:13 maybe this other foam finger points even farther overway to that fifth value the
728:15 farther overway to that fifth value the key being that you can stitch together
728:17 key being that you can stitch together fancier data structures without having
728:19 fancier data structures without having to like pick all of these up and find
728:21 to like pick all of these up and find new space you just have to at least
728:23 new space you just have to at least connect the dots somehow we just need to
728:25 connect the dots somehow we just need to somehow point from one to the other and
728:27 somehow point from one to the other and that's going to make things much more
728:29 that's going to make things much more efficient it would seem so how do we get
728:31 efficient it would seem so how do we get there so here's my computer's memory as
728:33 there so here's my computer's memory as always suppose that I'm storing the
728:35 always suppose that I'm storing the value one somewhere in there and it's at
728:37 value one somewhere in there and it's at ox123 address whatever and I'm storing
728:40 ox123 address whatever and I'm storing the number two somewhere else in memory
728:41 the number two somewhere else in memory Ox 456 and number three at address Ox
728:45 Ox 456 and number three at address Ox 789 this is not an array by definition
728:49 789 this is not an array by definition why even though it's the only three
728:51 why even though it's the only three things on the screen what makes this not
728:53 things on the screen what makes this not an array it's not contiguous so this
728:56 an array it's not contiguous so this violates the definition of an array but
728:58 violates the definition of an array but you know especially since they're
729:00 you know especially since they're sequential it kind of looks to a human
729:02 sequential it kind of looks to a human like a list so it would be nice if there
729:04 like a list so it would be nice if there were a data type called list and there
729:06 were a data type called list and there isn't in C there will be in Python but
729:09 isn't in C there will be in Python but you know what if I could somehow like
729:11 you know what if I could somehow like Stitch together these three values so I
729:13 Stitch together these three values so I can get from one to the next to the next
729:15 can get from one to the next to the next then I think we could achieve the idea
729:17 then I think we could achieve the idea the concept of a list without this
729:19 the concept of a list without this really annoying constraint that they all
729:21 really annoying constraint that they all be contiguous as in an array so how do I
729:24 be contiguous as in an array so how do I do that well at the end of the day I
729:26 do that well at the end of the day I only have memory at my disposal right
729:29 only have memory at my disposal right there's no more training wheels to take
729:31 there's no more training wheels to take off here this is what we've got
729:33 off here this is what we've got underneath the hood of a computer so if
729:35 underneath the hood of a computer so if all I have is memory I think the
729:37 all I have is memory I think the solution to this problem of stitching
729:39 solution to this problem of stitching together those values in a list must be
729:42 together those values in a list must be to spend a bit more memory that's
729:44 to spend a bit more memory that's literally the only resource we have
729:46 literally the only resource we have right now so let me propose that if we
729:48 right now so let me propose that if we want to create a list conceptually out
729:50 want to create a list conceptually out of three values that are in random
729:52 of three values that are in random although pictorially pretty positions in
729:55 although pictorially pretty positions in memory let me just add a little bit more
729:57 memory let me just add a little bit more memory to the picture so in addition to
729:59 memory to the picture so in addition to storing the one I'm going to leave my
730:00 storing the one I'm going to leave my space myself some room a little scratch
730:02 space myself some room a little scratch pad if you will to use some other bits
730:04 pad if you will to use some other bits as well same for the two same for the
730:06 as well same for the two same for the three and you can perhaps see where this
730:09 three and you can perhaps see where this is going based on last week if I want to
730:12 is going based on last week if I want to somehow connect the one to the two any
730:17 somehow connect the one to the two any instincts as to what I should write in
730:20 instincts as to what I should write in this box here that would lead me
730:22 this box here that would lead me effectively from one to the two
730:25 effectively from one to the two what could go here
730:32 yeah we could store the address of Two And so specifically what would you have
730:33 And so specifically what would you have me write
730:40 here perfect ideally I would just put in this box another integer one that
730:43 this box another integer one that happens to be represented in heximal but
730:45 happens to be represented in heximal but that's just a base system it's just a
730:46 that's just a base system it's just a human thing for us to look at I'm going
730:48 human thing for us to look at I'm going to put the value Ox 456 here so let me
730:51 to put the value Ox 456 here so let me go ahead and reveal that Ox 456 goes
730:54 go ahead and reveal that Ox 456 goes there you can perhaps see further where
730:55 there you can perhaps see further where this is going well if I want to get from
730:57 this is going well if I want to get from the two to the three I think I need to
730:59 the two to the three I think I need to put below the two the address of the
731:01 put below the two the address of the three which gives me Ox 789 now if three
731:04 three which gives me Ox 789 now if three is the end of the list I don't want to
731:06 is the end of the list I don't want to let it be some garbage value because
731:08 let it be some garbage value because that would imply that it who knows where
731:10 that would imply that it who knows where it's pointing I need some definitive
731:12 it's pointing I need some definitive value and just what would your instincts
731:13 value and just what would your instincts be if I want to make clear with some
731:16 be if I want to make clear with some special Sentinel value that the buck
731:18 special Sentinel value that the buck stops here what do I put what my my
731:21 stops here what do I put what my my options be yeah so null not n per se but
731:24 options be yeah so null not n per se but n l which was the new keyword we
731:27 n l which was the new keyword we introduced last week which just
731:29 introduced last week which just represents an empty pointer if you will
731:32 represents an empty pointer if you will technically the address o x0 so
731:35 technically the address o x0 so literally the zero address and what
731:37 literally the zero address and what humans did years ago they just decided
731:39 humans did years ago they just decided you know what nothing should ever live
731:41 you know what nothing should ever live at address zero in memory we're just
731:43 at address zero in memory we're just going to reserve that one special bite
731:45 going to reserve that one special bite to be a special signal a sentinel value
731:47 to be a special signal a sentinel value such that if you ever see a zero address
731:50 such that if you ever see a zero address in a pointer it just means it's it's
731:52 in a pointer it just means it's it's invalid it does not exist now now we
731:54 invalid it does not exist now now we write that though a little more
731:55 write that though a little more pleasantly for the eyes as just n l in
731:58 pleasantly for the eyes as just n l in all caps and that's a key word in C as
732:00 all caps and that's a key word in C as well but of course last week I claimed
732:01 well but of course last week I claimed that who cares where things are in
732:03 that who cares where things are in memory and honestly like this quickly
732:05 memory and honestly like this quickly gets tedious even worrying about these
732:07 gets tedious even worrying about these values so let me abstract this away and
732:09 values so let me abstract this away and propose that if we want to remember
732:11 propose that if we want to remember where all of these numbers are in memory
732:14 where all of these numbers are in memory let's give oursel one final piece of
732:16 let's give oursel one final piece of memory that just allows us to start the
732:19 memory that just allows us to start the whole process let me allocate on the
732:21 whole process let me allocate on the left hand side here not room for a
732:23 left hand side here not room for a number like 1 two 3 just room for a
732:26 number like 1 two 3 just room for a pointer that henceforth I think I'll
732:28 pointer that henceforth I think I'll call list by convention and then store
732:31 call list by convention and then store in that one additional pointer a value
732:34 in that one additional pointer a value that just kickstarts the whole process
732:36 that just kickstarts the whole process this is the sort of treasure map if you
732:38 this is the sort of treasure map if you will that you get handed and this has
732:40 will that you get handed and this has the address of the very first actual
732:43 the address of the very first actual node in memory now technically we could
732:46 node in memory now technically we could just start with this but it turns out
732:47 just start with this but it turns out we'll see it's just a little cleaner to
732:49 we'll see it's just a little cleaner to use a simple single pointer that leads
732:52 use a simple single pointer that leads to the things you care about as opposed
732:53 to the things you care about as opposed to just starting with the first element
732:55 to just starting with the first element why well if you ever want to get rid of
732:57 why well if you ever want to get rid of this element it'd be nice if you could
732:59 this element it'd be nice if you could at least still hang on to an empty sheet
733:00 at least still hang on to an empty sheet of paper that indicates that the list is
733:02 of paper that indicates that the list is empty would be one argument for that so
733:05 empty would be one argument for that so again who cares about these addresses
733:06 again who cares about these addresses now now with the wave of the hand let's
733:09 now now with the wave of the hand let's just abstract it away and there are our
733:11 just abstract it away and there are our pointers each of those addresses in the
733:13 pointers each of those addresses in the rec uh the squares at the bottom are
733:16 rec uh the squares at the bottom are simply pointing to the next element in
733:18 simply pointing to the next element in the list the jargon to introduce here
733:21 the list the jargon to introduce here would be that now that we have these
733:22 would be that now that we have these integers 1 2 3 but they're in these like
733:25 integers 1 2 3 but they're in these like wrappers if you will these structures
733:27 wrappers if you will these structures that have metadata that is additional
733:29 that have metadata that is additional data that is related to but not the data
733:32 data that is related to but not the data you actually care about this is data
733:34 you actually care about this is data this is metadata this thing here
733:36 this is metadata this thing here rectangularly we'll call a node n o and
733:39 rectangularly we'll call a node n o and it's just a term of art that means it's
733:40 it's just a term of art that means it's like a container in code for storing
733:43 like a container in code for storing some
733:44 some values this then is a linked list and
733:47 values this then is a linked list and this then is the sort of graphical
733:50 this then is the sort of graphical incarnation of like one node pointing to
733:52 incarnation of like one node pointing to the other in this case case they happen
733:54 the other in this case case they happen to be by chance and by design of this
733:56 to be by chance and by design of this desk contiguous initially but there's no
733:59 desk contiguous initially but there's no requirement that they be such the one
734:00 requirement that they be such the one could be over there the two over there
734:02 could be over there the two over there the three over there I would just need
734:03 the three over there I would just need more foam fingers to point at one to the
734:05 more foam fingers to point at one to the next questions on this concept of a
734:08 next questions on this concept of a linked
734:10 linked list yeah and
734:16 [Music] back can you say that
734:25 again a good question do traditional arrays start with a pointer that's
734:27 arrays start with a pointer that's outside of the structure short answer no
734:29 outside of the structure short answer no arrays are special in C and certain
734:32 arrays are special in C and certain other languages and the name of an array
734:34 other languages and the name of an array is technically a symbol if you will that
734:37 is technically a symbol if you will that the computer the program knows maps to a
734:40 the computer the program knows maps to a specific location in memory it's just a
734:42 specific location in memory it's just a label a synonym for a memory address it
734:45 label a synonym for a memory address it does not take up space so to be clear
734:48 does not take up space so to be clear the name of an array does not take up
734:50 the name of an array does not take up space like that extra Square on the left
734:52 space like that extra Square on the left but you do need that extra Square on the
734:54 but you do need that extra Square on the left when implementing a link list so
734:56 left when implementing a link list so that you can determine if the list is of
734:58 that you can determine if the list is of size zero there's nothing being pointed
735:00 size zero there's nothing being pointed at or size three in this case we're sort
735:03 at or size three in this case we're sort of taking on more responsibility
735:05 of taking on more responsibility ourselves
735:14 yeah how do you point to the next element can you
735:16 element can you [Music]
735:22 elaborate ah good question if each of these elements is pointing to the next
735:23 these elements is pointing to the next how is three point to the others short
735:25 how is three point to the others short answer it doesn't at least in this
735:27 answer it doesn't at least in this design we have more technically what's
735:29 design we have more technically what's called a singly linked list and as the
735:32 called a singly linked list and as the arrows imply it only goes in One
735:34 arrows imply it only goes in One Direction so if you somehow find incode
735:36 Direction so if you somehow find incode maybe a for Loop maybe a while loop
735:38 maybe a for Loop maybe a while loop somehow you're sort of encode over here
735:40 somehow you're sort of encode over here you have no way in code to go backwards
735:43 you have no way in code to go backwards unless we changed this to a doubly link
735:45 unless we changed this to a doubly link list where I add another box that lets
735:49 list where I add another box that lets me have arrows in both directions or
735:51 me have arrows in both directions or maybe I just kind of make it uh uh
735:53 maybe I just kind of make it uh uh circular and I connect the three back to
735:56 circular and I connect the three back to the one which you can totally do but
735:58 the one which you can totally do but that tends to you know make life harder
736:00 that tends to you know make life harder because now you have to figure out when
736:01 because now you have to figure out when you're stuck in a loop in your data
736:02 you're stuck in a loop in your data structure but it's doable as well but as
736:04 structure but it's doable as well but as is it's a dead end by Design other
736:08 is it's a dead end by Design other questions on this design
736:14 here all right well how might we implement this structure in code well
736:15 implement this structure in code well let me just connect the dots to
736:17 let me just connect the dots to something like we've seen before here
736:19 something like we've seen before here like this is how a couple of weeks ago
736:21 like this is how a couple of weeks ago we introduced the notion of a a person
736:23 we introduced the notion of a a person and we claimed a person might have a
736:25 and we claimed a person might have a name and a number last week of course we
736:27 name and a number last week of course we took off some of these training wheels
736:28 took off some of these training wheels and a string is really technically a
736:30 and a string is really technically a Char star in both cases but really
736:32 Char star in both cases but really there's no conceptual difference beyond
736:34 there's no conceptual difference beyond that but let's use this same Paradigm to
736:37 that but let's use this same Paradigm to implement a node as I described it in
736:40 implement a node as I described it in that picture so let me get rid of the
736:42 that picture so let me get rid of the name and the number because that's
736:43 name and the number because that's related only to a person and let me
736:45 related only to a person and let me rename this structure for discussion
736:47 rename this structure for discussion sake to node that then invites the
736:49 sake to node that then invites the question well what needs to go inside of
736:52 question well what needs to go inside of a node well minimally an
736:55 a node well minimally an integer but this is now where we need to
736:58 integer but this is now where we need to think a little harder just conceptually
737:00 think a little harder just conceptually even if you have no idea how to type it
737:02 even if you have no idea how to type it at the keyboard what else needs to be
737:04 at the keyboard what else needs to be part of a node based on these
737:06 part of a node based on these rectangular pictures that we've
737:08 rectangular pictures that we've drawn what more do we need
737:12 drawn what more do we need yeah yeah we need a pointer to another
737:15 yeah yeah we need a pointer to another node so if I don't know how to implement
737:17 node so if I don't know how to implement this yet you know it could be something
737:20 this yet you know it could be something like you know pointer to another node
737:22 like you know pointer to another node how do I do that well you know what but
737:23 how do I do that well you know what but it turns out you would ideally say this
737:26 it turns out you would ideally say this if you know that the next node is itself
737:30 if you know that the next node is itself a node by definition well anytime we've
737:33 a node by definition well anytime we've needed a pointer we just use the data
737:34 needed a pointer we just use the data type and a star and I'm going to
737:36 type and a star and I'm going to arbitrarily but I think reasonably call
737:38 arbitrarily but I think reasonably call this second square at the bottom of
737:41 this second square at the bottom of those rectangles next as the name of my
737:44 those rectangles next as the name of my attribute here but node star just
737:46 attribute here but node star just connotes that the next variable is going
737:49 connotes that the next variable is going to be not a node per se but the address
737:51 to be not a node per se but the address of a node and that's exactly what we did
737:53 of a node and that's exactly what we did you had me put Ox 456 Ox 789 in that box
737:57 you had me put Ox 456 Ox 789 in that box which is the address of another node so
737:59 which is the address of another node so the way we would Express this in code
738:01 the way we would Express this in code would be node star next but we could
738:04 would be node star next but we could call the variable anything we want now
738:07 call the variable anything we want now this is a bit of a white lie but we'll
738:09 this is a bit of a white lie but we'll fix this right now this code won't
738:11 fix this right now this code won't actually compile C takes you pretty
738:13 actually compile C takes you pretty literally recall and if you use some
738:16 literally recall and if you use some term at the top of your file that you
738:18 term at the top of your file that you don't Define until later in your file
738:20 don't Define until later in your file you're going to see some error message
738:22 you're going to see some error message right we've seen this when I've messed
738:23 right we've seen this when I've messed up and forgot to include the function
738:25 up and forgot to include the function prototypes at the top of my code this is
738:27 prototypes at the top of my code this is related in spirit I seem here on my 1
738:29 related in spirit I seem here on my 1 two 3 fourth line of code I'm trying to
738:31 two 3 fourth line of code I'm trying to use this new term of art that I invented
738:33 use this new term of art that I invented here in my code called node even though
738:35 here in my code called node even though it's a CS term as well but nowhere Above
738:39 it's a CS term as well but nowhere Above This it would seem did I even Define
738:40 This it would seem did I even Define what a node is it's not a data type and
738:42 what a node is it's not a data type and C every computer scientist know what a
738:44 C every computer scientist know what a node is but it doesn't come for free
738:45 node is but it doesn't come for free with the language so I need to do
738:48 with the language so I need to do something else like I need this word
738:50 something else like I need this word here to come first so that I can use it
738:53 here to come first so that I can use it here and so we have this sort of Catch
738:54 here and so we have this sort of Catch 22 like how can a structure be
738:58 22 like how can a structure be self-referential that is point to
739:00 self-referential that is point to another version of itself if the word
739:02 another version of itself if the word doesn't yet exist so the solution to
739:04 doesn't yet exist so the solution to this in C which we didn't need for a
739:06 this in C which we didn't need for a person because there was no notion of
739:09 person because there was no notion of listing connecting as a list we need one
739:11 listing connecting as a list we need one more keyword here that we didn't need
739:13 more keyword here that we didn't need for a person and we reuse that keyword
739:16 for a person and we reuse that keyword here so kind of an annoying detail but
739:18 here so kind of an annoying detail but if we preemptively call this whole thing
739:21 if we preemptively call this whole thing struct node you can now refer to the
739:24 struct node you can now refer to the thing on the inside as a struct node
739:26 thing on the inside as a struct node star but then you can shorten the name
739:29 star but then you can shorten the name of the whole thing from struct node to
739:32 of the whole thing from struct node to just node sort of an annoying sequence
739:34 just node sort of an annoying sequence of steps but in short anytime you're
739:35 of steps but in short anytime you're building a node a linked list in memory
739:38 building a node a linked list in memory this is just the Paradigm you use type
739:40 this is just the Paradigm you use type def struct the name of the thing you
739:41 def struct the name of the thing you want to Define like node you use that
739:44 want to Define like node you use that name on the inside if you want to point
739:46 name on the inside if you want to point from one to another and then you can
739:47 from one to another and then you can shorten it down here to just be called
739:51 shorten it down here to just be called node questions then on this code
739:55 node questions then on this code here questions on what we just did well
740:00 here questions on what we just did well if I rewind just a moment to that final
740:03 if I rewind just a moment to that final picture what would be the upside to be
740:05 picture what would be the upside to be clear of having jumped through these
740:06 clear of having jumped through these hoops and added this complexity if you
740:10 hoops and added this complexity if you will what problem did we just solve by
740:12 will what problem did we just solve by linking together these three values to
740:14 linking together these three values to be clear
740:15 be clear [Music]
740:17 [Music] yeah making lists that
740:20 yeah making lists that are that are not contiguous if you will
740:23 are that are not contiguous if you will so making lists that are not contiguous
740:25 so making lists that are not contiguous in memory the upside of which is that if
740:27 in memory the upside of which is that if I want to add the number four to this
740:29 I want to add the number four to this list it looks like I could choose from
740:31 list it looks like I could choose from any chunks of available memory on the
740:33 any chunks of available memory on the screen I just need to sort of point from
740:35 screen I just need to sort of point from the end of the current list to wherever
740:38 the end of the current list to wherever that other one is in memory what I don't
740:39 that other one is in memory what I don't need to do to be clear is copy the One
740:42 need to do to be clear is copy the One the two or the three everything can just
740:44 the two or the three everything can just stay put which means TimeWise I can do
740:47 stay put which means TimeWise I can do this much more quickly it would seem
740:48 this much more quickly it would seem without copying things again and again
740:50 without copying things again and again and even without using realloc to let it
740:52 and even without using realloc to let it do all of the copying potentially for me
740:54 do all of the copying potentially for me all right but as we'll start seeing even
740:56 all right but as we'll start seeing even more in the coming weeks every time we
740:58 more in the coming weeks every time we benefit and solve some problem we pay a
741:00 benefit and solve some problem we pay a price there's a tradeoff what is a
741:03 price there's a tradeoff what is a downside as you might perceive now of
741:05 downside as you might perceive now of using a linked list instead of an array
741:09 using a linked list instead of an array yeah as much memory yeah I mean we use
741:12 yeah as much memory yeah I mean we use twice as much memory because now in
741:13 twice as much memory because now in addition to storing the integers one two
741:15 addition to storing the integers one two three I also need to store a pointer for
741:17 three I also need to store a pointer for each of those and honestly even this
741:19 each of those and honestly even this picture is a bit of uh simplification
741:21 picture is a bit of uh simplification technically in most systems today each
741:23 technically in most systems today each int would be four bytes technically
741:25 int would be four bytes technically today most pointers though would be
741:27 today most pointers though would be eight bytes I just didn't want to draw
741:28 eight bytes I just didn't want to draw this weird shape on the board where the
741:30 this weird shape on the board where the bottom square is even bigger than the
741:31 bottom square is even bigger than the top square but technically we're using
741:34 top square but technically we're using even more than twice as much space for
741:36 even more than twice as much space for these pointers so there's that trade-off
741:37 these pointers so there's that trade-off now thankfully decades after SE was
741:39 now thankfully decades after SE was invented memory is generally much
741:41 invented memory is generally much cheaper nowadays and so it's okay to
741:43 cheaper nowadays and so it's okay to sort of spend more of it if you need to
741:45 sort of spend more of it if you need to and it depends on what you want to
741:46 and it depends on what you want to optimize for but that's absolutely here
741:48 optimize for but that's absolutely here a downside what's another downside of
741:50 a downside what's another downside of having transitioned to in a uh link list
741:58 you can't index into it now I haven't even tried in code but when you have a
742:00 even tried in code but when you have a linked list you can no longer use square
742:03 linked list you can no longer use square bracket notation because why well square
742:06 bracket notation because why well square bracket notation just assumes the
742:08 bracket notation just assumes the contiguousness of memory location zero
742:10 contiguousness of memory location zero is here location one is literally one to
742:12 is here location one is literally one to the right location two is literally one
742:14 the right location two is literally one to the right one to the right these
742:16 to the right one to the right these things even though I've drawn it from
742:17 things even though I've drawn it from right to left to just keep things pretty
742:19 right to left to just keep things pretty there are gaps here and this is just my
742:22 there are gaps here and this is just my interpretation of this these gaps could
742:23 interpretation of this these gaps could be big they could be narrow they could
742:25 be big they could be narrow they could be down here up here they could be
742:27 be down here up here they could be anywhere so long as we're linking things
742:29 anywhere so long as we're linking things together in this list the computer can't
742:31 together in this list the computer can't just use bracket zero bracket 1 bracket
742:33 just use bracket zero bracket 1 bracket two anymore because it can't do simple
742:35 two anymore because it can't do simple arithmetic and jump to like the middle
742:37 arithmetic and jump to like the middle and now here's perhaps the worst price
742:39 and now here's perhaps the worst price we've paid if you don't have square
742:41 we've paid if you don't have square bracket notation or really you don't
742:43 bracket notation or really you don't have
742:44 have contiguousness what algorithm did we
742:46 contiguousness what algorithm did we just sacrifice for this
742:48 just sacrifice for this dynamism if you rewind even back to week
742:52 dynamism if you rewind even back to week zero and we gave it a name in week
742:56 zero and we gave it a name in week three what algorithm can we not use now
742:58 three what algorithm can we not use now if we can't assume that the memory is
743:00 if we can't assume that the memory is back to back to back to
743:02 back to back to back to back binary search why because binary
743:06 back binary search why because binary search just like the phone book back in
743:08 search just like the phone book back in the first week requires being able to
743:10 the first week requires being able to arithmetically jump right to the middle
743:12 arithmetically jump right to the middle right take the total length of it divide
743:14 right take the total length of it divide by two and boom you're right there in
743:16 by two and boom you're right there in the Middle with some simple arithmetic
743:18 the Middle with some simple arithmetic here they might be laid out again with
743:20 here they might be laid out again with these big or small gaps there's no
743:23 these big or small gaps there's no simple math I can do to just jump
743:24 simple math I can do to just jump immediately to the one in the middle and
743:26 immediately to the one in the middle and in fact again if this TV were bigger the
743:28 in fact again if this TV were bigger the two could technically be in memory be
743:29 two could technically be in memory be way down here or even way over here the
743:32 way down here or even way over here the foam finger could be pointing in any
743:33 foam finger could be pointing in any number of directions depending on where
743:35 number of directions depending on where malok put the thing there's just no way
743:37 malok put the thing there's just no way to do binary search and so it would seem
743:39 to do binary search and so it would seem that we've paid another price indeed in
743:41 that we've paid another price indeed in terms of it performance we're now
743:43 terms of it performance we're now talking about linear time again so
743:46 talking about linear time again so that's a regression now that's also a
743:48 that's a regression now that's also a lot things like feels like a good time
743:50 lot things like feels like a good time for some muffins and fruit out in the
743:51 for some muffins and fruit out in the lobby and when we come back we'll try to
743:53 lobby and when we come back we'll try to solve the problem we just created so see
743:55 solve the problem we just created so see you in 10 so we are back and let's see
743:58 you in 10 so we are back and let's see if we can't now take some of these
743:59 if we can't now take some of these higher level concepts of like stitching
744:01 higher level concepts of like stitching together these nodes in memory and
744:03 together these nodes in memory and translate it to some actual code but
744:05 translate it to some actual code but we'll do it step by step first before I
744:07 we'll do it step by step first before I actually start writing it in vs code so
744:08 actually start writing it in vs code so if Carter you wouldn't mind helping me
744:09 if Carter you wouldn't mind helping me step through with some visuals let me
744:12 step through with some visuals let me propose that line by line we solve some
744:14 propose that line by line we solve some of the problems that we've just created
744:15 of the problems that we've just created for ourselves in building this thing in
744:17 for ourselves in building this thing in memory so let's go ahead and first
744:20 memory so let's go ahead and first consider how we could build a length
744:23 consider how we could build a length list containing the numbers indeed one
744:25 list containing the numbers indeed one then two then three and let's translate
744:27 then two then three and let's translate each of those steps to code and then
744:28 each of those steps to code and then we'll put it all together into something
744:29 we'll put it all together into something that actually runs so how about first
744:31 that actually runs so how about first step here will just be this to declare a
744:34 step here will just be this to declare a pointer called list that's initially has
744:37 pointer called list that's initially has no value at least at this point in the
744:38 no value at least at this point in the story list is the name of the variable
744:40 story list is the name of the variable node star just means that this is
744:42 node star just means that this is essentially going to be our little
744:44 essentially going to be our little square over here that points to the
744:46 square over here that points to the beginning of the list of course it's
744:48 beginning of the list of course it's ideal If It ultimately has a value
744:50 ideal If It ultimately has a value because when we initially I'll call this
744:53 because when we initially I'll call this line of code it just gives us indeed
744:55 line of code it just gives us indeed that square over here on the left but
744:57 that square over here on the left but it's got a garbage value because there's
744:58 it's got a garbage value because there's no equal sign on the other side there so
745:01 no equal sign on the other side there so let's propose that we do one more step
745:02 let's propose that we do one more step here and actually initialize it to null
745:05 here and actually initialize it to null so that if only we know that it's not
745:08 so that if only we know that it's not garbage it at least has some known value
745:10 garbage it at least has some known value and null is a good way of signifying
745:12 and null is a good way of signifying that at this point in the story The List
745:13 that at this point in the story The List is empty indeed null indicates there's
745:16 is empty indeed null indicates there's no nodes in the list so that picture
745:18 no nodes in the list so that picture would now look like this whereby let's
745:20 would now look like this whereby let's just draw instead of writing null
745:21 just draw instead of writing null everywhere I'll just leave the squares
745:22 everywhere I'll just leave the squares blank when it's not a garbage value per
745:24 blank when it's not a garbage value per se it's literally Ox Z or null all right
745:27 se it's literally Ox Z or null all right so that's it for building a link list of
745:29 so that's it for building a link list of size zero like we're sort of done then
745:32 size zero like we're sort of done then but we want to now add a one and then a
745:33 but we want to now add a one and then a two then a three so next step here might
745:35 two then a three so next step here might be this if I want to allocate the first
745:38 be this if I want to allocate the first of my rectangles on our previous picture
745:41 of my rectangles on our previous picture I'm going to call malok and I'm going to
745:42 I'm going to call malok and I'm going to ask for enough memory to fit a whole
745:45 ask for enough memory to fit a whole node now technically I think that's
745:47 node now technically I think that's going to be like four bytes for the int
745:48 going to be like four bytes for the int and eight bytes for the pointer even
745:50 and eight bytes for the pointer even though I did not draw it to scale on the
745:52 though I did not draw it to scale on the board so that's technically going to be
745:54 board so that's technically going to be what 12 bytes but again size of node
745:56 what 12 bytes but again size of node just figures out how many bytes I
745:58 just figures out how many bytes I actually need dynamically that's going
746:00 actually need dynamically that's going to return to me the address of that
746:02 to return to me the address of that chunk of memory which apparently I'm
746:03 chunk of memory which apparently I'm going to store inside of a temporary
746:05 going to store inside of a temporary variable called n for short for node but
746:08 variable called n for short for node but let's see what this does pictorially so
746:10 let's see what this does pictorially so when this line of code is executed I
746:12 when this line of code is executed I first get on the left that variable n
746:15 first get on the left that variable n it's got a garbage value by default
746:17 it's got a garbage value by default because I haven't executed the whole
746:19 because I haven't executed the whole thing from right to left meanwhile on
746:20 thing from right to left meanwhile on the right hand side of the expression
746:22 the right hand side of the expression I've got now a node somewhere in memory
746:24 I've got now a node somewhere in memory it happened to be free here this is
746:25 it happened to be free here this is where malok put it for me but it does
746:27 where malok put it for me but it does have two garbage values initially but
746:29 have two garbage values initially but because it's a node per my type def
746:32 because it's a node per my type def earlier every node I proposed is going
746:34 earlier every node I proposed is going to have a number and a next pointer so
746:37 to have a number and a next pointer so we can see those labeled here but
746:39 we can see those labeled here but they've got two garbage values initially
746:40 they've got two garbage values initially but all I care about initially is that
746:42 but all I care about initially is that ultimately n is pointing at that chunk
746:46 ultimately n is pointing at that chunk of code so initially if we could back up
746:47 of code so initially if we could back up two
746:48 two steps we have two steps so we have initi
746:53 steps we have two steps so we have initi one step forward we have this line of
746:55 one step forward we have this line of code gives us this variable here which
746:58 code gives us this variable here which has garbage when this side of the
747:00 has garbage when this side of the expression is executed that allocates
747:02 expression is executed that allocates the memory and then when we copy from
747:04 the memory and then when we copy from right to left the address of that chunk
747:06 right to left the address of that chunk of memory that's what gives us
747:08 of memory that's what gives us conceptually this arrow and the garbage
747:10 conceptually this arrow and the garbage goes away because it's a valid pointer
747:11 goes away because it's a valid pointer now of course there's still two garbage
747:13 now of course there's still two garbage values there because we haven't set this
747:14 values there because we haven't set this node to store a number like the number
747:16 node to store a number like the number one so let's go ahead and execute one
747:18 one so let's go ahead and execute one other line of code like this which while
747:20 other line of code like this which while cryptic looking is just an application
747:22 cryptic looking is just an application of ideas we've seen in week four and
747:24 of ideas we've seen in week four and prior star N means to start at this
747:27 prior star N means to start at this variable and go there Follow the arrow
747:30 variable and go there Follow the arrow is what the star or the D reference
747:32 is what the star or the D reference operator does for us and then the dot
747:34 operator does for us and then the dot operator recall when we first introduce
747:36 operator recall when we first introduce structs like for a person struct allows
747:38 structs like for a person struct allows us to go at the number field or the next
747:40 us to go at the number field or the next field so if I do star n and then in
747:43 field so if I do star n and then in parentheses to make sure order of
747:44 parentheses to make sure order of operations is preserved do number and
747:47 operations is preserved do number and then assign it the actual number one
747:49 then assign it the actual number one which puts the one in the top of that
747:50 which puts the one in the top of that rectangle now admittedly this syntax is
747:53 rectangle now admittedly this syntax is not very user friendly it's annoying to
747:54 not very user friendly it's annoying to remember you have to the parentheses so
747:56 remember you have to the parentheses so there's another Syntax for this whenever
747:58 there's another Syntax for this whenever you're doing two things like this in
748:00 you're doing two things like this in code dereferencing a pointer that is
748:03 code dereferencing a pointer that is going to an address and then further
748:05 going to an address and then further using the dot notation to go inside of
748:07 using the dot notation to go inside of the structure you find that wonderfully
748:09 the structure you find that wonderfully C gives us this syntax whereby you can
748:12 C gives us this syntax whereby you can just change the star and the parentheses
748:14 just change the star and the parentheses and the dot to just be an arrow and
748:16 and the dot to just be an arrow and again it's not a single character on
748:18 again it's not a single character on your keyboard it's a hyphen and then an
748:19 your keyboard it's a hyphen and then an open angle bracket but I kind of like
748:21 open angle bracket but I kind of like the semantics of this because this code
748:23 the semantics of this because this code now pretty much matches the picture n
748:26 now pretty much matches the picture n arrow leads you to the value that you
748:28 arrow leads you to the value that you want to access or ultimately change in
748:30 want to access or ultimately change in this way there's one step though we've
748:32 this way there's one step though we've forgotten of course which is that we
748:34 forgotten of course which is that we can't leave this garbage value here
748:36 can't leave this garbage value here because the garbage value is some
748:38 because the garbage value is some unknown value that effectively is
748:40 unknown value that effectively is pointing who knows where and we don't
748:42 pointing who knows where and we don't want to accidentally misinterpret that
748:43 want to accidentally misinterpret that garbage value as being a valid address
748:45 garbage value as being a valid address and risk going there so of course what
748:49 and risk going there so of course what value should we put here instead our old
748:51 value should we put here instead our old friend null just to signify that this is
748:53 friend null just to signify that this is indeed the end of the list and we could
748:55 indeed the end of the list and we could do that with a line of code like this
748:56 do that with a line of code like this and again we'll canote as much by just
748:58 and again we'll canote as much by just leaving that empty box blank so now we
749:01 leaving that empty box blank so now we have a list of size one let's go ahead
749:03 have a list of size one let's go ahead and add the second number to it as with
749:05 and add the second number to it as with these lines here list equals n allows us
749:08 these lines here list equals n allows us to remember that indeed we have this
749:11 to remember that indeed we have this list here so if we can step one step
749:13 list here so if we can step one step forward here's what the picture now
749:15 forward here's what the picture now looks like and technically let's go one
749:17 looks like and technically let's go one step further here this is now really
749:20 step further here this is now really what's going on in memory once my list
749:21 what's going on in memory once my list of size exists my main variable called
749:25 of size exists my main variable called list is pointing at exactly that first
749:27 list is pointing at exactly that first node at this point in the story I don't
749:29 node at this point in the story I don't need to know or care about the temporary
749:31 need to know or care about the temporary variable that I called n even though it
749:33 variable that I called n even though it might very well still be there but
749:35 might very well still be there but indeed this now represents that link
749:36 indeed this now represents that link list let's now indeed add the number two
749:39 list let's now indeed add the number two so with the same line of code as before
749:41 so with the same line of code as before I'm going to allocate another node size
749:43 I'm going to allocate another node size of node ideally I would be checking for
749:45 of node ideally I would be checking for null here but we're doing the juicy
749:46 null here but we're doing the juicy Parts only on the slides let's now go
749:48 Parts only on the slides let's now go ahead and depict that so what happens
749:50 ahead and depict that so what happens with this this brings back our n pointer
749:53 with this this brings back our n pointer which might have been there the whole
749:54 which might have been there the whole time but we're doing this step by step
749:56 time but we're doing this step by step it's a garbage value though because we
749:57 it's a garbage value though because we haven't yet copied from right to left
749:59 haven't yet copied from right to left Malo of course gives us a second chunk
750:01 Malo of course gives us a second chunk of memory which maybe ends up there with
750:03 of memory which maybe ends up there with two garbage values by default I've
750:05 two garbage values by default I've omitted the labels now just because
750:06 omitted the labels now just because they're still going to be number and
750:08 they're still going to be number and next respectively once we copy from
750:11 next respectively once we copy from right to left the garbage value indeed
750:12 right to left the garbage value indeed becomes an arrow Oscar disappears
750:15 becomes an arrow Oscar disappears because it's now indeed a valid pointer
750:17 because it's now indeed a valid pointer pointing here now the values themselves
750:19 pointing here now the values themselves number and next are invalid garbage
750:21 number and next are invalid garbage values so here is where we can now start
750:24 values so here is where we can now start using our new syntax like the arrow
750:26 using our new syntax like the arrow notation or the star and the dot if you
750:28 notation or the star and the dot if you prefer and we can change the value of n
750:31 prefer and we can change the value of n Follow the arrow to number and that
750:33 Follow the arrow to number and that becomes two similarly we can do this
750:35 becomes two similarly we can do this again and set n arrow next so start at n
750:39 again and set n arrow next so start at n Follow the arrow access the next field
750:42 Follow the arrow access the next field and set that equal to null now we're not
750:44 and set that equal to null now we're not quite done yet because we haven't
750:46 quite done yet because we haven't actually linked things together so
750:48 actually linked things together so here's now where things get interesting
750:50 here's now where things get interesting how do I combine these two well let me
750:52 how do I combine these two well let me me propose this let me propose on our
750:54 me propose this let me propose on our next line here we actually update for
750:56 next line here we actually update for Now list equal to n that is to say
751:00 Now list equal to n that is to say whatever address this is whatever it's
751:02 whatever address this is whatever it's pointing at change list to be the same
751:04 pointing at change list to be the same address that is point at the same thing
751:06 address that is point at the same thing so if n is pointing here let's change
751:08 so if n is pointing here let's change list to point here and go ahead and do
751:11 list to point here and go ahead and do that Carter if you could I don't like
751:13 that Carter if you could I don't like this can you go one further step this is
751:16 this can you go one further step this is bad what is wrong about my sequence of
751:19 bad what is wrong about my sequence of operations here where I updated list to
751:22 operations here where I updated list to point my new
751:23 point my new node
751:25 node yeah yeah we lost the pointer to the
751:28 yeah yeah we lost the pointer to the other node so I don't even care about
751:30 other node so I don't even care about the ordering 21 or one two the bigger
751:33 the ordering 21 or one two the bigger problem now as the lack of arrows over
751:36 problem now as the lack of arrows over there suggests is that I have a memory
751:37 there suggests is that I have a memory leak I have orphaned my original node in
751:40 leak I have orphaned my original node in the sense that nothing is pointing at it
751:42 the sense that nothing is pointing at it anymore now absolutely I could fix this
751:45 anymore now absolutely I could fix this by adding some temporary variables I
751:46 by adding some temporary variables I could add it to the mix but at this
751:47 could add it to the mix but at this point in the story I have not done any
751:49 point in the story I have not done any such uh recollection thereof so let me
751:51 such uh recollection thereof so let me back this up and let's go forward in the
751:53 back this up and let's go forward in the slides this is where we left off a
751:55 slides this is where we left off a moment ago I think I need to take into
751:57 moment ago I think I need to take into account order of operations and I'm
751:59 account order of operations and I'm going to keep this simple I'm not going
752:00 going to keep this simple I'm not going to care about the order of the numbers
752:02 to care about the order of the numbers for now I'm fine with a list that is two
752:05 for now I'm fine with a list that is two and then one so with that said let me go
752:08 and then one so with that said let me go ahead and update I think this box here
752:11 ahead and update I think this box here to point at my original node so let's
752:13 to point at my original node so let's see how we can do this in code okay n
752:16 see how we can do this in code okay n arrow next so n arrow next should equal
752:21 arrow next so n arrow next should equal the current list and this is a little
752:22 the current list and this is a little weird again but recall what list is list
752:25 weird again but recall what list is list is this pointer here that just contains
752:27 is this pointer here that just contains the address of the original address of
752:30 the address of the original address of the list or equivalently it contains
752:32 the list or equivalently it contains this Arrow whatever it's pointing at so
752:34 this Arrow whatever it's pointing at so what this means in this line of code n
752:37 what this means in this line of code n bracket next means start at n Follow the
752:39 bracket next means start at n Follow the arrow access the next pointer and set it
752:41 arrow access the next pointer and set it equal to whatever list equals so if list
752:44 equal to whatever list equals so if list is pointing here then next should point
752:48 is pointing here then next should point there as well this I think is safe
752:50 there as well this I think is safe because now we have redundancy now we've
752:52 because now we have redundancy now we've got two pointers pointing at the
752:54 got two pointers pointing at the original list and now I think we can do
752:56 original list and now I think we can do another step whereby we update list to
752:59 another step whereby we update list to equal n same line of code before that
753:01 equal n same line of code before that got us into trouble but I'm doing it
753:02 got us into trouble but I'm doing it second now instead of first when I
753:04 second now instead of first when I execute list equals n this now sets list
753:08 execute list equals n this now sets list equal to the same thing that n equals
753:10 equal to the same thing that n equals and so now I have successfully inserted
753:13 and so now I have successfully inserted my new node containing two into the list
753:17 my new node containing two into the list and in fact if we advance one more we
753:19 and in fact if we advance one more we can just clear up the Clutter assume
753:20 can just clear up the Clutter assume that the temporary variable is gone from
753:22 that the temporary variable is gone from the story now we have a linked list
753:24 the story now we have a linked list where admittedly ordering is wrong it's
753:26 where admittedly ordering is wrong it's 21 instead of one two but at least it's
753:28 21 instead of one two but at least it's linked correctly and I didn't orphan or
753:30 linked correctly and I didn't orphan or leak any
753:32 leak any memory questions on this sequence of
753:35 memory questions on this sequence of steps
753:36 steps here yeah in
753:38 here yeah in [Music]
753:47 back yeah spot on so this would fall under that category of a stack if you
753:49 under that category of a stack if you will although I've not called it that by
753:51 will although I've not called it that by name because I just pushed the number
753:53 name because I just pushed the number two onto this data structure if you will
753:56 two onto this data structure if you will and indeed it ended up at the beginning
753:57 and indeed it ended up at the beginning of the list instead of the end and so
754:00 of the list instead of the end and so here's where we see a distinction
754:01 here's where we see a distinction between an abstract data structure which
754:02 between an abstract data structure which is where we began a stack is a thing
754:04 is where we began a stack is a thing like the pile of sweaters that just has
754:06 like the pile of sweaters that just has push and pop properties and lifo access
754:09 push and pop properties and lifo access like uh last in first out how do you
754:12 like uh last in first out how do you implement something like that in memory
754:14 implement something like that in memory well it would seem that you could
754:16 well it would seem that you could implement the notion of a stack here not
754:18 implement the notion of a stack here not for sweaters but for numbers using a
754:20 for sweaters but for numbers using a linked list so long as you implement
754:23 linked list so long as you implement insertion AKA pushing by prepending new
754:26 insertion AKA pushing by prepending new values to the list by prepending again
754:29 values to the list by prepending again and again and if Carter you don't mind
754:30 and again and if Carter you don't mind hitting the keyboard one more time if I
754:32 hitting the keyboard one more time if I wanted to add the number three now you
754:34 wanted to add the number three now you would could imagine prepending it to the
754:36 would could imagine prepending it to the list why well honestly especially as
754:38 list why well honestly especially as this list gets longer and longer I kind
754:41 this list gets longer and longer I kind of like the appeal of prepending these
754:43 of like the appeal of prepending these elements why because even if this list
754:45 elements why because even if this list gets crazy long and way way out here you
754:48 gets crazy long and way way out here you didn't notice me following all of the
754:50 didn't notice me following all of the arrows earlier to do the insert if I
754:52 arrows earlier to do the insert if I want to insert a fourth number a fifth
754:54 want to insert a fourth number a fifth number a sixth number all I have to do
754:56 number a sixth number all I have to do is like insert it here if you will point
754:58 is like insert it here if you will point it at the original uh start of the list
755:01 it at the original uh start of the list then update this pointer and done and I
755:03 then update this pointer and done and I would say that's like two steps give or
755:05 would say that's like two steps give or take it's not going to be end steps as
755:07 take it's not going to be end steps as it would be if I had to upend the new
755:10 it would be if I had to upend the new nodes to the end of the list now of
755:12 nodes to the end of the list now of course we've sacrificed ordering of
755:14 course we've sacrificed ordering of these numbers they're literally in the
755:15 these numbers they're literally in the opposite order or whatever order they
755:17 opposite order or whatever order they were inserted in but that might very
755:19 were inserted in but that might very well be okay depending on the goal at
755:21 well be okay depending on the goal at hand all right thank you to Carter for
755:23 hand all right thank you to Carter for stepping through this what if now we
755:24 stepping through this what if now we wanted to translate this oh sure thank
755:31 you it's all for you none for me in this example so here we have perhaps a way of
755:34 example so here we have perhaps a way of translating this now to some actual code
755:36 translating this now to some actual code and this will be the last of like the
755:38 and this will be the last of like the sort of intense code here just to give
755:40 sort of intense code here just to give you a sense of how we can translate this
755:41 you a sense of how we can translate this idea now to actual step so this is list.
755:44 idea now to actual step so this is list. C and VSS code here let me go ahead and
755:47 C and VSS code here let me go ahead and make a couple of changes up top let me
755:49 make a couple of changes up top let me go ahead and how about uh declaring a
755:52 go ahead and how about uh declaring a node using typ def uh struct node using
755:56 node using typ def uh struct node using our new framing as before I'm going to
755:58 our new framing as before I'm going to give every node a number as I proposed
756:00 give every node a number as I proposed and every node a pointer to the next
756:03 and every node a pointer to the next element which is going to be implemented
756:06 element which is going to be implemented just as before and I'm going to simplify
756:07 just as before and I'm going to simplify the whole name as just node so all of
756:10 the whole name as just node so all of that is is the exact same type depth
756:12 that is is the exact same type depth that we proposed earlier now let me go
756:14 that we proposed earlier now let me go ahead and get rid of all of this code
756:16 ahead and get rid of all of this code which we wrote earlier and recall that
756:18 which we wrote earlier and recall that this was the most recent version that
756:20 this was the most recent version that was not a linked list this was just in
756:22 was not a linked list this was just in Array that we allocated and then
756:24 Array that we allocated and then reallocated so this is sort of the old
756:26 reallocated so this is sort of the old way of doing things but it was
756:28 way of doing things but it was inefficient because we might have to
756:30 inefficient because we might have to lean on a for Loop or lean on realloc to
756:32 lean on a for Loop or lean on realloc to copy everything around we're now going
756:34 copy everything around we're now going to reimplement the notion of a list as
756:36 to reimplement the notion of a list as an actual linked list not as an array so
756:41 an actual linked list not as an array so my main function now might do something
756:42 my main function now might do something like this and I'm going to really just
756:44 like this and I'm going to really just copy the lines of code that we just
756:46 copy the lines of code that we just stepped through on the board so let me
756:47 stepped through on the board so let me give myself a uh special variable called
756:50 give myself a uh special variable called list that's going to be initialized to
756:52 list that's going to be initialized to null and this is just my pointer the
756:53 null and this is just my pointer the square on the left hand side of the
756:55 square on the left hand side of the screen that represents the start of the
756:57 screen that represents the start of the list and if it's null it means the list
756:58 list and if it's null it means the list is empty so done I'm done implementing a
757:01 is empty so done I'm done implementing a linked list of size zero well now how do
757:04 linked list of size zero well now how do I want to run this code well let me
757:06 I want to run this code well let me propose for the sake of discussion that
757:07 propose for the sake of discussion that this version of the program will take
757:09 this version of the program will take command line arguments so I want to be
757:11 command line arguments so I want to be able to do something like this I want to
757:12 able to do something like this I want to run this program ultimately and type in
757:15 run this program ultimately and type in three command line arguments like this 1
757:17 three command line arguments like this 1 2 3 and I want my program in a couple
757:20 2 3 and I want my program in a couple minutes to allocate one two three nodes
757:22 minutes to allocate one two three nodes and Stitch them together just like the
757:25 and Stitch them together just like the visualization on the board uh I could
757:27 visualization on the board uh I could use get int but it's just going to be
757:29 use get int but it's just going to be faster if we use command line arguments
757:31 faster if we use command line arguments so again I'm just borrowing some
757:32 so again I'm just borrowing some Concepts from week two but none of
757:34 Concepts from week two but none of that's possible yet until I change my
757:36 that's possible yet until I change my code here so let's do this in argc uh
757:39 code here so let's do this in argc uh string uh argv but you know what we know
757:43 string uh argv but you know what we know that strings are not actually a thing
757:45 that strings are not actually a thing anymore so I can change my command line
757:47 anymore so I can change my command line argument definition to be what it really
757:49 argument definition to be what it really is it's really charar but it's the exact
757:52 is it's really charar but it's the exact same thing as in week two just strings
757:54 same thing as in week two just strings are no more at least without the
757:56 are no more at least without the training wheels on anymore like last
757:58 training wheels on anymore like last week and now let me do this uh for in I
758:01 week and now let me do this uh for in I equal 1 uh I is less than ARG C i++ so
758:06 equal 1 uh I is less than ARG C i++ so what I'm doing with this Loop is I just
758:08 what I'm doing with this Loop is I just want to iterate over the command line
758:09 want to iterate over the command line argument so I have one number at a time
758:11 argument so I have one number at a time from The Prompt um what else do I want
758:14 from The Prompt um what else do I want to do here uh well let's go ahead and
758:18 to do here uh well let's go ahead and how about do this um let's get
758:22 how about do this um let's get a number so in number equals arv braet I
758:26 a number so in number equals arv braet I so a couple of notes Here one I'm
758:28 so a couple of notes Here one I'm starting my for loop at one instead of
758:31 starting my for loop at one instead of zero but I'm going up to RC RC is
758:33 zero but I'm going up to RC RC is argument count how many words are at the
758:35 argument count how many words are at the prompt why am I starting at one instead
758:37 prompt why am I starting at one instead of zero though given my
758:40 of zero though given my goal why am I starting at one
758:51 yeah yeah so the first value in RV is is actually the name of the program that's
758:53 actually the name of the program that's obviously not a number so I want the
758:54 obviously not a number so I want the second value so I'm going to start
758:56 second value so I'm going to start iterating over those command line
758:57 iterating over those command line arguments at I equals 1 so that's all I
758:59 arguments at I equals 1 so that's all I just want to get the actual numbers at
759:01 just want to get the actual numbers at the prompt um unfortunately argv bracket
759:04 the prompt um unfortunately argv bracket I is a string AKA Char star that is not
759:08 I is a string AKA Char star that is not an INT so this line of code won't work
759:09 an INT so this line of code won't work but can anyone think back to like week
759:11 but can anyone think back to like week two where we had a function for
759:14 two where we had a function for converting strings to
759:15 converting strings to integers anyone yeah so a to I is a
759:19 integers anyone yeah so a to I is a function that converts asky to an
759:21 function that converts asky to an integer assuming what you give it as an
759:23 integer assuming what you give it as an argument looks like a number like one or
759:25 argument looks like a number like one or two or three so let me fix this let me
759:27 two or three so let me fix this let me actually do the conversion if I were
759:28 actually do the conversion if I were really being careful I would error check
759:30 really being careful I would error check this make sure that there's no digits
759:32 this make sure that there's no digits just like you might have in problem set
759:34 just like you might have in problem set two but for today's purposes I'm just
759:35 two but for today's purposes I'm just going to assume the honor System that
759:37 going to assume the honor System that the user me is going to run the program
759:39 the user me is going to run the program correctly all right so now that I have a
759:42 correctly all right so now that I have a variable containing the number from the
759:43 variable containing the number from the command line let's just allocate a node
759:45 command line let's just allocate a node for it so let me do node star n just
759:48 for it so let me do node star n just like we did in the visualization and
759:50 like we did in the visualization and let's malok enough space for the size of
759:53 let's malok enough space for the size of one such node here I now need to just be
759:56 one such node here I now need to just be super safe so if n equals equals null
760:00 super safe so if n equals equals null like if I'm out of memory you know what
760:02 like if I'm out of memory you know what let me go ahead and just immediately
760:04 let me go ahead and just immediately return one here otherwise if that's not
760:07 return one here otherwise if that's not the case let me go ahead and update the
760:11 the case let me go ahead and update the number field of this new node which it
760:14 number field of this new node which it line 24 does exist because it did not
760:16 line 24 does exist because it did not return null so I did not exit early with
760:18 return null so I did not exit early with return and let me just store whatever
760:20 return and let me just store whatever number that human typed in first so the
760:23 number that human typed in first so the return value of a to I which per line 17
760:26 return value of a to I which per line 17 is in my variable called number and then
760:28 is in my variable called number and then let me go ahead and just prepend this to
760:33 let me go ahead and just prepend this to the list let me go ahead and say that um
760:38 the list let me go ahead and say that um this next field first has a known value
760:41 this next field first has a known value null just so that we get rid of that
760:43 null just so that we get rid of that second garbage value and let me go ahead
760:45 second garbage value and let me go ahead and now prepend it to the list so if I
760:47 and now prepend it to the list so if I want to prepend it that means this new
760:49 want to prepend it that means this new node must have a next field field that
760:52 node must have a next field field that points to the current beginning of the
760:56 points to the current beginning of the list and again the goal here is to preen
760:59 list and again the goal here is to preen Preen Preen so whatever the current list
761:01 Preen Preen so whatever the current list is let's change it so that this new node
761:03 is let's change it so that this new node points to that existing list and now
761:07 points to that existing list and now step two as before was to update the
761:10 step two as before was to update the actual list to point at this node so
761:13 actual list to point at this node so recall in red on the screen before I
761:14 recall in red on the screen before I screwed up originally and I only did
761:16 screwed up originally and I only did this line by moving the pointer too
761:18 this line by moving the pointer too early if you will but I fixed that once
761:21 early if you will but I fixed that once Carter helped me rewind and we got rid
761:22 Carter helped me rewind and we got rid of the red line which indicated error
761:25 of the red line which indicated error and I just do n arrow next to change the
761:28 and I just do n arrow next to change the next field of this new node to point to
761:30 next field of this new node to point to the existing list so I'm not orphaning
761:32 the existing list so I'm not orphaning anything all right at this point in the
761:34 anything all right at this point in the story I think my code is
761:38 story I think my code is correct not batting very well though
761:40 correct not batting very well though today but I think my code is correct but
761:42 today but I think my code is correct but the program doesn't do anything
761:43 the program doesn't do anything interesting so it would be nice to kind
761:45 interesting so it would be nice to kind of now iterate over this link list in
761:47 of now iterate over this link list in memory whatever its order is and print
761:49 memory whatever its order is and print things out well how do we do that well
761:51 things out well how do we do that well it turns out if you want to iterate over
761:53 it turns out if you want to iterate over a linked List the general Paradigm is to
761:55 a linked List the general Paradigm is to do something like this to define a
761:57 do something like this to define a temporary variable I could call it temp
761:59 temporary variable I could call it temp but another convention that you might as
762:00 but another convention that you might as well see is called pointer PTR for short
762:03 well see is called pointer PTR for short but you can call it anything you want
762:05 but you can call it anything you want and you can have a temporary variable
762:06 and you can have a temporary variable first point at the first node in the
762:08 first point at the first node in the list and then in some kind of loop like
762:10 list and then in some kind of loop like a while loop you point it at the second
762:12 a while loop you point it at the second node in the list and then you keep
762:13 node in the list and then you keep iterating you point it at the last node
762:15 iterating you point it at the last node in the list and then eventually you
762:16 in the list and then eventually you iterate too far effectively pointing at
762:18 iterate too far effectively pointing at null at which point your while loop can
762:20 null at which point your while loop can presumably terminate so how do I
762:22 presumably terminate so how do I Implement that idea of allocating a
762:24 Implement that idea of allocating a temporary pointer that just points at
762:26 temporary pointer that just points at each node in the list and lets me print
762:28 each node in the list and lets me print out ultimately each of those numbers
762:30 out ultimately each of those numbers well let's go back to my code here and
762:32 well let's go back to my code here and let me do this let me go ahead and
762:35 let me do this let me go ahead and declare this temporary pointer which is
762:37 declare this temporary pointer which is going to be a node star also why because
762:39 going to be a node star also why because it's the address of a node the first the
762:41 it's the address of a node the first the second the third and I'm going to set
762:42 second the third and I'm going to set that equal to whatever the beginning of
762:44 that equal to whatever the beginning of the list is so that is going to be
762:46 the list is so that is going to be equivalent to this version of the
762:48 equivalent to this version of the picture here where pointer is just
762:49 picture here where pointer is just temporarily pointing at the first node
762:52 temporarily pointing at the first node in the list it's not pointing at list
762:53 in the list it's not pointing at list per se it's pointing at the first node
762:55 per se it's pointing at the first node in the list which list is also pointing
762:57 in the list which list is also pointing at itself all right once I've done this
763:00 at itself all right once I've done this I think I can translate this to code
763:01 I think I can translate this to code that's a little new but it's
763:03 that's a little new but it's conceptually familiar perhaps now while
763:06 conceptually familiar perhaps now while that pointer does not equal null so
763:10 that pointer does not equal null so while I have a valid pointer like my
763:12 while I have a valid pointer like my finger or that arrow is pointing at an
763:14 finger or that arrow is pointing at an actual node in memory well let me go
763:16 actual node in memory well let me go ahead and print it out so let me print
763:18 ahead and print it out so let me print out with percent I back sln whatever is
763:21 out with percent I back sln whatever is in the current node at the number field
763:25 in the current node at the number field within and again this is going to have
763:27 within and again this is going to have the effect hopefully of first printing
763:29 the effect hopefully of first printing the three and I think I just need to Now
763:32 the three and I think I just need to Now update the pointer so that on the next
763:35 update the pointer so that on the next iteration it's pointing at the next
763:36 iteration it's pointing at the next value so if this is where the story is
763:39 value so if this is where the story is how do I update pointer to point at the
763:42 how do I update pointer to point at the second element of the list well I want
763:43 second element of the list well I want pointer to point at the two and I want
763:45 pointer to point at the two and I want pointer to eventually point at the three
763:48 pointer to eventually point at the three well how do I do that well the way in
763:49 well how do I do that well the way in code I can follow these arrows is as
763:51 code I can follow these arrows is as follows if I currently have pointer
763:54 follows if I currently have pointer pointing at this node but I want to
763:56 pointing at this node but I want to point it at the next node I can borrow
763:59 point it at the next node I can borrow this pointer here so whatever this
764:01 this pointer here so whatever this address is in the first node aka the
764:04 address is in the first node aka the next field I can copy that into pointer
764:07 next field I can copy that into pointer because then pointer will point at
764:09 because then pointer will point at whatever this is pointing at by just
764:10 whatever this is pointing at by just setting one equal to the other so once
764:13 setting one equal to the other so once I've done that the picture will
764:16 I've done that the picture will become this and how do I translate that
764:19 become this and how do I translate that to code it while new syntax is
764:21 to code it while new syntax is surprisingly straightforward all I need
764:24 surprisingly straightforward all I need do is say pointer after printing it
764:26 do is say pointer after printing it equals whatever pointer currently is but
764:30 equals whatever pointer currently is but grab its next field
764:32 grab its next field instead and this is a very common
764:34 instead and this is a very common Paradigm when iterating over a link list
764:37 Paradigm when iterating over a link list and you're using some temporary variable
764:38 and you're using some temporary variable like pointer you can simply set pointer
764:41 like pointer you can simply set pointer equal to pointer next and what that
764:43 equal to pointer next and what that means here is as follows if this is
764:46 means here is as follows if this is pointer pointing from here down to here
764:50 pointer pointing from here down to here pointer next is Follow the arrow grab
764:52 pointer next is Follow the arrow grab the next field so if you set pointer
764:54 the next field so if you set pointer equal to this thing that's the same
764:56 equal to this thing that's the same thing as pointing this at this same box
764:59 thing as pointing this at this same box and indeed if I advance to the next
765:02 and indeed if I advance to the next slide even though the arrows are
765:04 slide even though the arrows are technically pointing at different parts
765:05 technically pointing at different parts of the rectangles that's just for
765:06 of the rectangles that's just for graphic sake pointer is now pointing at
765:09 graphic sake pointer is now pointing at the second node and when I do this again
765:10 the second node and when I do this again on my next iteration it points at this
765:12 on my next iteration it points at this and then this last step notice when I
765:15 and then this last step notice when I keep doing pointer equals pointer next
765:18 keep doing pointer equals pointer next this will become eventually this value
765:20 this will become eventually this value but what's this value in this link list
765:23 but what's this value in this link list it's null technically so this Arrow will
765:26 it's null technically so this Arrow will eventually take on this value when I set
765:28 eventually take on this value when I set pointer equal to pointer next and at
765:31 pointer equal to pointer next and at that point PTR my temporary pointer is
765:34 that point PTR my temporary pointer is going to be null so it might as well
765:36 going to be null so it might as well look like this pictorially and what does
765:38 look like this pictorially and what does that mean for my Loop once pointer is
765:42 that mean for my Loop once pointer is null because you've walked off the end
765:43 null because you've walked off the end of the length list what's going to be
765:45 of the length list what's going to be true of this Loop here started in line
765:49 true of this Loop here started in line 32 any OB observations
765:53 32 any OB observations here what's going to be true what will
765:55 here what's going to be true what will happen now as soon as we hit the end of
765:57 happen now as soon as we hit the end of the list yeah
765:59 the list yeah sorry the loop is going to break out why
766:01 sorry the loop is going to break out why because line 32 which is constantly
766:03 because line 32 which is constantly asking well pointer does not equal null
766:05 asking well pointer does not equal null well if pointer finally equals null
766:07 well if pointer finally equals null three steps later the four the while
766:10 three steps later the four the while loop is now done and so what I can do at
766:12 loop is now done and so what I can do at the end of this program once I've
766:14 the end of this program once I've printed out those values well first
766:15 printed out those values well first let's go ahead and open my terminal
766:17 let's go ahead and open my terminal window let's make list okay a compile
766:20 window let's make list okay a compile do/ list and let me try the same values
766:22 do/ list and let me try the same values one and two and three that's going to
766:25 one and two and three that's going to again allocate one node two node three
766:27 again allocate one node two node three nodes by prepending prepending
766:28 nodes by prepending prepending prepending each of those values and it's
766:30 prepending each of those values and it's then going to iterate over them from
766:32 then going to iterate over them from left to right and so when I hit enter
766:34 left to right and so when I hit enter now what should I see on the screen if
766:36 now what should I see on the screen if my code is
766:41 correct what will I see feel fre to just call it
766:43 call it out 321 because I've prepended
766:47 out 321 because I've prepended presumably and here we go I indeed see
766:49 presumably and here we go I indeed see 321 so the list is is backwards but all
766:52 321 so the list is is backwards but all of the elements are there now
766:53 of the elements are there now technically if I ran valgrind on this
766:55 technically if I ran valgrind on this valgren would not be happy because I
766:56 valgren would not be happy because I have never freed any of my memory so I
766:59 have never freed any of my memory so I should probably now have a second Loop
767:00 should probably now have a second Loop here that does something like this let
767:02 here that does something like this let me again set pointer equal to list I
767:05 me again set pointer equal to list I don't need to redeclare it because I've
767:06 don't need to redeclare it because I've already created this thing on line 31 I
767:09 already created this thing on line 31 I just want to reset it to be the
767:10 just want to reset it to be the beginning of the list again and now I
767:12 beginning of the list again and now I can do the same kind of thing while PTR
767:14 can do the same kind of thing while PTR not equals null go ahead and do this
767:18 not equals null go ahead and do this well I don't want to just do free
767:20 well I don't want to just do free pointer and then do pointer gets pointer
767:25 pointer and then do pointer gets pointer next y my goal is to free all of my
767:28 next y my goal is to free all of my memory but I think this is going to get
767:30 memory but I think this is going to get me in trouble pointer equals list just
767:32 me in trouble pointer equals list just gives me a temporary pointer that points
767:34 gives me a temporary pointer that points at the three and then eventually the two
767:36 at the three and then eventually the two and then the one how well while pointer
767:38 and then the one how well while pointer not equal null I'm freeing the pointer
767:40 not equal null I'm freeing the pointer so this is like saying to Malo free that
767:41 so this is like saying to Malo free that node free that node free that node but
767:45 node free that node free that node but what's the problem with what I've just
767:46 what's the problem with what I've just done
767:48 done here this code is technically Bugg
767:52 here this code is technically Bugg [Music]
767:54 [Music] yeah exactly after you call free on
767:57 yeah exactly after you call free on pointer You Are by social contract with
768:00 pointer You Are by social contract with c not allowed to touch pointer anymore
768:03 c not allowed to touch pointer anymore it is invalid now it's still going to be
768:05 it is invalid now it's still going to be a number it's still going to be a
768:06 a number it's still going to be a pattern of bits but it's invalid and
768:08 pattern of bits but it's invalid and you'll very often get a segmentation
768:11 you'll very often get a segmentation fault if you tempt fate in that way so I
768:13 fault if you tempt fate in that way so I can't free the pointer and then use it
768:15 can't free the pointer and then use it literally the next line the solution
768:17 literally the next line the solution here kind of like our swapping of the
768:19 here kind of like our swapping of the liquids last time was to maybe just have
768:21 liquids last time was to maybe just have a temporary variable so I can do a
768:23 a temporary variable so I can do a Switcheroo and so a common way to solve
768:25 Switcheroo and so a common way to solve this problem to get the order of
768:26 this problem to get the order of operations right would be to do
768:28 operations right would be to do something like this give yourself a
768:29 something like this give yourself a temporary pointer like node star next
768:33 temporary pointer like node star next set it equal to the place you want to go
768:36 set it equal to the place you want to go next so one step ahead now you can free
768:39 next so one step ahead now you can free pointer and then you can update pointer
768:42 pointer and then you can update pointer to be that next value so essentially you
768:44 to be that next value so essentially you need need like two hands now you create
768:46 need need like two hands now you create on line 41 another pointer that if this
768:48 on line 41 another pointer that if this is pointing at the first Noe the three
768:51 is pointing at the first Noe the three your new pointer is pointing at the two
768:53 your new pointer is pointing at the two temporarily so now you can tell malok
768:55 temporarily so now you can tell malok via free release this memory but I
768:58 via free release this memory but I haven't forgotten where I want to go
769:00 haven't forgotten where I want to go next and so I can now continue on so a
769:03 next and so I can now continue on so a common Paradigm for just iterating over
769:06 common Paradigm for just iterating over these nodes and then freeing them a
769:08 these nodes and then freeing them a couple of observations strictly speaking
769:10 couple of observations strictly speaking I could have Consolidated this I don't
769:12 I could have Consolidated this I don't need two Loops to print the nodes and
769:14 need two Loops to print the nodes and then free the nodes I could do that all
769:16 then free the nodes I could do that all at once but let's assume that there's
769:18 at once but let's assume that there's other stuff of interest in my program
769:19 other stuff of interest in my program and I don't want to just immediately
769:21 and I don't want to just immediately free it there's one other bug that I
769:22 free it there's one other bug that I should probably address here there is
769:25 should probably address here there is still a potential memory leak up here
769:27 still a potential memory leak up here and this one is super subtle the valind
769:29 and this one is super subtle the valind would help you find it notice that in
769:32 would help you find it notice that in this Loop here when I'm calling malok
769:35 this Loop here when I'm calling malok this line of code is fine if the first
769:36 this line of code is fine if the first line of malok fails and returns null
769:39 line of malok fails and returns null because I immediately return and I'm
769:41 because I immediately return and I'm done but what if the second call but not
769:43 done but what if the second call but not the first or the third call but not the
769:45 the first or the third call but not the first or second fail this line of code
769:47 first or second fail this line of code has me returning immediately you really
769:50 has me returning immediately you really need to to do some garbage collection so
769:52 need to to do some garbage collection so to speak whereby you really need to go
769:54 to speak whereby you really need to go in and free any nodes that you did
769:56 in and free any nodes that you did allocate successfully earlier honestly
769:58 allocate successfully earlier honestly that's going to be a pin in the neck we
769:59 that's going to be a pin in the neck we won't do that here but probably what I'd
770:01 won't do that here but probably what I'd want to do is write a function called
770:03 want to do is write a function called free list or something like that and
770:05 free list or something like that and call that function to free any nodes I
770:07 call that function to free any nodes I had previously created so it's not quite
770:09 had previously created so it's not quite at the finish line but the building
770:11 at the finish line but the building blocks are indeed here questions on this
770:17 blocks are indeed here questions on this code and I think it's safe for me to
770:19 code and I think it's safe for me to promise that it won't escalate further
770:21 promise that it won't escalate further from
770:23 from that questions on
770:26 that questions on [Music]
770:27 [Music] this no well let me show you one
770:29 this no well let me show you one alternative that you might prefer and
770:31 alternative that you might prefer and I'm pretty sure this isn't an escalation
770:33 I'm pretty sure this isn't an escalation it's just an alternative formulation
770:34 it's just an alternative formulation another way you can iterate over nodes
770:36 another way you can iterate over nodes in a list could be this instead of a
770:38 in a list could be this instead of a while loop for instance let me actually
770:40 while loop for instance let me actually show you one other piece of syntax here
770:42 show you one other piece of syntax here you could technically use a for Loop you
770:44 you could technically use a for Loop you could give yourself a node pointer here
770:46 could give yourself a node pointer here that initialized is initialized to the
770:48 that initialized is initialized to the list you can then check in your for Loop
770:50 list you can then check in your for Loop that it's not equal to null and then you
770:52 that it's not equal to null and then you can do your update as usual like this
770:54 can do your update as usual like this either of these are equivalent even
770:56 either of these are equivalent even though this one I suspect looks scarier
770:58 though this one I suspect looks scarier it's doing the exact same thing in one
771:00 it's doing the exact same thing in one line instead of two but there's no
771:02 line instead of two but there's no reason we can't use four Loops instead
771:04 reason we can't use four Loops instead of while Loops to achieve the same idea
771:06 of while Loops to achieve the same idea but I'll leave these two as
771:07 but I'll leave these two as demonstrations of one approach or the
771:09 demonstrations of one approach or the other but that's just like in week one
771:11 other but that's just like in week one four Loops while Loops whatever looks
771:13 four Loops while Loops whatever looks simpler to you even though admittedly
771:15 simpler to you even though admittedly neither of these probably looks super
771:18 neither of these probably looks super clean all right so let's take the back
771:20 clean all right so let's take the back to things more conceptual here up until
771:23 to things more conceptual here up until now we've been inserting elements into
771:25 now we've been inserting elements into this link list by prepending them let's
771:28 this link list by prepending them let's consider what the running time then is
771:30 consider what the running time then is of these operations so if I've got a
771:31 of these operations so if I've got a link list of size three or size N More
771:34 link list of size three or size N More generally time has passed and I've added
771:35 generally time has passed and I've added a lot of things to it what's going to be
771:37 a lot of things to it what's going to be the running time for instance of
771:38 the running time for instance of searching a linked list for some value
771:41 searching a linked list for some value and I'll tell you already it's not login
771:43 and I'll tell you already it's not login because again binary search is off the
771:45 because again binary search is off the table as per before break so what might
771:49 table as per before break so what might the running time be of searching a
771:50 the running time be of searching a linked list for some value like two or
771:54 linked list for some value like two or three or 1 or
771:55 three or 1 or 50 what might the running time
771:59 50 what might the running time be o of I heard it over here o of n and
772:03 be o of I heard it over here o of n and y who was that oh in the middle here why
772:05 y who was that oh in the middle here why o of
772:10 [Music] n exactly you're going to have to go
772:11 n exactly you're going to have to go through every item in the list starting
772:13 through every item in the list starting from the left from the beginning which
772:15 from the left from the beginning which is how we've been drawing things and
772:16 is how we've been drawing things and connecting the dots and in the worst
772:18 connecting the dots and in the worst case the element might very well be at
772:19 case the element might very well be at the very end so it's going to be Big O
772:21 the very end so it's going to be Big O of n what about insertion how many steps
772:24 of n what about insertion how many steps in terms of Big O notation has it been
772:26 in terms of Big O notation has it been taking me to insert elements into the
772:28 taking me to insert elements into the link list using this prepended
772:35 design yeah so it's technically constant time Big O of one and again one is just
772:38 time Big O of one and again one is just representative of any constant it could
772:39 representative of any constant it could technically be two steps or three steps
772:41 technically be two steps or three steps or even 10 steps or 100 steps but if
772:43 or even 10 steps or 100 steps but if it's always finite and fixed then indeed
772:47 it's always finite and fixed then indeed you can say it's in Big O of one now why
772:49 you can say it's in Big O of one now why is that well over again no matter how
772:51 is that well over again no matter how long this list gets so long as there's
772:53 long this list gets so long as there's memory available for me I can just kind
772:55 memory available for me I can just kind of uh create a little splice at the
772:57 of uh create a little splice at the beginning of the list to put in the new
772:59 beginning of the list to put in the new node update the original list and I'm on
773:01 node update the original list and I'm on my way and it keeps getting longer even
773:02 my way and it keeps getting longer even though it might not be spread out in
773:04 though it might not be spread out in memory so big O of one is possible with
773:06 memory so big O of one is possible with these link lists if I indeed Preen
773:09 these link lists if I indeed Preen things of course if I Preen things
773:12 things of course if I Preen things everything's going to get out of order
773:13 everything's going to get out of order potentially and we're going to have
773:14 potentially and we're going to have maybe the stack property instead of a q
773:16 maybe the stack property instead of a q property so we might want to do things
773:17 property so we might want to do things slightly differently so instead of doing
773:20 slightly differently so instead of doing this
773:20 this whereby we kept prepending prepending
773:23 whereby we kept prepending prepending prepending suppose we append to the end
773:25 prepending suppose we append to the end of the list instead so if we now insert
773:28 of the list instead so if we now insert the one the two and the three as we
773:29 the one the two and the three as we might want to for a q to maintain that
773:31 might want to for a q to maintain that fairness property we might start with an
773:33 fairness property we might start with an empty list we might add the one we might
773:36 empty list we might add the one we might append the two append the three and so
773:39 append the two append the three and so it just is sort of laid out differently
773:41 it just is sort of laid out differently in memory and again if I can come to you
773:42 in memory and again if I can come to you in the middle what's the running time of
773:44 in the middle what's the running time of search again when the link list uses
773:46 search again when the link list uses this append
773:49 this append implementation yeah still Big O of n
773:51 implementation yeah still Big O of n because in the worst case you're going
773:52 because in the worst case you're going to have to go through the whole list
773:54 to have to go through the whole list just to find it and notice it doesn't
773:56 just to find it and notice it doesn't matter if you have an intuition now that
773:58 matter if you have an intuition now that the bigger numbers might very well be at
774:00 the bigger numbers might very well be at the end you have no way to jump to the
774:02 the end you have no way to jump to the end you have no way to jump to the
774:03 end you have no way to jump to the middle or do anything resembling binary
774:05 middle or do anything resembling binary search every search has to start from
774:07 search every search has to start from the left and follow the arrows again and
774:10 the left and follow the arrows again and again all right so I don't think we've
774:12 again all right so I don't think we've done any better there and in fact what
774:13 done any better there and in fact what is insertions running time now in Big O
774:16 is insertions running time now in Big O when we're appending to the list in this
774:18 when we're appending to the list in this way as we might to implement a Q instead
774:20 way as we might to implement a Q instead of a stack what's the running time of
774:23 of a stack what's the running time of inserting a new
774:25 inserting a new value Big O
774:28 value Big O of so not Big O of one in this case but
774:31 of so not Big O of one in this case but Big O of n because if I'm appending by
774:34 Big O of n because if I'm appending by definition I have to start here and
774:37 definition I have to start here and Traverse the whole thing looking for the
774:38 Traverse the whole thing looking for the end now this is a bit of an
774:40 end now this is a bit of an overstatement right you could obviously
774:41 overstatement right you could obviously optimize this slightly by maybe adding
774:43 optimize this slightly by maybe adding another variable that always points to
774:46 another variable that always points to the last element sort of a cheat sheet a
774:48 the last element sort of a cheat sheet a shortcut that gets you all the way to to
774:50 shortcut that gets you all the way to to the end that's totally fine it's not
774:52 the end that's totally fine it's not doesn't really fit the traditional
774:53 doesn't really fit the traditional definition of a singly linked list but
774:55 definition of a singly linked list but there's absolutely smart engineering
774:56 there's absolutely smart engineering solutions to these kinds of problems but
774:58 solutions to these kinds of problems but as designed it would indeed be Big O of
775:00 as designed it would indeed be Big O of end to insert to if you've got to go all
775:02 end to insert to if you've got to go all the way to the end and you're not using
775:04 the way to the end and you're not using a little extra memory to get yourself
775:06 a little extra memory to get yourself there quickly well what if we want to
775:08 there quickly well what if we want to take things one last step and not just a
775:11 take things one last step and not just a pen blindly because even though I
775:12 pen blindly because even though I inserted one two three if I inserted
775:15 inserted one two three if I inserted them in random order they would end up
775:17 them in random order they would end up in random order what if you want to
775:18 in random order what if you want to maintain a sorted list from smallest to
775:21 maintain a sorted list from smallest to largest well then you might want to
775:22 largest well then you might want to insert numbers like this starting from
775:24 insert numbers like this starting from an empty list we might have a two then
775:27 an empty list we might have a two then we might try inserting a one but we want
775:30 we might try inserting a one but we want to keep it sorted so now we're going to
775:32 to keep it sorted so now we're going to prepend in our code but then you might
775:34 prepend in our code but then you might want to insert a four so you would
775:36 want to insert a four so you would append the four because you're probably
775:38 append the four because you're probably going to look for the right spot to
775:39 going to look for the right spot to insert it then we're going to insert a
775:42 insert it then we're going to insert a three and this one's getting a little
775:43 three and this one's getting a little annoying because now you have to like
775:45 annoying because now you have to like iterate over the list look for the right
775:47 iterate over the list look for the right spot and then do a little smarter of a
775:50 spot and then do a little smarter of a splice but it's possible but you don't
775:52 splice but it's possible but you don't want to Orphan the four for instance and
775:54 want to Orphan the four for instance and then ultimately we get back to this
775:56 then ultimately we get back to this question what would the performance be
775:58 question what would the performance be of your linked list if you're trying to
776:00 of your linked list if you're trying to maintain sorted order well search I
776:04 maintain sorted order well search I think is going to be Big O of n for the
776:06 think is going to be Big O of n for the same reasons as before what about
776:09 same reasons as before what about insertion big go of what for inserting
776:13 insertion big go of what for inserting into a sorted linked
776:16 into a sorted linked list yeah in the worst
776:18 list yeah in the worst case yeah it's still Big O ofen so it's
776:21 case yeah it's still Big O ofen so it's no worse than but it's not really any
776:23 no worse than but it's not really any better than a pending but we gain the
776:25 better than a pending but we gain the additional property of maintaining a
776:27 additional property of maintaining a sorted list which might very well be
776:28 sorted list which might very well be useful if you're sorting your contacts
776:30 useful if you're sorting your contacts in your phone or something like that
776:32 in your phone or something like that where it just maintain makes sense to
776:33 where it just maintain makes sense to maintain sorted order now in the code
776:35 maintain sorted order now in the code for online today if you take a look at
776:37 for online today if you take a look at some of the final versions of code like
776:39 some of the final versions of code like list 6. C and list 5.c as we'll post on
776:43 list 6. C and list 5.c as we'll post on the website you can actually see code
776:44 the website you can actually see code that will solve all three of these
776:46 that will solve all three of these problems the Preen version that we wrote
776:48 problems the Preen version that we wrote live the appen version which we talk
776:50 live the appen version which we talk through as well as this sorted order one
776:52 through as well as this sorted order one but I think I'll avoid showing it live
776:54 but I think I'll avoid showing it live just because I do think that starts to
776:56 just because I do think that starts to escalate quickly but I think we have
776:57 escalate quickly but I think we have enough of a building block if we're
776:59 enough of a building block if we're comfortable with prepending to at least
777:01 comfortable with prepending to at least solve some real world problems with
777:03 solve some real world problems with these link
777:04 these link lists questions then on link list which
777:07 lists questions then on link list which we'll now leave behind on their own but
777:10 we'll now leave behind on their own but now use this technique to solve fancier
777:14 now use this technique to solve fancier problems but much less
777:16 problems but much less code questions on linked list all right
777:21 code questions on linked list all right so to C recap we've kind of taken a s
777:24 so to C recap we've kind of taken a s side step with link list like we have
777:25 side step with link list like we have this dynamism now where we can grow and
777:27 this dynamism now where we can grow and Shrink our chunks of memory without over
777:30 Shrink our chunks of memory without over allocating or accidentally
777:31 allocating or accidentally underallocation as in the world of an
777:33 underallocation as in the world of an array we don't have to worry about
777:35 array we don't have to worry about copying values endlessly because once
777:37 copying values endlessly because once you allocate the node it can just stay
777:38 you allocate the node it can just stay wherever it is in memory and you can
777:40 wherever it is in memory and you can just maintain uh you can just Stitch it
777:42 just maintain uh you can just Stitch it together somehow but unfortunately we've
777:44 together somehow but unfortunately we've sacrificed what we started the class
777:46 sacrificed what we started the class with in week zero which was like binary
777:48 with in week zero which was like binary search divide and conquer which was like
777:50 search divide and conquer which was like gave us that log and running time which
777:52 gave us that log and running time which was really compelling if you think back
777:54 was really compelling if you think back to the demonstrations and the the
777:56 to the demonstrations and the the visuals can we get the best of both
777:58 visuals can we get the best of both worlds can we get the sort of uh speed
778:02 worlds can we get the sort of uh speed of binary search something logarithmic
778:04 of binary search something logarithmic but the dynamism of something like a
778:06 but the dynamism of something like a link list well we can actually I think
778:08 link list well we can actually I think if we start to think not in a single
778:10 if we start to think not in a single Dimension just the x-axis if you will
778:12 Dimension just the x-axis if you will but two Dimensions such that our data
778:15 but two Dimensions such that our data structures can maybe now have width and
778:17 structures can maybe now have width and height if you will and so a tree is
778:19 height if you will and so a tree is perhaps the right term here much like a
778:21 perhaps the right term here much like a family tree if you have sort of your
778:23 family tree if you have sort of your elders up here in the tree and then the
778:24 elders up here in the tree and then the branches below them for their children
778:26 branches below them for their children and grandchildren and the like that's
778:28 and grandchildren and the like that's actually what a computer scientist means
778:29 actually what a computer scientist means when they talk about trees not a tree
778:31 when they talk about trees not a tree that grows up like this but really one
778:33 that grows up like this but really one that typically is depicted growing down
778:35 that typically is depicted growing down although this is just an artist's
778:37 although this is just an artist's depiction no matter what but there are
778:39 depiction no matter what but there are certain types of trees in the world
778:41 certain types of trees in the world called binary search trees that are
778:43 called binary search trees that are structured on paper and in visually like
778:45 structured on paper and in visually like a uh family tree but they have a special
778:48 a uh family tree but they have a special property that lends themselves to
778:50 property that lends themselves to exactly that feature binary search so
778:52 exactly that feature binary search so for instance here is an array back from
778:55 for instance here is an array back from week two and I've sorted a whole bunch
778:57 week two and I've sorted a whole bunch of numbers here in from 1 to seven we
778:59 of numbers here in from 1 to seven we know we can do binary search on this
779:02 know we can do binary search on this structure if it's implemented as an
779:04 structure if it's implemented as an array but what feature do arrays to be
779:06 array but what feature do arrays to be clear not have that link lists
779:09 clear not have that link lists do today's kind of a seesaw like what
779:12 do today's kind of a seesaw like what did we just gain by adding link list
779:14 did we just gain by adding link list that arrays do not
779:16 that arrays do not allow
779:18 allow yeah
779:21 yeah yeah you can insert more elements
779:23 yeah you can insert more elements without having to copy or moving
779:25 without having to copy or moving everything else around like right now in
779:26 everything else around like right now in this single Dimension if these values to
779:29 this single Dimension if these values to the left and or right are already used
779:31 the left and or right are already used then you have to move everything and
779:33 then you have to move everything and that's where we started today's story so
779:34 that's where we started today's story so arrays kind of paint you into a corner
779:36 arrays kind of paint you into a corner because you have to by definition decide
779:38 because you have to by definition decide in advance how big they are well
779:40 in advance how big they are well couldn't we have some kind of array that
779:42 couldn't we have some kind of array that can still grow but still is contiguous
779:45 can still grow but still is contiguous so we can do binary search in some way
779:48 so we can do binary search in some way well yes if we sort of rethink how we
779:51 well yes if we sort of rethink how we Implement binary search Let Me propose
779:53 Implement binary search Let Me propose that this I've chosen these seven
779:56 that this I've chosen these seven elements in the array much like the
779:58 elements in the array much like the lockers from uh week two to be ordered
780:01 lockers from uh week two to be ordered from smallest to largest I've
780:03 from smallest to largest I've highlighted now in yellow the middle
780:05 highlighted now in yellow the middle elements here and if we were telling the
780:07 elements here and if we were telling the story of week two going left or going
780:09 story of week two going left or going right let me highlight in red the middle
780:11 right let me highlight in red the middle elements of the left half and the right
780:13 elements of the left half and the right half and then let me further highlight
780:15 half and then let me further highlight in green the other elements in between
780:18 in green the other elements in between those and there's a pattern here as you
780:21 those and there's a pattern here as you might notice whereby there's one yellow
780:23 might notice whereby there's one yellow in the middle and then there's the two
780:24 in the middle and then there's the two red and the four green there's kind of
780:26 red and the four green there's kind of an implicit structure there if you will
780:28 an implicit structure there if you will and what if I do start to think in two
780:30 and what if I do start to think in two dimensions and instead of laying out an
780:32 dimensions and instead of laying out an array of lockers like this on the x-axis
780:35 array of lockers like this on the x-axis only what if I kind of like Slide the
780:38 only what if I kind of like Slide the four up and pull the uh the the one the
780:41 four up and pull the uh the the one the three the five down and kind of draw
780:44 three the five down and kind of draw this in two Dimensions instead well let
780:46 this in two Dimensions instead well let me do that as by separating these things
780:49 me do that as by separating these things like this such that now let me propose
780:52 like this such that now let me propose that each of these squares maybe it
780:54 that each of these squares maybe it doesn't have to be contiguous it can be
780:56 doesn't have to be contiguous it can be anywhere in the computer's memory but I
780:58 anywhere in the computer's memory but I can't have these crazy gaps among them
781:01 can't have these crazy gaps among them how could I perhaps keep these things
781:03 how could I perhaps keep these things connected conceptually what should I add
781:06 connected conceptually what should I add to the picture if you
781:13 will yeah branches say again so branches metaphorically here and more technically
781:15 metaphorically here and more technically in the language of C maybe just some
781:17 in the language of C maybe just some arrows some pointers so I won't bother
781:20 arrows some pointers so I won't bother drawing things as rectangles constantly
781:22 drawing things as rectangles constantly let me propose that we're now just
781:23 let me propose that we're now just abstracting away what a node is but let
781:25 abstracting away what a node is but let me claim that each of these squares now
781:27 me claim that each of these squares now is a node and a node might have a number
781:29 is a node and a node might have a number but it might also have a pointer heck
781:30 but it might also have a pointer heck maybe even two or more pointers and let
781:33 maybe even two or more pointers and let me draw those now I don't care about
781:35 me draw those now I don't care about addresses like ox1 2 3 4 5 6 7 8 n
781:37 addresses like ox1 2 3 4 5 6 7 8 n anymore let's just draw our pointers
781:39 anymore let's just draw our pointers with arrows but now let me propose that
781:42 with arrows but now let me propose that we could very well think about this as a
781:46 we could very well think about this as a tree storing what was previous
781:49 tree storing what was previous previously array data but now each of
781:52 previously array data but now each of these nodes can be anywhere in memory
781:55 these nodes can be anywhere in memory and moreover even though I've kind of
781:56 and moreover even though I've kind of painted myself into a corner visually on
781:58 painted myself into a corner visually on the screen so long as there's more
782:00 the screen so long as there's more memory in the computer I could put the
782:01 memory in the computer I could put the number zero over here I could put the
782:04 number zero over here I could put the number eight over here and I'm if I'm
782:06 number eight over here and I'm if I'm smart I could probably if I want to
782:08 smart I could probably if I want to insert other numbers like 2.5 or 1.5 or
782:11 insert other numbers like 2.5 or 1.5 or values in between you know I bet we
782:13 values in between you know I bet we could kind of make room by swiveling
782:15 could kind of make room by swiveling things around and just kind of hanging
782:17 things around and just kind of hanging things off of these branches slightly
782:19 things off of these branches slightly differently and so what does this gain
782:21 differently and so what does this gain me well if I instead start to model my
782:24 me well if I instead start to model my data not single dimensionally but in two
782:26 data not single dimensionally but in two dimensions and I connect those nodes
782:29 dimensions and I connect those nodes with these pointers what can I now do I
782:32 with these pointers what can I now do I think I just gave myself back binary
782:34 think I just gave myself back binary search why suppose I'm searching for the
782:36 search why suppose I'm searching for the number five how do I find it Well just
782:38 number five how do I find it Well just like in a family tree where you might
782:39 like in a family tree where you might visually start reading from top to
782:41 visually start reading from top to bottom I'm always going to start from
782:42 bottom I'm always going to start from the so-called root of a binary search
782:44 the so-called root of a binary search tree this is just like the list pointer
782:47 tree this is just like the list pointer that kicks off the whole link list
782:49 that kicks off the whole link list process
782:50 process this is the so-called route all right
782:51 this is the so-called route all right here I am at the number four I want to
782:53 here I am at the number four I want to find the number five what decision can I
782:56 find the number five what decision can I make when I see that I'm currently at
782:57 make when I see that I'm currently at the number four just like the phone book
782:59 the number four just like the phone book from week zero where is five
783:03 from week zero where is five not it's not to the left and if I were
783:06 not it's not to the left and if I were had you know built a little mobile here
783:07 had you know built a little mobile here or something we could very dramatically
783:09 or something we could very dramatically snip off this Branch this is this is
783:12 snip off this Branch this is this is like very lowbudget animation these
783:14 like very lowbudget animation these nodes could like fall to the ground and
783:16 nodes could like fall to the ground and we're left with half of essentially a
783:18 we're left with half of essentially a tree but what do I now know it's
783:20 tree but what do I now know it's obviously the five to the right so let
783:21 obviously the five to the right so let me go to the right six is obviously not
783:24 me go to the right six is obviously not the one I'm looking for but what do I
783:25 the one I'm looking for but what do I now know about the five well five is
783:27 now know about the five well five is less than the sixth so I can sort of
783:30 less than the sixth so I can sort of snip this off here because I know it's
783:32 snip this off here because I know it's not going to be down there and I can
783:34 not going to be down there and I can follow the remaining Arrow here and
783:36 follow the remaining Arrow here and voila I just found it and now without
783:38 voila I just found it and now without getting into the weeds of the math I've
783:40 getting into the weeds of the math I've got here what Seven Elements that's
783:42 got here what Seven Elements that's roughly eight if I round up and if I do
783:44 roughly eight if I round up and if I do some log base two I actually 1 two three
783:46 some log base two I actually 1 two three is the key detail here the height of
783:48 is the key detail here the height of this tree
783:50 this tree is three because I took a list of size
783:52 is three because I took a list of size seven and IED it and IED it in order to
783:55 seven and IED it and IED it in order to let it dangle in these two Dimensions
783:57 let it dangle in these two Dimensions plus or minus one for rounding sake so
783:59 plus or minus one for rounding sake so what do I get back I now have binary
784:02 what do I get back I now have binary search but it's not like H the middle of
784:04 search but it's not like H the middle of the middle of the middle I now follow
784:06 the middle of the middle I now follow these arrows in one of two directions so
784:08 these arrows in one of two directions so each of these nodes now has an INT and
784:10 each of these nodes now has an INT and maybe a left pointer and a right pointer
784:13 maybe a left pointer and a right pointer but you can call them anything you want
784:14 but you can call them anything you want and so I've gotten back binary search
784:17 and so I've gotten back binary search and dynamism because if you want to add
784:19 and dynamism because if you want to add zero or eight or 9 or 10 we can just
784:22 zero or eight or 9 or 10 we can just dangle them at the bottom of the binary
784:24 dangle them at the bottom of the binary search tree so what would this look like
784:26 search tree so what would this look like in code but we won't actually implement
784:28 in code but we won't actually implement it line by line well here was previously
784:31 it line by line well here was previously our definition of a node for a link list
784:34 our definition of a node for a link list which was onedimensional if you will
784:36 which was onedimensional if you will even though it might bounce up and down
784:37 even though it might bounce up and down on the screen it was still just a line
784:39 on the screen it was still just a line if you will well let me get rid of the
784:41 if you will well let me get rid of the single pointer in the linked list let me
784:44 single pointer in the linked list let me make a little bit of room here in this
784:45 make a little bit of room here in this type death and let me propose that we
784:47 type death and let me propose that we just add two pointers each of which is a
784:49 just add two pointers each of which is a struct node star one will be called left
784:52 struct node star one will be called left by convention one will be called right
784:55 by convention one will be called right by convention and so long as someone not
784:57 by convention and so long as someone not me not today not in class writes the
784:59 me not today not in class writes the code that stitches together this data
785:01 code that stitches together this data structure too handling both the left
785:04 structure too handling both the left child and the right child so to speak I
785:06 child and the right child so to speak I think we can indeed stitch together that
785:08 think we can indeed stitch together that two-dimensional structure and moreover
785:11 two-dimensional structure and moreover once you have this in memory you can
785:13 once you have this in memory you can translate pretty elegantly to code
785:16 translate pretty elegantly to code binary search itself using a principle
785:18 binary search itself using a principle we talked about recently too here is for
785:21 we talked about recently too here is for instance a function that I'll write by
785:24 instance a function that I'll write by just clicking through steps called
785:26 just clicking through steps called search whose purpose in life is to
785:28 search whose purpose in life is to return a Boolean true or false the
785:30 return a Boolean true or false the number I'm looking for is in the tree
785:33 number I'm looking for is in the tree this search function therefore takes two
785:34 this search function therefore takes two arguments the number I'm looking for
785:36 arguments the number I'm looking for called number and then a pointer to the
785:38 called number and then a pointer to the tree the so-called root of the tree now
785:41 tree the so-called root of the tree now how can I Implement binary search in
785:43 how can I Implement binary search in code will recall our brief discussion of
785:45 code will recall our brief discussion of recursion it turns out recursion is a
785:48 recursion it turns out recursion is a beautiful technique and honestly more
785:50 beautiful technique and honestly more obvious technique when you have two
785:51 obvious technique when you have two dimensional structures which finally
785:54 dimensional structures which finally after five plus weeks we now do here's
785:56 after five plus weeks we now do here's maybe my first line of code here if the
785:58 maybe my first line of code here if the tree is null then obviously return false
786:01 tree is null then obviously return false you've handed me an empty tree there's
786:02 you've handed me an empty tree there's nothing going on obviously the number
786:04 nothing going on obviously the number you're looking for is not going to be
786:05 you're looking for is not going to be here so that's my like safe base case to
786:07 here so that's my like safe base case to make sure I don't screw up and recurse
786:09 make sure I don't screw up and recurse infinitely well what else might be the
786:11 infinitely well what else might be the case well if the number I'm looking for
786:14 case well if the number I'm looking for is less than the tree's own number and
786:18 is less than the tree's own number and now recall that trees a node star so
786:20 now recall that trees a node star so even though I'm calling it a tree it's
786:21 even though I'm calling it a tree it's really the current node that's been
786:23 really the current node that's been passed in so if the number I'm looking
786:25 passed in so if the number I'm looking for is less than the current nodes
786:28 for is less than the current nodes number then I must know that the number
786:31 number then I must know that the number I'm looking for is to the left so to
786:33 I'm looking for is to the left so to speak so how can I solve that well this
786:35 speak so how can I solve that well this is where the magic of recursion just
786:37 is where the magic of recursion just return whatever the answer is to calling
786:39 return whatever the answer is to calling search again but on a sub tree if you
786:43 search again but on a sub tree if you will this is the sort of equivalent of
786:44 will this is the sort of equivalent of snipping off half of the tree pass in
786:47 snipping off half of the tree pass in the left sub tree if you will with the
786:49 the left sub tree if you will with the same number else if the number you're
786:51 same number else if the number you're looking for isn't less than the current
786:53 looking for isn't less than the current nodes number but greater than snip off
786:56 nodes number but greater than snip off the other subtree instead and just
786:58 the other subtree instead and just return whatever search says it finds in
787:00 return whatever search says it finds in the right subtree here and then there's
787:03 the right subtree here and then there's a fourth and final case what else could
787:05 a fourth and final case what else could be true
787:06 be true logically
787:08 logically [Music]
787:10 [Music] yeah perfect if the number you're
787:12 yeah perfect if the number you're looking for equals equals the number in
787:15 looking for equals equals the number in this node then I'm just going to return
787:17 this node then I'm just going to return true and you might recall from our
787:19 true and you might recall from our recurring discussions of design I don't
787:23 recurring discussions of design I don't strictly need to ask that explicitly
787:25 strictly need to ask that explicitly either there's no node it's to the left
787:27 either there's no node it's to the left it's to the right or you found it so I
787:29 it's to the right or you found it so I can just Whittle that down as usual to
787:31 can just Whittle that down as usual to an else and this now returns my true so
787:34 an else and this now returns my true so here too this is where recursion once
787:36 here too this is where recursion once you get comfy with it sort of gets
787:38 you get comfy with it sort of gets pretty elegant and cool in the sense
787:40 pretty elegant and cool in the sense that wow even though there's a lot of
787:41 that wow even though there's a lot of lines here I mean there's only a few
787:43 lines here I mean there's only a few interesting lines a lot of it's like
787:45 interesting lines a lot of it's like Curly braces at that which strictly
787:46 Curly braces at that which strictly speaking I could get rid of and so
787:48 speaking I could get rid of and so recurs lends itself to Elegance when it
787:50 recurs lends itself to Elegance when it comes to traversing these
787:52 comes to traversing these two-dimensional data structures as well
787:55 two-dimensional data structures as well so that is in code how you might
787:56 so that is in code how you might Implement something like
788:00 Implement something like search questions then on these
788:04 search questions then on these trees we have dynamism we can insert
788:07 trees we have dynamism we can insert more nodes to them they're faster
788:08 more nodes to them they're faster because we get B search back but but but
788:10 because we get B search back but but but there's got to be a price paid any
788:12 there's got to be a price paid any downsides or question or
788:14 downsides or question or downside okay let me come back to that
788:16 downside okay let me come back to that in just one sec downside though what
788:18 in just one sec downside though what price of we paid for this dynamism and
788:20 price of we paid for this dynamism and for this binary
788:22 for this binary searchability even though I've
788:24 searchability even though I've abstracted it away in the
788:27 abstracted it away in the picture say
788:29 picture say again we're using a lot of memory right
788:32 again we're using a lot of memory right I'm kind of misleading you now because
788:33 I'm kind of misleading you now because I'm just drawing these little squares
788:34 I'm just drawing these little squares with the simple numbers but there's
788:36 with the simple numbers but there's actually three things in there a four
788:38 actually three things in there a four byte integer an 8 by left pointer a 8 by
788:41 byte integer an 8 by left pointer a 8 by right pointer so we're already up to 16
788:43 right pointer so we're already up to 16 20 bytes now to store individual ins
788:46 20 bytes now to store individual ins that's probably okay though if memory is
788:47 that's probably okay though if memory is relatively cheap and voluminous as it
788:49 relatively cheap and voluminous as it nowadays is but these are the kinds of
788:52 nowadays is but these are the kinds of trade-offs and here too you see a hint
788:54 trade-offs and here too you see a hint of why some people still do like and use
788:57 of why some people still do like and use C and in fact it's so omnipresent
789:00 C and in fact it's so omnipresent because when you have C you can really
789:01 because when you have C you can really fine-tune how much memory is being used
789:03 fine-tune how much memory is being used for Better or For Worse under the hood
789:05 for Better or For Worse under the hood as we transition soon to python these
789:07 as we transition soon to python these decisions get made for you and you have
789:09 decisions get made for you and you have much much less control about how many me
789:11 much much less control about how many me how much memory is being used by your
789:13 how much memory is being used by your program because someone else made those
789:14 program because someone else made those designed decisions for you
789:17 designed decisions for you question
789:20 question is it bad if we don't know the parent
789:21 is it bad if we don't know the parent node uh not necessarily there's no
789:24 node uh not necessarily there's no reason why you need to have pointers in
789:27 reason why you need to have pointers in both directions however that can lend
789:29 both directions however that can lend itself to efficiency by spending more
789:31 itself to efficiency by spending more space and having arrows go up too you
789:33 space and having arrows go up too you can actually save more time when
789:34 can actually save more time when searching the tree in other context this
789:36 searching the tree in other context this though would be the canonical way the
789:38 though would be the canonical way the typical way to implement it um but
789:40 typical way to implement it um but absolutely just like a doubly link list
789:42 absolutely just like a doubly link list that could help you solve other problems
789:44 that could help you solve other problems too all right so turns out I'm kind of
789:48 too all right so turns out I'm kind of overselling binary search trees there
789:50 overselling binary search trees there are perversions of them so to speak
789:52 are perversions of them so to speak whereby they won't actually behave as
789:54 whereby they won't actually behave as advertised for instance here's a a good
789:57 advertised for instance here's a a good situation suppose you've got an empty
789:59 situation suppose you've got an empty tree initially and you insert the number
790:01 tree initially and you insert the number two well it's got to go somewhere so it
790:03 two well it's got to go somewhere so it might as well become the root of this
790:04 might as well become the root of this binary search tree and let's assume that
790:06 binary search tree and let's assume that someone wrote the code to do this now
790:08 someone wrote the code to do this now you want to insert the number one and
790:10 you want to insert the number one and you want to maintain the searchability
790:11 you want to maintain the searchability of this tree well it's important to note
790:14 of this tree well it's important to note that binary search tree is different
790:16 that binary search tree is different from tree if you just got a tree in
790:18 from tree if you just got a tree in memory there is no social contract with
790:21 memory there is no social contract with where the numbers need to go they can be
790:23 where the numbers need to go they can be completely random all over the place
790:25 completely random all over the place binary search tree means that you can do
790:27 binary search tree means that you can do binary search means that any node here
790:30 binary search means that any node here is going to be greater than every node
790:32 is going to be greater than every node here and less than every node here and
790:35 here and less than every node here and that's a definition it's a recursive
790:37 that's a definition it's a recursive structural definition that must be true
790:39 structural definition that must be true to be a binary search tree or BST so if
790:42 to be a binary search tree or BST so if we maintain that property ourselves let
790:44 we maintain that property ourselves let me insert two let me insert one one
790:46 me insert two let me insert one one belongs there by that definition let me
790:48 belongs there by that definition let me insert three three belongs there by that
790:51 insert three three belongs there by that definition but I kind of got lucky in
790:53 definition but I kind of got lucky in that I in the story inserted two and
790:56 that I in the story inserted two and then one and then three let me propose a
790:59 then one and then three let me propose a sort of perversion of the algorithm
791:00 sort of perversion of the algorithm whereby we just get unlucky let me
791:03 whereby we just get unlucky let me propose that we insert in uh insert one
791:05 propose that we insert in uh insert one first and then we insert two well where
791:08 first and then we insert two well where does two go well logically it goes to
791:10 does two go well logically it goes to the right because it's larger all right
791:11 the right because it's larger all right now the user inserts three where does it
791:14 now the user inserts three where does it go okay it goes there logically and how
791:17 go okay it goes there logically and how does this story un fold the user inserts
791:20 does this story un fold the user inserts four five six it's wonderfully sorted in
791:22 four five six it's wonderfully sorted in advance by luck but this is a perversion
791:26 advance by luck but this is a perversion of the structure in what
791:28 of the structure in what sense it's still technically a binary
791:30 sense it's still technically a binary search tree but what does it look more
791:35 search tree but what does it look more like it really is devolving if you will
791:38 like it really is devolving if you will into a linked list and so if you the
791:40 into a linked list and so if you the programmer don't Implement a binary
791:42 programmer don't Implement a binary search tree with some kind of repairs
791:45 search tree with some kind of repairs going on such that as soon as something
791:47 going on such that as soon as something gets whoa a little too long in stringy I
791:49 gets whoa a little too long in stringy I think I can fix this it's going to be an
791:51 think I can fix this it's going to be an annoying line number of lines of code
791:52 annoying line number of lines of code which we're not going to write here or
791:54 which we're not going to write here or or in a pet but we could kind of pivot
791:57 or in a pet but we could kind of pivot this thing right and we could just
791:58 this thing right and we could just rejigger things so that the two becomes
792:00 rejigger things so that the two becomes the new root the one becomes the left
792:02 the new root the one becomes the left child the three becomes the right child
792:04 child the three becomes the right child but that's what like two three plus
792:06 but that's what like two three plus lines of code it's possible it's doable
792:08 lines of code it's possible it's doable but it's it's extra work it's extra code
792:10 but it's it's extra work it's extra code so unless you write that code though and
792:13 so unless you write that code though and maintain balance of these trees just
792:16 maintain balance of these trees just because it's a binary search tree does
792:17 because it's a binary search tree does not mean its height is going to be log
792:19 not mean its height is going to be log base 2 of n the height could be log it
792:22 base 2 of n the height could be log it could be n in which case you don't get
792:24 could be n in which case you don't get those properties so when it comes to
792:26 those properties so when it comes to looking up in a balanced binary search
792:28 looking up in a balanced binary search tree yes it's log in but if it's
792:30 tree yes it's log in but if it's unbalanced if you don't add that
792:32 unbalanced if you don't add that additional logic and those repairs so to
792:34 additional logic and those repairs so to speak you could it could devolve into
792:36 speak you could it could devolve into Big O of N and this is a whole category
792:39 Big O of N and this is a whole category of algorithms and fanciness that you
792:40 of algorithms and fanciness that you would explore in a higher level course
792:42 would explore in a higher level course on algorithms and data structures
792:44 on algorithms and data structures there's lots of way to do that sort of
792:45 there's lots of way to do that sort of fixing that I'm alluding to in the
792:47 fixing that I'm alluding to in the picture there on the screen
792:49 picture there on the screen screen all right a few other data
792:51 screen all right a few other data structures if you will toward an end of
792:54 structures if you will toward an end of a sort of computer science Holy Grail so
792:56 a sort of computer science Holy Grail so log in is repeatedly a really good place
793:00 log in is repeatedly a really good place to end up we started in week zero when
793:02 to end up we started in week zero when we got log in we lost it this earlier
793:04 we got log in we lost it this earlier today by introducing link list but we
793:06 today by introducing link list but we just got it back albeit at the price of
793:08 just got it back albeit at the price of spending more space but the Holy Grail
793:11 spending more space but the Holy Grail so to speak when it comes to algorithms
793:13 so to speak when it comes to algorithms would not be Big O of n certainly
793:14 would not be Big O of n certainly definitely not n squar like our mer like
793:16 definitely not n squar like our mer like our bubble sorts and selection sorts and
793:18 our bubble sorts and selection sorts and not even Big O of Logan what's better
793:20 not even Big O of Logan what's better than all of
793:23 than all of those big O of one constant time right
793:26 those big O of one constant time right that's the Holy Grail because if we
793:27 that's the Holy Grail because if we could store huge amounts of data but
793:29 could store huge amounts of data but find it instantly in one step or two
793:32 find it instantly in one step or two steps or heck even 10 or 20 steps but
793:34 steps or heck even 10 or 20 steps but independent of the size of the data
793:36 independent of the size of the data structure that's pretty powerful I mean
793:38 structure that's pretty powerful I mean that's the secret sauce of the Googles
793:40 that's the secret sauce of the Googles and the twitters of the world trying to
793:41 and the twitters of the world trying to get back results really really fast well
793:44 get back results really really fast well it turns out another abstract data type
793:47 it turns out another abstract data type or abstract data structure might be
793:49 or abstract data structure might be something called a dictionary just like
793:50 something called a dictionary just like the maram Webster Oxford English
793:52 the maram Webster Oxford English dictionaries that you might know which
793:54 dictionaries that you might know which associate say words with definitions
793:56 associate say words with definitions well you can think of a dictionary
793:57 well you can think of a dictionary really abstractly as this like two
793:59 really abstractly as this like two columns maybe on a spreadsheet of sorts
794:01 columns maybe on a spreadsheet of sorts where the left column represents
794:03 where the left column represents something and the right column
794:04 something and the right column represents something else like the word
794:05 represents something else like the word is on the left and its definition is on
794:07 is on the left and its definition is on the right and that's almost literally
794:09 the right and that's almost literally what a dictionary is on paper you've got
794:10 what a dictionary is on paper you've got all the words and all the definitions
794:12 all the words and all the definitions right next to it but more generally in
794:15 right next to it but more generally in Computing a dictionary really just has
794:18 Computing a dictionary really just has not words and definitions per se but key
794:20 not words and definitions per se but key value pairs this is a term of Art and
794:22 value pairs this is a term of Art and we're going to see this again and again
794:24 we're going to see this again and again especially as we transition to web
794:25 especially as we transition to web programming keys and values key is what
794:28 programming keys and values key is what you use to look for something the value
794:31 you use to look for something the value is what you find ultimately via that key
794:34 is what you find ultimately via that key so that's the generic term there we've
794:36 so that's the generic term there we've seen key value pairs really in the past
794:38 seen key value pairs really in the past in week zero we talked about your
794:40 in week zero we talked about your contacts in your iPhone or Android phone
794:42 contacts in your iPhone or Android phone uh being an app that has a whole bunch
794:43 uh being an app that has a whole bunch of contexts presumably alphabetized by
794:45 of contexts presumably alphabetized by first name or last name or the like well
794:48 first name or last name or the like well one one of those contact cards
794:49 one one of those contact cards ultimately has someone's number for
794:51 ultimately has someone's number for instance like John Harvard in this case
794:53 instance like John Harvard in this case so in that type of application the keys
794:56 so in that type of application the keys is the name like John Harvard that you
794:58 is the name like John Harvard that you use to find information and the value is
795:01 use to find information and the value is the number that you find there or if
795:03 the number that you find there or if there's more information like where he
795:05 there's more information like where he lives and uh email address and the like
795:07 lives and uh email address and the like the whole contact card could be the
795:09 the whole contact card could be the value thereof the key is what you use to
795:11 value thereof the key is what you use to look up John Harvard now back in week
795:14 look up John Harvard now back in week zero oh and rather the corresponding
795:16 zero oh and rather the corresponding table then if we draw this in two
795:17 table then if we draw this in two columns wouldn't be word and definition
795:19 columns wouldn't be word and definition or key value generically it would be
795:21 or key value generically it would be name and number for instance so we're
795:23 name and number for instance so we're just slapping some new terminology on
795:25 just slapping some new terminology on this old contact problem well this is
795:27 this old contact problem well this is the picture we drew way back in week
795:29 the picture we drew way back in week zero whereby I claimed that log of n was
795:32 zero whereby I claimed that log of n was really really good and indeed it was and
795:34 really really good and indeed it was and has been since but the Holy Grail would
795:36 has been since but the Holy Grail would indeed be something more like this in
795:38 indeed be something more like this in this dashed Green Line constant time and
795:41 this dashed Green Line constant time and maybe not literally one step but a fixed
795:43 maybe not literally one step but a fixed number of steps that even as the problem
795:45 number of steps that even as the problem gets huge and you go way way out on the
795:47 gets huge and you go way way out on the right of the X AIS the problem does not
795:50 right of the X AIS the problem does not depend on the side the uh the uh the
795:54 depend on the side the uh the uh the time to solve the problem does not
795:55 time to solve the problem does not depend at all on the size of the problem
795:58 depend at all on the size of the problem itself you can have a thousand contacts
796:00 itself you can have a thousand contacts or 100,000 contacts constant time means
796:03 or 100,000 contacts constant time means it takes the same number of steps no
796:05 it takes the same number of steps no matter what well how can we get to that
796:07 matter what well how can we get to that point well there's a couple of final
796:09 point well there's a couple of final building blocks today and there's one
796:11 building blocks today and there's one called hashing and this is something
796:12 called hashing and this is something that will recur a few times but for now
796:14 that will recur a few times but for now hashing is all about taking as input
796:17 hashing is all about taking as input some value
796:19 some value and outputting a simpler version thereof
796:22 and outputting a simpler version thereof so for instance here's a a gratuitously
796:25 so for instance here's a a gratuitously large deck of cards which are all the
796:27 large deck of cards which are all the more visible as a result and in a deck
796:29 more visible as a result and in a deck of cards typically you've got like what
796:30 of cards typically you've got like what 52 cards plus maybe the Jokers and
796:32 52 cards plus maybe the Jokers and whatnot and each of those cards has a
796:34 whatnot and each of those cards has a number of sorts and a suit on it and
796:37 number of sorts and a suit on it and here are literally four buckets on the
796:38 here are literally four buckets on the stage and how might I go about sorting
796:41 stage and how might I go about sorting these cards not just by number but also
796:43 these cards not just by number but also by suit well you could certainly like
796:44 by suit well you could certainly like spread them all out and sort of make a
796:46 spread them all out and sort of make a mess of things and just kind of reason
796:48 mess of things and just kind of reason your way through it and get everything
796:49 your way through it and get everything in order according to suit and corded by
796:51 in order according to suit and corded by number but most of us even if you don't
796:53 number but most of us even if you don't have four buckets at home probably are
796:55 have four buckets at home probably are going to do something a little more
796:57 going to do something a little more intuitive feels like an optimization
796:59 intuitive feels like an optimization where if I find like the nine of Hearts
797:01 where if I find like the nine of Hearts I'm going to put that into the hearts
797:02 I'm going to put that into the hearts bucket the King of Spades I'm going to
797:04 bucket the King of Spades I'm going to put that into the Spades bucket the jack
797:06 put that into the Spades bucket the jack of diamonds over here and I'll do this
797:08 of diamonds over here and I'll do this with the Queen of Diamonds and uh the
797:11 with the Queen of Diamonds and uh the Ace of clubs here and the three here and
797:13 Ace of clubs here and the three here and the 10 here and even though it's still
797:15 the 10 here and even though it's still going to be 52 steps why am I and maybe
797:18 going to be 52 steps why am I and maybe at home like why would you perhaps do
797:20 at home like why would you perhaps do this step
797:21 this step first what's the value of bucketization
797:49 reduces the probability of errors or the like and what I'm doing here to give it
797:51 like and what I'm doing here to give it a technical term is that I'm hashing the
797:53 a technical term is that I'm hashing the values I'm taking as input a card like
797:55 values I'm taking as input a card like this and I'm reducing it more simply
797:58 this and I'm reducing it more simply from a larger domain to a much smaller
798:01 from a larger domain to a much smaller range if you will so here's a domain of
798:04 range if you will so here's a domain of like 52 possibilities I want to map that
798:06 like 52 possibilities I want to map that to a range of four possible outcomes the
798:09 to a range of four possible outcomes the Diamonds the clubs the carts or the
798:12 Diamonds the clubs the carts or the Spades here and by doing that I'm just
798:14 Spades here and by doing that I'm just shrinking the size of the problem so
798:15 shrinking the size of the problem so hashing does that it's like literally an
798:17 hashing does that it's like literally an F ofx type Arrangement whereby you pass
798:19 F ofx type Arrangement whereby you pass something in and you get back a simpler
798:22 something in and you get back a simpler known value well a hash function more
798:25 known value well a hash function more technically is the algorithm or even the
798:27 technically is the algorithm or even the math or even the code that implements
798:29 math or even the code that implements that idea converting something bigger to
798:32 that idea converting something bigger to something smaller to this indeed finite
798:34 something smaller to this indeed finite range of values and it turns out that
798:37 range of values and it turns out that hash tables are a wonderful application
798:42 hash tables are a wonderful application of arrays and length lists to try to
798:45 of arrays and length lists to try to Leverage The Best of Both Worlds the
798:47 Leverage The Best of Both Worlds the goal being theoretically to achieve that
798:49 goal being theoretically to achieve that Holy Grail of constant time and that's
798:52 Holy Grail of constant time and that's going to be a bit of an overstatement
798:53 going to be a bit of an overstatement because you're not always going to
798:54 because you're not always going to achieve it exactly but at least we can
798:56 achieve it exactly but at least we can get a little closer there too so with
798:59 get a little closer there too so with hash tables you have something that
799:00 hash tables you have something that looks like this this is just an array
799:03 looks like this this is just an array this is an artist rendition of drawing
799:04 this is an artist rendition of drawing it vertically instead of horizontally
799:06 it vertically instead of horizontally but that's just a a detail graphically
799:09 but that's just a a detail graphically and this array for instance maybe uh is
799:12 and this array for instance maybe uh is of size 26 and where am I going with
799:15 of size 26 and where am I going with this well how does Apple how does Google
799:17 this well how does Apple how does Google Store your contexts alphabetically in
799:20 Store your contexts alphabetically in your phone and search for things quickly
799:22 your phone and search for things quickly well they might they probably
799:24 well they might they probably alphabetize at least in English
799:25 alphabetize at least in English according to a through z or if we
799:27 according to a through z or if we convert that to numbers it's like what
799:28 convert that to numbers it's like what 65 through whatever or really 0 through
799:31 65 through whatever or really 0 through 25 suffices if we're using an array of
799:33 25 suffices if we're using an array of size 26 we start counting at zero and we
799:36 size 26 we start counting at zero and we count up to 25 but let's abstract that
799:38 count up to 25 but let's abstract that away as just letters of the alphabet so
799:40 away as just letters of the alphabet so maybe what Google and apple are doing in
799:42 maybe what Google and apple are doing in your phone is storing all of the A's up
799:44 your phone is storing all of the A's up there all of the Z's down there and
799:46 there all of the Z's down there and everything else in between and so this
799:48 everything else in between and so this works pretty well if you start adding
799:50 works pretty well if you start adding your friends and your family so for
799:51 your friends and your family so for instance and I'll get rid of the letter
799:53 instance and I'll get rid of the letter so it's to not distract uh alvus might
799:55 so it's to not distract uh alvus might go in that first spot because a you
799:57 go in that first spot because a you subtract the 65 maps to zero so we put
800:00 subtract the 65 maps to zero so we put him in the first bucket the a bucket uh
800:02 him in the first bucket the a bucket uh maybe Zacharias ends up all the way at
800:03 maybe Zacharias ends up all the way at the end there and then in the middle
800:05 the end there and then in the middle might here be Hermione and if we do this
800:07 might here be Hermione and if we do this dot dot dot you keep adding all of your
800:08 dot dot dot you keep adding all of your classmates you might get a uh contact
800:12 classmates you might get a uh contact database that has all of this data here
800:14 database that has all of this data here in now each of these nodes they're drawn
800:17 in now each of these nodes they're drawn differently because this is just another
800:18 differently because this is just another artist rendition these rectangles these
800:21 artist rendition these rectangles these long rectangles represent a contact card
800:23 long rectangles represent a contact card like John Harvard's that's got the name
800:24 like John Harvard's that's got the name maybe email definitely phone number and
800:26 maybe email definitely phone number and things like that so this seems great why
800:31 things like that so this seems great why how can I find Albus well I go to the a
800:34 how can I find Albus well I go to the a bucket how do I find Zacharias I go to
800:36 bucket how do I find Zacharias I go to the Z bucket how do I find Hermione I go
800:37 the Z bucket how do I find Hermione I go to the H bucket but but but I've done
800:41 to the H bucket but but but I've done this very deliberately what problem will
800:44 this very deliberately what problem will arise eventually assuming you have
800:46 arise eventually assuming you have enough classmates
800:53 yeah there'll be too many people too many contacts for all of the available
800:55 many contacts for all of the available spaces in the array there's still some
800:57 spaces in the array there's still some room here but I'm pretty sure if I think
801:00 room here but I'm pretty sure if I think back to this particular class uh we've
801:02 back to this particular class uh we've got not hermy but also Harry who's also
801:05 got not hermy but also Harry who's also an H Hagrid who's also an H so where do
801:08 an H Hagrid who's also an H so where do I put them I could just put them
801:10 I put them I could just put them arbitrarily in any of the open spots but
801:13 arbitrarily in any of the open spots but then you lose the immediacy of jumping
801:15 then you lose the immediacy of jumping right to the H right to the a right to
801:16 right to the H right to the a right to the Z but now that we have link lists we
801:19 the Z but now that we have link lists we can kind of combine these ideas right
801:20 can kind of combine these ideas right use an array to get to the first letter
801:23 use an array to get to the first letter of the name you care about and then if
801:25 of the name you care about and then if you have a collision so to speak whereby
801:28 you have a collision so to speak whereby someone's already there you don't do
801:29 someone's already there you don't do something stupid like put Harry down
801:31 something stupid like put Harry down here just because it's available or
801:33 here just because it's available or maybe Hagrid down here just because it's
801:35 maybe Hagrid down here just because it's available because then you're losing the
801:36 available because then you're losing the immediacy of the lookup why don't you
801:38 immediacy of the lookup why don't you just kind of stitch them together in a
801:40 just kind of stitch them together in a linked list now what does this mean this
801:43 linked list now what does this mean this means for most of the characters here
801:46 means for most of the characters here you have constant time lookup you look
801:47 you have constant time lookup you look up alvas boom you're done Zacharias boom
801:49 up alvas boom you're done Zacharias boom you're done okay Harry Hermione Hagrid
801:53 you're done okay Harry Hermione Hagrid it might be one two or three steps so
801:55 it might be one two or three steps so that's actually devolving into something
801:57 that's actually devolving into something linear but here we make a distinction
801:59 linear but here we make a distinction today between theoretical running times
802:01 today between theoretical running times which we keep talking about and honestly
802:03 which we keep talking about and honestly a clock on the wall running times that
802:05 a clock on the wall running times that actual humans care about this is way
802:08 actual humans care about this is way faster than a linked list because you
802:11 faster than a linked list because you don't have to search every name it's
802:13 don't have to search every name it's even faster than a an array because you
802:16 even faster than a an array because you don't need to do binary search
802:18 don't need to do binary search you can literally for most of the names
802:20 you can literally for most of the names find them in constant time one step and
802:24 find them in constant time one step and again it's not theoretically constant
802:25 again it's not theoretically constant because these if you only befriend
802:27 because these if you only befriend people who have H names it's going to be
802:28 people who have H names it's going to be a crazy long link list anyway so again
802:31 a crazy long link list anyway so again it really kind of depends on what the
802:33 it really kind of depends on what the nature of the data is here but this is
802:35 nature of the data is here but this is pretty close to constant time and in
802:38 pretty close to constant time and in fact how could we get even closer how
802:39 fact how could we get even closer how could we reduce the probability of
802:41 could we reduce the probability of collisions for the H's or any other
802:44 collisions for the H's or any other letters how could we avoid putting too
802:46 letters how could we avoid putting too many H names together
802:59 L okay yeah so we could add another dimension if you will but let's not add
803:01 dimension if you will but let's not add a third dimension per se but let's
803:02 a third dimension per se but let's indeed look at not just the first letter
803:04 indeed look at not just the first letter of everyone's name but the first and the
803:06 of everyone's name but the first and the second and in fact let's see if that
803:08 second and in fact let's see if that gets us a little uh further along so let
803:10 gets us a little uh further along so let me go ahead and propose if you go
803:12 me go ahead and propose if you go through the whole Harry Potter Universe
803:13 through the whole Harry Potter Universe there's actually a lot of collisions if
803:15 there's actually a lot of collisions if we keep going and so we've got the L's
803:17 we keep going and so we've got the L's here the RS the S's and so forth well
803:19 here the RS the S's and so forth well let's clean this up here Hermione
803:21 let's clean this up here Hermione originally went to the H location but
803:23 originally went to the H location but let's decrease the probability of
803:25 let's decrease the probability of collisions there and everywhere else
803:27 collisions there and everywhere else instead of putting hermion Harry and
803:29 instead of putting hermion Harry and Hagrid Al together let's go ahead and do
803:32 Hagrid Al together let's go ahead and do this instead instead of labeling these
803:34 this instead instead of labeling these buckets A through Z let's just give
803:36 buckets A through Z let's just give ourselves more buckets so in fact this
803:38 ourselves more buckets so in fact this might be H well instead of H maybe this
803:40 might be H well instead of H maybe this should be ha and then this should be HB
803:43 should be ha and then this should be HB HC HD h e HF now some of those are a
803:45 HC HD h e HF now some of those are a little nonsensical cuz I can't think of
803:47 little nonsensical cuz I can't think of names that match most of those but it's
803:50 names that match most of those but it's deterministic at least we know the
803:52 deterministic at least we know the bucket will be there which is important
803:54 bucket will be there which is important even if it's empty and now we can put
803:56 even if it's empty and now we can put Hermione here we can put Harry here but
803:59 Hermione here we can put Harry here but uhoh we didn't do this perfectly well
804:01 uhoh we didn't do this perfectly well Hagrid still Collide so let me come back
804:03 Hagrid still Collide so let me come back to you how can we reduce the probability
804:05 to you how can we reduce the probability of Harry and Hagrid
804:07 of Harry and Hagrid colliding yeah so we could look at the
804:09 colliding yeah so we could look at the third letter okay so let me try that
804:11 third letter okay so let me try that instead of ha let's look at haa ha ha C
804:15 instead of ha let's look at haa ha ha C dot dot dot haq dot dot dot h h e q h e
804:18 dot dot dot haq dot dot dot h h e q h e r hes and so forth and now I think those
804:22 r hes and so forth and now I think those names and probably all the others we saw
804:24 names and probably all the others we saw are now much more cleanly distributed
804:27 are now much more cleanly distributed there's much lower probability of
804:29 there's much lower probability of collisions unless two people have like
804:31 collisions unless two people have like almost the same names or one is like a
804:33 almost the same names or one is like a prefix of the other but but but even
804:35 prefix of the other but but but even though we're now closer than ever to
804:38 though we're now closer than ever to constant time because the odds that we
804:40 constant time because the odds that we hit a collision and have to devolve to a
804:42 hit a collision and have to devolve to a link list or much lower what's the
804:44 link list or much lower what's the downside that's not completely obvious
804:46 downside that's not completely obvious from how I've depicted this on
804:49 from how I've depicted this on screen what's the price I'm paying here
804:52 screen what's the price I'm paying here yeah this is a huge amount of memory the
804:55 yeah this is a huge amount of memory the number of cells here in the array is now
804:57 number of cells here in the array is now what 26 * 26 * 26 for the first the
805:01 what 26 * 26 * 26 for the first the second and the third possible characters
805:03 second and the third possible characters all combinatorically combined here
805:05 all combinatorically combined here that's a lot I didn't even draw them I
805:07 that's a lot I didn't even draw them I have the dot dot dot to evoke that
805:09 have the dot dot dot to evoke that instead that's a huge amount of memory
805:11 instead that's a huge amount of memory this is a very sparse data set now and
805:13 this is a very sparse data set now and odds are you're going to waste so much
805:15 odds are you're going to waste so much memory even for the names like Hae ha
805:19 memory even for the names like Hae ha like HQ like I can't even think of names
805:21 like HQ like I can't even think of names so many of those buckets are going to be
805:23 so many of those buckets are going to be empty not to mention the AAA and the zzz
805:26 empty not to mention the AAA and the zzz and everything else in between so it's a
805:29 and everything else in between so it's a trade-off and it might be too expensive
805:31 trade-off and it might be too expensive a trade-off and so you might have to
805:32 a trade-off and so you might have to tolerate something like the collisions
805:34 tolerate something like the collisions we had earlier whereby even though they
805:36 we had earlier whereby even though they might very well happen at least you uh
805:39 might very well happen at least you uh are decreasing the probability by
805:42 are decreasing the probability by perhaps having more buckets like this
805:44 perhaps having more buckets like this and in fact if I rewind now to where we
805:45 and in fact if I rewind now to where we might have gone with this here here's
805:47 might have gone with this here here's how we might represent these nodes in
805:50 how we might represent these nodes in the tree previously in the past we've
805:52 the tree previously in the past we've had a person who had a string name and a
805:54 had a person who had a string name and a string number AKA now Char star and so
805:57 string number AKA now Char star and so here now might be how in this hash table
806:00 here now might be how in this hash table we represent someone's name and number
806:03 we represent someone's name and number as well as a pointer to the next element
806:05 as well as a pointer to the next element in the list we let me rewind just to the
806:07 in the list we let me rewind just to the picture here we keep drawing different
806:09 picture here we keep drawing different shapes because again these are
806:10 shapes because again these are abstractions who really cares if they're
806:12 abstractions who really cares if they're to scale now we've got enough room for
806:14 to scale now we've got enough room for the person's name not pictured on the
806:15 the person's name not pictured on the screen is herm's number that's somewhere
806:18 screen is herm's number that's somewhere in this rectangle but yes pictured here
806:20 in this rectangle but yes pictured here in this little square is a pointer to
806:22 in this little square is a pointer to the next node in the list so by storing
806:25 the next node in the list so by storing name and number maybe her address maybe
806:27 name and number maybe her address maybe her mailing address whatever in addition
806:28 her mailing address whatever in addition to a pointer allows each of these nodes
806:31 to a pointer allows each of these nodes to be connectable just like the nodes in
806:34 to be connectable just like the nodes in a linked list but where they're starting
806:36 a linked list but where they're starting is in an array so the array gets us like
806:38 is in an array so the array gets us like 126 of or gets us um Narrows the problem
806:42 126 of or gets us um Narrows the problem from size 26 to one gets us to the link
806:44 from size 26 to one gets us to the link list in question hopefully it's a single
806:46 list in question hopefully it's a single person or perhaps it has more than that
806:49 person or perhaps it has more than that meanwhile what is the hash table itself
806:52 meanwhile what is the hash table itself the hash table the whole thing is
806:54 the hash table the whole thing is literally just an array I've hardcoded
806:57 literally just an array I've hardcoded the simplest version of size 26 but what
806:59 the simplest version of size 26 but what do each of those boxes in the vertical
807:02 do each of those boxes in the vertical array represent just a pointer to
807:04 array represent just a pointer to potentially a node a node in a length
807:06 potentially a node a node in a length list and if there's no one there if
807:07 list and if there's no one there if there's no one in location y or X or the
807:10 there's no one in location y or X or the like in that Universe well it's just a
807:11 like in that Universe well it's just a null pointer signifying there's no uh
807:14 null pointer signifying there's no uh one there but if there is it's going to
807:16 one there but if there is it's going to be a pointer to a valid node from which
807:18 be a pointer to a valid node from which we can get to any of the others as well
807:21 we can get to any of the others as well and that so-called hash function just
807:23 and that so-called hash function just like the one I did with the greeting
807:24 like the one I did with the greeting cards well it's just an a black box if
807:26 cards well it's just an a black box if you will but implemented somewhere in
807:28 you will but implemented somewhere in code like in C and so if you pass in
807:30 code like in C and so if you pass in Albus what is the hash value of Albus
807:34 Albus what is the hash value of Albus well in the first version of the story
807:35 well in the first version of the story with 26 buckets it should be a zero if
807:38 with 26 buckets it should be a zero if you pass in Zacharias it should be 25
807:41 you pass in Zacharias it should be 25 and so just as my cards were being
807:43 and so just as my cards were being hashed to one of 1 2 3 four values now
807:46 hashed to one of 1 2 3 four values now these names are being hashed to one of
807:48 these names are being hashed to one of 26 possibilities or 26 time 26 or 26 to
807:52 26 possibilities or 26 time 26 or 26 to the 3 power if you have more and more
807:54 the 3 power if you have more and more granularity than
807:56 granularity than that questions on this implementation
807:59 that questions on this implementation now of this idea of a hash
808:17 table if uh if you say that again with the
808:27 [Music] null oh a good question so if there's so
808:30 null oh a good question so if there's so much spareness there's all of these
808:31 much spareness there's all of these empty cells in the array couldn't you
808:33 empty cells in the array couldn't you just go in and free them or delete them
808:35 just go in and free them or delete them or just kind of shrink the array and not
808:37 or just kind of shrink the array and not have a a AA and aab and a a only have
808:41 have a a AA and aab and a a only have the prefixes two or three characters
808:43 the prefixes two or three characters that you need you absolutely could do
808:45 that you need you absolutely could do that but now what you you lose is the
808:48 that but now what you you lose is the arithmetic benefit of being able to map
808:50 arithmetic benefit of being able to map each letter to a number if you start uh
808:53 each letter to a number if you start uh freeing up unused space you don't know
808:56 freeing up unused space you don't know that Zacharias is necessarily at
808:58 that Zacharias is necessarily at location 25 Albus is still going to be
809:00 location 25 Albus is still going to be at location zero but if you've deleted
809:02 at location zero but if you've deleted some of the elements in the middle
809:04 some of the elements in the middle Zacharias could be at 24 if you've
809:05 Zacharias could be at 24 if you've deleted one 23 if you've deleted another
809:08 deleted one 23 if you've deleted another and so you don't have that arithmetic
809:10 and so you don't have that arithmetic immediacy that you need in order to
809:12 immediacy that you need in order to index into the array with constant time
809:14 index into the array with constant time and the same is going to be true if it's
809:16 and the same is going to be true if it's uh two letters or three letters you need
809:19 uh two letters or three letters you need to be able to trust that you can do some
809:20 to be able to trust that you can do some quick math and jump to the right index
809:22 quick math and jump to the right index in constant time and that's again the
809:24 in constant time and that's again the appeal of these arrays so when it comes
809:26 appeal of these arrays so when it comes to the running time of a hash table
809:29 to the running time of a hash table inserting values into it searching for
809:31 inserting values into it searching for values therein at the end of the day it
809:33 values therein at the end of the day it technically is Big O of n because in the
809:35 technically is Big O of n because in the craziest case you might have a huge
809:38 craziest case you might have a huge fancy hash table but everyone in the
809:40 fancy hash table but everyone in the universe has a name starting with h and
809:42 universe has a name starting with h and then it just evolves into a really long
809:44 then it just evolves into a really long link list just like a binary search tree
809:47 link list just like a binary search tree could do the same but if you choose a
809:48 could do the same but if you choose a smarter hash function maybe you mitigate
809:50 smarter hash function maybe you mitigate that and you don't rely only on the
809:52 that and you don't rely only on the first letter but on the second or the
809:54 first letter but on the second or the third as well or some other combination
809:56 third as well or some other combination of that input and make your hash
809:57 of that input and make your hash function smarter odds are if you get a
810:00 function smarter odds are if you get a good hash function you want it get it to
810:02 good hash function you want it get it to be more of like order of n divided by K
810:04 be more of like order of n divided by K where K means constant mathematically
810:06 where K means constant mathematically and so K is the number of buckets so
810:09 and so K is the number of buckets so ideally you want like a uniform
810:10 ideally you want like a uniform distribution you want like this many
810:12 distribution you want like this many people here this many people here you
810:14 people here this many people here you don't want there to be some or no people
810:16 don't want there to be some or no people you want a uniform statistical
810:18 you want a uniform statistical distribution and maybe you get that from
810:20 distribution and maybe you get that from Human names maybe you don't but that's
810:21 Human names maybe you don't but that's kind of the challenge of a hash function
810:24 kind of the challenge of a hash function of course Big O of n over K is not a
810:26 of course Big O of n over K is not a thing because we always throw away
810:28 thing because we always throw away constants like K so it's still Big O of
810:30 constants like K so it's still Big O of n but again the distinction today is
810:33 n but again the distinction today is that okay yes academically you learned
810:35 that okay yes academically you learned in cs50 that sure it's Big O ofin but my
810:37 in cs50 that sure it's Big O ofin but my God it's 26 times faster if you do the
810:39 God it's 26 times faster if you do the hash function well and you spread
810:41 hash function well and you spread everyone out over the hash table and
810:44 everyone out over the hash table and that's the appeal of these kinds of
810:46 that's the appeal of these kinds of structures
810:47 structures and we've got one more for you if I may
810:50 and we've got one more for you if I may something now known as a try so it turns
810:54 something now known as a try so it turns out that a try is even cooler if you
810:56 out that a try is even cooler if you like this kind of thing in that it does
811:00 like this kind of thing in that it does not devolve into Big O of n it is truly
811:03 not devolve into Big O of n it is truly constant time but there's going to be a
811:05 constant time but there's going to be a price there's going to be a gotcha a try
811:08 price there's going to be a gotcha a try is sort of a fancier tree and it's short
811:12 is sort of a fancier tree and it's short for retrieval but pronounced try for
811:14 for retrieval but pronounced try for weird historical reasons but a try is a
811:17 weird historical reasons but a try is a tree each of whose nodes is an array
811:21 tree each of whose nodes is an array right so this is all like crazy mashups
811:22 right so this is all like crazy mashups now people started inventing data
811:24 now people started inventing data structures just by combining different
811:25 structures just by combining different ones unfortunately a lot of the good
811:26 ones unfortunately a lot of the good ideas are taken but you just have
811:30 ideas are taken but you just have benefits from certain aspects of those
811:32 benefits from certain aspects of those data structures and combining them
811:33 data structures and combining them ideally gives you the best of both
811:34 ideally gives you the best of both worlds so to speak so here might be the
811:36 worlds so to speak so here might be the root of a try it's literally a big node
811:39 root of a try it's literally a big node a big rectangle but it's actually an
811:41 a big rectangle but it's actually an array so there's like 26 locations in
811:43 array so there's like 26 locations in this picture here and here's how you use
811:45 this picture here and here's how you use a try for instance to store names just
811:48 a try for instance to store names just like the hash table it you treat each of
811:51 like the hash table it you treat each of the elements of that array in that node
811:54 the elements of that array in that node as like a letter of the alphabet so a
811:55 as like a letter of the alphabet so a through z or 0 through 25 and if you
811:58 through z or 0 through 25 and if you want to store someone's name in here you
812:00 want to store someone's name in here you do so as follows if you want to store
812:03 do so as follows if you want to store like uh an H you index into the H
812:06 like uh an H you index into the H location and if you want to store the
812:08 location and if you want to store the second letter of someone's name like an
812:10 second letter of someone's name like an a well you add another node below it and
812:13 a well you add another node below it and such one is connected to the other and
812:15 such one is connected to the other and you then identify the a in that array
812:18 you then identify the a in that array and then you go on and maybe put a g if
812:20 and then you go on and maybe put a g if the goal is to store spoiler now Hagrid
812:23 the goal is to store spoiler now Hagrid in this data structure and then the r
812:26 in this data structure and then the r and the I and then the D but when you
812:28 and the I and then the D but when you get to the D the end of the name you
812:30 get to the D the end of the name you have to somehow flag that this is the
812:32 have to somehow flag that this is the end of a name that we've embedded into
812:34 end of a name that we've embedded into this data structure so whereas all of
812:36 this data structure so whereas all of these are called out in white just to
812:38 these are called out in white just to make obvious what we're connecting to
812:39 make obvious what we're connecting to what green has to be like a bull that's
812:42 what green has to be like a bull that's true that just indicates like the buck
812:44 true that just indicates like the buck stops here like d is the last letter in
812:46 stops here like d is the last letter in someone's actual name and what's kind of
812:49 someone's actual name and what's kind of cool now about a try is that we can
812:52 cool now about a try is that we can repeat this for other names as well so
812:54 repeat this for other names as well so for instance here is where we might put
812:56 for instance here is where we might put Harry as well and notice they share a
812:58 Harry as well and notice they share a common prefix ha for Hagrid ha for Harry
813:03 common prefix ha for Hagrid ha for Harry so we're reusing some of these nodes
813:05 so we're reusing some of these nodes some of these arrays we can even slip
813:07 some of these arrays we can even slip Hermione in here too borrowing only the
813:09 Hermione in here too borrowing only the H but she gets the H then the E then r r
813:13 H but she gets the H then the E then r r m i o n e and so forth and we Mark at
813:16 m i o n e and so forth and we Mark at the end of her name too that she's in
813:18 the end of her name too that she's in there now what's the takeaway here well
813:22 there now what's the takeaway here well what is the running time of a try how
813:25 what is the running time of a try how many steps does it take to find someone
813:28 many steps does it take to find someone in this data structure and let me zoom
813:29 in this data structure and let me zoom out so that it sort of suddenly becomes
813:31 out so that it sort of suddenly becomes a massive data structure with even more
813:33 a massive data structure with even more in it uh maybe it looks sorry no I'll
813:36 in it uh maybe it looks sorry no I'll keep it on this one maybe it looks a
813:37 keep it on this one maybe it looks a little something like this with just
813:38 little something like this with just these three names but how many steps
813:40 these three names but how many steps does it take to find Hagrid or Harry or
813:44 does it take to find Hagrid or Harry or Hermione no matter how many names are in
813:47 Hermione no matter how many names are in this data structure there's three at the
813:49 this data structure there's three at the moment but it takes what h a g r i d so
813:54 moment but it takes what h a g r i d so six steps to find Hagrid uh h a r r y
813:58 six steps to find Hagrid uh h a r r y five steps to find Harry h e r m i o n e
814:02 five steps to find Harry h e r m i o n e eight steps to find Hermione but notice
814:05 eight steps to find Hermione but notice that those steps are only dependent on
814:08 that those steps are only dependent on what the lengths of the human's names
814:10 what the lengths of the human's names and let's assume that no one's going to
814:12 and let's assume that no one's going to have a infinitely long name it's going
814:14 have a infinitely long name it's going to max out at what like eight no maybe
814:16 to max out at what like eight no maybe eight 18 maybe 20 30 there's actually
814:18 eight 18 maybe 20 30 there's actually some pretty long human names out there
814:20 some pretty long human names out there but it's going to be finite you know
814:22 but it's going to be finite you know it's abounded whereas most contexts n
814:26 it's abounded whereas most contexts n could grow forever so what's compelling
814:28 could grow forever so what's compelling here is if you assume that the longest
814:30 here is if you assume that the longest name is I don't know 50 for the sake of
814:32 name is I don't know 50 for the sake of a theme here then you know that finding
814:34 a theme here then you know that finding anyone in this data structure will take
814:36 anyone in this data structure will take you no more than 50 steps 50 is thus a
814:39 you no more than 50 steps 50 is thus a constant which means you have big O of
814:41 constant which means you have big O of one running time it doesn't matter if
814:44 one running time it doesn't matter if there's a million people in this phone
814:46 there's a million people in this phone book or a billion people in this phone
814:47 book or a billion people in this phone book that's going to definitely add more
814:49 book that's going to definitely add more nodes to it but it's still going to take
814:50 nodes to it but it's still going to take you h a r uh sorry h a g r i d six steps
814:55 you h a r uh sorry h a g r i d six steps to find Hagrid h a r r y five steps to
814:58 to find Hagrid h a r r y five steps to find Harry even if there's a billion
815:00 find Harry even if there's a billion other people in that data structure so
815:03 other people in that data structure so now we actually do seem to have constant
815:05 now we actually do seem to have constant time if you assume that there's going to
815:07 time if you assume that there's going to be a bound on the length of the name why
815:09 be a bound on the length of the name why don't we use tries for everything then
815:12 don't we use tries for everything then what's the price we're paying for this
815:13 what's the price we're paying for this data structure even though we've
815:15 data structure even though we've represented just three characters here
815:19 represented just three characters here yeah it's a lot of memory yeah and you
815:22 yeah it's a lot of memory yeah and you can see it even with these three names
815:24 can see it even with these three names most of the squares on the screen are
815:26 most of the squares on the screen are empty like bites and bits that are there
815:28 empty like bites and bits that are there and are allocated and they need to be
815:30 and are allocated and they need to be there because you need to be able to do
815:31 there because you need to be able to do that arithmetic thing of this being zero
815:33 that arithmetic thing of this being zero this being 25 so you can jump from boom
815:35 this being 25 so you can jump from boom boom boom boom based on each of the
815:37 boom boom boom based on each of the letters but it's a hugely sparse data
815:39 letters but it's a hugely sparse data structure which means it takes up a
815:41 structure which means it takes up a crazy amount of memory now maybe that's
815:44 crazy amount of memory now maybe that's tolerable especially for short names but
815:46 tolerable especially for short names but that's going to be the trade-off as well
815:47 that's going to be the trade-off as well and this is such a tension in Computing
815:49 and this is such a tension in Computing almost any time you want to improve time
815:52 almost any time you want to improve time you want to speed up the efficiency the
815:54 you want to speed up the efficiency the speed of your algorithm you're going to
815:56 speed of your algorithm you're going to spend space if by contrast you want to
815:58 spend space if by contrast you want to decrease the amount of space you might
816:00 decrease the amount of space you might very well have to increase the running
816:03 very well have to increase the running time it is indeed this seesaw back and
816:05 time it is indeed this seesaw back and forth and you your colleagues your
816:07 forth and you your colleagues your company need to decide what resource is
816:09 company need to decide what resource is the most precious heck it might be much
816:11 the most precious heck it might be much harder to code one of these data
816:13 harder to code one of these data structures than another you're a human
816:15 structures than another you're a human your time is valuable do you really want
816:16 your time is valuable do you really want to spend hours implementing a try when
816:19 to spend hours implementing a try when you know hey in 30 minutes I can bang
816:20 you know hey in 30 minutes I can bang out an array nowadays or a linked list
816:22 out an array nowadays or a linked list even there too development time is going
816:24 even there too development time is going to be yet another resource and why
816:26 to be yet another resource and why sometimes there's good code or bad code
816:27 sometimes there's good code or bad code it depends on what you're prioritizing
816:30 it depends on what you're prioritizing so what do each of these nodes look like
816:31 so what do each of these nodes look like in a try well we can keep calling it a
816:34 in a try well we can keep calling it a node this is a very generic term for
816:35 node this is a very generic term for just a container in these data
816:37 just a container in these data structures in this story though let me
816:40 structures in this story though let me claim that everyone has a number like a
816:43 claim that everyone has a number like a phone number a string akhr star every
816:46 phone number a string akhr star every every node has 26 children or
816:49 every node has 26 children or technically an array of size 26 that can
816:52 technically an array of size 26 that can point to more of these nodes notice that
816:54 point to more of these nodes notice that I don't need to store the name of
816:57 I don't need to store the name of someone in a try because it's implicit
816:59 someone in a try because it's implicit in the path that you take to find them
817:01 in the path that you take to find them so that's a minor optimization but it
817:03 so that's a minor optimization but it saves us some space but this would be
817:05 saves us some space but this would be just a different data structure we could
817:07 just a different data structure we could use to actually solve this problem as
817:10 use to actually solve this problem as well albeit at a very expensive cost and
817:12 well albeit at a very expensive cost and what do we need our variable to be that
817:14 what do we need our variable to be that stores the try just like four we just
817:16 stores the try just like four we just need a single pointer that hangs on to
817:19 need a single pointer that hangs on to the root of this structure that's null
817:21 the root of this structure that's null if it's empty or non-null if it's
817:23 if it's empty or non-null if it's actually pointing at
817:25 actually pointing at something any questions then on
817:28 something any questions then on tries and if it's feeling like a lot the
817:30 tries and if it's feeling like a lot the fire the the fire hydrant it is we
817:32 fire the the fire hydrant it is we started with arrays then link list then
817:35 started with arrays then link list then tries but questions on how we've just
817:38 tries but questions on how we've just assembled from these basic building
817:39 assembled from these basic building blocks
817:41 blocks [Music]
817:43 [Music] yeah a good question why is this not uh
817:46 yeah a good question why is this not uh size 26 uh it's just like with the try
817:50 size 26 uh it's just like with the try just like with the link list before it
817:52 just like with the link list before it just tends to be en code convenient to
817:54 just tends to be en code convenient to have a separate additional pointer
817:56 have a separate additional pointer that's small that just points to the
817:58 that's small that just points to the beginning of the data structure because
817:59 beginning of the data structure because that way it can be null thereby clearly
818:01 that way it can be null thereby clearly indicating there are no nodes the whole
818:03 indicating there are no nodes the whole structure is empty if you allocated one
818:05 structure is empty if you allocated one of those nodes you absolutely could but
818:08 of those nodes you absolutely could but then you'd be just wasting space even if
818:10 then you'd be just wasting space even if it's empty and it creates an ambiguity
818:12 it's empty and it creates an ambiguity so just having a single pointer linked
818:13 so just having a single pointer linked to the beginnings of all of these things
818:15 to the beginnings of all of these things is a good thing other questions now on
818:18 is a good thing other questions now on tries or trees or hash tables or
818:25 tries or trees or hash tables or arrays so what problems might arise well
818:28 arrays so what problems might arise well here's a counter example what names are
818:31 here's a counter example what names are manifest in this try
818:42 out what do you see Danielle and Danielle so presumably
818:47 see Danielle and Danielle so presumably if these are two uh names here one of
818:49 if these are two uh names here one of which is a prefix of another notice that
818:51 which is a prefix of another notice that the data structure still works and I
818:53 the data structure still works and I chose you know a friend's name and then
818:54 chose you know a friend's name and then appended a couple of more characters to
818:56 appended a couple of more characters to it that's also a name because we have
818:57 it that's also a name because we have here d a n i e l and the green
819:01 here d a n i e l and the green technically is implemented as a bull or
819:02 technically is implemented as a bull or something like that that indicates a
819:04 something like that that indicates a word stops here but we don't want to
819:05 word stops here but we don't want to preclude storing Danielle as well who's
819:08 preclude storing Danielle as well who's a super string if you will of Daniel and
819:10 a super string if you will of Daniel and so that's okay too so long as the
819:12 so that's okay too so long as the structure allows for the pointers to
819:14 structure allows for the pointers to keep going so even that works out okay
819:17 keep going so even that works out okay whereas it might not have otherwise and
819:19 whereas it might not have otherwise and in terms of the running time just to be
819:20 in terms of the running time just to be clear at the end of the day tries do
819:23 clear at the end of the day tries do give you actual constant time for
819:27 give you actual constant time for insertions lookups deletions and the
819:29 insertions lookups deletions and the like because it's dependent only on the
819:31 like because it's dependent only on the length of the input the key if you will
819:34 length of the input the key if you will and not on how many other people are in
819:36 and not on how many other people are in your phone or a dress book and now
819:39 your phone or a dress book and now thought we'd conclude with a visual if
819:41 thought we'd conclude with a visual if you've gotten out into the square anyone
819:43 you've gotten out into the square anyone recognize this okay okay sweet green a
819:47 recognize this okay okay sweet green a local salad place what are we looking at
819:49 local salad place what are we looking at here and what's its connection to today
819:51 here and what's its connection to today um you're about to become all the
819:52 um you're about to become all the geekier in the real world cuz you will
819:54 geekier in the real world cuz you will start to see data structures
819:56 start to see data structures everywhere what is this or how does this
819:59 everywhere what is this or how does this work maybe in salad form who's been to
820:03 work maybe in salad form who's been to Sweet
820:03 Sweet green okay either of you so how does
820:06 green okay either of you so how does this
820:08 this [Music]
820:17 work okay good so if you order a salad for someone named L when it's ready they
820:19 for someone named L when it's ready they put it in the l section here and so this
820:22 put it in the l section here and so this is kind of a set of key value pairs
820:24 is kind of a set of key value pairs right if L is the first letter of
820:26 right if L is the first letter of someone's name the value hopefully is
820:28 someone's name the value hopefully is the salad and so what you kind of have
820:30 the salad and so what you kind of have here is a dictionary key value pairs
820:32 here is a dictionary key value pairs where it's not words and definitions
820:33 where it's not words and definitions it's names and salads and you can think
820:36 it's names and salads and you can think of this too as kind of a hash table why
820:38 of this too as kind of a hash table why even though it actually doesn't fit on
820:40 even though it actually doesn't fit on one long shelf cu the store is only so
820:42 one long shelf cu the store is only so big this is really an array and
820:43 big this is really an array and apparently a is missing or maybe it's
820:45 apparently a is missing or maybe it's around around the corner but this array
820:47 around around the corner but this array just happens to wrap onto multiple lines
820:49 just happens to wrap onto multiple lines but it's still conceptually a single
820:51 but it's still conceptually a single Dimension but suppose two people have
820:53 Dimension but suppose two people have the name L what do they do
821:01 typically yeah so maybe they they well if they run put that much effort into it
821:03 if they run put that much effort into it they might look at the second letter and
821:04 they might look at the second letter and then the third letter odds are this is
821:05 then the third letter odds are this is not that interesting a problem to solve
821:07 not that interesting a problem to solve optimally in that way but they probably
821:09 optimally in that way but they probably do start stacking the salads on top of
821:11 do start stacking the salads on top of each other maybe scooching it over just
821:13 each other maybe scooching it over just a little bit and so what do you have
821:15 a little bit and so what do you have there well now you start to view the
821:16 there well now you start to view the lens through like cs50 glasses like okay
821:19 lens through like cs50 glasses like okay you have an array and then you have like
821:20 you have an array and then you have like these link lists that are sort of
821:21 these link lists that are sort of growing here but even then you run into
821:23 growing here but even then you run into a problem why because it's not really a
821:24 a problem why because it's not really a link list because at some point you're
821:26 link list because at some point you're going to hit the boundary here so it's
821:28 going to hit the boundary here so it's kind of like an array of arrays because
821:29 kind of like an array of arrays because you can only fit what like three or four
821:31 you can only fit what like three or four salads here and so long story short we
821:33 salads here and so long story short we started today deliberately talking about
821:35 started today deliberately talking about real world things like stacks and cues
821:37 real world things like stacks and cues and even though it did escalate quickly
821:39 and even though it did escalate quickly into binary search trees and hash tables
821:41 into binary search trees and hash tables and tries even those things are
821:43 and tries even those things are everywhere even though they don't call
821:45 everywhere even though they don't call them as such these are just solutions to
821:47 them as such these are just solutions to problems and now with this final week of
821:49 problems and now with this final week of SE under your belt you have all the more
821:51 SE under your belt you have all the more of a technical toolkit via which to
821:53 of a technical toolkit via which to implement these things and code next
821:55 implement these things and code next week we'll be able to trust that someone
821:57 week we'll be able to trust that someone else solved all these problems we'll
821:58 else solved all these problems we'll introduce Python and lines of code like
822:00 introduce Python and lines of code like this will finally become lines of code
822:02 this will finally become lines of code like that so that's the promise ahead
822:04 like that so that's the promise ahead and we'll see you next
822:06 and we'll see you next [Applause]
822:08 [Applause] [Music]
823:31 [Applause] [Music]
824:00 all right this is cs50 and this is week six wherein we finally transitioned from
824:03 six wherein we finally transitioned from scratch to see to now Python and indeed
824:06 scratch to see to now Python and indeed this is going to be somewhat of a unique
824:08 this is going to be somewhat of a unique experience in that just like a few weeks
824:11 experience in that just like a few weeks past perhaps for the first time and now
824:14 past perhaps for the first time and now uh today you're going to learn a new
824:16 uh today you're going to learn a new language but the goal isn't just to
824:17 language but the goal isn't just to throw another fire hose of content and
824:20 throw another fire hose of content and syntax and whatnot at you but rather to
824:22 syntax and whatnot at you but rather to really equip you all to actually teach
824:24 really equip you all to actually teach yourself new languages in the future and
824:26 yourself new languages in the future and so indeed what we'll do today what we'll
824:27 so indeed what we'll do today what we'll do this coming week is sort of prepare
824:29 do this coming week is sort of prepare you to stand on your own and once python
824:31 you to stand on your own and once python is p a and the world has moved on to
824:33 is p a and the world has moved on to some other language in some number of
824:35 some other language in some number of years you'll be well equipped to figure
824:36 years you'll be well equipped to figure out how to wrap your mind around some
824:38 out how to wrap your mind around some new syntax some new language and solve
824:40 new syntax some new language and solve problems as well now you recall in week
824:41 problems as well now you recall in week zero this is where we started just
824:44 zero this is where we started just saying hello to the world and that
824:46 saying hello to the world and that quickly escalated just a week later in C
824:48 quickly escalated just a week later in C to see be something much much more
824:50 to see be something much much more cryptic and if you've still sort of
824:52 cryptic and if you've still sort of struggled with some of the syntax find
824:54 struggled with some of the syntax find yourself checking your notes or your
824:55 yourself checking your notes or your previous code like that's totally normal
824:57 previous code like that's totally normal and that's one of the reasons why there
824:59 and that's one of the reasons why there are languages besides C out there among
825:02 are languages besides C out there among them this language called python humans
825:04 them this language called python humans over the decades have realized gee that
825:06 over the decades have realized gee that wasn't necessarily the best designed
825:07 wasn't necessarily the best designed decision or humans have realized wow you
825:09 decision or humans have realized wow you know what now that computers have gotten
825:11 know what now that computers have gotten faster with more memory and more faster
825:14 faster with more memory and more faster CPUs we can actually do more with our
825:16 CPUs we can actually do more with our programming languages so just as human
825:18 programming languages so just as human languages evolve so do actual
825:20 languages evolve so do actual programming languages and even within a
825:22 programming languages and even within a programming language there's typically
825:24 programming language there's typically different versions We for instance have
825:26 different versions We for instance have been using version C11 of C which was
825:29 been using version C11 of C which was updated in 2011 um but python itself
825:32 updated in 2011 um but python itself continues to evolve and it's now up to
825:33 continues to evolve and it's now up to version three plus and so there too
825:36 version three plus and so there too these things will evolve in the coming
825:37 these things will evolve in the coming days thankfully what you're about to see
825:40 days thankfully what you're about to see is hello world for the third time but
825:42 is hello world for the third time but it's going to be literally this none of
825:44 it's going to be literally this none of the crazy syntax above or below fewer
825:47 the crazy syntax above or below fewer semicolons if any fewer curly braces and
825:50 semicolons if any fewer curly braces and really a lot of the distractions get out
825:52 really a lot of the distractions get out of the way so to get there let's
825:54 of the way so to get there let's consider exactly how we've been
825:56 consider exactly how we've been programming up until now so you write a
825:58 programming up until now so you write a program in C and you've got hopefully no
826:00 program in C and you've got hopefully no syntax error so you're ready to build it
826:02 syntax error so you're ready to build it that is compil it and so you've run make
826:05 that is compil it and so you've run make and then you've run the program like/
826:06 and then you've run the program like/ hello or if you think back to week two
826:09 hello or if you think back to week two where we took a peak underneath the hood
826:11 where we took a peak underneath the hood of what make is doing it's really
826:12 of what make is doing it's really running the actual compiler something
826:14 running the actual compiler something called clang maybe with some command
826:16 called clang maybe with some command line arguments creating a program called
826:18 line arguments creating a program called hello and then you could do do/ hello so
826:21 hello and then you could do do/ hello so today you're going to start doing
826:22 today you're going to start doing something similar in spirit but fewer
826:24 something similar in spirit but fewer steps no longer will you have to compile
826:26 steps no longer will you have to compile your code and then run it and then maybe
826:28 your code and then run it and then maybe fix or change it and then compile your
826:30 fix or change it and then compile your code and run it and then repeat repeat
826:32 code and run it and then repeat repeat the process of running your code is
826:34 the process of running your code is going to be distilled into just a single
826:36 going to be distilled into just a single step and the way to think of this for
826:38 step and the way to think of this for now is that where a c is frequently used
826:40 now is that where a c is frequently used as indeed a compiled language whereby
826:43 as indeed a compiled language whereby you convert it first to zeros and ones
826:45 you convert it first to zeros and ones Python's going to let you speed things
826:46 Python's going to let you speed things up whereby you the human programmer
826:48 up whereby you the human programmer don't have to compile it you're just
826:50 don't have to compile it you're just going to run what's called an
826:51 going to run what's called an interpreter which by Design is named the
826:54 interpreter which by Design is named the exact same thing as the language itself
826:56 exact same thing as the language itself and by running this program installed in
826:59 and by running this program installed in VSS code or eventually on your own Max
827:00 VSS code or eventually on your own Max or PCS this is just going to tell your
827:02 or PCS this is just going to tell your computer to interpret this code and
827:05 computer to interpret this code and figure out how to get down to that lower
827:07 figure out how to get down to that lower level of zeros and ones but you don't
827:08 level of zeros and ones but you don't have to compile the code yourself
827:10 have to compile the code yourself anymore so with that said let's consider
827:14 anymore so with that said let's consider what the code is going to look like side
827:15 what the code is going to look like side by side in fact let's look back at some
827:17 by side in fact let's look back at some scratch blocks just like we did with C
827:19 scratch blocks just like we did with C in week one and do some side by sides
827:21 in week one and do some side by sides because even though some of the syntax
827:22 because even though some of the syntax this week and Beyond's going to be
827:23 this week and Beyond's going to be different like the ideas are truly going
827:26 different like the ideas are truly going to be the same there's not all that much
827:28 to be the same there's not all that much intellectually new just yet so whereas
827:30 intellectually new just yet so whereas in week zero we might have said hello to
827:32 in week zero we might have said hello to the world with this purple puzzle piece
827:34 the world with this purple puzzle piece today of course uh or rather in week one
827:36 today of course uh or rather in week one it looked like this in C but today
827:40 it looked like this in C but today moving forward it's going to quite
827:41 moving forward it's going to quite simply look like this instead and if we
827:43 simply look like this instead and if we go back and forth for just a moment here
827:45 go back and forth for just a moment here again is the version in C noticing the
827:48 again is the version in C noticing the very seike characteristics and just at a
827:50 very seike characteristics and just at a glance here in Python I claim it's now
827:53 glance here in Python I claim it's now this what do you apparently need not
827:55 this what do you apparently need not worry about
827:56 worry about anymore what's gone so semicolon is gone
828:00 anymore what's gone so semicolon is gone and indeed you don't need those to
828:01 and indeed you don't need those to finish most of your thoughts anymore
828:03 finish most of your thoughts anymore anything else so the back sln is absent
828:07 anything else so the back sln is absent and that's kind of curious because we're
828:08 and that's kind of curious because we're still going to get a new line but we'll
828:10 still going to get a new line but we'll see that it's become the default and
828:11 see that it's become the default and this one's a little more subtle but now
828:13 this one's a little more subtle but now the function is called print instead of
828:15 the function is called print instead of print F so it's a little more familiar
828:17 print F so it's a little more familiar in that sense all right so when it comes
828:20 in that sense all right so when it comes to using libraries that is code that
828:22 to using libraries that is code that other people have written in the past
828:24 other people have written in the past we've done things like hash include
828:25 we've done things like hash include cs50.h to use cs50's own header file or
828:28 cs50.h to use cs50's own header file or standard IO or standard lib or string or
828:31 standard IO or standard lib or string or any number of other header files you
828:32 any number of other header files you have all used well moving forward we're
828:34 have all used well moving forward we're going to give you for this first week a
828:36 going to give you for this first week a similar cs-50 Library just very
828:38 similar cs-50 Library just very short-term uh training wheels that will
828:40 short-term uh training wheels that will quickly take off because in reality it's
828:42 quickly take off because in reality it's a lot easier to do things in python as
828:44 a lot easier to do things in python as we'll see but the Syntax for this now is
828:46 we'll see but the Syntax for this now is going to be to import the cs50 library
828:48 going to be to import the cs50 library in this way and when we have now this
828:51 in this way and when we have now this ability we can actually start writing
828:53 ability we can actually start writing some code right away in fact let me
828:54 some code right away in fact let me switch over to vs code here and just as
828:57 switch over to vs code here and just as in the past I'll create a new file but
828:59 in the past I'll create a new file but instead of creating something called C
829:01 instead of creating something called C I'm going to go ahead and create my
829:02 I'm going to go ahead and create my first program called hello.py using Code
829:05 first program called hello.py using Code space hello.py that of course gives me
829:08 space hello.py that of course gives me this new tab and let me actually quite
829:09 this new tab and let me actually quite simply do what I proposed print quote
829:12 simply do what I proposed print quote unquote hello world without the back
829:14 unquote hello world without the back slash without the semicolon without the
829:17 slash without the semicolon without the F in print and now let me go down to my
829:19 F in print and now let me go down to my terminal window and I don't have to
829:21 terminal window and I don't have to compile it I don't have to do dot slash
829:24 compile it I don't have to do dot slash i instead run a program called python
829:26 i instead run a program called python whose purpose in life is now to
829:28 whose purpose in life is now to interpret my code top to bottom left to
829:30 interpret my code top to bottom left to right and if I run python of hello.py
829:32 right and if I run python of hello.py crossing my fingers as always voila now
829:35 crossing my fingers as always voila now I have printed out hello world so we
829:37 I have printed out hello world so we seem to have gotten the new line for
829:39 seem to have gotten the new line for free in this sense where it's
829:41 free in this sense where it's automatically happening the dollar sign
829:43 automatically happening the dollar sign isn't weirdly on the same line like it
829:44 isn't weirdly on the same line like it want was in week one but that's just a a
829:47 want was in week one but that's just a a minor detail here if we switch back to
829:50 minor detail here if we switch back to now some other capabilities well indeed
829:52 now some other capabilities well indeed with the cs50 library you can also not
829:54 with the cs50 library you can also not just import the library itself but
829:56 just import the library itself but specific functions and you'll see that
829:58 specific functions and you'll see that temporarily we're going to give you a
829:59 temporarily we're going to give you a helper function called get string just
830:02 helper function called get string just like in C that just makes it work
830:04 like in C that just makes it work exactly the same way as in C and we'll
830:06 exactly the same way as in C and we'll see a couple of other functions that
830:07 see a couple of other functions that will just make life easier initially but
830:09 will just make life easier initially but quickly will we take those training
830:10 quickly will we take those training wheels off so that nothing is indeed
830:12 wheels off so that nothing is indeed cs50 specific all right well how about
830:15 cs50 specific all right well how about functions more generally in Python let's
830:17 functions more generally in Python let's do a whirlwind tour if you will much
830:19 do a whirlwind tour if you will much like we did in that first week of C
830:21 like we did in that first week of C comparing one to the other so back in
830:24 comparing one to the other so back in our world of scratch one of the first
830:26 our world of scratch one of the first programs we wrote was this one here
830:27 programs we wrote was this one here whereby we asked the human their name we
830:30 whereby we asked the human their name we then used the return value that was sort
830:32 then used the return value that was sort of automatically stored in this answer
830:34 of automatically stored in this answer variable as a second argument to join so
830:38 variable as a second argument to join so that we could say hello David or hello
830:40 that we could say hello David or hello Carter so this was back in week one uh
830:43 Carter so this was back in week one uh week zero in week one we converted it to
830:45 week zero in week one we converted it to this and here's a perfect example of
830:47 this and here's a perfect example of things like escalating quickly and again
830:49 things like escalating quickly and again this is why we start in scratch there's
830:50 this is why we start in scratch there's just so much distraction here to achieve
830:52 just so much distraction here to achieve the same idea but even today we're going
830:54 the same idea but even today we're going to chip away at some of that syntax so
830:56 to chip away at some of that syntax so in C we had to declare the argument as a
831:00 in C we had to declare the argument as a we had to declare the variable as a
831:01 we had to declare the variable as a string here we of course had the
831:03 string here we of course had the semicolon and more well in Python the
831:05 semicolon and more well in Python the comparable code now is going to look
831:08 comparable code now is going to look more simply like this so semicolon is
831:12 more simply like this so semicolon is again gone on both lines for that matter
831:14 again gone on both lines for that matter so that's good what else appears to have
831:16 so that's good what else appears to have changed or disappeared yeah type of
831:19 changed or disappeared yeah type of variable yeah so I didn't have to
831:21 variable yeah so I didn't have to specifically say that answer is now a
831:23 specifically say that answer is now a string and indeed python is is
831:25 string and indeed python is is dynamically typed and in fact it will
831:27 dynamically typed and in fact it will infer from Context exactly what it is
831:30 infer from Context exactly what it is you are storing in that variable other
831:33 you are storing in that variable other details that seem a little bit
831:39 different little bit different what else jumps out at you here I'll go back this
831:41 jumps out at you here I'll go back this was the C version and maybe Focus now on
831:44 was the C version and maybe Focus now on the second line because we've rather
831:46 the second line because we've rather exhausted the first here's now the
831:47 exhausted the first here's now the python version what's different here
831:57 yeah yeah there's no percent s anymore there's no second argument at the moment
831:59 there's no second argument at the moment per se to print now it is still a little
832:02 per se to print now it is still a little weird it's as though I've like deployed
832:03 weird it's as though I've like deployed some addition here arithmetically but
832:05 some addition here arithmetically but that's not the case some of you have
832:06 that's not the case some of you have programmed before and plus some of you
832:08 programmed before and plus some of you might know means what in this
832:10 might know means what in this context so to combine or more
832:13 context so to combine or more technically anyone know the buzz word
832:14 technically anyone know the buzz word yeah
832:15 yeah to concatenate so to concatenate is like
832:18 to concatenate so to concatenate is like the fancy way of what scratch calls
832:19 the fancy way of what scratch calls joining which is to take one string on
832:21 joining which is to take one string on the left one string on the right and to
832:23 the left one string on the right and to join them together to glue them together
832:25 join them together to glue them together if you will so this is not addition it
832:28 if you will so this is not addition it would be if it were numbers involved
832:29 would be if it were numbers involved instead but because we've got a string
832:31 instead but because we've got a string hello comma and another string
832:33 hello comma and another string implicitly in this variable based on
832:34 implicitly in this variable based on what the human typed in in response to
832:36 what the human typed in in response to this get string function that's going to
832:38 this get string function that's going to concatenate hello comma space and then
832:41 concatenate hello comma space and then David or Carter or whatever the human
832:43 David or Carter or whatever the human has typed in but turns out there's going
832:45 has typed in but turns out there's going to be different ways to do this in
832:46 to be different ways to do this in Python and we'll show you a few
832:48 Python and we'll show you a few different ones and here too try not to
832:49 different ones and here too try not to get too hung up on or frustrated by like
832:52 get too hung up on or frustrated by like all of the different ways you can solve
832:53 all of the different ways you can solve problems odds are you're going to be
832:55 problems odds are you're going to be picking up tips and techniques for years
832:56 picking up tips and techniques for years to come if you continue programming so
832:58 to come if you continue programming so let's just give you a few of the
833:00 let's just give you a few of the possible ways so here's a second way you
833:02 possible ways so here's a second way you could print out hello comma David or
833:04 could print out hello comma David or hello comma Carter but what has changed
833:07 hello comma Carter but what has changed in the previous version I used
833:08 in the previous version I used concatenation explicitly and the space
833:11 concatenation explicitly and the space here is important grammatically just so
833:13 here is important grammatically just so we get that in the final phrase
833:15 we get that in the final phrase now I'm proposing to get rid of that
833:17 now I'm proposing to get rid of that space to add a comma outside of the
833:20 space to add a comma outside of the double quotes as well but if you think
833:22 double quotes as well but if you think back to C this probably just means that
833:24 back to C this probably just means that print similar in spirit to print F can
833:27 print similar in spirit to print F can take not just one argument but even two
833:29 take not just one argument but even two and in fact because of this comma in the
833:31 and in fact because of this comma in the middle that's outside of the double
833:32 middle that's outside of the double quotes it's hello comma and then it will
833:35 quotes it's hello comma and then it will be automatically concatenated with even
833:38 be automatically concatenated with even without using the plus to whatever the
833:39 without using the plus to whatever the value of answer is and by default just
833:42 value of answer is and by default just for grammatical prettiness the print fun
833:44 for grammatical prettiness the print fun function always gives you a space for
833:46 function always gives you a space for free in between each of the multiple
833:48 free in between each of the multiple arguments you pass in we'll see how you
833:50 arguments you pass in we'll see how you can override that down the line but for
833:52 can override that down the line but for now that's just another way to do it now
833:54 now that's just another way to do it now perhaps the better if slightly cryptic
833:56 perhaps the better if slightly cryptic way to do this or just the increasingly
833:58 way to do this or just the increasingly common way is probably this third
834:00 common way is probably this third version which looks a little weird too
834:03 version which looks a little weird too and probably the weirdness jumps out
834:04 and probably the weirdness jumps out we've automatic we've suddenly
834:05 we've automatic we've suddenly introduced these like Curly braces which
834:08 introduced these like Curly braces which I promised were mostly gone and they are
834:10 I promised were mostly gone and they are but inside of this string here I've done
834:13 but inside of this string here I've done a curly brace which might mean what just
834:16 a curly brace which might mean what just intuitively and here is sort of an
834:18 intuitively and here is sort of an example of how you learn a new language
834:20 example of how you learn a new language just kind of infer from Context how
834:22 just kind of infer from Context how python probably
834:23 python probably works what might this mean yeah able to
834:26 works what might this mean yeah able to tell that this is not one of the actual
834:27 tell that this is not one of the actual stat
834:29 stat inside yeah so this is an indication
834:32 inside yeah so this is an indication because the curly braces because this is
834:33 because the curly braces because this is the way python was designed that we want
834:35 the way python was designed that we want to plug in the value of answer not
834:38 to plug in the value of answer not literally a ANS w r and the fancy word
834:41 literally a ANS w r and the fancy word here is that the answer variable will be
834:43 here is that the answer variable will be interpolated that is substituted with
834:45 interpolated that is substituted with its actual value but but but and this is
834:48 its actual value but but but and this is actually weird looking and this was
834:49 actually weird looking and this was introduced a few years ago to python
834:51 introduced a few years ago to python what else did I have to change to make
834:53 what else did I have to change to make these curly braces work
834:55 these curly braces work apparently
834:57 apparently yeah yeah there's this weird F and so
835:00 yeah yeah there's this weird F and so it's sort of like part of print F but
835:02 it's sort of like part of print F but now it's inside the curly it's inside
835:04 now it's inside the curly it's inside the parenthesis there this is just the
835:06 the parenthesis there this is just the way python designed this so a few years
835:08 way python designed this so a few years ago when they introduced what are called
835:09 ago when they introduced what are called format strings or F strings you
835:12 format strings or F strings you literally prefix your quoted string with
835:14 literally prefix your quoted string with the uh letter F and then you can use
835:18 the uh letter F and then you can use trickery like this like putting curly
835:19 trickery like this like putting curly braces so that the value will be
835:21 braces so that the value will be substituted automatically if you forget
835:23 substituted automatically if you forget the F you're going to literally see
835:24 the F you're going to literally see hello comma curly brace answer close
835:27 hello comma curly brace answer close curly brace if you add the F it's indeed
835:29 curly brace if you add the F it's indeed interpolated the value is plugged in all
835:32 interpolated the value is plugged in all right questions on how we can just say
835:34 right questions on how we can just say hello to the world via python in this
835:37 hello to the world via python in this case
835:38 case yeah if you do this without the without
835:42 yeah if you do this without the without the F if you omit the F you will
835:44 the F if you omit the F you will literally see c h e l l o comma curly
835:46 literally see c h e l l o comma curly brace a NS w r close curly brace so in
835:49 brace a NS w r close curly brace so in fact let's do this let me go back to vs
835:51 fact let's do this let me go back to vs code here quickly I've still got my file
835:54 code here quickly I've still got my file called hello.py open and let me go ahead
835:57 called hello.py open and let me go ahead and change this ever so slightly so I'm
835:59 and change this ever so slightly so I'm going to go ahead and uh let's say from
836:03 going to go ahead and uh let's say from cs50 import get string and that's just
836:05 cs50 import get string and that's just the new syntax I propose using to import
836:08 the new syntax I propose using to import a function from someone else's Library
836:10 a function from someone else's Library I'm going to now go ahead and ask the
836:13 I'm going to now go ahead and ask the question uh let's go ahead and use get
836:16 question uh let's go ahead and use get string storing the result and answer so
836:18 string storing the result and answer so get string quote unquote what's your
836:20 get string quote unquote what's your name question mark and then on this line
836:23 name question mark and then on this line I'm going to deliberately make a mistake
836:25 I'm going to deliberately make a mistake here exactly to your question let me
836:27 here exactly to your question let me just say hello comma answer and just
836:30 just say hello comma answer and just this now even though answer is a
836:32 this now even though answer is a variable Python's not going to be so
836:34 variable Python's not going to be so presumptuous as to just plug in the
836:36 presumptuous as to just plug in the value of a variable called answer what
836:38 value of a variable called answer what it's going to do of course is if I type
836:40 it's going to do of course is if I type in my name whoops I typed too fast let
836:43 in my name whoops I typed too fast let me go ahead and rerun that again if I
836:45 me go ahead and rerun that again if I run python of hello.py type in my name
836:47 run python of hello.py type in my name and hit enter I get hello comma answer
836:50 and hit enter I get hello comma answer well let me do one better let me apply
836:53 well let me do one better let me apply these curly braces as before let me
836:55 these curly braces as before let me rerun python of hello.py What's Your
836:58 rerun python of hello.py What's Your Name daav ID and here's again the answer
837:00 Name daav ID and here's again the answer to your question now we get literally
837:02 to your question now we get literally the curly braces so the fix here
837:04 the curly braces so the fix here ultimately is just going to be to add
837:05 ultimately is just going to be to add the F there rerun my program again with
837:08 the F there rerun my program again with daav ID and now hello comma David so
837:11 daav ID and now hello comma David so this is admittedly a little more cryptic
837:13 this is admittedly a little more cryptic than the ones with the Plus the comma
837:15 than the ones with the Plus the comma but this is just increasingly common why
837:17 but this is just increasingly common why because you can read it left to right
837:18 because you can read it left to right it's nice and convenient it's less
837:19 it's nice and convenient it's less cryptic than the percent s's so it's
837:21 cryptic than the percent s's so it's sort of a new and improved version if
837:22 sort of a new and improved version if you will of printf in C based on Decades
837:25 you will of printf in C based on Decades of experience of programmers doing
837:27 of experience of programmers doing things like this questions on printing
837:32 things like this questions on printing in this
837:33 in this way we're now on our way to programming
837:35 way we're now on our way to programming in
837:36 in Python anything all right well what more
837:39 Python anything all right well what more can we do with this language here well
837:41 can we do with this language here well let me propose that we consider that we
837:44 let me propose that we consider that we have for
837:46 have for instance a few other features that we
837:49 instance a few other features that we can add to the mix as well
837:52 can add to the mix as well namely let's say some data types as well
837:57 namely let's say some data types as well so let me flip over here to um back to
837:59 so let me flip over here to um back to the slides and there's different data
838:01 the slides and there's different data types in python as we'll soon see but
838:03 types in python as we'll soon see but they're not as explicit as we already
838:05 they're not as explicit as we already saw by using a string from get string
838:07 saw by using a string from get string you don't have to explicitly State what
838:09 you don't have to explicitly State what it is but you solve recall and see all
838:12 it is but you solve recall and see all of these various data types and then in
838:14 of these various data types and then in python kind of nicely enough this list
838:17 python kind of nicely enough this list is about to get shorter and so here is
838:18 is about to get shorter and so here is our list in C here is an abbreviated
838:21 our list in C here is an abbreviated list in Python so we're still going to
838:23 list in Python so we're still going to have strings but they're going to be
838:25 have strings but they're going to be more succinctly called stirs now St Str
838:27 more succinctly called stirs now St Str we're still going to have in for
838:29 we're still going to have in for integers we're still going to have
838:30 integers we're still going to have floats for floating Point values we're
838:32 floats for floating Point values we're even going to have bulls for true and
838:33 even going to have bulls for true and false but what's missing now from the
838:35 false but what's missing now from the list is long and floats and why is that
838:39 list is long and floats and why is that or rather long and double well recall
838:41 or rather long and double well recall that in C those used more bits well in
838:43 that in C those used more bits well in Python the smaller data types previously
838:46 Python the smaller data types previously int and Float themselves just use more
838:48 int and Float themselves just use more bits for you and so you don't need to
838:50 bits for you and so you don't need to distinguish between small and large you
838:52 distinguish between small and large you just use one data type and the language
838:54 just use one data type and the language gives you a bigger range than before it
838:57 gives you a bigger range than before it turns out though there's going to be
838:58 turns out though there's going to be some other features as well of python
839:00 some other features as well of python these data types one of which will be
839:02 these data types one of which will be called range another of which will be
839:04 called range another of which will be list So Gone will be arrays we'll
839:06 list So Gone will be arrays we'll actually use something literally called
839:07 actually use something literally called a list tle sort of like XY pairs for
839:10 a list tle sort of like XY pairs for coordinates and things like that uh dict
839:13 coordinates and things like that uh dict for dictionaries so we have built-in
839:15 for dictionaries so we have built-in capabilities for storing keys and values
839:17 capabilities for storing keys and values we'll see and even a set sort of
839:19 we'll see and even a set sort of mathematically a set is like a
839:20 mathematically a set is like a collection of values but it
839:21 collection of values but it automatically gets rid of duplicates for
839:23 automatically gets rid of duplicates for you so all of these things we could
839:24 you so all of these things we could absolutely Implement in C if we wanted
839:28 absolutely Implement in C if we wanted and indeed in problem set five you've
839:30 and indeed in problem set five you've been implementing your very own spell
839:32 been implementing your very own spell checker using some form of hashtable
839:35 checker using some form of hashtable well it turns out that in Python you can
839:37 well it turns out that in Python you can solve those same problems but perhaps a
839:39 solve those same problems but perhaps a little more readily in fact let me go
839:41 little more readily in fact let me go back over here to vs code and let me
839:44 back over here to vs code and let me propose that I do the following let me
839:47 propose that I do the following let me go ahead and create a file called
839:49 go ahead and create a file called dictionary. let me propose that I try to
839:52 dictionary. let me propose that I try to implement say problem set five our spell
839:54 implement say problem set five our spell checker in Python instead of c and
839:57 checker in Python instead of c and Achieve ultimately the same kind of
840:00 Achieve ultimately the same kind of behavior uh whereby I'll be able to
840:02 behavior uh whereby I'll be able to spell check a whole bunch of words so
840:04 spell check a whole bunch of words so this is jumping the gun a little bit
840:05 this is jumping the gun a little bit because you're about to see syntax will
840:06 because you're about to see syntax will revisit over the course of today but for
840:08 revisit over the course of today but for now I've got a new file called
840:10 now I've got a new file called dictionary. py and let me begin to
840:12 dictionary. py and let me begin to create uh some placehold ERS for
840:14 create uh some placehold ERS for functions we'll see in just a bit that
840:16 functions we'll see in just a bit that in Python you can define a function
840:18 in Python you can define a function called check and that check function can
840:20 called check and that check function can take a word as it's input and I'll come
840:23 take a word as it's input and I'll come back to this in just a moment in Python
840:25 back to this in just a moment in Python I can define a second function like load
840:27 I can define a second function like load which itself will take a whole
840:29 which itself will take a whole dictionary just like in problem set five
840:31 dictionary just like in problem set five and I'll go ahead and come back to the
840:33 and I'll go ahead and come back to the implementation of this meanwhile we
840:36 implementation of this meanwhile we might similarly Implement a function
840:38 might similarly Implement a function called size which takes no arguments but
840:40 called size which takes no arguments but ultimately is going to return the size
840:42 ultimately is going to return the size of my dictionary of words and then last
840:44 of my dictionary of words and then last lastly for consistency with problem set
840:46 lastly for consistency with problem set five we might Define an unload function
840:48 five we might Define an unload function whose purpose in life is to free any
840:50 whose purpose in life is to free any memory that you've been using just to
840:52 memory that you've been using just to give it back to the computer now odds
840:54 give it back to the computer now odds are whether you're still working on
840:55 are whether you're still working on speller or half finished speller like
840:57 speller or half finished speller like you wrote a decent amount of lines of
841:00 you wrote a decent amount of lines of code and indeed it's been by design a
841:02 code and indeed it's been by design a challenge but one of the reasons for
841:04 challenge but one of the reasons for these higher level languages like python
841:07 these higher level languages like python is that you can stand on the shoulders
841:08 is that you can stand on the shoulders of programmers before you and solve very
841:11 of programmers before you and solve very common problems much more quickly so
841:13 common problems much more quickly so that you can f us on building your new
841:15 that you can f us on building your new app or your web application or your own
841:16 app or your web application or your own project to solve problems of interest to
841:18 project to solve problems of interest to you so at the risk of crushing some
841:22 you so at the risk of crushing some Spirits let me propose that in Python if
841:24 Spirits let me propose that in Python if you want a dictionary for something like
841:26 you want a dictionary for something like a spell checker well that's fine go
841:28 a spell checker well that's fine go ahead and give yourself a variable like
841:30 ahead and give yourself a variable like words to store all of those words and
841:32 words to store all of those words and just assign it equal to a dictionary or
841:36 just assign it equal to a dictionary or dict for short in Python that will give
841:38 dict for short in Python that will give you a hashtable now it turns out in
841:40 you a hashtable now it turns out in speller recall you don't need to worry
841:42 speller recall you don't need to worry about words and definition conditions
841:44 about words and definition conditions it's just about spellchecking the words
841:46 it's just about spellchecking the words so strictly speaking we don't need keys
841:48 so strictly speaking we don't need keys and values we just need keys so I'm
841:50 and values we just need keys so I'm going to save myself a few more key
841:52 going to save myself a few more key strokes by just saying that technically
841:53 strokes by just saying that technically in Python using a set suffices again a
841:56 in Python using a set suffices again a set is just a collection of values with
841:57 set is just a collection of values with no duplicates but they don't necessarily
841:59 no duplicates but they don't necessarily have uh keys and values it's just one or
842:02 have uh keys and values it's just one or the other but now that I have on line
842:04 the other but now that I have on line one I claim the equivalent in Python of
842:06 one I claim the equivalent in Python of a hash table I can actually do something
842:09 a hash table I can actually do something like this here's how I might implement
842:11 like this here's how I might implement the check function in Python if the word
842:14 the check function in Python if the word passed into this function is in my
842:17 passed into this function is in my variable called words well return true
842:20 variable called words well return true else go ahead and return false done wait
842:25 else go ahead and return false done wait you're thinking if anything at all maybe
842:27 you're thinking if anything at all maybe we want to handle lowercase instead of
842:29 we want to handle lowercase instead of just uppercase and lowercase well you
842:31 just uppercase and lowercase well you know what in Python if you want to force
842:33 know what in Python if you want to force a whole word to lowercase you don't have
842:34 a whole word to lowercase you don't have to iterate over it with a loop you don't
842:36 to iterate over it with a loop you don't have to use any of that ctype functions
842:37 have to use any of that ctype functions or anything just say word. lower and
842:40 or anything just say word. lower and that will convert the whole thing to
842:41 that will convert the whole thing to lowercase for parody with the dictionary
842:43 lowercase for parody with the dictionary all right how about something like the
842:45 all right how about something like the load function in Python well in Python
842:47 load function in Python well in Python you can open files just like in C for
842:49 you can open files just like in C for instance in Python I might do open the
842:52 instance in Python I might do open the dictionary argument in read mode just
842:54 dictionary argument in read mode just like fopen in Python I might do
842:57 like fopen in Python I might do something like this for each line in
842:59 something like this for each line in that file let me go ahead and add to my
843:03 that file let me go ahead and add to my words variable that line and then let me
843:06 words variable that line and then let me go ahead and close that file and I think
843:10 go ahead and close that file and I think I'm done I'm just going to go ahead and
843:11 I'm done I'm just going to go ahead and return true just because I think think
843:14 return true just because I think think I'm already done now here too I could
843:15 I'm already done now here too I could nitpick a little bit technically if I'm
843:18 nitpick a little bit technically if I'm reading in every line from the file
843:20 reading in every line from the file every line in the dictionary ends with
843:22 every line in the dictionary ends with technically a back sln but there's an
843:23 technically a back sln but there's an easy way to get rid of that uh just like
843:26 easy way to get rid of that uh just like you might in see with an alternative
843:27 you might in see with an alternative syntax what I'm actually going to do is
843:29 syntax what I'm actually going to do is this let me grab from the current line
843:32 this let me grab from the current line the current word by stripping off with
843:35 the current word by stripping off with reverse strip R strip a function will
843:37 reverse strip R strip a function will again see that just gets rid of the
843:39 again see that just gets rid of the trailing new line the back sln at the
843:41 trailing new line the back sln at the end of that line and what I really want
843:43 end of that line and what I really want to do then is add this word to that
843:45 to do then is add this word to that dictionary meanwhile if I want to figure
843:47 dictionary meanwhile if I want to figure out what the size is of my dictionary
843:49 out what the size is of my dictionary well and see you're probably writing
843:51 well and see you're probably writing code to iterate over all of those lines
843:53 code to iterate over all of those lines and you're just going to uh count them
843:55 and you're just going to uh count them up using a variable not so in Python you
843:57 up using a variable not so in Python you can just return the length of those
843:59 can just return the length of those words and better still in Python you
844:02 words and better still in Python you don't have to manage your own memory no
844:04 don't have to manage your own memory no more Malo no more free no more manual
844:07 more Malo no more free no more manual thinking about memory the language just
844:09 thinking about memory the language just deals with all of that for you so you
844:12 deals with all of that for you so you know what it suffices for me to just
844:14 know what it suffices for me to just return true and claim that unloading is
844:16 return true and claim that unloading is done for me and that's it again whether
844:20 done for me and that's it again whether you're in the middle of or already
844:21 you're in the middle of or already finished this might perhaps suggest some
844:23 finished this might perhaps suggest some frustration but also Enlightenment in
844:26 frustration but also Enlightenment in this in that this is why higher level
844:28 this in that this is why higher level languages exist you can build on top of
844:31 languages exist you can build on top of the same principles the same ideas with
844:33 the same principles the same ideas with which you've been dealing struggling
844:35 which you've been dealing struggling even this past week but you can now
844:37 even this past week but you can now express yourself all the more succinctly
844:39 express yourself all the more succinctly like this one line implements a hash
844:42 like this one line implements a hash table for you and all of this now now
844:44 table for you and all of this now now just uses that hash table in a simpler
844:46 just uses that hash table in a simpler way any questions now on this keeping in
844:50 way any questions now on this keeping in mind that the point nonetheless of
844:51 mind that the point nonetheless of speller and P said 5 is to understand
844:54 speller and P said 5 is to understand what's really going on underneath the
844:56 what's really going on underneath the hood and better still to notice this
844:59 hood and better still to notice this this might seem all rather amazing but
845:01 this might seem all rather amazing but let me go ahead and do this I've
845:02 let me go ahead and do this I've actually got a couple of versions of
845:04 actually got a couple of versions of speller written here and I've got a
845:06 speller written here and I've got a version written in C that I won't show
845:08 version written in C that I won't show the source code for but I'm going to go
845:09 the source code for but I'm going to go ahead and make that version of speller
845:12 ahead and make that version of speller in C and I'm going to go ahead here and
845:15 in C and I'm going to go ahead here and let's say split my window here for just
845:18 let's say split my window here for just a moment and I'm going to go into a
845:20 a moment and I'm going to go into a python version of spell really that I
845:22 python version of spell really that I just wrote and on the left hand side
845:24 just wrote and on the left hand side here let me go ahead and run speller the
845:27 here let me go ahead and run speller the version I compiled in C using a big text
845:30 version I compiled in C using a big text like uh the Sherlock Holmes text which
845:32 like uh the Sherlock Holmes text which is a whole lot of words in it and on the
845:34 is a whole lot of words in it and on the right hand side let me run python of
845:37 right hand side let me run python of spell. Pi which is a separate file I
845:39 spell. Pi which is a separate file I wrote in advance just like we give you
845:40 wrote in advance just like we give you speller.c and I'll similarly run this on
845:43 speller.c and I'll similarly run this on the Sherlock Holmes text and I'm going
845:45 the Sherlock Holmes text and I'm going to do my best to hit enter on the left
845:48 to do my best to hit enter on the left and the right of my screen at the same
845:50 and the right of my screen at the same time but we should see hopefully the
845:52 time but we should see hopefully the same list of misspelled words and the
845:53 same list of misspelled words and the timings thereof so here we go on the
845:56 timings thereof so here we go on the right here we go on the
845:59 right here we go on the left all right sort of a race to see
846:02 left all right sort of a race to see which one wins here C is on the left
846:04 which one wins here C is on the left python is on the right okay
846:09 python is on the right okay interesting hopefully Python's close
846:11 interesting hopefully Python's close behind note that some of the is internet
846:14 behind note that some of the is internet delay and so it might not necessarily be
846:16 delay and so it might not necessarily be a crazy number of seconds but the system
846:19 a crazy number of seconds but the system is indeed using if we measure at a low
846:21 is indeed using if we measure at a low level how much time the CPU spent
846:23 level how much time the CPU spent executing my code C took a total of 1.64
846:26 executing my code C took a total of 1.64 seconds that was pretty fast even though
846:27 seconds that was pretty fast even though it took a moment more for all of the
846:29 it took a moment more for all of the btes to come over the Internet the
846:31 btes to come over the Internet the python version though took what 2.44
846:34 python version though took what 2.44 seconds so what might an inference be I
846:37 seconds so what might an inference be I mean one maybe I'm just better at
846:39 mean one maybe I'm just better at programming in c than I am in Python
846:41 programming in c than I am in Python which is probably not true
846:44 which is probably not true but what else might you infer from this
846:53 example should we maybe give up on python stick with
846:55 python stick with C no so where what might be going on
846:58 C no so where what might be going on here like why is the python version that
847:00 here like why is the python version that I claim is correct and I think the
847:01 I claim is correct and I think the numbers all line up just not the
847:04 numbers all line up just not the times where's the trade-off here well
847:06 times where's the trade-off here well here again is sort of this design
847:08 here again is sort of this design tradeoff
847:16 yeah yeah exactly in order to save the human programmer time there's a lot more
847:18 human programmer time there's a lot more features built into python more
847:19 features built into python more functions more automatic management of
847:22 functions more automatic management of memory and so forth and you have to pay
847:24 memory and so forth and you have to pay a price like someone else's code is
847:26 a price like someone else's code is doing all of that work for you but if
847:28 doing all of that work for you but if they've written some number of lines of
847:29 they've written some number of lines of code those are just more lines of code
847:31 code those are just more lines of code that need to be executed for you whereas
847:33 that need to be executed for you whereas here the computer is at the risk of
847:35 here the computer is at the risk of oversimplifying only running my lines of
847:38 oversimplifying only running my lines of code so there's just less overhead and
847:40 code so there's just less overhead and so this is a Perpetual trade-off
847:42 so this is a Perpetual trade-off typically when using a more userfriendly
847:43 typically when using a more userfriendly and more modern language one of the
847:45 and more modern language one of the prices you might pay is performance now
847:48 prices you might pay is performance now there's a lot of smart computer
847:49 there's a lot of smart computer scientists in the world though trying to
847:50 scientists in the world though trying to push back on those same tradeoffs and so
847:53 push back on those same tradeoffs and so these interpreters like the command I
847:55 these interpreters like the command I wrote python technically can especially
847:58 wrote python technically can especially if you run a program again and again
848:00 if you run a program again and again they can actually sort of secretly
848:01 they can actually sort of secretly behind the scenes compile your code for
848:03 behind the scenes compile your code for you down to zeros and ones and then the
848:05 you down to zeros and ones and then the second the third the fourth time you run
848:07 second the third the fourth time you run that program it might very well be
848:09 that program it might very well be faster so this is a bit of a head fake
848:10 faster so this is a bit of a head fake here in that I'm running them once and
848:13 here in that I'm running them once and only once but we could get benefit over
848:15 only once but we could get benefit over time if we kept running the python
848:17 time if we kept running the python version again and again and perhaps
848:19 version again and again and perhaps fine-tune the performance but in general
848:21 fine-tune the performance but in general there's going to be this trade-off now
848:23 there's going to be this trade-off now would you rather spend the 60 seconds I
848:25 would you rather spend the 60 seconds I wrote implementing a spell checker or
848:26 wrote implementing a spell checker or the 6 hours 16 hours you might be or
848:30 the 6 hours 16 hours you might be or have spent implementing the same and C
848:32 have spent implementing the same and C you know probably not for productivity
848:34 you know probably not for productivity sake this is why we have these
848:35 sake this is why we have these additional languages just for fun let me
848:38 additional languages just for fun let me flip over to another screen here and
848:41 flip over to another screen here and open up a version of python that's
848:43 open up a version of python that's actually on on my in just a second on my
848:45 actually on on my in just a second on my own uh Mac instead of the cloud so that
848:48 own uh Mac instead of the cloud so that I can actually do something with
848:50 I can actually do something with Graphics so here I just have a black and
848:52 Graphics so here I just have a black and white terminal window on my very own Mac
848:54 white terminal window on my very own Mac and I've pre-installed python just like
848:56 and I've pre-installed python just like we've done so for VSS code in the cloud
848:58 we've done so for VSS code in the cloud for you uh notice that I've got this uh
849:00 for you uh notice that I've got this uh photo of uh perhaps one of your favorite
849:03 photo of uh perhaps one of your favorite TV shows here with the cast of The
849:05 TV shows here with the cast of The Office notice all of the faces in this
849:08 Office notice all of the faces in this image here and let me propose that we
849:10 image here and let me propose that we try to find one face in the crowd sort
849:13 try to find one face in the crowd sort of CSI style whereby we want to find
849:16 of CSI style whereby we want to find perhaps the stranton Strangler so to
849:18 perhaps the stranton Strangler so to speak and so here is an example of this
849:20 speak and so here is an example of this this guy's face now how do we go about
849:23 this guy's face now how do we go about finding this specific face in the crowd
849:25 finding this specific face in the crowd well our human eyes obviously can pluck
849:26 well our human eyes obviously can pluck him out especially if you're familiar
849:28 him out especially if you're familiar with the show but let me go ahead and do
849:30 with the show but let me go ahead and do this instead let me go ahead and propose
849:33 this instead let me go ahead and propose that we run code that I already wrote in
849:36 that we run code that I already wrote in advance here this is a Python program
849:38 advance here this is a Python program with more lines of code that we won't
849:40 with more lines of code that we won't dwell on for today but it's meant to
849:41 dwell on for today but it's meant to motivate what we can do from a a pillow
849:45 motivate what we can do from a a pillow uh Library implying a python image
849:47 uh Library implying a python image Library I want to import some type of
849:50 Library I want to import some type of information called some type of some
849:52 information called some type of some feature called image so that I can
849:53 feature called image so that I can manipulate images not unlike our own
849:55 manipulate images not unlike our own problem set 4 and this is kind of
849:57 problem set 4 and this is kind of powerful you in Python you can just
849:59 powerful you in Python you can just import face recognition as a library
850:03 import face recognition as a library that someone else wrote from there I'm
850:05 that someone else wrote from there I'm going to create a variable called image
850:07 going to create a variable called image I'm going to use this face recognitions
850:08 I'm going to use this face recognitions libraries load image file function it's
850:11 libraries load image file function it's a little verbose but it's similar in
850:13 a little verbose but it's similar in Spirit to F open and I'm going to open
850:15 Spirit to F open and I'm going to open office. jpeg I'm going to then declare a
850:18 office. jpeg I'm going to then declare a second variable called face locations
850:20 second variable called face locations plural because what I'm expecting to get
850:23 plural because what I'm expecting to get back per the documentation for this
850:24 back per the documentation for this library is a list of all of the faces
850:27 library is a list of all of the faces locations that are detected all right
850:29 locations that are detected all right then I'm going to iterate over each of
850:31 then I'm going to iterate over each of those uh faces using a for Loop that
850:33 those uh faces using a for Loop that we'll see in more detail I'm going to
850:35 we'll see in more detail I'm going to then infer what the top right bottom and
850:37 then infer what the top right bottom and left Corners are of that face and then
850:40 left Corners are of that face and then what I'm going to do here is show that
850:43 what I'm going to do here is show that face alone if I've detected the face in
850:46 face alone if I've detected the face in question so let me go ahead here and run
850:52 question so let me go ahead here and run detect. and we'll see not just the one
850:55 detect. and we'll see not just the one face we're looking for but if I run
850:57 face we're looking for but if I run python of detect. piy it's going to do
851:00 python of detect. piy it's going to do all of the analysis I'll see a big
851:02 all of the analysis I'll see a big opening here now of all of the faces
851:06 opening here now of all of the faces that were detected in this here program
851:10 that were detected in this here program okay some better than others I guess if
851:11 okay some better than others I guess if you zoom in on catching someone typical
851:13 you zoom in on catching someone typical Angela if you now want to Now find that
851:16 Angela if you now want to Now find that one face I think we need to train the
851:18 one face I think we need to train the software a bit more so let me actually
851:20 software a bit more so let me actually open up a second program called
851:21 open up a second program called recognize that's got more going on but
851:24 recognize that's got more going on but let me with a wave of a hand point out
851:25 let me with a wave of a hand point out that I'm now loading not only the
851:27 that I'm now loading not only the office. JPEG but also toby. JPEG to sort
851:30 office. JPEG but also toby. JPEG to sort of train the algorithm to find that
851:33 of train the algorithm to find that specific face and so now if I run this
851:36 specific face and so now if I run this second version recognize. with python of
851:38 second version recognize. with python of recognize. py hold my breath for just a
851:41 recognize. py hold my breath for just a moment it's an analyzing presumably all
851:43 moment it's an analyzing presumably all of the faces you see the same original
851:46 of the faces you see the same original photo but do you see one such face
851:49 photo but do you see one such face highlighted here this adversion of the
851:51 highlighted here this adversion of the code found Toby highlighted him with
851:54 code found Toby highlighted him with this green and voila we have face
851:56 this green and voila we have face recognition so for better for worse this
851:57 recognition so for better for worse this is what's happening increasingly
851:59 is what's happening increasingly societally nowadays and honestly even
852:01 societally nowadays and honestly even though I didn't write the code live
852:02 though I didn't write the code live because it's a good dozen or more lines
852:04 because it's a good dozen or more lines of code it's not terribly many and
852:06 of code it's not terribly many and literally all the authorities all we
852:08 literally all the authorities all we have to do is import face recognition
852:10 have to do is import face recognition and voila you have access like these
852:12 and voila you have access like these techn IES are here already but let's
852:14 techn IES are here already but let's consider for just a moment how did we
852:16 consider for just a moment how did we find Toby like how might that Library
852:19 find Toby like how might that Library even though we're not going to look at
852:21 even though we're not going to look at its implementation details how does it
852:22 its implementation details how does it find Toby and distinguish him from all
852:24 find Toby and distinguish him from all of these other faces in the crowd what
852:28 of these other faces in the crowd what might it be doing
852:30 might it be doing intuitively think back even to pet 4
852:33 intuitively think back even to pet 4 like what you yourselves have access to
852:34 like what you yourselves have access to data wise
852:44 yeah you know pixels in one area area and a lot
852:45 and a lot of
852:47 of that it's a lot of a lot of
852:57 simar yeah exactly and to summarize for for camera here we have trained the
852:59 for camera here we have trained the software if you will by giving it a
853:00 software if you will by giving it a photo of Toby's face by so by looking
853:02 photo of Toby's face by so by looking for the same or really similar pixels
853:04 for the same or really similar pixels especially if it's a slightly different
853:06 especially if it's a slightly different image of Toby we can perhaps identify
853:08 image of Toby we can perhaps identify him in the crowd and what really is a
853:10 him in the crowd and what really is a human face well at the end of the day
853:12 human face well at the end of the day the computer only knows it as a pattern
853:13 the computer only knows it as a pattern of bits or really at a higher level a
853:15 of bits or really at a higher level a pattern of pixels so maybe a human face
853:17 pattern of pixels so maybe a human face is perhaps best defined in general as
853:19 is perhaps best defined in general as like two eyes and a nose and a mouth
853:22 like two eyes and a nose and a mouth that even though all of us look similar
853:24 that even though all of us look similar structurally odds are the measurement
853:26 structurally odds are the measurement between the eyes and the nose and the
853:27 between the eyes and the nose and the width of the mouth the skin tone and all
853:29 width of the mouth the skin tone and all of these other physical characteristics
853:31 of these other physical characteristics are patterns that software could perhaps
853:33 are patterns that software could perhaps detect and then look sort of
853:35 detect and then look sort of statistically through the image looking
853:36 statistically through the image looking for the closest possible match to these
853:39 for the closest possible match to these various measurement shapes colors and
853:41 various measurement shapes colors and sizes and the like and indeed that might
853:43 sizes and the like and indeed that might might be the intuition but what's
853:44 might be the intuition but what's powerful here again is just how easy and
853:47 powerful here again is just how easy and readily available this technology now is
853:50 readily available this technology now is all right so with that said let's
853:52 all right so with that said let's propose to consider what more we can do
853:55 propose to consider what more we can do with python itself get back to the
853:56 with python itself get back to the fundamentals so that you yourselves can
853:58 fundamentals so that you yourselves can start to implement something along those
854:00 start to implement something along those same lines so besides having access to
854:04 same lines so besides having access to things like a get string function um the
854:06 things like a get string function um the cs50 library provides a few other things
854:08 cs50 library provides a few other things as well namely in C we had these but in
854:12 as well namely in C we had these but in Python we're going to have fewer in
854:14 Python we're going to have fewer in Python our library short term is going
854:15 Python our library short term is going to give you not only get string but also
854:17 to give you not only get string but also get in and get float why it's actually
854:19 get in and get float why it's actually just kind of annoying as we'll student
854:21 just kind of annoying as we'll student see to get back an integer or a float
854:23 see to get back an integer or a float from a user and just make sure that it's
854:25 from a user and just make sure that it's an INT and a float and not like a word
854:28 an INT and a float and not like a word like cat or dog or some string that's
854:30 like cat or dog or some string that's not actually a number well we can import
854:33 not actually a number well we can import not just the specific function get
854:35 not just the specific function get string but we can actually import all of
854:37 string but we can actually import all of these functions one at a time like this
854:39 these functions one at a time like this as we'll soon see or you can even in
854:41 as we'll soon see or you can even in Python import specific functions from a
854:44 Python import specific functions from a file one of you asked a while back when
854:46 file one of you asked a while back when you import when you include something
854:47 you import when you include something like cs50.h or standard i.h you're
854:50 like cs50.h or standard i.h you're actually getting all of the code in that
854:52 actually getting all of the code in that file which potentially can add bulk to
854:54 file which potentially can add bulk to your own program or time in this case
854:57 your own program or time in this case when you import specific functions from
854:59 when you import specific functions from python you can be a little more narrowly
855:02 python you can be a little more narrowly uh precise as to what it is you want to
855:04 uh precise as to what it is you want to have access to all right so with that
855:06 have access to all right so with that said let's go ahead and see what
855:08 said let's go ahead and see what conditionals look like in Python so in
855:11 conditionals look like in Python so in the left hand side again here we'll see
855:12 the left hand side again here we'll see scrp
855:13 scrp and here for instance was just kind of a
855:14 and here for instance was just kind of a contrived example asking if x is less
855:17 contrived example asking if x is less than y then say x is less than y in C it
855:21 than y then say x is less than y in C it looked like this in Python now it's
855:23 looked like this in Python now it's going to look like this instead and
855:25 going to look like this instead and here's before in C and here's after and
855:29 here's before in C and here's after and just to call out a few of the obvious
855:31 just to call out a few of the obvious differences what has changed in Python
855:33 differences what has changed in Python for conditionals it would
855:40 seem sort of what's the difference yeah yeah so there's no more curly
855:42 yeah yeah so there's no more curly braces and indeed you don't use those
855:44 braces and indeed you don't use those what appears to be taking their place if
855:46 what appears to be taking their place if you might
855:47 you might infer what seems to have taken their
855:49 infer what seems to have taken their place what do you think so the colon at
855:52 place what do you think so the colon at the start of this line here but also
855:54 the start of this line here but also even more important now is this
855:56 even more important now is this indentation below it so some of you and
855:59 indentation below it so some of you and we know this from Office hours have a
856:00 we know this from Office hours have a habit of like uh indenting everything on
856:03 habit of like uh indenting everything on the left right and it's just kind of
856:04 the left right and it's just kind of this crazy mess to look at frustrating
856:06 this crazy mess to look at frustrating for you surely but C and clang is pretty
856:09 for you surely but C and clang is pretty tolerant when it comes to things like
856:11 tolerant when it comes to things like Whit space in a program python uh-uh
856:13 Whit space in a program python uh-uh they realized years ago that let's help
856:15 they realized years ago that let's help humans help themselves and just require
856:17 humans help themselves and just require standard indentation so four spaces
856:20 standard indentation so four spaces would be the norm here but because it's
856:21 would be the norm here but because it's indented below that colon that indeed
856:23 indented below that colon that indeed indicates that this now is part of that
856:26 indicates that this now is part of that condition something else has gone
856:27 condition something else has gone missing versus C in this conditional
856:31 missing versus C in this conditional what else is a little
856:33 what else is a little simplified yeah so no more parentheses
856:36 simplified yeah so no more parentheses you can still use them especially when
856:37 you can still use them especially when you need to logically to do order of
856:39 you need to logically to do order of operations like in math but in this case
856:41 operations like in math but in this case if you just want to ask a simple
856:42 if you just want to ask a simple question like if x less than y you can
856:44 question like if x less than y you can just do it like that how about when you
856:46 just do it like that how about when you have an if else well this is almost the
856:48 have an if else well this is almost the same here with these same changes in C
856:50 same here with these same changes in C this looked like this and it's starting
856:52 this looked like this and it's starting to get a bit bulky at least if we use
856:53 to get a bit bulky at least if we use our curly braces in this way in Python
856:55 our curly braces in this way in Python we can tighten things up further even
856:57 we can tighten things up further even though strictly speaking in C you don't
856:58 though strictly speaking in C you don't always need the curly braces but here
857:01 always need the curly braces but here gone are the parentheses again gone are
857:03 gone are the parentheses again gone are the curly braces indentation is
857:05 the curly braces indentation is consistent and we've just added another
857:07 consistent and we've just added another keyword else with a colon but no more
857:09 keyword else with a colon but no more semicolons as well how about something
857:11 semicolons as well how about something larger like this and if else if else
857:14 larger like this and if else if else this one's a little curious but in C it
857:17 this one's a little curious but in C it looked like this if else if else in
857:20 looked like this if else if else in Python it now looks like this and
857:22 Python it now looks like this and there's perhaps one curiosity here that
857:24 there's perhaps one curiosity here that honestly all these years later I still
857:26 honestly all these years later I still can't remember how to spell it half the
857:27 can't remember how to spell it half the time what's weird about
857:30 time what's weird about this what do you spot as
857:34 this what do you spot as different uh yeah over
857:41 here yeah instead of else if it's l if why apparently El space if was just too
857:43 why apparently El space if was just too many keystrokes for humans to type so
857:45 many keystrokes for humans to type so they condensed it into this way probably
857:47 they condensed it into this way probably means it's a little more distinguishable
857:49 means it's a little more distinguishable too for the computer between the if and
857:51 too for the computer between the if and the else to but just something to
857:52 the else to but just something to remember now it's indeed L if and not
857:54 remember now it's indeed L if and not else if all right so what about
857:56 else if all right so what about variables in Python I've used a couple
857:58 variables in Python I've used a couple of them already but let's let's um
858:01 of them already but let's let's um distill exactly how you define uh and
858:03 distill exactly how you define uh and declare these things as well so in
858:04 declare these things as well so in scratch if we wanted to create a
858:06 scratch if we wanted to create a variable called counter and set it equal
858:07 variable called counter and set it equal initially to zero we would do something
858:09 initially to zero we would do something like this specify that it's an INT use
858:11 like this specify that it's an INT use the assignment op operator and the
858:13 the assignment op operator and the thought with a semicolon in Python it's
858:16 thought with a semicolon in Python it's just simpler you name the variable you
858:18 just simpler you name the variable you use the assignment operator as before
858:20 use the assignment operator as before you set it equal to some value and
858:21 you set it equal to some value and that's it you don't mention the type you
858:23 that's it you don't mention the type you don't mention the semicolon or anything
858:25 don't mention the semicolon or anything more what if you want to change a
858:27 more what if you want to change a variable like the counter by one that is
858:29 variable like the counter by one that is incremented by one you have a few
858:31 incremented by one you have a few different ways here in C we saw syntax
858:33 different ways here in C we saw syntax like this where you can say counter
858:35 like this where you can say counter equals counter plus one which again
858:37 equals counter plus one which again feels like illogical how can counter
858:40 feels like illogical how can counter equal counter plus one but again we read
858:42 equal counter plus one but again we read this code really right to left updating
858:44 this code really right to left updating its value by one um in Python it's
858:47 its value by one um in Python it's almost the same you just get rid of the
858:48 almost the same you just get rid of the semicolon so that logic is there but
858:50 semicolon so that logic is there but recall in C we could do something
858:51 recall in C we could do something slightly different that we can also do
858:53 slightly different that we can also do in Python in Python you can also more
858:55 in Python in Python you can also more succinctly do this plus equals and then
858:58 succinctly do this plus equals and then whatever number you want to add or you
859:00 whatever number you want to add or you can even change it to subtract if you
859:01 can even change it to subtract if you prefer sadly gone is something you've
859:04 prefer sadly gone is something you've probably typed a whole lot what was the
859:06 probably typed a whole lot what was the other way you can add
859:08 other way you can add one Plus+ is no more sadly in Python
859:11 one Plus+ is no more sadly in Python just too many ways to do the same thing
859:13 just too many ways to do the same thing so they got rid of it in favor of just
859:15 so they got rid of it in favor of just this syntax here so keep that in mind as
859:17 this syntax here so keep that in mind as well what about loops when you want to
859:18 well what about loops when you want to do something in Python again and again
859:21 do something in Python again and again well in scratch in week zero here's how
859:22 well in scratch in week zero here's how we meowed three times specifically in C
859:25 we meowed three times specifically in C we had a couple of ways of doing this
859:27 we had a couple of ways of doing this this was like the more mechanical
859:28 this was like the more mechanical approach where you create a variable
859:30 approach where you create a variable called I you set it equal to zero you
859:32 called I you set it equal to zero you then do while I is less than three the
859:35 then do while I is less than three the following and then you yourself
859:37 following and then you yourself increment I again and again mechanical
859:39 increment I again and again mechanical in the sense that like you have to
859:41 in the sense that like you have to implement all of these gears and make
859:42 implement all of these gears and make them turn yourself but this was a
859:44 them turn yourself but this was a correct way to do that in Python we can
859:46 correct way to do that in Python we can still achieve the same idea but we don't
859:48 still achieve the same idea but we don't need the int keyword we don't need any
859:51 need the int keyword we don't need any of the semicolons we don't need the
859:52 of the semicolons we don't need the parentheses we don't need the curly
859:54 parentheses we don't need the curly braces we can't use the Plus+ so maybe
859:56 braces we can't use the Plus+ so maybe that's a minor step backwards if you're
859:58 that's a minor step backwards if you're a fan but otherwise the code the logic
860:00 a fan but otherwise the code the logic is exactly the same but there's other
860:03 is exactly the same but there's other ways to achieve this same idea recall
860:05 ways to achieve this same idea recall that in C we could also do this you
860:07 that in C we could also do this you could use a for Loop which it does
860:09 could use a for Loop which it does exactly the same thing both are correct
860:11 exactly the same thing both are correct both are arguably welld designed it's
860:13 both are arguably welld designed it's kind of to each their own when it comes
860:15 kind of to each their own when it comes to choosing between these in Python
860:17 to choosing between these in Python though we're going to have to think
860:19 though we're going to have to think through how to do this so you don't do
860:22 through how to do this so you don't do the same for loop as in C the closest I
860:25 the same for loop as in C the closest I could come up with is this where you say
860:27 could come up with is this where you say four I or whatever variable you want to
860:30 four I or whatever variable you want to do the counting in literally the
860:32 do the counting in literally the preposition and then you use square
860:34 preposition and then you use square brackets here and we've used square
860:36 brackets here and we've used square brackets before in the context of like
860:38 brackets before in the context of like arrays and things like that and the 012
860:41 arrays and things like that and the 012 looks like an array in some sense even
860:43 looks like an array in some sense even though we've also seen arrays with curly
860:45 though we've also seen arrays with curly braces but these square brackets for now
860:47 braces but these square brackets for now denote a list python does not have
860:49 denote a list python does not have arrays an array is that contiguous chunk
860:51 arrays an array is that contiguous chunk of memory back to back to back that you
860:54 of memory back to back to back that you have to resize somehow by moving things
860:56 have to resize somehow by moving things around in memory as per two weeks ago in
860:59 around in memory as per two weeks ago in Python though you can just create a list
861:02 Python though you can just create a list like this using square brackets and
861:04 like this using square brackets and better still as we'll see you can add or
861:05 better still as we'll see you can add or even remove things from that list down
861:08 even remove things from that list down the road um this though is not going to
861:11 the road um this though is not going to be very welld designed this will work
861:13 be very welld designed this will work this will iterate in Python three
861:15 this will iterate in Python three times but what might rub you the wrong
861:18 times but what might rub you the wrong way about this design even if you've
861:19 way about this design even if you've never seen python before how does this
861:22 never seen python before how does this example not end well
861:28 yeah yeah like if you're making a large list you have to type out each one of
861:30 list you have to type out each one of these numbers like comma three comma
861:31 these numbers like comma three comma four comma 5 comma dot dot do 50 comma
861:34 four comma 5 comma dot dot do 50 comma dot dot dot 500 like surely that's not
861:36 dot dot dot 500 like surely that's not the best solution to have all of these
861:38 the best solution to have all of these numbers on the code on the screen
861:40 numbers on the code on the screen wrapping endlessly on the screen so in
861:42 wrapping endlessly on the screen so in Python another way to do this would be
861:44 Python another way to do this would be to use a function called range which
861:46 to use a function called range which technically is a data type unto itself
861:48 technically is a data type unto itself and this returns to you as many values
861:51 and this returns to you as many values as you ask for it range takes some other
861:53 as you ask for it range takes some other arguments as well but the simplest use
861:55 arguments as well but the simplest use case here is if you want back the
861:56 case here is if you want back the numbers 0 1 and two a total of three
861:59 numbers 0 1 and two a total of three values you say hey python please give me
862:02 values you say hey python please give me a range of three values and by default
862:04 a range of three values and by default they start at zero on up but this is
862:06 they start at zero on up but this is more efficient than it would be to
862:09 more efficient than it would be to hardcode the entire list at once and the
862:11 hardcode the entire list at once and the best metaphor I could up with is
862:12 best metaphor I could up with is something like this like here for
862:14 something like this like here for instance is a deck of cards this is sort
862:15 instance is a deck of cards this is sort of normal human size and there's
862:17 of normal human size and there's presumably 52 cards here so writing out
862:20 presumably 52 cards here so writing out 0 through 51 on code would be a little
862:23 0 through 51 on code would be a little ridiculous for the reasons you know it
862:24 ridiculous for the reasons you know it would just be very unwieldy and ugly and
862:27 would just be very unwieldy and ugly and wrapping and all of that it would be the
862:29 wrapping and all of that it would be the phys it would be the virtual equivalent
862:30 phys it would be the virtual equivalent of me like handing you all of these
862:32 of me like handing you all of these cards at once to just deal with and
862:34 cards at once to just deal with and right you know they're not that big but
862:35 right you know they're not that big but like it's a lot of cards to hold on to
862:37 like it's a lot of cards to hold on to it requires a lot of memory or physical
862:39 it requires a lot of memory or physical storage if you will what range does
862:41 storage if you will what range does metaphorically is if you ask me for
862:43 metaphorically is if you ask me for three cards I hand you them one at a
862:46 three cards I hand you them one at a time like this so that at any point in
862:49 time like this so that at any point in time you only have one number in the
862:51 time you only have one number in the computer's memory until you're handed
862:54 computer's memory until you're handed the next the alternative the previous
862:56 the next the alternative the previous version would be to hand me all three
862:57 version would be to hand me all three cards at once or all 52 cards at once
862:59 cards at once or all 52 cards at once but in this case range is just way more
863:01 but in this case range is just way more efficient you can do range of a thousand
863:04 efficient you can do range of a thousand that's not going to give you a list of a
863:05 that's not going to give you a list of a thousand values all at once it's going
863:07 thousand values all at once it's going to give you a thousand values one at a
863:10 to give you a thousand values one at a time reducing memory sign signicantly in
863:13 time reducing memory sign signicantly in the computer itself all right so besides
863:16 the computer itself all right so besides this what about doing something forever
863:18 this what about doing something forever in scratch well we could do this
863:20 in scratch well we could do this literally with a forever block which
863:22 literally with a forever block which didn't quite exist in C in C we had had
863:24 didn't quite exist in C in C we had had to hack it together by saying while true
863:27 to hack it together by saying while true because true is by definition TR always
863:30 because true is by definition TR always true so this just in uh deliberately
863:32 true so this just in uh deliberately induces an infinite Loop for us in
863:35 induces an infinite Loop for us in Python the logic's going to be almost
863:37 Python the logic's going to be almost the same and infinite Loops in Python
863:39 the same and infinite Loops in Python tend to actually be even more common
863:41 tend to actually be even more common because you can always break out of them
863:43 because you can always break out of them as you couldn't see in Python it looks
863:45 as you couldn't see in Python it looks like this and this is slightly more
863:47 like this and this is slightly more subtle but gone are the curly braces
863:50 subtle but gone are the curly braces gone or the parentheses but ever so
863:52 gone or the parentheses but ever so slight difference
863:54 slight difference toal capital T for true and it's going
863:56 toal capital T for true and it's going to be a capital f for false stupid
863:58 to be a capital f for false stupid little differences eventually you're
863:59 little differences eventually you're going to mistype one or the other but
864:00 going to mistype one or the other but these are the kinds of things to keep an
864:02 these are the kinds of things to keep an eye out and to start recognizing in your
864:04 eye out and to start recognizing in your mind's eye when you read code questions
864:06 mind's eye when you read code questions now on any of these building
864:09 now on any of these building blocks
864:11 blocks yeah
864:18 I in the for Loop was I re uh it was set to zero on the first iteration then one
864:20 to zero on the first iteration then one on the next then two on the third and
864:23 on the next then two on the third and the same thing for range it just doesn't
864:25 the same thing for range it just doesn't use up as much memory all at once other
864:28 use up as much memory all at once other questions now on any of these building
864:30 questions now on any of these building blocks of
864:33 blocks of python no all right well let's go ahead
864:35 python no all right well let's go ahead and build something a little more than
864:37 and build something a little more than hello let me propose that over here we
864:39 hello let me propose that over here we Implement maybe the the simplest of
864:42 Implement maybe the the simplest of calculators here so let me go back to vs
864:44 calculators here so let me go back to vs code here open my terminal uh window and
864:47 code here open my terminal uh window and open up say a file called
864:50 open up say a file called calculator. and in calculator. we'll
864:53 calculator. and in calculator. we'll have an opportunity to explore some of
864:55 have an opportunity to explore some of these building blocks but we'll allow
864:56 these building blocks but we'll allow things to escalate pretty quickly to
864:58 things to escalate pretty quickly to more interesting examples so that we can
865:00 more interesting examples so that we can do the same thing ultimately as well and
865:02 do the same thing ultimately as well and in fact let me go ahead and do this
865:04 in fact let me go ahead and do this moreover I've brought some code with me
865:06 moreover I've brought some code with me in advance uh for instance something
865:08 in advance uh for instance something called calculator 0. C from the first
865:11 called calculator 0. C from the first week of C
865:12 week of C and let me go ahead and split my window
865:14 and let me go ahead and split my window here in fact so that I can now do
865:16 here in fact so that I can now do something like uh this let me move this
865:19 something like uh this let me move this over here here calculator. Pi so now I
865:23 over here here calculator. Pi so now I have on the left of my screen
865:24 have on the left of my screen calculator. C or calculator z.c because
865:27 calculator. C or calculator z.c because that's the first version I made and
865:28 that's the first version I made and calculator. Pi on the right let me go
865:30 calculator. Pi on the right let me go ahead and Implement really the same idea
865:32 ahead and Implement really the same idea here so on the right hand side the
865:34 here so on the right hand side the analog of including cs50.h would be from
865:37 analog of including cs50.h would be from cs50 import get int if I want to indeed
865:40 cs50 import get int if I want to indeed use this function now I'm going to go
865:42 use this function now I'm going to go ahead and give myself a variable X
865:43 ahead and give myself a variable X without defining its type I'm going to
865:45 without defining its type I'm going to use this get int function and I'm going
865:47 use this get int function and I'm going to prompt the user for X just like in C
865:49 to prompt the user for X just like in C I'm then going to go ahead and prompt
865:51 I'm then going to go ahead and prompt the user for another int like y here
865:54 the user for another int like y here just like in C and at the very end I'm
865:56 just like in C and at the very end I'm going to go ahead and do print X Plus Y
865:59 going to go ahead and do print X Plus Y and that's it now granted I have some
866:01 and that's it now granted I have some comments in my C version of the code
866:03 comments in my C version of the code just to remind you of what each line is
866:05 just to remind you of what each line is doing but I've still distilled this into
866:07 doing but I've still distilled this into like six lines or really four if I get
866:08 like six lines or really four if I get rid of the blank line so it's already
866:10 rid of the blank line so it's already perhaps a bit tighter here here but
866:13 perhaps a bit tighter here here but there's also it's tighter because
866:15 there's also it's tighter because something really important historically
866:17 something really important historically is missing what did I seem to Omit
866:20 is missing what did I seem to Omit altogether that we haven't really
866:22 altogether that we haven't really highlighted yet
866:24 highlighted yet yeah yeah the main function is gone and
866:27 yeah yeah the main function is gone and in fact maybe you took for granted that
866:29 in fact maybe you took for granted that it just worked a moment ago when I wrote
866:31 it just worked a moment ago when I wrote hello but I didn't have a main function
866:32 hello but I didn't have a main function in hello either and this too is a
866:34 in hello either and this too is a feature of python and a lot of other
866:36 feature of python and a lot of other languages as well instead of having to
866:38 languages as well instead of having to adhere to these long-standing Traditions
866:39 adhere to these long-standing Traditions if you just want to write code and get
866:41 if you just want to write code and get something done fine just write code and
866:42 something done fine just write code and get something done without necessarily
866:44 get something done without necessarily all of the same boiler plate so whatever
866:47 all of the same boiler plate so whatever is in your python file left indented if
866:49 is in your python file left indented if you will by default is just going to be
866:51 you will by default is just going to be the code that The Interpreter runs top
866:53 the code that The Interpreter runs top to bottom left to right well let me go
866:56 to bottom left to right well let me go ahead now and run code like this let me
866:59 ahead now and run code like this let me go ahead and open that back up my
867:00 go ahead and open that back up my terminal window run python of
867:02 terminal window run python of calculator. piy and I'll do X is one y
867:05 calculator. piy and I'll do X is one y is 2 and as you might expect it gives me
867:07 is 2 and as you might expect it gives me three slight aesthetic bug I put my
867:09 three slight aesthetic bug I put my space in the wrong place here so that's
867:11 space in the wrong place here so that's a new mistake let me fix that
867:12 a new mistake let me fix that aesthetically let me rerun python of
867:14 aesthetically let me rerun python of calculator. Pi type in one type in two
867:17 calculator. Pi type in one type in two and voila there is now my same version
867:20 and voila there is now my same version again but let me propose now that we get
867:23 again but let me propose now that we get rid of this training wheel we don't want
867:24 rid of this training wheel we don't want to keep taking one step forward and then
867:26 to keep taking one step forward and then two steps back by adding these training
867:27 two steps back by adding these training wheels so let me instead do this in my
867:30 wheels so let me instead do this in my version of calculator. suppose that we
867:32 version of calculator. suppose that we take away already the training wheel
867:34 take away already the training wheel that is the cs50 library here and let me
867:38 that is the cs50 library here and let me instead then use just Python's built-in
867:40 instead then use just Python's built-in function called input which literally
867:42 function called input which literally does just that it gets input from the
867:45 does just that it gets input from the user and it stores it as before in X and
867:48 user and it stores it as before in X and Y so this is not cs50 specific this is
867:50 Y so this is not cs50 specific this is real world Python Programming well let
867:52 real world Python Programming well let me go ahead and run again python of
867:54 me go ahead and run again python of calculator. Pi and of course if x is one
867:57 calculator. Pi and of course if x is one and Y is 2 X + Y should of course still
868:00 and Y is 2 X + Y should of course still be three
868:03 be three hm it's apparently 12 according to
868:06 hm it's apparently 12 according to python until cs50's Library gets
868:08 python until cs50's Library gets involved but does anyone want to
868:10 involved but does anyone want to infer what's just went wrong
868:19 yeah exactly the input function by Design always returns a string of text
868:22 Design always returns a string of text after all that's what the human typed in
868:24 after all that's what the human typed in and even though yes I type the number
868:25 and even though yes I type the number keys on the keyboard it's still coming
868:27 keys on the keyboard it's still coming back is all text now maybe we should use
868:30 back is all text now maybe we should use like a get in function well that doesn't
868:32 like a get in function well that doesn't exist in Python all you can do is get
868:34 exist in Python all you can do is get textual input a string from the user but
868:37 textual input a string from the user but we can convert one to the other and so a
868:39 we can convert one to the other and so a fix for this so that we don't accident
868:41 fix for this so that we don't accident Al concatenate that is join X+ y
868:44 Al concatenate that is join X+ y together would be to do something like
868:46 together would be to do something like this let me go back to my python code
868:48 this let me go back to my python code here and whereas in C we could
868:51 here and whereas in C we could previously do type casting we could
868:53 previously do type casting we could convert one type to another that
868:55 convert one type to another that generally wasn't the case when you were
868:57 generally wasn't the case when you were doing something complex like a string to
868:59 doing something complex like a string to an INT you could do a Char to an INT and
869:02 an INT you could do a Char to an INT and vice versa but for a string recall there
869:04 vice versa but for a string recall there was a special function in the ctype
869:06 was a special function in the ctype library called a to I like ask e to
869:08 library called a to I like ask e to integer that's uh that closest analog
869:12 integer that's uh that closest analog here and in fact the way to do this in
869:13 here and in fact the way to do this in Python would be to use a function called
869:16 Python would be to use a function called int which indeed is the name of the data
869:18 int which indeed is the name of the data type 2 even though I have not yet had to
869:20 type 2 even though I have not yet had to type it and I can convert the output of
869:23 type it and I can convert the output of the input function automatically from a
869:26 the input function automatically from a string immediately to an INT and now if
869:29 string immediately to an INT and now if I go back to my terminal window rerun
869:32 I go back to my terminal window rerun python of calculator. piy with one and
869:34 python of calculator. piy with one and two for X and Y now I'm back in business
869:37 two for X and Y now I'm back in business so that then is for instance what the
869:39 so that then is for instance what the cs50 library does if temporarily this
869:41 cs50 library does if temporarily this week is it just deals with the
869:43 week is it just deals with the conversion for you and in fact bad
869:45 conversion for you and in fact bad things could happen if I type the wrong
869:47 things could happen if I type the wrong thing like dog or cat instead of a
869:50 thing like dog or cat instead of a number but we'll cross that bridge in
869:51 number but we'll cross that bridge in just a moment as well all right what if
869:53 just a moment as well all right what if we do something slightly different now
869:55 we do something slightly different now with our calculator instead of just
869:57 with our calculator instead of just addition let me go ahead and do how
869:59 addition let me go ahead and do how about uh div instead of addition let's
870:02 about uh div instead of addition let's do division instead so Z equals x / y
870:05 do division instead so Z equals x / y thereby giving me a third variable Z let
870:08 thereby giving me a third variable Z let me go ahead and run python of
870:10 me go ahead and run python of calculator. piy again I'll type in one
870:13 calculator. piy again I'll type in one I'll type in three this time and what
870:17 I'll type in three this time and what prog what problem do you think we're
870:19 prog what problem do you think we're about to
870:20 about to see or is it gone what happened when I
870:23 see or is it gone what happened when I did this in C albeit with some slightly
870:26 did this in C albeit with some slightly more cryptic syntax when I divided one
870:28 more cryptic syntax when I divided one number like one one divided by
870:31 number like one one divided by three anyone recall
870:35 three anyone recall yeah yeah so it would round down to the
870:38 yeah yeah so it would round down to the nearest integer whereby you experience
870:41 nearest integer whereby you experience trunc so if you take an integer like one
870:44 trunc so if you take an integer like one you divide it by another integer like
870:46 you divide it by another integer like three that technically should be
870:48 three that technically should be 0.33333 infinitely long but in uh C
870:53 0.33333 infinitely long but in uh C recall you truncate the value if you
870:55 recall you truncate the value if you divide an INT by an INT you get back an
870:56 divide an INT by an INT you get back an INT which means you get only the integer
870:58 INT which means you get only the integer part which was the zero now python
871:00 part which was the zero now python actually handles this for us and avoids
871:02 actually handles this for us and avoids the truncation but it leaves us still
871:05 the truncation but it leaves us still with one other problem here which is
871:07 with one other problem here which is going to be for instance not necessarily
871:10 going to be for instance not necessarily visible at a GL
871:12 visible at a GL this looks correct this has solved the
871:13 this looks correct this has solved the problem in C so truncation does not
871:16 problem in C so truncation does not happen the integers are automatically
871:17 happen the integers are automatically converted to a float a floating point
871:19 converted to a float a floating point value but what other problem did we trip
871:22 value but what other problem did we trip over back
871:23 over back in week uh
871:31 one what else got a little dicey when dealing with simple
871:33 dealing with simple arithmetic anyone recall well the syntax
871:36 arithmetic anyone recall well the syntax in Python's a little different but let
871:37 in Python's a little different but let me go ahead and do this it turns out in
871:40 me go ahead and do this it turns out in Python if you want to see more
871:42 Python if you want to see more significant digits than what I'm seeing
871:43 significant digits than what I'm seeing here by the default which is a dozen or
871:45 here by the default which is a dozen or so let me go ahead and print out Z as
871:47 so let me go ahead and print out Z as follows let me first print out a format
871:50 follows let me first print out a format string because I want to format Z in an
871:52 string because I want to format Z in an interesting way and notice this would
871:54 interesting way and notice this would have no effect on the difference this is
871:56 have no effect on the difference this is just a format string that for no
871:57 just a format string that for no compelling reason at the moment is
871:59 compelling reason at the moment is interpolating z in those curly braces
872:01 interpolating z in those curly braces using an F string or format string if I
872:04 using an F string or format string if I run this again with one and three we'll
872:06 run this again with one and three we'll see indeed the exact same thing but when
872:08 see indeed the exact same thing but when you use an F string you indeed have the
872:10 you use an F string you indeed have the ability to format that string more
872:12 ability to format that string more precisely just like with percent F in
872:15 precisely just like with percent F in Python you could start to fine-tune how
872:17 Python you could start to fine-tune how many significant digits you see in uh p
872:20 many significant digits you see in uh p in C rather in Python you can do the
872:23 in C rather in Python you can do the same but the syntax is a little
872:24 same but the syntax is a little different if you want the computer to
872:26 different if you want the computer to interpolate Z and show you 50
872:29 interpolate Z and show you 50 significant digits that is 50 numbers
872:32 significant digits that is 50 numbers after the decimal point syntax is
872:34 after the decimal point syntax is similar to C but it's a little different
872:36 similar to C but it's a little different you literally put a colon after the
872:37 you literally put a colon after the variable's name 50 means show me the
872:40 variable's name 50 means show me the decimal point and then 50 digits to the
872:43 decimal point and then 50 digits to the right and the F just indicates please
872:45 right and the F just indicates please treat this as a floating point value so
872:47 treat this as a floating point value so now if I rerun python of calculator. Pi
872:50 now if I rerun python of calculator. Pi divide 1 by 3 unfortunately python has
872:54 divide 1 by 3 unfortunately python has not solved all of the world's problems
872:56 not solved all of the world's problems for us this again was an example of
872:58 for us this again was an example of floating point in Precision so that
873:00 floating point in Precision so that problem is still latent so just because
873:03 problem is still latent so just because the world has advanced doesn't
873:04 the world has advanced doesn't necessarily mean that all of our
873:05 necessarily mean that all of our problems from C have gone away there are
873:07 problems from C have gone away there are solutions using third-party libraries
873:09 solutions using third-party libraries for scientific calculations and the like
873:12 for scientific calculations and the like but out of the box floating point in
873:13 but out of the box floating point in Precision is still an issue meanwhile
873:17 Precision is still an issue meanwhile there was one other problem in C that we
873:20 there was one other problem in C that we ran into involving numbers and that was
873:22 ran into involving numbers and that was this integer overflow recall that an
873:25 this integer overflow recall that an integer in C only took up what like 32
873:27 integer in C only took up what like 32 bits typically which meant you could
873:28 bits typically which meant you could count as high as four billion or maybe
873:30 count as high as four billion or maybe if you're doing positive and negatives
873:31 if you're doing positive and negatives as high as two billion after which weird
873:34 as high as two billion after which weird things would happen the number would go
873:35 things would happen the number would go to zero or negative or just it would
873:37 to zero or negative or just it would overflow or wrap back around well
873:39 overflow or wrap back around well wonderfully in Python they did it at
873:41 wonderfully in Python they did it at least address this whereby you can count
873:44 least address this whereby you can count as high as you want and python will just
873:46 as high as you want and python will just use more and more and more and more bits
873:48 use more and more and more and more bits and bites to store really big numbers so
873:51 and bites to store really big numbers so integer overflow is not a thing with
873:54 integer overflow is not a thing with that said python is limited to how many
873:57 that said python is limited to how many digits it will show you on the screen at
873:59 digits it will show you on the screen at once as a string but mathematically your
874:01 once as a string but mathematically your math will be correct now so we've taken
874:04 math will be correct now so we've taken a couple steps forward One Step sideways
874:06 a couple steps forward One Step sideways but indeed we've solved some of our
874:08 but indeed we've solved some of our problems here all right questions now
874:11 problems here all right questions now now on any of these examples thus
874:16 now on any of these examples thus far
874:17 far question all right well how about uh how
874:21 question all right well how about uh how about another problem that we
874:22 about another problem that we encountered in C let's revisit it here
874:25 encountered in C let's revisit it here in python as well so let me go ahead and
874:27 in python as well so let me go ahead and on the left hand side here let me open
874:28 on the left hand side here let me open up a file called say compare let's see
874:33 up a file called say compare let's see uh how about a file called compare 3. C
874:37 uh how about a file called compare 3. C on the left and let me go ahead and
874:39 on the left and let me go ahead and create a new file on the right called
874:41 create a new file on the right called compare. Pi because recall that bad
874:43 compare. Pi because recall that bad things happened when we needed to
874:45 things happened when we needed to compare two values in C so on the left
874:48 compare two values in C so on the left here is a reminder of what we once did
874:50 here is a reminder of what we once did in C whereby if we want to compare
874:53 in C whereby if we want to compare values we can get an INT in C stored in
874:55 values we can get an INT in C stored in x a get int in C stored in y we then
874:58 x a get int in C stored in y we then have our familiar conditional logic here
875:00 have our familiar conditional logic here just printing out if x is less than y or
875:03 just printing out if x is less than y or not well we can certainly do the same
875:05 not well we can certainly do the same thing ultimately in Python by using some
875:08 thing ultimately in Python by using some fairly familiar syntax and let's just
875:10 fairly familiar syntax and let's just demonstrate this one quickly let me go
875:12 demonstrate this one quickly let me go over here too I'll do from cs50 import
875:16 over here too I'll do from cs50 import uh get int even though I could do this
875:18 uh get int even though I could do this instead with the input function itself x
875:21 instead with the input function itself x equals get int and I'll prompt the user
875:23 equals get int and I'll prompt the user for that y equals get int and I'll
875:26 for that y equals get int and I'll prompt the user for that after that
875:28 prompt the user for that after that recall that I can say without
875:29 recall that I can say without parentheses if x is less than y then
875:32 parentheses if x is less than y then print out without the F uh X is less
875:35 print out without the F uh X is less than y then I can go ahead and say else
875:38 than y then I can go ahead and say else if x is greater than y i can print out
875:42 if x is greater than y i can print out uh quote unquote X is greater than y if
875:46 uh quote unquote X is greater than y if you'd like to interject now what did I
875:47 you'd like to interject now what did I screw
875:49 screw up anyone yeah L if right so L if L if x
875:55 up anyone yeah L if right so L if L if x is greater than y else this part's the
875:57 is greater than y else this part's the same print X is equal to Y so there's
876:01 same print X is equal to Y so there's not all that much new there's no New
876:03 not all that much new there's no New Logic going on here but at least
876:05 Logic going on here but at least syntactically it's a little cleaner
876:06 syntactically it's a little cleaner indeed this program is only 11 lines
876:08 indeed this program is only 11 lines long albeit without any comments let me
876:10 long albeit without any comments let me go ahead and run python of compare. Pi
876:12 go ahead and run python of compare. Pi let's see is 1 less than two indeed
876:15 let's see is 1 less than two indeed let's run it again is 2 less than 1 no
876:18 let's run it again is 2 less than 1 no it's greater than and let's lastly type
876:20 it's greater than and let's lastly type in one and one twice X is equal to Y so
876:23 in one and one twice X is equal to Y so we've got a pretty side by side onetoone
876:26 we've got a pretty side by side onetoone conversion here let's do something a
876:27 conversion here let's do something a little more interesting then and see how
876:30 little more interesting then and see how about I open instead something where we
876:32 about I open instead something where we actually compared for a purpose so if I
876:34 actually compared for a purpose so if I open up from last uh from earlier in the
876:37 open up from last uh from earlier in the course how about uh agree do C which
876:42 course how about uh agree do C which prompt the user to agree to something or
876:44 prompt the user to agree to something or not and let me code up a new version
876:46 not and let me code up a new version here called agree. pi and I'll do this
876:49 here called agree. pi and I'll do this on the right hand side with agree. pi
876:51 on the right hand side with agree. pi but on gree do c on the left notice that
876:53 but on gree do c on the left notice that this is how we did this sort of yes no
876:55 this is how we did this sort of yes no thing in C we compared c a character
876:59 thing in C we compared c a character equal to single quotes y or equal to
877:02 equal to single quotes y or equal to single quotes little Y and then the same
877:04 single quotes little Y and then the same thing for n now in Python this one's
877:06 thing for n now in Python this one's actually going to be a little bit
877:07 actually going to be a little bit different here let me go ahead and in
877:09 different here let me go ahead and in the python version of this let me do
877:12 the python version of this let me do something like this uh we'll use get
877:14 something like this uh we'll use get string uh actually no we'll just use
877:16 string uh actually no we'll just use input in this case so let's do uh s
877:19 input in this case so let's do uh s equals input and we'll ask the user the
877:22 equals input and we'll ask the user the same thing do you agree question mark
877:25 same thing do you agree question mark then let's go ahead and say if s equals
877:29 then let's go ahead and say if s equals equals how about uh
877:33 equals how about uh y huh how do I do this well a few things
877:36 y huh how do I do this well a few things turns out I'm going to do this s equals
877:38 turns out I'm going to do this s equals equals little y then I'm going to go
877:40 equals little y then I'm going to go ahead and print out a agreed and L if s
877:44 ahead and print out a agreed and L if s equals equals capital N or S equals
877:46 equals equals capital N or S equals equals lowercase n I'm going to go ahead
877:48 equals lowercase n I'm going to go ahead and print out not agreed and I claimed
877:51 and print out not agreed and I claimed for the moment that this is identical
877:52 for the moment that this is identical now to the program on the right the
877:55 now to the program on the right the program on the left in C but what's
877:58 program on the left in C but what's different so we're still doing the same
877:59 different so we're still doing the same kind of logic these equal equals for
878:01 kind of logic these equal equals for comparing for equality but notice that
878:04 comparing for equality but notice that nicely enough python got rid of the two
878:05 nicely enough python got rid of the two vertical bars and it's just literally
878:07 vertical bars and it's just literally the word or if you recall seeing
878:09 the word or if you recall seeing Ampersand Ampersand to express a logical
878:11 Ampersand Ampersand to express a logical and in C you can just write literally
878:13 and in C you can just write literally the word and and so here's a hint of why
878:16 the word and and so here's a hint of why python tends to be pretty popular people
878:18 python tends to be pretty popular people just like that it's a little closer to
878:20 just like that it's a little closer to English there's a little less of the
878:21 English there's a little less of the cryptic syntax here now this is correct
878:24 cryptic syntax here now this is correct as this code will now work but I've also
878:27 as this code will now work but I've also used double quotes instead of single
878:29 used double quotes instead of single quotes and I also omitted a few minutes
878:32 quotes and I also omitted a few minutes ago for my list of data types in Python
878:34 ago for my list of data types in Python the word Char in Python there are no
878:37 the word Char in Python there are no chars there are no individual characters
878:39 chars there are no individual characters if you want to manipulate individual
878:41 if you want to manipulate individual character you use a string that is to
878:43 character you use a string that is to say a stir of size one now in Python you
878:47 say a stir of size one now in Python you can use single quotes or double quotes
878:49 can use single quotes or double quotes I'm deliberately using double quotes
878:51 I'm deliberately using double quotes everywhere just for consistency with how
878:52 everywhere just for consistency with how we treat strings in C it's pretty common
878:55 we treat strings in C it's pretty common though to use single quotes instead if
878:57 though to use single quotes instead if only because on most keyboards you don't
878:58 only because on most keyboards you don't have to hold the shift key anymore I
879:00 have to hold the shift key anymore I mean humans have really started to
879:02 mean humans have really started to optimize just how quickly they want to
879:03 optimize just how quickly they want to be able to code so using a single quote
879:05 be able to code so using a single quote tends to be pretty popular in Python and
879:07 tends to be pretty popular in Python and other languages as well they are
879:09 other languages as well they are fundamentally the same uh single or
879:12 fundamentally the same uh single or double unlike in C where they have
879:14 double unlike in C where they have meaning so this is correct I claim and
879:17 meaning so this is correct I claim and in fact let me run this real quick I'll
879:19 in fact let me run this real quick I'll open up my terminal window here let me
879:21 open up my terminal window here let me get rid of the version and see run
879:23 get rid of the version and see run python of agree. piy and I'll type in y
879:26 python of agree. piy and I'll type in y okay I'll run it again and type in
879:27 okay I'll run it again and type in little Y and I'll stipulate it's going
879:29 little Y and I'll stipulate it's going to work for no as well but this isn't
879:32 to work for no as well but this isn't necessarily the only way we can do this
879:34 necessarily the only way we can do this there are other ways to implement the
879:35 there are other ways to implement the same idea and in fact I can go about
879:39 same idea and in fact I can go about doing this this instead let me go back
879:42 doing this this instead let me go back up to my code here and we saw a hint of
879:46 up to my code here and we saw a hint of this earlier we know that lists exist in
879:49 this earlier we know that lists exist in Python and you can create them just by
879:51 Python and you can create them just by using square brackets so what if I
879:53 using square brackets so what if I simplify the code a little bit and just
879:55 simplify the code a little bit and just say if s is in the following list of
879:58 say if s is in the following list of values capital y or lowercase y it's not
880:02 values capital y or lowercase y it's not all that different logically but it's a
880:04 all that different logically but it's a little tighter it's a little more
880:06 little tighter it's a little more compact so L ifs is in capital N or
880:10 compact so L ifs is in capital N or lowercase n i can express that same idea
880:13 lowercase n i can express that same idea too so here again it's just getting a
880:14 too so here again it's just getting a little more pleasant to write code
880:16 little more pleasant to write code there's less like hitting of the
880:18 there's less like hitting of the keyboard you can express yourself a
880:19 keyboard you can express yourself a little more succinctly and using the
880:21 little more succinctly and using the keyword in Python will figure out how to
880:24 keyword in Python will figure out how to search the entire list for whatever the
880:27 search the entire list for whatever the value of s is and if it finds it it will
880:30 value of s is and if it finds it it will return true automatically else it will
880:32 return true automatically else it will return false so if I run agree. Pi again
880:35 return false so if I run agree. Pi again and type in capital y or lowercase y
880:38 and type in capital y or lowercase y that still now works well I can typen
880:41 that still now works well I can typen tighten this up further if I want to add
880:44 tighten this up further if I want to add more features well what if I want to
880:46 more features well what if I want to support not just why Big Y and little y
880:49 support not just why Big Y and little y but how about yes or yes or in case the
880:54 but how about yes or yes or in case the user's yelling or you know someone who
880:56 user's yelling or you know someone who doesn't really isn't good with caps lock
880:57 doesn't really isn't good with caps lock types in yes wait a minute but it could
881:00 types in yes wait a minute but it could be weird like do we want to support this
881:03 be weird like do we want to support this or this I mean this this just gets
881:06 or this I mean this this just gets really tedious quickly combinatorically
881:08 really tedious quickly combinatorically if you consider all of these possible
881:09 if you consider all of these possible permutations what would be smarter than
881:11 permutations what would be smarter than doing something like this if you want to
881:13 doing something like this if you want to just be able to tolerate yes in any form
881:16 just be able to tolerate yes in any form of capitalization like logically what
881:19 of capitalization like logically what would be nice maybe whatever theut is
881:22 would be nice maybe whatever theut is you justf over to all lower and then
881:25 you justf over to all lower and then exactly super common Paradigm why don't
881:27 exactly super common Paradigm why don't we just force the users's input to all
881:29 we just force the users's input to all lowercase or all uppercase doesn't
881:31 lowercase or all uppercase doesn't matter so long as we're self-consistent
881:33 matter so long as we're self-consistent and just compare against all uppercase
881:35 and just compare against all uppercase or all lowercase and that will get rid
881:37 or all lowercase and that will get rid of all of the possible permutations
881:39 of all of the possible permutations otherwise now in C we might have done
881:42 otherwise now in C we might have done something like this we might have
881:43 something like this we might have simplified this whole list and just said
881:46 simplified this whole list and just said let's say uh we'll do how about
881:50 let's say uh we'll do how about lowercase so y or yes and we'll just
881:54 lowercase so y or yes and we'll just leave it at that but we need to force
881:55 leave it at that but we need to force now s to lowercase well in C we would
881:58 now s to lowercase well in C we would have used the ctype library we would
882:00 have used the ctype library we would have done like two lower and called that
882:03 have done like two lower and called that function passing it in although not
882:05 function passing it in although not really cuz in C type those operate on
882:07 really cuz in C type those operate on individual characters or chars not whole
882:09 individual characters or chars not whole strings we have actually didn't see a
882:11 strings we have actually didn't see a function that could convert the whole
882:12 function that could convert the whole string in C to lowercase but in Python
882:16 string in C to lowercase but in Python we're going to benefit from some other
882:18 we're going to benefit from some other feature as well it turns out that python
882:21 feature as well it turns out that python supports what's called objectoriented
882:23 supports what's called objectoriented programming and we're only going to
882:24 programming and we're only going to scratch the surface of this in cs50 but
882:26 scratch the surface of this in cs50 but if you take a higher level course in
882:28 if you take a higher level course in programming or CS you explore this as a
882:30 programming or CS you explore this as a different Paradigm up until now in C
882:32 different Paradigm up until now in C we've been focusing on what's called
882:34 we've been focusing on what's called really procedural programming you write
882:36 really procedural programming you write procedures you write functions top to
882:38 procedures you write functions top to bottom uh left to right and when you
882:40 bottom uh left to right and when you want to change some value we were in the
882:42 want to change some value we were in the habit of using a procedure that is a
882:44 habit of using a procedure that is a function you would pass something like a
882:46 function you would pass something like a variable into a function like to Upper
882:48 variable into a function like to Upper or to lower and it would do its thing
882:50 or to lower and it would do its thing and hand you back a value well it turns
882:52 and hand you back a value well it turns out that it would be nicer programming
882:55 out that it would be nicer programming wise if some data types just had
882:58 wise if some data types just had built-in functionality like why do we
883:00 built-in functionality like why do we have our variables over here and all of
883:02 have our variables over here and all of our helper functions like two upper and
883:03 our helper functions like two upper and two lower over here such that we
883:05 two lower over here such that we constantly have to pass one into the
883:07 constantly have to pass one into the other it would be nice to sort of bake
883:09 other it would be nice to sort of bake into our data types buil-in
883:11 into our data types buil-in functionality so that you can change
883:14 functionality so that you can change variables using their own default be uh
883:17 variables using their own default be uh buil-in functionality and so
883:19 buil-in functionality and so object-oriented programming otherwise
883:20 object-oriented programming otherwise known as oop is a technique whereby
883:23 known as oop is a technique whereby certain types of values like a string
883:26 certain types of values like a string AKA stir not only have properties inside
883:31 AKA stir not only have properties inside of them attributes just like a struct in
883:33 of them attributes just like a struct in C your data can also have functions
883:37 C your data can also have functions built into them as well so whereas in C
883:40 built into them as well so whereas in C which is not objectoriented you have
883:42 which is not objectoriented you have strs and strs can only store data like a
883:44 strs and strs can only store data like a name and a number when implementing a
883:46 name and a number when implementing a person in Python you can for instance
883:49 person in Python you can for instance have not just a structure otherwise
883:52 have not just a structure otherwise known as a class storing a name and a
883:54 known as a class storing a name and a number you can have a function like call
883:57 number you can have a function like call that person or email that person or
884:00 that person or email that person or actual verbs or actions associated with
884:02 actual verbs or actions associated with that piece of data now in the context of
884:05 that piece of data now in the context of strings it turns out that strings come
884:07 strings it turns out that strings come with a lot of useful functionality and
884:09 with a lot of useful functionality and in fact this URL here which is in docs.
884:12 in fact this URL here which is in docs. python.org which is the official
884:14 python.org which is the official documentation for python you'll see a
884:17 documentation for python you'll see a whole list of methods that is functions
884:19 whole list of methods that is functions that come with strings that you can
884:22 that come with strings that you can actually use to modify their values and
884:24 actually use to modify their values and what I mean by this is the following if
884:27 what I mean by this is the following if we go through the documentation poke
884:28 we go through the documentation poke around it turns out that strings come
884:30 around it turns out that strings come with a function called Lower and if you
884:33 with a function called Lower and if you want to use that function you just have
884:34 want to use that function you just have to use slightly different syntax than in
884:36 to use slightly different syntax than in C you do not do to lower and you do not
884:39 C you do not do to lower and you do not say as I just did lower because this
884:42 say as I just did lower because this function is built into s itself and just
884:46 function is built into s itself and just like in C when you want to go inside of
884:48 like in C when you want to go inside of a variable like a structure and access a
884:51 a variable like a structure and access a piece of data inside of it like name or
884:53 piece of data inside of it like name or number when you also have functions
884:55 number when you also have functions built into data types AKA methods a
884:58 built into data types AKA methods a method is just a function that is built
885:00 method is just a function that is built into a piece of data you can do s do
885:04 into a piece of data you can do s do lower open pen closed pen in this case
885:08 lower open pen closed pen in this case and I can do this down here as well if
885:10 and I can do this down here as well if if s do lower in quote unquote uh n or
885:16 if s do lower in quote unquote uh n or no the whole thing I can force this
885:19 no the whole thing I can force this whole thing to lowercase so the only
885:20 whole thing to lowercase so the only difference here now is in
885:21 difference here now is in object-oriented programming instead of
885:23 object-oriented programming instead of constantly passing a value into a
885:25 constantly passing a value into a function you just access a function
885:28 function you just access a function that's inside of the value it just works
885:31 that's inside of the value it just works because of how the language itself is
885:33 because of how the language itself is defined and the only way you know that
885:34 defined and the only way you know that these functions exist is the
885:36 these functions exist is the documentation a Class A book a website
885:38 documentation a Class A book a website or the like questions now on this
885:46 technique all right I claim this is correct now even though you've never
885:48 correct now even though you've never programmed most of you in Python before
885:50 programmed most of you in Python before not super well-designed there's an
885:52 not super well-designed there's an subtle inefficiency now on lines three
885:55 subtle inefficiency now on lines three and five together what's dumb about how
885:58 and five together what's dumb about how I've used lower might you
886:01 I've used lower might you think
886:08 yeah yeah if you're going to use the same function twice and ask the same
886:09 same function twice and ask the same question expecting the same answer why
886:11 question expecting the same answer why are you calling the function itself
886:13 are you calling the function itself twice maybe we should just store the
886:15 twice maybe we should just store the result in a variable so we could do this
886:16 result in a variable so we could do this in a couple of different ways we for
886:18 in a couple of different ways we for instance could go up here and create
886:19 instance could go up here and create another variable called T and set that
886:21 another variable called T and set that equal to s. lower and then we could just
886:24 equal to s. lower and then we could just change this to be T here but honestly I
886:27 change this to be T here but honestly I don't think we technically need another
886:28 don't think we technically need another variable Al together here I could just
886:30 variable Al together here I could just do something like this let's change the
886:32 do something like this let's change the value of s to be the lowercase version
886:36 value of s to be the lowercase version thereof and so now I can quite simply
886:38 thereof and so now I can quite simply refer to S again and again like this
886:40 refer to S again and again like this this reusing that same value now to be
886:42 this reusing that same value now to be sure I have now just lost the user's
886:45 sure I have now just lost the user's original input and if I care about that
886:47 original input and if I care about that if they typed in all caps I have no idea
886:49 if they typed in all caps I have no idea anymore so maybe I do want to use a
886:51 anymore so maybe I do want to use a separate variable altogether but a
886:52 separate variable altogether but a takeaway here too is that strings in
886:54 takeaway here too is that strings in Python are technically what we'll call
886:57 Python are technically what we'll call immutable that is they cannot be changed
887:00 immutable that is they cannot be changed this was not true in C once we gave you
887:02 this was not true in C once we gave you arrays in week two or memory in week
887:05 arrays in week two or memory in week four you could go to town on a string
887:07 four you could go to town on a string and change any of the characters you
887:08 and change any of the characters you want upper casing lower casing changing
887:10 want upper casing lower casing changing it shortening it and so forth but in
887:12 it shortening it and so forth but in this case uh this returns a copy of s
887:16 this case uh this returns a copy of s forced to lowercase it doesn't change
887:19 forced to lowercase it doesn't change the original string that is the memory
887:21 the original string that is the memory the bytes in the computer's memory when
887:23 the bytes in the computer's memory when you assign it back to S you're
887:25 you assign it back to S you're essentially forgetting about the old
887:27 essentially forgetting about the old version of s but because python does
887:29 version of s but because python does memory management for you there's no
887:31 memory management for you there's no maloc there's no free python
887:33 maloc there's no free python automatically frees up the original
887:35 automatically frees up the original bytes like yees and hands them back to
887:37 bytes like yees and hands them back to the operating system for you all right
887:40 the operating system for you all right questions now on this
887:43 questions now on this technique questions on
887:45 technique questions on this in general I'll call out the python
887:49 this in general I'll call out the python documentation will start to be your
887:51 documentation will start to be your friend because in class we'll only
887:52 friend because in class we'll only scratch the surface with some of these
887:54 scratch the surface with some of these things but in docs. python.org for
887:56 things but in docs. python.org for instance there's a whole reference of
887:57 instance there's a whole reference of all of the built-in functions that come
887:59 all of the built-in functions that come with the language as well as for
888:01 with the language as well as for instance those with the string all right
888:03 instance those with the string all right well let me go ahead and before we take
888:05 well let me go ahead and before we take a break let's go ahead and create
888:06 a break let's go ahead and create something a little familiar to based on
888:09 something a little familiar to based on our week here in C let me propose that
888:12 our week here in C let me propose that we revisit those examples in involving
888:14 we revisit those examples in involving some meow so for instance when we had
888:17 some meow so for instance when we had our cat meow back in the first week and
888:18 our cat meow back in the first week and then second in C we did something that
888:20 then second in C we did something that was a little stupid at first whereby we
888:23 was a little stupid at first whereby we created a file as I'll do here this time
888:25 created a file as I'll do here this time called meow. p and if I want a cat to
888:27 called meow. p and if I want a cat to meow three times I could run it once
888:30 meow three times I could run it once like this little copy paste and now
888:33 like this little copy paste and now python of meow. py and I'm done now
888:35 python of meow. py and I'm done now we've visited this example like two
888:37 we've visited this example like two times at least now in scratch it and see
888:39 times at least now in scratch it and see it's correct I'll stipulate but what's
888:41 it's correct I'll stipulate but what's obviously poorly
888:43 obviously poorly designed what's the fault here
888:47 designed what's the fault here yeah it should just be a loop right like
888:49 yeah it should just be a loop right like why type it three times literally
888:50 why type it three times literally copying and pasting is almost always a
888:52 copying and pasting is almost always a bad thing except in C when you have the
888:54 bad thing except in C when you have the function prototypes that you need to
888:55 function prototypes that you need to borrow but in this case this is just
888:57 borrow but in this case this is just inefficient so what could we do better
888:59 inefficient so what could we do better here in Python well in Python we could
889:01 here in Python well in Python we could probably change this in a few different
889:03 probably change this in a few different ways we could borrow some of the syntax
889:04 ways we could borrow some of the syntax we proposed in slide form earlier like
889:07 we proposed in slide form earlier like give me a variable called I set it to
889:09 give me a variable called I set it to zero no semicolon while I is less than
889:12 zero no semicolon while I is less than three if I want to do this three times I
889:14 three if I want to do this three times I can go ahead and print out meow and then
889:16 can go ahead and print out meow and then I can do i+ equals one and I think this
889:19 I can do i+ equals one and I think this would do the trick python of meow. and
889:21 would do the trick python of meow. and we're back in business already well if I
889:23 we're back in business already well if I wanted to change this to a for Loop well
889:26 wanted to change this to a for Loop well in Python it would be a little tighter
889:27 in Python it would be a little tighter but this would not be the best approach
889:29 but this would not be the best approach so for I in uh
889:32 so for I in uh 012 I could just do print meow like this
889:37 012 I could just do print meow like this and that too would get the job done but
889:39 and that too would get the job done but to my to our discussion earlier this
889:41 to my to our discussion earlier this would get stupid pretty quickly if you
889:43 would get stupid pretty quickly if you had to keep enumerating all of these
889:45 had to keep enumerating all of these values like what did we introduce
889:46 values like what did we introduce instead the the range function exactly
889:50 instead the the range function exactly so that hands me back way more
889:51 so that hands me back way more efficiently just the values I want
889:54 efficiently just the values I want indeed one at a time so even this if I
889:55 indeed one at a time so even this if I run it a third a third or fourth time
889:58 run it a third a third or fourth time we've got the same result but now let's
890:00 we've got the same result but now let's transition to where we went with this
890:02 transition to where we went with this back in the day how can we start to
890:03 back in the day how can we start to modularize this like just like it would
890:05 modularize this like just like it would be nice I claimed if MIT had given us a
890:07 be nice I claimed if MIT had given us a meow function wouldn't it be nice if
890:09 meow function wouldn't it be nice if like python had given us a meow function
890:11 like python had given us a meow function maybe less compelling in Python but how
890:14 maybe less compelling in Python but how can I build my own function well I did
890:15 can I build my own function well I did this briefly with the spell checker
890:17 this briefly with the spell checker earlier but let me go ahead and propose
890:19 earlier but let me go ahead and propose that we could Implement now our own
890:21 that we could Implement now our own version of this in python as follows let
890:25 version of this in python as follows let me go ahead and start fresh here and use
890:27 me go ahead and start fresh here and use the keyword defa so this did not exist
890:29 the keyword defa so this did not exist in C you had the return value the
890:32 in C you had the return value the function name the arguments in Python
890:34 function name the arguments in Python you literally say defa to define a
890:36 you literally say defa to define a function you give it a name like meow
890:39 function you give it a name like meow and now now I'm going to go ahead and in
890:40 and now now I'm going to go ahead and in this function just print out meow and
890:42 this function just print out meow and this lets me change it to anything else
890:44 this lets me change it to anything else I want in the future but for now it's an
890:47 I want in the future but for now it's an abstraction and in fact I can uh move it
890:51 abstraction and in fact I can uh move it out of sight out of mind just going to
890:52 out of sight out of mind just going to hit enter a bunch of times to pretend
890:54 hit enter a bunch of times to pretend like now it exists but I don't care how
890:56 like now it exists but I don't care how it is implemented and up here now I can
890:59 it is implemented and up here now I can do something like this 4 I in range of
891:01 do something like this 4 I in range of three let me go ahead and not print meow
891:04 three let me go ahead and not print meow anymore let me just call meow and
891:06 anymore let me just call meow and tightening up my code further but I
891:08 tightening up my code further but I think let's see python of meow. py this
891:11 think let's see python of meow. py this is I think going to be the first time it
891:13 is I think going to be the first time it does not work
891:15 does not work correctly okay so here we have sadly our
891:19 correctly okay so here we have sadly our first python error and let's see the
891:22 first python error and let's see the syntax is going to be different from C
891:23 syntax is going to be different from C or clangs output traceback is like the
891:25 or clangs output traceback is like the term of art here this is like a trace
891:27 term of art here this is like a trace back of all of the lines of code that
891:29 back of all of the lines of code that were just executed or really functions
891:31 were just executed or really functions you called the file name is
891:33 you called the file name is uninteresting this is like my codes
891:34 uninteresting this is like my codes space specifically but the file name is
891:36 space specifically but the file name is important here meow. py uh line two is
891:39 important here meow. py uh line two is is the issue okay I didn't get very far
891:41 is the issue okay I didn't get very far before I screwed up and then there's a
891:43 before I screwed up and then there's a name error and you'll see in Python
891:45 name error and you'll see in Python there's typically these capitalized uh
891:47 there's typically these capitalized uh keywords that hint at what the issue is
891:49 keywords that hint at what the issue is it's something related to names of
891:51 it's something related to names of variables meow is not defined all right
891:54 variables meow is not defined all right you're programming python for the first
891:55 you're programming python for the first time you've screwed up you're following
891:57 time you've screwed up you're following some online tutorial you're seeing this
892:00 some online tutorial you're seeing this reason through it like why might meow
892:04 reason through it like why might meow not be defined what can weer infer about
892:08 not be defined what can weer infer about python how to troubleshoot
892:11 python how to troubleshoot logically is it me ised after maybe is
892:14 logically is it me ised after maybe is it because meow is defined after you
892:16 it because meow is defined after you know as smart as python seems to be Visa
892:18 know as smart as python seems to be Visa VC they have some similar design
892:21 VC they have some similar design characteristics so let's try that so let
892:22 characteristics so let's try that so let me scroll all the way back down to where
892:24 me scroll all the way back down to where I move this earlier let me uh get rid of
892:27 I move this earlier let me uh get rid of it way down there I'll copy it to my
892:30 it way down there I'll copy it to my clipboard and let me just kind of hack
892:32 clipboard and let me just kind of hack something together let me just put it up
892:34 something together let me just put it up here and let's see if this works so now
892:36 here and let's see if this works so now let me clear my terminal run python of
892:38 let me clear my terminal run python of meow. okay we're back in business so
892:40 meow. okay we're back in business so that was actually really good intuition
892:42 that was actually really good intuition good debugging technique to sort of
892:44 good debugging technique to sort of reason through it now this is kind of
892:45 reason through it now this is kind of contradicting what I claimed back in
892:47 contradicting what I claimed back in week one which was that you know the
892:48 week one which was that you know the main part of your program ideally should
892:50 main part of your program ideally should just be at the top of the file like
892:52 just be at the top of the file like don't make me look for it it's not a
892:53 don't make me look for it it's not a huge deal with like a four-line program
892:55 huge deal with like a four-line program but if you've got 40 lines 400 lines you
892:57 but if you've got 40 lines 400 lines you don't want like the juice juicy part of
892:59 don't want like the juice juicy part of your program to be way down here and all
893:01 your program to be way down here and all of these functions way up here so it
893:03 of these functions way up here so it would be nice maybe if we actually have
893:05 would be nice maybe if we actually have a main function and so it actually turns
893:07 a main function and so it actually turns out to be a convention in Python to
893:10 out to be a convention in Python to define a main function it's not a
893:12 define a main function it's not a special function that's automatically
893:14 special function that's automatically called like in C but humans realized you
893:16 called like in C but humans realized you know what that was a pretty useful
893:17 know what that was a pretty useful feature let me Define a function called
893:20 feature let me Define a function called main let me indent these lines
893:22 main let me indent these lines underneath it let me practice what I'm
893:24 underneath it let me practice what I'm preaching which is put the main code at
893:26 preaching which is put the main code at the top of the file and wonderfully in
893:29 the top of the file and wonderfully in Python now you do not need prototypes
893:32 Python now you do not need prototypes there's none of that hackish copying and
893:34 there's none of that hackish copying and pasting of the return type the name and
893:35 pasting of the return type the name and the arguments to a function like we
893:37 the arguments to a function like we needed in C this is now
893:40 needed in C this is now okay instead except for one Minor Detail
893:43 okay instead except for one Minor Detail let me go ahead and run python of
893:45 let me go ahead and run python of meow. hopefully now I've solve this
893:48 meow. hopefully now I've solve this problem by having a main function but
893:50 problem by having a main function but now nothing has happened all right even
893:53 now nothing has happened all right even if you've never programmed in Python
893:55 if you've never programmed in Python before What might explain this
893:59 before What might explain this behavior and how do I
894:01 behavior and how do I fix again when you're off in the real
894:03 fix again when you're off in the real world learning some new language all you
894:05 world learning some new language all you have is deductive logic to debug yeah I
894:09 have is deductive logic to debug yeah I remember
894:18 right so the solution to be clear in C was that we had to put the Prototype up
894:20 was that we had to put the Prototype up here otherwise we'd get an error message
894:21 here otherwise we'd get an error message in this case I'm actually not getting an
894:23 in this case I'm actually not getting an error message and indeed I'll claim that
894:25 error message and indeed I'll claim that you don't need the prototypes in Python
894:27 you don't need the prototypes in Python just not necessary because that was
894:28 just not necessary because that was annoying if nothing
894:30 annoying if nothing else but what else might explain yeah
894:33 else but what else might explain yeah I'm
894:35 I'm back yeah maybe you have to call Main
894:37 back yeah maybe you have to call Main itself if main is not some some special
894:40 itself if main is not some some special status in Python maybe just because it
894:42 status in Python maybe just because it exists isn't enough and indeed if you
894:44 exists isn't enough and indeed if you want to call Maine the new convention is
894:47 want to call Maine the new convention is actually going to be as the very last
894:49 actually going to be as the very last line of your program typically to
894:50 line of your program typically to literally call Main it's a little stupid
894:53 literally call Main it's a little stupid stupid looking but you know they made a
894:55 stupid looking but you know they made a design decision and this is how now we
894:56 design decision and this is how now we work around it python of meowy now we're
895:00 work around it python of meowy now we're back in business but now logically why
895:02 back in business but now logically why does this work the way it does well in
895:04 does this work the way it does well in this case top to bottom line one is
895:07 this case top to bottom line one is telling python to define a fun fun
895:09 telling python to define a fun fun called Main and then Define it as
895:11 called Main and then Define it as follows lines two and three but it's not
895:12 follows lines two and three but it's not calling main yet line six is telling
895:15 calling main yet line six is telling python how to define a function called
895:17 python how to define a function called meow but it's not calling these lines
895:19 meow but it's not calling these lines yet now line 10 you're telling python
895:22 yet now line 10 you're telling python call Main and at that point python has
895:24 call Main and at that point python has been trained if you will to know what
895:26 been trained if you will to know what main is on line one to know what meow is
895:28 main is on line one to know what meow is on line six and so it's now perfectly
895:31 on line six and so it's now perfectly okay for Maine to be above meow because
895:34 okay for Maine to be above meow because you never called them yet you defined
895:36 you never called them yet you defined defined and then you called and that's
895:39 defined and then you called and that's the logic behind this any questions now
895:41 the logic behind this any questions now on the structure of this technique
895:45 on the structure of this technique here now let's do one more then recall
895:48 here now let's do one more then recall that the last thing we did in scratch
895:49 that the last thing we did in scratch and in Python uh scratch and in C was to
895:52 and in Python uh scratch and in C was to actually
895:53 actually parameterize uh these same function so
895:55 parameterize uh these same function so suppose that you don't want Maine to be
895:57 suppose that you don't want Maine to be responsible for the loop here you
895:59 responsible for the loop here you instead want to very simply do something
896:00 instead want to very simply do something like meow three times and be done with
896:02 like meow three times and be done with it well in Python it's going to be
896:04 it well in Python it's going to be similar in spirit toy but again we don't
896:06 similar in spirit toy but again we don't need to keep mentioning data types if
896:08 need to keep mentioning data types if you want me now to take some argument
896:10 you want me now to take some argument like a number n you can just specify n
896:14 like a number n you can just specify n as the name of that argument or you
896:15 as the name of that argument or you could call it anything else of course
896:17 could call it anything else of course that you want you don't have to specify
896:18 that you want you don't have to specify int or anything else in your code now
896:21 int or anything else in your code now inside of meow you can do something like
896:23 inside of meow you can do something like for I in let's say I definitely now
896:27 for I in let's say I definitely now can't do this because like that would be
896:29 can't do this because like that would be weird to start the list and end it with
896:31 weird to start the list and end it with n so if I can come back over here what's
896:32 n so if I can come back over here what's the solution how can I do something n
896:36 the solution how can I do something n times yeah using range so range is nice
896:40 times yeah using range so range is nice cuz I can pass in now this variable n
896:42 cuz I can pass in now this variable n and now I can meow whoops now I can
896:45 and now I can meow whoops now I can print out quote unquote meow so it's
896:48 print out quote unquote meow so it's almost the same as in scratch almost the
896:49 almost the same as in scratch almost the same as in C but it's a little simpler
896:51 same as in C but it's a little simpler and if now I run meow. P I'll have the
896:54 and if now I run meow. P I'll have the ability now to do this here as well all
896:58 ability now to do this here as well all right questions on any of this right now
897:01 right questions on any of this right now we're sort of like taking the stroll
897:03 we're sort of like taking the stroll through week one we're going to
897:05 through week one we're going to momentarily escalate things to look not
897:07 momentarily escalate things to look not only at some of these basic but also
897:09 only at some of these basic but also other features like we saw with face
897:11 other features like we saw with face recognition with the speller or the like
897:13 recognition with the speller or the like um because of how many of us are here we
897:15 um because of how many of us are here we have a huge amount of candy out in the
897:17 have a huge amount of candy out in the lobby so why don't we go ahead and take
897:18 lobby so why don't we go ahead and take a 10-minute break and we come back we'll
897:20 a 10-minute break and we come back we'll do even fancier more powerful things
897:21 do even fancier more powerful things with python in 10 all right so we are
897:26 with python in 10 all right so we are back among our goals now are to
897:27 back among our goals now are to introduce a few more building blocks so
897:29 introduce a few more building blocks so that we can solve more interesting
897:31 that we can solve more interesting problems at the end much like those that
897:33 problems at the end much like those that we began with you recall from a few
897:34 we began with you recall from a few weeks ago we played with this sort of
897:36 weeks ago we played with this sort of two-dimensional Super Mario World and we
897:38 two-dimensional Super Mario World and we tried to print a vertical column of like
897:40 tried to print a vertical column of like three or more bricks well let me propose
897:42 three or more bricks well let me propose that we use this as an opportunity to
897:44 that we use this as an opportunity to now Tinker with some of Python's more uh
897:46 now Tinker with some of Python's more uh useful more userfriendly functionality
897:48 useful more userfriendly functionality as well so let me code a file called
897:50 as well so let me code a file called mario. py and let's just print out like
897:54 mario. py and let's just print out like that the equivalent of that vertical
897:55 that the equivalent of that vertical column so it's of height three each one
897:57 column so it's of height three each one is a hash so let's do for I in range of
898:00 is a hash so let's do for I in range of three initially and let's just print out
898:02 three initially and let's just print out a single hash and I think now python of
898:05 a single hash and I think now python of mario. py voila we're in business
898:07 mario. py voila we're in business printing out just that same pyramid
898:10 printing out just that same pyramid there or just that same column there
898:12 there or just that same column there what if though we want to print a column
898:14 what if though we want to print a column of like some variable height where the
898:16 of like some variable height where the user tells us how tall they want it to
898:18 user tells us how tall they want it to be well let me go up here for instance
898:20 be well let me go up here for instance and instead how about we'll use um let's
898:24 and instead how about we'll use um let's do this how about uh from cs50
898:28 do this how about uh from cs50 import how about the get in function as
898:31 import how about the get in function as before so it will deal with making sure
898:33 before so it will deal with making sure the user gives us an integer and now in
898:36 the user gives us an integer and now in the past whenever we wanted to get a
898:37 the past whenever we wanted to get a number from a user we've actually
898:40 number from a user we've actually followed a certain Paradigm in fact if I
898:42 followed a certain Paradigm in fact if I open up here for instance uh how about
898:46 open up here for instance uh how about Mario in how about Mario 1. C from a
898:50 Mario in how about Mario 1. C from a while back you might recall that we had
898:54 while back you might recall that we had code like this and we specifically use
898:57 code like this and we specifically use the do while loop and see whenever we
898:59 the do while loop and see whenever we want to like get something from the user
899:01 want to like get something from the user maybe again and again and again until
899:02 maybe again and again and again until they cooperate at which point we finally
899:04 they cooperate at which point we finally break out of the loop so it turns out
899:06 break out of the loop so it turns out python does have while Loops does have
899:07 python does have while Loops does have four Loops does not have do while loops
899:10 four Loops does not have do while loops and yet pretty much anytime you've
899:11 and yet pretty much anytime you've gotten user input you've probably used
899:13 gotten user input you've probably used this Paradigm so it turns out that the
899:15 this Paradigm so it turns out that the python equivalent of this is to do
899:18 python equivalent of this is to do similar in spirit but using only a while
899:20 similar in spirit but using only a while loop and a common Paradigm in python as
899:22 loop and a common Paradigm in python as I alluded earlier is to actually
899:24 I alluded earlier is to actually deliberately induce an infinite Loop
899:26 deliberately induce an infinite Loop while true capital T and then do what
899:29 while true capital T and then do what you want to do like get an INT from the
899:32 you want to do like get an INT from the user and prompt them for the height for
899:34 user and prompt them for the height for instance in question and then if you're
899:38 instance in question and then if you're sure that the user has given you what
899:40 sure that the user has given you what you want like n is greater than zero
899:42 you want like n is greater than zero which is what I want in this case cuz I
899:44 which is what I want in this case cuz I want a positive integer otherwise
899:45 want a positive integer otherwise there's nothing to print you literally
899:47 there's nothing to print you literally just break out of the loop and so we
899:49 just break out of the loop and so we could actually use this technique in C
899:51 could actually use this technique in C it's just not really done in C you could
899:54 it's just not really done in C you could absolutely in C have done a while true
899:56 absolutely in C have done a while true loop with the parentheses lowercase true
899:58 loop with the parentheses lowercase true you could break out of it and so forth
900:00 you could break out of it and so forth but in Python this is like the python
900:02 but in Python this is like the python way and this is actually a term of art
900:04 way and this is actually a term of art this way in Python is pythonic like this
900:06 this way in Python is pythonic like this is the way everyone does it quote
900:09 is the way everyone does it quote unquote doesn't mean you have to but
900:10 unquote doesn't mean you have to but that's sort of the way like the cool
900:12 that's sort of the way like the cool python programmers would Implement an
900:14 python programmers would Implement an idea like this trying to do something
900:15 idea like this trying to do something again and again and again until the user
900:17 again and again and again until the user actually cooperates but all we've done
900:19 actually cooperates but all we've done is take away the do while loop but still
900:21 is take away the do while loop but still logically we can implement the same idea
900:24 logically we can implement the same idea now below this let me go ahead and just
900:26 now below this let me go ahead and just print out for I in range of n this time
900:29 print out for I in range of n this time because I want it to be variable and not
900:31 because I want it to be variable and not three I can go ahead and print out the
900:33 three I can go ahead and print out the hash let me go ahead and get rid of the
900:35 hash let me go ahead and get rid of the C version here open my terminal window
900:38 C version here open my terminal window and I'll run again python of mario. py
900:40 and I'll run again python of mario. py I'll type in three and I get back those
900:42 I'll type in three and I get back those three hashes but if I instead type in
900:44 three hashes but if I instead type in four I now get four hashes instead so
900:47 four I now get four hashes instead so the takeaway here is quite simply that
900:49 the takeaway here is quite simply that this would be the way for instance to
900:50 this would be the way for instance to actually get back a value in Python that
900:54 actually get back a value in Python that is consistent with some parameter like
900:56 is consistent with some parameter like greater than zero how about this let's
900:58 greater than zero how about this let's actually uh practice what we preached a
901:00 actually uh practice what we preached a moment ago with our meowing examples and
901:02 moment ago with our meowing examples and kind of factoring all this out let me go
901:04 kind of factoring all this out let me go ahead and Define a main function as
901:06 ahead and Define a main function as before let me go ahead and assume for
901:09 before let me go ahead and assume for the moment that a get height function
901:11 the moment that a get height function exists which is not a thing in Python
901:13 exists which is not a thing in Python I'm going to invent it in just a moment
901:15 I'm going to invent it in just a moment and now I'm going to go ahead and do
901:16 and now I'm going to go ahead and do something like this for I in the range
901:19 something like this for I in the range of that height well let's go ahead and
901:21 of that height well let's go ahead and print out those hashes so I'm assuming
901:24 print out those hashes so I'm assuming that get height exists let me go ahead
901:26 that get height exists let me go ahead and Implement that abstraction so Define
901:28 and Implement that abstraction so Define a function now called get height it's
901:30 a function now called get height it's not going to take any arguments in this
901:32 not going to take any arguments in this design while true I can go ahead and do
901:36 design while true I can go ahead and do the same thing as before assign a
901:38 the same thing as before assign a variable n the return value of get int
901:40 variable n the return value of get int prompting the user for that height and
901:43 prompting the user for that height and then if n is greater than zero I can go
901:46 then if n is greater than zero I can go ahead and break but if I break here I
901:51 ahead and break but if I break here I logically just like can see end up
901:53 logically just like can see end up executing below the loop in question but
901:56 executing below the loop in question but there's nothing there but if I want get
901:58 there's nothing there but if I want get height to return the height what should
902:01 height to return the height what should I type here on line 14
902:07 logically what do I want to return to be clear yeah so I actually want to return
902:10 clear yeah so I actually want to return n and here's another curiosity of python
902:13 n and here's another curiosity of python Visa BC there doesn't seem to be an
902:16 Visa BC there doesn't seem to be an issue of scope anymore right in C it was
902:19 issue of scope anymore right in C it was super important to not only declare your
902:20 super important to not only declare your variables with the data types you also
902:22 variables with the data types you also had to be mindful of like where they
902:23 had to be mindful of like where they exist inside of those curly braces in
902:26 exist inside of those curly braces in Python it turns out you can be a little
902:28 Python it turns out you can be a little looser with things for better for worse
902:30 looser with things for better for worse and so on line 11 if I create a variable
902:33 and so on line 11 if I create a variable called n it exists on line 11 12 and
902:37 called n it exists on line 11 12 and even 13
902:39 even 13 outside of the while loop so to be clear
902:42 outside of the while loop so to be clear in C with a while loop we would have
902:45 in C with a while loop we would have ordinarily had not the colon we would
902:47 ordinarily had not the colon we would have had the curly brace like here and
902:49 have had the curly brace like here and over here and a week ago I would have
902:51 over here and a week ago I would have claimed that in C N does not exist
902:54 claimed that in C N does not exist outside of the while loop by nature of
902:56 outside of the while loop by nature of those curly braces even though the curly
902:57 those curly braces even though the curly braces are gone python actually allows
903:00 braces are gone python actually allows you to use a variable anytime after you
903:03 you to use a variable anytime after you have assigned it a value so slightly
903:06 have assigned it a value so slightly more powerful as such how however I can
903:09 more powerful as such how however I can tighten this up a little bit logically
903:11 tighten this up a little bit logically and this is true in C I don't really
903:13 and this is true in C I don't really need to break out of the loop by using
903:15 need to break out of the loop by using break recall that or know that I can
903:18 break recall that or know that I can actually once I'm ready to go I can just
903:21 actually once I'm ready to go I can just return the value I care about even
903:23 return the value I care about even inside of the loop and that will have
903:25 inside of the loop and that will have the side effect of breaking me out of
903:26 the side effect of breaking me out of the loop and also breaking me out of and
903:29 the loop and also breaking me out of and returning from the entire function so
903:31 returning from the entire function so nothing too new here in terms of C
903:33 nothing too new here in terms of C versus python except for this issue of
903:35 versus python except for this issue of scope and I indeed returned n at the
903:37 scope and I indeed returned n at the bottom there just to make clear that n
903:39 bottom there just to make clear that n would still exist so either of those are
903:42 would still exist so either of those are correct now I just have a Python program
903:45 correct now I just have a Python program that I think is going to allow me to
903:48 that I think is going to allow me to implement this same Mario idea so let's
903:50 implement this same Mario idea so let's run python of mario. and okay so nothing
903:53 run python of mario. and okay so nothing happened uh python of mario.
903:56 happened uh python of mario. py what did I do wrong yeah I have to
904:00 py what did I do wrong yeah I have to call Main so at the bottom of my code I
904:02 call Main so at the bottom of my code I have to call Main here and this is a
904:05 have to call Main here and this is a stylistic detail that's been subtle um
904:07 stylistic detail that's been subtle um generally speaking when when you are
904:08 generally speaking when when you are writing in Python um there's not a cs50
904:11 writing in Python um there's not a cs50 style guide per se there's actually a
904:13 style guide per se there's actually a python style guide that most people
904:16 python style guide that most people adhere to um it's and in this case
904:19 adhere to um it's and in this case double blank lines between functions is
904:21 double blank lines between functions is the norm I'm doing that deliberately
904:23 the norm I'm doing that deliberately although uh it might otherwise not be
904:25 although uh it might otherwise not be obvious but now that I've called main on
904:27 obvious but now that I've called main on line 16 let's run mario. once more aha
904:30 line 16 let's run mario. once more aha now we get there now we see it type in
904:32 now we get there now we see it type in three and I'm back in business printing
904:34 three and I'm back in business printing out the values there
904:37 out the values there yeah
904:42 sure why do I need the if condition at all why can't I just return n here as by
904:45 all why can't I just return n here as by doing return n or if I really want to be
904:48 doing return n or if I really want to be succinct I could technically just do
904:50 succinct I could technically just do this the only reason I added the if
904:53 this the only reason I added the if condition is because if the user types
904:55 condition is because if the user types in negative 1 -2 I wanted to prompt them
904:57 in negative 1 -2 I wanted to prompt them again and again that's all but that
904:59 again and again that's all but that would be totally acceptable too if you
905:00 would be totally acceptable too if you were okay with that result instead well
905:03 were okay with that result instead well let me do one other thing here to point
905:05 let me do one other thing here to point out why we are using get in so
905:08 out why we are using get in so frequently this new training wheel all
905:09 frequently this new training wheel all be it temporarily so let me go back to
905:11 be it temporarily so let me go back to the way it was a moment ago and let me
905:13 the way it was a moment ago and let me propose now to take away get int I
905:17 propose now to take away get int I claimed earlier that if you're not using
905:20 claimed earlier that if you're not using get int you can just use the input
905:22 get int you can just use the input function itself from
905:24 function itself from python but that always returns a string
905:27 python but that always returns a string or a stir and so recall that you have to
905:29 or a stir and so recall that you have to pass the output of the input function to
905:31 pass the output of the input function to an INT either on the same line or if you
905:34 an INT either on the same line or if you prefer on another line instead but it
905:36 prefer on another line instead but it turns out what I didn't do was show you
905:38 turns out what I didn't do was show you what happens if you uh don't cooperate
905:41 what happens if you uh don't cooperate with the user uh with the program so if
905:43 with the user uh with the program so if I run python of mario. now works great
905:47 I run python of mario. now works great even without the get int function and I
905:49 even without the get int function and I can do it with four still works great
905:51 can do it with four still works great but let me clear my terminal and be
905:53 but let me clear my terminal and be difficult now as the user and type in C
905:54 difficult now as the user and type in C for the height instead enter now we see
905:57 for the height instead enter now we see one of those tracebacks again this one
905:59 one of those tracebacks again this one is different this isn't a name error but
906:01 is different this isn't a name error but apparently a value error and if I kind
906:03 apparently a value error and if I kind of ignore the stuff I don't understand I
906:05 of ignore the stuff I don't understand I can see invalid literal for INT with
906:07 can see invalid literal for INT with base 10 cat that's a super cryptic way
906:10 base 10 cat that's a super cryptic way of saying that c a is not a number in
906:12 of saying that c a is not a number in decimal notation and so I would seem to
906:15 decimal notation and so I would seem to have to somehow handle this case and if
906:17 have to somehow handle this case and if you want to be more Curious you'll see
906:19 you want to be more Curious you'll see that this is indeed a trace back and um
906:22 that this is indeed a trace back and um C tends to do this too or the debugger
906:23 C tends to do this too or the debugger would do this for you too you can see
906:25 would do this for you too you can see all of the functions that have been
906:26 all of the functions that have been called to get you to this point so
906:28 called to get you to this point so apparently my problem is initially in
906:31 apparently my problem is initially in line 14 but line 14 if I keep scrolling
906:34 line 14 but line 14 if I keep scrolling is uninteresting it's main but line 14
906:37 is uninteresting it's main but line 14 leads me to execute line two which is
906:39 leads me to execute line two which is indeed in main that leads me to execute
906:41 indeed in main that leads me to execute line nine which is in get height and
906:44 line nine which is in get height and okay here's the issue so the closest
906:46 okay here's the issue so the closest line number to the error message is the
906:48 line number to the error message is the one that probably reveals the most line
906:50 one that probably reveals the most line nine is where my issue is so I can't
906:52 nine is where my issue is so I can't just blindly ask the user for input and
906:54 just blindly ask the user for input and then convert it to an INT if they're not
906:55 then convert it to an INT if they're not going to give me an INT now how do we
906:57 going to give me an INT now how do we deal with this well back in problem set
906:59 deal with this well back in problem set two you might recall validating that the
907:01 two you might recall validating that the user typed in a number and using a for
907:03 user typed in a number and using a for Loop and the like well it turns out
907:05 Loop and the like well it turns out there's a better way to do this in
907:06 there's a better way to do this in Python and the are kind of there if you
907:09 Python and the are kind of there if you want to try to convert something for a
907:11 want to try to convert something for a number to a number that might not
907:13 number to a number that might not actually be a number turns out Python
907:16 actually be a number turns out Python and certain other languages literally
907:17 and certain other languages literally have a keyword called try and if only
907:20 have a keyword called try and if only this existed for the past few weeks I
907:22 this existed for the past few weeks I know but like you can try to do the
907:23 know but like you can try to do the following with your code what do I want
907:25 following with your code what do I want to try to do well I want to try to
907:27 to try to do well I want to try to execute those few lines except if
907:30 execute those few lines except if there's an error so I can say except if
907:32 there's an error so I can say except if there's a value error specifically the
907:35 there's a value error specifically the one I screwed up and created a moment
907:37 one I screwed up and created a moment ago and if there is a value error I can
907:39 ago and if there is a value error I can print out an informative message to the
907:41 print out an informative message to the user like not an integer or anything
907:44 user like not an integer or anything else and what's happening here now is
907:47 else and what's happening here now is literally this operative word try the
907:49 literally this operative word try the pro python is going to try to get input
907:52 pro python is going to try to get input and try to convert it to an in and it's
907:54 and try to convert it to an in and it's going to try to check if it's greater
907:56 going to try to check if it's greater than zero and then try to return it all
907:59 than zero and then try to return it all why all of three of those lines are
908:01 why all of three of those lines are inside of indented underneath the tri
908:03 inside of indented underneath the tri block except if something goes wrong
908:05 block except if something goes wrong specifically a value error happens then
908:08 specifically a value error happens then it prints this but it doesn't return
908:10 it prints this but it doesn't return anything and because I'm in a loop that
908:12 anything and because I'm in a loop that means it's going to do it again and
908:14 means it's going to do it again and again and again until the human actually
908:16 again and again until the human actually cooperates and gives me an actual number
908:20 cooperates and gives me an actual number and so this too is what the world would
908:21 and so this too is what the world would call pythonic in Python you don't
908:24 call pythonic in Python you don't necessarily rigorously try to validate
908:26 necessarily rigorously try to validate the users's input make sure they haven't
908:27 the users's input make sure they haven't screwed up you honestly take a more
908:29 screwed up you honestly take a more laxidasical approach and just try to do
908:31 laxidasical approach and just try to do something but catch an error if it
908:34 something but catch an error if it happens so catch is also a term of art
908:36 happens so catch is also a term of art even though it's not a keyword here
908:38 even though it's not a keyword here except if something happens you handle
908:40 except if something happens you handle it so you try and you handle it it's
908:42 it so you try and you handle it it's sort of best effort programming if you
908:43 sort of best effort programming if you will but this is baked into the mindset
908:46 will but this is baked into the mindset of the Python uh programming community
908:48 of the Python uh programming community so now if I do python of mario. py and I
908:51 so now if I do python of mario. py and I cooperate works great as before try and
908:53 cooperate works great as before try and succeed three Works four works if though
908:56 succeed three Works four works if though I try and fail by typing in cat it
909:00 I try and fail by typing in cat it doesn't crash per se it doesn't show me
909:02 doesn't crash per se it doesn't show me an error it shows me something more user
909:04 an error it shows me something more user friendly like not an integer and then I
909:05 friendly like not an integer and then I can try again with dog not an integer I
909:07 can try again with dog not an integer I can try again with five and now it works
909:10 can try again with five and now it works so we won't generally have you write
909:12 so we won't generally have you write much in the way of these try except
909:14 much in the way of these try except blocks only because they get a little
909:16 blocks only because they get a little sophisticated quickly but that is to
909:18 sophisticated quickly but that is to reveal what the get int function is
909:20 reveal what the get int function is doing this is why we give you the
909:21 doing this is why we give you the training wheels so that when you want to
909:23 training wheels so that when you want to get an INT you don't have to jump
909:24 get an INT you don't have to jump through all these annoying Hoops to do
909:26 through all these annoying Hoops to do so but that's all the library is really
909:28 so but that's all the library is really doing for you is just try and accept you
909:31 doing for you is just try and accept you won't be left with any training wheels
909:32 won't be left with any training wheels ultimately questions now on getting
909:35 ultimately questions now on getting inputs and trying in this way
909:49 yeah Tri block it say that oh you could you put the condition outside of the tri
909:50 you put the condition outside of the tri block short answer yes and in fact I
909:52 block short answer yes and in fact I struggled with this last night when
909:53 struggled with this last night when tweaking this example to show the
909:55 tweaking this example to show the simplest version I will disclaim that
909:58 simplest version I will disclaim that really I should only be trying literally
910:01 really I should only be trying literally to do the The Fragile part and then down
910:04 to do the The Fragile part and then down here I should be really doing what
910:06 here I should be really doing what you're proposing which is do the
910:08 you're proposing which is do the condition out here the problem is though
910:10 condition out here the problem is though that logically this gets messy quickly
910:12 that logically this gets messy quickly right because except if there's a value
910:14 right because except if there's a value error I want to print out not an integer
910:16 error I want to print out not an integer I can't compare n against zero then
910:18 I can't compare n against zero then because n doesn't exist because there
910:19 because n doesn't exist because there was an error so it turns out and I'll
910:21 was an error so it turns out and I'll show you this this is now the advanced
910:23 show you this this is now the advanced version of python there's actually an
910:24 version of python there's actually an else keyword you can use in Python that
910:27 else keyword you can use in Python that does not accompany if or L if it
910:29 does not accompany if or L if it accompanies try and accept which I think
910:32 accompanies try and accept which I think is weirdly confusing a different word
910:34 is weirdly confusing a different word would have been better but if you really
910:36 would have been better but if you really prefer I could have done this instead
910:38 prefer I could have done this instead dead and this is one of these design
910:39 dead and this is one of these design things where like reasonable people will
910:40 things where like reasonable people will disagree generally speaking you should
910:42 disagree generally speaking you should only try to do the one line that might
910:44 only try to do the one line that might very well fail but honestly this looks
910:46 very well fail but honestly this looks kind of stupid now it's just
910:48 kind of stupid now it's just unnecessarily complicated and so my own
910:50 unnecessarily complicated and so my own preference was actually the original
910:52 preference was actually the original which was yeah I'm trying a few extra
910:54 which was yeah I'm trying a few extra lines that really aren't going to fail
910:56 lines that really aren't going to fail mathematically but it's just tighter
910:57 mathematically but it's just tighter it's cleaner this way and here's again
910:59 it's cleaner this way and here's again the sort of like you know arguments
911:01 the sort of like you know arguments you'll start to make yourself as you get
911:02 you'll start to make yourself as you get more comfortable with programming you'll
911:03 more comfortable with programming you'll have an opinion you'll disagree with
911:04 have an opinion you'll disagree with someone and so long as you can back your
911:06 someone and so long as you can back your argument up pretty
911:08 argument up pretty probably all right so how about we now
911:12 probably all right so how about we now take away some piece of magic that's
911:15 take away some piece of magic that's been here for a while let me go ahead
911:16 been here for a while let me go ahead and uh Delete all of this here and let
911:18 and uh Delete all of this here and let me propose that we revisit uh not that
911:21 me propose that we revisit uh not that vertical column and the exceptions that
911:23 vertical column and the exceptions that might result from getting input but
911:25 might result from getting input but these like horizontal question marks
911:26 these like horizontal question marks that we saw a while ago so I want all of
911:28 that we saw a while ago so I want all of those question marks on the same line
911:30 those question marks on the same line and yet I worry we're about to see a
911:32 and yet I worry we're about to see a challenge here because print up until
911:33 challenge here because print up until now has been putting new lines
911:35 now has been putting new lines everywhere automatically even without
911:37 everywhere automatically even without those backslash NS well let me propose
911:39 those backslash NS well let me propose that we do this for I in the range of
911:42 that we do this for I in the range of four if I want four question marks let
911:44 four if I want four question marks let me just print four question marks
911:47 me just print four question marks unfortunately I don't think this is
911:48 unfortunately I don't think this is correct yet let me run python of mario.
911:51 correct yet let me run python of mario. and of course this gives me a column
911:53 and of course this gives me a column instead of the row of question marks
911:56 instead of the row of question marks that I want so how do we do this well it
911:58 that I want so how do we do this well it turns out if you read the documentation
912:00 turns out if you read the documentation for the print function it turns out that
912:03 for the print function it turns out that print not surprisingly perhaps takes a
912:05 print not surprisingly perhaps takes a lot of different arguments as well and
912:06 lot of different arguments as well and in fact if you go to the documentation
912:08 in fact if you go to the documentation for it you'll see that it takes not just
912:11 for it you'll see that it takes not just positional arguments that is from left
912:13 positional arguments that is from left to right separated by commas turns out
912:15 to right separated by commas turns out python has supports a fancier feature
912:17 python has supports a fancier feature with arguments where you can pass the
912:19 with arguments where you can pass the names of arguments to functions too so
912:21 names of arguments to functions too so what do I mean by this if I go back to
912:24 what do I mean by this if I go back to vs code here and I've read the
912:27 vs code here and I've read the documentation it turns out that yes as
912:30 documentation it turns out that yes as before you can pass multiple arguments
912:33 before you can pass multiple arguments to python like this like hello comma
912:35 to python like this like hello comma David comma me that will just
912:37 David comma me that will just automatically concatenate all three of
912:39 automatically concatenate all three of those positional arguments together
912:41 those positional arguments together they're positional in the sense that
912:43 they're positional in the sense that they literally flow from left to right
912:44 they literally flow from left to right separated by commas but if you don't
912:46 separated by commas but if you don't want to just pass in values like that
912:48 want to just pass in values like that you want to actually print out as I did
912:50 you want to actually print out as I did before a question mark but you want to
912:52 before a question mark but you want to override the default behavior of print
912:55 override the default behavior of print by changing the line ending you can
912:58 by changing the line ending you can actually do this you can use the name of
913:00 actually do this you can use the name of an argument that you know exists from
913:02 an argument that you know exists from the documentation set it equal to some
913:05 the documentation set it equal to some alternative value and in fact even
913:08 alternative value and in fact even though this looks cryptic this is how I
913:10 though this looks cryptic this is how I would override the end of each line to
913:13 would override the end of each line to be quote unquote that is nothing because
913:16 be quote unquote that is nothing because if you read the documentation the
913:18 if you read the documentation the default value for this end argument does
913:21 default value for this end argument does someone want to guess is is back sln so
913:24 someone want to guess is is back sln so if you read the documentation you'll see
913:26 if you read the documentation you'll see that back sln is the implied default for
913:29 that back sln is the implied default for this end argument and so if you want to
913:31 this end argument and so if you want to change it you just say end equals
913:33 change it you just say end equals something else and so here I can change
913:37 something else and so here I can change change it to nothing and now rerun
913:40 change it to nothing and now rerun python of mario. and now they're all on
913:42 python of mario. and now they're all on the same line now looks a little stupid
913:44 the same line now looks a little stupid cuz I made that sort of week one mistake
913:46 cuz I made that sort of week one mistake where I still need to move the cursor to
913:47 where I still need to move the cursor to the next line that's just a different
913:49 the next line that's just a different problem I'm just going to go over here
913:51 problem I'm just going to go over here and print nothing I don't even need to
913:52 and print nothing I don't even need to print back sln because if print
913:54 print back sln because if print automatically gives you a backslash n
913:57 automatically gives you a backslash n just call print with nothing and you'll
913:58 just call print with nothing and you'll get that for free so let me rerun python
914:01 get that for free so let me rerun python of mario. Pi and now it looks a little
914:03 of mario. Pi and now it looks a little prettier at the prompt and to be super
914:05 prettier at the prompt and to be super clear as to what's going on suppose I
914:06 clear as to what's going on suppose I want to sort of make an exclamation here
914:08 want to sort of make an exclamation here I could change the back sln default to
914:11 I could change the back sln default to like an exclamation point Just for kicks
914:13 like an exclamation point Just for kicks and if I run python of mario. py again
914:15 and if I run python of mario. py again now I get this sort of you know
914:17 now I get this sort of you know exclamation with question marks and
914:19 exclamation with question marks and exclamation points as well so that's all
914:22 exclamation points as well so that's all that's going on here and this is what's
914:23 that's going on here and this is what's called a named argument it literally has
914:25 called a named argument it literally has a name that you can specify when calling
914:28 a name that you can specify when calling it in and it's different from positional
914:29 it in and it's different from positional in that you're literally using the
914:31 in that you're literally using the name let me propose something else
914:33 name let me propose something else though and this is why people kind of
914:35 though and this is why people kind of like python there's just kind of cool
914:36 like python there's just kind of cool ways to do things that's kind of a you
914:39 ways to do things that's kind of a you know ver it's a three line verbose way
914:42 know ver it's a three line verbose way of printing out four question marks you
914:45 of printing out four question marks you know I could certainly take the you know
914:47 know I could certainly take the you know shortcut and just do this but that's not
914:49 shortcut and just do this but that's not really that interesting for anyone
914:50 really that interesting for anyone especially if I want to do it a variable
914:52 especially if I want to do it a variable number of times but python does let you
914:54 number of times but python does let you do this if you want to uh multiply a
914:57 do this if you want to uh multiply a character some number of times not only
915:00 character some number of times not only can you use Plus for concatenation you
915:02 can you use Plus for concatenation you can use star or an asterisk for
915:06 can use star or an asterisk for multiplication if you will that is
915:08 multiplication if you will that is concatenation again and again and again
915:10 concatenation again and again and again so if I just print out quote unquote
915:12 so if I just print out quote unquote question mark Time 4 that's actually
915:15 question mark Time 4 that's actually going to be the tightest way the most
915:17 going to be the tightest way the most distinct way I can print four question
915:20 distinct way I can print four question marks instead and if I don't use four I
915:21 marks instead and if I don't use four I use n where I get n from the user bang
915:24 use n where I get n from the user bang like now I've gotten rid of the four
915:25 like now I've gotten rid of the four Loop entirely and I'm using the the star
915:29 Loop entirely and I'm using the the star operator to manipulate it instead and to
915:32 operator to manipulate it instead and to be super clear here in so far as python
915:34 be super clear here in so far as python does not have milock or free or memory
915:38 does not have milock or free or memory management that you have to do guess
915:39 management that you have to do guess what python also doesn't
915:45 have anything on your minds the past couple of
915:46 couple of weeks doesn't have pointers yes so
915:50 weeks doesn't have pointers yes so python does not have pointers which just
915:53 python does not have pointers which just means that all of that happens for you
915:54 means that all of that happens for you automatically underneath the hood Again
915:56 automatically underneath the hood Again by way of code that someone else wrote
915:58 by way of code that someone else wrote how about one more throwback with Mario
916:00 how about one more throwback with Mario we've talked about in week one this sort
916:02 we've talked about in week one this sort of two-dimensional structure where it's
916:05 of two-dimensional structure where it's like I claim like 3x3 a grid of bricks
916:08 like I claim like 3x3 a grid of bricks if you will well how can we do this in
916:10 if you will well how can we do this in Python we can do this in a couple of
916:11 Python we can do this in a couple of ways now let me go back to my mario. py
916:15 ways now let me go back to my mario. py and let me do something like for I in
916:17 and let me do something like for I in range of we'll just do three even though
916:19 range of we'll just do three even though I know now I could use get int or I
916:21 I know now I could use get int or I could use input and int and if I want to
916:23 could use input and int and if I want to do something two-dimensionally just like
916:25 do something two-dimensionally just like in C you can Nest your for Loop so maybe
916:27 in C you can Nest your for Loop so maybe I could do 4J in range of three and then
916:31 I could do 4J in range of three and then in here I could print out a uh hash
916:34 in here I could print out a uh hash symbol and then let's see if that gives
916:37 symbol and then let's see if that gives me nine total so if I've got a nested
916:39 me nine total so if I've got a nested Loop like this python of mario.
916:41 Loop like this python of mario. hopefully gives me a grid no it gave me
916:44 hopefully gives me a grid no it gave me a column of
916:45 a column of nine why
916:48 nine why logically even though I've got my row
916:51 logically even though I've got my row and my
916:53 and my columns
916:55 columns yeah yeah the line ending so in my row I
916:59 yeah yeah the line ending so in my row I can't let print just keep adding new
917:01 can't let print just keep adding new line adding new lines so I just have to
917:02 line adding new lines so I just have to override this here and let me not screw
917:04 override this here and let me not screw up like before let me print one at the
917:06 up like before let me print one at the end of the whole row just to move the
917:08 end of the whole row just to move the cursor down and I think now together now
917:10 cursor down and I think now together now we've got our 3x3 of course we could
917:13 we've got our 3x3 of course we could tighten this up further like if I don't
917:15 tighten this up further like if I don't like the nested loop I probably could go
917:17 like the nested loop I probably could go in here and just print out for instance
917:20 in here and just print out for instance a uh a brick times three or I could
917:23 a uh a brick times three or I could change the three to a variable if I've
917:24 change the three to a variable if I've gotten it from the user so I can tighten
917:26 gotten it from the user so I can tighten this up further so again just different
917:28 this up further so again just different ways to solve the same problem and again
917:29 ways to solve the same problem and again sort of evidence of why a lot of people
917:31 sort of evidence of why a lot of people like python there's just some more
917:33 like python there's just some more pleasant ways to solve problems without
917:34 pleasant ways to solve problems without getting into the weeds constantly of
917:36 getting into the weeds constantly of doing things like like with um uh for
917:40 doing things like like with um uh for loops and wild Loops endlessly all right
917:42 loops and wild Loops endlessly all right well how about some other building
917:43 well how about some other building blocks lists are going to be so
917:44 blocks lists are going to be so incredibly useful in Python just as
917:47 incredibly useful in Python just as arrays were in C but arrays are annoying
917:49 arrays were in C but arrays are annoying because you have to manage the memory
917:50 because you have to manage the memory yourself you have to know in advance how
917:51 yourself you have to know in advance how big they are or you have to use pointers
917:53 big they are or you have to use pointers and malok or realloc to resize them like
917:56 and malok or realloc to resize them like oh my God like the past two weeks have
917:57 oh my God like the past two weeks have been painful in that sense but python
917:59 been painful in that sense but python does this all for free for you in fact
918:02 does this all for free for you in fact there's a whole bunch of functions that
918:04 there's a whole bunch of functions that come with python that involve lists and
918:06 come with python that involve lists and they'll ow us ultimately um to do things
918:11 they'll ow us ultimately um to do things again and again and again uh with uh
918:14 again and again and again uh with uh within the same data structure and for
918:16 within the same data structure and for instance we'll be able to get the length
918:17 instance we'll be able to get the length of a list you don't have to remember it
918:19 of a list you don't have to remember it yourself in a variable you can just ask
918:21 yourself in a variable you can just ask python how many elements are in this
918:23 python how many elements are in this list and with this I think we can solve
918:25 list and with this I think we can solve some some old problems too so let me go
918:28 some some old problems too so let me go back here to vs code let me close Mario
918:32 back here to vs code let me close Mario and give us a new program called scores.
918:34 and give us a new program called scores. piy and rather than show the C and the
918:37 piy and rather than show the C and the python now let's just focus on Python
918:39 python now let's just focus on Python and in scores. C way back when we just
918:42 and in scores. C way back when we just averaged like three test scores or
918:43 averaged like three test scores or something like that 72 73 and 33 a few
918:46 something like that 72 73 and 33 a few weeks ago so if I want to create a list
918:48 weeks ago so if I want to create a list in this python version of 72 73 33 I
918:52 in this python version of 72 73 33 I just use my square bracket notation C
918:54 just use my square bracket notation C let you use curly braces if you know the
918:56 let you use curly braces if you know the values in advance but Python's just this
918:58 values in advance but Python's just this and now if I want to compute the average
919:01 and now if I want to compute the average in pi in C recall I did something with a
919:03 in pi in C recall I did something with a loop I added all the values together I
919:05 loop I added all the values together I then divided by the total number values
919:07 then divided by the total number values just like you would in grade school and
919:09 just like you would in grade school and that gave me the average well python
919:11 that gave me the average well python comes with a lot of super handy
919:13 comes with a lot of super handy functions not just length but others as
919:15 functions not just length but others as well and so in fact if you want to
919:17 well and so in fact if you want to compute the average you can take the sum
919:20 compute the average you can take the sum of all of those scores and divide it by
919:22 of all of those scores and divide it by the length of all of those scores so
919:24 the length of all of those scores so python comes with length comes with sum
919:27 python comes with length comes with sum you can just pass in a whole list of any
919:29 you can just pass in a whole list of any size and let it deal with that problem
919:31 size and let it deal with that problem for you so if I want to now print out
919:33 for you so if I want to now print out this average I can print out average
919:35 this average I can print out average colon and then I'll plug in my average
919:38 colon and then I'll plug in my average uh string for variable for interpolation
919:40 uh string for variable for interpolation let me make this an F string so that it
919:42 let me make this an F string so that it gets formatted and let me just run
919:44 gets formatted and let me just run python of scores. Pi and there's my
919:46 python of scores. Pi and there's my average it's sort of rounding weird
919:48 average it's sort of rounding weird because we're still vulnerable to some
919:49 because we're still vulnerable to some floating point in Precision but at least
919:52 floating point in Precision but at least I didn't need loops and I didn't have to
919:54 I didn't need loops and I didn't have to write all this darn code just to do
919:56 write all this darn code just to do something that you know Excel and Google
919:58 something that you know Excel and Google spreadsheets can just do like that well
920:00 spreadsheets can just do like that well python is closer to those kinds of tools
920:02 python is closer to those kinds of tools but more powerful and that you can
920:04 but more powerful and that you can manipulate the data yourself how about
920:06 manipulate the data yourself how about though if I want to um get a bunch of
920:08 though if I want to um get a bunch of scores manually from the user and then
920:10 scores manually from the user and then sum them together well let's combine a
920:12 sum them together well let's combine a few ideas here how about this first let
920:15 few ideas here how about this first let me go ahead and uh import um the cs50 LI
920:19 me go ahead and uh import um the cs50 LI get in function from the cs50 library
920:20 get in function from the cs50 library just so we don't have to deal with try
920:22 just so we don't have to deal with try and accept or all of that and let me go
920:24 and accept or all of that and let me go ahead and give myself an empty list and
920:27 ahead and give myself an empty list and this is powerful in py in C there's
920:30 this is powerful in py in C there's really there's no point to an empty
920:32 really there's no point to an empty array because if you create an empty
920:33 array because if you create an empty array with square bracket notation like
920:35 array with square bracket notation like it's not useful for anything but in
920:37 it's not useful for anything but in Python you can create it empty because
920:39 Python you can create it empty because python will grow and shrink the list for
920:42 python will grow and shrink the list for you automatically as you add things to
920:44 you automatically as you add things to it so if I want to get three scores from
920:45 it so if I want to get three scores from the user I could do something like this
920:47 the user I could do something like this for I in range of three and then I can
920:50 for I in range of three and then I can grab a variable called score or anything
920:53 grab a variable called score or anything I could call get int prompt the human
920:55 I could call get int prompt the human for the score that they want to type in
920:57 for the score that they want to type in and then once they do I can do this
920:59 and then once they do I can do this thinking back to our objectoriented
921:02 thinking back to our objectoriented programming capability now I could do
921:04 programming capability now I could do scores dot a pen
921:07 scores dot a pen and I Canen that score to it and you
921:09 and I Canen that score to it and you would only know this from having read
921:10 would only know this from having read the documentation heard it in class in a
921:12 the documentation heard it in class in a book or whatnot but it turns out that
921:14 book or whatnot but it turns out that just like strings have functions like
921:16 just like strings have functions like lower built into them lists have
921:19 lower built into them lists have functions like append built into them
921:21 functions like append built into them that just literally appends to the end
921:23 that just literally appends to the end of the list for you and python will grow
921:25 of the list for you and python will grow or Shrink it as needed no more Malo or C
921:28 or Shrink it as needed no more Malo or C or Realo or the like so this just
921:30 or Realo or the like so this just appends to the scores array the scores
921:32 appends to the scores array the scores list that score and then again and again
921:35 list that score and then again and again and again so the array starts at
921:37 and again so the array starts at sorry the list starts at size zero then
921:39 sorry the list starts at size zero then grows to one then two then three without
921:42 grows to one then two then three without you having to do anything else and so
921:44 you having to do anything else and so now down here I can compute an average
921:47 now down here I can compute an average with the sum of those scores divided by
921:50 with the sum of those scores divided by the length of the total number of scores
921:52 the length of the total number of scores and to be clear length is the total
921:54 and to be clear length is the total number of elements in the list doesn't
921:56 number of elements in the list doesn't matter how big the values themselves are
921:58 matter how big the values themselves are now I can go ahead and print out an FST
922:01 now I can go ahead and print out an FST string uh with something like average
922:04 string uh with something like average colon average and curly braces and and
922:07 colon average and curly braces and and if I run python of scores. piy I'll type
922:09 if I run python of scores. piy I'll type in just for the sake of discussion the
922:11 in just for the sake of discussion the three values I still get the same answer
922:14 three values I still get the same answer but that would have been painful to do
922:15 but that would have been painful to do in see unless you committ it in advance
922:18 in see unless you committ it in advance to a fixed size array which we already
922:20 to a fixed size array which we already decided weeks ago was annoying or uh you
922:24 decided weeks ago was annoying or uh you uh grew it dynamically using malok or
922:27 uh grew it dynamically using malok or realloc or the like all right what else
922:30 realloc or the like all right what else can I do well there's some nice things
922:32 can I do well there's some nice things you might as well know exist um instead
922:34 you might as well know exist um instead of scores. aen you can do slight
922:37 of scores. aen you can do slight fanciness like this like if you want to
922:39 fanciness like this like if you want to append something to a list you can
922:42 append something to a list you can actually do plus equals and then put
922:44 actually do plus equals and then put that thing in a a temporary list of its
922:47 that thing in a a temporary list of its own and just use what is essentially
922:49 own and just use what is essentially concatenation but not concatenation of
922:51 concatenation but not concatenation of strings but concatination of lists so
922:54 strings but concatination of lists so this new line six appends to the scores
922:57 this new line six appends to the scores list this tiny little list I'm
922:59 list this tiny little list I'm temporarily creating with just the
923:00 temporarily creating with just the current new score so just another piece
923:03 current new score so just another piece of syntax that's worth seeing that
923:05 of syntax that's worth seeing that allows you to do something like that as
923:07 allows you to do something like that as well all right well how about we go back
923:09 well all right well how about we go back to strings for a moment and all these
923:11 to strings for a moment and all these examples as always are on the course's
923:12 examples as always are on the course's website afterward suppose we want to do
923:14 website afterward suppose we want to do something like converting characters to
923:17 something like converting characters to uppercase well to be clear I could do
923:19 uppercase well to be clear I could do something like this let me create a
923:20 something like this let me create a program called uppercase dop let me
923:23 program called uppercase dop let me prompt the user for a before string as
923:25 prompt the user for a before string as by using the input function or get
923:27 by using the input function or get string which is almost the same and I'll
923:29 string which is almost the same and I'll prompt the user for a string
923:31 prompt the user for a string beforeand then let me go ahead and print
923:33 beforeand then let me go ahead and print out uh how about the keyword after
923:37 out uh how about the keyword after and then end the new line with nothing
923:40 and then end the new line with nothing just so that I can see before on one
923:42 just so that I can see before on one line and after on the next line and then
923:44 line and after on the next line and then let me do this and here's where python
923:46 let me do this and here's where python gets pleasant too with loops for C in
923:51 gets pleasant too with loops for C in before print c. uper n equals quote
923:55 before print c. uper n equals quote unquote and then I'll print this here
923:57 unquote and then I'll print this here all right that was fast but let's try to
923:59 all right that was fast but let's try to infer what's going on so line one just
924:01 infer what's going on so line one just gets input from the user stores it in a
924:02 gets input from the user stores it in a variable called before line two
924:04 variable called before line two literally just prints after but doesn't
924:07 literally just prints after but doesn't move the new line to uh the cursor to
924:08 move the new line to uh the cursor to the next line what it then does is this
924:11 the next line what it then does is this and in C this was a little more annoying
924:14 and in C this was a little more annoying you needed a for loop with I you needed
924:16 you needed a for loop with I you needed array in uh notation with the square
924:18 array in uh notation with the square brackets but python if you say four
924:22 brackets but python if you say four variable in string so for c for
924:25 variable in string so for c for character in string Python's going to
924:27 character in string Python's going to automatically assign C to the first word
924:30 automatically assign C to the first word letter that the user types in then on
924:31 letter that the user types in then on the next iteration the second letter the
924:33 the next iteration the second letter the third letter and the fourth so you don't
924:34 third letter and the fourth so you don't need any square bracket notation you
924:36 need any square bracket notation you just you see and python will do it for
924:38 just you see and python will do it for you and just hand you back one at a time
924:40 you and just hand you back one at a time each of the letters that the user has
924:42 each of the letters that the user has typed in so if I go back over here and I
924:45 typed in so if I go back over here and I run for instance python of uppercase
924:48 run for instance python of uppercase dopy and I'll type in how about uh David
924:52 dopy and I'll type in how about uh David in all lowercase and hit enter you'll
924:54 in all lowercase and hit enter you'll now see that it's all uppercase instead
924:57 now see that it's all uppercase instead by iterating over it indeed one
924:58 by iterating over it indeed one character at a time but we already know
925:00 character at a time but we already know thanks to object-oriented programming
925:02 thanks to object-oriented programming strings themselves have the
925:04 strings themselves have the functionality built in to not just
925:05 functionality built in to not just uppercase Single Character
925:07 uppercase Single Character but the whole string so honestly this
925:09 but the whole string so honestly this was a bit of a silly exercise I don't
925:11 was a bit of a silly exercise I don't need to use a loop anymore like in see
925:15 need to use a loop anymore like in see and so some of the habits you've only
925:16 and so some of the habits you've only just developed in recent weeks it's time
925:18 just developed in recent weeks it's time to start breaking them when they're not
925:19 to start breaking them when they're not necessary I can create a variable called
925:22 necessary I can create a variable called after set it equal to before do uper
925:25 after set it equal to before do uper which indeed exists just like lower
925:27 which indeed exists just like lower exists and then what I can go ahead and
925:29 exists and then what I can go ahead and print out is for instance uh let's get
925:32 print out is for instance uh let's get rid of this print line here and do it at
925:34 rid of this print line here and do it at the end after and print the value of
925:37 the end after and print the value of that variable so now if I rerun
925:40 that variable so now if I rerun uppercase Pi type in David and all
925:42 uppercase Pi type in David and all lowercase I can just uppercase the whole
925:44 lowercase I can just uppercase the whole thing all at once because again in C in
925:47 thing all at once because again in C in Python you don't have to operate on
925:49 Python you don't have to operate on characters individually questions on any
925:53 characters individually questions on any of these tricks up until
925:57 of these tricks up until now now all right how about a few other
925:59 now now all right how about a few other techniques that we saw and C that we'll
926:01 techniques that we saw and C that we'll bring back now in Python so it turns out
926:03 bring back now in Python so it turns out in Python there are other librar you can
926:06 in Python there are other librar you can use two that unlock even more
926:08 use two that unlock even more functionality so in C if you wanted
926:11 functionality so in C if you wanted command line arguments you just change
926:12 command line arguments you just change the proo the signature for main to be
926:15 the proo the signature for main to be void instead of void to be int argc
926:18 void instead of void to be int argc comma string argv Open brackets for an
926:21 comma string argv Open brackets for an array or Char star eventually well it
926:24 array or Char star eventually well it turns out in Python that if you want to
926:25 turns out in Python that if you want to access command line arguments it's a
926:27 access command line arguments it's a little simpler but they're tucked away
926:28 little simpler but they're tucked away in a library otherwise known as a module
926:31 in a library otherwise known as a module called CIS the CIS or system module now
926:34 called CIS the CIS or system module now this is similar in spirit to the cs50
926:36 this is similar in spirit to the cs50 library and that it's got a bunch of
926:37 library and that it's got a bunch of functionality built in but this one
926:38 functionality built in but this one comes with python itself so if I want to
926:41 comes with python itself so if I want to create a program like greet py in VSS
926:44 create a program like greet py in VSS code here let me go ahead and do this
926:46 code here let me go ahead and do this from the CIS Library let's import argv
926:50 from the CIS Library let's import argv and that's just a thing that exists it's
926:52 and that's just a thing that exists it's not built into main because there is no
926:54 not built into main because there is no main per se anymore so it's tucked away
926:56 main per se anymore so it's tucked away in that library and now I can do
926:57 in that library and now I can do something like this if the length of
926:59 something like this if the length of argv equals equals 2 well let's go ahead
927:02 argv equals equals 2 well let's go ahead and print out something friendly like
927:04 and print out something friendly like hello comma AR V bracket 1 and then
927:08 hello comma AR V bracket 1 and then close quotes else if the length of RV is
927:11 close quotes else if the length of RV is not equal to two let's just go ahead and
927:13 not equal to two let's just go ahead and print out hello world now at a glance
927:15 print out hello world now at a glance this might look a little cryptic but
927:17 this might look a little cryptic but it's identical to what we did a few
927:18 it's identical to what we did a few weeks ago when I run this python of
927:22 weeks ago when I run this python of greet with no arguments it just says
927:24 greet with no arguments it just says hello world but if I instead add a
927:28 hello world but if I instead add a command line argument like my first name
927:30 command line argument like my first name and hit enter now the length of arv is
927:33 and hit enter now the length of arv is no longer one it's going to be two and
927:36 no longer one it's going to be two and so it prints out hello David instead so
927:39 so it prints out hello David instead so the takeaway here is that whereas in C
927:42 the takeaway here is that whereas in C argv technically contained the name of
927:44 argv technically contained the name of your program like hello or dog greet and
927:48 your program like hello or dog greet and then everything the human typed Python's
927:50 then everything the human typed Python's a little different in that because we're
927:52 a little different in that because we're using The Interpreter in this way
927:54 using The Interpreter in this way Technically when you run python of greet
927:57 Technically when you run python of greet py the length of arv is only one it
928:01 py the length of arv is only one it contains only greet so the name of the
928:03 contains only greet so the name of the file it does not unnecessarily contain
928:05 file it does not unnecessarily contain python itself because what's the point
928:07 python itself because what's the point of that being there omnes it does
928:09 of that being there omnes it does contain the number of words that the
928:10 contain the number of words that the human typed after python itself so argv
928:14 human typed after python itself so argv is length one here argv is length two
928:16 is length one here argv is length two here and that's why when it did equal to
928:18 here and that's why when it did equal to I saw hello David instead of the default
928:20 I saw hello David instead of the default hello world so same ability to access
928:23 hello world so same ability to access command line arguments add these kinds
928:25 command line arguments add these kinds of inputs to your functions but you have
928:27 of inputs to your functions but you have to unlock it by way of using Arvy uh
928:30 to unlock it by way of using Arvy uh instead in this way if you want to see
928:33 instead in this way if you want to see all of the words you could do something
928:34 all of the words you could do something like this uh just as if we combine ideas
928:38 like this uh just as if we combine ideas here for I in range of how about length
928:42 here for I in range of how about length of
928:43 of arv then I can do this print argv
928:46 arv then I can do this print argv bracket I all right a little cryptic but
928:48 bracket I all right a little cryptic but line three is just a for Loop iterating
928:51 line three is just a for Loop iterating over the range of length of argv so if
928:53 over the range of length of argv so if the human types in two words the length
928:55 the human types in two words the length of argv will be two so this is just a
928:57 of argv will be two so this is just a way of saying iterate over all of the
928:59 way of saying iterate over all of the words in arv printing them one at a time
929:02 words in arv printing them one at a time so python of greet dopy enter just
929:05 so python of greet dopy enter just prints out the name of the program
929:07 prints out the name of the program python of greet dopy with David prints
929:10 python of greet dopy with David prints out greet dopy and then David I can keep
929:12 out greet dopy and then David I can keep running it though with more words and
929:14 running it though with more words and they'll each get printed one at a time
929:17 they'll each get printed one at a time but what's nice too about Python and
929:20 but what's nice too about Python and this is the point of this exercise
929:21 this is the point of this exercise honestly this looks pretty cryptic this
929:23 honestly this looks pretty cryptic this is not very pleasant to look at if you
929:25 is not very pleasant to look at if you just want to iterate over every word in
929:28 just want to iterate over every word in a list which argv is watch what I can do
929:32 a list which argv is watch what I can do I can do for ARG or any variable name in
929:35 I can do for ARG or any variable name in ARG V let me just now print out that
929:38 ARG V let me just now print out that argument I could keep calling it I but I
929:40 argument I could keep calling it I but I seems weird when it's not a number so
929:41 seems weird when it's not a number so I'm changing to AR as a word instead if
929:44 I'm changing to AR as a word instead if I now do python of greet py it does this
929:48 I now do python of greet py it does this if I do python of greet David it does
929:50 if I do python of greet David it does that again David ma it does that again
929:53 that again David ma it does that again so this is again why Python's just very
929:55 so this is again why Python's just very appealing you want to do something this
929:56 appealing you want to do something this many times iterate over a list just say
929:58 many times iterate over a list just say it and it reads a little more like
929:59 it and it reads a little more like English and there's even other fanciness
930:01 English and there's even other fanciness too if I may it's a little stupid that I
930:03 too if I may it's a little stupid that I keep seeing the name of the program
930:05 keep seeing the name of the program greet dopy so I it'd be nice if I could
930:07 greet dopy so I it'd be nice if I could remove that python also supports what
930:10 remove that python also supports what are called slices of arrays sorry slices
930:14 are called slices of arrays sorry slices of lists even though I get the
930:15 of lists even though I get the terminology confused if Arvy is a list
930:19 terminology confused if Arvy is a list then it's going to print out everything
930:20 then it's going to print out everything in it but if I want a slice of it that
930:22 in it but if I want a slice of it that starts at location one all the way to
930:25 starts at location one all the way to the end you can use this funky syntax in
930:28 the end you can use this funky syntax in between the square brackets which we've
930:30 between the square brackets which we've not seen yet that's going to start at
930:32 not seen yet that's going to start at item one and go all the way to the end
930:35 item one and go all the way to the end and so this is a nice clever way of
930:37 and so this is a nice clever way of slicing off if you will the very first
930:40 slicing off if you will the very first element because now when I run greet doy
930:42 element because now when I run greet doy David men I should only see David and
930:45 David men I should only see David and men if I only want one element I could
930:48 men if I only want one element I could do one to two if I want all of them I
930:50 do one to two if I want all of them I could do zero onward I could give myself
930:53 could do zero onward I could give myself just two of one of them in this way so
930:55 just two of one of them in this way so you can play with the start value and
930:57 you can play with the start value and the end value in this way to sort of
930:59 the end value in this way to sort of slice and dice these lists in different
931:01 slice and dice these lists in different ways that would have been a pain in see
931:04 ways that would have been a pain in see just because we didn't really have the
931:06 just because we didn't really have the built-in support for manipulating arrays
931:09 built-in support for manipulating arrays as cleanly as this all right just so
931:12 as cleanly as this all right just so you've seen it too though this one is
931:14 you've seen it too though this one is less uh exciting to see live if I go
931:16 less uh exciting to see live if I go ahead and create a quick program here it
931:18 ahead and create a quick program here it turns out there's something else in the
931:19 turns out there's something else in the CIS Library the ability to exit programs
931:22 CIS Library the ability to exit programs either exiting with status code one or
931:24 either exiting with status code one or zero as we've been doing anytime
931:25 zero as we've been doing anytime something goes right or wrong so for
931:28 something goes right or wrong so for instance Let Me Whip up a quick program
931:29 instance Let Me Whip up a quick program that just says if the length of cy.
931:31 that just says if the length of cy. argv uh does not equal two then let's y
931:36 argv uh does not equal two then let's y at the user and say you're missing a
931:37 at the user and say you're missing a command line argument otherwise command
931:40 command line argument otherwise command line argument and let's then return cy.
931:44 line argument and let's then return cy. exit one else let's go ahead and
931:47 exit one else let's go ahead and logically just say print a formatted
931:49 logically just say print a formatted string that says hello as before cy.
931:53 string that says hello as before cy. arv1 now things look different all of a
931:55 arv1 now things look different all of a sudden but I'm doing something
931:57 sudden but I'm doing something deliberately first let's see what this
931:58 deliberately first let's see what this does so on line one I'm importing not
932:01 does so on line one I'm importing not argv specifically I'm importing the
932:04 argv specifically I'm importing the whole CIS library and we'll see why in
932:06 whole CIS library and we'll see why in second well it turns out that this Arvy
932:08 second well it turns out that this Arvy the CIS library has not only the Arvy
932:11 the CIS library has not only the Arvy list it also has a function called exit
932:13 list it also has a function called exit which I'd like to be able to use as well
932:15 which I'd like to be able to use as well so it turns out that if you import a
932:17 so it turns out that if you import a whole library in this way that's fine
932:20 whole library in this way that's fine but you have to refer to the things
932:21 but you have to refer to the things inside of it by using that same
932:23 inside of it by using that same library's name and a DOT to sort of
932:25 library's name and a DOT to sort of namespace it so to speak so here I'm
932:28 namespace it so to speak so here I'm just saying if the user types in does
932:30 just saying if the user types in does not type in two words yell at them with
932:32 not type in two words yell at them with missing command line argument and then
932:33 missing command line argument and then exit with one just like in C when you do
932:35 exit with one just like in C when you do exit one just means something went wrong
932:38 exit one just means something went wrong otherwise print out hello to this and
932:39 otherwise print out hello to this and this is starting to look cryptic but
932:41 this is starting to look cryptic but it's just a combination of ideas the
932:42 it's just a combination of ideas the curly braces means interpolate this
932:45 curly braces means interpolate this value plug it in here cy. Arvy is just
932:48 value plug it in here cy. Arvy is just the verbose way of saying go into the
932:49 the verbose way of saying go into the CIS library and get the argv variable
932:52 CIS library and get the argv variable therein and bracket one of course just
932:55 therein and bracket one of course just like arrays and C is just the second
932:57 like arrays and C is just the second element at the prompt so when I run this
933:01 element at the prompt so when I run this version now python of exit. py with no
933:03 version now python of exit. py with no arguments I get yelled at in this way if
933:06 arguments I get yelled at in this way if however I type in two arguments total
933:09 however I type in two arguments total the name of the file and my own name now
933:11 the name of the file and my own name now I get greeted with hello David and it's
933:14 I get greeted with hello David and it's the same idea before this was a very
933:15 the same idea before this was a very low-level technique but same thing here
933:17 low-level technique but same thing here if you do Echo dollar sign question mark
933:20 if you do Echo dollar sign question mark enter you'll see the exit code of your
933:22 enter you'll see the exit code of your program so if I do this incorrectly
933:25 program so if I do this incorrectly again let me rerun it without my name
933:28 again let me rerun it without my name enter I get yelled at but if I do Echo
933:30 enter I get yelled at but if I do Echo dollar sign question mark there's the
933:32 dollar sign question mark there's the secret one that's returned again just to
933:35 secret one that's returned again just to show You parody with C in this case
933:38 show You parody with C in this case questions now on any of these techniques
933:45 here all right how about something that's a little more powerful too we
933:47 that's a little more powerful too we spent so much time in week zero and one
933:49 spent so much time in week zero and one doing searching and then eventually
933:51 doing searching and then eventually sorting in week three well it turns out
933:53 sorting in week three well it turns out python can help with some of this too
933:54 python can help with some of this too let me go ahead and create a program
933:55 let me go ahead and create a program called names. py that's just going to be
933:57 called names. py that's just going to be an opportunity to maybe search over a
934:00 an opportunity to maybe search over a whole bunch of names let me go ahead and
934:02 whole bunch of names let me go ahead and import CIS and then just so I have
934:04 import CIS and then just so I have access to exit and let me go ahead and
934:06 access to exit and let me go ahead and create a variable called names that's
934:08 create a variable called names that's going to be a list with a whole bunch of
934:10 going to be a list with a whole bunch of names uh how about Here Charlie and Fred
934:14 names uh how about Here Charlie and Fred and George and Jenny and Percy and
934:18 and George and Jenny and Percy and lastly Ron so a whole bunch of names
934:20 lastly Ron so a whole bunch of names here and you know it'd be a little
934:22 here and you know it'd be a little Annoying to implement code that iterates
934:23 Annoying to implement code that iterates over that from left to right and see
934:25 over that from left to right and see searching for one of those names in fact
934:28 searching for one of those names in fact what name well let's go ahead and ask
934:29 what name well let's go ahead and ask the user to input the name that they
934:31 the user to input the name that they want to search for so that we can tell
934:32 want to search for so that we can tell them if the name is there or not and we
934:35 them if the name is there or not and we could do this similar to C in Python
934:37 could do this similar to C in Python doing something like this so for n in
934:40 doing something like this so for n in names where n is just a variable to
934:43 names where n is just a variable to iterate over each name if how about the
934:46 iterate over each name if how about the name I'm looking for equals the current
934:49 name I'm looking for equals the current name in the list AKA n let's print out
934:52 name in the list AKA n let's print out something friendly like found and then
934:54 something friendly like found and then let's do cy. Exit 0 to indicate that we
934:56 let's do cy. Exit 0 to indicate that we found whoever that is otherwise if we
934:59 found whoever that is otherwise if we get all the way to the bottom here
935:01 get all the way to the bottom here outside of this Loop let's just print
935:02 outside of this Loop let's just print not found because if we haven't exited
935:04 not found because if we haven't exited yet and then let's just exit with one
935:07 yet and then let's just exit with one just to be clear I can continue
935:09 just to be clear I can continue importing all of CIS or I could do from
935:13 importing all of CIS or I could do from CIS import exit and then I could get rid
935:15 CIS import exit and then I could get rid of CIS dot everywhere else but you know
935:18 of CIS dot everywhere else but you know sometimes it's helpful to know exactly
935:20 sometimes it's helpful to know exactly where functions came from so this two is
935:22 where functions came from so this two is just a matter of style in this case all
935:24 just a matter of style in this case all right so let's go ahead and run this
935:26 right so let's go ahead and run this python of names. piy and let's look for
935:29 python of names. piy and let's look for like Ron all the way at the end you know
935:31 like Ron all the way at the end you know all right he's found and let's search
935:33 all right he's found and let's search for someone outside of the family here
935:35 for someone outside of the family here like herion not found okay so it seems
935:38 like herion not found okay so it seems to be working in this way but I've
935:39 to be working in this way but I've essentially implemented what
935:42 essentially implemented what algorithm what algorithm would this seem
935:44 algorithm what algorithm would this seem to be per lines seven and eight and nine
935:49 to be per lines seven and eight and nine and
935:50 and 10 yeah so it's just linear search it's
935:52 10 yeah so it's just linear search it's a loop even though the syntax is a
935:53 a loop even though the syntax is a little more succinct today and it's just
935:55 little more succinct today and it's just iterating over the whole thing well
935:56 iterating over the whole thing well honestly we've seen an even more tur way
935:59 honestly we've seen an even more tur way to do this in Python and this again is
936:00 to do this in Python and this again is what makes it a more pleasant language
936:03 what makes it a more pleasant language sometimes why don't I just do this
936:05 sometimes why don't I just do this instead in of iterating one at a time
936:07 instead in of iterating one at a time why don't I just say this let me go
936:09 why don't I just say this let me go ahead and change my condition to just be
936:12 ahead and change my condition to just be How about if the name we're looking for
936:14 How about if the name we're looking for is in the names list we're done we found
936:18 is in the names list we're done we found it use the in preposition that we've
936:20 it use the in preposition that we've seen a couple of times now that itself
936:22 seen a couple of times now that itself asks the question is something in
936:24 asks the question is something in something else and python will take care
936:27 something else and python will take care of linear search for us and it's going
936:28 of linear search for us and it's going to work exactly the same if I do python
936:31 to work exactly the same if I do python of names. search for run it's still
936:33 of names. search for run it's still going to find him and it's still going
936:34 going to find him and it's still going to do it linearly in this case but I
936:36 to do it linearly in this case but I don't have to write all of the lower
936:38 don't have to write all of the lower level code myself in this
936:42 level code myself in this case questions now on any of this the
936:47 case questions now on any of this the code's just getting shorter and
936:49 code's just getting shorter and shorter now what about uh let's see what
936:52 shorter now what about uh let's see what else might we have here how about this
936:54 else might we have here how about this it turns out let's go ahead and
936:56 it turns out let's go ahead and Implement that phone book that we
936:57 Implement that phone book that we started metaphorically with in the
936:59 started metaphorically with in the beginning of the course let's code up a
937:01 beginning of the course let's code up a program called phonebook dopy and in
937:03 program called phonebook dopy and in this case let's go ahead and let's
937:05 this case let's go ahead and let's create a dictionary this time recall
937:07 create a dictionary this time recall that a dictionary is a little something
937:09 that a dictionary is a little something that implements something like this like
937:11 that implements something like this like a two column table that's got keys and
937:14 a two column table that's got keys and values words and definitions names and
937:17 values words and definitions names and numbers and let's focus on the last of
937:18 numbers and let's focus on the last of those names and numbers in this case
937:21 those names and numbers in this case well I claimed earlier that python has
937:22 well I claimed earlier that python has built-in support for dictionaries dict
937:24 built-in support for dictionaries dict objects that you can create with one
937:26 objects that you can create with one line I didn't need it for speller
937:28 line I didn't need it for speller because a set is sufficient when you
937:29 because a set is sufficient when you only want one of the keys or the values
937:31 only want one of the keys or the values not both but now I want some names and
937:33 not both but now I want some names and numbers so it turns out in pyth python
937:35 numbers so it turns out in pyth python you can create an empty dictionary by
937:37 you can create an empty dictionary by saying dict open parenthesis close and
937:40 saying dict open parenthesis close and that just gives you essentially a chart
937:42 that just gives you essentially a chart that looks like this with nothing in it
937:44 that looks like this with nothing in it or there's more succinct syntax you can
937:46 or there's more succinct syntax you can alternatively do uh this with two curly
937:50 alternatively do uh this with two curly braces instead and in fact I've been
937:52 braces instead and in fact I've been using a shortcut all this time when I
937:54 using a shortcut all this time when I had a list earlier where my
937:58 had a list earlier where my variable uh was called scores and I did
938:01 variable uh was called scores and I did this that was actually the shorthand
938:03 this that was actually the shorthand version of this hey python give me an
938:05 version of this hey python give me an empty list so there's different Syntax
938:07 empty list so there's different Syntax for achieving the same goal in this case
938:09 for achieving the same goal in this case if I want a dictionary for people I can
938:12 if I want a dictionary for people I can either do this or more commonly just two
938:15 either do this or more commonly just two curly braces like that all right well
938:17 curly braces like that all right well what do I want to put in this well let
938:19 what do I want to put in this well let me actually put some things in this and
938:20 me actually put some things in this and I'm going to just move my Clos curly
938:22 I'm going to just move my Clos curly brace to a new line if I want to
938:24 brace to a new line if I want to implement this idea of keys and values
938:27 implement this idea of keys and values the way you do this in Python is key
938:29 the way you do this in Python is key colon value comma key colon value so
938:33 colon value comma key colon value so you'd implement it more in code so for
938:35 you'd implement it more in code so for instance if I want Carter to be the
938:36 instance if I want Carter to be the first key in my phone book and I want
938:39 first key in my phone book and I want his number to be+ one 617495 1000 I can
938:43 his number to be+ one 617495 1000 I can put that as the corresponding value the
938:45 put that as the corresponding value the colon is in between both are strings or
938:48 colon is in between both are strings or stirs so I've quoted both deliberately
938:50 stirs so I've quoted both deliberately if I want to add myself I can put a
938:52 if I want to add myself I can put a comma and then just to keep things
938:53 comma and then just to keep things pretty I'm moving the cursor to the next
938:55 pretty I'm moving the cursor to the next line but that's not strictly required
938:56 line but that's not strictly required aesthetically it's just good style and
938:58 aesthetically it's just good style and here I might do+ 1 949 468 uh
939:03 here I might do+ 1 949 468 uh 2750 and now I have a dictionary that
939:06 2750 and now I have a dictionary that essentially has two rows here David uh
939:08 essentially has two rows here David uh Carter and his number and David and his
939:11 Carter and his number and David and his number as well and if I kept adding to
939:12 number as well and if I kept adding to this this call this chart would just get
939:14 this this call this chart would just get longer and longer suppose I want to
939:15 longer and longer suppose I want to search for one of our numbers well let's
939:17 search for one of our numbers well let's prompt the user for the name for whose
939:19 prompt the user for the name for whose number you want to search by getting
939:21 number you want to search by getting string or you know what we don't need
939:23 string or you know what we don't need the cs50 library let's just use input
939:25 the cs50 library let's just use input and prompt the user for a name and now
939:28 and prompt the user for a name and now we can use this super syntax and just
939:30 we can use this super syntax and just say if name in people print the format
939:35 say if name in people print the format added string number colon and here we
939:38 added string number colon and here we can do this people bracket name okay so
939:42 can do this people bracket name okay so this is getting kind of cool kind of
939:44 this is getting kind of cool kind of quickly kind of confusingly so let me
939:46 quickly kind of confusingly so let me run this python of phone
939:49 run this python of phone book. let's type in Carter and indeed I
939:52 book. let's type in Carter and indeed I see his number let's run it again with
939:54 see his number let's run it again with David and I see my number here so what's
939:58 David and I see my number here so what's going on well it turns out that a
940:00 going on well it turns out that a dictionary is very similar in spirit to
940:03 dictionary is very similar in spirit to a list it's actually very similar in
940:04 a list it's actually very similar in spirit to an array in C but instead of
940:07 spirit to an array in C but instead of being limited to keys that are numbers
940:10 being limited to keys that are numbers like bracket 0o bracket 1 bracket two
940:13 like bracket 0o bracket 1 bracket two you can actually use words and that's
940:15 you can actually use words and that's all I'm doing here on line eight if I
940:18 all I'm doing here on line eight if I want to check for the name Carter which
940:20 want to check for the name Carter which is currently in this variable called
940:22 is currently in this variable called name I can index into my people
940:25 name I can index into my people dictionary using not a number but using
940:28 dictionary using not a number but using literally a string the name Carter or
940:30 literally a string the name Carter or David or anything else to make this
940:33 David or anything else to make this clear too notice that I'm at the moment
940:35 clear too notice that I'm at the moment you using this format string which is
940:36 you using this format string which is adding some undue complexity but I could
940:39 adding some undue complexity but I could clarify this perhaps further as this I
940:41 clarify this perhaps further as this I could give myself another variable
940:42 could give myself another variable called number set it equal to the people
940:45 called number set it equal to the people dictionary indexing into it using the
940:47 dictionary indexing into it using the current name and now I can shorten this
940:49 current name and now I can shorten this to make it clear that all I'm doing is
940:51 to make it clear that all I'm doing is printing the value of that and in fact I
940:55 printing the value of that and in fact I can do this even more cryptically if I
940:57 can do this even more cryptically if I this would be weird to do but if I only
940:59 this would be weird to do but if I only ever want to show David's phone number
941:01 ever want to show David's phone number and never Carter's I can literally quote
941:03 and never Carter's I can literally quote unquote index into the people dictionary
941:06 unquote index into the people dictionary because now when I run this even if I
941:08 because now when I run this even if I type Carter I'm going to get back my
941:10 type Carter I'm going to get back my number instead but that's all that's
941:12 number instead but that's all that's happening if I undo that because that's
941:14 happening if I undo that because that's now a bug but I index into it using the
941:18 now a bug but I index into it using the value of name dictionaries are just so
941:21 value of name dictionaries are just so wonderfully convenient because now you
941:22 wonderfully convenient because now you can associate anything with anything
941:24 can associate anything with anything else but not using numbers but entire
941:26 else but not using numbers but entire keywords instead so here's how if in
941:29 keywords instead so here's how if in spell we gave you not just words but
941:31 spell we gave you not just words but hundreds of thousands of definitions as
941:34 hundreds of thousands of definitions as well you could essentially store them as
941:36 well you could essentially store them as this and then when the human wants to
941:38 this and then when the human wants to look up a definition in a proper
941:39 look up a definition in a proper dictionary not just for spellchecking
941:41 dictionary not just for spellchecking you could index into the dictionary
941:43 you could index into the dictionary using square brackets and get back the
941:46 using square brackets and get back the definition in English as well
941:49 definition in English as well questions on this
942:05 yeah location a really good question so how
942:08 location a really good question so how to summarize how is python finding that
942:10 to summarize how is python finding that name within that dictionary this is
942:12 name within that dictionary this is where honestly speller in pet 5 is what
942:15 where honestly speller in pet 5 is what Python's all about so you have struggled
942:17 Python's all about so you have struggled are struggling with implementing your
942:18 are struggling with implementing your own spell checker and implementing your
942:19 own spell checker and implementing your own hash table and recall that per last
942:21 own hash table and recall that per last week the goal of a hash table is to
942:23 week the goal of a hash table is to ideally get constant time access not
942:26 ideally get constant time access not something linear which is slow and even
942:28 something linear which is slow and even better than something uh uh logarithmic
942:30 better than something uh uh logarithmic like log base 2 of n so Python and the
942:33 like log base 2 of n so Python and the really smart people who invented it they
942:35 really smart people who invented it they have written the code that does its best
942:37 have written the code that does its best to give you constant time searches of
942:40 to give you constant time searches of dictionaries and they're not always
942:41 dictionaries and they're not always going to succeed just as you and your
942:42 going to succeed just as you and your own problem set probably going to have
942:44 own problem set probably going to have some collisions once in a while and
942:45 some collisions once in a while and start to have chains of length lists of
942:47 start to have chains of length lists of words but this is where again you defer
942:49 words but this is where again you defer to someone else someone smarter than you
942:51 to someone else someone smarter than you someone with more time than you to solve
942:52 someone with more time than you to solve these problems for you and if you read
942:54 these problems for you and if you read Python's documentation you'll see that
942:56 Python's documentation you'll see that it doesn't guarantee constant time but
942:58 it doesn't guarantee constant time but it's going to ideally optimize the data
943:00 it's going to ideally optimize the data structure for you to get as fast as
943:03 structure for you to get as fast as possible and of all of the data
943:05 possible and of all of the data structures um like a dictionary a
943:07 structures um like a dictionary a hashtable is really like the Swiss army
943:09 hashtable is really like the Swiss army knife of computing because it just lets
943:10 knife of computing because it just lets you associate something with something
943:12 you associate something with something else and even though we keep focusing on
943:14 else and even though we keep focusing on names and numbers that's a really
943:15 names and numbers that's a really powerful thing because it's more
943:17 powerful thing because it's more powerful than lists and arrays which are
943:18 powerful than lists and arrays which are only numbers and something else now you
943:20 only numbers and something else now you can have any sorts of relationships
943:22 can have any sorts of relationships instead all right let me show a few
943:24 instead all right let me show a few other examples before we culminate with
943:26 other examples before we culminate with some more powerful techniques in pythons
943:28 some more powerful techniques in pythons thanks to libraries how about this
943:30 thanks to libraries how about this problem we encountered in week four
943:32 problem we encountered in week four which was this let me code up a program
943:35 which was this let me code up a program called again compare. Pi here but this
943:38 called again compare. Pi here but this time compare to Strings and not numbers
943:41 time compare to Strings and not numbers so let me for instance do uh get one
943:43 so let me for instance do uh get one string from the user called s just for
943:46 string from the user called s just for the sake of discussion let me get
943:47 the sake of discussion let me get another string from the user uh called T
943:50 another string from the user uh called T so that we can actually do some
943:51 so that we can actually do some comparison here and if s equals equals T
943:54 comparison here and if s equals equals T let's go ahead and print out that
943:56 let's go ahead and print out that they're the same else let's go ahead and
943:58 they're the same else let's go ahead and print out that they're different so this
944:00 print out that they're different so this is very similar to what we did in week
944:01 is very similar to what we did in week four but in week four recall we did this
944:04 four but in week four recall we did this spef specifically because we had
944:06 spef specifically because we had encountered a problem for instance if I
944:10 encountered a problem for instance if I run whoops
944:12 run whoops uh if I
944:21 run what's going on uh input T come
944:28 on oh the okay wow okay long day all right if I run the proper command python
944:30 right if I run the proper command python of compare. py then let's go ahead and
944:33 of compare. py then let's go ahead and type in something like cat in all
944:36 type in something like cat in all lowercase cats in all lowercase and
944:38 lowercase cats in all lowercase and they're the same uh if though I do this
944:41 they're the same uh if though I do this again with dog and dog they're the same
944:44 again with dog and dog they're the same and of course cat and dog they're
944:45 and of course cat and dog they're different but does anyone recall from
944:47 different but does anyone recall from two weeks ago when I typed in my name
944:50 two weeks ago when I typed in my name twice both identically capitalized what
944:53 twice both identically capitalized what did it say that they were in
944:56 did it say that they were in fact different and why was that like why
944:58 fact different and why was that like why were two strings in C different even
945:00 were two strings in C different even though I typed literally the same
945:03 though I typed literally the same thing two different places in memory so
945:06 thing two different places in memory so each string might look the same
945:07 each string might look the same aesthetically but of course was stored
945:09 aesthetically but of course was stored elsewhere in memory and yet python
945:11 elsewhere in memory and yet python appears to be using the equality
945:13 appears to be using the equality operator equals equals like you and I
945:16 operator equals equals like you and I would expect as humans actually
945:18 would expect as humans actually comparing for us Char by Char in each of
945:21 comparing for us Char by Char in each of those strings for actual equality so
945:23 those strings for actual equality so this is a feature of python in that it's
945:25 this is a feature of python in that it's just easier to do and why well this
945:27 just easier to do and why well this derives from the reality that in Python
945:28 derives from the reality that in Python there are no pointers anymore there's no
945:30 there are no pointers anymore there's no underlying memory management it's not up
945:32 underlying memory management it's not up to you now to worry about those lower
945:34 to you now to worry about those lower level details the language itself takes
945:36 level details the language itself takes care of that for you and so similarly if
945:38 care of that for you and so similarly if I do this and don't ask the user for two
945:40 I do this and don't ask the user for two strings but just one and then I do
945:42 strings but just one and then I do something like this how about give
945:44 something like this how about give myself a second variable T set it equal
945:47 myself a second variable T set it equal to s.
945:48 to s. capitalize which note is not the same as
945:51 capitalize which note is not the same as upper capitalize by Design per Python's
945:53 upper capitalize by Design per Python's documentation will only capitalize the
945:55 documentation will only capitalize the first letter for you I can now print out
945:58 first letter for you I can now print out say two F strings here what the value of
946:00 say two F strings here what the value of s is and then let me print out with
946:02 s is and then let me print out with another F string what the value of t is
946:05 another F string what the value of t is and recall that in C this was a problem
946:07 and recall that in C this was a problem because if you capitalize s and store it
946:10 because if you capitalize s and store it in t we accidentally capitalized both s
946:13 in t we accidentally capitalized both s and t but in this case in Python when I
946:16 and t but in this case in Python when I actually run this and type in cat in all
946:18 actually run this and type in cat in all lowercase the original s is unchanged
946:22 lowercase the original s is unchanged because when I use capitalize on line
946:25 because when I use capitalize on line three this is indeed capitalizing s but
946:27 three this is indeed capitalizing s but it's returning a copy of the result it
946:30 it's returning a copy of the result it cannot change S itself because again for
946:33 cannot change S itself because again for that technical term s is IM mutable
946:35 that technical term s is IM mutable strings once they exist cannot be
946:37 strings once they exist cannot be changed themselves but you can return
946:39 changed themselves but you can return copies and modified mutated copies of
946:42 copies and modified mutated copies of those same strings so in short all of
946:44 those same strings so in short all of those headaches we encountered in week
946:46 those headaches we encountered in week four are now solved really in the way
946:48 four are now solved really in the way you might expect and here's another one
946:50 you might expect and here's another one that we dwell on in week four with the
946:52 that we dwell on in week four with the colored uh liquid in glasses let me code
946:54 colored uh liquid in glasses let me code up a program called swap. pi and in
946:57 up a program called swap. pi and in swap. pi let me set x equal to 1 y equal
947:00 swap. pi let me set x equal to 1 y equal to 2 and then let me just print out an F
947:02 to 2 and then let me just print out an F string here so how about X X is this
947:06 string here so how about X X is this comma Y is that and then let me do that
947:10 comma Y is that and then let me do that twice just for the sake of demonstration
947:12 twice just for the sake of demonstration and in here recall that we had to create
947:14 and in here recall that we had to create a swap function but then we had to pass
947:16 a swap function but then we had to pass it in by reference with the Amper sand
947:18 it in by reference with the Amper sand and like oh my God like that was kind of
947:21 and like oh my God like that was kind of peak complexity and see well if you want
947:23 peak complexity and see well if you want to swap X and Y in Python you could do X
947:26 to swap X and Y in Python you could do X comma yal y comma X and now python of
947:29 comma yal y comma X and now python of swap whoops python of
947:32 swap whoops python of swap. and there we go all of that's
947:35 swap. and there we go all of that's handled for you sort of like a shell
947:37 handled for you sort of like a shell game without even a temporary variable
947:39 game without even a temporary variable in mind so what more can we do here how
947:43 in mind so what more can we do here how about a few final building blocks and
947:45 about a few final building blocks and these related now to files from that
947:47 these related now to files from that week four suppose that I want to save
947:49 week four suppose that I want to save some names and numbers in like a a CSV
947:51 some names and numbers in like a a CSV file comma separated values which is
947:53 file comma separated values which is like a very lightweight spreadsheet well
947:55 like a very lightweight spreadsheet well first let me create a uh phonebook CSV
947:59 first let me create a uh phonebook CSV file that just has name comma number as
948:02 file that just has name comma number as like the first row there but after after
948:04 like the first row there but after after that I'm going to go ahead now and code
948:06 that I'm going to go ahead now and code up a phonebook dopy program that
948:09 up a phonebook dopy program that actually allows me to add things to this
948:10 actually allows me to add things to this phone book so let me split my screen
948:12 phone book so let me split my screen here so that we can see the old and the
948:14 here so that we can see the old and the new and down here in my code for phone
948:17 new and down here in my code for phone book. in this new and improved version
948:20 book. in this new and improved version I'm going to actually import a whole
948:22 I'm going to actually import a whole other Library this one called CSV and
948:24 other Library this one called CSV and here too especially for people in data
948:26 here too especially for people in data science and the like really like being
948:28 science and the like really like being in a manipulate files and data that
948:30 in a manipulate files and data that might very well be stored in
948:31 might very well be stored in spreadsheets or csvs comma separated
948:33 spreadsheets or csvs comma separated values which we we saw briefly in week
948:35 values which we we saw briefly in week four in phonebook then it suffices to
948:38 four in phonebook then it suffices to just import CSV after reading the
948:40 just import CSV after reading the documentation therefore because this is
948:42 documentation therefore because this is going to give me functionality in code
948:45 going to give me functionality in code related to CSV files so here's how I
948:47 related to CSV files so here's how I might open a file in Python I literally
948:50 might open a file in Python I literally call open it's not fop now it's just
948:52 call open it's not fop now it's just open and I open this file called
948:54 open and I open this file called phonebook.com
948:55 phonebook.com going to open it in a pend mode not
948:58 going to open it in a pend mode not right where it would change the whole
949:00 right where it would change the whole thing I want to append new line at a
949:02 thing I want to append new line at a time uh after this I want to get maybe
949:05 time uh after this I want to get maybe how about a name from the user so let's
949:07 how about a name from the user so let's prompt the user for some input for their
949:09 prompt the user for some input for their name and then let's prompt the user for
949:11 name and then let's prompt the user for a number as well using input prompting
949:14 a number as well using input prompting for number all right and now this is a
949:16 for number all right and now this is a little cryptic and you'd only know this
949:18 little cryptic and you'd only know this from the documentation but if you want
949:19 from the documentation but if you want to write line rows to a CSV file that
949:23 to write line rows to a CSV file that you can then view in Excel or the like
949:25 you can then view in Excel or the like you can do this give me a variable
949:27 you can do this give me a variable called writer but I could call it
949:28 called writer but I could call it anything I want let me use a CSV do
949:31 anything I want let me use a CSV do writer function that comes with this CSV
949:34 writer function that comes with this CSV Library passing in the file this is like
949:37 Library passing in the file this is like saying hey python treat this open file
949:39 saying hey python treat this open file as a CSV file so that things are
949:41 as a CSV file so that things are separated with commas and nicely
949:43 separated with commas and nicely formatted in rows and columns now I'm
949:45 formatted in rows and columns now I'm going to do this use that writer to
949:48 going to do this use that writer to write a row well what do I want to write
949:50 write a row well what do I want to write I want to write a short list namely the
949:52 I want to write a short list namely the current name and the current number to
949:55 current name and the current number to that file but I don't want to use FR
949:56 that file but I don't want to use FR printf and percent s and all of that
949:59 printf and percent s and all of that stuff that we might have had in the past
950:01 stuff that we might have had in the past and now I just want to close the file
950:03 and now I just want to close the file let me reopen my ter teral let me run
950:05 let me reopen my ter teral let me run python of phone book. and let me type in
950:08 python of phone book. and let me type in how about David and then +1
950:11 how about David and then +1 949468 2750 and hold crossing my fingers
950:15 949468 2750 and hold crossing my fingers watching the actual CSV at top left my
950:18 watching the actual CSV at top left my code has just added me to the file and
950:20 code has just added me to the file and if I were to run it again for instance
950:22 if I were to run it again for instance with Carter in plus1 617495 1000
950:24 with Carter in plus1 617495 1000 crossing my fingers again we've updated
950:27 crossing my fingers again we've updated the file and it turns out there's code
950:29 the file and it turns out there's code now via which I can even read that file
950:30 now via which I can even read that file but I can first tighten this up just so
950:32 but I can first tighten this up just so you've seen it it turns out into python
950:34 you've seen it it turns out into python it's so common to open files and close
950:36 it's so common to open files and close them you know humans make mistakes and
950:38 them you know humans make mistakes and they often forget to close files which
950:40 they often forget to close files which might then end up using more memory than
950:42 might then end up using more memory than you intend so you can alternatively do
950:44 you intend so you can alternatively do this in Python so that you don't have to
950:45 this in Python so that you don't have to worry about closing files you can use
950:48 worry about closing files you can use this keyword instead you can say with
950:52 this keyword instead you can say with the opening of this file as a variable
950:54 the opening of this file as a variable called file do all of the following
950:57 called file do all of the following underneath so I'm indenting most of my
950:59 underneath so I'm indenting most of my code I'm using this new python specific
951:01 code I'm using this new python specific keyword called wi and this is just a
951:03 keyword called wi and this is just a matter of saying with the following
951:05 matter of saying with the following opening of the file do those next four
951:07 opening of the file do those next four lines of code and then automatically
951:09 lines of code and then automatically close it for me at the end of the
951:11 close it for me at the end of the indentation it's a minor optimization
951:13 indentation it's a minor optimization but this again is sort of the pythonic
951:15 but this again is sort of the pythonic way to do things instead how else might
951:18 way to do things instead how else might I do this too well it turns out that the
951:21 I do this too well it turns out that the code I've written here on line n
951:23 code I've written here on line n especially is a little fragile right if
951:25 especially is a little fragile right if any human opens the spreadsheet the CSV
951:27 any human opens the spreadsheet the CSV file in Excel Google spreadsheets Apple
951:30 file in Excel Google spreadsheets Apple numbers and maybe like moves the columns
951:32 numbers and maybe like moves the columns around just because maybe they're
951:33 around just because maybe they're fussing they SA it and they don't
951:34 fussing they SA it and they don't realize they've now changed my
951:36 realize they've now changed my assumptions I don't want to necessarily
951:38 assumptions I don't want to necessarily write name and number always in that
951:40 write name and number always in that order CU what if someone screws up and
951:42 order CU what if someone screws up and flips those two columns by literally
951:44 flips those two columns by literally dragging and dropping so it turns out
951:46 dragging and dropping so it turns out that instead of using a list here we can
951:48 that instead of using a list here we can use another feature of this Library as
951:50 use another feature of this Library as follows instead of using a writer
951:53 follows instead of using a writer there's something called a dictionary
951:55 there's something called a dictionary writer or dict writer that takes the
951:57 writer or dict writer that takes the same argument as input the file that's
951:59 same argument as input the file that's opened but now the one difference here
952:02 opened but now the one difference here is that you need to tell this dictionary
952:03 is that you need to tell this dictionary writer
952:04 writer that your field names are name and
952:09 that your field names are name and number and let me close the CSV here
952:11 number and let me close the CSV here name and number are the names of the
952:13 name and number are the names of the fields The Columns in this CSV file and
952:16 fields The Columns in this CSV file and when it comes time to write a new row
952:18 when it comes time to write a new row the syntax here is going to be a little
952:20 the syntax here is going to be a little uglier but it's just a dictionary the
952:22 uglier but it's just a dictionary the name I want to write to the dictionary
952:24 name I want to write to the dictionary is going to be whatever name the human
952:25 is going to be whatever name the human typed in the number that I want to write
952:28 typed in the number that I want to write to the the CSV file is going to be
952:30 to the the CSV file is going to be whatever the number the human typed in
952:33 whatever the number the human typed in but what's different next about this
952:34 but what's different next about this code is by simply using a dictionary
952:37 code is by simply using a dictionary writer here instead of the generic
952:39 writer here instead of the generic writer now the columns can be in this
952:42 writer now the columns can be in this order or this order or any order and the
952:46 order or this order or any order and the dictionary writer is going to figure out
952:47 dictionary writer is going to figure out based on the first line of text in that
952:49 based on the first line of text in that CSV where to put name where to put
952:51 CSV where to put name where to put number so if you flip them no big deal
952:53 number so if you flip them no big deal it's going to notice oh wait the columns
952:55 it's going to notice oh wait the columns changed and it's going to insert the
952:57 changed and it's going to insert the columns correctly so just again another
952:59 columns correctly so just again another more powerful feature that lets you
953:01 more powerful feature that lets you focus
953:02 focus on lets you focus on real work as
953:04 on lets you focus on real work as opposed to actually uh getting tied up
953:07 opposed to actually uh getting tied up in the weeds of writing code like this
953:10 in the weeds of writing code like this otherwise questions on this one as well
953:15 otherwise questions on this one as well but what we will do now is come full
953:17 but what we will do now is come full circle to some of the more uh
953:19 circle to some of the more uh sophisticated examples with which we
953:21 sophisticated examples with which we began and I'm going to go back over to
953:23 began and I'm going to go back over to my own Mac laptop here where I've got my
953:26 my own Mac laptop here where I've got my own terminal window up and running and I
953:28 own terminal window up and running and I was just going to introduce a couple of
953:30 was just going to introduce a couple of final libraries that really speak to
953:31 final libraries that really speak to just how powerful python can can be and
953:34 just how powerful python can can be and how quickly you can get up and running
953:36 how quickly you can get up and running to be fair can't necessarily do all of
953:38 to be fair can't necessarily do all of these things in the cloud like in code
953:39 these things in the cloud like in code spaces because you need access to your
953:41 spaces because you need access to your own speakers or microphone or the like
953:43 own speakers or microphone or the like so that's why I'm doing it on my own Mac
953:45 so that's why I'm doing it on my own Mac here but let me go ahead and open up a
953:48 here but let me go ahead and open up a program called
953:49 program called speech. and I'm not using VSS code here
953:51 speech. and I'm not using VSS code here I'm using a program called VI that's
953:53 I'm using a program called VI that's entirely terminal window based but it's
953:55 entirely terminal window based but it's going to allow me for instance to import
953:57 going to allow me for instance to import the python text to speech version three
953:59 the python text to speech version three Library I'm going to give myself a
954:01 Library I'm going to give myself a variable called engine that's going to
954:03 variable called engine that's going to be set equal to the python text to
954:05 be set equal to the python text to speech 3 libraries init method which is
954:09 speech 3 libraries init method which is just going to initialize this library
954:10 just going to initialize this library that relates to text to speech I'm going
954:12 that relates to text to speech I'm going to then use the engines say function to
954:16 to then use the engines say function to say something like how about hello comma
954:19 say something like how about hello comma world and then as my last line I'm going
954:21 world and then as my last line I'm going to say engine. runand wait capitalized
954:24 to say engine. runand wait capitalized as such to tell my program now to run
954:27 as such to tell my program now to run that speech and wait until it's done all
954:29 that speech and wait until it's done all right I'm going to save this file I'm
954:31 right I'm going to save this file I'm going to run python of speech. py and
954:34 going to run python of speech. py and I'm going to cross my fingers as always
954:35 I'm going to cross my fingers as always and hello world all right so now I have
954:39 and hello world all right so now I have a program that's actually synthesizing
954:41 a program that's actually synthesizing speech using a library like this how can
954:43 speech using a library like this how can I now modify this to be a little more
954:45 I now modify this to be a little more interesting well how about this let me
954:47 interesting well how about this let me go ahead and prompt the user for their
954:49 go ahead and prompt the user for their name like we've done several times here
954:51 name like we've done several times here using Python's built-in name function
954:53 using Python's built-in name function and now let me go ahead and use a format
954:55 and now let me go ahead and use a format string in conjunction with this Library
954:57 string in conjunction with this Library interpolating the value of name there
954:59 interpolating the value of name there and at least if my name is somewhat
955:01 and at least if my name is somewhat phonetically pronounceable let's go
955:03 phonetically pronounceable let's go ahead and run python ofp
955:05 ahead and run python ofp speech. type in my name and hello David
955:09 speech. type in my name and hello David okay it's a sort of weird choice of
955:11 okay it's a sort of weird choice of inflection but we're starting to
955:12 inflection but we're starting to synthesize voice not unlike Siri or
955:14 synthesize voice not unlike Siri or Google assistant or Alexa or the like
955:16 Google assistant or Alexa or the like now we can maybe do something a little
955:18 now we can maybe do something a little more advanced too in addition to
955:21 more advanced too in addition to synthesizing speech in this way we could
955:24 synthesizing speech in this way we could synthesize for instance uh an actual
955:27 synthesize for instance uh an actual graphic let me go ahead now and do
955:29 graphic let me go ahead now and do something like this let me create a
955:30 something like this let me create a program called
955:32 program called qr. I'm going to go ahead and import a
955:35 qr. I'm going to go ahead and import a library called OS which gives you access
955:37 library called OS which gives you access to operating system related
955:38 to operating system related functionality in Python I'm going to
955:40 functionality in Python I'm going to import a library I've pre-installed
955:41 import a library I've pre-installed called QR code which is a
955:43 called QR code which is a two-dimensional barcode that you might
955:44 two-dimensional barcode that you might have seen in the real world I'm going to
955:46 have seen in the real world I'm going to go ahead and create an image variable
955:48 go ahead and create an image variable using this QR code library's make
955:50 using this QR code library's make function which per its documentation
955:52 function which per its documentation takes a URL like one of cs50's own
955:54 takes a URL like one of cs50's own videos so we'll do this with YouTube uh
955:58 videos so we'll do this with YouTube uh /xv f
956:04 zj5 p g g0 so hopefully that's the right
956:08 p g g0 so hopefully that's the right lecture and now we've got image. saave
956:11 lecture and now we've got image. saave which is going to allow me to create a
956:12 which is going to allow me to create a file called qr. ping think back now on
956:15 file called qr. ping think back now on problem set 4 and how painful it was to
956:17 problem set 4 and how painful it was to save files we'll just use the save
956:18 save files we'll just use the save function now in Python and save this as
956:21 function now in Python and save this as a ping file portable Network graphic and
956:23 a ping file portable Network graphic and then lastly let's just go ahead and open
956:25 then lastly let's just go ahead and open uh the with the command open qr. ping on
956:28 uh the with the command open qr. ping on my Mac so that hopefully this just
956:29 my Mac so that hopefully this just automatically opens all right I'm going
956:31 automatically opens all right I'm going to go ahead and just double check my
956:33 to go ahead and just double check my syntax here so that I haven't made any
956:35 syntax here so that I haven't made any mistakes I'm going to go ahead and run
956:37 mistakes I'm going to go ahead and run python of qr. enter that opens up this
956:41 python of qr. enter that opens up this let me go ahead and zoom in if you've
956:43 let me go ahead and zoom in if you've got a phone handy and you'd like to scan
956:46 got a phone handy and you'd like to scan this code
956:47 this code here whether in person or
956:51 here whether in person or online I apologize you won't appreciate
956:55 online I apologize you won't appreciate it amazing okay and lastly let me go
957:00 it amazing okay and lastly let me go back into our speech example here uh
957:03 back into our speech example here uh create a final ending here on our final
957:05 create a final ending here on our final moments and how about we just say
957:06 moments and how about we just say something like this was cs50 like this
957:10 something like this was cs50 like this let's go ahead here fix my
957:12 let's go ahead here fix my capitalization just for tidiness get rid
957:13 capitalization just for tidiness get rid of the name and now with our final
957:15 of the name and now with our final flourish and your introduction to python
957:18 flourish and your introduction to python equipped here we go this was cs50 all
957:21 equipped here we go this was cs50 all right we'll see you next
957:22 right we'll see you next [Applause]
959:18 all right this is cs50 and this is already week seven and this is the week
959:20 already week seven and this is the week where we'll continue where we left off
959:22 where we'll continue where we left off with python introducing you to a bit
959:24 with python introducing you to a bit more syntax and capabilities of the
959:25 more syntax and capabilities of the language so you can solve like
959:26 language so you can solve like interesting problems but a lot of those
959:28 interesting problems but a lot of those problems increasingly are now going to
959:29 problems increasingly are now going to involve data in some form after all if
959:32 involve data in some form after all if you think of most any website or mobile
959:34 you think of most any website or mobile app uh or uh process nowadays that
959:37 app uh or uh process nowadays that involves solving problems it almost
959:39 involves solving problems it almost always involves some amount of data and
959:40 always involves some amount of data and often data at scale lots and lots of
959:42 often data at scale lots and lots of data and so what we're going to see
959:43 data and so what we're going to see first today is that yes you can use
959:45 first today is that yes you can use Python to solve all the problems past
959:47 Python to solve all the problems past that we've seen and also some data
959:49 that we've seen and also some data specific ones but sometimes it's just
959:50 specific ones but sometimes it's just going to be annoying it's going to be a
959:52 going to be annoying it's going to be a little painful it's just going to be
959:53 little painful it's just going to be more work than you might like to just
959:55 more work than you might like to just get to some answer and so today we'll
959:57 get to some answer and so today we'll too introduce you to a new language
959:59 too introduce you to a new language called SQL structured query language and
960:01 called SQL structured query language and this is a language that wur is actually
960:04 this is a language that wur is actually much smaller relatively speaking than C
960:06 much smaller relatively speaking than C and python it sort of does less but it
960:09 and python it sort of does less but it does it really well and it's a language
960:11 does it really well and it's a language for querying databases storing data in
960:13 for querying databases storing data in it updating it inserting it deleting it
960:15 it updating it inserting it deleting it and so much more and it's the kind of
960:17 and so much more and it's the kind of Technology that's used nowadays in
960:18 Technology that's used nowadays in indeed web apps and mobile apps data
960:21 indeed web apps and mobile apps data science analytics and and so much more
960:23 science analytics and and so much more it's really good at storing lots and
960:24 it's really good at storing lots and lots of data now this is yet another
960:27 lots of data now this is yet another language and believe it or not next week
960:29 language and believe it or not next week we'll introduce you to three more
960:31 we'll introduce you to three more languages HTML and CSS which are not
960:33 languages HTML and CSS which are not technically programming languages
960:35 technically programming languages they're all about Aesthetics and markup
960:37 they're all about Aesthetics and markup of information but also JavaScript which
960:39 of information but also JavaScript which is in fact a programming language but
960:41 is in fact a programming language but the goals here in cs50 really are going
960:43 the goals here in cs50 really are going to be to empower you to program more
960:45 to be to empower you to program more generally and indeed when you're out
960:47 generally and indeed when you're out there in the real world some years from
960:48 there in the real world some years from now invariably there's going to be some
960:50 now invariably there's going to be some new other popular language out there and
960:53 new other popular language out there and hopefully in this week and next week and
960:54 hopefully in this week and next week and Beyond among the goals is not just to
960:56 Beyond among the goals is not just to teach you these languages specifically
960:58 teach you these languages specifically but again like how to teach yourself the
961:00 but again like how to teach yourself the future languages that we've not even
961:01 future languages that we've not even heard about just yet so with that said
961:04 heard about just yet so with that said let's begin with a survey of sorts if
961:06 let's begin with a survey of sorts if you go to this URL on your phone or
961:09 you go to this URL on your phone or laptop cs50. l/f favorites a very simple
961:13 laptop cs50. l/f favorites a very simple Google form awaits you that's just going
961:15 Google form awaits you that's just going to ask you a couple of multiple choice
961:18 to ask you a couple of multiple choice questions so go to cs50.
961:21 questions so go to cs50. l/f favorites and that should lead you
961:25 l/f favorites and that should lead you to a Google form that looks a little
961:27 to a Google form that looks a little something like this asking you first as
961:30 something like this asking you first as of now in week seven what is your
961:31 of now in week seven what is your favorite language among those options
961:34 favorite language among those options here and then further down one more
961:36 here and then further down one more question if you think back on problem
961:37 question if you think back on problem sets 0 through six what was if any your
961:41 sets 0 through six what was if any your favorite uh problem set problem be it in
961:45 favorite uh problem set problem be it in scratch or C or python so answer those
961:48 scratch or C or python so answer those two
961:50 two questions and in a moment I'll flip over
961:52 questions and in a moment I'll flip over to my screen here where you'll see and
961:55 to my screen here where you'll see and anyone who's used Google forms knows the
961:58 anyone who's used Google forms knows the spreadsheet that's collecting now this
961:59 spreadsheet that's collecting now this data um Microsoft Office 365 can do the
962:02 data um Microsoft Office 365 can do the same if you use one of those forms and
962:03 same if you use one of those forms and what you see here now is a spreadsheet
962:05 what you see here now is a spreadsheet in Google Sheets enumerating all of the
962:08 in Google Sheets enumerating all of the audience's questions language is in
962:10 audience's questions language is in column B problem is in column C and each
962:12 column B problem is in column C and each row represents one student who has
962:14 row represents one student who has responded uh a few of you were super
962:16 responded uh a few of you were super eager for class today at 8:33 a.m.
962:19 eager for class today at 8:33 a.m. eastern time 10:32 11:10 okay so now
962:22 eastern time 10:32 11:10 okay so now we're getting into the actual class time
962:23 we're getting into the actual class time here and if I scroll down we'll probably
962:25 here and if I scroll down we'll probably see few dozen couple hundred Answers by
962:28 see few dozen couple hundred Answers by now and yeah so we're getting a whole
962:30 now and yeah so we're getting a whole lot of answers here and I'm seeing some
962:32 lot of answers here and I'm seeing some patterns emerge but but it's not
962:33 patterns emerge but but it's not necessarily obvious to the human eyes
962:35 necessarily obvious to the human eyes what those patterns are now of course
962:37 what those patterns are now of course you can use Google spreadsheets you can
962:39 you can use Google spreadsheets you can like highlight the data and you can
962:40 like highlight the data and you can create charts magically out of it but
962:42 create charts magically out of it but you can only do what Google lets you do
962:44 you can only do what Google lets you do with the data and same thing for
962:45 with the data and same thing for Microsoft Excel or apple numbers but
962:47 Microsoft Excel or apple numbers but wouldn't it be nice to just be able to
962:49 wouldn't it be nice to just be able to manipulate the raw data relatively
962:51 manipulate the raw data relatively simple though it is to just answer
962:53 simple though it is to just answer questions about the data Maybe long-term
962:54 questions about the data Maybe long-term create your own charts customize it just
962:56 create your own charts customize it just the way you want rather than be holding
962:58 the way you want rather than be holding to like software that's off the shelf
963:00 to like software that's off the shelf like this well how could we go about
963:03 like this well how could we go about doing this well let me propose that we
963:06 doing this well let me propose that we treat this data set now as what we're
963:08 treat this data set now as what we're going to call for now a flat file
963:10 going to call for now a flat file database we'll see today that there's
963:12 database we'll see today that there's fancier databases but the simplest
963:13 fancier databases but the simplest database in the world is really just
963:15 database in the world is really just like a CSV file and we saw that a couple
963:18 like a CSV file and we saw that a couple of weeks ago in C we wrote a bit of C
963:20 of weeks ago in C we wrote a bit of C code that used F printf to write data to
963:23 code that used F printf to write data to a file using commas as the separator we
963:26 a file using commas as the separator we didn't really do much more with csvs at
963:27 didn't really do much more with csvs at the time though because it's really
963:28 the time though because it's really annoying painful timec consuming not fun
963:31 annoying painful timec consuming not fun to use see for something like that
963:33 to use see for something like that because of malok and memory and all that
963:35 because of malok and memory and all that stuff but with python it's going to be
963:36 stuff but with python it's going to be much easier and so anytime you have
963:39 much easier and so anytime you have access to some data set where you can
963:41 access to some data set where you can just like download it to your own Mac or
963:42 just like download it to your own Mac or PC or your Cloud environment it's sort
963:44 PC or your Cloud environment it's sort of a candidate for now writing code to
963:47 of a candidate for now writing code to do something with the data Maybe analyze
963:49 do something with the data Maybe analyze it right away if it's been human inputed
963:51 it right away if it's been human inputed manually maybe you have to clean it up
963:53 manually maybe you have to clean it up by doing a lot of find and replace but
963:55 by doing a lot of find and replace but not with your keyboard but rather with
963:56 not with your keyboard but rather with code and so let me go ahead and do this
963:58 code and so let me go ahead and do this let me go back to my uh Google sheet
964:01 let me go back to my uh Google sheet here that has all of the data that's
964:03 here that has all of the data that's come in now and let me go ahead and
964:06 come in now and let me go ahead and download this via uh the file menu
964:10 download this via uh the file menu here and let's see download and you can
964:13 here and let's see download and you can see a whole bunch of options of most
964:14 see a whole bunch of options of most formats might be familiar but today
964:16 formats might be familiar but today we'll focus just on this one comma
964:18 we'll focus just on this one comma separated values or CSV that's going to
964:20 separated values or CSV that's going to go ahead and download it on my Mac here
964:22 go ahead and download it on my Mac here into my own downloads folder and now I'm
964:23 into my own downloads folder and now I'm going to go ahead and do this let me go
964:25 going to go ahead and do this let me go ahead and pull up VSS code in the cloud
964:27 ahead and pull up VSS code in the cloud here and if you've never done this
964:28 here and if you've never done this before there's a couple of ways to do it
964:30 before there's a couple of ways to do it but the simplest way to upload a file to
964:32 but the simplest way to upload a file to your codes space so to speak is just a
964:33 your codes space so to speak is just a sort of drag and drop that's going to
964:35 sort of drag and drop that's going to magically upload it to the server there
964:38 magically upload it to the server there and we'll see that one it has a very
964:40 and we'll see that one it has a very long file name which I'm I'm actually
964:42 long file name which I'm I'm actually going to clean this up because that's
964:43 going to clean this up because that's going to be very tedious to type in my
964:45 going to be very tedious to type in my code so I could either rightclick of of
964:48 code so I could either rightclick of of course up here but I'm going to use my
964:50 course up here but I'm going to use my Linux command so let's move this file
964:52 Linux command so let's move this file called cs50 2022 something or other and
964:54 called cs50 2022 something or other and let's just name it more simply
964:55 let's just name it more simply favorites. CSV so all lowercase no
964:58 favorites. CSV so all lowercase no spaces sort of good Basics and let me go
965:01 spaces sort of good Basics and let me go ahead now and open up this file with
965:03 ahead now and open up this file with code favorites. CSV I'll close my file
965:06 code favorites. CSV I'll close my file explorer and we'll see exactly the same
965:08 explorer and we'll see exactly the same data as before but not quite as pretty
965:10 data as before but not quite as pretty as Google Sheets makes it be rather we
965:12 as Google Sheets makes it be rather we see here that I still have three columns
965:14 see here that I still have three columns timestamp language problem and then all
965:17 timestamp language problem and then all of the values down below including the
965:18 of the values down below including the timestamps and the answers therefore but
965:21 timestamps and the answers therefore but it doesn't have proper columns it just
965:22 it doesn't have proper columns it just has commas separating them now we could
965:25 has commas separating them now we could very easily write python code just like
965:27 very easily write python code just like we wrote code to manipulate files like
965:31 we wrote code to manipulate files like this either to write or read but in
965:33 this either to write or read but in instead let's do something that's a
965:34 instead let's do something that's a little more pleasant which is indeed in
965:36 little more pleasant which is indeed in the form of python so python actually
965:37 the form of python so python actually comes with Native support for csvs it
965:40 comes with Native support for csvs it has indeed a package called CSV that
965:43 has indeed a package called CSV that just lets you read and write and do a
965:44 just lets you read and write and do a whole bunch of useful stuff when it
965:46 whole bunch of useful stuff when it comes to CSV files so let's go ahead and
965:48 comes to CSV files so let's go ahead and do something with this file let me go
965:50 do something with this file let me go back here to VSS code I'm going to close
965:52 back here to VSS code I'm going to close favorites. CSV for now but just remember
965:55 favorites. CSV for now but just remember in your mind that timestamp was the
965:56 in your mind that timestamp was the first column language was the second
965:59 first column language was the second column and problem was the third and
966:01 column and problem was the third and notice because we're using commas they
966:02 notice because we're using commas they don't again line up perfectly but that's
966:04 don't again line up perfectly but that's not a problem there are two commas in
966:06 not a problem there are two commas in every line presumably and I'm going to
966:08 every line presumably and I'm going to go ahead and now create a file called
966:09 go ahead and now create a file called how about uh favorites. piy so that I
966:12 how about uh favorites. piy so that I can start writing some code to
966:13 can start writing some code to manipulate this data and let's do
966:15 manipulate this data and let's do something simple let's just write a
966:16 something simple let's just write a simple program in Python that opens this
966:18 simple program in Python that opens this file reads it and prints something out
966:21 file reads it and prints something out just as like a safety check that I know
966:23 just as like a safety check that I know what I'm doing even though it's not
966:24 what I'm doing even though it's not going to be useful so in Python if you
966:26 going to be useful so in Python if you want CSV support you import CSV and that
966:29 want CSV support you import CSV and that gives you access to all the magical
966:31 gives you access to all the magical capabilities thereof let me now go ahead
966:33 capabilities thereof let me now go ahead and use this technique to open a file in
966:36 and use this technique to open a file in Python which is similar in C but with
966:38 Python which is similar in C but with python I'm going to do this the keyword
966:40 python I'm going to do this the keyword with I'm going to open a file called
966:43 with I'm going to open a file called favorites. CSV which was the shorter
966:45 favorites. CSV which was the shorter name I gave it this is optional but just
966:47 name I gave it this is optional but just for explicitness I'm going to open it in
966:49 for explicitness I'm going to open it in read mode explicitly just like f open
966:52 read mode explicitly just like f open took a second argument as well and I'm
966:54 took a second argument as well and I'm going to name this file once open quite
966:56 going to name this file once open quite simply file though I could call it
966:57 simply file though I could call it anything I want and now it's just an
967:00 anything I want and now it's just an open file so far as python knows at this
967:02 open file so far as python knows at this moment it's just text or better yet it's
967:05 moment it's just text or better yet it's just zeros and ones if you want this
967:07 just zeros and ones if you want this python package called CSV to actually do
967:10 python package called CSV to actually do something useful with it you have to
967:12 something useful with it you have to load this file now into the library and
967:14 load this file now into the library and the simplest way to do this is to give
967:16 the simplest way to do this is to give myself like a variable called reader
967:18 myself like a variable called reader because I want to read this file though
967:20 because I want to read this file though this too I could call anything else I'm
967:22 this too I could call anything else I'm going to then set that equal to the
967:23 going to then set that equal to the return value of a function called CSV do
967:26 return value of a function called CSV do reader and I pass to that per the
967:28 reader and I pass to that per the documentation the open file so step one
967:31 documentation the open file so step one I open the file and this just gives me
967:32 I open the file and this just gives me me access to the bytes there in step two
967:35 me access to the bytes there in step two now with csvreader tells the python
967:38 now with csvreader tells the python package called CSV to do something
967:40 package called CSV to do something useful with it and start analyzing the
967:42 useful with it and start analyzing the commas and uh allow me to uh parse it
967:45 commas and uh allow me to uh parse it further so let's go ahead and do this
967:47 further so let's go ahead and do this let me go ahead now and within this Loop
967:50 let me go ahead now and within this Loop let's say this with sorry within this
967:52 let's say this with sorry within this open file let's do this for every row if
967:57 open file let's do this for every row if you will or line in the file AKA reader
968:01 you will or line in the file AKA reader let's go ahead and print out now just
968:03 let's go ahead and print out now just how about Row Bracket one now what's
968:07 how about Row Bracket one now what's going on here well it turns out if you
968:09 going on here well it turns out if you read the documentation for the CSV
968:10 read the documentation for the CSV reader function what it hands you back
968:13 reader function what it hands you back is essentially this special object so to
968:16 is essentially this special object so to speak that allows you to treat it as
968:18 speak that allows you to treat it as though it's just a really big list of
968:20 though it's just a really big list of lines from the file AKA reader so by
968:23 lines from the file AKA reader so by saying for Row in reader this is a way
968:26 saying for Row in reader this is a way more succinct way of saying uh give me
968:29 more succinct way of saying uh give me the first line in the file plus plus
968:31 the first line in the file plus plus give me the second line in the file Plus
968:32 give me the second line in the file Plus plus and so forth that we would have
968:33 plus and so forth that we would have done what much more mechanically in C
968:36 done what much more mechanically in C this is just much more pythonic and
968:38 this is just much more pythonic and English friendly if you will so in every
968:40 English friendly if you will so in every iteration of this Loop row is going to
968:43 iteration of this Loop row is going to contain all of the data from the current
968:45 contain all of the data from the current row but better yet what the reader
968:47 row but better yet what the reader function does for me is it hands me each
968:49 function does for me is it hands me each row not just as a big string or Stir of
968:52 row not just as a big string or Stir of text in Python it gives me what
968:55 text in Python it gives me what apparently based on the syntax on line
968:58 apparently based on the syntax on line six any Instinct yeah it's giving me
969:01 six any Instinct yeah it's giving me back indeed a list and I'm I presume the
969:03 back indeed a list and I'm I presume the visual clue for you was the fact that
969:05 visual clue for you was the fact that we're using square brackets here and
969:06 we're using square brackets here and indeed Row Bracket one is going to be
969:09 indeed Row Bracket one is going to be not the first but the second element in
969:11 not the first but the second element in that list and so just take a guess when
969:13 that list and so just take a guess when I run this code in a moment What's going
969:16 I run this code in a moment What's going to get printed the timestamp the
969:18 to get printed the timestamp the language or the
969:25 problem the yeah oh the language the language because it's the second column
969:27 language because it's the second column that is in the file delimited by those
969:29 that is in the file delimited by those those commas so let me go ahead and do
969:31 those commas so let me go ahead and do this let me clear my terminal down here
969:32 this let me clear my terminal down here here let me run python of favorites.i
969:35 here let me run python of favorites.i and enter and there's everything it was
969:37 and enter and there's everything it was super fast but there's a really long
969:38 super fast but there's a really long list here and in fact if I increase the
969:41 list here and in fact if I increase the size of my terminal and start scrolling
969:43 size of my terminal and start scrolling up you'll just see all of the raw data
969:45 up you'll just see all of the raw data now this isn't that useful yet I could
969:47 now this isn't that useful yet I could have just glanced at the CSV but clearly
969:48 have just glanced at the CSV but clearly now I have the ability to open the file
969:51 now I have the ability to open the file parse it so to speak that is break it up
969:53 parse it so to speak that is break it up into its constituent parts and do
969:55 into its constituent parts and do something with specific Parts therein
969:59 something with specific Parts therein all right so if I want to do this a
970:00 all right so if I want to do this a little more pleasantly though let me at
970:02 little more pleasantly though let me at least least make this semantically a
970:04 least least make this semantically a little cleaner and you know what just
970:05 little cleaner and you know what just for clarity let me just give myself a
970:07 for clarity let me just give myself a variable it's not strictly necessary but
970:10 variable it's not strictly necessary but I know that this is the favorite uh for
970:13 I know that this is the favorite uh for instance uh language so let's just call
970:15 instance uh language so let's just call it favorite set it equal to Row Bracket
970:18 it favorite set it equal to Row Bracket 1 and now just to be more explicit in my
970:20 1 and now just to be more explicit in my code even though again we don't need the
970:22 code even though again we don't need the variable per se this codee's of course
970:24 variable per se this codee's of course going to do the same thing it's just
970:25 going to do the same thing it's just using an additional variable called
970:26 using an additional variable called favorite if I go down here scroll up run
970:30 favorite if I go down here scroll up run the program again I get back the exact
970:31 the program again I get back the exact same data but this is a stepping stone
970:33 same data but this is a stepping stone to something that's even more powerful
970:36 to something that's even more powerful about python support for CSV files is
970:38 about python support for CSV files is that you don't have to just treat the
970:40 that you don't have to just treat the return value as a list with zero and one
970:45 return value as a list with zero and one and two so just thinking intuitively
970:47 and two so just thinking intuitively here why is this maybe not the best
970:50 here why is this maybe not the best design to hand you the programmer back
970:52 design to hand you the programmer back the data in a list that's numerically
970:55 the data in a list that's numerically indexed with
970:57 indexed with 012 it clearly works but critique this
971:02 012 it clearly works but critique this what could go wrong what's a little
971:04 what could go wrong what's a little poorly designed yeah you have to always
971:06 poorly designed yeah you have to always remember what
971:09 remember what are
971:15 yeah exactly so if yeah so it's up to you to repeat it's up to you to remember
971:18 you to repeat it's up to you to remember like what column the data is actually in
971:20 like what column the data is actually in and you know God forbid you're you're
971:22 and you know God forbid you're you're collaborating with someone else on the
971:23 collaborating with someone else on the spreadsheet you know you've if you've
971:25 spreadsheet you know you've if you've used Google spreadsheets you can move
971:26 used Google spreadsheets you can move columns around maybe just because you
971:28 columns around maybe just because you want to visually reorganize things and
971:29 want to visually reorganize things and if you do this and then someone else
971:31 if you do this and then someone else downloads that same data all of their
971:33 downloads that same data all of their code is going to break so that's just
971:34 code is going to break so that's just really bad design it's fragile just
971:36 really bad design it's fragile just because it's you're sort of on the honor
971:38 because it's you're sort of on the honor System that one means the data that you
971:40 System that one means the data that you want so wouldn't it be nice if we could
971:42 want so wouldn't it be nice if we could be a little more explicit well recall
971:44 be a little more explicit well recall that the very first line in this file is
971:48 that the very first line in this file is actually this and I paused the output
971:50 actually this and I paused the output this time so that we can see more
971:52 this time so that we can see more optionally I just reran favorites St pi
971:55 optionally I just reran favorites St pi and notice one of these things is not
971:56 and notice one of these things is not like the other every output was either
971:58 like the other every output was either scratch or C or python except for this
972:00 scratch or C or python except for this first one why am I seeing the word
972:02 first one why am I seeing the word language
972:08 here where did language come from you didn't have the ability to manual input
972:10 didn't have the ability to manual input oh no where did it come from
972:13 oh no where did it come from yeah yeah the header the very first row
972:16 yeah yeah the header the very first row in the file which by human convention
972:17 in the file which by human convention generally just defines what the columns
972:19 generally just defines what the columns represent so that there's some human
972:21 represent so that there's some human useful information there now that's not
972:23 useful information there now that's not really intended to be part of my output
972:26 really intended to be part of my output at the moment so there is a way to skip
972:27 at the moment so there is a way to skip this if you want to skip the first row
972:30 this if you want to skip the first row you can actually do something like this
972:31 you can actually do something like this you can say next row and that will just
972:33 you can say next row and that will just ignore that row so that I'm starting
972:35 ignore that row so that I'm starting really with the every row thereafter but
972:38 really with the every row thereafter but there's a better way to handle this than
972:40 there's a better way to handle this than that that will get rid of the row in the
972:41 that that will get rid of the row in the output but let me go ahead and use a
972:43 output but let me go ahead and use a different feature of the CSV package
972:45 different feature of the CSV package that's just going to make this a little
972:46 that's just going to make this a little cleaner Al together so let me clear my
972:48 cleaner Al together so let me clear my terminal window here let me undo this
972:50 terminal window here let me undo this next thing that I just added and instead
972:52 next thing that I just added and instead of using a reader let me go ahead and
972:55 of using a reader let me go ahead and use a dictionary reader abbreviated dict
972:57 use a dictionary reader abbreviated dict reader that's going to now return me the
973:01 reader that's going to now return me the equivalent of all of the rows one at a
973:04 equivalent of all of the rows one at a time so I can still call it Reader just
973:06 time so I can still call it Reader just as before but as the name implies what
973:09 as before but as the name implies what this reader is going to return is not a
973:11 this reader is going to return is not a list after list after list but a
973:13 list after list after list but a dictionary a dictionary a dictionary and
973:15 dictionary a dictionary a dictionary and remember a dictionary is just a
973:16 remember a dictionary is just a collection of key value pairs so what
973:19 collection of key value pairs so what does that mean what are the keys what
973:20 does that mean what are the keys what are the values well now that I'm using a
973:23 are the values well now that I'm using a dictionary reader I can actually do this
973:25 dictionary reader I can actually do this instead of sort of on the honor System
973:27 instead of sort of on the honor System remembering that I want column one I can
973:30 remembering that I want column one I can treat row now not as a list but as a
973:32 treat row now not as a list but as a dictionary and that means I can go in
973:34 dictionary and that means I can go in here and say quote unquote language and
973:36 here and say quote unquote language and we saw that back in week six python
973:39 we saw that back in week six python allows you to index into dictionaries
973:41 allows you to index into dictionaries using square bracket notation in strings
973:43 using square bracket notation in strings or stirs on the inside just like lists
973:45 or stirs on the inside just like lists allow for numbers but this now I think
973:48 allow for numbers but this now I think is going to be a little more robust if I
973:50 is going to be a little more robust if I run this again python of favorites. py
973:53 run this again python of favorites. py all of that worked out fine and let me
973:55 all of that worked out fine and let me pause the output to by using this
973:57 pause the output to by using this program called more now I don't even see
974:00 program called more now I don't even see the header so now whoever works it uh
974:03 the header so now whoever works it uh with python wrote the code for this
974:05 with python wrote the code for this package to just analyze that first line
974:07 package to just analyze that first line of code use the header as you just
974:09 of code use the header as you just called it as the keys and then every
974:12 called it as the keys and then every time you iterate through this Loop it
974:13 time you iterate through this Loop it updates the values the values the values
974:15 updates the values the values the values but the keys stay the same any questions
974:19 but the keys stay the same any questions then on this
974:21 then on this technique spiced to say this would be
974:24 technique spiced to say this would be painful in C
974:37 Associated exactly so the keys are always going to be quote unquote
974:39 always going to be quote unquote timestamp uh Pro language and problem
974:42 timestamp uh Pro language and problem but on each iteration of this Loop here
974:45 but on each iteration of this Loop here uh the row is going to contain a
974:46 uh the row is going to contain a different row of values different row
974:48 different row of values different row values different row values so you're
974:50 values different row values so you're going to get back one dictionary for
974:52 going to get back one dictionary for every student who submitted the Google
974:54 every student who submitted the Google form if you will while iterating through
974:56 form if you will while iterating through it there all right so once we have this
974:58 it there all right so once we have this ability here why don't we go ahead and
975:01 ability here why don't we go ahead and transition
975:02 transition into how about not just using that
975:05 into how about not just using that dictionary reader which makes the code a
975:07 dictionary reader which makes the code a little more robust because now if you
975:08 little more robust because now if you move the columns around no big deal it
975:10 move the columns around no big deal it doesn't matter if the numeric indices
975:13 doesn't matter if the numeric indices change you can still use those keywords
975:15 change you can still use those keywords instead well let's actually analyze the
975:17 instead well let's actually analyze the data now I'm just spitting it out which
975:18 data now I'm just spitting it out which is not solving any problems for anyone
975:21 is not solving any problems for anyone so let's go ahead and and count the
975:23 so let's go ahead and and count the popularity of scratch C and Python and
975:25 popularity of scratch C and Python and see what everyone's uh been thinking
975:28 see what everyone's uh been thinking here all right so how might I do this
975:29 here all right so how might I do this well let me go ahead and do this up here
975:32 well let me go ahead and do this up here before I start iterating let me give
975:34 before I start iterating let me give myself let's say three variables and to
975:36 myself let's say three variables and to keep things simple I'll say one variable
975:38 keep things simple I'll say one variable called scratch set it equal to zero for
975:41 called scratch set it equal to zero for zero students so far C is going to equal
975:44 zero students so far C is going to equal zero and python is going to equal zero
975:46 zero and python is going to equal zero there's a slightly prettier way of doing
975:48 there's a slightly prettier way of doing this just because this is like three
975:49 this just because this is like three lines of code to do something very
975:51 lines of code to do something very simple you could alternatively in Python
975:54 simple you could alternatively in Python but not C do scratch comma C comma
975:58 but not C do scratch comma C comma python equals 0 comma 0 so kind of
976:02 python equals 0 comma 0 so kind of slightly more elegant just to fit it all
976:03 slightly more elegant just to fit it all into one line but now let's just do
976:05 into one line but now let's just do something more interesting on line seven
976:07 something more interesting on line seven I'm still going to figure out what the
976:08 I'm still going to figure out what the current favorite language is and now I'm
976:11 current favorite language is and now I'm just going to do some conditional checks
976:14 just going to do some conditional checks How about if that favorite equals equals
976:17 How about if that favorite equals equals quote unquote scratch let's go ahead and
976:20 quote unquote scratch let's go ahead and increment Scratch by one we can't do
976:22 increment Scratch by one we can't do plus plus in Python but we can do plus
976:25 plus plus in Python but we can do plus equals 1 how about L if favorite equals
976:28 equals 1 how about L if favorite equals equals c then let's do C+ equal 1 L and
976:32 equals c then let's do C+ equal 1 L and not we could do else this is actually a
976:35 not we could do else this is actually a good design question should I do else
976:37 good design question should I do else should I do L if any instincts here
976:50 yeah yeah really good instincts just in case someone goes and adds another
976:52 case someone goes and adds another language to the form next week because
976:53 language to the form next week because we're obviously going to introduce
976:54 we're obviously going to introduce another language today you don't want
976:56 another language today you don't want your code to now artificially inflate
976:57 your code to now artificially inflate the scores for python just because
977:00 the scores for python just because you're conflating multiple languages
977:02 you're conflating multiple languages together so the more defensive sort of
977:04 together so the more defensive sort of better way to write this code I agree
977:06 better way to write this code I agree would be L if favorite equals equals
977:09 would be L if favorite equals equals python then let's go ahead and increment
977:11 python then let's go ahead and increment python plus equals 1 and if there's a
977:14 python plus equals 1 and if there's a new language next week we're obviously
977:15 new language next week we're obviously going to have to update the code but at
977:16 going to have to update the code but at least we're not miscounting we're just
977:18 least we're not miscounting we're just missing the new language so I think
977:19 missing the new language so I think that's slightly more robust all right
977:21 that's slightly more robust all right now at the very bottom of this program
977:23 now at the very bottom of this program and outside of the loop when I'm all
977:25 and outside of the loop when I'm all done counting let me go ahead and print
977:27 done counting let me go ahead and print out using some f strings how about the
977:30 out using some f strings how about the total number of people who uh whose
977:32 total number of people who uh whose favorite is scratch so this is just uh
977:35 favorite is scratch so this is just uh week six F string syntax let me go ahead
977:37 week six F string syntax let me go ahead and print out another F string for C and
977:40 and print out another F string for C and I'm of course putting the variables in
977:42 I'm of course putting the variables in curly braces all lowercase but the
977:44 curly braces all lowercase but the English words I'm doing capitalization
977:46 English words I'm doing capitalization for Let's do an final one with f uh
977:49 for Let's do an final one with f uh python colon and then in curly braces
977:51 python colon and then in curly braces python close quote and I think I'm done
977:54 python close quote and I think I'm done so let me just hide my terminal for a
977:56 so let me just hide my terminal for a second here's the total program same
977:58 second here's the total program same stuff as before open favorites. CSV open
978:01 stuff as before open favorites. CSV open it further with the dictionary reader to
978:04 it further with the dictionary reader to do that processing for us initialize
978:05 do that processing for us initialize three variables to zero just so we have
978:07 three variables to zero just so we have something to count with and then iterate
978:10 something to count with and then iterate over the file row by row and this is
978:12 over the file row by row and this is just some sort of week one style
978:13 just some sort of week one style conditional logic albe it in Python
978:15 conditional logic albe it in Python counting things all right so how can we
978:18 counting things all right so how can we now execute this let me go back to my
978:20 now execute this let me go back to my terminal python of favorites.i and here
978:23 terminal python of favorites.i and here we go uh as of today everyone who's
978:26 we go uh as of today everyone who's reporting in live via the Google form
978:28 reporting in live via the Google form their favorite languages are
978:32 their favorite languages are interesting that's pretty interesting
978:34 interesting that's pretty interesting too after just one week of python no
978:36 too after just one week of python no less so but scratch is a healthy
978:38 less so but scratch is a healthy Contender there a lot of C so a pretty
978:39 Contender there a lot of C so a pretty good mix here so is this going to be the
978:42 good mix here so is this going to be the best way to write this program long term
978:45 best way to write this program long term well as you noted if there's a new
978:46 well as you noted if there's a new language next week this week we're going
978:48 language next week this week we're going to have to constantly update this and
978:50 to have to constantly update this and here's where you should let your mind
978:52 here's where you should let your mind wander to like the future like if we
978:54 wander to like the future like if we have a fourth language fifth language
978:56 have a fourth language fifth language 6th sth eth which aspect here might kind
978:59 6th sth eth which aspect here might kind of have some code smell to it like this
979:02 of have some code smell to it like this probably isn't the best design to set us
979:04 probably isn't the best design to set us up for the
979:05 up for the future what might be better than this
979:09 future what might be better than this yeah add language line yeah we have to
979:12 yeah add language line yeah we have to keep adding a language to line five and
979:14 keep adding a language to line five and okay not a big deal we could add like
979:16 okay not a big deal we could add like SQL today and maybe JavaScript next week
979:19 SQL today and maybe JavaScript next week but you know anytime a a line of code a
979:21 but you know anytime a a line of code a line of logic is just going to kind of
979:22 line of logic is just going to kind of grow out of control and we've had this
979:24 grow out of control and we've had this chat a couple of times with different
979:25 chat a couple of times with different syntax there's probably a better way
979:27 syntax there's probably a better way than that so let's do that instead of
979:29 than that so let's do that instead of using these individual variables we
979:31 using these individual variables we could make maybe use a list but a list
979:33 could make maybe use a list but a list would be a little confusing because like
979:36 would be a little confusing because like what does braet zero mean what is
979:38 what does braet zero mean what is bracket 1 bracket two but a dictionary
979:40 bracket 1 bracket two but a dictionary recall is like this Swiss army knife of
979:42 recall is like this Swiss army knife of data structures whereby you can
979:43 data structures whereby you can associate anything with anything else
979:45 associate anything with anything else keys with values so I dare say a cleaner
979:48 keys with values so I dare say a cleaner way to solve this problem that sets us
979:50 way to solve this problem that sets us up for Less work or confusion later
979:52 up for Less work or confusion later would be to create like a new variable
979:54 would be to create like a new variable called counts if that's what we're doing
979:56 called counts if that's what we're doing counting things up and just set it equal
979:58 counting things up and just set it equal to an empty dictionary and you can
980:01 to an empty dictionary and you can literally say adct with the open
980:02 literally say adct with the open parenthesis closed parenthesis nothing
980:04 parenthesis closed parenthesis nothing or the more pythonic just use open and
980:07 or the more pythonic just use open and Clos curly braces with nothing inside
980:09 Clos curly braces with nothing inside that gives me an empty dictionary just
980:10 that gives me an empty dictionary just like square brackets gives me a list now
980:14 like square brackets gives me a list now my logic down here has to change a
980:15 my logic down here has to change a little bit but what's nice is I don't
980:17 little bit but what's nice is I don't need one conditional for every language
980:21 need one conditional for every language because again if we have a fourth a
980:22 because again if we have a fourth a fifth a sixth that chunk of code is also
980:25 fifth a sixth that chunk of code is also going to grow a bit out of control too
980:27 going to grow a bit out of control too so I can get rid of this here and what I
980:29 so I can get rid of this here and what I think I'm going to do is say this
980:31 think I'm going to do is say this whatever the current favorite is from
980:33 whatever the current favorite is from the current Row in the file why don't we
980:35 the current Row in the file why don't we go into our counts variable at that key
980:40 go into our counts variable at that key and again favorite is a variable it's
980:42 and again favorite is a variable it's not quote unquote favorite it's going to
980:44 not quote unquote favorite it's going to be scratch or C or Python and then why
980:47 be scratch or C or Python and then why don't we go ahead and just increment
980:49 don't we go ahead and just increment whatever the value of that count is at
980:52 whatever the value of that count is at that key now this is technically bugy
980:55 that key now this is technically bugy we're really close but there is a
980:58 we're really close but there is a bug does anyone want to conjecture what
981:01 bug does anyone want to conjecture what the bug is
981:12 yeah a good question that answers my question in uh nonetheless so no like
981:15 question in uh nonetheless so no like the magic you describe will not happen
981:16 the magic you describe will not happen and to repeat the the hypothesis will
981:18 and to repeat the the hypothesis will this automatically create a key for
981:22 this automatically create a key for every uh language that we try plugging
981:25 every uh language that we try plugging into those square brackets short answer
981:27 into those square brackets short answer no odds are this is going to create a
981:29 no odds are this is going to create a key error one of those traceback error
981:31 key error one of those traceback error messages that you've probably seen by
981:32 messages that you've probably seen by now either in class or in problem sets
981:34 now either in class or in problem sets whereby if scratch hasn't appeared in
981:37 whereby if scratch hasn't appeared in the dictionary before or C or python
981:39 the dictionary before or C or python like then the dictionary has no clue
981:41 like then the dictionary has no clue what you're talking about so I think we
981:43 what you're talking about so I think we actually still need some conditional
981:44 actually still need some conditional logic but not that's going to grow
981:46 logic but not that's going to grow longer and longer with each language
981:48 longer and longer with each language what I think we probably want to do is
981:49 what I think we probably want to do is this if the current favorite is in the
981:53 this if the current favorite is in the counts dictionary and this is the
981:55 counts dictionary and this is the pythonic way of just saying is this key
981:57 pythonic way of just saying is this key in this dictionary then go ahead and
982:00 in this dictionary then go ahead and safely do count favorite plus equals 1
982:04 safely do count favorite plus equals 1 else to your conjecture now else what do
982:07 else to your conjecture now else what do I want to do counts
982:09 I want to do counts favorite
982:11 favorite equals yeah one so initialize a brand
982:14 equals yeah one so initialize a brand new key to a brand new value of one
982:16 new key to a brand new value of one because I'm obviously just seen this
982:18 because I'm obviously just seen this language otherwise increment again and
982:21 language otherwise increment again and again and now down here I just need to
982:22 again and now down here I just need to tweak my syntax a little bit I don't
982:25 tweak my syntax a little bit I don't need to print out all of these things
982:27 need to print out all of these things one at a time manually I can actually
982:29 one at a time manually I can actually get away I think with another loop at
982:31 get away I think with another loop at the very bottom here so how about I do
982:33 the very bottom here so how about I do this for each favorite in those counts
982:36 this for each favorite in those counts and this is again the pythonic way to
982:38 and this is again the pythonic way to iterate over all of the keys in a
982:40 iterate over all of the keys in a dictionary go ahead and print out using
982:43 dictionary go ahead and print out using an F string whatever the current
982:46 an F string whatever the current favorite is scratch or C or Python and
982:50 favorite is scratch or C or Python and then a colon and then figure out what
982:53 then a colon and then figure out what its count is and you can do that by
982:55 its count is and you can do that by going into the counts dictionary looking
982:57 going into the counts dictionary looking at the favorite key and get back its
983:00 at the favorite key and get back its value so close my curly braces I close
983:02 value so close my curly braces I close my quotes and even though this looks
983:04 my quotes and even though this looks ugly at the moment now this is much more
983:06 ugly at the moment now this is much more dynamic because if we go and add SQL to
983:08 dynamic because if we go and add SQL to the CSV file tomorrow where we add
983:10 the CSV file tomorrow where we add JavaScript next week this will just work
983:13 JavaScript next week this will just work it will keep working now automatically
983:15 it will keep working now automatically all I change is the Google form not my
983:16 all I change is the Google form not my actual code all right let's try python
983:18 actual code all right let's try python of favorites.i cross my fingers as
983:21 of favorites.i cross my fingers as always and there now is the data as of
983:24 always and there now is the data as of now questions on this code here
983:32 yeah really good good question what if you wanted to print it in a particular
983:34 you wanted to print it in a particular order uh well I could give you a couple
983:37 order uh well I could give you a couple of solutions like if you want to print
983:38 of solutions like if you want to print it out in it's already coincidentally in
983:42 it out in it's already coincidentally in alphabetical order so you got that for
983:43 alphabetical order so you got that for free although that's just by chance here
983:46 free although that's just by chance here but there is a way to do this and let me
983:47 but there is a way to do this and let me propose that we go down here to my Loop
983:49 propose that we go down here to my Loop and I explicitly use a function you
983:51 and I explicitly use a function you might not have seen in Python yet but
983:52 might not have seen in Python yet but it's literally called sorted which is
983:54 it's literally called sorted which is going to take either a list or in this
983:56 going to take either a list or in this case a dictionary and by default sort it
983:58 case a dictionary and by default sort it by key alphabetically now if my
984:01 by key alphabetically now if my intuition is correct this is not going
984:03 intuition is correct this is not going to change the output because it's
984:04 to change the output because it's already alphabetical but if you read the
984:06 already alphabetical but if you read the documentation for the sorted function it
984:08 documentation for the sorted function it takes multiple parameters potentially
984:10 takes multiple parameters potentially some of which are named parameters and
984:12 some of which are named parameters and so you can actually do this if you want
984:14 so you can actually do this if you want to sort the counts but you want to
984:16 to sort the counts but you want to reverse the order for whatever reason
984:18 reverse the order for whatever reason here so that it's reverse alphabetical
984:19 here so that it's reverse alphabetical order now let me go ahead and rerun this
984:21 order now let me go ahead and rerun this and I'll keep the previous output on the
984:23 and I'll keep the previous output on the screen enter and now it's backwards uh
984:27 screen enter and now it's backwards uh alphabetically if you will other
984:29 alphabetically if you will other questions on this here
984:33 questions on this here here no how about then how about then we
984:37 here no how about then how about then we transition
984:39 transition to changing sorting by value and let me
984:42 to changing sorting by value and let me this is going to escalate a little
984:44 this is going to escalate a little quickly briefly but then we'll we'll
984:45 quickly briefly but then we'll we'll tone it down again notice that right now
984:48 tone it down again notice that right now this is indeed sorting by key what if
984:50 this is indeed sorting by key what if especially if I have lots of data it'd
984:51 especially if I have lots of data it'd be nice to make like a top 10 list or in
984:53 be nice to make like a top 10 list or in this case a top three list and actually
984:55 this case a top three list and actually see in order of the counts the values uh
984:59 see in order of the counts the values uh what these popular ones are so it's not
985:01 what these popular ones are so it's not C python scratch it should ideally be
985:03 C python scratch it should ideally be python then C then scratch because of
985:07 python then C then scratch because of the values and the magnitude thereof so
985:09 the values and the magnitude thereof so how can I do this well it turns out
985:11 how can I do this well it turns out there's another key another uh parameter
985:14 there's another key another uh parameter that you can pass to the sorted function
985:17 that you can pass to the sorted function that is typically implemented as a
985:19 that is typically implemented as a function itself and so I'm going to go
985:21 function itself and so I'm going to go ahead and do this I'm going to
985:22 ahead and do this I'm going to temporarily Define a function called get
985:24 temporarily Define a function called get value just to make my life easier and
985:27 value just to make my life easier and this get value function is going to take
985:29 this get value function is going to take I'll say a language par parameter and
985:32 I'll say a language par parameter and then all I'm going to do is return
985:34 then all I'm going to do is return whatever the count is of that language
985:37 whatever the count is of that language so out of context this is just a super
985:39 so out of context this is just a super simple function that you hand it a
985:41 simple function that you hand it a language like scratch or C or python
985:43 language like scratch or C or python it's just going to tell you what the
985:44 it's just going to tell you what the count is thereof in that uh in that
985:47 count is thereof in that uh in that dictionary called counts but what I can
985:49 dictionary called counts but what I can do now down here in my newly introduced
985:51 do now down here in my newly introduced call to sorted is I can tell it what to
985:55 call to sorted is I can tell it what to use as its key instead of using
985:57 use as its key instead of using literally the key scratch C python I can
986:00 literally the key scratch C python I can sort of override that behavior and say
986:02 sort of override that behavior and say you know what to figure out what to sort
986:05 you know what to figure out what to sort by go ahead and call this function
986:08 by go ahead and call this function called get value notice that I have not
986:11 called get value notice that I have not put parentheses after get value because
986:13 put parentheses after get value because I don't want to call get value right
986:16 I don't want to call get value right then and there I want to pass the get
986:18 then and there I want to pass the get value function as itself in argument to
986:22 value function as itself in argument to the sorted function so that the sorted
986:24 the sorted function so that the sorted function written years ago by the people
986:25 function written years ago by the people at python can call my version of get
986:28 at python can call my version of get value again and again and again when
986:30 value again and again and again when they try to sort sort this actual data
986:32 they try to sort sort this actual data so now if I add that and I leave reverse
986:36 so now if I add that and I leave reverse equals true let's see what happens
986:38 equals true let's see what happens python of favorites. py enter and now I
986:42 python of favorites. py enter and now I get my top 10 or in this case top three
986:44 get my top 10 or in this case top three list and if I had more sophisticated
986:46 list and if I had more sophisticated data with like more columns Al together
986:48 data with like more columns Al together that I actually care about I could even
986:50 that I actually care about I could even sort this more uh powerfully as well but
986:53 sort this more uh powerfully as well but let me clean this up a little bit just
986:54 let me clean this up a little bit just so you've seen it even though we won't
986:56 so you've seen it even though we won't use these that often in cs50 until the
986:58 use these that often in cs50 until the end of the class will they come up again
987:01 end of the class will they come up again technically this is a little bit H this
987:03 technically this is a little bit H this isn't necessarily the best design to
987:05 isn't necessarily the best design to spend all this time implementing a
987:06 spend all this time implementing a function and then only use it in one
987:08 function and then only use it in one place in general we've argued that H you
987:11 place in general we've argued that H you don't necessarily need a variable if
987:12 don't necessarily need a variable if you're only going to use it in one place
987:14 you're only going to use it in one place you don't really need a function if
987:15 you don't really need a function if you're only going to use it in one place
987:16 you're only going to use it in one place and here we kind of have a good
987:18 and here we kind of have a good candidate for that and so it turns out
987:20 candidate for that and so it turns out in Python if you don't want to bother
987:22 in Python if you don't want to bother creating a function just to use it once
987:25 creating a function just to use it once you can create what's called an
987:27 you can create what's called an anonymous function AKA a Lambda function
987:30 anonymous function AKA a Lambda function like the lamb Lambda symbol familiar and
987:32 like the lamb Lambda symbol familiar and a Lambda function the syntax is a little
987:34 a Lambda function the syntax is a little strange looking but you say this you
987:37 strange looking but you say this you literally say Lambda you literally then
987:39 literally say Lambda you literally then say the name of the argument that you
987:41 say the name of the argument that you want this Anonymous function with no
987:43 want this Anonymous function with no name to take then you have a colon and
987:47 name to take then you have a colon and then quite simply you write what you
987:49 then quite simply you write what you want the return value of this function
987:51 want the return value of this function to be you don't even say return
987:53 to be you don't even say return literally these Lambda functions are
987:55 literally these Lambda functions are meant to be used super turly so that you
987:59 meant to be used super turly so that you can in one line Express something like
988:02 can in one line Express something like this and I admit this looks more cryptic
988:04 this and I admit this looks more cryptic I think than the previous version but as
988:06 I think than the previous version but as you get more comfortable with python or
988:07 you get more comfortable with python or other languages that support this
988:08 other languages that support this feature it allows you to not bother with
988:11 feature it allows you to not bother with lines of code like that and just tighten
988:13 lines of code like that and just tighten up your code a little bit so this line
988:15 up your code a little bit so this line here lamba language colon counts
988:18 here lamba language colon counts language is the oneline version of this
988:21 language is the oneline version of this and you don't even need to bother
988:22 and you don't even need to bother picking a name for it Lambda tells
988:24 picking a name for it Lambda tells python I didn't waste any time thinking
988:26 python I didn't waste any time thinking of a name for this
988:27 of a name for this function so questions then on this
988:30 function so questions then on this technique technique of using python to
988:33 technique technique of using python to analyze data like
988:36 analyze data like this any
988:39 this any questions we're almost done with python
988:43 questions we're almost done with python questions no okay so why don't we make
988:46 questions no okay so why don't we make things a little more interesting because
988:48 things a little more interesting because we had a much juicier data set with the
988:50 we had a much juicier data set with the problems that we've assigned over the
988:52 problems that we've assigned over the past several weeks why don't we go ahead
988:54 past several weeks why don't we go ahead and quite simply you know I think we
988:57 and quite simply you know I think we wrote pretty darn good code here so I
988:59 wrote pretty darn good code here so I think we can pretty much just change a
989:03 think we can pretty much just change a bit of it to say let's see if I don't
989:05 bit of it to say let's see if I don't want language I want problem and if I
989:08 want language I want problem and if I want to sort by not language but problem
989:12 want to sort by not language but problem I think that's it I think if I didn't
989:14 I think that's it I think if I didn't Overlook something here just by changing
989:16 Overlook something here just by changing what column I'm reading the data from
989:18 what column I'm reading the data from and then just to be consistent renaming
989:20 and then just to be consistent renaming my variables just so I know what I'm
989:21 my variables just so I know what I'm looking at what will this program now do
989:24 looking at what will this program now do after those minor
989:26 after those minor changes what will I see when I run
989:29 changes what will I see when I run this
989:32 this what would be the first thing I see when
989:33 what would be the first thing I see when I run
989:40 this tough crowd today yes problem yeah the problem this the top problem so the
989:42 the problem this the top problem so the most popular problem which I'm a little
989:45 most popular problem which I'm a little worried it might be hello or just
989:46 worried it might be hello or just scratch but let's go ahead and see so
989:47 scratch but let's go ahead and see so let me go ahead and open my terminal
989:49 let me go ahead and open my terminal window I'll even maximize my terminal
989:51 window I'll even maximize my terminal window so we can see a lot let me go
989:53 window so we can see a lot let me go ahead and run python of favorites. py
989:56 ahead and run python of favorites. py I'm going to go ahead now and cross my
989:58 I'm going to go ahead now and cross my fingers that I didn't mess up and hit
989:59 fingers that I didn't mess up and hit enter and okay great we peaked early so
990:03 enter and okay great we peaked early so scratch was the most popular program
990:05 scratch was the most popular program according to the data at the time I
990:07 according to the data at the time I downloaded it I'm sure other votes have
990:10 downloaded it I'm sure other votes have come in since filter uh in week four was
990:13 come in since filter uh in week four was tied then with tan as well Mario is a
990:16 tied then with tan as well Mario is a close third there and so forth so this
990:19 close third there and so forth so this is helpful for us on staff that uh not
990:21 is helpful for us on staff that uh not so much love down here at the bottom of
990:23 so much love down here at the bottom of the list so it was a bunch of code to
990:25 the list so it was a bunch of code to write but now that we've written it in
990:26 write but now that we've written it in this very versatile Dynamic way it's
990:28 this very versatile Dynamic way it's pretty good for just like crunching data
990:30 pretty good for just like crunching data and doing some analytics but it's still
990:32 and doing some analytics but it's still a decent number of lines to have had to
990:34 a decent number of lines to have had to write manually and this is where
990:37 write manually and this is where sometimes it isn't necessarily the right
990:39 sometimes it isn't necessarily the right tool for the job but rather the job that
990:44 tool for the job but rather the job that uh but rather a candidate for using some
990:45 uh but rather a candidate for using some other language alog together especially
990:47 other language alog together especially when it's not just a one-time program
990:49 when it's not just a one-time program that you run and you want to see the
990:50 that you run and you want to see the answer what if you want to take input
990:52 answer what if you want to take input from the user and ask uh answer
990:54 from the user and ask uh answer questions dynamically like a mobile app
990:56 questions dynamically like a mobile app would like a website would like
990:57 would like a website would like Microsoft Excel or apple numbers or
990:59 Microsoft Excel or apple numbers or Google Sheets would for for you well
991:01 Google Sheets would for for you well let's make one final change for now to
991:03 let's make one final change for now to this version of the program and actually
991:05 this version of the program and actually take in some user input so besides just
991:08 take in some user input so besides just loading all of the data into memory
991:10 loading all of the data into memory let's go ahead and down below here not
991:14 let's go ahead and down below here not just print out the top 10 list if you
991:16 just print out the top 10 list if you will but prompt the user for their
991:18 will but prompt the user for their favorite so I'm going to use Python's
991:19 favorite so I'm going to use Python's input function and I'm just going to
991:21 input function and I'm just going to prompt them with favorite quote unquote
991:23 prompt them with favorite quote unquote like tell me what your favorite problem
991:25 like tell me what your favorite problem what problem uh rather uh you interested
991:29 what problem uh rather uh you interested in and now let me go ahead and say if
991:32 in and now let me go ahead and say if that favorite is in the counts variable
991:35 that favorite is in the counts variable so you didn't type in something random
991:37 so you didn't type in something random that we didn't actually assign as a
991:38 that we didn't actually assign as a problem then let me go ahead and print
991:40 problem then let me go ahead and print with a format string whatever that
991:43 with a format string whatever that favorite is of yours and show you the
991:46 favorite is of yours and show you the actual popularity thereof by indexing
991:49 actual popularity thereof by indexing into counts using that favorite as the
991:52 into counts using that favorite as the key and printing this so now it's a
991:54 key and printing this so now it's a dynamic program it doesn't dump all of
991:55 dynamic program it doesn't dump all of the data and all of the summations
991:57 the data and all of the summations rather it's going to allow me to see
991:59 rather it's going to allow me to see what my choice of favorite is and I'm
992:01 what my choice of favorite is and I'm going to go ahead and say uh let's see
992:04 going to go ahead and say uh let's see I'm a fan of Mario here so enter and
992:07 I'm a fan of Mario here so enter and indeed we see the same value we saw a
992:09 indeed we see the same value we saw a moment ago but just for Mario but the
992:11 moment ago but just for Mario but the point now is that one all of this is
992:13 point now is that one all of this is possible two it's way easier and more
992:16 possible two it's way easier and more pleasant than this would have been in C
992:18 pleasant than this would have been in C this is still only 15 lines of code and
992:19 this is still only 15 lines of code and in C again there's the memory management
992:21 in C again there's the memory management there's the iterating over the strings
992:23 there's the iterating over the strings trying to find the commas there's just a
992:24 trying to find the commas there's just a lot more work but honestly even when you
992:27 lot more work but honestly even when you just want to answer a question like this
992:28 just want to answer a question like this in Excel in Apple Numbers Google sheets
992:30 in Excel in Apple Numbers Google sheets you know generally you can just
992:31 you know generally you can just highlight things you can click a button
992:33 highlight things you can click a button and boom you get your answer for
992:34 and boom you get your answer for summation or Max or Min or any of those
992:37 summation or Max or Min or any of those sort of Basics wouldn't it be nice if we
992:39 sort of Basics wouldn't it be nice if we weren't taking a step backwards as
992:41 weren't taking a step backwards as programmers and being sort of more
992:42 programmers and being sort of more powerful and yet we now have to do more
992:44 powerful and yet we now have to do more of the work so maybe sometimes Python's
992:47 of the work so maybe sometimes Python's not or any language is not the best tool
992:50 not or any language is not the best tool for the job and that's going to now
992:51 for the job and that's going to now allow us to introduce more generally
992:54 allow us to introduce more generally something called a relational database
992:56 something called a relational database graduating from Mere flat file databases
992:58 graduating from Mere flat file databases like text files or binary files in which
993:00 like text files or binary files in which all of your data is stored to something
993:02 all of your data is stored to something more proper but first
993:17 questions really good question to reiterate if I were to is this case
993:19 reiterate if I were to is this case sensitive so if I were to type in Mario
993:21 sensitive so if I were to type in Mario in all lowercase and hit enter I
993:24 in all lowercase and hit enter I actually get no such response now that
993:26 actually get no such response now that might be acceptable because the problem
993:27 might be acceptable because the problem technically what is a capital M but
993:29 technically what is a capital M but that's a little uh ridiculous to be that
993:31 that's a little uh ridiculous to be that pedantic about the input so how could we
993:34 pedantic about the input so how could we solve
993:35 solve this any tips for how we can make this a
993:38 this any tips for how we can make this a little more robust
993:53 yeah yeah so we could use a few different functions one of which is
993:54 different functions one of which is called title which will change it to
993:55 called title which will change it to title case where it capitalizes like in
993:58 title case where it capitalizes like in most English sentences the first letter
993:59 most English sentences the first letter of that sentence we could use capitalize
994:02 of that sentence we could use capitalize we could use upper we could use lower
994:04 we could use upper we could use lower but indeed we could just decide how we
994:06 but indeed we could just decide how we want to standardize the capitalization
994:08 want to standardize the capitalization either uppercase lowercase or some
994:10 either uppercase lowercase or some combination thereof and just make sure
994:12 combination thereof and just make sure that you change the counts themselves
994:14 that you change the counts themselves make sure that you do the same to
994:15 make sure that you do the same to favorite and make sure that maybe you
994:17 favorite and make sure that maybe you keep a backup of the data if you want to
994:19 keep a backup of the data if you want to show the original version that came from
994:21 show the original version that came from the CSV without presuming to just
994:23 the CSV without presuming to just capitalize everything for the user but
994:25 capitalize everything for the user but indeed that would be the most common
994:27 indeed that would be the most common scenario you just make things case
994:28 scenario you just make things case insensitive when doing those matches
994:31 insensitive when doing those matches other questions now on python before we
994:34 other questions now on python before we leave it behind for the coming
994:38 leave it behind for the coming week all right well then let's introduce
994:40 week all right well then let's introduce these relational databases so relational
994:43 these relational databases so relational database is what like every is a super
994:46 database is what like every is a super popular way of storing lots of data like
994:48 popular way of storing lots of data like this is what the Twitter's of the world
994:49 this is what the Twitter's of the world the Googles of the world the metas of
994:51 the Googles of the world the metas of the world use to store some of their
994:53 the world use to store some of their data at scale there are alternatives to
994:56 data at scale there are alternatives to relational databases um indeed today
994:58 relational databases um indeed today we'll talk about a language called SQL
995:00 we'll talk about a language called SQL there's so a movement if you will or an
995:01 there's so a movement if you will or an alternative generally called No SQL
995:03 alternative generally called No SQL which is just the opposite you don't use
995:05 which is just the opposite you don't use SQL there are things called
995:06 SQL there are things called objectoriented databases and the like
995:08 objectoriented databases and the like but if you've ever heard of MySQL or
995:10 but if you've ever heard of MySQL or postgress SQL or uh Microsoft SQL server
995:14 postgress SQL or uh Microsoft SQL server or Oracle or Maria DB or a bunch of
995:17 or Oracle or Maria DB or a bunch of other products both free and Commercial
995:19 other products both free and Commercial this is what they're talking about
995:21 this is what they're talking about databases that are designed to store
995:23 databases that are designed to store lots of data and what's nice about
995:24 lots of data and what's nice about relational databases is that they're
995:26 relational databases is that they're really similar to the spreadsheets with
995:28 really similar to the spreadsheets with which you were presumably familiar long
995:30 which you were presumably familiar long before today's class so a relational
995:33 before today's class so a relational database is going to store as you'll see
995:35 database is going to store as you'll see all of the data in rows and columns now
995:38 all of the data in rows and columns now the terminology will thereafter be a
995:39 the terminology will thereafter be a little different instead of having
995:40 little different instead of having sheets you're going to have tables but
995:43 sheets you're going to have tables but those tables are still going to have
995:44 those tables are still going to have rows and columns and you're going to
995:45 rows and columns and you're going to have even more control over the
995:47 have even more control over the performance of your data when you start
995:51 performance of your data when you start to access it using this structured query
995:53 to access it using this structured query language or SQL this is a language you
995:56 language or SQL this is a language you can use for web apps mobile apps uh a
995:58 can use for web apps mobile apps uh a lot of analysts would sit down and their
996:00 lot of analysts would sit down and their Mac or PC and actually ask questions of
996:02 Mac or PC and actually ask questions of data to get back the answer and
996:04 data to get back the answer and wonderfully even though there will be
996:06 wonderfully even though there will be some new syntax today SQL really just
996:08 some new syntax today SQL really just does four basic things crud is the sort
996:11 does four basic things crud is the sort of crude acronym here crud is a way of
996:13 of crude acronym here crud is a way of remembering that a relational database
996:16 remembering that a relational database supports ultimately creating data
996:18 supports ultimately creating data reading data updating data and deleting
996:22 reading data updating data and deleting data so even if you're feeling like wow
996:24 data so even if you're feeling like wow this is a lot of new syntax which it
996:25 this is a lot of new syntax which it isn't relative to our past languages the
996:28 isn't relative to our past languages the only things you're doing really are
996:29 only things you're doing really are creating dat data reading data updating
996:32 creating dat data reading data updating and deleting the same now a little
996:34 and deleting the same now a little confusingly in SQL the corresponding
996:37 confusingly in SQL the corresponding functions or commands that exist that
996:40 functions or commands that exist that map to crud are actually this so it's
996:43 map to crud are actually this so it's still create but there's another one
996:45 still create but there's another one called insert uh it's not read which is
996:48 called insert uh it's not read which is more of the computer scientist way of
996:50 more of the computer scientist way of saying it but select which is a little
996:52 saying it but select which is a little more explicit like select data you care
996:53 more explicit like select data you care about update is still update delete is
996:56 about update is still update delete is still delete but there's another command
996:57 still delete but there's another command called drop which lets you drop that is
996:59 called drop which lets you drop that is delete entire tables as well so you can
997:03 delete entire tables as well so you can create tables using syntax that's
997:05 create tables using syntax that's generally going to look like this you'll
997:08 generally going to look like this you'll say create table you'll give the name of
997:10 say create table you'll give the name of the table uh which you can call almost
997:12 the table uh which you can call almost anything you want but generally all
997:14 anything you want but generally all lowercase no spaces is best then in
997:17 lowercase no spaces is best then in parentheses you can specify a comma
997:19 parentheses you can specify a comma separated list of the columns that you
997:21 separated list of the columns that you might want in this table so this is the
997:24 might want in this table so this is the code equivalent in the SQL language of
997:26 code equivalent in the SQL language of like manually opening Google Sheets or
997:28 like manually opening Google Sheets or Excel or numbers and like clicking in
997:30 Excel or numbers and like clicking in the top left cell and like typing
997:32 the top left cell and like typing timestamp and then in the next typing
997:34 timestamp and then in the next typing language and then in the third typing
997:36 language and then in the third typing problem this is the way to sort of
997:37 problem this is the way to sort of Define what your headers are if you will
997:40 Define what your headers are if you will in a spreadsheet but now it's called a
997:41 in a spreadsheet but now it's called a table now we won't use this command
997:43 table now we won't use this command manually first let's do something a
997:45 manually first let's do something a little simpler we're going to start off
997:47 little simpler we're going to start off by just importing this data ourselves
997:49 by just importing this data ourselves and I'm going to go ahead and do this
997:51 and I'm going to go ahead and do this let me go back to vs code here I'm going
997:53 let me go back to vs code here I'm going to leave behind favorites. py for now
997:56 to leave behind favorites. py for now because now we're going to transition to
997:57 because now we're going to transition to this other language called SQL and to do
998:00 this other language called SQL and to do this I am going to create a new database
998:04 this I am going to create a new database file and I'm going to do so using a
998:06 file and I'm going to do so using a command called SQL light 3 which is just
998:09 command called SQL light 3 which is just the third version thereof and I'm going
998:11 the third version thereof and I'm going to give the database a name of
998:13 to give the database a name of favorites. DB there's different
998:15 favorites. DB there's different conventions but this is one of the most
998:16 conventions but this is one of the most common when I hit enter this is going to
998:18 common when I hit enter this is going to create for me a new empty database just
998:21 create for me a new empty database just like opening an Untitled spreadsheet in
998:22 like opening an Untitled spreadsheet in Excel Google Sheets or Apple Numbers I'm
998:25 Excel Google Sheets or Apple Numbers I'm being prompted do I want to create
998:27 being prompted do I want to create favorites. DB I'll hit y for yes okay
998:30 favorites. DB I'll hit y for yes okay we're up and running now you're going to
998:31 we're up and running now you're going to notice a different prompt I'm not in my
998:34 notice a different prompt I'm not in my Linux prompt per se which is always the
998:35 Linux prompt per se which is always the dollar sign I'm now inside of the
998:38 dollar sign I'm now inside of the program called SQL light and we're going
998:39 program called SQL light and we're going to use SQL light SQL light 3 as just an
998:42 to use SQL light SQL light 3 as just an interactive way for now of playing with
998:44 interactive way for now of playing with SQL code at the end of today we'll show
998:47 SQL code at the end of today we'll show you how you can use SQL in Python code
998:51 you how you can use SQL in Python code so that you still write python code to
998:52 so that you still write python code to do whatever you want but you can talk to
998:54 do whatever you want but you can talk to databases using Python and this is
998:56 databases using Python and this is exactly how web apps mobile apps work
998:58 exactly how web apps mobile apps work for instance on iOS uh and an i phone an
999:00 for instance on iOS uh and an i phone an iPad or the like if you want to store
999:02 iPad or the like if you want to store data it's very often stored in a SQL
999:04 data it's very often stored in a SQL database as we're about to do um but you
999:07 database as we're about to do um but you might use a language called Swift or
999:09 might use a language called Swift or objective c and same exists in the world
999:10 objective c and same exists in the world of Android using Java or cotlin or
999:13 of Android using Java or cotlin or something else to query the database so
999:15 something else to query the database so we're going to see SQL in isolation for
999:17 we're going to see SQL in isolation for now like an analyst might just use at
999:18 now like an analyst might just use at their Mac or PC but we're going to tie
999:20 their Mac or PC but we're going to tie it together by Day end so at this
999:23 it together by Day end so at this terminal uh SQL light let me go ahead
999:27 terminal uh SQL light let me go ahead and execute uh this command first I'm
999:30 and execute uh this command first I'm going to first put SQL light into CSV
999:32 going to first put SQL light into CSV mode because I'm going to cut some
999:33 mode because I'm going to cut some Corners initially and I'm just going to
999:35 Corners initially and I'm just going to automatically import all of the data
999:37 automatically import all of the data that was submitted via that Google form
999:39 that was submitted via that Google form which I exported as a CSV and uploaded
999:42 which I exported as a CSV and uploaded to my code space and I'm just going to
999:44 to my code space and I'm just going to automatically say turn this CSV file
999:46 automatically say turn this CSV file into a SQL database for me just so I
999:49 into a SQL database for me just so I don't have to figure out what those
999:50 don't have to figure out what those create table commands are so to do this
999:52 create table commands are so to do this I'm going to say mode CSV so that SQL
999:56 I'm going to say mode CSV so that SQL light knows that this is the command uh
999:58 light knows that this is the command uh knows that this is a CSV file it's
1000:00 knows that this is a CSV file it's literally mode so the dot comes before
1000:02 literally mode so the dot comes before the keyword there and now I'm going to
1000:04 the keyword there and now I'm going to say do import and then the name of the
1000:07 say do import and then the name of the file I want to import which is
1000:08 file I want to import which is favorites. CSV and now the name of the
1000:12 favorites. CSV and now the name of the table that I want to create with that
1000:14 table that I want to create with that data and just for consistency I'm going
1000:16 data and just for consistency I'm going to call it favorites I could change
1000:17 to call it favorites I could change these things to be anything I want but
1000:19 these things to be anything I want but I'm going to do that and voila nothing
1000:22 I'm going to do that and voila nothing seems to have happened but just like in
1000:24 seems to have happened but just like in C and in Python in Linux when nothing
1000:26 C and in Python in Linux when nothing seems to happen that's usually a good
1000:27 seems to happen that's usually a good thing it means I didn't mess up so if I
1000:30 thing it means I didn't mess up so if I want to see what just happened there's
1000:32 want to see what just happened there's this other command and these commands
1000:34 this other command and these commands that start with dots these are SQL light
1000:37 that start with dots these are SQL light specific which is indeed a lightweight
1000:38 specific which is indeed a lightweight version of SQL they're not SQL per se so
1000:41 version of SQL they're not SQL per se so if you're using Oracle or something else
1000:43 if you're using Oracle or something else like that you're not going to use these
1000:44 like that you're not going to use these exact commands you'll see the ones we
1000:46 exact commands you'll see the ones we use in just a moment and here's the
1000:48 use in just a moment and here's the first when I type schema the schema of a
1000:51 first when I type schema the schema of a database is the design of the database
1000:53 database is the design of the database what are the tables what are the columns
1000:55 what are the tables what are the columns and all of that so when I type schema
1000:57 and all of that so when I type schema this actually in this case shows me that
1001:00 this actually in this case shows me that create table command that was
1001:02 create table command that was automatically run for me by just doing
1001:05 automatically run for me by just doing this import line once I get more
1001:06 this import line once I get more comfortable with SQL I could literally
1001:08 comfortable with SQL I could literally type this out myself or use some program
1001:10 type this out myself or use some program to generate that as well but what it's
1001:12 to generate that as well but what it's creating for me is this create table if
1001:15 creating for me is this create table if it doesn't exist even though it's more
1001:16 it doesn't exist even though it's more tur than that I want to create a table
1001:19 tur than that I want to create a table called favorites and then the columns
1001:21 called favorites and then the columns for that table are going to be timestamp
1001:24 for that table are going to be timestamp which is going to be text comma language
1001:27 which is going to be text comma language which is also going to be text comma
1001:29 which is also going to be text comma problem which is also going to be text
1001:31 problem which is also going to be text that was just inferred very trivially by
1001:33 that was just inferred very trivially by the do import command to just figure out
1001:36 the do import command to just figure out that yes just give me a three column
1001:38 that yes just give me a three column database table based on the Google
1001:41 database table based on the Google form okay questions on this these are
1001:44 form okay questions on this these are commands you run once to get up and
1001:46 commands you run once to get up and running you don't run these commands
1001:48 running you don't run these commands frequently but we have them on the slide
1001:50 frequently but we have them on the slide just for reference all right so now
1001:52 just for reference all right so now let's do something a little more
1001:53 let's do something a little more interesting I'm going to clear my SQL
1001:55 interesting I'm going to clear my SQL light terminal here but I'm still in SQL
1001:57 light terminal here but I'm still in SQL light I'm going to now use some of my
1001:59 light I'm going to now use some of my first SQL commands which recall were uh
1002:02 first SQL commands which recall were uh were among them uh select so crud c r UD
1002:06 were among them uh select so crud c r UD D the r was select this is maybe the
1002:08 D the r was select this is maybe the most common the most useful the most
1002:10 most common the most useful the most powerful thing to use with a SQL
1002:12 powerful thing to use with a SQL database selecting data to answer
1002:14 database selecting data to answer questions akin to the ones we were
1002:15 questions akin to the ones we were trying to answer with python this is the
1002:17 trying to answer with python this is the general syntax anytime you want to
1002:19 general syntax anytime you want to select data from a SQL database you
1002:21 select data from a SQL database you literally say select you then specify
1002:23 literally say select you then specify the column or columns that you want to
1002:25 the column or columns that you want to select data from you literally write the
1002:28 select data from you literally write the word from and then you specify the name
1002:30 word from and then you specify the name of the table you want to get that data
1002:32 of the table you want to get that data from semicolon in this case everything
1002:34 from semicolon in this case everything that's in capitals here is a sequel
1002:37 that's in capitals here is a sequel keyword strictly speaking you don't have
1002:38 keyword strictly speaking you don't have to capitalize things but we would
1002:40 to capitalize things but we would encourage you to do so stylistically and
1002:42 encourage you to do so stylistically and especially as you're learning and even
1002:44 especially as you're learning and even as you're writing it it just helps to
1002:46 as you're writing it it just helps to distinguish SQL from like words you
1002:48 distinguish SQL from like words you chose like the names of the columns and
1002:50 chose like the names of the columns and the data there're in so uh do adopt
1002:53 the data there're in so uh do adopt early on this convention so let me go
1002:55 early on this convention so let me go back now to my code space here I'm
1002:58 back now to my code space here I'm running my terminal window with SQL
1003:00 running my terminal window with SQL light 3 inside of it suppose that I just
1003:02 light 3 inside of it suppose that I just want to get all of the data from the
1003:04 want to get all of the data from the favorites table which was automatically
1003:06 favorites table which was automatically imported let's do this select I want
1003:09 imported let's do this select I want everything well I can do timestamp comma
1003:13 everything well I can do timestamp comma language comma problem but you know what
1003:16 language comma problem but you know what here's a uh convenience already if you
1003:18 here's a uh convenience already if you want everything there's what's called a
1003:20 want everything there's what's called a wild card character in SQL which is just
1003:22 wild card character in SQL which is just a star in asterisk which means give me
1003:24 a star in asterisk which means give me every column without my knowing even
1003:26 every column without my knowing even what they're called let me go ahead now
1003:28 what they're called let me go ahead now and say from favorite it's semicolon and
1003:31 and say from favorite it's semicolon and this is the SQL way of opening the
1003:33 this is the SQL way of opening the database iterating over every row
1003:35 database iterating over every row they're in printing out every row
1003:37 they're in printing out every row therein done so those three steps which
1003:40 therein done so those three steps which was like nine lines of python code give
1003:42 was like nine lines of python code give or take earlier is now one line of SQL I
1003:45 or take earlier is now one line of SQL I hit enter there is all of the data so I
1003:49 hit enter there is all of the data so I see now all of the data just outputed as
1003:51 see now all of the data just outputed as a CSV here but it's not the CSV file
1003:54 a CSV here but it's not the CSV file it's now actually the table and in fact
1003:55 it's now actually the table and in fact just for good measure let me do this CU
1003:57 just for good measure let me do this CU you'll see the behavior a little
1003:59 you'll see the behavior a little different the next time we open the file
1004:01 different the next time we open the file I've just exited out of SQL light 3 I'm
1004:03 I've just exited out of SQL light 3 I'm going to rerun it but I'm not going to
1004:04 going to rerun it but I'm not going to reimport the data or do anything like
1004:06 reimport the data or do anything like that because my file now exists in fact
1004:08 that because my file now exists in fact let me take one step back if I type LS
1004:10 let me take one step back if I type LS at my Linux prompt there's my favorites.
1004:13 at my Linux prompt there's my favorites. py from before there's my favorites. CSV
1004:15 py from before there's my favorites. CSV from before and here's a third file that
1004:17 from before and here's a third file that I did create a moment ago when I first
1004:20 I did create a moment ago when I first ran SQL light 3 so the data is
1004:22 ran SQL light 3 so the data is persistent it's not using Ram or memory
1004:25 persistent it's not using Ram or memory anything I do now is save there so let's
1004:27 anything I do now is save there so let's go ahead and rerun SQL light 3 with the
1004:29 go ahead and rerun SQL light 3 with the same file but I'm not going to I don't
1004:31 same file but I'm not going to I don't have to reort everything because the
1004:32 have to reort everything because the file already exists let me now do that
1004:34 file already exists let me now do that same thing again select star from
1004:36 same thing again select star from favorites to get all of the data and
1004:39 favorites to get all of the data and what you'll see now is the same data but
1004:40 what you'll see now is the same data but it's a little prettier now because I
1004:42 it's a little prettier now because I reran it I effectively disabled CSV mode
1004:46 reran it I effectively disabled CSV mode this time and what I'm now seeing is the
1004:48 this time and what I'm now seeing is the entire contents of this database table
1004:52 entire contents of this database table called favorit now there's nothing new
1004:54 called favorit now there's nothing new here but you're just seeing now like an
1004:56 here but you're just seeing now like an asky or Unicode version of all of the
1004:58 asky or Unicode version of all of the same data from that database well
1005:00 same data from that database well suppose I want to get a subset of the
1005:02 suppose I want to get a subset of the data well let me clear my screen and
1005:03 data well let me clear my screen and just like in Linux I can hit contrl L
1005:05 just like in Linux I can hit contrl L just to clean things up aesthetically
1005:07 just to clean things up aesthetically suppose I want to get just the languages
1005:08 suppose I want to get just the languages so I could do select language from
1005:11 so I could do select language from favorites and this will now select not
1005:13 favorites and this will now select not all three columns AKA star this will
1005:16 all three columns AKA star this will only select the language column and all
1005:18 only select the language column and all of the data they're in if I hit enter
1005:21 of the data they're in if I hit enter voila now I just see those there no time
1005:24 voila now I just see those there no time Stamps no problems it's just a slice of
1005:26 Stamps no problems it's just a slice of the table if you will all right not that
1005:28 the table if you will all right not that interesting still still because it's
1005:30 interesting still still because it's just a big column of data but now things
1005:33 just a big column of data but now things get more interesting it turns out in SQL
1005:36 get more interesting it turns out in SQL that there are functions that come with
1005:37 that there are functions that come with this language just like C just like
1005:39 this language just like C just like python in SQL some of the more useful
1005:42 python in SQL some of the more useful ones some of the simpler ones are these
1005:44 ones some of the simpler ones are these here average count distinct lower Max
1005:47 here average count distinct lower Max Min upper which pretty much uh do what
1005:49 Min upper which pretty much uh do what they say and count is a particularly
1005:52 they say and count is a particularly useful one let's start with that you
1005:53 useful one let's start with that you know it's a reasonable question to be
1005:55 know it's a reasonable question to be asked uh how many people submitted the
1005:57 asked uh how many people submitted the Google form by the time I actually
1005:59 Google form by the time I actually downloaded the CSV well why don't we go
1006:01 downloaded the CSV well why don't we go ahead and do this let me go back to VSS
1006:03 ahead and do this let me go back to VSS code here in my terminal window let me
1006:05 code here in my terminal window let me select not star but the count of star so
1006:09 select not star but the count of star so give me the count of the rows that are
1006:12 give me the count of the rows that are being returned from the database called
1006:14 being returned from the database called uh the database table called favorites
1006:16 uh the database table called favorites now when I hit enter I'm not going to
1006:17 now when I hit enter I'm not going to get all the data I'm just going to get
1006:19 get all the data I'm just going to get simply a number 430 rows came back so
1006:24 simply a number 430 rows came back so that's pretty good I now know how much
1006:26 that's pretty good I now know how much data is in there well what languages
1006:28 data is in there well what languages were in there well I could do select
1006:30 were in there well I could do select language from favorit just as before but
1006:33 language from favorit just as before but that's not that useful especially if I'm
1006:34 that's not that useful especially if I'm inheriting the data like I'm the analyst
1006:36 inheriting the data like I'm the analyst who's been handed a data set by my boss
1006:38 who's been handed a data set by my boss and they want me to like crunch some
1006:39 and they want me to like crunch some numbers okay I could like load this into
1006:41 numbers okay I could like load this into Excel I could sort it but you can use
1006:44 Excel I could sort it but you can use SQL now to answer pretty basic questions
1006:46 SQL now to answer pretty basic questions too if you want to select the distinct
1006:48 too if you want to select the distinct languages in the data set because you
1006:50 languages in the data set because you didn't you weren't privy to the Google
1006:52 didn't you weren't privy to the Google form let me go ahead and select only the
1006:55 form let me go ahead and select only the distinct languages from the favorites
1006:58 distinct languages from the favorites table and now I hit enter and I get back
1007:01 table and now I hit enter and I get back a much more succinct answer just the
1007:03 a much more succinct answer just the three languages in question not really
1007:05 three languages in question not really that useful since I created the Google
1007:07 that useful since I created the Google form but certainly if you're inheriting
1007:08 form but certainly if you're inheriting data from someone else you've just
1007:10 data from someone else you've just downloaded a data set at least now I'm
1007:11 downloaded a data set at least now I'm arguably wrapping my mind around what's
1007:13 arguably wrapping my mind around what's going on now this is not necessary for
1007:16 going on now this is not necessary for such a small data set but I can combine
1007:18 such a small data set but I can combine these things select the count of the
1007:20 these things select the count of the distinct languages in this data set
1007:23 distinct languages in this data set called favorites and now I should get
1007:25 called favorites and now I should get back what
1007:26 back what answer so hopefully indeed an answer
1007:29 answer so hopefully indeed an answer called three and what you're getting
1007:30 called three and what you're getting back notice aesthetically too is like a
1007:33 back notice aesthetically too is like a mini temporary table when I asked for uh
1007:36 mini temporary table when I asked for uh just the distinct languages what SQL
1007:38 just the distinct languages what SQL hands me back is this temporary table in
1007:41 hands me back is this temporary table in memory that has one column called
1007:43 memory that has one column called language and then two row uh three rows
1007:45 language and then two row uh three rows now this is not saved anywhere it's just
1007:48 now this is not saved anywhere it's just executed ephemerally like this but
1007:50 executed ephemerally like this but that's why it's depicted in this way
1007:51 that's why it's depicted in this way what you're getting is subsets of your
1007:54 what you're getting is subsets of your data smaller tables containing some of
1007:56 data smaller tables containing some of your data and same thing down here this
1007:58 your data and same thing down here this is like a crazy long
1007:59 is like a crazy long uh column name you can rename it if you
1008:01 uh column name you can rename it if you really want uh but uh that's all we're
1008:04 really want uh but uh that's all we're seeing there and in fact if that's a
1008:06 seeing there and in fact if that's a little ugly we can actually Alias These
1008:09 little ugly we can actually Alias These Things N is a common uh name for a
1008:12 Things N is a common uh name for a variable a number in any programming
1008:14 variable a number in any programming language so I can actually alas this to
1008:15 language so I can actually alas this to be a column called n hit enter and now
1008:19 be a column called n hit enter and now I'm getting a tiny tiny table whose
1008:21 I'm getting a tiny tiny table whose column is called n that just has the one
1008:23 column is called n that just has the one value
1008:24 value there all right questions on
1008:28 there all right questions on these application of these functions
1008:36 here no questions yeah say a little louder as oh as as
1008:40 yeah say a little louder as oh as as literally in English so name this column
1008:43 literally in English so name this column rename this column as this technically
1008:45 rename this column as this technically it creates an alias for the column so
1008:47 it creates an alias for the column so that's all
1008:54 yeah exactly distinct will operate on whatever you handed in parentheses and
1008:56 whatever you handed in parentheses and get rid of all of the duplicates giving
1008:57 get rid of all of the duplicates giving you back just the unique
1008:59 you back just the unique correct other questions here
1009:11 yeah good question when you define an alias like n which I just did does it
1009:13 alias like n which I just did does it become like a variable you can reuse
1009:14 become like a variable you can reuse short answer no in this case but you can
1009:16 short answer no in this case but you can reuse it within your same query even
1009:19 reuse it within your same query even though these these queries are getting a
1009:21 though these these queries are getting a little longer admittedly statements that
1009:23 little longer admittedly statements that they are uh you can actually reuse n in
1009:27 they are uh you can actually reuse n in even longer queries so later in your
1009:29 even longer queries so later in your query and we'll see a few that are start
1009:30 query and we'll see a few that are start to going to start to grow in length so
1009:32 to going to start to grow in length so it's a a nice way of nicknaming things
1009:35 it's a a nice way of nicknaming things just to be a little more tur in your
1009:36 just to be a little more tur in your query so we can transition to some of
1009:39 query so we can transition to some of these more sophisticated queries because
1009:41 these more sophisticated queries because it turns out there's some other uh
1009:43 it turns out there's some other uh techniques we can introduce as well here
1009:45 techniques we can introduce as well here are some other keywords in SQL and again
1009:47 are some other keywords in SQL and again even though like this is another list of
1009:49 even though like this is another list of things there's only four things
1009:50 things there's only four things fundamentally we're doing creating
1009:51 fundamentally we're doing creating reading updating and deleting data these
1009:54 reading updating and deleting data these are just allowing us to like fine-tune
1009:56 are just allowing us to like fine-tune how we do it exactly so where is going
1009:59 how we do it exactly so where is going to allow us to filter data as we'll do
1010:00 to allow us to filter data as we'll do in just a moment like select data where
1010:03 in just a moment like select data where this conditional is true uh like is
1010:06 this conditional is true uh like is going to be an alternative to an equal
1010:08 going to be an alternative to an equal sign so instead of looking for exactly
1010:10 sign so instead of looking for exactly scratch or exactly python or exactly C
1010:13 scratch or exactly python or exactly C you can look for something like dot dot
1010:15 you can look for something like dot dot dot and it can be a little bit of a
1010:17 dot and it can be a little bit of a fuzzier match if you will uh with other
1010:19 fuzzier match if you will uh with other characters as well order by is going to
1010:21 characters as well order by is going to deal with sorting limit is going to just
1010:23 deal with sorting limit is going to just let me limit the total number of rows
1010:25 let me limit the total number of rows that come back to one or 10 or finite if
1010:28 that come back to one or 10 or finite if I don't want to see all 400 plus rows
1010:29 I don't want to see all 400 plus rows all at once because I'm just trying to
1010:31 all at once because I'm just trying to wrap my mind around it and group by is
1010:33 wrap my mind around it and group by is best shown by example so let's play with
1010:35 best shown by example so let's play with just a couple of these as well let me go
1010:37 just a couple of these as well let me go back to vs code here I'll clear my
1010:39 back to vs code here I'll clear my screen I'm still in the same SQL light
1010:41 screen I'm still in the same SQL light instance and let's count how many of you
1010:43 instance and let's count how many of you likeed C without writing python code as
1010:45 likeed C without writing python code as before so let me go ahead and select the
1010:48 before so let me go ahead and select the count of the rows from favorites where
1010:53 count of the rows from favorites where the language in each row equals c and
1010:57 the language in each row equals c and the convention in SQL light is to use
1010:58 the convention in SQL light is to use single quotes anytime you're surrounding
1011:01 single quotes anytime you're surrounding a string that's meant to represent a
1011:03 a string that's meant to represent a literal piece of text uh as opposed to C
1011:06 literal piece of text uh as opposed to C which was double quotes or python which
1011:08 which was double quotes or python which was either so this is selecting the
1011:10 was either so this is selecting the count of rows from favorites table where
1011:13 count of rows from favorites table where the language in question is C enter and
1011:16 the language in question is C enter and this gives me 98 notice though if I omit
1011:19 this gives me 98 notice though if I omit that predicate like we did before you'll
1011:22 that predicate like we did before you'll get back the total number of rows that
1011:25 get back the total number of rows that were in the table so where is what's
1011:27 were in the table so where is what's called a predicate that just allows me
1011:28 called a predicate that just allows me to filter things just like an if
1011:30 to filter things just like an if condition or the like in a language that
1011:32 condition or the like in a language that we've seen before you can be a little
1011:34 we've seen before you can be a little more specific like how many people
1011:36 more specific like how many people really liked C and the Mario program uh
1011:40 really liked C and the Mario program uh problem specifically well let's do this
1011:42 problem specifically well let's do this uh let's go ahead and do select the
1011:44 uh let's go ahead and do select the number of rows from the favorites table
1011:48 number of rows from the favorites table where the language is C and so it's uh
1011:53 where the language is C and so it's uh still literally the word ands and or
1011:55 still literally the word ands and or just like in Python but not like in C uh
1011:58 just like in Python but not like in C uh and
1011:59 and equals Mario so let's see if there's any
1012:01 equals Mario so let's see if there's any fans of both C and the Mario problem and
1012:04 fans of both C and the Mario problem and three of us really like those two things
1012:06 three of us really like those two things together in this case all right what
1012:08 together in this case all right what else can we do well more compelling
1012:10 else can we do well more compelling might be to see kind of like in Python
1012:12 might be to see kind of like in Python for each language what was the
1012:14 for each language what was the popularity thereof and at the moment we
1012:16 popularity thereof and at the moment we don't really have a way of doing that
1012:18 don't really have a way of doing that except in Python where we had the loop
1012:20 except in Python where we had the loop and we had those variables with the
1012:21 and we had those variables with the dictionary that did all that counting
1012:22 dictionary that did all that counting for us you know totally doable but
1012:24 for us you know totally doable but tedious especially if your job is to
1012:26 tedious especially if your job is to analyze data my God like even writing 15
1012:28 analyze data my God like even writing 15 lines of code to answer simple questions
1012:30 lines of code to answer simple questions is kind of ridiculous SQL can do better
1012:33 is kind of ridiculous SQL can do better for us so let me go ahead and do this
1012:36 for us so let me go ahead and do this let me go ahead and select every
1012:38 let me go ahead and select every language and the count thereof from the
1012:43 language and the count thereof from the favorites table but this time Group by
1012:47 favorites table but this time Group by language so this was another one of the
1012:50 language so this was another one of the keywords that we can use in this
1012:52 keywords that we can use in this abbreviated list of extra features of
1012:54 abbreviated list of extra features of SQL and this one's a little takes a
1012:56 SQL and this one's a little takes a moment to wrap your mind around but this
1012:58 moment to wrap your mind around but this is going to give me a two column
1013:00 is going to give me a two column temporary table where the First Column
1013:02 temporary table where the First Column is a language and the second column is
1013:04 is a language and the second column is the count thereof from this data set and
1013:08 the count thereof from this data set and group by language just means that only
1013:10 group by language just means that only show me scratch once only show me c once
1013:13 show me scratch once only show me c once only show me python once that is group
1013:15 only show me python once that is group all of the identical values together but
1013:18 all of the identical values together but keep track of how many of them there are
1013:21 keep track of how many of them there are and so now if I go over to SQL light and
1013:25 and so now if I go over to SQL light and I hit enter now I have in SQL version
1013:29 I hit enter now I have in SQL version the exact same output that I had from
1013:31 the exact same output that I had from python that took me what 15 plus lines
1013:34 python that took me what 15 plus lines before now we're down to just one
1013:36 before now we're down to just one because SQL structured query language is
1013:38 because SQL structured query language is all about constructing queries like this
1013:40 all about constructing queries like this to answer questions and get back answers
1013:43 to answer questions and get back answers quickly if we want to clean this up a
1013:44 quickly if we want to clean this up a little bit you asked earlier about
1013:46 little bit you asked earlier about sorting order well we can do that too
1013:48 sorting order well we can do that too there's another uh key phrase we can use
1013:50 there's another uh key phrase we can use here we can order by the count of those
1013:54 here we can order by the count of those rows and then run that query here so now
1013:57 rows and then run that query here so now unfortunately they're from smallest to
1013:59 unfortunately they're from smallest to biggest but we can reverse that it turns
1014:01 biggest but we can reverse that it turns out and my query is starting to wrap
1014:03 out and my query is starting to wrap here I'll I'll zoom out for a moment if
1014:05 here I'll I'll zoom out for a moment if you want to order by count the default
1014:08 you want to order by count the default is in ascending order abbreviated ASC if
1014:11 is in ascending order abbreviated ASC if you want to reverse the sort in SQL
1014:13 you want to reverse the sort in SQL instead of using reverse equals true
1014:15 instead of using reverse equals true like we did in Python you say DC for
1014:17 like we did in Python you say DC for descending order and now we get almost
1014:19 descending order and now we get almost the same output but flipped in Reverse
1014:22 the same output but flipped in Reverse so it's just a lot faster to answer
1014:23 so it's just a lot faster to answer questions once of course you get some
1014:24 questions once of course you get some muscle memory and some comfort with it
1014:27 muscle memory and some comfort with it well what else can I do you know what if
1014:28 well what else can I do you know what if I just care about the most popular
1014:30 I just care about the most popular language I don't care about the second
1014:32 language I don't care about the second place or the third place languages or
1014:34 place or the third place languages or anything else well let me add one more
1014:36 anything else well let me add one more Clause here limit the answer to one and
1014:39 Clause here limit the answer to one and no matter how many rows should come back
1014:41 no matter how many rows should come back now I just get the number one language
1014:43 now I just get the number one language as of the data set we collected with 27
1014:46 as of the data set we collected with 27 uh 270 votes for
1014:49 uh 270 votes for it
1014:51 it questions on
1014:53 questions on this any questions
1014:56 this any questions here no well what if uh you know we're
1014:59 here no well what if uh you know we're starting to introduce SQL and it was
1015:01 starting to introduce SQL and it was kind of too late to make it into the
1015:03 kind of too late to make it into the this the Google form so it turns out
1015:05 this the Google form so it turns out there Syntax for this too you can create
1015:07 there Syntax for this too you can create data of course not just the tables but
1015:08 data of course not just the tables but the data they're in and here's like the
1015:10 the data they're in and here's like the typical Syntax for inserting data into a
1015:13 typical Syntax for inserting data into a SQL database you literally say insert
1015:15 SQL database you literally say insert into the name of the table and then in
1015:17 into the name of the table and then in parentheses you specify one or more
1015:20 parentheses you specify one or more columns for which you have values that
1015:22 columns for which you have values that you want to insert this is to say you
1015:24 you want to insert this is to say you don't have to give values for every
1015:26 don't have to give values for every column in the given row if you only have
1015:28 column in the given row if you only have answers to some of those questions you
1015:30 answers to some of those questions you can enumerate them here like this but
1015:32 can enumerate them here like this but the values you insert are going to be
1015:34 the values you insert are going to be these so you literally say after the
1015:35 these so you literally say after the closed parenthesis values and then in a
1015:38 closed parenthesis values and then in a second set of parentheses with the same
1015:40 second set of parentheses with the same length comma separated list you specify
1015:42 length comma separated list you specify what values do you want to insert so
1015:44 what values do you want to insert so it's a little verbose and frankly longer
1015:47 it's a little verbose and frankly longer term you're going to use like python
1015:48 term you're going to use like python code to automatically do these kinds of
1015:50 code to automatically do these kinds of insertions but let's go ahead and try
1015:52 insertions but let's go ahead and try this right now if I do select
1015:55 this right now if I do select distinct uh language from favorites
1015:59 distinct uh language from favorites again we see this just these three
1016:01 again we see this just these three candidates but we've now taught you a
1016:03 candidates but we've now taught you a bit of sequel so let's do insert into
1016:06 bit of sequel so let's do insert into favorites the column called
1016:10 favorites the column called language uh and you know what let's I'm
1016:13 language uh and you know what let's I'm going to give a problem here the values
1016:15 going to give a problem here the values for which and let me Zoom back out are
1016:16 for which and let me Zoom back out are going to be quote unquote SQL and quote
1016:19 going to be quote unquote SQL and quote unquote 50v you'll see soon see what
1016:21 unquote 50v you'll see soon see what that's all about semicolon nothing seems
1016:24 that's all about semicolon nothing seems to happen but that's usually a good
1016:26 to happen but that's usually a good thing and now if I scroll back up in my
1016:28 thing and now if I scroll back up in my my queries in SQL light three you can
1016:29 my queries in SQL light three you can scroll back and forth in time and uh to
1016:32 scroll back and forth in time and uh to avoid retyping things now I should see
1016:35 avoid retyping things now I should see indeed four candidate languages here now
1016:38 indeed four candidate languages here now suppose that you were never really a fan
1016:39 suppose that you were never really a fan of c and maybe you uh programmed a
1016:41 of c and maybe you uh programmed a little bit in high school or in the real
1016:43 little bit in high school or in the real world and you liked C++ well there's a
1016:46 world and you liked C++ well there's a whole lot of answers for C so select
1016:50 whole lot of answers for C so select star from favorites where language
1016:54 star from favorites where language equals quote unquote C so here's
1016:56 equals quote unquote C so here's everyone who submitted the answer for C
1016:59 everyone who submitted the answer for C let's presume that no they didn't really
1017:00 let's presume that no they didn't really want C they wanted C++ which is not a
1017:02 want C they wanted C++ which is not a language we teach in the class but I
1017:04 language we teach in the class but I could also now do this you can use the
1017:07 could also now do this you can use the update command to set a column or
1017:09 update command to set a column or columns to different values where some
1017:11 columns to different values where some condition is met so if I do update table
1017:14 condition is met so if I do update table name set column name equal to some value
1017:17 name set column name equal to some value filtering it perhaps by where some
1017:20 filtering it perhaps by where some condition is true so suppose I've
1017:22 condition is true so suppose I've changed my mind or you know what let's
1017:25 changed my mind or you know what let's go ahead and do
1017:27 go ahead and do update uh favorites set language equal
1017:31 update uh favorites set language equal to maybe C++ where language equals c now
1017:36 to maybe C++ where language equals c now this is destructive so you generally
1017:38 this is destructive so you generally don't want to do this unless you have a
1017:39 don't want to do this unless you have a backup of your data too overriding what
1017:41 backup of your data too overriding what people's answers are this seems to have
1017:43 people's answers are this seems to have been successful because no error
1017:44 been successful because no error messages and if I rerun the previous
1017:46 messages and if I rerun the previous select that gives me all of the
1017:48 select that gives me all of the favorites where language equals c now
1017:50 favorites where language equals c now indeed I get none but if I search for
1017:53 indeed I get none but if I search for C++ now I get a lot and if I get rid of
1017:56 C++ now I get a lot and if I get rid of that wear Clause altogether and just
1017:57 that wear Clause altogether and just look at the contents of my database now
1018:00 look at the contents of my database now you see that indeed C++ is comingled
1018:02 you see that indeed C++ is comingled with all the other data this is not what
1018:04 with all the other data this is not what you all intended of course so I can undo
1018:06 you all intended of course so I can undo this let me go ahead and undo what I
1018:08 this let me go ahead and undo what I just did let me set my favorite language
1018:10 just did let me set my favorite language to C where language equals C plus plus
1018:15 to C where language equals C plus plus but the predicate is important this I'm
1018:17 but the predicate is important this I'm not going to do what if I accidentally
1018:20 not going to do what if I accidentally omitted this predicate the wear Clause
1018:22 omitted this predicate the wear Clause how would that screw things up might you
1018:30 think uh yeah I'm back it would set Every Rose language to
1018:34 back it would set Every Rose language to indeed see and this is dangerous and if
1018:36 indeed see and this is dangerous and if you start googling around for like
1018:38 you start googling around for like sequel mistakes or the like people in
1018:40 sequel mistakes or the like people in the real world have accidentally run
1018:43 the real world have accidentally run commands like this and without naming
1018:44 commands like this and without naming names a former member of our teaching
1018:46 names a former member of our teaching staff at one point accidentally re ran a
1018:48 staff at one point accidentally re ran a command like this and changed every
1018:50 command like this and changed every student's name in our database to Bobby
1018:53 student's name in our database to Bobby I think it was the same name for every
1018:55 I think it was the same name for every row because they simply forgot a
1018:57 row because they simply forgot a predicate so here to two like there's
1018:59 predicate so here to two like there's dangers in code and you should adopt The
1019:01 dangers in code and you should adopt The Habit quite quickly of always one
1019:03 Habit quite quickly of always one backing up your data like with CP for
1019:05 backing up your data like with CP for instance in Linux or any other technique
1019:08 instance in Linux or any other technique or just making sure before you hit enter
1019:11 or just making sure before you hit enter that yes this is indeed the query I want
1019:12 that yes this is indeed the query I want to execute and generally speaking in the
1019:14 to execute and generally speaking in the real world there should be process
1019:16 real world there should be process controls in place like the intern should
1019:18 controls in place like the intern should not have access to the datab the
1019:19 not have access to the datab the production database the live database
1019:21 production database the live database and the like but you have a lot of power
1019:23 and the like but you have a lot of power now with these queries so just be all
1019:24 now with these queries so just be all the more careful cuz very easily can you
1019:26 the more careful cuz very easily can you do bad things so let me undo this where
1019:29 do bad things so let me undo this where language equals quote unquote C++ and
1019:33 language equals quote unquote C++ and I'll Zoom back out enter and now I think
1019:35 I'll Zoom back out enter and now I think we're back in business C is among the
1019:37 we're back in business C is among the answers
1019:46 yeah is essentially doing what at the end replace it's essentially find and
1019:48 end replace it's essentially find and replace yes in like lay person's terms
1019:50 replace yes in like lay person's terms this is find and replace implemented
1019:52 this is find and replace implemented with SQL and in fact the authors of
1019:55 with SQL and in fact the authors of Microsoft Word or Google Docs might very
1019:58 Microsoft Word or Google Docs might very well be using language like this sequel
1020:00 well be using language like this sequel when you go to the nice graphical
1020:02 when you go to the nice graphical userfriendly find and replace box this
1020:04 userfriendly find and replace box this may very well be what they're doing
1020:06 may very well be what they're doing underneath the hood or of course they
1020:07 underneath the hood or of course they could be using some other language
1020:09 could be using some other language altogether there's one last uh syntax
1020:12 altogether there's one last uh syntax that's worth knowing delete which for
1020:13 that's worth knowing delete which for better for worse is even more
1020:15 better for worse is even more destructive whereby it allows you to
1020:17 destructive whereby it allows you to delete rows from tables it's distinct
1020:19 delete rows from tables it's distinct from drop which lets you delete tables
1020:21 from drop which lets you delete tables themselves this focuses on rows so
1020:24 themselves this focuses on rows so suppose that you really really didn't
1020:28 suppose that you really really didn't like let's say uh tedin was a little
1020:30 like let's say uh tedin was a little challenging if you adopt if you tackle
1020:32 challenging if you adopt if you tackle that more comfortable problem so if you
1020:34 that more comfortable problem so if you really don't want to even think about t
1020:36 really don't want to even think about t in anymore so why don't we do uh delete
1020:39 in anymore so why don't we do uh delete from favorites where problem equals and
1020:42 from favorites where problem equals and I won't execute it for real tiamin this
1020:45 I won't execute it for real tiamin this would have the effect of deleting every
1020:46 would have the effect of deleting every row including the language therein and
1020:48 row including the language therein and the time stamp where the student
1020:51 the time stamp where the student answered tiamin worse than this would be
1020:55 answered tiamin worse than this would be this why might this be bad
1021:02 okay chuckling because like there's no predicate there's no filter which means
1021:04 predicate there's no filter which means literally this would delete all of the
1021:07 literally this would delete all of the data so again with great power here
1021:08 data so again with great power here comes great responsibility now this has
1021:10 comes great responsibility now this has just been a data set of what 430 rows by
1021:13 just been a data set of what 430 rows by us dynamically created there's of course
1021:15 us dynamically created there's of course some really juicy data sets in the real
1021:17 some really juicy data sets in the real world and one website you might have
1021:18 world and one website you might have heard or an app you might have used is
1021:20 heard or an app you might have used is IMDb the internet movie database which
1021:22 IMDb the internet movie database which wonderfully makes some of their data
1021:24 wonderfully makes some of their data available for download as CSV files or
1021:26 available for download as CSV files or technically tsv files tab separated
1021:29 technically tsv files tab separated values but what we did in advance of
1021:31 values but what we did in advance of class was download some of that data for
1021:33 class was download some of that data for both TV shows in the real world and
1021:35 both TV shows in the real world and movies in the real world and what's
1021:36 movies in the real world and what's wonderful about this data set is it's
1021:38 wonderful about this data set is it's not just dozens or hundreds or even
1021:41 not just dozens or hundreds or even thousands of lines there are millions of
1021:43 thousands of lines there are millions of rows of Juicy data TV shows and movies
1021:46 rows of Juicy data TV shows and movies with which most folks are probably
1021:48 with which most folks are probably familiar at least with the subset and
1021:49 familiar at least with the subset and we'll see in just a little bit that this
1021:52 we'll see in just a little bit that this data comes in the form of now six
1021:55 data comes in the form of now six different tables that we've given you
1021:56 different tables that we've given you and the tables and question for today
1021:58 and the tables and question for today are going to be the people in the TV
1022:00 are going to be the people in the TV business the Stars they in the shows
1022:02 business the Stars they in the shows that people are producing and the like
1022:04 that people are producing and the like this is a picture we'll revisit to let
1022:06 this is a picture we'll revisit to let enable you to wrap your minds around
1022:08 enable you to wrap your minds around what the actual data is this feels like
1022:10 what the actual data is this feels like a good opportunity though for a snack in
1022:12 a good opportunity though for a snack in fact in just a moment we have a whole
1022:14 fact in just a moment we have a whole lot of Rice Krispie treats out in the
1022:15 lot of Rice Krispie treats out in the lobby but if folks could perhaps
1022:17 lobby but if folks could perhaps acknowledge uh this mini wedding cake
1022:20 acknowledge uh this mini wedding cake here cs50 own Carter zeni is getting
1022:22 here cs50 own Carter zeni is getting married this week so congratulations to
1022:26 married this week so congratulations to Carter
1022:28 Carter as
1022:30 as well
1022:32 well congrats all right there's
1022:41 only okay there's only one piece of cake in that box but a lot of Rice Krispy
1022:43 in that box but a lot of Rice Krispy Treats in the transip let's take 10
1022:44 Treats in the transip let's take 10 minutes and we'll be back with Internet
1022:46 minutes and we'll be back with Internet Movie Database in 10 all right we are
1022:49 Movie Database in 10 all right we are back so if you've never been like you
1022:50 back so if you've never been like you can actually go to I imdb.com right now
1022:53 can actually go to I imdb.com right now and play around or download the mobile
1022:55 and play around or download the mobile app and it's just big database of a lot
1022:57 app and it's just big database of a lot of TV show and movies and actors and the
1023:00 of TV show and movies and actors and the like but what indeed is nice is you can
1023:02 like but what indeed is nice is you can download some of that data and that's in
1023:03 download some of that data and that's in what I've done in advance and what we've
1023:05 what I've done in advance and what we've done is we wrote some python code to
1023:07 done is we wrote some python code to convert some of the uh flat file
1023:10 convert some of the uh flat file databases that they let you download and
1023:12 databases that they let you download and we converted it into a SQL database with
1023:15 we converted it into a SQL database with six tables so not just one but six that
1023:18 six tables so not just one but six that ultimately are these here and let me
1023:20 ultimately are these here and let me just help you wrap your minds around
1023:22 just help you wrap your minds around what this picture is which is a entity
1023:24 what this picture is which is a entity relationship diagram which is just to
1023:26 relationship diagram which is just to say each of these boxes on the screen
1023:28 say each of these boxes on the screen represents a table and each of the
1023:30 represents a table and each of the arrows or edges represents some kind of
1023:32 arrows or edges represents some kind of relationship across the tables because
1023:34 relationship across the tables because up until now the only data we had were
1023:36 up until now the only data we had were those three columns in the favorites
1023:38 those three columns in the favorites table but what's gets really useful
1023:40 table but what's gets really useful about SQL databases just like a Google
1023:42 about SQL databases just like a Google spreadsheet or an Excel file is you can
1023:44 spreadsheet or an Excel file is you can have multiple sheets or in a database
1023:46 have multiple sheets or in a database multiple tables and so what we're about
1023:48 multiple tables and so what we're about to see is that in this IMDb database for
1023:51 to see is that in this IMDb database for TV shows there's going to be a dedicated
1023:53 TV shows there's going to be a dedicated table for all the people in the TV
1023:55 table for all the people in the TV business there's going to be a dedicated
1023:57 business there's going to be a dedicated table for all of the TV shows that are
1023:59 table for all of the TV shows that are in their database as of right now
1024:01 in their database as of right now there's going to be a dedicated table
1024:03 there's going to be a dedicated table for writers in that industry for the
1024:06 for writers in that industry for the ratings of uh shows for the genres to
1024:09 ratings of uh shows for the genres to which shows belong comedy and the like
1024:12 which shows belong comedy and the like and then lastly there's going to be this
1024:14 and then lastly there's going to be this table which somehow
1024:15 table which somehow Associates people with the TV shows that
1024:18 Associates people with the TV shows that they star in and vice versa and so let's
1024:22 they star in and vice versa and so let's consider first what this looks like in
1024:24 consider first what this looks like in code and we'll see that it's going to
1024:25 code and we'll see that it's going to overwhelm intentionally at first but I'm
1024:27 overwhelm intentionally at first but I'm going to do this I'm going to go back to
1024:29 going to do this I'm going to go back to my terminal window and during the break
1024:30 my terminal window and during the break I downloaded from the course's website a
1024:32 I downloaded from the course's website a file called shows. DB which we made in
1024:35 file called shows. DB which we made in advance for you and if I type LS I'll
1024:37 advance for you and if I type LS I'll see all of my favorites files from
1024:39 see all of my favorites files from before the CSV the DB and the python
1024:42 before the CSV the DB and the python file but now there's shows. DB so I'm
1024:45 file but now there's shows. DB so I'm going to go ahead in my full screen
1024:47 going to go ahead in my full screen terminal window here I'm not using
1024:48 terminal window here I'm not using actual tabs or code files now I'm going
1024:50 actual tabs or code files now I'm going to run SQL light3 on the file called
1024:53 to run SQL light3 on the file called shows. DB and I'm just going to see this
1024:55 shows. DB and I'm just going to see this version information here let me clear my
1024:58 version information here let me clear my screen and run the one command I ran
1024:59 screen and run the one command I ran earlier to show us the schema of the
1025:02 earlier to show us the schema of the favorites database now we'll see the
1025:04 favorites database now we'll see the schema for the shows database and
1025:06 schema for the shows database and there's a lot going on here but let me
1025:08 there's a lot going on here but let me scroll back up to the very top the
1025:09 scroll back up to the very top the beginning and we see this here so when I
1025:13 beginning and we see this here so when I run. schema we see a dump really of all
1025:18 run. schema we see a dump really of all of the SQL create table commands that
1025:20 of the SQL create table commands that were run in order to create this
1025:22 were run in order to create this database for you and one of those tables
1025:24 database for you and one of those tables is called genres and another people
1025:26 is called genres and another people ratings shows stars and so forth and the
1025:30 ratings shows stars and so forth and the columns therein even though it's
1025:31 columns therein even though it's formatted a little more prettily than
1025:33 formatted a little more prettily than the automatically generated create table
1025:35 the automatically generated create table statement for favorites whereby we have
1025:37 statement for favorites whereby we have one column per line of output here uh in
1025:41 one column per line of output here uh in the for instance people table there's
1025:43 the for instance people table there's going to be an ID column like unique
1025:45 going to be an ID column like unique identifier like a Harvard ID a Yale ID
1025:48 identifier like a Harvard ID a Yale ID or the like uh a name column a birth
1025:51 or the like uh a name column a birth year and then some other stuff if I
1025:53 year and then some other stuff if I scroll down to shows every show in the
1025:55 scroll down to shows every show in the world is going to have a unique ID as
1025:58 world is going to have a unique ID as well a title of course the year in which
1026:00 well a title of course the year in which it debuted and the total number of
1026:02 it debuted and the total number of episodes as of the time we downloaded
1026:04 episodes as of the time we downloaded the data and then what else is there
1026:06 the data and then what else is there some of these are a little less obvious
1026:08 some of these are a little less obvious like ratings here so ratings don't have
1026:12 like ratings here so ratings don't have an ID column but they have a show ID
1026:14 an ID column but they have a show ID column and a rating like a fivepoint
1026:16 column and a rating like a fivepoint scale or 10o scale or the like and then
1026:18 scale or 10o scale or the like and then the total number of votes that were
1026:19 the total number of votes that were collected to contribute to that rating
1026:21 collected to contribute to that rating IMDb allows people to like up vote and
1026:23 IMDb allows people to like up vote and down vote uh shows and movies and the
1026:25 down vote uh shows and movies and the like and then similarly is genre
1026:28 like and then similarly is genre structured there's a show ID and then
1026:30 structured there's a show ID and then there's a genre which is going to be
1026:32 there's a genre which is going to be like an English word like comedy or
1026:34 like an English word like comedy or drama or something else and then what
1026:36 drama or something else and then what else let's go a little further at the
1026:37 else let's go a little further at the bottom here for stars and writers if we
1026:41 bottom here for stars and writers if we go to the very bottom here stars and
1026:43 go to the very bottom here stars and writers are similarly structured too
1026:46 writers are similarly structured too they have a show ID and a person ID so
1026:49 they have a show ID and a person ID so show and person and then this writer's
1026:52 show and person and then this writer's table has a show ID and a person ID and
1026:55 table has a show ID and a person ID and there's a whole lot of other words that
1026:56 there's a whole lot of other words that we'll come to in just a moment but what
1026:58 we'll come to in just a moment but what are the what is this code hinting at
1027:00 are the what is this code hinting at well if I go back to the picture from
1027:02 well if I go back to the picture from earlier here you'll see that this
1027:05 earlier here you'll see that this picture captures the relationships among
1027:08 picture captures the relationships among these various tables so for instance if
1027:11 these various tables so for instance if we focus on shows for just a moment a
1027:13 we focus on shows for just a moment a show again has a unique ID a title a
1027:16 show again has a unique ID a title a year in which it debuted and a total
1027:18 year in which it debuted and a total number of episodes if you want to figure
1027:20 number of episodes if you want to figure out what genre or genr a show belongs to
1027:25 out what genre or genr a show belongs to cuz some shows are just comedies some
1027:26 cuz some shows are just comedies some shows are just just dramas but you know
1027:28 shows are just just dramas but you know some shows are arguably comedies and
1027:30 some shows are arguably comedies and dramas depending on the episode or the
1027:32 dramas depending on the episode or the like so you can imagine wanting to
1027:34 like so you can imagine wanting to associate two or three or even more
1027:36 associate two or three or even more genres with a show this line here in
1027:39 genres with a show this line here in this second table allows us to do that
1027:42 this second table allows us to do that every Row in the genres table we'll see
1027:45 every Row in the genres table we'll see has uh two items a show ID which relates
1027:49 has uh two items a show ID which relates to the ID of a show and that's why these
1027:52 to the ID of a show and that's why these lines literally line up with that
1027:55 lines literally line up with that specific column name and genre which is
1027:58 specific column name and genre which is going to be like quote unquote comedy
1027:59 going to be like quote unquote comedy quote unquote drama or something else
1028:01 quote unquote drama or something else now with that said design question why
1028:05 now with that said design question why have we deliberately not just gotten rid
1028:08 have we deliberately not just gotten rid of this genre's table and made our lives
1028:10 of this genre's table and made our lives simpler by just adding a genre column to
1028:14 simpler by just adding a genre column to this shows table and again a table is
1028:16 this shows table and again a table is just like a sheet with rows and columns
1028:19 just like a sheet with rows and columns at the moment shows only have four
1028:21 at the moment shows only have four columns ID title year episodes why not
1028:24 columns ID title year episodes why not just add a fifth column called genre and
1028:27 just add a fifth column called genre and put the show genre
1028:30 put the show genre there any intuition
1028:33 there any intuition here why not just keep things simple
1028:37 here why not just keep things simple like yeah and
1028:47 back exactly if you add a fifth column here and call it genre then you have to
1028:50 here and call it genre then you have to pick a genre specifically you have to
1028:52 pick a genre specifically you have to put in that cell presumably comedy or
1028:56 put in that cell presumably comedy or drama or music or something else now you
1028:59 drama or music or something else now you could write multiple words in the cell
1029:00 could write multiple words in the cell but generally speaking that would be
1029:01 but generally speaking that would be sloppy bad design like every cell just
1029:04 sloppy bad design like every cell just like in a spreadsheet should really have
1029:05 like in a spreadsheet should really have one value it might have multiple words
1029:07 one value it might have multiple words but it shouldn't be like a weirdly comma
1029:09 but it shouldn't be like a weirdly comma separated list of multiple things it
1029:10 separated list of multiple things it should just be in a different cell in
1029:12 should just be in a different cell in that case so if you instead were to
1029:14 that case so if you instead were to design this with just a single column
1029:15 design this with just a single column called genre you're imposing what a
1029:18 called genre you're imposing what a computer scientist would call a on toone
1029:20 computer scientist would call a on toone relationship every show has one genre
1029:24 relationship every show has one genre and that's not necessarily a good thing
1029:26 and that's not necessarily a good thing or strictly speaking it would be a many
1029:28 or strictly speaking it would be a many to one because the same genre could
1029:29 to one because the same genre could belong to multiple shows but each show
1029:31 belong to multiple shows but each show could only have one genre in that case
1029:34 could only have one genre in that case what a relational database allows you to
1029:35 what a relational database allows you to do and relational is indeed the
1029:37 do and relational is indeed the operative word it allows you to factor
1029:39 operative word it allows you to factor out some of your information and then
1029:42 out some of your information and then have maybe one show here in one row but
1029:45 have maybe one show here in one row but then in this genres table you could have
1029:47 then in this genres table you could have one row for that one show genre or you
1029:50 one row for that one show genre or you could have two rows in the genres table
1029:52 could have two rows in the genres table for comedy and for drama or if it has a
1029:55 for comedy and for drama or if it has a third genre you could just add another
1029:56 third genre you could just add another row here so you still have one row for
1030:00 row here so you still have one row for the show itself with all the juiciest
1030:02 the show itself with all the juiciest details but a variable number of rows by
1030:05 details but a variable number of rows by having this relationship with another
1030:07 having this relationship with another table meanwhile ratings work the same
1030:10 table meanwhile ratings work the same way at least in this case a show has ID
1030:14 way at least in this case a show has ID title year and episodes but if you want
1030:15 title year and episodes but if you want to figure out its rating you have to
1030:17 to figure out its rating you have to kind of Follow the arrow here so to
1030:18 kind of Follow the arrow here so to speak and look up the corresponding show
1030:21 speak and look up the corresponding show ID in this table find the rating of that
1030:23 ID in this table find the rating of that show and the total number of ratings so
1030:26 show and the total number of ratings so that's been factored out Two For Better
1030:27 that's been factored out Two For Better or For Worse um now let's consider
1030:30 or For Worse um now let's consider people people have just three columns ID
1030:33 people people have just three columns ID name and birth but there's no mention of
1030:35 name and birth but there's no mention of the TV show in which people have starred
1030:37 the TV show in which people have starred or the TV shows that a person has
1030:39 or the TV shows that a person has written well why is that well if you
1030:41 written well why is that well if you just had a fourth column here called
1030:44 just had a fourth column here called show well you would have to decide what
1030:46 show well you would have to decide what show is that person in and no one could
1030:48 show is that person in and no one could ever act again in another show because
1030:50 ever act again in another show because there's no room to store the data but if
1030:51 there's no room to store the data but if someone of course a popular actor can St
1030:54 someone of course a popular actor can St star in multiple shows well we could
1030:57 star in multiple shows well we could have one ID for that person one name one
1030:59 have one ID for that person one name one birth year obviously like there's only
1031:01 birth year obviously like there's only one Steve Carell as an actor in the
1031:03 one Steve Carell as an actor in the world of people but Steve Carell in this
1031:06 world of people but Steve Carell in this example could have his person ID
1031:09 example could have his person ID whatever his Harvard ID equivalent Yale
1031:10 whatever his Harvard ID equivalent Yale ID equivalent is appear in multiple rows
1031:12 ID equivalent is appear in multiple rows in this table so that it can be
1031:14 in this table so that it can be associated with multiple shows and this
1031:17 associated with multiple shows and this allows you to create what's called a one
1031:19 allows you to create what's called a one to many relationship or technically it's
1031:22 to many relationship or technically it's bidirectional it's a many to many
1031:24 bidirectional it's a many to many relationship why well one show can
1031:26 relationship why well one show can certain certainly have multiple people
1031:28 certain certainly have multiple people in it and multiple people writing for it
1031:30 in it and multiple people writing for it just in the real world but conversely
1031:32 just in the real world but conversely one uh person could certainly act in
1031:35 one uh person could certainly act in multiple shows or write multiple shows
1031:37 multiple shows or write multiple shows so this is what you get with relational
1031:39 so this is what you get with relational databases you put your sort of canonical
1031:42 databases you put your sort of canonical data for people in one place for for
1031:44 data for people in one place for for shows in another place and then you use
1031:46 shows in another place and then you use these additional tables to relate one
1031:50 these additional tables to relate one thing to another so we won't dwell on
1031:52 thing to another so we won't dwell on the pictures that's just if you sort of
1031:54 the pictures that's just if you sort of uh can wrap your mind around the data
1031:55 uh can wrap your mind around the data set better that way that's one way of
1031:57 set better that way that's one way of thinking about it but recall that the
1031:59 thinking about it but recall that the code we just saw for the schema again
1032:01 code we just saw for the schema again escalated quickly like there's a lot of
1032:03 escalated quickly like there's a lot of keywords I haven't mentioned yet but
1032:05 keywords I haven't mentioned yet but some of these are perhaps familiar
1032:06 some of these are perhaps familiar they're capitalized differently here but
1032:09 they're capitalized differently here but integer is on the list here null is on
1032:11 integer is on the list here null is on the list albeit technically not null so
1032:13 the list albeit technically not null so let's tease apart some of these key
1032:14 let's tease apart some of these key words and consider what they're actually
1032:16 words and consider what they're actually doing for your database because now
1032:17 doing for your database because now we're in exploring features that do not
1032:20 we're in exploring features that do not exist in the world of spreadsheets alone
1032:23 exist in the world of spreadsheets alone so it turns out in a SQL database
1032:25 so it turns out in a SQL database specifically SQL light which is the
1032:27 specifically SQL light which is the version of SQL we use in cs50 and which
1032:30 version of SQL we use in cs50 and which is commonly used for things like mobile
1032:32 is commonly used for things like mobile applications nowadays it's like a
1032:33 applications nowadays it's like a lightweight version of SQL it's when you
1032:35 lightweight version of SQL it's when you aren't trying to run Twitter and have
1032:37 aren't trying to run Twitter and have billions and billions of rows
1032:39 billions and billions of rows necessarily you've got hundreds
1032:40 necessarily you've got hundreds thousands tens of thousands maybe even a
1032:42 thousands tens of thousands maybe even a few million but not crazy uh numbers uh
1032:45 few million but not crazy uh numbers uh crazy amounts of data in the world of
1032:47 crazy amounts of data in the world of SQL light specifically there's these
1032:50 SQL light specifically there's these five data types so just like in C we had
1032:52 five data types so just like in C we had int and Char and the like in SQL we have
1032:55 int and Char and the like in SQL we have these uh BL which is kind of funny but
1032:57 these uh BL which is kind of funny but it just means binary large object so
1032:59 it just means binary large object so it's like a binary data type zeros and
1033:01 it's like a binary data type zeros and ones that aren't necessarily uh fitting
1033:04 ones that aren't necessarily uh fitting into the other categories integer which
1033:06 into the other categories integer which of course is an integer as we know it
1033:07 of course is an integer as we know it numeric which is kind of a catchall for
1033:10 numeric which is kind of a catchall for numbers that are formatted specially so
1033:12 numbers that are formatted specially so like a date uh would be like year year
1033:15 like a date uh would be like year year year year-month month Dash day day um
1033:18 year year-month month Dash day day um and this is actually a wonderful thing
1033:20 and this is actually a wonderful thing depending on the country you're from you
1033:21 depending on the country you're from you might think your date system in your
1033:23 might think your date system in your country is great or it's horrible the US
1033:25 country is great or it's horrible the US system is horrible because we have month
1033:28 system is horrible because we have month day and then year which is impossible to
1033:31 day and then year which is impossible to sort it is the wrong way objectively to
1033:33 sort it is the wrong way objectively to store data and yet here we are using
1033:35 store data and yet here we are using this at scale other countries have
1033:37 this at scale other countries have gotten this better numeric and SQL
1033:39 gotten this better numeric and SQL itself standardizes that stuff so it
1033:41 itself standardizes that stuff so it doesn't matter what country you're from
1033:42 doesn't matter what country you're from you're storing your data in this
1033:44 you're storing your data in this particular way for instance times are
1033:46 particular way for instance times are standardized and other types of numeric
1033:48 standardized and other types of numeric data as well real is synonymous with
1033:50 data as well real is synonymous with float so something with a decimal point
1033:52 float so something with a decimal point and some number of uh digits thereafter
1033:54 and some number of uh digits thereafter and then text is just uh for strings and
1033:57 and then text is just uh for strings and the like with other even fancier
1033:59 the like with other even fancier databases like MySQL postgress SQL
1034:02 databases like MySQL postgress SQL Oracle and other products you might have
1034:04 Oracle and other products you might have heard of there's even more data types
1034:06 heard of there's even more data types where you have to make even finer grain
1034:07 where you have to make even finer grain decisions but for SQL light it's indeed
1034:09 decisions but for SQL light it's indeed pretty lightweight and you or we just
1034:11 pretty lightweight and you or we just have to decide the data types for each
1034:13 have to decide the data types for each column in a table but there's these
1034:15 column in a table but there's these additional constraints in the world of
1034:16 additional constraints in the world of SQL you can additionally say that cells
1034:20 SQL you can additionally say that cells in this column may or may not be null so
1034:23 in this column may or may not be null so if you want to protect yourself from
1034:25 if you want to protect yourself from yourself so you don't screw up and
1034:27 yourself so you don't screw up and insert a null that is a blank value you
1034:29 insert a null that is a blank value you can explicitly design a table to have a
1034:31 can explicitly design a table to have a column that cannot be null and so in
1034:34 column that cannot be null and so in fact someone came up during the break to
1034:35 fact someone came up during the break to ask me about my having manually inserted
1034:38 ask me about my having manually inserted SQL quote unquote SQL into our favorites
1034:40 SQL quote unquote SQL into our favorites database you might recall that I kind of
1034:42 database you might recall that I kind of cheated I just inserted uh SQL quote
1034:45 cheated I just inserted uh SQL quote unquote and 50v the name of a new
1034:47 unquote and 50v the name of a new problem quote unquote but what did I not
1034:49 problem quote unquote but what did I not insert into the
1034:50 insert into the database a Tim stamp and I could have I
1034:53 database a Tim stamp and I could have I could have put like the current day in
1034:54 could have put like the current day in time a few minutes ago but I didn't and
1034:56 time a few minutes ago but I didn't and that's fine if it's uh if it's
1034:58 that's fine if it's uh if it's acceptable to you and the product you're
1035:00 acceptable to you and the product you're building but I could have prevented that
1035:02 building but I could have prevented that if we had defined the table to have a
1035:05 if we had defined the table to have a timestamp column that isn't just text
1035:07 timestamp column that isn't just text but it's text that's not null SQL would
1035:10 but it's text that's not null SQL would have complained and would not have let
1035:11 have complained and would not have let me complete that insertion so there's
1035:13 me complete that insertion so there's these kinds of built-in defenses that
1035:15 these kinds of built-in defenses that you don't necessarily get with a
1035:16 you don't necessarily get with a spreadsheet alone and unique means
1035:18 spreadsheet alone and unique means exactly that if you want to make sure
1035:20 exactly that if you want to make sure that every Row in that column is unique
1035:23 that every Row in that column is unique maybe for email addresses or in the US
1035:25 maybe for email addresses or in the US Social Security numbers or anything that
1035:27 Social Security numbers or anything that you want to make sure you don't have two
1035:30 you want to make sure you don't have two versions of you can specify that the
1035:31 versions of you can specify that the column is unique and there's other such
1035:33 column is unique and there's other such constraints as well but again this is
1035:35 constraints as well but again this is just a list of features that you get
1035:37 just a list of features that you get from a proper relational database but
1035:39 from a proper relational database but perhaps the most intellectually
1035:41 perhaps the most intellectually interesting one and the most powerful
1035:43 interesting one and the most powerful one is what's called here a primary key
1035:46 one is what's called here a primary key and a foreign key and let me go back now
1035:48 and a foreign key and let me go back now to this output if we look at shows
1035:52 to this output if we look at shows you'll see that a show again has an ID a
1035:55 you'll see that a show again has an ID a title a year and a number of episodes
1035:57 title a year and a number of episodes and now the data types might make sense
1035:59 and now the data types might make sense the ID it turns out just like a Harvard
1036:01 the ID it turns out just like a Harvard ID a Yale ID is going to be an integer
1036:03 ID a Yale ID is going to be an integer so a simple number the title of course
1036:05 so a simple number the title of course is going to be text but not null like it
1036:07 is going to be text but not null like it would be weird if a TV show had no name
1036:09 would be weird if a TV show had no name like that can't be like every the whole
1036:11 like that can't be like every the whole world would break or your TV gu and
1036:13 world would break or your TV gu and whatnot so that makes sense there to say
1036:14 whatnot so that makes sense there to say not null uh year is numeric so it's a
1036:17 not null uh year is numeric so it's a standardized form episodes is an integer
1036:19 standardized form episodes is an integer like how many episodes have been
1036:21 like how many episodes have been produced and then lastly notice this the
1036:23 produced and then lastly notice this the primary key of the show's table is
1036:26 primary key of the show's table is apparently the column called ID
1036:28 apparently the column called ID mentioned a few lines earlier this just
1036:30 mentioned a few lines earlier this just means that the database will use the ID
1036:34 means that the database will use the ID column as the unique identifier so it's
1036:37 column as the unique identifier so it's similar to the unique keyword but
1036:39 similar to the unique keyword but primary key just means the database is
1036:41 primary key just means the database is going to treat it as special too and
1036:42 going to treat it as special too and make sure that it is uniquely
1036:44 make sure that it is uniquely identifying your data but what's
1036:46 identifying your data but what's interesting is this notice if I scroll
1036:48 interesting is this notice if I scroll back up to people people were sort of
1036:51 back up to people people were sort of similarly structured but with different
1036:53 similarly structured but with different attributes like up here we had a person
1036:56 attributes like up here we had a person has an ID a name a birth year and a
1036:59 has an ID a name a birth year and a primary key of ID so a ID is again
1037:02 primary key of ID so a ID is again integer name is text but not null
1037:05 integer name is text but not null because it'd be weird to have a human
1037:06 because it'd be weird to have a human with absolutely no name textually birth
1037:09 with absolutely no name textually birth is going to be numeric but the primary
1037:11 is going to be numeric but the primary key of people is ID as well so those are
1037:14 key of people is ID as well so those are the unique columns that the database
1037:16 the unique columns that the database will just treat special why well we just
1037:18 will just treat special why well we just looked at shows we just looked at people
1037:21 looked at shows we just looked at people let's focus now on this one down here
1037:24 let's focus now on this one down here Stars how do you determine who star in a
1037:27 Stars how do you determine who star in a TV show well we had two columns the show
1037:29 TV show well we had two columns the show ID and the person ID this is the ex the
1037:31 ID and the person ID this is the ex the Incarnation of a many to- many
1037:32 Incarnation of a many to- many relationship one person could be in many
1037:34 relationship one person could be in many shows one show could certainly have many
1037:36 shows one show could certainly have many people in it or writing for it but
1037:38 people in it or writing for it but notice this within this table of two
1037:41 notice this within this table of two columns show ID and person ID there's
1037:43 columns show ID and person ID there's what's going to be called a foreign key
1037:46 what's going to be called a foreign key called show ID that references the shows
1037:50 called show ID that references the shows tables ID column and then another
1037:52 tables ID column and then another foreign key called person ID though I
1037:54 foreign key called person ID though I could call these things in parentheses
1037:55 could call these things in parentheses anything I want that references the
1037:58 anything I want that references the people tables ID column now you're not
1038:00 people tables ID column now you're not going to often have to type commands
1038:02 going to often have to type commands like this again you set the database up
1038:03 like this again you set the database up once in the beginning typically maybe
1038:05 once in the beginning typically maybe with some help from a TF maybe with help
1038:07 with some help from a TF maybe with help of Google or the like but once your
1038:09 of Google or the like but once your database is designed it's back to the
1038:11 database is designed it's back to the crud like create read update delete the
1038:13 crud like create read update delete the selects the inserts the deletions and
1038:14 selects the inserts the deletions and the like but what's this implying these
1038:17 the like but what's this implying these keywords like primary key and foreign
1038:19 keywords like primary key and foreign key are what are doing in code what this
1038:23 key are what are doing in code what this picture was painting a moment ago these
1038:26 picture was painting a moment ago these lines here are drawn literally to line
1038:28 lines here are drawn literally to line up with the corresponding things
1038:30 up with the corresponding things people's ID lines up with person ID
1038:33 people's ID lines up with person ID Show's ID lines up with show ID and so
1038:37 Show's ID lines up with show ID and so you're just seeing graphical version
1038:39 you're just seeing graphical version code version graphical code that creates
1038:41 code version graphical code that creates these relationships now given that let's
1038:45 these relationships now given that let's actually see what these things look like
1038:47 actually see what these things look like so let me go back to vs code here let me
1038:49 so let me go back to vs code here let me clear my screen I'm still within SQL
1038:51 clear my screen I'm still within SQL light with shows. DB let me go ahead and
1038:54 light with shows. DB let me go ahead and do what I do with like any new database
1038:56 do what I do with like any new database if I ever download something or I'm
1038:57 if I ever download something or I'm trying to wrap my mind around a problem
1038:59 trying to wrap my mind around a problem you know usually it doesn't come with a
1039:00 you know usually it doesn't come with a pretty picture or uh you know a
1039:02 pretty picture or uh you know a three-hour lecture to explain what the
1039:04 three-hour lecture to explain what the data set is rather you just have the
1039:06 data set is rather you just have the data set in your own knowledge of SQL so
1039:08 data set in your own knowledge of SQL so let me play around so schema shows me
1039:11 let me play around so schema shows me all the tables that might be a starting
1039:13 all the tables that might be a starting point okay this is interesting I know PE
1039:14 point okay this is interesting I know PE what people are let's go ahead and show
1039:17 what people are let's go ahead and show me all the people so select star from
1039:20 me all the people so select star from people I'm just trying to wrap my mind
1039:22 people I'm just trying to wrap my mind around what this data set looks like in
1039:23 around what this data set looks like in a more userfriendly way okay okay that's
1039:27 a more userfriendly way okay okay that's already a lot of people as you see the
1039:29 already a lot of people as you see the years flying by there's been a lot of
1039:31 years flying by there's been a lot of people in the TV
1039:32 people in the TV business so this was maybe not the best
1039:35 business so this was maybe not the best query to run but this is indicative of
1039:36 query to run but this is indicative of just how large this data set is from
1039:40 just how large this data set is from IMDb okay when in doubt and when
1039:43 IMDb okay when in doubt and when whenever you lose control over your
1039:44 whenever you lose control over your computer control C is your friend to
1039:46 computer control C is your friend to interrupt what would have been better
1039:48 interrupt what would have been better because I don't think I need to know all
1039:50 because I don't think I need to know all the million people in the world I could
1039:52 the million people in the world I could do like limit me to 10 people all right
1039:54 do like limit me to 10 people all right and that's enough now to get us sense of
1039:57 and that's enough now to get us sense of like Fred a stair has an IDE of one
1039:59 like Fred a stair has an IDE of one first person ever um birth year of 1899
1040:02 first person ever um birth year of 1899 Lauren ball and all of these other uh
1040:05 Lauren ball and all of these other uh people from yesterday year you see that
1040:07 people from yesterday year you see that they are the first 10 people in the
1040:09 they are the first 10 people in the database so there's an example of some
1040:11 database so there's an example of some of the data now if I want to rep my mind
1040:12 of the data now if I want to rep my mind around what a show is you know I know it
1040:14 around what a show is you know I know it technically I know it from the picture
1040:16 technically I know it from the picture but let's just look at some raw data so
1040:17 but let's just look at some raw data so instead of saying select star from
1040:19 instead of saying select star from people let me go ahead and select star
1040:21 people let me go ahead and select star from shows limit 10 and okay I've only
1040:25 from shows limit 10 and okay I've only heard heard of or seen a couple of these
1040:28 heard heard of or seen a couple of these but these are older shows at that but I
1040:30 but these are older shows at that but I see that every show has an ID a title a
1040:32 see that every show has an ID a title a year in which it debuted and a number of
1040:34 year in which it debuted and a number of episodes but perhaps most opaque is
1040:37 episodes but perhaps most opaque is going to be this select star from
1040:40 going to be this select star from Stars where this is the table that
1040:42 Stars where this is the table that Associates people with shows am I going
1040:44 Associates people with shows am I going to see any names or show titles here not
1040:49 to see any names or show titles here not according to the definition we saw
1040:51 according to the definition we saw earlier oh I should have done my limit
1040:52 earlier oh I should have done my limit let me interrupt that let me do that
1040:54 let me interrupt that let me do that again limit 10
1040:56 again limit 10 no and this is where now you're
1040:59 no and this is where now you're definitely in the programmer world
1041:00 definitely in the programmer world because like this would be the most
1041:02 because like this would be the most annoying spreadsheet to use on your Mac
1041:03 annoying spreadsheet to use on your Mac or PC ever if you just had like a sheet
1041:06 or PC ever if you just had like a sheet with all of these numbers that
1041:07 with all of these numbers that Associates one thing with the other like
1041:08 Associates one thing with the other like my God how do you figure out who this is
1041:10 my God how do you figure out who this is or what this is you have to like
1041:11 or what this is you have to like manually control F or command F looking
1041:14 manually control F or command F looking for the data but a database doesn't care
1041:16 for the data but a database doesn't care once you know SQL you can sort of Stitch
1041:19 once you know SQL you can sort of Stitch these things back together so what
1041:20 these things back together so what you're seeing here are foreign Keys
1041:23 you're seeing here are foreign Keys foreign Keys why because show ID
1041:26 foreign Keys why because show ID corresponds to the same numbers from
1041:28 corresponds to the same numbers from that other table called shows that has
1041:31 that other table called shows that has an proper primary key called ID person
1041:33 an proper primary key called ID person ID is a foreign key in this context
1041:35 ID is a foreign key in this context because it refers to numbers that belong
1041:37 because it refers to numbers that belong to really the people table and its ID
1041:41 to really the people table and its ID column so this is just a way of somehow
1041:42 column so this is just a way of somehow linking them and so if you think of I
1041:44 linking them and so if you think of I always like think of um this in my
1041:45 always like think of um this in my mind's eye is this if this is like the
1041:47 mind's eye is this if this is like the people table this is the shows table and
1041:50 people table this is the shows table and there's this middle table in between the
1041:53 there's this middle table in between the Stars table there's some way of like
1041:55 Stars table there's some way of like stitching those two together by lining
1041:57 stitching those two together by lining up the idas of one with the other and
1041:59 up the idas of one with the other and getting back some more data so let's
1042:01 getting back some more data so let's actually play with some of this data how
1042:04 actually play with some of this data how about we start where we emphasized
1042:06 about we start where we emphasized earlier genres so let me go ahead and
1042:08 earlier genres so let me go ahead and take a quick look at all of the genres
1042:10 take a quick look at all of the genres in this database so select star from
1042:12 in this database so select star from genres star is usually going to be a
1042:14 genres star is usually going to be a little overwhelming but it just gives me
1042:16 little overwhelming but it just gives me a sense of what the data is but let's
1042:18 a sense of what the data is but let's actually look at um uh let's go look at
1042:21 actually look at um uh let's go look at all of them there okay that's a lot
1042:23 all of them there okay that's a lot these are all official genres from IMDb
1042:26 these are all official genres from IMDb let me oh okay it went okay it wasn't
1042:28 let me oh okay it went okay it wasn't terribly long let me filter that down so
1042:31 terribly long let me filter that down so from genres where genre equals comedy uh
1042:36 from genres where genre equals comedy uh Capital C just based on the data I'm
1042:37 Capital C just based on the data I'm seeing okay so what am I seeing now and
1042:40 seeing okay so what am I seeing now and in fact let me limit this arbitrarily to
1042:43 in fact let me limit this arbitrarily to 10 though I could limit it to anything I
1042:44 10 though I could limit it to anything I want here are 10 comedies what are they
1042:48 want here are 10 comedies what are they well who the heck knows like all I know
1042:50 well who the heck knows like all I know are the 10 show IDs now I could do
1042:52 are the 10 show IDs now I could do something like this as we've seen before
1042:54 something like this as we've seen before with SQL I could do all right well let's
1042:56 with SQL I could do all right well let's figure out what this show idea is Select
1042:58 figure out what this show idea is Select star from shows where the IDE of the
1043:03 star from shows where the IDE of the show I'm looking for equals what 62 614
1043:08 show I'm looking for equals what 62 614 semicolon so I could like manually look
1043:10 semicolon so I could like manually look it up by cross referencing the other
1043:12 it up by cross referencing the other table okay so that was the show in
1043:14 table okay so that was the show in question there the first comedy in the
1043:16 question there the first comedy in the data set let me look up the second one
1043:18 data set let me look up the second one so instead of that let's do 6 3881 enter
1043:23 so instead of that let's do 6 3881 enter okay so that's that show and let's do
1043:25 okay so that's that show and let's do one more and suffice it to say this is
1043:27 one more and suffice it to say this is just getting tedious and vulnerable to
1043:29 just getting tedious and vulnerable to mistakes quickly this is not this surely
1043:32 mistakes quickly this is not this surely can't be the way to do this and indeed
1043:34 can't be the way to do this and indeed SQL is going to let us do this a little
1043:36 SQL is going to let us do this a little more powerfully instead let's do this
1043:39 more powerfully instead let's do this instead of getting this table
1043:40 instead of getting this table temporarily with all these show IDs and
1043:42 temporarily with all these show IDs and all these genres let's refine the query
1043:44 all these genres let's refine the query so let's just select the show ID from
1043:49 so let's just select the show ID from the genres table where the genre equals
1043:52 the genres table where the genre equals quote unquote comedy now I have a big b
1043:56 quote unquote comedy now I have a big b list of show IDs all of which are comedy
1043:59 list of show IDs all of which are comedy how many well I can combine ideas from
1044:00 how many well I can combine ideas from earlier I can just count all of those
1044:02 earlier I can just count all of those show IDs and or Star if I want to just
1044:05 show IDs and or Star if I want to just do that too but I can count all those
1044:06 do that too but I can count all those show IDs
1044:08 show IDs 4876 comedies and IMDB's database for TV
1044:11 4876 comedies and IMDB's database for TV shows so feels like a lot but how can I
1044:14 shows so feels like a lot but how can I now use that information and get back
1044:16 now use that information and get back the titles of comedies in the database
1044:18 the titles of comedies in the database without doing it manually well let's do
1044:21 without doing it manually well let's do this I have a moment ago this query
1044:23 this I have a moment ago this query select the show ID from genres where the
1044:27 select the show ID from genres where the current genre is quote unquote
1044:30 current genre is quote unquote comedy um what if I kind of Nest these
1044:33 comedy um what if I kind of Nest these queries kind of like grade school math
1044:34 queries kind of like grade school math in parenthesis what if I combine this
1044:36 in parenthesis what if I combine this whole thing in parenthesis and now let
1044:39 whole thing in parenthesis and now let me select what I really want let me go
1044:40 me select what I really want let me go ahead and select how about uh the title
1044:45 ahead and select how about uh the title of all shows where the idea of the show
1044:50 of all shows where the idea of the show is in this list of show
1044:53 is in this list of show IDs so if you agree that the shows table
1044:59 IDs so if you agree that the shows table has an ID column which is its otherwise
1045:01 has an ID column which is its otherwise known as its primary key the unique ID
1045:03 known as its primary key the unique ID that identifies it just like our Harvard
1045:05 that identifies it just like our Harvard IDs our Yale IDs and you agree that per
1045:07 IDs our Yale IDs and you agree that per a moment ago this shorter query will
1045:10 a moment ago this shorter query will give me back just the show IDs of all of
1045:13 give me back just the show IDs of all of the comedies in the database you can
1045:16 the comedies in the database you can actually combine or Nest these queries
1045:19 actually combine or Nest these queries together uh it's going to respect SQL
1045:21 together uh it's going to respect SQL light order of operations with
1045:22 light order of operations with parentheses just like grade school math
1045:24 parentheses just like grade school math so the thing in parentheses will be
1045:25 so the thing in parentheses will be executed first that gives it back a list
1045:27 executed first that gives it back a list of IDs like what 48,000 IDs and then
1045:30 of IDs like what 48,000 IDs and then this query the outer query is going to
1045:32 this query the outer query is going to get the title from all of the shows
1045:34 get the title from all of the shows where the ID of the show is in that big
1045:37 where the ID of the show is in that big list of 48,000 so if I now execute these
1045:40 list of 48,000 so if I now execute these together I think the list is still going
1045:42 together I think the list is still going to be a little long but let me execute
1045:43 to be a little long but let me execute it together now I see this long list of
1045:47 it together now I see this long list of outputs a little overwhelming let's go
1045:49 outputs a little overwhelming let's go ahead and maybe limit it to just 10 as
1045:53 ahead and maybe limit it to just 10 as before for discussion sake and now I see
1045:56 before for discussion sake and now I see 10 comedies ordered arbitrarily from
1045:58 10 comedies ordered arbitrarily from however they're in the database that
1046:00 however they're in the database that happen to indeed be have comedy as their
1046:03 happen to indeed be have comedy as their genre if I want to do this a little more
1046:05 genre if I want to do this a little more cleanly I could do this let's see uh why
1046:07 cleanly I could do this let's see uh why don't I order by title ascending order
1046:10 don't I order by title ascending order which is alphabetically or the default
1046:12 which is alphabetically or the default is also uh ascending limit 10 now I see
1046:16 is also uh ascending limit 10 now I see the top 10 I mean weirdly named things
1046:19 the top 10 I mean weirdly named things with hash symbols presumably to get
1046:21 with hash symbols presumably to get their titles up to the beginning or
1046:23 their titles up to the beginning or maybe these are hashtags uh here now we
1046:25 maybe these are hashtags uh here now we have alphabetically the top first 10
1046:27 have alphabetically the top first 10 shows that are
1046:29 shows that are comedies any questions on these kinds of
1046:33 comedies any questions on these kinds of queries it's kind of a lot but at the
1046:35 queries it's kind of a lot but at the same time it's just like composing the
1046:36 same time it's just like composing the smaller ideas from before into slightly
1046:39 smaller ideas from before into slightly more useful
1046:41 more useful queries
1046:50 yeah do for Keys have to set the relationship when you create the table
1046:52 relationship when you create the table the programmer or the database
1046:54 the programmer or the database administrator would create that
1046:56 administrator would create that relationship by using those keywords
1046:57 relationship by using those keywords primary key and foreign key that create
1046:59 primary key and foreign key that create that teaches the database what is
1047:01 that teaches the database what is related to what per the picture so you
1047:04 related to what per the picture so you do that once and now I being the sort of
1047:07 do that once and now I being the sort of programmer who's familiar with the
1047:09 programmer who's familiar with the database I am just using these foreign
1047:12 database I am just using these foreign keys in a way in a manner consistent
1047:14 keys in a way in a manner consistent with their design but and this is where
1047:16 with their design but and this is where it's useful at some point even if you no
1047:18 it's useful at some point even if you no one hands you a picture to make sure you
1047:20 one hands you a picture to make sure you understand the database because that's
1047:21 understand the database because that's going to inform literally what you type
1047:23 going to inform literally what you type in SQL to get the data you care about
1047:25 in SQL to get the data you care about about well let's do something a little
1047:27 about well let's do something a little more precise how about very reasonable
1047:29 more precise how about very reasonable question and honestly this is exactly
1047:31 question and honestly this is exactly what imdb.com in the app are for what if
1047:33 what imdb.com in the app are for what if you want to find all of the shows that
1047:35 you want to find all of the shows that Steve Carell is in like kind of a
1047:37 Steve Carell is in like kind of a reasonable query like literally
1047:38 reasonable query like literally something someone might type into Google
1047:40 something someone might type into Google or more specifically IMDb it's not
1047:43 or more specifically IMDb it's not really obvious at first glance how to do
1047:44 really obvious at first glance how to do that though because right like from my
1047:46 that though because right like from my database if these are my six tables well
1047:48 database if these are my six tables well I can pretty easily get Steve Carell
1047:50 I can pretty easily get Steve Carell from here but I can really only get his
1047:52 from here but I can really only get his ID number whatever that is his name
1047:54 ID number whatever that is his name which I know already and his birth year
1047:55 which I know already and his birth year okay interesting but has nothing to do
1047:57 okay interesting but has nothing to do with the shows that he's in I can look
1047:59 with the shows that he's in I can look at shows over here but there's no
1048:02 at shows over here but there's no mention of Steve Carell right because
1048:03 mention of Steve Carell right because there's no person ID here where is that
1048:05 there's no person ID here where is that relationship implemented well it's
1048:07 relationship implemented well it's implemented down here so how do we do
1048:09 implemented down here so how do we do this well here's the perfect example of
1048:11 this well here's the perfect example of a a lesson we've trying been trying to
1048:13 a a lesson we've trying been trying to emphasize for weeks of taking these baby
1048:15 emphasize for weeks of taking these baby steps like break larger problems down
1048:17 steps like break larger problems down into smaller ones and let's do something
1048:19 into smaller ones and let's do something like this let's just get everything I
1048:21 like this let's just get everything I know about Steve Carell from the
1048:22 know about Steve Carell from the database let's select star from people
1048:25 database let's select star from people where the name of the person is quote
1048:27 where the name of the person is quote unquote Steve Carell I just want to see
1048:30 unquote Steve Carell I just want to see what data we've got and here's what we
1048:31 what data we've got and here's what we have okay there's only one Steve Carell
1048:34 have okay there's only one Steve Carell born in 1962 and his unique ID is 136
1048:37 born in 1962 and his unique ID is 136 797 according to IMDb this isn't some
1048:40 797 according to IMDb this isn't some like Global uh actor identifier per se
1048:43 like Global uh actor identifier per se all right well how do I get now all of
1048:45 all right well how do I get now all of the shows that Steve Carell is in well I
1048:47 the shows that Steve Carell is in well I could do this
1048:49 could do this select star from Stars not to confuse
1048:53 select star from Stars not to confuse the two one's the symbol one's the table
1048:54 the two one's the symbol one's the table name uh where person ID equals 13
1049:00 name uh where person ID equals 13 6797 so I think this will now give me
1049:04 6797 so I think this will now give me everything from the Stars table that
1049:05 everything from the Stars table that relates to Steve Carell okay and you'll
1049:07 relates to Steve Carell okay and you'll see person ID is the same because I'm
1049:09 see person ID is the same because I'm literally searching for just Steve
1049:10 literally searching for just Steve Carell but there are what like 20 or so
1049:13 Carell but there are what like 20 or so shows that he's been in all right well
1049:15 shows that he's been in all right well here's where things would get tedious
1049:16 here's where things would get tedious what are those shows well I could do
1049:18 what are those shows well I could do Select Title from shows where the ID of
1049:22 Select Title from shows where the ID of the show equals and here's you know
1049:25 the show equals and here's you know whenever you copy paste you're probably
1049:26 whenever you copy paste you're probably doing something wrong okay he's was in
1049:29 doing something wrong okay he's was in The Dana Carvey Show familiar with that
1049:31 The Dana Carvey Show familiar with that let's do another one we'll copy paste
1049:32 let's do another one we'll copy paste this uh where ID equals this over the
1049:37 this uh where ID equals this over the top another and if we keep digging we'll
1049:38 top another and if we keep digging we'll probably find the office but my God like
1049:40 probably find the office but my God like that's going to take forever to do 20
1049:41 that's going to take forever to do 20 queries manually it's not very Dynamic
1049:43 queries manually it's not very Dynamic but what if we just Nest these queries a
1049:45 but what if we just Nest these queries a little more dynamically so let me start
1049:47 little more dynamically so let me start from the beginning again what if we go
1049:48 from the beginning again what if we go ahead and select everything we want we
1049:51 ahead and select everything we want we know about uh people whose name
1049:55 know about uh people whose name equals
1049:57 equals Steve Carell that gave us earlier this
1050:00 Steve Carell that gave us earlier this data I don't need all of that data I
1050:01 data I don't need all of that data I know his name I don't care about his
1050:03 know his name I don't care about his birth year so let's change this to just
1050:05 birth year so let's change this to just be give me the ID of Steve Carell and
1050:07 be give me the ID of Steve Carell and that gives me back now this smaller
1050:09 that gives me back now this smaller temporary data set all right can I now
1050:11 temporary data set all right can I now use
1050:12 use this uh inside of another query well let
1050:15 this uh inside of another query well let me wrap the whole thing with parentheses
1050:16 me wrap the whole thing with parentheses and now let me say select star from the
1050:19 and now let me say select star from the Stars table where the person ID equals
1050:24 Stars table where the person ID equals this so I'm Del liely not using in
1050:26 this so I'm Del liely not using in because I'm assuming there's indeed only
1050:27 because I'm assuming there's indeed only one Steve Carell in the world so I'm not
1050:29 one Steve Carell in the world so I'm not getting back a list of Steve Carell I'm
1050:30 getting back a list of Steve Carell I'm getting back the one and only in this
1050:32 getting back the one and only in this case so equal is fine in is when you
1050:34 case so equal is fine in is when you have multiple equal is when you have one
1050:37 have multiple equal is when you have one let me go ahead and hit enter now okay
1050:39 let me go ahead and hit enter now okay that's more data than I need I don't
1050:40 that's more data than I need I don't need like 20 copies of Steve Carell's
1050:42 need like 20 copies of Steve Carell's person ID so let me hit up let me go
1050:44 person ID so let me hit up let me go back and let me just get show ID from
1050:47 back and let me just get show ID from Steve Carell and now I have a list of
1050:50 Steve Carell and now I have a list of just the 20 or so show IDs that he has
1050:53 just the 20 or so show IDs that he has been in all right how can I now use this
1050:56 been in all right how can I now use this well let me hit up let me put the whole
1050:57 well let me hit up let me put the whole thing in parentheses and now let me
1051:00 thing in parentheses and now let me select what I really want Select Title
1051:02 select what I really want Select Title from shows where and here's the final
1051:06 from shows where and here's the final flourish the shows table has an ID has a
1051:10 flourish the shows table has an ID has a title has a year and has an episode and
1051:14 title has a year and has an episode and what I really want though is to check
1051:17 what I really want though is to check which
1051:18 which shows have ID that is what anyone want
1051:24 shows have ID that is what anyone want to finish the thought
1051:26 to finish the thought I just want to
1051:33 yeah exactly ID in this and this is getting ugly and when you actually write
1051:34 getting ugly and when you actually write your queries in like a text file you can
1051:36 your queries in like a text file you can format them nicely and indent them my
1051:38 format them nicely and indent them my font is just getting I don't want to
1051:39 font is just getting I don't want to make it too small to fit everything but
1051:41 make it too small to fit everything but now we have three queries one is in
1051:43 now we have three queries one is in doubly nested parenthesis then there's
1051:44 doubly nested parenthesis then there's the middle one then there's the outer
1051:46 the middle one then there's the outer one so this last query is going to get
1051:48 one so this last query is going to get me the title from shows where the ID of
1051:50 me the title from shows where the ID of the show is in this big list of 20 or so
1051:53 the show is in this big list of 20 or so show IDs that Steve Carell is in and I
1051:54 show IDs that Steve Carell is in and I knew that because I looked up his name
1051:57 knew that because I looked up his name here and notice what I did not do this
1051:58 here and notice what I did not do this time is I didn't manually hardcode his
1052:00 time is I didn't manually hardcode his ID number there's no need that would be
1052:02 ID number there's no need that would be kind of a bad way to implement a website
1052:04 kind of a bad way to implement a website uh if you're using a database underneath
1052:06 uh if you're using a database underneath the hood you want the IMDb for real to
1052:08 the hood you want the IMDb for real to search for whatever the human typed in
1052:10 search for whatever the human typed in and no one's going to know Steve
1052:12 and no one's going to know Steve Carell's person ID or anything else so
1052:14 Carell's person ID or anything else so here we've done this all dynamically and
1052:17 here we've done this all dynamically and now if I hit enter I think I get all of
1052:20 now if I hit enter I think I get all of his shows let's go ahead and order this
1052:22 his shows let's go ahead and order this by title just to make it tidy and you
1052:24 by title just to make it tidy and you probably will see at least one or more
1052:26 probably will see at least one or more shows that you know and probably the
1052:28 shows that you know and probably the most popular is dot dot dot the office
1052:32 most popular is dot dot dot the office so this is literally the kind of query
1052:34 so this is literally the kind of query that's being executed underneath the
1052:36 that's being executed underneath the hood when you go to uh websites or apps
1052:38 hood when you go to uh websites or apps like IMDb your textual query is probably
1052:41 like IMDb your textual query is probably being plugged into a longer SQL query
1052:44 being plugged into a longer SQL query like this where some programmer at IMDb
1052:46 like this where some programmer at IMDb probably wrote this whole query in
1052:48 probably wrote this whole query in advance weeks months years ago and
1052:51 advance weeks months years ago and they're just somehow plugging in the
1052:53 they're just somehow plugging in the value that you the human type into the
1052:55 value that you the human type into the search box or the like questions
1052:59 search box or the like questions now on finding this data or any
1053:04 now on finding this data or any other no okay so where else could we go
1053:08 other no okay so where else could we go with this well let's
1053:11 with this well let's consider how else we might combine data
1053:15 consider how else we might combine data suppose that the next question actually
1053:17 suppose that the next question actually perhaps appropriately would be focusing
1053:19 perhaps appropriately would be focusing in on not just like people and shows and
1053:21 in on not just like people and shows and these stars but how do we kind of like
1053:23 these stars but how do we kind of like gather more information about the shows
1053:25 gather more information about the shows themselves like the genres the ratings
1053:27 themselves like the genres the ratings or the like so indeed let's focus on
1053:28 or the like so indeed let's focus on just these two tables here recall that
1053:31 just these two tables here recall that every show has an ID a title a year and
1053:33 every show has an ID a title a year and episodes but it also might have one or
1053:35 episodes but it also might have one or more relationships with rose and this
1053:37 more relationships with rose and this other table is called genres and this is
1053:39 other table is called genres and this is so that a show can be a comedy can be a
1053:41 so that a show can be a comedy can be a drama can be any number of other things
1053:43 drama can be any number of other things one row per so you would see the same
1053:45 one row per so you would see the same show ID again and again and again with a
1053:48 show ID again and again and again with a different genre written in English like
1053:51 different genre written in English like comma comedy drama or the like well how
1053:53 comma comedy drama or the like well how do I kind of Recon subtitute that data
1053:56 do I kind of Recon subtitute that data Well turns out there's a few different
1053:58 Well turns out there's a few different ways to do this and let me propose that
1054:00 ways to do this and let me propose that we introduce this keyword here join and
1054:03 we introduce this keyword here join and this is really the most powerful of the
1054:05 this is really the most powerful of the keywords in SQL itself it doesn't have
1054:08 keywords in SQL itself it doesn't have to be used we've seen with uh nested
1054:10 to be used we've seen with uh nested queries that you can still select data
1054:12 queries that you can still select data across multiple tables but here is
1054:14 across multiple tables but here is another way so let me do this let me go
1054:17 another way so let me do this let me go back to my SQL light database and let me
1054:21 back to my SQL light database and let me select sort of in one uh breath exactly
1054:25 select sort of in one uh breath exactly the data I want select star from shows
1054:29 the data I want select star from shows and let's just limit this initially to
1054:31 and let's just limit this initially to 10 to see what it looks like all right
1054:33 10 to see what it looks like all right that's again the shows data select star
1054:35 that's again the shows data select star from genres let's limit that to 102 just
1054:38 from genres let's limit that to 102 just to wrap our minds around it and now this
1054:40 to wrap our minds around it and now this is not that useful however the data in
1054:44 is not that useful however the data in the leftmost column here is the primary
1054:46 the leftmost column here is the primary key in the shows table these are just
1054:48 key in the shows table these are just unique IDs the data here in the genres
1054:51 unique IDs the data here in the genres table recall show ID is the foreign key
1054:53 table recall show ID is the foreign key so it's the same numbers but just copied
1054:55 so it's the same numbers but just copied into another table so that we can have
1054:57 into another table so that we can have this relationship across them how do I
1054:59 this relationship across them how do I kind of line up these numbers with these
1055:02 kind of line up these numbers with these numbers to get back like a wider table
1055:04 numbers to get back like a wider table that has title and year and episodes and
1055:08 that has title and year and episodes and genre and heck ratings and all of that
1055:10 genre and heck ratings and all of that too if we want well you can join these
1055:12 too if we want well you can join these Tables by just telling the database what
1055:14 Tables by just telling the database what to join on what so let me do this select
1055:17 to join on what so let me do this select star from shows join that table though
1055:22 star from shows join that table though on the genres table well how do you want
1055:24 on the genres table well how do you want to join those two tables and again the
1055:27 to join those two tables and again the two tables from the picture looked like
1055:28 two tables from the picture looked like this how do you tell SQL
1055:30 this how do you tell SQL programmatically to sort of you know put
1055:32 programmatically to sort of you know put one of them right next to the other line
1055:33 one of them right next to the other line up all of the ID so that you just get
1055:35 up all of the ID so that you just get one larger data set well we can use
1055:38 one larger data set well we can use indeed this this query this uh syntax
1055:40 indeed this this query this uh syntax called join so back to VSS code here and
1055:43 called join so back to VSS code here and let me join these two
1055:45 let me join these two tables sorry typo here join genres on
1055:51 tables sorry typo here join genres on the shows tables ID column AKA it's
1055:54 the shows tables ID column AKA it's primary key equaling the genres tables
1055:58 primary key equaling the genres tables show ID column aka the foreign key so in
1056:01 show ID column aka the foreign key so in other words it looks a little cryptic
1056:03 other words it looks a little cryptic but I'm just telling SQL how to line up
1056:05 but I'm just telling SQL how to line up these two tables and what column to
1056:07 these two tables and what column to match with the other so that the numbers
1056:10 match with the other so that the numbers line up and I get essentially a wider
1056:12 line up and I get essentially a wider table let me go ahead and hit uh
1056:15 table let me go ahead and hit uh semicolon and enter and this is now
1056:18 semicolon and enter and this is now going to give me a lot of data we might
1056:19 going to give me a lot of data we might have to interrupt it but notice even at
1056:21 have to interrupt it but notice even at a glance we're getting the ID the title
1056:24 a glance we're getting the ID the title the year the number of episodes the ID
1056:26 the year the number of episodes the ID again redundantly but that's to be
1056:28 again redundantly but that's to be expected if I'm joining them and the
1056:30 expected if I'm joining them and the genre all the way on the right let me
1056:31 genre all the way on the right let me hit contrl C to interrupt let me just
1056:34 hit contrl C to interrupt let me just limit this to the office so where title
1056:37 limit this to the office so where title equals quote unquote the office so we
1056:39 equals quote unquote the office so we can focus on just one sample uh datum
1056:42 can focus on just one sample uh datum and here fun fact there's been more than
1056:45 and here fun fact there's been more than one office the one that you all probably
1056:47 one office the one that you all probably like is this one that started in 2005
1056:49 like is this one that started in 2005 with 188 episodes its ID in the shows
1056:53 with 188 episodes its ID in the shows table is 3866 76 that's confirmed over
1056:56 table is 3866 76 that's confirmed over here too so again we've just joined the
1056:58 here too so again we've just joined the two tables how by lining up those fields
1057:01 two tables how by lining up those fields but now that we can see that almost all
1057:03 but now that we can see that almost all of the offices produced over the decades
1057:04 of the offices produced over the decades are comedies except for this one there
1057:07 are comedies except for this one there was a version of the office produced in
1057:09 was a version of the office produced in 2001 that was considered more of uh a
1057:11 2001 that was considered more of uh a drama no unsure if it's related to the
1057:13 drama no unsure if it's related to the other how can we Link in other data well
1057:16 other how can we Link in other data well let's go ahead and Link in ratings too
1057:18 let's go ahead and Link in ratings too or instead so instead of joining this
1057:20 or instead so instead of joining this with genres let me go ahead and Rewind
1057:23 with genres let me go ahead and Rewind here and join shows on ratings on shows.
1057:28 here and join shows on ratings on shows. ID equals ratings. show ID and let's
1057:31 ID equals ratings. show ID and let's limit it to the office 2 for discussion
1057:33 limit it to the office 2 for discussion sake where uh title equals quote unquote
1057:35 sake where uh title equals quote unquote the office semicolon and now you can see
1057:39 the office semicolon and now you can see that among the various offices it looks
1057:42 that among the various offices it looks like the one that most of us probably
1057:44 like the one that most of us probably know and love is the highest rated also
1057:47 know and love is the highest rated also with a 9.0 with like 585,000 people
1057:51 with a 9.0 with like 585,000 people having cast votes for whereas these
1057:53 having cast votes for whereas these other shows seem to have been less
1057:54 other shows seem to have been less popular perhaps that's why indeed you
1057:55 popular perhaps that's why indeed you see fewer episodes for them as well so
1057:59 see fewer episodes for them as well so even though we've put the data in
1058:00 even though we've put the data in multiple places you can still kind of
1058:01 multiple places you can still kind of reconstitute it by lining things up in
1058:04 reconstitute it by lining things up in this way and rejoining the tables
1058:07 this way and rejoining the tables questions now on
1058:10 questions now on this like this is the heart of what SQL
1058:13 this like this is the heart of what SQL does and what relational databases do
1058:16 does and what relational databases do for
1058:18 for you
1058:19 you questions all right a few final features
1058:22 questions all right a few final features like there's not all that much that uh
1058:24 like there's not all that much that uh you know SQL takes practice like
1058:26 you know SQL takes practice like anything else but in terms of syntax and
1058:27 anything else but in terms of syntax and capabilities let's just introduce you to
1058:29 capabilities let's just introduce you to a couple of final features here and how
1058:31 a couple of final features here and how and problems that arise and how we might
1058:33 and problems that arise and how we might solve them let's do this uh as well so
1058:37 solve them let's do this uh as well so let me go back into VSS code here and
1058:39 let me go back into VSS code here and let's just find out um Steve Carell's
1058:42 let's just find out um Steve Carell's information again last time we did it
1058:44 information again last time we did it with this nested nested query by getting
1058:46 with this nested nested query by getting his ID and then the show IDs and then
1058:48 his ID and then the show IDs and then the titles for those show IDs with join
1058:50 the titles for those show IDs with join you can do it a little differently and
1058:51 you can do it a little differently and any of these ways are fine one might
1058:53 any of these ways are fine one might become EAS easier to you mentally than
1058:55 become EAS easier to you mentally than another let's go ahead and select the
1058:57 another let's go ahead and select the titles from what let's select the title
1059:01 titles from what let's select the title from uh the people table but and I'm
1059:04 from uh the people table but and I'm going to hit enter and when you're using
1059:06 going to hit enter and when you're using SQL light 3 interactively if you ever
1059:08 SQL light 3 interactively if you ever find yourself with a prompt that says
1059:09 find yourself with a prompt that says dot dot dot angle bracket it means
1059:12 dot dot dot angle bracket it means you're continuing your thought onto the
1059:13 you're continuing your thought onto the next line if you didn't intend that you
1059:15 next line if you didn't intend that you can sometimes hit uh semicolon to just
1059:17 can sometimes hit uh semicolon to just end the thought and hit enter and just
1059:19 end the thought and hit enter and just even if it triggers an error but this is
1059:21 even if it triggers an error but this is one way of formatting my queries now a
1059:23 one way of formatting my queries now a little more nicely I'm just going to add
1059:24 little more nicely I'm just going to add some wh space so that it's a little
1059:25 some wh space so that it's a little easier to read what do I want to select
1059:28 easier to read what do I want to select well I want to select the title of shows
1059:30 well I want to select the title of shows from the people table joined with the
1059:34 from the people table joined with the Stars table on the people tables ID
1059:37 Stars table on the people tables ID column equaling the Stars tables person
1059:41 column equaling the Stars tables person ID column so in other words if you think
1059:44 ID column so in other words if you think back to what people are and what stars
1059:46 back to what people are and what stars are one has an ID one has a person ID
1059:48 are one has an ID one has a person ID I'm just now connecting those two tables
1059:50 I'm just now connecting those two tables I'm joining those two but I want to do
1059:53 I'm joining those two but I want to do this as well with with another table let
1059:55 this as well with with another table let me additionally join in so now I only
1059:58 me additionally join in so now I only have two hands but now I'm putting a
1059:59 have two hands but now I'm putting a third table joined in together here join
1060:02 third table joined in together here join shows on stars. show ID equals shows. ID
1060:08 shows on stars. show ID equals shows. ID so this is now linking three tables
1060:10 so this is now linking three tables together but I only care about this for
1060:12 together but I only care about this for one person so where the name of the
1060:14 one person so where the name of the person equals quote unquote Steve Carell
1060:17 person equals quote unquote Steve Carell so more cryptic to be to be sure but
1060:19 so more cryptic to be to be sure but what we're doing with this query is just
1060:21 what we're doing with this query is just taking all three tables that we care
1060:23 taking all three tables that we care about and we're joining them them all
1060:25 about and we're joining them them all together at once using this new join
1060:27 together at once using this new join syntax literally telling the database
1060:29 syntax literally telling the database what columns to line up with what and
1060:31 what columns to line up with what and then we filter at the very end just like
1060:33 then we filter at the very end just like before to get back if I hit enter the
1060:36 before to get back if I hit enter the answer we want which in this case is a
1060:38 answer we want which in this case is a little slower at the moment but that
1060:40 little slower at the moment but that same list of uh 20 or so shows that he's
1060:43 same list of uh 20 or so shows that he's been in there's one other way to do this
1060:45 been in there's one other way to do this and again uh these are all in the slides
1060:47 and again uh these are all in the slides online so you can repeat them without
1060:48 online so you can repeat them without having to jot down everything and we'll
1060:50 having to jot down everything and we'll put them in the notes too but there's
1060:51 put them in the notes too but there's another way to do this I could also use
1060:53 another way to do this I could also use an implicit join so that was an explicit
1060:56 an implicit join so that was an explicit join because I literally typed the word
1060:57 join because I literally typed the word join multiple times at that but let me
1061:00 join multiple times at that but let me go ahead and select the title from these
1061:02 go ahead and select the title from these three tables people stars and shows and
1061:05 three tables people stars and shows and this might just be nicer because if you
1061:06 this might just be nicer because if you know what tables you want to select data
1061:08 know what tables you want to select data from just enumerate them separated by
1061:10 from just enumerate them separated by commas which you might prefer in your
1061:11 commas which you might prefer in your mind where the people ID equals the
1061:16 mind where the people ID equals the Stars person
1061:18 Stars person ID and the Stars show ID equals the
1061:23 ID and the Stars show ID equals the shows ID and the name of the person
1061:26 shows ID and the name of the person equals Steve Carell so this is an
1061:28 equals Steve Carell so this is an implicit joint and honestly I constantly
1061:30 implicit joint and honestly I constantly reference my notes for some of this
1061:31 reference my notes for some of this stuff too it's not the kind of thing
1061:33 stuff too it's not the kind of thing that's going to come like this to you
1061:34 that's going to come like this to you after just one day but it's just a
1061:36 after just one day but it's just a different way of expressing the same
1061:37 different way of expressing the same thing I want to select data from three
1061:39 thing I want to select data from three different tables and hey SQL here is how
1061:42 different tables and hey SQL here is how I want you to line those tables up so
1061:45 I want you to line those tables up so that I can get like related data for
1061:47 that I can get like related data for Steve Carell and this now will achieve
1061:49 Steve Carell and this now will achieve the same results ultimately let me hit
1061:53 the same results ultimately let me hit enter
1061:55 enter H and there we go so a little slower and
1061:58 H and there we go so a little slower and performance might vary based on computer
1062:00 performance might vary based on computer based on uh implementation of SQL but I
1062:02 based on uh implementation of SQL but I think I still have the same answers now
1062:03 think I still have the same answers now suppose as I'm often do and I had to
1062:05 suppose as I'm often do and I had to look it up again last time suppose you
1062:07 look it up again last time suppose you forget what uh how to spell Steve
1062:09 forget what uh how to spell Steve Carell's name is it two RS two L's or
1062:12 Carell's name is it two RS two L's or the like well I could also do something
1062:14 the like well I could also do something like this select well let's just keep
1062:16 like this select well let's just keep this simple select star from people
1062:18 this simple select star from people where name equals I've been deliberately
1062:20 where name equals I've been deliberately getting it right so as to not embarrass
1062:22 getting it right so as to not embarrass myself that's the Steve Carell I keep
1062:23 myself that's the Steve Carell I keep querying if you forget well you could
1062:26 querying if you forget well you could try searching for just Steves but there
1062:29 try searching for just Steves but there interestingly there's a bunch of Steves
1062:30 interestingly there's a bunch of Steves we don't know when they were born uh but
1062:33 we don't know when they were born uh but that's probably not the Steve Carell we
1062:35 that's probably not the Steve Carell we want if we don't have his last name so I
1062:38 want if we don't have his last name so I could alternatively do well it's Steve
1062:39 could alternatively do well it's Steve and then it's starts with a C I think
1062:42 and then it's starts with a C I think well it turns out there's another wild
1062:43 well it turns out there's another wild card you can use in SQL we used the
1062:45 card you can use in SQL we used the asterisk to select all of the columns
1062:48 asterisk to select all of the columns you can in quotes use a percent sign to
1062:51 you can in quotes use a percent sign to say see something so there's some zero
1062:54 say see something so there's some zero or more characters after the letter c
1062:57 or more characters after the letter c and now this doesn't work cuz now I
1062:59 and now this doesn't work cuz now I would be literally looking for Steve
1063:01 would be literally looking for Steve space c something but recall earlier I
1063:05 space c something but recall earlier I mentioned that one other keyword which
1063:07 mentioned that one other keyword which is for fuzzier matching so to speak
1063:09 is for fuzzier matching so to speak where it's like not exactly what you're
1063:10 where it's like not exactly what you're looking for but it's like what you're
1063:12 looking for but it's like what you're looking for if you instead say where his
1063:14 looking for if you instead say where his name is like Steve space c something now
1063:17 name is like Steve space c something now we'll get back a whole bunch of Steves
1063:19 we'll get back a whole bunch of Steves but I think now I could probably find
1063:21 but I think now I could probably find the one I'm actually looking for if I
1063:22 the one I'm actually looking for if I don't remember his name you can use
1063:23 don't remember his name you can use multiple percent signs if you forget
1063:25 multiple percent signs if you forget what his first name is you could reverse
1063:26 what his first name is you could reverse the order but that too is a uh very
1063:29 the order but that too is a uh very powerful sequel feature at that
1063:32 powerful sequel feature at that questions on these queries here
1063:37 questions on these queries here yeah
1063:42 sorry what about it oh yeah sure so the query I used here
1063:46 it oh yeah sure so the query I used here there's a lot of Steves whose last name
1063:49 there's a lot of Steves whose last name starts with C oops too far uh the last
1063:52 starts with C oops too far uh the last query I executed was this one here so
1063:55 query I executed was this one here so where the name is like quote unquote
1063:57 where the name is like quote unquote Steve C percent sign so that's just
1063:59 Steve C percent sign so that's just another tool for your toolkit here but
1064:02 another tool for your toolkit here but you'll you'll perhaps have noticed that
1064:03 you'll you'll perhaps have noticed that those two the prior to that query the
1064:06 those two the prior to that query the joins I did were sort of slow and
1064:07 joins I did were sort of slow and honestly this database isn't even that
1064:09 honestly this database isn't even that big like yes it has tens of thousands of
1064:11 big like yes it has tens of thousands of rows in it but like in the real world
1064:13 rows in it but like in the real world and most of the apps you and I use a lot
1064:15 and most of the apps you and I use a lot every day or websites like there's
1064:17 every day or websites like there's Millions even billions of rows of data
1064:19 Millions even billions of rows of data and like if I had to wait on like my
1064:21 and like if I had to wait on like my computer here or my code space like a
1064:23 computer here or my code space like a second or two to get the data like
1064:25 second or two to get the data like that's not going to work for millions of
1064:27 that's not going to work for millions of users or customers certainly so how can
1064:29 users or customers certainly so how can we actually improve things well it turns
1064:32 we actually improve things well it turns out another upside of a proper
1064:35 out another upside of a proper relational database is that it's not
1064:38 relational database is that it's not just a spreadsheet where the onus is on
1064:39 just a spreadsheet where the onus is on like you to like find the data you're
1064:41 like you to like find the data you're looking for you can also tell the
1064:43 looking for you can also tell the database to index the data for you an
1064:46 database to index the data for you an index is an an efficient uh cheat sheet
1064:49 index is an an efficient uh cheat sheet for Finding data fast like books in the
1064:51 for Finding data fast like books in the real world often have indices at the end
1064:54 real world often have indices at the end of the at the end of the book where you
1064:55 of the at the end of the book where you can look things up alphabetically and
1064:57 can look things up alphabetically and then you can cross reference it for the
1064:58 then you can cross reference it for the pages that that topic appears on same
1065:00 pages that that topic appears on same idea in a database if you tell the
1065:02 idea in a database if you tell the database in advance that you want to
1065:03 database in advance that you want to search on a certain column frequently
1065:06 search on a certain column frequently you can tell it to build a fancy index
1065:08 you can tell it to build a fancy index that will just allow you to search that
1065:10 that will just allow you to search that column Faster by default these columns
1065:12 column Faster by default these columns are going to be searched most likely via
1065:14 are going to be searched most likely via linear search like not even binary
1065:16 linear search like not even binary search because the data might not be
1065:17 search because the data might not be sorted because it came in in any order
1065:19 sorted because it came in in any order but if you create an index you're
1065:22 but if you create an index you're probably going to get something closer
1065:23 probably going to get something closer to logar rmic than linear and that's
1065:25 to logar rmic than linear and that's going to be a big plus overall so let me
1065:27 going to be a big plus overall so let me do something simple here first let me
1065:29 do something simple here first let me turn on a SQL light specific feature
1065:31 turn on a SQL light specific feature that just is going to time all of my
1065:33 that just is going to time all of my queries by writing do timer on I just
1065:35 queries by writing do timer on I just want to keep track of uh how long each
1065:39 want to keep track of uh how long each of these commands takes this one is not
1065:40 of these commands takes this one is not a slow command so this is just going to
1065:42 a slow command so this is just going to be relative but let's just select
1065:43 be relative but let's just select everything from the shows table where
1065:45 everything from the shows table where the title thereof is the office let's
1065:48 the title thereof is the office let's see how long this relatively simple
1065:50 see how long this relatively simple query takes all right not very long at
1065:53 query takes all right not very long at all in real terms like less than a
1065:55 all in real terms like less than a second .035 seconds so not slow by any
1065:59 second .035 seconds so not slow by any means but if you've got hundreds
1066:00 means but if you've got hundreds thousands millions of users like every
1066:02 thousands millions of users like every one of those milliseconds could very
1066:04 one of those milliseconds could very well add up so can we do better well we
1066:07 well add up so can we do better well we can if I do this if I use syntax like
1066:10 can if I do this if I use syntax like this once in the beginning of the design
1066:12 this once in the beginning of the design of my database I create not a table but
1066:16 of my database I create not a table but an index with some name on a specific
1066:19 an index with some name on a specific table on one or more columns I can give
1066:21 table on one or more columns I can give a clue a hint to the database in advance
1066:23 a clue a hint to the database in advance saying please optimize with some Secret
1066:26 saying please optimize with some Secret Sauce searching or selecting on this
1066:29 Sauce searching or selecting on this column in this table so that my searches
1066:31 column in this table so that my searches are faster so let me do this let me go
1066:33 are faster so let me do this let me go back to vs code here let me create an
1066:36 back to vs code here let me create an index called how about title index I
1066:38 index called how about title index I could call it anything I want but I want
1066:40 could call it anything I want but I want to search faster on titles so I'm going
1066:42 to search faster on titles so I'm going to call this a title index where uh
1066:46 to call this a title index where uh rather uh title index on the table
1066:48 rather uh title index on the table called shows and then in parentheses is
1066:50 called shows and then in parentheses is the syntax the column called title so
1066:53 the syntax the column called title so again I've just borrowed this canonical
1066:55 again I've just borrowed this canonical syntax and I've just translated into
1066:57 syntax and I've just translated into something that's TV show specific all
1067:00 something that's TV show specific all right what is this going to do for me
1067:02 right what is this going to do for me once I hit enter this is going to create
1067:04 once I hit enter this is going to create in the computer's memory the database's
1067:06 in the computer's memory the database's memory something called a b tree it's
1067:08 memory something called a b tree it's not a binary tree a b tree is actually a
1067:11 not a binary tree a b tree is actually a potentially more efficient data
1067:13 potentially more efficient data structure that we didn't talk about a
1067:14 structure that we didn't talk about a few weeks back in week five but it looks
1067:16 few weeks back in week five but it looks a little something like this which looks
1067:18 a little something like this which looks similar to a binary tree but does anyone
1067:20 similar to a binary tree but does anyone notice what makes this not a binary tree
1067:31 exactly binary tree by implying two has no more than two children per node but
1067:34 no more than two children per node but here's a perfect example one two three
1067:35 here's a perfect example one two three and there could be four children five
1067:37 and there could be four children five children or more but the effect of that
1067:39 children or more but the effect of that if you have a very wide tree the upside
1067:41 if you have a very wide tree the upside is that it's like very short like it
1067:43 is that it's like very short like it pulls the data higher up closer to the
1067:45 pulls the data higher up closer to the node to the root node and recall that
1067:47 node to the root node and recall that the root node is where we began our
1067:49 the root node is where we began our searches in the past whether it was a
1067:50 searches in the past whether it was a BST a binary search tree even a try or
1067:53 BST a binary search tree even a try or data structures we always began at the
1067:55 data structures we always began at the top so the higher up you can pull the
1067:57 top so the higher up you can pull the data even if it makes the data structure
1067:59 data even if it makes the data structure very wide you're going to be able to do
1068:01 very wide you're going to be able to do boom boom boom look up queries look up
1068:03 boom boom boom look up queries look up data probably much faster certainly than
1068:05 data probably much faster certainly than if it's just a very long list like a
1068:07 if it's just a very long list like a column by default so with that said let
1068:10 column by default so with that said let me go back to vs code I didn't create
1068:13 me go back to vs code I didn't create the index yet let me go ahead and hit
1068:14 the index yet let me go ahead and hit enter and create it all right it took a
1068:16 enter and create it all right it took a minute a moment it took like half a
1068:18 minute a moment it took like half a second which obviously is not that slow
1068:20 second which obviously is not that slow but with more data that could have been
1068:22 but with more data that could have been even slower but it's a onetime operation
1068:24 even slower but it's a onetime operation as of now and now let me hit up and let
1068:27 as of now and now let me hit up and let me select the same data from shows where
1068:30 me select the same data from shows where title equals the office last time just a
1068:33 title equals the office last time just a moment ago it took 0.035 seconds no not
1068:36 moment ago it took 0.035 seconds no not slow but also that's going to add up if
1068:39 slow but also that's going to add up if I have lots of users of IMDb let's go
1068:41 I have lots of users of IMDb let's go ahead now and execute the same query
1068:43 ahead now and execute the same query again how long did that
1068:46 again how long did that take
1068:47 take 0.01 seconds now I mean practically
1068:50 0.01 seconds now I mean practically nothing and so that's the sort of
1068:52 nothing and so that's the sort of opportunity now when you've got lots of
1068:54 opportunity now when you've got lots of data and you want to really speed up
1068:55 data and you want to really speed up these searches these indexes these
1068:56 these searches these indexes these indices that just create for you these
1068:59 indices that just create for you these magical data structures in the databases
1069:01 magical data structures in the databases memory it allows you to search on
1069:02 memory it allows you to search on columns that you are pretty sure you
1069:04 columns that you are pretty sure you want to search on more effectively Now
1069:06 want to search on more effectively Now by contrast if you've ever used like
1069:08 by contrast if you've ever used like Google or Bing or some search engine
1069:09 Google or Bing or some search engine that has advanced search some of those
1069:12 that has advanced search some of those text boxes that you can search more
1069:13 text boxes that you can search more precisely in might very well be slower
1069:16 precisely in might very well be slower why well probably you don't want to go
1069:18 why well probably you don't want to go crazy and just index every column on
1069:20 crazy and just index every column on every table why well what might be the
1069:28 intuition like if logically indexes speed things up why not index everything
1069:30 speed things up why not index everything there's always going to be a tradeoff
1069:32 there's always going to be a tradeoff here what might that
1069:34 here what might that be
1069:36 be yeah yeah it's going to take a lot of
1069:38 yeah yeah it's going to take a lot of storage right this is just a slide on
1069:40 storage right this is just a slide on the screen but like this has to go
1069:42 the screen but like this has to go somewhere like this needs space in the
1069:44 somewhere like this needs space in the computer's memory or on the hard drive
1069:46 computer's memory or on the hard drive or the like and that's fine if you have
1069:48 or the like and that's fine if you have unlimited space but odds are like you
1069:51 unlimited space but odds are like you don't and that's going to get expensive
1069:52 don't and that's going to get expensive for different reasons so maybe you only
1069:54 for different reasons so maybe you only want to index certain columns and
1069:57 want to index certain columns and certain tables and not all of them
1069:59 certain tables and not all of them because you know what even if a user
1070:00 because you know what even if a user really wants to search maybe VI advanced
1070:02 really wants to search maybe VI advanced search on some other column or table
1070:06 search on some other column or table altogether fine if once in a while a
1070:08 altogether fine if once in a while a query is slow like we're probably
1070:09 query is slow like we're probably getting the bigger bang for our buck by
1070:11 getting the bigger bang for our buck by optimizing the common cases the more
1070:13 optimizing the common cases the more popular queries that people actually
1070:16 popular queries that people actually care about
1070:17 care about too all right so let's come full circle
1070:20 too all right so let's come full circle and bring this now back to uh how how we
1070:24 and bring this now back to uh how how we actually began which was with some uh
1070:26 actually began which was with some uh python code so it turns out these are
1070:28 python code so it turns out these are not either or decisions it turns out in
1070:30 not either or decisions it turns out in the real world developers are constantly
1070:32 the real world developers are constantly using one two three languages at once in
1070:35 using one two three languages at once in fact next week I rattled off HTML CSS
1070:38 fact next week I rattled off HTML CSS and JavaScript one of which is a proper
1070:39 and JavaScript one of which is a proper programming language but those languages
1070:41 programming language but those languages are often used together totally normal
1070:44 are often used together totally normal and common to use Python and SQL or Java
1070:47 and common to use Python and SQL or Java in SQL or Swift in SQL or any number of
1070:50 in SQL or Swift in SQL or any number of different combinations with a database
1070:52 different combinations with a database language you might use use your
1070:54 language you might use use your preferred programming language Java
1070:55 preferred programming language Java python C++ to create the user interface
1070:59 python C++ to create the user interface and the logic that builds the uh the
1071:01 and the logic that builds the uh the that implements the program itself but
1071:04 that implements the program itself but for your data like SQL is a really good
1071:05 for your data like SQL is a really good candidates and indeed we've seen already
1071:07 candidates and indeed we've seen already that SQL can just speed up certain
1071:09 that SQL can just speed up certain operations you can change you can uh
1071:11 operations you can change you can uh collapse 15 lines of code into just one
1071:13 collapse 15 lines of code into just one and you can use these things together so
1071:16 and you can use these things together so let me come back to I'm going to quit
1071:17 let me come back to I'm going to quit out of SQL light I'm going to minimize
1071:19 out of SQL light I'm going to minimize my terminal window and here's where we
1071:21 my terminal window and here's where we left off before with favorite .p with
1071:24 left off before with favorite .p with favorites. everything was being stored
1071:27 favorites. everything was being stored in uh favorites. CSV and recall that we
1071:30 in uh favorites. CSV and recall that we eventually imported that CSV file into
1071:33 eventually imported that CSV file into favorites. DB automatically with import
1071:36 favorites. DB automatically with import just so we could start playing around
1071:37 just so we could start playing around with SQL but we can now tie these two
1071:40 with SQL but we can now tie these two together and a way to do that is as
1071:42 together and a way to do that is as follows um cs50 has a library for python
1071:45 follows um cs50 has a library for python you might recall having available uh get
1071:47 you might recall having available uh get string get int get float you don't
1071:50 string get int get float you don't strictly need to use them in Python
1071:51 strictly need to use them in Python because it's much easier to just use the
1071:53 because it's much easier to just use the input function and then try accept and
1071:55 input function and then try accept and convert things to int or float or the
1071:57 convert things to int or float or the like but it's a lot more work to use SQL
1072:00 like but it's a lot more work to use SQL in Python without a third-party Library
1072:02 in Python without a third-party Library a lot of the commercial options or
1072:04 a lot of the commercial options or popular open source options are actually
1072:06 popular open source options are actually just complicated to use so cs50 does
1072:08 just complicated to use so cs50 does have a very useful function inside of
1072:10 have a very useful function inside of its library for python that you should
1072:12 its library for python that you should use and must use for the problem set
1072:14 use and must use for the problem set that just makes it easy to execute
1072:16 that just makes it easy to execute python uh execute SQL inside of your
1072:19 python uh execute SQL inside of your python code but it's built on top of a
1072:22 python code but it's built on top of a very popular open source alternative so
1072:24 very popular open source alternative so you can use that too in the real world
1072:26 you can use that too in the real world so the documentation for that at this
1072:28 so the documentation for that at this URL here but I'll show you what we need
1072:30 URL here but I'll show you what we need to know here by focusing back on
1072:32 to know here by focusing back on favorites.i so what I'm going to do here
1072:36 favorites.i so what I'm going to do here as follows is this let me delete
1072:38 as follows is this let me delete everything from favorites. py except for
1072:42 everything from favorites. py except for let's say uh this from cs50 import SQL
1072:47 let's say uh this from cs50 import SQL in all caps so that's importing a SQL
1072:49 in all caps so that's importing a SQL feature from cs50's library that's going
1072:51 feature from cs50's library that's going to allow me to open a DB file in code
1072:55 to allow me to open a DB file in code how do I do that well let me create a
1072:57 how do I do that well let me create a variable called DB for database though I
1072:59 variable called DB for database though I could call it anything I want let me
1073:01 could call it anything I want let me call this SQL function and pass in using
1073:03 call this SQL function and pass in using special syntax that's not cs50 specific
1073:06 special syntax that's not cs50 specific it's an industry thing SQL light colon
1073:09 it's an industry thing SQL light colon slash slash slash unlike every other URL
1073:13 slash slash slash unlike every other URL you type this one literally has three in
1073:16 you type this one literally has three in this context here and then the name of
1073:18 this context here and then the name of the database which in this case is
1073:19 the database which in this case is favorites. DB so this is just a way of
1073:22 favorites. DB so this is just a way of telling this SQL library that we wrote
1073:24 telling this SQL library that we wrote but that works exactly like third party
1073:26 but that works exactly like third party Alternatives openen favorites. DB using
1073:29 Alternatives openen favorites. DB using the SQL light technology if you will all
1073:33 the SQL light technology if you will all right let's just ask the user a question
1073:34 right let's just ask the user a question give me your favorite um uh problem so
1073:38 give me your favorite um uh problem so we're going to use input instead of get
1073:40 we're going to use input instead of get string but we could use get string but
1073:41 string but we could use get string but they're pretty much the same for our
1073:43 they're pretty much the same for our purposes let's ask the user for their
1073:44 purposes let's ask the user for their favorite and now in Python code let us
1073:49 favorite and now in Python code let us select from favorites. DB all of the
1073:53 select from favorites. DB all of the rows where students specify that problem
1073:57 rows where students specify that problem as their favorite so in SQL alone it
1073:59 as their favorite so in SQL alone it would be this select uh star from
1074:02 would be this select uh star from favorites where problem equals and I'll
1074:07 favorites where problem equals and I'll do um well whatever my favorite's going
1074:10 do um well whatever my favorite's going to be like problem equals Mario for
1074:12 to be like problem equals Mario for instance so if I were just using SQL I
1074:14 instance so if I were just using SQL I would literally write something like
1074:15 would literally write something like that but I'm in a piy file now like I
1074:17 that but I'm in a piy file now like I have to use Python syntax but python
1074:19 have to use Python syntax but python supports strings SQL is just text it's
1074:22 supports strings SQL is just text it's just a string so I could certainly just
1074:24 just a string so I could certainly just put my SQL code in a string perhaps and
1074:27 put my SQL code in a string perhaps and then pass it to a python function and
1074:29 then pass it to a python function and here's the bridge between the two if you
1074:31 here's the bridge between the two if you just treat SQL as any old text we can
1074:33 just treat SQL as any old text we can put it in a string and execute it so let
1074:35 put it in a string and execute it so let me actually do this let me go ahead and
1074:38 me actually do this let me go ahead and create a variable called rows which is
1074:40 create a variable called rows which is eventually going to contain all the rows
1074:41 eventually going to contain all the rows from the database let me go ahead and uh
1074:45 from the database let me go ahead and uh select db. execute this is the one
1074:48 select db. execute this is the one function you need to know about inside
1074:50 function you need to know about inside of cs50's library and it literally
1074:52 of cs50's library and it literally executes a SQL statement and then in
1074:54 executes a SQL statement and then in quotes you pass it literally what you
1074:57 quotes you pass it literally what you want to execute and let me go ahead and
1074:59 want to execute and let me go ahead and close the parenthesis at the end there
1075:01 close the parenthesis at the end there and now let me just try this so for Row
1075:03 and now let me just try this so for Row in rows let's iterate over all of the
1075:05 in rows let's iterate over all of the rows let me go ahead and print out how
1075:10 rows let me go ahead and print out how about uh row quote unquote and what do I
1075:15 about uh row quote unquote and what do I want here uh let's print out the Tim
1075:19 want here uh let's print out the Tim stamp of that person for kicks all right
1075:22 stamp of that person for kicks all right let me open open my terminal window
1075:24 let me open open my terminal window python of favorites. Pi crossing my
1075:26 python of favorites. Pi crossing my fingers here for
1075:29 fingers here for sure
1075:31 sure enter uh uh there we go favorit so I'll
1075:34 enter uh uh there we go favorit so I'll type in
1075:36 type in Mario okay so I got back it's not very
1075:39 Mario okay so I got back it's not very interesting but I got back all of the
1075:40 interesting but I got back all of the timestamps of students who typed in
1075:42 timestamps of students who typed in Mario that we imported into this
1075:44 Mario that we imported into this database well what I really care about
1075:46 database well what I really care about is how popular Mario is so let me change
1075:48 is how popular Mario is so let me change this a little bit let me change this to
1075:50 this a little bit let me change this to count the number of rows and let me keep
1075:53 count the number of rows and let me keep it simple let me give an alias like I
1075:55 it simple let me give an alias like I proposed earlier like as n where N is a
1075:58 proposed earlier like as n where N is a number so that now down here I can
1076:00 number so that now down here I can actually just do this print out the
1076:03 actually just do this print out the value of n all right let me go back to
1076:05 value of n all right let me go back to my terminal window run python to
1076:07 my terminal window run python to favorites. py let me type in Mario enter
1076:10 favorites. py let me type in Mario enter okay 39 now technically I'm cheating
1076:13 okay 39 now technically I'm cheating like honestly if I'm executing select
1076:16 like honestly if I'm executing select count we've seen before it only ever
1076:18 count we've seen before it only ever returns one row not multiple so there's
1076:20 returns one row not multiple so there's really nothing to iterate over but it's
1076:22 really nothing to iterate over but it's working fine it's just just iterating
1076:23 working fine it's just just iterating once but I'm getting lucky so
1076:25 once but I'm getting lucky so technically what I should probably just
1076:27 technically what I should probably just do is this I should probably give myself
1076:29 do is this I should probably give myself like a variable called row set it equal
1076:32 like a variable called row set it equal to the very first row and only row that
1076:36 to the very first row and only row that came back and now print out that rows n
1076:41 came back and now print out that rows n column let me rerun the program I'll
1076:43 column let me rerun the program I'll type in Mario again enter and I still
1076:45 type in Mario again enter and I still see
1076:46 see 39 so this of course I don't strictly
1076:49 39 so this of course I don't strictly need to do this I don't really need a
1076:51 need to do this I don't really need a variable I can do Row Bracket Z instead
1076:54 variable I can do Row Bracket Z instead but let me focus on what this library is
1076:56 but let me focus on what this library is now doing so per the documentation what
1076:59 now doing so per the documentation what the C what the cs-50 execute function
1077:02 the C what the cs-50 execute function always does for you is it returns a list
1077:06 always does for you is it returns a list of dictionaries so if your query returns
1077:10 of dictionaries so if your query returns nothing like no matches you get back an
1077:12 nothing like no matches you get back an empty list like Open Bracket close
1077:14 empty list like Open Bracket close bracket nothing in it any Loop is not
1077:15 bracket nothing in it any Loop is not going to execute anything useful because
1077:17 going to execute anything useful because there's nothing in it if though you get
1077:19 there's nothing in it if though you get back one row you're going to get back a
1077:21 back one row you're going to get back a list of size one in inside of which is a
1077:23 list of size one in inside of which is a single dictionary that dictionary is
1077:26 single dictionary that dictionary is going to have keys that correspond to
1077:28 going to have keys that correspond to whatever you selected be it the columns
1077:30 whatever you selected be it the columns or the count so when I selected star
1077:32 or the count so when I selected star before I could have like I would have
1077:34 before I could have like I would have gotten all of the columns that's how I
1077:35 gotten all of the columns that's how I was able to access timestamp here I'm
1077:38 was able to access timestamp here I'm just selecting count and I don't want to
1077:39 just selecting count and I don't want to have to type this down here that would
1077:41 have to type this down here that would just look kind of atrocious it would
1077:42 just look kind of atrocious it would work but it would look weird to just
1077:44 work but it would look weird to just keep retyping count peren star close pen
1077:47 keep retyping count peren star close pen so I just created an alias called n just
1077:49 so I just created an alias called n just to like make this my life easier or
1077:51 to like make this my life easier or cleaner down here so to be clear the SQL
1077:54 cleaner down here so to be clear the SQL the cs50 execute function returns a list
1077:57 the cs50 execute function returns a list of dictionaries when you're using select
1078:00 of dictionaries when you're using select and that is how I can now get back the
1078:02 and that is how I can now get back the first and only row and then print out
1078:05 first and only row and then print out that Row's n value it is identical to
1078:09 that Row's n value it is identical to let me do this let me highlight this
1078:12 let me do this let me highlight this whole line of text let me in my terminal
1078:15 whole line of text let me in my terminal window run SQL light three of favorites.
1078:17 window run SQL light three of favorites. DB like we did before break let me just
1078:19 DB like we did before break let me just copy paste this query enter that that's
1078:23 copy paste this query enter that that's the table I got back earlier when we
1078:25 the table I got back earlier when we played with SQL manually and so when I
1078:27 played with SQL manually and so when I get back this table here's the key
1078:29 get back this table here's the key here's the value and I only have one row
1078:32 here's the value and I only have one row which is why I'm just blindly indexing
1078:34 which is why I'm just blindly indexing into rows bracket zero because I know
1078:36 into rows bracket zero because I know there's always going to be an answer
1078:37 there's always going to be an answer there it's going to be zero or one or
1078:38 there it's going to be zero or one or more but I know now it's going to be
1078:40 more but I know now it's going to be called n because of this here so what
1078:43 called n because of this here so what have I just done well this is SQL down
1078:45 have I just done well this is SQL down here and this is just me being like a
1078:46 here and this is just me being like a data scientist asking questions about my
1078:48 data scientist asking questions about my data just using like black and white SQL
1078:50 data just using like black and white SQL queries this is me now being a python
1078:52 queries this is me now being a python programmer who wants to talk to a SQL
1078:54 programmer who wants to talk to a SQL database using Python and The Bridge
1078:56 database using Python and The Bridge we're using happens to be the cs-50
1078:58 we're using happens to be the cs-50 library but again there's third party
1079:00 library but again there's third party free libraries you can also use as well
1079:02 free libraries you can also use as well ours is just very simple and indeed the
1079:04 ours is just very simple and indeed the documentation will explain how execute
1079:06 documentation will explain how execute behaves a little differently for inserts
1079:08 behaves a little differently for inserts updates and deletes you don't get back a
1079:10 updates and deletes you don't get back a list because you're not selecting
1079:11 list because you're not selecting anything but you do get back some return
1079:14 anything but you do get back some return values questions on
1079:17 values questions on this that's the last of our python code
1079:20 this that's the last of our python code that ties everything together in
1079:24 that ties everything together in spirit
1079:33 yeah uh what this this one here yes so db. execute by definition
1079:37 here yes so db. execute by definition returns a list of rows and each of those
1079:40 returns a list of rows and each of those rows happens to be a dictionary because
1079:42 rows happens to be a dictionary because it's convenient key value pairs if I'm
1079:45 it's convenient key value pairs if I'm selecting the count of rows I just know
1079:47 selecting the count of rows I just know from Having learned squel an hour ago
1079:49 from Having learned squel an hour ago that this is always going to give me a
1079:51 that this is always going to give me a single row whose column in this case is
1079:54 single row whose column in this case is called n so if I know it's a single row
1079:57 called n so if I know it's a single row I can just blindly just like in C go
1079:59 I can just blindly just like in C go into that list or an array in C and go
1080:01 into that list or an array in C and go to the first location and then treat
1080:03 to the first location and then treat that as the single row what you don't
1080:05 that as the single row what you don't want to do is this even if you the human
1080:08 want to do is this even if you the human know the query returns one row you can't
1080:11 know the query returns one row you can't just magically change the variable name
1080:13 just magically change the variable name to be singular and expect to have only
1080:15 to be singular and expect to have only one value you will always have a list so
1080:17 one value you will always have a list so even if there is only one value in it
1080:19 even if there is only one value in it it's up to you to do something like this
1080:21 it's up to you to do something like this to get at it or if you prefer more
1080:23 to get at it or if you prefer more succinctness you can do rose bracket I
1080:25 succinctness you can do rose bracket I bracket n that'll achieve the same thing
1080:28 bracket n that'll achieve the same thing without a variable
1080:38 yeah good so I have been misleading this whole time and cheating because this is
1080:41 whole time and cheating because this is only ever going to return Mario I'm
1080:43 only ever going to return Mario I'm ignoring the favorite that the human
1080:45 ignoring the favorite that the human typed in here on line five so let me fix
1080:47 typed in here on line five so let me fix that and that's going to lead us to some
1080:49 that and that's going to lead us to some of the problems that arise ultimately
1080:51 of the problems that arise ultimately with SQL the right way to solve that
1080:53 with SQL the right way to solve that problem let me get rid of my terminal
1080:55 problem let me get rid of my terminal window here the right way to solve this
1080:57 window here the right way to solve this problem is not to use an F string like
1080:59 problem is not to use an F string like we did in Python generally because SQL
1081:02 we did in Python generally because SQL queries as we'll see in a moment can be
1081:04 queries as we'll see in a moment can be dangerous when you want to plug in users
1081:07 dangerous when you want to plug in users uh data into a query that you've written
1081:10 uh data into a query that you've written most of in advance you should you must
1081:13 most of in advance you should you must you had better use a placeholder namely
1081:16 you had better use a placeholder namely a question mark in this case this is
1081:19 a question mark in this case this is somewhat specific to cs50's library but
1081:20 somewhat specific to cs50's library but we just borrowed the convention that
1081:22 we just borrowed the convention that like every other Library uses too in the
1081:24 like every other Library uses too in the world of SQL single question marks are
1081:26 world of SQL single question marks are used as placeholders and the way you do
1081:29 used as placeholders and the way you do this is as follows if you want to plug
1081:31 this is as follows if you want to plug in a value for that question mark just
1081:34 in a value for that question mark just like in print F in C you specify as a
1081:37 like in print F in C you specify as a second or a third or fourth argument all
1081:39 second or a third or fourth argument all of the values you want plugged into this
1081:41 of the values you want plugged into this so in C weeks ago we were using percent
1081:44 so in C weeks ago we were using percent s same exact idea in SQL it's a question
1081:47 s same exact idea in SQL it's a question mark that you use instead this now if I
1081:50 mark that you use instead this now if I open back my terminal window and I run
1081:53 open back my terminal window and I run uh python of favorites. type in Mario I
1081:55 uh python of favorites. type in Mario I should still get 39 but now I can also
1081:58 should still get 39 but now I can also type in scratch perhaps and get 44 for
1082:01 type in scratch perhaps and get 44 for that very first piece at zero and that
1082:03 that very first piece at zero and that one is even more popular here so this
1082:05 one is even more popular here so this now is correct it would work to use an F
1082:08 now is correct it would work to use an F string here and then plug in a value
1082:10 string here and then plug in a value like favorite here but you'll see in
1082:13 like favorite here but you'll see in just a moment don't do that you will
1082:14 just a moment don't do that you will expose yourself to potential hack or
1082:17 expose yourself to potential hack or attacks um by trusting the user's input
1082:20 attacks um by trusting the user's input and so in fact let's transition from
1082:21 and so in fact let's transition from that to exact some of these kinds of
1082:23 that to exact some of these kinds of challenges namely two before we wrap up
1082:26 challenges namely two before we wrap up so in the world of SQL especially when
1082:28 so in the world of SQL especially when it's used at scale at the Twitter and
1082:30 it's used at scale at the Twitter and the Googles of the world like a lot of
1082:32 the Googles of the world like a lot of lots of data is probably coming into the
1082:34 lots of data is probably coming into the database all at once because multiple
1082:35 database all at once because multiple people are opening their phones at the
1082:37 people are opening their phones at the same time around the world they're
1082:38 same time around the world they're clicking on the same links roughly at
1082:40 clicking on the same links roughly at the same time around the world when you
1082:41 the same time around the world when you have thousands of people all using your
1082:43 have thousands of people all using your site at once like order of operations is
1082:45 site at once like order of operations is going to be important but unfortunately
1082:47 going to be important but unfortunately in SQL and in other contexts of
1082:49 in SQL and in other contexts of computing there's this risk of what's
1082:51 computing there's this risk of what's known as a race condition so for
1082:52 known as a race condition so for instance has anyone ever seen or liked
1082:54 instance has anyone ever seen or liked this this is like yes the world record
1082:57 this this is like yes the world record egg or it's like this thing that was
1082:59 egg or it's like this thing that was very popular while back it's still kind
1083:01 very popular while back it's still kind of going strong but if you go to the
1083:03 of going strong but if you go to the Instagram profile for world record egg
1083:05 Instagram profile for world record egg uh the goal was to make the most most
1083:07 uh the goal was to make the most most liked Instagram post ever and they did
1083:09 liked Instagram post ever and they did pretty well it's just this it's just a
1083:12 pretty well it's just this it's just a picture of an egg now at the height of
1083:14 picture of an egg now at the height of the popularity like there might have
1083:15 the popularity like there might have been hundreds thousands tens of
1083:17 been hundreds thousands tens of thousands of people clicking pretty much
1083:18 thousands of people clicking pretty much at the same time on this egg so it
1083:21 at the same time on this egg so it actually creates a potential problem
1083:23 actually creates a potential problem with the Integrity of Instagram's data
1083:26 with the Integrity of Instagram's data why well if you're have all these
1083:27 why well if you're have all these requests coming in at once how do you
1083:28 requests coming in at once how do you possibly keep track of all of them and
1083:30 possibly keep track of all of them and update your counter in a way that can
1083:32 update your counter in a way that can keep up with all of that traffic why
1083:34 keep up with all of that traffic why well let's just hypothesize what meta
1083:37 well let's just hypothesize what meta formerly Facebook was doing underneath
1083:38 formerly Facebook was doing underneath the hood with Instagram if this were
1083:40 the hood with Instagram if this were their code so suppose for the sake of
1083:42 their code so suppose for the sake of discussion that Instagram servers are
1083:44 discussion that Instagram servers are using a mix of python and SQL probably
1083:47 using a mix of python and SQL probably not using the cs50 library but they
1083:48 not using the cs50 library but they could absolutely be using those two
1083:50 could absolutely be using those two languages or two others together um
1083:53 languages or two others together um suppose they do this in order to update
1083:55 suppose they do this in order to update the number of likes for that post they
1083:58 the number of likes for that post they first execute a SQL query like select
1084:00 first execute a SQL query like select the current number of likes from a table
1084:03 the current number of likes from a table called posts where the ID of the post
1084:06 called posts where the ID of the post equals whatever the unique identifier is
1084:08 equals whatever the unique identifier is for that spec specific egg in the table
1084:11 for that spec specific egg in the table and then they store the result in this
1084:13 and then they store the result in this row variable just like I did and then
1084:15 row variable just like I did and then they do this they grab uh they create a
1084:17 they do this they grab uh they create a variable called likes they set it equal
1084:19 variable called likes they set it equal to rows bracket Z so the very first row
1084:22 to rows bracket Z so the very first row in the result set and they get the likes
1084:24 in the result set and they get the likes key so this is literally what I just did
1084:26 key so this is literally what I just did with the count let me hypothesize that
1084:28 with the count let me hypothesize that Instagram does something similar with
1084:30 Instagram does something similar with the total number of likes why are they
1084:32 the total number of likes why are they doing this because they then want to
1084:34 doing this because they then want to execute a third line of code that
1084:36 execute a third line of code that executes update the posts table set the
1084:39 executes update the posts table set the new number of likes equal to something
1084:42 new number of likes equal to something where the ID of the post equals this
1084:44 where the ID of the post equals this other thing now notice just like in
1084:46 other thing now notice just like in printf there's the comma separated list
1084:48 printf there's the comma separated list of values they want to update the
1084:50 of values they want to update the current number of likes from the current
1084:52 current number of likes from the current value to the current value plus one so
1084:54 value to the current value plus one so it's likes plus one and then we plug in
1084:56 it's likes plus one and then we plug in the ID for this so suppose this is what
1084:59 the ID for this so suppose this is what Instagram's doing unfortunately whenever
1085:02 Instagram's doing unfortunately whenever you execute multiple lines of code
1085:04 you execute multiple lines of code independently and you're so popular like
1085:06 independently and you're so popular like Instagram that you have thousands
1085:08 Instagram that you have thousands hundreds of thousands of servers
1085:10 hundreds of thousands of servers potentially it is quite possible that if
1085:12 potentially it is quite possible that if you and I and everyone else in the room
1085:13 you and I and everyone else in the room clicks that egg at the same time it's
1085:15 clicks that egg at the same time it's not going to be the case statistically
1085:17 not going to be the case statistically that like three lines of code are
1085:18 that like three lines of code are executed for me and then three lines for
1085:20 executed for me and then three lines for you and then three lines for you they're
1085:22 you and then three lines for you they're probably going to get interspersed like
1085:24 probably going to get interspersed like this gets executed for me and then this
1085:25 this gets executed for me and then this gets executed for you and then they get
1085:27 gets executed for you and then they get back to doing work for me and so forth
1085:29 back to doing work for me and so forth just to kind of multitask just like a
1085:31 just to kind of multitask just like a human might but at a super speed here
1085:33 human might but at a super speed here the problem though is if these lines of
1085:35 the problem though is if these lines of code get interrupted what could go wrong
1085:38 code get interrupted what could go wrong well suppose that Carter and I both
1085:39 well suppose that Carter and I both click the egg at the same time and
1085:41 click the egg at the same time and suppose the current number of likes back
1085:42 suppose the current number of likes back in the day is 100 that stores in this
1085:45 in the day is 100 that stores in this variable the value 100 but if we click
1085:48 variable the value 100 but if we click so close in time we might get back the
1085:50 so close in time we might get back the same answer to this select query as of
1085:53 same answer to this select query as of that moment in time when David and
1085:54 that moment in time when David and Carter clicked it had a 100 likes but
1085:57 Carter clicked it had a 100 likes but then this last line of code is executed
1085:58 then this last line of code is executed for me and then maybe Carter because
1086:01 for me and then maybe Carter because that answer the state of the database
1086:04 that answer the state of the database was stored in this variable then both
1086:07 was stored in this variable then both Carter and I will result in this line of
1086:11 Carter and I will result in this line of code being executed with the same value
1086:13 code being executed with the same value update the post table setting the likes
1086:16 update the post table setting the likes equal to
1086:17 equal to 101 for that post's ID why because again
1086:21 101 for that post's ID why because again if each of these lines of code running
1086:22 if each of these lines of code running on different servers are checking the
1086:25 on different servers are checking the value of the current number of likes but
1086:28 value of the current number of likes but then getting interrupted because Carter
1086:29 then getting interrupted because Carter clicked the darn thing too and then
1086:30 clicked the darn thing too and then resuming their work on my behalf we
1086:33 resuming their work on my behalf we might have a race condition where the
1086:34 might have a race condition where the code is sort of racing to finish but
1086:36 code is sort of racing to finish but getting interrupted by other users
1086:38 getting interrupted by other users clicks and the problem with that is that
1086:40 clicks and the problem with that is that if you are inspecting the value of some
1086:42 if you are inspecting the value of some variable or in this case a database cell
1086:45 variable or in this case a database cell and making a decision based on it like
1086:47 and making a decision based on it like how to update it you might now lose data
1086:49 how to update it you might now lose data and Instagram probably not good for
1086:50 and Instagram probably not good for advertising if they're losing likes and
1086:52 advertising if they're losing likes and so that's probably a problem not to
1086:54 so that's probably a problem not to retain the value 102 and instead insert
1086:57 retain the value 102 and instead insert the number 101 twice it's actually
1087:00 the number 101 twice it's actually similar in spirit to a story that uh was
1087:03 similar in spirit to a story that uh was told in a databases course I took myself
1087:05 told in a databases course I took myself years ago
1087:06 years ago whereby uh it's somewhat analogous to
1087:10 whereby uh it's somewhat analogous to kind of a contrived scenario involving
1087:11 kind of a contrived scenario involving like a refrigerator and this is the
1087:12 like a refrigerator and this is the closest thing to a refrigerator we could
1087:14 closest thing to a refrigerator we could get on stage but imagine you've got like
1087:15 get on stage but imagine you've got like one of these little dorm fridges in your
1087:16 one of these little dorm fridges in your dorm 2 and your roommate and maybe both
1087:19 dorm 2 and your roommate and maybe both of you as the story was told to me
1087:20 of you as the story was told to me really like milk and one of you is at
1087:22 really like milk and one of you is at class but the other of you comes home
1087:23 class but the other of you comes home and you open your dorm fridge and you're
1087:25 and you open your dorm fridge and you're like oh darn it we're out of milk and so
1087:28 like oh darn it we're out of milk and so you close the fridge you walk across the
1087:30 you close the fridge you walk across the street to CVS or some other store and
1087:32 street to CVS or some other store and you get in line to buy some milk
1087:33 you get in line to buy some milk Meanwhile your roommate gets out of
1087:35 Meanwhile your roommate gets out of class they come back to your dorm room
1087:36 class they come back to your dorm room they're really thirsty for some milk
1087:38 they're really thirsty for some milk they open up the fridge they say oh
1087:40 they open up the fridge they say oh we're out of milk and then they take a
1087:42 we're out of milk and then they take a different route perhaps to CVS or some
1087:44 different route perhaps to CVS or some other store nearby get in line to buy
1087:46 other store nearby get in line to buy some milk fast forward some amount of
1087:47 some milk fast forward some amount of time in this very contrived story and
1087:49 time in this very contrived story and what happens oh damn it we now ended up
1087:51 what happens oh damn it we now ended up with two gallons of milk and there's no
1087:53 with two gallons of milk and there's no way we can fit gallons of milk in there
1087:55 way we can fit gallons of milk in there let alone two of them so that's a
1087:57 let alone two of them so that's a problem but what's the relationship to
1088:00 problem but what's the relationship to this here well both of us yeah did
1088:20 what exactly
1088:27 exactly so to summarize both of us had a very similar thought process made a
1088:29 very similar thought process made a similar decision based on the same
1088:30 similar decision based on the same information not realizing that the
1088:32 information not realizing that the information the fridge was in the
1088:34 information the fridge was in the process of being updated and of course
1088:37 process of being updated and of course in the Instagram World happens like this
1088:39 in the Instagram World happens like this in the fridge World it might take a few
1088:41 in the fridge World it might take a few minutes but the problem is ultimately
1088:43 minutes but the problem is ultimately the result of our having made a decision
1088:45 the result of our having made a decision about the state of the world and the
1088:47 about the state of the world and the state of the world was in the middle of
1088:49 state of the world was in the middle of being updated the queries got mingled
1088:52 being updated the queries got mingled with others or in this case someone was
1088:54 with others or in this case someone was already on their way to the store so
1088:55 already on their way to the store so what's the solution in the real world
1088:57 what's the solution in the real world well you could you know very simply like
1088:58 well you could you know very simply like take a Post-It note and put like gone
1089:00 take a Post-It note and put like gone for milk so as to communicate to your
1089:02 for milk so as to communicate to your roommate that they should not inspect
1089:04 roommate that they should not inspect the value of that variable and make a
1089:06 the value of that variable and make a decision on it why because it's not yet
1089:08 decision on it why because it's not yet consistent with the outcome that's about
1089:11 consistent with the outcome that's about to happen you could be more traumatic
1089:12 to happen you could be more traumatic and you could actually lock the fridge
1089:14 and you could actually lock the fridge somehow put a pad lock around it or the
1089:16 somehow put a pad lock around it or the like so they can't even get in there and
1089:18 like so they can't even get in there and that would achieve the result uh the
1089:19 that would achieve the result uh the same effect too and that is actually
1089:22 same effect too and that is actually pretty much the solution to this problem
1089:23 pretty much the solution to this problem in code too it's not safe it's not
1089:25 in code too it's not safe it's not sufficient to only execute three lines
1089:28 sufficient to only execute three lines of code like this rather what you
1089:30 of code like this rather what you probably want to do is use additional
1089:33 probably want to do is use additional SQL keywords that we won't spend much
1089:35 SQL keywords that we won't spend much time on in the class itself but these
1089:37 time on in the class itself but these there are solutions to this problem you
1089:39 there are solutions to this problem you can begin what's called a transaction
1089:41 can begin what's called a transaction and you can more explicitly commit to
1089:44 and you can more explicitly commit to making a decision like updating the
1089:46 making a decision like updating the database to 10 1001 or 10 1002 or if you
1089:48 database to 10 1001 or 10 1002 or if you realize wait a minute Carter's query
1089:51 realize wait a minute Carter's query inist interrupting mine let me roll back
1089:53 inist interrupting mine let me roll back to the previous state and just uh rewind
1089:55 to the previous state and just uh rewind let me undo contrl Z if you will there's
1089:58 let me undo contrl Z if you will there's also another keyword that's not so much
1090:00 also another keyword that's not so much used anymore in SQL which is locking you
1090:02 used anymore in SQL which is locking you could literally back in the day lock the
1090:04 could literally back in the day lock the entire database table preventing anyone
1090:07 entire database table preventing anyone from updating it or making changes or
1090:09 from updating it or making changes or even reading it while someone else was
1090:11 even reading it while someone else was accessing it that was a very
1090:12 accessing it that was a very heavy-handed solution because it slowed
1090:13 heavy-handed solution because it slowed everything down but in short
1090:15 everything down but in short transactions are now a feature of SQL
1090:18 transactions are now a feature of SQL that you won't necessarily need to use
1090:19 that you won't necessarily need to use yourselves that do solve this problem by
1090:22 yourselves that do solve this problem by doing the equivalent of saying while
1090:24 doing the equivalent of saying while David's like counter is in the process
1090:26 David's like counter is in the process of being updated keep Carter at Bay
1090:28 of being updated keep Carter at Bay ideally briefly and then let his data go
1090:31 ideally briefly and then let his data go through too it's equivalent too to
1090:32 through too it's equivalent too to putting a note or a lock on the fridge
1090:34 putting a note or a lock on the fridge and indeed I mean lock litter they they
1090:36 and indeed I mean lock litter they they were Once Upon a Time called and still
1090:38 were Once Upon a Time called and still are in some texts called locks on
1090:41 are in some texts called locks on databases 2 and the last Pro and the
1090:44 databases 2 and the last Pro and the code for which you might do this is
1090:46 code for which you might do this is almost the same you simply wrap the
1090:49 almost the same you simply wrap the three queries uh with a transaction
1090:52 three queries uh with a transaction statement and a uh commit and the term
1090:54 statement and a uh commit and the term of art here is that this makes your uh
1090:57 of art here is that this makes your uh your uh statements Atomic so Atomic
1091:01 your uh statements Atomic so Atomic means they're either all executed or not
1091:03 means they're either all executed or not at all that is they're all very tightly
1091:04 at all that is they're all very tightly coupled together without interruption
1091:06 coupled together without interruption transactions solves that problem and
1091:08 transactions solves that problem and avoid having two gallons of milk and the
1091:11 avoid having two gallons of milk and the last problem that arises that Is
1091:13 last problem that arises that Is tragically so darn common in the real
1091:15 tragically so darn common in the real world today is what's called a SQL
1091:17 world today is what's called a SQL injection attack and it's what I alluded
1091:19 injection attack and it's what I alluded to earlier with the question mark So
1091:21 to earlier with the question mark So suppose you're in the habit of logging
1091:22 suppose you're in the habit of logging in uh to Yale websites with your net ID
1091:25 in uh to Yale websites with your net ID or password or at Harvard your Harvard
1091:26 or password or at Harvard your Harvard key and password as well suppose for the
1091:28 key and password as well suppose for the sake of discussion that the people that
1091:30 sake of discussion that the people that implemented like Harvard key login allow
1091:33 implemented like Harvard key login allow you to type in your email address of
1091:34 you to type in your email address of course and your password but suppose
1091:36 course and your password but suppose that they are using SQL underneath the
1091:39 that they are using SQL underneath the hood to check your username and password
1091:41 hood to check your username and password to make sure that you are David menen or
1091:43 to make sure that you are David menen or Carter zeni or whoever you claim to be I
1091:46 Carter zeni or whoever you claim to be I haven't shown you the syntax yet but it
1091:48 haven't shown you the syntax yet but it turns out that in SQL Das Dash is a
1091:51 turns out that in SQL Das Dash is a special way of indicating a comment it
1091:53 special way of indicating a comment it means ignore everything to the right so
1091:55 means ignore everything to the right so it's just like SL slash and C or the
1091:57 it's just like SL slash and C or the hash symbol in Python dash dash just
1092:00 hash symbol in Python dash dash just means ignore everything to the right and
1092:01 means ignore everything to the right and we've of course seen single quotes So
1092:04 we've of course seen single quotes So one way to wage a SQL injection attack
1092:07 one way to wage a SQL injection attack is to try to inject malicious SQL code
1092:10 is to try to inject malicious SQL code into someone else's database without
1092:11 into someone else's database without them realizing it how do you do this
1092:13 them realizing it how do you do this well suppose I log in as M harvard.edu
1092:15 well suppose I log in as M harvard.edu single quote Das Dash I'm not double
1092:19 single quote Das Dash I'm not double quoting anything clearly and there's
1092:22 quoting anything clearly and there's nothing to the right of the dash dash
1092:24 nothing to the right of the dash dash anyway but it this imbalance is going to
1092:27 anyway but it this imbalance is going to be useful why because if I'm a hacker
1092:29 be useful why because if I'm a hacker and I'm presuming you know someone at
1092:31 and I'm presuming you know someone at Harvard probably is using SQL uh single
1092:33 Harvard probably is using SQL uh single quotes to wrap the user's email address
1092:36 quotes to wrap the user's email address and wrap the user's password what if I
1092:38 and wrap the user's password what if I try to like complete their thought for
1092:40 try to like complete their thought for them and close one of those quotes for
1092:42 them and close one of those quotes for them what might happen well we could do
1092:45 them what might happen well we could do this here for instance let me
1092:46 this here for instance let me hypothesize is the code that Harvard
1092:48 hypothesize is the code that Harvard wrote hopefully not underneath the hood
1092:50 wrote hopefully not underneath the hood so they're using CS library and Python
1092:52 so they're using CS library and Python and they're using SQL inside suppose
1092:54 and they're using SQL inside suppose that they have a query like this select
1092:57 that they have a query like this select star from users where username equals uh
1093:01 star from users where username equals uh question mark and password equals
1093:03 question mark and password equals question mark and then suppose they just
1093:04 question mark and then suppose they just plug in whatever username and password
1093:06 plug in whatever username and password was typed in and then if they get back
1093:09 was typed in and then if they get back some number of rows dot dot dot they
1093:10 some number of rows dot dot dot they assume I am David they assume Carter is
1093:12 assume I am David they assume Carter is Carter if both the username and password
1093:14 Carter if both the username and password are in the database just end of story
1093:16 are in the database just end of story there this is good this has the question
1093:19 there this is good this has the question mark placeholders we discussed earlier
1093:21 mark placeholders we discussed earlier but what if you don't quite remember
1093:23 but what if you don't quite remember that you don't quite take that to heart
1093:24 that you don't quite take that to heart and you use your more familiar last week
1093:26 and you use your more familiar last week F strings whereby we use these curly
1093:29 F strings whereby we use these curly braces to plug in values what if you do
1093:31 braces to plug in values what if you do this instead so it's almost the same
1093:33 this instead so it's almost the same idea it's still DB execute but now it's
1093:36 idea it's still DB execute but now it's select star from users where username
1093:37 select star from users where username equals and now notice I'm doing the
1093:39 equals and now notice I'm doing the single quotes which is required by SQL
1093:42 single quotes which is required by SQL but I'm using F strings with the curly
1093:43 but I'm using F strings with the curly braces and the password equals single
1093:46 braces and the password equals single quote password and then close single
1093:48 quote password and then close single quote the problem is if you're just
1093:50 quote the problem is if you're just blindly pasting equ effectively the
1093:53 blindly pasting equ effectively the user's input into that web form into the
1093:56 user's input into that web form into the username field and the password field
1093:58 username field and the password field there's nothing stopping a malicious
1094:00 there's nothing stopping a malicious user student faculty staff from
1094:03 user student faculty staff from including a single quote in their name
1094:05 including a single quote in their name or maybe even an uh uh you know
1094:08 or maybe even an uh uh you know benevolently if their name happens to
1094:09 benevolently if their name happens to have a single quote as some last names
1094:11 have a single quote as some last names in particular do so this is very fragile
1094:14 in particular do so this is very fragile why well suppose that if we plug in my
1094:17 why well suppose that if we plug in my malicious value Ma at harvard.edu single
1094:19 malicious value Ma at harvard.edu single quote-- notice what happens to username
1094:22 quote-- notice what happens to username here the username variable inside of the
1094:25 here the username variable inside of the curly quotes will get replaced with this
1094:28 curly quotes will get replaced with this and notice single quote which the
1094:30 and notice single quote which the Harvard programmer wrote M an
1094:33 Harvard programmer wrote M an harvard.edu single quote which I wrote
1094:37 harvard.edu single quote which I wrote dash dash which I wrote single quote
1094:40 dash dash which I wrote single quote which Harvard wrote and whatever else
1094:42 which Harvard wrote and whatever else they want after that what's the
1094:44 they want after that what's the implication though of the dash
1094:50 dash everything to the right is going to be ignored so the password is never even
1094:52 be ignored so the password is never even checked in this scenario I'm tricking
1094:55 checked in this scenario I'm tricking the server into ignoring everything
1094:57 the server into ignoring everything after the dash dash but I have
1094:58 after the dash dash but I have constructed very cleverly very
1095:00 constructed very cleverly very maliciously a syntactically valid query
1095:03 maliciously a syntactically valid query why because I provided the single quote
1095:05 why because I provided the single quote that's going to finish the thought of
1095:07 that's going to finish the thought of that first single quote and now I would
1095:09 that first single quote and now I would only know how to do this if I saw the
1095:11 only know how to do this if I saw the code or if I just randomly try putting
1095:13 code or if I just randomly try putting apostrophes into web forms and see if
1095:15 apostrophes into web forms and see if things break that's often how
1095:16 things break that's often how adversaries attack systems they type in
1095:18 adversaries attack systems they type in potentially dangerous characters hit
1095:19 potentially dangerous characters hit enter if something breaks they're not
1095:21 enter if something breaks they're not necessarily into the system but they
1095:23 necessarily into the system but they know that there might be a vulnerability
1095:25 know that there might be a vulnerability and then they start trying more
1095:26 and then they start trying more methodically things like this so This
1095:28 methodically things like this so This Then is going to be bad because it
1095:29 Then is going to be bad because it effectively Grays out the rest of the
1095:31 effectively Grays out the rest of the query and this query is surely going to
1095:34 query and this query is surely going to return some rows without even knowing my
1095:36 return some rows without even knowing my password and so this logic here dot dot
1095:38 password and so this logic here dot dot dot means well if a data came back from
1095:41 dot means well if a data came back from this query Harvard is presumably going
1095:42 this query Harvard is presumably going to assume that men logged in show them
1095:45 to assume that men logged in show them uh show him like uh his account or
1095:48 uh show him like uh his account or whatever is being protected here so in
1095:50 whatever is being protected here so in short using F strings bad using any
1095:53 short using F strings bad using any equivalent like p uh percent s in C bad
1095:56 equivalent like p uh percent s in C bad when it comes to SQL using question
1095:58 when it comes to SQL using question marks or whatever a thirdparty library
1096:00 marks or whatever a thirdparty library like cs50's prescribed is the way to
1096:03 like cs50's prescribed is the way to solve this why because libraries like
1096:06 solve this why because libraries like ours are designed to at least be smart
1096:08 ours are designed to at least be smart and be paranoid and what we will do is
1096:11 and be paranoid and what we will do is this when you use the question marks and
1096:13 this when you use the question marks and the values are plugged in we will escape
1096:17 the values are plugged in we will escape any potentially dangerous characters
1096:19 any potentially dangerous characters inside of those placeholders and so
1096:21 inside of those placeholders and so effectively the single quote will no
1096:23 effectively the single quote will no longer be considered a grammatical
1096:25 longer be considered a grammatical single quote it will just be literally a
1096:27 single quote it will just be literally a character in the username or password so
1096:29 character in the username or password so the library takes care of this for you
1096:32 the library takes care of this for you because you're plugging in the username
1096:33 because you're plugging in the username and password as separate arguments and
1096:36 and password as separate arguments and then we or the third party you're using
1096:38 then we or the third party you're using actually sanitize that is uh clean up
1096:41 actually sanitize that is uh clean up the data and prevent those bad
1096:43 the data and prevent those bad characters now this is kind of an
1096:44 characters now this is kind of an internet meme that went around for a
1096:46 internet meme that went around for a while um if you've ever uh driven a car
1096:48 while um if you've ever uh driven a car been in a car where there's like the
1096:50 been in a car where there's like the automatic reader for tolls uh this
1096:52 automatic reader for tolls uh this person thought it might be funny to try
1096:54 person thought it might be funny to try doing something like this what are they
1096:55 doing something like this what are they presumably doing the presumption here is
1096:58 presumably doing the presumption here is whether or not it worked as unclear is
1097:00 whether or not it worked as unclear is that here's like the end of a actual
1097:02 that here's like the end of a actual license plate number but here's an
1097:04 license plate number but here's an interesting single quote and a semicolon
1097:06 interesting single quote and a semicolon that's especially bad because it means
1097:07 that's especially bad because it means you can maybe execute a second query on
1097:09 you can maybe execute a second query on the database this is someone having fun
1097:11 the database this is someone having fun trying to drop the entire database table
1097:14 trying to drop the entire database table for whatever municipality is scanning
1097:16 for whatever municipality is scanning through cameras uh their license plate
1097:18 through cameras uh their license plate code and I would be remiss if we didn't
1097:20 code and I would be remiss if we didn't end on this note at least in computer
1097:22 end on this note at least in computer science circles um there is someone
1097:25 science circles um there is someone named no relation to the TF name we put
1097:27 named no relation to the TF name we put in the database earlier um little Bobby
1097:30 in the database earlier um little Bobby tables which ends with this XKCD
1097:33 tables which ends with this XKCD comic and if you chuckle if you laugh
1097:35 comic and if you chuckle if you laugh you're now legit SQL
1097:43 programmers nice nice like every CS student out there
1097:45 nice like every CS student out there knows about little Bobby tables so if
1097:47 knows about little Bobby tables so if you name drop little Bobby tables now
1097:48 you name drop little Bobby tables now like you're in all right that's it
1097:50 like you're in all right that's it though for today we will see you next
1099:54 [Music] [Applause]
1100:08 [Music] [Applause]
1100:14 all the way to the top and then you're passing
1100:15 passing down this is for you Yale we love you
1100:18 down this is for you Yale we love you Yale we're here to Har
1100:22 Yale we're here to Har go
1100:39 down it's going to happen it's actually going to happen I
1100:43 happen it's actually going to happen I can't believe this what do you think of
1100:45 can't believe this what do you think of Y they don't think
1100:52 good can't does everyone have it does everyone have their stuff does everyone
1100:54 everyone have their stuff does everyone have their stuff probably that it's
1100:55 have their stuff probably that it's going to beable very
1101:01 small I know what
1101:04 know what houses how many exra how many
1101:16 EXT no F forer yeah just make sure everyone
1101:18 everyone has pass all the car distributed
1101:24 all right we can do it [Applause]
1101:38 [Applause] [Music]
1101:55 what do you think of Y sir go go one more time one
1102:05 [Applause] more there goes
1102:08 more there goes [Applause]
1102:19 [Applause] again
1102:20 again [Applause]
1102:31 all right this is cs50 welcome to week 8 last week we learned how to create read
1102:34 last week we learned how to create read update and delete databases using squl
1102:37 update and delete databases using squl but this
1102:38 but this week Adam everyone happy
1102:52 Halloween all right so this is cs50 and this is week eight already my thanks to
1102:54 this is week eight already my thanks to Adam on today this happy Halloween uh in
1102:57 Adam on today this happy Halloween uh in the coming moments we're going to learn
1103:00 the coming moments we're going to learn all about how the internet itself Works
1103:02 all about how the internet itself Works which of course is a technology that
1103:03 which of course is a technology that like we're all take we all use every day
1103:05 like we're all take we all use every day probably using in some form right now
1103:07 probably using in some form right now but we'll see that if you start to
1103:09 but we'll see that if you start to understand some of the underlying
1103:10 understand some of the underlying building blocks that power the internet
1103:12 building blocks that power the internet itself we can actually start to build
1103:13 itself we can actually start to build interesting things on top of it and a
1103:15 interesting things on top of it and a lot of the apps the websites that you
1103:16 lot of the apps the websites that you all use every day should become all the
1103:18 all use every day should become all the more familiar things that you yourself
1103:20 more familiar things that you yourself VES can create and honestly when things
1103:22 VES can create and honestly when things go wrong you'll have all the more of a
1103:23 go wrong you'll have all the more of a mental model for how things work or are
1103:26 mental model for how things work or are not in fact working so that you can
1103:27 not in fact working so that you can ultimately diagnose diagnose all the
1103:30 ultimately diagnose diagnose all the more issues yourselves so if we take a
1103:32 more issues yourselves so if we take a look at the internet in the early days
1103:34 look at the internet in the early days it pretty much was just this this
1103:35 it pretty much was just this this happens to be of course the geography of
1103:37 happens to be of course the geography of the United States and just some of the
1103:38 the United States and just some of the first uh points on the internet were
1103:41 first uh points on the internet were these here this was so-called arpanet
1103:43 these here this was so-called arpanet back in 1969 and indeed the internet had
1103:46 back in 1969 and indeed the internet had its Origins here in the United States
1103:48 its Origins here in the United States with just a few computers interconnect
1103:50 with just a few computers interconnect somehow initially that of course began
1103:53 somehow initially that of course began to grow over time such that we
1103:55 to grow over time such that we eventually had the West Coast connected
1103:56 eventually had the West Coast connected to the east coast and nowadays what you
1103:58 to the east coast and nowadays what you can think of these dots on the screen is
1104:00 can think of these dots on the screen is representing are these things called
1104:02 representing are these things called routers sort of computers or really
1104:04 routers sort of computers or really servers that somehow have wires or maybe
1104:06 servers that somehow have wires or maybe wireless connections between them that
1104:08 wireless connections between them that allow data to flow from point A to B to
1104:11 allow data to flow from point A to B to C and then this of course has been now
1104:13 C and then this of course has been now magnified across the entire Globe um and
1104:15 magnified across the entire Globe um and even above ground as well so that we can
1104:17 even above ground as well so that we can connect all the more readily uh to
1104:19 connect all the more readily uh to systems anywhere now in order to Route
1104:22 systems anywhere now in order to Route the data from one router to another we
1104:25 the data from one router to another we need to somehow make routing decisions
1104:27 need to somehow make routing decisions and this is the kind of thing that the
1104:29 and this is the kind of thing that the internet service providers the isps of
1104:31 internet service providers the isps of the world just handle for us you and I
1104:33 the world just handle for us you and I plug our Macs our PCS into the network
1104:35 plug our Macs our PCS into the network here at Harvard or equivalently at Yale
1104:37 here at Harvard or equivalently at Yale or we somehow get online via Wi-Fi or
1104:40 or we somehow get online via Wi-Fi or cellular technology and then some of
1104:41 cellular technology and then some of these larger entities these bigger
1104:43 these larger entities these bigger companies or countries handle most of
1104:45 companies or countries handle most of the data getting from point A to point B
1104:48 the data getting from point A to point B and if you think about what these
1104:49 and if you think about what these routers present they're indeed just
1104:51 routers present they're indeed just servers somehow interconnected not
1104:53 servers somehow interconnected not unlike this grid of tiles here for
1104:55 unlike this grid of tiles here for instance back in the zoom days and in
1104:57 instance back in the zoom days and in fact here we have I claim a grid of
1104:59 fact here we have I claim a grid of routers implemented here by the courses
1105:01 routers implemented here by the courses teaching fellows and course assistants
1105:03 teaching fellows and course assistants and Tas and if the goal at hand for
1105:05 and Tas and if the goal at hand for instance is for Phyllis to Route some
1105:08 instance is for Phyllis to Route some piece of information maybe it's an email
1105:10 piece of information maybe it's an email maybe it's a request for a web page in
1105:11 maybe it's a request for a web page in the bottom right hand corner all the way
1105:13 the bottom right hand corner all the way up to say Brian here in the top left
1105:15 up to say Brian here in the top left hand corner suffice it to say each each
1105:17 hand corner suffice it to say each each of these tiles represents a router a
1105:19 of these tiles represents a router a server that can move the data back forth
1105:21 server that can move the data back forth left and right that packet of
1105:23 left and right that packet of information so to speak from Phyllis to
1105:25 information so to speak from Phyllis to Brian could take any number of different
1105:27 Brian could take any number of different possible routes up down left right to go
1105:30 possible routes up down left right to go from the one corner to another so let me
1105:32 from the one corner to another so let me go ahead and hit play on this video here
1105:34 go ahead and hit play on this video here we're in the teaching fellows play the
1105:36 we're in the teaching fellows play the same
1105:37 same [Music]
1105:49 [Music] role
1105:54 all right so in this particular case the data was routed pretty straightforwardly
1105:56 data was routed pretty straightforwardly up and then to the left but suppose that
1105:58 up and then to the left but suppose that one or more of the staff were a bit busy
1106:00 one or more of the staff were a bit busy maybe one of the routers is congested
1106:02 maybe one of the routers is congested that is to say just got way more
1106:04 that is to say just got way more envelopes at a moment in time that it
1106:06 envelopes at a moment in time that it can handle thankfully the design of the
1106:08 can handle thankfully the design of the internet is such that there's often
1106:09 internet is such that there's often multiple ways that data can get from
1106:11 multiple ways that data can get from point A to point B maybe going through
1106:13 point A to point B maybe going through Point C or Point D instead and so
1106:15 Point C or Point D instead and so there's a resilience there even as some
1106:17 there's a resilience there even as some of these servers themselves might go
1106:19 of these servers themselves might go down so allow me to propose that we use
1106:21 down so allow me to propose that we use the same grid of routers now to Route
1106:23 the same grid of routers now to Route the data in a slightly different way
1106:26 the data in a slightly different way this
1106:27 this [Music]
1106:39 [Music] [Applause]
1106:41 [Applause] [Music]
1106:43 [Music] [Applause]
1106:49 [Music] time
1106:56 so success you'll see perhaps later just how many takes it took us to actually
1106:58 how many takes it took us to actually get that routing right but it does in
1107:00 get that routing right but it does in fact manifest that you can uh travel
1107:02 fact manifest that you can uh travel different paths in order to get the data
1107:04 different paths in order to get the data from point A to point B so as we talk
1107:06 from point A to point B so as we talk about routers as you think of the
1107:07 about routers as you think of the internet I mean think of those humans as
1107:09 internet I mean think of those humans as just representing these routers points a
1107:11 just representing these routers points a to p and everywhere in between now how
1107:14 to p and everywhere in between now how did the teaching staff know to route
1107:16 did the teaching staff know to route that packet up and then down or left and
1107:19 that packet up and then down or left and right in order to get get to the
1107:20 right in order to get get to the destination well all of them were
1107:22 destination well all of them were programmed so to speak to understand
1107:24 programmed so to speak to understand protocols it's called TCP and IP
1107:26 protocols it's called TCP and IP Otherwise Known together typically as
1107:28 Otherwise Known together typically as tcpip and you've probably seen these
1107:31 tcpip and you've probably seen these acronyms at some point in the real world
1107:33 acronyms at some point in the real world on the internet on some kind of
1107:35 on the internet on some kind of documentation a text even if you haven't
1107:36 documentation a text even if you haven't really thought hard about it but IP is
1107:38 really thought hard about it but IP is certainly the more common of the two
1107:39 certainly the more common of the two perhaps in common culture so what does
1107:42 perhaps in common culture so what does TCP and IP do for us well really two
1107:44 TCP and IP do for us well really two primary things any computer or any
1107:46 primary things any computer or any teaching staff member who understands
1107:48 teaching staff member who understands tcpip knows how to get data from point A
1107:51 tcpip knows how to get data from point A to point B but how well let's break down
1107:53 to point B but how well let's break down what that problem to be solved is IP
1107:56 what that problem to be solved is IP otherwise known as Internet Protocol is
1107:58 otherwise known as Internet Protocol is a protocol that computers speak that
1108:00 a protocol that computers speak that allow them to know how to address
1108:02 allow them to know how to address computers on the internet and a protocol
1108:05 computers on the internet and a protocol is just a set of conventions that
1108:06 is just a set of conventions that computers adhere to so someone wrote
1108:08 computers adhere to so someone wrote code that probably has a whole lot of
1108:10 code that probably has a whole lot of conditionals that tells the computer
1108:12 conditionals that tells the computer what to do if something happens like if
1108:14 what to do if something happens like if I receive a packet then send it to the
1108:16 I receive a packet then send it to the next server or something like that in
1108:18 next server or something like that in the human world we have protocols too
1108:20 the human world we have protocols too you know pre in healthier times it was
1108:22 you know pre in healthier times it was quite common to sort of extend your hand
1108:23 quite common to sort of extend your hand to another human in order to greet them
1108:25 to another human in order to greet them and if they're following human protocol
1108:28 and if they're following human protocol they would presumably grab your hand and
1108:29 they would presumably grab your hand and shake it at least in a a culture like
1108:31 shake it at least in a a culture like this one here on campus and now that is
1108:34 this one here on campus and now that is a human protocol in that someone
1108:36 a human protocol in that someone initiates it someone responds to it and
1108:38 initiates it someone responds to it and you both sort of know what to do you're
1108:39 you both sort of know what to do you're programmed to know what to do so same
1108:41 programmed to know what to do so same idea with Internet Protocol computers
1108:43 idea with Internet Protocol computers just know what to do when they've been
1108:44 just know what to do when they've been programmed to do so so what does this
1108:46 programmed to do so so what does this mean IP had decides that every compter
1108:50 mean IP had decides that every compter computer in the world will have a unique
1108:52 computer in the world will have a unique address just like uh a the science
1108:55 address just like uh a the science center around the corner might have a
1108:57 center around the corner might have a unique address of one Oxford Street
1108:59 unique address of one Oxford Street Cambridge Massachusetts 02138 USA IP
1109:03 Cambridge Massachusetts 02138 USA IP dictates that every computer on the
1109:04 dictates that every computer on the internet have a unique address of this
1109:06 internet have a unique address of this form and this too is probably something
1109:08 form and this too is probably something you've seen in the real world even if
1109:09 you've seen in the real world even if you haven't thought too hard about it
1109:11 you haven't thought too hard about it it's a number and what's called dotted
1109:13 it's a number and what's called dotted decimal notation which means it's a
1109:15 decimal notation which means it's a decimal number do something do something
1109:17 decimal number do something do something do something so four digits separated by
1109:20 do something so four digits separated by convention by decimal points although
1109:21 convention by decimal points although there are newer and bigger versions of
1109:23 there are newer and bigger versions of the same and these so-called IP
1109:26 the same and these so-called IP addresses that might be as simple as
1109:27 addresses that might be as simple as 1.2.3.4 uniquely identify a computer on
1109:32 1.2.3.4 uniquely identify a computer on the internet uh the numbers have to
1109:34 the internet uh the numbers have to range from 0 to 255 each and that's a
1109:38 range from 0 to 255 each and that's a bit of a hint just as you start to think
1109:39 bit of a hint just as you start to think more computationally if each of these is
1109:41 more computationally if each of these is a number from 0 to 255 how many bits
1109:44 a number from 0 to 255 how many bits does that suggest each number is
1109:46 does that suggest each number is using feel free to shout it
1109:48 using feel free to shout it out how many bits gives us 255 256 total
1109:53 out how many bits gives us 255 256 total possibilities so eight bits that should
1109:55 possibilities so eight bits that should just be sort of a heuristic in your mind
1109:57 just be sort of a heuristic in your mind anytime you hear something that's in the
1109:58 anytime you hear something that's in the range of 0 to 255 or 256 values total
1110:02 range of 0 to 255 or 256 values total think back to week zero which gives us
1110:03 think back to week zero which gives us eight bits plus another eight bits
1110:05 eight bits plus another eight bits another eight bits and another eight
1110:06 another eight bits and another eight bits which is to say an IP address
1110:08 bits which is to say an IP address typically is 32 bits in total now if we
1110:11 typically is 32 bits in total now if we do another bit of quick mental math or
1110:13 do another bit of quick mental math or think back to week zero if every IP
1110:15 think back to week zero if every IP address is 32 bits how many computers
1110:17 address is 32 bits how many computers can we have on the
1110:18 can we have on the internet at once give or take
1110:24 internet at once give or take roughly 4 billion is the ballpark and we
1110:27 roughly 4 billion is the ballpark and we don't need to be super precise for
1110:28 don't need to be super precise for discussion's sake but roughly 4 billion
1110:31 discussion's sake but roughly 4 billion is how high you can account assuming no
1110:32 is how high you can account assuming no negative numbers if you have 32 bits in
1110:35 negative numbers if you have 32 bits in total now that's not terribly uh many
1110:37 total now that's not terribly uh many number uh numbers of addresses
1110:39 number uh numbers of addresses especially considering the number of
1110:41 especially considering the number of humans in the world the number of us
1110:42 humans in the world the number of us that do have laptops or desktops or
1110:44 that do have laptops or desktops or devices more generally phones in our
1110:46 devices more generally phones in our pockets and the like so let me just
1110:48 pockets and the like so let me just stipulate for today's purposes that
1110:49 stipulate for today's purposes that there's even a newer and improved
1110:51 there's even a newer and improved version of Ip otherwise known as version
1110:53 version of Ip otherwise known as version 6 this is version four but still super
1110:56 6 this is version four but still super popular version six uses 128 bits which
1110:59 popular version six uses 128 bits which is a huge number of possible
1111:01 is a huge number of possible premutations I dare say I can't even
1111:03 premutations I dare say I can't even pronounce that number it's so big so
1111:05 pronounce that number it's so big so there are ways around even this
1111:07 there are ways around even this limitation already so every computer has
1111:10 limitation already so every computer has an address like this what does that
1111:12 an address like this what does that really mean well suppose that I was
1111:14 really mean well suppose that I was Phyllis in the story told visually
1111:16 Phyllis in the story told visually earlier and I want to send a message to
1111:17 earlier and I want to send a message to Brian well both Phyllis and Brian have I
1111:19 Brian well both Phyllis and Brian have I IP addresses and suppose that Brian's IP
1111:22 IP addresses and suppose that Brian's IP address happens to be 1.2.3.4 in that
1111:25 address happens to be 1.2.3.4 in that top left hand corner well phyllis's Mac
1111:27 top left hand corner well phyllis's Mac or PC or phone would essentially do the
1111:30 or PC or phone would essentially do the equivalent on this human envelope by
1111:32 equivalent on this human envelope by writing the two address in the middle of
1111:35 writing the two address in the middle of the envelope as is our human convention
1111:37 the envelope as is our human convention like this so this is an envelope a piece
1111:39 like this so this is an envelope a piece of information an email a text message
1111:41 of information an email a text message whatever destined for Brian and so she
1111:43 whatever destined for Brian and so she would have her computer put Brian's IP
1111:45 would have her computer put Brian's IP address in the middle her IP address is
1111:47 address in the middle her IP address is Maybe 5.6.7 do8 so just like our human
1111:50 Maybe 5.6.7 do8 so just like our human convention I might write 5.
1111:53 convention I might write 5. 6.7.8 at the top of the envelope
1111:56 6.7.8 at the top of the envelope 5678 thereby indicating what the return
1111:59 5678 thereby indicating what the return address is and this is helpful because
1112:01 address is and this is helpful because if Brian's computer needs to acknowledge
1112:03 if Brian's computer needs to acknowledge receipt if he needs to reply in some
1112:05 receipt if he needs to reply in some form this way the envelope has all the
1112:07 form this way the envelope has all the information we need but in the real
1112:09 information we need but in the real world servers do a lot of things
1112:10 world servers do a lot of things nowadays not just email but maybe chat
1112:13 nowadays not just email but maybe chat Maybe video conferencing maybe any
1112:15 Maybe video conferencing maybe any number of other services as well and so
1112:17 number of other services as well and so it turns out that an address alone might
1112:19 it turns out that an address alone might not be sufficient because how do how
1112:21 not be sufficient because how do how does Brian's computer know when he opens
1112:23 does Brian's computer know when he opens the envelope so to speak that this
1112:25 the envelope so to speak that this should be interpreted as an email or
1112:27 should be interpreted as an email or interpret it as a chat message or
1112:29 interpret it as a chat message or interpret it as like a video attachment
1112:31 interpret it as like a video attachment that Phyllis has sent well we need some
1112:33 that Phyllis has sent well we need some other mechanism some other hint on this
1112:35 other mechanism some other hint on this envelope to distinguish one type of
1112:37 envelope to distinguish one type of Internet service from another and so
1112:39 Internet service from another and so that's where the other acronym in TCI
1112:42 that's where the other acronym in TCI pip comes in which is TCP so this stands
1112:45 pip comes in which is TCP so this stands for transmission control protocol which
1112:47 for transmission control protocol which is just a different set of conventions
1112:50 is just a different set of conventions that computers adhere to in order to
1112:52 that computers adhere to in order to solve a couple of different problems one
1112:54 solve a couple of different problems one is this problem of distinguishing one
1112:56 is this problem of distinguishing one type of service from another now what
1112:58 type of service from another now what does that mean well humans decades ago
1113:01 does that mean well humans decades ago decided as they started inventing all of
1113:03 decided as they started inventing all of these various internet services the web
1113:05 these various internet services the web being the first one how they might or
1113:07 being the first one how they might or the web now being one of the most
1113:09 the web now being one of the most popular ones they decided to assign
1113:11 popular ones they decided to assign different services that can be used on
1113:13 different services that can be used on the internet unique numbers and so two
1113:15 the internet unique numbers and so two of the most common are these 80 is the
1113:18 of the most common are these 80 is the number that a bunch of humans decided
1113:20 number that a bunch of humans decided years ago will represent what you and I
1113:21 years ago will represent what you and I know as HTTP and we'll talk more
1113:23 know as HTTP and we'll talk more technically in a bit about what HTTP is
1113:26 technically in a bit about what HTTP is but obviously it's the thing that's in
1113:28 but obviously it's the thing that's in the beginning of every URL nowadays or
1113:30 the beginning of every URL nowadays or https which of course has the S added to
1113:33 https which of course has the S added to it and that has its own unique number
1113:35 it and that has its own unique number and for now the S just means secure one
1113:37 and for now the S just means secure one is encrypted or scrambled somehow for
1113:39 is encrypted or scrambled somehow for privacy sake and the other is
1113:41 privacy sake and the other is unencrypted it's a little more
1113:42 unencrypted it's a little more vulnerable to interception so these two
1113:44 vulnerable to interception so these two numbers are what the world decided when
1113:46 numbers are what the world decided when implementing TCP shall uniquely identify
1113:50 implementing TCP shall uniquely identify those services so what does this mean
1113:51 those services so what does this mean well this means that if Brian's computer
1113:53 well this means that if Brian's computer in the story from before is hosting not
1113:55 in the story from before is hosting not like an email server but maybe he has a
1113:57 like an email server but maybe he has a website and Phyllis is requesting
1113:58 website and Phyllis is requesting Brian's homepage or something like that
1114:01 Brian's homepage or something like that she would have her Mac or PC or phone
1114:04 she would have her Mac or PC or phone not only write Brian's IP address in the
1114:07 not only write Brian's IP address in the middle of the envelope but also the
1114:09 middle of the envelope but also the number otherwise known as a port number
1114:11 number otherwise known as a port number that she wants this envelope to be
1114:13 that she wants this envelope to be routed to now 80 would be insecure
1114:16 routed to now 80 would be insecure nowadays HTTP col is sort of p and we
1114:20 nowadays HTTP col is sort of p and we almost always see https colon now so I'm
1114:24 almost always see https colon now so I'm just going to go with best practice and
1114:26 just going to go with best practice and I'm going to add a colon and then the
1114:27 I'm going to add a colon and then the number
1114:29 number 443 at the end of Brian's IP address so
1114:32 443 at the end of Brian's IP address so now I have an IP address for Brian the
1114:34 now I have an IP address for Brian the port number for the service that this is
1114:36 port number for the service that this is relevant to and I'm not going to bother
1114:37 relevant to and I'm not going to bother writing it but it turns out that
1114:39 writing it but it turns out that phyllis's computer would also choose a
1114:40 phyllis's computer would also choose a port number maybe a random port number
1114:42 port number maybe a random port number so that Brian can conversely reply and
1114:45 so that Brian can conversely reply and then the computer can know which
1114:47 then the computer can know which response is in uh coming back for which
1114:50 response is in uh coming back for which request but the most important one is
1114:52 request but the most important one is this one in the two field whereby this
1114:54 this one in the two field whereby this distinguishes this from like an email a
1114:56 distinguishes this from like an email a chat message a video conference session
1114:57 chat message a video conference session or a zoom or whatnot from anything else
1114:59 or a zoom or whatnot from anything else happening on Brian's computer at the
1115:01 happening on Brian's computer at the same time so all this time if you've
1115:03 same time so all this time if you've seen these terms TCP and IP those are
1115:05 seen these terms TCP and IP those are really two of the most important things
1115:08 really two of the most important things that they do but TCP does one other
1115:10 that they do but TCP does one other thing that's super useful too it turns
1115:12 thing that's super useful too it turns out that it's
1115:15 out that it's super common nowadays especially to
1115:17 super common nowadays especially to transmit a lot of media on the internet
1115:19 transmit a lot of media on the internet internet whether it's an image or maybe
1115:21 internet whether it's an image or maybe it's a movie file and it would be a
1115:22 it's a movie file and it would be a little obnoxious to say the least if
1115:24 little obnoxious to say the least if you're downloading a really big file
1115:27 you're downloading a really big file meant that no one else in your dorm room
1115:28 meant that no one else in your dorm room or your household could actually
1115:30 or your household could actually download anything until you're actually
1115:31 download anything until you're actually done so of course multiple people
1115:33 done so of course multiple people nowadays can be on the internet at once
1115:35 nowadays can be on the internet at once even if all of the connections are a
1115:36 even if all of the connections are a little slower but like one person's
1115:38 little slower but like one person's usage does not block someone else's now
1115:40 usage does not block someone else's now how does this work well TCP in
1115:42 how does this work well TCP in conjunction with IP can also allow you
1115:45 conjunction with IP can also allow you to take like a really big image of a cat
1115:46 to take like a really big image of a cat which is the internet of course is
1115:48 which is the internet of course is filled with and take a big image of a
1115:50 filled with and take a big image of a cat or a big video file of a cat and
1115:53 cat or a big video file of a cat and fragment it into multiple pieces so I'm
1115:55 fragment it into multiple pieces so I'm just going to sort of roughly tear it
1115:57 just going to sort of roughly tear it down the middle and then maybe tear it
1115:58 down the middle and then maybe tear it down the middle again so now it's four
1116:00 down the middle again so now it's four different fragments and I'm I'm sorry
1116:02 different fragments and I'm I'm sorry but the computer will be reassembling
1116:04 but the computer will be reassembling these for us and what phyllis's computer
1116:06 these for us and what phyllis's computer could do now if she's like uploading
1116:09 could do now if she's like uploading this picture of a cat to Brian's web
1116:11 this picture of a cat to Brian's web server well she could put one fragment
1116:14 server well she could put one fragment in this envelope and then have three
1116:16 in this envelope and then have three separate envelopes for the other three
1116:18 separate envelopes for the other three fragments and what you could then do on
1116:20 fragments and what you could then do on the outside of this envelope is just
1116:22 the outside of this envelope is just kind of number them somehow and in fact
1116:24 kind of number them somehow and in fact this is something else that TCP and IP
1116:26 this is something else that TCP and IP together would do for us this first
1116:28 together would do for us this first envelope now might say something like
1116:30 envelope now might say something like one out of four in the memo field so to
1116:34 one out of four in the memo field so to speak of the uh metaphorical envelope
1116:37 speak of the uh metaphorical envelope here now this should be enough
1116:38 here now this should be enough information because now if Brian gets
1116:40 information because now if Brian gets all four of these envelopes he
1116:42 all four of these envelopes he presumably knows how to reassemble the
1116:43 presumably knows how to reassemble the picture of the cat in order top to
1116:45 picture of the cat in order top to bottom left to right but more
1116:46 bottom left to right but more importantly suppose that one of the
1116:48 importantly suppose that one of the routers one of the TFS in the video is
1116:51 routers one of the TFS in the video is sort of distracted and they sort of drop
1116:53 sort of distracted and they sort of drop one of the packets and that's a metaphor
1116:55 one of the packets and that's a metaphor actually in practice for when a router
1116:57 actually in practice for when a router gets really busy it's got way too much
1116:58 gets really busy it's got way too much data coming in it might metaphorically
1117:00 data coming in it might metaphorically drop packets what does that mean in
1117:02 drop packets what does that mean in practice I mean it literally just
1117:04 practice I mean it literally just ignores the zeros and ones it doesn't
1117:05 ignores the zeros and ones it doesn't save them to its memory because there's
1117:07 save them to its memory because there's just no room left so it's equivalent to
1117:09 just no room left so it's equivalent to sort of dropping the packet so suppose
1117:11 sort of dropping the packet so suppose now that Brian gets one of four three of
1117:14 now that Brian gets one of four three of four and four of four what can his
1117:16 four and four of four what can his computer infer
1117:18 computer infer now after receiving those three packets
1117:21 now after receiving those three packets one of four three of four and four of
1117:23 one of four three of four and four of four what's the use there yeah I think
1117:26 four what's the use there yeah I think you're you're signaling with your
1117:28 you're you're signaling with your fingers which one did which one can I
1117:30 fingers which one did which one can I call on you yeah so he's missing two out
1117:32 call on you yeah so he's missing two out of four the second of the packets and
1117:33 of four the second of the packets and this is useful now because you could
1117:35 this is useful now because you could imagine he can send some message back to
1117:37 imagine he can send some message back to Phyllis saying hey please retransmit
1117:39 Phyllis saying hey please retransmit number two of four without having to
1117:41 number two of four without having to redownload the entirety of the cat so
1117:43 redownload the entirety of the cat so there's there's an efficiency there as
1117:46 there's there's an efficiency there as well so tcpip allows data really to go
1117:49 well so tcpip allows data really to go from point A to point B while solving a
1117:52 from point A to point B while solving a bunch of these problems along the way so
1117:54 bunch of these problems along the way so nowadays if you ever see mention on your
1117:56 nowadays if you ever see mention on your Mac or PC of your so-called IP address
1117:59 Mac or PC of your so-called IP address that is the sort of problem that's being
1118:02 that is the sort of problem that's being solved questions now on these protocols
1118:05 solved questions now on these protocols these conventions called TCP and
1118:09 these conventions called TCP and IP that's the extent to which we'll need
1118:11 IP that's the extent to which we'll need to understand them won't have to
1118:12 to understand them won't have to implement them per se we'll just take
1118:14 implement them per se we'll just take them Hereafter for
1118:16 them Hereafter for granted any questions that you've ever
1118:18 granted any questions that you've ever been wondering wondering about your home
1118:20 been wondering wondering about your home network
1118:29 yeah a really good question uh how does TCP know that a user got a message
1118:32 TCP know that a user got a message another aspect another feature of TCP is
1118:35 another aspect another feature of TCP is that Brian's computer by design of this
1118:38 that Brian's computer by design of this protocol will also acknowledge the
1118:40 protocol will also acknowledge the packets that he's received and it will
1118:41 packets that he's received and it will do it efficiently if Brian receives all
1118:43 do it efficiently if Brian receives all four packets in a pretty narrow window
1118:45 four packets in a pretty narrow window of time his computer will send to
1118:47 of time his computer will send to phyllis's computer a quick message
1118:48 phyllis's computer a quick message saying essentially received all four
1118:50 saying essentially received all four otherwise he'll say the opposite which
1118:52 otherwise he'll say the opposite which is that I'm missing for for instance two
1118:54 is that I'm missing for for instance two out of four and that just ensures
1118:56 out of four and that just ensures ultimately that all of the data is
1118:57 ultimately that all of the data is indeed uh arrived so that you're not
1119:00 indeed uh arrived so that you're not missing like uh a quarter a quadrant of
1119:03 missing like uh a quarter a quadrant of the cat in question all right but th
1119:05 the cat in question all right but th that's not the only problem that needs
1119:07 that's not the only problem that needs to be solved ultimately we also need to
1119:10 to be solved ultimately we also need to make the internet userfriendly if you
1119:13 make the internet userfriendly if you will and it would be really tedious if
1119:16 will and it would be really tedious if you had to visit websites for instance
1119:18 you had to visit websites for instance by way of of their IP addresses right
1119:21 by way of of their IP addresses right 1.2.3.4 is pretty memorable but there's
1119:24 1.2.3.4 is pretty memorable but there's like 4 billion other possible addresses
1119:26 like 4 billion other possible addresses available and it would be super tedious
1119:28 available and it would be super tedious to remember those it would be bad
1119:30 to remember those it would be bad marketing to advertise those in fact
1119:33 marketing to advertise those in fact most of you probably don't even know the
1119:35 most of you probably don't even know the phone numbers of your closest friends
1119:36 phone numbers of your closest friends and family members anymore because you
1119:38 and family members anymore because you instead store them in your contacts
1119:40 instead store them in your contacts you're in your dress book associating
1119:41 you're in your dress book associating with numbers that are completely opaque
1119:44 with numbers that are completely opaque with actual names or strings if you will
1119:46 with actual names or strings if you will the same is goes for the internet too
1119:49 the same is goes for the internet too even though every computer does have and
1119:50 even though every computer does have and must have a unique IP address
1119:52 must have a unique IP address numerically why well routers or
1119:54 numerically why well routers or computers computers just crunch numbers
1119:56 computers computers just crunch numbers very readily but we humans work better
1119:58 very readily but we humans work better with strings of text we need some system
1120:01 with strings of text we need some system for converting userfriendly strings like
1120:03 for converting userfriendly strings like harvard.edu or yale.edu or google.com to
1120:07 harvard.edu or yale.edu or google.com to the underlying IP addresses and that's
1120:09 the underlying IP addresses and that's where the next acronym comes in today
1120:12 where the next acronym comes in today which is DNS domain name system so this
1120:15 which is DNS domain name system so this is just another technology that's been
1120:17 is just another technology that's been in use for some time now and it's a
1120:19 in use for some time now and it's a collection of servers on the internet
1120:21 collection of servers on the internet that whose purpose in life is to convert
1120:24 that whose purpose in life is to convert domain names to IP addresses and maybe
1120:26 domain names to IP addresses and maybe vice versa as well so let me stipulate
1120:29 vice versa as well so let me stipulate for today's purposes there are some root
1120:31 for today's purposes there are some root DNS servers in the world that long story
1120:33 DNS servers in the world that long story short know about all of the Dooms all of
1120:36 short know about all of the Dooms all of the edus all of the dot dot dot all of
1120:38 the edus all of the dot dot dot all of the other top level domains around the
1120:41 the other top level domains around the world as well as in the US and then
1120:43 world as well as in the US and then there are some smaller DNS servers owned
1120:46 there are some smaller DNS servers owned by companies owned by universities and
1120:48 by companies owned by universities and even in your apartments or homes most
1120:51 even in your apartments or homes most likely indeed if you have a home router
1120:53 likely indeed if you have a home router plugged into the wall somewhere that's
1120:54 plugged into the wall somewhere that's not only routing your data in and out of
1120:56 not only routing your data in and out of your apartment or home or dorm room it's
1120:59 your apartment or home or dorm room it's also typically serving as a local
1121:03 also typically serving as a local therefore faster DNS server a cash if
1121:05 therefore faster DNS server a cash if you will it's sort of locally saving
1121:07 you will it's sort of locally saving your most frequently accessed websites
1121:09 your most frequently accessed websites and their IP addresses just to avoid
1121:11 and their IP addresses just to avoid bothering the bigger more expensive
1121:13 bothering the bigger more expensive busier servers all day long so there's
1121:15 busier servers all day long so there's DNS servers all over if you poke around
1121:17 DNS servers all over if you poke around your settings in Windows or Mac OS or
1121:20 your settings in Windows or Mac OS or Android or iOS you'll see mention of DNS
1121:23 Android or iOS you'll see mention of DNS and you'll probably see the IP addresses
1121:25 and you'll probably see the IP addresses of the servers whose purpose in life is
1121:27 of the servers whose purpose in life is to do this conversion for you but this
1121:28 to do this conversion for you but this is a requisite feature if we just want
1121:30 is a requisite feature if we just want the internet to be user friendly and
1121:32 the internet to be user friendly and allow us to use words instead of numbers
1121:35 allow us to use words instead of numbers alone what's inside of these DNS servers
1121:38 alone what's inside of these DNS servers you know it's essentially a spreadsheet
1121:40 you know it's essentially a spreadsheet or if we can say it more geeky it's
1121:41 or if we can say it more geeky it's essentially like a hash table of some
1121:43 essentially like a hash table of some sort which it has keys and values like
1121:46 sort which it has keys and values like the key is the domain name harvard.edu
1121:48 the key is the domain name harvard.edu gale. edug google.com and the value is
1121:51 gale. edug google.com and the value is the corresponding IP address or in many
1121:53 the corresponding IP address or in many cases IP addresses plural of the
1121:56 cases IP addresses plural of the corresponding servers so here already
1121:58 corresponding servers so here already even though I've drawn it fairly
1122:00 even though I've drawn it fairly abstractly like you would on a
1122:01 abstractly like you would on a chalkboard it's really probably
1122:03 chalkboard it's really probably implemented as some kind of table maybe
1122:04 implemented as some kind of table maybe a hash table maybe a database table
1122:07 a hash table maybe a database table maybe SQL or something like that or
1122:09 maybe SQL or something like that or maybe it's even just a link list or an
1122:11 maybe it's even just a link list or an array we just have to somehow enable
1122:13 array we just have to somehow enable this computer to convert one to the
1122:16 this computer to convert one to the other now just to be super precise
1122:19 other now just to be super precise DNS servers actually convert what are
1122:21 DNS servers actually convert what are called fully qualified domain names
1122:23 called fully qualified domain names which is generally not just harvard.edu
1122:25 which is generally not just harvard.edu but more verbosely
1122:27 but more verbosely www.harvard.edu and
1122:29 www.harvard.edu and www.google.com so the whole thing that
1122:31 www.google.com so the whole thing that you would see as a substring of the URL
1122:34 you would see as a substring of the URL so that's what DNS does and that's what
1122:36 so that's what DNS does and that's what your University your company your home
1122:38 your University your company your home router are doing for you let me pause
1122:41 router are doing for you let me pause here to see if there are any
1122:44 here to see if there are any questions this to is just a technology
1122:47 questions this to is just a technology now we'll take for granted just
1122:50 now we'll take for granted just works questions at all all right so
1122:54 works questions at all all right so let's now transition among our protocols
1122:57 let's now transition among our protocols really to the last for today which will
1122:59 really to the last for today which will set the stage for actually solving
1123:00 set the stage for actually solving problems with these and writing some
1123:02 problems with these and writing some code ultimately um HTTP this is
1123:05 code ultimately um HTTP this is something that you see or hear all day
1123:07 something that you see or hear all day long even though you rarely have to
1123:08 long even though you rarely have to bother typing it anymore odds are if you
1123:10 bother typing it anymore odds are if you go to harvard.edu yale.edu google.com
1123:13 go to harvard.edu yale.edu google.com you don't bother typing HTTP let alone
1123:15 you don't bother typing HTTP let alone https manually anymore why because your
1123:18 https manually anymore why because your browser Auto completes that kind of
1123:19 browser Auto completes that kind of thing just to make life easier but it is
1123:21 thing just to make life easier but it is officially at the beginning of every URL
1123:24 officially at the beginning of every URL you visit either HTTP or the more secure
1123:27 you visit either HTTP or the more secure https whenever you're using your browser
1123:30 https whenever you're using your browser to access some website so
1123:32 to access some website so HTTP stands for hyper text transfer
1123:36 HTTP stands for hyper text transfer protocol and it's uh easily one of the
1123:39 protocol and it's uh easily one of the most popular dare say one of the most
1123:40 most popular dare say one of the most powerful features of the internet
1123:42 powerful features of the internet nowadays but the mental model to have
1123:44 nowadays but the mental model to have here is that HTTP or the web more
1123:46 here is that HTTP or the web more generally is kind of a service that runs
1123:48 generally is kind of a service that runs on top of the internet and maybe Zoom or
1123:51 on top of the internet and maybe Zoom or Microsoft teams is another service that
1123:53 Microsoft teams is another service that runs on top of the internet an iMessage
1123:56 runs on top of the internet an iMessage and Technologies like it is another
1123:57 and Technologies like it is another service that runs on top of the internet
1123:59 service that runs on top of the internet so the internet is really like the lower
1124:01 so the internet is really like the lower level Plumbing the tcpip stuff the DNS
1124:03 level Plumbing the tcpip stuff the DNS stuff that just gets data from point A
1124:05 stuff that just gets data from point A to point B but now and we're in a a
1124:07 to point B but now and we're in a a software development class ultimately
1124:10 software development class ultimately here in cs50 HTTP is the application
1124:13 here in cs50 HTTP is the application Level protocol it's sort of what
1124:15 Level protocol it's sort of what programmers use what companies use what
1124:18 programmers use what companies use what uh developers use ultimately to use the
1124:21 uh developers use ultimately to use the underlying Plumbing to build interesting
1124:23 underlying Plumbing to build interesting and Powerful things so what does this
1124:25 and Powerful things so what does this mean when it comes to accessing Services
1124:28 mean when it comes to accessing Services via HTTP or the more secure
1124:30 via HTTP or the more secure https well here is a representative URL
1124:34 https well here is a representative URL even though you might not type the whole
1124:35 even though you might not type the whole thing if you poke around your address
1124:37 thing if you poke around your address bar this is what's up there with that
1124:39 bar this is what's up there with that said a lot of browsers nowadays are kind
1124:41 said a lot of browsers nowadays are kind of simplifying if not dumbing down what
1124:43 of simplifying if not dumbing down what you see with your human eyes just to
1124:45 you see with your human eyes just to shorten the strings especially on mobile
1124:47 shorten the strings especially on mobile devices but almost always if you click
1124:49 devices but almost always if you click the URL or highlight it then you see the
1124:51 the URL or highlight it then you see the whole thing but on many browsers you
1124:53 whole thing but on many browsers you might only ever see example.com but all
1124:56 might only ever see example.com but all of this information is there it's just
1124:58 of this information is there it's just getting more and more hidden just for
1125:00 getting more and more hidden just for user interfaces sake well it turns out
1125:02 user interfaces sake well it turns out when you visit a URL by default
1125:04 when you visit a URL by default especially if you type nothing after the
1125:06 especially if you type nothing after the do com in this case you're technically
1125:09 do com in this case you're technically implicitly adding a single slash so a
1125:11 implicitly adding a single slash so a single slash denotes the root of the
1125:14 single slash denotes the root of the server that is the default page or
1125:16 server that is the default page or folder in the server and the slash
1125:19 folder in the server and the slash whether or not you type it or not is
1125:20 whether or not you type it or not is implicitly going to be there and that
1125:22 implicitly going to be there and that just means give me the default whatever
1125:23 just means give me the default whatever is at www.example.com give me that page
1125:27 is at www.example.com give me that page or that folder but URLs can be longer
1125:29 or that folder but URLs can be longer than this and more generally there can
1125:32 than this and more generally there can be a path so to speak and this is a term
1125:34 be a path so to speak and this is a term of art a path is some sequence of folder
1125:37 of art a path is some sequence of folder Andor file names after a URL like this
1125:40 Andor file names after a URL like this and so you might see more specifically
1125:42 and so you might see more specifically that a URL contains a very specific file
1125:44 that a URL contains a very specific file this isn't as common nowadays anymore
1125:47 this isn't as common nowadays anymore though we will begin to today by using
1125:49 though we will begin to today by using this technique but if there is a file
1125:51 this technique but if there is a file called literally file. HTML or something
1125:54 called literally file. HTML or something else on the server that file is going to
1125:56 else on the server that file is going to be what this URL pulls up on the
1125:59 be what this URL pulls up on the computer meanwhile you might have slash
1126:01 computer meanwhile you might have slash folder slash which just means show me
1126:03 folder slash which just means show me whatever is inside of this folder or you
1126:05 whatever is inside of this folder or you might have more verbosely folder file.
1126:08 might have more verbosely folder file. HTML which will show you that file in
1126:10 HTML which will show you that file in that folder and meanwhile just to give
1126:13 that folder and meanwhile just to give some other terms of art this is the
1126:14 some other terms of art this is the so-called fully qualified domain name
1126:17 so-called fully qualified domain name and again these vocab don't matter all
1126:19 and again these vocab don't matter all that much but you'll hear or see them
1126:20 that much but you'll hear or see them over time we generally colloquially just
1126:23 over time we generally colloquially just refer to this as the domain name which
1126:26 refer to this as the domain name which is a little less precise but gets the
1126:28 is a little less precise but gets the job done certainly in conversation and
1126:30 job done certainly in conversation and this part here I described briefly
1126:32 this part here I described briefly earlier what's the name for this suffix
1126:36 earlier what's the name for this suffix at the very end of the fully qualified
1126:38 at the very end of the fully qualified domain
1126:39 domain name the yeah yeah top level domain or
1126:43 name the yeah yeah top level domain or TLD and this is just some form of
1126:45 TLD and this is just some form of categorization of the URLs now the
1126:47 categorization of the URLs now the internet in got it start within the
1126:49 internet in got it start within the United States and a lot of the first
1126:51 United States and a lot of the first websites of course came from the US and
1126:52 websites of course came from the US and so For Better or For Worse the sort of
1126:54 so For Better or For Worse the sort of steak was planted in the ground so
1126:55 steak was planted in the ground so generally do indicated at least early on
1126:59 generally do indicated at least early on that it was some kind of commercial
1127:00 that it was some kind of commercial Enterprise a business that owned a
1127:02 Enterprise a business that owned a domain name edu is a educational
1127:05 domain name edu is a educational institution net was some kind of
1127:06 institution net was some kind of network. goov was the US government now
1127:09 network. goov was the US government now there are also country code tlds cctlds
1127:13 there are also country code tlds cctlds like UK or JP every country in the world
1127:17 like UK or JP every country in the world has its own two letter uh TLD um that
1127:21 has its own two letter uh TLD um that might very well be restricted to only
1127:23 might very well be restricted to only servers or companies or people in that
1127:25 servers or companies or people in that uh in that country many of them can be
1127:27 uh in that country many of them can be used by anyone you've used a lot of URLs
1127:29 used by anyone you've used a lot of URLs in this class ending in
1127:32 in this class ending in cs50. that doesn't mean input output um
1127:35 cs50. that doesn't mean input output um it actually is a TLD from another
1127:37 it actually is a TLD from another country that lets anyone on the internet
1127:40 country that lets anyone on the internet um pay for and on an annual basis using
1127:42 um pay for and on an annual basis using that domain. TV for instance you might
1127:45 that domain. TV for instance you might see in some cases like twitch.tv and the
1127:47 see in some cases like twitch.tv and the like um that too is owned by another
1127:50 like um that too is owned by another country that allows others in the
1127:51 country that allows others in the English-speaking World in this case to
1127:53 English-speaking World in this case to use it as though it connotes TV but
1127:55 use it as though it connotes TV but those are just different types of TLS
1127:57 those are just different types of TLS that roughly categorize where the domain
1127:59 that roughly categorize where the domain lives but it doesn't necessarily mean
1128:01 lives but it doesn't necessarily mean it's commercial anymore it doesn't
1128:02 it's commercial anymore it doesn't necessarily mean it's a Network anymore
1128:05 necessarily mean it's a Network anymore for the most part there are hundreds of
1128:07 for the most part there are hundreds of tlds now for better for worse most of
1128:10 tlds now for better for worse most of which are less common than these big
1128:12 which are less common than these big ones um but most anyone can buy most of
1128:14 ones um but most anyone can buy most of them with just some restrictions on
1128:16 them with just some restrictions on things like edu and goov that are still
1128:19 things like edu and goov that are still very much regulated this meanwhile is
1128:22 very much regulated this meanwhile is what we might call the host name www
1128:24 what we might call the host name www it's obviously a super common convention
1128:27 it's obviously a super common convention like almost every website uses www as
1128:29 like almost every website uses www as its host name but that's a human
1128:31 its host name but that's a human convention it's not a requirement and
1128:33 convention it's not a requirement and indeed some websites don't even bother
1128:35 indeed some websites don't even bother having a host name they just use their
1128:37 having a host name they just use their domain to advertise their websites this
1128:40 domain to advertise their websites this now is going to be the scheme or the
1128:42 now is going to be the scheme or the protocol and this is just going to
1128:44 protocol and this is just going to indicate via what protocol the computer
1128:48 indicate via what protocol the computer your Mac your PC your phone should use
1128:50 your Mac your PC your phone should use when accessing content at that address
1128:53 when accessing content at that address because indeed there are other protocols
1128:55 because indeed there are other protocols you can use but for the most part
1128:57 you can use but for the most part will'll only focus on HTTP or
1128:59 will'll only focus on HTTP or equivalently
1129:01 equivalently https all right any questions now on
1129:04 https all right any questions now on those just definitions building blocks
1129:06 those just definitions building blocks of URLs just so we all sort of share a
1129:08 of URLs just so we all sort of share a common
1129:10 common vocabulary any questions at all yeah
1129:13 vocabulary any questions at all yeah what is the
1129:15 what is the local sure we'll come back to this
1129:17 local sure we'll come back to this actually later today there's a a
1129:19 actually later today there's a a technical term known as Local Host which
1129:21 technical term known as Local Host which is a generic name for your computer your
1129:23 is a generic name for your computer your Mac your PC your phone especially when
1129:25 Mac your PC your phone especially when you're doing software development and by
1129:27 you're doing software development and by convention your own computer has not
1129:29 convention your own computer has not only whatever IP address you get from
1129:31 only whatever IP address you get from your University or your internet service
1129:33 your University or your internet service provider it also has a reflexive IP
1129:35 provider it also has a reflexive IP address one that just always refers to
1129:37 address one that just always refers to itself which is 127.0.0.1 and that's
1129:41 itself which is 127.0.0.1 and that's just a human convention humans decided
1129:43 just a human convention humans decided that shall refer always to your computer
1129:45 that shall refer always to your computer and it's actually going to be useful
1129:46 and it's actually going to be useful today and onward because we can use that
1129:47 today and onward because we can use that when development on our own computers
1129:50 when development on our own computers ultimately other questions on URLs IP
1129:53 ultimately other questions on URLs IP DNS or any of these building
1129:57 DNS or any of these building blocks all right so what do we mean by
1130:00 blocks all right so what do we mean by HTTP being a protocol when I extended my
1130:03 HTTP being a protocol when I extended my hand earlier as a human handshake you
1130:05 hand earlier as a human handshake you know a typical human in healthy times
1130:07 know a typical human in healthy times would know to respond in turn well how
1130:09 would know to respond in turn well how does Brian's computer know to respond to
1130:12 does Brian's computer know to respond to phyllis's envelope whatever message is
1130:14 phyllis's envelope whatever message is they're in well assuming that Brian is
1130:16 they're in well assuming that Brian is indeed still a web server in the store
1130:18 indeed still a web server in the store and Phyllis was trying to upload a cat
1130:20 and Phyllis was trying to upload a cat or maybe download a cat from Brian's web
1130:22 or maybe download a cat from Brian's web server inside of phyllis's envelope
1130:25 server inside of phyllis's envelope would have been a message literally in
1130:27 would have been a message literally in text and it's English for the most part
1130:30 text and it's English for the most part and it would contain at the beginning of
1130:32 and it would contain at the beginning of that message literally one of these
1130:33 that message literally one of these English verbs either get which means
1130:36 English verbs either get which means just that get me the homepage get me a
1130:37 just that get me the homepage get me a picture of a cat get me a picture of a
1130:39 picture of a cat get me a picture of a dog or a video or anything else or post
1130:43 dog or a video or anything else or post which often means post that is um put
1130:46 which often means post that is um put that is um upload a picture of a cat or
1130:49 that is um upload a picture of a cat or a dog or something else to the server
1130:50 a dog or something else to the server instead that's not strictly the only use
1130:53 instead that's not strictly the only use cases for these but you can generally
1130:55 cases for these but you can generally think them as one is just getting
1130:56 think them as one is just getting information and the other is posting
1130:58 information and the other is posting from the client from Phyllis to the
1131:00 from the client from Phyllis to the server in this case Brian so those are
1131:02 server in this case Brian so those are the two keywords that we might see and
1131:04 the two keywords that we might see and why is this useful well it turns out we
1131:06 why is this useful well it turns out we can start to see in our own Mac or PC
1131:08 can start to see in our own Mac or PC some of these very same messages for
1131:10 some of these very same messages for instance if Phyllis were visiting not
1131:14 instance if Phyllis were visiting not Brian's but example.com that web server
1131:17 Brian's but example.com that web server inside of her metaphorical envelope
1131:19 inside of her metaphorical envelope there would be a textual message that
1131:21 there would be a textual message that literally starts with get slash then the
1131:24 literally starts with get slash then the word HTTP then the version she's using
1131:27 word HTTP then the version she's using 1.1 is very common two and three are
1131:29 1.1 is very common two and three are becoming more common but HTTP generally
1131:31 becoming more common but HTTP generally looks like this the next line of text in
1131:34 looks like this the next line of text in her envelope would probably say host
1131:37 her envelope would probably say host colon then literally the fully qualified
1131:39 colon then literally the fully qualified domain name of the server she's
1131:41 domain name of the server she's accessing just in case and this happens
1131:43 accessing just in case and this happens super commonly especially on small
1131:44 super commonly especially on small websites if one server is hosting
1131:47 websites if one server is hosting multiple domain names multiple websites
1131:50 multiple domain names multiple websites this just distinguishes which one she
1131:51 this just distinguishes which one she actually wants and then there's usually
1131:53 actually wants and then there's usually a whole bunch of other lines of text as
1131:55 a whole bunch of other lines of text as well so where can you actually see this
1131:58 well so where can you actually see this well let me actually go ahead and do
1132:01 well let me actually go ahead and do this give me just a moment and I'm going
1132:03 this give me just a moment and I'm going to open up on my computer here uh an
1132:06 to open up on my computer here uh an empty Chrome window in incognito mode
1132:08 empty Chrome window in incognito mode generally speaking incognito mode or
1132:10 generally speaking incognito mode or private mode is used when you don't want
1132:12 private mode is used when you don't want there to be left remnants of what
1132:13 there to be left remnants of what websites you visited and it has the
1132:14 websites you visited and it has the effect for software developers of just
1132:16 effect for software developers of just forgetting any things you might have
1132:19 forgetting any things you might have tried already within your browser
1132:20 tried already within your browser including things called cookies more on
1132:22 including things called cookies more on those another time uh your autocomplete
1132:24 those another time uh your autocomplete history and the like so for development
1132:26 history and the like so for development purposes incognito mode is especially
1132:28 purposes incognito mode is especially helpful because it's sort of like
1132:29 helpful because it's sort of like starting with a clean slate every time
1132:31 starting with a clean slate every time you open a new private or incognito mode
1132:33 you open a new private or incognito mode so there's not going to be like any
1132:34 so there's not going to be like any remnants of previous testing or code
1132:36 remnants of previous testing or code that you've been playing with and I'm
1132:38 that you've been playing with and I'm going to go ahead and do this I'm going
1132:39 going to go ahead and do this I'm going to go ahead and uh right click or
1132:42 to go ahead and uh right click or control click on Chrome I'm going to
1132:44 control click on Chrome I'm going to choose inspect and it's going to pull up
1132:46 choose inspect and it's going to pull up this window sometimes on the side
1132:48 this window sometimes on the side sometimes on the bottom I'm going to
1132:49 sometimes on the bottom I'm going to move it to the bottom just so we can see
1132:51 move it to the bottom just so we can see it a little more readily and I'm going
1132:52 it a little more readily and I'm going to zoom in and it's going to look a
1132:54 to zoom in and it's going to look a little Arcane at first and I'm going to
1132:57 little Arcane at first and I'm going to just highlight a few of these tabs we'll
1132:59 just highlight a few of these tabs we'll see here along the top that there's
1133:01 see here along the top that there's elements console sources Network and
1133:03 elements console sources Network and whole bunch of other things as well this
1133:05 whole bunch of other things as well this is sort of the advanced mode in Chrome
1133:07 is sort of the advanced mode in Chrome and Safari and Firefox and Edge have
1133:09 and Safari and Firefox and Edge have their own equivalent of these features
1133:11 their own equivalent of these features they've always been there even if you've
1133:12 they've always been there even if you've never clicked the right button to enable
1133:14 never clicked the right button to enable these features and I'm going to focus
1133:16 these features and I'm going to focus for a moment on network like this
1133:18 for a moment on network like this this is a feature of the browser that's
1133:20 this is a feature of the browser that's going to allow me the programmer in this
1133:21 going to allow me the programmer in this case so the the engineer to just kind of
1133:24 case so the the engineer to just kind of look at what messages my browser is
1133:26 look at what messages my browser is actually sending to a server so let me
1133:28 actually sending to a server so let me go ahead and do something like this let
1133:30 go ahead and do something like this let me go ahead and visit uh for instance uh
1133:34 me go ahead and visit uh for instance uh in my browser here and I'm going to
1133:36 in my browser here and I'm going to shrink the window just a little bit so
1133:37 shrink the window just a little bit so we can see it exactly I'm going to visit
1133:41 we can see it exactly I'm going to visit https uh
1133:44 https uh www.harvard.edu and now I'm going to hit
1133:47 www.harvard.edu and now I'm going to hit enter
1133:48 enter and a whole bunch of stuff just happened
1133:50 and a whole bunch of stuff just happened along the bottom of my screen and I'm
1133:52 along the bottom of my screen and I'm going to try to pull my window up just a
1133:54 going to try to pull my window up just a little bit so we can focus on a subset
1133:56 little bit so we can focus on a subset of this let me pull this up covering up
1133:58 of this let me pull this up covering up really the content of the page focusing
1134:00 really the content of the page focusing on these lower level details down here
1134:03 on these lower level details down here and what I want to see first
1134:06 and what I want to see first is let me oh sorry let me go ahead and
1134:11 is let me oh sorry let me go ahead and reload this page here after retaining
1134:13 reload this page here after retaining the log so that we can see absolutely
1134:15 the log so that we can see absolutely everything on the screen and to be clear
1134:17 everything on the screen and to be clear I just checked because I forgot earlier
1134:19 I just checked because I forgot earlier preserve log because I wanted to
1134:20 preserve log because I wanted to preserve everything on the screen I want
1134:22 preserve everything on the screen I want to see everything all at once and we'll
1134:24 to see everything all at once and we'll see this the very first line of output
1134:26 see this the very first line of output is completely overwhelming with detail
1134:28 is completely overwhelming with detail at first glance but what you'll see here
1134:30 at first glance but what you'll see here if I start to scroll down and down and
1134:33 if I start to scroll down and down and down and down are the so-called request
1134:36 down and down are the so-called request headers and let me zoom in here and what
1134:38 headers and let me zoom in here and what you're seeing inside of chrome inside of
1134:41 you're seeing inside of chrome inside of its Network tab in its so-called
1134:43 its Network tab in its so-called developer tools again this is just for
1134:45 developer tools again this is just for engineering types you'll see all of the
1134:47 engineering types you'll see all of the headers all of the lines of text that
1134:50 headers all of the lines of text that magically were sent by my Mac to
1134:52 magically were sent by my Mac to harvard.edu much like from Phyllis to
1134:55 harvard.edu much like from Phyllis to Brian server in that story so I can see
1134:58 Brian server in that story so I can see exactly what messages are being sent and
1135:00 exactly what messages are being sent and a lot of this we haven't talked about
1135:01 a lot of this we haven't talked about yet but we do see some mention of get
1135:04 yet but we do see some mention of get and we see some mention of Slash and a
1135:06 and we see some mention of Slash and a bunch of other Arcane details but notice
1135:08 bunch of other Arcane details but notice they're all sort of key value pairs with
1135:10 they're all sort of key value pairs with the here indicating what the
1135:12 the here indicating what the corresponding value is now most of this
1135:14 corresponding value is now most of this is not going to be interesting and we're
1135:16 is not going to be interesting and we're not going to focus too much on the weeds
1135:17 not going to focus too much on the weeds of of all of this but it indeed gives us
1135:19 of of all of this but it indeed gives us a sense of what's inside of that virtual
1135:22 a sense of what's inside of that virtual envelope now harvard.edu is one thing
1135:25 envelope now harvard.edu is one thing but there's other uh websites we might
1135:27 but there's other uh websites we might visit as well um and no matter what they
1135:30 visit as well um and no matter what they are we're going to expect ultimately an
1135:32 are we're going to expect ultimately an HTTP response so in addition to a
1135:34 HTTP response so in addition to a computer like my Mac or phyllis's
1135:36 computer like my Mac or phyllis's computer sending a request containing
1135:39 computer sending a request containing gets and host and those details too
1135:41 gets and host and those details too you'll see here in my slide form just
1135:44 you'll see here in my slide form just representative response from the server
1135:47 representative response from the server and notice that key here is that the
1135:49 and notice that key here is that the server is responding in the same version
1135:50 server is responding in the same version of HTTP in this example it's sending
1135:52 of HTTP in this example it's sending back this so-called status code just a
1135:54 back this so-called status code just a numeric code that indicates in this case
1135:56 numeric code that indicates in this case that everything's okay and it includes
1135:59 that everything's okay and it includes this header this HTTP header which again
1136:01 this header this HTTP header which again is just a key value pair saying that the
1136:03 is just a key value pair saying that the type of this content that's coming back
1136:05 type of this content that's coming back from the server is text/html more on
1136:08 from the server is text/html more on HTML in just a little bit but for our
1136:10 HTML in just a little bit but for our purposes now this just means that
1136:12 purposes now this just means that harvard.edu is sending me back a web
1136:14 harvard.edu is sending me back a web page and indeed if we hide all of this
1136:17 page and indeed if we hide all of this techn techical stuff that's the web page
1136:19 techn techical stuff that's the web page that we saw up here with all of the
1136:20 that we saw up here with all of the usual imagery and the like and in fact I
1136:23 usual imagery and the like and in fact I can see this if I scroll back up not to
1136:26 can see this if I scroll back up not to request headers but response headers
1136:29 request headers but response headers you'll see up here that we get back
1136:32 you'll see up here that we get back responses including the date that the
1136:34 responses including the date that the server responded and a whole bunch of
1136:36 server responded and a whole bunch of other details as well and honestly this
1136:38 other details as well and honestly this has always been under your fingertips
1136:40 has always been under your fingertips and it will soon be useful as we start
1136:41 and it will soon be useful as we start making web-based applications ultimately
1136:44 making web-based applications ultimately but this very quickly gets overwhelming
1136:46 but this very quickly gets overwhelming quickly and so better in this might
1136:48 quickly and so better in this might actually be a tool that we can use
1136:50 actually be a tool that we can use within our code space itself so let me
1136:52 within our code space itself so let me go back to vs code here I didn't open
1136:54 go back to vs code here I didn't open any code tabs I'm just going to use my
1136:56 any code tabs I'm just going to use my terminal window for a moment and I'm
1136:57 terminal window for a moment and I'm going to run a couple of commands that
1136:59 going to run a couple of commands that are going to allow me to actually see
1137:01 are going to allow me to actually see what is going on when I request one
1137:04 what is going on when I request one website let me go ahead and use a
1137:05 website let me go ahead and use a command called curl for connect URL and
1137:09 command called curl for connect URL and this is like a command line black and
1137:10 this is like a command line black and white program that's going to pretend to
1137:12 white program that's going to pretend to be a browser and it's going to connect
1137:14 be a browser and it's going to connect to the URL show me the headers but it's
1137:16 to the URL show me the headers but it's not going to show me the images are the
1137:17 not going to show me the images are the graphics which might very well be useful
1137:19 graphics which might very well be useful to the humans but not to me right now as
1137:20 to the humans but not to me right now as the developer so I'm going to do curl
1137:23 the developer so I'm going to do curl I'm going to do Dashi and then I'm going
1137:25 I'm going to do Dashi and then I'm going to do
1137:26 to do https www.harvard.edu as though I'm
1137:31 https www.harvard.edu as though I'm pretending to be a browser requesting
1137:33 pretending to be a browser requesting the home page and what's nice about curl
1137:35 the home page and what's nice about curl is albeit overwhelming too you'll get
1137:38 is albeit overwhelming too you'll get back a whole response from the server
1137:40 back a whole response from the server containing only those header values the
1137:43 containing only those header values the key value pairs inside of the envelope
1137:45 key value pairs inside of the envelope and we'll ignore almost all of these but
1137:47 and we'll ignore almost all of these but here is the response from the server it
1137:49 here is the response from the server it responded using a new and improved
1137:50 responded using a new and improved version of HTTP in this case version two
1137:53 version of HTTP in this case version two and it gave me back a 200 there's my
1137:56 and it gave me back a 200 there's my content type text/html and then this
1137:58 content type text/html and then this Char set happens to do with the encoding
1138:00 Char set happens to do with the encoding if it's Unicode or asky or something
1138:03 if it's Unicode or asky or something else and then there's all this other
1138:04 else and then there's all this other overwhelming detail for now but this is
1138:06 overwhelming detail for now but this is the beginnings of my ability to just
1138:08 the beginnings of my ability to just kind of poke around and see how the
1138:10 kind of poke around and see how the server works and it turns out too that
1138:13 server works and it turns out too that we'll be able to see other potential
1138:16 we'll be able to see other potential responses as well so for instance uh
1138:19 responses as well so for instance uh HTTP might not only return 100 what if I
1138:23 HTTP might not only return 100 what if I do this instead let me go ahead and
1138:25 do this instead let me go ahead and visit c-i
1138:27 visit c-i HTTP
1138:29 HTTP colon uh www.harvard.edu so notice I
1138:32 colon uh www.harvard.edu so notice I deliberately use the insecure version of
1138:34 deliberately use the insecure version of the URL which maybe Harvard's
1138:36 the URL which maybe Harvard's Administration system administration
1138:38 Administration system administration doesn't like anymore well how can they
1138:40 doesn't like anymore well how can they ensure that I the end user the student
1138:44 ensure that I the end user the student nonetheless use
1138:45 nonetheless use https even if I didn't type it myself
1138:48 https even if I didn't type it myself well let me run just that command with
1138:50 well let me run just that command with just HTTP not https and you'll see that
1138:53 just HTTP not https and you'll see that everything is not okay it didn't come
1138:55 everything is not okay it didn't come back with a 200 it came back with 301 in
1138:58 back with a 200 it came back with 301 in this message saying Harvard moved
1138:59 this message saying Harvard moved permanently but here's where you can
1139:01 permanently but here's where you can look for another clue among all of these
1139:04 look for another clue among all of these lines most of which I don't care about
1139:06 lines most of which I don't care about there's a location header colon that's a
1139:09 there's a location header colon that's a little hint to me that says where
1139:10 little hint to me that says where Harvard University has apparently moved
1139:12 Harvard University has apparently moved too on the web and what's different
1139:14 too on the web and what's different about this URL just to be
1139:16 about this URL just to be clear it has the S included and what
1139:19 clear it has the S included and what your browser will do by default because
1139:21 your browser will do by default because Google and Microsoft and Mozilla
1139:23 Google and Microsoft and Mozilla programmed it this way whenever it sees
1139:25 programmed it this way whenever it sees a 301 response instead of 200 it won't
1139:29 a 301 response instead of 200 it won't show you any web page it will look for a
1139:31 show you any web page it will look for a location header find that URL and then
1139:33 location header find that URL and then automatically quote unquote redirect you
1139:36 automatically quote unquote redirect you there to so this is why it doesn't
1139:38 there to so this is why it doesn't matter what we type in the browser
1139:39 matter what we type in the browser Harvard can have its server send these
1139:41 Harvard can have its server send these semi-secret messages to our browsers and
1139:44 semi-secret messages to our browsers and then it will just visit a second URL all
1139:46 then it will just visit a second URL all automatically and you can do this with
1139:48 automatically and you can do this with host names as well suppose that Harvard
1139:51 host names as well suppose that Harvard does not want to standardize on
1139:53 does not want to standardize on harvard.edu why they just want it to
1139:55 harvard.edu why they just want it to always be www maybe it's a branding
1139:58 always be www maybe it's a branding thing maybe it's a technical thing we
1139:59 thing maybe it's a technical thing we can see the exact same response here
1140:02 can see the exact same response here this first tells me when I visit HTTP
1140:06 this first tells me when I visit HTTP harvard.edu with no www Harvard
1140:08 harvard.edu with no www Harvard minimally wants me to be using a secure
1140:10 minimally wants me to be using a secure connection if I then okay fine cooperate
1140:14 connection if I then okay fine cooperate let me go ahead and clear my screen let
1140:16 let me go ahead and clear my screen let me add the s but not the www you can see
1140:19 me add the s but not the www you can see here that it again responded with 301 up
1140:22 here that it again responded with 301 up here and the location now adds the www
1140:26 here and the location now adds the www so it's just a way of bouncing users
1140:27 so it's just a way of bouncing users from one place to another and this is
1140:30 from one place to another and this is all thanks to http boiling down to
1140:32 all thanks to http boiling down to relatively simple messages inside of the
1140:35 relatively simple messages inside of the envelope that tell the computer the
1140:38 envelope that tell the computer the browser in this case how to respond now
1140:41 browser in this case how to respond now odds are you've seen others besides 301
1140:44 odds are you've seen others besides 301 even though you've probably never seen
1140:46 even though you've probably never seen that actual number unless you've done
1140:47 that actual number unless you've done this kind of thing before but there
1140:49 this kind of thing before but there probably is a number that like everyone
1140:51 probably is a number that like everyone in this room has seen if if You' never
1140:52 in this room has seen if if You' never really wondered why is it that number I
1140:55 really wondered why is it that number I think you're smiling what number are you
1140:56 think you're smiling what number are you thinking of yeah so 404 why is 404 well
1141:01 thinking of yeah so 404 why is 404 well 404 indicates by convention not found
1141:04 404 indicates by convention not found and now why the world decided years ago
1141:06 and now why the world decided years ago to show us normal humans on the internet
1141:08 to show us normal humans on the internet 404 is anything significant is unclear
1141:12 404 is anything significant is unclear that's sort of like bad design like what
1141:13 that's sort of like bad design like what do I care if the status code is 404 but
1141:16 do I care if the status code is 404 but it's common enough on the internet that
1141:18 it's common enough on the internet that probably all of us have seen it but that
1141:20 probably all of us have seen it but that just means that some server when you
1141:22 just means that some server when you visit a URL that's incorrect maybe it's
1141:24 visit a URL that's incorrect maybe it's outdated the URL has been changed if you
1141:27 outdated the URL has been changed if you see a 404 it just means that the virtual
1141:30 see a 404 it just means that the virtual envelope that came from the server back
1141:31 envelope that came from the server back to your Mac or PC or phone contains not
1141:34 to your Mac or PC or phone contains not 200 okay not 301 moved permanently but
1141:37 200 okay not 301 moved permanently but 404 not found instead and it's usually
1141:40 404 not found instead and it's usually accompanied by a technical message maybe
1141:42 accompanied by a technical message maybe a cute picture of a cat sort of hiding
1141:44 a cute picture of a cat sort of hiding because it means not found or something
1141:45 because it means not found or something like that the Aesthetics are entirely up
1141:47 like that the Aesthetics are entirely up to the server but that's what the 404
1141:50 to the server but that's what the 404 means and there's other codes too a few
1141:52 means and there's other codes too a few of which you'll use in the coming weeks
1141:53 of which you'll use in the coming weeks as we transition from commandline
1141:55 as we transition from commandline programs in C and python to web-based
1141:58 programs in C and python to web-based ones we've seen a few of these already
1142:00 ones we've seen a few of these already and several of them are kind of variants
1142:02 and several of them are kind of variants of the same thing like these 300 ones
1142:04 of the same thing like these 300 ones here but we'll see others like 403
1142:07 here but we'll see others like 403 Forbidden means like you probably forgot
1142:09 Forbidden means like you probably forgot to log in or you need to log in or
1142:11 to log in or you need to log in or something like that uh internal server
1142:14 something like that uh internal server error right now no in the coming weeks
1142:16 error right now no in the coming weeks you will absolutely encounter 500 errors
1142:18 you will absolutely encounter 500 errors and they're always your fault it's going
1142:20 and they're always your fault it's going to be the equivalent of like a
1142:22 to be the equivalent of like a segmentation fault in C a 500 error
1142:25 segmentation fault in C a 500 error means like you or I screwed up when
1142:26 means like you or I screwed up when writing some code so we're going to see
1142:28 writing some code so we're going to see that but it's just going to be an
1142:29 that but it's just going to be an opportunity for us to fix it if a
1142:31 opportunity for us to fix it if a server's overloaded you often see 503
1142:34 server's overloaded you often see 503 like something's unavailable because
1142:35 like something's unavailable because something's too popular uh or is maybe
1142:38 something's too popular uh or is maybe worse getting attacked um this is an old
1142:40 worse getting attacked um this is an old uh um April Fool's joke 418 is not
1142:43 uh um April Fool's joke 418 is not actually used in practice but someone
1142:45 actually used in practice but someone like took the time to write up an entire
1142:46 like took the time to write up an entire formal text technical proposal so that
1142:48 formal text technical proposal so that servers can respond saying I'm a teapot
1142:50 servers can respond saying I'm a teapot so it's kind of part of Internet lore
1142:52 so it's kind of part of Internet lore and there's other ones of these status
1142:53 and there's other ones of these status codes as well but this is useful because
1142:56 codes as well but this is useful because eventually we'll see in code you can use
1142:59 eventually we'll see in code you can use this understanding uh high level as it
1143:01 this understanding uh high level as it might be of HTTP to do some interesting
1143:04 might be of HTTP to do some interesting and Powerful things so for instance we
1143:07 and Powerful things so for instance we can even send fancier URLs to servers it
1143:10 can even send fancier URLs to servers it turns out as we'll soon see if you send
1143:12 turns out as we'll soon see if you send a message like this get/ search question
1143:16 a message like this get/ search question question mark Q equals cats and then
1143:19 question mark Q equals cats and then HTTP 1.1 or whatever version and you
1143:22 HTTP 1.1 or whatever version and you send that message to Google server
1143:25 send that message to Google server www.google.com this is how you can
1143:27 www.google.com this is how you can specify not just the path of a web page
1143:30 specify not just the path of a web page that you want SL search in this case the
1143:32 that you want SL search in this case the question mark it turns out is going to
1143:34 question mark it turns out is going to be a convention in the internet in the
1143:36 be a convention in the internet in the web specifically for passing hum human
1143:40 web specifically for passing hum human uh user input to the server as well in
1143:43 uh user input to the server as well in fact you've probably never paid close
1143:44 fact you've probably never paid close attention to URLs but they very often
1143:46 attention to URLs but they very often have question question marks they very
1143:48 have question question marks they very often have equal signs and indeed even
1143:50 often have equal signs and indeed even google.com supports a certain key Q in
1143:53 google.com supports a certain key Q in this case for query and you can put
1143:55 this case for query and you can put anything you want after that in order to
1143:57 anything you want after that in order to search for actual cats so if I actually
1143:59 search for actual cats so if I actually go back to Chrome itself here for a
1144:02 go back to Chrome itself here for a moment let me pull back my uh pull back
1144:05 moment let me pull back my uh pull back open my Chrome browser here previously I
1144:08 open my Chrome browser here previously I was using uh incognito mode for
1144:10 was using uh incognito mode for harvard.edu I've gone ahead and Clos
1144:12 harvard.edu I've gone ahead and Clos that window and opened a new one so we
1144:13 that window and opened a new one so we can start fresh by visiting Google
1144:15 can start fresh by visiting Google normally you and I are in the habit of
1144:16 normally you and I are in the habit of going to google.com and searching via
1144:18 going to google.com and searching via the form or nowadays you just type like
1144:20 the form or nowadays you just type like your search query in the browser itself
1144:22 your search query in the browser itself and it brings you automatically to
1144:24 and it brings you automatically to Google or Bing or something else but I
1144:26 Google or Bing or something else but I can really be pedantic here let me go
1144:28 can really be pedantic here let me go ahead and zoom in and I'll manually go
1144:30 ahead and zoom in and I'll manually go to
1144:31 to https
1144:33 https www.google.com search question mark Q
1144:36 www.google.com search question mark Q equals maybe cats now this would not be
1144:39 equals maybe cats now this would not be a very userfriendly experience if all of
1144:41 a very userfriendly experience if all of us had to manually type out something
1144:42 us had to manually type out something crazy like that but that's what the form
1144:45 crazy like that but that's what the form is redirecting you to when you type in
1144:47 is redirecting you to when you type in more user friendly cats into like a text
1144:50 more user friendly cats into like a text box if I hit enter here we'll get back
1144:53 box if I hit enter here we'll get back indeed a whole bunch of search results
1144:55 indeed a whole bunch of search results about cats if I zoom back in and maybe I
1144:57 about cats if I zoom back in and maybe I change it from cats to dogs that too is
1145:00 change it from cats to dogs that too is going to change and notice it's
1145:01 going to change and notice it's pre-populating the text box because
1145:03 pre-populating the text box because Google has written its code in order to
1145:05 Google has written its code in order to do so as well now appr propo of the
1145:09 do so as well now appr propo of the video with which we began today from
1145:12 video with which we began today from yester year one of the better uh Yale
1145:14 yester year one of the better uh Yale pranks over the years um has anyone one
1145:17 pranks over the years um has anyone one actually ever been to uh
1145:19 actually ever been to uh safetyschool.org
1145:21 safetyschool.org and to our friends at Yale watching live
1145:23 and to our friends at Yale watching live hi safetyschool.org so it's kind of fun
1145:27 hi safetyschool.org so it's kind of fun if you actually visit it uh depending on
1145:30 if you actually visit it uh depending on who you are so if I open up a new window
1145:32 who you are so if I open up a new window here and I go to
1145:34 here and I go to https
1145:36 https safetyschool.org zooming
1145:39 safetyschool.org zooming in enter my oh my look at where it goes
1145:44 in enter my oh my look at where it goes now how does that okay okay thank
1145:53 you this is not a cs50 thing someone out there who I don't know who they are for
1145:54 there who I don't know who they are for like 20 years has been paying annually
1145:57 like 20 years has been paying annually for that domain safetyschool.org for
1146:00 for that domain safetyschool.org for just this joke uh but if I now go back
1146:03 just this joke uh but if I now go back into VSS code here in my terminal window
1146:05 into VSS code here in my terminal window and a little more you know
1146:07 and a little more you know sophisticatedly let me do curl dasi HTTP
1146:10 sophisticatedly let me do curl dasi HTTP col andsafety
1146:12 col andsafety school.org the browser of course
1146:14 school.org the browser of course immediately redirected me to the website
1146:15 immediately redirected me to the website but what's going on under underneath the
1146:17 but what's going on under underneath the hood well 20 some years ago someone
1146:19 hood well 20 some years ago someone bought the domain configured the server
1146:21 bought the domain configured the server quite simply to spit this out safety
1146:24 quite simply to spit this out safety school.org for years has moved
1146:26 school.org for years has moved permanently to
1146:28 permanently to www.yale.edu so just a little
1146:31 www.yale.edu so just a little demonstration of what you can do with
1146:32 demonstration of what you can do with just a little bit of understanding of
1146:34 just a little bit of understanding of HTTP that's been a lot already we've
1146:36 HTTP that's been a lot already we've laid the foundation for understanding
1146:37 laid the foundation for understanding how the internet works now we're going
1146:39 how the internet works now we're going to use it with a language called HTML
1146:41 to use it with a language called HTML hypertext markup language CSS cascading
1146:43 hypertext markup language CSS cascading stylesheets and JavaScript a proper
1146:46 stylesheets and JavaScript a proper program in language but first it feels
1146:48 program in language but first it feels opportune to take a 10-minute break we
1146:49 opportune to take a 10-minute break we have some wonderful Halloween candy in
1146:51 have some wonderful Halloween candy in the transcept and we'll be back in 10
1146:52 the transcept and we'll be back in 10 with those
1146:55 with those languages we're headed up to uh
1146:58 languages we're headed up to uh Austin checking out the stadium for the
1147:06 prank a few years ago I was at a math conference and I was sitting around at
1147:07 conference and I was sitting around at the table at dinner with a few other
1147:10 the table at dinner with a few other mathematicians and uh one of them went
1147:13 mathematicians and uh one of them went to Harvard and started telling the story
1147:15 to Harvard and started telling the story of this amazing prank that was you know
1147:19 of this amazing prank that was you know against Harvard and at that point I felt
1147:21 against Harvard and at that point I felt I had to interrupt and said well
1147:23 I had to interrupt and said well actually I can tell you a lot more about
1147:26 actually I can tell you a lot more about that okay okay the idea was perfected in
1147:29 that okay okay the idea was perfected in a dorm room came up with the idea
1147:31 a dorm room came up with the idea actually to prank them with signs at the
1147:33 actually to prank them with signs at the football game we threw some ideas out
1147:35 football game we threw some ideas out there as far as what what the signs
1147:37 there as far as what what the signs would say we uh eventually settled on we
1147:39 would say we uh eventually settled on we suck and my immediate reaction was no
1147:42 suck and my immediate reaction was no this will never work however the problem
1147:45 this will never work however the problem solver in me started thinking well maybe
1147:47 solver in me started thinking well maybe we can make this
1147:49 we can make this work the problem they had to infiltrate
1147:52 work the problem they had to infiltrate Harvard Stadium without getting caught
1147:55 Harvard Stadium without getting caught sneak in 1,800 placards distribute them
1147:58 sneak in 1,800 placards distribute them to unsuspecting Harvard fans and then
1148:00 to unsuspecting Harvard fans and then convince those fans to prank themselves
1148:03 convince those fans to prank themselves it's great we thought about basically
1148:06 it's great we thought about basically every possible thing that could go wrong
1148:09 every possible thing that could go wrong and tried to come up with a solution for
1148:10 and tried to come up with a solution for it and then you put two Reds on top of
1148:12 it and then you put two Reds on top of it they made fake Harvard IDs and fake
1148:15 it they made fake Harvard IDs and fake backstories
1148:17 backstories fake placard designs and a 28 member
1148:20 fake placard designs and a 28 member fake Pep
1148:22 fake Pep Squad on November 20th 2004 a fake
1148:26 Squad on November 20th 2004 a fake Harvard student smuggled the placards
1148:28 Harvard student smuggled the placards into the game what do you think
1148:33 [Music] [Applause]
1148:41 [Applause] of but then trouble what houses
1148:47 of but then trouble what houses how many how many extra
1148:58 are I you know just showed him the front of this ID and all of a sudden he just
1149:00 of this ID and all of a sudden he just ran away and he felt so
1149:02 ran away and he felt so embarrassed having escaped one
1149:04 embarrassed having escaped one confrontation they couldn't risk another
1149:07 confrontation they couldn't risk another it was
1149:08 it was time this just looks like a total mess
1149:11 time this just looks like a total mess we have absolutely no idea if this is
1149:13 we have absolutely no idea if this is going to work look at the it's going to
1149:17 going to work look at the it's going to happen it's actually going to
1149:19 happen it's actually going to happen I can't believe this what was
1149:22 happen I can't believe this what was once a
1149:23 once a prank became a
1149:33 legend and immediately we started hearing chance from the other side
1149:41 you and I think it was at that point in time that we knew we had pulled it
1149:44 time that we knew we had pulled it off on
1149:47 off on there it goes
1149:48 there it goes [Applause]
1149:51 [Applause] again I really think it didn't matter
1149:53 again I really think it didn't matter that Harvard won because of the prank
1149:56 that Harvard won because of the prank for a lot of Yale students in alumni we
1149:59 for a lot of Yale students in alumni we definitely won that
1150:00 definitely won that [Music]
1150:26 Hallen thank you all okay thank you oh here oh all right little Halloween candy
1150:29 here oh all right little Halloween candy all right thank you all so glad I wore
1150:32 all right thank you all so glad I wore the same thing
1150:34 the same thing today so in just a
1150:39 today so in just a moment thank you okay so in just a
1150:43 moment thank you okay so in just a moment we'll transition to understanding
1150:45 moment we'll transition to understanding all the now what we can do with this
1150:47 all the now what we can do with this underlying infrastructure so again HTTP
1150:50 underlying infrastructure so again HTTP and below it TCP IP is all about just
1150:52 and below it TCP IP is all about just getting the data from point A to pointb
1150:54 getting the data from point A to pointb in some standardized way but now let's
1150:56 in some standardized way but now let's talk about HTML this is the language in
1150:58 talk about HTML this is the language in which web pages themselves are written
1151:01 which web pages themselves are written hyper text markup language now some of
1151:03 hyper text markup language now some of you might have used this before to make
1151:04 you might have used this before to make personal homepages some of you might
1151:06 personal homepages some of you might have dabbled even if using some website
1151:08 have dabbled even if using some website to create your own homepage but
1151:10 to create your own homepage but understanding this language is useful
1151:12 understanding this language is useful certainly for creating the Aesthetics of
1151:15 certainly for creating the Aesthetics of a web page can vying data that's of
1151:17 a web page can vying data that's of interest but at the end of the day it
1151:19 interest but at the end of the day it and the language we look at next CSS uh
1151:22 and the language we look at next CSS uh are not programming languages there's
1151:24 are not programming languages there's going to be no functions no Loops uh no
1151:27 going to be no functions no Loops uh no programming logic but we will end today
1151:29 programming logic but we will end today with a teaser of a proper programming
1151:30 with a teaser of a proper programming language called JavaScript via which you
1151:32 language called JavaScript via which you can manipulate all of these various uh
1151:35 can manipulate all of these various uh other languages as well so ultimately
1151:37 other languages as well so ultimately HTML has like two features and this is a
1151:39 HTML has like two features and this is a language that we spend very brief amount
1151:42 language that we spend very brief amount of time on because it really boils down
1151:43 of time on because it really boils down to just a couple of basic ideas and then
1151:46 to just a couple of basic ideas and then vocabulary that you'll build out over
1151:47 vocabulary that you'll build out over time just by Googling looking up
1151:49 time just by Googling looking up references looking at other Pages source
1151:51 references looking at other Pages source code but tags and attributes are what
1151:53 code but tags and attributes are what characterize HTML now what do I mean by
1151:55 characterize HTML now what do I mean by that here for instance is the HTML code
1151:58 that here for instance is the HTML code via which you can make probably the
1152:00 via which you can make probably the simplest of all web pages one that quite
1152:02 simplest of all web pages one that quite simply says in the uh browser window
1152:05 simply says in the uh browser window hello title and hello body for instance
1152:08 hello title and hello body for instance now what does this actually mean if you
1152:09 now what does this actually mean if you imagine opening up uh this code in a
1152:13 imagine opening up uh this code in a browser be it on a Mac or PC or phone
1152:15 browser be it on a Mac or PC or phone you'll see typically like some kind of
1152:16 you'll see typically like some kind of rectangular window and there's usually a
1152:18 rectangular window and there's usually a tab that has the title of that page and
1152:20 tab that has the title of that page and then most of the rectangular region is
1152:22 then most of the rectangular region is the web page itself what you're looking
1152:24 the web page itself what you're looking at then is the code that's going to put
1152:25 at then is the code that's going to put hello title in the title bar in the tab
1152:28 hello title in the title bar in the tab at the very top and down at the bottom
1152:30 at the very top and down at the bottom hello body is going to be all that's in
1152:32 hello body is going to be all that's in the big black and white box that
1152:34 the big black and white box that composes the rest of the browser window
1152:36 composes the rest of the browser window itself now what are the Salient
1152:37 itself now what are the Salient characteristics here that we'll now
1152:39 characteristics here that we'll now start to take for granted well first
1152:40 start to take for granted well first whoops uh first let's go ahead and give
1152:45 whoops uh first let's go ahead and give me just a moment here um and actually do
1152:48 me just a moment here um and actually do something with this code so I'm going to
1152:50 something with this code so I'm going to go ahead and do this back in vs code
1152:51 go ahead and do this back in vs code here I'm going to first create a file
1152:53 here I'm going to first create a file called say hello.html and in this tab
1152:57 called say hello.html and in this tab I'm going to go ahead and really repeat
1152:59 I'm going to go ahead and really repeat exactly that same code now I had this
1153:01 exactly that same code now I had this line first DOC type HTML then I had this
1153:04 line first DOC type HTML then I had this line HTML Lang equals quote unquote n
1153:07 line HTML Lang equals quote unquote n close quote then I had inside of that
1153:10 close quote then I had inside of that head then I had inside of that title
1153:12 head then I had inside of that title then inside of that I had hello title
1153:13 then inside of that I had hello title and I'm doing this quickly because we'll
1153:14 and I'm doing this quickly because we'll tease apart in a moment what it actually
1153:16 tease apart in a moment what it actually all means and then down here below that
1153:20 all means and then down here below that so-called head I had just the text hello
1153:23 so-called head I had just the text hello body so at the moment I that I claim is
1153:26 body so at the moment I that I claim is the entirety of a web page but it
1153:27 the entirety of a web page but it currently lives in my code space so to
1153:30 currently lives in my code space so to speak in a file called hello.html that's
1153:33 speak in a file called hello.html that's fine if I want to create it but how do I
1153:35 fine if I want to create it but how do I how do you how does anyone on the
1153:36 how do you how does anyone on the internet actually view it well to serve
1153:38 internet actually view it well to serve a web page you indeed need a web server
1153:41 a web page you indeed need a web server and it turns out that codespaces comes
1153:43 and it turns out that codespaces comes with one of these pre-installed because
1153:45 with one of these pre-installed because we cs50 staff uh did so for you and what
1153:48 we cs50 staff uh did so for you and what you can do in a terminal window once you
1153:50 you can do in a terminal window once you have an HTML file ready to go that you
1153:53 have an HTML file ready to go that you want the world to see you can literally
1153:55 want the world to see you can literally run in your terminal window
1153:58 run in your terminal window http-server single command and what
1154:00 http-server single command and what that's going to do for you is start a
1154:02 that's going to do for you is start a web server that is to say a program
1154:04 web server that is to say a program whose purpose in life is just to serve
1154:07 whose purpose in life is just to serve web pages and even though probably up
1154:08 web pages and even though probably up until now for years you probably if
1154:10 until now for years you probably if you're like me equate server quote
1154:12 you're like me equate server quote unquote with a physical device server is
1154:15 unquote with a physical device server is really aie piece of software it just
1154:17 really aie piece of software it just tends to run on big fancy devices so
1154:19 tends to run on big fancy devices so when we say server we often all think of
1154:21 when we say server we often all think of in our Mind's Eye you know big expensive
1154:23 in our Mind's Eye you know big expensive devices perhaps but a server is just a
1154:25 devices perhaps but a server is just a program whose purpose in life is to
1154:27 program whose purpose in life is to respond to requests with responses and
1154:30 respond to requests with responses and that's the vernacular there now once you
1154:32 that's the vernacular there now once you run HTTP server and I'm going to do a
1154:34 run HTTP server and I'm going to do a bit of magic because I set this up
1154:35 bit of magic because I set this up before class just to make sure it goes
1154:37 before class just to make sure it goes smoothly you'll see some output like
1154:39 smoothly you'll see some output like this whereby your server is now
1154:42 this whereby your server is now available on a very long URL mine here
1154:46 available on a very long URL mine here uh uh is a very long URL that will be
1154:48 uh uh is a very long URL that will be different from yours but what this is is
1154:50 different from yours but what this is is a unique identifier that your codes
1154:51 a unique identifier that your codes space has temporarily generated so that
1154:54 space has temporarily generated so that you can now access and ideally only you
1154:56 you can now access and ideally only you can access that file using your browser
1155:00 can access that file using your browser now if I flip the URL or you flip the
1155:01 now if I flip the URL or you flip the your all to public by right clicking or
1155:03 your all to public by right clicking or control clicking the right features of
1155:05 control clicking the right features of vs code you can enable anyone in the
1155:07 vs code you can enable anyone in the world to visit it but we're not going to
1155:08 world to visit it but we're not going to ultimately host our websites in your
1155:10 ultimately host our websites in your code space because as soon as you log
1155:12 code space because as soon as you log off for the night and the thing shuts
1155:13 off for the night and the thing shuts down like the website will go down but
1155:15 down like the website will go down but at the end end of the semester
1155:16 at the end end of the semester particularly for final projects we'll
1155:18 particularly for final projects we'll show you ways that you can put your own
1155:20 show you ways that you can put your own website your own code on the actual
1155:22 website your own code on the actual internet
1155:24 internet 247365 even with your own domain name if
1155:26 247365 even with your own domain name if you want to get one so that it lives uh
1155:29 you want to get one so that it lives uh independent of your own sleep schedule
1155:31 independent of your own sleep schedule and usage schedule of vs code here so
1155:33 and usage schedule of vs code here so I'm going to go ahead now and visit um
1155:36 I'm going to go ahead now and visit um this URL in another tab of my browser
1155:39 this URL in another tab of my browser and what I'll see here is this this is
1155:41 and what I'll see here is this this is the output of that program called HTTP
1155:44 the output of that program called HTTP server and essentially what it is doing
1155:46 server and essentially what it is doing is it's using TCP and IP in conjunction
1155:49 is it's using TCP and IP in conjunction with HTTP to just run your very own web
1155:52 with HTTP to just run your very own web server on gith hubs own servers as well
1155:55 server on gith hubs own servers as well and that's because of different ports
1155:57 and that's because of different ports again we won't go too much into the
1155:58 again we won't go too much into the weeds of the TCP the IP and all of that
1156:00 weeds of the TCP the IP and all of that stuff but recall that different port
1156:02 stuff but recall that different port numbers can allow you to distinguish one
1156:04 numbers can allow you to distinguish one service from another now one of the
1156:06 service from another now one of the services is of course your code space
1156:08 services is of course your code space VSS code in the cloud that we've been
1156:10 VSS code in the cloud that we've been using for weeks but if you want to use
1156:12 using for weeks but if you want to use the same physical server that GitHub
1156:14 the same physical server that GitHub controls but actually visit your own web
1156:17 controls but actually visit your own web server that I just ran in my terminal
1156:19 server that I just ran in my terminal window in another tab that's fine
1156:21 window in another tab that's fine they're just going to be using different
1156:22 they're just going to be using different TCP ports and you and I don't have to
1156:24 TCP ports and you and I don't have to care what they are but just that this is
1156:25 care what they are but just that this is a feature that TCP supports so what you
1156:28 a feature that TCP supports so what you see here is somewhat Arcane this is not
1156:30 see here is somewhat Arcane this is not like a thing that most people on the
1156:32 like a thing that most people on the internet should ever see I'm just doing
1156:33 internet should ever see I'm just doing this for development purposes but this
1156:35 this for development purposes but this is the index that is the directory the
1156:37 is the index that is the directory the the folder contents of my code space and
1156:40 the folder contents of my code space and because I deleted everything from prior
1156:42 because I deleted everything from prior weeks already all we see right now is
1156:44 weeks already all we see right now is hello.html
1156:46 hello.html which I just created so if I click on
1156:48 which I just created so if I click on hello.html within this folder listing
1156:51 hello.html within this folder listing you'll be a little underwhelmed and I'll
1156:53 you'll be a little underwhelmed and I'll zoom in just so there's something more
1156:54 zoom in just so there's something more interesting there but now you see hello
1156:57 interesting there but now you see hello comma body but what's interesting
1157:00 comma body but what's interesting perhaps is that after this long very
1157:02 perhaps is that after this long very cryptic and uninteresting URL notice at
1157:05 cryptic and uninteresting URL notice at the very end of it and I'll zoom in in a
1157:06 the very end of it and I'll zoom in in a moment what do you see slh hello.html
1157:10 moment what do you see slh hello.html which follows the convention I claimed
1157:12 which follows the convention I claimed before break is how a browser would
1157:15 before break is how a browser would allow you to to access a specific file
1157:17 allow you to to access a specific file on a server by doing slash and then the
1157:20 on a server by doing slash and then the name of the file name everything before
1157:22 name of the file name everything before it is very cryptic it would be better if
1157:24 it is very cryptic it would be better if I you know uh buy a domain name that's a
1157:26 I you know uh buy a domain name that's a little more easy to remember and set
1157:28 little more easy to remember and set that up some other time but for now
1157:29 that up some other time but for now let's just focus on only the file names
1157:31 let's just focus on only the file names that I'm actually creating all right so
1157:34 that I'm actually creating all right so the code is up and running underwhelming
1157:37 the code is up and running underwhelming though it might be with the body in the
1157:39 though it might be with the body in the middle of the page and let me zoom in up
1157:41 middle of the page and let me zoom in up here too hello comma title is indeed in
1157:43 here too hello comma title is indeed in the tab just as promised so what's
1157:46 the tab just as promised so what's actually going on with code like this
1157:48 actually going on with code like this that we just created well let's go back
1157:49 that we just created well let's go back to the slide version of the same and let
1157:51 to the slide version of the same and let me just highlight a few of these lines
1157:53 me just highlight a few of these lines the very first line is what's called
1157:55 the very first line is what's called your document type declaration doesn't
1157:57 your document type declaration doesn't really matter to remember that by
1157:58 really matter to remember that by phrasing and this is just something you
1158:00 phrasing and this is just something you copy paste or do from memory at the top
1158:02 copy paste or do from memory at the top of any HTML file that you create when
1158:04 of any HTML file that you create when making your own web page it's a implicit
1158:08 making your own web page it's a implicit indicator to the browser that you're
1158:09 indicator to the browser that you're using the very latest version of HTML
1158:12 using the very latest version of HTML which is version five you don't mention
1158:13 which is version five you don't mention the number five just browsers now
1158:15 the number five just browsers now nowadays are program to look for this to
1158:17 nowadays are program to look for this to know that you're using the very latest
1158:19 know that you're using the very latest version of the language languages just
1158:21 version of the language languages just like human languages evolve over time
1158:23 like human languages evolve over time we're up to version five of HTML but new
1158:25 we're up to version five of HTML but new features get added every few years so
1158:27 features get added every few years so indeed this lecture this class has been
1158:29 indeed this lecture this class has been evolving over time too so let's now
1158:31 evolving over time too so let's now focus on the next line as well as the
1158:33 focus on the next line as well as the bottom line and you'll notice some
1158:35 bottom line and you'll notice some deliberate symmetry here this here is
1158:37 deliberate symmetry here this here is what we're going to call a tag and it's
1158:39 what we're going to call a tag and it's technically different from this this is
1158:41 technically different from this this is a document type declaration it's got the
1158:42 a document type declaration it's got the weird exclamation point that's the only
1158:44 weird exclamation point that's the only anomaly everything else follows pattern
1158:46 anomaly everything else follows pattern this is a tag in HTML and it's the HTML
1158:50 this is a tag in HTML and it's the HTML tag and a tag generally both starts and
1158:53 tag and a tag generally both starts and stops or opens and closes at some point
1158:56 stops or opens and closes at some point so this is the so-called start tag or
1158:59 so this is the so-called start tag or open tag and this just means essentially
1159:01 open tag and this just means essentially to the browser hey browser here comes
1159:03 to the browser hey browser here comes some HTML the language in which web
1159:04 some HTML the language in which web pages are written this here with the
1159:07 pages are written this here with the forward slash after the angled bracket
1159:10 forward slash after the angled bracket means hey browser that's it for the HTML
1159:12 means hey browser that's it for the HTML of this page so that's what I mean by
1159:13 of this page so that's what I mean by symmetry I started a thought here
1159:15 symmetry I started a thought here finished it down here what's in between
1159:17 finished it down here what's in between those two thoughts well here browser or
1159:20 those two thoughts well here browser or rather let me clarify one thing this
1159:22 rather let me clarify one thing this thing here is that other uh keyword an
1159:25 thing here is that other uh keyword an attribute an attribute is something that
1159:27 attribute an attribute is something that modifies the behavior of a tag so it's
1159:30 modifies the behavior of a tag so it's similar to an argument in C or in Python
1159:32 similar to an argument in C or in Python like a parameter to a function these
1159:34 like a parameter to a function these aren't functions but it's the same idea
1159:36 aren't functions but it's the same idea it just modifies the default behavior of
1159:38 it just modifies the default behavior of something instead Lang equals you can
1159:40 something instead Lang equals you can probably guess just means that hey
1159:42 probably guess just means that hey browser assume that everything Hereafter
1159:44 browser assume that everything Hereafter is in English and that might be useful
1159:46 is in English and that might be useful for like Google translate or just search
1159:48 for like Google translate or just search engine optimization so that just the
1159:50 engine optimization so that just the server the browser know like what human
1159:52 server the browser know like what human language you have actual content in like
1159:54 language you have actual content in like hello title hello body even though a
1159:56 hello title hello body even though a good computer can probably infer from
1159:58 good computer can probably infer from Context often all right so that's an
1160:00 Context often all right so that's an attribute that's a tag and the whole
1160:03 attribute that's a tag and the whole thing here everything in between the
1160:05 thing here everything in between the start tag and end tag we would also call
1160:07 start tag and end tag we would also call an HTML element that just means
1160:09 an HTML element that just means everything related to that open and
1160:12 everything related to that open and close tag all right now notice indented
1160:15 close tag all right now notice indented inside of so to speak the HTML open and
1160:18 inside of so to speak the HTML open and close tag are another pair of tags the
1160:20 close tag are another pair of tags the head tag and the body tag or the head
1160:23 head tag and the body tag or the head element collectively and the body
1160:24 element collectively and the body element collectively and same idea hey
1160:26 element collectively and same idea hey browser here comes the head of my page
1160:28 browser here comes the head of my page hey browser that's it for the head hey
1160:30 hey browser that's it for the head hey browser here comes the body of my page
1160:31 browser here comes the body of my page hey browser that's it for the body the
1160:34 hey browser that's it for the body the head is essentially the tiny little
1160:36 head is essentially the tiny little strip at the very top including the tab
1160:38 strip at the very top including the tab itself the body is like 95% of
1160:41 itself the body is like 95% of everything else the big rectangular
1160:43 everything else the big rectangular region what's inside of your title at
1160:45 region what's inside of your title at the at of your head of the web page at
1160:47 the at of your head of the web page at the moment just the title so this
1160:49 the moment just the title so this indicates hey browser Here Comes My
1160:51 indicates hey browser Here Comes My Title hey browser that's it for the
1160:52 Title hey browser that's it for the title the title of course is literally
1160:54 title the title of course is literally quote unquote hello comma title
1160:57 quote unquote hello comma title meanwhile if we bounce back out here is
1160:59 meanwhile if we bounce back out here is the uh second element inside of the HTML
1161:03 the uh second element inside of the HTML tag uh this says Hey browser here comes
1161:05 tag uh this says Hey browser here comes the body hey browser that's it for the
1161:06 the body hey browser that's it for the body and hey browser this is the
1161:08 body and hey browser this is the contents these are the contents of the
1161:10 contents these are the contents of the body itself now the indentation is a
1161:12 body itself now the indentation is a stylistic thing I did it just to be sort
1161:14 stylistic thing I did it just to be sort of neat and en TIY because it suggests
1161:17 of neat and en TIY because it suggests what is inside of what but it also
1161:19 what is inside of what but it also suggests a sort of hierarchy and in fact
1161:21 suggests a sort of hierarchy and in fact we'll use terminology from like the
1161:23 we'll use terminology from like the world of family trees if this is like a
1161:26 world of family trees if this is like a parent so to speak head and body would
1161:29 parent so to speak head and body would be the child elements of the HTML tag
1161:33 be the child elements of the HTML tag meanwhile title is a child of the head
1161:35 meanwhile title is a child of the head tag or equivalently tital is a
1161:37 tag or equivalently tital is a grandchild of HTML so you can use the
1161:39 grandchild of HTML so you can use the same sort of vernacular as in the human
1161:42 same sort of vernacular as in the human world when it comes to uh familial
1161:44 world when it comes to uh familial relationships too and that just H set
1161:45 relationships too and that just H set again the same hierarchy so we have tags
1161:48 again the same hierarchy so we have tags and they include HTML head title body
1161:52 and they include HTML head title body and that's it for now we have attributes
1161:55 and that's it for now we have attributes we've seen one example of them Lang but
1161:57 we've seen one example of them Lang but we'll see many other examples of the
1161:59 we'll see many other examples of the same idea but these building blocks are
1162:01 same idea but these building blocks are exactly the same generally you start a
1162:03 exactly the same generally you start a thought you finish a thought and you
1162:04 thought you finish a thought and you might do something in between questions
1162:06 might do something in between questions on this basic structure of any web
1162:10 on this basic structure of any web page any questions at
1162:12 page any questions at all no all right so let's now now allow
1162:16 all no all right so let's now now allow things to ramp up a little more
1162:17 things to ramp up a little more interestingly and do something with
1162:19 interestingly and do something with these building blocks but so that you
1162:21 these building blocks but so that you have a mental model for everything
1162:22 have a mental model for everything that's going on here on after think of
1162:25 that's going on here on after think of this same HTML being related in spirit
1162:28 this same HTML being related in spirit to week five when we talked all about
1162:29 to week five when we talked all about data structures if I really wanted to I
1162:32 data structures if I really wanted to I could take to heart this idea of
1162:33 could take to heart this idea of children and parents and grandparents
1162:36 children and parents and grandparents and really depict this thing graphically
1162:38 and really depict this thing graphically and in fact this tree here if you will
1162:40 and in fact this tree here if you will and it's not a binary tree it's not a
1162:42 and it's not a binary tree it's not a binary search tree it's just a tree uh
1162:44 binary search tree it's just a tree uh used week five's terminology if this
1162:47 used week five's terminology if this special node here represents the whole
1162:49 special node here represents the whole document well the root element as I
1162:51 document well the root element as I called it is HTML HTML has two children
1162:54 called it is HTML HTML has two children head and body the head tag has in turn a
1162:57 head and body the head tag has in turn a title uh child and in turn has some text
1163:00 title uh child and in turn has some text just as the body has some text and so
1163:02 just as the body has some text and so this is what your browser is doing you
1163:04 this is what your browser is doing you and I the programmers write this stuff
1163:06 and I the programmers write this stuff the browser reads this code top to
1163:09 the browser reads this code top to bottom left to right whenever you visit
1163:10 bottom left to right whenever you visit a website and inside of the computer's
1163:12 a website and inside of the computer's memory Chrome Edge Firefox Safari what
1163:15 memory Chrome Edge Firefox Safari what what not they build this data structure
1163:17 what not they build this data structure in the computer's memory so as to know
1163:20 in the computer's memory so as to know what it is you have told them to do and
1163:22 what it is you have told them to do and we'll see over time at the end of today
1163:24 we'll see over time at the end of today you can write code in an actual
1163:25 you can write code in an actual programming language JavaScript to maybe
1163:28 programming language JavaScript to maybe dynamically add or remove things from
1163:30 dynamically add or remove things from this tree and this is how things like
1163:32 this tree and this is how things like Gmail work when you open up your Gmail
1163:33 Gmail work when you open up your Gmail inbox if you're a Gmail user if you just
1163:36 inbox if you're a Gmail user if you just stay there long enough you'll probably
1163:37 stay there long enough you'll probably get more and more mail and what happens
1163:39 get more and more mail and what happens you don't have to like reload the page
1163:41 you don't have to like reload the page or rebuild the tree per se it just all
1163:43 or rebuild the tree per se it just all of a sudden appears at the top
1163:45 of a sudden appears at the top at the top at the top what's happening
1163:47 at the top at the top what's happening there is that Google wrote some code
1163:49 there is that Google wrote some code that just keeps adding more nodes to
1163:52 that just keeps adding more nodes to this tree every time they realize you've
1163:54 this tree every time they realize you've got a new message again and again so
1163:55 got a new message again and again so that's the relationship now even with
1163:58 that's the relationship now even with this world of HTML with all of the
1164:00 this world of HTML with all of the programmatic ideas we looked at in the
1164:03 programmatic ideas we looked at in the past all right so let's go ahead and do
1164:05 past all right so let's go ahead and do something with this that's a little more
1164:07 something with this that's a little more interesting than just hello itself I'm
1164:09 interesting than just hello itself I'm going to go ahead and hide my terminal
1164:10 going to go ahead and hide my terminal window because the server is now running
1164:12 window because the server is now running and all I want to do now is experiment
1164:14 and all I want to do now is experiment with uh hello and other examples as well
1164:17 with uh hello and other examples as well let me go ahead and actually before I do
1164:19 let me go ahead and actually before I do that let me go ahead and run uh code of
1164:23 that let me go ahead and run uh code of paragraphs. HTML just so I can keep my
1164:25 paragraphs. HTML just so I can keep my code separate and now I'll hide the
1164:27 code separate and now I'll hide the terminal window again um paragraphs.
1164:29 terminal window again um paragraphs. HTML I'm going to do almost exactly the
1164:32 HTML I'm going to do almost exactly the same let me go ahead and start with
1164:34 same let me go ahead and start with something familiar and eventually I'll
1164:37 something familiar and eventually I'll start copying and pasting just to save
1164:38 start copying and pasting just to save time so doc type HTML is always there
1164:42 time so doc type HTML is always there open the HTML tag and now notice I
1164:44 open the HTML tag and now notice I didn't type the rest of that just like
1164:46 didn't type the rest of that just like with C just like with python we try to
1164:48 with C just like with python we try to save you some keystrokes by closing
1164:50 save you some keystrokes by closing parentheses adding quotes the HTML
1164:52 parentheses adding quotes the HTML support in VSS code is pretty good too
1164:54 support in VSS code is pretty good too and it tries to finish your thought when
1164:56 and it tries to finish your thought when it comes to tags as well it can screw
1164:58 it comes to tags as well it can screw things up if you if it does something
1165:00 things up if you if it does something you don't want it to do so sometimes you
1165:01 you don't want it to do so sometimes you have to delete but it's just
1165:02 have to delete but it's just autocomplete as we've seen before uh
1165:05 autocomplete as we've seen before uh let's go ahead and let me add Lang
1165:06 let's go ahead and let me add Lang equals as all of my examples today will
1165:09 equals as all of my examples today will be let's add the head tag let's go and
1165:12 be let's add the head tag let's go and proactively add the body tag and now
1165:15 proactively add the body tag and now let's go ahead and give this a title tag
1165:17 let's go ahead and give this a title tag uh which has a I'll just call this
1165:19 uh which has a I'll just call this paragraphs just so I remember which
1165:21 paragraphs just so I remember which example is what now notice all of this
1165:23 example is what now notice all of this white space and all of this neat and
1165:25 white space and all of this neat and tidy indentation the browser ultimately
1165:27 tidy indentation the browser ultimately is not going to care about this is just
1165:29 is not going to care about this is just for us humans to kind of keep ourselves
1165:31 for us humans to kind of keep ourselves saying when we look at the code it's
1165:32 saying when we look at the code it's just easier to read but strictly
1165:34 just easier to read but strictly speaking I could minimally delete all of
1165:37 speaking I could minimally delete all of this white space and I could just move
1165:39 this white space and I could just move all of this tag up to the same line both
1165:42 all of this tag up to the same line both I think are fine I'll just going to
1165:43 I think are fine I'll just going to follow a certain convention but this too
1165:45 follow a certain convention but this too would have the exact same meaning but
1165:47 would have the exact same meaning but we'll see where that detail about whites
1165:49 we'll see where that detail about whites space could potentially get us into
1165:50 space could potentially get us into trouble later in my paragraphs tag let's
1165:53 trouble later in my paragraphs tag let's do this in advance I've written up some
1165:56 do this in advance I've written up some Latin like text a really long paragraph
1165:58 Latin like text a really long paragraph of Latin like text like this it's
1166:00 of Latin like text like this it's actually random nonsense it's not real
1166:01 actually random nonsense it's not real Latin even though a couple of the words
1166:02 Latin even though a couple of the words might look familiar and so here we have
1166:05 might look familiar and so here we have three paragraphs of text and I've
1166:07 three paragraphs of text and I've deliberately hit enter in between them
1166:09 deliberately hit enter in between them so that just like an essay in Google
1166:11 so that just like an essay in Google Docs or Microsoft Word hopefully I'll
1166:12 Docs or Microsoft Word hopefully I'll see three separate paragraphs let me now
1166:15 see three separate paragraphs let me now change tabs and I'll close hello.html
1166:17 change tabs and I'll close hello.html from before I'm going to go back to my
1166:20 from before I'm going to go back to my other tab here I'm going to click back
1166:22 other tab here I'm going to click back to go back to that index of all of my
1166:25 to go back to that index of all of my files which I started at earlier and
1166:27 files which I started at earlier and you'll see now that I have two files
1166:29 you'll see now that I have two files because I obviously just created a
1166:30 because I obviously just created a second file called paragraphs. HTML so
1166:32 second file called paragraphs. HTML so let's click on this to see our three
1166:34 let's click on this to see our three paragraphs of Latin like text and voila
1166:37 paragraphs of Latin like text and voila I'll zoom out all right first bug if you
1166:41 I'll zoom out all right first bug if you will this just looks like one massive
1166:43 will this just looks like one massive blob of text not three Blobs of text and
1166:47 blob of text not three Blobs of text and why might that be borrowing the the hint
1166:50 why might that be borrowing the the hint I offered a moment
1166:51 I offered a moment ago why are we not seeing break yeah so
1166:55 ago why are we not seeing break yeah so we need some kind of line breaks here
1166:57 we need some kind of line breaks here because the browser turns out is only
1166:58 because the browser turns out is only going to take us literally and if you
1167:00 going to take us literally and if you just give it text text text it's just
1167:01 just give it text text text it's just going to show you text and anytime
1167:03 going to show you text and anytime there's more than a single white space
1167:05 there's more than a single white space whether it's two or 20 or 200 it's going
1167:08 whether it's two or 20 or 200 it's going to just assume that you did that just to
1167:10 to just assume that you did that just to be neat and tidy and it's going to
1167:11 be neat and tidy and it's going to collapse them into just one space
1167:13 collapse them into just one space visually like this so there are in fact
1167:16 visually like this so there are in fact a couple of solutions one is this here I
1167:19 a couple of solutions one is this here I could add some explicit line breaks and
1167:20 could add some explicit line breaks and it turns out that there's a br tag like
1167:23 it turns out that there's a br tag like this and just for uh visibility sake let
1167:26 this and just for uh visibility sake let me do two of them so like hitting enter
1167:27 me do two of them so like hitting enter enter on my keyboard I'll do it here too
1167:30 enter on my keyboard I'll do it here too BR for break break and now let me go
1167:32 BR for break break and now let me go back to my other tab nothing's changed
1167:35 back to my other tab nothing's changed yet but that's because I have to reload
1167:37 yet but that's because I have to reload I've changed it on the server but now I
1167:38 I've changed it on the server but now I need to change it in the browser by
1167:40 need to change it in the browser by reloading and now it looks a little
1167:42 reloading and now it looks a little better albeit nonsensical but you'll
1167:44 better albeit nonsensical but you'll know is a curiosity per this BR tag this
1167:48 know is a curiosity per this BR tag this is kind of poorly designed it's a little
1167:49 is kind of poorly designed it's a little hackish to just say enter enter and make
1167:51 hackish to just say enter enter and make the browser do this breaks line breaks
1167:54 the browser do this breaks line breaks don't actually require Clos tags or end
1167:57 don't actually require Clos tags or end tags so not all tags need to be closed
1168:00 tags so not all tags need to be closed at least those that it just makes no
1168:02 at least those that it just makes no semantic sense to close them right like
1168:03 semantic sense to close them right like the break is there or it's not you can't
1168:06 the break is there or it's not you can't imagine like starting to move to the
1168:08 imagine like starting to move to the next line and then eventually getting
1168:09 next line and then eventually getting around to finishing like it's either
1168:11 around to finishing like it's either there or it's not so some tags do not
1168:13 there or it's not so some tags do not have closed tags as necessary but
1168:15 have closed tags as necessary but there's a more elegant way here I dare
1168:18 there's a more elegant way here I dare say not just sort of hackish putting in
1168:20 say not just sort of hackish putting in these line breaks let me do this instead
1168:22 these line breaks let me do this instead I'll delete those and let me go ahead
1168:24 I'll delete those and let me go ahead and as the name of this file suggests
1168:26 and as the name of this file suggests let me add a paragraph tag now here I
1168:29 let me add a paragraph tag now here I need to fight with vs codes autocomplete
1168:31 need to fight with vs codes autocomplete because I don't want to finish the
1168:32 because I don't want to finish the sentence uh the thought there let me go
1168:34 sentence uh the thought there let me go ahead and open the paragraph tag and
1168:36 ahead and open the paragraph tag and close a paragraph tag and just to keep
1168:38 close a paragraph tag and just to keep things tidy I'll go ahead and indent two
1168:40 things tidy I'll go ahead and indent two even though the indentation itself
1168:42 even though the indentation itself doesn't matter let me go ahead and
1168:43 doesn't matter let me go ahead and create another tag for opening this
1168:46 create another tag for opening this paragraph and I'll close this one here
1168:49 paragraph and I'll close this one here and now let me see sometimes it's
1168:51 and now let me see sometimes it's fighting with my auto complete but
1168:52 fighting with my auto complete but that's fine because I did this sort of
1168:53 that's fine because I did this sort of the wrong way at first and now let me go
1168:56 the wrong way at first and now let me go ahead and finish this thought by closing
1168:59 ahead and finish this thought by closing this paragraph tag here and I've
1169:01 this paragraph tag here and I've manually fixed all of my indentation so
1169:04 manually fixed all of my indentation so now on line 10 I have the equivalent of
1169:07 now on line 10 I have the equivalent of hey browser start a paragraph and then
1169:09 hey browser start a paragraph and then it does the Latin like text then on line
1169:11 it does the Latin like text then on line 12 hey browser that's it for this
1169:12 12 hey browser that's it for this paragraph and repeat repeat repeat if I
1169:15 paragraph and repeat repeat repeat if I now go back to my other Tab and reload
1169:19 now go back to my other Tab and reload again shouldn't be all that different
1169:21 again shouldn't be all that different but semantically it's a little bit
1169:23 but semantically it's a little bit better why because just saying break
1169:25 better why because just saying break break doesn't really mean anything but
1169:27 break doesn't really mean anything but by saying paragraph paragraph paragraph
1169:30 by saying paragraph paragraph paragraph now there's some more semantic
1169:31 now there's some more semantic information there now if like Google is
1169:33 information there now if like Google is analyzing your page or if the programmer
1169:36 analyzing your page or if the programmer is trying to understand what it is you
1169:37 is trying to understand what it is you did in the past when writing this code
1169:39 did in the past when writing this code you just know semantically oh this is a
1169:41 you just know semantically oh this is a paragraph This is a paragraph This is a
1169:42 paragraph This is a paragraph This is a paragraph just like in a book or or an
1169:44 paragraph just like in a book or or an essay so it's a little more clear
1169:46 essay so it's a little more clear focusing more on what it is not how you
1169:49 focusing more on what it is not how you want to display it any questions then on
1169:52 want to display it any questions then on these
1169:54 these paragraphs now all right so a few more
1169:57 paragraphs now all right so a few more tags and indeed these first few examples
1169:58 tags and indeed these first few examples will really just be like sort of bang
1170:00 will really just be like sort of bang bang bang just a bunch of different
1170:01 bang bang just a bunch of different vocabulary words in the form of these
1170:03 vocabulary words in the form of these new tags but we won't go through the
1170:05 new tags but we won't go through the entire laundry list of tags this is
1170:06 entire laundry list of tags this is indeed the thing for which web
1170:08 indeed the thing for which web references and books and the like are
1170:09 references and books and the like are ultimately helpful just like a
1170:10 ultimately helpful just like a dictionary in the real world so I'm
1170:12 dictionary in the real world so I'm going to go ahead and do this let me go
1170:13 going to go ahead and do this let me go ahead and copy this let me create a new
1170:16 ahead and copy this let me create a new file called headings. HTML just so we
1170:18 file called headings. HTML just so we have a new file for this to save time
1170:20 have a new file for this to save time I'm just going to paste that exact same
1170:22 I'm just going to paste that exact same code just to get me started I'm going to
1170:24 code just to get me started I'm going to change the title of it for clarity for
1170:25 change the title of it for clarity for the code online to headings and now just
1170:28 the code online to headings and now just like a book or an essay or a thesis let
1170:30 like a book or an essay or a thesis let me actually put some actual headings
1170:32 me actually put some actual headings here now if my first heading like
1170:34 here now if my first heading like chapter one I could do something like
1170:37 chapter one I could do something like this up here I could have a paragraph
1170:39 this up here I could have a paragraph like I just learned and I could say
1170:40 like I just learned and I could say something like chapter one here but
1170:43 something like chapter one here but that's not really a paragraph and so
1170:44 that's not really a paragraph and so it's sort of better designed to tell the
1170:46 it's sort of better designed to tell the browser and really tell the world what
1170:48 browser and really tell the world what it is so it turns out there's another
1170:50 it is so it turns out there's another tag I can use like H1 for heading and
1170:53 tag I can use like H1 for heading and like most important heading and in here
1170:55 like most important heading and in here I'm just going to keep it simple and I'm
1170:56 I'm just going to keep it simple and I'm going to say something like one and in
1170:58 going to say something like one and in fact this is so short here's a good
1170:59 fact this is so short here's a good candidate for just keeping this all on
1171:01 candidate for just keeping this all on the same line but this has no functional
1171:03 the same line but this has no functional difference but it'll just make it a
1171:05 difference but it'll just make it a little tur more tur on the screen now
1171:07 little tur more tur on the screen now let me go ahead down here and I could
1171:09 let me go ahead down here and I could have multiple headings so H1 2 and down
1171:12 have multiple headings so H1 2 and down here I could have another one H1
1171:14 here I could have another one H1 three and if I go back to my other tab I
1171:18 three and if I go back to my other tab I reload it now we should see just like a
1171:20 reload it now we should see just like a book or an essay now we have some
1171:23 book or an essay now we have some proper now we have some oops I have to
1171:26 proper now we have some oops I have to go to the right file sorry if I go back
1171:27 go to the right file sorry if I go back to the index now we see the new third
1171:30 to the index now we see the new third file called headings. HTML and now we
1171:32 file called headings. HTML and now we indeed see some fairly pretty if simple
1171:35 indeed see some fairly pretty if simple headings as well now if these aren't
1171:37 headings as well now if these aren't three chapters 1 two three but maybe
1171:38 three chapters 1 two three but maybe it's a chapter then a section then a
1171:40 it's a chapter then a section then a subsection such that just visually you
1171:42 subsection such that just visually you want things to get smaller and smaller
1171:44 want things to get smaller and smaller well those exist too and in fact you can
1171:46 well those exist too and in fact you can do H1 through H6 H1 a little
1171:49 do H1 through H6 H1 a little paradoxically is the biggest and boldest
1171:51 paradoxically is the biggest and boldest H6 is the smallest but still bold so it
1171:54 H6 is the smallest but still bold so it might make sense to make this H2 both
1171:56 might make sense to make this H2 both open and close and maybe this H3 open
1171:59 open and close and maybe this H3 open and close if again this is a section or
1172:01 and close if again this is a section or a subsection inside of that chapter if I
1172:04 a subsection inside of that chapter if I reload now notice just gets a little
1172:06 reload now notice just gets a little smaller so it's more similar to what
1172:08 smaller so it's more similar to what you'd see on the printed page but this
1172:11 you'd see on the printed page but this now is just another three tags that I
1172:13 now is just another three tags that I might use in my own code all right well
1172:15 might use in my own code all right well how about lists of things I have three
1172:17 how about lists of things I have three paragraphs here but let's do this let me
1172:19 paragraphs here but let's do this let me go back to vs code I'm going to copy
1172:21 go back to vs code I'm going to copy this code so I have a starting point I'm
1172:23 this code so I have a starting point I'm going to create a new file called say
1172:26 going to create a new file called say list. HTML here I'm going to copy paste
1172:29 list. HTML here I'm going to copy paste I'm going to change my title to be list
1172:31 I'm going to change my title to be list just for clarity and in here I'm going
1172:33 just for clarity and in here I'm going to go ahead and get rid of this whole
1172:34 to go ahead and get rid of this whole body because let's move away from these
1172:36 body because let's move away from these massive paragraphs and keep it simpler
1172:38 massive paragraphs and keep it simpler for now if I want to have a list of
1172:40 for now if I want to have a list of things uh for instance uh if you haven't
1172:42 things uh for instance uh if you haven't seen these already a computer scientist
1172:44 seen these already a computer scientist when they're fishing for just some
1172:45 when they're fishing for just some arbitrary meaningless words uh they
1172:47 arbitrary meaningless words uh they often use Foo bar and baz just as their
1172:50 often use Foo bar and baz just as their go-to just like a mathematician might
1172:51 go-to just like a mathematician might use XY Z for variables so Fu bar and baz
1172:54 use XY Z for variables so Fu bar and baz are on three separate lines and maybe
1172:56 are on three separate lines and maybe this is like my to-do list or my
1172:57 this is like my to-do list or my shopping list but you can probably
1172:59 shopping list but you can probably imagine if I go back to my other tab go
1173:02 imagine if I go back to my other tab go back to the index I now see my new file
1173:04 back to the index I now see my new file list.
1173:05 list. HTML but it's probably going to look
1173:08 HTML but it's probably going to look wrong I think I'm just going to say yeah
1173:10 wrong I think I'm just going to say yeah Fubar baz all in one breath if you will
1173:13 Fubar baz all in one breath if you will the not on new line and you can try to
1173:15 the not on new line and you can try to fight this like you can be like really
1173:17 fight this like you can be like really want to put some line breaks there go
1173:19 want to put some line breaks there go back and reload it's still not going to
1173:21 back and reload it's still not going to make any change how do I want to fix
1173:24 make any change how do I want to fix this well I can do this in a few ways I
1173:25 this well I can do this in a few ways I could make them paragraphs but they're
1173:27 could make them paragraphs but they're not really paragraphs they're a list so
1173:29 not really paragraphs they're a list so I'm going to use a different tag instead
1173:31 I'm going to use a different tag instead I'm going to create for instance an
1173:33 I'm going to create for instance an unordered list using the UL tag open and
1173:36 unordered list using the UL tag open and close inside of that I'm going to use
1173:38 close inside of that I'm going to use the list item tag Li I and I'm going to
1173:41 the list item tag Li I and I'm going to say Foo inside of another tag I'm going
1173:43 say Foo inside of another tag I'm going to say bar inside of a third tag open
1173:46 to say bar inside of a third tag open and close I'm going to say baz so it's
1173:48 and close I'm going to say baz so it's getting a little verbose but it's still
1173:49 getting a little verbose but it's still relatively succinct Li is all you need
1173:52 relatively succinct Li is all you need type for list item UL is all you need
1173:54 type for list item UL is all you need type for unordered list so there's some
1173:57 type for unordered list so there's some shorthand syntax here that's adopted if
1173:59 shorthand syntax here that's adopted if I now reload you're going to see a
1174:01 I now reload you're going to see a so-called unordered list like not sorted
1174:04 so-called unordered list like not sorted which means by convention to show it as
1174:05 which means by convention to show it as bullets though it could be displayed in
1174:08 bullets though it could be displayed in different ways visually as well if you
1174:10 different ways visually as well if you actually change your mind and you
1174:11 actually change your mind and you realize oh I'd really like to number
1174:13 realize oh I'd really like to number this well you could obviously like just
1174:14 this well you could obviously like just add one and two and three but that's
1174:17 add one and two and three but that's going to get annoying especially if the
1174:19 going to get annoying especially if the list grows you want to change something
1174:20 list grows you want to change something insert something in the middle then you
1174:22 insert something in the middle then you have to reorder it I mean we using
1174:24 have to reorder it I mean we using computers here they can do this for us
1174:26 computers here they can do this for us so we can change the UL to any
1174:29 so we can change the UL to any guesses o maybe for ordered list which
1174:33 guesses o maybe for ordered list which is sort of the opposite here so let's
1174:34 is sort of the opposite here so let's try changing that to O let me go back to
1174:37 try changing that to O let me go back to my browser and I'm just hitting command
1174:39 my browser and I'm just hitting command r or controlr to reload the page instead
1174:42 r or controlr to reload the page instead of clicking the button every time now I
1174:44 of clicking the button every time now I automatically get 1 2 and three and you
1174:46 automatically get 1 2 and three and you can even override the Aesthetics using
1174:48 can even override the Aesthetics using different numera bles or symbology
1174:50 different numera bles or symbology instead but that would be perhaps the
1174:52 instead but that would be perhaps the most common there as well all right it's
1174:55 most common there as well all right it's a lot of tags quickly but any questions
1174:58 a lot of tags quickly but any questions on lists paragraphs headings or the like
1175:02 on lists paragraphs headings or the like no all right so let me go ahead and
1175:03 no all right so let me go ahead and propose this here um let's go ahead and
1175:07 propose this here um let's go ahead and create what we'll call a table so let me
1175:09 create what we'll call a table so let me copy and paste this into a new file code
1175:12 copy and paste this into a new file code of table. HTML and in table. HTML let me
1175:16 of table. HTML and in table. HTML let me again rename the title to table let me
1175:18 again rename the title to table let me get rid of that ordered list from before
1175:20 get rid of that ordered list from before and let me now use a table tag open and
1175:23 and let me now use a table tag open and close this one's a little weird but
1175:25 close this one's a little weird but inside of a table you typically have a
1175:28 inside of a table you typically have a head of the table so uh I'll say well
1175:31 head of the table so uh I'll say well let's say the first row we'll keep this
1175:33 let's say the first row we'll keep this one simple a table row or TR inside of a
1175:36 one simple a table row or TR inside of a table row you would ideally have columns
1175:39 table row you would ideally have columns but that's not the nomenclature instead
1175:41 but that's not the nomenclature instead you have data so TD for table data and
1175:43 you have data so TD for table data and let me go ahead and just have the first
1175:45 let me go ahead and just have the first datm be one I'm just going to
1175:46 datm be one I'm just going to arbitrarily do 1 two three just so we
1175:48 arbitrarily do 1 two three just so we have something to play with and you know
1175:50 have something to play with and you know what just for demonstration sake I am
1175:52 what just for demonstration sake I am going to deliberately copy paste this
1175:54 going to deliberately copy paste this twice and I'm just going to manually
1175:56 twice and I'm just going to manually change the numbers just so we can see
1175:58 change the numbers just so we can see what I'm creating 789 and then maybe
1176:02 what I'm creating 789 and then maybe just for good measure if you're seeing
1176:04 just for good measure if you're seeing where this is going let me copy this one
1176:06 where this is going let me copy this one more time and give myself a final row
1176:08 more time and give myself a final row with an asterisk a zero and a pound sign
1176:11 with an asterisk a zero and a pound sign if maybe you see where this is going let
1176:13 if maybe you see where this is going let me go back to my other tab let me go
1176:16 me go back to my other tab let me go back to the index there's my new file
1176:18 back to the index there's my new file table. HTML I'll click that and while
1176:21 table. HTML I'll click that and while it's not very pretty I'll zoom in it's
1176:24 it's not very pretty I'll zoom in it's indeed a table of data I happen to mimic
1176:27 indeed a table of data I happen to mimic like a key uh a telephone keypad but you
1176:29 like a key uh a telephone keypad but you could imagine this being much juicier
1176:31 could imagine this being much juicier much more interesting scientific or
1176:32 much more interesting scientific or financial data or the like laid out into
1176:34 financial data or the like laid out into these rows TRS and these columns AKA
1176:37 these rows TRS and these columns AKA table data as well so we have that
1176:39 table data as well so we have that ability as well for structured data now
1176:42 ability as well for structured data now of course the internet um has lots of
1176:44 of course the internet um has lots of images on it and in fact this is all
1176:46 images on it and in fact this is all just text how can we introduce images
1176:49 just text how can we introduce images well let me go ahead and do this let me
1176:52 well let me go ahead and do this let me first uh sort of semi secretly copy an
1176:55 first uh sort of semi secretly copy an image file that I brought from uh
1176:57 image file that I brought from uh earlier just so we have something to
1176:59 earlier just so we have something to play with and I have in my account here
1177:02 play with and I have in my account here now an image called harbor. JPEG and I
1177:04 now an image called harbor. JPEG and I uploaded this semi- secretly a second
1177:06 uploaded this semi- secretly a second ago into my account so that I can
1177:08 ago into my account so that I can reference a second file let me go ahead
1177:10 reference a second file let me go ahead and copy this HTML just to save myself
1177:13 and copy this HTML just to save myself some keystrokes
1177:14 some keystrokes let me go ahead and do code of image.
1177:17 let me go ahead and do code of image. HTML and let me paste that code and hide
1177:19 HTML and let me paste that code and hide my terminal window I'm going to get rid
1177:21 my terminal window I'm going to get rid of all of this table as just
1177:23 of all of this table as just uninteresting now we're going to make
1177:25 uninteresting now we're going to make this even simpler by changing my title
1177:28 this even simpler by changing my title to image to keep all these
1177:30 to image to keep all these demonstrations uh separate and now if I
1177:33 demonstrations uh separate and now if I want to make a web page that when
1177:35 want to make a web page that when visited shows us a picture of Harvard
1177:37 visited shows us a picture of Harvard well there's an image tag abbreviated
1177:39 well there's an image tag abbreviated IMG for short I can specify what the
1177:42 IMG for short I can specify what the source of that image is and if my file a
1177:45 source of that image is and if my file a JPEG in this case is literally in the
1177:47 JPEG in this case is literally in the same folder I can just say quote unquote
1177:50 same folder I can just say quote unquote harvard. jpeg if it's in a folder I
1177:52 harvard. jpeg if it's in a folder I should mention the folder name in a
1177:53 should mention the folder name in a slash or something like that if the
1177:55 slash or something like that if the image is on the internet somewhere with
1177:57 image is on the internet somewhere with a URL I could also have a whole URL
1178:00 a URL I could also have a whole URL https colon and then the URL of the
1178:03 https colon and then the URL of the image but I upload it in in advance now
1178:05 image but I upload it in in advance now this is just going to visually display
1178:07 this is just going to visually display on the screen but not everyone of course
1178:08 on the screen but not everyone of course can see images screen readers might need
1178:10 can see images screen readers might need a bit of assistance and even search
1178:12 a bit of assistance and even search engines might want to analyze the page
1178:14 engines might want to analyze the page and know what this is an image of now
1178:17 and know what this is an image of now machine learning and artificial
1178:18 machine learning and artificial intelligence are maybe getting better to
1178:20 intelligence are maybe getting better to be fair at figuring out just by
1178:22 be fair at figuring out just by analyzing images what they are but
1178:23 analyzing images what they are but they're certainly imperfect I am a human
1178:25 they're certainly imperfect I am a human I know pretty well what I took a photo
1178:27 I know pretty well what I took a photo of for instance so maybe what I should
1178:29 of for instance so maybe what I should do proactively which would be good for
1178:31 do proactively which would be good for accessibility is have this alt tag for
1178:33 accessibility is have this alt tag for alternative text and then literally say
1178:35 alternative text and then literally say like Harvard University so that someone
1178:37 like Harvard University so that someone who can't see or so that a a server can
1178:40 who can't see or so that a a server can actually know with higher probability
1178:42 actually know with higher probability what it is uh they're looking at and I
1178:43 what it is uh they're looking at and I could be even more detailed than just a
1178:45 could be even more detailed than just a phrase I could describe the image as
1178:48 phrase I could describe the image as well all right let me go back to my
1178:50 well all right let me go back to my index in the second tab let me go back
1178:53 index in the second tab let me go back and zoom back out there's my new file
1178:56 and zoom back out there's my new file and there's my new JPEG that I quickly
1178:58 and there's my new JPEG that I quickly uploaded before I can click now on
1179:00 uploaded before I can click now on image. HTML and albeit a little
1179:03 image. HTML and albeit a little overwhelming that is a really big image
1179:06 overwhelming that is a really big image of harvor now apparently it's too big to
1179:08 of harvor now apparently it's too big to fit on the screen so this isn't the best
1179:10 fit on the screen so this isn't the best user experience to have to scroll up
1179:13 user experience to have to scroll up okay so there's the image horrible
1179:15 okay so there's the image horrible horrible design if you will at least in
1179:17 horrible design if you will at least in terms of my code but there's going to be
1179:19 terms of my code but there's going to be ways where I can sort of rein that in
1179:21 ways where I can sort of rein that in and affect the height or the width as
1179:23 and affect the height or the width as well but for now it's just deliberately
1179:25 well but for now it's just deliberately a little overwhelming instead now we can
1179:28 a little overwhelming instead now we can do something a little more fun and
1179:29 do something a little more fun and topical today uh which might be to use
1179:32 topical today uh which might be to use an IM a video instead so let me go ahead
1179:35 an IM a video instead so let me go ahead here and very quickly grab another file
1179:37 here and very quickly grab another file today which is uh I brought in advance
1179:40 today which is uh I brought in advance and that you might have seen briefly
1179:42 and that you might have seen briefly earlier which is an MP4 an actual video
1179:45 earlier which is an MP4 an actual video file and what I'm going to do here by
1179:47 file and what I'm going to do here by revealing vs code again is I'm going to
1179:49 revealing vs code again is I'm going to code a file called video. HTML get
1179:51 code a file called video. HTML get another demonstration here I'm going to
1179:53 another demonstration here I'm going to change my title to video just to keep
1179:56 change my title to video just to keep these things straight and instead of the
1179:58 these things straight and instead of the image tag you might imagine using now
1180:00 image tag you might imagine using now indeed a video tag and this is a
1180:02 indeed a video tag and this is a relatively newer tag that has increasing
1180:05 relatively newer tag that has increasing support among browsers so it's good now
1180:06 support among browsers so it's good now to use and inside of this the syntax is
1180:09 to use and inside of this the syntax is a little different you specify and this
1180:12 a little different you specify and this is weirdly annoyingly in consistent not
1180:14 is weirdly annoyingly in consistent not SRC for Source you literally say source
1180:18 SRC for Source you literally say source and then in Source you use a source
1180:20 and then in Source you use a source attribute horrible design semantically
1180:23 attribute horrible design semantically but like this is what we're stuck with
1180:25 but like this is what we're stuck with halloween. MP4 is the name of the video
1180:27 halloween. MP4 is the name of the video we uploaded in advance made by some of
1180:29 we uploaded in advance made by some of harder Harvard's digital artists and the
1180:31 harder Harvard's digital artists and the type of this video so that the browser
1180:33 type of this video so that the browser knows for sure is video/ MP4 that's a
1180:36 knows for sure is video/ MP4 that's a so-called content type that you just
1180:38 so-called content type that you just know or you look up to figure it out and
1180:40 know or you look up to figure it out and just so that this is as animated as
1180:42 just so that this is as animated as possible I'm going to tell the video tag
1180:45 possible I'm going to tell the video tag with a few attributes to autoplay and it
1180:48 with a few attributes to autoplay and it turns out that attributes often have key
1180:50 turns out that attributes often have key value pairs whereby it's the key the
1180:53 value pairs whereby it's the key the attribute name equals quote unquote some
1180:56 attribute name equals quote unquote some value just like Lang equals quote
1180:57 value just like Lang equals quote unquote for English but not all
1181:00 unquote for English but not all attributes need values in fact if you
1181:02 attributes need values in fact if you read the documentation for html's video
1181:04 read the documentation for html's video tag there's an autoplay attribute where
1181:06 tag there's an autoplay attribute where you can literally just say the key and
1181:07 you can literally just say the key and it needs no value it's just going to
1181:09 it needs no value it's just going to mean autoplay and if you don't want to
1181:10 mean autoplay and if you don't want to autoplay you just omit it alt together
1181:13 autoplay you just omit it alt together so so you don't necessarily need a value
1181:15 so so you don't necessarily need a value on or off uh I want the thing to Loop
1181:17 on or off uh I want the thing to Loop just so it keeps going I want it to be
1181:19 just so it keeps going I want it to be muted so that we don't hear any sound in
1181:21 muted so that we don't hear any sound in fact there is no sound but browsers
1181:23 fact there is no sound but browsers nowadays for anti-spam and advertising
1181:25 nowadays for anti-spam and advertising reasons often will not play a video if
1181:28 reasons often will not play a video if it has sound because it's just kind of
1181:30 it has sound because it's just kind of obnoxious if you visit a page and all of
1181:31 obnoxious if you visit a page and all of a sudden your speakers start blaring so
1181:34 a sudden your speakers start blaring so I know this from having read up on this
1181:35 I know this from having read up on this that I should mute it so if I want it to
1181:37 that I should mute it so if I want it to actually autoplay for real and then I'll
1181:39 actually autoplay for real and then I'll set the width manually for now to be
1181:41 set the width manually for now to be like 128 pixels across just from some
1181:44 like 128 pixels across just from some trial and error earlier and that with
1181:46 trial and error earlier and that with attribute does have a value now I'm
1181:49 attribute does have a value now I'm being a little um a little uptight here
1181:52 being a little um a little uptight here by alphabetizing all of my attributes
1181:54 by alphabetizing all of my attributes not at all necessary I do it just so I
1181:56 not at all necessary I do it just so I can skim things faster and know if
1181:57 can skim things faster and know if something is there or not so for me it's
1181:59 something is there or not so for me it's just a matter of style let me go back to
1182:01 just a matter of style let me go back to my other tab go back to my index and
1182:04 my other tab go back to my index and you'll see two new files again the mp4
1182:07 you'll see two new files again the mp4 file and video. HTML I'll click on the
1182:10 file and video. HTML I'll click on the ladder and if I did this well here we
1182:13 ladder and if I did this well here we have thank thanks to our friends in
1182:15 have thank thanks to our friends in Harvard U our artistic friends at
1182:17 Harvard U our artistic friends at Harvard very like an oo would help with
1182:20 Harvard very like an oo would help with the drama here but okay but we have a
1182:22 the drama here but okay but we have a very dramatic nice Halloween type view
1182:24 very dramatic nice Halloween type view here as well so we have videos embedded
1182:26 here as well so we have videos embedded as well and suffice it to say there's
1182:28 as well and suffice it to say there's ways to embed YouTube videos or Vimeo or
1182:30 ways to embed YouTube videos or Vimeo or other services as well using yet more
1182:33 other services as well using yet more tags too but the web is of course all
1182:35 tags too but the web is of course all about hyperlink Hyper text markup
1182:38 about hyperlink Hyper text markup language where you click on something
1182:39 language where you click on something and you end up somewhere else and this
1182:41 and you end up somewhere else and this is how the web is so powerfully
1182:42 is how the web is so powerfully interconnected
1182:44 interconnected so how do we start creating links from
1182:45 so how do we start creating links from one website or web page to another
1182:48 one website or web page to another either that I made or someone else well
1182:50 either that I made or someone else well let me go ahead and open back up my
1182:53 let me go ahead and open back up my terminal window and let's create a file
1182:54 terminal window and let's create a file called link. HTML just to demonstrate
1182:57 called link. HTML just to demonstrate what you and I know as a link I'll hide
1182:59 what you and I know as a link I'll hide my terminal window now let me copy paste
1183:01 my terminal window now let me copy paste just to save myself some keystrokes and
1183:03 just to save myself some keystrokes and let me get rid of the video tag so we
1183:05 let me get rid of the video tag so we can focus now on links suppose that I
1183:08 can focus now on links suppose that I want you to visit Harvard virtually well
1183:10 want you to visit Harvard virtually well I could say something like visit uh
1183:13 I could say something like visit uh Harvard period this is uninteresting
1183:16 Harvard period this is uninteresting because it's just going to be text I
1183:17 because it's just going to be text I probably want you to actually visit
1183:19 probably want you to actually visit harvard.edu instead more specifically
1183:21 harvard.edu instead more specifically and I'll lower case it just to be
1183:23 and I'll lower case it just to be consistent with what browsers do in the
1183:25 consistent with what browsers do in the address bar all right let me go now to
1183:27 address bar all right let me go now to the video uh back to this video Tab and
1183:29 the video uh back to this video Tab and go back where we now see my index I'll
1183:32 go back where we now see my index I'll Zoom back in and there's link. HTML
1183:34 Zoom back in and there's link. HTML unfortunately when I click this and I'll
1183:36 unfortunately when I click this and I'll zoom in you literally just see the text
1183:38 zoom in you literally just see the text that I wrote and yet on every social
1183:40 that I wrote and yet on every social media platform nowadays except like
1183:42 media platform nowadays except like Instagram when you type a URL or what
1183:44 Instagram when you type a URL or what looks like a URL even if you didn't
1183:45 looks like a URL even if you didn't bother with the HTTP or https it usually
1183:49 bother with the HTTP or https it usually automatically links it for you on
1183:51 automatically links it for you on Facebook on Twitter and other sites as
1183:53 Facebook on Twitter and other sites as well that's just a convenience Discord
1183:55 well that's just a convenience Discord and slack do that too but they're just
1183:57 and slack do that too but they're just doing it to make things more
1183:59 doing it to make things more userfriendly but they have to generate
1184:02 userfriendly but they have to generate HTML with the proper tags and attributes
1184:06 HTML with the proper tags and attributes so to get this to actually work it's not
1184:08 so to get this to actually work it's not even good enough to say
1184:09 even good enough to say https www.harvard.edu because if I go
1184:13 https www.harvard.edu because if I go back now and reload now you'll literally
1184:15 back now and reload now you'll literally just see all of that as text if you want
1184:17 just see all of that as text if you want the browser to treat this as a link you
1184:19 the browser to treat this as a link you need to use the anchor tag it'd be great
1184:22 need to use the anchor tag it'd be great if it were called the link tag but it's
1184:24 if it were called the link tag but it's not it's called The Anchor tag or a for
1184:26 not it's called The Anchor tag or a for short and the way you reference the URL
1184:29 short and the way you reference the URL to which you want to lead the user is
1184:31 to which you want to lead the user is via href for hyper reference this is one
1184:34 via href for hyper reference this is one of the earliest tags perhaps among the
1184:36 of the earliest tags perhaps among the most Arcane now but if I then put that
1184:38 most Arcane now but if I then put that whole URL in quotes and close my tag I
1184:42 whole URL in quotes and close my tag I now have to opportunity to finish my
1184:44 now have to opportunity to finish my thought in between the start tag and the
1184:46 thought in between the start tag and the end tag for this anchor and what I put
1184:49 end tag for this anchor and what I put in between the start and end tag is
1184:51 in between the start and end tag is whatever the human's going to see so
1184:53 whatever the human's going to see so here I can say Harvard I can go back to
1184:56 here I can say Harvard I can go back to my other tab I can reload the page and
1184:59 my other tab I can reload the page and now you see the familiar blue underline
1185:02 now you see the familiar blue underline this now is an actual link and if I
1185:04 this now is an actual link and if I click it I'll be whisked away to the
1185:05 click it I'll be whisked away to the actual Harvard websites but there's a
1185:09 actual Harvard websites but there's a risk here can anyone imagine pretty
1185:12 risk here can anyone imagine pretty simply after like what 60 seconds of the
1185:15 simply after like what 60 seconds of the link tag of the anchor tag how could
1185:17 link tag of the anchor tag how could someone an adversary misuse this tag
1185:20 someone an adversary misuse this tag alone How could a website run by an
1185:23 alone How could a website run by an adversary How could a spammer misuse
1185:25 adversary How could a spammer misuse this tag do you think
1185:33 yeah yeah absolutely you could have it say one thing but lead another lead
1185:35 say one thing but lead another lead elsewhere so I could say Yale in here
1185:38 elsewhere so I could say Yale in here nothing stopping me as the developer go
1185:40 nothing stopping me as the developer go back to the page reload now it says
1185:42 back to the page reload now it says visit Yale you click on Yale and voila
1185:44 visit Yale you click on Yale and voila you end up applying to the wrong place
1185:46 you end up applying to the wrong place instead now there's some hints of this I
1185:48 instead now there's some hints of this I could hover over this and super small
1185:51 could hover over this and super small like this isn't very good for your
1185:53 like this isn't very good for your anti-hacking techniques but way down
1185:55 anti-hacking techniques but way down here you can actually see the URL that
1185:57 here you can actually see the URL that it's going to go to in most browsers
1185:59 it's going to go to in most browsers indeed do this at least on desktops and
1186:01 indeed do this at least on desktops and laptops so it's a little bit of a hint
1186:03 laptops so it's a little bit of a hint but what you're seeing here even though
1186:04 but what you're seeing here even though this is kind of a silly um uh playful
1186:07 this is kind of a silly um uh playful example this is exactly how fishing
1186:09 example this is exactly how fishing attacks work p i sh iing work whereby an
1186:13 attacks work p i sh iing work whereby an adversary tells you to log into your
1186:15 adversary tells you to log into your PayPal account but it doesn't go to
1186:17 PayPal account but it doesn't go to paypal.com it goes to some other random
1186:20 paypal.com it goes to some other random website that they bought and built that
1186:22 website that they bought and built that then tries to collect your username and
1186:24 then tries to collect your username and password and stored in their database so
1186:25 password and stored in their database so now they can log into your PayPal
1186:27 now they can log into your PayPal account as you and it boils down to that
1186:30 account as you and it boils down to that simple primitive and you can be even
1186:32 simple primitive and you can be even more manipulative to you can even say
1186:35 more manipulative to you can even say the whole URL for Yale like yale.edu or
1186:38 the whole URL for Yale like yale.edu or Worse htps www.yale.edu reload that and
1186:43 Worse htps www.yale.edu reload that and now who I mean who among you and people
1186:45 now who I mean who among you and people in your lives are necessarily going to
1186:47 in your lives are necessarily going to be so paranoid as to not just blindly
1186:49 be so paranoid as to not just blindly click on that URL this is why just being
1186:52 click on that URL this is why just being a defensive real world person nowadays
1186:54 a defensive real world person nowadays digitally is just ever more so important
1186:57 digitally is just ever more so important so these same things that can be used
1186:58 so these same things that can be used for good or uh benign use cases can also
1187:01 for good or uh benign use cases can also be used for ill purposes too and it is
1187:03 be used for ill purposes too and it is literally that simple questions now on
1187:07 literally that simple questions now on any of these tags thus
1187:09 any of these tags thus far just a few more to offer up any
1187:13 far just a few more to offer up any questions on this
1187:15 questions on this here no well let me open up a couple
1187:18 here no well let me open up a couple that I brought in advance just so we
1187:20 that I brought in advance just so we don't have to type all of them here um
1187:23 don't have to type all of them here um if you for instance have a web page
1187:24 if you for instance have a web page that's got quite a bit of code let me go
1187:27 that's got quite a bit of code let me go ahead and grab from the website a couple
1187:29 ahead and grab from the website a couple of examples real fast here namely one
1187:33 of examples real fast here namely one that we'll call how about uh meta
1187:37 that we'll call how about uh meta HTML and in this example here give me
1187:40 HTML and in this example here give me just a moment to full screen it we're
1187:42 just a moment to full screen it we're going to
1187:43 going to have a file So codem Meta HTML I'll open
1187:48 have a file So codem Meta HTML I'll open this up next no relationship to what we
1187:50 this up next no relationship to what we now know as meta the company but rather
1187:53 now know as meta the company but rather this is going to be a page that I copied
1187:55 this is going to be a page that I copied and pasted the same chunk of Latin like
1187:56 and pasted the same chunk of Latin like text from earlier so it's going to be a
1187:58 text from earlier so it's going to be a really big paragraph of text and this is
1188:00 really big paragraph of text and this is an example where if you were to open
1188:01 an example where if you were to open this web page not on my own Mac or your
1188:03 this web page not on my own Mac or your PC but on your phone the font might
1188:05 PC but on your phone the font might actually be really annoying and
1188:07 actually be really annoying and difficult to read why because your
1188:08 difficult to read why because your phone's going to try to squeeze all of
1188:10 phone's going to try to squeeze all of the content onto the tiny viewport the
1188:12 the content onto the tiny viewport the rectangular region of your phone instead
1188:14 rectangular region of your phone instead so it turns out there are ways pretty
1188:16 so it turns out there are ways pretty easy ways to make your website mobile
1188:19 easy ways to make your website mobile friendly as well Otherwise Known
1188:20 friendly as well Otherwise Known technically as responsive and the
1188:22 technically as responsive and the easiest way to do this is to include
1188:26 easiest way to do this is to include this tag here a meta tag again no
1188:29 this tag here a meta tag again no relationship to Facebook this has been
1188:30 relationship to Facebook this has been here much longer and this case here this
1188:32 here much longer and this case here this meta tag online five has its own sort of
1188:36 meta tag online five has its own sort of approach to key value pairs this is a
1188:38 approach to key value pairs this is a good example of where it'd be nice if it
1188:39 good example of where it'd be nice if it looked just like everything else but
1188:41 looked just like everything else but this is what we have historically
1188:43 this is what we have historically you can have a meta tag with an
1188:45 you can have a meta tag with an attribute called name that refers to the
1188:47 attribute called name that refers to the name of some feature of the browser in
1188:49 name of some feature of the browser in this case viewport is the technical term
1188:51 this case viewport is the technical term for like the big rectangular region to
1188:53 for like the big rectangular region to which I keep referring the body really
1188:54 which I keep referring the body really of your page the content for the
1188:57 of your page the content for the viewport you can say some AR esoteric
1188:59 viewport you can say some AR esoteric details like this the initial scale
1189:01 details like this the initial scale should be one that is no matter who
1189:03 should be one that is no matter who visits your site it shouldn't start
1189:05 visits your site it shouldn't start zoomed in it shouldn't start zoomed out
1189:07 zoomed in it shouldn't start zoomed out it should start at just the default
1189:08 it should start at just the default sizing and then this here with equals
1189:11 sizing and then this here with equals device width is a very arcane way of
1189:13 device width is a very arcane way of saying if the user has a small screen
1189:15 saying if the user has a small screen show the text proportional to that size
1189:18 show the text proportional to that size don't just try to cram it all into a
1189:20 don't just try to cram it all into a tiny little window so it's super simple
1189:22 tiny little window so it's super simple but if for the next problem set or
1189:24 but if for the next problem set or future projects as well you find that
1189:26 future projects as well you find that just things look really bad on mobile
1189:28 just things look really bad on mobile like this kind of tag is the place to
1189:31 like this kind of tag is the place to start meta there aren't terribly many of
1189:33 start meta there aren't terribly many of these that you'll use but they're useful
1189:35 these that you'll use but they're useful for other mechanisms as well in fact let
1189:37 for other mechanisms as well in fact let me go ahead and semi secretly pull up
1189:39 me go ahead and semi secretly pull up one other example as well whereby I'm
1189:43 one other example as well whereby I'm going to grab another example that uses
1189:45 going to grab another example that uses more of these tags and in just a moment
1189:49 more of these tags and in just a moment I'll reveal it here give me just one
1189:51 I'll reveal it here give me just one second here I'll have I'll propose that
1189:56 second here I'll have I'll propose that in this
1189:57 in this example of
1189:58 example of meta we now add these properties instead
1190:03 meta we now add these properties instead so I copy pasted this from an existing
1190:05 so I copy pasted this from an existing file just so as to not waste time typing
1190:07 file just so as to not waste time typing all of these out if you've ever shared a
1190:09 all of these out if you've ever shared a URL on Facebook or Twitter or slack or
1190:12 URL on Facebook or Twitter or slack or Discord order any number of websites
1190:14 Discord order any number of websites nowadays that automatically show a nice
1190:15 nowadays that automatically show a nice preview where you sudden instantly see
1190:17 preview where you sudden instantly see like the default image of the page maybe
1190:19 like the default image of the page maybe a few sentences or words of text or
1190:21 a few sentences or words of text or something like that sometimes those
1190:24 something like that sometimes those applications those websites will just
1190:25 applications those websites will just choose like the first image it finds on
1190:27 choose like the first image it finds on the website or the first sentence it
1190:29 the website or the first sentence it sees and show that but that might not be
1190:31 sees and show that but that might not be very user friendly or search engine
1190:33 very user friendly or search engine friendly and so a prog a developer might
1190:35 friendly and so a prog a developer might want to control what it is that slack
1190:38 want to control what it is that slack Discord Facebook Twitter and other such
1190:40 Discord Facebook Twitter and other such sites show by default for that can use
1190:43 sites show by default for that can use nowadays what are called open graph tags
1190:45 nowadays what are called open graph tags which is to say there's other uses of
1190:47 which is to say there's other uses of the meta tag and you just look these
1190:49 the meta tag and you just look these things up even I had to look this up to
1190:50 things up even I had to look this up to remember what the key value pairs are
1190:52 remember what the key value pairs are the meta tag can also have a property
1190:54 the meta tag can also have a property attribute that can be these very
1190:56 attribute that can be these very specific strings OG title OG description
1190:59 specific strings OG title OG description OG image which denotes open graph which
1191:02 OG image which denotes open graph which again is this standard that's evolved in
1191:03 again is this standard that's evolved in recent years and what you can do here is
1191:06 recent years and what you can do here is tell browsers and in turn servers what
1191:09 tell browsers and in turn servers what you want them to show as the default
1191:11 you want them to show as the default title of the page P the description of
1191:13 title of the page P the description of the page and even the default image just
1191:15 the page and even the default image just so you can exercise more control when
1191:17 so you can exercise more control when sharing things socially nowadays as well
1191:20 sharing things socially nowadays as well again it just boils down to these key
1191:21 again it just boils down to these key value pairs this is absolutely the kind
1191:23 value pairs this is absolutely the kind of thing you look up as needed to cross
1191:25 of thing you look up as needed to cross check but those capabilities are there
1191:26 check but those capabilities are there and so literally the next time you paste
1191:28 and so literally the next time you paste a link into slack or Discord or any
1191:30 a link into slack or Discord or any online site that then displays it in
1191:32 online site that then displays it in embedded fashion just know that all this
1191:35 embedded fashion just know that all this time a little bit of textual code like
1191:37 time a little bit of textual code like this in HTML has been there by whoever
1191:40 this in HTML has been there by whoever authored the site
1191:43 authored the site all right let's do one final example in
1191:45 all right let's do one final example in HTML alone before we transition to just
1191:48 HTML alone before we transition to just cleaning up the Aesthetics and improving
1191:49 cleaning up the Aesthetics and improving the visuals of everything we've been
1191:51 the visuals of everything we've been creating let me go ahead here and close
1191:53 creating let me go ahead here and close meta HTML let me code up a new file
1191:56 meta HTML let me code up a new file called how about search. HTML and see if
1192:00 called how about search. HTML and see if we can't draw some inspiration from our
1192:02 we can't draw some inspiration from our cursory understanding earlier of how
1192:04 cursory understanding earlier of how URLs work to see if we can't reinvent
1192:07 URLs work to see if we can't reinvent google.com itself so recall that a
1192:09 google.com itself so recall that a canonical URL might look like this here
1192:11 canonical URL might look like this here and in particular if you want to pass in
1192:13 and in particular if you want to pass in user input to that URL again you can
1192:17 user input to that URL again you can potentially have a question mark and
1192:19 potentially have a question mark and then a key equals value pair or for that
1192:21 then a key equals value pair or for that matter you can even have multiple key
1192:23 matter you can even have multiple key value pairs that by convention are
1192:25 value pairs that by convention are separated by ampers sense these things
1192:26 separated by ampers sense these things are everywhere like later today when you
1192:28 are everywhere like later today when you pull up almost any website in your
1192:29 pull up almost any website in your browser look at the URL and you'll just
1192:32 browser look at the URL and you'll just see a lot of this a lot of noise too and
1192:34 see a lot of this a lot of noise too and distractions but there's going to be
1192:35 distractions but there's going to be some equal signs most likely Andor some
1192:38 some equal signs most likely Andor some ampersands as well and those are just
1192:39 ampersands as well and those are just separating key value pairs now what can
1192:42 separating key value pairs now what can I do here well if you think back to how
1192:44 I do here well if you think back to how we manually searched for cats earlier
1192:47 we manually searched for cats earlier let me quickly do this I'll do this one
1192:49 let me quickly do this I'll do this one manually doc type HTML as my very first
1192:52 manually doc type HTML as my very first line HTML tag with how about my Lang
1192:55 line HTML tag with how about my Lang attribute for English up here and then
1192:58 attribute for English up here and then inside of this I'll have a head tag
1193:00 inside of this I'll have a head tag inside of this I'll have a title I'll
1193:02 inside of this I'll have a title I'll call this example uh search and then
1193:05 call this example uh search and then down here I'll have my beginning of a
1193:07 down here I'll have my beginning of a body tag and now let me introduce you to
1193:09 body tag and now let me introduce you to really a final tag for now uh a form tag
1193:12 really a final tag for now uh a form tag which will create a web form the thing
1193:14 which will create a web form the thing with text boxes and buttons that you and
1193:16 with text boxes and buttons that you and I use every day on any number of
1193:17 I use every day on any number of websites inside of this form I'm going
1193:20 websites inside of this form I'm going to have an input like a text box whose
1193:22 to have an input like a text box whose name is going to be Q for query because
1193:25 name is going to be Q for query because I'm trying to re-implement Google here
1193:27 I'm trying to re-implement Google here uh the uh type of that I want to be a
1193:31 uh the uh type of that I want to be a text box or if I know I'm using this for
1193:33 text box or if I know I'm using this for search I can actually change this to a
1193:35 search I can actually change this to a search box and it's going to let me it's
1193:37 search box and it's going to let me it's going to generally put a little X there
1193:38 going to generally put a little X there so you can clear it quickly that's a
1193:40 so you can clear it quickly that's a nice little enhancement as well and then
1193:42 nice little enhancement as well and then I'm going to give myself a submit button
1193:44 I'm going to give myself a submit button by doing input whoops I'm going to give
1193:47 by doing input whoops I'm going to give myself a submit button by doing input
1193:49 myself a submit button by doing input type equals submit and then I'll leave
1193:53 type equals submit and then I'll leave that as such here all right now I need
1193:55 that as such here all right now I need to do a little bit more but let's see
1193:57 to do a little bit more but let's see how this looks let me go over to my
1193:58 how this looks let me go over to my other tab let me go back to my index and
1194:01 other tab let me go back to my index and if I zoom
1194:03 if I zoom out there is search. HTML I'll click it
1194:07 out there is search. HTML I'll click it and there's not much going on here even
1194:08 and there's not much going on here even if I zoom in but I do indeed have a
1194:10 if I zoom in but I do indeed have a really big text box and a submit button
1194:13 really big text box and a submit button but I haven't in my HTML told anyone
1194:16 but I haven't in my HTML told anyone anywhere that I want this input whether
1194:18 anywhere that I want this input whether I type cat or dog to go to google.com so
1194:20 I type cat or dog to go to google.com so for that I need a couple of more
1194:22 for that I need a couple of more attributes and I know this from having
1194:23 attributes and I know this from having done this before and any online
1194:24 done this before and any online reference will say the same you can add
1194:26 reference will say the same you can add an action attribute like what do you
1194:28 an action attribute like what do you want the action of this form to be and
1194:30 want the action of this form to be and you can put the URL to which you want
1194:32 you can put the URL to which you want this form to be submitted and I know
1194:34 this form to be submitted and I know from tinkering that it should be hdps
1194:38 from tinkering that it should be hdps www.google.com search I don't need to
1194:41 www.google.com search I don't need to put any question marks here myself but I
1194:43 put any question marks here myself but I do want the uh browser to do that for me
1194:47 do want the uh browser to do that for me so let me go back to my other tab let me
1194:49 so let me go back to my other tab let me reload and nothing visually has happened
1194:52 reload and nothing visually has happened but watch this when I now type in cats
1194:55 but watch this when I now type in cats but before I hit enter notice that I'm
1194:57 but before I hit enter notice that I'm currently at some long crazy URL search.
1195:01 currently at some long crazy URL search. HTML is expected if I now go down to the
1195:04 HTML is expected if I now go down to the submit button and click submit watch
1195:06 submit button and click submit watch what happens to the URL and the page
1195:08 what happens to the URL and the page itself I'm whisked away to the actual
1195:11 itself I'm whisked away to the actual google.com and indeed there are those
1195:13 google.com and indeed there are those same cats and if I zoom in here you'll
1195:15 same cats and if I zoom in here you'll see that my URL has changed to be indeed
1195:17 see that my URL has changed to be indeed SL search question mark Q equals cats so
1195:20 SL search question mark Q equals cats so this is just how web forms work when you
1195:22 this is just how web forms work when you submit any form on the web in this way
1195:24 submit any form on the web in this way the browser automatically goes to that
1195:26 the browser automatically goes to that action URL adds a question mark puts any
1195:29 action URL adds a question mark puts any key value pairs that you manually typed
1195:31 key value pairs that you manually typed into the text boxes and lets the server
1195:33 into the text boxes and lets the server do its thing now here's where Chrome is
1195:35 do its thing now here's where Chrome is starting to simplify things Safari does
1195:37 starting to simplify things Safari does this too if you double click on your url
1195:39 this too if you double click on your url now you see the full URL but if any
1195:41 now you see the full URL but if any parts are missing that's just a UI thing
1195:43 parts are missing that's just a UI thing to eliminate visual distractions
1195:45 to eliminate visual distractions nowadays meanwhile if I go back to my
1195:48 nowadays meanwhile if I go back to my own form if I search this time for dogs
1195:50 own form if I search this time for dogs and hit enter now again the URL changes
1195:53 and hit enter now again the URL changes to be Q equals dogs and it all reduces
1195:57 to be Q equals dogs and it all reduces to this basic building block of using an
1196:00 to this basic building block of using an a form tag now I can be more explicit if
1196:02 a form tag now I can be more explicit if I know I want to use get it which is
1196:05 I know I want to use get it which is actually the default I can literally say
1196:06 actually the default I can literally say quote unquote get in all lowercase even
1196:08 quote unquote get in all lowercase even though the verb earlier was by Design in
1196:10 though the verb earlier was by Design in uppercase but here now I'm just being
1196:12 uppercase but here now I'm just being ever more explicit if I don't want the
1196:14 ever more explicit if I don't want the label of this button to be very
1196:17 label of this button to be very generically submit maybe I want it to be
1196:19 generically submit maybe I want it to be Google search quote unquote well if you
1196:21 Google search quote unquote well if you read the documentation for forms you can
1196:23 read the documentation for forms you can actually change the value of the button
1196:25 actually change the value of the button to be quote unquote Google search and if
1196:28 to be quote unquote Google search and if I now go back here and reload I get a
1196:30 I now go back here and reload I get a fresh form and now I get a button that
1196:33 fresh form and now I get a button that literally says Google Search and if I
1196:35 literally says Google Search and if I tinker with this further because this
1196:37 tinker with this further because this isn't very user friendly there's even
1196:38 isn't very user friendly there's even more attributes I can do I can add on my
1196:41 more attributes I can do I can add on my t uh search input a uh autoc complete
1196:46 t uh search input a uh autoc complete equals uh off if I don't want to see my
1196:48 equals uh off if I don't want to see my own history for whatever reason I don't
1196:50 own history for whatever reason I don't want people knowing I'm searching for
1196:51 want people knowing I'm searching for cats and dogs on this page I can
1196:53 cats and dogs on this page I can autofocus on the text box so that it
1196:55 autofocus on the text box so that it shows the cursor blinking in that box by
1196:58 shows the cursor blinking in that box by default and I can even do something like
1197:00 default and I can even do something like this I can have a placeholder attribute
1197:03 this I can have a placeholder attribute that says something like query or some
1197:05 that says something like query or some other documentation for the user and if
1197:07 other documentation for the user and if I now go back and reload you'll see
1197:10 I now go back and reload you'll see notice it says query and it's subtle but
1197:12 notice it says query and it's subtle but my cursor is already positioned there it
1197:14 my cursor is already positioned there it gave it focus and I can type cats now
1197:17 gave it focus and I can type cats now without having to click in the Box
1197:18 without having to click in the Box manually which is just marginally better
1197:20 manually which is just marginally better for the user's experience any questions
1197:23 for the user's experience any questions now on all of this
1197:27 now on all of this here any
1197:29 here any questions all right that too was a lot
1197:31 questions all right that too was a lot why don't we take a casual five minute
1197:32 why don't we take a casual five minute break and when we resume we'll take a
1197:34 break and when we resume we'll take a look at CSS add in some JavaScript and
1197:36 look at CSS add in some JavaScript and then wrap up so 5 minutes only for now
1197:39 then wrap up so 5 minutes only for now all right we are back so that's
1197:42 all right we are back so that's technically it for HTML like here on out
1197:45 technically it for HTML like here on out it'll be up to like online resources and
1197:47 it'll be up to like online resources and references we point you to just to fill
1197:48 references we point you to just to fill in your vocabulary for more tags and
1197:50 in your vocabulary for more tags and attributes but like conceptually that's
1197:52 attributes but like conceptually that's it there's attributes uh there are tags
1197:54 it there's attributes uh there are tags and there are attributes and the rest of
1197:57 and there are attributes and the rest of it really is just kind of a laundry list
1197:58 it really is just kind of a laundry list of capable uh possible features but it
1198:01 of capable uh possible features but it turns out too you'll see over time that
1198:03 turns out too you'll see over time that you can even see the HTML for websit so
1198:05 you can even see the HTML for websit so for instance if I go over to harvard.edu
1198:08 for instance if I go over to harvard.edu in my browser which I'll go ahead in
1198:11 in my browser which I'll go ahead in just a moment here here and do here and
1198:13 just a moment here here and do here and do
1198:13 do https
1198:15 https www.harvard.edu
1198:17 www.harvard.edu enter it again will pull up today's
1198:20 enter it again will pull up today's version of Harvard's website and if I
1198:22 version of Harvard's website and if I rightclick or control click on it again
1198:23 rightclick or control click on it again and go to inspect you'll see those
1198:25 and go to inspect you'll see those so-called developer tools and earlier we
1198:28 so-called developer tools and earlier we focused on the network tab just so we
1198:30 focused on the network tab just so we could see the HTTP stuff going on but
1198:33 could see the HTTP stuff going on but what I glossed over earlier was the
1198:35 what I glossed over earlier was the so-called elements tab which actually
1198:38 so-called elements tab which actually shows you the HTML underlying any web
1198:41 shows you the HTML underlying any web page on the internet and so for instance
1198:43 page on the internet and so for instance here is the underlying HTML for
1198:45 here is the underlying HTML for Harvard's homepage as of right now and
1198:47 Harvard's homepage as of right now and aesthetically some of it's been
1198:49 aesthetically some of it's been collapsed so if I click on these various
1198:50 collapsed so if I click on these various triangles I'll see what's actually
1198:52 triangles I'll see what's actually inside of that is the children of some
1198:54 inside of that is the children of some of these HTML tags but here on out if
1198:56 of these HTML tags but here on out if you're ever curious as to like how a web
1198:58 you're ever curious as to like how a web page uh made some feature visually you
1199:01 page uh made some feature visually you can just literally use these developer
1199:03 can just literally use these developer tools built into your own browser just
1199:04 tools built into your own browser just to see what the uh web developer
1199:07 to see what the uh web developer actually did and you can do things too
1199:09 actually did and you can do things too like this like if you really like maybe
1199:13 like this like if you really like maybe uh let's see if you really like this
1199:15 uh let's see if you really like this menu in the top right hand corner of
1199:17 menu in the top right hand corner of Harvard's website you can even
1199:18 Harvard's website you can even rightclick that or control click that
1199:21 rightclick that or control click that specifically choose inspect and what
1199:24 specifically choose inspect and what browsers will do is jump to the HTML
1199:27 browsers will do is jump to the HTML corresponding to that visual element on
1199:29 corresponding to that visual element on the page and here you can see though
1199:31 the page and here you can see though we've not talked about this tag before
1199:33 we've not talked about this tag before there's a button tag there's an ID
1199:35 there's a button tag there's an ID attribute and there's some other
1199:37 attribute and there's some other attributes as well that Define that
1199:39 attributes as well that Define that button um you can do other things too in
1199:42 button um you can do other things too in the web page let me scroll down for
1199:44 the web page let me scroll down for instance here and let's go actually
1199:46 instance here and let's go actually let's go to another one like yale.edu
1199:48 let's go to another one like yale.edu here in today's theme and suppose we
1199:51 here in today's theme and suppose we want to do something like uh change the
1199:55 want to do something like uh change the Aesthetics of this website well let's do
1199:58 Aesthetics of this website well let's do how about this over here life at Yale
1200:00 how about this over here life at Yale let's rightclick on this choose inspect
1200:02 let's rightclick on this choose inspect that's going to jump to that part of the
1200:04 that's going to jump to that part of the page and notice what you can do here in
1200:05 page and notice what you can do here in this elements tab we can be a little a
1200:08 this elements tab we can be a little a little playful in return today life at
1200:10 little playful in return today life at Harvard and voila we've now changed
1200:13 Harvard and voila we've now changed Yale's website it would seem so have we
1200:17 Yale's website it would seem so have we really like hopefully hacking is not
1200:19 really like hopefully hacking is not actually this
1200:20 actually this easy what did we actually do based on
1200:24 easy what did we actually do based on today's mental
1200:25 today's mental model like I have changed the page
1200:28 model like I have changed the page but yeah just changed how it is for me
1200:31 but yeah just changed how it is for me right because my browser just like with
1200:32 right because my browser just like with Phyllis and Brian from the GetGo
1200:34 Phyllis and Brian from the GetGo requested Yale's web page I got back a
1200:36 requested Yale's web page I got back a virtual envelope containing that HTML as
1200:38 virtual envelope containing that HTML as we've now called it my browser has a
1200:40 we've now called it my browser has a local copy it's got its own tree
1200:42 local copy it's got its own tree otherwise known as a Dom document object
1200:43 otherwise known as a Dom document object model built up in its memory and yeah I
1200:46 model built up in its memory and yeah I went to town and changed my copy of it
1200:48 went to town and changed my copy of it but of course hopefully I've not changed
1200:50 but of course hopefully I've not changed the actual server and in fact if I
1200:52 the actual server and in fact if I reload Yale's website now hopefully it
1200:55 reload Yale's website now hopefully it will revert back to indeed yep what it
1200:58 will revert back to indeed yep what it should be instead life at Yale but this
1201:01 should be instead life at Yale but this ability in your own browser be it Chrome
1201:03 ability in your own browser be it Chrome or Firefox or Edge or Safari to have
1201:05 or Firefox or Edge or Safari to have these built-in developer tools are very
1201:07 these built-in developer tools are very powerful because it's going to enable
1201:08 powerful because it's going to enable you to not only diagnose problems that
1201:10 you to not only diagnose problems that will invariably arise in the coming
1201:12 will invariably arise in the coming weeks with your own code but is also
1201:14 weeks with your own code but is also going to allow you to learn from other
1201:15 going to allow you to learn from other sites like how you can do things and
1201:17 sites like how you can do things and Tinker as well but up until now we
1201:19 Tinker as well but up until now we focused only on tags and attributes and
1201:21 focused only on tags and attributes and on the structure of a web page let's now
1201:24 on the structure of a web page let's now focus more on the Aesthetics and
1201:26 focus more on the Aesthetics and fine-tuning that it turns out that HTML
1201:28 fine-tuning that it turns out that HTML has very limited support for anything
1201:31 has very limited support for anything aesthetic like font sizes and colors and
1201:33 aesthetic like font sizes and colors and so forth and in recent years people have
1201:36 so forth and in recent years people have used necessarily a second language
1201:38 used necessarily a second language called CSS not a programming language
1201:40 called CSS not a programming language again a markup language if you will to
1201:43 again a markup language if you will to just fine-tune the Aesthetics of a page
1201:45 just fine-tune the Aesthetics of a page font sizes colors margins and all of
1201:47 font sizes colors margins and all of that so CSS is going to allow us to
1201:50 that so CSS is going to allow us to define a whole bunch of properties which
1201:51 define a whole bunch of properties which is just another group of people's
1201:53 is just another group of people's terminology for key value pairs indeed
1201:56 terminology for key value pairs indeed ever since week five like key value
1201:57 ever since week five like key value pairs are everywhere in the world not
1202:00 pairs are everywhere in the world not just at like sweet green and restaurants
1202:02 just at like sweet green and restaurants but indeed under in line code and
1202:04 but indeed under in line code and languages and Technologies like these so
1202:07 languages and Technologies like these so properties is the new word in CSS for
1202:09 properties is the new word in CSS for what a moment ago we called attributes
1202:11 what a moment ago we called attributes in H but it's the same idea just
1202:13 in H but it's the same idea just different vocabulary that you get used
1202:14 different vocabulary that you get used to over time a few phrases I might use
1202:17 to over time a few phrases I might use now and you'll hear in the coming days
1202:19 now and you'll hear in the coming days would be these type selector class
1202:21 would be these type selector class selector ID selector attribute selector
1202:23 selector ID selector attribute selector which just refer to different techniques
1202:25 which just refer to different techniques we're about to see that are going to
1202:27 we're about to see that are going to allow you to control more precisely the
1202:29 allow you to control more precisely the Aesthetics of specific things on the
1202:31 Aesthetics of specific things on the page and the way we're going to do this
1202:33 page and the way we're going to do this is we're going to take our basic HTML
1202:35 is we're going to take our basic HTML like we saw earlier and we're going to
1202:37 like we saw earlier and we're going to introduce in the next few minutes just a
1202:38 introduce in the next few minutes just a couple of more tags and or attributes
1202:40 couple of more tags and or attributes one we're going to introduce you to a
1202:43 one we're going to introduce you to a tag called style which nicely named
1202:45 tag called style which nicely named allows you to um allows you to control
1202:48 allows you to um allows you to control the style the Aesthetics the visuals of
1202:50 the style the Aesthetics the visuals of the web page or we're going to introduce
1202:52 the web page or we're going to introduce you to a link tag which very confusingly
1202:55 you to a link tag which very confusingly does not give you a link that you can
1202:57 does not give you a link that you can click on it just links to another file
1202:59 click on it just links to another file that then gets automatically included or
1203:02 that then gets automatically included or imported to borrow our language from C
1203:04 imported to borrow our language from C or in Python but same idea this will
1203:06 or in Python but same idea this will allow us to include secondary files and
1203:08 allow us to include secondary files and we're going to ultimately show you how
1203:10 we're going to ultimately show you how you can leverage third third party
1203:12 you can leverage third third party Frameworks libraries that other people
1203:13 Frameworks libraries that other people wrote so as to not get stuck in the
1203:15 wrote so as to not get stuck in the weeds of all the fine tuning of
1203:17 weeds of all the fine tuning of Aesthetics and just make pretty things
1203:18 Aesthetics and just make pretty things fast so you can focus really on the
1203:20 fast so you can focus really on the intellectually interesting part if
1203:21 intellectually interesting part if that's your choice of building the
1203:23 that's your choice of building the content the site out the application out
1203:25 content the site out the application out yourself all right so with that said let
1203:27 yourself all right so with that said let me go back to vs code here and let me go
1203:30 me go back to vs code here and let me go ahead and create a simple example called
1203:32 ahead and create a simple example called home.html like a very simple homepage
1203:34 home.html like a very simple homepage for John Harvard for instance let me
1203:36 for John Harvard for instance let me give myself three paragraphs initially
1203:39 give myself three paragraphs initially the first of which is just going to have
1203:41 the first of which is just going to have uh the person name the second paragraph
1203:43 uh the person name the second paragraph is going to say something like welcome
1203:44 is going to say something like welcome to my
1203:45 to my homepage uh to greet visitors and the
1203:48 homepage uh to greet visitors and the third is going to be like a little
1203:49 third is going to be like a little footer like copyright uh how about John
1203:53 footer like copyright uh how about John Harvard 1636 or something like that all
1203:57 Harvard 1636 or something like that all right let me go back to my other tab as
1203:58 right let me go back to my other tab as before reload my index there's my new
1204:01 before reload my index there's my new file home.html and I'll click that and
1204:04 file home.html and I'll click that and you'll see okay I mean this is sort of
1204:06 you'll see okay I mean this is sort of 1636 style web page super simple all
1204:08 1636 style web page super simple all text nothing really interesting going on
1204:11 text nothing really interesting going on there
1204:12 there but we can start to style it a little
1204:13 but we can start to style it a little differently like if the title of the
1204:15 differently like if the title of the page is John Harvard and then it's
1204:17 page is John Harvard and then it's welcome to my homepage and then this
1204:18 welcome to my homepage and then this less important footer why don't we have
1204:20 less important footer why don't we have the text be large then medium then small
1204:22 the text be large then medium then small so something arbitrary but a little more
1204:24 so something arbitrary but a little more nuanced so let me go back to vs code
1204:27 nuanced so let me go back to vs code here and in my home.html file let me
1204:30 here and in my home.html file let me introduce not yet the style tag but what
1204:33 introduce not yet the style tag but what I'm going to call temporarily the style
1204:35 I'm going to call temporarily the style attribute both indeed exist this one's
1204:37 attribute both indeed exist this one's simpler and it's going to be correct but
1204:39 simpler and it's going to be correct but we'll see in a moment not as well
1204:41 we'll see in a moment not as well designed arguably as is often our
1204:43 designed arguably as is often our narrative so inside of the style tag you
1204:45 narrative so inside of the style tag you can put this language called CSS key
1204:48 can put this language called CSS key value pairs otherwise known as
1204:50 value pairs otherwise known as properties the only way you know what
1204:52 properties the only way you know what properties exist what keys exist is by
1204:54 properties exist what keys exist is by taking a class reading a book looking at
1204:56 taking a class reading a book looking at an online reference and we're going to
1204:57 an online reference and we're going to give you just a sampling of what's out
1204:59 give you just a sampling of what's out there so suppose I want to control the
1205:01 there so suppose I want to control the font size of this first paragraph I can
1205:03 font size of this first paragraph I can literally say font Das size in all lower
1205:06 literally say font Das size in all lower case colon and then a word like large or
1205:09 case colon and then a word like large or I can specify 12p point or 18 point or
1205:12 I can specify 12p point or 18 point or something more precise like that like
1205:13 something more precise like that like from Google Docs or Microsoft Word and
1205:16 from Google Docs or Microsoft Word and suppose I want to make this text down
1205:19 suppose I want to make this text down here uh medium well uh I'll do quote
1205:22 here uh medium well uh I'll do quote unquote font size colon medium and down
1205:25 unquote font size colon medium and down here I'll do style equals uh font Das
1205:30 here I'll do style equals uh font Das size small so I'm going to start with
1205:32 size small so I'm going to start with just these three key value pairs same
1205:34 just these three key value pairs same key but different values I'll go back to
1205:37 key but different values I'll go back to my page and in a moment I'll reload and
1205:38 my page and in a moment I'll reload and it's going to be somewhat subtle but
1205:40 it's going to be somewhat subtle but watch how the font size do change when I
1205:42 watch how the font size do change when I reload now all right so got a little
1205:45 reload now all right so got a little bigger middle one's about the same and
1205:46 bigger middle one's about the same and the last one is a little smaller what if
1205:49 the last one is a little smaller what if I want to center it just like many web
1205:50 I want to center it just like many web pages have the text like this centered
1205:52 pages have the text like this centered well I can separate these key value
1205:54 well I can separate these key value pairs with semicolons and I'm sorry
1205:56 pairs with semicolons and I'm sorry semicolons are kind of sort of back with
1205:57 semicolons are kind of sort of back with CSS but I can do text-align Colon Center
1206:02 CSS but I can do text-align Colon Center strictly speaking I don't need the last
1206:03 strictly speaking I don't need the last semicolon if there's no more key value
1206:05 semicolon if there's no more key value pairs but I'll just do it to be
1206:07 pairs but I'll just do it to be consistent uh text align Colon Center
1206:10 consistent uh text align Colon Center and then down here after another
1206:12 and then down here after another semicolon text align Colon Center all
1206:16 semicolon text align Colon Center all right let's go back reload now it's
1206:18 right let's go back reload now it's going to be much more obvious the change
1206:20 going to be much more obvious the change and we now have the beginnings of a
1206:22 and we now have the beginnings of a homepage still pretty basic but at least
1206:24 homepage still pretty basic but at least it's a little more interesting turns out
1206:26 it's a little more interesting turns out we can do a little better with the
1206:27 we can do a little better with the copyright symbol like most computers
1206:29 copyright symbol like most computers actually have support for a circle with
1206:31 actually have support for a circle with a c in it but you can't just do that
1206:34 a c in it but you can't just do that with uh text like this there's different
1206:36 with uh text like this there's different ways to do this you could copy paste it
1206:38 ways to do this you could copy paste it from like a website that already has it
1206:39 from like a website that already has it so you don't have to figure out the mag
1206:41 so you don't have to figure out the mag iCal keystroke on your Mac or PC but
1206:43 iCal keystroke on your Mac or PC but there's also in HTML what are called
1206:45 there's also in HTML what are called entities and you can actually specify
1206:47 entities and you can actually specify using heximal or decimal codes numbers
1206:51 using heximal or decimal codes numbers like this H1 169 semicolon after an
1206:54 like this H1 169 semicolon after an Amper sand and this is a special symbol
1206:57 Amper sand and this is a special symbol that you can look up in any online
1206:58 that you can look up in any online reference for like special characters
1207:00 reference for like special characters that are hard or impossible to type
1207:01 that are hard or impossible to type manually at your keyboard and this let
1207:04 manually at your keyboard and this let me zoom in just so it's obvious if I
1207:06 me zoom in just so it's obvious if I reload now instead of being two
1207:08 reload now instead of being two parentheses and a c character
1207:11 parentheses and a c character now it's a proper Copyright symbol so
1207:14 now it's a proper Copyright symbol so you'll see these out there they're not
1207:15 you'll see these out there they're not necessarily that frequently used
1207:17 necessarily that frequently used nowadays but it's good to know that they
1207:19 nowadays but it's good to know that they exist but let me go back now to my code
1207:21 exist but let me go back now to my code and propose that while correct uh this
1207:24 and propose that while correct uh this is arguably not very welld designed and
1207:27 is arguably not very welld designed and even if you've never seen HTML never
1207:29 even if you've never seen HTML never seen CSS before what Instinct might you
1207:31 seen CSS before what Instinct might you have for why this is poorly
1207:36 have for why this is poorly designed
1207:38 designed yeah there's repetition right in general
1207:41 yeah there's repetition right in general in the past several weeks see python SQL
1207:43 in the past several weeks see python SQL like repetition generally bad and and
1207:45 like repetition generally bad and and sloppy and it's not going to scale well
1207:47 sloppy and it's not going to scale well so the repetition I think you're
1207:48 so the repetition I think you're probably alluding to is textalign Center
1207:50 probably alluding to is textalign Center textalign Center textalign Center well
1207:53 textalign Center textalign Center well we can factor that out in CSS the C in
1207:56 we can factor that out in CSS the C in CSS means cascading and this means that
1207:59 CSS means cascading and this means that if you move some properties to like a
1208:01 if you move some properties to like a parent or a grandparent uh the children
1208:04 parent or a grandparent uh the children or grandchildren will inherit those
1208:07 or grandchildren will inherit those properties that is they will Cascade
1208:08 properties that is they will Cascade down the family tree so to speak and so
1208:11 down the family tree so to speak and so so let me go ahead and remove all of
1208:12 so let me go ahead and remove all of these since I claim they're indeed
1208:15 these since I claim they're indeed redundant and let me preserve just one
1208:17 redundant and let me preserve just one of them by well let me do this it's not
1208:20 of them by well let me do this it's not quite right to put a a paragraph inside
1208:22 quite right to put a a paragraph inside of a paragraph like that's just not a
1208:24 of a paragraph like that's just not a thing in English writing or in writing
1208:26 thing in English writing or in writing more generally so I'm going to do one
1208:28 more generally so I'm going to do one thing first it turns out that these two
1208:30 thing first it turns out that these two are arguably not paragraphs right this
1208:32 are arguably not paragraphs right this is like a header the body the essence of
1208:34 is like a header the body the essence of the page and then the footer so if a
1208:37 the page and then the footer so if a paragraph isn't quite the right English
1208:39 paragraph isn't quite the right English semantics you can actually use more
1208:41 semantics you can actually use more generically a tag that's all over the
1208:43 generically a tag that's all over the internet called div for division of the
1208:45 internet called div for division of the page and this is just a very generic
1208:47 page and this is just a very generic term for like a big rectangular region
1208:49 term for like a big rectangular region that divides the page again and again
1208:51 that divides the page again and again just so that you can think about
1208:53 just so that you can think about different regions now that I have div
1208:56 different regions now that I have div which really has no more meaning than
1208:57 which really has no more meaning than that it's a division of the page
1208:59 that it's a division of the page interpret as you will now I can have
1209:01 interpret as you will now I can have multiple ones of these and let me go
1209:02 multiple ones of these and let me go ahead and open a div tag here let me
1209:05 ahead and open a div tag here let me close a new div tag here and then just
1209:08 close a new div tag here and then just to keep everything tidy I'm going to
1209:10 to keep everything tidy I'm going to highlight everything in between and hit
1209:11 highlight everything in between and hit Tab and that just automatically indents
1209:13 Tab and that just automatically indents everything for me now I have a three
1209:16 everything for me now I have a three divs inside of another div and that's
1209:18 divs inside of another div and that's totally fine this is very commonly done
1209:21 totally fine this is very commonly done now I'm going to do this style equals
1209:23 now I'm going to do this style equals quote unquote text align Colon Center
1209:26 quote unquote text align Colon Center semicolon or not and now I have some
1209:29 semicolon or not and now I have some cascading capabilities now the parent of
1209:32 cascading capabilities now the parent of those three children John Harvard welome
1209:35 those three children John Harvard welome to my homepage and the copyright will
1209:36 to my homepage and the copyright will now all inherit that property so when I
1209:38 now all inherit that property so when I hit reload nothing aesthetically has
1209:40 hit reload nothing aesthetically has changed whoops sorry
1209:42 changed whoops sorry um I should have done reload slightly
1209:45 um I should have done reload slightly earlier when you use a div instead of a
1209:47 earlier when you use a div instead of a paragraph it actually gets rid of the
1209:49 paragraph it actually gets rid of the par uh the space between those
1209:50 par uh the space between those paragraphs it just sandwiches them a
1209:52 paragraphs it just sandwiches them a little closer together I can fix this in
1209:54 little closer together I can fix this in another way but that aside everything is
1209:57 another way but that aside everything is still centered and the text is still
1209:58 still centered and the text is still large medium and small but I should have
1210:00 large medium and small but I should have called out that change in the paragraph
1210:02 called out that change in the paragraph spacing but we could bring that back
1210:04 spacing but we could bring that back before long if we wanted now what more
1210:07 before long if we wanted now what more could I do to maybe improve this well
1210:09 could I do to maybe improve this well strictly speaking I don't really need
1210:11 strictly speaking I don't really need that parent div right because these
1210:13 that parent div right because these three divs inside already had a parent
1210:16 three divs inside already had a parent so let me actually get rid of that new
1210:18 so let me actually get rid of that new div just undo what I did I'll highlight
1210:20 div just undo what I did I'll highlight this and if you haven't seen this trick
1210:22 this and if you haven't seen this trick shift tab will unindent nicely which is
1210:24 shift tab will unindent nicely which is perhaps helpful I could just put that
1210:27 perhaps helpful I could just put that text align Center on the body tag so
1210:30 text align Center on the body tag so text align Colon Center quote unquote
1210:34 text align Colon Center quote unquote this two would work as well so long as
1210:36 this two would work as well so long as you go up the family tree so to speak
1210:38 you go up the family tree so to speak reload and now indeed there's nothing
1210:40 reload and now indeed there's nothing aesthetically that has changed this time
1210:43 aesthetically that has changed this time but it turns out nowadays the web is
1210:45 but it turns out nowadays the web is getting a little more sophisticated and
1210:46 getting a little more sophisticated and even though you will see so many
1210:47 even though you will see so many examples online and tutorials and books
1210:49 examples online and tutorials and books using div div div div all over the place
1210:52 using div div div div all over the place there are newer semantic tags semantic
1210:55 there are newer semantic tags semantic just means they have more meaning than
1210:56 just means they have more meaning than this generic notion of a division and if
1210:58 this generic notion of a division and if you look up the a documentation for HTML
1211:01 you look up the a documentation for HTML you'll see that if you want to have a
1211:03 you'll see that if you want to have a header on a page not a heading like H1
1211:05 header on a page not a heading like H1 H2 but a header there's literally
1211:07 H2 but a header there's literally nowadays a header tag and this is
1211:10 nowadays a header tag and this is marginally better because it now says
1211:12 marginally better because it now says what it is search engines like Google
1211:14 what it is search engines like Google and Bing can detect oh that's the header
1211:16 and Bing can detect oh that's the header of the page maybe we should use this and
1211:17 of the page maybe we should use this and give it more prominence in the search
1211:19 give it more prominence in the search results you can then have a main part of
1211:21 results you can then have a main part of the page so literally a tag called main
1211:23 the page so literally a tag called main nowadays you can literally have a footer
1211:25 nowadays you can literally have a footer of the page and again these are often
1211:27 of the page and again these are often useful for screen readers to help recite
1211:29 useful for screen readers to help recite things verbally for folks who might
1211:31 things verbally for folks who might otherwise not be able to read them and
1211:32 otherwise not be able to read them and probably these screen readers might
1211:35 probably these screen readers might highlight the header and the main part
1211:37 highlight the header and the main part but maybe not might not spend time for
1211:38 but maybe not might not spend time for the user on the footer which is arguably
1211:40 the user on the footer which is arguably a little less important semantically
1211:42 a little less important semantically usually um or search engines again now
1211:45 usually um or search engines again now know what's the header what's the footer
1211:46 know what's the header what's the footer what's the main part of the page so they
1211:48 what's the main part of the page so they know what to search and analyze so this
1211:50 know what to search and analyze so this would arguably be a a better design
1211:52 would arguably be a a better design nowadays as
1211:54 nowadays as well but what else remains as a problem
1211:58 well but what else remains as a problem well this is now getting a little bit
1212:00 well this is now getting a little bit more subtle and takes some experience
1212:02 more subtle and takes some experience but this practice of putting HTML and
1212:05 but this practice of putting HTML and CSS all in the same file it's a little
1212:07 CSS all in the same file it's a little sloppy why because it means I'm
1212:09 sloppy why because it means I'm co-mingling my data with the
1212:11 co-mingling my data with the presentation thereof like the juicy
1212:13 presentation thereof like the juicy stuff I care about like John Harvard and
1212:15 stuff I care about like John Harvard and the phrase welcome to my homepage and
1212:17 the phrase welcome to my homepage and all of the Aesthetics that I might want
1212:18 all of the Aesthetics that I might want to change over time and honestly because
1212:21 to change over time and honestly because everything is currently in one big file
1212:23 everything is currently in one big file it's going to make it really hard for me
1212:24 it's going to make it really hard for me to collaborate with a classmate or a
1212:26 to collaborate with a classmate or a colleague at work so that maybe I do the
1212:28 colleague at work so that maybe I do the HTML they do the CSS like uhuh not if
1212:31 HTML they do the CSS like uhuh not if you're all working in the same file it
1212:32 you're all working in the same file it would be a nightmare even if you use vs
1212:34 would be a nightmare even if you use vs code sharing feature like Google Docs
1212:36 code sharing feature like Google Docs and both are typing at the same time
1212:38 and both are typing at the same time like you're going to mess up somehow
1212:39 like you're going to mess up somehow it'd be nice if we could separate these
1212:41 it'd be nice if we could separate these two languages well one way to do that
1212:43 two languages well one way to do that would be as follows let me get rid of
1212:45 would be as follows let me get rid of all of the style tags sorry style
1212:48 all of the style tags sorry style attributes that I've added up until
1212:50 attributes that I've added up until now on all four now of these tags and
1212:54 now on all four now of these tags and let me introduce the style tag that we
1212:57 let me introduce the style tag that we saw on the slide earlier instead I'm
1213:00 saw on the slide earlier instead I'm going to go up here into the head of the
1213:01 going to go up here into the head of the page which is where technically these
1213:03 page which is where technically these style tags must go so that they're
1213:05 style tags must go so that they're already loaded into memory before the
1213:07 already loaded into memory before the body is even analyzed by the browser and
1213:10 body is even analyzed by the browser and inside of the the style tag I'm actually
1213:12 inside of the the style tag I'm actually going to select the HTML elements that I
1213:15 going to select the HTML elements that I want to stylize if you will so if I want
1213:18 want to stylize if you will so if I want to change the body's Aesthetics I'm
1213:20 to change the body's Aesthetics I'm going to literally type the name of that
1213:22 going to literally type the name of that tag body and then I'm sorry curly braces
1213:25 tag body and then I'm sorry curly braces are back also from c u inside of these
1213:28 are back also from c u inside of these curly braces I'm going to put text align
1213:31 curly braces I'm going to put text align Center so the key value pairs are the
1213:33 Center so the key value pairs are the same the only new thing I've done is
1213:35 same the only new thing I've done is I've moved some of the syntax up to this
1213:37 I've moved some of the syntax up to this new style tag in the head if I want to
1213:39 new style tag in the head if I want to now control the header tag as well I can
1213:42 now control the header tag as well I can use the same curly braces this is
1213:44 use the same curly braces this is convention to put the open curly brace
1213:46 convention to put the open curly brace on the same line the closed curly brace
1213:48 on the same line the closed curly brace on another the browser doesn't really
1213:49 on another the browser doesn't really care but this is a common CSS style
1213:52 care but this is a common CSS style convention I'm going to do font size
1213:54 convention I'm going to do font size large semicolon then for the main tag
1213:57 large semicolon then for the main tag I'm going to do font size medium and
1214:00 I'm going to do font size medium and then for the footer tag I'm going to do
1214:02 then for the footer tag I'm going to do font size small so same exact thing and
1214:05 font size small so same exact thing and it's admittedly a little bit more
1214:07 it's admittedly a little bit more verbose it's taking up more lines of
1214:09 verbose it's taking up more lines of code it doesn't all quite fit on the
1214:10 code it doesn't all quite fit on the screen but if you scroll back down now
1214:13 screen but if you scroll back down now and you'll acquire an eye for this this
1214:15 and you'll acquire an eye for this this is just better like it's just more
1214:17 is just better like it's just more compact it's more readable the the
1214:19 compact it's more readable the the content the data jumps out and there's
1214:21 content the data jumps out and there's no visual distractions like the CSS
1214:24 no visual distractions like the CSS properties as before upside here too is
1214:27 properties as before upside here too is that we don't actually need to uh this
1214:29 that we don't actually need to uh this doesn't actually change the Aesthetics
1214:30 doesn't actually change the Aesthetics if I reload the same page it still looks
1214:33 if I reload the same page it still looks the same but I've taken a step toward
1214:35 the same but I've taken a step toward some slightly better design but let me
1214:37 some slightly better design but let me propose that there's other ways to do
1214:39 propose that there's other ways to do this too we just selected things by way
1214:42 this too we just selected things by way of their uh type so that was a so-called
1214:45 of their uh type so that was a so-called type selector when I literally just
1214:47 type selector when I literally just specifi the type of tag body header main
1214:50 specifi the type of tag body header main footer but there's other ways that now
1214:53 footer but there's other ways that now we can lay the foundation for making
1214:54 we can lay the foundation for making reusable CSS that you and colleagues and
1214:57 reusable CSS that you and colleagues and classmates can use and reuse in multiple
1215:00 classmates can use and reuse in multiple files and even in multiple projects so
1215:02 files and even in multiple projects so let me actually go ahead and do this
1215:04 let me actually go ahead and do this instead of just very explicitly saying I
1215:06 instead of just very explicitly saying I want the body to be centered let me
1215:08 want the body to be centered let me invent an adjective if you will and let
1215:10 invent an adjective if you will and let me change this to do centered and this
1215:13 me change this to do centered and this new uh this new vocabulary word centered
1215:16 new uh this new vocabulary word centered will literally mean texal Center let me
1215:19 will literally mean texal Center let me go ahead here and I'm just going to
1215:21 go ahead here and I'm just going to create a new adjective called large a
1215:24 create a new adjective called large a new adjective called medium and a new
1215:27 new adjective called medium and a new adjective called small they are
1215:29 adjective called small they are deliberately consistent with what the
1215:31 deliberately consistent with what the properties do but these are now my own
1215:34 properties do but these are now my own vocabulary words and they are called
1215:36 vocabulary words and they are called classes so a class is just a collection
1215:39 classes so a class is just a collection of key value
1215:41 of key value a collection of properties that you get
1215:42 a collection of properties that you get to invent for yourself and what it lets
1215:45 to invent for yourself and what it lets you do now is this now if I want the
1215:48 you do now is this now if I want the whole body to be centered I can add this
1215:51 whole body to be centered I can add this tag which we actually saw briefly in
1215:52 tag which we actually saw briefly in Yale's HTML class equals centered down
1215:56 Yale's HTML class equals centered down here in the header if I want this to be
1215:57 here in the header if I want this to be large I can say class equals quote
1216:00 large I can say class equals quote unquote large down here on Main I can
1216:02 unquote large down here on Main I can say class equals quote unquote medium
1216:05 say class equals quote unquote medium and down here I can have class equals
1216:07 and down here I can have class equals quote unquote small now I have taken one
1216:10 quote unquote small now I have taken one step backward by read addding some of
1216:12 step backward by read addding some of the Aesthetics to the page but it's not
1216:14 the Aesthetics to the page but it's not the actual properties it's not the key
1216:16 the actual properties it's not the key value pairs it's now more semantically
1216:18 value pairs it's now more semantically nice because now I just know from
1216:20 nice because now I just know from reading the HTML what these things are
1216:22 reading the HTML what these things are going to look like whereas the
1216:23 going to look like whereas the implementation details for all four of
1216:26 implementation details for all four of those adjectives is now relegated up
1216:28 those adjectives is now relegated up above and these are literally my words I
1216:30 above and these are literally my words I could change it to Fu and use class
1216:32 could change it to Fu and use class equals quote unquote Foo but obviously
1216:34 equals quote unquote Foo but obviously that would not be the best choice of
1216:35 that would not be the best choice of words in this case all right any
1216:39 words in this case all right any questions on this this now is what we
1216:42 questions on this this now is what we would call a class selector by using
1216:46 would call a class selector by using literally the dot even though the dot
1216:47 literally the dot even though the dot does not appear elsewhere but dot means
1216:49 does not appear elsewhere but dot means this is a
1216:51 this is a class these are not always the best
1216:53 class these are not always the best syntactic design decisions that the
1216:55 syntactic design decisions that the world makes all right well one last
1216:57 world makes all right well one last trick then notice that this is a little
1216:59 trick then notice that this is a little Annoying that I'm still working in the
1217:01 Annoying that I'm still working in the same file and if my classmate wants to
1217:02 same file and if my classmate wants to clean up my Aesthetics make my homepage
1217:04 clean up my Aesthetics make my homepage look way better if my colleague wants to
1217:06 look way better if my colleague wants to do the same wouldn't it be nice if we
1217:07 do the same wouldn't it be nice if we could actually move all of this code to
1217:09 could actually move all of this code to a different function file like a python
1217:11 a different function file like a python library or a c header file well you can
1217:14 library or a c header file well you can let me go ahead and delete that whole
1217:17 let me go ahead and delete that whole style tag let me add a confusingly named
1217:20 style tag let me add a confusingly named link tag the href of which let's call a
1217:24 link tag the href of which let's call a new file styles.css and let's say that
1217:28 new file styles.css and let's say that the relationship of that file is that of
1217:30 the relationship of that file is that of stylesheet so this is a term of Art in
1217:32 stylesheet so this is a term of Art in the world of web development a
1217:34 the world of web development a stylesheet is a text file that contains
1217:36 stylesheet is a text file that contains lots of styles lots of CSS properties
1217:38 lots of styles lots of CSS properties let me open my terminal real fast
1217:41 let me open my terminal real fast and let me do code of
1217:44 and let me do code of styles.css
1217:46 styles.css enter and in this file I'm going to
1217:49 enter and in this file I'm going to paste all of those same lines as earlier
1217:51 paste all of those same lines as earlier but now they're in a separate file and
1217:53 but now they're in a separate file and indeed if I hide my terminal window and
1217:55 indeed if I hide my terminal window and I give this file to a colleague they can
1217:56 I give this file to a colleague they can now work on the Aesthetics of the page
1217:58 now work on the Aesthetics of the page and make things a lot prettier than this
1217:59 and make things a lot prettier than this maybe use specific font sizes maybe add
1218:01 maybe use specific font sizes maybe add colors and the like whereas I can focus
1218:04 colors and the like whereas I can focus entirely on the HTML because this file
1218:06 entirely on the HTML because this file now will reference that other and if I
1218:08 now will reference that other and if I go back to my other Tab and reload Cod
1218:10 go back to my other Tab and reload Cod the content's going to be exactly the
1218:12 the content's going to be exactly the same but now I'm using some separate
1218:15 same but now I'm using some separate file
1218:17 file instead any questions now about these
1218:20 instead any questions now about these techniques
1218:22 techniques here no all right so with that said let
1218:26 here no all right so with that said let me show just one example now of what I
1218:29 me show just one example now of what I called a moment ago Frameworks and this
1218:31 called a moment ago Frameworks and this is where web development gets kind of
1218:32 is where web development gets kind of fun at least if you like this especially
1218:33 fun at least if you like this especially if you like the sort of logical design
1218:35 if you like the sort of logical design the presentation of information you care
1218:37 the presentation of information you care about but you really don't want to
1218:38 about but you really don't want to struggle with like font sizes and colors
1218:40 struggle with like font sizes and colors and getting everything Pixel Perfect so
1218:41 and getting everything Pixel Perfect so to speak let me propose that I open up
1218:44 to speak let me propose that I open up here an example in just a moment in vs
1218:46 here an example in just a moment in vs code that I prepared in advance and this
1218:49 code that I prepared in advance and this one is going to be an opportunity to
1218:53 one is going to be an opportunity to consider how you might take some of the
1218:55 consider how you might take some of the data from last week which we had wherein
1218:58 data from last week which we had wherein we collected everyone's favorites and
1219:00 we collected everyone's favorites and lay it out in a really big HTML table so
1219:02 lay it out in a really big HTML table so I wrote this out in advance because it
1219:03 I wrote this out in advance because it was a huge amount of data but it's the
1219:05 was a huge amount of data but it's the same data from the Google form from last
1219:07 same data from the Google form from last week and you'll see already the hints of
1219:09 week and you'll see already the hints of a table tag and these TRS and I added a
1219:12 a table tag and these TRS and I added a few other tags for Aesthetics it turns
1219:14 few other tags for Aesthetics it turns out when you have a more visually
1219:15 out when you have a more visually interesting header for your table
1219:17 interesting header for your table there's another tag called T head
1219:19 there's another tag called T head there's another tag called T body these
1219:21 there's another tag called T body these are not all that intellectually
1219:22 are not all that intellectually interesting I just read the
1219:23 interesting I just read the documentation and realized oh to make
1219:25 documentation and realized oh to make things prettier I need a t head a t body
1219:27 things prettier I need a t head a t body and so forth but what's interesting here
1219:29 and so forth but what's interesting here is that if I go to my index here and
1219:32 is that if I go to my index here and open this file called favorites. HTML
1219:35 open this file called favorites. HTML here is all of the data from last week's
1219:37 here is all of the data from last week's Google spreadsheet which we exported as
1219:39 Google spreadsheet which we exported as CS and I manually before class converted
1219:42 CS and I manually before class converted to just HTML it's indeed a table but
1219:45 to just HTML it's indeed a table but it's really not pretty like the columns
1219:47 it's really not pretty like the columns are really close together it's kind of
1219:48 are really close together it's kind of hard to distinguish one row from another
1219:51 hard to distinguish one row from another but this is just raw HTML written by me
1219:53 but this is just raw HTML written by me now I could use CSS and some of the
1219:55 now I could use CSS and some of the tricks we just saw to maybe change font
1219:58 tricks we just saw to maybe change font size there's ways to change color
1219:59 size there's ways to change color background color and a lot of things
1220:01 background color and a lot of things like that but honestly other surely
1220:04 like that but honestly other surely other people in the world have presented
1220:05 other people in the world have presented tabular data in pretty ways right I've
1220:07 tabular data in pretty ways right I've been to many websites that have prettier
1220:09 been to many websites that have prettier tables than M can I maybe use someone
1220:12 tables than M can I maybe use someone else's framework someone else's CSS
1220:15 else's framework someone else's CSS include it in my page but then stand on
1220:17 include it in my page but then stand on their shoulders and just make my stuff
1220:19 their shoulders and just make my stuff look prettier well I dare say I can let
1220:22 look prettier well I dare say I can let me go ahead here and semi secretly open
1220:24 me go ahead here and semi secretly open up vs code again and let me grab a
1220:27 up vs code again and let me grab a slightly different version of favorites.
1220:29 slightly different version of favorites. HTML that I also opened in advance
1220:32 HTML that I also opened in advance wherein I add this line of code instead
1220:37 wherein I add this line of code instead give me just a moment to foreground this
1220:38 give me just a moment to foreground this version and the data is is all the same
1220:40 version and the data is is all the same as before but I've added one of these
1220:42 as before but I've added one of these link tags and I'm not linking to my own
1220:45 link tags and I'm not linking to my own styles.css I'm using a popular Library
1220:48 styles.css I'm using a popular Library called bootstrap and bootstrap is just
1220:51 called bootstrap and bootstrap is just one of many popular libraries out there
1220:52 one of many popular libraries out there free at that that has a whole bunch of
1220:55 free at that that has a whole bunch of CSS files and soon JavaScript files that
1220:57 CSS files and soon JavaScript files that you can just use for free in your own
1220:59 you can just use for free in your own projects personally or professionally
1221:01 projects personally or professionally that just make things look and behave
1221:04 that just make things look and behave better without you having to reinvent
1221:06 better without you having to reinvent Wheels now to access their CSS I had to
1221:09 Wheels now to access their CSS I had to read their documentation and grab this
1221:10 read their documentation and grab this very long URL here but it's the same
1221:12 very long URL here but it's the same idea link a forre equals quote unquote
1221:15 idea link a forre equals quote unquote something and I read their documentation
1221:17 something and I read their documentation and they told me to add this they told
1221:19 and they told me to add this they told me that if I want my tables to be
1221:21 me that if I want my tables to be prettier I have to add a class attribute
1221:24 prettier I have to add a class attribute to my own table tag and specify a little
1221:27 to my own table tag and specify a little weirdly but this is what bootstrap told
1221:29 weirdly but this is what bootstrap told me to do a class called table and that
1221:31 me to do a class called table and that will make it a prettier bootstrap table
1221:33 will make it a prettier bootstrap table and if I want to stripe it like every
1221:35 and if I want to stripe it like every other row is gray instead of white just
1221:37 other row is gray instead of white just to make it pop a little more visually I
1221:39 to make it pop a little more visually I can also add a second class separated by
1221:41 can also add a second class separated by a space called table striped that's all
1221:45 a space called table striped that's all I did I added line five and I changed
1221:47 I did I added line five and I changed line nine and that is it the rest of the
1221:50 line nine and that is it the rest of the hundreds of lines in favorites. HTML are
1221:52 hundreds of lines in favorites. HTML are the same but if I go back here now and
1221:54 the same but if I go back here now and reload the browser now thanks to
1221:56 reload the browser now thanks to bootstrap voila like it's much prettier
1221:59 bootstrap voila like it's much prettier now I can zoom out and that changes the
1222:01 now I can zoom out and that changes the font size just locally for me and even
1222:03 font size just locally for me and even if you don't love their Aesthetics I
1222:05 if you don't love their Aesthetics I mean this is easily better than my own
1222:08 mean this is easily better than my own there and it turns out we can can do
1222:10 there and it turns out we can can do even better by adding interactivity to
1222:12 even better by adding interactivity to this too but to do that we're going to
1222:14 this too but to do that we're going to need one final language for today and
1222:16 need one final language for today and this one is an actual programming
1222:18 this one is an actual programming language and we won't use it all that
1222:20 language and we won't use it all that much in cs50 but we introduce it here as
1222:22 much in cs50 but we introduce it here as we begin web stuff because there's just
1222:24 we begin web stuff because there's just so many free libraries and professional
1222:26 so many free libraries and professional libraries that you can use just to make
1222:27 libraries that you can use just to make your web applications fancier and more
1222:30 your web applications fancier and more interactive mobile applications as well
1222:32 interactive mobile applications as well increasingly use HTML CSS and JavaScript
1222:36 increasingly use HTML CSS and JavaScript to power our iPhones and Android devices
1222:38 to power our iPhones and Android devices as well so a quick tour some syntax and
1222:41 as well so a quick tour some syntax and then we'll conclude with just some
1222:42 then we'll conclude with just some hopefully inspiring examples to give you
1222:44 hopefully inspiring examples to give you a taste of what JavaScript can do so
1222:46 a taste of what JavaScript can do so JavaScript supports conditionals just
1222:49 JavaScript supports conditionals just like C and python before it if we rewind
1222:51 like C and python before it if we rewind to our scratch days here of course is a
1222:53 to our scratch days here of course is a conditional here is the corresponding
1222:55 conditional here is the corresponding JavaScript code as of today it's pretty
1222:57 JavaScript code as of today it's pretty much identical to see with the syntax
1223:00 much identical to see with the syntax here uh if we had an if an if else in
1223:03 here uh if we had an if an if else in scratch it looked like this in
1223:04 scratch it looked like this in JavaScript it's going to look like this
1223:06 JavaScript it's going to look like this instead so it's a bit of a regression V
1223:08 instead so it's a bit of a regression V ofv python like the the parentheses are
1223:10 ofv python like the the parentheses are back the curly braces are back the
1223:12 back the curly braces are back the semicolons I mentioned in CSS are also
1223:14 semicolons I mentioned in CSS are also back in JavaScript potentially but it's
1223:16 back in JavaScript potentially but it's familiar is the point here and it's a
1223:18 familiar is the point here and it's a different language that's frequently
1223:19 different language that's frequently used for the web whereas you can't use
1223:22 used for the web whereas you can't use python in the ways we're about to use
1223:23 python in the ways we're about to use JavaScript it just wasn't designed for
1223:26 JavaScript it just wasn't designed for that purpose meanwhile if you have an if
1223:28 that purpose meanwhile if you have an if El it's else if else in scratch well in
1223:32 El it's else if else in scratch well in JavaScript just like in C it's going to
1223:33 JavaScript just like in C it's going to look like this instead variables in
1223:35 look like this instead variables in JavaScript of course are a thing too and
1223:37 JavaScript of course are a thing too and in scratch we might have initialized a
1223:39 in scratch we might have initialized a counter variable to Z in JavaScript a
1223:42 counter variable to Z in JavaScript a few different ways to do this and just
1223:44 few different ways to do this and just the for now the keyword is let it's sort
1223:46 the for now the keyword is let it's sort of a polite way of asking for a variable
1223:47 of a polite way of asking for a variable let uh counter equal zero semicolon so
1223:51 let uh counter equal zero semicolon so you don't mention the type but you do
1223:52 you don't mention the type but you do use a keyword here in this case called
1223:54 use a keyword here in this case called let if you want to increment counter by
1223:56 let if you want to increment counter by one few different ways in JavaScript you
1223:59 one few different ways in JavaScript you can do this just like in C in JavaScript
1224:01 can do this just like in C in JavaScript you can do this just like in C and in
1224:03 you can do this just like in C and in Python in JavaScript you can also get
1224:05 Python in JavaScript you can also get this so plus plus is back so maybe that
1224:07 this so plus plus is back so maybe that counterbalances the other syntax as well
1224:09 counterbalances the other syntax as well that was was not the case in Python
1224:11 that was was not the case in Python Loops are back of course in JavaScript
1224:14 Loops are back of course in JavaScript whereas in scratch you could repeat
1224:15 whereas in scratch you could repeat three times like this in JavaScript it's
1224:17 three times like this in JavaScript it's pretty much just like C the only
1224:19 pretty much just like C the only difference here is that you say let
1224:20 difference here is that you say let instead of int for an example like this
1224:23 instead of int for an example like this meanwhile if you want to do something
1224:24 meanwhile if you want to do something forever in scratch in JavaScript just
1224:26 forever in scratch in JavaScript just like in C you say while true in this
1224:29 like in C you say while true in this case so this is to say we're sort of
1224:32 case so this is to say we're sort of comfortable spending relatively little
1224:33 comfortable spending relatively little time on JavaScript at least for today's
1224:35 time on JavaScript at least for today's purposes because syntactically it's
1224:37 purposes because syntactically it's really the same as we've seen before
1224:40 really the same as we've seen before with maybe a slight variance here or
1224:42 with maybe a slight variance here or there but what's interesting today
1224:44 there but what's interesting today arguably is just what kinds of things
1224:45 arguably is just what kinds of things you can do with it so with that said
1224:49 you can do with it so with that said what kinds of things can we do it's all
1224:51 what kinds of things can we do it's all comes back to this picture if this is a
1224:54 comes back to this picture if this is a simple web page on the left and this is
1224:56 simple web page on the left and this is the corresponding tree or Dom document
1224:59 the corresponding tree or Dom document object model on the right that is the
1225:00 object model on the right that is the tree the browser automatically creates
1225:02 tree the browser automatically creates in memory or RAM for you JavaScript is
1225:05 in memory or RAM for you JavaScript is now a proper programming language that
1225:07 now a proper programming language that lets us dynamically manipul ulate like
1225:10 lets us dynamically manipul ulate like read data from this change this and this
1225:13 read data from this change this and this is how Google for instance implements
1225:14 is how Google for instance implements your inbox they might have uh in your
1225:16 your inbox they might have uh in your inbox it's like a table so TR TR TR TR
1225:20 inbox it's like a table so TR TR TR TR probably something like that or heck
1225:21 probably something like that or heck maybe div div div div using JavaScript
1225:24 maybe div div div div using JavaScript anytime they realize someone sent you
1225:26 anytime they realize someone sent you new mail they can create a new node a
1225:28 new mail they can create a new node a new rectangle in memory and you the
1225:30 new rectangle in memory and you the human see a new div or a new TR again
1225:33 human see a new div or a new TR again and again and again so with JavaScript
1225:36 and again and again so with JavaScript you just have the ability to control the
1225:38 you just have the ability to control the user's experience and instead of like
1225:40 user's experience and instead of like I've been doing constantly hitting
1225:42 I've been doing constantly hitting reload in the page to see some new
1225:44 reload in the page to see some new content to see some new content
1225:45 content to see some new content JavaScript can be running 247 so that
1225:48 JavaScript can be running 247 so that you can actually see all of these
1225:50 you can actually see all of these changes live all right let's go about
1225:52 changes live all right let's go about writing some JavaScript code now instead
1225:55 writing some JavaScript code now instead of writing it on the server and
1225:56 of writing it on the server and executing it on the server we're going
1225:58 executing it on the server we're going to actually use a very common Paradigm
1225:59 to actually use a very common Paradigm whereas JavaScript is actually executed
1226:02 whereas JavaScript is actually executed in the browser client side that is to
1226:04 in the browser client side that is to say we can actually start writing some
1226:06 say we can actually start writing some JavaScript code inside of our own HTML
1226:08 JavaScript code inside of our own HTML file so that when a user visits that web
1226:11 file so that when a user visits that web page with their browser not only is the
1226:13 page with their browser not only is the HTML and any CSS downloaded to the
1226:15 HTML and any CSS downloaded to the user's browser so is that JavaScript
1226:17 user's browser so is that JavaScript code so that it's executed indeed client
1226:20 code so that it's executed indeed client side on the browser rather than server
1226:23 side on the browser rather than server side as has been the case with python in
1226:25 side as has been the case with python in previous weeks well where do we go about
1226:27 previous weeks well where do we go about writing some JavaScript code let's go
1226:29 writing some JavaScript code let's go ahead and revisit hello.html which
1226:32 ahead and revisit hello.html which previously was a completely static
1226:33 previously was a completely static example that literally just said hello
1226:35 example that literally just said hello title hello body indeed if I open this
1226:38 title hello body indeed if I open this up using HTTP server and view it now in
1226:40 up using HTTP server and view it now in a separate tab all it said was exactly
1226:42 a separate tab all it said was exactly that hello title in the tab and hello
1226:44 that hello title in the tab and hello body in the main part of the viewport so
1226:46 body in the main part of the viewport so to speak well let's make this example a
1226:49 to speak well let's make this example a little more Dynamic so it doesn't just
1226:50 little more Dynamic so it doesn't just say hello body but maybe says hello to
1226:53 say hello body but maybe says hello to an actual person so let's go ahead and
1226:55 an actual person so let's go ahead and do this let's go ahead and remove the
1226:57 do this let's go ahead and remove the hardcoded hello body and let's actually
1227:00 hardcoded hello body and let's actually go ahead here and use a form tag but
1227:03 go ahead here and use a form tag but we're not going to use this form in the
1227:05 we're not going to use this form in the usual way whereby the data gets sent all
1227:07 usual way whereby the data gets sent all the way back to the server we're going
1227:08 the way back to the server we're going to Leverage control over this form
1227:10 to Leverage control over this form client side instead so I'm going to go
1227:12 client side instead so I'm going to go ahead and create this open form tag
1227:14 ahead and create this open form tag close form tag inside of that let me
1227:17 close form tag inside of that let me give myself a text input that's going to
1227:19 give myself a text input that's going to have autocomplete equals quote unquote
1227:21 have autocomplete equals quote unquote off just to ensure that what I
1227:23 off just to ensure that what I previously type in my examples doesn't
1227:24 previously type in my examples doesn't reappear accidentally we're going to
1227:26 reappear accidentally we're going to autofocus it so that the cursor is
1227:28 autofocus it so that the cursor is blinking right there in that text box
1227:30 blinking right there in that text box and this time I'm going to go ahead and
1227:32 and this time I'm going to go ahead and give it how about a uh placeholder of
1227:37 give it how about a uh placeholder of quote unquote name to make clear that
1227:39 quote unquote name to make clear that prompting the user for their name and
1227:41 prompting the user for their name and then the type of this text box will be
1227:42 then the type of this text box will be the default or more explicitly here text
1227:45 the default or more explicitly here text and then I'm going to have as we've seen
1227:47 and then I'm going to have as we've seen before a button the type of which is
1227:49 before a button the type of which is submit also our typical default and then
1227:52 submit also our typical default and then inside of this button is going to be the
1227:53 inside of this button is going to be the label how about something like uh how
1227:56 label how about something like uh how about we'll call this greet so that's
1227:59 about we'll call this greet so that's what the button will actually say well
1228:00 what the button will actually say well let me actually go back into my browser
1228:02 let me actually go back into my browser tab let me reload this page and we
1228:04 tab let me reload this page and we should now see a relatively simple form
1228:07 should now see a relatively simple form whereby I have the cursor blinking on a
1228:09 whereby I have the cursor blinking on a text inp input prompting the user for
1228:11 text inp input prompting the user for their name and then a greet button that
1228:12 their name and then a greet button that I can click but if I click this button
1228:14 I can click but if I click this button now it's not going to do anything useful
1228:16 now it's not going to do anything useful because I haven't written any code to
1228:18 because I haven't written any code to tell the browser what to do when I click
1228:20 tell the browser what to do when I click that button but it turns out there's all
1228:22 that button but it turns out there's all sorts of events in the world of
1228:24 sorts of events in the world of JavaScript that you can listen for so to
1228:26 JavaScript that you can listen for so to speak in fact here's just a list of some
1228:28 speak in fact here's just a list of some of them anytime something changes in a
1228:30 of them anytime something changes in a form field anytime the user clicks or
1228:32 form field anytime the user clicks or drags on something anytime the user
1228:35 drags on something anytime the user presses a key and maybe lifts their
1228:36 presses a key and maybe lifts their finger up anytime the mouse goes down or
1228:38 finger up anytime the mouse goes down or over or up on top of something or
1228:41 over or up on top of something or anytime a form is submitted those are
1228:44 anytime a form is submitted those are events in the same way that we talked
1228:45 events in the same way that we talked about events back in week zero in
1228:47 about events back in week zero in scratch and in JavaScript just like in
1228:50 scratch and in JavaScript just like in scratch where you can do something when
1228:52 scratch where you can do something when green flag clicked in JavaScript you can
1228:54 green flag clicked in JavaScript you can write code that actually listens for any
1228:56 write code that actually listens for any of these events or more so with that
1228:58 of these events or more so with that said let's go back to vs code here and
1229:00 said let's go back to vs code here and let's make a couple of changes instead
1229:03 let's make a couple of changes instead let's go ahead and add to this form a
1229:05 let's go ahead and add to this form a new attribute that's not the best way to
1229:07 new attribute that's not the best way to do it but it's perhaps the simplest for
1229:08 do it but it's perhaps the simplest for version one here and let's say onsubmit
1229:11 version one here and let's say onsubmit do the following so onsubmit is an HTML
1229:14 do the following so onsubmit is an HTML attribute and curiously its value inside
1229:16 attribute and curiously its value inside of the quotes there can actually be some
1229:19 of the quotes there can actually be some JavaScript code and let's go ahead now
1229:21 JavaScript code and let's go ahead now and let's assume there exists a function
1229:23 and let's assume there exists a function in the world called greet and what I
1229:25 in the world called greet and what I want to do is call that function right
1229:28 want to do is call that function right then and there well now in JavaScript
1229:29 then and there well now in JavaScript how do I go about making that function
1229:31 how do I go about making that function exist it doesn't come out of the box
1229:33 exist it doesn't come out of the box just like print might or say might in
1229:36 just like print might or say might in python or scratch respectively but I can
1229:38 python or scratch respectively but I can do this let me go up into the head of
1229:40 do this let me go up into the head of this page inside of a script tag here
1229:43 this page inside of a script tag here both open and close let me actually
1229:45 both open and close let me actually write some JavaScript code and just so
1229:46 write some JavaScript code and just so it stands out I'm going to give myself a
1229:47 it stands out I'm going to give myself a couple of blank lines though not
1229:49 couple of blank lines though not strictly necessary and let me Define a
1229:51 strictly necessary and let me Define a new function in JavaScript called greet
1229:54 new function in JavaScript called greet and this is the syntax in JavaScript for
1229:55 and this is the syntax in JavaScript for creating your own function similar in
1229:57 creating your own function similar in Python instead of saying defa in
1229:59 Python instead of saying defa in JavaScript you just say function then
1230:01 JavaScript you just say function then the name of the function and any
1230:03 the name of the function and any arguments within the parentheses
1230:05 arguments within the parentheses thereafter but I'm not going to pass in
1230:06 thereafter but I'm not going to pass in any here then inside of curly braces
1230:09 any here then inside of curly braces what I'm going to do is use a built-in
1230:10 what I'm going to do is use a built-in JavaScript function that comes with any
1230:12 JavaScript function that comes with any browser called alert it's not the best
1230:14 browser called alert it's not the best or prettiest user interface but for now
1230:16 or prettiest user interface but for now it's going to get the job done what do I
1230:18 it's going to get the job done what do I want to say to the user well let's first
1230:20 want to say to the user well let's first just say something simple like hello
1230:22 just say something simple like hello comma World close quote semicolon
1230:25 comma World close quote semicolon thereby alerting the user with precisely
1230:28 thereby alerting the user with precisely that message now what I'm going to do
1230:30 that message now what I'm going to do down here is make one other change I
1230:33 down here is make one other change I don't want this form to actually get
1230:35 don't want this form to actually get submitted to the server just like we've
1230:37 submitted to the server just like we've seen in the past when you submit a form
1230:38 seen in the past when you submit a form it often goes to something like
1230:40 it often goes to something like google.com/ search I actually want my
1230:43 google.com/ search I actually want my JavaScript code to take control over the
1230:46 JavaScript code to take control over the entire user experience of this form so
1230:48 entire user experience of this form so that I'm just using the form as a user
1230:50 that I'm just using the form as a user input mechanism not to actually send via
1230:53 input mechanism not to actually send via get or post this data to some other
1230:56 get or post this data to some other server including my own so this is going
1230:58 server including my own so this is going to look a little ugly but after calling
1231:00 to look a little ugly but after calling greet I'm actually going to do this
1231:02 greet I'm actually going to do this which I've read the documentation about
1231:03 which I've read the documentation about and I know that if you actually hardcode
1231:05 and I know that if you actually hardcode return false here that just tells the
1231:07 return false here that just tells the browser please don't actually submit the
1231:09 browser please don't actually submit the form only call the Greet function all
1231:12 form only call the Greet function all right well let me go back to my browser
1231:14 right well let me go back to my browser here let me reload this because I need
1231:16 here let me reload this because I need to download the latest version of the
1231:18 to download the latest version of the JavaScript code and I'm just going to go
1231:20 JavaScript code and I'm just going to go ahead without even typing my name I'm
1231:21 ahead without even typing my name I'm going to click on the Greet button and
1231:23 going to click on the Greet button and you'll see that Al be it a little
1231:25 you'll see that Al be it a little cryptically at the top we see an alert
1231:27 cryptically at the top we see an alert that says hello world there's my ugly
1231:29 that says hello world there's my ugly URL of my code space there at the moment
1231:31 URL of my code space there at the moment but we do indeed see that string but
1231:33 but we do indeed see that string but what I haven't of course done is taken
1231:35 what I haven't of course done is taken any actual name from the user so how can
1231:37 any actual name from the user so how can we go about doing that well ideally I
1231:40 we go about doing that well ideally I want to alert the user with hello comma
1231:43 want to alert the user with hello comma David or hello comma Carter whatever a
1231:44 David or hello comma Carter whatever a name I type into that box so how can I
1231:47 name I type into that box so how can I go about doing that well let me create a
1231:49 go about doing that well let me create a variable called name and let me set it
1231:51 variable called name and let me set it equal to this function call
1231:54 equal to this function call document. query
1231:57 document. query selector that comes with JavaScript
1232:00 selector that comes with JavaScript itself let me then in parenthesis pass
1232:02 itself let me then in parenthesis pass in an argument that is going to be huh
1232:04 in an argument that is going to be huh the ID the unique I need a unique
1232:06 the ID the unique I need a unique identifier for the thing I want to
1232:09 identifier for the thing I want to select so let me actually go back to my
1232:10 select so let me actually go back to my HTML code here and instead of giving
1232:12 HTML code here and instead of giving this form field a name like Q for query
1232:16 this form field a name like Q for query let me actually use another HTML
1232:17 let me actually use another HTML attribute called ID where now I can call
1232:20 attribute called ID where now I can call this anything I want and for clarity I'm
1232:22 this anything I want and for clarity I'm just going to call this input element
1232:25 just going to call this input element uniquely name now up here in query
1232:28 uniquely name now up here in query selector just like in CSS where you can
1232:31 selector just like in CSS where you can use hashes and dots and other symbology
1232:34 use hashes and dots and other symbology in order to select certain nodes in your
1232:36 in order to select certain nodes in your Dom that is rectangles in that memory
1232:39 Dom that is rectangles in that memory tree well I can go ahead and select hash
1232:41 tree well I can go ahead and select hash name which again is just the Syntax for
1232:43 name which again is just the Syntax for uniquely selecting the element whose ID
1232:46 uniquely selecting the element whose ID is in this case name so you have the
1232:49 is in this case name so you have the hash up here you don't need the hash as
1232:51 hash up here you don't need the hash as the value of the attribute down here on
1232:53 the value of the attribute down here on line 20 and now if I want to actually
1232:55 line 20 and now if I want to actually get the value of that text box I
1232:57 get the value of that text box I literally just say do value so document
1232:59 literally just say do value so document refers to the whole web page itself
1233:01 refers to the whole web page itself query selector is a function that's
1233:02 query selector is a function that's built into that object so to speak and
1233:05 built into that object so to speak and the value accessible via value just like
1233:08 the value accessible via value just like a c stru or even a python class allows
1233:10 a c stru or even a python class allows me to go inside of that text field and
1233:12 me to go inside of that text field and get whatever the value the user has
1233:15 get whatever the value the user has typed in now as I've uh been able to do
1233:18 typed in now as I've uh been able to do in languages like python pretty Le
1233:21 in languages like python pretty Le fairly readily I can concatenate this
1233:23 fairly readily I can concatenate this name onto the string hello comma space
1233:27 name onto the string hello comma space so as to form a complete phrase and
1233:28 so as to form a complete phrase and you'll notice here that I'm actually
1233:30 you'll notice here that I'm actually using single quotes in my JavaScript
1233:32 using single quotes in my JavaScript double quotes in my HTML this is perhaps
1233:34 double quotes in my HTML this is perhaps a common convention in JavaScript the
1233:37 a common convention in JavaScript the language does not care if you use double
1233:39 language does not care if you use double quotes or single quotes but I dare say
1233:40 quotes or single quotes but I dare say single quotes are just more common and
1233:42 single quotes are just more common and so that's what I've done here all right
1233:44 so that's what I've done here all right now as always I'm going to cross my
1233:45 now as always I'm going to cross my fingers go back to this page I'm going
1233:47 fingers go back to this page I'm going to reload because I've changed the
1233:49 to reload because I've changed the JavaScript and I need my browser to
1233:50 JavaScript and I need my browser to download it and now I'm going to type in
1233:52 download it and now I'm going to type in my name for instance David click greets
1233:56 my name for instance David click greets with fingers crossed and voila now I see
1233:59 with fingers crossed and voila now I see hello comma David all right so it turns
1234:02 hello comma David all right so it turns out that while functional this isn't the
1234:04 out that while functional this isn't the best design and co-mingling your uh HTML
1234:08 best design and co-mingling your uh HTML with your JavaScript script code AS with
1234:09 with your JavaScript script code AS with this onsubmit attribute isn't
1234:11 this onsubmit attribute isn't particularly clean it's better as with
1234:13 particularly clean it's better as with CSS to keep your HTML over here your CSS
1234:17 CSS to keep your HTML over here your CSS over here and your JavaScript now over
1234:19 over here and your JavaScript now over here so to speak and better still
1234:21 here so to speak and better still perhaps even in some separate files so
1234:23 perhaps even in some separate files so how can I go about changing this a
1234:24 how can I go about changing this a little bit well let me go ahead and
1234:26 little bit well let me go ahead and actually let's go ahead and delete all
1234:29 actually let's go ahead and delete all of this code for just a moment and let
1234:31 of this code for just a moment and let me go and get rid of this on submit
1234:33 me go and get rid of this on submit Handler down here and really just
1234:35 Handler down here and really just distill my HTML only into the HTML and
1234:39 distill my HTML only into the HTML and the attributes therefore and what I'm
1234:41 the attributes therefore and what I'm instead going to do now is do this I can
1234:44 instead going to do now is do this I can use JavaScript to achieve the listening
1234:47 use JavaScript to achieve the listening for that submit event or that onsubmit
1234:50 for that submit event or that onsubmit event I don't need to actually use HTML
1234:53 event I don't need to actually use HTML for that I can use JavaScript entirely
1234:56 for that I can use JavaScript entirely so it turns out I can access some other
1234:58 so it turns out I can access some other uh member of this document by doing
1235:00 uh member of this document by doing document. query selector again but this
1235:03 document. query selector again but this time let's select the actual form tag
1235:06 time let's select the actual form tag and it doesn't have an ID because it has
1235:08 and it doesn't have an ID because it has no ID in its HTML but it does have a tag
1235:11 no ID in its HTML but it does have a tag name so just like in CSS when you can
1235:12 name so just like in CSS when you can Target Elements by way of their name I'm
1235:15 Target Elements by way of their name I'm just going to select the one and only
1235:17 just going to select the one and only form on this page by using that same
1235:19 form on this page by using that same query selector function and now I'm
1235:21 query selector function and now I'm going to use another function that just
1235:22 going to use another function that just comes with JavaScript in the context of
1235:24 comes with JavaScript in the context of browsers whereby once you select an
1235:26 browsers whereby once you select an element like that form I can call add
1235:30 element like that form I can call add event listener which is similar in
1235:32 event listener which is similar in spirit to scratches when green flag
1235:34 spirit to scratches when green flag clicked or any block like that you can
1235:36 clicked or any block like that you can then tell the browser what event you
1235:38 then tell the browser what event you want to listen for I want to listen for
1235:40 want to listen for I want to listen for the submit event so you don't say
1235:41 the submit event so you don't say onsubmit here now that we're in pure
1235:43 onsubmit here now that we're in pure JavaScript you just say submit and now I
1235:46 JavaScript you just say submit and now I can do something like this I can go
1235:48 can do something like this I can go ahead and say call the following
1235:50 ahead and say call the following function and I'm not even going to
1235:52 function and I'm not even going to bother giving this function a name and
1235:53 bother giving this function a name and that is allowed to in JavaScript as we
1235:55 that is allowed to in JavaScript as we saw briefly in Python and what I'm going
1235:57 saw briefly in Python and what I'm going to do now inside of curly braces after
1236:00 to do now inside of curly braces after that keyword function is the same kind
1236:02 that keyword function is the same kind of code as before I'm going to do let
1236:04 of code as before I'm going to do let name equals document. query selector I'm
1236:08 name equals document. query selector I'm going to select that same IDE uh same
1236:11 going to select that same IDE uh same Name ID as before and get its value and
1236:14 Name ID as before and get its value and then I'm going to do alert and then pass
1236:16 then I'm going to do alert and then pass in hello comma a single quote again
1236:19 in hello comma a single quote again after that concatenate with that the
1236:21 after that concatenate with that the name and then semicolon but I need to do
1236:25 name and then semicolon but I need to do one other thing it turns out that this
1236:26 one other thing it turns out that this function and if you read the
1236:28 function and if you read the documentation for this technique
1236:30 documentation for this technique actually takes automatically a special
1236:32 actually takes automatically a special argument called by convention event and
1236:34 argument called by convention event and this is just an a variable if you will
1236:37 this is just an a variable if you will that refers to whatever event just
1236:39 that refers to whatever event just happened in this case it's of course
1236:40 happened in this case it's of course going to be submit but in other contexts
1236:42 going to be submit but in other contexts it might be a click event a mouse down
1236:44 it might be a click event a mouse down event or something else entirely so this
1236:46 event or something else entirely so this allows me now to do this which is a
1236:48 allows me now to do this which is a little cryptic but you get used to these
1236:49 little cryptic but you get used to these conventions I can use that event
1236:51 conventions I can use that event whatever it is and then prevent whatever
1236:54 whatever it is and then prevent whatever the default behavior is by calling a
1236:56 the default behavior is by calling a special function called prevent default
1236:58 special function called prevent default with a capital D this is the alternative
1237:00 with a capital D this is the alternative to that Messier return fall semicolon
1237:03 to that Messier return fall semicolon that I had inside of my HTML before so
1237:06 that I had inside of my HTML before so all I've done here now is I've left all
1237:08 all I've done here now is I've left all of my HTML as pure HTML down here and
1237:11 of my HTML as pure HTML down here and I've put all of my JavaScript code as
1237:13 I've put all of my JavaScript code as pure JavaScript up here this sort of
1237:15 pure JavaScript up here this sort of separation of concerns similar to what
1237:17 separation of concerns similar to what we started doing with CSS just a bit ago
1237:19 we started doing with CSS just a bit ago in order to keep those two languages
1237:21 in order to keep those two languages separate too well let me go back to my
1237:23 separate too well let me go back to my browser here reload the page and
1237:26 browser here reload the page and unfortunately there's a subtle mistake
1237:27 unfortunately there's a subtle mistake I've made here let me go ahead and type
1237:29 I've made here let me go ahead and type in David and click greet and
1237:32 in David and click greet and unfortunately nothing actually seems to
1237:34 unfortunately nothing actually seems to happen well maybe it's just my name
1237:36 happen well maybe it's just my name Carter greet and nothing seems to happen
1237:39 Carter greet and nothing seems to happen that alert does not come up well why is
1237:41 that alert does not come up well why is that well let me go back to vs code here
1237:44 that well let me go back to vs code here and point out that order of operations
1237:46 and point out that order of operations in zscript matters similar in spirit to
1237:48 in zscript matters similar in spirit to C because on line seven I'm selecting
1237:51 C because on line seven I'm selecting the form and trying to add an event
1237:53 the form and trying to add an event listener for submitting that form
1237:55 listener for submitting that form unfortunately the form had better exist
1237:57 unfortunately the form had better exist at that moment in time but it doesn't
1237:59 at that moment in time but it doesn't because just like in C and in some cases
1238:02 because just like in C and in some cases python where the compiler or The
1238:03 python where the compiler or The Interpreter reads the code top to bottom
1238:05 Interpreter reads the code top to bottom notice that the form doesn't actually
1238:07 notice that the form doesn't actually exist and therefore get loaded into the
1238:09 exist and therefore get loaded into the computer's memory until line 19 so we've
1238:12 computer's memory until line 19 so we've got to kind of reorder these somehow now
1238:14 got to kind of reorder these somehow now maybe the simplest way to do this would
1238:17 maybe the simplest way to do this would just be to perhaps do something like
1238:19 just be to perhaps do something like this let me scroll back up to my script
1238:21 this let me scroll back up to my script tag and perhaps a little more explicitly
1238:24 tag and perhaps a little more explicitly move it into the order in which I want
1238:27 move it into the order in which I want it to be executed so I'm going to go
1238:28 it to be executed so I'm going to go below my form and inside of my body
1238:31 below my form and inside of my body which is actually okay for JavaScript
1238:32 which is actually okay for JavaScript here and just use that same code and
1238:35 here and just use that same code and assuming I didn't make any typos let's
1238:36 assuming I didn't make any typos let's go back to the browser click reload
1238:38 go back to the browser click reload again to get the latest typee in my name
1238:41 again to get the latest typee in my name again using that purely JavaScript
1238:42 again using that purely JavaScript solution and the only change I made was
1238:44 solution and the only change I made was I move the code from up here to down
1238:46 I move the code from up here to down here clicking greet now and wow it's now
1238:49 here clicking greet now and wow it's now back we get the alert with hello comma
1238:52 back we get the alert with hello comma David so those kinds of things those
1238:53 David so those kinds of things those kinds of principles matter at least when
1238:55 kinds of principles matter at least when we're back in this world but there's
1238:56 we're back in this world but there's other Solutions too and just so that
1238:58 other Solutions too and just so that you've seen it because it's a common
1239:00 you've seen it because it's a common convention in libraries as well let me
1239:02 convention in libraries as well let me undo that change and put that script tag
1239:05 undo that change and put that script tag back in the head or really anywhere else
1239:07 back in the head or really anywhere else in the page where it might be
1239:08 in the page where it might be and let me propose that there's one
1239:10 and let me propose that there's one other way to solve this problem to
1239:12 other way to solve this problem to postpone that code on line 7 through 11
1239:15 postpone that code on line 7 through 11 getting executed until really the whole
1239:17 getting executed until really the whole Dom the tree is ready to go and the
1239:20 Dom the tree is ready to go and the Syntax for this might be as follows I
1239:23 Syntax for this might be as follows I can do document and I can add to the
1239:25 can do document and I can add to the document a an event listener that's
1239:28 document a an event listener that's going to listen for something a little
1239:29 going to listen for something a little special and I always have to look this
1239:30 special and I always have to look this up myself to remember the spelling and
1239:32 up myself to remember the spelling and the capitalization but it turns out that
1239:34 the capitalization but it turns out that the browser itself once it's done
1239:35 the browser itself once it's done loading all of your HTML top to bottom
1239:37 loading all of your HTML top to bottom left right it will raise an event called
1239:41 left right it will raise an event called Dom content loaded capitalized exactly
1239:45 Dom content loaded capitalized exactly as such and if you want to call some
1239:47 as such and if you want to call some function and I don't even need an event
1239:49 function and I don't even need an event argument in this case you can open curly
1239:51 argument in this case you can open curly braces just as before and put inside of
1239:54 braces just as before and put inside of those curly braces the code that you
1239:55 those curly braces the code that you want to execute only once the dumbs
1239:58 want to execute only once the dumbs content has been loaded top to bottom
1240:00 content has been loaded top to bottom and now let me just finish my thought
1240:02 and now let me just finish my thought with a closed curly brace Clos
1240:03 with a closed curly brace Clos parenthesis and semicolon it gets a
1240:05 parenthesis and semicolon it gets a little Annoying to visually line all of
1240:07 little Annoying to visually line all of this up but I think I'm still good and
1240:09 this up but I think I'm still good and now even though this code is at the top
1240:11 now even though this code is at the top of my file or really above the form tag
1240:15 of my file or really above the form tag itself I think we're okay so let's go
1240:17 itself I think we're okay so let's go back to the browser here reload the page
1240:20 back to the browser here reload the page type in David and click greet and we
1240:22 type in David and click greet and we still get the same correct behavior and
1240:25 still get the same correct behavior and so this is just a very common Paradigm
1240:26 so this is just a very common Paradigm to use these kinds of events to listen
1240:29 to use these kinds of events to listen and listen and listen for something to
1240:30 and listen and listen for something to happen and then only do something once
1240:33 happen and then only do something once that thing has transpired all right well
1240:35 that thing has transpired all right well let's take one more step with JavaScript
1240:37 let's take one more step with JavaScript code before we take a look at what's
1240:39 code before we take a look at what's really fun about this language and what
1240:40 really fun about this language and what you can do with browsers in particular
1240:43 you can do with browsers in particular by just cleaning things up a little bit
1240:44 by just cleaning things up a little bit further I'm going to go back into the
1240:46 further I'm going to go back into the code here and I'm actually going to
1240:48 code here and I'm actually going to remove uh or cut all of this code out of
1240:51 remove uh or cut all of this code out of the hello.html file itself and I'm going
1240:54 the hello.html file itself and I'm going to change my script tag to have nothing
1240:56 to change my script tag to have nothing in between the open and close tag but I
1240:59 in between the open and close tag but I am going to give it a source attribute
1241:01 am going to give it a source attribute and let's go ahead and call this for
1241:02 and let's go ahead and call this for instance hello.js sojs would be the
1241:05 instance hello.js sojs would be the convention for the file extension for a
1241:07 convention for the file extension for a Javascript file and even though this is
1241:09 Javascript file and even though this is a little weird that we have the script
1241:11 a little weird that we have the script tag and a source attribute then nothing
1241:13 tag and a source attribute then nothing in between the open and close tag this
1241:15 in between the open and close tag this is indeed the convention when you want
1241:16 is indeed the convention when you want to put all of your code in a separate
1241:19 to put all of your code in a separate file and let me go ahead and do that let
1241:20 file and let me go ahead and do that let me go ahead and open my terminal window
1241:23 me go ahead and open my terminal window create a new file called
1241:25 create a new file called hello.js and then in that file I'm just
1241:27 hello.js and then in that file I'm just going to paste the very code that I just
1241:29 going to paste the very code that I just cut from the previous file so no changes
1241:32 cut from the previous file so no changes to the code all I'm doing is factoring
1241:34 to the code all I'm doing is factoring it out and now I'm doing something just
1241:35 it out and now I'm doing something just like our CSS factorization before which
1241:38 like our CSS factorization before which confusingly use the link tag this uses
1241:40 confusingly use the link tag this uses the script tag this just now allows me
1241:42 the script tag this just now allows me to collaborate with someone like Carter
1241:43 to collaborate with someone like Carter or someone else so that they can do the
1241:45 or someone else so that they can do the JavaScript code I can do the HTML maybe
1241:47 JavaScript code I can do the HTML maybe a third person can do the CSS and indeed
1241:49 a third person can do the CSS and indeed maybe we can build even grander things
1241:52 maybe we can build even grander things by uh designing things in this way all
1241:55 by uh designing things in this way all right well let me go back to my browser
1241:57 right well let me go back to my browser again reload the page I shouldn't see
1242:00 again reload the page I shouldn't see any visual changes but if I type in my
1242:02 any visual changes but if I type in my name again David and click greet this
1242:05 name again David and click greet this still now works and what my browser has
1242:07 still now works and what my browser has just done underneath the hood is not
1242:09 just done underneath the hood is not only download the uh hello.html file as
1242:12 only download the uh hello.html file as always because there's now this script
1242:15 always because there's now this script tag that's referencing the source of
1242:16 tag that's referencing the source of another file just like an image tag
1242:18 another file just like an image tag might reference the source of an image
1242:20 might reference the source of an image the browser is automatically helping me
1242:22 the browser is automatically helping me out by loading that into its memory as
1242:25 out by loading that into its memory as well and now how about one final example
1242:29 well and now how about one final example and for this one I'm going to go ahead
1242:30 and for this one I'm going to go ahead and not write it live but open it up as
1242:32 and not write it live but open it up as prepared in advance just to show you
1242:33 prepared in advance just to show you what you can do by listening for some of
1242:36 what you can do by listening for some of these other events as well like the key
1242:38 these other events as well like the key up the finger going down the finger
1242:39 up the finger going down the finger going up and listening for exactly that
1242:41 going up and listening for exactly that so as the user is typing something you
1242:43 so as the user is typing something you can do something interesting as well I'm
1242:45 can do something interesting as well I'm going to go back into my directory
1242:47 going to go back into my directory listing here and I click on this Source
1242:49 listing here and I click on this Source a directory which has all of the
1242:50 a directory which has all of the examples that I wrote here in advance
1242:52 examples that I wrote here in advance and I'm going to scroll down to one
1242:54 and I'm going to scroll down to one called hello 5. HTML and in hello 5 now
1242:58 called hello 5. HTML and in hello 5 now we've gotten rid of the button and we
1243:00 we've gotten rid of the button and we just have this text box but notice now
1243:02 just have this text box but notice now what happens if I start typing my name
1243:04 what happens if I start typing my name as d a v i d D I'm not typing enter at
1243:09 as d a v i d D I'm not typing enter at all and in fact if I start deleting and
1243:11 all and in fact if I start deleting and I change my mind and start typing
1243:12 I change my mind and start typing Carter's name notice now that the web
1243:15 Carter's name notice now that the web page the Dom inside of the computer's
1243:17 page the Dom inside of the computer's memory is now automatically updating
1243:19 memory is now automatically updating itself so it's not even listening it
1243:21 itself so it's not even listening it would seem for a submit event anymore
1243:23 would seem for a submit event anymore but maybe for a key up event instead so
1243:26 but maybe for a key up event instead so let me go back to VSS code here open my
1243:29 let me go back to VSS code here open my terminal window and in my source a
1243:31 terminal window and in my source a directory let me open up hello 5. HTML
1243:35 directory let me open up hello 5. HTML inside of the script tag you'll see some
1243:37 inside of the script tag you'll see some code that's similar in spirit to before
1243:39 code that's similar in spirit to before whereby I'm adding an event listener to
1243:41 whereby I'm adding an event listener to the document waiting for the whole Dom's
1243:43 the document waiting for the whole Dom's content to be loaded but then inside of
1243:45 content to be loaded but then inside of that function I'm now doing this I'm
1243:49 that function I'm now doing this I'm creating a variable called input and
1243:51 creating a variable called input and selecting from the document the one and
1243:53 selecting from the document the one and only input tag that we saw just a moment
1243:55 only input tag that we saw just a moment ago I'm then adding on line 11 in event
1243:58 ago I'm then adding on line 11 in event listener for key up which is exactly
1244:01 listener for key up which is exactly that gesture so that I can execute some
1244:03 that gesture so that I can execute some additional code anytime the human lists
1244:04 additional code anytime the human lists their finger from the keyboard after
1244:06 their finger from the keyboard after typing a key what do I then do well I'm
1244:08 typing a key what do I then do well I'm going to go ahead it seems and declare
1244:10 going to go ahead it seems and declare another variable called name and I'm
1244:12 another variable called name and I'm just going to select some P tag on the
1244:14 just going to select some P tag on the page and now we didn't really see a P
1244:16 page and now we didn't really see a P tag so I think it's time to look at the
1244:17 tag so I think it's time to look at the HTML if I scroll down to the bottom of
1244:20 HTML if I scroll down to the bottom of the page where my actual HTML is you'll
1244:22 the page where my actual HTML is you'll see that there's just a form tag and no
1244:24 see that there's just a form tag and no onsubmit Handler anymore as before
1244:26 onsubmit Handler anymore as before there's just an input tag and no button
1244:29 there's just an input tag and no button at all but there is on line 29 here an
1244:32 at all but there is on line 29 here an open and close P tag just so I have an
1244:34 open and close P tag just so I have an empty placeholder in which to put
1244:35 empty placeholder in which to put something like hello David or hello
1244:38 something like hello David or hello Carter so that's why now on line 12 I
1244:41 Carter so that's why now on line 12 I can define a variable called name and I
1244:42 can define a variable called name and I can select that P tag so that what do I
1244:45 can select that P tag so that what do I want to do well if inside of the input
1244:48 want to do well if inside of the input there's a value so this is essentially
1244:50 there's a value so this is essentially checking for null or the absence of a
1244:52 checking for null or the absence of a string so if input. value does not equal
1244:56 string so if input. value does not equal nothing that is there is a value in that
1244:58 nothing that is there is a value in that text box well this syntax here on line
1245:00 text box well this syntax here on line 14 is a very clever way of doing the
1245:02 14 is a very clever way of doing the following go into that name tag that is
1245:06 following go into that name tag that is to say the empty par paragraph change
1245:09 to say the empty par paragraph change the inner HTML of it the HTML inside of
1245:11 the inner HTML of it the HTML inside of it to be literally this hello comma and
1245:15 it to be literally this hello comma and then just so you've seen an additional
1245:17 then just so you've seen an additional piece of syntax this is similar to
1245:18 piece of syntax this is similar to Python's F strings the syntax is a
1245:20 Python's F strings the syntax is a little weird in JavaScript you actually
1245:22 little weird in JavaScript you actually use not single quotes not double quotes
1245:24 use not single quotes not double quotes but back ticks which on a US English
1245:26 but back ticks which on a US English keyboard are typically the top leftand
1245:28 keyboard are typically the top leftand key of your keyboard or thereabouts and
1245:30 key of your keyboard or thereabouts and using dollar sign curly braces just like
1245:33 using dollar sign curly braces just like the curly braces alone in Python allows
1245:36 the curly braces alone in Python allows us to plug in whatever the value is of
1245:38 us to plug in whatever the value is of that input however if there's no value
1245:41 that input however if there's no value there it looks like I'm just going to
1245:42 there it looks like I'm just going to say hello whoever you are and in fact we
1245:45 say hello whoever you are and in fact we can see that behavior now is I delete
1245:47 can see that behavior now is I delete delete delete delete delete delete and
1245:49 delete delete delete delete delete and nothing's there now that if condition is
1245:51 nothing's there now that if condition is no longer true and so we see this
1245:53 no longer true and so we see this default value instead so this is only to
1245:56 default value instead so this is only to say that by harnessing these various
1245:58 say that by harnessing these various events that are constantly happening on
1246:00 events that are constantly happening on most any web page we can now register
1246:02 most any web page we can now register code just like we did way back in
1246:04 code just like we did way back in scratch to actually listen for those
1246:06 scratch to actually listen for those events and do something with them now it
1246:09 events and do something with them now it turns out we can do some interesting
1246:11 turns out we can do some interesting things even using third party coat and
1246:13 things even using third party coat and just as we used bootstrap a bit ago to
1246:14 just as we used bootstrap a bit ago to make our table prettier allow me to
1246:17 make our table prettier allow me to propose that we also take a look at this
1246:19 propose that we also take a look at this version of favorites as well let me go
1246:21 version of favorites as well let me go back into my source a directory and open
1246:24 back into my source a directory and open up favorites 2 which I made in advance
1246:26 up favorites 2 which I made in advance which looks almost the same though I've
1246:27 which looks almost the same though I've zoomed in here a bit but you'll notice
1246:29 zoomed in here a bit but you'll notice somewhat subtly over the leftmost column
1246:34 somewhat subtly over the leftmost column in this table you'll see now this arrow
1246:37 in this table you'll see now this arrow in Gray pointing up and pointing down
1246:39 in Gray pointing up and pointing down previously those were not there all I
1246:40 previously those were not there all I had was a static HTML table with all of
1246:43 had was a static HTML table with all of this data sorted in whatever order it
1246:45 this data sorted in whatever order it was inputed the other day in that form
1246:47 was inputed the other day in that form but now notice what I can do if I want
1246:50 but now notice what I can do if I want to sort in uh one order I can click this
1246:53 to sort in uh one order I can click this Arrow or the other order I can sort in
1246:55 Arrow or the other order I can sort in this Arrow so essentially doing it
1246:56 this Arrow so essentially doing it chronologically forward or backward now
1246:59 chronologically forward or backward now how is that sorting happening it's
1247:00 how is that sorting happening it's presumably based on all of the
1247:02 presumably based on all of the timestamps that were registered when we
1247:04 timestamps that were registered when we submitted that Google form just a bit
1247:06 submitted that Google form just a bit ago um uh when it was live but now using
1247:10 ago um uh when it was live but now using JavaScript it turns out that we can use
1247:12 JavaScript it turns out that we can use some logic somehow and sort this data by
1247:14 some logic somehow and sort this data by the same and you don't get that
1247:15 the same and you don't get that automatically just by using HTML alone
1247:18 automatically just by using HTML alone now how did I achieve that well it turns
1247:20 now how did I achieve that well it turns out if I go ahead and close these hello
1247:22 out if I go ahead and close these hello files and in vs code let's open up uh
1247:25 files and in vs code let's open up uh favorites 2. HTML you'll see that all of
1247:28 favorites 2. HTML you'll see that all of the HTML is actually the same if I
1247:30 the HTML is actually the same if I scroll down and down through this file
1247:32 scroll down and down through this file but I added a little something
1247:34 but I added a little something interesting at top I copied and pasted
1247:36 interesting at top I copied and pasted the appropriate URLs and HTML tags from
1247:39 the appropriate URLs and HTML tags from bootstraps documentation and you'll see
1247:41 bootstraps documentation and you'll see here that I have a file called not only
1247:43 here that I have a file called not only bootstrap.css but also
1247:46 bootstrap.css but also bootstrap.min.js as well as a couple of
1247:49 bootstrap.min.js as well as a couple of other things as well that allows me
1247:51 other things as well that allows me ultimately using third-party libraries
1247:54 ultimately using third-party libraries to add some special HTML attributes that
1247:56 to add some special HTML attributes that those libraries told me to add and then
1247:59 those libraries told me to add and then as soon as those libraries detect the
1248:01 as soon as those libraries detect the presence of these attributes now on my
1248:02 presence of these attributes now on my own raw data they do their thing and
1248:05 own raw data they do their thing and javascrip defy not a technical term the
1248:07 javascrip defy not a technical term the entire table and now make it interactive
1248:10 entire table and now make it interactive and not static so you'll see here that
1248:12 and not static so you'll see here that the Aesthetics of the table are as
1248:14 the Aesthetics of the table are as before table and table striped but I'm
1248:16 before table and table striped but I'm adding now another HTML attribute called
1248:18 adding now another HTML attribute called Data toggle whose value is table and I
1248:20 Data toggle whose value is table and I know that only from the documentation of
1248:22 know that only from the documentation of these libraries indicating that's that's
1248:24 these libraries indicating that's that's how I can now enable this table to be
1248:27 how I can now enable this table to be interactive as I can too by adding data
1248:29 interactive as I can too by adding data sortable equals quote unquote true on
1248:32 sortable equals quote unquote true on specifically the timestamp column and
1248:34 specifically the timestamp column and the only thing unfamiliar here perhaps
1248:36 the only thing unfamiliar here perhaps is I'm using t P for table heading as
1248:39 is I'm using t P for table heading as opposed to TD as I do elsewhere but
1248:41 opposed to TD as I do elsewhere but that's all that it takes to now focus on
1248:43 that's all that it takes to now focus on the raw data you want to present and let
1248:45 the raw data you want to present and let someone else do the heavy lifting of
1248:47 someone else do the heavy lifting of actually implementing the logic well
1248:49 actually implementing the logic well let's end with just a look at what more
1248:52 let's end with just a look at what more you can do with Java script and just how
1248:54 you can do with Java script and just how powerful it is when you combine a
1248:56 powerful it is when you combine a language like this with the data and the
1248:59 language like this with the data and the uh user interface you want to convey
1249:01 uh user interface you want to convey let's go ahead and open up within Source
1249:03 let's go ahead and open up within Source a directory something called background.
1249:05 a directory something called background. HTML now this interface here is quite
1249:08 HTML now this interface here is quite simple it just has three buttons RG and
1249:10 simple it just has three buttons RG and B and the background of course noticed
1249:12 B and the background of course noticed by default is just white but when I
1249:14 by default is just white but when I click on the r the background turns red
1249:16 click on the r the background turns red when the I click on the G it turns green
1249:18 when the I click on the G it turns green and the blue it turns blue and again and
1249:20 and the blue it turns blue and again and again so how is this working well if you
1249:23 again so how is this working well if you think back again to the available events
1249:25 think back again to the available events perhaps I'm just listening for a click
1249:27 perhaps I'm just listening for a click on those buttons and then doing
1249:29 on those buttons and then doing something with maybe the CSS of the page
1249:31 something with maybe the CSS of the page to allow me to see those different
1249:33 to allow me to see those different colors so in fact let's go back to vs
1249:35 colors so in fact let's go back to vs code here and let's open up background.
1249:38 code here and let's open up background. HTML and in here you'll see some simple
1249:41 HTML and in here you'll see some simple HTML at the top just three buttons but
1249:43 HTML at the top just three buttons but I've given each a unique ID so that I
1249:46 I've given each a unique ID so that I can reference it in code and then inside
1249:48 can reference it in code and then inside of a script tag here below because I
1249:50 of a script tag here below because I didn't bother with the uh Dom content
1249:52 didn't bother with the uh Dom content loaded event here notice that I'm doing
1249:55 loaded event here notice that I'm doing the following I'm creating a variable
1249:57 the following I'm creating a variable called body that lets me select the body
1249:59 called body that lets me select the body tag I then have in these three line some
1250:02 tag I then have in these three line some code that handles red what am I doing
1250:04 code that handles red what am I doing well I'm selecting from the document
1250:06 well I'm selecting from the document whatever h HTML tag has unique ID of red
1250:09 whatever h HTML tag has unique ID of red and then I'm adding an event listener
1250:11 and then I'm adding an event listener for any click on that button and anytime
1250:13 for any click on that button and anytime someone clicks on that red button I call
1250:15 someone clicks on that red button I call this function anonymously it doesn't
1250:17 this function anonymously it doesn't even have or need a name and this syntax
1250:20 even have or need a name and this syntax here is powerful because now in
1250:21 here is powerful because now in JavaScript I can alter the CSS of my
1250:24 JavaScript I can alter the CSS of my page by doing body which is the tag that
1250:26 page by doing body which is the tag that I selected two lines ago accessing its
1250:29 I selected two lines ago accessing its style accessing its background color
1250:32 style accessing its background color property and setting it equal to quote
1250:34 property and setting it equal to quote unquote red and I do the same down below
1250:36 unquote red and I do the same down below for green I do the same down below for
1250:38 for green I do the same down below for blue and the only thing worth noting
1250:39 blue and the only thing worth noting here is that in CSS it turns out it's
1250:41 here is that in CSS it turns out it's the case that the CSS property for the
1250:44 the case that the CSS property for the background color of a page is actually
1250:46 background color of a page is actually background Das color in all lower case
1250:49 background Das color in all lower case with a hyphen in between unfortunately
1250:51 with a hyphen in between unfortunately in the world of JavaScript a hyphen
1250:53 in the world of JavaScript a hyphen would be mistaken for subtraction like
1250:55 would be mistaken for subtraction like background minus color which would be
1250:57 background minus color which would be wrong so the convention in JavaScript is
1250:59 wrong so the convention in JavaScript is when you're trying to manipulate CSS you
1251:01 when you're trying to manipulate CSS you take whatever the property name is uh
1251:04 take whatever the property name is uh font size background color and you
1251:06 font size background color and you change it into so-called camel case here
1251:09 change it into so-called camel case here you get rid of The Hyphen and you
1251:10 you get rid of The Hyphen and you capitalize the subsequent words like
1251:12 capitalize the subsequent words like color in this case here all right how
1251:15 color in this case here all right how about another well it turns out back in
1251:16 about another well it turns out back in the day back in my day there was a HTML
1251:19 the day back in my day there was a HTML tag that would actually allow you to do
1251:21 tag that would actually allow you to do this create blinking text on a screen
1251:24 this create blinking text on a screen it's rather unpleasant at this rate
1251:26 it's rather unpleasant at this rate certainly but how might this work well
1251:28 certainly but how might this work well it turns out in JavaScript if we take a
1251:29 it turns out in JavaScript if we take a look at the blink. HTML file here we'll
1251:33 look at the blink. HTML file here we'll see that you can in your HTML do
1251:36 see that you can in your HTML do something literally as simple as hello
1251:38 something literally as simple as hello world in the body but then you can call
1251:41 world in the body but then you can call this function here turns out just like
1251:43 this function here turns out just like document there's another Global special
1251:45 document there's another Global special variable you can use in JavaScript and
1251:46 variable you can use in JavaScript and browsers called window which refers to
1251:48 browsers called window which refers to all things related to the window itself
1251:50 all things related to the window itself the window comes with a set interval
1251:52 the window comes with a set interval function which lets you do exactly that
1251:54 function which lets you do exactly that set an interval in milliseconds and
1251:57 set an interval in milliseconds and after every expiration of that interval
1251:59 after every expiration of that interval some function will be called for you so
1252:01 some function will be called for you so in this case it's saying every 50
1252:03 in this case it's saying every 50 milliseconds but let's actually slow
1252:05 milliseconds but let's actually slow that down now to 500 milliseconds or for
1252:07 that down now to 500 milliseconds or for one half a second call a function called
1252:09 one half a second call a function called blink notice I do not have parentheses
1252:12 blink notice I do not have parentheses after the blink name because I don't
1252:13 after the blink name because I don't want to call blink now I want to tell
1252:16 want to call blink now I want to tell JavaScript to call the function called
1252:17 JavaScript to call the function called blink every 500 milliseconds now we'll
1252:20 blink every 500 milliseconds now we'll see in a moment what that code looks
1252:21 see in a moment what that code looks like but let's go back to the page and
1252:22 like but let's go back to the page and reload and you'll see now that it's a
1252:24 reload and you'll see now that it's a more pleasant blinking if that's even
1252:26 more pleasant blinking if that's even the case every half second because I'm
1252:29 the case every half second because I'm now firing that event that is I'm uh
1252:32 now firing that event that is I'm uh calling that function now every 500
1252:34 calling that function now every 500 milliseconds instead how am I doing that
1252:36 milliseconds instead how am I doing that well this same script tag I've invented
1252:39 well this same script tag I've invented my own blink function this is has the
1252:41 my own blink function this is has the distinction back in the day of actually
1252:43 distinction back in the day of actually being an HTML tag and among the few tags
1252:45 being an HTML tag and among the few tags that the world removed and got rid of so
1252:48 that the world removed and got rid of so that it's no longer used because it's
1252:49 that it's no longer used because it's not all that user friendly but down here
1252:52 not all that user friendly but down here what am I doing I'm getting the body of
1252:54 what am I doing I'm getting the body of the document itself with this variable
1252:57 the document itself with this variable and then I'm checking two CSS properties
1252:59 and then I'm checking two CSS properties that we didn't talk about before but it
1253:00 that we didn't talk about before but it turns out that you can check and set the
1253:03 turns out that you can check and set the visibility of an element in JavaScript
1253:05 visibility of an element in JavaScript by going into that tag checking its
1253:07 by going into that tag checking its style and getting its visibility uh
1253:10 style and getting its visibility uh property and if it happens to equal
1253:12 property and if it happens to equal hidden the next line of code here 13
1253:15 hidden the next line of code here 13 sets it equal to visible instead else if
1253:18 sets it equal to visible instead else if it's not hidden it must already be
1253:19 it's not hidden it must already be visible and so line 17 flips it the
1253:21 visible and so line 17 flips it the other way and changes it to Hidden here
1253:24 other way and changes it to Hidden here left hand right hand clearly not talking
1253:26 left hand right hand clearly not talking no idea why the opposite of visible Is
1253:28 no idea why the opposite of visible Is Not Invisible it's indeed visible and
1253:31 Not Invisible it's indeed visible and hidden but this just allows you every
1253:33 hidden but this just allows you every time this function is called to change
1253:35 time this function is called to change the property from one value to another
1253:37 the property from one value to another achieving that blinking effect you can
1253:39 achieving that blinking effect you can do even more powerful things that you
1253:41 do even more powerful things that you and I take for granted every day let's
1253:42 and I take for granted every day let's go into Source 8 and go to autocom
1253:45 go into Source 8 and go to autocom complete. HTML which I wrote in advance
1253:47 complete. HTML which I wrote in advance and this is a page that also loads into
1253:50 and this is a page that also loads into memory a really big dictionary that you
1253:52 memory a really big dictionary that you might recall from problem set five and
1253:55 might recall from problem set five and if I go ahead and type in something like
1253:56 if I go ahead and type in something like c a t you'll notice dynamically an
1254:01 c a t you'll notice dynamically an unordered bulleted list appearing below
1254:03 unordered bulleted list appearing below the text box that shows you all of the
1254:05 the text box that shows you all of the words in that dictionary from pet 5 that
1254:08 words in that dictionary from pet 5 that start with c and then CA and then T just
1254:11 start with c and then CA and then T just like the autocomplete you see every day
1254:13 like the autocomplete you see every day on your phone in Google or websites like
1254:15 on your phone in Google or websites like it how is that working well probably I'm
1254:17 it how is that working well probably I'm listening for the key press going up as
1254:20 listening for the key press going up as soon as that key is pressed I'm probably
1254:22 soon as that key is pressed I'm probably searching through a big array uh really
1254:24 searching through a big array uh really of all of those words maybe using linear
1254:26 of all of those words maybe using linear search maybe binary search if uh faster
1254:29 search maybe binary search if uh faster than that and then looking for any
1254:30 than that and then looking for any string in that array that starts with C
1254:33 string in that array that starts with C or CA or c a t and then I'm generating
1254:36 or CA or c a t and then I'm generating automattic IC Ally the HTML therefore
1254:39 automattic IC Ally the HTML therefore but perhaps most familiar nowadays is
1254:41 but perhaps most familiar nowadays is just how much your phone and your laptop
1254:43 just how much your phone and your laptop know about you and let me go into a
1254:46 know about you and let me go into a final example here in Source 8 called
1254:49 final example here in Source 8 called geolocation HTML which is a term of art
1254:51 geolocation HTML which is a term of art that just refers to figuring out your
1254:53 that just refers to figuring out your geography for instance your GPS
1254:54 geography for instance your GPS coordinates now here we have a third and
1254:57 coordinates now here we have a third and final Global variable that your browser
1254:59 final Global variable that your browser provides you with called Navigator and
1255:01 provides you with called Navigator and in Navigator there's a special object
1255:03 in Navigator there's a special object called geolocation that comes with a
1255:04 called geolocation that comes with a function called get current position
1255:07 function called get current position so if you call Navigator geolocation dog
1255:11 so if you call Navigator geolocation dog getcurrent position and then pass in a
1255:13 getcurrent position and then pass in a function of your own that doesn't need a
1255:14 function of your own that doesn't need a name but does take an argument in this
1255:16 name but does take an argument in this case I called it position as soon as the
1255:19 case I called it position as soon as the browser or as soon as the phone has
1255:20 browser or as soon as the phone has figured out where in the world that user
1255:23 figured out where in the world that user is with that browser at some latitude
1255:25 is with that browser at some latitude and longitude the browser will
1255:27 and longitude the browser will automatically call that function for you
1255:30 automatically call that function for you and you can do anything you want with
1255:31 and you can do anything you want with the position that comes back the
1255:32 the position that comes back the latitude and longitude respectively so
1255:34 latitude and longitude respectively so I'm going to use a function that's not
1255:36 I'm going to use a function that's not often that help but for our purposes
1255:38 often that help but for our purposes today it's just going to write to the
1255:39 today it's just going to write to the document itself to my big rectangular
1255:41 document itself to my big rectangular region whatever the latitude is then a
1255:43 region whatever the latitude is then a comma and then the longitude as well so
1255:46 comma and then the longitude as well so if we go back with this final flourish
1255:48 if we go back with this final flourish into Source 8 open up geolocation HTML
1255:53 into Source 8 open up geolocation HTML you'll see that my browser first wants
1255:54 you'll see that my browser first wants my permission to let this website my own
1255:57 my permission to let this website my own know my location I'm going to go ahead
1255:59 know my location I'm going to go ahead and click allow crossing my fingers
1256:01 and click allow crossing my fingers because it might take a moment for the
1256:02 because it might take a moment for the phone or the laptop to figure it out and
1256:04 phone or the laptop to figure it out and it looks like according to Google I am
1256:07 it looks like according to Google I am right this moment with my Mac at
1256:09 right this moment with my Mac at latitude longitude 42375 comma
1256:13 latitude longitude 42375 comma 7111 let's go ahead and highlight and
1256:15 7111 let's go ahead and highlight and copy that let's go to a website like
1256:17 copy that let's go to a website like Googl maps.com crossing our fingers if
1256:20 Googl maps.com crossing our fingers if you've never done this you can search
1256:21 you've never done this you can search for GPS coordinates too let's hit enter
1256:24 for GPS coordinates too let's hit enter and amazing we are indeed in Sanders
1256:28 and amazing we are indeed in Sanders Theater roughly there standing on this
1256:30 Theater roughly there standing on this stage on Halloween and that then is week
1256:34 stage on Halloween and that then is week eight we will see you next time happy
1256:36 eight we will see you next time happy Halloween
1256:38 Halloween nothing
1256:39 nothing go buffering
1256:42 go buffering okay Josh
1256:50 [Music] nice it's Moy no oh
1257:07 Josh uh um Sophie [Music]
1257:14 [Music] amazing that was
1257:16 amazing that was [Music]
1257:20 [Music] perfect I think I
1257:23 perfect I think I hey to
1257:25 hey to you
1257:30 [Music] oh that was amazing thank you all so
1257:35 oh that was amazing thank you all so good
1259:30 all right this is cs50 and this is already week nine which is our second to
1259:33 already week nine which is our second to last indeed this is really the last week
1259:35 last indeed this is really the last week where you'll learn in this class how to
1259:37 where you'll learn in this class how to program but indeed it's this week that's
1259:39 program but indeed it's this week that's really meant to be the the pedagogical
1259:41 really meant to be the the pedagogical climax of like all of these various
1259:43 climax of like all of these various languages we've been looking at all of
1259:44 languages we've been looking at all of these various techniques all of this
1259:46 these various techniques all of this syntax so that at the end of cs50 in
1259:48 syntax so that at the end of cs50 in just a few weeks you indeed feel that
1259:50 just a few weeks you indeed feel that you didn't take a class on C and you
1259:52 you didn't take a class on C and you didn't take a class on python but you
1259:53 didn't take a class on python but you really more generally took a class on
1259:55 really more generally took a class on programming because indeed we know
1259:56 programming because indeed we know already about half of you uh will go on
1259:59 already about half of you uh will go on to study computer science further but
1260:00 to study computer science further but half of you will not and indeed all of
1260:02 half of you will not and indeed all of your programming chops here on out
1260:04 your programming chops here on out theoretically will have a foundation in
1260:06 theoretically will have a foundation in what we been doing these past many weeks
1260:08 what we been doing these past many weeks but here on out it's really going to be
1260:09 but here on out it's really going to be up to you to learn some new fangled
1260:11 up to you to learn some new fangled language when it comes out or to follow
1260:13 language when it comes out or to follow some new trend when some language
1260:15 some new trend when some language eclipses the ones we've been using as
1260:17 eclipses the ones we've been using as more popular as more appropriate for
1260:19 more popular as more appropriate for problems you want to solve and so today
1260:21 problems you want to solve and so today really is about synthesizing so many of
1260:23 really is about synthesizing so many of the past few weeks but doing it in the
1260:25 the past few weeks but doing it in the context of web programming which For
1260:26 context of web programming which For Better or For Worse is so very much
1260:28 Better or For Worse is so very much invogue nowadays both on our laptops and
1260:30 invogue nowadays both on our laptops and phones and indeed the languages we
1260:32 phones and indeed the languages we looked at in recent weeks are used not
1260:34 looked at in recent weeks are used not only to make websites but also full
1260:36 only to make websites but also full fledged applications and app stores and
1260:38 fledged applications and app stores and the like so this really will be the
1260:40 the like so this really will be the culmination of those past several weeks
1260:42 culmination of those past several weeks and indeed we'll even talk about some
1260:43 and indeed we'll even talk about some familiar Concepts like shopping carts
1260:45 familiar Concepts like shopping carts when you're on Amazon and these things
1260:47 when you're on Amazon and these things called cookies when you're visiting
1260:48 called cookies when you're visiting websites all of those topics too will
1260:50 websites all of those topics too will come into play and you'll have an
1260:52 come into play and you'll have an understanding of what all that means
1260:53 understanding of what all that means from the ground up so how did we get
1260:55 from the ground up so how did we get here well just last week we focused on
1260:58 here well just last week we focused on HTML and CSS primarily which are not
1261:00 HTML and CSS primarily which are not programming languages they're just about
1261:02 programming languages they're just about Aesthetics structuring your data
1261:04 Aesthetics structuring your data presenting your data and so forth um and
1261:06 presenting your data and so forth um and we served the web pages we wrote using
1261:09 we served the web pages we wrote using this program HTTP server this is just
1261:11 this program HTTP server this is just one such program there's dozens hundreds
1261:13 one such program there's dozens hundreds of different web servers that you can
1261:16 of different web servers that you can use out there this is just a super
1261:17 use out there this is just a super simple one we pre-installed in your
1261:19 simple one we pre-installed in your codes space for you in vs code so that
1261:21 codes space for you in vs code so that you can just serve up web pages at the
1261:23 you can just serve up web pages at the end of last week two though we teased
1261:25 end of last week two though we teased JavaScript a full-fledged programming
1261:27 JavaScript a full-fledged programming language that you can use to manipulate
1261:30 language that you can use to manipulate the users experience for the better to
1261:32 the users experience for the better to make things more Dynamic and interactive
1261:34 make things more Dynamic and interactive by actually running code in the user
1261:36 by actually running code in the user users's browser on their Mac their PC
1261:38 users's browser on their Mac their PC their phone as opposed to server side
1261:40 their phone as opposed to server side which up until now is where all of our
1261:42 which up until now is where all of our code in C and python has been written so
1261:44 code in C and python has been written so you're writing code on a server you're
1261:46 you're writing code on a server you're serving code from a server but now with
1261:48 serving code from a server but now with HTML CSS and JavaScript it's getting
1261:50 HTML CSS and JavaScript it's getting executed in a browser but today we're
1261:53 executed in a browser but today we're going to give you one final feature of
1261:55 going to give you one final feature of python or really languages like it that
1261:57 python or really languages like it that you can also use code on the server to
1261:59 you can also use code on the server to generate automatically dynamically the
1262:02 generate automatically dynamically the HTML uh the JavaScript the CSS that you
1262:04 HTML uh the JavaScript the CSS that you actually want the user to rece receive
1262:06 actually want the user to rece receive you don't have to hardcode everything as
1262:08 you don't have to hardcode everything as you have when making your own homepage
1262:10 you have when making your own homepage well let's consider what what some of
1262:12 well let's consider what what some of the building blocks were last week so
1262:14 the building blocks were last week so here's a sample URL and over here slash
1262:17 here's a sample URL and over here slash is sort of the default page on any web
1262:19 is sort of the default page on any web server it might be index.html it might
1262:21 server it might be index.html it might be something else that's just a
1262:22 be something else that's just a convention but it refers to whatever the
1262:24 convention but it refers to whatever the default actually is you can visit of
1262:26 default actually is you can visit of course when any browser like a URL that
1262:28 course when any browser like a URL that ends in file. HTML or something else.
1262:31 ends in file. HTML or something else. HTML and that literally means your
1262:33 HTML and that literally means your browser wants this file on this server
1262:36 browser wants this file on this server or of course we saw that it can be a
1262:37 or of course we saw that it can be a folder and inside of that folder is
1262:40 folder and inside of that folder is presumably some default file name like
1262:42 presumably some default file name like again index.html or you can be more
1262:44 again index.html or you can be more explicit like folder file. HTML and
1262:47 explicit like folder file. HTML and these more generally we just called
1262:48 these more generally we just called paths and indeed a path is just a
1262:51 paths and indeed a path is just a location on your Mac your PC or on a
1262:53 location on your Mac your PC or on a server of some piece of information but
1262:55 server of some piece of information but today we're just going to rename this
1262:57 today we're just going to rename this only to use other common terminology but
1262:59 only to use other common terminology but they're really just synonyms today we're
1263:01 they're really just synonyms today we're going to refer to those same things as
1263:02 going to refer to those same things as routes because now today we're going to
1263:04 routes because now today we're going to ultimately replace HT PTP server which
1263:07 ultimately replace HT PTP server which just serves up static content that you
1263:09 just serves up static content that you all write with your own web server like
1263:11 all write with your own web server like now you will be the ones controlling
1263:13 now you will be the ones controlling what it is the server does in response
1263:15 what it is the server does in response to the user in uh requests so that you
1263:18 to the user in uh requests so that you can respond interactively and
1263:19 can respond interactively and dynamically but we're still going to see
1263:21 dynamically but we're still going to see techniques like this these were our
1263:23 techniques like this these were our so-called HTTP parameters they're
1263:26 so-called HTTP parameters they're everything that comes after a question
1263:27 everything that comes after a question mark in a URL and it can be like key
1263:29 mark in a URL and it can be like key equals value and an example was what
1263:31 equals value and an example was what when we played with Google what was the
1263:34 when we played with Google what was the key and what was the value
1263:36 key and what was the value that I first tried any
1263:42 recollection I was searching for cats and so the key I figured that was Q
1263:44 and so the key I figured that was Q because that's what Larry and Sergey who
1263:46 because that's what Larry and Sergey who created Google years ago decided the
1263:48 created Google years ago decided the name would be of the HTML text box that
1263:51 name would be of the HTML text box that you type your query into and if I type
1263:53 you type your query into and if I type cat for cat the value of that would end
1263:55 cat for cat the value of that would end up in the URL for Google as being
1263:57 up in the URL for Google as being question mark cat equals value and I
1263:59 question mark cat equals value and I mentioned that it's often the case that
1264:01 mentioned that it's often the case that you want to send two different inputs to
1264:02 you want to send two different inputs to a server and this is why I propose that
1264:04 a server and this is why I propose that you just keep an eye out for Amper Sands
1264:05 you just keep an eye out for Amper Sands and and Ampersand separate these key
1264:07 and and Ampersand separate these key value pairs but again this is the same
1264:09 value pairs but again this is the same darn Paradigm as before and we've seen
1264:11 darn Paradigm as before and we've seen this so many times right key value pairs
1264:13 this so many times right key value pairs in dictionaries in Python we've seen uh
1264:15 in dictionaries in Python we've seen uh HTML attributes and their values we've
1264:18 HTML attributes and their values we've seen CSS properties and their values
1264:21 seen CSS properties and their values it's all the same thing associating
1264:23 it's all the same thing associating something with something else even
1264:24 something with something else even though every language every person seems
1264:26 though every language every person seems to have their own uh vernacular for it
1264:28 to have their own uh vernacular for it it really is just the same idea this
1264:30 it really is just the same idea this associating of something with something
1264:32 associating of something with something else we'll continue to see and here to
1264:35 else we'll continue to see and here to be concrete were the
1264:37 be concrete were the HTTP lines of text that were in those
1264:40 HTTP lines of text that were in those virtual envelopes if you will if I were
1264:42 virtual envelopes if you will if I were indeed selecting trying to search for
1264:44 indeed selecting trying to search for something like cats on Google this
1264:45 something like cats on Google this recall was the message that got sent to
1264:48 recall was the message that got sent to the server by my browser in order to
1264:50 the server by my browser in order to tell Google to please search for not
1264:52 tell Google to please search for not dogs but in this case cats now what is
1264:55 dogs but in this case cats now what is HTTP server been doing for us well it's
1264:57 HTTP server been doing for us well it's just been serving up HTML files CSS
1264:59 just been serving up HTML files CSS files maybe some js or JavaScript files
1265:02 files maybe some js or JavaScript files but it has been ignoring any HTTP
1265:05 but it has been ignoring any HTTP parameters like HTTP server does not
1265:07 parameters like HTTP server does not take user input why well what's it going
1265:09 take user input why well what's it going to do with it because you already wrote
1265:10 to do with it because you already wrote the HTML you already wrote the CSS like
1265:12 the HTML you already wrote the CSS like there's no decisions to be made until we
1265:15 there's no decisions to be made until we introduce a proper programming language
1265:17 introduce a proper programming language on the server and so we're going to move
1265:18 on the server and so we're going to move away now from this simple HTTP server
1265:20 away now from this simple HTTP server program and introduce you to your own
1265:23 program and introduce you to your own server that's going to handle the
1265:25 server that's going to handle the parsing that is the extraction of these
1265:27 parsing that is the extraction of these key value pairs so that you and I don't
1265:29 key value pairs so that you and I don't have to write python code all of a
1265:30 have to write python code all of a sudden that like analyzes this stuff
1265:32 sudden that like analyzes this stuff figures out what pages requested the key
1265:34 figures out what pages requested the key value pairs all of that were still going
1265:36 value pairs all of that were still going to get for free by just using the right
1265:38 to get for free by just using the right framework and so today we revisit python
1265:42 framework and so today we revisit python uh which we've now used in some form the
1265:44 uh which we've now used in some form the past few weeks and indeed it's kind of
1265:45 past few weeks and indeed it's kind of been the glue that allows us to stitch
1265:47 been the glue that allows us to stitch together some of our own logic we saw it
1265:49 together some of our own logic we saw it with SQL we're going to now see it with
1265:51 with SQL we're going to now see it with HTML CSS and even JavaScript if we want
1265:54 HTML CSS and even JavaScript if we want and we're also going to see another
1265:55 and we're also going to see another language today not a programming
1265:56 language today not a programming language called Ginga and this is going
1265:58 language called Ginga and this is going to be a common Paradigm in the real
1265:59 to be a common Paradigm in the real world whereby different languages
1266:02 world whereby different languages different libraries different Frameworks
1266:04 different libraries different Frameworks often like borrow from each other or
1266:06 often like borrow from each other or they use uh technologies that someone
1266:08 they use uh technologies that someone else wrote just so they don't have to
1266:09 else wrote just so they don't have to reinvent that wheel so flask is just a
1266:12 reinvent that wheel so flask is just a framework that is a third party Library
1266:14 framework that is a third party Library it's pretty popular nowadays it's
1266:15 it's pretty popular nowadays it's relatively simple which is why we use it
1266:17 relatively simple which is why we use it in cs50 if you've programmed before cs50
1266:19 in cs50 if you've programmed before cs50 Jango is another popular framework or
1266:22 Jango is another popular framework or library in the python sta space but it's
1266:24 library in the python sta space but it's a little more complicated so we focus on
1266:26 a little more complicated so we focus on flask and Ginga we'll see is not a
1266:28 flask and Ginga we'll see is not a programming language it's just going to
1266:30 programming language it's just going to be some syntax thankfully familiar with
1266:32 be some syntax thankfully familiar with curly braces that allow us to use
1266:34 curly braces that allow us to use placeholders in our actual web pages so
1266:37 placeholders in our actual web pages so again you'll wrap your minds all
1266:39 again you'll wrap your minds all eventually around where the lines are
1266:40 eventually around where the lines are among these various Technologies but
1266:42 among these various Technologies but these are not the interesting ideas the
1266:43 these are not the interesting ideas the interesting ideas are the ones we'll
1266:45 interesting ideas are the ones we'll focus on in code but starting today
1266:47 focus on in code but starting today instead of running HTTP server to serve
1266:50 instead of running HTTP server to serve up a static website we'll have you start
1266:52 up a static website we'll have you start running literally flask space run in
1266:55 running literally flask space run in your terminal window to run your own web
1266:58 your terminal window to run your own web server that's implemented in Python
1267:01 server that's implemented in Python using this flask framework so bootstrap
1267:04 using this flask framework so bootstrap was a library for making your CSS and
1267:07 was a library for making your CSS and JavaScript prettier and more interactive
1267:09 JavaScript prettier and more interactive flask is a framework or library for just
1267:11 flask is a framework or library for just making your python code more pleasant to
1267:13 making your python code more pleasant to use since you're borrowing features from
1267:16 use since you're borrowing features from someone else all right so how can we go
1267:18 someone else all right so how can we go about doing this well if you are to
1267:19 about doing this well if you are to write your very own web application your
1267:22 write your very own web application your own amazon.com your own google.com in
1267:25 own amazon.com your own google.com in Python using flask minimally you need to
1267:27 Python using flask minimally you need to have a file called app.py by convention
1267:30 have a file called app.py by convention which is where all your python code goes
1267:31 which is where all your python code goes and then a folder called templates which
1267:33 and then a folder called templates which is where all of your templates go and
1267:35 is where all of your templates go and for now your templates are just your
1267:36 for now your templates are just your HTML files so if we're going to now
1267:38 HTML files so if we're going to now start building more interesting
1267:40 start building more interesting interactive things like google.com or
1267:42 interactive things like google.com or amazon.com we need to be able to execute
1267:45 amazon.com we need to be able to execute code on the server and so this is the
1267:46 code on the server and so this is the convention it's not index.html anymore
1267:48 convention it's not index.html anymore necessarily it's these two things at the
1267:50 necessarily it's these two things at the top level with that said we'll quickly
1267:53 top level with that said we'll quickly see that there's some other conventions
1267:54 see that there's some other conventions and in my examples online and in the uh
1267:57 and in my examples online and in the uh problem set nine you'll see another file
1267:59 problem set nine you'll see another file called requirements.txt which is just a
1268:01 called requirements.txt which is just a text file that allows you to enumerate
1268:03 text file that allows you to enumerate all of the thirdparty libraries your
1268:05 all of the thirdparty libraries your application might want to use it's a
1268:07 application might want to use it's a convention so that the server can like
1268:09 convention so that the server can like automatically install things for you
1268:11 automatically install things for you without you having to do it manually and
1268:12 without you having to do it manually and then static is going to be where
1268:14 then static is going to be where literally your static content goes so if
1268:16 literally your static content goes so if you've got images for your web
1268:17 you've got images for your web application if you've got JavaScript
1268:19 application if you've got JavaScript files CSS files by convention that goes
1268:22 files CSS files by convention that goes in static these are just conventions
1268:24 in static these are just conventions like all of this can be changed but this
1268:25 like all of this can be changed but this is like the way to do things so we'll
1268:27 is like the way to do things so we'll introduce you to the defaults all right
1268:30 introduce you to the defaults all right so what does this mean how for instance
1268:32 so what does this mean how for instance could I go about implementing my own web
1268:34 could I go about implementing my own web application using python that somehow
1268:37 application using python that somehow spits out a message like Hello World all
1268:40 spits out a message like Hello World all right well turns out just this now we'll
1268:43 right well turns out just this now we'll tease this apart in just a moment but
1268:45 tease this apart in just a moment but this is the content of a sample app.py
1268:48 this is the content of a sample app.py file that apparently uses some Library
1268:50 file that apparently uses some Library stuff like familiar syntax from
1268:52 stuff like familiar syntax from something import something else we've
1268:54 something import something else we've seen that before with csvs and other
1268:56 seen that before with csvs and other libraries this is somewhat new syntax
1268:58 libraries this is somewhat new syntax but it's kind of copy paste for now this
1269:00 but it's kind of copy paste for now this is definitely new syntax and kind of
1269:01 is definitely new syntax and kind of weird with the at sign here but we'll
1269:03 weird with the at sign here but we'll see this again and again today and it's
1269:05 see this again and again today and it's just copy paste initially until you
1269:07 just copy paste initially until you understand what it's doing for you but
1269:08 understand what it's doing for you but at least there's some familiar stuff
1269:10 at least there's some familiar stuff here like index.html is still going to
1269:13 here like index.html is still going to be with us but it's going to be up to us
1269:15 be with us but it's going to be up to us when and how to show it to the user so
1269:17 when and how to show it to the user so let's make this more real let me go over
1269:19 let's make this more real let me go over to vs code here and let me go ahead and
1269:23 to vs code here and let me go ahead and create a how about we'll do this in
1269:26 create a how about we'll do this in hello let me do makeer hello to make a
1269:28 hello let me do makeer hello to make a new folder called hello and I'm going to
1269:30 new folder called hello and I'm going to CD into it just to isolate all of these
1269:32 CD into it just to isolate all of these files to the same directory so that we
1269:33 files to the same directory so that we have different apps today and different
1269:35 have different apps today and different folders and now I'm going to do code of
1269:38 folders and now I'm going to do code of let's do this actually let's do our
1269:40 let's do this actually let's do our maker templates. HTM ah sorry not
1269:43 maker templates. HTM ah sorry not templates. HTML let me rename that to
1269:45 templates. HTML let me rename that to templates using the MV command this has
1269:48 templates using the MV command this has nothing to do with web programming this
1269:49 nothing to do with web programming this is me making typos so if I type LS now
1269:51 is me making typos so if I type LS now I've got a folder called templates all
1269:54 I've got a folder called templates all right in there let's create a file
1269:56 right in there let's create a file called index.html that is going to be
1269:59 called index.html that is going to be super simple and pretty much copy paste
1270:01 super simple and pretty much copy paste from last week let me hide my terminal
1270:02 from last week let me hide my terminal window and let me just very quickly whip
1270:05 window and let me just very quickly whip up a simp simple hello world page using
1270:07 up a simp simple hello world page using my HTML tag Lang will equal English then
1270:10 my HTML tag Lang will equal English then inside of this I'm going to have a head
1270:13 inside of this I'm going to have a head tag inside of this I'm going to have a
1270:15 tag inside of this I'm going to have a title tag and I'm just going to call
1270:16 title tag and I'm just going to call this thing hello uh I'm going to then
1270:18 this thing hello uh I'm going to then have a body and in this I'm only going
1270:20 have a body and in this I'm only going to say something simple like hello comma
1270:22 to say something simple like hello comma world and just so this is mobile
1270:24 world and just so this is mobile friendly recall that we touched on these
1270:26 friendly recall that we touched on these meta tags so just in case you after
1270:28 meta tags so just in case you after class play with your mobile device
1270:29 class play with your mobile device instead of your laptop I'll do name
1270:31 instead of your laptop I'll do name equals quote unquote
1270:33 equals quote unquote viewport uh viewport and and content
1270:36 viewport uh viewport and and content equals and I never remember this I'm
1270:38 equals and I never remember this I'm literally reading it off of a cheat
1270:39 literally reading it off of a cheat sheet initial scale equals 1 width
1270:43 sheet initial scale equals 1 width equals device width and this is just
1270:45 equals device width and this is just this magical incantation that says to
1270:47 this magical incantation that says to the browser like size things
1270:49 the browser like size things appropriately for the size of the device
1270:51 appropriately for the size of the device it blows up the font sizes a bit all
1270:54 it blows up the font sizes a bit all right so that's what I would have done
1270:55 right so that's what I would have done last week and I would have served this
1270:57 last week and I would have served this web page by running HTTP server in the
1270:59 web page by running HTTP server in the same directory and boom I would see that
1271:00 same directory and boom I would see that HTML but let's now start to take some
1271:02 HTML but let's now start to take some control over the user's experience and
1271:05 control over the user's experience and for now it's going to be underwhelming
1271:06 for now it's going to be underwhelming it's just going to always say hello
1271:08 it's just going to always say hello world but in a moment version two is
1271:10 world but in a moment version two is going to say hello David or hello Carter
1271:12 going to say hello David or hello Carter a bit more dynamically and we'll quickly
1271:14 a bit more dynamically and we'll quickly escalate from there to just more
1271:16 escalate from there to just more interesting applications as well
1271:18 interesting applications as well culminating with things like cookies and
1271:19 culminating with things like cookies and shopping carts and the like so let me go
1271:22 shopping carts and the like so let me go back into my terminal window and as
1271:24 back into my terminal window and as promised let me create another file
1271:25 promised let me create another file called
1271:27 called app.py and this is where now I need to
1271:29 app.py and this is where now I need to implement the web server I'm going to
1271:31 implement the web server I'm going to run using this flask framework and for
1271:34 run using this flask framework and for now I'm just going to kind of do some
1271:35 now I'm just going to kind of do some copy paste from uh what we saw on the
1271:37 copy paste from uh what we saw on the slide a moment ago from the flask
1271:40 slide a moment ago from the flask Library which we've pre-installed for
1271:41 Library which we've pre-installed for you I'm going to import a uh function
1271:45 you I'm going to import a uh function called flask capital F it's subtle but
1271:48 called flask capital F it's subtle but it's important there and I'm also going
1271:50 it's important there and I'm also going to import a few other things a function
1271:52 to import a few other things a function called render template and another
1271:55 called render template and another variable called request and the only way
1271:57 variable called request and the only way I know this is from having taught this
1271:59 I know this is from having taught this before read the documentation followed a
1272:01 before read the documentation followed a tutorial like you wouldn't know this
1272:03 tutorial like you wouldn't know this unless someone told you or you read how
1272:04 unless someone told you or you read how to do this this but what this means is
1272:06 to do this this but what this means is that this Library called flask has three
1272:09 that this Library called flask has three things in it a function called flask
1272:11 things in it a function called flask capital f a function called render
1272:12 capital f a function called render template and a variable built into it
1272:15 template and a variable built into it called request and this is going to be
1272:16 called request and this is going to be all the building blocks I need to
1272:18 all the building blocks I need to implement my own web server the
1272:20 implement my own web server the convention in flask when you want to
1272:21 convention in flask when you want to create a web app in Python is you create
1272:24 create a web app in Python is you create a variable by convention called app and
1272:26 a variable by convention called app and then you assign it the return value of
1272:28 then you assign it the return value of that flask function capital F and pass
1272:31 that flask function capital F and pass into it underscore uncore name uncore
1272:34 into it underscore uncore name uncore underscore which is weird but we have
1272:36 underscore which is weird but we have seen this before a few weeks ago anyone
1272:39 seen this before a few weeks ago anyone recall when and why we mentioned uncore
1272:41 recall when and why we mentioned uncore uncore name uncore
1272:43 uncore name uncore uncore yeah I think was
1272:51 it name or something yeah if we wanted to check if the name of the file was
1272:54 to check if the name of the file was itself main so that we avoided a
1272:58 itself main so that we avoided a situation where if you're writing your
1272:59 situation where if you're writing your own Library code you don't want your
1273:01 own Library code you don't want your code to be executed automatically you
1273:03 code to be executed automatically you want to potentially execute the main
1273:05 want to potentially execute the main function and that was a solution to that
1273:07 function and that was a solution to that problem here for today's purposes this
1273:09 problem here for today's purposes this is just the way you do it underscore
1273:10 is just the way you do it underscore underscore name underscore uncore refers
1273:12 underscore name underscore uncore refers to the current file and so this is just
1273:14 to the current file and so this is just a little trick that says turn this file
1273:16 a little trick that says turn this file into a flask application that's all it
1273:18 into a flask application that's all it is and for now uh that line suffices all
1273:22 is and for now uh that line suffices all right what do I want to do after that
1273:23 right what do I want to do after that well now I'm in charge of the web server
1273:25 well now I'm in charge of the web server I need to write the code that decides
1273:28 I need to write the code that decides based on the browsers request what file
1273:31 based on the browsers request what file or files I'm going to send from the
1273:33 or files I'm going to send from the server to the browser last week http
1273:35 server to the browser last week http server did all of this for us just based
1273:36 server did all of this for us just based on the file name but today I'm going to
1273:38 on the file name but today I'm going to take over control over that process and
1273:40 take over control over that process and the way I do that is as follows I say
1273:42 the way I do that is as follows I say app. route with weirdly an at sign in
1273:46 app. route with weirdly an at sign in front of it this is known in python as a
1273:48 front of it this is known in python as a decorator and it's a feature of python
1273:50 decorator and it's a feature of python not a flask that we just didn't
1273:52 not a flask that we just didn't introduce in weeks past but it's a
1273:54 introduce in weeks past but it's a special it's a handy trick to do what
1273:56 special it's a handy trick to do what we're about to do the route I want to
1273:58 we're about to do the route I want to Define is quote unquote slash so that is
1274:01 Define is quote unquote slash so that is here is code I want the server to
1274:04 here is code I want the server to execute whenever I user visits forward
1274:06 execute whenever I user visits forward slash the default page of the website
1274:09 slash the default page of the website well what code do I want them to execute
1274:10 well what code do I want them to execute well I want them to execute a function
1274:12 well I want them to execute a function and I can therefore Define in Python a
1274:14 and I can therefore Define in Python a function I can technically call this
1274:16 function I can technically call this thing anything I want X or Y or Z but
1274:18 thing anything I want X or Y or Z but because they're accessing the default
1274:20 because they're accessing the default page otherwise known as the index of the
1274:22 page otherwise known as the index of the site I'm going to just more reasonably
1274:24 site I'm going to just more reasonably call this function index but just a
1274:26 call this function index but just a convention you could call it anything
1274:27 convention you could call it anything you want but X Y or Z is probably a bad
1274:29 you want but X Y or Z is probably a bad stylistic choice it doesn't need to take
1274:32 stylistic choice it doesn't need to take any arguments in this case and the only
1274:34 any arguments in this case and the only thing this code this function is going
1274:35 thing this code this function is going to do is for now let's go ahead and have
1274:40 to do is for now let's go ahead and have it return hello world quote unquote and
1274:44 it return hello world quote unquote and that's it all right now let me go into
1274:47 that's it all right now let me go into my terminal window let me go ahead and
1274:50 my terminal window let me go ahead and do flask run in the same directory that
1274:52 do flask run in the same directory that has appy and hit enter I'm going to see
1274:54 has appy and hit enter I'm going to see some cryptic output but including a URL
1274:56 some cryptic output but including a URL of my code space and if I open that URL
1274:59 of my code space and if I open that URL after hovering over it I'll indeed see
1275:01 after hovering over it I'll indeed see hello world as you might hope but let me
1275:03 hello world as you might hope but let me do this let me go ahead and rightclick
1275:06 do this let me go ahead and rightclick on the page and click view page source
1275:08 on the page and click view page source which if you haven't done before shows
1275:10 which if you haven't done before shows you all of the HTML for a page however
1275:12 you all of the HTML for a page however pretty or messy it is and that's it
1275:14 pretty or messy it is and that's it there's no HTML that I've spit out it's
1275:16 there's no HTML that I've spit out it's just quote unquote hello world well if I
1275:19 just quote unquote hello world well if I actually want to spit out a full web
1275:21 actually want to spit out a full web page which is not a big deal here
1275:23 page which is not a big deal here because who cares it's just the text
1275:24 because who cares it's just the text anyway but if I want to spit out a whole
1275:26 anyway but if I want to spit out a whole file let me do this I want to return
1275:30 file let me do this I want to return essentially the contents of index.html
1275:33 essentially the contents of index.html which have all of the tags I want the
1275:34 which have all of the tags I want the mobile friend stuff and all of that well
1275:36 mobile friend stuff and all of that well I can't just return index.html but I can
1275:39 I can't just return index.html but I can return this render template quote
1275:42 return this render template quote unquote
1275:44 unquote index.html and per the documentation for
1275:46 index.html and per the documentation for flask this render template function will
1275:48 flask this render template function will go find that file for me in my templates
1275:50 go find that file for me in my templates folder by convention it will open it up
1275:52 folder by convention it will open it up and then it will spit the whole thing
1275:54 and then it will spit the whole thing out to the browser for me so I can keep
1275:56 out to the browser for me so I can keep all my HTML in one place and all my
1275:58 all my HTML in one place and all my python code in this one place so now if
1276:01 python code in this one place so now if I go back to my browser and reload I
1276:04 I go back to my browser and reload I don't think I'll really see a difference
1276:06 don't think I'll really see a difference because it's the same text ultimately
1276:08 because it's the same text ultimately but if I view page Source now notice
1276:11 but if I view page Source now notice that ah there is all of the HTML that
1276:14 that ah there is all of the HTML that was just sent to the browser so this is
1276:16 was just sent to the browser so this is only to say we have the building blocks
1276:18 only to say we have the building blocks the puzzle pieces if you will via which
1276:20 the puzzle pieces if you will via which to now store all of our HTML in one
1276:22 to now store all of our HTML in one place and presumably CSS JavaScript and
1276:24 place and presumably CSS JavaScript and so forth but then serve up whatever we
1276:27 so forth but then serve up whatever we want even though I'm just blindly
1276:28 want even though I'm just blindly spitting out
1276:30 spitting out index.html so before we proceed any
1276:33 index.html so before we proceed any questions on this which again I claim is
1276:35 questions on this which again I claim is like my manual version of what HTTP
1276:38 like my manual version of what HTTP server was doing for us automatically
1276:40 server was doing for us automatically last week but this is how you do it
1276:42 last week but this is how you do it yourself any
1276:44 yourself any questions all right well let's make it
1276:46 questions all right well let's make it more interesting which we could not do
1276:48 more interesting which we could not do with HTTP server and HTML alone why
1276:51 with HTTP server and HTML alone why don't we go ahead and do this let me
1276:55 don't we go ahead and do this let me visit the same URL and I'm going to zoom
1276:56 visit the same URL and I'm going to zoom in and your url will differ from my code
1276:59 in and your url will differ from my code space but it's going to end similarly
1277:01 space but it's going to end similarly here I'm going to do slash question mark
1277:03 here I'm going to do slash question mark name equals David for instance or Q
1277:07 name equals David for instance or Q equals cats or name equals Carter any
1277:09 equals cats or name equals Carter any key value pair I want I'm going to
1277:11 key value pair I want I'm going to append after a slash and a question mark
1277:13 append after a slash and a question mark thereby providing user input to the
1277:15 thereby providing user input to the server albe it in a very user unfriendly
1277:17 server albe it in a very user unfriendly way no one's going to normally do this
1277:18 way no one's going to normally do this in their browser enter nothing changes
1277:21 in their browser enter nothing changes here it just says hello world but
1277:23 here it just says hello world but wouldn't it be nice if it says hello
1277:24 wouldn't it be nice if it says hello David or equivalently if I zoom in here
1277:26 David or equivalently if I zoom in here again and change David to Carter and hit
1277:28 again and change David to Carter and hit enter wouldn't it be nice if it says
1277:30 enter wouldn't it be nice if it says hello Carter instead so we need some
1277:33 hello Carter instead so we need some dynamism there and here's now python is
1277:35 dynamism there and here's now python is going to be our friend if I want to
1277:39 going to be our friend if I want to access the HTTP parameters that the user
1277:43 access the HTTP parameters that the user has provided via the URL be it Q equals
1277:45 has provided via the URL be it Q equals cats or name equals David I can use this
1277:47 cats or name equals David I can use this special variable I already preemptively
1277:50 special variable I already preemptively imported earlier and I can do this if
1277:52 imported earlier and I can do this if there is an HTTP parameter called name
1277:55 there is an HTTP parameter called name in what I'm going to call request. args
1277:59 in what I'm going to call request. args then I'm going to go ahead and create a
1278:00 then I'm going to go ahead and create a variable called name and I'm going to
1278:02 variable called name and I'm going to set it equal to request. args bracket
1278:05 set it equal to request. args bracket name else if there is no quote unquote
1278:08 name else if there is no quote unquote name key in this special variable called
1278:10 name key in this special variable called request. args I'm going to just assume
1278:12 request. args I'm going to just assume that the user's name is World by default
1278:15 that the user's name is World by default now what's going on here well it turns
1278:17 now what's going on here well it turns out that flask provides us with this
1278:18 out that flask provides us with this special variable called request. ARs and
1278:21 special variable called request. ARs and in there is all of the key value pairs
1278:24 in there is all of the key value pairs that might have come in via the URL so
1278:27 that might have come in via the URL so if you had to guess what type of data or
1278:31 if you had to guess what type of data or what data type is request. args that's
1278:34 what data type is request. args that's its name and here is in context line n
1278:36 its name and here is in context line n might provide a clue in Python what data
1278:40 might provide a clue in Python what data type might request. args
1278:42 type might request. args be
1278:44 be yeah uh it's not going to be an array or
1278:47 yeah uh it's not going to be an array or a list because those are always in every
1278:49 a list because those are always in every language we've seen numerically indexed
1278:50 language we've seen numerically indexed but you're
1278:52 but you're close someone else it's a dictionary so
1278:56 close someone else it's a dictionary so a dictionary is similar syntactically to
1278:58 a dictionary is similar syntactically to a list in Python but instead of numeric
1279:00 a list in Python but instead of numeric indices like 012 you can literally use
1279:03 indices like 012 you can literally use strings like quote unquote name now
1279:05 strings like quote unquote name now that's a bit of a white lie it is a
1279:06 that's a bit of a white lie it is a dictionary but it's flask's special
1279:08 dictionary but it's flask's special fancy version of a dictionary but the
1279:10 fancy version of a dictionary but the syntax via which you can access it is
1279:12 syntax via which you can access it is exactly the same and I actually this is
1279:14 exactly the same and I actually this is a typo I didn't mean to say names there
1279:16 a typo I didn't mean to say names there I meant to say name singular but
1279:18 I meant to say name singular but otherwise I think the code is correct
1279:21 otherwise I think the code is correct this is going to on line eight check if
1279:23 this is going to on line eight check if there's a key called name in request.
1279:25 there's a key called name in request. ARs and if so it's going to set it equal
1279:27 ARs and if so it's going to set it equal to that value otherwise it's going to
1279:29 to that value otherwise it's going to default to world I deliberately did not
1279:32 default to world I deliberately did not do this I added this IFL
1279:35 do this I added this IFL and did not do this why what error might
1279:39 and did not do this why what error might happen if I just blindly grab
1279:47 name exactly if there was nothing at the end of the URL that was of the form
1279:49 end of the URL that was of the form question mark name equals someone then
1279:51 question mark name equals someone then there would be no name key and this is
1279:54 there would be no name key and this is uh you know a couple weeks back but this
1279:55 uh you know a couple weeks back but this would give you one of those annoying key
1279:57 would give you one of those annoying key errors when you get a trace back because
1279:58 errors when you get a trace back because you screwed up because you used a string
1280:00 you screwed up because you used a string that doesn't exist that's why I'm just
1280:02 that doesn't exist that's why I'm just proactively trying to avoid that
1280:03 proactively trying to avoid that situation just like I'm might have a
1280:05 situation just like I'm might have a couple of weeks ago so even though it's
1280:06 couple of weeks ago so even though it's more verbose this is just much more
1280:09 more verbose this is just much more defensive so that I don't accidentally
1280:11 defensive so that I don't accidentally index into a dictionary where there is
1280:13 index into a dictionary where there is no key but we'll see how we can tighten
1280:15 no key but we'll see how we can tighten this up to be not four lines but one but
1280:17 this up to be not four lines but one but I think now I can do this wouldn't it be
1280:19 I think now I can do this wouldn't it be nice if now in my index.html file which
1280:23 nice if now in my index.html file which recall is in my templates folder
1280:25 recall is in my templates folder wouldn't it be nice if I could do the
1280:27 wouldn't it be nice if I could do the equivalent in C of like a percent s here
1280:30 equivalent in C of like a percent s here for instance or in Python something like
1280:33 for instance or in Python something like this name well it's close and this is
1280:35 this name well it's close and this is just because different humans invent
1280:37 just because different humans invent different languages invent different uh
1280:39 different languages invent different uh Frameworks the Syntax for this in flask
1280:43 Frameworks the Syntax for this in flask is to actually do whoops two curly
1280:46 is to actually do whoops two curly braces and then name of the variable
1280:49 braces and then name of the variable inside of it why it's just probably
1280:52 inside of it why it's just probably someone figured what are the odds that a
1280:54 someone figured what are the odds that a normal person is ever going to use two
1280:55 normal person is ever going to use two curly braces at once versus just one so
1280:58 curly braces at once versus just one so this is probably decreasing the
1280:59 this is probably decreasing the probability that people actually want to
1281:00 probability that people actually want to Output literal curly braces like this so
1281:03 Output literal curly braces like this so it's similar in spirit to Python's F
1281:05 it's similar in spirit to Python's F strings it's similar in spirit to C's
1281:07 strings it's similar in spirit to C's percent s it's similar in spirit to
1281:08 percent s it's similar in spirit to sql's question marks same idea slightly
1281:11 sql's question marks same idea slightly different syntax and this there is
1281:13 different syntax and this there is ginger so it's not programming code per
1281:15 ginger so it's not programming code per se it's just a template and indeed
1281:18 se it's just a template and indeed that's why this folder is called
1281:19 that's why this folder is called templates it is sort of like a a
1281:21 templates it is sort of like a a blueprint for what I want to be spit out
1281:23 blueprint for what I want to be spit out to the user but I've got these
1281:24 to the user but I've got these placeholders like this variable that I
1281:26 placeholders like this variable that I want to plug into that value now this
1281:29 want to plug into that value now this alone is not enough watch what happens
1281:32 alone is not enough watch what happens if I go back to my other browser and I
1281:35 if I go back to my other browser and I reload the page after changing up here
1281:37 reload the page after changing up here let's do name equals David again enter
1281:40 let's do name equals David again enter nothing outputs after the hello comma so
1281:44 nothing outputs after the hello comma so it seems that the name variable doesn't
1281:46 it seems that the name variable doesn't exist yet and that's why indeed if I do
1281:49 exist yet and that's why indeed if I do view page Source you can see what was
1281:50 view page Source you can see what was sent to the browser something's wrong
1281:52 sent to the browser something's wrong with my placeholder but I just need to
1281:54 with my placeholder but I just need to be a little more explicit as to what I
1281:56 be a little more explicit as to what I want to send where so it turns out that
1281:59 want to send where so it turns out that the render template function takes not
1282:01 the render template function takes not just one argument the name of the
1282:03 just one argument the name of the template you want to spit out but it
1282:04 template you want to spit out but it takes after that with commas all of the
1282:07 takes after that with commas all of the placeholders you want to plug in so for
1282:10 placeholders you want to plug in so for instance if you want the placeholder to
1282:13 instance if you want the placeholder to be this literally placeholder inside of
1282:15 be this literally placeholder inside of those curly braces you can then specify
1282:18 those curly braces you can then specify as the second argument to rep uh render
1282:20 as the second argument to rep uh render template a placeholder named argument
1282:23 template a placeholder named argument equals whatever the name is so name is
1282:27 equals whatever the name is so name is the variable in the lines above
1282:29 the variable in the lines above placeholder is the name of my literal
1282:31 placeholder is the name of my literal placeholder in the curly braces and so
1282:33 placeholder in the curly braces and so now if I go back to my browser and
1282:36 now if I go back to my browser and reload this with still quote unquote
1282:38 reload this with still quote unquote with still question mark name equals
1282:39 with still question mark name equals David in the URL now I indeed see hello
1282:43 David in the URL now I indeed see hello comma David and if I zoom in here and
1282:46 comma David and if I zoom in here and let me move over here let me type in
1282:47 let me move over here let me type in Carter and hit enter now I see Hello
1282:50 Carter and hit enter now I see Hello Carter instead now this is a little
1282:53 Carter instead now this is a little unnecessary to explicitly call the
1282:54 unnecessary to explicitly call the placeholder placeholder especially if
1282:56 placeholder placeholder especially if you want to have two or three of them so
1282:58 you want to have two or three of them so you can actually call this anything you
1282:59 you can actually call this anything you want and I'm going to change it back to
1283:01 want and I'm going to change it back to name which is a little more
1283:02 name which is a little more straightforward the only weird thing
1283:04 straightforward the only weird thing here is that now you'll see that you're
1283:06 here is that now you'll see that you're writing code like this and this is
1283:08 writing code like this and this is correct and this is the norm it just
1283:10 correct and this is the norm it just looks weird but the thing on the left of
1283:12 looks weird but the thing on the left of the equal sign is the placeholder you're
1283:15 the equal sign is the placeholder you're using in the template the thing on the
1283:17 using in the template the thing on the right can be any value you want
1283:19 right can be any value you want including a variable so even though I'm
1283:21 including a variable so even though I'm naming them exactly the same which looks
1283:23 naming them exactly the same which looks stupid admittedly like this is what
1283:24 stupid admittedly like this is what people tend to do just because it's uh
1283:27 people tend to do just because it's uh simpler than introducing another word
1283:29 simpler than introducing another word like placeholder any questions now on
1283:33 like placeholder any questions now on this
1283:34 this any questions on these
1283:36 any questions on these placeholders no all right well let's
1283:38 placeholders no all right well let's tighten this up a little bit and see if
1283:40 tighten this up a little bit and see if we can't get things to be more Dynamic
1283:43 we can't get things to be more Dynamic still let me propose now that instead of
1283:46 still let me propose now that instead of outputting instead of using this
1283:48 outputting instead of using this condition which made a very simple idea
1283:50 condition which made a very simple idea like very verbose with four different
1283:52 like very verbose with four different lines it turns out there's an easier way
1283:54 lines it turns out there's an easier way to do this you can actually still create
1283:56 to do this you can actually still create a variable called name and you can set
1283:58 a variable called name and you can set it equal to request. args but instead of
1284:02 it equal to request. args but instead of just blindly indexing into to that
1284:04 just blindly indexing into to that dictionary it turns out that request.
1284:08 dictionary it turns out that request. ARS comes with a function as well called
1284:11 ARS comes with a function as well called get you can pass it an argument that
1284:13 get you can pass it an argument that tells you what value you want to get and
1284:17 tells you what value you want to get and by default if there is no key called
1284:20 by default if there is no key called name in that dictionary this function
1284:22 name in that dictionary this function will not throw a key error it's just
1284:24 will not throw a key error it's just going to return none an O the special V
1284:27 going to return none an O the special V the Special Value in Python so it avoids
1284:29 the Special Value in Python so it avoids a bug in your code but it tightens up
1284:31 a bug in your code but it tightens up four lines into one but even nicer if
1284:33 four lines into one but even nicer if you read the documentation
1284:34 you read the documentation the get function can also take a
1284:36 the get function can also take a explicit default value so if you don't
1284:38 explicit default value so if you don't want none to be on the screen like hello
1284:40 want none to be on the screen like hello comma blank or I mean that would be
1284:42 comma blank or I mean that would be weird too you can just put in a default
1284:44 weird too you can just put in a default value per the documentation of this
1284:46 value per the documentation of this function like world so now we've gone
1284:49 function like world so now we've gone from four lines to just one so arguably
1284:51 from four lines to just one so arguably it's better designed and if I go back to
1284:53 it's better designed and if I go back to the browser now still with Carter in the
1284:55 the browser now still with Carter in the URL and hit reload same thing happens
1284:58 URL and hit reload same thing happens but we notice this suppose I uh get rid
1285:02 but we notice this suppose I uh get rid of the name parameter altogether and hit
1285:04 of the name parameter altogether and hit enter now it goes to the default instead
1285:07 enter now it goes to the default instead world so it's just a little better a
1285:08 world so it's just a little better a little better designed than doing it the
1285:10 little better designed than doing it the other way instead all right how about we
1285:13 other way instead all right how about we take things up one more Notch and how
1285:15 take things up one more Notch and how about we introduce multiple routes and
1285:18 about we introduce multiple routes and actually introduce perhaps a form to the
1285:20 actually introduce perhaps a form to the mix because again no normal person is
1285:23 mix because again no normal person is going to like visit a URL and add a
1285:25 going to like visit a URL and add a slash and a question mark and their name
1285:27 slash and a question mark and their name like that's not how browsers work uh
1285:29 like that's not how browsers work uh well that's how browsers work that's not
1285:30 well that's how browsers work that's not how humans interact with browsers you
1285:32 how humans interact with browsers you and I use a form to quickly instead so
1285:35 and I use a form to quickly instead so now things can get a little more
1285:36 now things can get a little more interesting when making our own web
1285:38 interesting when making our own web application cuz maybe we could do
1285:40 application cuz maybe we could do something like this let me go and zoom
1285:43 something like this let me go and zoom out again let me go back to my code here
1285:46 out again let me go back to my code here and let me move this around and focus
1285:48 and let me move this around and focus now on the index.html file instead of
1285:51 now on the index.html file instead of just this placeholder why don't we go
1285:53 just this placeholder why don't we go ahead and give ourselves a form like
1285:55 ahead and give ourselves a form like we've played with a little bit in the
1285:57 we've played with a little bit in the past be it for Google or something else
1285:59 past be it for Google or something else and let's do this uh form and inside of
1286:03 and let's do this uh form and inside of this form let's have an input and the
1286:05 this form let's have an input and the name of this input will be quote unquote
1286:07 name of this input will be quote unquote name so that too is confusing but inputs
1286:10 name so that too is confusing but inputs have name attributes but this is a
1286:12 have name attributes but this is a person's name so I'm saying name equals
1286:14 person's name so I'm saying name equals name here so just a messy world of
1286:17 name here so just a messy world of semantics and let me go ahead and make
1286:19 semantics and let me go ahead and make this a text box by default and then let
1286:22 this a text box by default and then let me give myself a button whose default
1286:24 me give myself a button whose default type will be submit and the name of this
1286:27 type will be submit and the name of this button will be greet for instance so
1286:29 button will be greet for instance so let's see what happens here but let me
1286:31 let's see what happens here but let me change app.py to just be the original
1286:33 change app.py to just be the original simpler I'm not passing in any
1286:34 simpler I'm not passing in any placeholders now and I'm going to even
1286:36 placeholders now and I'm going to even get rid of this I'm just going to rewind
1286:37 get rid of this I'm just going to rewind to the first version of this for
1286:39 to the first version of this for Simplicity let's now change the url to
1286:42 Simplicity let's now change the url to get rid of Carter and myself so we just
1286:44 get rid of Carter and myself so we just go to slash and hit enter and now we
1286:46 go to slash and hit enter and now we have a super simple form again all right
1286:49 have a super simple form again all right this is not super userfriendly but
1286:50 this is not super userfriendly but there's some nice enhancements we can
1286:52 there's some nice enhancements we can make for instance like we can uh for
1286:54 make for instance like we can uh for instance turn off autocomplete
1286:56 instance turn off autocomplete especially if I want to type David and
1286:58 especially if I want to type David and Carter manually and I don't want it
1286:59 Carter manually and I don't want it finishing my thought during class uh we
1287:01 finishing my thought during class uh we can do autofocus which puts the cursor
1287:03 can do autofocus which puts the cursor there BL linking by default which is
1287:05 there BL linking by default which is nice cuz then the human doesn't have to
1287:06 nice cuz then the human doesn't have to deal with that um and then we can even
1287:08 deal with that um and then we can even have a placeholder attribute placeholder
1287:10 have a placeholder attribute placeholder equals name so that it's like built in
1287:12 equals name so that it's like built in instructions for this thing and so now
1287:14 instructions for this thing and so now if I go back to the other tab nothing's
1287:16 if I go back to the other tab nothing's changed yet because I have to download
1287:18 changed yet because I have to download the HTML again reload okay now it's a
1287:20 the HTML again reload okay now it's a little more user friendly it says name
1287:22 little more user friendly it says name and light gray the cursor is blinking
1287:24 and light gray the cursor is blinking and I'm sort of ready to go but this
1287:26 and I'm sort of ready to go but this form hasn't been wired up to go anywhere
1287:29 form hasn't been wired up to go anywhere yet and so let's do this let's for
1287:31 yet and so let's do this let's for instance say that the action of this
1287:33 instance say that the action of this form
1287:34 form is not going to be something like
1287:35 is not going to be something like google.com which we did last time with
1287:37 google.com which we did last time with for cats I am now going to be both the
1287:40 for cats I am now going to be both the front end and the back end of this
1287:42 front end and the back end of this website the front end is what the human
1287:44 website the front end is what the human sees the web page the graphics the forms
1287:46 sees the web page the graphics the forms the back end is the stuff the human
1287:48 the back end is the stuff the human typically doesn't see the python code
1287:49 typically doesn't see the python code the SQL code the server itself but now
1287:53 the SQL code the server itself but now I'm in control of both sides of the
1287:55 I'm in control of both sides of the experience the HTML and also the routes
1287:58 experience the HTML and also the routes so let's just propose that we invent our
1288:00 so let's just propose that we invent our own route and instead of calling it SL
1288:02 own route and instead of calling it SL search like Google does let's call it
1288:04 search like Google does let's call it SLG greet and let me specify that the
1288:07 SLG greet and let me specify that the method this form will use which is
1288:09 method this form will use which is technically the default will be get and
1288:11 technically the default will be get and confusingly it is lowercase get even
1288:13 confusingly it is lowercase get even though in the envelope we keep talking
1288:15 though in the envelope we keep talking about virtually it's actually capitals
1288:17 about virtually it's actually capitals again left hand wasn't talking to right
1288:18 again left hand wasn't talking to right hand when these things were decided all
1288:21 hand when these things were decided all right so all I've done is create a web
1288:23 right so all I've done is create a web form that's going to submit whatever the
1288:26 form that's going to submit whatever the text box value is to a route called SLG
1288:29 text box value is to a route called SLG greet by default because there's no HTTP
1288:31 greet by default because there's no HTTP or htps or no domain name SLG greet is
1288:35 or htps or no domain name SLG greet is going to be assumed to be not at
1288:36 going to be assumed to be not at google.com but whatever my own serers
1288:38 google.com but whatever my own serers URL is so whatever my code spaces URL is
1288:42 URL is so whatever my code spaces URL is that's going to be the implicit prefix
1288:44 that's going to be the implicit prefix this SLG greet is just the route so now
1288:47 this SLG greet is just the route so now let's go back to VSS codes app.py file
1288:51 let's go back to VSS codes app.py file how do I now Stitch this together well I
1288:54 how do I now Stitch this together well I think we're good to go with index.html
1288:56 think we're good to go with index.html if index. html's purpose in life is just
1288:58 if index. html's purpose in life is just to spit out this form we're done with
1289:00 to spit out this form we're done with one of my routes but if I want to have a
1289:03 one of my routes but if I want to have a second route greet that actually spits
1289:06 second route greet that actually spits out some greeting to the user well let's
1289:09 out some greeting to the user well let's prepare that template too let me go
1289:11 prepare that template too let me go ahead and highlight all of this HTML let
1289:14 ahead and highlight all of this HTML let me go back into my terminal window and
1289:16 me go back into my terminal window and into my hello directory and then into my
1289:20 into my hello directory and then into my templates directory and let me create
1289:22 templates directory and let me create another template called greet HTML whose
1289:25 another template called greet HTML whose purpose in life will not be to show a
1289:27 purpose in life will not be to show a form but to greet the user with hello so
1289:29 form but to greet the user with hello so and so so in this file I'm going to
1289:32 and so so in this file I'm going to paste all that same HTML but I'm going
1289:34 paste all that same HTML but I'm going to get rid of the form and essentially
1289:36 to get rid of the form and essentially revert to our previous version hello
1289:38 revert to our previous version hello comma and then using the ginger syntax
1289:41 comma and then using the ginger syntax name so one template index.html is for
1289:44 name so one template index.html is for the form the second template now is for
1289:46 the form the second template now is for the greeting of hello comma so and so
1289:49 the greeting of hello comma so and so but otherwise these files notice are
1289:51 but otherwise these files notice are almost the same except one has the form
1289:54 almost the same except one has the form one has just the hello so now let's
1289:56 one has just the hello so now let's finish this up in app doop High let me
1289:58 finish this up in app doop High let me go down here after a couple of blank
1290:00 go down here after a couple of blank lines stylistically let me do app. route
1290:03 lines stylistically let me do app. route quote unquote SLG greet but I could call
1290:06 quote unquote SLG greet but I could call this route anything I want I'm just
1290:07 this route anything I want I'm just using a a reasonable verb then let's
1290:10 using a a reasonable verb then let's define another function I could call the
1290:11 define another function I could call the function anything I want X Y or Z I'm
1290:13 function anything I want X Y or Z I'm going to call it more reasonably greet
1290:16 going to call it more reasonably greet no arguments and then now is the code
1290:18 no arguments and then now is the code where I want to render the template so I
1290:20 where I want to render the template so I do return render template greet HTML but
1290:25 do return render template greet HTML but but I need to do one more
1290:27 but I need to do one more thing what else do I want to do if I
1290:29 thing what else do I want to do if I want greet HTML to have access to the
1290:31 want greet HTML to have access to the human's name just to
1290:35 human's name just to recap I think we solved this already but
1290:37 recap I think we solved this already but I deleted it but what do I have to add
1290:41 I deleted it but what do I have to add back
1290:42 back yeahh yeah so I got to pass in the
1290:45 yeahh yeah so I got to pass in the placeholder somehow so I can do this a
1290:47 placeholder somehow so I can do this a couple of different ways I I'm going to
1290:48 couple of different ways I I'm going to keep it a little more elegant this time
1290:50 keep it a little more elegant this time I'm just going to put my name uh
1290:53 I'm just going to put my name uh argument there and I'm going to set it
1290:55 argument there and I'm going to set it equal to request. ar. get quote unquote
1290:58 equal to request. ar. get quote unquote name comma world before I used a
1291:01 name comma world before I used a separate variable but I only used it in
1291:02 separate variable but I only used it in one place so that's not strictly
1291:04 one place so that's not strictly necessary so this is fine too but if
1291:06 necessary so this is fine too but if this gets a little overwhelming notice
1291:08 this gets a little overwhelming notice that I can alternatively do this I can
1291:10 that I can alternatively do this I can create an actual variable called name
1291:13 create an actual variable called name and then I can pass in an argument
1291:16 and then I can pass in an argument called name with a value that is that
1291:18 called name with a value that is that variable but again what's really the
1291:21 variable but again what's really the point here it was kind of prettier all
1291:22 point here it was kind of prettier all on one line so these are the exact same
1291:24 on one line so these are the exact same things I'm just trying to tighten things
1291:25 things I'm just trying to tighten things up further here all right so what just
1291:28 up further here all right so what just happened if I go back to my form this is
1291:30 happened if I go back to my form this is still index.html if I reload it nothing
1291:33 still index.html if I reload it nothing has changed if I type in my name to this
1291:35 has changed if I type in my name to this form notice again the URL I'm currently
1291:38 form notice again the URL I'm currently at this is Chrome hiding things it's
1291:40 at this is Chrome hiding things it's technically slash by default even though
1291:42 technically slash by default even though many browsers are just hiding
1291:44 many browsers are just hiding unnecessary uh characters these days but
1291:47 unnecessary uh characters these days but Watch What Happens now if I scroll over
1291:49 Watch What Happens now if I scroll over here and I click greet on this new
1291:52 here and I click greet on this new form notice my URL my route changed to
1291:57 form notice my URL my route changed to SL greet question mark name equals David
1292:00 SL greet question mark name equals David and the body of the page at top left
1292:02 and the body of the page at top left says hello comma David so this is
1292:04 says hello comma David so this is exactly how google.com works and it's
1292:06 exactly how google.com works and it's how we implemented search. HTML last
1292:07 how we implemented search. HTML last time but instead of submitting the form
1292:10 time but instead of submitting the form to Google via the form I'm submitting it
1292:13 to Google via the form I'm submitting it to myself my very own route so I'm
1292:16 to myself my very own route so I'm implementing my own backend for this
1292:18 implementing my own backend for this same front end all right any questions
1292:21 same front end all right any questions just yet much less interesting than
1292:24 just yet much less interesting than Google certainly but we kind of have all
1292:26 Google certainly but we kind of have all of the wiring
1292:29 of the wiring now any
1292:31 now any questions no
1292:34 questions no all right so what can we do to further
1292:37 all right so what can we do to further uh tighten this up and adhere to some
1292:39 uh tighten this up and adhere to some conventions well let me propose that in
1292:42 conventions well let me propose that in this version we solve one problem and
1292:45 this version we solve one problem and even if you've never done this sort of
1292:46 even if you've never done this sort of thing before I dare say we have enough
1292:48 thing before I dare say we have enough weeks of cs-50 where if I show you
1292:50 weeks of cs-50 where if I show you index.html again and greet HTML again
1292:54 index.html again and greet HTML again odds are to someone's mind there's a
1292:58 odds are to someone's mind there's a opportunity for
1293:00 opportunity for improvement why is this web app super
1293:03 improvement why is this web app super simple though it is arguably poorly
1293:05 simple though it is arguably poorly designed at the
1293:07 designed at the moment and the answer lies somewhere in
1293:10 moment and the answer lies somewhere in these two templates index.html and
1293:13 these two templates index.html and greet.tolowercase
1293:35 probably did necessarily for your homepage why because when you have HTML
1293:37 homepage why because when you have HTML only maybe CSS and even JavaScript
1293:40 only maybe CSS and even JavaScript that's all you can do is copy paste copy
1293:42 that's all you can do is copy paste copy paste and just make sure that you have
1293:44 paste and just make sure that you have the same structure maybe you have the
1293:45 the same structure maybe you have the same CSS file the same Javascript file
1293:47 same CSS file the same Javascript file the same third party libraries but it
1293:49 the same third party libraries but it makes it very very annoying as you might
1293:51 makes it very very annoying as you might have realized already to just m make a
1293:53 have realized already to just m make a change that affects everything so
1293:55 change that affects everything so wouldn't it be nice to like factor out
1293:56 wouldn't it be nice to like factor out all of this and all of this and just let
1294:00 all of this and all of this and just let the body change so here too is something
1294:02 the body change so here too is something that flask and really other equivalent
1294:04 that flask and really other equivalent Frameworks let us do it allows us to
1294:07 Frameworks let us do it allows us to create what we're going to call
1294:08 create what we're going to call conventionally a layout instead so I'm
1294:10 conventionally a layout instead so I'm going to go ahead and do this I'm going
1294:11 going to go ahead and do this I'm going to copy one last time all of the same
1294:14 to copy one last time all of the same HTML I'm going to go into uh my terminal
1294:17 HTML I'm going to go into uh my terminal window and I'm going to create by
1294:19 window and I'm going to create by convention a file called layout. HTML
1294:22 convention a file called layout. HTML this is truly going to be a blueprint of
1294:24 this is truly going to be a blueprint of sorts and in layout. HTML I'm going to
1294:27 sorts and in layout. HTML I'm going to paste all of that same code but I'm
1294:29 paste all of that same code but I'm going to use now some ginger syntax to
1294:32 going to use now some ginger syntax to indicate that I don't want to plug in
1294:34 indicate that I don't want to plug in just a variable like name here I want to
1294:37 just a variable like name here I want to actually plug the contents of a whole
1294:39 actually plug the contents of a whole other file so instead of just using
1294:42 other file so instead of just using curly braces two of them left and right
1294:44 curly braces two of them left and right I have to use slightly different syntax
1294:46 I have to use slightly different syntax to say I want a whole block of HTML here
1294:49 to say I want a whole block of HTML here from some other file and the way to do
1294:51 from some other file and the way to do this even though the syntax is a little
1294:53 this even though the syntax is a little non-obvious is you use open curly brace
1294:56 non-obvious is you use open curly brace percent sign block then you can call the
1294:59 percent sign block then you can call the next word anything you want it just has
1295:01 next word anything you want it just has to be a special type of placeholder for
1295:03 to be a special type of placeholder for an actual file not for just a variable
1295:06 an actual file not for just a variable I'm going to call it body only because
1295:07 I'm going to call it body only because I'm in the body so I'm want a
1295:09 I'm in the body so I'm want a placeholder to be the entire body and
1295:11 placeholder to be the entire body and then outside of this you then say in one
1295:13 then outside of this you then say in one word no space end block so it looks kind
1295:17 word no space end block so it looks kind of stupid honestly and why do we have
1295:19 of stupid honestly and why do we have yet more ugly syntax again just
1295:20 yet more ugly syntax again just different software developers in the
1295:22 different software developers in the world are all choosing their own Syntax
1295:24 world are all choosing their own Syntax for their own libraries so they all kind
1295:25 for their own libraries so they all kind of look different but are all kind of
1295:27 of look different but are all kind of similar in spirit and you just get used
1295:28 similar in spirit and you just get used to seeing the different syntax this now
1295:31 to seeing the different syntax this now is not nearly as pretty as the pair of
1295:34 is not nearly as pretty as the pair of curly braces for variables but this is
1295:36 curly braces for variables but this is how I can say plug the contents of an
1295:38 how I can say plug the contents of an entire file Here and Now what does this
1295:41 entire file Here and Now what does this let me do I can now go back into my
1295:45 let me do I can now go back into my index.html file which at the moment
1295:48 index.html file which at the moment still looks like this but almost all of
1295:50 still looks like this but almost all of this is copy paste the only lines that
1295:52 this is copy paste the only lines that are interesting and different are these
1295:55 are interesting and different are these four lines here in the body so what I
1295:57 four lines here in the body so what I can actually do now is I'm going to
1295:59 can actually do now is I'm going to highlight that and cut it and then I'm
1296:01 highlight that and cut it and then I'm going to highlight everything else and
1296:02 going to highlight everything else and just delete the entire file and I'm
1296:04 just delete the entire file and I'm going to use some of that same syntax
1296:06 going to use some of that same syntax and say curly brace percent sign extends
1296:10 and say curly brace percent sign extends quote unquote layout. HTML and then I
1296:14 quote unquote layout. HTML and then I close my thought with a percent sign and
1296:16 close my thought with a percent sign and close curly brace so this syntax as you
1296:19 close curly brace so this syntax as you might just be inferring is now saying
1296:21 might just be inferring is now saying please extend whatever layout. HTML
1296:25 please extend whatever layout. HTML looks like that's the original blueprint
1296:27 looks like that's the original blueprint the mold out of which I want to make
1296:28 the mold out of which I want to make this web page and now here the syntax is
1296:31 this web page and now here the syntax is a little weird too but similar at least
1296:33 a little weird too but similar at least from before I can now say the block the
1296:36 from before I can now say the block the body block that I want you to plug into
1296:38 body block that I want you to plug into that layout is going to be everything
1296:42 that layout is going to be everything between these two tags which we already
1296:45 between these two tags which we already saw earlier but in layout. HTML they're
1296:48 saw earlier but in layout. HTML they're sort of giving a placeholder in
1296:50 sort of giving a placeholder in index.html this is what I'm going to
1296:52 index.html this is what I'm going to plug in to those other placeholders as
1296:54 plug in to those other placeholders as well so I'm just going to give myself
1296:56 well so I'm just going to give myself some extra white space I'm going to
1296:58 some extra white space I'm going to paste the HTML that was there if I want
1297:00 paste the HTML that was there if I want to make clear what's going on I can
1297:01 to make clear what's going on I can indent it although this has no no
1297:03 indent it although this has no no functional impact but it just makes
1297:05 functional impact but it just makes clear that just like in HTML you can
1297:06 clear that just like in HTML you can open a ginger tag and close it but in
1297:09 open a ginger tag and close it but in ginger here we have this here hey uh Hey
1297:12 ginger here we have this here hey uh Hey python here comes the body of this page
1297:15 python here comes the body of this page hey python that's it for the body of
1297:17 hey python that's it for the body of this page and all of this stuff should
1297:19 this page and all of this stuff should be plugged into this main parent layout
1297:23 be plugged into this main parent layout if you will so super ugly admittedly but
1297:26 if you will so super ugly admittedly but now at least things get way less
1297:28 now at least things get way less redundant because I'm going to do the
1297:29 redundant because I'm going to do the exact same thing over here in greet HTML
1297:32 exact same thing over here in greet HTML it looks like this but now I'm going to
1297:34 it looks like this but now I'm going to do this extends layout. HTML also just
1297:39 do this extends layout. HTML also just as before uh the body the uh the body
1297:42 as before uh the body the uh the body that I want to plug in is going to be
1297:43 that I want to plug in is going to be everything inside of these tags here and
1297:47 everything inside of these tags here and this body is just going to be hello
1297:48 this body is just going to be hello comma name in curly braces like that so
1297:53 comma name in curly braces like that so again ugly syntax got really ugly fast
1297:55 again ugly syntax got really ugly fast but it's really just following these
1297:57 but it's really just following these patterns now and we have two types of
1297:59 patterns now and we have two types of placeholders two curly braces for
1298:01 placeholders two curly braces for variables and now this kind of syntax
1298:04 variables and now this kind of syntax with the percent signs and the single
1298:06 with the percent signs and the single curly braces for like contents of actual
1298:08 curly braces for like contents of actual files and so now in this world or in the
1298:12 files and so now in this world or in the world of a homepage if you were using
1298:13 world of a homepage if you were using flask and python to make your personal
1298:15 flask and python to make your personal homepage with all of those various Pages
1298:18 homepage with all of those various Pages you would probably design one main
1298:19 you would probably design one main layout with all of your pretty logos and
1298:21 layout with all of your pretty logos and colors and fonts and like what you want
1298:23 colors and fonts and like what you want the site to look like and then each of
1298:25 the site to look like and then each of your smaller Pages would now be
1298:27 your smaller Pages would now be distilled into just these smaller
1298:29 distilled into just these smaller fragments and whether you're using
1298:31 fragments and whether you're using python or Java or Javas script or other
1298:33 python or Java or Javas script or other languages too all different programming
1298:35 languages too all different programming languages have popular Frameworks that
1298:37 languages have popular Frameworks that do things like this the idea is the same
1298:40 do things like this the idea is the same across all of them all right let's see
1298:42 across all of them all right let's see if it works let's go back into the
1298:43 if it works let's go back into the browser let me go back to my slash route
1298:47 browser let me go back to my slash route there's that same form let me type in
1298:49 there's that same form let me type in David and type and click greet and
1298:52 David and type and click greet and indeed I see hello comma David I see
1298:55 indeed I see hello comma David I see that greet was automatically added to
1298:57 that greet was automatically added to the URL by the browser when I submitted
1298:58 the URL by the browser when I submitted the form followed by the key value Pairs
1299:00 the form followed by the key value Pairs and if I view the page source as I did
1299:02 and if I view the page source as I did earlier you'll see that you have the
1299:04 earlier you'll see that you have the entirety of that layout with hello David
1299:08 entirety of that layout with hello David plugged in meanwhile if I go back to the
1299:10 plugged in meanwhile if I go back to the form and view this page Source you'll
1299:13 form and view this page Source you'll see the exact same layout but with the
1299:15 see the exact same layout but with the form tag plugged in and here's where you
1299:17 form tag plugged in and here's where you can be a little less uh nitpicky with
1299:20 can be a little less uh nitpicky with styling okay yes this isn't technically
1299:22 styling okay yes this isn't technically indented inside of the body but it was
1299:24 indented inside of the body but it was relative to the original file so at this
1299:26 relative to the original file so at this point in the game you don't need to
1299:28 point in the game you don't need to worry about your outputed HTML looking
1299:30 worry about your outputed HTML looking super pretty you want your source code
1299:32 super pretty you want your source code that the see to be pretty not the
1299:34 that the see to be pretty not the browser this is not a stylistic
1299:37 browser this is not a stylistic concern okay questions on these
1299:40 concern okay questions on these capabilities then a
1299:42 capabilities then a flask or problems that we've just solved
1299:45 flask or problems that we've just solved and why
1299:54 yeah uh okay so if the files in question are in different folders for instance if
1299:57 are in different folders for instance if I go back into my uh index page which
1300:01 I go back into my uh index page which has the form um the routes here are
1300:05 has the form um the routes here are entirely dependent on what is an app.py
1300:08 entirely dependent on what is an app.py there's no notion of a folder when it
1300:11 there's no notion of a folder when it comes to implementing a web application
1300:12 comes to implementing a web application anymore they are more generically routes
1300:15 anymore they are more generically routes however and we've not done this yet you
1300:17 however and we've not done this yet you can put your static content your images
1300:19 can put your static content your images your video files your CSS files in a
1300:21 your video files your CSS files in a folder called Static and there can be
1300:23 folder called Static and there can be subfolders therein and that would affect
1300:25 subfolders therein and that would affect what you use as your Source attributes
1300:27 what you use as your Source attributes for images or your Source uh tags for
1300:30 for images or your Source uh tags for video or any of those kinds of assets
1300:33 video or any of those kinds of assets and we'll see that eventually in the
1300:35 and we'll see that eventually in the home in the uh the problem set next
1300:37 home in the uh the problem set next other questions on what we've just done
1300:39 other questions on what we've just done here
1300:41 here yeah good question how do I how did I
1300:44 yeah good question how do I how did I ensure that the web app starts on the
1300:46 ensure that the web app starts on the form and then goes to the hello page so
1300:49 form and then goes to the hello page so whatever you decide your default index
1300:52 whatever you decide your default index route is like the implicit slash that is
1300:55 route is like the implicit slash that is what is going to be pulled up when a
1300:57 what is going to be pulled up when a user visits the domain name where your
1300:59 user visits the domain name where your website is hosted so if I go back over
1301:01 website is hosted so if I go back over here to app Pi because my slash route is
1301:05 here to app Pi because my slash route is designed to return index.html that's
1301:08 designed to return index.html that's exactly why that response came back good
1301:11 exactly why that response came back good question all right so beyond this let's
1301:14 question all right so beyond this let's consider now issues of privacy and we'll
1301:16 consider now issues of privacy and we'll touch on this too as we get to issues
1301:17 touch on this too as we get to issues like shopping carts and cookies if I
1301:19 like shopping carts and cookies if I zoom in on the URL here even though the
1301:21 zoom in on the URL here even though the URL itself is a little long and ugly and
1301:23 URL itself is a little long and ugly and cryptic but when I type in my name and
1301:26 cryptic but when I type in my name and hit uh greet of course we keep seeing
1301:30 hit uh greet of course we keep seeing name equals David in the URL in what
1301:33 name equals David in the URL in what sense might this be bad
1301:35 sense might this be bad design or in what kinds of web apps
1301:39 design or in what kinds of web apps might you not want the name to show up
1301:41 might you not want the name to show up in the URL like that because this is
1301:43 in the URL like that because this is what Google does this is what my app
1301:46 what Google does this is what my app does
1301:47 does yeah yeah so if I'm logging in with a
1301:50 yeah yeah so if I'm logging in with a username and password I I could imagine
1301:52 username and password I I could imagine that they show up in the URL after the
1301:54 that they show up in the URL after the question mark where username equals
1301:56 question mark where username equals mailin and password equals 1 2 3 4 5 but
1301:59 mailin and password equals 1 2 3 4 5 but then all my you know like nosy siblings
1302:01 then all my you know like nosy siblings need to do is go through through my
1302:02 need to do is go through through my browser history and boom like it's right
1302:04 browser history and boom like it's right there for them to copy paste so that
1302:06 there for them to copy paste so that doesn't seem particularly uh secure or
1302:08 doesn't seem particularly uh secure or if someone's walking past you in a cafe
1302:11 if someone's walking past you in a cafe they can just look at your url if it's
1302:12 they can just look at your url if it's revealed by the browser and they see it
1302:14 revealed by the browser and they see it too so get is not necessarily the best
1302:17 too so get is not necessarily the best verb to use even though it's the default
1302:19 verb to use even though it's the default when submitting forms typically when
1302:21 when submitting forms typically when you've got anything remotely sensitive
1302:23 you've got anything remotely sensitive or anything large so be it a password or
1302:25 or anything large so be it a password or credit card number or like an image or a
1302:28 credit card number or like an image or a video that you're uploading to uh
1302:30 video that you're uploading to uh Instagram or to YouTube or any such site
1302:33 Instagram or to YouTube or any such site like that you don't want the data going
1302:35 like that you don't want the data going into the URL and thankfully there's
1302:38 into the URL and thankfully there's actually an easy way to fix this I can
1302:40 actually an easy way to fix this I can go into my form which is currently in
1302:43 go into my form which is currently in index.html and I can just change the
1302:45 index.html and I can just change the method from get to post in lower case
1302:48 method from get to post in lower case here but the verb in the virtual
1302:50 here but the verb in the virtual envelope we discussed last week would
1302:51 envelope we discussed last week would itself be capital p o St now
1302:55 itself be capital p o St now unfortunately Watch What Happens here
1302:58 unfortunately Watch What Happens here let me go over to my original form by
1303:01 let me go over to my original form by going back to the Slash rout and I'm
1303:03 going back to the Slash rout and I'm reloading the page to make sure I get
1303:04 reloading the page to make sure I get the latest freshest HTML and just as a
1303:07 the latest freshest HTML and just as a um just to confirm here yep if I view
1303:11 um just to confirm here yep if I view Source method is now post so let me go
1303:13 Source method is now post so let me go ahead and type in David now and click
1303:15 ahead and type in David now and click greet and before we saw hello David but
1303:18 greet and before we saw hello David but now I get method not allowed and this is
1303:20 now I get method not allowed and this is somewhat subtle but in the title of the
1303:23 somewhat subtle but in the title of the tab notice that it's a 405 error which
1303:26 tab notice that it's a 405 error which is not familiar probably almost all of
1303:28 is not familiar probably almost all of us have seen 404 file not found turns
1303:30 us have seen 404 file not found turns out 405 a little more Arcane is the
1303:33 out 405 a little more Arcane is the method the HTTP verb is not allowed why
1303:36 method the HTTP verb is not allowed why because by default my app.py only
1303:39 because by default my app.py only currently supports get by default how do
1303:42 currently supports get by default how do I support post well I just need a little
1303:44 I support post well I just need a little bit more syntax so let me go back into
1303:46 bit more syntax so let me go back into vs code here let me go into app.py now
1303:50 vs code here let me go into app.py now and after changing the form I just need
1303:52 and after changing the form I just need to inform flask that you know what the
1303:55 to inform flask that you know what the method I want this GRE route to use
1303:57 method I want this GRE route to use should not be the default which is only
1303:59 should not be the default which is only get I want it to use these methods and
1304:02 get I want it to use these methods and it takes a second argument called
1304:04 it takes a second argument called Methods the value of which is a list the
1304:07 Methods the value of which is a list the default of which is quote unquote get so
1304:09 default of which is quote unquote get so that's the default this has not made any
1304:11 that's the default this has not made any changes but if I want to support post
1304:13 changes but if I want to support post instead I can explicitly pass a list
1304:16 instead I can explicitly pass a list with one string in it P instead and now
1304:20 with one string in it P instead and now what does this mean we didn't talk about
1304:22 what does this mean we didn't talk about this in any detail last week but inside
1304:24 this in any detail last week but inside of this virtual envelope typically is
1304:26 of this virtual envelope typically is that line like get slash search Q equals
1304:30 that line like get slash search Q equals cat after the ex after the question mark
1304:32 cat after the ex after the question mark if you want to hide that kind of
1304:34 if you want to hide that kind of information for privacy sake or because
1304:35 information for privacy sake or because you want to upload like an image which
1304:37 you want to upload like an image which just doesn't make sense to put in the
1304:38 just doesn't make sense to put in the URL essentially the part of the story
1304:40 URL essentially the part of the story would be well the computer looks deeper
1304:43 would be well the computer looks deeper inside of that virtual envelope and
1304:44 inside of that virtual envelope and anything submitted via post goes below
1304:47 anything submitted via post goes below the htdp headers like deeper in that
1304:49 the htdp headers like deeper in that envelope so they're still there they're
1304:51 envelope so they're still there they're just not obviously visible uh for prying
1304:54 just not obviously visible uh for prying eyes in the user's own browser so just
1304:57 eyes in the user's own browser so just by making that change in the HTML
1304:59 by making that change in the HTML telling the browser to submit the data
1305:01 telling the browser to submit the data via post and changing app.py to tell the
1305:04 via post and changing app.py to tell the route to expect the data via post I can
1305:07 route to expect the data via post I can now go back to my other tab let me go
1305:10 now go back to my other tab let me go back to the original page let me reload
1305:12 back to the original page let me reload just so I've got the latest HTML and
1305:14 just so I've got the latest HTML and indeed view page Source it's still yep
1305:16 indeed view page Source it's still yep it's still post but now when I type in
1305:19 it's still post but now when I type in DAV ID and click greet now it works but
1305:23 DAV ID and click greet now it works but but but notice the Privacy implication
1305:27 but but notice the Privacy implication I'm at the SLG greet route but where's
1305:29 I'm at the SLG greet route but where's my name it's not actually there it's
1305:32 my name it's not actually there it's still went to the server but it's not in
1305:34 still went to the server but it's not in your autocomplete or your history now
1305:36 your autocomplete or your history now for privacy sake questions now on
1305:40 for privacy sake questions now on post yeah oh No Just scratching all
1305:44 post yeah oh No Just scratching all right can you the programmer see this
1305:46 right can you the programmer see this well let me show you a couple of other
1305:47 well let me show you a couple of other features of Chrome's uh Chrome and
1305:50 features of Chrome's uh Chrome and Safari and other browsers as well I keep
1305:53 Safari and other browsers as well I keep going to view page source which just
1305:54 going to view page source which just shows you like a readon version of your
1305:56 shows you like a readon version of your HTML but recall that last time I
1305:58 HTML but recall that last time I actually right-clicked and went to
1306:00 actually right-clicked and went to inspect or viewed developer tool tools
1306:02 inspect or viewed developer tool tools and this brings up a much fancier
1306:04 and this brings up a much fancier version of the developer tools and under
1306:06 version of the developer tools and under elements here you see everything and
1306:08 elements here you see everything and it's nice and pretty printed it's
1306:09 it's nice and pretty printed it's hierarchical it collapses things into
1306:11 hierarchical it collapses things into these clickable triangles but it's the
1306:12 these clickable triangles but it's the exact same thing it's just more
1306:14 exact same thing it's just more interactive but notice what I can do
1306:16 interactive but notice what I can do today is this if I go to the network tab
1306:19 today is this if I go to the network tab here and let me zoom out a little bit
1306:21 here and let me zoom out a little bit let me go ahead and re uh load the form
1306:25 let me go ahead and re uh load the form here and type in David again and click
1306:29 here and type in David again and click greet notice now in the network tab of
1306:33 greet notice now in the network tab of Chrome's developer tools I see a few
1306:36 Chrome's developer tools I see a few things as we saw before one I see that
1306:38 things as we saw before one I see that the request method is post two I see
1306:41 the request method is post two I see that the server automatically without me
1306:42 that the server automatically without me writing any code for this returns 200
1306:44 writing any code for this returns 200 when it's successful but I can scroll
1306:47 when it's successful but I can scroll down down down down down and you'll see
1306:50 down down down down down and you'll see that eventually after all these cookies
1306:52 that eventually after all these cookies more on those later if I click on
1306:55 more on those later if I click on payload the second tab next to headers
1306:57 payload the second tab next to headers you can see as the developer what was
1307:00 you can see as the developer what was actually sent to the server so indeed
1307:02 actually sent to the server so indeed this is going to be super useful like
1307:03 this is going to be super useful like when doing problem set 9 maybe your
1307:05 when doing problem set 9 maybe your final projects if you want to see what's
1307:07 final projects if you want to see what's going from browser to server you have
1307:09 going from browser to server you have complete control over all of that
1307:11 complete control over all of that information even if you're using htps
1307:13 information even if you're using htps because your browser and you the
1307:14 because your browser and you the developer can certainly see all of this
1307:17 developer can certainly see all of this so again these developer tools even
1307:19 so again these developer tools even though there's a lot of tabs and buttons
1307:20 though there's a lot of tabs and buttons you probably won't need anytime soon
1307:22 you probably won't need anytime soon some of them like elements and network
1307:24 some of them like elements and network and with JavaScript console are going to
1307:26 and with JavaScript console are going to be super useful to start to get familiar
1307:30 be super useful to start to get familiar with all right any questions now on this
1307:35 with all right any questions now on this implication of
1307:38 implication of post anything at
1307:42 post anything at all no okay how about one final hello
1307:45 all no okay how about one final hello example that ties a few of these things
1307:47 example that ties a few of these things together how about now we try to tighten
1307:50 together how about now we try to tighten things up further only in anticipation
1307:52 things up further only in anticipation of something like problem set 9 or
1307:54 of something like problem set 9 or really more complicated web apps where
1307:57 really more complicated web apps where you might have not two but 20 or maybe
1308:00 you might have not two but 20 or maybe even more different routes it might
1308:02 even more different routes it might might be ideal to just minimize how many
1308:04 might be ideal to just minimize how many total routes we have so we don't get a
1308:06 total routes we have so we don't get a little too overwhelmed and I dare say
1308:08 little too overwhelmed and I dare say that these two routes are so short maybe
1308:11 that these two routes are so short maybe I can combine them into one and maybe I
1308:14 I can combine them into one and maybe I can keep the user at what seems to be
1308:16 can keep the user at what seems to be the same URL but just a kind of Tidy
1308:18 the same URL but just a kind of Tidy things up so let me propose that we do
1308:20 things up so let me propose that we do this instead let me get rid of my greet
1308:23 this instead let me get rid of my greet route and let me go into my form in
1308:27 route and let me go into my form in index.html and let me go ahead and just
1308:30 index.html and let me go ahead and just have the action of this form still slash
1308:33 have the action of this form still slash so I want the form to be visible at
1308:35 so I want the form to be visible at slash the index of the site but I also
1308:37 slash the index of the site but I also want the form to submit to itself if
1308:40 want the form to submit to itself if only because I don't want to introduce
1308:42 only because I don't want to introduce another route like SLG greet which
1308:44 another route like SLG greet which eventually indeed will be compelling so
1308:45 eventually indeed will be compelling so you don't have one route for everything
1308:47 you don't have one route for everything you want your website to do so
1308:49 you want your website to do so technically this is the default to and
1308:51 technically this is the default to and if I omit action the exact same thing
1308:53 if I omit action the exact same thing would happen as well but let me rewind
1308:55 would happen as well but let me rewind and let me now go into app.py to see how
1308:57 and let me now go into app.py to see how we can make this happen well if I want
1308:59 we can make this happen well if I want my one and now only route to support
1309:01 my one and now only route to support both methods I can say methods equals
1309:04 both methods I can say methods equals and then a list with both get and post
1309:07 and then a list with both get and post in any order but I'll keep them
1309:08 in any order but I'll keep them alphabetical like this this now tells
1309:10 alphabetical like this this now tells python hey this route should handle both
1309:13 python hey this route should handle both get and post requests at the same place
1309:17 get and post requests at the same place let's now go into this function I kind
1309:19 let's now go into this function I kind of want to say the equivalent of this if
1309:23 of want to say the equivalent of this if get then I want to return the form else
1309:26 get then I want to return the form else if post I want to then return render
1309:29 if post I want to then return render template of greet Doh HTML with the
1309:32 template of greet Doh HTML with the user's name but this is not yet complete
1309:35 user's name but this is not yet complete code but I think I can do this I'm going
1309:37 code but I think I can do this I'm going to go ahead and say the following I'm
1309:39 to go ahead and say the following I'm going to go ahead and say if
1309:42 going to go ahead and say if request. method equals equals get then
1309:46 request. method equals equals get then indeed return
1309:47 indeed return index.html L if request. method equals
1309:51 index.html L if request. method equals equals post then go ahead and return
1309:53 equals post then go ahead and return greet HTML this isn't quite enough
1309:56 greet HTML this isn't quite enough though because I still want to pass in
1309:58 though because I still want to pass in that placeholder so let me again add
1310:00 that placeholder so let me again add back name equals request. args doget
1310:04 back name equals request. args doget quote unquote name and then a default
1310:06 quote unquote name and then a default value of world what does this Now do for
1310:10 value of world what does this Now do for me well let me go back to my other tab
1310:13 me well let me go back to my other tab here let me close the developer tools
1310:15 here let me close the developer tools let me go back to the form here let me
1310:16 let me go back to the form here let me reload to make sure I have the latest
1310:18 reload to make sure I have the latest let me view page Source just to make
1310:19 let me view page Source just to make sure I have the latest and yep I have
1310:21 sure I have the latest and yep I have the latest because it still says post
1310:23 the latest because it still says post but it now says slash and let's see what
1310:25 but it now says slash and let's see what happens now if I type in my name David
1310:27 happens now if I type in my name David previously this submitted bya post so I
1310:29 previously this submitted bya post so I didn't see any name or value thereof in
1310:32 didn't see any name or value thereof in the URL but I did end up at SLG greet
1310:34 the URL but I did end up at SLG greet but if the action is now slash and I
1310:36 but if the action is now slash and I click greet notice that it still kind of
1310:39 click greet notice that it still kind of works I see hello comma World although
1310:41 works I see hello comma World although that didn't quite work so we'll come
1310:43 that didn't quite work so we'll come back to that issue in a moment but
1310:44 back to that issue in a moment but notice the URL ends in just slash and
1310:47 notice the URL ends in just slash and again Chrome is hiding the slash because
1310:48 again Chrome is hiding the slash because that's all that's there but it does not
1310:50 that's all that's there but it does not end in name equals David in this case or
1310:54 end in name equals David in this case or name and equals world now notice this
1310:56 name and equals world now notice this too if I reload I'm going to get this
1310:59 too if I reload I'm going to get this warning do you want to confirm form
1311:02 warning do you want to confirm form resubmission the page you're looking for
1311:03 resubmission the page you're looking for used information that you entered
1311:04 used information that you entered returning to that page might cause any
1311:06 returning to that page might cause any action you took to be repeated do you
1311:07 action you took to be repeated do you want to continue you might have seen
1311:09 want to continue you might have seen this on websites you've actually visited
1311:10 this on websites you've actually visited where you hit reload and you're prompted
1311:12 where you hit reload and you're prompted wait a minute do you want to do that
1311:14 wait a minute do you want to do that odds are you've been prompted to reload
1311:16 odds are you've been prompted to reload explicitly because why whatever you just
1311:18 explicitly because why whatever you just did was post instead of get and by
1311:21 did was post instead of get and by convention besides post being used for
1311:23 convention besides post being used for privacy to like hide your username your
1311:26 privacy to like hide your username your password your credit card number or the
1311:27 password your credit card number or the like besides being used to upload bigger
1311:29 like besides being used to upload bigger files like images or videos post is also
1311:32 files like images or videos post is also used by convention to make changes to
1311:34 used by convention to make changes to the server to add something to your
1311:36 the server to add something to your shopping cart to add something to the
1311:38 shopping cart to add something to the database whereas get is the name
1311:40 database whereas get is the name suggests is all about getting
1311:41 suggests is all about getting information not posting or sending
1311:44 information not posting or sending information instead so this is Chrome
1311:46 information instead so this is Chrome being a little careful because you know
1311:48 being a little careful because you know if you just checked out on Amazon and
1311:50 if you just checked out on Amazon and then you hit reload you don't want to
1311:51 then you hit reload you don't want to accidentally like buy the same book
1311:53 accidentally like buy the same book again so to speak even though Amazon and
1311:55 again so to speak even though Amazon and fancy websites have other defenses for
1311:57 fancy websites have other defenses for this too to avoid this issue now there
1312:00 this too to avoid this issue now there is a bug though here it says hello comma
1312:03 is a bug though here it says hello comma World instead of hello comma David and
1312:05 World instead of hello comma David and it actually would have said the same a
1312:06 it actually would have said the same a moment ago and I just didn't retest the
1312:09 moment ago and I just didn't retest the code and reveal as much to you or if I
1312:11 code and reveal as much to you or if I did I didn't even notice it said hello
1312:12 did I didn't even notice it said hello world instead of hello David it turns
1312:15 world instead of hello David it turns out that request. RGS is only used for
1312:19 out that request. RGS is only used for get when using get request. args is a
1312:22 get when using get request. args is a dictionary that contains all of your key
1312:24 dictionary that contains all of your key value pairs but somewhat confusingly
1312:26 value pairs but somewhat confusingly when using post with flask you have to
1312:29 when using post with flask you have to go into request. form I have no idea why
1312:32 go into request. form I have no idea why these are not sort of more obvious
1312:33 these are not sort of more obvious opposites like request.get or request.
1312:36 opposites like request.get or request. form and sorry request.get and request.
1312:39 form and sorry request.get and request. poost would be sort of sensible names in
1312:41 poost would be sort of sensible names in this case though we have request. args
1312:43 this case though we have request. args for get and request. form for post all
1312:47 for get and request. form for post all right that's an easy fix though if I go
1312:48 right that's an easy fix though if I go back to vs code here let's change
1312:50 back to vs code here let's change request. ARS to request. form let's go
1312:53 request. ARS to request. form let's go back to my other tab let me just reload
1312:56 back to my other tab let me just reload and you know what I'm going to say okay
1312:58 and you know what I'm going to say okay continue to resubmit the same form
1313:00 continue to resubmit the same form because the form was okay it was my
1313:01 because the form was okay it was my python code that was buggy hitting enter
1313:04 python code that was buggy hitting enter now it's accessing David okay but watch
1313:07 now it's accessing David okay but watch this again if I hit reload command r or
1313:09 this again if I hit reload command r or control r i get the same warning are you
1313:11 control r i get the same warning are you sure you want to submit the form yes if
1313:13 sure you want to submit the form yes if I do it manually with the reload icon I
1313:15 I do it manually with the reload icon I get the same warning as before but if I
1313:18 get the same warning as before but if I want to manually induce a get request
1313:20 want to manually induce a get request well that's fine don't hit reload and
1313:22 well that's fine don't hit reload and send the same request instead go up to
1313:24 send the same request instead go up to your URL and just put the cursor up
1313:27 your URL and just put the cursor up there and hit enter and now notice same
1313:29 there and hit enter and now notice same URL is a get by default so anytime you
1313:32 URL is a get by default so anytime you and I have typed URLs into browsers get
1313:35 and I have typed URLs into browsers get is always the default only when you
1313:37 is always the default only when you click on a button typically that the
1313:39 click on a button typically that the programmer has configured to use post
1313:42 programmer has configured to use post are you actually adding things to your
1313:44 are you actually adding things to your shopping cart or the like all right so
1313:47 shopping cart or the like all right so we are back and if I go way back in time
1313:51 we are back and if I go way back in time myself like this is actually like the
1313:53 myself like this is actually like the first web application I made back in
1313:57 first web application I made back in 1997 I believe uh so at the time this
1314:01 1997 I believe uh so at the time this was would have been what my sophomore or
1314:03 was would have been what my sophomore or so year I had taken cs50 I took a
1314:05 so year I had taken cs50 I took a follow- on class called cs51 which is a
1314:07 follow- on class called cs51 which is a different type of programming and then I
1314:08 different type of programming and then I pretty much taught myself a language
1314:10 pretty much taught myself a language called Pearl which is somewhat less
1314:11 called Pearl which is somewhat less popular nowadays but it's another
1314:13 popular nowadays but it's another language like python like Java like
1314:15 language like python like Java like JavaScript like others that can be used
1314:17 JavaScript like others that can be used to make web-based applications and the
1314:19 to make web-based applications and the web was very young at the time and the
1314:22 web was very young at the time and the process via which students my classmates
1314:24 process via which students my classmates could register uh for the first year
1314:26 could register uh for the first year intral sports program AKA Frost's was to
1314:29 intral sports program AKA Frost's was to grab a piece of paper and like write
1314:31 grab a piece of paper and like write your name and email address on it and
1314:32 your name and email address on it and walk it across across the yard to
1314:34 walk it across across the yard to Wigglesworth I believe where the Proctor
1314:36 Wigglesworth I believe where the Proctor lived and you'd Slide the piece of paper
1314:37 lived and you'd Slide the piece of paper under the door and like that was how we
1314:39 under the door and like that was how we submitted forms in my day um so this was
1314:42 submitted forms in my day um so this was an opportunity even back in 1997 is to
1314:44 an opportunity even back in 1997 is to like move things online and the website
1314:47 like move things online and the website went on to live on until I think like
1314:48 went on to live on until I think like 2007 I found this online and then it's
1314:51 2007 I found this online and then it's become something else since um but this
1314:53 become something else since um but this was a website via which people could
1314:55 was a website via which people could register for sports and people could log
1314:56 register for sports and people could log in the scores for various games and
1314:58 in the scores for various games and whatnot and so underneath the hood I
1315:00 whatnot and so underneath the hood I didn't even know anything anything about
1315:01 didn't even know anything anything about databases at the time it was just like
1315:03 databases at the time it was just like CSV files that I was storing the data in
1315:06 CSV files that I was storing the data in but there were HTML forms and there was
1315:09 but there were HTML forms and there was with pearl the language at the time the
1315:11 with pearl the language at the time the way to do the exact kind of stuff that
1315:12 way to do the exact kind of stuff that we've just been doing already with flask
1315:15 we've just been doing already with flask and so what I thought we'd do is
1315:17 and so what I thought we'd do is Implement a slightly less ugly version
1315:19 Implement a slightly less ugly version of this um repeating uh graphical
1315:22 of this um repeating uh graphical backgrounds were invogue in like 1997 as
1315:25 backgrounds were invogue in like 1997 as you can see here um but this is where
1315:27 you can see here um but this is where these were the Aesthetics of the day uh
1315:29 these were the Aesthetics of the day uh including the the so-called blink tag so
1315:31 including the the so-called blink tag so let's at least focus on the
1315:32 let's at least focus on the functionality of this website and not so
1315:34 functionality of this website and not so much the Aesthetics and see if we can't
1315:35 much the Aesthetics and see if we can't Implement some of the plumbing for
1315:37 Implement some of the plumbing for actually solving like a real world
1315:38 actually solving like a real world representative problem be it for
1315:40 representative problem be it for freshman inal sports or something else
1315:42 freshman inal sports or something else like it where you're getting data from
1315:43 like it where you're getting data from users and processing it somehow so let
1315:45 users and processing it somehow so let me go over here to VSS code let me
1315:47 me go over here to VSS code let me create a new directory called fros IMS
1315:50 create a new directory called fros IMS just so we can keep all of this code in
1315:51 just so we can keep all of this code in its own directory let me CD into fros
1315:54 its own directory let me CD into fros IMS let me proactively make another
1315:56 IMS let me proactively make another directory called templates in which our
1315:58 directory called templates in which our templates our HTML files do need to live
1316:02 templates our HTML files do need to live and eventually I'm going to go ahead and
1316:04 and eventually I'm going to go ahead and create a two files minimally app.py and
1316:08 create a two files minimally app.py and index.html so let's do the first of
1316:10 index.html so let's do the first of those app.py will live in my Frost im's
1316:13 those app.py will live in my Frost im's directory and I'm just going to recreate
1316:15 directory and I'm just going to recreate something very simple like we have
1316:17 something very simple like we have previously so from flask in lowercase
1316:20 previously so from flask in lowercase import flask capitalized render template
1316:23 import flask capitalized render template and also request so same first line is
1316:25 and also request so same first line is before let me then give myself a
1316:27 before let me then give myself a variable called app set it equal to
1316:29 variable called app set it equal to calling the flask function capital f
1316:31 calling the flask function capital f with underscore uncore name underscore
1316:33 with underscore uncore name underscore uncore and then let me give myself a
1316:37 uncore and then let me give myself a route for slash as before with an index
1316:41 route for slash as before with an index function though again I could call that
1316:43 function though again I could call that anything I want and just for now let's
1316:45 anything I want and just for now let's return render template of quote unquote
1316:47 return render template of quote unquote index.html as though that exists so this
1316:50 index.html as though that exists so this is not really a web application as much
1316:51 is not really a web application as much as it is at the moment just a recreation
1316:53 as it is at the moment just a recreation of HTTP server for one file let's now in
1316:57 of HTTP server for one file let's now in another tab create a templates file uh
1317:01 another tab create a templates file uh called called
1317:02 called called index.html and I'm going to save myself
1317:04 index.html and I'm going to save myself a few keystrokes I let me copy paste
1317:06 a few keystrokes I let me copy paste from earlier almost all of the layout
1317:08 from earlier almost all of the layout from before I've change the title in
1317:10 from before I've change the title in advance to frosts instead of hello but
1317:13 advance to frosts instead of hello but this is essentially the same template
1317:14 this is essentially the same template and for now though because I'm in an
1317:16 and for now though because I'm in an index.html I'm not going to use extends
1317:18 index.html I'm not going to use extends or any of that fancy block stuff yet I'm
1317:21 or any of that fancy block stuff yet I'm just going to go ahead and create a
1317:22 just going to go ahead and create a relatively simple form via which back in
1317:25 relatively simple form via which back in the day my classmates could have
1317:26 the day my classmates could have registered for interal Sports so let's
1317:29 registered for interal Sports so let's go ahead here and I'll propose that we
1317:33 go ahead here and I'll propose that we do this um in this page we'll have a
1317:36 do this um in this page we'll have a form the action of which will be a route
1317:39 form the action of which will be a route called SL register though I could call
1317:41 called SL register though I could call that anything I want it'll be somewhat
1317:43 that anything I want it'll be somewhat private so I'm going to use post instead
1317:45 private so I'm going to use post instead of get just so that people don't
1317:47 of get just so that people don't accidentally maybe register twice by
1317:49 accidentally maybe register twice by hitting reload uh without warning uh
1317:51 hitting reload uh without warning uh inside of this form let's go ahead and
1317:54 inside of this form let's go ahead and give them an input uh where autocomplete
1317:57 give them an input uh where autocomplete will be off as always for demonstration
1317:59 will be off as always for demonstration sake autofocus so the cursor goes there
1318:02 sake autofocus so the cursor goes there initially the name of this field will be
1318:04 initially the name of this field will be literally named because I want my
1318:05 literally named because I want my classmates's name if they want to
1318:07 classmates's name if they want to register for some sport the placeholder
1318:09 register for some sport the placeholder will again be quote unquote Nam just so
1318:11 will again be quote unquote Nam just so they see some gray instructions and the
1318:13 they see some gray instructions and the type of this field will indeed be text
1318:16 type of this field will indeed be text as before and then I need to give them
1318:18 as before and then I need to give them the ability to register for a few Sports
1318:20 the ability to register for a few Sports why don't we keep it simple like back in
1318:22 why don't we keep it simple like back in the day basketball soccer and Ultimate
1318:24 the day basketball soccer and Ultimate Frisbee were three of the sports that we
1318:26 Frisbee were three of the sports that we supported and so let me do this and you
1318:28 supported and so let me do this and you might not have seen this before uh
1318:30 might not have seen this before uh unless you dabbled further on with forms
1318:31 unless you dabbled further on with forms on your own but I can create a select
1318:33 on your own but I can create a select menu otherwise known as a drop-down menu
1318:36 menu otherwise known as a drop-down menu in HTML inside of which are a whole
1318:38 in HTML inside of which are a whole bunch of options and each option
1318:40 bunch of options and each option typically follows this Paradigm the
1318:42 typically follows this Paradigm the value of the option and then the actual
1318:44 value of the option and then the actual text that the human sees so the value of
1318:46 text that the human sees so the value of these options will be how about we do uh
1318:50 these options will be how about we do uh basketball as one and I want the human
1318:53 basketball as one and I want the human to see literally the same thing though
1318:55 to see literally the same thing though just like with a link in HTML they could
1318:56 just like with a link in HTML they could be different but I'm going to keep them
1318:58 be different but I'm going to keep them the same another option will be
1319:01 the same another option will be uh let's say
1319:03 uh let's say soccer and whoops let me fix my quotes
1319:06 soccer and whoops let me fix my quotes and this human will see the exact same
1319:08 and this human will see the exact same thing though it could say something else
1319:09 thing though it could say something else and then lastly the value will be quote
1319:11 and then lastly the value will be quote unquote Ultimate Frisbee and the humans
1319:15 unquote Ultimate Frisbee and the humans will see the same thing there ultimate
1319:18 will see the same thing there ultimate frisbe all right so this is going to
1319:20 frisbe all right so this is going to create as we'll soon see just a drop-
1319:21 create as we'll soon see just a drop- down menu with three separate options if
1319:23 down menu with three separate options if I want the students to be able to submit
1319:25 I want the students to be able to submit this now let me give them a button the
1319:26 this now let me give them a button the type of which is submit and this button
1319:29 type of which is submit and this button will be like the word register on it so
1319:32 will be like the word register on it so I think we're pretty much good to go
1319:33 I think we're pretty much good to go like this is all just HTML no python no
1319:36 like this is all just HTML no python no flask per se except for the rendering of
1319:38 flask per se except for the rendering of the same template so let me go into my
1319:40 the same template so let me go into my terminal window let me do flask run
1319:43 terminal window let me do flask run inside of this directory because I need
1319:45 inside of this directory because I need to serve this app instead I'm going to
1319:46 to serve this app instead I'm going to see some ugly output including my own
1319:48 see some ugly output including my own URL and if I hover over that and then
1319:50 URL and if I hover over that and then open that URL I should now see a more
1319:53 open that URL I should now see a more interesting form it's got not only a
1319:54 interesting form it's got not only a field for their name but also this
1319:56 field for their name but also this drop-down menu with all three Sports now
1319:59 drop-down menu with all three Sports now this isn't maybe the best user uh
1320:01 this isn't maybe the best user uh experience thus far because I feel like
1320:03 experience thus far because I feel like I'm biasing people to registering for
1320:05 I'm biasing people to registering for basketball maybe because it's checked by
1320:07 basketball maybe because it's checked by default I mean a lot of forms nowadays
1320:09 default I mean a lot of forms nowadays have like a blank placeholder for the
1320:11 have like a blank placeholder for the form so this is just an aesthetic thing
1320:13 form so this is just an aesthetic thing but I can do this let me go back to the
1320:15 but I can do this let me go back to the same form and let me give myself just a
1320:18 same form and let me give myself just a a blank option at the top that in fact
1320:20 a blank option at the top that in fact I'm going to disable so you technically
1320:22 I'm going to disable so you technically can't select it proactively but I am
1320:24 can't select it proactively but I am going to select it by default and so we
1320:26 going to select it by default and so we probably haven't seen those HTML
1320:28 probably haven't seen those HTML attributes before but if I want to
1320:30 attributes before but if I want to create the equivalent of like a a a
1320:33 create the equivalent of like a a a title for this dropdown I'm going to
1320:35 title for this dropdown I'm going to literally create a disabled option
1320:37 literally create a disabled option that's automatically selected called
1320:39 that's automatically selected called sport so that you can't select it per se
1320:41 sport so that you can't select it per se but it is there at the top so if I go
1320:44 but it is there at the top so if I go back now to my other tab reload you know
1320:47 back now to my other tab reload you know it's just marginally prettier than
1320:48 it's just marginally prettier than before and I'm not biasing people toward
1320:50 before and I'm not biasing people toward accidentally registering for basketball
1320:52 accidentally registering for basketball alone and if I click on this you'll see
1320:54 alone and if I click on this you'll see that sport is great out and therefore
1320:55 that sport is great out and therefore not manually selectable but I can select
1320:57 not manually selectable but I can select any of these other three still all right
1321:00 any of these other three still all right well un unfortunately if I type in David
1321:02 well un unfortunately if I type in David and I try registering for instance for
1321:04 and I try registering for instance for soccer and click register I do end up
1321:08 soccer and click register I do end up atreg and there's no question mark or
1321:10 atreg and there's no question mark or name or sport so it's probably indeed
1321:12 name or sport so it's probably indeed post instead of get those are hints but
1321:15 post instead of get those are hints but not found notice the tab here very uh uh
1321:19 not found notice the tab here very uh uh uh succinctly says 404 not found well
1321:22 uh succinctly says 404 not found well why is that just to be clear why did SL
1321:26 why is that just to be clear why did SL register give me a
1321:32 404 what's the logic here perhaps just State the obvious
1321:37 here perhaps just State the obvious or it doesn't exist right we haven't
1321:39 or it doesn't exist right we haven't done that step yet all right so
1321:41 done that step yet all right so something as simple as that and so I
1321:42 something as simple as that and so I actually U sort of belabor that point
1321:44 actually U sort of belabor that point because as you're learning like a lot of
1321:46 because as you're learning like a lot of these conventions and some of this new
1321:47 these conventions and some of this new syntax like honestly you're just going
1321:49 syntax like honestly you're just going to make stupid mistakes something's not
1321:50 to make stupid mistakes something's not going to work but again go back to First
1321:52 going to work but again go back to First principles why is it not found all
1321:54 principles why is it not found all right/ register should be a template
1321:56 right/ register should be a template maybe called register. HTML oh I forgot
1321:58 maybe called register. HTML oh I forgot my app. route so that should be the kind
1322:00 my app. route so that should be the kind of of thinking as you try to diagnose
1322:02 of of thinking as you try to diagnose these problems moving forward all right
1322:04 these problems moving forward all right so let me go into app. and let me give
1322:07 so let me go into app. and let me give myself a second route here uh so app.
1322:10 myself a second route here uh so app. route quote unquote SL route then let me
1322:13 route quote unquote SL route then let me Define a function called anything I want
1322:15 Define a function called anything I want but I'm going to call it oh sorry not SL
1322:17 but I'm going to call it oh sorry not SL route SL register let me call the
1322:19 route SL register let me call the function just to be consistent register
1322:22 function just to be consistent register so but I could call that anything I want
1322:24 so but I could call that anything I want and just for now let's not do anything
1322:26 and just for now let's not do anything too interesting let's just return the
1322:28 too interesting let's just return the rendering of a template called success
1322:30 rendering of a template called success .html let's just pretend for now that
1322:32 .html let's just pretend for now that registration is successful no matter who
1322:34 registration is successful no matter who you are or what you do now I need that
1322:36 you are or what you do now I need that template and I only have index.html at
1322:39 template and I only have index.html at this point so let me actually now do my
1322:41 this point so let me actually now do my best practices let me copy all of that
1322:44 best practices let me copy all of that let me in a separate terminal window let
1322:47 let me in a separate terminal window let me do code uh let me go into my Frost
1322:49 me do code uh let me go into my Frost im's directory and let me create a new
1322:52 im's directory and let me create a new template called layout. HTML just like
1322:54 template called layout. HTML just like before let me paste all that same code
1322:57 before let me paste all that same code let me delete the form and just put in
1322:59 let me delete the form and just put in that big placehold folder so block body
1323:03 that big placehold folder so block body and then end block is all I did earlier
1323:06 and then end block is all I did earlier this is just kind of boiler plate now
1323:08 this is just kind of boiler plate now convention everything else I'm going to
1323:09 convention everything else I'm going to leave the same but if I wanted to make
1323:11 leave the same but if I wanted to make it prettier I could add my CSS up top if
1323:13 it prettier I could add my CSS up top if I wanted to add like this crazy
1323:15 I wanted to add like this crazy repeating background I could probably do
1323:16 repeating background I could probably do that up top too so I could make every
1323:18 that up top too so I could make every page look as ugly as it did back in my
1323:20 page look as ugly as it did back in my day but we'll focus just today on the
1323:22 day but we'll focus just today on the text all right so now that I have
1323:24 text all right so now that I have layout. HTML let me clean up index.html
1323:28 layout. HTML let me clean up index.html I don't need all this redundancy I don't
1323:30 I don't need all this redundancy I don't need all of these tags at the top
1323:32 need all of these tags at the top instead recall I think I just need
1323:36 instead recall I think I just need extends quote unquote layout. HTML with
1323:40 extends quote unquote layout. HTML with the appropriate percent signs and curly
1323:42 the appropriate percent signs and curly braces I then have the appropriate block
1323:44 braces I then have the appropriate block body though I could call Body anything I
1323:46 body though I could call Body anything I want but I'm going to stick with my
1323:47 want but I'm going to stick with my convention earlier and I'm going to
1323:49 convention earlier and I'm going to delete the tags down here that I no
1323:51 delete the tags down here that I no longer need why because if I go into
1323:54 longer need why because if I go into layout. HTML I already have all my open
1323:56 layout. HTML I already have all my open tags all my Clos tags the only stuff I
1323:59 tags all my Clos tags the only stuff I want in index.html is is going to be
1324:01 want in index.html is is going to be which belongs in the body so end block
1324:04 which belongs in the body so end block down here and just to be pedantic let me
1324:07 down here and just to be pedantic let me go ahead and highlight all that hit
1324:09 go ahead and highlight all that hit shift Tab and that will like unindent it
1324:11 shift Tab and that will like unindent it just to line things up just to be tidy
1324:14 just to line things up just to be tidy all right so better even though it looks
1324:15 all right so better even though it looks a little cryptic now but now I've laid
1324:17 a little cryptic now but now I've laid the foundation for making a third page a
1324:19 the foundation for making a third page a fourth page that don't have all of that
1324:21 fourth page that don't have all of that same copy paste all right so now let's
1324:23 same copy paste all right so now let's go back into app.py success. HTML is
1324:26 go back into app.py success. HTML is where I left off so okay let me open my
1324:28 where I left off so okay let me open my terminal window let me code up a
1324:30 terminal window let me code up a template called success. HTML whose
1324:33 template called success. HTML whose purpose in life is literally just going
1324:35 purpose in life is literally just going to be like to say you are registered
1324:37 to be like to say you are registered just so that we see some informative
1324:39 just so that we see some informative message on the screen so this part I do
1324:41 message on the screen so this part I do still need extends layout. HTML so
1324:44 still need extends layout. HTML so there's a little bit of copy paste still
1324:46 there's a little bit of copy paste still which is a little ugly but so be it
1324:49 which is a little ugly but so be it block body for this template and I'm
1324:51 block body for this template and I'm just going to say you are registered
1324:53 just going to say you are registered exclamation point all right and then end
1324:56 exclamation point all right and then end block so super simple it's just an
1324:58 block so super simple it's just an informative message claiming that the
1325:00 informative message claiming that the Stu the student is registered all right
1325:02 Stu the student is registered all right let's go back to the original form which
1325:04 let's go back to the original form which is this let me reload to make sure my
1325:06 is this let me reload to make sure my HTML has reloaded type in David I'm
1325:09 HTML has reloaded type in David I'm going to register again for soccer and
1325:11 going to register again for soccer and click register and oh interesting method
1325:15 click register and oh interesting method not allowed so I'm not getting a 404
1325:18 not allowed so I'm not getting a 404 anymore I'm getting
1325:20 anymore I'm getting 405 at SL
1325:23 405 at SL register what's the deduction here how
1325:25 register what's the deduction here how did I screw up this
1325:31 time 405 is progress yeah the placeholder uh so it's not the
1325:34 placeholder uh so it's not the placeholder I think is okay this is now
1325:36 placeholder I think is okay this is now about the underlying HTTP stuff the
1325:38 about the underlying HTTP stuff the method was disallow was not
1325:42 method was disallow was not allowed say again so get purposes post
1325:45 allowed say again so get purposes post thing too so by default all of these
1325:47 thing too so by default all of these routes in flask just by default assume
1325:49 routes in flask just by default assume get because it's safe it doesn't allow
1325:51 get because it's safe it doesn't allow you to send information to the server in
1325:53 you to send information to the server in quite the same way but if I do want to
1325:55 quite the same way but if I do want to support post recall that we changed this
1325:57 support post recall that we changed this to be methods equals and then a list
1325:59 to be methods equals and then a list with quote quote Post in it so I just
1326:01 with quote quote Post in it so I just need to enable support for that method
1326:04 need to enable support for that method that that is that HTTP verb all right
1326:06 that that is that HTTP verb all right let's go back to the form reload just to
1326:08 let's go back to the form reload just to make sure I haven't screwed up type in
1326:09 make sure I haven't screwed up type in my name David select soccer from the
1326:12 my name David select soccer from the dropdown click register and now I'm not
1326:15 dropdown click register and now I'm not only at SL register in the URL it claims
1326:18 only at SL register in the URL it claims that I am indeed registered now of
1326:19 that I am indeed registered now of course I'm not I've done nothing
1326:20 course I'm not I've done nothing interesting there's no database there's
1326:22 interesting there's no database there's no CSV file we'll get to that in a bit
1326:24 no CSV file we'll get to that in a bit but at least I now have the plumbing in
1326:26 but at least I now have the plumbing in place to do something Dynamic based on
1326:28 place to do something Dynamic based on that sport all right well how can I now
1326:31 that sport all right well how can I now improve upon this how about we go ahead
1326:34 improve upon this how about we go ahead and implement store the actual
1326:37 and implement store the actual registrant in a dictionary in the
1326:38 registrant in a dictionary in the computer's memory so instead of just
1326:40 computer's memory so instead of just claiming that they're registered let's
1326:41 claiming that they're registered let's actually make a notation and the
1326:43 actually make a notation and the simplest way as we did weeks ago in
1326:44 simplest way as we did weeks ago in Python is just store things in like a
1326:46 Python is just store things in like a variable in memory like a list or a
1326:48 variable in memory like a list or a dictionary a set anything like that all
1326:51 dictionary a set anything like that all right well let me go back into vs code
1326:53 right well let me go back into vs code and in app.py and I think what I'm going
1326:55 and in app.py and I think what I'm going to have to do here is change my register
1326:57 to have to do here is change my register route to actually do some useful
1327:00 route to actually do some useful information but before I register the
1327:03 information but before I register the user let's consider where I want to
1327:05 user let's consider where I want to actually put them and so let me propose
1327:07 actually put them and so let me propose that how about we do this at the top of
1327:12 that how about we do this at the top of my file let me go ahead and declare a
1327:14 my file let me go ahead and declare a global variable called registrant uh and
1327:18 global variable called registrant uh and set that equal to an empty dictionary so
1327:21 set that equal to an empty dictionary so we've done this before when we were
1327:22 we've done this before when we were playing around previously with using
1327:24 playing around previously with using dictionaries to store key value Pairs
1327:26 dictionaries to store key value Pairs and I'm going to propose that we store
1327:27 and I'm going to propose that we store the registrant as a dictionary why cuz
1327:29 the registrant as a dictionary why cuz I'm going to keep it simple like the
1327:30 I'm going to keep it simple like the name is going to be the student's name
1327:33 name is going to be the student's name sorry the key is going to be the
1327:34 sorry the key is going to be the student's name and the value is going to
1327:35 student's name and the value is going to be whatever sport they registered for so
1327:37 be whatever sport they registered for so David and soccer and Carter and
1327:39 David and soccer and Carter and basketball and so it kind of makes sense
1327:41 basketball and so it kind of makes sense for like a two column dictionary so to
1327:43 for like a two column dictionary so to speak as we often depict it on screen so
1327:46 speak as we often depict it on screen so how can I use this dictionary well let
1327:47 how can I use this dictionary well let me go ahead and do this down here under
1327:50 me go ahead and do this down here under SL register let me go ahead and
1327:53 SL register let me go ahead and initially do this how about we get the
1327:55 initially do this how about we get the user's name from request. form. Get and
1327:59 user's name from request. form. Get and set it equal to whatever the value of
1328:01 set it equal to whatever the value of name is and I'm not going to give a
1328:02 name is and I'm not going to give a default value now because I don't want
1328:03 default value now because I don't want to call the student world or something
1328:06 to call the student world or something strange like that I'm just going to
1328:07 strange like that I'm just going to assume for now that it's there let's
1328:09 assume for now that it's there let's then create another variable called
1328:10 then create another variable called Sport and do request. form doget quote
1328:13 Sport and do request. form doget quote unquote sport to get these students
1328:15 unquote sport to get these students Sport and then let's go ahead and do
1328:18 Sport and then let's go ahead and do this in the
1328:19 this in the registrant dictionary let's index into
1328:22 registrant dictionary let's index into it using the student's name and let's
1328:24 it using the student's name and let's set it equal to whatever the sport is so
1328:27 set it equal to whatever the sport is so I've got these variables just to keep my
1328:29 I've got these variables just to keep my code tidy and I'm now putting a key
1328:31 code tidy and I'm now putting a key value pair in that uh into that
1328:35 value pair in that uh into that dictionary all right well what do I want
1328:38 dictionary all right well what do I want to now do and I'll I'll go ahead and say
1328:41 to now do and I'll I'll go ahead and say success. HTML sure let's go ahead and do
1328:43 success. HTML sure let's go ahead and do that but now I think success. HTML means
1328:45 that but now I think success. HTML means that so let me go back to the form
1328:47 that so let me go back to the form reload let me type in David and soccer
1328:52 reload let me type in David and soccer register okay let me go back and say
1328:55 register okay let me go back and say Carter and basketball registered okay
1329:01 Carter and basketball registered okay now let's see what I want to do next how
1329:03 now let's see what I want to do next how about I go
1329:05 about I go into um Let me give myself another route
1329:08 into um Let me give myself another route and let's play around here so app. route
1329:11 and let's play around here so app. route let's give myself another third route
1329:14 let's give myself another third route called registrant whose purpose in life
1329:16 called registrant whose purpose in life is just to show me who all of those
1329:17 is just to show me who all of those registrants are just like you would
1329:19 registrants are just like you would expect from a website like this and then
1329:21 expect from a website like this and then let me Define a function called
1329:22 let me Define a function called registrants or anything else and then
1329:25 registrants or anything else and then let me return the rendering of a
1329:27 let me return the rendering of a template called
1329:29 template called registrant H ML and let me pass in this
1329:33 registrant H ML and let me pass in this is kind of neat I can do registrant
1329:35 is kind of neat I can do registrant equals registrant which again looks
1329:37 equals registrant which again looks weird but what am I doing I'm presuming
1329:39 weird but what am I doing I'm presuming to pass in a placeholder called
1329:41 to pass in a placeholder called registrant the value of which is this
1329:44 registrant the value of which is this dictionary that I've been collecting all
1329:46 dictionary that I've been collecting all of the registrations in so similar to
1329:48 of the registrations in so similar to the name placeholder before but it's a
1329:50 the name placeholder before but it's a little more powerful because now it's a
1329:51 little more powerful because now it's a whole dictionary not just a single
1329:53 whole dictionary not just a single string so I think now let me be creative
1329:56 string so I think now let me be creative here let me go into my
1329:59 here let me go into my templates under uh my templates folder
1330:03 templates under uh my templates folder and let's do this uh let's go into uh my
1330:06 and let's do this uh let's go into uh my terminal window let's create another
1330:08 terminal window let's create another template called registrant HTML that's
1330:11 template called registrant HTML that's actually going to do this displaying of
1330:14 actually going to do this displaying of all of the registrant for us so extends
1330:16 all of the registrant for us so extends layout. HTML just so I can borrow all of
1330:18 layout. HTML just so I can borrow all of the same HTML as
1330:20 the same HTML as before and let's define block body just
1330:23 before and let's define block body just like before and inside of this and block
1330:26 like before and inside of this and block I want to put I don't know like a
1330:28 I want to put I don't know like a bulleted list or an ordered list list of
1330:30 bulleted list or an ordered list list of all of the registrant so how can I do
1330:32 all of the registrant so how can I do this well let's do uh an unordered list
1330:35 this well let's do uh an unordered list ul and here's where Ginger and flask
1330:39 ul and here's where Ginger and flask more generally get kind of interesting
1330:41 more generally get kind of interesting like I want there to be something like
1330:43 like I want there to be something like this An
1330:44 this An Li uh and then like the students's name
1330:49 Li uh and then like the students's name and then an L uh maybe Li yeah like that
1330:53 and then an L uh maybe Li yeah like that and then maybe like sport something like
1330:55 and then maybe like sport something like this but I didn't pass in a name I
1330:57 this but I didn't pass in a name I didn't pass in a sport I passed in the
1331:00 didn't pass in a sport I passed in the entire dictionary of registrants now in
1331:02 entire dictionary of registrants now in Python if we were just doing something
1331:03 Python if we were just doing something at the black and white terminal window
1331:06 at the black and white terminal window and doing a command line program you
1331:08 and doing a command line program you know I'd probably have some kind of for
1331:09 know I'd probably have some kind of for Loop in Python Ginger does allow you to
1331:12 Loop in Python Ginger does allow you to do this so a templating language tends
1331:14 do this so a templating language tends to come with very lightweight mechanisms
1331:16 to come with very lightweight mechanisms for doing placeholders doing Simple
1331:18 for doing placeholders doing Simple Loops doing Simple conditions so python
1331:20 Loops doing Simple conditions so python like syntax and it's almost identical so
1331:23 like syntax and it's almost identical so watch what I can do inside of this
1331:25 watch what I can do inside of this unordered list let me not start to
1331:27 unordered list let me not start to manually output a single Li let me use
1331:30 manually output a single Li let me use this syntax the same Ginger syntax that
1331:33 this syntax the same Ginger syntax that I used for Block so curly brace percent
1331:36 I used for Block so curly brace percent sign and I'm going to say this for name
1331:38 sign and I'm going to say this for name in registrant so this is just like
1331:41 in registrant so this is just like python Syntax for iterating over a
1331:43 python Syntax for iterating over a dictionary and now this is going to look
1331:45 dictionary and now this is going to look stupid but the opposite of that is end4
1331:48 stupid but the opposite of that is end4 so in HTML you use the slash in ginger
1331:50 so in HTML you use the slash in ginger you literally use the word end no space
1331:52 you literally use the word end no space and then the name of the keyword so end4
1331:54 and then the name of the keyword so end4 is how you close this but this is where
1331:57 is how you close this but this is where templating gets really cool you can now
1331:59 templating gets really cool you can now do Li and in here I can do something
1332:03 do Li and in here I can do something like that student's name and that's it
1332:06 like that student's name and that's it I'm going to leave it like that and what
1332:08 I'm going to leave it like that and what I'm doing here is using really a
1332:10 I'm doing here is using really a template as templates are intended I've
1332:13 template as templates are intended I've got like the basic building blocks of
1332:15 got like the basic building blocks of what I want this output to look like but
1332:17 what I want this output to look like but thanks to this little for Loop here
1332:19 thanks to this little for Loop here thanks to Ginger syntax the curly brace
1332:21 thanks to Ginger syntax the curly brace and the percent sign I'm going to
1332:23 and the percent sign I'm going to iterate over every dictionary printing
1332:24 iterate over every dictionary printing out name name name name and so if I've
1332:27 out name name name name and so if I've got two kids registered now I'm going to
1332:28 got two kids registered now I'm going to see two liis David and Carter
1332:31 see two liis David and Carter respectively so let's see let me go back
1332:34 respectively so let's see let me go back to uh my Frost IM tab here and I don't
1332:39 to uh my Frost IM tab here and I don't have a link yet so I got to do this
1332:41 have a link yet so I got to do this manually uh like a developer would let
1332:43 manually uh like a developer would let me go to slash registrant and I'll zoom
1332:47 me go to slash registrant and I'll zoom out and hit
1332:48 out and hit enter and you'll see what you'll
1332:50 enter and you'll see what you'll probably see two when making mistakes
1332:52 probably see two when making mistakes for the first time in this world so
1332:54 for the first time in this world so where is the error message unfortunately
1332:56 where is the error message unfortunately internal Ser server error is not all
1332:58 internal Ser server error is not all that useful but we do tell you you see
1333:00 that useful but we do tell you you see terminal window so if I go to the
1333:02 terminal window so if I go to the terminal window I haven't been paying
1333:04 terminal window I haven't been paying attention to this for quite some time
1333:05 attention to this for quite some time and in fact I have two terminal windows
1333:07 and in fact I have two terminal windows open so that I can still use commands at
1333:09 open so that I can still use commands at the prompt but if I go back to my first
1333:11 the prompt but if I go back to my first terminal window AKA bash there you'll
1333:13 terminal window AKA bash there you'll see in your terminal window When
1333:15 see in your terminal window When developing web applications like all of
1333:17 developing web applications like all of the mistakes you made in the terminal
1333:20 the mistakes you made in the terminal itself this is one of those python
1333:21 itself this is one of those python tracebacks that's related to me screwing
1333:23 tracebacks that's related to me screwing up here now let me go ahead here and
1333:25 up here now let me go ahead here and let's see uh type error function is not
1333:29 let's see uh type error function is not iterable
1333:30 iterable and block for name function is not
1333:38 iterable all right so what mistake did I make well this is what happens when I
1333:40 make well this is what happens when I don't follow my notes and make changes
1333:41 don't follow my notes and make changes on the fly so I have this variable on
1333:44 on the fly so I have this variable on line five called registrant in all lower
1333:46 line five called registrant in all lower case but what did I then do on the Fly
1333:48 case but what did I then do on the Fly here in line
1333:54 22 I defined a function called registrant so like newbie mistake like I
1333:57 registrant so like newbie mistake like I shouldn't have done this I can't have a
1333:58 shouldn't have done this I can't have a variable and a function of the same name
1333:59 variable and a function of the same name name because the symbols are literally
1334:01 name because the symbols are literally identical so just to make clear that
1334:03 identical so just to make clear that this variable up here is actually Global
1334:05 this variable up here is actually Global we'll use our convention like we did in
1334:07 we'll use our convention like we did in C often when we had a global variable
1334:10 C often when we had a global variable we'll capitalize it all just to make it
1334:11 we'll capitalize it all just to make it stand out like a a constant value up
1334:14 stand out like a a constant value up there and so down here what I'm going to
1334:16 there and so down here what I'm going to do is pass in registrants in all cap so
1334:19 do is pass in registrants in all cap so that was stupid didn't mean to confuse
1334:21 that was stupid didn't mean to confuse there but the reason for that error to
1334:22 there but the reason for that error to be clear is that you can't have a
1334:24 be clear is that you can't have a function that's the same name as a
1334:25 function that's the same name as a variable I could just change the
1334:26 variable I could just change the variable name alt together I'm going to
1334:28 variable name alt together I'm going to go ahead and just capitalize it to make
1334:29 go ahead and just capitalize it to make it really stand out that this is in fact
1334:31 it really stand out that this is in fact a global variable up top all right now
1334:33 a global variable up top all right now I'm going to go back to my browser let's
1334:36 I'm going to go back to my browser let's do David and
1334:42 socer all right but there's going to be some other mistakes here so on line 17
1334:45 some other mistakes here so on line 17 let me go ahead and change this variable
1334:47 let me go ahead and change this variable to be capitalized there because indeed I
1334:49 to be capitalized there because indeed I want to put the key and the value in
1334:51 want to put the key and the value in this newly named variable as all
1334:53 this newly named variable as all capitals registrant let me now go back
1334:55 capitals registrant let me now go back to vs code here let me go back to the
1334:57 to vs code here let me go back to the form and let me start adding some data
1335:00 form and let me start adding some data fresh let me register David for soccer
1335:03 fresh let me register David for soccer clicking register now and we should see
1335:05 clicking register now and we should see you are registered but hopefully now
1335:07 you are registered but hopefully now it's indeed in the computer's memory let
1335:09 it's indeed in the computer's memory let me go back and register now Carter for
1335:12 me go back and register now Carter for basketball clicking register again and
1335:15 basketball clicking register again and hopefully it's now registered if I now
1335:17 hopefully it's now registered if I now change my route manually to B SL
1335:20 change my route manually to B SL registrant which is this newly added
1335:22 registrant which is this newly added route that I made and hit enter now I
1335:24 route that I made and hit enter now I see thank God now I see the unordered
1335:27 see thank God now I see the unordered list containing everything in the
1335:29 list containing everything in the computer's memory so when I say you are
1335:31 computer's memory so when I say you are registered I kind of mean it now because
1335:33 registered I kind of mean it now because the server is still running and in the
1335:35 the server is still running and in the computer's memory is in this registrant
1335:38 computer's memory is in this registrant Global variable a dictionary of key
1335:40 Global variable a dictionary of key value pairs of course we're only seeing
1335:42 value pairs of course we're only seeing the keys at the moment so it might be
1335:44 the keys at the moment so it might be nice to actually see the values in as
1335:47 nice to actually see the values in as well so let me go back to VSS code and
1335:50 well so let me go back to VSS code and let me go into registr trans. HTML and
1335:53 let me go into registr trans. HTML and I'll just do something a little messy
1335:55 I'll just do something a little messy I'll just say uh how about let just make
1335:57 I'll just say uh how about let just make it a sentence is registered
1336:00 it a sentence is registered four and now another placeholder I'm
1336:03 four and now another placeholder I'm going to say registrant bracket name so
1336:06 going to say registrant bracket name so just like in Python if registr is itself
1336:09 just like in Python if registr is itself a dictionary registr bracket and then
1336:12 a dictionary registr bracket and then the key you want to index into is
1336:15 the key you want to index into is perfectly valid syntax as well so now
1336:17 perfectly valid syntax as well so now let me go back to SL registrant let me
1336:21 let me go back to SL registrant let me click reload again so why isn't it
1336:23 click reload again so why isn't it working everyone what's the bug that I
1336:26 working everyone what's the bug that I introduced earlier if David is
1336:28 introduced earlier if David is registered for none and Carter is
1336:29 registered for none and Carter is registered for none but David and Carter
1336:31 registered for none but David and Carter are in the dictionary like that's a good
1336:33 are in the dictionary like that's a good thing so some of the data is in there so
1336:36 thing so some of the data is in there so why are there no Sports Associated well
1336:38 why are there no Sports Associated well the first thing I literally just did in
1336:40 the first thing I literally just did in front of you all was I went to app.py
1336:42 front of you all was I went to app.py and I stared at line 17 thinking like
1336:45 and I stared at line 17 thinking like how did I screw this up I'm putting
1336:47 how did I screw this up I'm putting sport as the value of the key which is
1336:49 sport as the value of the key which is the student's name all right line 17
1336:51 the student's name all right line 17 looked fine to me a few seconds ago so I
1336:53 looked fine to me a few seconds ago so I looked then with my eyes at line 16 and
1336:55 looked then with my eyes at line 16 and this too looked okay my first thought
1336:57 this too looked okay my first thought was oh did I use request. args instead
1336:59 was oh did I use request. args instead of request. form instead because that
1337:02 of request. form instead because that would have assumed get instead of post
1337:04 would have assumed get instead of post but no like that looks okay too so then
1337:06 but no like that looks okay too so then my final Instinct was oh my god did I
1337:08 my final Instinct was oh my god did I screw up the HTML form and so that's why
1337:11 screw up the HTML form and so that's why I went back over to my tab here I went
1337:13 I went back over to my tab here I went to the original form here I then view
1337:16 to the original form here I then view page source and this might not be as
1337:19 page source and this might not be as obvious to you if you've never seen the
1337:20 obvious to you if you've never seen the select menu before what is apparently
1337:24 select menu before what is apparently missing here that might explain my
1337:27 missing here that might explain my mistake yeah
1337:30 mistake yeah yeah I didn't name this form field quote
1337:32 yeah I didn't name this form field quote unquote sport now to be fair you haven't
1337:34 unquote sport now to be fair you haven't seen me do this as a select menu before
1337:36 seen me do this as a select menu before and it's different from this input when
1337:38 and it's different from this input when you have an input tag you literally say
1337:40 you have an input tag you literally say name equals whatever on the input tag it
1337:43 name equals whatever on the input tag it turns out I don't know why I skipped
1337:44 turns out I don't know why I skipped this earlier I probably meant to come
1337:46 this earlier I probably meant to come back to it the select tag also can take
1337:48 back to it the select tag also can take a name parameter so if I go back to the
1337:50 a name parameter so if I go back to the name
1337:51 name parameter here and go back and add the
1337:54 parameter here and go back and add the name parameter let me go into that
1337:56 name parameter let me go into that template which is
1337:57 template which is index.html let me add name equals quote
1338:00 index.html let me add name equals quote unquote sport in all lowercase which is
1338:03 unquote sport in all lowercase which is different from the visual aesthetic of
1338:05 different from the visual aesthetic of this temporary disabled option that's
1338:08 this temporary disabled option that's just there to make things prettier for
1338:09 just there to make things prettier for the human now let me go ahead here and
1338:13 the human now let me go ahead here and first I'm going to go into my terminal
1338:15 first I'm going to go into my terminal window and I'm actually going to hit
1338:17 window and I'm actually going to hit control C to stop the server alt
1338:19 control C to stop the server alt together because I want to throw away
1338:20 together because I want to throw away the contents of memory and therefore get
1338:22 the contents of memory and therefore get rid of that dictionary that had David
1338:24 rid of that dictionary that had David and Carter and those nun values so this
1338:26 and Carter and those nun values so this is sort of me clearing the computer's
1338:27 is sort of me clearing the computer's memory I'm going to rerun flask run I
1338:31 memory I'm going to rerun flask run I get that same URL as before so I'm going
1338:33 get that same URL as before so I'm going to hover over that and open the new tab
1338:35 to hover over that and open the new tab and just to be sure I'm going to do view
1338:36 and just to be sure I'm going to do view page source and here I see now okay now
1338:39 page source and here I see now okay now the form has both a name and a sport in
1338:42 the form has both a name and a sport in it all right now I'm really going to
1338:44 it all right now I'm really going to cross my fingers because I intend for
1338:46 cross my fingers because I intend for this now to work David will register
1338:48 this now to work David will register again for soccer register claims we are
1338:51 again for soccer register claims we are registered I'm going to go back and do
1338:53 registered I'm going to go back and do it again for Carter and basketball
1338:55 it again for Carter and basketball register we still don't have a link so
1338:57 register we still don't have a link so I'm going to manually go up to the URL
1339:01 I'm going to manually go up to the URL and change/ register to registrant as
1339:03 and change/ register to registrant as before zooming out and hit enter and
1339:06 before zooming out and hit enter and thank God now I'm actually registered
1339:09 thank God now I'm actually registered properly for this so oh thank
1339:17 you so what is it like 20 years later I'm still struggling to implement this
1339:18 I'm still struggling to implement this site okay so um so here now we have for
1339:22 site okay so um so here now we have for the first time in Python and web stuff
1339:24 the first time in Python and web stuff like now we have a proper web
1339:25 like now we have a proper web application and it's not just echoing
1339:27 application and it's not just echoing back hello David hello Carter this could
1339:29 back hello David hello Carter this could now work for any of you and it's
1339:30 now work for any of you and it's currently served privately but if I made
1339:32 currently served privately but if I made this URL public I could put this on the
1339:34 this URL public I could put this on the web now and let anyone in the world
1339:36 web now and let anyone in the world register but there's kind of some issues
1339:39 register but there's kind of some issues here there's some security flaws
1339:41 here there's some security flaws potentially and so for instance let me
1339:43 potentially and so for instance let me go back to the web form here and let me
1339:46 go back to the web form here and let me open up the inspect tab the developer
1339:48 open up the inspect tab the developer tools and just remind you that anyone on
1339:51 tools and just remind you that anyone on the internet not only you the developer
1339:52 the internet not only you the developer but a an adversary can see all of your
1339:55 but a an adversary can see all of your HTML see all of your CSS see all of your
1339:57 HTML see all of your CSS see all of your JavaScript but more importantly because
1339:59 JavaScript but more importantly because this is all client side in the browser
1340:02 this is all client side in the browser there is literally nothing technically
1340:04 there is literally nothing technically stopping them from changing the HTML or
1340:07 stopping them from changing the HTML or at least their copy of it and I did that
1340:08 at least their copy of it and I did that last week with Yale I changed their
1340:10 last week with Yale I changed their website but no I changed my copy of
1340:12 website but no I changed my copy of their website but when forms get
1340:14 their website but when forms get involved you could maybe be actually
1340:16 involved you could maybe be actually malicious now because even though this
1340:18 malicious now because even though this drop-down menu only has basketball
1340:22 drop-down menu only has basketball soccer and Ultimate Frisbee suppose I
1340:24 soccer and Ultimate Frisbee suppose I really want to register for how about uh
1340:28 really want to register for how about uh let's say uh name your favorite sport
1340:32 let's say uh name your favorite sport volleyball we really want to register
1340:34 volleyball we really want to register for volleyball but like this website
1340:36 for volleyball but like this website won't let me well there's nothing
1340:38 won't let me well there's nothing stopping me from going under the
1340:39 stopping me from going under the elements tab in my browser going into
1340:42 elements tab in my browser going into this select menu here and you know what
1340:44 this select menu here and you know what no one ultimate for me let's change this
1340:47 no one ultimate for me let's change this to volleyball and let's change this to
1340:50 to volleyball and let's change this to volleyball enter I'm going to close the
1340:53 volleyball enter I'm going to close the inspector now and as requested now we
1340:56 inspector now and as requested now we support volleyball in the form now now
1341:00 support volleyball in the form now now it's not changed on the server to be
1341:01 it's not changed on the server to be fair but think about how HTTP works when
1341:04 fair but think about how HTTP works when I fill out this with uh say let's see
1341:06 I fill out this with uh say let's see Bernie's name Bernie really wants to
1341:09 Bernie's name Bernie really wants to register for volleyball as well at the
1341:11 register for volleyball as well at the moment my code is just going to trust
1341:13 moment my code is just going to trust that what's in request. form is what was
1341:16 that what's in request. form is what was in the original form itself no matter
1341:19 in the original form itself no matter whether the human adversarially actually
1341:21 whether the human adversarially actually changed it so if I actually submit this
1341:23 changed it so if I actually submit this form and click register for Bernie and
1341:25 form and click register for Bernie and volleyball even though that's not one of
1341:27 volleyball even though that's not one of the supported available Sports if I now
1341:30 the supported available Sports if I now go to registrant my website nonetheless
1341:33 go to registrant my website nonetheless has trusted that uh Bernie and perhaps
1341:35 has trusted that uh Bernie and perhaps you are registered for volleyball so
1341:37 you are registered for volleyball so what's the implication of this this has
1341:39 what's the implication of this this has surely happened in the past when like
1341:40 surely happened in the past when like really poorly implemented websites um
1341:43 really poorly implemented websites um allow you to uh specify the price of an
1341:46 allow you to uh specify the price of an item for instance in your shopping cart
1341:47 item for instance in your shopping cart and they just trust that when you click
1341:49 and they just trust that when you click submit or add to cart it adds the price
1341:52 submit or add to cart it adds the price to the backend server if you're not
1341:54 to the backend server if you're not validating the price and making sure as
1341:56 validating the price and making sure as with a database that wait a minute that
1341:58 with a database that wait a minute that price is valid valid or wait a minute
1341:59 price is valid valid or wait a minute those sports are valid like who knows
1342:02 those sports are valid like who knows what people are going to do to your site
1342:03 what people are going to do to your site and it's that simple to actually hack a
1342:06 and it's that simple to actually hack a website accordingly now we can very
1342:08 website accordingly now we can very easily fix this with some um some week
1342:11 easily fix this with some um some week six style python we really just need to
1342:13 six style python we really just need to do a bit of logic here and so let me
1342:15 do a bit of logic here and so let me propose this let me go into app.py here
1342:18 propose this let me go into app.py here and at the very top let me also create
1342:21 and at the very top let me also create how about a uh Global variable called
1342:24 how about a uh Global variable called Sports in all caps and I'm going to set
1342:26 Sports in all caps and I'm going to set that equal to in square brackets the
1342:28 that equal to in square brackets the list of Sports I actually want to
1342:30 list of Sports I actually want to support so I'm going to put in
1342:31 support so I'm going to put in basketball here I'm going to put in
1342:33 basketball here I'm going to put in soccer here and I'm sorry no volleyball
1342:36 soccer here and I'm sorry no volleyball officially I'm going to put in ultimate
1342:37 officially I'm going to put in ultimate frisbee here so I've got this Global
1342:39 frisbee here so I've got this Global list of supported Sports now think about
1342:42 list of supported Sports now think about how I made this form a while ago I just
1342:44 how I made this form a while ago I just hardcoded these Sports here well I don't
1342:46 hardcoded these Sports here well I don't have to do that I can sort of draw upon
1342:49 have to do that I can sort of draw upon my own official list of sports instead
1342:51 my own official list of sports instead so let me scroll down to my
1342:54 so let me scroll down to my index.html rendering template here let
1342:56 index.html rendering template here let me say that the sports I want to support
1342:58 me say that the sports I want to support are are these so just using the same
1343:00 are are these so just using the same placeholder trick as before but I'm now
1343:03 placeholder trick as before but I'm now telling the template what sports we
1343:04 telling the template what sports we currently support now if I go back into
1343:08 currently support now if I go back into index.html I don't have to manually do
1343:10 index.html I don't have to manually do any of this let me get rid of all three
1343:12 any of this let me get rid of all three of those options which I manually
1343:14 of those options which I manually inputed earlier let me use my new trick
1343:16 inputed earlier let me use my new trick with ginger syntax and say for sport in
1343:20 with ginger syntax and say for sport in sports then let me proactively say end
1343:22 sports then let me proactively say end for just to finish that thought and then
1343:24 for just to finish that thought and then in here let me do option value equals in
1343:28 in here let me do option value equals in curly BRAC is Sport and then so that the
1343:30 curly BRAC is Sport and then so that the human also sees the same words I'm going
1343:33 human also sees the same words I'm going to say sport out here so I've completely
1343:35 to say sport out here so I've completely changed what was hardcoded manually
1343:38 changed what was hardcoded manually typed to something now that's completely
1343:39 typed to something now that's completely Dynamic so now it's not going to stop
1343:42 Dynamic so now it's not going to stop someone adversar like me from changing
1343:44 someone adversar like me from changing the HTML but watch this the behavior on
1343:46 the HTML but watch this the behavior on the form if we go back is still now the
1343:50 the form if we go back is still now the same drop down as before so
1343:52 same drop down as before so aesthetically it looks the same but you
1343:54 aesthetically it looks the same but you know what why don't we be clever now and
1343:55 know what why don't we be clever now and let's go into app.py and the/ register
1343:58 let's go into app.py and the/ register route and why don't we say this uh if
1344:02 route and why don't we say this uh if how about sport not in sports then let's
1344:07 how about sport not in sports then let's return render template uh failure. HTML
1344:12 return render template uh failure. HTML now this template doesn't exist yet so
1344:14 now this template doesn't exist yet so let me just quickly make this real fast
1344:15 let me just quickly make this real fast I'm going to copy that code from before
1344:18 I'm going to copy that code from before let me create a code file in uh failure.
1344:22 let me create a code file in uh failure. HTML I'm just going to paste this here
1344:24 HTML I'm just going to paste this here so I have a super simple error message
1344:26 so I have a super simple error message and I'm going to say you are not
1344:27 and I'm going to say you are not registered just to that's what we mean
1344:29 registered just to that's what we mean by failure and now in app.py consider
1344:32 by failure and now in app.py consider what logic I've added Sports in all caps
1344:35 what logic I've added Sports in all caps on line 22 is that same Global list as
1344:38 on line 22 is that same Global list as before by asking pythonic if sport not
1344:41 before by asking pythonic if sport not in sports well then you hacked me like
1344:44 in sports well then you hacked me like you tried to inject volleyball or some
1344:46 you tried to inject volleyball or some other sport into request. form so I'm
1344:49 other sport into request. form so I'm just going to say no failure not letting
1344:51 just going to say no failure not letting you register and I can do this a little
1344:54 you register and I can do this a little more uh verbosely too why don't I also
1344:57 more uh verbosely too why don't I also say this if if not name so if the name
1345:00 say this if if not name so if the name is blank let's similarly return a render
1345:03 is blank let's similarly return a render template of failure. HTML in other words
1345:06 template of failure. HTML in other words if you didn't give me a name you left it
1345:07 if you didn't give me a name you left it blank that's not useful for me running
1345:09 blank that's not useful for me running the sports program let's also consider
1345:11 the sports program let's also consider that to be a failure so if I go back to
1345:13 that to be a failure so if I go back to this tab now I'm going to reload just to
1345:15 this tab now I'm going to reload just to make sure I have the latest client side
1345:17 make sure I have the latest client side let me be lazy and just click register
1345:20 let me be lazy and just click register enter you are not registered because I
1345:22 enter you are not registered because I didn't give it an actual name all right
1345:24 didn't give it an actual name all right well let's go back how about I now type
1345:26 well let's go back how about I now type David but no no I'm not going to choose
1345:27 David but no no I'm not going to choose a sport I just want register myself nope
1345:31 a sport I just want register myself nope that did not work now let me go ahead
1345:33 that did not work now let me go ahead here and choose soccer this I think does
1345:37 here and choose soccer this I think does work let me go back now and try this
1345:39 work let me go back now and try this hacker trick whereby I go into the drop-
1345:42 hacker trick whereby I go into the drop- down menu I go into the select menu I
1345:46 down menu I go into the select menu I change as before Ultimate Frisbee to
1345:48 change as before Ultimate Frisbee to volleyball and I'll change this one here
1345:50 volleyball and I'll change this one here to
1345:52 to volleyball let me close the tab now this
1345:55 volleyball let me close the tab now this looks like it's available now but when I
1345:57 looks like it's available now but when I click register this this time it says
1346:00 click register this this time it says you are not registered and this is much
1346:03 you are not registered and this is much better than relying on other techniques
1346:05 better than relying on other techniques you might see or have seen online with
1346:07 you might see or have seen online with regard to HTML because there's also this
1346:08 regard to HTML because there's also this trick let me go back to the screen here
1346:11 trick let me go back to the screen here let me go back to index.html and you
1346:13 let me go back to index.html and you might have seen online or you might
1346:14 might have seen online or you might eventually see online that there's other
1346:16 eventually see online that there's other attributes you can use like required you
1346:18 attributes you can use like required you can literally tell the browser uhuh this
1346:20 can literally tell the browser uhuh this field is required you cannot leave it
1346:22 field is required you cannot leave it blank if I go back to the browser now
1346:24 blank if I go back to the browser now reload and I again presume to be lazy
1346:27 reload and I again presume to be lazy and I don't type in any name and click
1346:29 and I don't type in any name and click register okay so that's kind of nice
1346:31 register okay so that's kind of nice like now the browser is being a little
1346:33 like now the browser is being a little more helpful for me saying no no no this
1346:35 more helpful for me saying no no no this is required you have to fill this out
1346:38 is required you have to fill this out but again if you know what you're doing
1346:39 but again if you know what you're doing okay well I disagree with your requiring
1346:42 okay well I disagree with your requiring a name of me let me go in here let me go
1346:44 a name of me let me go in here let me go over to this tag let me delete the
1346:46 over to this tag let me delete the required attribute and now I slip
1346:51 required attribute and now I slip through but I didn't slip through on the
1346:52 through but I didn't slip through on the server and so there's a difference here
1346:54 server and so there's a difference here and an important distinction and so many
1346:56 and an important distinction and so many people in the real world still screw
1346:57 people in the real world still screw this up there's client side validation
1347:00 this up there's client side validation like actually checking that the data is
1347:01 like actually checking that the data is as you expect on the client side the
1347:03 as you expect on the client side the browser and there server side validation
1347:05 browser and there server side validation and even though client side validation
1347:07 and even though client side validation like adding that required attribute
1347:08 like adding that required attribute makes things more user friendly right
1347:10 makes things more user friendly right like that was a pretty little popup it
1347:11 like that was a pretty little popup it tells me that it's required it just
1347:12 tells me that it's required it just looks better than the previous version
1347:15 looks better than the previous version it is not trustable you cannot trust any
1347:18 it is not trustable you cannot trust any input that ever comes from the user
1347:20 input that ever comes from the user because clearly with like a an hour or
1347:22 because clearly with like a an hour or so of cs50 like they can learn how to
1347:24 so of cs50 like they can learn how to turn all of these defenses off so even
1347:26 turn all of these defenses off so even if you like the user interface better
1347:28 if you like the user interface better client side you have to have to have to
1347:31 client side you have to have to have to do server side validation always users
1347:33 do server side validation always users are not to be trusted and as soon as any
1347:35 are not to be trusted and as soon as any app or website you make becomes popular
1347:38 app or website you make becomes popular unfortunately then you have to deal with
1347:39 unfortunately then you have to deal with all of the adversarial possibilities as
1347:50 well oh good question could the adversary potentially access things
1347:52 adversary potentially access things sensitive like app.py theoretically no
1347:55 sensitive like app.py theoretically no like if flask itself is buggy then sure
1347:57 like if flask itself is buggy then sure maybe if you're running some other
1347:58 maybe if you're running some other software on your server on your laptop
1348:00 software on your server on your laptop then sure uh it it's possible however if
1348:03 then sure uh it it's possible however if your server is properly configured
1348:04 your server is properly configured theoretically they should not be able to
1348:06 theoretically they should not be able to get access to that with that said we'll
1348:08 get access to that with that said we'll soon see or You might with your final
1348:10 soon see or You might with your final project if you do something web based
1348:11 project if you do something web based you're never going to want to write like
1348:13 you're never going to want to write like usernames and passwords in your actual
1348:16 usernames and passwords in your actual code you can put them in what are called
1348:18 code you can put them in what are called environment variables so sort of in the
1348:19 environment variables so sort of in the computer's memory but not in your code
1348:21 computer's memory but not in your code just in case you or someone screws up
1348:24 just in case you or someone screws up there are uh still ways to defend
1348:26 there are uh still ways to defend against those kinds of possibilities
1348:28 against those kinds of possibilities however slim
1348:38 yeah a good question and this comes back to First principles just like in C and
1348:40 to First principles just like in C and in python as soon as you return from a
1348:42 in python as soon as you return from a function that's it nothing below that
1348:45 function that's it nothing below that line of code executes and so to
1348:47 line of code executes and so to summarize the question even though I'm
1348:49 summarize the question even though I'm returning this failure. HTML template
1348:51 returning this failure. HTML template how am I making sure we still don't
1348:53 how am I making sure we still don't accidentally put volleyball in that
1348:55 accidentally put volleyball in that Global dictionary it's because for
1348:57 Global dictionary it's because for instance if you don't give me a name on
1348:59 instance if you don't give me a name on line 22 at the moment I'm returning the
1349:01 line 22 at the moment I'm returning the failure template and that's it lines 23
1349:04 failure template and that's it lines 23 24 25 26 27 never execute in particular
1349:07 24 25 26 27 never execute in particular 26 never executes and that's where I
1349:09 26 never executes and that's where I would have been saving the name
1349:11 would have been saving the name similarly if we do we get a invalid
1349:14 similarly if we do we get a invalid sport that is either blank or not in the
1349:17 sport that is either blank or not in the original authoritative list we return
1349:20 original authoritative list we return failure. HTML is a template in line 25
1349:22 failure. HTML is a template in line 25 we never get to line 26 so it just boils
1349:25 we never get to line 26 so it just boils down to return and what that means in
1349:27 down to return and what that means in Python 2 good question other questions
1349:30 Python 2 good question other questions as
1349:32 as well no all right how about we make one
1349:35 well no all right how about we make one more enhancement here or so because the
1349:38 more enhancement here or so because the problem with storing everything in this
1349:40 problem with storing everything in this Global dictionary might be what we've
1349:45 Global dictionary might be what we've got it all working finally but why is
1349:48 got it all working finally but why is probably a global dictionary not the
1349:50 probably a global dictionary not the place to store Frost's registration
1349:53 place to store Frost's registration data what's the implication of
1349:57 data what's the implication of this okay might slow the whole process
1349:59 this okay might slow the whole process down but that actually Ram is actually
1350:01 down but that actually Ram is actually good memory is actually generally a good
1350:03 good memory is actually generally a good thing so not going to be a deal breaker
1350:05 thing so not going to be a deal breaker here why might I not want to store that
1350:07 here why might I not want to store that data though in that
1350:13 variable you can perhaps infer how I fix something
1350:15 something earlier yeah and
1350:18 earlier yeah and back yeah it gets it's the memory gets
1350:21 back yeah it gets it's the memory gets deleted garbage collected if you will as
1350:23 deleted garbage collected if you will as soon as flask stops running so if you so
1350:25 soon as flask stops running so if you so much as hit control C like you've just
1350:27 much as hit control C like you've just lost all of your freshmen who registered
1350:30 lost all of your freshmen who registered for the sport probably not a good thing
1350:32 for the sport probably not a good thing I did this deliberately a moment ago and
1350:33 I did this deliberately a moment ago and I hit control C because I did want to
1350:36 I hit control C because I did want to clear the dictionary but trusting that
1350:38 clear the dictionary but trusting that your server will never crash and your
1350:40 your server will never crash and your code will always work and the power will
1350:41 code will always work and the power will never go out like that's not the right
1350:43 never go out like that's not the right way to build any kind of web application
1350:45 way to build any kind of web application with persistent data so what we probably
1350:47 with persistent data so what we probably want to do is reintroduce csvs and we've
1350:49 want to do is reintroduce csvs and we've played with those in C and in Python
1350:51 played with those in C and in Python could totally use csvs but we also now
1350:53 could totally use csvs but we also now have p uh SQL at our disposal and let me
1350:56 have p uh SQL at our disposal and let me propose that we do this in SQL in
1350:58 propose that we do this in SQL in instead and for this let me go ahead and
1350:59 instead and for this let me go ahead and open up a version of the program that I
1351:01 open up a version of the program that I wrote in advance so let me go ahead and
1351:03 wrote in advance so let me go ahead and close these templates which will look
1351:05 close these templates which will look very similar but a little different from
1351:07 very similar but a little different from the ones I wrote in advance and let me
1351:09 the ones I wrote in advance and let me go ahead and open up uh in today's let
1351:12 go ahead and open up uh in today's let me go into Source 9 let me go into Frost
1351:15 me go into Source 9 let me go into Frost im's how about version four technically
1351:19 im's how about version four technically in the versions online and let me go
1351:21 in the versions online and let me go ahead and open up app.py as follows so
1351:23 ahead and open up app.py as follows so here is an already made version that
1351:25 here is an already made version that does just a little something different
1351:27 does just a little something different at the very top I'm importing cs50 SQL
1351:29 at the very top I'm importing cs50 SQL Library which you might recall we used a
1351:31 Library which you might recall we used a couple of weeks past just to write
1351:32 couple of weeks past just to write python that talks to a SQL database and
1351:34 python that talks to a SQL database and this feels like an opportune moment to
1351:36 this feels like an opportune moment to bring that idea back down here on line
1351:38 bring that idea back down here on line eight I'm creating a DB variable that
1351:40 eight I'm creating a DB variable that opens up a file called frost. DB using
1351:43 opens up a file called frost. DB using syntax that we've seen before I did
1351:45 syntax that we've seen before I did create this FRS DB file in advance of
1351:48 create this FRS DB file in advance of class just so that we have a couple of
1351:49 class just so that we have a couple of columns in which to store names and
1351:51 columns in which to store names and sports and such here's that same Global
1351:53 sports and such here's that same Global array a global list called Sports and
1351:56 array a global list called Sports and let's just see what's going on down
1351:58 let's just see what's going on down below if I scroll down to Index this is
1352:01 below if I scroll down to Index this is the same as before as we wrote together
1352:04 the same as before as we wrote together on the Fly let's skip deregister for a
1352:06 on the Fly let's skip deregister for a moment and go now into register so this
1352:08 moment and go now into register so this one's a little different but let's see
1352:10 one's a little different but let's see what I've done I've got some comments in
1352:11 what I've done I've got some comments in here because I wrote it in advance and I
1352:13 here because I wrote it in advance and I think this logic is pretty much the same
1352:14 think this logic is pretty much the same though I tightened it up and I'm asking
1352:16 though I tightened it up and I'm asking two questions at once using on line 38
1352:19 two questions at once using on line 38 the or keyword here just to say if
1352:21 the or keyword here just to say if there's not a name or the sport is not
1352:22 there's not a name or the sport is not in sports that is what we'll call now a
1352:24 in sports that is what we'll call now a failure but what's fun now is that on
1352:27 failure but what's fun now is that on line 42
1352:28 line 42 I'm using the cs50 SQL library to
1352:31 I'm using the cs50 SQL library to execute some actual SQL and I'm going to
1352:33 execute some actual SQL and I'm going to insert into a table called registrant
1352:36 insert into a table called registrant two columns name and Sport what names
1352:39 two columns name and Sport what names and Sport well these two values with
1352:41 and Sport well these two values with placeholders plugging in name and Sport
1352:43 placeholders plugging in name and Sport notice I'm using the question marks
1352:45 notice I'm using the question marks absolutely necessary so we don't get one
1352:47 absolutely necessary so we don't get one of those SQL injection attacks because
1352:48 of those SQL injection attacks because that too could be possible if someone
1352:50 that too could be possible if someone typed in some dangerous words or
1352:52 typed in some dangerous words or keywords like delete or single quotes or
1352:54 keywords like delete or single quotes or semicolons in the form here I'm letting
1352:56 semicolons in the form here I'm letting the library sanitize the data and then
1352:59 the library sanitize the data and then this is a trick we haven't yet seen and
1353:00 this is a trick we haven't yet seen and it's really going to start tying things
1353:02 it's really going to start tying things together I can also use a redirect
1353:05 together I can also use a redirect function in flask that has the effect of
1353:07 function in flask that has the effect of doing the the if you will uh
1353:09 doing the the if you will uh safetyschool.org trick again whereby
1353:12 safetyschool.org trick again whereby after the user registers if I want to
1353:14 after the user registers if I want to automatically show them now everyone who
1353:17 automatically show them now everyone who is registered atreg I don't have to
1353:19 is registered atreg I don't have to manually expect that they'll change the
1353:21 manually expect that they'll change the url like I've been doing for the past
1353:22 url like I've been doing for the past few minutes I can just redirect them
1353:25 few minutes I can just redirect them anywhere I want on my app or heck I
1353:27 anywhere I want on my app or heck I could redirect them to any URL on the
1353:29 could redirect them to any URL on the internet using this function call and
1353:31 internet using this function call and it's just a nice way to send them to a
1353:32 it's just a nice way to send them to a different route if you want them to see
1353:34 different route if you want them to see in this case those registrant so let me
1353:36 in this case those registrant so let me do this in the same directory let me
1353:39 do this in the same directory let me increase my terminal window size let me
1353:41 increase my terminal window size let me do SQL light three of frost. DB and let
1353:45 do SQL light three of frost. DB and let me type schema and you can indeed see it
1353:48 me type schema and you can indeed see it wraps on to two lines here that each
1353:51 wraps on to two lines here that each registrant has an ID which will be
1353:53 registrant has an ID which will be automatically assigned one two three on
1353:55 automatically assigned one two three on up a name which is not null text and a
1353:58 up a name which is not null text and a sport which is also the same and the
1354:00 sport which is also the same and the primary key is just going to be this
1354:01 primary key is just going to be this unique identifier so that I made in
1354:03 unique identifier so that I made in advance but if I do select star from
1354:06 advance but if I do select star from registrant semicolon there's no one
1354:08 registrant semicolon there's no one currently registered for any sports but
1354:11 currently registered for any sports but let's try now running this let me go
1354:13 let's try now running this let me go ahead and close my old version which we
1354:15 ahead and close my old version which we wrote together and I'll close that tab
1354:18 wrote together and I'll close that tab let me do flask run in this version four
1354:21 let me do flask run in this version four here all right I'm going to see some
1354:23 here all right I'm going to see some similar output I'm going to open the URL
1354:26 similar output I'm going to open the URL now and you'll see that I made a couple
1354:27 now and you'll see that I made a couple of Chang changes before instead of using
1354:29 of Chang changes before instead of using a select menu I used what are called
1354:32 a select menu I used what are called radio buttons now which is a reference
1354:34 radio buttons now which is a reference to Old School radio buttons that were
1354:35 to Old School radio buttons that were mutually exclusive in cars back in the
1354:37 mutually exclusive in cars back in the day and we'll see how to do this but
1354:39 day and we'll see how to do this but it's just an alternative to a select
1354:40 it's just an alternative to a select menu and I'm going to go ahead and type
1354:42 menu and I'm going to go ahead and type in my name again here so I'll do David
1354:46 in my name again here so I'll do David I'll do soccer by selecting this radio
1354:48 I'll do soccer by selecting this radio button and I'm going to click register
1354:50 button and I'm going to click register now and notice what happened it's a
1354:52 now and notice what happened it's a little ugly the formatting but so again
1354:55 little ugly the formatting but so again was this 20 years ago here I have now
1354:57 was this 20 years ago here I have now now at the slash registrant route
1355:00 now at the slash registrant route instead of an unordered list I'm just
1355:02 instead of an unordered list I'm just using a simple HTML table so I'll show
1355:04 using a simple HTML table so I'll show you what this looks like in just a
1355:06 you what this looks like in just a moment too and I'll show you this
1355:07 moment too and I'll show you this deregister button which is sort of
1355:09 deregister button which is sort of unnecessarily large I also have
1355:11 unnecessarily large I also have functionality we'll soon see for how you
1355:13 functionality we'll soon see for how you can unregister someone from a sport as
1355:15 can unregister someone from a sport as well so take your name out of contention
1355:18 well so take your name out of contention well let me go back to my terminal
1355:20 well let me go back to my terminal window here and I'm going to click the
1355:21 window here and I'm going to click the plus to give myself s a second terminal
1355:24 plus to give myself s a second terminal so I can go back into Source 9 Frost IMS
1355:26 so I can go back into Source 9 Frost IMS 4 I'm going to do SQL light of frost. DB
1355:31 4 I'm going to do SQL light of frost. DB I'm going to do select star from
1355:33 I'm going to do select star from registrant now and now you'll see that
1355:36 registrant now and now you'll see that indeed there's David registered for
1355:38 indeed there's David registered for soccer and in fact if I quit the flask
1355:42 soccer and in fact if I quit the flask program with control C and rerun it
1355:44 program with control C and rerun it again no big deal because that next
1355:46 again no big deal because that next version of flask will just use the
1355:48 version of flask will just use the database as well so I'm persisting
1355:50 database as well so I'm persisting keeping the data in SQL light whereas
1355:53 keeping the data in SQL light whereas I'm actually grabbing it using my python
1355:55 I'm actually grabbing it using my python code in flask all right let's put one
1355:57 code in flask all right let's put one more person in here so we can delete one
1355:59 more person in here so we can delete one of us too Carter for basketball register
1356:03 of us too Carter for basketball register and now we see both of us here all right
1356:05 and now we see both of us here all right so let's see how we did this let's go
1356:06 so let's see how we did this let's go back over to VSS code let me shrink down
1356:08 back over to VSS code let me shrink down my terminal window let me go into the
1356:13 my terminal window let me go into the actually let's go into the templates
1356:16 actually let's go into the templates directory and let's look at for instance
1356:19 directory and let's look at for instance index.html so previously we were using a
1356:22 index.html so previously we were using a select menu turns out radio buttons use
1356:25 select menu turns out radio buttons use the input tag but instead of having
1356:27 the input tag but instead of having input of uh type equals text like for
1356:30 input of uh type equals text like for the human's name you have type equals
1356:32 the human's name you have type equals radio and so long as each of your radio
1356:35 radio and so long as each of your radio buttons has the same name the same name
1356:37 buttons has the same name the same name the same name that's what makes them
1356:38 the same name that's what makes them mutually exclusive so checking one radio
1356:41 mutually exclusive so checking one radio button turns off the others because they
1356:43 button turns off the others because they have the same name the value I want to
1356:45 have the same name the value I want to assign to each of these radio buttons is
1356:47 assign to each of these radio buttons is just the sport placeholder this is what
1356:49 just the sport placeholder this is what the human sees on the screen so it's
1356:51 the human sees on the screen so it's almost the same as the select menu it
1356:52 almost the same as the select menu it just looks aesthetically different but
1356:54 just looks aesthetically different but there's my same button so that's all the
1356:56 there's my same button so that's all the difference I made there and I added a
1356:57 difference I made there and I added a heading tag H1 just to say register to
1357:00 heading tag H1 just to say register to make clear what it is but let's take a
1357:02 make clear what it is but let's take a look at another file uh this one now
1357:06 look at another file uh this one now being how about the SL registrant route
1357:09 being how about the SL registrant route so if I open up registr trans. HTML here
1357:13 so if I open up registr trans. HTML here now it's way more verbose than my
1357:15 now it's way more verbose than my unordered list but this is just kind of
1357:17 unordered list but this is just kind of boring HTML here's my table tag table
1357:19 boring HTML here's my table tag table head table row table heading this makes
1357:22 head table row table heading this makes things bold as the first row of the
1357:24 things bold as the first row of the table name sport are my two columns I've
1357:27 table name sport are my two columns I've got a third empty column just so I can
1357:28 got a third empty column just so I can fit that button as we'll soon see again
1357:31 fit that button as we'll soon see again T body for table body here's the same
1357:33 T body for table body here's the same for Loop trick again so that I can
1357:35 for Loop trick again so that I can output for every registrant a whole
1357:37 output for every registrant a whole table row and there's this weird form in
1357:40 table row and there's this weird form in there but we'll come back to that but
1357:41 there but we'll come back to that but there's the registrant's name there's
1357:43 there's the registrant's name there's the registrant sport but notice the
1357:46 the registrant sport but notice the slightly different syntax here recall
1357:49 slightly different syntax here recall that cs50's select uh cs50's execute
1357:53 that cs50's select uh cs50's execute function when it returns to you a list
1357:55 function when it returns to you a list of dictionaries you can then get at the
1357:58 of dictionaries you can then get at the individual columns by way of those keys
1358:00 individual columns by way of those keys so let's go to the/ registrant Route let
1358:03 so let's go to the/ registrant Route let me go back to app.py scroll down here
1358:05 me go back to app.py scroll down here and it's actually super simple here I
1358:08 and it's actually super simple here I have a SL registr route that first
1358:11 have a SL registr route that first executes select star from registrant so
1358:13 executes select star from registrant so just old SQL stuff give me everyone from
1358:15 just old SQL stuff give me everyone from the registr table let me then render the
1358:17 the registr table let me then render the template called registr trans. HTML and
1358:19 template called registr trans. HTML and just pass in this list of dictionaries
1358:23 just pass in this list of dictionaries and we haven't quite done this yet but
1358:24 and we haven't quite done this yet but if you go back to register. HTML how do
1358:27 if you go back to register. HTML how do you iterate over each dictionary in that
1358:30 you iterate over each dictionary in that list well the syntax is just for
1358:32 list well the syntax is just for registrant in registrants that makes
1358:34 registrant in registrants that makes this a dictionary one at a time in the
1358:37 this a dictionary one at a time in the list just like in Python so registrant
1358:40 list just like in Python so registrant name and registrants sport is just
1358:42 name and registrants sport is just another Syntax for using the square
1358:44 another Syntax for using the square bracket notation it's just a little
1358:47 bracket notation it's just a little cleaner and slightly more succinct than
1358:49 cleaner and slightly more succinct than having quotes and square brackets
1358:51 having quotes and square brackets everywhere and then the rest of this is
1358:53 everywhere and then the rest of this is just
1358:54 just HTML so what happens now if I want to uh
1358:58 HTML so what happens now if I want to uh like Carter has been cut from the
1358:59 like Carter has been cut from the basketball team if you will so how do we
1359:01 basketball team if you will so how do we do that well we want to click this
1359:03 do that well we want to click this button deregister next to Carter's name
1359:06 button deregister next to Carter's name but how does this work and think about
1359:08 but how does this work and think about now any website you visited that has
1359:10 now any website you visited that has something like a shopping cart uh where
1359:12 something like a shopping cart uh where you can remove things from your cart or
1359:14 you can remove things from your cart or update quantities or add more quantities
1359:17 update quantities or add more quantities to your shopping cart on Amazon or
1359:18 to your shopping cart on Amazon or anything else well let's actually look
1359:20 anything else well let's actually look at the HTML that my app has spit out
1359:23 at the HTML that my app has spit out let's actually look at this here and
1359:25 let's actually look at this here and we'll see the following we'll see that
1359:27 we'll see the following we'll see that we have here in the HTML that reached
1359:30 we have here in the HTML that reached the user not only is David in the first
1359:33 the user not only is David in the first column socer in the second notice that
1359:35 column socer in the second notice that my register. HTML form is also spitting
1359:38 my register. HTML form is also spitting out a tiny little web form of its own
1359:41 out a tiny little web form of its own it's ugly but I only care about its
1359:43 it's ugly but I only care about its functionality for now and notice what
1359:45 functionality for now and notice what I'm doing here every registrant in this
1359:48 I'm doing here every registrant in this database gets their very own deregister
1359:51 database gets their very own deregister button and that form has a button that
1359:53 button and that form has a button that says deregister but notice what else
1359:56 says deregister but notice what else each of those forms have there's no text
1359:58 each of those forms have there's no text box there's no drop down menu there's no
1359:59 box there's no drop down menu there's no radio buttons rather you have a hidden
1360:04 radio buttons rather you have a hidden input field here so there is a way with
1360:07 input field here so there is a way with HTML to have a form that will submit
1360:10 HTML to have a form that will submit information but you don't have to give
1360:12 information but you don't have to give the user the ability to change that
1360:14 the user the ability to change that information you can just go ahead and
1360:16 information you can just go ahead and tuck it inside of the form invisibly if
1360:18 tuck it inside of the form invisibly if you will hidden in fashion and so what's
1360:21 you will hidden in fashion and so what's going to happen is if I click the D
1360:23 going to happen is if I click the D register button next to Carter his
1360:26 register button next to Carter his primary key is two
1360:28 primary key is two mine is instead one so what's going to
1360:30 mine is instead one so what's going to happen if I click his deregister button
1360:33 happen if I click his deregister button it submits a form with a
1360:37 it submits a form with a ID parameter whose value is two and it
1360:40 ID parameter whose value is two and it submits it to the deregister route so
1360:43 submits it to the deregister route so what do that mean well if I go to VSS
1360:44 what do that mean well if I go to VSS code and I go to app.py let's look at
1360:47 code and I go to app.py let's look at the deregister route that I skipped over
1360:50 the deregister route that I skipped over so if you access the deregister route
1360:52 so if you access the deregister route via post this code gets called I grab
1360:56 via post this code gets called I grab from request form the ID that was
1360:58 from request form the ID that was submitted in Hidden fashion if there's
1361:01 submitted in Hidden fashion if there's indeed an ID that is it's not blank it's
1361:03 indeed an ID that is it's not blank it's not zero it's an actual number like one
1361:05 not zero it's an actual number like one 2 three or more I execute delete from
1361:08 2 three or more I execute delete from registrant where ID equals that value
1361:12 registrant where ID equals that value with a question mark Place holder and
1361:13 with a question mark Place holder and then I redirect the user back to
1361:16 then I redirect the user back to registrant now if I go back to this form
1361:18 registrant now if I go back to this form here I click deregister we'll see that
1361:21 here I click deregister we'll see that in action gone is now Carter and in fact
1361:24 in action gone is now Carter and in fact if I go back to my terminal window here
1361:26 if I go back to my terminal window here I open open up uh SQL light 3 of frost.
1361:30 I open open up uh SQL light 3 of frost. DB and rerun select star from registrant
1361:33 DB and rerun select star from registrant Carter is now gone so again using very
1361:36 Carter is now gone so again using very simple HTML forms you can get buttons
1361:39 simple HTML forms you can get buttons and links and other such UI mechanisms
1361:41 and links and other such UI mechanisms to like do things on the server that you
1361:43 to like do things on the server that you want but there is a danger here this now
1361:46 want but there is a danger here this now is really meant this example is like an
1361:48 is really meant this example is like an administrative website like it was some
1361:50 administrative website like it was some 20 years ago just for us internal staff
1361:53 20 years ago just for us internal staff to be doing things technically this is
1361:56 to be doing things technically this is dangerous what I've just done
1361:58 dangerous what I've just done two even though Carter's ID is two and
1362:02 two even though Carter's ID is two and hidden and mine is one in Hidden what
1362:05 hidden and mine is one in Hidden what could this allow an adversary to do if
1362:06 could this allow an adversary to do if they had admin access to the same
1362:14 site any thoughts
1362:16 thoughts yeah
1362:18 yeah yeah yeah they could change the value of
1362:20 yeah yeah they could change the value of that hidden attri by opening up chrom's
1362:22 that hidden attri by opening up chrom's like developer tools change the number
1362:24 like developer tools change the number in the HTML they could delete anyone
1362:26 in the HTML they could delete anyone deregister anyone want from the database
1362:28 deregister anyone want from the database now in this case I claim this is fine
1362:31 now in this case I claim this is fine because this is only meant for us staff
1362:32 because this is only meant for us staff who were running Sports back in the day
1362:34 who were running Sports back in the day but it's indeed a risk so wouldn't it be
1362:36 but it's indeed a risk so wouldn't it be nice if we could actually ensure that
1362:39 nice if we could actually ensure that only those users who are authorized are
1362:41 only those users who are authorized are allowed to execute certain actions I
1362:44 allowed to execute certain actions I think for this capability we're actually
1362:46 think for this capability we're actually going to need to introduce something a
1362:49 going to need to introduce something a bit more and so here of course is an
1362:51 bit more and so here of course is an opportunity to talk briefly about really
1362:53 opportunity to talk briefly about really what you and I do all day long every day
1362:55 what you and I do all day long every day we log into one or more websites or apps
1362:58 we log into one or more websites or apps or at least until uh you're logged out
1363:00 or at least until uh you're logged out automatically and you have to do it
1363:01 automatically and you have to do it again so here for instance is a
1363:02 again so here for instance is a screenshot of Gmail when you type in
1363:04 screenshot of Gmail when you type in your username you type in your password
1363:06 your username you type in your password maybe your to factor code that gets
1363:08 maybe your to factor code that gets texted or sent to your phone then you're
1363:10 texted or sent to your phone then you're logged in and thankfully you're not
1363:12 logged in and thankfully you're not prompted to log in again typically for
1363:13 prompted to log in again typically for number of hours or days or weeks
1363:16 number of hours or days or weeks depending on the website like Gmail
1363:18 depending on the website like Gmail keeps you logged in for ages your bank
1363:20 keeps you logged in for ages your bank probably logs you out within an hour or
1363:22 probably logs you out within an hour or so for safety sake so that is completely
1363:24 so for safety sake so that is completely configurable on the server but how does
1363:26 configurable on the server but how does g Emil know how does Google know that
1363:29 g Emil know how does Google know that even as you're checking different mails
1363:31 even as you're checking different mails again and again and again how do they
1363:33 again and again and again how do they know that you're still the same person
1363:34 know that you're still the same person who logged in well it turns out that
1363:37 who logged in well it turns out that using these same building blocks as
1363:38 using these same building blocks as today HTTP and HTML and more you can
1363:42 today HTTP and HTML and more you can actually implement the notion of a login
1363:44 actually implement the notion of a login feature by doing the equivalent of
1363:47 feature by doing the equivalent of something with something called cookies
1363:49 something with something called cookies essentially what happens when you first
1363:51 essentially what happens when you first log into a website for the very first
1363:53 log into a website for the very first time successfully with your username and
1363:55 time successfully with your username and password a cookie so to speak is planted
1363:57 password a cookie so to speak is planted on your computer and metaphorically this
1363:59 on your computer and metaphorically this is kind of like taking a hand stamp and
1364:01 is kind of like taking a hand stamp and your hand is now stamped in this case a
1364:03 your hand is now stamped in this case a smiley face so that every other time you
1364:05 smiley face so that every other time you click on a link on that same website
1364:07 click on a link on that same website google.com gmail.com whatever
1364:09 google.com gmail.com whatever unbeknownst to you your browser is
1364:11 unbeknownst to you your browser is constantly presenting that handstamp
1364:13 constantly presenting that handstamp just like going into a club or an
1364:15 just like going into a club or an amusement park or something like that
1364:17 amusement park or something like that where they don't want to check your ID
1364:18 where they don't want to check your ID again they don't want to check your
1364:19 again they don't want to check your ticket again they just want to quickly
1364:20 ticket again they just want to quickly see the same hand stamp and so that is
1364:23 see the same hand stamp and so that is one of the things that a browser is
1364:24 one of the things that a browser is always doing for you once you're logged
1364:26 always doing for you once you're logged in any cookies that have been planted so
1364:29 in any cookies that have been planted so to speak in your Mac or PC or phone are
1364:31 to speak in your Mac or PC or phone are constantly represented to the site every
1364:33 constantly represented to the site every time you click a link or make another
1364:35 time you click a link or make another request to that website and mechanically
1364:38 request to that website and mechanically how this works not just metaphorically
1364:40 how this works not just metaphorically and ink essentially this is what happens
1364:42 and ink essentially this is what happens here is a example of an HTTP request to
1364:45 here is a example of an HTTP request to something like Gmail and suppose for
1364:47 something like Gmail and suppose for instance that you've logged in typically
1364:50 instance that you've logged in typically as of last week we said that coming back
1364:52 as of last week we said that coming back from the server would be another virtual
1364:54 from the server would be another virtual envelope containing like a 200 okay
1364:56 envelope containing like a 200 okay message and then like the actual web
1364:58 message and then like the actual web page or the picture of a cat or whatever
1365:00 page or the picture of a cat or whatever it may be but Google can also if they
1365:03 it may be but Google can also if they verified that you have some username and
1365:05 verified that you have some username and password correctly inputed they can do
1365:07 password correctly inputed they can do the equivalent of stamping your hand and
1365:09 the equivalent of stamping your hand and the way they do this is they send an
1365:11 the way they do this is they send an additional line of text in that virtual
1365:13 additional line of text in that virtual envelope from the server to your browser
1365:16 envelope from the server to your browser literally using another HTTP header not
1365:20 literally using another HTTP header not content type which just mundanely tells
1365:22 content type which just mundanely tells you what kind of content has come back
1365:24 you what kind of content has come back they literally send an http header
1365:27 they literally send an http header called set-cookie and then they set a
1365:31 called set-cookie and then they set a key value pair on your Mac or PC this is
1365:33 key value pair on your Mac or PC this is the technical equivalent of this smiley
1365:35 the technical equivalent of this smiley face handstamp and what your computer is
1365:38 face handstamp and what your computer is designed to do because your computer and
1365:40 designed to do because your computer and intern browser are supposed to implement
1365:43 intern browser are supposed to implement HTTP anytime you click another link on
1365:46 HTTP anytime you click another link on Gmail or click on another mail or the
1365:48 Gmail or click on another mail or the like your browser unbeknownst to you
1365:50 like your browser unbeknownst to you presents that handstamp and how it does
1365:52 presents that handstamp and how it does it technically is in the envelope it
1365:54 it technically is in the envelope it sends to Google from your browser it
1365:57 sends to Google from your browser it doesn't send set cookie it just sends
1365:59 doesn't send set cookie it just sends cookie colon and the exact same thing
1366:02 cookie colon and the exact same thing and so long as Google is smart and they
1366:04 and so long as Google is smart and they have a database or something of all of
1366:07 have a database or something of all of the session Valu session is the
1366:09 the session Valu session is the technical term for this maintenance of
1366:11 technical term for this maintenance of information across HTTP so long as
1366:14 information across HTTP so long as Google has a big database that knows
1366:15 Google has a big database that knows that my cookie value is one 123 and your
1366:18 that my cookie value is one 123 and your cookie value is 456 they can infer from
1366:21 cookie value is 456 they can infer from this virtual handstamp whose emails they
1366:25 this virtual handstamp whose emails they should be showing mine or yours or the
1366:27 should be showing mine or yours or the like so this is just scratching the
1366:29 like so this is just scratching the surface but if I really wanted to enable
1366:31 surface but if I really wanted to enable only Carter to deregister himself I just
1366:34 only Carter to deregister himself I just have to make sure that I log him in
1366:36 have to make sure that I log him in somehow username password and all of
1366:38 somehow username password and all of that I stamp his hand or really put a
1366:41 that I stamp his hand or really put a cookie on his computer and only if his
1366:43 cookie on his computer and only if his cookie lines up with the user ID he's
1366:46 cookie lines up with the user ID he's trying to deregister should he be
1366:48 trying to deregister should he be allowed to in fact do so so all of this
1366:51 allowed to in fact do so so all of this is quite possible and indeed the
1366:53 is quite possible and indeed the technical term for this is session and
1366:55 technical term for this is session and what we thought we'd do in our remaining
1366:56 what we thought we'd do in our remaining time Today show you some examples of
1366:59 time Today show you some examples of exactly how some of the most familiar
1367:01 exactly how some of the most familiar web functionality is implemented today
1367:03 web functionality is implemented today some of which you'll use in your own uh
1367:05 some of which you'll use in your own uh problem set 9 uh which itself will be a
1367:07 problem set 9 uh which itself will be a web app or perhaps even your final
1367:09 web app or perhaps even your final project so let me go ahead and do this
1367:11 project so let me go ahead and do this let me close my previous tabs and all
1367:13 let me close my previous tabs and all things for ashs and let's move on to
1367:16 things for ashs and let's move on to implementing some notion of login so in
1367:18 implementing some notion of login so in just a moment I'll switch over here to
1367:20 just a moment I'll switch over here to VSS code and what I'm going to do is
1367:23 VSS code and what I'm going to do is indeed in my source 9 directory I'm
1367:25 indeed in my source 9 directory I'm going to go into a login directory and
1367:27 going to go into a login directory and if I type LS here you'll see app.py
1367:30 if I type LS here you'll see app.py requirements. text which just refers to
1367:32 requirements. text which just refers to libraries I want to automatically
1367:34 libraries I want to automatically install and a templates folder as well
1367:36 install and a templates folder as well I'm going to go ahead and stop the
1367:38 I'm going to go ahead and stop the previous server and close that terminal
1367:39 previous server and close that terminal window and I'm going to open up this
1367:41 window and I'm going to open up this version of
1367:42 version of app.py so there's a few new lines here
1367:45 app.py so there's a few new lines here and we'll give you these lines for
1367:46 and we'll give you these lines for problems at nine but I've got some of
1367:48 problems at nine but I've got some of the familiar stuff up here including
1367:50 the familiar stuff up here including this new redirect function we just used
1367:52 this new redirect function we just used and I have a session variable that comes
1367:54 and I have a session variable that comes with flask 2 so what's nice again about
1367:57 with flask 2 so what's nice again about flask is that it deals with all of this
1367:58 flask is that it deals with all of this cookie stuff for you it sets the cookie
1368:01 cookie stuff for you it sets the cookie it checks the cookie and what flask does
1368:03 it checks the cookie and what flask does for you is it gives you the abstraction
1368:06 for you is it gives you the abstraction of a variable called session so that
1368:09 of a variable called session so that anything you put in the session variable
1368:11 anything you put in the session variable which itself is a dictionary will be
1368:13 which itself is a dictionary will be there again and again and again whenever
1368:15 there again and again and again whenever that same user comes back a session is
1368:17 that same user comes back a session is how you implement essentially like the
1368:19 how you implement essentially like the proverbial shopping cart uh if I'm
1368:22 proverbial shopping cart uh if I'm logged into Amazon you're logged into
1368:24 logged into Amazon you're logged into Amazon Amazon knows which of us is which
1368:27 Amazon Amazon knows which of us is which by way of that cookie and Amazon if
1368:29 by way of that cookie and Amazon if they're using flask provides the
1368:31 they're using flask provides the programmer with a dictionary called
1368:33 programmer with a dictionary called session and flasks make sure that when
1368:35 session and flasks make sure that when Carter is visiting the site the code
1368:37 Carter is visiting the site the code uses his session object when I'm
1368:39 uses his session object when I'm visiting the site it uses my session
1368:42 visiting the site it uses my session object but it's all implemented with
1368:43 object but it's all implemented with those same cookies this is the same as
1368:45 those same cookies this is the same as before these lines are new and you'll
1368:47 before these lines are new and you'll see these in problem Set n this is how
1368:49 see these in problem Set n this is how we enable sessions in a web application
1368:52 we enable sessions in a web application and I'll just wave my hands at the
1368:53 and I'll just wave my hands at the detail there's different ways to
1368:55 detail there's different ways to implement sessions whether you use
1368:57 implement sessions whether you use cookies on the server cookies on the
1368:59 cookies on the server cookies on the browser or other things these just
1369:01 browser or other things these just ensure that we're storing the the
1369:03 ensure that we're storing the the session information the shopping cart on
1369:05 session information the shopping cart on the server itself now down here let's go
1369:08 the server itself now down here let's go ahead and do this let's go ahead and run
1369:10 ahead and do this let's go ahead and run flask run so I can see what this app
1369:14 flask run so I can see what this app does if I do this and visit the URL that
1369:17 does if I do this and visit the URL that gets outputed you'll see a very simple
1369:19 gets outputed you'll see a very simple web page here and if I type in for
1369:21 web page here and if I type in for instance my name I'm not going to bother
1369:23 instance my name I'm not going to bother with a password and click log in you'll
1369:25 with a password and click log in you'll see that you are logged in as David and
1369:28 see that you are logged in as David and now I can log out so I'm going to go
1369:30 now I can log out so I'm going to go ahead and click log out and now it seems
1369:32 ahead and click log out and now it seems to know that I'm not logged in again I
1369:35 to know that I'm not logged in again I can log in as Carter because I didn't
1369:36 can log in as Carter because I didn't bother implementing passwords for
1369:37 bother implementing passwords for Simplicity but now the site knows I'm
1369:39 Simplicity but now the site knows I'm logged in as Carter better yet if I
1369:41 logged in as Carter better yet if I reload reload reload or click this
1369:44 reload reload reload or click this button again and again notice it still
1369:46 button again and again notice it still knows that I'm Carter uh until such time
1369:49 knows that I'm Carter uh until such time as I log out all right well how is this
1369:52 as I log out all right well how is this working well let's go back to vs code
1369:54 working well let's go back to vs code here and let me scroll down to first
1369:57 here and let me scroll down to first this route this is a very common
1369:59 this route this is a very common Paradigm here whereby I'm checking for
1370:01 Paradigm here whereby I'm checking for the index route if there is not a name
1370:05 the index route if there is not a name in the session redirect the user to SL
1370:07 in the session redirect the user to SL login now what does that mean well let
1370:09 login now what does that mean well let me go back to vs code here let me go to
1370:13 me go back to vs code here let me go to the slash route so again your url will
1370:15 the slash route so again your url will be different but I'm just going to go to
1370:17 be different but I'm just going to go to slash and hit enter notice that I got
1370:19 slash and hit enter notice that I got automatically redirected to login and so
1370:21 automatically redirected to login and so many websites do this if you go to a
1370:23 many websites do this if you go to a website and you're not logged in you're
1370:24 website and you're not logged in you're very often redirected to SL login or SL
1370:27 very often redirected to SL login or SL account or something like that where
1370:29 account or something like that where you're prompted the code for doing that
1370:31 you're prompted the code for doing that is right here if there's no name in the
1370:33 is right here if there's no name in the session if there's no name in the
1370:35 session if there's no name in the shopping cart if you will go ahead and
1370:37 shopping cart if you will go ahead and return the user to login that route
1370:40 return the user to login that route otherwise implicitly if they are logged
1370:42 otherwise implicitly if they are logged in show them index.html so let's go down
1370:45 in show them index.html so let's go down that rabbit hole let me open up in VSS
1370:47 that rabbit hole let me open up in VSS code a second terminal window let me go
1370:49 code a second terminal window let me go into Source 9 and go into this same
1370:52 into Source 9 and go into this same login demonstration and let me open up
1370:55 login demonstration and let me open up the template called
1370:58 the template called index.html and here's all this is
1371:00 index.html and here's all this is there's some layout but who cares at
1371:01 there's some layout but who cares at this point it's just the boiler plate
1371:03 this point it's just the boiler plate generic HTML here's the body block I
1371:06 generic HTML here's the body block I have this and this two is ginger because
1371:08 have this and this two is ginger because of the curly brace and the percent sign
1371:12 of the curly brace and the percent sign if there's a name in the session this is
1371:14 if there's a name in the session this is just python syntax then say this
1371:17 just python syntax then say this sentence you are logged in as whatever
1371:19 sentence you are logged in as whatever name is in the session in the shopping
1371:21 name is in the session in the shopping cart if you will and then I just have
1371:22 cart if you will and then I just have this HTML link for logging the user out
1371:25 this HTML link for logging the user out else if there is no name in the session
1371:28 else if there is no name in the session logically just say you are not logged in
1371:30 logically just say you are not logged in and give them a manual link for logging
1371:32 and give them a manual link for logging in instead so that's all this particular
1371:34 in instead so that's all this particular template does but how does the slash
1371:37 template does but how does the slash login work well let's go into this other
1371:40 login work well let's go into this other template code of login. HTML to which
1371:42 template code of login. HTML to which I'm redirected super simple this is just
1371:45 I'm redirected super simple this is just copy paste from HTML before I've got a
1371:47 copy paste from HTML before I've got a login form that's going to have an
1371:48 login form that's going to have an action of Slash login submits for
1371:51 action of Slash login submits for privacy sake just via post and then the
1371:54 privacy sake just via post and then the rest of this is just a simple form and
1371:56 rest of this is just a simple form and I'm using using an input type equals
1371:57 I'm using using an input type equals submit instead of button equals type
1371:59 submit instead of button equals type equals submit but same idea here too and
1372:01 equals submit but same idea here too and if I go back to app.py well let's see
1372:03 if I go back to app.py well let's see how login works all right it's a lot all
1372:06 how login works all right it's a lot all at once but they're relatively simple
1372:07 at once but they're relatively simple reapplications of the same idea so if
1372:10 reapplications of the same idea so if the user visits SL login via get or post
1372:14 the user visits SL login via get or post call this function login if the user has
1372:17 call this function login if the user has submitted via post and we saw this
1372:19 submitted via post and we saw this technique before go ahead and do this on
1372:21 technique before go ahead and do this on line 23 store in this special session
1372:25 line 23 store in this special session variable that comes with flask a name
1372:28 variable that comes with flask a name key and store in it the user's own name
1372:32 key and store in it the user's own name so quote unquote name will have a value
1372:34 so quote unquote name will have a value of David or Carter or the like and as
1372:36 of David or Carter or the like and as soon as you do that redirect the user
1372:38 soon as you do that redirect the user back to slash just so they see the
1372:40 back to slash just so they see the homepage again and this is how Amazon
1372:42 homepage again and this is how Amazon and all these other websites work too
1372:44 and all these other websites work too otherwise if they visit this page
1372:46 otherwise if they visit this page implicitly via get and even though I
1372:48 implicitly via get and even though I didn't say equals equals get anywhere
1372:50 didn't say equals equals get anywhere that's sort of the implication because
1372:52 that's sort of the implication because if you can only get here via get or post
1372:54 if you can only get here via get or post and we already handled post logically
1372:56 and we already handled post logically All That Remains is get well then just
1372:58 All That Remains is get well then just show them the login screen instead but
1373:01 show them the login screen instead but there's half a dozen ways we could
1373:02 there's half a dozen ways we could express that same logic and then for log
1373:05 express that same logic and then for log out this is kind of straightforward if
1373:07 out this is kind of straightforward if the user clicks that log out link and
1373:09 the user clicks that log out link and ends up at SL logout this route well
1373:11 ends up at SL logout this route well just change the value of that key in the
1373:14 just change the value of that key in the session to be none effectively no Carter
1373:18 session to be none effectively no Carter is gone David's gone there's no one
1373:20 is gone David's gone there's no one logged in so that is all that's required
1373:23 logged in so that is all that's required to actually implement the notion of
1373:25 to actually implement the notion of logging in and logging out of website
1373:26 logging in and logging out of website plus the password thing which should
1373:27 plus the password thing which should probably involve a database but one
1373:29 probably involve a database but one thing at a time and really session is
1373:31 thing at a time and really session is sort of like the code version of a
1373:33 sort of like the code version of a shopping cart whereby if I visit the
1373:35 shopping cart whereby if I visit the same code I get my own session object if
1373:38 same code I get my own session object if Carter visits the website he gets his
1373:39 Carter visits the website he gets his own session object and the way flask
1373:41 own session object and the way flask Keeps Us straight is they put one cookie
1373:44 Keeps Us straight is they put one cookie on my computer a different cookie on his
1373:46 on my computer a different cookie on his computer and uses those to line up with
1373:49 computer and uses those to line up with making sure the right session gets shown
1373:51 making sure the right session gets shown to the right actual
1373:54 to the right actual user questions on this notion of
1374:03 sessions no all right how about a couple final examples just to tie this all
1374:05 final examples just to tie this all together let me go back into vs code
1374:08 together let me go back into vs code here let me quit my previous version of
1374:10 here let me quit my previous version of flask let's go into Source 9 and go into
1374:13 flask let's go into Source 9 and go into store which is a separate app Al
1374:15 store which is a separate app Al together and let's start by just running
1374:16 together and let's start by just running flask to see what it does let's hover
1374:19 flask to see what it does let's hover over the URL and open it in another Tab
1374:21 over the URL and open it in another Tab and this is pretty ugly too let me zoom
1374:23 and this is pretty ugly too let me zoom in but it's a very simple bookstore like
1374:27 in but it's a very simple bookstore like an early amazon.com for each of these
1374:29 an early amazon.com for each of these seven books here Each of which seems to
1374:31 seven books here Each of which seems to be like maybe this is H1 this is H2 H2
1374:33 be like maybe this is H1 this is H2 H2 H2 H2 and then there's a button
1374:36 H2 H2 and then there's a button underneath each well now let's use this
1374:38 underneath each well now let's use this as an opportunity to kind of infer like
1374:40 as an opportunity to kind of infer like for any website how this thing works let
1374:42 for any website how this thing works let me go ahead and do view page source and
1374:44 me go ahead and do view page source and you can do this for any website on the
1374:46 you can do this for any website on the internet let's try to figure out how
1374:49 internet let's try to figure out how this bookstore adds things to a cart
1374:51 this bookstore adds things to a cart well here's the H1 tag uninteresting H2
1374:54 well here's the H1 tag uninteresting H2 H2 H2 so the juicy part is in these
1374:56 H2 H2 so the juicy part is in these forms each of these forms has an action
1375:00 forms each of these forms has an action of SL cart so that's the route that's
1375:02 of SL cart so that's the route that's going to be interesting in a moment and
1375:03 going to be interesting in a moment and it uses post for privacy sake each of
1375:06 it uses post for privacy sake each of these forms like the deregister feature
1375:08 these forms like the deregister feature for Carter has an ID attribute an ID
1375:12 for Carter has an ID attribute an ID parameter that's hidden visually that
1375:15 parameter that's hidden visually that has a value of one or two or three so
1375:18 has a value of one or two or three so like the unique uh uh like barcodes for
1375:21 like the unique uh uh like barcodes for the books if you will but super small
1375:22 the books if you will but super small numbers in our case and then each of
1375:24 numbers in our case and then each of these other forms just had each of these
1375:26 these other forms just had each of these other books has an identical form except
1375:28 other books has an identical form except for the value of this year now in this
1375:31 for the value of this year now in this case this isn't such a big deal that a
1375:33 case this isn't such a big deal that a user could technically hack the HTML of
1375:35 user could technically hack the HTML of this bookstore amazon.com and change the
1375:38 this bookstore amazon.com and change the IDS because whoa what's the worst
1375:39 IDS because whoa what's the worst they're going to do like buy more books
1375:41 they're going to do like buy more books by adding more IDs to their shopping
1375:43 by adding more IDs to their shopping cart like that's not a problem there's
1375:45 cart like that's not a problem there's no prices here it's just the unique
1375:47 no prices here it's just the unique ideas of books so whereas the deregister
1375:49 ideas of books so whereas the deregister was maybe worrisome because you're
1375:51 was maybe worrisome because you're changing the server I think it's okay
1375:53 changing the server I think it's okay because the user can only at worst buy
1375:55 because the user can only at worst buy more books than then uh they might via
1375:57 more books than then uh they might via the buttons alone so how does this now
1376:00 the buttons alone so how does this now work well let's go into vs code again
1376:03 work well let's go into vs code again here let me give myself another terminal
1376:05 here let me give myself another terminal window and in Source 9/ store let me
1376:08 window and in Source 9/ store let me open up app.py which is where all the
1376:10 open up app.py which is where all the logic is so I'll flip through most of
1376:12 logic is so I'll flip through most of this quickly because we've seen this
1376:14 this quickly because we've seen this before these Imports are pretty much the
1376:16 before these Imports are pretty much the same as before this line is the same
1376:18 same as before this line is the same this line is almost the same but the
1376:20 this line is almost the same but the database now is called store. DB instead
1376:22 database now is called store. DB instead of frost. DB this is the boilerplate
1376:25 of frost. DB this is the boilerplate code for just enabling sessions this
1376:27 code for just enabling sessions this notion of a shopping cart and so let's
1376:29 notion of a shopping cart and so let's see how the index Works how is that I'm
1376:31 see how the index Works how is that I'm seeing all seven books at once well in
1376:34 seeing all seven books at once well in this index function I'm using on line 19
1376:37 this index function I'm using on line 19 select star from books to get all the
1376:38 select star from books to get all the books from the database and then I'm
1376:40 books from the database and then I'm rendering a template called books. HTML
1376:43 rendering a template called books. HTML passing in as a placeholder all of those
1376:45 passing in as a placeholder all of those books all right let's go down that
1376:46 books all right let's go down that rabbit hole for a second let me open up
1376:48 rabbit hole for a second let me open up books. HTML in my templates directory
1376:51 books. HTML in my templates directory and here again even though it's you know
1376:53 and here again even though it's you know new today it's probably increasingly
1376:55 new today it's probably increasingly familiar syntactically here's the H1
1376:59 familiar syntactically here's the H1 here is my Loop here's the H2 which is
1377:01 here is my Loop here's the H2 which is going to Output the current book's title
1377:03 going to Output the current book's title in that Loop here's the form here's the
1377:06 in that Loop here's the form here's the ID of that book that's going to be
1377:07 ID of that book that's going to be outputed in the form so I didn't
1377:09 outputed in the form so I didn't manually type out seven long forms I
1377:11 manually type out seven long forms I just did one in this template so that it
1377:14 just did one in this template so that it gets generated automatically literally
1377:16 gets generated automatically literally what a website CL Amazon would do to
1377:18 what a website CL Amazon would do to show you 10 books at a time or 10 search
1377:20 show you 10 books at a time or 10 search results or more at a time all right well
1377:23 results or more at a time all right well let's go to SLC cart which was the juicy
1377:26 let's go to SLC cart which was the juicy one and this one's longer but let's see
1377:28 one and this one's longer but let's see if we can reason through it if the user
1377:31 if we can reason through it if the user submits via get or post to/ cart well we
1377:34 submits via get or post to/ cart well we first do this and this is just necessary
1377:36 first do this and this is just necessary in some boilerplate rewrote we're going
1377:39 in some boilerplate rewrote we're going to use the session object to store a
1377:41 to use the session object to store a variable called cart that in this case
1377:43 variable called cart that in this case is going to be a list so session again
1377:45 is going to be a list so session again is just a dictionary you can put
1377:47 is just a dictionary you can put anything in it you want previously we
1377:49 anything in it you want previously we put students names and sports now uh
1377:53 put students names and sports now uh what I want to do is actually sorry prev
1377:55 what I want to do is actually sorry prev previously we put uh the student name
1377:59 previously we put uh the student name the user's name in it now I'm going to
1378:01 the user's name in it now I'm going to actually store a cart key whose value is
1378:05 actually store a cart key whose value is a list why because I want to aggregate
1378:07 a list why because I want to aggregate more and more books in this list all
1378:09 more and more books in this list all right so that just makes sure that I
1378:10 right so that just makes sure that I have at least an empty shopping cart the
1378:12 have at least an empty shopping cart the very first time the user does this if
1378:15 very first time the user does this if they visit this form via post let's go
1378:17 they visit this form via post let's go ahead and get the ID of the book that
1378:19 ahead and get the ID of the book that they posted if it is not empty if there
1378:21 they posted if it is not empty if there is a number like one or two or three
1378:23 is a number like one or two or three let's go ahead and go into the shopping
1378:25 let's go ahead and go into the shopping C cart which is a list per this line and
1378:29 C cart which is a list per this line and just append that ID so this list of
1378:31 just append that ID so this list of books in your shopping cart is going to
1378:33 books in your shopping cart is going to contain like one comma 2 comma four
1378:35 contain like one comma 2 comma four comma six whatever books you're actually
1378:37 comma six whatever books you're actually buying and then the user gets redirected
1378:39 buying and then the user gets redirected to cart what if though the user got here
1378:42 to cart what if though the user got here via get and not post well this one's
1378:44 via get and not post well this one's relatively straightforward if you just
1378:46 relatively straightforward if you just visit slart we select star from books
1378:49 visit slart we select star from books where ID is in okay so this is
1378:53 where ID is in okay so this is interesting this list and and this is a
1378:56 interesting this list and and this is a syntax you might not have seen before
1378:57 syntax you might not have seen before but if you read the documentation for
1378:58 but if you read the documentation for cs50's Library if you select something
1379:02 cs50's Library if you select something and use a question mark placeholder and
1379:04 and use a question mark placeholder and the placeholder itself is a list we
1379:07 the placeholder itself is a list we output a comma separated list of values
1379:10 output a comma separated list of values just like you would use maybe in problem
1379:12 just like you would use maybe in problem Set uh seven for doing SQL queries on
1379:15 Set uh seven for doing SQL queries on your own so this just means show me only
1379:17 your own so this just means show me only those books in my shopping cart not
1379:20 those books in my shopping cart not Carters not someone else's not in the
1379:21 Carters not someone else's not in the whole database only show me the books in
1379:24 whole database only show me the books in my shopping cart and then
1379:26 my shopping cart and then render it as such so we only saw what
1379:28 render it as such so we only saw what the catalog here looks like at slash
1379:31 the catalog here looks like at slash books Let's go ahead and in slash let's
1379:34 books Let's go ahead and in slash let's go ahead and add maybe the first book to
1379:36 go ahead and add maybe the first book to my cart and now I see at SLC cart only
1379:40 my cart and now I see at SLC cart only that first book whose ID is one let me
1379:43 that first book whose ID is one let me now go back to the bookstore here scroll
1379:45 now go back to the bookstore here scroll down to maybe the seventh book and add
1379:47 down to maybe the seventh book and add that to my cart and now I see this here
1379:50 that to my cart and now I see this here too meanwhile all of this information is
1379:52 too meanwhile all of this information is stored in my session and so when I
1379:56 stored in my session and so when I reload this cart again and again the
1379:58 reload this cart again and again the reason I'm only seeing my two is because
1380:01 reason I'm only seeing my two is because we're checking only the list in my
1380:03 we're checking only the list in my session and flasks make sure again that
1380:05 session and flasks make sure again that my session is different from your
1380:07 my session is different from your session is different from Carter's
1380:08 session is different from Carter's session as well but you write the code
1380:11 session as well but you write the code once and it works for thousands millions
1380:13 once and it works for thousands millions of people in
1380:15 of people in parallel any questions on
1380:20 parallel any questions on this this yes in the
1380:23 this this yes in the back sorry say a little louder
1380:33 uh so to recap so users will never have the same session values theoretically
1380:35 the same session values theoretically the cookie that gets planted does not
1380:36 the cookie that gets planted does not look like a smiley face for everyone
1380:38 look like a smiley face for everyone each of us gets a big random number
1380:40 each of us gets a big random number that's assigned to us so it' be like
1380:42 that's assigned to us so it' be like each of us gets a completely unique hand
1380:44 each of us gets a completely unique hand stamp that no one else can see the
1380:45 stamp that no one else can see the reason no one else can see it is because
1380:47 reason no one else can see it is because if the website's using https every time
1380:50 if the website's using https every time this hand stamp is shown every time this
1380:51 this hand stamp is shown every time this cookie is sent back and forth It's all
1380:53 cookie is sent back and forth It's all encrypted as well so each of us can even
1380:56 encrypted as well so each of us can even if we have the same contents by
1380:58 if we have the same contents by coincidence because we like the same
1380:59 coincidence because we like the same books they will be separate cookies
1381:01 books they will be separate cookies separate memory separate sessions behind
1381:04 separate memory separate sessions behind you
1381:10 yeah really good question when does the session end totally configurable
1381:12 session end totally configurable typically it ends when you close the tab
1381:14 typically it ends when you close the tab or when you quit the browser or you can
1381:16 or when you quit the browser or you can also configure cookies to themselves be
1381:19 also configure cookies to themselves be persistent for a day for a week for
1381:21 persistent for a day for a week for longer so for instance when you log into
1381:24 longer so for instance when you log into um uh say Gmail they plant a cookie on
1381:27 um uh say Gmail they plant a cookie on your computer probably for a week a
1381:29 your computer probably for a week a month a year something like that because
1381:31 month a year something like that because it would be annoying and probably drive
1381:32 it would be annoying and probably drive you to like Outlook or something else if
1381:34 you to like Outlook or something else if you kept having to log into your account
1381:36 you kept having to log into your account whereas your bank account might actually
1381:38 whereas your bank account might actually wait for you to just close the tab and
1381:40 wait for you to just close the tab and then for your own Financial safety they
1381:42 then for your own Financial safety they just automatically delete the session
1381:43 just automatically delete the session far sooner but totally configurable by
1381:45 far sooner but totally configurable by default as I'm using it it will
1381:47 default as I'm using it it will typically be thrown away when the
1381:48 typically be thrown away when the browser itself quits and here too is
1381:50 browser itself quits and here too is another reason to develop websites using
1381:53 another reason to develop websites using incognito mode because if you want to
1381:55 incognito mode because if you want to just throw throw away all of your
1381:56 just throw throw away all of your cookies you close the incognito window
1381:58 cookies you close the incognito window mode open a new one and now you're
1381:59 mode open a new one and now you're starting from scratch you don't have to
1382:01 starting from scratch you don't have to manually delete all your cookies which
1382:03 manually delete all your cookies which could log you out of websites you
1382:05 could log you out of websites you actually care about
1382:13 yeah a good question when using sessions if someone maliciously changes the value
1382:14 if someone maliciously changes the value of sub forms could it affect other
1382:16 of sub forms could it affect other people theoretically no because the
1382:19 people theoretically no because the worst you can do is like add books to
1382:20 worst you can do is like add books to your own shopping cart that you don't
1382:22 your own shopping cart that you don't want there so at that point even though
1382:23 want there so at that point even though it's on the server it doesn't affect you
1382:25 it's on the server it doesn't affect you or Carter or anyone else unless there is
1382:29 or Carter or anyone else unless there is something more globally happening like
1382:32 something more globally happening like registering or deregistering for a sport
1382:34 registering or deregistering for a sport or removing books from the Amazon
1382:36 or removing books from the Amazon database that would be problematic but
1382:38 database that would be problematic but in this case we're removing things only
1382:40 in this case we're removing things only from my own session that the website is
1382:43 from my own session that the website is giving me all right the last topic for
1382:46 giving me all right the last topic for today is this thing here which is sort
1382:48 today is this thing here which is sort of everywhere nowadays these things
1382:50 of everywhere nowadays these things called apis or application programming
1382:52 called apis or application programming interfaces and this is a very generic
1382:54 interfaces and this is a very generic term in fact because any function you've
1382:56 term in fact because any function you've used in C in scratch in Python in SQL
1383:00 used in C in scratch in Python in SQL are all apis like there is a standard
1383:03 are all apis like there is a standard way of interfacing with those functions
1383:06 way of interfacing with those functions they all have names they sometimes have
1383:08 they all have names they sometimes have return values they sometimes have
1383:09 return values they sometimes have arguments and an API is just how you use
1383:12 arguments and an API is just how you use a function or more generally an API just
1383:16 a function or more generally an API just specifies how you interact with some
1383:18 specifies how you interact with some service and so nowadays there's a lot of
1383:20 service and so nowadays there's a lot of web-based services that you can use to
1383:23 web-based services that you can use to get back data like the weather or the
1383:24 get back data like the weather or the current time or the database of Amazon
1383:27 current time or the database of Amazon books for instance all might have apis
1383:30 books for instance all might have apis often web-based that allow you using
1383:32 often web-based that allow you using URLs or some other technology to just
1383:34 URLs or some other technology to just get data from someone else as though
1383:36 get data from someone else as though it's a function you're calling remotely
1383:38 it's a function you're calling remotely but HTTP is very often the mechanism
1383:41 but HTTP is very often the mechanism that's used to actually get data from
1383:43 that's used to actually get data from servers and the way the data can come
1383:45 servers and the way the data can come back can be as follows let me end with
1383:48 back can be as follows let me end with one final example using some of our
1383:50 one final example using some of our familiar shows from uh weeks past let me
1383:53 familiar shows from uh weeks past let me go ahead and close the old flask version
1383:55 go ahead and close the old flask version version go back into Source 9 and go
1383:58 version go back into Source 9 and go into how about an example called shows
1384:02 into how about an example called shows and the first version of this zero I'm
1384:04 and the first version of this zero I'm just going to go ahead and run with
1384:06 just going to go ahead and run with flask run I'll hover over my URL and
1384:08 flask run I'll hover over my URL and open it here and you'll see now that I
1384:11 open it here and you'll see now that I have a very simple form as we keep doing
1384:13 have a very simple form as we keep doing today I'm going to type in like o f f i
1384:15 today I'm going to type in like o f f i c office into this search box and click
1384:18 c office into this search box and click search and you'll see now that I ended
1384:21 search and you'll see now that I ended up at a URL ending in/ search question
1384:24 up at a URL ending in/ search question mark Q equals office so this is like my
1384:27 mark Q equals office so this is like my own baby version of google.com but I
1384:29 own baby version of google.com but I implemented it myself and for any title
1384:32 implemented it myself and for any title of a TV show from a couple of weeks past
1384:34 of a TV show from a couple of weeks past that matches o i I spit it out into an
1384:38 that matches o i I spit it out into an unordered list how is this working you
1384:41 unordered list how is this working you can maybe imagine even if you might not
1384:42 can maybe imagine even if you might not be able to program this off the top of
1384:44 be able to program this off the top of your head certainly so soon let me go
1384:46 your head certainly so soon let me go into Source 9 let me go into uh show
1384:50 into Source 9 let me go into uh show zero let me open up
1384:52 zero let me open up app.py and in this file you'll see that
1384:55 app.py and in this file you'll see that that I'm grabbing a uh file called
1384:57 that I'm grabbing a uh file called shows. DB which is like a simpler
1384:59 shows. DB which is like a simpler version of the one from a couple of
1385:00 version of the one from a couple of weeks past uh here is why I see the web
1385:03 weeks past uh here is why I see the web form my first route my index is super
1385:05 form my first route my index is super simple it just spits out that form and
1385:07 simple it just spits out that form and my search route like you can think of
1385:09 my search route like you can think of this as google.com there only like four
1385:11 this as google.com there only like four lines of code so if the user sends data
1385:14 lines of code so if the user sends data to SL search this function called search
1385:17 to SL search this function called search is called I declare a variable called
1385:19 is called I declare a variable called shows I execute a SQL command that is
1385:22 shows I execute a SQL command that is Select star from shows where title like
1385:25 Select star from shows where title like question mark and the syntax here is a
1385:27 question mark and the syntax here is a little crazy but I want to prefix to the
1385:30 little crazy but I want to prefix to the user's input percent sign and suffix it
1385:34 user's input percent sign and suffix it with a percent sign as well putting in
1385:36 with a percent sign as well putting in the between those two values the actual
1385:38 the between those two values the actual input why in SQL what is it mean if you
1385:41 input why in SQL what is it mean if you have a percent sign to the left and to
1385:43 have a percent sign to the left and to the right nothing to do with Ginger
1385:45 the right nothing to do with Ginger today yeah it's a it's a wild card so it
1385:48 today yeah it's a it's a wild card so it means match zero or more characters on
1385:50 means match zero or more characters on the left or match zero or more
1385:52 the left or match zero or more characters on the right you have to do
1385:54 characters on the right you have to do the concatenation
1385:55 the concatenation as the second argument to this function
1385:57 as the second argument to this function you can't do something clever like put
1385:59 you can't do something clever like put it here around the like the is the
1386:02 it here around the like the is the placeholder that you plug these values
1386:03 placeholder that you plug these values into but this just means hey SQL show me
1386:06 into but this just means hey SQL show me all of the titles that have o f i
1386:08 all of the titles that have o f i somewhere in them that gives me back an
1386:11 somewhere in them that gives me back an a list of dictionaries I pass that in as
1386:14 a list of dictionaries I pass that in as a placeholder for a variable called
1386:16 a placeholder for a variable called shows and if we look at search. HTML
1386:19 shows and if we look at search. HTML let's look at that in my templates
1386:20 let's look at that in my templates directory there's something called
1386:21 directory there's something called search. HTML super simple I mean this is
1386:24 search. HTML super simple I mean this is like the essence of google.com search
1386:26 like the essence of google.com search results I'm using an unordered list to
1386:29 results I'm using an unordered list to keep things simple but I iterate over
1386:31 keep things simple but I iterate over every show in the shows list that came
1386:33 every show in the shows list that came back and I output An Li with each of
1386:35 back and I output An Li with each of those shows titles and that's it now
1386:36 those shows titles and that's it now Google has like Blue Links and like
1386:38 Google has like Blue Links and like little previews and other text the first
1386:40 little previews and other text the first sentence or so from each page but like
1386:42 sentence or so from each page but like that's the idea like this is really
1386:43 that's the idea like this is really similar in spirit to what google.com
1386:46 similar in spirit to what google.com search does for you now how is this
1386:49 search does for you now how is this working there's no API involved here yet
1386:52 working there's no API involved here yet this is just very basic HTTP I submit
1386:54 this is just very basic HTTP I submit the the form I go to another route and I
1386:56 the the form I go to another route and I get back the results but check out this
1386:58 get back the results but check out this version Let me close these tabs here and
1387:01 version Let me close these tabs here and open my first terminal window let me go
1387:03 open my first terminal window let me go into shows one from today's Source 9
1387:06 into shows one from today's Source 9 directory and do flask run this time let
1387:09 directory and do flask run this time let me go ahead and hover over that URL and
1387:11 me go ahead and hover over that URL and open it here and gone now is the submit
1387:13 open it here and gone now is the submit button now I'm going to make an user
1387:16 button now I'm going to make an user interface that uses a technique called
1387:18 interface that uses a technique called Ajax for asynchronous uh JavaScript and
1387:21 Ajax for asynchronous uh JavaScript and XML which is somewhat of a data term
1387:23 XML which is somewhat of a data term because we're not using something called
1387:25 because we're not using something called ml anymore but Ajax is a technique
1387:27 ml anymore but Ajax is a technique whereby you don't have to submit forms
1387:30 whereby you don't have to submit forms anymore to get more data from the server
1387:32 anymore to get more data from the server you can use JavaScript per last week
1387:35 you can use JavaScript per last week listen for an event like the key press
1387:37 listen for an event like the key press coming down or up and as soon as you
1387:39 coming down or up and as soon as you hear such an event you can secretly in
1387:42 hear such an event you can secretly in JavaScript code send a request to the
1387:43 JavaScript code send a request to the server to get back more data and then
1387:46 server to get back more data and then plug it into the Dom the tree in the
1387:49 plug it into the Dom the tree in the computer's memory and this just makes
1387:51 computer's memory and this just makes for more seamless experiences like
1387:52 for more seamless experiences like autocomplete on any website so now let
1387:55 autocomplete on any website so now let me try typing o okay we got auto
1387:57 me try typing o okay we got auto complete super fast f f i c e and you'll
1388:02 complete super fast f f i c e and you'll see every time I add more keys to my
1388:05 see every time I add more keys to my input I'm doing another search another
1388:07 input I'm doing another search another search another search and the data is
1388:09 search another search and the data is changing now how is this working well
1388:11 changing now how is this working well let me go back to vs code here and in my
1388:13 let me go back to vs code here and in my other terminal window let me open up uh
1388:16 other terminal window let me open up uh app.py and in app.py you'll see that
1388:20 app.py and in app.py you'll see that there's still a
1388:21 there's still a search route down below that turns a
1388:25 search route down below that turns a search template but watch this let me go
1388:28 search template but watch this let me go into templates search. HTML and notice
1388:33 into templates search. HTML and notice here that we're indeed getting back an
1388:36 here that we're indeed getting back an unordered list of shows again and again
1388:40 unordered list of shows again and again and again and this HTML that's coming
1388:44 and again and this HTML that's coming back let me go here let me open my
1388:48 back let me go here let me open my terminal oh sorry this is the wrong
1388:50 terminal oh sorry this is the wrong version uh sorry I was in the wrong
1388:52 version uh sorry I was in the wrong folder let's fix this and shows one code
1388:55 folder let's fix this and shows one code of app.py it's almost the same thing
1388:58 of app.py it's almost the same thing inserch
1388:59 inserch here okay well I changed this slightly
1389:02 here okay well I changed this slightly let me show you this version of search
1389:03 let me show you this version of search if I open up app.py here's my search
1389:06 if I open up app.py here's my search route I'm getting a variable called Q
1389:09 route I'm getting a variable called Q giving it the value of whatever request.
1389:11 giving it the value of whatever request. RX has from the user like Q equals
1389:13 RX has from the user like Q equals office and then I'm checking if the user
1389:15 office and then I'm checking if the user actually typed something in execute this
1389:17 actually typed something in execute this SQL query select star from shows where
1389:19 SQL query select star from shows where title is like that using the same and
1389:21 title is like that using the same and this time just to keep things efficient
1389:23 this time just to keep things efficient I limited the total results to 50
1389:25 I limited the total results to 50 instead of an infinite number otherwise
1389:27 instead of an infinite number otherwise if the user type nothing just to be
1389:29 if the user type nothing just to be super safe here I'm setting shows equal
1389:32 super safe here I'm setting shows equal to an empty list so if you don't type
1389:33 to an empty list so if you don't type anything there's nothing to show and no
1389:35 anything there's nothing to show and no matter what I render this template
1389:37 matter what I render this template called search. HTML well let's look at
1389:40 called search. HTML well let's look at that if I open up templat search. HTML
1389:43 that if I open up templat search. HTML this time there's no layout there's no
1389:46 this time there's no layout there's no inheritance of that layout. HTML I am
1389:49 inheritance of that layout. HTML I am literally generating just a whole bunch
1389:51 literally generating just a whole bunch of Li fragments why well let's see what
1389:54 of Li fragments why well let's see what what's happening in the browser and this
1389:55 what's happening in the browser and this is the beginning of an API if I wanted
1389:58 is the beginning of an API if I wanted to make this API available this
1390:00 to make this API available this application programming interface I
1390:02 application programming interface I could tell the world that if you want to
1390:03 could tell the world that if you want to search for TV shows in my database go to
1390:07 search for TV shows in my database go to URL that's something something/ search
1390:10 URL that's something something/ search question mark Q equals office or cat or
1390:15 question mark Q equals office or cat or dog or anything else and what I will
1390:17 dog or anything else and what I will return to you is this enter I will just
1390:20 return to you is this enter I will just give you a whole bunch of Li tags which
1390:22 give you a whole bunch of Li tags which almost looks the same but let me view
1390:23 almost looks the same but let me view the page source only am I going to hand
1390:26 the page source only am I going to hand you back a fragment of HTML I'm not
1390:28 you back a fragment of HTML I'm not giving you an HTML tag a body tag a
1390:30 giving you an HTML tag a body tag a title tag a head tag I'm not giving you
1390:32 title tag a head tag I'm not giving you a web page I'm giving you a fragment of
1390:34 a web page I'm giving you a fragment of HTML that you can now do whatever you
1390:36 HTML that you can now do whatever you want including insert this into your own
1390:40 want including insert this into your own unordered list so notice what happens in
1390:43 unordered list so notice what happens in this actual app if I go back to vs code
1390:46 this actual app if I go back to vs code here let me open up my index template
1390:49 here let me open up my index template here and you'll see some JavaScript
1390:51 here and you'll see some JavaScript magic so in JavaScript here in my form
1390:55 magic so in JavaScript here in my form that only had the text box and no button
1390:58 that only had the text box and no button what am I doing in a script tag here I
1391:01 what am I doing in a script tag here I am creating a variable called input and
1391:03 am creating a variable called input and I'm using this function called query
1391:05 I'm using this function called query selector that just gets me a reference
1391:08 selector that just gets me a reference to the input text box on the form so I
1391:11 to the input text box on the form so I can see what the human typed this is a
1391:13 can see what the human typed this is a little different today but I'm using
1391:15 little different today but I'm using input. atevent listener which is a way
1391:18 input. atevent listener which is a way in JavaScript to tell it just like in
1391:20 in JavaScript to tell it just like in scratch listen for something to happen
1391:23 scratch listen for something to happen like the green flag being clicked but in
1391:24 like the green flag being clicked but in this case listen for an event that
1391:27 this case listen for an event that involves input that is like typing on
1391:29 involves input that is like typing on the keyboard whether it's by a copy
1391:31 the keyboard whether it's by a copy paste manual input or anything else then
1391:35 paste manual input or anything else then whenever that happens call this function
1391:37 whenever that happens call this function and async stands for asynchronous this
1391:39 and async stands for asynchronous this is a term of art which means that this
1391:41 is a term of art which means that this this function might take like a split
1391:43 this function might take like a split second maybe even a second or two to
1391:44 second maybe even a second or two to execute so it's going to do it behind
1391:47 execute so it's going to do it behind the scenes like in the background so to
1391:48 the scenes like in the background so to speak and what is it going to do well
1391:51 speak and what is it going to do well it's going to call a JavaScript function
1391:53 it's going to call a JavaScript function that all browsers now Support called
1391:55 that all browsers now Support called Fetch which is a function that uses HTTP
1391:58 Fetch which is a function that uses HTTP to go fetch more data via the from the
1392:00 to go fetch more data via the from the server it's going to fetch data from a
1392:02 server it's going to fetch data from a route called SL search question mark Q
1392:05 route called SL search question mark Q equals and whatever the value is that
1392:07 equals and whatever the value is that the user typed in so I'm just sort of
1392:09 the user typed in so I'm just sort of manually creating my own mini URL and
1392:11 manually creating my own mini URL and telling JavaScript go fetch me that HTML
1392:14 telling JavaScript go fetch me that HTML when it comes back via this line of text
1392:17 when it comes back via this line of text here called response. text and let me
1392:19 here called response. text and let me wave my hand at await await just means
1392:21 wave my hand at await await just means this might not come back immediately
1392:22 this might not come back immediately let's await the response and when it
1392:24 let's await the response and when it does come and then let's execute this
1392:25 does come and then let's execute this code I'm going to do this I'm going to
1392:28 code I'm going to do this I'm going to search the document the whole web page
1392:30 search the document the whole web page for this UL tag which is somewhere in
1392:33 for this UL tag which is somewhere in this page that we'll see in a moment
1392:35 this page that we'll see in a moment change its inner HTML to be that
1392:37 change its inner HTML to be that fragment of Li Li Li of all of those
1392:40 fragment of Li Li Li of all of those matching shows and where does this all
1392:42 matching shows and where does this all go well if we scroll up here you'll
1392:44 go well if we scroll up here you'll notice that there's my usual HTML up at
1392:47 notice that there's my usual HTML up at top head tag body tag and all of that
1392:50 top head tag body tag and all of that there's the text box that we've talked
1392:52 there's the text box that we've talked about already there's no button for
1392:54 about already there's no button for submitting cuz it all happens
1392:55 submitting cuz it all happens automatically but there is by default
1392:58 automatically but there is by default this empty UL that has nothing by
1393:00 this empty UL that has nothing by default until we start using that API
1393:03 default until we start using that API and the final flourish here is this this
1393:06 and the final flourish here is this this is kind of a uh ugly sloppy way to get
1393:10 is kind of a uh ugly sloppy way to get data from a server to just get back like
1393:12 data from a server to just get back like a fragment of HTML like what if I'm not
1393:14 a fragment of HTML like what if I'm not using HTML I want to store these uh TV
1393:17 using HTML I want to store these uh TV shows in a PDF or in some other wet tag
1393:19 shows in a PDF or in some other wet tag in a table or something like that it
1393:21 in a table or something like that it doesn't really make sense for the server
1393:23 doesn't really make sense for the server to be presuming that I want Li tags
1393:25 to be presuming that I want Li tags surrounding each of my data better would
1393:28 surrounding each of my data better would be to get a more generic format back and
1393:30 be to get a more generic format back and that format is almost always nowadays
1393:32 that format is almost always nowadays called this our final acronym Json
1393:36 called this our final acronym Json JavaScript object notation and let me do
1393:38 JavaScript object notation and let me do this let me close these two tabs here
1393:41 this let me close these two tabs here and open my terminal window and C cancel
1393:45 and open my terminal window and C cancel the previous version of flask that was
1393:47 the previous version of flask that was running let me close this version and
1393:49 running let me close this version and look at our final version called shows
1393:51 look at our final version called shows two and do flask run on I'm going to
1393:54 two and do flask run on I'm going to hover over that URL and open it in a
1393:56 hover over that URL and open it in a browser and I'm going to manually visit
1393:59 browser and I'm going to manually visit after zooming in let's do again slash uh
1394:03 after zooming in let's do again slash uh search question mark uh Q equals Office
1394:07 search question mark uh Q equals Office enter and this is what Json looks like
1394:10 enter and this is what Json looks like now at a glance this does not seem like
1394:11 now at a glance this does not seem like an improvement like this looks crazy
1394:13 an improvement like this looks crazy that it's just this Big Blob of text but
1394:15 that it's just this Big Blob of text but it's just enough text for the computer
1394:17 it's just enough text for the computer to be able to process it reliably notice
1394:20 to be able to process it reliably notice that there's a cur a square bracket here
1394:22 that there's a cur a square bracket here and if I actually scroll to the Bott
1394:24 and if I actually scroll to the Bott botom there' be a closed square bracket
1394:25 botom there' be a closed square bracket like way down there inside of that
1394:27 like way down there inside of that square bracket is a curly brace then ID
1394:31 square bracket is a curly brace then ID colon and then a number then a title
1394:33 colon and then a number then a title quote unquote colon and then the title
1394:36 quote unquote colon and then the title and then the closed curly brace so what
1394:38 and then the closed curly brace so what you're seeing in JavaScript object
1394:40 you're seeing in JavaScript object notation is a very standard super
1394:42 notation is a very standard super popular format that's just text that
1394:44 popular format that's just text that still uses square brackets for lists AKA
1394:46 still uses square brackets for lists AKA arrays that still uses curly braces for
1394:49 arrays that still uses curly braces for dictionaries key value pairs so what you
1394:51 dictionaries key value pairs so what you see here is a massive list up to 50 I
1394:54 see here is a massive list up to 50 I think think shows that came back from
1394:56 think think shows that came back from this API Each of which has a dictionary
1394:59 this API Each of which has a dictionary if you will an object of key value pairs
1395:02 if you will an object of key value pairs what keys and values an ID key and a
1395:05 what keys and values an ID key and a title key Each of which has a value
1395:07 title key Each of which has a value respectively and this is the same data
1395:09 respectively and this is the same data from IMDb some of which you might be
1395:11 from IMDb some of which you might be recalling visually this is just a very
1395:14 recalling visually this is just a very raw computer friendly way of returning a
1395:16 raw computer friendly way of returning a whole bunch of data that we humans don't
1395:18 whole bunch of data that we humans don't need to see but I can use this data by
1395:21 need to see but I can use this data by going back into vs code let me open
1395:23 going back into vs code let me open another terminal window and go into
1395:25 another terminal window and go into Source
1395:26 Source 9/ shows 2 and in here let me go ahead
1395:31 9/ shows 2 and in here let me go ahead and open up how about uh templates
1395:35 and open up how about uh templates index.html which previously just used
1395:38 index.html which previously just used that inner HTML trick and this is not
1395:41 that inner HTML trick and this is not going to impress you're not going to be
1395:43 going to impress you're not going to be pleased with this syntax but let me just
1395:45 pleased with this syntax but let me just at least explain what we're doing it
1395:47 at least explain what we're doing it turns out that Json is just the better
1395:49 turns out that Json is just the better way in general the more generic
1395:51 way in general the more generic multi-purpose user agnostic language
1395:54 multi-purpose user agnostic language agnostic way of returning data from a
1395:55 agnostic way of returning data from a server because it's just text so it
1395:57 server because it's just text so it doesn't matter if you're using python or
1395:58 doesn't matter if you're using python or C or C++ or JavaScript or Ruby or PHP or
1396:01 C or C++ or JavaScript or Ruby or PHP or something else like all of those
1396:03 something else like all of those languages can process Json information
1396:06 languages can process Json information and indeed here is some JavaScript that
1396:08 and indeed here is some JavaScript that does just that same code as before
1396:10 does just that same code as before initially I declare a variable called
1396:12 initially I declare a variable called input that gives me access to the user's
1396:14 input that gives me access to the user's text box I listen for input like key
1396:17 text box I listen for input like key strokes going up and down and when they
1396:19 strokes going up and down and when they happen I call this Anonymous function uh
1396:22 happen I call this Anonymous function uh I fetch data from the server using the
1396:24 I fetch data from the server using the exact same code as before search
1396:27 exact same code as before search question mark Q equals office or
1396:29 question mark Q equals office or anything else and then this is just now
1396:32 anything else and then this is just now new code that I use to convert that Json
1396:35 new code that I use to convert that Json data into my own HTML format be it an
1396:38 data into my own HTML format be it an unordered list an ordered list a table
1396:41 unordered list an ordered list a table or anything else what am I doing I've
1396:43 or anything else what am I doing I've got a variable called HTML initialized
1396:45 got a variable called HTML initialized to nothing so I've got no HTML initially
1396:48 to nothing so I've got no HTML initially I then iterate over every ID in those
1396:51 I then iterate over every ID in those shows so every one of IMDB's unique
1396:53 shows so every one of IMDB's unique identifiers I iterate over them one at a
1396:55 identifiers I iterate over them one at a time and then I go into the show at its
1396:59 time and then I go into the show at its ID location and I grab its title and
1397:03 ID location and I grab its title and this
1397:05 this is forget this for just a moment I then
1397:08 is forget this for just a moment I then take this HTML variable concatenate or
1397:11 take this HTML variable concatenate or join onto it my own Li tag plus the
1397:15 join onto it my own Li tag plus the title plus the close Li tag and I
1397:18 title plus the close Li tag and I skipped this because it got scary pretty
1397:20 skipped this because it got scary pretty fast but it turns out that if some TV
1397:22 fast but it turns out that if some TV shows have actually angled brackets in
1397:24 shows have actually angled brackets in that could break my HTML entirely so it
1397:27 that could break my HTML entirely so it turns out you might recall super briefly
1397:29 turns out you might recall super briefly last week we had the copyright symbol
1397:31 last week we had the copyright symbol using an HTML entity using the Ampersand
1397:34 using an HTML entity using the Ampersand and the hash symbol and 169 semicolon it
1397:37 and the hash symbol and 169 semicolon it turns out there are other such cryptic
1397:39 turns out there are other such cryptic sequences of characters that represent
1397:41 sequences of characters that represent otherwise dangerous or untypable
1397:44 otherwise dangerous or untypable characters like this which could confuse
1397:46 characters like this which could confuse the computer into thinking it's at the
1397:47 the computer into thinking it's at the beginning of a tag and an Amper sand
1397:49 beginning of a tag and an Amper sand which could similarly trick the computer
1397:51 which could similarly trick the computer into thinking that it's a entity which
1397:53 into thinking that it's a entity which it isn't but long story short there are
1397:55 it isn't but long story short there are libraries thankfully that handle much of
1397:57 libraries thankfully that handle much of this for you for our purposes the
1397:59 this for you for our purposes the takeaway is that now that you understand
1398:01 takeaway is that now that you understand a bit of
1398:02 a bit of HTTP uh now that you understand a bit of
1398:04 HTTP uh now that you understand a bit of HTML CSS and JavaScript all of which
1398:06 HTML CSS and JavaScript all of which they have their roles you can use them
1398:08 they have their roles you can use them ultimately to start assembling your own
1398:10 ultimately to start assembling your own web applications as you will for problem
1398:12 web applications as you will for problem set nine um stitching together all of
1398:14 set nine um stitching together all of those languages and building
1398:16 those languages and building full-fledged web applications mobile
1398:18 full-fledged web applications mobile applications or anything more and for
1398:21 applications or anything more and for that I think we are all set and if the
1398:24 that I think we are all set and if the first one up here uh can have these
1398:25 first one up here uh can have these cookies as well we'll see you next time
1398:27 cookies as well we'll see you next time for our very last cs50
1398:52 [Music] lecture
1400:29 good afternoon my name is Sarah and my name is Grant and we are the Harvard
1400:31 name is Grant and we are the Harvard crocodillos and pitches now Sarah and I
1400:34 crocodillos and pitches now Sarah and I understand that today is the final
1400:35 understand that today is the final lecture of cs50 it's been a tough
1400:37 lecture of cs50 it's been a tough semester we made it through pets four
1400:40 semester we made it through pets four five and even Finance now I know this is
1400:43 five and even Finance now I know this is an unpopular opinion but I particularly
1400:44 an unpopular opinion but I particularly enjoyed Finance I spent a lot of time
1400:46 enjoyed Finance I spent a lot of time with my
1400:47 with my flask the P set there was a p
1400:52 flask the P set there was a p set well um at least things are looking
1400:54 set well um at least things are looking up um today is our last lecture and look
1400:57 up um today is our last lecture and look how far we've come if I were an emoji
1401:00 how far we've come if I were an emoji right now I'd be the face with tears of
1401:03 right now I'd be the face with tears of joy sorry about that we're just trying
1401:05 joy sorry about that we're just trying to work some cs50 references into the uh
1401:06 to work some cs50 references into the uh intro I I mean uh boy I sure hope this
1401:09 intro I I mean uh boy I sure hope this uh tide man doesn't run off my Mario
1401:13 uh tide man doesn't run off my Mario filter you could say that for Loop x
1401:17 filter you could say that for Loop x equals Open Bracket one comma 2 close
1401:21 equals Open Bracket one comma 2 close bracket boy I sure wish we had checked
1401:23 bracket boy I sure wish we had checked to see if these jokes were funny when we
1401:25 to see if these jokes were funny when we wrote
1401:26 wrote them any who we hope that you'll enjoy
1401:29 them any who we hope that you'll enjoy this brief
1401:30 this brief [Music]
1401:36 serenade your boot
1401:50 KN House of well there's friers Andy truck barbecue ribs Tri
1402:10 the boy boy bo boy bo bo boy boy down at the
1402:16 boy bo boy bo bo boy boy down at the house the house of well your root and
1402:19 house the house of well your root and you walk on down to a knock down Shack
1402:21 you walk on down to a knock down Shack on the edge of town There's a l
1402:24 on the edge of town There's a l there just quit you see
1402:29 there just quit you see fall down the house the house
1402:50 [Applause] of up your boots and you walk on down
1403:04 at the house the House of Blue
1403:33 [Applause] good afternoon everyone we are the
1403:35 good afternoon everyone we are the Harvard crocodillos and it is such an
1403:36 Harvard crocodillos and it is such an honor to be here with the Radcliff
1403:38 honor to be here with the Radcliff pitches performing for cs50's final
1403:40 pitches performing for cs50's final lecture congratulations to everyone and
1403:42 lecture congratulations to everyone and we hope you'll enjoy this our tribute to
1404:11 4 C is for the language I once knew O is for all notation I must do D is for
1404:17 for all notation I must do D is for dynamic flask run and finance it is even
1404:21 dynamic flask run and finance it is even more than David May in canor so code is
1404:26 more than David May in canor so code is all that I can give to you to youe
1404:31 all that I can give to you to youe debugging it since
1404:34 debugging it since PE soon deadlines I'll make it hit
1404:37 PE soon deadlines I'll make it hit compile and please don't break it code
1404:41 compile and please don't break it code was made by
1404:42 was made by [Music]
1404:55 I for notation I must do is
1405:06 more is [Music]
1405:28 I is for the language 0 1 0 1 0 oh is for otation 0 1 1 D is for dynamic flas
1405:35 for otation 0 1 1 D is for dynamic flas run and finance e is even more than
1405:39 run and finance e is even more than David mail in canor so C is all that I
1405:43 David mail in canor so C is all that I can give to
1405:46 can give to you debugging it since
1405:49 you debugging it since PE soon deadlines I'll make it hit
1405:53 PE soon deadlines I'll make it hit compile and please don't break it code
1405:56 compile and please don't break it code was made by me for COD was made by me
1406:00 was made by me for COD was made by me for COD was
1406:02 for COD was [Music]
1406:30 all right this is cs50 and cs50 this was the Harvard crocodillos and the Radcliff
1406:33 the Harvard crocodillos and the Radcliff pitches if one more time we could thank
1406:35 pitches if one more time we could thank them for joining us
1406:44 today so this is already week 10 our last and indeed among the goals for
1406:46 last and indeed among the goals for today are to hopefully give you all the
1406:47 today are to hopefully give you all the more of appreciation of truly just how
1406:50 more of appreciation of truly just how far you've come recall that in week zero
1406:52 far you've come recall that in week zero we began with this this visual here
1406:54 we began with this this visual here whereby it was described this class as a
1406:56 whereby it was described this class as a bit of a fire hose whereby drinking from
1406:59 bit of a fire hose whereby drinking from that fire hose or really a fire hose
1407:01 that fire hose or really a fire hose from a water fountain uh is not unlike
1407:04 from a water fountain uh is not unlike getting an education down the road too
1407:06 getting an education down the road too and so this is to say that if you're
1407:07 and so this is to say that if you're feeling like you didn't quite get it all
1407:09 feeling like you didn't quite get it all down like that's actually okay and
1407:11 down like that's actually okay and that's to be expected and even if you
1407:12 that's to be expected and even if you felt that with each passing week 0 1 two
1407:15 felt that with each passing week 0 1 two all the way up now till 10 it never
1407:17 all the way up now till 10 it never really ever got easier perhaps just
1407:19 really ever got easier perhaps just consider that what was once hard like
1407:22 consider that what was once hard like Mario and like getting hello world to
1407:24 Mario and like getting hello world to compile is indeed the right measure of
1407:26 compile is indeed the right measure of the Delta between week Zer and now in
1407:28 the Delta between week Zer and now in week 10 in fact you might recall that
1407:30 week 10 in fact you might recall that again in week zero 2third of your
1407:32 again in week zero 2third of your classmates had never taken a CS course
1407:35 classmates had never taken a CS course before now of course you all have and
1407:37 before now of course you all have and indeed if you think back too to this
1407:38 indeed if you think back too to this final sentiment from week zero that
1407:40 final sentiment from week zero that indeed what ultimately matters in this
1407:42 indeed what ultimately matters in this course is not where you end up relative
1407:44 course is not where you end up relative to your classmates but where you end up
1407:45 to your classmates but where you end up relative to where you yourself began so
1407:48 relative to where you yourself began so I would take some pride take some
1407:50 I would take some pride take some satisfaction take some relief even
1407:52 satisfaction take some relief even though a little bit more work does
1407:53 though a little bit more work does remain at really just how far you've
1407:55 remain at really just how far you've come since that week zero and recall
1407:56 come since that week zero and recall that in week zero we literally started
1407:58 that in week zero we literally started with just zeros and ones and by now many
1408:00 with just zeros and ones and by now many of you might have gleaned that these 64
1408:03 of you might have gleaned that these 64 zeros and ones have been spelling
1408:04 zeros and ones have been spelling something week by week in fact today is
1408:06 something week by week in fact today is our very last message here in binary uh
1408:09 our very last message here in binary uh encoded on stage but then quickly we
1408:11 encoded on stage but then quickly we introduced scratch and we started to
1408:13 introduced scratch and we started to assemble some building blocks of
1408:14 assemble some building blocks of programming Loops conditions uh uh Loops
1408:17 programming Loops conditions uh uh Loops conditions functions and the like but
1408:19 conditions functions and the like but without all the distractions of
1408:20 without all the distractions of semicolons and curly braces and all of
1408:22 semicolons and curly braces and all of that which admittedly we introduce the
1408:23 that which admittedly we introduce the next week when we introduce you to C but
1408:26 next week when we introduce you to C but even now that we've transitioned to
1408:27 even now that we've transitioned to python hopefully even those kinds of
1408:29 python hopefully even those kinds of Curiosities or confusions are hopefully
1408:31 Curiosities or confusions are hopefully starting to just get more familiar and
1408:32 starting to just get more familiar and so you finally start to see the missing
1408:34 so you finally start to see the missing semicolon as opposed to spending time on
1408:36 semicolon as opposed to spending time on that kind of struggle recall too that in
1408:38 that kind of struggle recall too that in week two we started talking already
1408:40 week two we started talking already about memory and like how you can manage
1408:42 about memory and like how you can manage things in arrays that later became of
1408:44 things in arrays that later became of course in Python lists uh the week after
1408:46 course in Python lists uh the week after we talked not only about uh debugging uh
1408:49 we talked not only about uh debugging uh bugs in code but how to debug those same
1408:51 bugs in code but how to debug those same programs um thereafter we started
1408:52 programs um thereafter we started talking talking about algorithms and we
1408:54 talking talking about algorithms and we took a step back from code and looked at
1408:56 took a step back from code and looked at the bubble sorts and the selection sorts
1408:57 the bubble sorts and the selection sorts and the merge sorts and all of the
1408:59 and the merge sorts and all of the searches as well that go hand in hand
1409:01 searches as well that go hand in hand with that and indeed this ultimately is
1409:02 with that and indeed this ultimately is what a lot of problem solving moving
1409:04 what a lot of problem solving moving forward is going to be about just
1409:05 forward is going to be about just solving problems with some form of
1409:06 solving problems with some form of algorithm and you have so many different
1409:08 algorithm and you have so many different languages um in your toolkit now with
1409:11 languages um in your toolkit now with which to approach problems like those we
1409:13 which to approach problems like those we talk thereafter about pointers which are
1409:15 talk thereafter about pointers which are not likely to come back in any modern
1409:17 not likely to come back in any modern languages that you now use but hopefully
1409:18 languages that you now use but hopefully you have an all the better of a sense
1409:20 you have an all the better of a sense underneath the hood of like what's going
1409:22 underneath the hood of like what's going on inside of the computer so that when
1409:23 on inside of the computer so that when you're designing something you're using
1409:25 you're designing something you're using something something crashes you at least
1409:27 something something crashes you at least have a mental model for what's going on
1409:28 have a mental model for what's going on and it's no longer that week zero black
1409:31 and it's no longer that week zero black box as it once was I mean you built
1409:33 box as it once was I mean you built things like this think back to week five
1409:35 things like this think back to week five when you built your own hash table and
1409:37 when you built your own hash table and those things are everywhere key value
1409:39 those things are everywhere key value pairs whether it's in python or in C or
1409:41 pairs whether it's in python or in C or if it's now in CSS and JavaScript and
1409:44 if it's now in CSS and JavaScript and even HTML like that principle of key
1409:46 even HTML like that principle of key value pairs is really everywhere and so
1409:48 value pairs is really everywhere and so of course now code doesn't necessarily
1409:50 of course now code doesn't necessarily have to look like this it now
1409:51 have to look like this it now wonderfully looks a little something
1409:53 wonderfully looks a little something more like this but eventually you're
1409:55 more like this but eventually you're probably not I'm not going to use Python
1409:56 probably not I'm not going to use Python anymore something new and better is
1409:58 anymore something new and better is going to come along but odds are like a
1410:00 going to come along but odds are like a lot of the building blocks from these
1410:01 lot of the building blocks from these past 11 weeks are still going to be
1410:04 past 11 weeks are still going to be useful for wrapping your mind around
1410:05 useful for wrapping your mind around those new worlds and indeed SQL we
1410:07 those new worlds and indeed SQL we introduced you a little bit too and even
1410:09 introduced you a little bit too and even if you don't feel yourself yet an expert
1410:11 if you don't feel yourself yet an expert hopefully have a sense of like what you
1410:12 hopefully have a sense of like what you can do with it and what problems you can
1410:14 can do with it and what problems you can solve uh it's of course a better
1410:15 solve uh it's of course a better alternative to something like something
1410:17 alternative to something like something simple like a spreadsheet and now of
1410:19 simple like a spreadsheet and now of course like web stuff is everywhere
1410:21 course like web stuff is everywhere whether it's on your laptop or desktop
1410:22 whether it's on your laptop or desktop or a lot of the mobile apps that you use
1410:25 or a lot of the mobile apps that you use on your phone even though they're native
1410:27 on your phone even though they're native applications like you install them from
1410:28 applications like you install them from Google Play or the Apple App Store like
1410:30 Google Play or the Apple App Store like they're implemented increasingly with
1410:33 they're implemented increasingly with HTML CSS and JavaScript but they're put
1410:35 HTML CSS and JavaScript but they're put in a little rectangular window so you
1410:36 in a little rectangular window so you don't even notice that that's actually
1410:38 don't even notice that that's actually really just an embedded browser and then
1410:40 really just an embedded browser and then of course you can build things as you
1410:42 of course you can build things as you might for your final project that to
1410:43 might for your final project that to might very well be web based if you go
1410:45 might very well be web based if you go that route I mean I'm still clinging to
1410:47 that route I mean I'm still clinging to like the very first like web app I ever
1410:49 like the very first like web app I ever made years ago um but honestly I do that
1410:51 made years ago um but honestly I do that in part because I was just so darn proud
1410:54 in part because I was just so darn proud that like I taught myself how to do
1410:55 that like I taught myself how to do something and it actually worked and was
1410:57 something and it actually worked and was used by other people so whether it's
1410:59 used by other people so whether it's just used by you or your classmates or
1411:01 just used by you or your classmates or your roommates or your family or your
1411:02 your roommates or your family or your company down the line there's a great
1411:04 company down the line there's a great sense of satisfaction that comes despite
1411:06 sense of satisfaction that comes despite all of the the pain that might be along
1411:08 all of the the pain that might be along the way when you just can't see or fix
1411:10 the way when you just can't see or fix that bug now of course we'll transition
1411:13 that bug now of course we'll transition as you'll see in the coming days to try
1411:15 as you'll see in the coming days to try empowering you to code client side as
1411:17 empowering you to code client side as well up until now you've been using our
1411:19 well up until now you've been using our own vs code installation in the cloud
1411:21 own vs code installation in the cloud which is nice cuz you got up in running
1411:23 which is nice cuz you got up in running super fast in week one focusing only on
1411:25 super fast in week one focusing only on code challenges not on technical
1411:27 code challenges not on technical difficulties but among the goals now if
1411:29 difficulties but among the goals now if you so choose and want to program after
1411:31 you so choose and want to program after this class even if you never take
1411:33 this class even if you never take another CS course before you can use
1411:35 another CS course before you can use these same real world deao standard
1411:37 these same real world deao standard tools on your own Mac or PC and so
1411:39 tools on your own Mac or PC and so pictured here is a screenshot of like vs
1411:41 pictured here is a screenshot of like vs code on the Mac and even though yes
1411:43 code on the Mac and even though yes you're going to have to like jump
1411:44 you're going to have to like jump through a couple of more Hoops to just
1411:45 through a couple of more Hoops to just get python or some other language
1411:47 get python or some other language working on your own Mac or PC like
1411:49 working on your own Mac or PC like that's what programming is ultimately
1411:50 that's what programming is ultimately going to be about and we deliberately
1411:52 going to be about and we deliberately transition you to this at terms end so
1411:54 transition you to this at terms end so that now you have 11 weeks of more
1411:56 that now you have 11 weeks of more Comfort under your belt with which to
1411:58 Comfort under your belt with which to solve sort of silly technical support
1412:00 solve sort of silly technical support headaches that might have been deal
1412:01 headaches that might have been deal breakers so many weeks ago so there's
1412:03 breakers so many weeks ago so there's still more to be done uh in the coming
1412:05 still more to be done uh in the coming weeks and indeed a support structure
1412:07 weeks and indeed a support structure there for the cs50 hackathon of course
1412:09 there for the cs50 hackathon of course will be this 700 p.m. till 7 a.m.
1412:12 will be this 700 p.m. till 7 a.m. opportunity to dive into your final
1412:14 opportunity to dive into your final project well really continue diving into
1412:16 project well really continue diving into your final project ideally at that point
1412:19 your final project ideally at that point uh alongside classmates perhaps your
1412:20 uh alongside classmates perhaps your project Partners if you're working
1412:22 project Partners if you're working collaboratively
1412:23 collaboratively uh and awaiting you will be such
1412:25 uh and awaiting you will be such memories and excitement hopefully as
1412:26 memories and excitement hopefully as these even as you then turn your
1412:28 these even as you then turn your attention back to your final project
1412:29 attention back to your final project there of course will be several meals
1412:31 there of course will be several meals during the day culminating with 500 a.m.
1412:33 during the day culminating with 500 a.m. uh shuttles to IHOP the local Pancake
1412:36 uh shuttles to IHOP the local Pancake Place uh if you are so awake at that
1412:38 Place uh if you are so awake at that point or even if you get there uh this
1412:40 point or even if you get there uh this is not an uncommon site as you might
1412:42 is not an uncommon site as you might recall from week zero and then lastly is
1412:44 recall from week zero and then lastly is the cs-50 fair which is finally back
1412:46 the cs-50 fair which is finally back after a couple of years now of it not
1412:48 after a couple of years now of it not being on campus and this will be an
1412:49 being on campus and this will be an opportunity for everyone to present
1412:51 opportunity for everyone to present their final projects to passers by
1412:53 their final projects to passers by classmates faculty and staff and really
1412:55 classmates faculty and staff and really just Delight in what it is you created
1412:56 just Delight in what it is you created on your Mac your PC your phone in the
1412:58 on your Mac your PC your phone in the cloud or anywhere else and indeed it's
1413:01 cloud or anywhere else and indeed it's just going to be an opportunity to bring
1413:02 just going to be an opportunity to bring your laptop to a shared space or your
1413:04 your laptop to a shared space or your phone and introduce your project to
1413:06 phone and introduce your project to passers by such as might appear and
1413:08 passers by such as might appear and ultimately celebrate what you all uh
1413:10 ultimately celebrate what you all uh accomplished and indeed will you be
1413:12 accomplished and indeed will you be handed at the cs50 fair your very own I
1413:14 handed at the cs50 fair your very own I took cs50 t-shirt which I dare say I'm
1413:17 took cs50 t-shirt which I dare say I'm still wearing all of these years later
1413:20 still wearing all of these years later and so you
1413:21 and so you to
1413:23 to we'll have that uh ahead of you as well
1413:26 we'll have that uh ahead of you as well so for what's on the agenda today we
1413:29 so for what's on the agenda today we thought we would not only look back but
1413:30 thought we would not only look back but look forward but first we wanted to
1413:32 look forward but first we wanted to thank so many of the team members that
1413:33 thank so many of the team members that have been helping both on stage and off
1413:35 have been helping both on stage and off who've made this course and these
1413:37 who've made this course and these sections and so much more about cs50
1413:39 sections and so much more about cs50 possible of course um the building that
1413:41 possible of course um the building that we are now in there's a whole team
1413:43 we are now in there's a whole team downstairs in Memorial Hall who helps us
1413:45 downstairs in Memorial Hall who helps us get set up and organized each day our
1413:47 get set up and organized each day our thanks to them there's the education
1413:49 thanks to them there's the education Support Services team who makes
1413:50 Support Services team who makes everything look and sound so well down
1413:52 everything look and sound so well down here especially when we have all of the
1413:53 here especially when we have all of the more microphones as well our friends the
1413:55 more microphones as well our friends the harbard crocodillos and the redcliff
1413:57 harbard crocodillos and the redcliff pitches most recently and then of course
1413:59 pitches most recently and then of course cs50's own team uh but butter cs50's own
1414:04 cs50's own team uh but butter cs50's own favorite restaurant Chang show down the
1414:06 favorite restaurant Chang show down the road indeed if you find yourself in
1414:08 road indeed if you find yourself in Cambridge for the next one two 3 four
1414:09 Cambridge for the next one two 3 four years or visiting from out of town uh do
1414:11 years or visiting from out of town uh do pay a visit to our friends just down the
1414:13 pay a visit to our friends just down the road and in fact we'll have our very
1414:14 road and in fact we'll have our very last cs50 lunch this Friday if you're
1414:16 last cs50 lunch this Friday if you're able locally to partake and then there
1414:19 able locally to partake and then there cs50's own team um both on stage and off
1414:22 cs50's own team um both on stage and off in thanks truly because not only do they
1414:24 in thanks truly because not only do they make the everything run so smoothly they
1414:26 make the everything run so smoothly they capture it for students here who might
1414:28 capture it for students here who might not be physically present here for our
1414:29 not be physically present here for our friends down in New Haven at Yale and
1414:31 friends down in New Haven at Yale and certainly for anyone online who might be
1414:33 certainly for anyone online who might be tuning in as well and then lastly wanted
1414:36 tuning in as well and then lastly wanted to thank of course the huge team of your
1414:39 to thank of course the huge team of your classmates your peers that make cs50
1414:41 classmates your peers that make cs50 possible in sections and office hours
1414:43 possible in sections and office hours tutorials and more allow me to share
1414:46 tutorials and more allow me to share with you the outtakes so that even we
1414:49 with you the outtakes so that even we the teaching staff sometimes struggle
1414:51 the teaching staff sometimes struggle with computer science here are some of
1414:53 with computer science here are some of the clips that we captured when just
1414:54 the clips that we captured when just passing packets via tcpip a while back
1414:57 passing packets via tcpip a while back you saw the finally the the nicely
1414:59 you saw the finally the the nicely polished version but here are if I may
1415:02 polished version but here are if I may if we could dim the lights are some of
1415:04 if we could dim the lights are some of the
1415:05 the outakes nothing
1415:07 outakes nothing go buffering
1415:10 go buffering okay Josh
1415:12 okay Josh nice
1415:19 [Music] Helen no oh wait
1415:34 Josh uh um [Music]
1415:42 Sophie amazing that was
1415:44 amazing that was perfect
1415:51 B I think I what
1415:52 I what you oh nice
1415:56 you oh nice [Music]
1415:58 [Music] guy that was amazing thank you all so
1416:06 good indeed and that moment if we could just one round of applause for everyone
1416:07 just one round of applause for everyone who's helped out this
1416:17 semester so back in week zero uh we introduced you of course to this idea of
1416:19 introduced you of course to this idea of computational thinking which is to think
1416:22 computational thinking which is to think a a little more methodically a little
1416:23 a a little more methodically a little more algorithmically and by way of these
1416:25 more algorithmically and by way of these various languages hopefully that is
1416:26 various languages hopefully that is something you notice maybe not in the
1416:28 something you notice maybe not in the moment but in the months and the years
1416:29 moment but in the months and the years to come that you do find that your
1416:31 to come that you do find that your thoughts are indeed a little more
1416:32 thoughts are indeed a little more cleaned up and you're just able to
1416:33 cleaned up and you're just able to express yourself a little more precisely
1416:35 express yourself a little more precisely and even spot illogic in someone else's
1416:37 and even spot illogic in someone else's document or statements as well but at
1416:39 document or statements as well but at the end of the day really a course like
1416:41 the end of the day really a course like this is also about critical thinking and
1416:43 this is also about critical thinking and indeed rewind again to week zero when we
1416:45 indeed rewind again to week zero when we frame the entirety of computer science
1416:47 frame the entirety of computer science is really just this like problem solving
1416:49 is really just this like problem solving and any problem in the world be it CS or
1416:51 and any problem in the world be it CS or otherwise has some input and we decided
1416:53 otherwise has some input and we decided how to represent those inputs it needs
1416:55 how to represent those inputs it needs some output the solution there too and
1416:57 some output the solution there too and then all of what you focused on doing
1416:59 then all of what you focused on doing and learning and applying these past
1417:00 and learning and applying these past several weeks is in that proverbial
1417:02 several weeks is in that proverbial black box which hopefully is not such an
1417:04 black box which hopefully is not such an abstraction anymore but is indeed
1417:06 abstraction anymore but is indeed something that you know how to harness
1417:08 something that you know how to harness and know what could be going on
1417:10 and know what could be going on underneath the hood even if it's in some
1417:11 underneath the hood even if it's in some technology or some language that maybe
1417:13 technology or some language that maybe we ourselves didn't cover because a lot
1417:15 we ourselves didn't cover because a lot of those first principles remain the
1417:17 of those first principles remain the same now along the way we talked about
1417:18 same now along the way we talked about the quality of solutions to those
1417:20 the quality of solutions to those problems we happen to focus on
1417:22 problems we happen to focus on correctness just does it work design
1417:24 correctness just does it work design which is a bit more qualitative and
1417:26 which is a bit more qualitative and subjective and then style the Aesthetics
1417:28 subjective and then style the Aesthetics of it all and these two are
1417:29 of it all and these two are characteristic maybe not with these same
1417:31 characteristic maybe not with these same words of just how you might write or
1417:33 words of just how you might write or evaluate other creations in life be it
1417:35 evaluate other creations in life be it physical or written or the like so think
1417:37 physical or written or the like so think about too as you solve problems just how
1417:39 about too as you solve problems just how you can sort of frame for yourself like
1417:41 you can sort of frame for yourself like am I doing a good job or not by
1417:43 am I doing a good job or not by quantizing it along these or perhaps
1417:45 quantizing it along these or perhaps other axes as well and we thought we'd
1417:47 other axes as well and we thought we'd highlight just two topics from that week
1417:49 highlight just two topics from that week zero that have really been manifest for
1417:51 zero that have really been manifest for the past several weeks namely
1417:52 the past several weeks namely abstraction like taking complicated
1417:54 abstraction like taking complicated things and ideas and trying to simplify
1417:56 things and ideas and trying to simplify them so that we can sort of operate at
1417:57 them so that we can sort of operate at this level and like solve problems we
1417:59 this level and like solve problems we care about without getting into the
1418:01 care about without getting into the weeds of implementation detail so to
1418:04 weeds of implementation detail so to speak but there's this tension because
1418:06 speak but there's this tension because you know now from all of these different
1418:07 you know now from all of these different languages that code is fairly
1418:09 languages that code is fairly unforgiving I mean even omitting a
1418:11 unforgiving I mean even omitting a stupid semicolon sometimes breaks
1418:12 stupid semicolon sometimes breaks everything and so Precision is sort of
1418:14 everything and so Precision is sort of at odd sometimes with this idea of
1418:16 at odd sometimes with this idea of leveraging abstraction and so we thought
1418:19 leveraging abstraction and so we thought we would try to tease this apart
1418:21 we would try to tease this apart especially all these Weeks Later here uh
1418:23 especially all these Weeks Later here uh with a bit of a uh live demonstration so
1418:26 with a bit of a uh live demonstration so on the way in you probably all received
1418:28 on the way in you probably all received a sheet of paper if not but someone near
1418:30 a sheet of paper if not but someone near you did just tear theirs in half and
1418:32 you did just tear theirs in half and borrow half a sheet if you can or any
1418:34 borrow half a sheet if you can or any piece of loose leaf paper or the like
1418:36 piece of loose leaf paper or the like will suffice so long as you have a pen
1418:37 will suffice so long as you have a pen or pencil and for this allow me to
1418:41 or pencil and for this allow me to propose that we invite up maybe two
1418:43 propose that we invite up maybe two final cs50 volunteers this semester and
1418:47 final cs50 volunteers this semester and like a lot of hands are going up in this
1418:49 like a lot of hands are going up in this okay a lot of hands how about I saw the
1418:50 okay a lot of hands how about I saw the first hand there uh yes yes who's yes
1418:53 first hand there uh yes yes who's yes who's pointing at herself now come on
1418:56 who's pointing at herself now come on down we just need the one hand for now
1418:58 down we just need the one hand for now but oh wait oh wait uh you'll be our
1419:01 but oh wait oh wait uh you'll be our number two well okay we have way too
1419:02 number two well okay we have way too many volunteers now no no please please
1419:04 many volunteers now no no please please come down yes in the black shirt and if
1419:07 come down yes in the black shirt and if you guys we will okay we'll do pair
1419:10 you guys we will okay we'll do pair programming in just a bit if you want to
1419:11 programming in just a bit if you want to hang out in the wings here we'll have
1419:13 hang out in the wings here we'll have our second demonstration as well so okay
1419:15 our second demonstration as well so okay now maybe a round of applause for our
1419:17 now maybe a round of applause for our three
1419:24 volunteers oh come on up first oh second and third okay you come first we'll do
1419:26 and third okay you come first we'll do order no uh this is a q okay Q here okay
1419:30 order no uh this is a q okay Q here okay what's your name I'm Danny Danny okay
1419:31 what's your name I'm Danny Danny okay take this mic okay so we will DQ you
1419:34 take this mic okay so we will DQ you momentarily all right so Danny come on
1419:36 momentarily all right so Danny come on over to the middle here and in a moment
1419:38 over to the middle here and in a moment I'm going to hand to Danny a sheet of
1419:40 I'm going to hand to Danny a sheet of paper that has a picture on it and this
1419:42 paper that has a picture on it and this picture is going to be something that
1419:43 picture is going to be something that I'd like you to verbally program the
1419:45 I'd like you to verbally program the audience to draw you can use any words
1419:49 audience to draw you can use any words any abstractions any PR level of
1419:50 any abstractions any PR level of precision that you want but you just
1419:52 precision that you want but you just can't make hand gestures or sort of show
1419:54 can't make hand gestures or sort of show them what to draw But first you want to
1419:56 them what to draw But first you want to tell us a little something about
1419:57 tell us a little something about yourself including everyone here I'm
1419:59 yourself including everyone here I'm daddy and I took ts50 okay wonderful
1420:03 daddy and I took ts50 okay wonderful wonderful so I'm going to reveal the
1420:05 wonderful so I'm going to reveal the picture only to Danny and if each of you
1420:08 picture only to Danny and if each of you would like to take out that sheet of
1420:09 would like to take out that sheet of paper and just make sure that no one
1420:10 paper and just make sure that no one else can see this if you want to hold it
1420:12 else can see this if you want to hold it up this way everyone here is now holding
1420:15 up this way everyone here is now holding their pen or pencil and in some number
1420:17 their pen or pencil and in some number of steps give them a verbal algorithm
1420:19 of steps give them a verbal algorithm for drawing what you see and you can say
1420:21 for drawing what you see and you can say anything you want but no gestures okay
1420:23 anything you want but no gestures okay so you're going to want to
1420:25 so you're going to want to draw a square in the center of the paper
1420:30 draw a square in the center of the paper with the
1420:31 with the diagonal pointing to
1420:34 diagonal pointing to the center of the
1420:43 edge wait no actually I scratched that draw a rbus in the center of your
1420:50 paper and for those who forget what a rhombus is for um a d
1420:53 rhombus is for um a d Diamond a a square that's on its
1420:57 Diamond a a square that's on its side and then from the bottom vertex
1421:02 side and then from the bottom vertex draw a straight line down but not all
1421:04 draw a straight line down but not all the way to the edge of the
1421:12 paper okay and then keep your pencil or pen at that point and you're going to
1421:14 pen at that point and you're going to want to draw a line that's parallel to
1421:18 want to draw a line that's parallel to the line of the original rumus to the
1421:27 right and then keep your pencil or pen at that point and draw a line straight
1421:30 at that point and draw a line straight up connecting to the side
1421:39 vertex yes and then go back to the line that you drew from the bottom vertex to
1421:42 that you drew from the bottom vertex to the bottom of the paper and then draw a
1421:45 the bottom of the paper and then draw a line parallel to the left edge of the
1421:50 line parallel to the left edge of the rumus and then then keep your L your
1421:53 rumus and then then keep your L your paper your pencil at that point and draw
1421:55 paper your pencil at that point and draw a line up to the vertex of the rumus
1422:05 again the end the end all right well thank you to Danny hang on to your paper
1422:07 thank you to Danny hang on to your paper thank you so much and if you want to
1422:09 thank you so much and if you want to step off to the stage there we will
1422:11 step off to the stage there we will reveal thank you a round of applause if
1422:13 reveal thank you a round of applause if we could for Danny that is not an easy
1422:16 we could for Danny that is not an easy task I'm sure and if Carter wouldn't
1422:19 task I'm sure and if Carter wouldn't mind just grabbing a few samples here
1422:21 mind just grabbing a few samples here let's actually take a look on the
1422:22 let's actually take a look on the overhead if we could I'm going to pop
1422:23 overhead if we could I'm going to pop down over here real fast we don't need
1422:25 down over here real fast we don't need to collect them all but if you're
1422:26 to collect them all but if you're feeling either good or bad with what you
1422:28 feeling either good or bad with what you drew happy to collect a few of them
1422:32 drew happy to collect a few of them okay okay thank you thank
1422:36 okay okay thank you thank you okay hope you won't mind if I can't
1422:38 you okay hope you won't mind if I can't reach everyone just a couple more okay
1422:40 reach everyone just a couple more okay over here okay all right that's that's
1422:45 over here okay all right that's that's okay this one's really funny okay I'm
1422:47 okay this one's really funny okay I'm going to go with this one if I may and
1422:49 going to go with this one if I may and Carter has some
1422:50 Carter has some too
1422:59 okay thank you so much okay so just a random assortment here let me turn on a
1423:01 random assortment here let me turn on a camera so I can show you what I see here
1423:04 camera so I can show you what I see here for instance is one classmate drawing
1423:08 for instance is one classmate drawing which might resemble perhaps what you
1423:10 which might resemble perhaps what you drew here uh here is the beginnings of a
1423:14 drew here uh here is the beginnings of a house it
1423:16 house it seems nice this
1423:19 seems nice this one okay this one is larger
1423:23 one okay this one is larger and how about a couple of others that
1423:25 and how about a couple of others that were getting closer I think okay so more
1423:28 were getting closer I think okay so more edges and vertices there this one seems
1423:31 edges and vertices there this one seems a little similar in
1423:33 a little similar in spirit if not
1423:36 spirit if not proportional this is uh Zach's the best
1423:41 proportional this is uh Zach's the best one but it turns out if I may Zach
1423:43 one but it turns out if I may Zach you're not all that far off here Denny
1423:46 you're not all that far off here Denny is what you were reciting to everyone
1423:48 is what you were reciting to everyone algorithmically indeed it was this here
1423:51 algorithmically indeed it was this here Cube and so Danny can you come on back
1423:52 Cube and so Danny can you come on back up for a
1423:54 up for a moment so if you'd like to share for
1423:56 moment so if you'd like to share for just a moment like what were some of the
1423:58 just a moment like what were some of the thoughts going through your head and why
1423:59 thoughts going through your head and why did you choose the words that you did
1424:01 did you choose the words that you did okay so what was going through my head
1424:03 okay so what was going through my head when I saw the cube um I didn't know if
1424:06 when I saw the cube um I didn't know if I could say draw Cube so I decided to
1424:10 I could say draw Cube so I decided to start with the top and so draw a Remus
1424:12 start with the top and so draw a Remus in the center of your paper and then
1424:14 in the center of your paper and then draw a line down and just like do the
1424:18 draw a line down and just like do the first part then the second part then the
1424:21 first part then the second part then the third part and then you would get a cube
1424:23 third part and then you would get a cube like Zach yeah and so had you said and
1424:25 like Zach yeah and so had you said and you could have said draw a cube which
1424:27 you could have said draw a cube which would be more of an abstraction even
1424:29 would be more of an abstraction even that's not necessarily sufficiently
1424:30 that's not necessarily sufficiently precise right because you don't
1424:31 precise right because you don't necessarily know what the orientation of
1424:33 necessarily know what the orientation of that cube is the size of it the
1424:35 that cube is the size of it the positioning on the paper so you instead
1424:36 positioning on the paper so you instead took a lower level approach which is not
1424:38 took a lower level approach which is not unlike scratch if you think about
1424:39 unlike scratch if you think about scratch being able to move up down left
1424:41 scratch being able to move up down left right turn 90 degrees turn 15 degrees
1424:43 right turn 90 degrees turn 15 degrees and the like I mean that is ultimately
1424:45 and the like I mean that is ultimately how a lot of graphical programs and
1424:47 how a lot of graphical programs and games even might be implemented by
1424:48 games even might be implemented by really focusing at the level you do but
1424:50 really focusing at the level you do but of course there's this tension with us
1424:51 of course there's this tension with us humans whereby we prefer often to think
1424:53 humans whereby we prefer often to think at this level but even that might not be
1424:55 at this level but even that might not be sufficient which is to say here in week
1424:56 sufficient which is to say here in week 10 like these are still going to be in
1424:59 10 like these are still going to be in our hard problems uh but a hand if we
1425:01 our hard problems uh but a hand if we could one more time for Danny for
1425:02 could one more time for Danny for getting us that far along thank you so
1425:05 getting us that far along thank you so much let me give you a stress ball here
1425:07 much let me give you a stress ball here all right and if we could have both of
1425:09 all right and if we could have both of our volunteers come up here we're going
1425:12 our volunteers come up here we're going to have yes come on up come on up and
1425:16 to have yes come on up come on up and let me uh have you guys introduce
1425:17 let me uh have you guys introduce yourselves in the middle
1425:19 yourselves in the middle here hi I'm
1425:22 here hi I'm hey there I'm sadik from Turkey nice to
1425:24 hey there I'm sadik from Turkey nice to meet you all wonderful welcome and this
1425:27 meet you all wonderful welcome and this time we're going to flip it around so as
1425:29 time we're going to flip it around so as to have the audience do what Danny just
1425:31 to have the audience do what Danny just did for us the only catch here is that
1425:34 did for us the only catch here is that the only means we have for showing the
1425:35 the only means we have for showing the audience what they need to tell you to
1425:37 audience what they need to tell you to draw is like literally right above the
1425:39 draw is like literally right above the chalkboard so on our system here that
1425:41 chalkboard so on our system here that your eyes must stay on the chalkboard
1425:43 your eyes must stay on the chalkboard and not look up and in just a moment if
1425:46 and not look up and in just a moment if you guys want to both stand in front of
1425:47 you guys want to both stand in front of the chalkboard back to the audience and
1425:50 the chalkboard back to the audience and as you're talking with each other other
1425:52 as you're talking with each other other verbalize it through the microphone if
1425:53 verbalize it through the microphone if you will I'm going to show everyone else
1425:55 you will I'm going to show everyone else in the room a second and final drawing
1425:57 in the room a second and final drawing and we'll just go rapid fire around the
1425:59 and we'll just go rapid fire around the room give us one step at a time
1426:00 room give us one step at a time collectively and we'll see if these guys
1426:02 collectively and we'll see if these guys can't draw exactly that same outcome so
1426:05 can't draw exactly that same outcome so is there another chunk what's that is
1426:07 is there another chunk what's that is there another chunk or just just the one
1426:08 there another chunk or just just the one so you'll have to collaborate and let's
1426:10 so you'll have to collaborate and let's give you a clean slate here
1426:15 give you a clean slate here literally all right so no looking up
1426:18 literally all right so no looking up that's the only rule for you guys here
1426:20 that's the only rule for you guys here we go for the audience here is what we'd
1426:23 we go for the audience here is what we'd like them ironically to
1426:26 like them ironically to draw step one from anyone in the
1426:30 draw step one from anyone in the audience yes draw a circle draw a circle
1426:35 audience yes draw a circle draw a circle anywhere not anywhere not
1426:44 anywhere okay that's step one step two someone else yeah in the
1426:52 middle draw a line down from the bottom of the
1426:54 draw a line down from the bottom of the circle about halfway down I think there
1426:56 circle about halfway down I think there was a hand in front of you too number
1427:10 three
1427:12 [Music] okay okay the overarching goal here for
1427:15 okay okay the overarching goal here for those unable here is to draw a person
1427:18 those unable here is to draw a person fig okay it may be a SI figure draw the
1427:22 fig okay it may be a SI figure draw the left leg of the person of this person
1427:25 left leg of the person of this person okay good job all right next step four
1427:30 okay good job all right next step four yeah
1427:32 yeah of line up Circle
1427:36 of line up Circle okay the left oh sorry to the right draw
1427:39 okay the left oh sorry to the right draw a v okay to the right of the vertex at
1427:42 a v okay to the right of the vertex at the bottom of the circle draw a v draw a
1427:44 the bottom of the circle draw a v draw a v um like what V nope not interactive
1427:48 v um like what V nope not interactive draw a v um no no
1427:58 well yeah it seems Weir weird let's get ready for maybe
1428:00 weird let's get ready for maybe something like step
1428:02 something like step five okay we'll go with that step five
1428:05 five okay we'll go with that step five someone else step five someone else
1428:08 someone else step five someone else someone else
1428:09 someone else yeah draw the right side of the leg okay
1428:13 yeah draw the right side of the leg okay nice step six step six happy face six
1428:19 nice step six step six happy face six six yes erase the line
1428:27 erase the line that you have on the left on the
1428:29 on the okay okay step seven yes instead of like
1428:33 okay okay step seven yes instead of like that line that was before going up make
1428:35 that line that was before going up make it go down instead of that line before
1428:37 it go down instead of that line before going up make it go down mhm okay step
1428:40 going up make it go down mhm okay step eight step eight step eight yes connect
1428:45 eight step eight step eight yes connect that line to the hip connect that line
1428:47 that line to the hip connect that line to the hip not like not touching not
1428:51 to the hip not like not touching not touching something like this
1428:53 touching something like this maybe okay compromise not touching okay
1428:57 maybe okay compromise not touching okay not touching okay all right step nine
1429:00 not touching okay all right step nine almost there I think step
1429:17 back uh write the word high on the top left of the circle
1429:19 left of the circle here okay
1429:22 here okay and step 10 almost there line draw a
1429:25 and step 10 almost there line draw a line pointing to high so like a spee
1429:28 line pointing to high so like a spee bubble basically
1429:30 bubble basically yeah okay and step uh 10
1429:34 yeah okay and step uh 10 11
1429:36 11 yeah erase the exclamation point nice 12
1429:41 yeah erase the exclamation point nice 12 do we want to give them one
1429:43 do we want to give them one more 12 or we good yeah last one erase
1429:49 more 12 or we good yeah last one erase the erase erace the right arm okay I
1429:52 the erase erace the right arm okay I think we're going to need a 13
1429:55 think we're going to need a 13 then and
1429:57 then and then yeah
1430:01 then yeah repat repeat the left arm but rotate it
1430:03 repat repeat the left arm but rotate it by
1430:13 wrong wait how would you like as an organic human being how put your
1430:16 organic human being how put your arms like would you put your would you
1430:18 arms like would you put your would you ever structure your arm like that that
1430:20 ever structure your arm like that that would not be a stick
1430:22 would not be a stick figure CU would you do this or would you
1430:25 figure CU would you do this or would you do that or a little hint maybe get a get
1430:29 do that or a little hint maybe get a get a give me a step 14 step 14 and final
1430:32 a give me a step 14 step 14 and final step
1430:34 step 14 I think we just got to tell them what
1430:36 14 I think we just got to tell them what to do step 14
1430:51 yes think of a walking man and and have the left the right hand
1430:55 and and have the left the right hand walking to your right all right so like
1430:59 walking to your right all right so like it's like where could the hand go where
1431:02 it's like where could the hand go where should the hand go on that
1431:04 should the hand go on that arm but yeah yeah okay yes
1431:09 arm but yeah yeah okay yes no yeah I mean look look look like right
1431:12 no yeah I mean look look look like right here look right here look right I yes
1431:14 here look right here look right I yes sorry thank you 14
1431:21 St that's pretty close so congratulations to you guys and thank
1431:23 congratulations to you guys and thank you as well all right so I mean these
1431:26 you as well all right so I mean these these things too are not yes Round of
1431:28 these things too are not yes Round of Applause then sure
1431:36 so so this is to say that these ideas of abstraction and precision and really
1431:38 abstraction and precision and really every other term of art that we explored
1431:40 every other term of art that we explored this term are sort of omnipresent and
1431:42 this term are sort of omnipresent and can be easier or harder to implement
1431:43 can be easier or harder to implement depending on exactly what the problem is
1431:46 depending on exactly what the problem is but what we thought we' do now in our uh
1431:48 but what we thought we' do now in our uh final day is try to now similarly
1431:50 final day is try to now similarly prepare prepare you for life after cs50
1431:53 prepare prepare you for life after cs50 and this is really going to be a list of
1431:56 and this is really going to be a list of really potential to-dos so that you can
1431:58 really potential to-dos so that you can stand on your own after the class after
1432:00 stand on your own after the class after the class's infrastructure write actual
1432:02 the class's infrastructure write actual code and then we'll come full circle one
1432:04 code and then we'll come full circle one final time with our friend Jennifer 8
1432:06 final time with our friend Jennifer 8 Lee to look at the world of emojis and
1432:08 Lee to look at the world of emojis and how they relate to all forms of
1432:10 how they relate to all forms of representation that we've talked to uh
1432:12 representation that we've talked to uh talked about up until now so one how can
1432:14 talked about up until now so one how can you go about programming after cs50 so
1432:16 you go about programming after cs50 so one you can actually install command
1432:18 one you can actually install command line tools on your own Mac or PC perhaps
1432:21 line tools on your own Mac or PC perhaps unbeknownst to you Windows has what's
1432:23 unbeknownst to you Windows has what's generally called a command prompt Mac OS
1432:25 generally called a command prompt Mac OS literally comes with a terminal program
1432:27 literally comes with a terminal program in your applications utilities folder
1432:29 in your applications utilities folder and so even if you've never run those
1432:30 and so even if you've never run those programs you've actually had a sort of
1432:32 programs you've actually had a sort of blinking cursor black and white prompt
1432:34 blinking cursor black and white prompt available to you might not have all of
1432:36 available to you might not have all of the same software installed as your code
1432:38 the same software installed as your code space in the cloud but you have that
1432:40 space in the cloud but you have that command line interface even within
1432:41 command line interface even within today's graphical tools and among the
1432:43 today's graphical tools and among the tools you can install within that
1432:45 tools you can install within that command line interface would be
1432:47 command line interface would be something called xcode on the Mac which
1432:48 something called xcode on the Mac which comes not only with a guey IDE
1432:51 comes not only with a guey IDE integrated development environment but
1432:52 integrated development environment but also those command line tools and
1432:54 also those command line tools and Microsoft for Windows has something
1432:55 Microsoft for Windows has something similar as well learning git so we've
1432:58 similar as well learning git so we've used git only unbeknownst to you
1433:00 used git only unbeknownst to you underneath the hood for the most part
1433:02 underneath the hood for the most part but git is a very very popular tool if
1433:05 but git is a very very popular tool if challenging to pick up for the first
1433:06 challenging to pick up for the first time that makes it easy to push code to
1433:08 time that makes it easy to push code to a website called GitHub or any
1433:10 a website called GitHub or any equivalent and then collaborate more
1433:12 equivalent and then collaborate more effectively with classmates there's
1433:13 effectively with classmates there's definitely a bit of a learning curve but
1433:15 definitely a bit of a learning curve but thanks to cs50's own Brian youu you can
1433:17 thanks to cs50's own Brian youu you can start for instance with a video like
1433:18 start for instance with a video like this and this indeed is going to be one
1433:20 this and this indeed is going to be one of these deao standards in the real
1433:21 of these deao standards in the real world at least for the next several
1433:23 world at least for the next several years that you'll probably encounter if
1433:25 years that you'll probably encounter if you work in Tech or really any company
1433:27 you work in Tech or really any company where you're doing some programming vs
1433:29 where you're doing some programming vs code itself will walk you through this
1433:30 code itself will walk you through this process in the coming days but you can
1433:32 process in the coming days but you can indeed install it on your own Mac or PC
1433:34 indeed install it on your own Mac or PC and what can you do when you write code
1433:36 and what can you do when you write code well you can certainly write software
1433:37 well you can certainly write software for your Mac for your PC for your phone
1433:40 for your Mac for your PC for your phone or of course per week 10 uh week nine
1433:42 or of course per week 10 uh week nine you can host uh your own website be it
1433:44 you can host uh your own website be it static as in week uh week eight um
1433:48 static as in week uh week eight um hosting it at websites like these which
1433:50 hosting it at websites like these which gener generally have free or
1433:51 gener generally have free or student-friendly accounts via which you
1433:53 student-friendly accounts via which you can put something statically on the web
1433:55 can put something statically on the web at a real domain name that you might
1433:57 at a real domain name that you might choose or you can host a full-fledged
1433:59 choose or you can host a full-fledged web app and using uh student tiers on
1434:02 web app and using uh student tiers on Amazon and Microsoft and Google's cloud
1434:04 Amazon and Microsoft and Google's cloud services or others you can sign up for
1434:06 services or others you can sign up for being a student certainly a whole lot of
1434:09 being a student certainly a whole lot of free software free hosting so as to if
1434:11 free software free hosting so as to if nothing else um experiment and uh
1434:13 nothing else um experiment and uh perhaps maximally get your own app or
1434:15 perhaps maximally get your own app or website up and running so know that
1434:17 website up and running so know that those are resources available to you and
1434:19 those are resources available to you and this is by uh certainly a non-exhaustive
1434:22 this is by uh certainly a non-exhaustive list if you'd like to geek out in the
1434:24 list if you'd like to geek out in the coming months in the coming years these
1434:26 coming months in the coming years these are just some of the places that people
1434:28 are just some of the places that people who take computer science classes who
1434:30 who take computer science classes who write code might tend to hang out and
1434:32 write code might tend to hang out and ask and answer questions of each other
1434:34 ask and answer questions of each other um so keep an eye for instance on these
1434:36 um so keep an eye for instance on these here and then cs50 has its own
1434:38 here and then cs50 has its own communities as you'll see if you go to
1434:39 communities as you'll see if you go to this URL here via the open coreware
1434:41 this URL here via the open coreware version of cs50 which is open to the
1434:43 version of cs50 which is open to the world do uh is there a Vibrant Community
1434:46 world do uh is there a Vibrant Community uh thanks to time zones that's pretty
1434:47 uh thanks to time zones that's pretty much active 247 365 talking about not
1434:50 much active 247 365 talking about not only cs50 going on in problem sets and
1434:52 only cs50 going on in problem sets and projects but really technology more
1434:54 projects but really technology more generally as well so certainly feel
1434:56 generally as well so certainly feel welcome to partake either asking or
1434:58 welcome to partake either asking or answering questions now in speaking of
1435:01 answering questions now in speaking of asking and answering questions a couple
1435:02 asking and answering questions a couple of weeks ago you kindly gave us a whole
1435:04 of weeks ago you kindly gave us a whole bunch of review questions which we
1435:07 bunch of review questions which we called through and picked out our
1435:08 called through and picked out our favorite 20 of them these of course were
1435:10 favorite 20 of them these of course were multiple choice questions and in
1435:12 multiple choice questions and in preparation for this week uh in
1435:14 preparation for this week uh in preparation for life ahead we thought we
1435:17 preparation for life ahead we thought we would choreograph a bit of a a quiz show
1435:19 would choreograph a bit of a a quiz show here and ceed as you came in at the
1435:21 here and ceed as you came in at the start of class you might recall being
1435:24 start of class you might recall being invited to go to this URL here
1435:26 invited to go to this URL here cs50. either here in person or if you're
1435:28 cs50. either here in person or if you're watching live from home at this URL here
1435:32 watching live from home at this URL here you can use a phone or a laptop and if
1435:36 you can use a phone or a laptop and if it's easier on a phone you can point
1435:37 it's easier on a phone you can point your camera at this 2D barcode here
1435:41 your camera at this 2D barcode here we'll give folks a moment to pull that
1435:43 we'll give folks a moment to pull that up and again that URL was cs50.
1435:51 l/p and once it looks like most folks have
1435:54 and once it looks like most folks have it up and running our friend Carter here
1435:58 it up and running our friend Carter here will help us dive into this uh review
1436:01 will help us dive into this uh review session if you will with a bit of fun
1436:04 session if you will with a bit of fun along the way all right Carter if you'd
1436:05 along the way all right Carter if you'd like to take it away what do we have as
1436:08 like to take it away what do we have as our first question you should see on
1436:10 our first question you should see on your phone or laptop this same question
1436:11 your phone or laptop this same question being asked the first question is how do
1436:13 being asked the first question is how do you print quote un quote hello world in
1436:16 you print quote un quote hello world in Python so among the possible answers are
1436:19 Python so among the possible answers are these here
1436:22 these here buzz in on your phone or your
1436:28 laptop we've got a few hundred responses are
1436:29 are ready 7 Seconds to make your
1436:37 decision this is question one of 20 go to it with some confidence I think we're
1436:38 to it with some confidence I think we're down to zero on the clock and Carter it
1436:41 down to zero on the clock and Carter it looks like 98% of you uh indeed said
1436:44 looks like 98% of you uh indeed said hello world and Carter per the check
1436:46 hello world and Carter per the check mark That's indeed the correct answer
1436:48 mark That's indeed the correct answer here now to make things interesting in
1436:50 here now to make things interesting in know that you'll see some number of
1436:52 know that you'll see some number of points and we've deliberately anonymized
1436:54 points and we've deliberately anonymized it so only you know what number you are
1436:56 it so only you know what number you are so a whole lot of guests have a perfect
1436:58 so a whole lot of guests have a perfect score of 1,000 at the moment hopefully
1437:01 score of 1,000 at the moment hopefully we'll see over the next several
1437:02 we'll see over the next several questions things start to bridge out uh
1437:05 questions things start to bridge out uh but know that the speed with which you
1437:07 but know that the speed with which you buzz in will also factor into how many
1437:09 buzz in will also factor into how many points you now get So the faster you
1437:11 points you now get So the faster you move the more points you get question
1437:13 move the more points you get question two if we could what does DNS stand for
1437:17 two if we could what does DNS stand for from just a couple of weeks back domain
1437:19 from just a couple of weeks back domain number system domain name system data
1437:22 number system domain name system data numbering structure or there's no such
1437:24 numbering structure or there's no such thing as
1437:26 thing as DNS few hundred responses are
1437:29 DNS few hundred responses are in 8 seconds
1437:33 in 8 seconds remain fewer points now but still a
1437:35 remain fewer points now but still a chance to buzz in and now as we hit zero
1437:39 chance to buzz in and now as we hit zero the responses are these domain name
1437:41 the responses are these domain name system which is indeed correct and 84%
1437:44 system which is indeed correct and 84% of you got that one correctly and indeed
1437:46 of you got that one correctly and indeed exists we talked about it a couple of
1437:48 exists we talked about it a couple of weeks ago so we're still seeing a whole
1437:50 weeks ago so we're still seeing a whole lot lot of ties at 2,000 we'll see if
1437:52 lot lot of ties at 2,000 we'll see if someone starts to pull away before long
1437:54 someone starts to pull away before long question three what is the upper bound
1437:56 question three what is the upper bound of merge sorts
1437:57 of merge sorts runtime so that escalated
1438:00 runtime so that escalated quickly Big O of n log n Big O of log n
1438:05 quickly Big O of n log n Big O of log n Theta or Omega of log n or Big O of
1438:09 Theta or Omega of log n or Big O of one what is the upper bound of merge
1438:12 one what is the upper bound of merge sorts
1438:14 sorts runtime that was the last of the
1438:15 runtime that was the last of the algorithms we solve for sorting and in 1
1438:18 algorithms we solve for sorting and in 1 second we'll see that the correct answer
1438:20 second we'll see that the correct answer is is just edging out everyone else
1438:22 is is just edging out everyone else indeed 46% it is n I know it's n log now
1438:29 indeed 46% it is n I know it's n log now if I may as the teacher it can't be
1438:31 if I may as the teacher it can't be login because login is strictly less
1438:33 login because login is strictly less than n and you can't possibly sort n
1438:35 than n and you can't possibly sort n elements unless you minimally look at or
1438:37 elements unless you minimally look at or touch each of them so it's got to be at
1438:39 touch each of them so it's got to be at least greater than n intuitively we
1438:41 least greater than n intuitively we still have a whole bunch of ties let's
1438:42 still have a whole bunch of ties let's move on to number four what is stored in
1438:46 move on to number four what is stored in ARG
1438:47 ARG C back to the language C is it in Array
1438:50 C back to the language C is it in Array of arguments the maximum size of an
1438:52 of arguments the maximum size of an array the count of arguments given to a
1438:53 array the count of arguments given to a program when first run or how much
1438:56 program when first run or how much memory is allocated to a
1438:58 memory is allocated to a function again you wrote all of these
1439:01 function again you wrote all of these questions and we have 5 seconds for the
1439:05 questions and we have 5 seconds for the reveal ARG C
1439:08 reveal ARG C is indeed the count of arguments given
1439:11 is indeed the count of arguments given to a program when first run think back
1439:13 to a program when first run think back to C when we did command line arguments
1439:15 to C when we did command line arguments there was argc and arv argv was the
1439:18 there was argc and arv argv was the array but Arc was indeed the count the
1439:21 array but Arc was indeed the count the CN AR save all right we still have a
1439:23 CN AR save all right we still have a whole bunch of ties at the top here but
1439:26 whole bunch of ties at the top here but let's move on then to number five what
1439:29 let's move on then to number five what is the duck debugger favorite hobby
1439:32 is the duck debugger favorite hobby according to one of your classmates
1439:34 according to one of your classmates dressing up like Dracula swimming across
1439:36 dressing up like Dracula swimming across the stage filling up the entire bathroom
1439:38 the stage filling up the entire bathroom of a guy's house and sitting quietly on
1439:43 of a guy's house and sitting quietly on stage the third of course is a reference
1439:45 stage the third of course is a reference to a YouTube video that was on the
1439:46 to a YouTube video that was on the course's website that week but according
1439:49 course's website that week but according to your class mate number two seconds
1439:53 to your class mate number two seconds remaining sitting quietly on stage is
1439:56 remaining sitting quietly on stage is its favorite pastime so a little harder
1439:59 its favorite pastime so a little harder perhaps than the
1440:01 perhaps than the others 5,000 now we're starting to see
1440:04 others 5,000 now we're starting to see some spread so we only have six six
1440:07 some spread so we only have six six guests in contention for first place and
1440:09 guests in contention for first place and the next question now is six what is the
1440:11 the next question now is six what is the function used to open a file in
1440:14 function used to open a file in C F open open file open
1440:19 C F open open file open file
1440:21 file what is the function used to open a file
1440:23 what is the function used to open a file in
1440:29 C 7 Seconds there's some differences between
1440:32 Seconds there's some differences between C and python here and the reveal it is
1440:36 C and python here and the reveal it is indeed fop it's 77% correct too all
1440:40 indeed fop it's 77% correct too all right let's see the rankings now if you
1440:42 right let's see the rankings now if you are guest 15 9715 6171 3753 or 3273
1440:47 are guest 15 9715 6171 3753 or 3273 you're now in the lead as we move on on
1440:50 you're now in the lead as we move on on to se question seven how does Sterling
1440:53 to se question seven how does Sterling compute the average sorry how does
1440:55 compute the average sorry how does Sterling compute the length of a string
1440:57 Sterling compute the length of a string in C it looks at how much memory the
1440:59 in C it looks at how much memory the string uses it counts the number of
1441:01 string uses it counts the number of characters until it reaches back0 it
1441:03 characters until it reaches back0 it counts the number of bits in the string
1441:05 counts the number of bits in the string or it creates pointers for each
1441:06 or it creates pointers for each character and counts
1441:08 character and counts them 10 seconds Sterling in
1441:13 them 10 seconds Sterling in C recall that we implemented this
1441:16 C recall that we implemented this ourselves in class but then we used the
1441:18 ourselves in class but then we used the library thereafter and in indeed with
1441:20 library thereafter and in indeed with 85% it simply counts the number of
1441:23 85% it simply counts the number of characters until it reaches that
1441:24 characters until it reaches that Sentinel back sl0 AKA null and in this
1441:28 Sentinel back sl0 AKA null and in this case we
1441:30 case we have five four four of you tied now for
1441:34 have five four four of you tied now for first all right question eight where
1441:36 first all right question eight where does Malo allocate memory
1441:39 does Malo allocate memory from the stack the Heap the pointers or
1441:43 from the stack the Heap the pointers or the
1441:44 the temp where does malok allocate memory
1441:49 temp where does malok allocate memory from responses are coming in 8
1441:58 seconds a good review question at that in two seconds we'll see that malok
1441:59 in two seconds we'll see that malok allocates memory from woo close one the
1442:02 allocates memory from woo close one the Heap is correct the Heap is correct the
1442:05 Heap is correct the Heap is correct the stack recall is where functions store
1442:07 stack recall is where functions store their local variables and their
1442:08 their local variables and their arguments and that just happens
1442:10 arguments and that just happens automatically the Heap represented in
1442:12 automatically the Heap represented in our pictures up top is where malok draws
1442:14 our pictures up top is where malok draws from now we have guest
1442:18 from now we have guest 15 has
1442:20 15 has made its way to the top here but others
1442:23 made its way to the top here but others can catch up if they don't buzz in fast
1442:25 can catch up if they don't buzz in fast enough so number nine how many people
1442:27 enough so number nine how many people flew from 50v to New York on the day of
1442:30 flew from 50v to New York on the day of the
1442:32 the crime 16 29 8 or
1442:42 three anyone with a laptop perhaps has an advantage
1442:51 here 5 Seconds and the answers are but the answer is
1442:57 and the answers are but the answer is 16 let's see if guest 15 got
1443:00 16 let's see if guest 15 got this they did not goodbye to guest 15 at
1443:05 this they did not goodbye to guest 15 at the top all right question 10 we're
1443:07 the top all right question 10 we're about halfway there what are meta tags
1443:10 about halfway there what are meta tags used for in
1443:11 used for in HTML to describe a web page to Define
1443:15 HTML to describe a web page to Define parameters for an element to group
1443:16 parameters for an element to group elements together to translate content
1443:18 elements together to translate content into machine readable format
1443:20 into machine readable format at what are meta tags used for in HTML
1443:24 at what are meta tags used for in HTML we saw a few of
1443:27 we saw a few of them for different use
1443:31 them for different use cases and with one second we see that
1443:34 cases and with one second we see that indeed the number one answer was to
1443:36 indeed the number one answer was to describe the web page be it for a mobile
1443:38 describe the web page be it for a mobile device be it for screen scrapers like
1443:41 device be it for screen scrapers like Facebook and Twitter and other such apps
1443:42 Facebook and Twitter and other such apps that grab images and descriptions
1443:44 that grab images and descriptions thereof all right we're in the second
1443:46 thereof all right we're in the second half now guest nine guest four 669 has
1443:51 half now guest nine guest four 669 has edged ahead guess 15 is now in sixth
1443:54 edged ahead guess 15 is now in sixth place all right number 11 is how do you
1443:56 place all right number 11 is how do you find the address of a variable in C
1443:59 find the address of a variable in C think back a few weeks star dollar sign
1444:02 think back a few weeks star dollar sign Ampersand or
1444:10 ask from one of your own classmates how do you find the address of a variable in
1444:22 C and the number one answer is ersan which is indeed the address of operator
1444:23 which is indeed the address of operator at 62% nicely done let's see who's the
1444:26 at 62% nicely done let's see who's the top of the list now guess 4669 has
1444:30 top of the list now guess 4669 has retained their lead so we move on to 12
1444:32 retained their lead so we move on to 12 what does the arrow operator mean in C a
1444:36 what does the arrow operator mean in C a hyphen and a greater than sign nothing
1444:39 hyphen and a greater than sign nothing starts a comment replaces a star and Dot
1444:42 starts a comment replaces a star and Dot operator declares a
1444:44 operator declares a pointer what does this Arrow operator
1444:47 pointer what does this Arrow operator mean in
1444:48 mean in C
1444:51 C again from a few weeks back 3 seconds
1444:55 again from a few weeks back 3 seconds harder assortment perhaps and it's oh
1444:59 harder assortment perhaps and it's oh replaces a star and Dot operator the
1445:02 replaces a star and Dot operator the number two answer was indeed correct
1445:04 number two answer was indeed correct this was just a cleaner way syntactic
1445:06 this was just a cleaner way syntactic sugar for collapsing what would be a
1445:08 sugar for collapsing what would be a star and then some parentheses and then
1445:10 star and then some parentheses and then a DOT into quite simply something that
1445:12 a DOT into quite simply something that looks like an arrow itself all right
1445:13 looks like an arrow itself all right Carter who's in the lead now still that
1445:16 Carter who's in the lead now still that same guest and let's see what 13 has for
1445:20 same guest and let's see what 13 has for us which of these is not a data type in
1445:22 us which of these is not a data type in SQL light blob string integer
1445:34 text we used a few of these more commonly than others but not all of
1445:36 commonly than others but not all of these are for
1445:38 these are for real 5 Seconds to make your
1445:42 real 5 Seconds to make your decision and the results are blob is a
1445:46 decision and the results are blob is a thing string is not in SQL light it's of
1445:49 thing string is not in SQL light it's of course called text as we've seen it blob
1445:53 course called text as we've seen it blob as goofy as it sounds is just binary
1445:55 as goofy as it sounds is just binary large object but indeed it's how you
1445:57 large object but indeed it's how you might store a binary file in your
1445:59 might store a binary file in your database all right the rankings now oh
1446:03 database all right the rankings now oh guest 8444 has Eed ahead so we move on
1446:06 guest 8444 has Eed ahead so we move on to 14 which of the following is a valid
1446:09 to 14 which of the following is a valid way to print Exclamation point
1446:11 way to print Exclamation point Exclamation point Exclamation point
1446:12 Exclamation point Exclamation point Exclamation point in
1446:15 Exclamation point in Python I'll let you read these
1446:18 Python I'll let you read these yourselves
1446:20 yourselves which is a valid
1446:22 which is a valid way everyone got quiet in is
1446:31 thinking all right 6 seconds few hundred responses in so
1446:34 responses in so far all right and yes nicely done 78% is
1446:38 far all right and yes nicely done 78% is correct you can use the star operator to
1446:40 correct you can use the star operator to essentially multiply the character at
1446:42 essentially multiply the character at left all right who's in the lead Carter
1446:45 left all right who's in the lead Carter we now have still G guest
1446:47 we now have still G guest 8444 and so we move on now to 15 what
1446:51 8444 and so we move on now to 15 what does the free function do D allocates
1446:54 does the free function do D allocates memory from A Primitive deallocates
1446:56 memory from A Primitive deallocates memory at the given pointer terminates a
1446:58 memory at the given pointer terminates a loop or returns a value from the
1447:00 loop or returns a value from the function what does the free function
1447:10 do all right 5 Seconds what does the free function do call it's the opposite
1447:12 free function do call it's the opposite essentially of malok and it deallocates
1447:15 essentially of malok and it deallocates the memory at the given pointer as well
1447:18 the memory at the given pointer as well and in C that's on you in Python you
1447:20 and in C that's on you in Python you don't need to worry about allocating or
1447:22 don't need to worry about allocating or freeing aik all right we now have at the
1447:26 freeing aik all right we now have at the top still guest 8444 and seeing more and
1447:29 top still guest 8444 and seeing more and more spread 16 which is not a step of
1447:34 more spread 16 which is not a step of compiling think back now to week two
1447:38 compiling think back now to week two compiling pre-processing linking or
1447:40 compiling pre-processing linking or threading which is not a step of
1447:44 threading which is not a step of compiling we use it as a catchall but it
1447:47 compiling we use it as a catchall but it technically means a few different things
1447:57 collectively all right and threading is indeed not on the list that is a
1447:59 indeed not on the list that is a technical thing generally meaning a
1448:01 technical thing generally meaning a program can do multiple things at once
1448:03 program can do multiple things at once but that is not related here to
1448:05 but that is not related here to compiling all right guest 8444 is still
1448:09 compiling all right guest 8444 is still at the top we have just a few questions
1448:11 at the top we have just a few questions left and so 17 what was the surprise at
1448:14 left and so 17 what was the surprise at the beginning of the Halloween lecture
1448:16 the beginning of the Halloween lecture according to your classmates someone
1448:18 according to your classmates someone scared me someone hid candy under every
1448:21 scared me someone hid candy under every chair someone dressed up as me the
1448:23 chair someone dressed up as me the entire staff dressed up as
1448:31 Carter interesting litmus test of who came to or watch lecture
1448:34 came to or watch lecture perhaps let's see which guests got this
1448:37 perhaps let's see which guests got this correct someone someone's really dressed
1448:40 correct someone someone's really dressed up as indeed me at 64% so I think we
1448:43 up as indeed me at 64% so I think we have attendance here essentially from
1448:45 have attendance here essentially from that lecture so let's move on now to the
1448:47 that lecture so let's move on now to the final few questions after seeing that
1448:49 final few questions after seeing that guest a 8444 8444 is still doing well
1448:52 guest a 8444 8444 is still doing well can anyone Dethrone them why is it
1448:54 can anyone Dethrone them why is it incorrect to use the equals equals
1448:56 incorrect to use the equals equals operator in C to compare
1448:59 operator in C to compare strings it's computationally inefficient
1449:02 strings it's computationally inefficient you comparing the locations of the
1449:03 you comparing the locations of the strings strings don't exist using equal
1449:06 strings strings don't exist using equal equals will lead to buffer
1449:13 overflow why is it incorrect to use equal equals in C to compare strings 3
1449:16 equal equals in C to compare strings 3 seconds we saw this live and it
1449:18 seconds we saw this live and it motivated like an entire week because
1449:20 motivated like an entire week because you're comparing the locations that is
1449:22 you're comparing the locations that is the addresses in memory of those same
1449:25 the addresses in memory of those same strings all right let's see if this
1449:27 strings all right let's see if this leaves the rankings the same guest 8444
1449:30 leaves the rankings the same guest 8444 is still at the top and pulling ahead
1449:32 is still at the top and pulling ahead two final questions 19 what is the
1449:35 two final questions 19 what is the difference between null one L and null 2
1449:39 difference between null one L and null 2 L's they null and null mean the same
1449:42 L's they null and null mean the same thing nulls refers to back sl0 whereas
1449:45 thing nulls refers to back sl0 whereas null to L's is the zero address null is
1449:47 null to L's is the zero address null is the zero address whereas null to L's
1449:49 the zero address whereas null to L's refers to back sl0 null is null but
1449:54 refers to back sl0 null is null but lazier 5
1449:56 lazier 5 Seconds subtle not the best design
1449:58 Seconds subtle not the best design perhaps to have in technical terms but
1450:02 perhaps to have in technical terms but indeed 62% of you got that NL is the
1450:05 indeed 62% of you got that NL is the first thing we talked about when we
1450:06 first thing we talked about when we talked about back sl0 and N is a pointer
1450:09 talked about back sl0 and N is a pointer it's the zero pointer same thing same
1450:12 it's the zero pointer same thing same number but different context all right
1450:14 number but different context all right Carter guest 8444 is the person to beat
1450:18 Carter guest 8444 is the person to beat with our final 20th questions what do
1450:21 with our final 20th questions what do the binary bulbs on stage spell
1450:31 today and these are your four choices different from usual we usually use 8
1450:33 different from usual we usually use 8 bit asky today we are using utf8 which
1450:37 bit asky today we are using utf8 which is a form of Unicode which is the larger
1450:40 is a form of Unicode which is the larger subset that uses one or two or three or
1450:42 subset that uses one or two or three or even four bites to spell a single
1450:46 even four bites to spell a single character and the answer wow close close
1450:49 character and the answer wow close close is indeed a cupcake indeed a cupcake
1450:53 is indeed a cupcake indeed a cupcake well done and let's see the final
1450:55 well done and let's see the final results 8444 is the winner are they here
1450:58 results 8444 is the winner are they here in person perhaps
1451:01 in person perhaps 8444 you're 844 come on
1451:05 8444 you're 844 come on [Applause]
1451:14 down thank you here you go congratulations oh you're all right so
1451:19 congratulations oh you're all right so today if we
1451:22 today if we may give me just one
1451:45 right all right so today we are so pleased to be joined by uh Jennifer 8
1451:48 pleased to be joined by uh Jennifer 8 Lee who's an of the college a dear
1451:50 Lee who's an of the college a dear friend and is actually really the reason
1451:52 friend and is actually really the reason why there's evidence of Muppets in cs50
1451:55 why there's evidence of Muppets in cs50 in fact some years ago I was visiting
1451:57 in fact some years ago I was visiting her and she had on her shelf like this
1451:59 her and she had on her shelf like this custom Muppet it wasn't one that appears
1452:01 custom Muppet it wasn't one that appears on TV but she had somehow gone on a
1452:03 on TV but she had somehow gone on a website former toy store called FAO
1452:05 website former toy store called FAO Schwarz at the time and you're allowed
1452:07 Schwarz at the time and you're allowed to configure your own Muppet whatnot
1452:09 to configure your own Muppet whatnot choose the eyes the nose the face and
1452:10 choose the eyes the nose the face and the Torso and I just thought this was
1452:12 the Torso and I just thought this was the coolest thing and so in the taxi on
1452:14 the coolest thing and so in the taxi on the way home I was like going on the
1452:15 the way home I was like going on the website trying to purchase our very
1452:17 website trying to purchase our very first Muppet I then woke up the next
1452:19 first Muppet I then woke up the next morning thinking why did I just buy a
1452:21 morning thinking why did I just buy a puppet in the back of a taxi and so it
1452:23 puppet in the back of a taxi and so it sat on the shelf for really 2 years and
1452:25 sat on the shelf for really 2 years and then a colleague of mine within cs50's
1452:26 then a colleague of mine within cs50's team decided after I brought it into the
1452:28 team decided after I brought it into the office to sit on a shelf there to
1452:30 office to sit on a shelf there to actually bring it to life and indeed if
1452:32 actually bring it to life and indeed if you Google around cs50 Muppet and
1452:34 you Google around cs50 Muppet and puppetry online you'll see in fact these
1452:36 puppetry online you'll see in fact these as characters not only over the past
1452:38 as characters not only over the past couple of years in coid times when
1452:39 couple of years in coid times when really there was next to no one actually
1452:41 really there was next to no one actually here and so they were instead um but
1452:44 here and so they were instead um but indeed she's brought not only this this
1452:46 indeed she's brought not only this this educational element this pedagogical
1452:47 educational element this pedagogical element this playful element El to cs50
1452:50 element this playful element El to cs50 and we have her here today to speak to
1452:52 and we have her here today to speak to exactly the sorts of encodings that are
1452:54 exactly the sorts of encodings that are here on stage Jenny is the former Vice
1452:56 here on stage Jenny is the former Vice chair of the Unicode subcommittee on
1452:58 chair of the Unicode subcommittee on emoji which is to say that she and her
1453:01 emoji which is to say that she and her colleagues have been influential in
1453:02 colleagues have been influential in taking emoji from what was a very
1453:04 taking emoji from what was a very limited character set early on and by
1453:06 limited character set early on and by far unrepresentative of much uh human
1453:09 far unrepresentative of much uh human emotion in speech into really an
1453:11 emotion in speech into really an initiative now to capture digitally all
1453:13 initiative now to capture digitally all of the world's languages past present
1453:16 of the world's languages past present and future as well as the range of
1453:18 and future as well as the range of emotions that might see here in the form
1453:20 emotions that might see here in the form of that pillow or even in the cake that
1453:22 of that pillow or even in the cake that awaits so allow me to introduce Jennifer
1453:24 awaits so allow me to introduce Jennifer 8 Lee thank
1453:27 8 Lee thank [Music]
1453:29 [Music] you I much drink okay clicker hi all
1453:33 you I much drink okay clicker hi all right hold on I have to hide my drinks I
1453:36 right hold on I have to hide my drinks I might need more water all right um I'm
1453:39 might need more water all right um I'm really excited to speak to speak here
1453:41 really excited to speak to speak here last time last year I was here uh one I
1453:44 last time last year I was here uh one I was wearing a mask which is like a real
1453:45 was wearing a mask which is like a real bummer if you're lecturing and then the
1453:46 bummer if you're lecturing and then the entire like front part was all Muppet so
1453:49 entire like front part was all Muppet so I'm really happy to see humans actually
1453:51 I'm really happy to see humans actually um and it's always an honor to speak at
1453:54 um and it's always an honor to speak at Sanders and and then Dave and I were
1453:57 Sanders and and then Dave and I were actually classmates way back when so I
1453:58 actually classmates way back when so I do remember him when he was an undergrad
1454:00 do remember him when he was an undergrad much like you so I um I'm going to give
1454:03 much like you so I um I'm going to give a talk on the world of emoji and how I
1454:05 a talk on the world of emoji and how I kind of became an emoji activist so it
1454:08 kind of became an emoji activist so it kind of all starts um with my friend e
1454:11 kind of all starts um with my friend e inl who is a designer who's well known
1454:14 inl who is a designer who's well known for doing the Twitter fail whale which
1454:16 for doing the Twitter fail whale which was kind of laid to rest except now this
1454:18 was kind of laid to rest except now this week it seems like it may be like may be
1454:21 week it seems like it may be like may be necessary again um so she and I one day
1454:24 necessary again um so she and I one day were texting this is back in 2015 we're
1454:26 were texting this is back in 2015 we're texting about dumplings because we are
1454:28 texting about dumplings because we are Chinese is women and we like like to
1454:30 Chinese is women and we like like to text about food and so I sent her this
1454:33 text about food and so I sent her this picture of dumplings she was like yum
1454:34 picture of dumplings she was like yum yum yum yum yum yum yum yum um and then
1454:38 yum yum yum yum yum yum yum um and then she was like oh Apple doesn't have a
1454:41 she was like oh Apple doesn't have a dumpling emoji and I was like oh that's
1454:43 dumpling emoji and I was like oh that's kind of interesting and didn't really
1454:44 kind of interesting and didn't really think anything about it because like you
1454:47 think anything about it because like you know people Point things out to you all
1454:48 know people Point things out to you all the time and then you just like forget
1454:51 the time and then you just like forget you just like move on but then half an
1454:52 you just like move on but then half an hour later um on my phone appears this
1454:56 hour later um on my phone appears this like dumpling with hard eyes and you
1454:57 like dumpling with hard eyes and you don't see it in in because it's a still
1454:59 don't see it in in because it's a still shot but it actually had like blinking
1455:01 shot but it actually had like blinking eyes so she liked to call it like bling
1455:02 eyes so she liked to call it like bling bling dumpling so she as a designer had
1455:05 bling dumpling so she as a designer had decided to like go in and like make her
1455:07 decided to like go in and like make her own dumpling Emoji um because she was
1455:10 own dumpling Emoji um because she was like I'm a designer I can fix it but
1455:11 like I'm a designer I can fix it but that actually got me thinking I was like
1455:14 that actually got me thinking I was like where do Emoji come from and like how is
1455:15 where do Emoji come from and like how is there not a dumpling Emoji cuz from my
1455:18 there not a dumpling Emoji cuz from my perspective dumplings are this kind of
1455:19 perspective dumplings are this kind of universal food right so and there are a
1455:23 universal food right so and there are a lot of Japanese Foods
1455:27 lot of Japanese Foods on the emoji keyboard and I I was not
1455:30 on the emoji keyboard and I I was not this was like back in 2015 I was like
1455:31 this was like back in 2015 I was like not a big Emoji user like at all so I
1455:34 not a big Emoji user like at all so I mean you have things like Ramen you have
1455:36 mean you have things like Ramen you have Bento boxes you have Curry you have
1455:38 Bento boxes you have Curry you have tempura you even have like kind of
1455:41 tempura you even have like kind of obscure um kind of foods like this thing
1455:44 obscure um kind of foods like this thing things on a stick turns out to be fish
1455:46 things on a stick turns out to be fish fish things on a stick then this pink
1455:48 fish things on a stick then this pink and white white swirly thing is also a
1455:51 and white white swirly thing is also a fish thing and there's even like that
1455:53 fish thing and there's even like that triangle rice ball that looks like it's
1455:54 triangle rice ball that looks like it's had a bikini wax all well represented on
1455:57 had a bikini wax all well represented on the Emoji Keyboard but no dumplings and
1456:00 the Emoji Keyboard but no dumplings and it's very strange cuz like all cultures
1456:02 it's very strange cuz like all cultures kind of have their dumpling right
1456:03 kind of have their dumpling right whether or not it's um kinali or ravioli
1456:06 whether or not it's um kinali or ravioli or aanas like essentially everyone sort
1456:08 or aanas like essentially everyone sort of like discover the idea of like um
1456:11 of like discover the idea of like um yummy goodness inside a carbo hydrate
1456:13 yummy goodness inside a carbo hydrate carbohydrate shell whether not baked or
1456:15 carbohydrate shell whether not baked or fried or um steamed so I was like okay I
1456:21 fried or um steamed so I was like okay I literally Google I was like who controls
1456:23 literally Google I was like who controls emoji and you discover that they're
1456:25 emoji and you discover that they're actually regulated by a nonprofit called
1456:27 actually regulated by a nonprofit called the Unicode Consortium and um it is you
1456:31 the Unicode Consortium and um it is you know and I just like went on their
1456:33 know and I just like went on their website and I discovered that they had
1456:35 website and I discovered that they had 12 full voting members as of 2015 so
1456:37 12 full voting members as of 2015 so this is 2015 and they were like mostly
1456:39 this is 2015 and they were like mostly us multinational tech companies it was
1456:41 us multinational tech companies it was Oracle it was IBM Microsoft Adobe Google
1456:45 Oracle it was IBM Microsoft Adobe Google Apple Facebook and Yahoo and of the
1456:48 Apple Facebook and Yahoo and of the three that were not multinational US
1456:51 three that were not multinational US tech companies they were let's see a
1456:54 tech companies they were let's see a German company called sap a Chinese
1456:56 German company called sap a Chinese Telecom company called Huawei and then
1456:58 Telecom company called Huawei and then like the government of Oman like those
1457:00 like the government of Oman like those were basically the 12 full voting
1457:02 were basically the 12 full voting members of the US multi-national tech
1457:05 members of the US multi-national tech companies so they at that point paid um
1457:09 companies so they at that point paid um $118,000 a year to have full voting
1457:12 $118,000 a year to have full voting power on the you know Unicode committee
1457:14 power on the you know Unicode committee and I was like oh that's a that's a lot
1457:16 and I was like oh that's a that's a lot of money and I kind of felt indignant
1457:18 of money and I kind of felt indignant about this and uh but then like if you
1457:21 about this and uh but then like if you kind of keep on digging on their website
1457:23 kind of keep on digging on their website you found like there was this kind of
1457:24 you found like there was this kind of interesting loophole which is you could
1457:26 interesting loophole which is you could join as an individual for
1457:28 join as an individual for $75 um you don't get voting power but it
1457:32 $75 um you don't get voting power but it gave you the right to put yourself on
1457:34 gave you the right to put yourself on the email list and also to like attend
1457:36 the email list and also to like attend the quarterly Unicode meeting so I was
1457:38 the quarterly Unicode meeting so I was like I'll do that I had no idea what I
1457:39 like I'll do that I had no idea what I was doing but I'm like I'm going to go
1457:41 was doing but I'm like I'm going to go fight um for this dumpling Emoji because
1457:43 fight um for this dumpling Emoji because from my perspective dumplings are
1457:45 from my perspective dumplings are Universal Emoji are kind of universal so
1457:49 Universal Emoji are kind of universal so uh the fact there was no dumpling Emoji
1457:51 uh the fact there was no dumpling Emoji meant like something was wrong in the
1457:52 meant like something was wrong in the universe and I was I was determined to
1457:54 universe and I was I was determined to fix this so um you know I was on this
1457:58 fix this so um you know I was on this email list and then a couple of like
1457:59 email list and then a couple of like maybe even like a couple weeks later I
1458:01 maybe even like a couple weeks later I got you know they they they kind of sent
1458:03 got you know they they they kind of sent out this note that's like hey who's
1458:04 out this note that's like hey who's coming to the quarterly meeting and I
1458:07 coming to the quarterly meeting and I was like um I looked at the calendar I
1458:09 was like um I looked at the calendar I looked at my schedule I was like oh I'll
1458:10 looked at my schedule I was like oh I'll be in like you know Silicon Valley that
1458:13 be in like you know Silicon Valley that time so I basically like rsvpd and I was
1458:16 time so I basically like rsvpd and I was like I will be there and took um cow
1458:19 like I will be there and took um cow train to an Apple building it's a legal
1458:21 train to an Apple building it's a legal building in I think it was sunny Veil so
1458:23 building in I think it was sunny Veil so I just like show up and I don't know
1458:25 I just like show up and I don't know what I was sort of expecting like with
1458:27 what I was sort of expecting like with like you know the Unicode I think maybe
1458:28 like you know the Unicode I think maybe thought it was going to be like a baby
1458:29 thought it was going to be like a baby Congress like you know like with a
1458:31 Congress like you know like with a little you know very formal seats people
1458:33 little you know very formal seats people with gabbles um that is not what I found
1458:35 with gabbles um that is not what I found uh basically it is a it was a conference
1458:38 uh basically it is a it was a conference room full of people who skewed wider
1458:40 room full of people who skewed wider skewed older skewed Mal skewed engineers
1458:42 skewed older skewed Mal skewed engineers and this is basically the room where it
1458:43 and this is basically the room where it happen so this is 2015 these were the
1458:46 happen so this is 2015 these were the people who decided your Emoji um all
1458:49 people who decided your Emoji um all very nice and um there was you know one
1458:52 very nice and um there was you know one one even had a daughter who had a sense
1458:53 one even had a daughter who had a sense of humor and made him a shirt that said
1458:55 of humor and made him a shirt that said shadowy Emoji Overlord um so I just kind
1458:59 shadowy Emoji Overlord um so I just kind of listen to them debate things like
1459:01 of listen to them debate things like milk emoji and beans emoji and it just
1459:04 milk emoji and beans emoji and it just seemed like not quite right to me that
1459:06 seemed like not quite right to me that like it would be uh This Global visual
1459:09 like it would be uh This Global visual language that were basically decided
1459:11 language that were basically decided decided by like a a small group of
1459:13 decided by like a a small group of people inside a conference room in
1459:15 people inside a conference room in Silicon Valley so I decided to former
1459:18 Silicon Valley so I decided to former group called Emoji nation whose motto is
1459:20 group called Emoji nation whose motto is like Emoji by the people for the people
1459:22 like Emoji by the people for the people and it basically advocates for more kind
1459:24 and it basically advocates for more kind of Representative inclusive Emoji um you
1459:28 of Representative inclusive Emoji um you know we we we we start with a
1459:29 know we we we we start with a Kickstarter campaign uh dumpling Emoji
1459:32 Kickstarter campaign uh dumpling Emoji process trying to like you know write
1459:34 process trying to like you know write the wrong in this world and uh made this
1459:36 the wrong in this world and uh made this little cute video sort of advocating
1459:38 little cute video sort of advocating dink for one of the most universal
1459:40 dink for one of the most universal crosscultural Foods in the world Georgia
1459:42 crosscultural Foods in the world Georgia has Kali Japan has giosa Korea has Mandu
1459:46 has Kali Japan has giosa Korea has Mandu Italy has ravioli Poland has barog
1459:48 Italy has ravioli Poland has barog Russia has pelman Argentina has
1459:50 Russia has pelman Argentina has empanadas Jewish people have kler China
1459:53 empanadas Jewish people have kler China has pot stickers Nepal and Tibet have
1459:56 has pot stickers Nepal and Tibet have Momos yet somehow despite their
1459:58 Momos yet somehow despite their popularity there is no dumpling Emoji in
1460:00 popularity there is no dumpling Emoji in the standard set why is that emoji
1460:03 the standard set why is that emoji exists for pizza tempora Sushi spaghetti
1460:06 exists for pizza tempora Sushi spaghetti hot dog and now tacos which Taco Bell
1460:08 hot dog and now tacos which Taco Bell takes credit for we need to write this
1460:10 takes credit for we need to write this disparity dumplings are Global Emoji a
1460:13 disparity dumplings are Global Emoji a global isn't it time we brought them
1460:17 global isn't it time we brought them together
1460:18 together oh yeah and while we're at it how about
1460:20 oh yeah and while we're at it how about an emoji for Chinese
1460:27 takeout so uh I did put together a dumpling Emoji proposal I wrote this uh
1460:29 dumpling Emoji proposal I wrote this uh I remember Thanksgiving Day 2015 on a
1460:31 I remember Thanksgiving Day 2015 on a plane and uh actually and we got it past
1460:35 plane and uh actually and we got it past basically dumpling takeout box
1460:37 basically dumpling takeout box Chopsticks and uh fortune cookie I have
1460:40 Chopsticks and uh fortune cookie I have to say I don't think fortune cookie
1460:42 to say I don't think fortune cookie would have made it on its own merits but
1460:44 would have made it on its own merits but it kind of like slid in on the clo tails
1460:46 it kind of like slid in on the clo tails of the other ones um and so these were
1460:48 of the other ones um and so these were the proposals as we submitted them and
1460:50 the proposals as we submitted them and then these are the ones that kind of uh
1460:52 then these are the ones that kind of uh exist now on the Apple keyboard and I
1460:54 exist now on the Apple keyboard and I have to say the dumpling looks really
1460:55 have to say the dumpling looks really really realistic um oddly realistic and
1460:59 really realistic um oddly realistic and whereas like the fortune cookie is think
1461:00 whereas like the fortune cookie is think it's like a big fail because first of
1461:02 it's like a big fail because first of all there's like it has no it has no Gap
1461:04 all there's like it has no it has no Gap it looks like a dead 3D Pac-Man so I'm
1461:06 it looks like a dead 3D Pac-Man so I'm very
1461:07 very disappointed in uh the manifestation of
1461:10 disappointed in uh the manifestation of that but that's okay that's okay um and
1461:13 that but that's okay that's okay um and so it's kind of interesting like what is
1461:15 so it's kind of interesting like what is the process of getting an emoji um ped
1461:18 the process of getting an emoji um ped and I will sort of walk you through it
1461:20 and I will sort of walk you through it so first of all you come up with your
1461:22 so first of all you come up with your idea right and then you kind of write
1461:25 idea right and then you kind of write this proposal and then you submit it to
1461:27 this proposal and then you submit it to the Unicode Emoji subcommittee who then
1461:29 the Unicode Emoji subcommittee who then kind of gives you comments and then
1461:30 kind of gives you comments and then sends it back to you and you kind of go
1461:32 sends it back to you and you kind of go around and around in the circle um and
1461:36 around and around in the circle um and when so these are things that we
1461:38 when so these are things that we consider so somewhere in there I also
1461:39 consider so somewhere in there I also like fought my way on to the Emoji
1461:41 like fought my way on to the Emoji committee and then also became a vice
1461:43 committee and then also became a vice chair became a vice chair sort of a
1461:45 chair became a vice chair sort of a extracurricular that's like completely
1461:46 extracurricular that's like completely run a muck in my life um um so things
1461:49 run a muck in my life um um so things that matter uh popular demand is a
1461:52 that matter uh popular demand is a frequently requested Emoji um multiple
1461:55 frequently requested Emoji um multiple usages usages and meaning so that's
1461:58 usages usages and meaning so that's actually kind of very important for
1461:59 actually kind of very important for something like you know certain animals
1462:01 something like you know certain animals have meaning so we did you know sloth a
1462:05 have meaning so we did you know sloth a while ago and that also has not only the
1462:07 while ago and that also has not only the literal meaning but sort of like um like
1462:10 literal meaning but sort of like um like um connotations there are visually
1462:13 um connotations there are visually distinctiveness it can be recognized so
1462:15 distinctiveness it can be recognized so this was a blocker for one of the uh
1462:17 this was a blocker for one of the uh emojis that I have worked on before
1462:20 emojis that I have worked on before which is cave which is kind of hard to
1462:21 which is cave which is kind of hard to do I think you know meaning wise is
1462:24 do I think you know meaning wise is pretty good but it's really hard to get
1462:25 pretty good but it's really hard to get a cave down in emoji sizes and then it
1462:28 a cave down in emoji sizes and then it filling a gap completeness so a good
1462:30 filling a gap completeness so a good example for that is like for some reason
1462:32 example for that is like for some reason for many years we had Red Heart yellow
1462:34 for many years we had Red Heart yellow heart green heart blue heart purple
1462:36 heart green heart blue heart purple heart and uh no orange heart so so
1462:39 heart and uh no orange heart so so somewhat so you know you would do the
1462:41 somewhat so you know you would do the rainbow but people would substitute the
1462:43 rainbow but people would substitute the pumpkin in so You' like you know have a
1462:44 pumpkin in so You' like you know have a have a rainbow heart thing with a little
1462:46 have a rainbow heart thing with a little pumpkin stuck in the middle so orange
1462:48 pumpkin stuck in the middle so orange heart obviously should be added and give
1462:51 heart obviously should be added and give a sense of completeness um and then
1462:53 a sense of completeness um and then something else is existing vendor cap
1462:55 something else is existing vendor cap cap compatibility and so a good example
1462:57 cap compatibility and so a good example for that was um many years ago what's
1463:01 for that was um many years ago what's app decided to uh add the gender
1463:04 app decided to uh add the gender non-binary emoji and then once it did
1463:06 non-binary emoji and then once it did that then all the other kind of vendors
1463:08 that then all the other kind of vendors um jumped on um so what kind of knocks
1463:12 um jumped on um so what kind of knocks out an emoji so too specific or narrow
1463:16 out an emoji so too specific or narrow so we'll often see that with like very
1463:19 so we'll often see that with like very specific animals or a very specific
1463:21 specific animals or a very specific group it's redundant so one
1463:23 group it's redundant so one year oh my God who makes that Butterball
1463:25 year oh my God who makes that Butterball Butterball makes the turkeys Butterball
1463:27 Butterball makes the turkeys Butterball submitted um a an emoji proposal that
1463:30 submitted um a an emoji proposal that was like a cooked turkey but we already
1463:32 was like a cooked turkey but we already had a live turkey so it seem kind of
1463:34 had a live turkey so it seem kind of redundant have both like a cooked turkey
1463:35 redundant have both like a cooked turkey for Thanksgiving and a live turkey so so
1463:38 for Thanksgiving and a live turkey so so not visually discernible um this is a
1463:41 not visually discernible um this is a struggle for things like I don't my
1463:42 struggle for things like I don't my friends have kind of proposed
1463:46 friends have kind of proposed kimchi kimchi is is really hard on emoji
1463:49 kimchi kimchi is is really hard on emoji sizes for many reasons and part of that
1463:51 sizes for many reasons and part of that but part of that tension is because it's
1463:53 but part of that tension is because it's not visually discernable then there are
1463:54 not visually discernable then there are no logos Brands deities or celebrities
1463:57 no logos Brands deities or celebrities so no Nike swish no McDonald's M and
1464:00 so no Nike swish no McDonald's M and then this is one that we kind of decided
1464:01 then this is one that we kind of decided in the last uh year or so which is no
1464:04 in the last uh year or so which is no more flags flags are a very complicated
1464:06 more flags flags are a very complicated thing um and as a result Unico does not
1464:09 thing um and as a result Unico does not want to be in the business of deciding
1464:10 want to be in the business of deciding what is a country or not a country uh so
1464:12 what is a country or not a country uh so like you know when you get a proposal
1464:13 like you know when you get a proposal from like Kurdistan you're like yeah so
1464:16 from like Kurdistan you're like yeah so right now the way that the Emoji flags
1464:18 right now the way that the Emoji flags are decided is they kind of depend on
1464:20 are decided is they kind of depend on what the UN recognizes and then those
1464:22 what the UN recognizes and then those get passed down to the international
1464:24 get passed down to the international standards organization and then and then
1464:25 standards organization and then and then Uno just does that like it does not want
1464:27 Uno just does that like it does not want to be in the business of kind of you
1464:29 to be in the business of kind of you know geopolitical
1464:31 know geopolitical Affairs um so once it comes out of the
1464:34 Affairs um so once it comes out of the subcommittee it goes to the full Unicode
1464:37 subcommittee it goes to the full Unicode technical committee UTC those were the
1464:39 technical committee UTC those were the people that were in the room that I
1464:40 people that were in the room that I showed you um and they vote once a year
1464:45 showed you um and they vote once a year basically to pass all the Emoji and
1464:48 basically to pass all the Emoji and takes a while there's a lot of like
1464:49 takes a while there's a lot of like coordinating with the international
1464:50 coordinating with the international standards organization and like a lot of
1464:53 standards organization and like a lot of working with the companies and
1464:54 working with the companies and eventually it takes a long time but it
1464:56 eventually it takes a long time but it ends up on your on your devices and that
1464:59 ends up on your on your devices and that historically is taken about 18 to 24
1465:00 historically is taken about 18 to 24 months from when you put in the propose
1465:02 months from when you put in the propose to when you get it in it's going to be a
1465:03 to when you get it in it's going to be a little bit longer um going forward
1465:06 little bit longer um going forward probably is my sense as we were're going
1465:09 probably is my sense as we were're going to Unicode has decided to sort of space
1465:11 to Unicode has decided to sort of space out some of the Unicode releases so yeah
1465:14 out some of the Unicode releases so yeah Emoji Nation you know kind of has done
1465:16 Emoji Nation you know kind of has done its thing so of the weird things is like
1465:18 its thing so of the weird things is like how did Unicode this like kind of
1465:20 how did Unicode this like kind of nonprofit organization based in Mountain
1465:22 nonprofit organization based in Mountain View California end up controlling this
1465:24 View California end up controlling this like Global visual language um so a lot
1465:27 like Global visual language um so a lot of it has to start with has has to do
1465:29 of it has to start with has has to do with the fact that emoji started in
1465:31 with the fact that emoji started in Japan uh back in the late 1990s one of
1465:33 Japan uh back in the late 1990s one of the this set from dok 1999 is considered
1465:38 the this set from dok 1999 is considered is widely considered sort of like the
1465:39 is widely considered sort of like the first like color Emoji set it has been
1465:41 first like color Emoji set it has been collected by the Museum of Modern Art
1465:44 collected by the Museum of Modern Art and um so these the Japanese telecom
1465:46 and um so these the Japanese telecom companies would use
1465:48 companies would use basically would have their own sets of
1465:50 basically would have their own sets of emoji and then they were different
1465:52 emoji and then they were different companies so they would have different
1465:53 companies so they would have different sets so you could basically only send
1465:55 sets so you could basically only send these like visual characters with
1465:56 these like visual characters with someone who is on your same carrier so
1465:59 someone who is on your same carrier so it's like it's like basically equivalent
1466:01 it's like it's like basically equivalent of if you were on like Verizon you can
1466:02 of if you were on like Verizon you can only text people on Verizon with like
1466:05 only text people on Verizon with like Emoji or if you were on um T-Mobile you
1466:07 Emoji or if you were on um T-Mobile you could only do that so at a certain point
1466:10 could only do that so at a certain point they um decided they were they the Apple
1466:14 they um decided they were they the Apple and Google came into Japan they wanted
1466:16 and Google came into Japan they wanted to start selling smartphones and they
1466:18 to start selling smartphones and they realized that it was a hodgepodge of
1466:21 realized that it was a hodgepodge of systems and they wanted to unify it and
1466:23 systems and they wanted to unify it and so in 2007 they went to unic code and
1466:26 so in 2007 they went to unic code and they're like okay help us unify the
1466:29 they're like okay help us unify the Emoji um like kind of like basically all
1466:33 Emoji um like kind of like basically all the Emojis so that we have one standard
1466:34 the Emojis so that we have one standard system and part of the reason is why
1466:37 system and part of the reason is why Unicode because Unicode basically has
1466:39 Unicode because Unicode basically has this mission is to enable everyone
1466:41 this mission is to enable everyone speaking every language on Earth to be
1466:43 speaking every language on Earth to be able to use your languages on computer
1466:44 able to use your languages on computer and smartphone so it basically unifies
1466:47 and smartphone so it basically unifies um all written languages into one
1466:50 um all written languages into one ginormous set and that was not the case
1466:52 ginormous set and that was not the case actually when I was uh growing up there
1466:54 actually when I was uh growing up there was a point where like if you you were
1466:56 was a point where like if you you were Japanese on Apple that would be
1466:58 Japanese on Apple that would be different than Japanese on um dos or
1467:00 different than Japanese on um dos or like Chinese or Arabic so it drove
1467:03 like Chinese or Arabic so it drove everyone crazy and they basically
1467:04 everyone crazy and they basically decided um around the late 80s early
1467:07 decided um around the late 80s early 1990s that they were going to come up
1467:09 1990s that they were going to come up with one standardized system that sort
1467:11 with one standardized system that sort of encoded all characters in in one
1467:15 of encoded all characters in in one ginormous set so um there's three main
1467:18 ginormous set so um there's three main projects for Unicode if you care so one
1467:20 projects for Unicode if you care so one is encoding characters including Emoji
1467:22 is encoding characters including Emoji now they're about 100,000 characters
1467:24 now they're about 100,000 characters aside assigned so that includes like
1467:27 aside assigned so that includes like like Chinese Japanese Korean uh Arabic
1467:30 like Chinese Japanese Korean uh Arabic cilic um actually all the hieroglyphics
1467:33 cilic um actually all the hieroglyphics all of the Emoji um a lot of things like
1467:36 all of the Emoji um a lot of things like the Bitcoin symbol or like copy left or
1467:38 the Bitcoin symbol or like copy left or whatever those all assigned were about
1467:40 whatever those all assigned were about 100,000 uh characters even those
1467:43 100,000 uh characters even those languages that are basically out of use
1467:45 languages that are basically out of use so the other thing it does it creates
1467:48 so the other thing it does it creates um localization resources so things
1467:51 um localization resources so things things so that you know like oh if
1467:53 things so that you know like oh if you're in this country this is this is
1467:55 you're in this country this is this is uh you're using the Euro or you're using
1467:58 uh you're using the Euro or you're using the pesos or something so there's a lot
1467:59 the pesos or something so there's a lot of localization that is that data that
1468:02 of localization that is that data that is needed depending on which um
1468:05 is needed depending on which um geography you're using your device from
1468:07 geography you're using your device from or like that you know you you know that
1468:08 or like that you know you you know that the time is used this way or the dates
1468:10 the time is used this way or the dates are shown that way so that is called um
1468:12 are shown that way so that is called um the common local data repository or cldr
1468:16 the common local data repository or cldr as they call it and the other thing they
1468:17 as they call it and the other thing they do is they kind of maintain libraries
1468:19 do is they kind of maintain libraries for developers in order to do all um to
1468:22 for developers in order to do all um to kind of so they are not building
1468:23 kind of so they are not building everything from scratch and that is
1468:25 everything from scratch and that is called ICU so it's very funny cuz like
1468:27 called ICU so it's very funny cuz like it's called cldr and then one day one of
1468:30 it's called cldr and then one day one of our friends girlfriends made him this
1468:33 our friends girlfriends made him this thing which she called a seal deer
1468:37 thing which she called a seal deer because whenever he talked about
1468:39 because whenever he talked about cldr he thought he thought she thought
1468:41 cldr he thought he thought she thought he was talking about seal deers so now
1468:44 he was talking about seal deers so now this is his little like mascot for like
1468:46 this is his little like mascot for like Unicode related things so what is um how
1468:49 Unicode related things so what is um how does Unicode do this so it basically
1468:50 does Unicode do this so it basically asss code points and each code point is
1468:53 asss code points and each code point is a unique number assigned to each Unicode
1468:56 a unique number assigned to each Unicode character so uh face with tears of joy
1469:00 character so uh face with tears of joy can be written like this with a code
1469:02 can be written like this with a code point or it can be written like this in
1469:04 point or it can be written like this in terms of decimal numbers and it can also
1469:05 terms of decimal numbers and it can also be written this in binary numbers so
1469:07 be written this in binary numbers so these are all basically the same and the
1469:09 these are all basically the same and the key thing to know at least about Emoji
1469:12 key thing to know at least about Emoji is that when your device if you're
1469:13 is that when your device if you're talking your iPhone with um someone
1469:16 talking your iPhone with um someone who's on a laptop or an iPad or like um
1469:18 who's on a laptop or an iPad or like um an Android device it's not sending the
1469:20 an Android device it's not sending the images back and forth it's sending just
1469:22 images back and forth it's sending just a number back and forth and then locally
1469:24 a number back and forth and then locally your phone or like your laptop decides
1469:27 your phone or like your laptop decides like oh this number correlates with
1469:29 like oh this number correlates with which image um in terms of our emoji
1469:32 which image um in terms of our emoji font and then pulls it up so this is
1469:34 font and then pulls it up so this is really key to know why different Emoji
1469:37 really key to know why different Emoji look different on different
1469:39 look different on different platforms so 2007 to 2010 it took about
1469:43 platforms so 2007 to 2010 it took about three years but Unicode 6.0 came out
1469:45 three years but Unicode 6.0 came out with a first our first little baby set
1469:46 with a first our first little baby set of emoji um and it just kind of hung out
1469:49 of emoji um and it just kind of hung out there for a year like no one it wasn't
1469:50 there for a year like no one it wasn't doing anything so 2011 though Apple
1469:53 doing anything so 2011 though Apple starts adding the emoji keyboard and it
1469:56 starts adding the emoji keyboard and it it just like explodes like I feel like
1469:58 it just like explodes like I feel like in some ways Emoji are were not like
1470:01 in some ways Emoji are were not like invented they were discovered they like
1470:03 invented they were discovered they like obviously touch something very very
1470:05 obviously touch something very very Primal to um to like our human desire to
1470:09 Primal to um to like our human desire to like communicate in like little colorful
1470:11 like communicate in like little colorful glyphs on electronic devices and you
1470:14 glyphs on electronic devices and you kind of have what's kind of really
1470:15 kind of have what's kind of really interesting is like the ambiguity that
1470:18 interesting is like the ambiguity that comes with what emoji kind of mean and
1470:21 comes with what emoji kind of mean and so one of the you know this one my
1470:22 so one of the you know this one my favorite emoji is sort of like an upside
1470:24 favorite emoji is sort of like an upside down smiley face very very ambiguous
1470:26 down smiley face very very ambiguous clearly very ambiguous because if you
1470:27 clearly very ambiguous because if you start typing into Google like the top
1470:29 start typing into Google like the top hits are like what does it mean from a
1470:31 hits are like what does it mean from a guy what does it mean from a girl like
1470:32 guy what does it mean from a girl like it's clearly something that a lot of
1470:34 it's clearly something that a lot of people are are using in like complex
1470:36 people are are using in like complex situationships between each other so um
1470:41 situationships between each other so um so so one of the fun things is who can
1470:42 so so one of the fun things is who can propose Emoji technically anyone can
1470:45 propose Emoji technically anyone can anyone here can normal normal humans can
1470:47 anyone here can normal normal humans can we have basically Google doc uh or
1470:49 we have basically Google doc uh or Google form that we throw up uh
1470:51 Google form that we throw up uh historically between August sorry April
1470:54 historically between August sorry April and August in the last two years um and
1470:57 and August in the last two years um and uh so this is one of our my our favorite
1471:00 uh so this is one of our my our favorite examples this is Ru aumed she was a
1471:02 examples this is Ru aumed she was a 15-year-old Saudi Arabian girl who was
1471:04 15-year-old Saudi Arabian girl who was living in Vienna at the time that she
1471:06 living in Vienna at the time that she proposed the um hijab emoji and then you
1471:10 proposed the um hijab emoji and then you know then she was like Time Magazine
1471:12 know then she was like Time Magazine like you know coolest teens she got like
1471:13 like you know coolest teens she got like a whole bunch of different things she
1471:14 a whole bunch of different things she got into Harvard and Stanford and she
1471:16 got into Harvard and Stanford and she went to Stanford um and uh so this is a
1471:20 went to Stanford um and uh so this is a PR we got then there was a group of
1471:22 PR we got then there was a group of folks from Argentina who got the mate
1471:24 folks from Argentina who got the mate Emoji kind of they you know similar
1471:26 Emoji kind of they you know similar their national drink then we worked with
1471:29 their national drink then we worked with um there was a a nonprofit that really
1471:31 um there was a a nonprofit that really wanted to like get a menstruation emoji
1471:36 wanted to like get a menstruation emoji and so what they proposed to begin with
1471:38 and so what they proposed to begin with was like bloody underwear and I was just
1471:40 was like bloody underwear and I was just like no no no so many different reasons
1471:43 like no no no so many different reasons why this is a terrible Emoji uh but so
1471:46 why this is a terrible Emoji uh but so we did get blood drops so then you can
1471:47 we did get blood drops so then you can do Moon and blood drop or underwear and
1471:49 do Moon and blood drop or underwear and blood drop there's a lot of different
1471:51 blood drop there's a lot of different ways actually it's really funny cuz I
1471:52 ways actually it's really funny cuz I was ping my friends beforehand what they
1471:55 was ping my friends beforehand what they would use to indicate menstruation
1471:57 would use to indicate menstruation before there you know there was a blood
1471:59 before there you know there was a blood drop so there was like red wine there
1472:01 drop so there was like red wine there was like kind of that Rose um with the
1472:05 was like kind of that Rose um with the like falling petal and then my favorite
1472:07 like falling petal and then my favorite is actually um my friend who used a
1472:09 is actually um my friend who used a Japanese flag as a way to indicate that
1472:11 Japanese flag as a way to indicate that she was having her
1472:13 she was having her period so um one of the biggest
1472:16 period so um one of the biggest contributors this uh skin tone Emoji
1472:19 contributors this uh skin tone Emoji appeared I think in 2015 it was amazing
1472:22 appeared I think in 2015 it was amazing it is and it was proposed by a mom
1472:25 it is and it was proposed by a mom Katrina parrot she is a entrepreneur and
1472:27 Katrina parrot she is a entrepreneur and a mom who is just like at home one day
1472:29 a mom who is just like at home one day and her daughter comes home and is
1472:32 and her daughter comes home and is like you know I wish there were emoji
1472:35 like you know I wish there were emoji that looked like me and her mom was like
1472:37 that looked like me and her mom was like that's great honey what's an emoji and
1472:40 that's great honey what's an emoji and so she like me I guess Googled and just
1472:43 so she like me I guess Googled and just figured out that uh Unicode controlled
1472:45 figured out that uh Unicode controlled emoji and she just came up with a
1472:47 emoji and she just came up with a proposal saying we should not only have
1472:48 proposal saying we should not only have the yellow you know skin tones at that
1472:50 the yellow you know skin tones at that time everything was sort of Simpsons
1472:51 time everything was sort of Simpsons yellow um it's really interesting to see
1472:55 yellow um it's really interesting to see how race and like nationality are like
1472:59 how race and like nationality are like depicted in different parts of the world
1473:01 depicted in different parts of the world so originally in Japan everyone was
1473:04 so originally in Japan everyone was yellow um but this these were the non
1473:07 yellow um but this these were the non you know by default everyone was just
1473:08 you know by default everyone was just like like you know human or Japanese but
1473:11 like like you know human or Japanese but they had like a couple things that were
1473:12 they had like a couple things that were like not like one was you had a blonde
1473:15 like not like one was you had a blonde person so there's an emoji called like
1473:17 person so there's an emoji called like blonde blonde woman or whatever that
1473:19 blonde blonde woman or whatever that represents all westerners okay so that
1473:21 represents all westerners okay so that was one and then they have one that's
1473:23 was one and then they have one that's like an Indian guy with a turban so
1473:24 like an Indian guy with a turban so that's supposed to represent Indian
1473:25 that's supposed to represent Indian people and then there's like a like a
1473:28 people and then there's like a like a guy with a little like um little hat
1473:30 guy with a little like um little hat that's supposed to represent like
1473:31 that's supposed to represent like Chinese people so that was like that was
1473:33 Chinese people so that was like that was the view Japanese view of race which was
1473:35 the view Japanese view of race which was like default then you were I then you
1473:37 like default then you were I then you were like blonde Western Chinese or
1473:39 were like blonde Western Chinese or Indian and and that is all there was and
1473:42 Indian and and that is all there was and um obviously in the United States we
1473:44 um obviously in the United States we care a lot about race and then so she
1473:46 care a lot about race and then so she came up with this system with five skin
1473:48 came up with this system with five skin tones um like just like normal people
1473:51 tones um like just like normal people some guy in Germany decided that he
1473:52 some guy in Germany decided that he wanted to do a a a face with one eyebrow
1473:56 wanted to do a a a face with one eyebrow raised or as we call it the coar Emoji
1473:59 raised or as we call it the coar Emoji um and oh this one's fun so woman's flat
1474:04 um and oh this one's fun so woman's flat shoe I have to say not highly used
1474:06 shoe I have to say not highly used statistically at this point but I really
1474:08 statistically at this point but I really kind of appreciate it because it was a
1474:09 kind of appreciate it because it was a mom who was very offended that all
1474:11 mom who was very offended that all women's shoes had heels even the sandals
1474:14 women's shoes had heels even the sandals so this is her she had like three kids
1474:16 so this is her she had like three kids at the time now is four she was very
1474:18 at the time now is four she was very fertile um and she also did um the women
1474:25 fertile um and she also did um the women women's flat okay she also did um um one
1474:30 women's flat okay she also did um um one piece bathing suit because she was also
1474:32 piece bathing suit because she was also um offended by the fact that the only
1474:34 um offended by the fact that the only kind of bathing suit you had was like
1474:35 kind of bathing suit you had was like this like little itsy-bitsy polka dot
1474:38 this like little itsy-bitsy polka dot bikini thing which is not great if
1474:39 bikini thing which is not great if you're like taking your six-year-old so
1474:41 you're like taking your six-year-old so I have to say that got passed but like
1474:42 I have to say that got passed but like it didn't go over like super well with
1474:44 it didn't go over like super well with everyone um so you know unic Cod because
1474:46 everyone um so you know unic Cod because it's very public submits things for
1474:48 it's very public submits things for comments and um we got this comment back
1474:51 comments and um we got this comment back so one piece bathing suit why a person
1474:54 so one piece bathing suit why a person want to indicate the use of swimwear
1474:56 want to indicate the use of swimwear can't use existing bikini is this really
1474:58 can't use existing bikini is this really necessary what about a Victorian bathing
1475:01 necessary what about a Victorian bathing costume or a wet suit or water rings
1475:03 costume or a wet suit or water rings this is like literally in in like uh the
1475:06 this is like literally in in like uh the records and like do not encode um and so
1475:10 records and like do not encode um and so the person who did it's actually very
1475:11 the person who did it's actually very impressive he's actually the person who
1475:13 impressive he's actually the person who created the middle finger emoji and
1475:16 created the middle finger emoji and actually you ever seen the Vulcan Emoji
1475:18 actually you ever seen the Vulcan Emoji he he the Vulcan hand emoji he's
1475:20 he he the Vulcan hand emoji he's actually very active and I have to say
1475:23 actually very active and I have to say this is actually I think one of the more
1475:24 this is actually I think one of the more impressive Emoji so so obviously
1475:26 impressive Emoji so so obviously obviously we have a lot of active debate
1475:29 obviously we have a lot of active debate um sometimes you get like whole
1475:30 um sometimes you get like whole countries submitting so literally the
1475:32 countries submitting so literally the government of Finland as in like their
1475:34 government of Finland as in like their equivalent in the state department their
1475:35 equivalent in the state department their diplomacy kind of thing uh submitted a
1475:38 diplomacy kind of thing uh submitted a proposal for SAA and um these were so
1475:42 proposal for SAA and um these were so creepy and so
1475:46 creepy and so weird so much is wrong with this and but
1475:50 weird so much is wrong with this and but I I I felt where they were coming from
1475:52 I I I felt where they were coming from first of all like they're naked they
1475:54 first of all like they're naked they have no they have club feet and so we
1475:57 have no they have club feet and so we decided to help them we're like okay we
1475:59 decided to help them we're like okay we see where you're going with this let's
1476:01 see where you're going with this let's see if we can like help you like come up
1476:03 see if we can like help you like come up you know like we had the spoon like
1476:05 you know like we had the spoon like should there be like steam around them
1476:07 should there be like steam around them like should they be naked or wearing a
1476:08 like should they be naked or wearing a tow towel it's like super like dicey but
1476:10 tow towel it's like super like dicey but we wanted to help them because it was
1476:11 we wanted to help them because it was like literally a foreign government
1476:13 like literally a foreign government coming from an Unicode advocating for
1476:15 coming from an Unicode advocating for the sauna emoji on behalf of their ENT
1476:17 the sauna emoji on behalf of their ENT country so then this is um is sort of
1476:19 country so then this is um is sort of evolved into just basically person in
1476:21 evolved into just basically person in stey room which is which is the most
1476:23 stey room which is which is the most sort of like the PG version of sauna no
1476:25 sort of like the PG version of sauna no and there's no spoon they're all dressed
1476:27 and there's no spoon they're all dressed it's very odd but um so you can see the
1476:30 it's very odd but um so you can see the evolution of what it started out what we
1476:31 evolution of what it started out what we submitted and what it ended up so
1476:34 submitted and what it ended up so there's a lot of like Evolution
1476:36 there's a lot of like Evolution throughout the entire process um and
1476:39 throughout the entire process um and like companies can submit Emoji
1476:41 like companies can submit Emoji proposals too so Google actually worked
1476:43 proposals too so Google actually worked on this one I love this one okay so just
1476:44 on this one I love this one okay so just to give you some context as of 2015
1476:47 to give you some context as of 2015 there are many ways you could be or have
1476:49 there are many ways you could be or have an occupation as a male on the Emoji
1476:52 an occupation as a male on the Emoji Keyboard right like like for example you
1476:55 Keyboard right like like for example you could be a police officer you could be a
1476:57 could be a police officer you could be a detective you could be a Buckingham
1476:59 detective you could be a Buckingham Palace guard you could even be Santa
1477:01 Palace guard you could even be Santa Claus like these are so many jobs that
1477:03 Claus like these are so many jobs that you could have but if you were a woman
1477:04 you could have but if you were a woman as of 2015 there were four things that
1477:06 as of 2015 there were four things that you could be you could be a princess you
1477:08 you could be you could be a princess you could be a bride you could be a dancer
1477:11 could be a bride you could be a dancer or you could be a Playboy Bunny these
1477:12 or you could be a Playboy Bunny these were the sum total of all the
1477:14 were the sum total of all the occupations that we can have so so we
1477:18 occupations that we can have so so we there was sort of this movement at that
1477:19 there was sort of this movement at that time there was like this like video on
1477:21 time there was like this like video on YouTube that like went viral there was
1477:23 YouTube that like went viral there was like a New York Times op that was like
1477:25 like a New York Times op that was like where are the women with professions so
1477:27 where are the women with professions so um basically they came up with a set of
1477:29 um basically they came up with a set of emoji for professions and what's nice is
1477:32 emoji for professions and what's nice is not only did women have these
1477:34 not only did women have these professions now men have them too so of
1477:36 professions now men have them too so of emoji Nation Emoji these are some of the
1477:38 emoji Nation Emoji these are some of the ones that we've worked on I think about
1477:40 ones that we've worked on I think about 130 of the Emoji on your keyboard
1477:42 130 of the Emoji on your keyboard probably came through touched our system
1477:44 probably came through touched our system in in some way including I I have to say
1477:48 in in some way including I I have to say uh microbe or virus I think I have the
1477:50 uh microbe or virus I think I have the opinion that every Emoji has its day
1477:52 opinion that every Emoji has its day right like it might not be like today it
1477:54 right like it might not be like today it might not be next year but I have to say
1477:56 might not be next year but I have to say virus was not doing anything then came
1477:58 virus was not doing anything then came 2020 and that was like such a good
1478:00 2020 and that was like such a good moment for it um along with soap we had
1478:02 moment for it um along with soap we had also done soap so um you know among the
1478:06 also done soap so um you know among the other emoji that we have worked on are
1478:07 other emoji that we have worked on are sari moon cake llama like uh teddy bear
1478:10 sari moon cake llama like uh teddy bear there were no toys I felt really sad for
1478:12 there were no toys I felt really sad for toys we have like giraffe um there was
1478:15 toys we have like giraffe um there was Hut bubble te bubble tea was very
1478:18 Hut bubble te bubble tea was very controversial actually I have to say it
1478:20 controversial actually I have to say it kind we tried to slide it in originally
1478:22 kind we tried to slide it in originally with the um takeout box and the
1478:24 with the um takeout box and the dumplings and people were not not having
1478:26 dumplings and people were not not having it I understand that CU there's not a
1478:28 it I understand that CU there's not a lot like compared to like beer or wine
1478:30 lot like compared to like beer or wine like bubble tea does not is not long
1478:32 like bubble tea does not is not long does not have a long history on this
1478:33 does not have a long history on this planet um but I will say that um they
1478:39 planet um but I will say that um they submitted again actually kind of
1478:41 submitted again actually kind of originally proposing that it was not
1478:43 originally proposing that it was not just bubble tea but but like a black
1478:45 just bubble tea but but like a black ball and milk and tea it was It was kind
1478:47 ball and milk and tea it was It was kind of cool and I I have to say there was
1478:49 of cool and I I have to say there was definitely a generational divide between
1478:51 definitely a generational divide between like the Asian women who sit in that
1478:53 like the Asian women who sit in that room and are like this absolutely is a
1478:54 room and are like this absolutely is a thing that we consume like almost like
1478:56 thing that we consume like almost like every week of our life and people who
1478:58 every week of our life and people who are a little bit older who are like that
1479:00 are a little bit older who are like that looks like a parfait how do you not know
1479:02 looks like a parfait how do you not know that's a parfet and we're like we
1479:03 that's a parfet and we're like we absolutely know it is uh not a parfait
1479:05 absolutely know it is uh not a parfait and so so it got in eventually so it
1479:07 and so so it got in eventually so it does sort of influence it kind of shows
1479:09 does sort of influence it kind of shows like who is in the room influences you
1479:12 like who is in the room influences you know the decisions um that get made or
1479:14 know the decisions um that get made or sometimes in the room now sometimes more
1479:16 sometimes in the room now sometimes more more likely in the zoom um I actually I
1479:18 more likely in the zoom um I actually I just say beaver Emoji if you see Beaver
1479:21 just say beaver Emoji if you see Beaver Emoji that's one of one of the ones I'm
1479:22 Emoji that's one of one of the ones I'm most proud of so that is actually
1479:24 most proud of so that is actually co-authored by a professor here at
1479:26 co-authored by a professor here at Harvard um who is both lesbian and was
1479:29 Harvard um who is both lesbian and was married to a woman from Canada so it was
1479:31 married to a woman from Canada so it was very important to her to get um the
1479:33 very important to her to get um the beaver Emoji passed and she promised me
1479:36 beaver Emoji passed and she promised me it would always be the first line of her
1479:38 it would always be the first line of her bio so and indeed if you go to her
1479:40 bio so and indeed if you go to her Twitter handle uh it's like Joan Donovan
1479:43 Twitter handle uh it's like Joan Donovan creator of the Beaver Emoji comma is
1479:46 creator of the Beaver Emoji comma is headed research at the shoren scene
1479:47 headed research at the shoren scene Center at the Harvard Kennedy School
1479:49 Center at the Harvard Kennedy School It's Kind it's pretty impressive um and
1479:52 It's Kind it's pretty impressive um and then we did greens actually greens was
1479:54 then we did greens actually greens was really interesting because people this
1479:57 really interesting because people this was also like a generational cultural
1479:58 was also like a generational cultural thing people were like why do we need
1480:00 thing people were like why do we need greens we have salad and I was like
1480:02 greens we have salad and I was like we're Chinese we don't eat raw greens
1480:04 we're Chinese we don't eat raw greens cuz like you don't know where it's been
1480:06 cuz like you don't know where it's been or if it's clean so we cook our greens
1480:08 or if it's clean so we cook our greens so salad is not something that we have
1480:09 so salad is not something that we have So eventually I got my greens and um so
1480:13 So eventually I got my greens and um so that was kind of fun and then so these
1480:14 that was kind of fun and then so these are some of the people who sort of have
1480:16 are some of the people who sort of have contributed to our little Emoji Nation
1480:18 contributed to our little Emoji Nation things including a number of Native
1480:19 things including a number of Native Americans who help get feather so why do
1480:22 Americans who help get feather so why do I care so part of it is because I'm
1480:23 I care so part of it is because I'm Chinese I grew up speaking Chinese and
1480:25 Chinese I grew up speaking Chinese and English at the same time and it's really
1480:26 English at the same time and it's really interesting to see like Chinese and
1480:28 interesting to see like Chinese and English characters in terms of emoji and
1480:31 English characters in terms of emoji and Chinese together right so this is fire
1480:33 Chinese together right so this is fire you have like Fire characters and now
1480:35 you have like Fire characters and now you have the Emoji things and it's kind
1480:36 you have the Emoji things and it's kind of It kind of shows that there is a
1480:38 of It kind of shows that there is a longevity in The Human Experience of
1480:40 longevity in The Human Experience of something that was designed 4,000 years
1480:41 something that was designed 4,000 years ago as the same visual cues in the human
1480:43 ago as the same visual cues in the human experiences as now so mouth Tre tree
1480:47 experiences as now so mouth Tre tree Moon Sun you can mix and match them
1480:50 Moon Sun you can mix and match them which is super fun um so you know two
1480:54 which is super fun um so you know two trees kind of make a forest oops sorry
1480:57 trees kind of make a forest oops sorry oh well okay then the um Moon and Sun
1481:01 oh well okay then the um Moon and Sun together means bright which I like um I
1481:07 together means bright which I like um I like this one so if you stop and you
1481:08 like this one so if you stop and you think about this so this is a basically
1481:12 think about this so this is a basically a pig under a roof and what does that
1481:16 a pig under a roof and what does that mean
1481:17 mean it does not mean Farm as you might think
1481:19 it does not mean Farm as you might think it um it actually means home or family
1481:22 it um it actually means home or family so in the Chinese kind of structure and
1481:25 so in the Chinese kind of structure and outlook on The View it's like where you
1481:26 outlook on The View it's like where you keep your pigs is actually where your
1481:29 keep your pigs is actually where your home is and what your farm is um so it
1481:33 home is and what your farm is um so it gets kind of weird in all kinds of ways
1481:35 gets kind of weird in all kinds of ways so one of my favorite radicals so this
1481:37 so one of my favorite radicals so this character means woman uh KN
1481:42 character means woman uh KN and as I was learning Chinese you kind
1481:44 and as I was learning Chinese you kind of notice like how it shows up so so
1481:46 of notice like how it shows up so so this is a woman underneath a roof and
1481:49 this is a woman underneath a roof and you're like oh it means mom or wife or
1481:52 you're like oh it means mom or wife or or like whatever like home um it does
1481:55 or like whatever like home um it does not it means peace because things are at
1481:59 not it means peace because things are at home when the woman is or things are at
1482:02 home when the woman is or things are at peace when the woman is at home
1482:04 peace when the woman is at home underneath a roof which I always thought
1482:06 underneath a roof which I always thought was a little bit odd um then there is
1482:09 was a little bit odd um then there is also uh woman plus child so you're like
1482:13 also uh woman plus child so you're like oh and actually specifically Boy Child
1482:15 oh and actually specifically Boy Child the connotation there is a little
1482:16 the connotation there is a little unclear so you're like a woman plus
1482:18 unclear so you're like a woman plus child family mom you know whatever um it
1482:21 child family mom you know whatever um it is not it means good so the standard for
1482:23 is not it means good so the standard for goodness in ancient China was a woman
1482:25 goodness in ancient China was a woman who had a male child child so that kind
1482:27 who had a male child child so that kind of kind of just like kind of irked me um
1482:30 of kind of just like kind of irked me um growing up and then you know three women
1482:32 growing up and then you know three women together means evil which is like very
1482:34 together means evil which is like very like
1482:36 like MCB this character means greed this
1482:40 MCB this character means greed this character means slave this marriage
1482:43 character means slave this marriage let's see I think this one is jealousy
1482:45 let's see I think this one is jealousy and this means means adultery or
1482:47 and this means means adultery or betrayal so like definitely not loving
1482:51 betrayal so like definitely not loving the way women were P you know portrayed
1482:53 the way women were P you know portrayed um on the emoji keyboard so uh in case
1482:56 um on the emoji keyboard so uh in case you're wondering we just came out with a
1482:57 you're wondering we just came out with a kids book called had emoji that kind of
1482:58 kids book called had emoji that kind of Compares emoji and uh Chinese and I
1483:01 Compares emoji and uh Chinese and I think I I think they sent a bunch of
1483:03 think I I think they sent a bunch of books so that you guys can do some kind
1483:05 books so that you guys can do some kind of contest some like later on with cs50
1483:08 of contest some like later on with cs50 um so but the mixing and matching is
1483:10 um so but the mixing and matching is really interesting right for example the
1483:13 really interesting right for example the skin tones are actually the same yellow
1483:16 skin tones are actually the same yellow character plus a layer of skin tone on
1483:18 character plus a layer of skin tone on top of this so I kind of took my lessons
1483:20 top of this so I kind of took my lessons from Chinese in terms of seeing how
1483:22 from Chinese in terms of seeing how things can be binded so there's
1483:23 things can be binded so there's something you should know about which is
1483:25 something you should know about which is zge this is also an invisible emoji
1483:27 zge this is also an invisible emoji character it stands for zero with Joiner
1483:30 character it stands for zero with Joiner and it was actually originally created
1483:31 and it was actually originally created mostly for I think Arabic where you
1483:34 mostly for I think Arabic where you would you would basically kind of force
1483:36 would you would basically kind of force something to have like be in the
1483:37 something to have like be in the beginning of of a word or an end of the
1483:39 beginning of of a word or an end of the word by kind of having this like
1483:41 word by kind of having this like invisible character so the rainbow flag
1483:43 invisible character so the rainbow flag for example is actually a rainbow plus
1483:45 for example is actually a rainbow plus the white flag
1483:47 the white flag um and we we could have all kinds of fun
1483:50 um and we we could have all kinds of fun combinations if you look at polar bear
1483:52 combinations if you look at polar bear it actually is if you have an older
1483:54 it actually is if you have an older device or it breaks apart it is bear
1483:56 device or it breaks apart it is bear plus snow which is really cute it was
1483:59 plus snow which is really cute it was originally I had bear plus white and
1484:01 originally I had bear plus white and then we decided that bear plus snow made
1484:04 then we decided that bear plus snow made a lot more sense so uh another one this
1484:06 a lot more sense so uh another one this is new I I think if you guys should have
1484:09 is new I I think if you guys should have it if you've updated your phones in the
1484:11 it if you've updated your phones in the last year or so so mending heart is
1484:13 last year or so so mending heart is heart plus
1484:15 heart plus Band-Aid um um what is this one oh this
1484:17 Band-Aid um um what is this one oh this is interesting um there was a
1484:20 is interesting um there was a breastfeeding woman for a long time and
1484:22 breastfeeding woman for a long time and people felt like there was not gender
1484:23 people felt like there was not gender par was actually really interesting all
1484:24 par was actually really interesting all the people who wrote it and were like I
1484:26 the people who wrote it and were like I want to I want to be a dad showing that
1484:28 want to I want to be a dad showing that I'm holding my baby why is there only a
1484:30 I'm holding my baby why is there only a woman holding my baby so so we kind of
1484:32 woman holding my baby so so we kind of created a whole set so this is man plus
1484:36 created a whole set so this is man plus bottle Tada um and so this is and all of
1484:39 bottle Tada um and so this is and all of these occupations are actually often
1484:41 these occupations are actually often times a woman plus like you know the
1484:43 times a woman plus like you know the fried egg or like um a school or a
1484:47 fried egg or like um a school or a tractor and that's how you got the
1484:48 tractor and that's how you got the occupations if you if you if you send an
1484:50 occupations if you if you if you send an emoji over into an older system
1484:53 emoji over into an older system sometimes you'll it'll break apart so
1484:55 sometimes you'll it'll break apart so one of my kind of favorite kind of
1484:56 one of my kind of favorite kind of contributions in this in this world is
1484:58 contributions in this in this world is interracial couple Emoji which we did
1485:00 interracial couple Emoji which we did with um Tinder which is super fun
1485:03 with um Tinder which is super fun because then you could have you know
1485:04 because then you could have you know different couples that are and and like
1485:06 different couples that are and and like so many combin this is like this is a
1485:08 so many combin this is like this is a fun if you guys ever have to do a
1485:10 fun if you guys ever have to do a combinatoric test um this is really fun
1485:12 combinatoric test um this is really fun because you have two genders plus a
1485:14 because you have two genders plus a third like gender plus two people plus
1485:18 third like gender plus two people plus five skin tones plus yellow like how
1485:20 five skin tones plus yellow like how many Emoji couples can you come come up
1485:22 many Emoji couples can you come come up with when you introduce this Factor so
1485:25 with when you introduce this Factor so this is um and underneath it it's just a
1485:27 this is um and underneath it it's just a wige sequence it's like two people
1485:28 wige sequence it's like two people standing together that are like glued
1485:30 standing together that are like glued together now this actually gets
1485:32 together now this actually gets interesting from a cs-50 perspective
1485:34 interesting from a cs-50 perspective because in many cases even though you
1485:36 because in many cases even though you only see one character underneath the
1485:38 only see one character underneath the hood depending on how your system works
1485:41 hood depending on how your system works they're counting each one of these as an
1485:42 they're counting each one of these as an individual character so your string
1485:44 individual character so your string length actually might be five and
1485:46 length actually might be five and instead of one and this this kind of
1485:47 instead of one and this this kind of became a problem with things like
1485:49 became a problem with things like Twitter where things had a hard skin um
1485:51 Twitter where things had a hard skin um length so gender inclusivity um is
1485:53 length so gender inclusivity um is actually one of the things that we've
1485:54 actually one of the things that we've been dealing most with um in the last
1485:58 been dealing most with um in the last couple years so it's kind of interesting
1486:00 couple years so it's kind of interesting if you think about both what a pictorial
1486:01 if you think about both what a pictorial language looks like versus the
1486:03 language looks like versus the abstractness of a of a spoken language
1486:06 abstractness of a of a spoken language so because you know we had boy and we
1486:08 so because you know we had boy and we had
1486:08 had girl but there was no way to say a
1486:11 girl but there was no way to say a generic child right like if you were on
1486:13 generic child right like if you were on you wanted to say child you had to pick
1486:14 you wanted to say child you had to pick a boy or a girl but not a way to say
1486:16 a boy or a girl but not a way to say like just some little person and that's
1486:19 like just some little person and that's really key cuz in English at least there
1486:21 really key cuz in English at least there is no gender implied by
1486:24 is no gender implied by child um so how do we mimic that and it
1486:27 child um so how do we mimic that and it also is key for something like doctor
1486:29 also is key for something like doctor right doctor and teacher those are those
1486:32 right doctor and teacher those are those have don't have gender implied but when
1486:35 have don't have gender implied but when we have them on on the Emoji Keyboard
1486:36 we have them on on the Emoji Keyboard you to pick a male teacher or um you
1486:39 you to pick a male teacher or um you know a a a female doctor or whatnot so
1486:43 know a a a female doctor or whatnot so there was actually uh a guy at Adobe who
1486:45 there was actually uh a guy at Adobe who can considers himself non-gender binary
1486:47 can considers himself non-gender binary also The Man Behind the orange um the
1486:50 also The Man Behind the orange um the orange heart he fought and got basically
1486:55 orange heart he fought and got basically uh the first three non-gender uh
1486:58 uh the first three non-gender uh non-gender binary emojis so child adult
1487:01 non-gender binary emojis so child adult and old older adults so those are
1487:03 and old older adults so those are creative and then we started having to
1487:05 creative and then we started having to propagate them through actually all the
1487:07 propagate them through actually all the occupations so these are the gender
1487:08 occupations so these are the gender neutral versions of many of those um but
1487:11 neutral versions of many of those um but then uh we got into this whole thing
1487:13 then uh we got into this whole thing where every emoji that had a gender
1487:15 where every emoji that had a gender originally had to be mirrored so
1487:17 originally had to be mirrored so originally we had bearded man and then
1487:19 originally we had bearded man and then we're like okay well we actually have to
1487:20 we're like okay well we actually have to get bearded woman so that is on your
1487:22 get bearded woman so that is on your keyboard there is pregnant woman there
1487:24 keyboard there is pregnant woman there is now pregnant man which is interesting
1487:28 is now pregnant man which is interesting um there is you know woman in a bridal
1487:31 um there is you know woman in a bridal gown there's now man in a bridal gown um
1487:34 gown there's now man in a bridal gown um and then there were ones that actually
1487:35 and then there were ones that actually had to be created that were neither man
1487:38 had to be created that were neither man or woman so this is a mer person so
1487:40 or woman so this is a mer person so there was merman there was mermaid and
1487:41 there was merman there was mermaid and there was M person it was really
1487:42 there was M person it was really interesting to figure out like how do
1487:43 interesting to figure out like how do you draw a gender neutral M person like
1487:46 you draw a gender neutral M person like a bunch of them in the beginning
1487:47 a bunch of them in the beginning actually had the arms crossed around
1487:49 actually had the arms crossed around sort of the chest um Monarch so there
1487:51 sort of the chest um Monarch so there was prince and princess and now there is
1487:53 was prince and princess and now there is monarch and one of my favorite actually
1487:55 monarch and one of my favorite actually is there was Santa Claus and there was
1487:57 is there was Santa Claus and there was Mrs Claus and now there's MX Claus like
1487:59 Mrs Claus and now there's MX Claus like the name of this character literally in
1488:01 the name of this character literally in Unicode is MX claw so I feel like it's
1488:03 Unicode is MX claw so I feel like it's sort of like a very official enshrining
1488:05 sort of like a very official enshrining of gender non biner in like the world um
1488:09 of gender non biner in like the world um not everyone loved it New York Post did
1488:10 not everyone loved it New York Post did not love this that they like you know
1488:12 not love this that they like you know we're we're cing into like Emoji woke
1488:14 we're we're cing into like Emoji woke Wars um so some Emoji stats for you this
1488:18 Wars um so some Emoji stats for you this is very fascinating this is sort of like
1488:19 is very fascinating this is sort of like the Gen the General Distribution by far
1488:22 the Gen the General Distribution by far the single emoji that used more than
1488:24 the single emoji that used more than anything else is the face with tears of
1488:26 anything else is the face with tears of joy about 10% of all Emoji scent is that
1488:29 joy about 10% of all Emoji scent is that one character and then number two is
1488:31 one character and then number two is heart red heart and then it kind of goes
1488:33 heart red heart and then it kind of goes down so um there's a frequency of emoji
1488:36 down so um there's a frequency of emoji use these this is sort of done by um
1488:39 use these this is sort of done by um order magnitude so one is half of two
1488:43 order magnitude so one is half of two two is half of one so it's really
1488:46 two is half of one so it's really interesting it's a very very steep drop
1488:47 interesting it's a very very steep drop off after the first couple um in case
1488:49 off after the first couple um in case you ever want to go onto the the Emoji
1488:52 you ever want to go onto the the Emoji kind of Unicode website you can you too
1488:54 kind of Unicode website you can you too can see all the frequency things so I
1488:57 can see all the frequency things so I think it's really funny so basically
1488:58 think it's really funny so basically it's green going this way it increased
1489:01 it's green going this way it increased in usage between 2019 and uh 2021 and
1489:03 in usage between 2019 and uh 2021 and it's red going this way it drop and so
1489:06 it's red going this way it drop and so pleading face which is a relatively new
1489:08 pleading face which is a relatively new emoe you just sort of shut up on the
1489:10 emoe you just sort of shut up on the charts um and whereas actually like
1489:14 charts um and whereas actually like smiling face with heart eyes
1489:16 smiling face with heart eyes like kind of kind of slipped which is
1489:19 like kind of kind of slipped which is interesting so we just closed our Emoji
1489:21 interesting so we just closed our Emoji proposal round for 2022 these were sort
1489:23 proposal round for 2022 these were sort of the breakdowns people love submitting
1489:25 of the breakdowns people love submitting Smileys and food and beverages animals
1489:27 Smileys and food and beverages animals and nature da da da I don't I mean these
1489:29 and nature da da da I don't I mean these are very googly colors um so what is the
1489:31 are very googly colors um so what is the future of emoji I will um I will tell
1489:34 future of emoji I will um I will tell you because we just had a meeting two
1489:35 you because we just had a meeting two weeks ago so I can now publicly talk
1489:37 weeks ago so I can now publicly talk about it so historically there was this
1489:39 about it so historically there was this whole idea like Unico doesn't want to be
1489:41 whole idea like Unico doesn't want to be in the world of like encoding glyphs for
1489:45 in the world of like encoding glyphs for like devices everywhere like there was
1489:47 like devices everywhere like there was very controversial when it started doing
1489:48 very controversial when it started doing that because mostly what Unicode used to
1489:50 that because mostly what Unicode used to do was you take an existing language
1489:52 do was you take an existing language could be dead you know and then it would
1489:54 could be dead you know and then it would just take it and digitize it right it
1489:56 just take it and digitize it right it took languages that existed and just
1489:58 took languages that existed and just digitize them and uh then when it kind
1490:01 digitize them and uh then when it kind of wandered into Emoji World Suddenly
1490:03 of wandered into Emoji World Suddenly It's like deciding what deserves to be
1490:06 It's like deciding what deserves to be like an emoji decides to be digitized so
1490:10 like an emoji decides to be digitized so um trying to get kind of get out of it
1490:12 um trying to get kind of get out of it and they have proposals over time where
1490:14 and they have proposals over time where it's like oh maybe we should like come
1490:16 it's like oh maybe we should like come up with a a way to just send pictures
1490:18 up with a a way to just send pictures back and forth where you you it's a
1490:21 back and forth where you you it's a fixed picture and you like use a hash so
1490:24 fixed picture and you like use a hash so that you know like we would look at the
1490:26 that you know like we would look at the picture and then like go do a lookup
1490:28 picture and then like go do a lookup somewhere like that did not go over well
1490:30 somewhere like that did not go over well then there was actually a really
1490:32 then there was actually a really interesting proposal I kind of like uh
1490:34 interesting proposal I kind of like uh didn't go over well which is using
1490:35 didn't go over well which is using something called the qid which is in
1490:37 something called the qid which is in Wiki in the Wikipedia world so in
1490:40 Wiki in the Wikipedia world so in Wikipedia World items all have uh
1490:43 Wikipedia World items all have uh numbers across the different language
1490:45 numbers across the different language Wikipedia so Obama human Earth they will
1490:49 Wikipedia so Obama human Earth they will have an ID number so that the page in
1490:51 have an ID number so that the page in English and the page in German the page
1490:53 English and the page in German the page in Chinese all know that they're
1490:55 in Chinese all know that they're pointing to the same thing so the
1490:56 pointing to the same thing so the question so one idea came up like why
1490:58 question so one idea came up like why don't we use the numbering system so we
1491:00 don't we use the numbering system so we can use like Eiffel Tower in you know
1491:02 can use like Eiffel Tower in you know see the number and then like oh people
1491:04 see the number and then like oh people know like oh you're trying to say Eiffel
1491:06 know like oh you're trying to say Eiffel Tower that did not go over well so those
1491:08 Tower that did not go over well so those are both both of those proposals seem
1491:09 are both both of those proposals seem dead as of yet um and it's too bad cuz
1491:13 dead as of yet um and it's too bad cuz and you'll see kind of what's happening
1491:14 and you'll see kind of what's happening okay so what's coming in 22 so these are
1491:16 okay so what's coming in 22 so these are the Emoji that I actually sort of
1491:19 the Emoji that I actually sort of thought they would be on your phones by
1491:20 thought they would be on your phones by now but cuz we're in mid November and
1491:21 now but cuz we're in mid November and they usually update early November so
1491:23 they usually update early November so three more Hearts people love hearts
1491:26 three more Hearts people love hearts like uh wing blackbird Goose Birds also
1491:29 like uh wing blackbird Goose Birds also purple flowers jellyfish moose face
1491:31 purple flowers jellyfish moose face donkey donkey was a little bit late for
1491:33 donkey donkey was a little bit late for the kind of Elections um Ginger peep pod
1491:36 the kind of Elections um Ginger peep pod Wireless cond shaking face um folding
1491:40 Wireless cond shaking face um folding hand pan that one was interesting cuz
1491:42 hand pan that one was interesting cuz when people first proposed it they
1491:44 when people first proposed it they proposed it as like an electric fan and
1491:46 proposed it as like an electric fan and that didn't like who knows what electric
1491:49 that didn't like who knows what electric fans will look like in 100 years because
1491:50 fans will look like in 100 years because the thing is Once An Emoji always an
1491:51 the thing is Once An Emoji always an emoji they never retire so they're
1491:54 emoji they never retire so they're always looking for things that have a
1491:55 always looking for things that have a long visual longevity floppy disc did
1491:58 long visual longevity floppy disc did not actually do that so there's always
1492:00 not actually do that so there's always like we don't want another floppy disc
1492:02 like we don't want another floppy disc um and then hairpick is interesting so
1492:04 um and then hairpick is interesting so there was a whole debate about how to
1492:06 there was a whole debate about how to convey um like afro African hair like
1492:10 convey um like afro African hair like the the curly hair that they introduced
1492:12 the the curly hair that they introduced a couple years ago was supposed to do
1492:13 a couple years ago was supposed to do that and most of the vendors actually
1492:15 that and most of the vendors actually have it in a sort of Afro way except for
1492:17 have it in a sort of Afro way except for Apple so there's a lot of complaints but
1492:19 Apple so there's a lot of complaints but um hairpick was sort of a an interesting
1492:21 um hairpick was sort of a an interesting way it means both comb but also has sort
1492:23 way it means both comb but also has sort of an interesting historic connotation
1492:25 of an interesting historic connotation and it's been around for about 2,000
1492:27 and it's been around for about 2,000 years uh a couple music things maracus
1492:29 years uh a couple music things maracus and flute uh Beyond 2022 one of the
1492:31 and flute uh Beyond 2022 one of the things that's going to die oops can I go
1492:34 things that's going to die oops can I go back no I can't oh well uh we're going
1492:36 back no I can't oh well uh we're going to retire the the family Emoji um they
1492:41 to retire the the family Emoji um they didn't go over so well there were so
1492:42 didn't go over so well there were so many of them combinatorically if you had
1492:44 many of them combinatorically if you had all the everything in in uh you know all
1492:48 all the everything in in uh you know all the race all the races all because you
1492:50 the race all the races all because you wanted to have skin tones because you
1492:52 wanted to have skin tones because you didn't want to imply that families can
1492:53 didn't want to imply that families can only be one race it was such an ordeal
1492:56 only be one race it was such an ordeal uh essentially we're all like no one
1492:57 uh essentially we're all like no one uses them and there's so many and it's
1492:59 uses them and there's so many and it's like the fonts like in terms of the load
1493:02 like the fonts like in terms of the load is like too large so they're just going
1493:03 is like too large so they're just going to make them all into like basically
1493:05 to make them all into like basically little like bathroom symbol Type U folks
1493:08 little like bathroom symbol Type U folks um so I think that is those will
1493:10 um so I think that is those will disappear what what's actually really
1493:11 disappear what what's actually really interesting about the family Emoji is
1493:13 interesting about the family Emoji is they had gay when they introduced the
1493:15 they had gay when they introduced the family they had gay family emoji and the
1493:17 family they had gay family emoji and the Russian government went berserk and
1493:19 Russian government went berserk and actually you can Google this in 2015
1493:21 actually you can Google this in 2015 you'll see a bunch of articles about
1493:22 you'll see a bunch of articles about like the Russian government considering
1493:24 like the Russian government considering it homosexual propaganda to Youth and
1493:26 it homosexual propaganda to Youth and there was a big debate about whether or
1493:27 there was a big debate about whether or not they were going to ban Apple devices
1493:29 not they were going to ban Apple devices and what so you can see a lot of the
1493:30 and what so you can see a lot of the media coverage from that time but I
1493:32 media coverage from that time but I thought it was really interesting how
1493:34 thought it was really interesting how upset a national government can get
1493:36 upset a national government can get about little pictures on your phone um
1493:39 about little pictures on your phone um another thing that's going to that's on
1493:41 another thing that's going to that's on the agenda as of you know a couple of
1493:43 the agenda as of you know a couple of weeks ago is directionality in terms of
1493:46 weeks ago is directionality in terms of emoji so as you know most emojis kind of
1493:48 emoji so as you know most emojis kind of just flip one way or another and the
1493:49 just flip one way or another and the reason why it matters is because not all
1493:52 reason why it matters is because not all languages run in the same direction so
1493:54 languages run in the same direction so for example Arabic so we are used to
1493:57 for example Arabic so we are used to left to right but a lot of languages go
1493:59 left to right but a lot of languages go right to left um so and this kind of It
1494:02 right to left um so and this kind of It kind of changes the meaning of emoji for
1494:04 kind of changes the meaning of emoji for example right to left I send this a lot
1494:06 example right to left I send this a lot to my friends when I'm going flying from
1494:07 to my friends when I'm going flying from the Bay Area to New York um if you do it
1494:12 the Bay Area to New York um if you do it from left to right however that is what
1494:14 from left to right however that is what it looks like so it looks like um you
1494:16 it looks like so it looks like um you know you're in NE Bay but the plane is
1494:18 know you're in NE Bay but the plane is still going that you know kind of up and
1494:20 still going that you know kind of up and to the right and then now it looks like
1494:22 to the right and then now it looks like you're going from New York to the Bay
1494:24 you're going from New York to the Bay Area the other place is like oh it's
1494:26 Area the other place is like oh it's it's a girl and she's running really
1494:28 it's a girl and she's running really fast right uh that is left to right to
1494:31 fast right uh that is left to right to left in our world wait left to right
1494:33 left in our world wait left to right sorry sorry that was that's that's
1494:34 sorry sorry that was that's that's supposed to be left to right and in here
1494:36 supposed to be left to right and in here it would be she's like behind like
1494:39 it would be she's like behind like pollution or something like that so
1494:40 pollution or something like that so sorry about so so an example this is
1494:42 sorry about so so an example this is actually in The Proposal like in one
1494:44 actually in The Proposal like in one case if it's left or right you're
1494:46 case if it's left or right you're running away from a line of cars and the
1494:48 running away from a line of cars and the other one it's a warning to not run
1494:50 other one it's a warning to not run behind car fumes so they are trying to
1494:53 behind car fumes so they are trying to figure out like how do we mirror a bunch
1494:54 figure out like how do we mirror a bunch of the Emoji um but the LA the main
1494:57 of the Emoji um but the LA the main thing that I think sort of I don't know
1495:00 thing that I think sort of I don't know when it's going to happen I'm really
1495:01 when it's going to happen I'm really hopeful is going to happen is trying to
1495:03 hopeful is going to happen is trying to come up with a system that supports
1495:05 come up with a system that supports little stickers in line that don't need
1495:08 little stickers in line that don't need Unicode so this is like slack or on um
1495:11 Unicode so this is like slack or on um twitch you can embed little pictures on
1495:14 twitch you can embed little pictures on in line and all all all the vendors have
1495:15 in line and all all all the vendors have to get together and agree to come up
1495:18 to get together and agree to come up with a standard way to do that they have
1495:19 with a standard way to do that they have not yet come up with that but that is
1495:21 not yet come up with that but that is sort of one of the ways that unic coded
1495:23 sort of one of the ways that unic coded like want it wants to back away from
1495:26 like want it wants to back away from actually being like a global regulator
1495:28 actually being like a global regulator for like little colorful glyphs and so
1495:31 for like little colorful glyphs and so if you ever need to reach me um on in my
1495:34 if you ever need to reach me um on in my emoji world you can find me um Jenny
1495:36 emoji world you can find me um Jenny EmojiNation org there will
1495:39 EmojiNation org there will be um the it will be actually a while I
1495:42 be um the it will be actually a while I think before we see a next generation of
1495:44 think before we see a next generation of emoji showing up in it used to be like
1495:46 emoji showing up in it used to be like every year they would get new code
1495:47 every year they would get new code points it might be a little bit less
1495:49 points it might be a little bit less than every year now as they work on
1495:51 than every year now as they work on things like directionality over time so
1495:54 things like directionality over time so that uh if anyone has questions you can
1495:56 that uh if anyone has questions you can ask questions you can find me afterwards
1495:58 ask questions you can find me afterwards I think I've I feel like there's
1496:00 I think I've I feel like there's supposed to be some some hubub right now
1496:03 supposed to be some some hubub right now about maybe maybe
1496:06 about maybe maybe um microphones but maybe not but maybe
1496:09 um microphones but maybe not but maybe I'm just done and if if there are
1496:11 I'm just done and if if there are questions or if David is around I'm
1496:12 questions or if David is around I'm happy you know he can I'm I'm happy to
1496:14 happy you know he can I'm I'm happy to to answer any questions that folks have
1496:17 to answer any questions that folks have yes hi yes I was wondering what were
1496:20 yes hi yes I was wondering what were your thoughts on the
1496:22 your thoughts on the emo so the question is what are my
1496:25 emo so the question is what are my thoughts on the Emoji Movie you're
1496:26 thoughts on the Emoji Movie you're talking about the Sony animated one yes
1496:29 talking about the Sony animated one yes okay my thought on that is it is better
1496:32 okay my thought on that is it is better than a 6% rating on um Rotten Tomatoes
1496:35 than a 6% rating on um Rotten Tomatoes would lead you to believe so that's my
1496:37 would lead you to believe so that's my one thought and my my next thought is
1496:39 one thought and my my next thought is that um that was a rush job from an
1496:42 that um that was a rush job from an animation perspective that was that was
1496:45 animation perspective that was that was was about 18 months of whereas a typical
1496:48 was about 18 months of whereas a typical animated movie takes four years so in my
1496:51 animated movie takes four years so in my spare time I also produce movies and
1496:52 spare time I also produce movies and documentaries so one thing that is key
1496:55 documentaries so one thing that is key to know
1496:56 to know about about movies and animated movies
1497:00 about about movies and animated movies and this is very important they take a
1497:01 and this is very important they take a very long time but you can always fix it
1497:04 very long time but you can always fix it because you haven't shot anything and a
1497:07 because you haven't shot anything and a very good example of that is I assume
1497:08 very good example of that is I assume you guys have seen Frozen if you haven't
1497:10 you guys have seen Frozen if you haven't seen you seen of the age you would have
1497:12 seen you seen of the age you would have seen Frozen um I do not understand like
1497:14 seen Frozen um I do not understand like how huge and a phenomenon or why it was
1497:17 how huge and a phenomenon or why it was such a huge phenomenon but
1497:21 such a huge phenomenon but um they actually did a original cut of
1497:24 um they actually did a original cut of Frozen and it did so I don't know you
1497:26 Frozen and it did so I don't know you guys know the the Eye the the sort of
1497:28 guys know the the Eye the the sort of Snow Queen thing but she's like super
1497:29 Snow Queen thing but she's like super dark and like not fun and like kind of
1497:32 dark and like not fun and like kind of evil and like not someone you want to
1497:34 evil and like not someone you want to like get behind as a character so they
1497:36 like get behind as a character so they actually did sort of a rough cut of that
1497:38 actually did sort of a rough cut of that of Frozen and they came out of that with
1497:41 of Frozen and they came out of that with um it's just storyboarding and they're
1497:43 um it's just storyboarding and they're like that is not good and they killed it
1497:46 like that is not good and they killed it so they were like we can't go with this
1497:48 so they were like we can't go with this and then started from scratch more or
1497:50 and then started from scratch more or less again starting with the song um Let
1497:53 less again starting with the song um Let It Go which is actually written by a kid
1497:55 It Go which is actually written by a kid from my elementary school Bobby Lopez or
1497:58 from my elementary school Bobby Lopez or co-written by Bobby Lopez um I also
1498:00 co-written by Bobby Lopez um I also actually fun fact I also went I would
1498:02 actually fun fact I also went I would took the school bus with uh Lyn mmel
1498:04 took the school bus with uh Lyn mmel Miranda so I was a fourth grader when he
1498:06 Miranda so I was a fourth grader when he was like a kindergartener so we had a
1498:07 was like a kindergartener so we had a very musical Elementary School in New
1498:09 very musical Elementary School in New York City but the thing is they could
1498:11 York City but the thing is they could fix it because they had enough time and
1498:13 fix it because they had enough time and have enough money not like movies where
1498:16 have enough money not like movies where you shoot humans much harder to fix so
1498:18 you shoot humans much harder to fix so you have the footage that you have and
1498:20 you have the footage that you have and you can do little pickups but you can't
1498:21 you can do little pickups but you can't fix it so essentially what happened in
1498:23 fix it so essentially what happened in that case I think um it's 18 months and
1498:26 that case I think um it's 18 months and it could have gotten better and a lot of
1498:28 it could have gotten better and a lot of the movies that you see with Pixar like
1498:30 the movies that you see with Pixar like it's very it's actually sort emotionally
1498:32 it's very it's actually sort emotionally similar to the movie called inside out
1498:35 similar to the movie called inside out and uh but inside they just have more
1498:36 and uh but inside they just have more time and so it's better so as opposed to
1498:38 time and so it's better so as opposed to 18 months which is not long enough to
1498:40 18 months which is not long enough to make a animated movie good but uh the
1498:44 make a animated movie good but uh the other fun thing is is it was the it it
1498:46 other fun thing is is it was the it it was so weird cuz they sold sponsorships
1498:48 was so weird cuz they sold sponsorships it was like like oh my God here comes
1498:52 it was like like oh my God here comes the Bots and the malware let's go into
1498:55 the Bots and the malware let's go into Dropbox and protect ourselves and was so
1498:58 Dropbox and protect ourselves and was so I think that it got a lot of like bad
1499:00 I think that it got a lot of like bad kind of um kind of uh vibes from the
1499:03 kind of um kind of uh vibes from the from the press for doing things like
1499:04 from the press for doing things like that but from a kids perspective it's
1499:06 that but from a kids perspective it's it's fine I think if um I don't know
1499:09 it's fine I think if um I don't know that I would like put into my top 10 of
1499:12 that I would like put into my top 10 of animated pictures but it's better than
1499:13 animated pictures but it's better than 6% on Rotten Tomatoes
1499:16 6% on Rotten Tomatoes and then actually if you guys ever care
1499:17 and then actually if you guys ever care we we have done uh I did a documentary
1499:20 we we have done uh I did a documentary about Emoji so and all the people that
1499:22 about Emoji so and all the people that create helped create emoji and uh we did
1499:27 create helped create emoji and uh we did a cs50x movie night I think during the
1499:30 a cs50x movie night I think during the pandemic was it during the pandemic
1499:31 pandemic was it during the pandemic everything sort of like blurred together
1499:32 everything sort of like blurred together but it was during the pandemic yeah Are
1499:35 but it was during the pandemic yeah Are we more questions yes um I wanted to
1499:40 we more questions yes um I wanted to know you mentioned that one of the
1499:43 know you mentioned that one of the criteria for
1499:46 criteria for AC do or there's demand
1499:53 yeah yeah that's a very good question yeah so the question is one of the
1499:56 yeah so the question is one of the propos one of our criterias of of um
1500:00 propos one of our criterias of of um getting an emoji accepted is to sort of
1500:01 getting an emoji accepted is to sort of to demonstrate demand and how do we
1500:03 to demonstrate demand and how do we demonstrate demand and I would say in a
1500:05 demonstrate demand and I would say in a in a pretty um clumsy way actually at
1500:09 in a pretty um clumsy way actually at this point so the main thing that you
1500:11 this point so the main thing that you have in our in our current proposal
1500:13 have in our in our current proposal process is we have um a median Emoji
1500:16 process is we have um a median Emoji which is elephant so elephant is like if
1500:18 which is elephant so elephant is like if you stack ranked all the emoji for
1500:20 you stack ranked all the emoji for popularity elephant is like right there
1500:21 popularity elephant is like right there in the middle and it's also a concept
1500:23 in the middle and it's also a concept that's like universally understood
1500:24 that's like universally understood across all
1500:25 across all languages so um elephant shows up
1500:29 languages so um elephant shows up somewhere between 500 million and 700
1500:31 somewhere between 500 million and 700 million in Google search results like if
1500:34 million in Google search results like if you type it into a laptop you'll see you
1500:36 you type it into a laptop you'll see you know elephant 500,000 uh 500 million
1500:39 know elephant 500,000 uh 500 million search results and generally you're
1500:40 search results and generally you're you're trying to when you're comparing
1500:42 you're trying to when you're comparing your term to elephant you want to see
1500:46 your term to elephant you want to see very roughly how many Google search
1500:48 very roughly how many Google search results B search results sometimes
1500:50 results B search results sometimes Instagram so actually something that was
1500:52 Instagram so actually something that was really surprising to me was someone
1500:54 really surprising to me was someone proposed hummingbird I think hummingbird
1500:56 proposed hummingbird I think hummingbird is uh a good proposal and um but if you
1501:01 is uh a good proposal and um but if you look at Hummingbird it's only like 21
1501:03 look at Hummingbird it's only like 21 million in terms of the stat so which I
1501:06 million in terms of the stat so which I thought was like very surprisingly low
1501:08 thought was like very surprisingly low so that's one of the main ways that we
1501:10 so that's one of the main ways that we kind of we kind of see like is it also
1501:12 kind of we kind of see like is it also visually used and all of that yeah any
1501:16 visually used and all of that yeah any other questions are we good I didn't
1501:20 other questions are we good I didn't even need my water or anything oh can I
1501:22 even need my water or anything oh can I take I'm going to take a picture I'm
1501:23 take I'm going to take a picture I'm going to take a picture for because now
1501:24 going to take a picture for because now you guys are actually human and not
1501:26 you guys are actually human and not Muppets so I'm very very excited about
1501:28 Muppets so I'm very very excited about this so I will send this to like my
1501:29 this so I will send this to like my block mates and be like I just lectured
1501:32 block mates and be like I just lectured at CS you know in Sanders Theater my
1501:35 at CS you know in Sanders Theater my thanks to Jenny Lee thank
1501:39 thanks to Jenny Lee thank you yeah you can stay up here for give
1501:41 you yeah you can stay up here for give me one
1501:43 me one second
1501:51 so if up until now thought it would be appropriate to toss this up on the board
1501:53 appropriate to toss this up on the board if up until now you've not yet gotten
1501:54 if up until now you've not yet gotten cs50 stress ball on the way out please
1501:56 cs50 stress ball on the way out please do grab one we got some extras as well
1501:58 do grab one we got some extras as well off to the side but I would also keep in
1501:59 off to the side but I would also keep in mind back in week zero where again we
1502:03 mind back in week zero where again we began we asked you to categorize
1502:04 began we asked you to categorize yourselves as to whether you are among
1502:06 yourselves as to whether you are among those less comfortable those more
1502:08 those less comfortable those more comfortable or those somewhere in
1502:09 comfortable or those somewhere in between uh please know now that you are
1502:12 between uh please know now that you are officially all some uh please know now
1502:15 officially all some uh please know now that you are all officially among those
1502:17 that you are all officially among those more comfortable and indeed even though
1502:19 more comfortable and indeed even though a couple of more Milestones await we
1502:21 a couple of more Milestones await we cannot wait to see what you accomplish
1502:23 cannot wait to see what you accomplish with your final projects in the meantime
1502:26 with your final projects in the meantime as always this is and now this was
1502:54 [Laughter] [Music]
1503:08 Mr when I in CS I'll say truth be told I thought
1503:13 I in CS I'll say truth be told I thought i' be real coding hero because I did
1503:24 so it picked up so fast turn out that harder scratch hell world and
1503:28 harder scratch hell world and goodby
1503:29 goodby [Applause]
1503:31 [Applause] [Music]
1503:39 because Mr I was on
1503:42 Mr I was on the trying so hard not fall behind
1503:46 the trying so hard not fall behind attending section and wanting a stuck in
1503:50 attending section and wanting a stuck in the in void of my
1504:02 computer to pyth in why on because this is
1504:15 s Mr s we completed JavaScript sequ CSS
1504:18 JavaScript sequ CSS and every language our to reset don't
1504:33 get again I repli obviously because
1504:36 obviously because cie
1504:39 cie [Applause]
1506:43 all right so this is cs50 my name is David May and this is Harvard
1506:45 David May and this is Harvard University's introduction to the
1506:46 University's introduction to the intellectual Enterprises of computer
1506:48 intellectual Enterprises of computer science and the Art of programming and
1506:50 science and the Art of programming and this of course is our special family
1506:52 this of course is our special family weekend wherein not only are cs50's own
1506:54 weekend wherein not only are cs50's own students here in the audience but also
1506:56 students here in the audience but also some family members as well now you're
1506:58 some family members as well now you're showing up in the semester a little bit
1507:00 showing up in the semester a little bit late we've just tackled week eight which
1507:02 late we've just tackled week eight which is really our ninth week since computer
1507:04 is really our ninth week since computer scientists start counting from zero so
1507:05 scientists start counting from zero so we've done a whole lot of work over the
1507:07 we've done a whole lot of work over the past few weeks as you might have heard
1507:09 past few weeks as you might have heard via emails or text messages home
1507:11 via emails or text messages home including a language known here as binar
1507:13 including a language known here as binar so on the screen here of course is a lot
1507:15 so on the screen here of course is a lot of zeros and ones and suffice it to say
1507:17 of zeros and ones and suffice it to say let me sum up the past nine weeks with
1507:20 let me sum up the past nine weeks with this is what's going on underneath the
1507:22 this is what's going on underneath the hood but of course today we thought we'd
1507:23 hood but of course today we thought we'd make things a little more accessible a
1507:25 make things a little more accessible a little more broadly applicable and
1507:27 little more broadly applicable and indeed our Focus today will not be on
1507:29 indeed our Focus today will not be on what these patterns of zeros and ones
1507:31 what these patterns of zeros and ones represent which in astute eye might
1507:33 represent which in astute eye might notice are replicated visually with
1507:35 notice are replicated visually with these light bulbs being in a pattern on
1507:37 these light bulbs being in a pattern on and off and as your child might have
1507:39 and off and as your child might have hinted uh before class or perhaps now
1507:42 hinted uh before class or perhaps now this might very well spell a word up to
1507:44 this might very well spell a word up to eight characters long because you can
1507:46 eight characters long because you can encode even in the real world things
1507:48 encode even in the real world things digital too but today we'll focus on
1507:50 digital too but today we'll focus on things much more high level this notion
1507:51 things much more high level this notion of cyber security like our the security
1507:54 of cyber security like our the security of our data our privacy of our systems
1507:58 of our data our privacy of our systems particularly on the internet nowadays
1507:59 particularly on the internet nowadays because presumably all of us are
1508:01 because presumably all of us are carrying Technologies around in our
1508:02 carrying Technologies around in our pocket using laptops and desktops every
1508:04 pocket using laptops and desktops every day and so the goal today is to
1508:07 day and so the goal today is to stipulate that this is what's going on
1508:09 stipulate that this is what's going on underneath the hood but let's solve some
1508:11 underneath the hood but let's solve some problems at a higher level so that your
1508:12 problems at a higher level so that your homework when you go back to wherever
1508:14 homework when you go back to wherever you're visiting from can actually be to
1508:16 you're visiting from can actually be to apply some of today's Lessons Learned so
1508:19 apply some of today's Lessons Learned so with that said perhaps the most common
1508:21 with that said perhaps the most common familiar defense uh of one's systems and
1508:24 familiar defense uh of one's systems and data phone and laptops and desktops
1508:26 data phone and laptops and desktops would just be these simple passwords
1508:28 would just be these simple passwords unfortunately you and I are frankly as
1508:30 unfortunately you and I are frankly as humans not all that good at choosing
1508:32 humans not all that good at choosing passwords and this is in itself a
1508:34 passwords and this is in itself a relatively weak form of Defense even
1508:36 relatively weak form of Defense even though each of us has dozens hundreds of
1508:39 though each of us has dozens hundreds of passwords nowadays or at least dozens or
1508:41 passwords nowadays or at least dozens or hundreds of accounts Maybe fives or tens
1508:46 hundreds of accounts Maybe fives or tens of dozens of passwords indeed if you're
1508:48 of dozens of passwords indeed if you're in the habit of reusing passwords we'll
1508:50 in the habit of reusing passwords we'll see today probably among our first
1508:52 see today probably among our first Lessons Learned so for instance if we
1508:54 Lessons Learned so for instance if we look back at the past year 2021 thanks
1508:57 look back at the past year 2021 thanks to security researchers who take a look
1508:59 to security researchers who take a look at data that has been hacked or leaked
1509:01 at data that has been hacked or leaked online by way of public databases we
1509:04 online by way of public databases we have a sense as computer scientists of
1509:06 have a sense as computer scientists of what the most popular or equivalently
1509:08 what the most popular or equivalently what some of the worst passwords are
1509:10 what some of the worst passwords are that you and I are choosing for our
1509:11 that you and I are choosing for our system so as of this past year according
1509:13 system so as of this past year according to one measure the most commonly used
1509:16 to one measure the most commonly used password in systems everywhere was 1 2 3
1509:19 password in systems everywhere was 1 2 3 4 5 6 all right number two password in
1509:23 4 5 6 all right number two password in our top 10 here list was only slightly
1509:25 our top 10 here list was only slightly longer 1 2 3 4 5 6 7 8 9 after that we
1509:29 longer 1 2 3 4 5 6 7 8 9 after that we took a turn in the other direction 1 2 3
1509:32 took a turn in the other direction 1 2 3 4 five alone after that got a little
1509:35 4 five alone after that got a little more interesting quiry which might sound
1509:37 more interesting quiry which might sound pretty cryptic but not if you look down
1509:38 pretty cryptic but not if you look down at your US keyboard and it's the top
1509:40 at your US keyboard and it's the top leftand row of the keys on a an American
1509:43 leftand row of the keys on a an American keyboard so also not all that hard uh
1509:46 keyboard so also not all that hard uh perhaps not surprisingly uh a little
1509:49 perhaps not surprisingly uh a little disconcertingly number five was
1509:52 disconcertingly number five was password meanwhile number six returns us
1509:54 password meanwhile number six returns us to digits 1 2 3 4 5 6 7 8 after that
1509:58 to digits 1 2 3 4 5 6 7 8 after that really less effort 111 111 uh after that
1510:02 really less effort 111 111 uh after that a little more variation but not all that
1510:04 a little more variation but not all that much 1 two 3 one two 3 after that it's
1510:06 much 1 two 3 one two 3 after that it's getting even less interesting 1 2 3 4 5
1510:08 getting even less interesting 1 2 3 4 5 6 7
1510:09 6 7 890 and then lastly topping the list is
1510:11 890 and then lastly topping the list is just 1 2 3 4 5 6 7 so this is not a good
1510:15 just 1 2 3 4 5 6 7 so this is not a good top 10 list to be on so among today's
1510:17 top 10 list to be on so among today's first takeaways is if you see your
1510:18 first takeaways is if you see your password on the screen like you didn't
1510:21 password on the screen like you didn't make the list in a good way this means
1510:23 make the list in a good way this means hundreds thousands millions of other
1510:25 hundreds thousands millions of other people probably have that password of
1510:27 people probably have that password of yours now in of itself that's not
1510:29 yours now in of itself that's not necessarily worrisome because I don't
1510:31 necessarily worrisome because I don't know who has these passwords in a room
1510:33 know who has these passwords in a room as large as this but just intuitively
1510:35 as large as this but just intuitively why is this a bad thing either parent or
1510:39 why is this a bad thing either parent or child welcome to raise a hand here why
1510:42 child welcome to raise a hand here why might this be
1510:44 might this be intuitively yeah access to it so access
1510:47 intuitively yeah access to it so access to it like I mean we literally as
1510:49 to it like I mean we literally as computer scientists now have a database
1510:51 computer scientists now have a database of really common passwords and your your
1510:53 of really common passwords and your your thoughts password and can just find it
1510:55 thoughts password and can just find it out quickly yeah you can just find it
1510:57 out quickly yeah you can just find it out quickly I mean you could imagine
1510:59 out quickly I mean you could imagine trying to guess someone's password by
1511:01 trying to guess someone's password by just typing in random letters random
1511:03 just typing in random letters random numbers random words but not if you have
1511:05 numbers random words but not if you have a top 10 list like the the adversaries
1511:07 a top 10 list like the the adversaries in the world might as well just start
1511:09 in the world might as well just start with this list now you'll notice that
1511:11 with this list now you'll notice that even absent from this are slight
1511:12 even absent from this are slight variants like some of you might be
1511:13 variants like some of you might be thinking I'm not on the list cuz I do
1511:15 thinking I'm not on the list cuz I do something clever like I use an
1511:16 something clever like I use an exclamation point for the number one or
1511:19 exclamation point for the number one or a three for an e or a five for an S and
1511:23 a three for an e or a five for an S and based on the smiles in the room right
1511:24 based on the smiles in the room right now you're not all that clever it turns
1511:26 now you're not all that clever it turns out because other people are smiling too
1511:29 out because other people are smiling too which is to say that an adversary can
1511:30 which is to say that an adversary can take those same fistic that you might
1511:32 take those same fistic that you might think are making things more secure by
1511:34 think are making things more secure by just tweaking some letters to numbers or
1511:36 just tweaking some letters to numbers or vice versa but if you're doing it and
1511:37 vice versa but if you're doing it and other people are doing it the bad guys
1511:39 other people are doing it the bad guys so to speak are going to be doing it as
1511:41 so to speak are going to be doing it as well so unfortunately when it comes to
1511:43 well so unfortunately when it comes to passwords better is longer and random
1511:47 passwords better is longer and random and really unguessable but that's not
1511:48 and really unguessable but that's not what most of us have in fact case in
1511:50 what most of us have in fact case in point on our phones whether you have an
1511:51 point on our phones whether you have an Android device or an iPhone nowadays you
1511:53 Android device or an iPhone nowadays you know odds are you have something
1511:55 know odds are you have something relatively simplistic protecting it if
1511:57 relatively simplistic protecting it if you have anything at all but at least
1511:58 you have anything at all but at least Apple and Google are pretty good at
1511:59 Apple and Google are pretty good at least nudging us to choose these kinds
1512:01 least nudging us to choose these kinds of passcodes now and a four-digit
1512:03 of passcodes now and a four-digit passcode is quite common nowadays and so
1512:06 passcode is quite common nowadays and so here's where we have an opportunity
1512:08 here's where we have an opportunity thanks to uh the URL that you saw on the
1512:10 thanks to uh the URL that you saw on the screen earlier to conjecture as a group
1512:13 screen earlier to conjecture as a group just how long might it take an adversary
1512:16 just how long might it take an adversary someone out there who's out to get us or
1512:18 someone out there who's out to get us or get one of us uh how long might it take
1512:20 get one of us uh how long might it take an adversary to figure out your phone's
1512:23 an adversary to figure out your phone's 4digit passcode this is a cs50 is on
1512:26 4digit passcode this is a cs50 is on Carter Carter if we could switch over
1512:27 Carter Carter if we could switch over and poll the audience here if you take
1512:30 and poll the audience here if you take out your phone or laptop whatever device
1512:31 out your phone or laptop whatever device you might have used a few minutes ago to
1512:33 you might have used a few minutes ago to scan that QR code or to visit that same
1512:38 scan that QR code or to visit that same URL you can see these questions on your
1512:41 URL you can see these questions on your browser and if you can't that's fine
1512:43 browser and if you can't that's fine we'll share some aggregate data
1512:45 we'll share some aggregate data nonetheless but you should have an
1512:47 nonetheless but you should have an opportunity to tap one of your answers
1512:48 opportunity to tap one of your answers and we'll give folks a few more
1512:50 and we'll give folks a few more seconds if you'd like to play along at
1512:54 seconds if you'd like to play along at home and here in just a
1512:57 home and here in just a moment probably have many people
1513:00 moment probably have many people reporting but why don't we go ahead and
1513:01 reporting but why don't we go ahead and take a look at some percentages it looks
1513:03 take a look at some percentages it looks like most of you 67% are proposing just
1513:06 like most of you 67% are proposing just a few seconds so that's not all that
1513:08 a few seconds so that's not all that good news if it's a four-digit passcode
1513:10 good news if it's a four-digit passcode some of you are hoping it's a few
1513:11 some of you are hoping it's a few minutes uh 8% are hoping a few hours
1513:14 minutes uh 8% are hoping a few hours four more than 4% of you are really
1513:16 four more than 4% of you are really hoping perhaps it's a few days well
1513:19 hoping perhaps it's a few days well let's actually consider how we can
1513:20 let's actually consider how we can answer this question and make today not
1513:21 answer this question and make today not just conceptual but a little
1513:23 just conceptual but a little quantitative too and see if we can't
1513:24 quantitative too and see if we can't slap some numbers on questions like
1513:26 slap some numbers on questions like these so ultimately you can make more
1513:27 these so ultimately you can make more informed decisions with your system
1513:29 informed decisions with your system security so for instance when it comes
1513:31 security so for instance when it comes to four digigit passcodes rather than
1513:33 to four digigit passcodes rather than just consider how secure it is well
1513:36 just consider how secure it is well let's make it a more precise question
1513:38 let's make it a more precise question like what are the forms of attack well
1513:39 like what are the forms of attack well the simplest attack might be just
1513:41 the simplest attack might be just someone grabbing your phone be it in
1513:42 someone grabbing your phone be it in your family or maybe at Starbucks or the
1513:45 your family or maybe at Starbucks or the airport or the like and just starting
1513:46 airport or the like and just starting all possible combinations maybe 00000000
1513:49 all possible combinations maybe 00000000 then 00001 and 00002 we could maybe
1513:53 then 00001 and 00002 we could maybe automate this a little bit so for
1513:55 automate this a little bit so for instance I might potentially be able to
1513:57 instance I might potentially be able to do something like uh roboticize this
1514:00 do something like uh roboticize this here let me go ahead and full screen a
1514:02 here let me go ahead and full screen a quick video here that's just going to
1514:03 quick video here that's just going to paint a picture in just a moment on the
1514:05 paint a picture in just a moment on the screen of how if we're a really clever
1514:08 screen of how if we're a really clever adversary and know how to build things
1514:09 adversary and know how to build things well at least maybe we could automate
1514:11 well at least maybe we could automate some of that process so here's an
1514:12 some of that process so here's an Android phone on a counter here's a very
1514:15 Android phone on a counter here's a very simple tripod and a little touch device
1514:17 simple tripod and a little touch device robotically doing all of that hacking
1514:19 robotically doing all of that hacking for you starting at 0000 Z probably all
1514:22 for you starting at 0000 Z probably all the way up to 9999 now that too wasn't
1514:25 the way up to 9999 now that too wasn't necessarily all of that all that fast
1514:27 necessarily all of that all that fast but at least you the adversary can step
1514:29 but at least you the adversary can step away and doesn't actually have to be
1514:31 away and doesn't actually have to be bothered with the time involved the cost
1514:33 bothered with the time involved the cost involved in actually hacking that
1514:35 involved in actually hacking that particular device well let's go one
1514:37 particular device well let's go one level deeper a little more interestingly
1514:39 level deeper a little more interestingly and consider here um the how much much
1514:43 and consider here um the how much much time really this so-called Brute Force
1514:46 time really this so-called Brute Force attack would take and that's actually a
1514:47 attack would take and that's actually a term of art much like in yester year
1514:49 term of art much like in yester year when maybe there was a battering ram
1514:50 when maybe there was a battering ram trying to brute force their way into a
1514:52 trying to brute force their way into a castle or something like that a Brute
1514:54 castle or something like that a Brute Force attack digitally is just someone
1514:56 Force attack digitally is just someone trying manually all possible codes or
1514:58 trying manually all possible codes or maybe robotically trying all possible
1515:00 maybe robotically trying all possible codes but generally automating the
1515:02 codes but generally automating the process in some way to go through all
1515:04 process in some way to go through all possibilities well if you've got for
1515:07 possibilities well if you've got for instance um a 4digit passcode let's ask
1515:11 instance um a 4digit passcode let's ask maybe a follow-up question here not how
1515:13 maybe a follow-up question here not how long will take but how many possible
1515:16 long will take but how many possible 4digit passcodes are there because then
1515:20 4digit passcodes are there because then maybe we can do some quick math and if
1515:21 maybe we can do some quick math and if every passcode takes me a second or a
1515:24 every passcode takes me a second or a few milliseconds or the like then I
1515:25 few milliseconds or the like then I think we can try to extrapolate from
1515:27 think we can try to extrapolate from that whether the first answer was
1515:28 that whether the first answer was seconds or minutes or days or hours or
1515:31 seconds or minutes or days or hours or something else so how many four-digit
1515:33 something else so how many four-digit passcodes are possible if you take out
1515:35 passcodes are possible if you take out your same device it should have just
1515:36 your same device it should have just changed automatically if it doesn't seem
1515:38 changed automatically if it doesn't seem to have maybe reload your browser with
1515:40 to have maybe reload your browser with some menu option and then tap in here
1515:43 some menu option and then tap in here how many four digigit passcodes are
1515:45 how many four digigit passcodes are possible four total 40 99,999 10,000 or
1515:49 possible four total 40 99,999 10,000 or unsure is okay
1515:51 unsure is okay too so let's see we'll give you a few
1515:54 too so let's see we'll give you a few more moments how many four digigit
1515:57 more moments how many four digigit passcodes are possible and shall we
1515:59 passcodes are possible and shall we reveal the results so now it looks like
1516:03 reveal the results so now it looks like uh a a few of you uh 2% of you are
1516:06 uh a a few of you uh 2% of you are saying just four passcodes 40 99,999
1516:10 saying just four passcodes 40 99,999 there's definitely some contention here
1516:12 there's definitely some contention here and 6% are un sure well how do we wrap
1516:15 and 6% are un sure well how do we wrap our minds around this well let's just
1516:17 our minds around this well let's just kind of do this real simply here let me
1516:19 kind of do this real simply here let me switch back over to doing a bit of math
1516:22 switch back over to doing a bit of math and if we have here 10 possibilities for
1516:25 and if we have here 10 possibilities for each digit if there's four digits each
1516:27 each digit if there's four digits each digit can be 0 1 2 3 4 5 6 7 8 nine so
1516:29 digit can be 0 1 2 3 4 5 6 7 8 nine so that's 10 possibilities so if you think
1516:31 that's 10 possibilities so if you think about the number of permutations that's
1516:33 about the number of permutations that's 10 possibilities for the first digit
1516:35 10 possibilities for the first digit times 10 for the next time 10 to the for
1516:37 times 10 for the next time 10 to the for the next times 10 for the next and so if
1516:39 the next times 10 for the next and so if we do that out 10 * 10 * 10 * 10 or 10
1516:42 we do that out 10 * 10 * 10 * 10 or 10 the 4th there are indeed as 66% of you
1516:45 the 4th there are indeed as 66% of you uh found uh 10,000 possibilities and so
1516:48 uh found uh 10,000 possibilities and so now we can kind of work backwards and
1516:50 now we can kind of work backwards and decide how long is it going to take for
1516:51 decide how long is it going to take for an adversary to hack into this phone
1516:54 an adversary to hack into this phone because if it's one attack one guess per
1516:56 because if it's one attack one guess per second well that's going to map out to
1516:58 second well that's going to map out to 10,000 seconds but maybe not if the
1517:00 10,000 seconds but maybe not if the adversary isn't a roboticist or a human
1517:02 adversary isn't a roboticist or a human what if they're like at a software
1517:04 what if they're like at a software programmer someone who's taken even a
1517:06 programmer someone who's taken even a class introductory like cs50 and learned
1517:08 class introductory like cs50 and learned a little bit of programming well a
1517:10 a little bit of programming well a little bit frighteningly it's not all
1517:11 little bit frighteningly it's not all that hard to hack into systems if you
1517:14 that hard to hack into systems if you just know how to code to and really have
1517:16 just know how to code to and really have the computer do your work for you so in
1517:18 the computer do your work for you so in fact let me go ahead and change over to
1517:20 fact let me go ahead and change over to another screen on my computer here um
1517:22 another screen on my computer here um this is different for students in the
1517:23 this is different for students in the group from vs code this is just a black
1517:25 group from vs code this is just a black and white version of it that we've used
1517:26 and white version of it that we've used briefly in the past and I'm just going
1517:28 briefly in the past and I'm just going to go ahead and create a program called
1517:30 to go ahead and create a program called crack.pie to crack something just
1517:32 crack.pie to crack something just technically means to figure out what it
1517:34 technically means to figure out what it is figure out a password in this case
1517:36 is figure out a password in this case and dop means I'm going to use a
1517:38 and dop means I'm going to use a programming language that we here in
1517:39 programming language that we here in cs50 have been dabbling in in the past
1517:41 cs50 have been dabbling in in the past couple of weeks with more to come next
1517:43 couple of weeks with more to come next week as well so it turns out and you
1517:46 week as well so it turns out and you need not understand each of these lines
1517:47 need not understand each of these lines of code if I want to try maybe
1517:50 of code if I want to try maybe generating all 10,000 possible codes I'm
1517:53 generating all 10,000 possible codes I'm not going to bother with a robot I've
1517:55 not going to bother with a robot I've got all these cables coming out of my
1517:56 got all these cables coming out of my computer and odds are one of them is a
1517:58 computer and odds are one of them is a USB cable or a lightning cable surely we
1518:01 USB cable or a lightning cable surely we could figure out how to connect like
1518:02 could figure out how to connect like laptop or desktop to phone and just like
1518:04 laptop or desktop to phone and just like automate the process nowadays by just
1518:06 automate the process nowadays by just sending all of the numbers into the
1518:08 sending all of the numbers into the phone until one uh unlocks the trick
1518:10 phone until one uh unlocks the trick just like in the movies or TV well in
1518:12 just like in the movies or TV well in Python I could write a program that does
1518:14 Python I could write a program that does this as follows I can import so to speak
1518:17 this as follows I can import so to speak all of the decimal digits 0 through n
1518:19 all of the decimal digits 0 through n and this for students in the room is
1518:21 and this for students in the room is just a slightly better version of typing
1518:23 just a slightly better version of typing out 10 different numbers manually I can
1518:26 out 10 different numbers manually I can also import from a library so to speak
1518:28 also import from a library so to speak called iter tools for iteration tools
1518:30 called iter tools for iteration tools which means to do something again and
1518:32 which means to do something again and again I can import a function called
1518:35 again I can import a function called Product which means the cross product
1518:37 Product which means the cross product like combine this with this some number
1518:39 like combine this with this some number of times and then it's just two more
1518:40 of times and then it's just two more lines of code I can use what's called a
1518:42 lines of code I can use what's called a loop and programming so for every
1518:44 loop and programming so for every passcode in the cross product of all 10
1518:47 passcode in the cross product of all 10 of those digits repeated a total of four
1518:51 of those digits repeated a total of four times let me go ahead and rather than
1518:54 times let me go ahead and rather than bother connecting my phone and hacking
1518:55 bother connecting my phone and hacking my own phone let me just print out every
1518:57 my own phone let me just print out every one of those 10,000 codes on the screen
1518:59 one of those 10,000 codes on the screen and we'll see how fast the hacker could
1519:01 and we'll see how fast the hacker could do this let me go ahead and print and
1519:03 do this let me go ahead and print and with an asterisk which is a little trick
1519:04 with an asterisk which is a little trick to format it nicely I'm going to print
1519:06 to format it nicely I'm going to print out each of those passcodes and that's
1519:08 out each of those passcodes and that's it four lines of code maybe 40 seconds
1519:10 it four lines of code maybe 40 seconds of talking but maybe really four seconds
1519:12 of talking but maybe really four seconds of code if I actually did this without
1519:14 of code if I actually did this without the audience and now let me go ahead and
1519:16 the audience and now let me go ahead and save the file and I'm going to run as we
1519:18 save the file and I'm going to run as we do every in class of late python of
1519:21 do every in class of late python of crack.pie and when I hit enter I should
1519:23 crack.pie and when I hit enter I should see on the screen all 10,000
1519:25 see on the screen all 10,000 possibilities from 000000 to 9999 so
1519:28 possibilities from 000000 to 9999 so let's see is it a few seconds minutes
1519:30 let's see is it a few seconds minutes hours or days done so barely even
1519:35 hours or days done so barely even seconds plural if that so that should be
1519:37 seconds plural if that so that should be a little disconcerting because all that
1519:38 a little disconcerting because all that adversary needs to do is grab your phone
1519:40 adversary needs to do is grab your phone off the counter plug in a cable and boom
1519:42 off the counter plug in a cable and boom they're done like there's no ticking
1519:44 they're done like there's no ticking clock or worries as in the movies or TV
1519:46 clock or worries as in the movies or TV that maybe you're going to come into the
1519:48 that maybe you're going to come into the room you don't need that much of a
1519:49 room you don't need that much of a window of time so what would be better
1519:52 window of time so what would be better than this well let's consider what our
1519:55 than this well let's consider what our options might be if we don't want to
1519:56 options might be if we don't want to just use four-digit passcode some of you
1519:58 just use four-digit passcode some of you indeed might have better passcodes than
1520:00 indeed might have better passcodes than that and maybe you use four letter
1520:02 that and maybe you use four letter passcodes instead so A through Z maybe
1520:05 passcodes instead so A through Z maybe uppercase and lowercase that starts to
1520:07 uppercase and lowercase that starts to make things a little more interesting so
1520:09 make things a little more interesting so should we pull this question too if we
1520:11 should we pull this question too if we upgrade from four digits to just four
1520:13 upgrade from four digits to just four letters English letters a through z
1520:15 letters English letters a through z uppercase and lowercase why don't we go
1520:17 uppercase and lowercase why don't we go ahead and pull the grip here and ask how
1520:19 ahead and pull the grip here and ask how many four letter passcodes are there
1520:25 many four letter passcodes are there instead so this time the range starts at
1520:28 instead so this time the range starts at four still not the right answer though
1520:30 four still not the right answer though this time how many four-letter passcodes
1520:33 this time how many four-letter passcodes are
1520:39 possible not just just take a couple more
1520:41 more seconds
1520:44 seconds all right almost a couple hundred
1520:47 all right almost a couple hundred responses in already few more
1520:54 seconds and why don't we go ahead and reveal now
1520:57 and why don't we go ahead and reveal now the answers which are Okay so we've
1520:59 the answers which are Okay so we've solved a couple problems at least so we
1521:02 solved a couple problems at least so we well we well okay someone's just messing
1521:04 well we well okay someone's just messing with us now all right so it looks like
1521:06 with us now all right so it looks like most of you 76% of you have claimed it's
1521:09 most of you 76% of you have claimed it's 7 million plus possibilities so that's
1521:11 7 million plus possibilities so that's encouraging cuz that's a whole order of
1521:13 encouraging cuz that's a whole order of magnitude more than before well let's
1521:15 magnitude more than before well let's figure out how we might do this
1521:16 figure out how we might do this mathematically so if we've got 26
1521:18 mathematically so if we've got 26 lowercase 26 uppercase that's 52
1521:20 lowercase 26 uppercase that's 52 possibilities now for each of those four
1521:22 possibilities now for each of those four digits so that's 52 times itself four
1521:25 digits so that's 52 times itself four times which indeed either off the top of
1521:27 times which indeed either off the top of your head a good guess a calculator on
1521:29 your head a good guess a calculator on the same device you're using right now
1521:31 the same device you're using right now indeed gives us 7 million instead well
1521:34 indeed gives us 7 million instead well what might be slightly better than that
1521:36 what might be slightly better than that well maybe four characters and this
1521:38 well maybe four characters and this indeed is what your Macs PCS and phones
1521:40 indeed is what your Macs PCS and phones are urging us to do nowadays not just
1521:41 are urging us to do nowadays not just numbers not just letters but like really
1521:43 numbers not just letters but like really annoying punctuation so it really looks
1521:46 annoying punctuation so it really looks cryptic not just to the adversary but
1521:48 cryptic not just to the adversary but also to you and me unfortunately and
1521:50 also to you and me unfortunately and that's the downside but here now we have
1521:52 that's the downside but here now we have a mental model and really a
1521:54 a mental model and really a computational framework via which we can
1521:56 computational framework via which we can evaluate the security of these and I'll
1521:58 evaluate the security of these and I'll go ahead and spoil some of the math here
1522:00 go ahead and spoil some of the math here if we've got 52 uh letters of the
1522:02 if we've got 52 uh letters of the alphabet uppercase and lowercase 10
1522:04 alphabet uppercase and lowercase 10 digits and if I count them out on my
1522:06 digits and if I count them out on my keyboard about 32 punctuation symbols in
1522:09 keyboard about 32 punctuation symbols in typical English grammar that actually
1522:10 typical English grammar that actually gives us 94 possibilities now which is
1522:13 gives us 94 possibilities now which is up from 52 which is up from 10 so now
1522:17 up from 52 which is up from 10 so now we're really moving and now that would
1522:19 we're really moving and now that would give us 78 million possibility so
1522:21 give us 78 million possibility so another order of magnitude now it's
1522:23 another order of magnitude now it's still going to be relatively fast
1522:25 still going to be relatively fast because you know what I can actually do
1522:26 because you know what I can actually do this let me go back into my code here
1522:29 this let me go back into my code here let me reopen the same program and I can
1522:32 let me reopen the same program and I can point out just how easy it is to make
1522:33 point out just how easy it is to make these changes instead of importing
1522:35 these changes instead of importing digits as before I can import as your uh
1522:38 digits as before I can import as your uh child might know asky letters which are
1522:40 child might know asky letters which are a through z uppercase lowercase and I
1522:43 a through z uppercase lowercase and I can just change this here asky letters
1522:46 can just change this here asky letters and so this was that first version where
1522:48 and so this was that first version where we just changed to letters let me now
1522:49 we just changed to letters let me now rerun the code and instead of seeing
1522:51 rerun the code and instead of seeing numbers we'll see letters flying across
1522:53 numbers we'll see letters flying across the screen and if I walk over here to
1522:55 the screen and if I walk over here to the screen we'll see that by the time I
1522:57 the screen we'll see that by the time I get here we're halfway through the
1522:59 get here we're halfway through the entire alphabet lowercase if I now start
1523:01 entire alphabet lowercase if I now start walking away I think yep we're already
1523:03 walking away I think yep we're already done now with uppercase as well if I
1523:06 done now with uppercase as well if I upgrade this slightly further let's go
1523:08 upgrade this slightly further let's go ahead and take it one more level and
1523:10 ahead and take it one more level and perhaps do let's say ask letters and
1523:13 perhaps do let's say ask letters and digits and punctuation and this would be
1523:16 digits and punctuation and this would be the pythonic way to say that and I'm
1523:18 the pythonic way to say that and I'm going to add to those letters those same
1523:21 going to add to those letters those same digits those same punctuation symbols
1523:23 digits those same punctuation symbols let me shrink my font just so the code
1523:25 let me shrink my font just so the code still fits on the screen and what we now
1523:27 still fits on the screen and what we now have is with a two seconds of changes a
1523:30 have is with a two seconds of changes a program that if I run this version
1523:33 program that if I run this version whoops without the typographical error
1523:36 whoops without the typographical error this is what we call in cs50 a bug so
1523:39 this is what we call in cs50 a bug so now we run the same this is what we call
1523:41 now we run the same this is what we call in cs50 a second
1523:44 in cs50 a second bug
1523:46 bug punctuation okay this is where I cross
1523:49 punctuation okay this is where I cross my fingers okay so now it's going to be
1523:51 my fingers okay so now it's going to be a little hard to see as it flies across
1523:52 a little hard to see as it flies across the screen but you probably are seeing
1523:54 the screen but you probably are seeing glimpses of some weird punctuation
1523:56 glimpses of some weird punctuation characters as well and I won't waste our
1523:58 characters as well and I won't waste our time trying to talk through this because
1524:00 time trying to talk through this because this is going to take longer we're still
1524:01 this is going to take longer we're still in the lower case I'm still over here
1524:03 in the lower case I'm still over here already we've not even gotten to n now O
1524:06 already we've not even gotten to n now O then P so this is going to run longer
1524:08 then P so this is going to run longer but let's end with one final question on
1524:11 but let's end with one final question on the security of all these systems I'm
1524:12 the security of all these systems I'm going to cancel that by hitting contrl C
1524:14 going to cancel that by hitting contrl C on my keyboard and let's ask the
1524:16 on my keyboard and let's ask the question instead if we use eight
1524:19 question instead if we use eight character passwords so twice as many
1524:20 character passwords so twice as many characters but even that is not terribly
1524:23 characters but even that is not terribly long right this is eight characters
1524:24 long right this is eight characters alone on the stage eight characters
1524:26 alone on the stage eight characters using letters numbers and punctuation
1524:28 using letters numbers and punctuation might be better let's do one final vote
1524:30 might be better let's do one final vote here if we could on your same device how
1524:33 here if we could on your same device how many eight character possibilities are
1524:36 many eight character possibilities are there now
1524:38 there now for these
1524:40 for these passcodes and now for didn't even make
1524:42 passcodes and now for didn't even make the list this
1524:44 the list this time all right few more seconds about
1524:46 time all right few more seconds about 100 responses so
1524:48 100 responses so far how about we go ahead and Carter if
1524:51 far how about we go ahead and Carter if you would't mind let's reveal the
1524:52 you would't mind let's reveal the results based on the vote a pretty
1524:55 results based on the vote a pretty decent spread here although the
1524:56 decent spread here although the quadrillions are quickly buzzing in and
1524:58 quadrillions are quickly buzzing in and they're contending with the others here
1525:00 they're contending with the others here looks like 44% of you said quintilian
1525:02 looks like 44% of you said quintilian 34% said quadrillion and this time for
1525:05 34% said quadrillion and this time for the first time uh you overbid so indeed
1525:08 the first time uh you overbid so indeed if we go back to the math here at least
1525:11 if we go back to the math here at least the majority overbid if we have eight
1525:13 the majority overbid if we have eight character passcodes that gives us 94
1525:15 character passcodes that gives us 94 times itself 8 times or 94 to the eth
1525:18 times itself 8 times or 94 to the eth power and in fact that gives us roughly
1525:20 power and in fact that gives us roughly 6 quadrillion 95 trillion 689 Bill 385
1525:26 6 quadrillion 95 trillion 689 Bill 385 mil 410,000 and
1525:28 mil 410,000 and 86 possible passcodes now what does that
1525:31 86 possible passcodes now what does that mean well the adversary's algorithm the
1525:34 mean well the adversary's algorithm the step-by-step code that they write to try
1525:36 step-by-step code that they write to try to hack into your phone is no different
1525:38 to hack into your phone is no different and honestly if your passcode is eight
1525:40 and honestly if your passcode is eight characters long but there're zeros
1525:43 characters long but there're zeros 00000000000000 you're no more secure
1525:45 00000000000000 you're no more secure fundamentally you really want to be
1525:47 fundamentally you really want to be somewhere in The Sweet Spot of that
1525:49 somewhere in The Sweet Spot of that massive range of values so that if the
1525:51 massive range of values so that if the adversary tries this Brute Force attack
1525:53 adversary tries this Brute Force attack just running through all possibilities
1525:55 just running through all possibilities they will eventually reach your passcode
1525:58 they will eventually reach your passcode just mathematically it will be there
1526:00 just mathematically it will be there hopefully though well maybe not
1526:02 hopefully though well maybe not hopefully you and I and they will be
1526:04 hopefully you and I and they will be gone from this world because that much
1526:06 gone from this world because that much time will have passed and if we fact if
1526:08 time will have passed and if we fact if we do out the math here uh this number
1526:11 we do out the math here uh this number of seconds for instance is long past uh
1526:14 of seconds for instance is long past uh when uh uh we will uh no longer be here
1526:16 when uh uh we will uh no longer be here so that's the sort of measure is we
1526:18 so that's the sort of measure is we don't sort of fundamentally change the
1526:20 don't sort of fundamentally change the equation for the adversary it's still
1526:21 equation for the adversary it's still the same risk it's still the same attack
1526:23 the same risk it's still the same attack but you significantly drive down the
1526:25 but you significantly drive down the probability of success on their part or
1526:27 probability of success on their part or conceptually you drive up the cost to
1526:30 conceptually you drive up the cost to the adversary and indeed even in the
1526:32 the adversary and indeed even in the physical world this is true you just
1526:34 physical world this is true you just want your passcode in the digital world
1526:36 want your passcode in the digital world really to be better than someone else's
1526:38 really to be better than someone else's because you want someone else's passcode
1526:40 because you want someone else's passcode to be the one that the adversary does
1526:42 to be the one that the adversary does something with with just like in the
1526:43 something with with just like in the physical world even though it's a bit uh
1526:45 physical world even though it's a bit uh uncomfortable to consider your house
1526:47 uncomfortable to consider your house doesn't need to be 100% secure and
1526:50 doesn't need to be 100% secure and indeed it's difficult to make it such
1526:52 indeed it's difficult to make it such there's always going to be a a point of
1526:53 there's always going to be a a point of weakness maybe it's that window the door
1526:55 weakness maybe it's that window the door or something like that but if your home
1526:57 or something like that but if your home is more secure than the next door home
1526:59 is more secure than the next door home just probabilistically you are more
1527:02 just probabilistically you are more secure you're not secure and indeed any
1527:04 secure you're not secure and indeed any website you see down the road that says
1527:06 website you see down the road that says we are secure because we do X Y or Z
1527:08 we are secure because we do X Y or Z like that's nonsense security is really
1527:10 like that's nonsense security is really about comparisons and Val ating things
1527:13 about comparisons and Val ating things if quantitatively relative to some other
1527:15 if quantitatively relative to some other system relative to some other code so
1527:18 system relative to some other code so what's the takeaway here well hopefully
1527:20 what's the takeaway here well hopefully a non-trivial number of you we'll go
1527:21 a non-trivial number of you we'll go home this weekend on Monday and change
1527:24 home this weekend on Monday and change at least one passcode um but there's
1527:26 at least one passcode um but there's going to be a trade-off here and we talk
1527:27 going to be a trade-off here and we talk about this all the time in cs50 anytime
1527:29 about this all the time in cs50 anytime we improve something we pay some price
1527:32 we improve something we pay some price in time and performance and cost
1527:34 in time and performance and cost somewhere else so what's the downside
1527:36 somewhere else so what's the downside then of this advice that you should use
1527:38 then of this advice that you should use minimally eight character
1527:40 minimally eight character passcodes why might you want to say nay
1527:43 passcodes why might you want to say nay and not do
1527:44 and not do this say again you have to remember you
1527:47 this say again you have to remember you have to remember it right and so here
1527:49 have to remember it right and so here there's sort of some sociology there's
1527:51 there's sort of some sociology there's some human behavior you know some of you
1527:52 some human behavior you know some of you might have colleagues if you're working
1527:54 might have colleagues if you're working in the real world at least back in
1527:55 in the real world at least back in healthier times when you had colleagues
1527:57 healthier times when you had colleagues with desks and cubicles and there's
1527:58 with desks and cubicles and there's probably one person in the office with
1528:00 probably one person in the office with like a Post-It note on their monitor
1528:02 like a Post-It note on their monitor with their passcode you know it's a bit
1528:03 with their passcode you know it's a bit of a cyber security offense but it's
1528:07 of a cyber security offense but it's also a sort of real world side effect
1528:09 also a sort of real world side effect maybe of corporate policies that aren't
1528:11 maybe of corporate policies that aren't really calibrated for human behavior so
1528:14 really calibrated for human behavior so we'll see if there's some other defenses
1528:15 we'll see if there's some other defenses and indeed let me propose that we talk
1528:17 and indeed let me propose that we talk briefly about one that actually tends to
1528:19 briefly about one that actually tends to kick in automatically even if your
1528:21 kick in automatically even if your passcode is not as strong as we've just
1528:22 passcode is not as strong as we've just seen one of these six quadrillion
1528:24 seen one of these six quadrillion possibilities well what could we do
1528:26 possibilities well what could we do instead well as anyone and I'll zoom in
1528:28 instead well as anyone and I'll zoom in on this here accidentally locked themsel
1528:31 on this here accidentally locked themsel out of their own phone before like when
1528:34 out of their own phone before like when does that happen yeah when you try the
1528:36 does that happen yeah when you try the password too many times yeah so too many
1528:38 password too many times yeah so too many times maybe your finger's slightly off
1528:40 times maybe your finger's slightly off maybe you're slightly off and you just
1528:42 maybe you're slightly off and you just don't input the same passcode correctly
1528:45 don't input the same passcode correctly after like five times 10 times there's
1528:47 after like five times 10 times there's some reasonable threshold and why does
1528:48 some reasonable threshold and why does that happen well Apple and Google
1528:50 that happen well Apple and Google equivalently figure just
1528:52 equivalently figure just probabilistically if after 10 guesses
1528:54 probabilistically if after 10 guesses you still haven't typed in the right
1528:56 you still haven't typed in the right passcode probably you're not you you're
1528:58 passcode probably you're not you you're someone else who's picked up your phone
1528:59 someone else who's picked up your phone so we're just going to go ahead and lock
1529:01 so we're just going to go ahead and lock you out now what's the effect of this
1529:03 you out now what's the effect of this well this means now that each of those
1529:05 well this means now that each of those possible passcodes no longer takes
1529:06 possible passcodes no longer takes roughly 1 second now it takes roughly 1
1529:09 roughly 1 second now it takes roughly 1 minute so the attack is still the same
1529:11 minute so the attack is still the same but if it's now one passcode or 10
1529:13 but if it's now one passcode or 10 guesses per minute we have significantly
1529:16 guesses per minute we have significantly by a factor of 60 in this story slowed
1529:18 by a factor of 60 in this story slowed things down and unfortunately does
1529:19 things down and unfortunately does anyone know what happens if you screw up
1529:21 anyone know what happens if you screw up again after a
1529:23 again after a minute yeah it goes longer it's like
1529:25 minute yeah it goes longer it's like five minutes and then 10 minutes and uh
1529:27 five minutes and then 10 minutes and uh Google is kind of obnoxious about it
1529:29 Google is kind of obnoxious about it they don't even give you a time frame
1529:31 they don't even give you a time frame they just say try again later and so
1529:34 they just say try again later and so that keeps not only the adversary out
1529:36 that keeps not only the adversary out but also potentially you so there in
1529:38 but also potentially you so there in lies that trade-off if you've forgotten
1529:39 lies that trade-off if you've forgotten your code if nowadays your finger is
1529:41 your code if nowadays your finger is slightly wet so the screen isn't
1529:43 slightly wet so the screen isn't responding correctly these could be
1529:45 responding correctly these could be usability downsides too so security is
1529:47 usability downsides too so security is really just about finding The Sweet Spot
1529:49 really just about finding The Sweet Spot among these various tradeoffs here but
1529:52 among these various tradeoffs here but there's other mechanisms too and some of
1529:54 there's other mechanisms too and some of you might recognize this screen from
1529:56 you might recognize this screen from Gmail via which of course you log in but
1529:58 Gmail via which of course you log in but after you log into Gmail or similar
1530:01 after you log into Gmail or similar websites or apps or systems at work
1530:04 websites or apps or systems at work nowadays especially you might be
1530:06 nowadays especially you might be presented with uh what's called
1530:08 presented with uh what's called two-factor authentication and what is
1530:10 two-factor authentication and what is this in a nutshell in Lay person
1530:12 this in a nutshell in Lay person terms most many of you if you do
1530:15 terms most many of you if you do anything digitally at work might have to
1530:17 anything digitally at work might have to do this now
1530:19 do this now yeah exactly you get texted at your
1530:21 yeah exactly you get texted at your phone an additional code that's not your
1530:24 phone an additional code that's not your same password it's typically a numeric
1530:26 same password it's typically a numeric code maybe six digits long it expires
1530:28 code maybe six digits long it expires after a minute or 10 minutes but why is
1530:30 after a minute or 10 minutes but why is this a good thing well one it's no
1530:32 this a good thing well one it's no longer just a piece of information that
1530:34 longer just a piece of information that you know or that you might have written
1530:36 you know or that you might have written down it's information that changes every
1530:38 down it's information that changes every time you try to log in but more
1530:40 time you try to log in but more importantly it's a fundamentally second
1530:42 importantly it's a fundamentally second factor which means it's not just
1530:44 factor which means it's not just something you know now it's something
1530:45 something you know now it's something you have so you for instance are the
1530:47 you have so you for instance are the only one theoretically that should be
1530:49 only one theoretically that should be receiving that code and so now the
1530:51 receiving that code and so now the adversary if they want to get into your
1530:52 adversary if they want to get into your account not only have to guess or brute
1530:54 account not only have to guess or brute force or maybe read off of a Post-It
1530:56 force or maybe read off of a Post-It note your password they also have to
1530:58 note your password they also have to physically have access now to that phone
1531:00 physically have access now to that phone so there's still a threat absolutely but
1531:02 so there's still a threat absolutely but it's not everyone on the internet with
1531:04 it's not everyone on the internet with an internet connection now it's only the
1531:06 an internet connection now it's only the people in Starbucks now it's only the
1531:08 people in Starbucks now it's only the people at work now it's only the people
1531:10 people at work now it's only the people in your home who might have access to
1531:12 in your home who might have access to that second Factor so there too it just
1531:14 that second Factor so there too it just raises the bar to the adversary making
1531:16 raises the bar to the adversary making it harder more timec consuming more
1531:17 it harder more timec consuming more geographically impossible for them to
1531:19 geographically impossible for them to attack you but what's the downside of
1531:21 attack you but what's the downside of two-factor authentication whether it's a
1531:23 two-factor authentication whether it's a device or even nowadays it's in software
1531:26 device or even nowadays it's in software whether it's on your keychain or on your
1531:27 whether it's on your keychain or on your phone where you're prompted for this
1531:29 phone where you're prompted for this code what's a downside as some of us
1531:31 code what's a downside as some of us have probably experienced
1531:33 have probably experienced too forg you forget your cell phone
1531:36 too forg you forget your cell phone absolutely right the the factor that you
1531:38 absolutely right the the factor that you have you don't have with you or maybe
1531:40 have you don't have with you or maybe you're in a basement somewhere don't
1531:41 you're in a basement somewhere don't have reception you're on a plane you
1531:42 have reception you're on a plane you can't get the code and so there too are
1531:45 can't get the code and so there too are these tradeoffs and even it departments
1531:46 these tradeoffs and even it departments need to keep that in mind because what
1531:48 need to keep that in mind because what does that mean for them well if you
1531:50 does that mean for them well if you don't have your phone with you and you
1531:51 don't have your phone with you and you are in the habit of calling it to help
1531:53 are in the habit of calling it to help you fix this now there's a a cost a
1531:55 you fix this now there's a a cost a human cost maybe even a financial cost
1531:57 human cost maybe even a financial cost and so it policy nowadays is really just
1532:00 and so it policy nowadays is really just about finding the right balance and
1532:02 about finding the right balance and where we want to spend our resources but
1532:03 where we want to spend our resources but at least raise the bar to the adversary
1532:07 at least raise the bar to the adversary but of course there's other ways too and
1532:09 but of course there's other ways too and this is going to be one of our homework
1532:11 this is going to be one of our homework assignments if you will after today
1532:12 assignments if you will after today there's this software called password
1532:14 there's this software called password managers and no need to buzz in on your
1532:16 managers and no need to buzz in on your phone but maybe with a physical hand how
1532:18 phone but maybe with a physical hand how many folks here use a password manager
1532:22 many folks here use a password manager okay let me ballpark this at 10 20%
1532:25 okay let me ballpark this at 10 20% perhaps Okay so we've got 80% upside
1532:27 perhaps Okay so we've got 80% upside here and a lesson learned potentially so
1532:29 here and a lesson learned potentially so a password manager is just a piece of
1532:31 a password manager is just a piece of software on your Mac your PC or your
1532:32 software on your Mac your PC or your phone nowadays that manages your
1532:35 phone nowadays that manages your passwords well what is that mean when
1532:36 passwords well what is that mean when you go to a website for the first time
1532:38 you go to a website for the first time or you download an app for the first
1532:39 or you download an app for the first time and you have to create an account
1532:41 time and you have to create an account you can can still use your email address
1532:43 you can can still use your email address or David as your username or whatever
1532:45 or David as your username or whatever your name might be so you don't have to
1532:46 your name might be so you don't have to change that methodology but instead of
1532:49 change that methodology but instead of typing in one 2 3 4 5 six as your same
1532:52 typing in one 2 3 4 5 six as your same password for that website or app as well
1532:54 password for that website or app as well as for every other now you use the
1532:57 as for every other now you use the password manager software to generate
1532:59 password manager software to generate something difficult to guess for you
1533:01 something difficult to guess for you that is you tell the password manager
1533:03 that is you tell the password manager give me an8 character random passcode
1533:06 give me an8 character random passcode not 0000 but something with punctuation
1533:08 not 0000 but something with punctuation with numbers with letters and better yet
1533:10 with numbers with letters and better yet the password manager is the name
1533:12 the password manager is the name suggests remembers that password for you
1533:15 suggests remembers that password for you and the next time you go to another
1533:16 and the next time you go to another website you do it again with a
1533:17 website you do it again with a completely different password maybe same
1533:19 completely different password maybe same username maybe two-factor authentication
1533:21 username maybe two-factor authentication but different password different
1533:22 but different password different password different password and it
1533:24 password different password and it doesn't have to be eight I mean I'm in
1533:25 doesn't have to be eight I mean I'm in the habit of using a dozen two dozen
1533:28 the habit of using a dozen two dozen characters in total and at that point I
1533:30 characters in total and at that point I can't even pronounce the number of
1533:31 can't even pronounce the number of possibilities because it goes well be
1533:33 possibilities because it goes well be Beyond uh the quadrillions so the
1533:36 Beyond uh the quadrillions so the probability that someone's going to get
1533:37 probability that someone's going to get into one of those accounts for me now is
1533:39 into one of those accounts for me now is very very very low and they're going to
1533:41 very very very low and they're going to take less interest in me and maybe more
1533:43 take less interest in me and maybe more interest in someone else that's not
1533:45 interest in someone else that's not using as good of a password now what
1533:47 using as good of a password now what does this mean in real terms well when
1533:48 does this mean in real terms well when you go to log into that managed site you
1533:51 you go to log into that managed site you don't manually type your password
1533:53 don't manually type your password anymore in fact you don't generally even
1533:55 anymore in fact you don't generally even need to know it nowadays I probably
1533:57 need to know it nowadays I probably don't know 90 plus 99% of my passwords I
1534:01 don't know 90 plus 99% of my passwords I entrust them to this password manager
1534:03 entrust them to this password manager now of course you'd like to think that
1534:05 now of course you'd like to think that the password manager itself is secure so
1534:08 the password manager itself is secure so what might that mean well those of you
1534:10 what might that mean well those of you who do use a password manager how do you
1534:12 who do use a password manager how do you access that software itself what's
1534:15 access that software itself what's protecting your data in your
1534:17 protecting your data in your understanding so maybe Biometrics like
1534:19 understanding so maybe Biometrics like your your face ID or maybe your
1534:21 your your face ID or maybe your fingerprint or maybe more simply what
1534:23 fingerprint or maybe more simply what else password maybe just a password and
1534:26 else password maybe just a password and hopefully that password that primary
1534:29 hopefully that password that primary password that gatekeeper is not itself 1
1534:31 password that gatekeeper is not itself 1 2 3 4 5 6 otherwise it doesn't matter
1534:34 2 3 4 5 6 otherwise it doesn't matter how secure all of the others are but if
1534:36 how secure all of the others are but if you're willing to put in the effort and
1534:38 you're willing to put in the effort and pick one pretty long somewhat random
1534:41 pick one pretty long somewhat random very unguessable password that you just
1534:43 very unguessable password that you just promise to commit to memory and maybe
1534:45 promise to commit to memory and maybe for backup you literally print it out
1534:47 for backup you literally print it out and put it in a safe deposit box or a
1534:48 and put it in a safe deposit box or a safe or just kind of hide it somewhere
1534:50 safe or just kind of hide it somewhere physically that there's very low
1534:52 physically that there's very low probability someone's going to find the
1534:53 probability someone's going to find the backup copy that might be Al loan but of
1534:55 backup copy that might be Al loan but of course the flip side is now if you
1534:58 course the flip side is now if you forget that Ma that primary password
1535:00 forget that Ma that primary password you've now lost all of the eggs in the
1535:02 you've now lost all of the eggs in the basket if someone gets that primary
1535:04 basket if someone gets that primary password now they have access to
1535:06 password now they have access to everything so that's rather the
1535:07 everything so that's rather the trade-off but I dare say you're probably
1535:10 trade-off but I dare say you're probably less threatened depending on your family
1535:12 less threatened depending on your family uh uh by the people immediately around
1535:14 uh uh by the people immediately around you than the billions of other people on
1535:17 you than the billions of other people on the internet that have access
1535:18 the internet that have access potentially to those same systems so
1535:20 potentially to those same systems so there to it's a trade-off and it's up to
1535:22 there to it's a trade-off and it's up to you to decide whether or not to manage
1535:24 you to decide whether or not to manage your passwords in this way but if you
1535:26 your passwords in this way but if you were on that top 10 list or even if
1535:28 were on that top 10 list or even if you're not but you can think of several
1535:30 you're not but you can think of several accounts that all have the same password
1535:32 accounts that all have the same password you're probably going to benefit from
1535:33 you're probably going to benefit from something like this and why is it bad to
1535:35 something like this and why is it bad to be clear to use the same password on
1535:38 be clear to use the same password on multiple sites in case that's never sort
1535:40 multiple sites in case that's never sort of dawned
1535:42 of dawned in thought why is that a bad thing to
1535:45 in thought why is that a bad thing to reuse a password on different websites
1535:47 reuse a password on different websites different apps any intuition yeah and
1535:55 back exactly once it's attacked you can the adversary presumably by transitivity
1535:57 the adversary presumably by transitivity can see oh well if this user's username
1535:59 can see oh well if this user's username is mailin harvard.edu on this website
1536:01 is mailin harvard.edu on this website and their password is foolishly 1 2 3 4
1536:03 and their password is foolishly 1 2 3 4 56 or even something way more
1536:06 56 or even something way more complicated they can probably just
1536:07 complicated they can probably just assume with high probability that if I'm
1536:09 assume with high probability that if I'm being a little reckless let's try access
1536:11 being a little reckless let's try access mailin at harvard.edu other accounts
1536:14 mailin at harvard.edu other accounts other apps using that exact same
1536:16 other apps using that exact same password and so by transitivity
1536:17 password and so by transitivity essentially you're putting your other uh
1536:19 essentially you're putting your other uh Accounts at risk so what's maybe a
1536:21 Accounts at risk so what's maybe a takeaway minimally here I would start to
1536:23 takeaway minimally here I would start to reconsider your passcodes on your most
1536:25 reconsider your passcodes on your most important data Maybe it's medical maybe
1536:26 important data Maybe it's medical maybe it's Financial maybe it's email anything
1536:28 it's Financial maybe it's email anything remotely personal that you really
1536:30 remotely personal that you really wouldn't want to have access uh do you
1536:32 wouldn't want to have access uh do you necessarily need the same form level of
1536:34 necessarily need the same form level of security on eCommerce sites or sites
1536:37 security on eCommerce sites or sites that you don't really care about or that
1536:38 that you don't really care about or that you signed up for once and after that H
1536:40 you signed up for once and after that H that's it probably not so you can decide
1536:42 that's it probably not so you can decide for yourself but again software like a
1536:44 for yourself but again software like a password manager and these are just some
1536:46 password manager and these are just some of the possibilities out there um are
1536:48 of the possibilities out there um are probably to be your friend a couple of
1536:50 probably to be your friend a couple of these are free they come with Windows or
1536:52 these are free they come with Windows or Mac OS a couple are commercial Harvard
1536:53 Mac OS a couple are commercial Harvard has a site license for students for uh
1536:56 has a site license for students for uh one of these as well so there are
1536:57 one of these as well so there are options out there but what else do
1536:59 options out there but what else do people use what else can people use to
1537:01 people use what else can people use to keep their system secure so most of us
1537:03 keep their system secure so most of us nowadays have probably heard of
1537:06 nowadays have probably heard of encryption this technique for just kind
1537:08 encryption this technique for just kind of scrambling information so when you
1537:09 of scrambling information so when you want to send a message an email or
1537:11 want to send a message an email or upload a a photograph or use your credit
1537:14 upload a a photograph or use your credit card hopefully it's not just being sent
1537:16 card hopefully it's not just being sent out for all to see but there's some kind
1537:17 out for all to see but there's some kind of scrambling going on and some fancy
1537:19 of scrambling going on and some fancy mathematics ensure that encryption
1537:21 mathematics ensure that encryption ensures that only you the sender and
1537:24 ensures that only you the sender and someone else the receiver can
1537:25 someone else the receiver can theoretically see what that credit card
1537:28 theoretically see what that credit card number is what that message is what that
1537:29 number is what that message is what that photograph is instead so encryption is
1537:32 photograph is instead so encryption is sort of Common Place nowadays both in
1537:34 sort of Common Place nowadays both in websites and apps and ATMs and other
1537:37 websites and apps and ATMs and other such devices but how does it work well
1537:38 such devices but how does it work well back in week two of cs50 uh your child
1537:41 back in week two of cs50 uh your child learned learned a little something about
1537:42 learned learned a little something about encryption otherwise known as
1537:43 encryption otherwise known as cryptography and one of the algorithms
1537:46 cryptography and one of the algorithms we talked about was quite simply
1537:47 we talked about was quite simply something like this this is what we
1537:50 something like this this is what we might call uh not only cs50 but plain
1537:52 might call uh not only cs50 but plain text so very plain text that in this
1537:54 text so very plain text that in this case is English and obviously everyone
1537:56 case is English and obviously everyone in the room can read it but what if I
1537:58 in the room can read it but what if I wanted to send this message out to
1537:59 wanted to send this message out to someone in this room or out on the
1538:01 someone in this room or out on the Internet or maybe equivalently back in
1538:03 Internet or maybe equivalently back in the day maybe write a message down on a
1538:05 the day maybe write a message down on a scrap of paper in grade school and pass
1538:07 scrap of paper in grade school and pass a secret note a secret love note to
1538:08 a secret note a secret love note to someone in class with hopes that the
1538:11 someone in class with hopes that the teach or any other students in the class
1538:13 teach or any other students in the class can't intercept it and read it well you
1538:14 can't intercept it and read it well you probably don't want to say this is cs50
1538:17 probably don't want to say this is cs50 or I love you or anything remotely
1538:19 or I love you or anything remotely sensitive but rather maybe you want to
1538:21 sensitive but rather maybe you want to encrypt it and let's change the the T to
1538:23 encrypt it and let's change the the T to a u maybe change the H to an i the I to
1538:26 a u maybe change the H to an i the I to a j the S to a t the I to a j the S to a
1538:30 a j the S to a t the I to a j the S to a t again the C to a d the S to a T and
1538:32 t again the C to a d the S to a T and we'll just leave the numbers alone even
1538:34 we'll just leave the numbers alone even though I worry someone could probably
1538:36 though I worry someone could probably guess what this now does say nonetheless
1538:38 guess what this now does say nonetheless but what was the algorithm as I rattled
1538:40 but what was the algorithm as I rattled those ch changes off whether student
1538:42 those ch changes off whether student from week two or parent from week now
1538:46 from week two or parent from week now yeah a on shift just a onlet shift and
1538:49 yeah a on shift just a onlet shift and this is more sophisticatedly called a
1538:51 this is more sophisticatedly called a rotational Cipher or a Caesar Cipher
1538:53 rotational Cipher or a Caesar Cipher after Caesar back in the day it's
1538:55 after Caesar back in the day it's relatively simplistic but back in the
1538:57 relatively simplistic but back in the day it's not so simplistic if you're the
1538:59 day it's not so simplistic if you're the first person in the world to ever use it
1539:01 first person in the world to ever use it or think of it but nowadays this is not
1539:03 or think of it but nowadays this is not actually what we use but it's similarly
1539:05 actually what we use but it's similarly mathematical in nature it's not quite as
1539:07 mathematical in nature it's not quite as simple as just adding one or subtracting
1539:09 simple as just adding one or subtracting one to go from now what we'd call text
1539:12 one to go from now what we'd call text to plain test but it's similarly math
1539:14 to plain test but it's similarly math that's involved and let me just
1539:15 that's involved and let me just stipulate that the way the math works is
1539:16 stipulate that the way the math works is that the sender and the receiver just
1539:19 that the sender and the receiver just have to have in mind some kind of secret
1539:21 have to have in mind some kind of secret and the Secret in this case would very
1539:22 and the Secret in this case would very trivially be one but it could be a much
1539:25 trivially be one but it could be a much bigger much more unguessable number or
1539:27 bigger much more unguessable number or maybe some other secret we share the
1539:29 maybe some other secret we share the presumption being that my classmates my
1539:31 presumption being that my classmates my teacher in that grade school classroom
1539:33 teacher in that grade school classroom if they don't know what that secret is
1539:34 if they don't know what that secret is that number is yeah they could try to
1539:36 that number is yeah they could try to brute force it and try all possible
1539:38 brute force it and try all possible mathematics plus one plus 2 plus three
1539:40 mathematics plus one plus 2 plus three but that's going to take them some time
1539:42 but that's going to take them some time and they probably don't care enough and
1539:43 and they probably don't care enough and so my data might be therefore relatively
1539:46 so my data might be therefore relatively secure but we use encryption all the
1539:48 secure but we use encryption all the time nowadays and so for instance this
1539:50 time nowadays and so for instance this is at the start of most URLs nowadays
1539:52 is at the start of most URLs nowadays even if you don't type it yourself with
1539:54 even if you don't type it yourself with that said Safari and even Chrome now or
1539:57 that said Safari and even Chrome now or kind of simplifying if not dumbing down
1539:59 kind of simplifying if not dumbing down user interfaces to just hide details
1540:01 user interfaces to just hide details that you and I as sort of normal users
1540:03 that you and I as sort of normal users don't need to see 24/7 but it is there
1540:06 don't need to see 24/7 but it is there and if in fact on your phone or laptop
1540:08 and if in fact on your phone or laptop you click on the URL even if it's super
1540:10 you click on the URL even if it's super short initially you'll probably see the
1540:12 short initially you'll probably see the whole thing starting with this and the s
1540:14 whole thing starting with this and the s means secure the s means that encryption
1540:16 means secure the s means that encryption is being used but there's other forms of
1540:18 is being used but there's other forms of this not just when you visit websites
1540:20 this not just when you visit websites there's this endtoend encryption which
1540:23 there's this endtoend encryption which is being talked about more nowadays
1540:25 is being talked about more nowadays especially during coid times with so
1540:27 especially during coid times with so many more of us on video and talking
1540:29 many more of us on video and talking about uh more sensitive things tele
1540:31 about uh more sensitive things tele medicine talking to doctors things that
1540:32 medicine talking to doctors things that you also wouldn't want to verbally or
1540:34 you also wouldn't want to verbally or visually get out into the wild just like
1540:36 visually get out into the wild just like text what's different about endtoend
1540:39 text what's different about endtoend encryption versus HTT GPS and the type
1540:42 encryption versus HTT GPS and the type of encryption that most of us use every
1540:43 of encryption that most of us use every day on websites
1540:45 day on websites alone end to end encryption is sort of a
1540:48 alone end to end encryption is sort of a better feature that you want to
1540:49 better feature that you want to increasingly seek when using services
1540:52 increasingly seek when using services like Zoom or Microsoft teams or Whatsapp
1540:55 like Zoom or Microsoft teams or Whatsapp or the
1540:56 or the like any instincts here yeah over on the
1541:08 right good so the encryption the scrambling of information happens in The
1541:09 scrambling of information happens in The Source the sender and the destination
1541:11 Source the sender and the destination the receiver without a so-called
1541:13 the receiver without a so-called middleman in between and this is
1541:15 middleman in between and this is actually very different from most
1541:17 actually very different from most contexts nowadays that use just https
1541:19 contexts nowadays that use just https because when you're using https to buy
1541:21 because when you're using https to buy something on Amazon securely with your
1541:23 something on Amazon securely with your credit card well of course Amazon needs
1541:25 credit card well of course Amazon needs to be able to decrep the message at the
1541:27 to be able to decrep the message at the end of the day and so that's fine but
1541:29 end of the day and so that's fine but even when you're using services like
1541:31 even when you're using services like video conferencing or maybe text
1541:33 video conferencing or maybe text messaging nowadays well if you're using
1541:35 messaging nowadays well if you're using Whatsapp that's owned by meta and if
1541:37 Whatsapp that's owned by meta and if you're using Instagram that's owned by
1541:39 you're using Instagram that's owned by meta there's a lot of middlemen in these
1541:41 meta there's a lot of middlemen in these apps that we're using and if they were
1541:43 apps that we're using and if they were only using encryption period or only
1541:46 only using encryption period or only using something like https yes your en
1541:49 using something like https yes your en your connection from you to WhatsApp and
1541:51 your connection from you to WhatsApp and in turn to the recipient might very well
1541:53 in turn to the recipient might very well be secure on each end of that channel
1541:56 be secure on each end of that channel but meta in the between the company and
1541:58 but meta in the between the company and any other company in between could
1542:00 any other company in between could theoretically For Better or For Worse be
1542:01 theoretically For Better or For Worse be looking at that data whether it's to
1542:03 looking at that data whether it's to mine it for advertising purposes whether
1542:05 mine it for advertising purposes whether it's to Snoop on data that you're
1542:06 it's to Snoop on data that you're sending that is not end to-end
1542:08 sending that is not end to-end encryption if the middleman a company
1542:10 encryption if the middleman a company typically has technically access to that
1542:12 typically has technically access to that data now zoom and Microsoft teams and
1542:15 data now zoom and Microsoft teams and WhatsApp and iMessage and other services
1542:17 WhatsApp and iMessage and other services with which you're familiar increasingly
1542:19 with which you're familiar increasingly are offering stronger guarantees of
1542:21 are offering stronger guarantees of encryption whereby it's indeed between
1542:23 encryption whereby it's indeed between parties A and B and not the one in the
1542:26 parties A and B and not the one in the middle now there's downsides here and
1542:27 middle now there's downsides here and you can actually see this kind of
1542:29 you can actually see this kind of functionality manifest in certain
1542:31 functionality manifest in certain settings for instance besides iMessage
1542:34 settings for instance besides iMessage uh which just does this for you on
1542:35 uh which just does this for you on iPhones or Macs besides Zoom um you can
1542:38 iPhones or Macs besides Zoom um you can actually fine-tune these settings indeed
1542:40 actually fine-tune these settings indeed within Zoom itself s so here's a
1542:41 within Zoom itself s so here's a screenshot that I took last night of
1542:43 screenshot that I took last night of just what the user interface looks like
1542:44 just what the user interface looks like today to create a new Zoom meeting with
1542:47 today to create a new Zoom meeting with the latest version of Zoom software and
1542:49 the latest version of Zoom software and maybe unbeknownst to you there's a
1542:51 maybe unbeknownst to you there's a choice of buttons down here and most
1542:53 choice of buttons down here and most likely yours is by default on enhanced
1542:56 likely yours is by default on enhanced encryption which is brilliant marketing
1542:58 encryption which is brilliant marketing speak because it's just encryption it's
1542:59 speak because it's just encryption it's not enhanced it actually ironically
1543:01 not enhanced it actually ironically means worse than this um but they want
1543:03 means worse than this um but they want you using it most likely why well it's a
1543:06 you using it most likely why well it's a little easier to implement it's a little
1543:08 little easier to implement it's a little less expensive for them computationally
1543:10 less expensive for them computationally and to be fair enhanced encryption does
1543:12 and to be fair enhanced encryption does scramble the data but not in a way that
1543:15 scramble the data but not in a way that Zoom can't see it Zoom can indeed see it
1543:17 Zoom can't see it Zoom can indeed see it but that's actually a plus in some
1543:19 but that's actually a plus in some context because if you want to do like
1543:21 context because if you want to do like Cloud recordings and you want a meeting
1543:22 Cloud recordings and you want a meeting recorded not on your Mac or PC but like
1543:24 recorded not on your Mac or PC but like let Zoom deal with that if you want
1543:26 let Zoom deal with that if you want automatic transcription nowadays so the
1543:28 automatic transcription nowadays so the words to appear whether it's English or
1543:30 words to appear whether it's English or something else on the screen well you
1543:31 something else on the screen well you can't really lock Zoom or any other
1543:33 can't really lock Zoom or any other middleman out of that because someone
1543:35 middleman out of that because someone needs to save it to the cloud someone
1543:37 needs to save it to the cloud someone needs to translate the voice to those
1543:39 needs to translate the voice to those English or some other language words so
1543:41 English or some other language words so enhanced encryption enables those
1543:43 enhanced encryption enables those features but they also allow a bad actor
1543:45 features but they also allow a bad actor malicious employee someone who's just
1543:47 malicious employee someone who's just nosy at Zoom or the equivalent middleman
1543:49 nosy at Zoom or the equivalent middleman to just kind of poke around your video
1543:51 to just kind of poke around your video conference and hear what you've said or
1543:53 conference and hear what you've said or see what you've typed as well unless you
1543:55 see what you've typed as well unless you instead check this box as well so
1543:58 instead check this box as well so increasingly look for mentions of end
1544:00 increasingly look for mentions of end to-end encryption or give that some
1544:01 to-end encryption or give that some thought when you choose a technology via
1544:03 thought when you choose a technology via which to communicate with someone
1544:05 which to communicate with someone whether it's within your family or
1544:07 whether it's within your family or without as well now last but not least
1544:10 without as well now last but not least there's other applications of encryption
1544:12 there's other applications of encryption too and this too might be a lesson
1544:14 too and this too might be a lesson learned as well full dis encryption so a
1544:17 learned as well full dis encryption so a dis is like where your data stored in
1544:19 dis is like where your data stored in your Mac or PC or even your phone and
1544:22 your Mac or PC or even your phone and full dis encryption just means ideally
1544:24 full dis encryption just means ideally that all of your data is encrypted that
1544:26 that all of your data is encrypted that is somehow scrambled now hopefully your
1544:28 is somehow scrambled now hopefully your password for your computer or phone is
1544:30 password for your computer or phone is good enough that even though the device
1544:32 good enough that even though the device is encrypted with that password at least
1544:34 is encrypted with that password at least you'll remember it and your phone or
1544:36 you'll remember it and your phone or your Mac or PC will automatically
1544:37 your Mac or PC will automatically decrypt it for you of course you can't
1544:39 decrypt it for you of course you can't scramble the information and hide it
1544:40 scramble the information and hide it from from ourselves one of us at least
1544:42 from from ourselves one of us at least for these devices needs to have access
1544:44 for these devices needs to have access but full dis encryption typically means
1544:46 but full dis encryption typically means that at least when you close the laptop
1544:48 that at least when you close the laptop lid or power down for the night that
1544:50 lid or power down for the night that even if someone else steals that device
1544:52 even if someone else steals that device opens the lid unless they don't unless
1544:55 opens the lid unless they don't unless they have your passcode they can't even
1544:57 they have your passcode they can't even plug in fancy cables to the device and
1544:59 plug in fancy cables to the device and just rip the zeros and ones off of the
1545:01 just rip the zeros and ones off of the device and see what's actually there
1545:03 device and see what's actually there full dis encryption means they could do
1545:05 full dis encryption means they could do that but they would just see seemingly
1545:07 that but they would just see seemingly random zeros and ones now there's a
1545:09 random zeros and ones now there's a downside here too this might slow things
1545:11 downside here too this might slow things down potentially but it is a feature
1545:13 down potentially but it is a feature increasingly that's offered and is
1545:15 increasingly that's offered and is absolutely something you should consider
1545:16 absolutely something you should consider enabling um in general especially if
1545:18 enabling um in general especially if your laptop or phone travels with you
1545:20 your laptop or phone travels with you and certainly your phone does or if you
1545:22 and certainly your phone does or if you plan to donate or sell or give away a
1545:25 plan to donate or sell or give away a device you don't want to leave all of
1545:27 device you don't want to leave all of the zeros and ones the remnants of your
1545:28 the zeros and ones the remnants of your own sensitive data passed on there so
1545:30 own sensitive data passed on there so Windows has a feature called bit Locker
1545:32 Windows has a feature called bit Locker Mac OS has a feature called file Vault
1545:34 Mac OS has a feature called file Vault there's commercial options as well but
1545:36 there's commercial options as well but generally we're at the point now in 2022
1545:38 generally we're at the point now in 2022 we're clicking a button is suff
1545:40 we're clicking a button is suff efficient to enable these features with
1545:43 efficient to enable these features with that said don't rush into all of these
1545:45 that said don't rush into all of these decisions I would make backups of your
1545:47 decisions I would make backups of your data and don't maybe email cs50 if
1545:49 data and don't maybe email cs50 if something goes wrong with that process
1545:51 something goes wrong with that process but I would do your own due diligence
1545:52 but I would do your own due diligence but this too would be a menu of
1545:55 but this too would be a menu of possibilities and now the bad side the
1545:57 possibilities and now the bad side the downside of what seems to be great this
1546:00 downside of what seems to be great this notion of full dis encryption
1546:02 notion of full dis encryption unfortunately just as we can encrypt our
1546:04 unfortunately just as we can encrypt our data to protect it from the adversaries
1546:06 data to protect it from the adversaries so can the adversaries if they get into
1546:08 so can the adversaries if they get into our devices encrypt our data and do what
1546:12 our devices encrypt our data and do what not tell us that secret key and so this
1546:14 not tell us that secret key and so this is generally applied in the context of
1546:16 is generally applied in the context of ransomware which tragically you
1546:17 ransomware which tragically you increasingly hear about in Hospital
1546:20 increasingly hear about in Hospital Systems school systems municipalities
1546:22 Systems school systems municipalities where systems are getting attacked and
1546:23 where systems are getting attacked and the data is not just getting stolen
1546:25 the data is not just getting stolen because what is the adversary typically
1546:26 because what is the adversary typically need with like local municipal or even
1546:28 need with like local municipal or even Hospital data the value to the adversary
1546:31 Hospital data the value to the adversary is encrypting all of the hospital all of
1546:34 is encrypting all of the hospital all of the mpali data preventing them from
1546:37 the mpali data preventing them from accessing it if they have no backups or
1546:38 accessing it if they have no backups or the like and so ransomware is liter
1546:40 the like and so ransomware is liter Lally about trying to convince someone
1546:41 Lally about trying to convince someone to pay you money or pay you Bitcoin or
1546:43 to pay you money or pay you Bitcoin or something like that to give you that
1546:45 something like that to give you that secret key and this key in this case is
1546:47 secret key and this key in this case is surely more sophisticated than the
1546:49 surely more sophisticated than the number one but it's really the same idea
1546:51 number one but it's really the same idea so here too yet again a trade-off just
1546:53 so here too yet again a trade-off just as we sort of invent something for good
1546:55 as we sort of invent something for good it can also be used for evil and so to
1546:57 it can also be used for evil and so to speak as well but it's really the same
1546:59 speak as well but it's really the same underlying principles even though we
1547:01 underlying principles even though we keep seeing it and hearing about it in
1547:03 keep seeing it and hearing about it in these different forms and lastly if only
1547:06 these different forms and lastly if only because folks are generally familiar but
1547:08 because folks are generally familiar but don't necessarily know what it is that
1547:10 don't necessarily know what it is that it's doing for them browsers nowadays
1547:12 it's doing for them browsers nowadays have what's often called incognito mode
1547:14 have what's often called incognito mode or private mode which has nothing to do
1547:16 or private mode which has nothing to do with encryption but does have to do with
1547:18 with encryption but does have to do with cyber security or really cyber privacy
1547:20 cyber security or really cyber privacy keeping your data from prying eyes uh
1547:23 keeping your data from prying eyes uh incognito mode if you open it in Chrome
1547:25 incognito mode if you open it in Chrome for instance looks a little something
1547:26 for instance looks a little something like this and we use it in cs50 when
1547:28 like this and we use it in cs50 when introducing students as we did last week
1547:30 introducing students as we did last week to web programming because it in effect
1547:32 to web programming because it in effect lets you start with a clean slate like a
1547:34 lets you start with a clean slate like a brand new browser that has never visited
1547:36 brand new browser that has never visited any websites before which is good for
1547:37 any websites before which is good for just diagnosing problems but it's often
1547:39 just diagnosing problems but it's often commonly used if you want to log into
1547:41 commonly used if you want to log into maybe your Gmail account on someone
1547:42 maybe your Gmail account on someone else's computer and you don't want your
1547:44 else's computer and you don't want your password being saved or you want to
1547:45 password being saved or you want to visit some website where you don't want
1547:47 visit some website where you don't want the URL or the Search terms ending up in
1547:48 the URL or the Search terms ending up in your autocomplete history so there's
1547:50 your autocomplete history so there's multiple uses for incognito mode but
1547:53 multiple uses for incognito mode but what does it really do well it doesn't
1547:55 what does it really do well it doesn't stop your company it doesn't stop your
1547:57 stop your company it doesn't stop your University your internet service
1547:59 University your internet service provider be it Comcast Verizon or the
1548:01 provider be it Comcast Verizon or the like from knowing what websites you go
1548:03 like from knowing what websites you go to because ask your student a couple
1548:05 to because ask your student a couple weeks ago we talked about actually a
1548:07 weeks ago we talked about actually a week ago we talked about how the
1548:08 week ago we talked about how the internet works and unfortunately
1548:10 internet works and unfortunately computer has an IP address which is
1548:12 computer has an IP address which is unique identifier which goes out anytime
1548:13 unique identifier which goes out anytime you go anywhere incognito mode or not so
1548:16 you go anywhere incognito mode or not so this isn't really covering your tracks
1548:18 this isn't really covering your tracks outside of your office or outside of
1548:20 outside of your office or outside of your home or outside of your company but
1548:22 your home or outside of your company but it is at least throwing away local
1548:24 it is at least throwing away local information and so we'll talk in fact in
1548:26 information and so we'll talk in fact in cs50 is week nine this coming Monday
1548:28 cs50 is week nine this coming Monday about cookies which you might generally
1548:31 about cookies which you might generally know about and what are called sessions
1548:32 know about and what are called sessions and so long story short what incognito
1548:35 and so long story short what incognito mode does is it throws away when you
1548:37 mode does is it throws away when you close the window any locally stored
1548:39 close the window any locally stored information to the these things called
1548:41 information to the these things called cookies which are sort of like virtual
1548:42 cookies which are sort of like virtual handstamps that just remember what
1548:43 handstamps that just remember what you've logged in as or what's in your
1548:45 you've logged in as or what's in your shopping cart or the like but it doesn't
1548:48 shopping cart or the like but it doesn't hide any information from anyone outside
1548:50 hide any information from anyone outside of your own Mac or PC it only prevents
1548:52 of your own Mac or PC it only prevents those local prying eyes so there too
1548:54 those local prying eyes so there too even though we have tools that many of
1548:56 even though we have tools that many of you are probably in the habit of using
1548:57 you are probably in the habit of using or thinking you should use to um be more
1549:00 or thinking you should use to um be more private be more secure on the internet
1549:01 private be more secure on the internet what we do really in cs50 boast week's
1549:04 what we do really in cs50 boast week's uh past and future is talk about how
1549:07 uh past and future is talk about how these Technologies work so that
1549:08 these Technologies work so that ultimately we have all the more of an
1549:09 ultimately we have all the more of an educated citizenry um here among
1549:12 educated citizenry um here among undergrads and here as well as on line
1549:14 undergrads and here as well as on line so that you can apply these same Lessons
1549:16 so that you can apply these same Lessons Learned to problems you'll encounter in
1549:18 Learned to problems you'll encounter in the future so as promised the homework
1549:20 the future so as promised the homework one should probably use a password
1549:23 one should probably use a password manager doesn't have to be one of those
1549:24 manager doesn't have to be one of those ones on the list but at least starting
1549:26 ones on the list but at least starting that conversation maybe with someone who
1549:28 that conversation maybe with someone who does maybe the you know it's often the
1549:30 does maybe the you know it's often the you know the the the student in your
1549:32 you know the the the student in your family perhaps who can advise you on
1549:33 family perhaps who can advise you on some of these Technologies consider
1549:35 some of these Technologies consider using a password manager to using
1549:37 using a password manager to using two-factor authentication whether it's
1549:39 two-factor authentication whether it's your phone or some key fob or or the
1549:40 your phone or some key fob or or the like but at least seeking out that
1549:42 like but at least seeking out that feature at least for accounts that you
1549:43 feature at least for accounts that you really care about your email social
1549:45 really care about your email social media Financial medical anything where
1549:47 media Financial medical anything where you'd be embarrassed at best or really
1549:49 you'd be embarrassed at best or really violated at worst if that kind of
1549:51 violated at worst if that kind of information got out and then
1549:52 information got out and then increasingly using not just encryption
1549:54 increasingly using not just encryption which you kind of get automatically for
1549:56 which you kind of get automatically for most Technologies today but increasingly
1549:58 most Technologies today but increasingly choosing technologies that offer
1550:00 choosing technologies that offer stronger guarantees that keep those
1550:02 stronger guarantees that keep those middlemen those companies out of the way
1550:04 middlemen those companies out of the way if only so that you can trust with
1550:06 if only so that you can trust with higher probability that only party B
1550:08 higher probability that only party B knows what party a has said or sent now
1550:12 knows what party a has said or sent now this of course was a whirlwind tour
1550:13 this of course was a whirlwind tour there's so much more that you can do
1550:15 there's so much more that you can do online indeed this course cs50 can be
1550:17 online indeed this course cs50 can be taken for free online via platforms like
1550:19 taken for free online via platforms like edex at edx.org cs50 I thought it might
1550:23 edex at edx.org cs50 I thought it might be appropriate to end on this note if
1550:25 be appropriate to end on this note if anyone would like to conjecture before
1550:28 anyone would like to conjecture before we start playing music and adjourn for
1550:31 we start playing music and adjourn for lunch what our final message here
1550:34 lunch what our final message here is if we reverse the plus one and maybe
1550:37 is if we reverse the plus one and maybe start minus one here minus one here and
1550:40 start minus one here minus one here and indeed thank you so much for coming this
1550:43 indeed thank you so much for coming this was
1550:44 was [Applause]
1550:46 [Applause] [Music]
1551:09 [Music] cs50