-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextract_notes.py
63 lines (44 loc) · 65.4 KB
/
extract_notes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf-8 -*-
"""Copy of Mem-Prompt Example
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1Ttt6OL6KGE5QfX88Kc-6fTcAGZ7EkkUM
"""
api_key = "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"
!pip install openai
import openai
openai.api_key = api_key
"""# Running a simple prompt + code"""
## You can store secrets by clicking on the Python kernel
TITLE = 'Collections Part I'
LNUM = 3
chunks = '''hello everybody and welcome to this special online asynchronous edition of cs106b which I hope is the only one of the corner so thank you so much for Ford Island on in today well what I want to do today is talk about collections I wish her all the time is called containers they are sometimes also called adt's which stands for abstract data types all of these are synonymous with one another and we'll talk a little bit more about the names in just a little bit but the idea for today is to go over essentially two major Concepts the first one I want to talk about is parameter passing this is basically how you move information from one function into another in C plus plus it turns out there's some interesting nuances there in C plus plus it gives you more fine brain control over this than many programming languages do so want to spend a few minutes going over the details there because it's going to inform all the code for going to ride over the course of the quarter from there I would like to introduce container types talk about what these things are the good news is you've already seen these even if you don't know them by the name container types just because there's many different names for the same thing in particular I want to focus on something called the factor which is basically like a list or a sequence of things or an array of things in the C plus plus programming language you've already worked with sequences of things in your prior programming experience this is mostly going to be about warning the syntax of how to do it and C plus plus finally because we started talking about recursion on Friday and it's the concept that becomes a lot easier with some practice we're going to do a little bit of work Fusion working on the doctor type so lots of fun stuff for today with all that said let's Dive Right In And I want to begin by talking about parameter passing in the C plus plus programming language to do this what I want to do is actually invite you to play a little game but I have here is a piece of C plus plus code and the goal is to make a prediction about what that code is going to do you might notice that I have marked one of the lines right here with a Cod and that line Prince of value out to the screen what I want you to do is take about thirty seconds to read over this code and then make a guess of what you think this code is going to print so it a few points to a pop up a little box that looks like this one over here at those formulate a hypothesis and that's what I want you to do so she pause and take about 30 seconds Reno for the code make a guess of what you think it's going to print and then hold on to that gas do you need to submit anywhere no you don't need to submit it do you need to I have the right answer no the whole point is to commit to something based on your mental model of how things are and then we'll take a look at what the code actually does and talk about how everything works so get pause and I'll see you in 30 seconds okay if I pause you really did great now that you hit the pause button and you have tried this out let's see what this code actually does so fortunately it because this is code we can run it and I have that setup actually right over here so here is the exact code that you just took a look at and we can kind of take a look at how the code works so is our main function value which we said 237 we pass into this function called become wealthy and then we print it out and what has become wealthy do it takes its argument and it sent it to a billion wow that's a lot of zeros so what are some reasonable gases for what this program might print a billion is certainly reasonable gas 137 certainly reasonable gas let's see what it actually does so I'm going to hit the wrong button and it pops up with 137 so actually speaking be questioned what is this Burg I'm going to print what will the indicated Line Print the answer is 137 but the question I asked was not there because I wanted to know that numerical answer I could just run the code to see what's going on here the reason I wanted to ask this question was to figure out why it prints what it said to do that I'm going to jump back over the slides over here let's actually Trace through this code and see how it works so we begin our main function and as you've seen with the example that we did on Wednesday of this week in on Friday of this week I'm going to draw this out as a note card so this is called a stack frame you'll see this an assignment one but I was going to have a new card with a code on it and I'm going to keep track of where we are so we make this variable called value and we set of equal to 137 and then we call this on to named become wealthy and we pass and the number value now where that's going to do is call dysfunction call become wealthy and went as you have seen whatever you call a function you think of it as putting a new note card on top of the stack now one of the things we have talked about is every time you call a function that pump call get the fresh copy of every single variable that's in there and even if there's another variable with that name elsewhere in the program is separate so you can see right here that we have this variable called net worth and it was an issue lies to the same value but this variable had back in Maine but it is a separate variable with no direct connection to what's going on in May the reason that matters is it when we set network equal to a billion that says take this variable right here which is called net worth and change it to be a billion importantly this variable back here in Maine it's a completely separate variable it has no idea that there was another variable that was given its initial value and has no idea net worth thing even exists it has no idea of the network was changed the net effect of all of this and that when is becoming wealthy function returns and we go back to Maine well value sitting there it says I'm 137 I've always been 137 and that's what gets printed out so if you call a functioning you pass an integer into that what happens if you make a new variable to get a copy of what that original integer hold and the new function is to work with that copy however it feels like the original is never touched its never modified now that's what we have seen a UPS you this point but it turns out that's not the only way that we could do things so the default parameter passing mechanism depasquale using something that's called pass by value so what is this mean value means that if you call a function and you pass an argument into that function was happening is your telepathy plus plus plus plus this is the thing I want to send it to the function can you make a Xerox if this for me can you copy this for me and I'll say sure it will make a brand new copy of that and then take the copy send that off into the function what that means is that if the function that makes any changes to what it sees as its parameter what was going to happen is great it changes the parameter that one copy of the original is never modified has no idea that anything has changed but you might notice I said by default because that's the default rule there are some cases where you might want to say no I don't want to hand a copy of this number to this function I want to take this actual number this variable I want to hand them their evil off of this function that if you make changes to it there's changes actually stick and there is a way to do this you can if you want to change the way that a parameter is passed into a function by using something called pass by reference now what does pass by reference mean mean I'm calling a function and I want to send this thing into it so I don't make a copy of it would be the default I say take this actual thing and put it into the function and that means that any changes that are made to it inside that function are changing the original thing not a copy of it the way that you do this in C plus plus is very subtle as a single character change is one of those things that you can easily miss the first couple times you're doing C plus plus code but you have to train yourself to look for it because it's significant the way that you do it is that instead of saying I'm going to take an integer as a parameter you've taken integer and you put a little a sand after it so I don't say into number I say a sand number the way I would read this out loud is not into a sand number I read that as integer by reference called number so that little change means that when the parameters pass into the function instead of being copied the actual honest-to-goodness thing is passed into the function to show you what that looks like let me switch us back to the code that we just took a look at the second ago so here we are we're going to do is make a one character change with this program instead of saying become wealthy and it's going to take an integer call net worth I'm going to put an ampersand here integer by a reference as this happens during Q Creator you type in an ampersand and then it just like gives you all the suggestions like I think that's what you meant in the change I'm making is just put in the Ampersand here that's the only difference let me run this program to show you what this does now so I hit the wrong button is going to have to compile it cuz I changed the code and now it's printing a billion rather than printing 137 okay A Single Character change is making a bit of a difference back to pretty big difference we should try to understand not just that it did something different but what did it do why are you saying something different with that little chain it goes back to what I was talking about in the slide the second ago but it's worth actually drawing this out by hand to see what's going on so we start up the same way we're in Maine if we get this variable called value would be such a hundred 37th and we call this function become wealthy
passing in value but the difference this time is because we have changed become wealthy to take an ant reference as a parameter instead of saying takes a number 137 make a copy of that and then hand that off to this other thing what it's going to do is something that looks a little bit like this so we have this new no card as we normally do but no net worth instead of saying oh not worth your little box on your own net worth says I'm actually this thing over here so whenever you talk about net worth you're really talking about this variable name to Value back in me so instead of there being two different things there is now only one think of this instead of saying hey here's a number 137 and you a copy it's let me hand you the box with 137 in it please don't do anything bad to it at this point we start executing inside of become wealthy so we go to net worth set it equal to a billion and what's net worth well net worth really is the value so we change it and now it's a billion and now on this function returns were back in Maine and suddenly you're seeing this change I want this is to say that this Single Character change really makes a world of difference to what's going on if you have a function that takes in an end what you're saying is give me a copy of the number that you are working with so I don't want the master copy of that number just give me a Xerox of it and it turns out that more times than not that's actually what you want to do so I don't want to be changing things I just want to copy but every now and then in the situation may be like this one where you really do want to change something you want to say I want to permanently modify this value to make it something different then put an ampersand here to say I really do want the ability to change this how do you know which one to use it's my contacts if you're saying hey function just FYI this thing exists usually take it in by value if you're saying hey function I want you to make some changes to this I want you to modify this have it taken the parameter by reference okay so that's the basics of values and references normally when you pass something in it is called pass by value you get a copy of it so network will be a copy of the number however if you put in an ampersand it changes it to buy reference and now you're not saying give me a copy of the number you're saying give me the number itself so that I can make some actual changes to it this is one example of pass by value pass by reference I would like to show you another one I'm going to have a little bit more fun here so I once again have a sample piece of C plus plus code that I'd like you to take a look at an ass before I'm going to pop up that little box formulate a hypothesis I want you to think about what this program is going to print at this line right here and right now I'm not going to tell you what the program does or how to think about it I want you to read it over for yourself and make a guess now we get when you make a guess the whole point is it to commit to something to say I think it's probably this and if you're right you know what there's no steaks if you're good you're right and if you're wrong you know what there's no steaks are on okay we'll talk about why the important thing to do is to commit to something so you can better understand it so it's a pause button right now okay you hit pause and then you hit unpause thought about it for about 30 seconds you have your hypothesis Okay cool so what does this program print well we see that we're starting with the string theory was initially wiggle we're going to call this function called golly gee and then print out whatever that string is and inside of golly gee we see we get a string which is called text and we are changing the first character to it she so either going to see wiggle or we're going to see giggle it's going to be one of those two of the question is which one well let's see which one it is but first let me give you one extra piece of information there is a big difference between how C plus plus three strings and how basically every other language does so in Python in Java and JavaScript when you have a string of variable the string itself or the spring variable itself is not the strength of variables says I'm not the strength you should look over there because that's where the actual string is sewing python in Java and JavaScript a fever variable called my string then and it has about you wiggle you really draw a picture like this so there's the variable my string it's got a pointer over to the string wiggle however in C plus plus if you have a variable of type string you think of it is it really truly is the string itself so variable of tie string is an actual honest-to-goodness string is not saying oh the real string is over there and you should follow this Arrow it says no I am really the string now why is that significant what is the purpose of me pointing this out to you it has to do with how this code is going to be interpreted Dakota for you one more time so we start off in Maine by making a variable called message which we set equal to wiggle and then we call golly gee I messaged that we printed out when you see string message equals wiggle you got to think that's an actual Spring right there and this golly gee function well it takes it a string what does that mean well let's see what happens and let's talk about why so it's either going to be wiggle or it's going to be giggle we run it and it prints out which is different than our most languages would treated so why is it wiggle and not giggle for that let's go back to the slide we start off as we do in Maine and we be getting here by making a strain called message which is equal to wiggle and again notice that the message variable really is the string wiggle it's not saying oh you know look over here the other string someplace else it really is just the string wiggle so we call this function. We be passing in message now what happens is this this Gully G-funk index in a string it doesn't say give me a reference to a string it says give me a strength and so C plus plus says okay you're taking in a string so what I'm going to do is I'm going to take the existing string I'm going to Xerox it and now you have two copies of the string you can see one of them is back here in Maine and you can see the other one of them is here and golly gee other two separate strings hear that means that when we say text 200 the G well that's going to take this string right here and permanently change it by changing the first character to achieve okay but know what is the original spring back in made it was never modified because it has no idea this other string exist so easy rock that we took this other copy we did whatever we wanted to the to do to that copy the original was never modified so when the function golly gee returns were back in Maine and now here we have the string wiggle unmodified because we never said to change anything and that's why we see wiggle and again why was it make you a copy it's making a copy because that's the default behavior in C plus plus we go back to the code over here when you say taken a string called text by default cpass bus will make a copy of the string you pass in and now text is a copy of it that's just the default rule this was an integer it would make a copy of the integer if this was a double it would make a copy of the double because it's a string it makes a copy of the string what that means though is the same way that we're able to change our previous code we could do the same thing over here so previously would we do we added a little a stand right here we said don't take in a copy of the string I want the actual string itself so don't Xerox wiggle give me the Xerox it won't let me change it take the original string wiggle and really put it into the function golly gee so that it can make some modifications to it if we put this a hand here let's hit the wrong button and let's see what we see now and now we're seeing giggle it's actually changing the strength again to see why that is let's go back to the slides and it kind of Trace through the code methodically one step at a time which is really the best way to understand this kind is this work well we start off here in Maine and we make a strain called message which is equal to wiggle what does that mean it means is going to look like this so instead of saying happy message to make a copy of spring Google hand me the string wiggle it says Hey golly gee whenever you see texts I'm really talking about this string over here I want you to make any changes to text to this thing over here there's no different texts is just an alias for message it's literally the same thing just two different names it is the morning star in the evening star is Gandalf the Grey and Gandalf the White it's time call Loretta Lynn born Baltimore different names for the same thing over here so when we now see a text at position zeros equal 2 GB that says go to this thing over here this is taxed mean says text is right over here go to the stream and change the first character to a cheap okay great we did that golly gee returns and that's why we're going to see what we see so to recap the default is to make a copy you pass a string into a function what that means is Xerox the string and the copy over you has an integer into a function Xerox the integer and the end of her into the function you can override this by putting an ampersand after the name of the type so if it's an ant Ampersand that means Bill copy the integer give me the actual integer to work with it was a string reference to string a and don't copy this train give me the actual strength and that pattern apply for basically every type in C plus plus why is it significant first from a practical perspective often times you're going to want to do something like this to take my string and modify it so you'll learn a stand over there but there's
another detail that he's a little bit significance I want to touch on it now now one of the themes that we're going to talk about this class is efficiency making code watches that works but that works quickly generally speaking I'm going to defer discussions of efficiency until later in the quarter so we're generally speaking not going to talk about how to make things really fast right now that's going to be a theme for once we got a little bit more practice and general comfort with C plus plus however there is one aspect of efficiency that does come up a lot early on and so I want to call attend what's imagine you have a bunch of the takes in a string not a string by reference but a string that means that whatever you call that function passing in the spring it will duplicate the string that means it has to actually make a copy of the string and strings can be pretty long so imagine that you have the text of a speech may be a speech given by a famous person and you passed that function by value meaning not by reference to take some a string on a string reference what that means when you call the function it has to go and duplicate the string now the computer can do a couple billion operations per second slow for a small string like a very call Lisa text you're not going to notice this but if you're working with a bigger strength they like the human genome 3 billion characters long are you going to feel about your program is really going to slow down if you taken a strong value so a general principle that comes up in C plus plus is it if you're passing a string into a function you usually want to use pass by reference rather than pass by value and where do you pass by reference to say it is faster for you to just take the original string and hand it to me than it is to Xerox a very long string and hand me the copy so commonly going forward when working with the taken strings will take in the strings by reference with an ampersand even though we don't intend to change things okay so that's just a nice little efficiency win but there's a slight complication that arises if you do that so imagine that you have spent Your Blood Sweat and Tears running the Next Great American novel you have backpack around the country you have brought your typewriter with you have typed it up and you have a single string variable it is the variable called my masterpiece and it's stored in a string and that is the next Great American novel you are going to win so many awards and you publish this now imagine that there is a pervert or so a function in your program and it looks like this it's called totally not sketchy by the way anything that says it is totally not sketchy is sketchy but it's called totally not sketchy and it takes in a string by reference would you make this function call that you should be thinking okay this string is really freaking important to you you have worked so hard to get that strength now dysfunction says that it takes on a string by reference what that means is it if you call the function you are taking your manuscript and you are handing it over to that function which can do whatever it wants to do with your strength now why is the function taken in by reference as we've said the general principle is it is a function takes in a string it should take it by reference because that avoid making the copy it slow if you have to copy something every time you pass it into a function on the other hand the function might take in a string by reference cuz it really does want to change it and if you have your one master copy of this thing like this is the one copy of your Masterpiece are you going to hand that over to some stranger who is like oh yeah I'm totally not going to do anything sketchy with this I mean you wouldn't do that with a physical manuscript of the like the one physical copy of your manuscript of an actual book why would you do that with a digital version so the problem is that when you see something to take something and by reference if you just see it like this it could mean one of two different things one day I'm not actually going to make any changes to your string I just don't want to pay for this expensive cost of copying a string when I'm just doing some very quick checks on it or two I actually do want to modify your string and so I really do need the app that a dance of the changes I made stick and those are two separate things is it look but don't touch or is it I really am going to modify this so turns out that he was actually has a third way of passing data from one function into another this is something called passed by Congress reference okay what is this all about so what you can do is tell a function the following you can say hey function when you get a parameter I don't want you to make a copy of the copying things is slow I don't want you to copy the entire Human Genome just to look at the first five characters of it or something like that and so I'm going to give it to you by reference and see what one says sure take it by reference so I can look at it and I can make modifications but then you say actually I don't want you to modify it you can look at it but you cannot make any changes to it and then what you do is use the special key word called cons cons for constant contact with a sense of its not going to check and the idea is going to cost references way of saying you can look at something but you can't touch and the way that you might write something like this is by writing a function that looks like this so let's imagine the function for the goal is to proofread in essays you want to look at the essay and see if there are any mistakes in it but you're not actually changing the S8 you're not going to modify the SIU was going to flag things in it so you can make it look like this it's the name of the function of proof read long-ass a okay that's fine notice that it takes in a string okay the Ampersand here means I am not sorry means I'm not going to copy this I'm going to hand you the original string but the contest means this function is not permitted to make any changes to this variable over here to get the little bit long-winded to take on string by reference but that actually is a free common thing to see in the C plus plus programming language to say it's a string I'm giving you the original strings you don't like copy and PS I am not going to change it and it turns out that she was possible actually enforce this for you if you look at the string that's fine but if you try making changes to it it will report an error and it won't actually let you run the program that let me show that to you so here we're back on our golly gee which actually does make a change to the text if I try to take this in by constant reference let me go and hit the build button real quick what you'll see is that we're going to get an error on this fine and particular it says assignment of read-only location yada yada yada what that saying is look you said that you want to look at the string but not touch it so this cost here means you are prohibited from making any changes to this string and hear the function tries to make a change the string the co-pilot rejected it says you can't run this program if you said you're not going to change the string then you shouldn't be able to change the string and it will not let you write something like this on the other hand if I wanted to say print out what the very first character of the string was and I tried to build this now that's perfectly fine because you can look at the first character of the string without changing it they want to print out how long the string is let's print out text. Length sure that would be perfectly fine you just not modifying it you're looking at it so what ended up happening is it the general convention in C plus plus when it comes to string is you rarely are going to see a function that just takes in a string by itself more commonly you'll either see you taken a string by reference if you want to modify the strings so hey give me the string and I'm actually going to change it or you're going to see if I cost reference please hand me the string so I don't make the copy but I promise you I'm not going to change anything and then that is perfectly fine it's a little weird when you see this other programming languages don't have this kind of functionality it can feel a little bit for both of your typing at the same thing over and over again you'll see a lot of cons in a lot of a sand in here but it turns out to generally be a really good thing to do is actually by having this kind of flexibility you can customize how your functions work and often times having that extra degree of control while it's a little bit more for both so many errors for you if you promise not to modify something and you tried changing it it's great that instead of having a weird thing happen when you run the program it is morning unless you're running it will say yeah either they shouldn't be asking you do you want to change it or you have a bug here because you tried to change the thing it was a constant either way when when so what this built into is this General flowchart you can you determine how you can pass values from one function into another so here's how you do this you want to function and you need to take a nap parameter what do you do to start the star place and the first question you ask is well the thing is being Pastor to that function do you need to make any changes to it maybe you do maybe you don't well if you actually want to change something that's passed into someone hands you something in your permanently going to change it you pass by reference that's exactly what it's designed for hand me the original so that I can modify it on the other hand imagine that you don't want to make any changes that you know what I actually do not wish to change that so I'm not going to change your string I'm not going to change your integer well then
we're going to say it's okay great that's okay then was the second question what kind of thing are you passing in if you're passing in a number so integer or a bull or double or a chore one of these things is kind of a lowercase and purple those things are very quick to copy it's very quick just copying a very quick to copy a Boolean very quick to copy a character so just use pass by value that sort of the default way to do things on the other hand if what you're passing it has a larger object State a string which is a lot of texts we haven't talked about other containers yet but we will little bit this week so I was actor is like a sequence of things are dictionary theater there's just lots and lots of stuff that's where you want to use this constant reference so this flowchart is the conventions throughout the C plus plus programming language and what I would say is over the course of quarters super familiar it is a little bit weird that you have to think about this kind of stuff right now but it turns out in the long run it's just become second nature is like the first time is it what the second timer like okay I guess that makes sense fourth fifth sixth time you just don't bat an eye at this but I will say this is a departure from other programming language is Java doesn't have this python doesn't have this job this group doesn't have this and so just the first time you see it like it's okay to be a little weirded out by this I'm going to pull up this flowchart a couple more times over the course of this week just to help reinforce everything but I would think that being the main thing to remember is the following if you don't want to change anything then either pass by value or pass by conference if you do you want to change something that's the reference and if you're not sure what you want to do sometimes it's good to actually positive it and think about what the function is going to do before you start coding about okay enough about parameter passing for now what I want to do is move on to the next major theme for today which are container ties so what is a container type okay there's three different names of the same thing so sometimes these are called collections sometimes these are called abstract data type sometimes these are called containers basically these are things like in Python like a list or a dictionary or a set if you're coming out of java this is your arraylist are your hashmap things like this basically these are types of things that hold lots of other pieces so I have a list of numbers is one thing a list but it contains a collection of numbers or maybe I have a dictionary I have keys and values for those who point out of java you would call this a map and pythons call dictionary but you have keys and values I have one thing which is the dictionary which is the map it's got that sort of Association it turns out that a huge amount of this general purpose programming is all about harnessing these types of containers and getting them to do the right thing understanding which container to select how to use it effectively and then building in Prosser program out of the smaller parts so we're going to do over the course of the rest of this lecture and then the next two lectures this week is talk about the most commonly used containers that you're going to encounter in the course of programming and show you some strengths and weaknesses the good places to use them effective situations where they come up and then later on we'll talk about the different efficiency trade-offs between then we'll talk about how they work internally for now we're just going to save these things exist you can make good use of them and we'll just kind of get our heads around how they work and then only want that's really rock solid will be come back and say okay let's talk about how fast they work off the containers there are two you're going to use more than anything else and the first one which is probably by far the most widely used container in C plus plus is this thing called a vector self a vector is the c-plus plus equivalent of say Jabez arraylist or JavaScript has a raise or python has list every programming language has some notion of a sequence of things we have nothing at the beginning then the next thing that the next thing the next thing the next thing at cetera and C plus plus it's called a factor why is it called a vector it's kind of historical accident I think it's C plus plus were to be redesigned from the ground up today we would not call this doctor but that name has stuck it was invented I think of the 1990s and so we still use it today so in Python you're used to the idea that you can make a list of things and C plus plus what you do this is well I think about C plus plus it's a little bit different than python is typing and C plus plus everything has a type so in 500 to say I have a list of things and see plus you have to say I have a list of say integers or maybe I have a list of strings or a list of real numbers or something like that so the syntax that you used to create a vector is you put the word factor and then in angle brackets you put what kind of thing you're going to store so say a vector bracket and would be a list of numbers back to being a list and being an integer or do you want a list of strengths that would be a vector bracket of strength where everything in the vector is a strength every time I teach a 1060 when people are coming out of python a very, what if you want to mix and match maybe you want some of them to be strings and some of them to be numbers it turns out it's actually not that common to do that it's exactly surprising you think that would be a common thing you'd want to do you think I'm getting up so much for everything for python 2 C plus plus turn that it's actually pretty uncommon in practice to do something like that and then there are specialized techniques you can use in that case if you're curious what those look like feel free to stop by my office hours post a question on Ed's damn something like that happen to talk with that looks like turns out it's just pretty uncommon to encounter it so more commonly used am working with a list of numbers midday to file picture of double-decker of Endor I want the lines of a song I want the first line of the song than the second and the third sector of string or something like that now I'm assuming that because you're coming out of either python or Java or JavaScript Hershey's C sharp are swifter basically every programming language of a list of things rather than by making reference to some other things we already understand so right here I have a piece of C plus plus code using a vector and here I have equivalent code written in three different programming languages what I want you to do is take about 30 seconds and I want you to pause the video and see if you can figure out what the C plus plus code does by looking over at these other programming language is finding one that you're familiar with anything because this one would do this that means this one would do this Etc and then after you've done that hit on pause and you can continue so you should pause right now
so I'm assuming that you actually pause the video and at this point that you have on pause because you would actually work through this great based on that let's take a look at this together so the very first thing we do is we make a vector of integers if he and set it equal to 1, 3, 7 so what is this do this says I want to list of things and specifically a list of integers the vector bracket end means this is a vector which is a list of integers the name of my variable is V and what am I saying it equal to sew in C plus plus when you're creating a container and you want to say holding the following things you use curly braces which are then, separated with the elements that you want now if you're coming out of python you're probably used to using square brackets for a list of things in curly braces for something else it turns out that in C plus plus curly braces are kind of the default for everything you can't use Square Rocketeer you do you square brackets to get some really scary messages rather I was going to say yeah I don't know what you mean by that what is Curly braces but the good news is you're getting pretty costumes not because you're starting to write in C plus plus at this point the plus equals 271 what is this do the plus equals operator is equivalent to a pending something so I say V plus two equals 271 that means take the list and then tacked to 71 on to the end so the list at this point will consist of one then 3 then 7 cuz that was it was already there and then followed up by 271 because that's what we sent to do now is the plus equals operator sometimes it does that mean like add this number to everything in the list no + equals is packed this on to the end of pens the following things to the vector take the vector and one new thing to it and it specifically goes at the end hey what do you want to answer something in the middle of the doctor turns out again not as common as you might think there is send text to do this I'll talk about how to look up some other things you can do on doctors in a little bit but it turns out most of the time we talked
how do you access individual elements out of a vector to access individual elements out of a vector we use square brackets that I say V square bracket zero that means select items 0 out of the doctor and the items in the vector are zero indexed which means that the very first thing in the doctor is in position 01 next thing is that position one the thing after that is a position to know if you are coming from you know you're adorable we'd like to call you know countries other than the United States most buildings are set up where the ground floor is 40 the next one's first floor or second or third floor that's kind of the model you want to use over here and the way it's like something out of square brackets the same syntax be used for a string cuz like one thing out of it now in python as I mentioned on Wednesday when we were talking about string in Python if you use square brackets with the negative number that means set up beginning at the beginning and Counting for that many steps that means go to the back and count backward that many steps C plus plus doesn't do that if you want to pick out the last thing in a vector you can't say V bracket minus one that will crash your program I wonder what you can do is do a little bit of arithmetic you can say how many elements are there in the vector okay then that would because it were starting to kind of zero that will be at the position one pass the very end of the vector so back off by 1 position and I side effect of showing you this is it if you need to determine how many things are in a vector of the syntax is B. Size that will tell you how many items are in the doctor okay sometimes you want to get Subway so you might say I have a list of a large number of elements I want to grab the first five of them the first 20 of them I don't want to grab from this position to this other position over here so you can do this using the sub list function so if you say V. Sub list from 0 to 2 that will mean give me a list of the Swarm by starting a position 0 and grabbing shoe elements that would give you a vector containing these two elements over here like the string if you say. Subway's and you don't spell buy an end point it will start at that position and just run all the way to the end so this will give you everything at the decision to or later finally how do you take things out of the doctor there's a function called remove you give it the index at which you'd like to remove something not the thing you want to remove device AV. Remove zero that does not mean take the number zero out of the vector that means remove the item that is at position zero and everything else is going to get shifted back one position it's a kind of the equivalent to the Dell operator for coming out of python okay to all of these are kind of standard seek with operations that you're probably familiar with Mother language is this is just changing up the syntax a little bit because it's a different language here you know we have moved from Spain to Italy it is a romance language but it's not exactly the same one okay I want to show you one other set of things that you can do on vectors so here is another piece of code on factors and again here are the equivalent versions in different programming languages so what I want you to do this again pause the video think about 30 seconds just think over what does this code do what do these different pieces mean in again Reason by analogy look at the other languages and see if you can stay up because in the language I know we see how to do this here this is what this is going to do in C plus plus to pause a video right now okay hopefully at this point you have unpause the video after having spent about 30 seconds figuring out what this code does you will learn a lot more if you do that if you didn't file chance okay okay cool let's take it one step at a time what exactly does this do now notice here we have now a vector of strengths that angle brackets dream means this is a list of strings not a list of number so what difference the main focus is what we have here is how to loom over all of the items in that list it's a very common thing to do if you don't want to take the entire lives to do something you want to go one element at a time and look at things or maybe you want to search for an element or print all of the elements there's two different ways you can do this and C plus plus the first one is what we call accounting for League do in Python one thing you can do is if you can say like you know for I and range when V and that would be in a guy count from zero up to the length of the vector kind of going one position at a time this is the c-plus plus way of doing this the variable I is now essentially an index in to the doctor and so will First Take on the volume zero than one than two and three than four... And it will stop right after it goes to the last index in the doctor so you can live this way by saying 4 and I go 0 is less than b. Saisai + + and then pull out individual Vector elements you want using the syntax of the bracket I now that is one way to work with a doctor and it's very useful if you need to know the indices it with your operating however there's another way you can leave a vector sometimes what you want to do is not countin say I want to take a position zero than one then to I just want to say for every ailment of the vector go and do something with it and for this you can use with Cole range baseball leave I believe the actual technically with this is enhanced for loop I think it's a very very cool name I like calling Ranch basically that's what everyone does as you can say for every element in the doctor do something with that element so what I do in my for a loop is I stay for string alembic whole Envy every time we go through this Loop there's going to be a variable name to LM which will be a string why is it going to be a string is it back to his drinks and then and each iteration the loop you can do whatever you would like with that element so you can think of this is print out every element of the vector it says instead of saying print out element zero than element one than hell and she would print out the first thing the second thing is third thing Etc now there are more operations you can perform on factors than what I have shown you here that's generally the case with the container types of Moana stuff you can do how can you learn more about this if you go to the Poors website you'll find a link to what we call the Stanford cplusplus Library documentation this is essentially a website that lists all the different things that you can do with these different types that has examples it shows you all the functions available as you're working for the programming assignments that's a really handy place to go and look it's a great resource you can use to learn more about what's available the Lincoln the spies will take you directly there you can also find it by navigating from the course website okay so what are we just talked about we talked about for a passing and then we talked about doctors so what should we do next Mulberry naturally let's talk about around her passing and the context of actors I have one last code prediction thing I want you to work through and once again here's a little sign that I told you I tell you what do you think this program is going to print so pause the video take 30 seconds make a guess and see what you think it's going to be
so you pause the video you pause the video you pause the video you pause the video great okay you paused you formulate a hypothesis let's go talk about what this codes going to do so just read it over from the top to the bottom we can see that we're starting off by making a vector of integers which is a list of numbers and contains these numbers 1929 1955 and in 1964 we call this function called dream and then we're going to print out the values what does the dream function do it says go to the numbers are Rey and position one since we start at position zero this is the second thing in the back and change it to 1963 and the very end we are going to print out that doctor so reasonable gases for what you could see would either be first using 1929 to 1955 to 1964 or that you see 1929 then 1963 and then 1964 it turns out he has one of those to the question is which well I want to repeat the same something I've already said before but it's really important to stress this cuz this is where C + B is different than other languages and this can catch you off-guard if you're not expecting it so remember if you're coming a python or Java or JavaScript when you have a variable representing a list of some sort than what your learned what you're trying to do what you feel you learn to do what you are trained to do what you have learned is that your variable is not the list you have the variable which says I'm not the list the actual list he wants over there and so the dryer G look something like this the variable is lobot and it points at a list of numbers but in C plus plus if you have a variable which is a vector that means I am an actual honest-to-goodness Vector I'm not saying look over there there's a victory over I actually am the director the same way if you a string variable it means I am actually a string and that means that all of these rules that we talked about about how to copy things and what I pass by value bass pass by reference mean all that's going to come back into play so let me switch back to the coat this is the code that we were just taking a look at over here let me run this for you and let's see what it does so I'm going to hit the wrong button it'll build it will run and it's going to print out 1929 then 1955 and then in 1964 which if you noticed are the original Three values in the array or in the vector here not what got changed from Lee called These Dreams option so again the question is not what's the answer the question is why is that the answer so let's go back to the slides and take a look at what's actually going on over here so we begin here in Maine and we're going to create our Vector of values and we can imagine it is looking like this we have this variable called values and it really does store all three of these numbers then we call the function dream now dream takes in a vector of integers which means because it's not a vector managers by reference at the factory managers that says 0 rocks that factor and hand me a copy of it so notice that we have the original Vector over here inside of the stream function we have our own Vector of integers and so when we say numbers of 1 is equal to 1963 that's going to take this number right here and change it at the original Vector back in Maine is completely unmodified that's when we return back over here and we print out the Specter of values we see the original over here but if we don't want that to happen if we want to change how this works which we very well might want to do we can instead of saying this is a vector of integers I want you to Xerox a factor of integers and give it to me I'm going to put an ampersand right here and again that a sand mean don't Xerox the vector give me the actual original Vector I want to do something with it and now if I run this program with that one character changed suddenly what gets printed out is this sequence of numbers over here the 1929 from the original the 1963 cuz we modified it and then the 1964 now again it's good to see that that's what gets printed let's look back at the colors that look like back in the slides here why does it do something different let's try it before we started man we get our Vector of values which looks like this and now we called dream and values because dream takes in a vector of integers by referee instead of numbers being a Xerox of the original list numbers is an alias for the original list is another name for a list that already happened to exist so when we say numbers one is equal to 1963 what this here is numbers so I guess we want to change that value over there and now it's 1963 so when the function returns we now see the change so the same rules that work for integers are the same rules that were four strains are the same rules that work for vectors by default things get copied when you pass them into functions if you want to override that you can buy taken in by reference or by conference that means that all the considerations we talked about what strings are still valid here so if you have an enormous Factory managers imagine how the doctor vinegars which is temperature readings from SFO airport every 15 seconds for 5 years huge number of numbers there if you have a vector event and you pass the Baton Vector event into a function and it takes a nap by value gets going to copy that entire list that will slow things down you'll actually see it in your program so the same way that was strings we don't generally strings into functions by value we do it either by reference or my conference same with a vector because it back there could be a very large number of things if you want to modify the contents of a vector take the vector and by reference if you have a sector and you want to look at it maybe do some calculations on it but not modify the vector pacify conference it is very unusual not unheard of but very unusual to take a vector of integers in by value Okay so we've come full circle on that we started with parameter passing we introduce two factors we've seen how all these all these work what are we going to do with the remaining time that we have here as promised I wanted to do one last example into a particular I would like to talk about recursion on the vector silver curtain and recursive problem solving is a major theme in this class and when you're working with vectors which are sequences of things there's a lot of very interesting and nice recursive inside so you can make that makes it pop Opus all problems that feel like they ought to require Lubes in a way that is purely recursive and example I want to give today as about finding the largest number so what do I mean by that what I want to do is write a function that the name of this function is going to be a Max of and the idea is that you give it a vector of integers and it will then find which of the numbers in that factory managers is the biggest now let's pause for a second notice that the way I'm passing the vector into the function is by constant reference what is that the reference mean Don't Rock the vector when you pass it into the funk and give me the original Vector not a copy of it but all I'm doing is finding the biggest thing in the doctor I'm not changing what's in the doctor be very weird if I said which of these is the biggest number and then you started Crossing numbers off changing into other numbers is that the biggest one is 7 cuz I made every number in the list of it the constant mean I'm not going to modify what is in this Factor okay so that's why it's a constant event by reference but I digress with that being said the idea is intuitively we're saying I'm going to take an effector I'm going to give you which of the things in here is the biggest one of them little coffee I will put in going forward if you have a vector with no numbers and if there isn't a largest thing in the doctor it's not even mathematically defined so we're going to assume the factor has at least one thing in it as we are working through this so the goal is now set we want to write a function binds the biggest thing in a vector and how exactly are we going to do it cuz I mentioned we're going to think about this one recursively so the idea behind thinking recursively is that you want to identify a base case something really simple something that is so simple that you can immediately read off the answer so what would a good base case here be well talk about Simplicity when you're working with doctors typically not always been typically based cases are going to correspond to a very very small vectors may be a factor with no things in it at all list of no numbers may be affected with one thing at a list of one number in our case when we're trying to find the maximum value in a list we're assuming that list has at least one thing in it so the smallest possible if you could ever deal with this list that has exactly one number in it so we can say well you know what our base case that's going to be if we have a list of just one thing if you have just one number if I say here's a number which number is the biggest you're saying is it that number right there like the only number you're holding up I say what's the biggest number out of 137
we're looking at something that's just a little bit more complicated than that something that's too complicated to just immediately read off the answer maybe we've got it back there with 15 numbers in or 100 numbers in 1000 number or going to try to do is make the problem smaller make it to the end instead of dealing with a very big list of numbers are working with a slightly smaller was because we cannot make a slightly smaller infinitely many times will eventually get so small that we can solve it directly with our base case so how are we going to do this let's Reason by analogy what did we do on Friday of the previous week one thing we did was adding up the duties of a number like the number 12 58 how do we do that we said well if you want to add a bowl that you can split that number into two smaller numbers a 1 digit number which is going to be our last ditch it and then everything else and then we also did this example of recursion of reversing the letters in a string how do we do that well we said there's actually a very nice way of putting us apart recursively you'll split into the first character and then everything else well the same basic idea here works when you have a list of numbers okay so if you have a list of numbers what can you do maybe you split off and shoot the first number in the list and then everything else and the idea is that we can do is say great we can just take this thing on itself and it will use workers in to figure out what we're supposed to do over here because the largest number in here is either this number or it's the biggest number in this group it's going to be one of those two how do you find the biggest thing in a list it's either the first number or the biggest thing in the rest of them and you just compare whichever those two are bigger in terms of actually writing code for this well we need to find a way to say how do you get this element right here how do you get just these numbers over here was take a look at that let's imagine that the name of our Vector is Alex well the very first element of the doctor is going to be a lens bracket zero and if I want to form a list of everything except for the first number. Stop list of one and if you look back to what we did on Friday and we were reversing a string recursively. Very very similar Square back in 20 to get the very first character and then substr in that case because it was a string of one to pick up everything after that with that being said we now actually have enough to write this function recursively so let me switch over to the code over here which looks like this I have a main function right here where we're going to create a vector I'm going to put in a bunch of numbers and they're going to print out the maxima and here I have a prototype for our Max of function that we have not yet written it so let's do that here I'm going to stay and mess up and it's going to take in a constant Vector integers by reference called in this case LX now we're proceeding recursively so we need to think about our base case as I said earlier bass case if the vector just has one element in it that element is the smallest smallest biggest element of the sector is also the smallest but we care about is the biggest so I'll say if a lens. Size is equal to one we're going to return elements of zero you only have one thing in the doctor that is the smallest
then otherwise we have our recursive case if the vector has more than one element what the factor into two parts the first elements and everything else the maximum is then either one the first element or to the biggest element in what remains okie dokie so let's see what that looks like I'm going to say in the first is equal to Ln of 0 and I'm going to stay in this case and rest is equal to a lamb's. Sub list of one again I am following the pattern that we've seen in the slide over here and how to break it apart so we've got our first element we have the remaining elements and then what do we do we're going to return whichever of the following two numbers is bigger either the first number or the maximum of the remaining elements this is Handy Dandy function built into cplusplus called Max to give it two numbers that returns whichever one is the biggest so it turns out that's all we need and you might live in and say there's no Loops there's there's no anything it's it's just functions that's the beauty of workers that you can do stuff like this let me go and run this and let's see if it works to the list of numbers that we have is 1 3 7 1 0 6 1 0 7 1 6 t610 3261 109 visit the biggest number so if we did everything correctly this should print out 261 so I'll hit run it's thinking antifreeze at 261 it works amazing but I have before it's not enough that it works the question is how does it work what is this code doing I want to do one quick code trace and then we're going to call today so let's see how this actually is put together so the idea is I'm going to work on a smaller doctor here just to 7 and 1 because if I put more things to know takes too long so we'll begin with this we make our vector and will print out the maximum thing so what's the maximum well we called the max of function which has a lens 27in what is there only one thing in the note OK Google what do we do give me the first number in the west which is to make me a factor of everything else that's seven and one and then return the bigger of the following two quantities the first Client First number once you look at is the first thing in the factor which is 2 and the second is the biggest number out of everything else which are these numbers over here so let's recursively find the maximum of seven and one how do we do that well we ask do we have just one number left no we don't so take the first number from the list that's a 7 look at the remaining numbers for Melissa of everything except the first that's just going to be the number one and then return whichever of the following is bigger either the first number in the doctor which is 7 or the biggest number out of everything that remains how do you find the biggest number out of everything that remains that's for corrosion we make another call to max out of passing in in this case the rest of the numbers which is so we asked OK Google find the biggest number in the list one well how many things are in here there's only one thing so the maximum value in this case must be in this case just the first element of the list which in this case happens to be the number one so we said what's the maximum of the number one is just one so this function call returns taking us up and we say what's bigger 7 or 17 and that's the answer we're going to return here what is the biggest out of seven and 1 it's 72 this recursive called returns and we're back so then we take us arms or back over here and we said great give me the biggest of 2 and 7 will that would be 7 which is indeed the biggest number in this list will returning the correct thing and this case 7 gets propagated up over here so thank you for bearing with us as we've gone through a lot of stuff today what's a summary of the major ideas first the idea of a vector just being a sequence of things you can make it back to events or vector of strings it's a list you've done listing things before second that passes by value if you say a function takes an ant it gets a copy of the ant if it takes in a string and gets a copy of the spring if it takes in a vector event it gets a copy of the doctor event unless you put in a standard which case you hand over the actual integer or the actual strength or the actual Vector event in some cases you want to say hey by the way I actually don't want to change this thing and which case you can use pass by conference is a way to do this and again something you seen before every time you make a function call to a recursive function to get a fresh copy of all the variables and that function so it's not like every time your recurring on something you're just kind of jumping back up higher in the code you really want to have that new model of note card stacking up that's why we're on this so many times
what are your action items for today reached a 5.1 and 5.2 of the textbook that talks about the vector type it also talks about grid we haven't talked about the grid but it's actually a lot easier to work with in the doctor and there's a beautiful example keep workout assignment one if you're following are suggested timetable your mind to complete the debugger warm-ups and make a good progress on the fire exercise by Monday try to start only connect on Monday if you can if you have started a late that's fine just Lisa buffer time for some recursion and finally look over this Max up option it's the most complicated recursive function that we've seen thus far and it a lot of really interesting little next to go into it you really learn a lot more about recursion if you start there when we come back in person on Wednesday we're going to talk about lexicons and stats and Matt which are wonderful. I did a structure that we can use the store words to store collections of things in the store Association these are work horses that were going to use so much over the course of the quarter that's all for today thank you so much for watching this take care stay stay stay healthy have a fantastic looking forward to seeing you on Wednesday by'''
notes = f'''- {TITLE}'''
print(f'#### CS 106B Lecture {LNUM}: {TITLE}\n- {TITLE}', end='')
for i, chunk in enumerate(chunks.strip().split('\n')):
chunk = chunk.strip()
prompt = f'''You are a perfectly articulate and knowledgable chatbot that turns lectures from Stanford's Computer Science 106B Programming Abstractions class into detailed notes for students to learn from. You are currently summarizing Lecture 1: Getting Started with C.
Here is the transcript of the for the {'first' if i == 0 else 'next'} chunk of the lecture:
{chunk}
Please convert this speech into notes. Speak in 3rd person, use bullet points only, and only include relevant information on the topic of this class: {TITLE}. Do not ever summarize the transcript given above or summarize your notes below.
{notes}'''
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.75,
max_tokens=256,
frequency_penalty=0.0,
presence_penalty=0.0
)
responseText = response.choices[0].text
notes += responseText
print(responseText, end='')