{"captions":[{"content":"[Walkthrough - Problem Set 2]","startTime":0,"duration":1940,"startOfParagraph":false},{"content":"[Zamyla Chan - Harvard University]","startTime":1940,"duration":2190,"startOfParagraph":false},{"content":"[This is CS50. CS50.TV]","startTime":5170,"duration":2320,"startOfParagraph":false},{"content":"All right. Hello, everyone, and welcome to Walkthrough 2.","startTime":7490,"duration":3260,"startOfParagraph":false},{"content":"First, I want to congratulate you for finishing pset 1.","startTime":10750,"duration":3580,"startOfParagraph":false},{"content":"I know that it could have been a bit tough for some of you,","startTime":14330,"duration":3810,"startOfParagraph":false},{"content":"could have been your first computer program that you wrote,","startTime":18140,"duration":2320,"startOfParagraph":false},{"content":"but just remember that at the end of this, when you look back at the end of the semester,","startTime":20460,"duration":4040,"startOfParagraph":false},{"content":"you'll look at pset 1 and you'll say, \"Hey, I could have done that in 5 minutes.\"","startTime":24500,"duration":5320,"startOfParagraph":false},{"content":"So know and trust that at the end of this you'll definitely find pset 1 quite simple.","startTime":29820,"duration":5880,"startOfParagraph":false},{"content":"But for now it's a huge accomplishment, and congratulations for getting done.","startTime":35700,"duration":4940,"startOfParagraph":false},{"content":"Now, also a quick note before we get into the meat of the walkthrough.","startTime":40640,"duration":3370,"startOfParagraph":false},{"content":"I just want to make a quick note that I sometimes won't have enough time","startTime":44010,"duration":4330,"startOfParagraph":false},{"content":"during the walkthroughs to go through every single way of doing the problem set","startTime":48340,"duration":4160,"startOfParagraph":false},{"content":"and rather just maybe focus on 1 or 2 kind of implementations, ","startTime":52500,"duration":3640,"startOfParagraph":false},{"content":"ways that you could do this.","startTime":56140,"duration":1610,"startOfParagraph":false},{"content":"But that isn't to say that you are forbidden from doing it another way.","startTime":57750,"duration":4220,"startOfParagraph":false},{"content":"There are often, as with computer science, numerous ways of doing things,","startTime":61970,"duration":4010,"startOfParagraph":false},{"content":"and so definitely feel free to use a different type of solution than I may have presented.","startTime":65980,"duration":6210,"startOfParagraph":false},{"content":"[pset 2: Crypto - Zamyla Chan - zamyla@cs50.net]","startTime":72190,"duration":2330,"startOfParagraph":false},{"content":"[pset2 - 0. A Section of Questions - 1. Caesar - 2. Vigenere]","startTime":74520,"duration":2640,"startOfParagraph":false},{"content":"All right. So problem set 2: Crypto is a fun one.","startTime":77160,"duration":3490,"startOfParagraph":false},{"content":"Again, with every pset you'll start with a section of questions","startTime":80650,"duration":3850,"startOfParagraph":false},{"content":"that's going to be conducted in your sections with your assigned teaching fellow.","startTime":84500,"duration":5100,"startOfParagraph":false},{"content":"We aren't going to go through these over the walkthrough,","startTime":89600,"duration":2070,"startOfParagraph":false},{"content":"but they definitely will help you complete the pset.","startTime":91670,"duration":3430,"startOfParagraph":false},{"content":"So the first part of the problem set is Caesar.","startTime":95100,"duration":3000,"startOfParagraph":false},{"content":"And so in Caesar someone will pass you a key with an integer,","startTime":98100,"duration":5370,"startOfParagraph":false},{"content":"and you will encrypt a string of text that they provide you","startTime":103470,"duration":4950,"startOfParagraph":false},{"content":"and give them back an encrypted thing.","startTime":108420,"duration":2250,"startOfParagraph":false},{"content":"If anyone watched A Christmas Story, there's an example of that there.","startTime":110670,"duration":5380,"startOfParagraph":false},{"content":"Then the second part of the problem set is Vigenere,","startTime":116050,"duration":3040,"startOfParagraph":false},{"content":"which is a more advanced encryption technique.","startTime":119090,"duration":2700,"startOfParagraph":false},{"content":"And so we're going to encipher a piece of text,","startTime":121790,"duration":3850,"startOfParagraph":false},{"content":"except instead with just a single integer, we're actually going to encode it","startTime":125640,"duration":3960,"startOfParagraph":false},{"content":"with a keyword that the user will provide us.","startTime":129600,"duration":3740,"startOfParagraph":false},{"content":"Okay, so the first tool in the toolbox today is actually going to be updating the appliance.","startTime":136270,"duration":5820,"startOfParagraph":false},{"content":"On the discussion board we would see things like, \"Why doesn't this work?\"","startTime":142090,"duration":4340,"startOfParagraph":false},{"content":"\"Why doesn't Submit 50 work?\" ","startTime":146430,"duration":1680,"startOfParagraph":false},{"content":"and often the solution is actually just to update your appliance.","startTime":148110,"duration":3720,"startOfParagraph":false},{"content":"And so if you just run in a terminal window in your appliance sudo yum -y--","startTime":151830,"duration":4900,"startOfParagraph":false},{"content":"that's a flag saying yes, update everything--update,","startTime":156730,"duration":3310,"startOfParagraph":false},{"content":"then your appliance will update if need be.","startTime":160040,"duration":2240,"startOfParagraph":false},{"content":"And it doesn't hurt if you already are at the most recent version of the appliance.","startTime":162280,"duration":4680,"startOfParagraph":false},{"content":"Then it will just say no new updates available and you can continue working along.","startTime":166960,"duration":4320,"startOfParagraph":false},{"content":"But this is good to execute even every time that you open the appliance","startTime":171280,"duration":4520,"startOfParagraph":false},{"content":"because we're still very much--","startTime":175800,"duration":1340,"startOfParagraph":false},{"content":"sometimes if we come into a bug--fixing it in the appliance.","startTime":177140,"duration":3180,"startOfParagraph":false},{"content":"So make sure that you have the most recent version of the appliance","startTime":180320,"duration":2860,"startOfParagraph":false},{"content":"and run that update there.","startTime":183180,"duration":4530,"startOfParagraph":false},{"content":"All right. So since we're dealing with letters and changing, enciphering things,","startTime":187710,"duration":6650,"startOfParagraph":false},{"content":"we're going to really want to become best friends with our ASCII chart.","startTime":194360,"duration":6050,"startOfParagraph":false},{"content":"There are numerous ones online, if you find. Maybe even make your own.","startTime":200410,"duration":3940,"startOfParagraph":false},{"content":"Basically, with every letter and every number and every character","startTime":204350,"duration":5600,"startOfParagraph":false},{"content":"there is a number associated with them,","startTime":209950,"duration":2260,"startOfParagraph":false},{"content":"and so it's good to see their ASCII values alongside the actual letter.","startTime":212210,"duration":6460,"startOfParagraph":false},{"content":"That will definitely help you in the problem set.","startTime":218670,"duration":3640,"startOfParagraph":false},{"content":"One thing that really helped me in this problem set was to actually print it out,","startTime":222310,"duration":3440,"startOfParagraph":false},{"content":"and as I was going through, I would actually draw on it,","startTime":225750,"duration":2630,"startOfParagraph":false},{"content":"write, \"If this has to go to there, then...\"","startTime":228380,"duration":2770,"startOfParagraph":false},{"content":"Kind of draw on it and mark it up, become best friends with your ASCII table.","startTime":231150,"duration":4120,"startOfParagraph":false},{"content":"Then we have a few other tools at our disposal.","startTime":237240,"duration":3510,"startOfParagraph":false},{"content":"This time instead of actually prompting the user for all of their input","startTime":240750,"duration":3000,"startOfParagraph":false},{"content":"we're going to do a combination.","startTime":243750,"duration":1480,"startOfParagraph":false},{"content":"We're going to prompt them for some input, ","startTime":245230,"duration":1650,"startOfParagraph":false},{"content":"but we're also going to just use the command line arguments.","startTime":246880,"duration":4470,"startOfParagraph":false},{"content":"So when they run their program, usually you say ./hello, for instance,","startTime":251350,"duration":4250,"startOfParagraph":false},{"content":"if your program was hello.c.","startTime":255600,"duration":1710,"startOfParagraph":false},{"content":"But this time instead of just saying that, they can put words, arguments afterwards.","startTime":257310,"duration":5190,"startOfParagraph":false},{"content":"And so we're going to use whatever they pass in to us as their input as well,","startTime":262500,"duration":4710,"startOfParagraph":false},{"content":"so moving beyond just prompting for integer but also using command line arguments.","startTime":267210,"duration":4510,"startOfParagraph":false},{"content":"And then we'll go into arrays and strings, which we'll be using a lot as well.","startTime":271720,"duration":4870,"startOfParagraph":false},{"content":"Here's just an example of 1 mini ASCII chart.","startTime":281460,"duration":3350,"startOfParagraph":false},{"content":"As I said, every letter corresponds to a number,","startTime":284810,"duration":3650,"startOfParagraph":false},{"content":"and so familiarize yourself with that. It will come in handy.","startTime":288460,"duration":4050,"startOfParagraph":false},{"content":"And later when we start doing some ASCIIMath dealing with the numbers--","startTime":292510,"duration":3100,"startOfParagraph":false},{"content":"adding, subtracting them--then definitely good to refer to this chart.","startTime":295610,"duration":4500,"startOfParagraph":false},{"content":"So here's an example of a Caesar cipher--something that you may have played with.","startTime":302860,"duration":4060,"startOfParagraph":false},{"content":"It is just a wheel. Essentially, there is an outer alphabet and then there is an inner alphabet.","startTime":306920,"duration":4270,"startOfParagraph":false},{"content":"So right here is an example of the Caesar cipher but with a key of 0.","startTime":311190,"duration":4100,"startOfParagraph":false},{"content":"Essentially, A is aligned with A, B is aligned with B, all the way up to Z.","startTime":315290,"duration":6250,"startOfParagraph":false},{"content":"But then say we wanted a key of 3, for instance.","startTime":321540,"duration":5050,"startOfParagraph":false},{"content":"Then we would rotate the inner wheel so that A now aligns with D, etc.","startTime":326590,"duration":6690,"startOfParagraph":false},{"content":"And so this is essentially what we're going to do.","startTime":333280,"duration":1970,"startOfParagraph":false},{"content":"We don't have a wheel, but what we're going to do is make our program","startTime":335250,"duration":3090,"startOfParagraph":false},{"content":"kind of shift the alphabet along with us a certain amount of numbers.","startTime":338340,"duration":6150,"startOfParagraph":false},{"content":"So as I said before, we're going to be dealing with command line arguments","startTime":344490,"duration":4160,"startOfParagraph":false},{"content":"as well as getting an integer.","startTime":348650,"duration":1740,"startOfParagraph":false},{"content":"So the way that a user will run your Caesar program is by saying ./caesar","startTime":350390,"duration":4660,"startOfParagraph":false},{"content":"and then entering a number after that.","startTime":355050,"duration":3040,"startOfParagraph":false},{"content":"And that number represents the key, the shift,","startTime":358090,"duration":3040,"startOfParagraph":false},{"content":"how many times you're going to be rotating the inner wheel of your Caesar cipher.","startTime":361130,"duration":5610,"startOfParagraph":false},{"content":"And so you see here an example.","startTime":366740,"duration":1650,"startOfParagraph":false},{"content":"If we entered the letters from A to L in our Caesar cipher,","startTime":368390,"duration":6160,"startOfParagraph":false},{"content":"then it would input D through O because that's every letter shifted over 3 times,","startTime":374550,"duration":4970,"startOfParagraph":false},{"content":"just like the example of the wheel that I showed you.","startTime":379520,"duration":2560,"startOfParagraph":false},{"content":"So then if you entered, for instance, This is CS50!","startTime":382080,"duration":3220,"startOfParagraph":false},{"content":"then it would also move all of the letters.","startTime":385300,"duration":2660,"startOfParagraph":false},{"content":"And that's an important thing in both Caesar and Vigenere","startTime":387960,"duration":3080,"startOfParagraph":false},{"content":"is that we're going to skip over any non-letters.","startTime":391040,"duration":3850,"startOfParagraph":false},{"content":"So any spaces, characters, etc, numbers, we're going to keep them the same.","startTime":394890,"duration":4270,"startOfParagraph":false},{"content":"We're only going to shift the letters in this case.","startTime":399160,"duration":3760,"startOfParagraph":false},{"content":"So as you see in the wheel, we only have the letters available to us,","startTime":402920,"duration":2950,"startOfParagraph":false},{"content":"so we only want to shift the letters and encrypt the letters.","startTime":405870,"duration":4280,"startOfParagraph":false},{"content":"So the first thing to do, you saw that the usage for Caesar in problem set 2","startTime":411370,"duration":5350,"startOfParagraph":false},{"content":"is to run Caesar and then enter a number when you run it in the terminal.","startTime":416720,"duration":8560,"startOfParagraph":false},{"content":"So what we need to do is to somehow get that key and access it.","startTime":425280,"duration":5660,"startOfParagraph":false},{"content":"And so we want to somehow see it's going to be the second command line argument.","startTime":430940,"duration":3790,"startOfParagraph":false},{"content":"The first one is going to be ./caesar, and the next one is going to be the key number.","startTime":434730,"duration":6220,"startOfParagraph":false},{"content":"So before we had int main (void) to start our C programs.","startTime":442190,"duration":7010,"startOfParagraph":false},{"content":"We're going to peel back a layer a little bit ","startTime":449200,"duration":2590,"startOfParagraph":false},{"content":"and actually see that instead of passing in void to our main function","startTime":451790,"duration":2930,"startOfParagraph":false},{"content":"we're actually dealing with 2 parameters.","startTime":454720,"duration":3200,"startOfParagraph":false},{"content":"We have an int named argc and then an array of strings called argv.","startTime":457920,"duration":6150,"startOfParagraph":false},{"content":"So argc is an integer, ","startTime":464070,"duration":1960,"startOfParagraph":false},{"content":"and it represents the number of arguments passed in to your program.","startTime":466030,"duration":3610,"startOfParagraph":false},{"content":"And then argv is actually the list of the arguments passed.","startTime":469640,"duration":3950,"startOfParagraph":false},{"content":"All of the arguments are strings, and so argv represents an array, a list, of strings.","startTime":473590,"duration":7230,"startOfParagraph":false},{"content":"Let's talk about arrays a little bit.","startTime":481830,"duration":2160,"startOfParagraph":false},{"content":"Arrays are essentially a new data structure.","startTime":483990,"duration":1950,"startOfParagraph":false},{"content":"We have ints, we have doubles, we have strings, and now we have arrays.","startTime":485940,"duration":3720,"startOfParagraph":false},{"content":"Arrays are data structures that can hold multiple values of the same type,","startTime":489660,"duration":4160,"startOfParagraph":false},{"content":"so essentially, a list of whatever type you want.","startTime":493820,"duration":4500,"startOfParagraph":false},{"content":"Essentially, if you wanted a list of integers all in 1 variable,","startTime":498320,"duration":6080,"startOfParagraph":false},{"content":"then you would create a new variable that was of type int array.","startTime":504400,"duration":4690,"startOfParagraph":false},{"content":"So arrays are zero-indexed, meaning that the first element of the array is at index 0.","startTime":509090,"duration":5360,"startOfParagraph":false},{"content":"If the array is of length 4, as in this example, then your last element would be at index 3,","startTime":514450,"duration":7349,"startOfParagraph":false},{"content":"which is 4 - 1.","startTime":521799,"duration":3011,"startOfParagraph":false},{"content":"So to create array, you would do something like this.","startTime":525940,"duration":2480,"startOfParagraph":false},{"content":"Say you wanted a double array.","startTime":528420,"duration":3020,"startOfParagraph":false},{"content":"This goes for any type of data type, though.","startTime":531440,"duration":5080,"startOfParagraph":false},{"content":"So say you want a double array. Say you want to call it mailbox.","startTime":536520,"duration":3690,"startOfParagraph":false},{"content":"Just like you would initialize any other double, ","startTime":540210,"duration":4550,"startOfParagraph":false},{"content":"you would say double and then the name, but this time we put the square brackets,","startTime":544760,"duration":5000,"startOfParagraph":false},{"content":"and then the number there will be the length of the array.","startTime":549760,"duration":3810,"startOfParagraph":false},{"content":"Note that in arrays we can't ever change the length,","startTime":553570,"duration":3270,"startOfParagraph":false},{"content":"so you always have to define and choose how many boxes, ","startTime":556840,"duration":4390,"startOfParagraph":false},{"content":"how many values your array is going to hold.","startTime":561230,"duration":4210,"startOfParagraph":false},{"content":"So to set different values in your array, you're going to use this following syntax,","startTime":565440,"duration":6380,"startOfParagraph":false},{"content":"as you see on the slide.","startTime":571820,"duration":1380,"startOfParagraph":false},{"content":"You have mailbox index 0 will be set to 1.2,","startTime":573200,"duration":4420,"startOfParagraph":false},{"content":"mailbox index 1 set to 2.4, etc.","startTime":577620,"duration":4560,"startOfParagraph":false},{"content":"So now that we've reviewed arrays a bit, let's go back to argc and argv.","startTime":582180,"duration":5730,"startOfParagraph":false},{"content":"We know that argv is now an array of strings.","startTime":587910,"duration":4310,"startOfParagraph":false},{"content":"So when a user passes in--say they're running a program--","startTime":592220,"duration":2860,"startOfParagraph":false},{"content":"they say ./hello David Malan,","startTime":595080,"duration":3660,"startOfParagraph":false},{"content":"what the program will do for you already is actually come up with what argc and argv are.","startTime":598740,"duration":6420,"startOfParagraph":false},{"content":"So you don't need to worry about that.","startTime":605160,"duration":2460,"startOfParagraph":false},{"content":"Argc in this case would be 3 because it sees 3 distinct words separated by spaces.","startTime":607620,"duration":6750,"startOfParagraph":false},{"content":"And so then the array in this instance, the first index would be ./hello,","startTime":614370,"duration":4480,"startOfParagraph":false},{"content":"the next one David, the next one Malan.","startTime":618850,"duration":2920,"startOfParagraph":false},{"content":"Does anyone see right away what the relationship between argv,","startTime":621770,"duration":3870,"startOfParagraph":false},{"content":" the array, and argc is?","startTime":625640,"duration":3350,"startOfParagraph":false},{"content":"Yeah. We'll get into that in an example in args.c.","startTime":632820,"duration":5270,"startOfParagraph":false},{"content":"Let's see if we can take advantage of the relationship between the 2.","startTime":638090,"duration":4790,"startOfParagraph":false},{"content":"Here you might find that in the appliance the default application ","startTime":642880,"duration":3670,"startOfParagraph":false},{"content":"to open .c files is sometimes Emacs. ","startTime":646550,"duration":2900,"startOfParagraph":false},{"content":"But we want to deal with gedit, so what you can do is you can right click on your C file,","startTime":649450,"duration":5210,"startOfParagraph":false},{"content":"go to Properties, Open With, and then choose gedit, Set as default,","startTime":654660,"duration":9920,"startOfParagraph":false},{"content":"and now your program should open in gedit instead of Emacs.","startTime":664580,"duration":8440,"startOfParagraph":false},{"content":"Perfect.","startTime":674710,"duration":1580,"startOfParagraph":false},{"content":"So here I have a program that I want to print out each command line argument.","startTime":677120,"duration":8400,"startOfParagraph":false},{"content":"So whatever the user inputs, I want to essentially return it back to them on a new line.","startTime":685520,"duration":6530,"startOfParagraph":false},{"content":"So what's a structure that we can use to iterate over something--","startTime":692050,"duration":4660,"startOfParagraph":false},{"content":"something that you probably used in your pset 1?","startTime":696710,"duration":3670,"startOfParagraph":false},{"content":"If you want to go through a set number of things? >>[student] For loop.","startTime":700380,"duration":5460,"startOfParagraph":false},{"content":"For loop. Exactly. So let's start with the for loop.","startTime":705840,"duration":3070,"startOfParagraph":false},{"content":"We have for int i = 0. Let's just start with a standard initialization variable.","startTime":708910,"duration":7990,"startOfParagraph":false},{"content":"I'm going to leave the condition for a set and then say i++, going to do things there.","startTime":716900,"duration":5470,"startOfParagraph":false},{"content":"All right.","startTime":722370,"duration":1720,"startOfParagraph":false},{"content":"So thinking back to argv, if argv is the list of arguments passed in to the program","startTime":724090,"duration":7500,"startOfParagraph":false},{"content":"and argc is the number of arguments in the program,","startTime":731590,"duration":3790,"startOfParagraph":false},{"content":"then that means that argc is essentially the length of argv, right,","startTime":735380,"duration":5900,"startOfParagraph":false},{"content":"because there are going to be as many arguments as the value of argc.","startTime":741280,"duration":7690,"startOfParagraph":false},{"content":"So if we want to iterate over each element in argv,","startTime":748970,"duration":6940,"startOfParagraph":false},{"content":"we're going to want to each time access the variable in argv at the given index.","startTime":755910,"duration":7380,"startOfParagraph":false},{"content":"That can be represented with this, right?","startTime":763290,"duration":5770,"startOfParagraph":false},{"content":"This variable here represents the particular string in this instance ","startTime":769060,"duration":4370,"startOfParagraph":false},{"content":"because it's a string array--the particular string at that given index.","startTime":773430,"duration":3600,"startOfParagraph":false},{"content":"What we want to do, in this case we want to print it out, so let's say printf.","startTime":777030,"duration":3660,"startOfParagraph":false},{"content":"And now argv is a string, so we want to put that placeholder there.","startTime":780690,"duration":3990,"startOfParagraph":false},{"content":"We want a new line just to make it look good.","startTime":784680,"duration":3750,"startOfParagraph":false},{"content":"So here we have a for loop. We don't have the condition yet.","startTime":788430,"duration":4100,"startOfParagraph":false},{"content":"So i starts at 0, and then every time it's going to print the given string","startTime":792530,"duration":7490,"startOfParagraph":false},{"content":"at that particular index in the array.","startTime":800020,"duration":2960,"startOfParagraph":false},{"content":"So when do we want to stop printing out elements in the array?","startTime":802980,"duration":5430,"startOfParagraph":false},{"content":"When we've finished, right? When we've reached the end of the array.","startTime":808410,"duration":7310,"startOfParagraph":false},{"content":"So we don't want to exceed past the length of the array,","startTime":815720,"duration":3150,"startOfParagraph":false},{"content":"and we already know we don't need to actually actively find out what the length of argv is","startTime":818870,"duration":4830,"startOfParagraph":false},{"content":"because it's given to us, and what's that? Argc. Exactly.","startTime":823700,"duration":3820,"startOfParagraph":false},{"content":"So we want to do this process argc number of times.","startTime":827520,"duration":9120,"startOfParagraph":false},{"content":"I'm not in the right directory.","startTime":836640,"duration":2910,"startOfParagraph":false},{"content":"All right.","startTime":842100,"duration":1390,"startOfParagraph":false},{"content":"Now let's make args. No errors, which is great.","startTime":843490,"duration":5500,"startOfParagraph":false},{"content":"So let's just run args.","startTime":848990,"duration":2440,"startOfParagraph":false},{"content":"What is this going to return to us? It's just going to print it back.","startTime":851430,"duration":3700,"startOfParagraph":false},{"content":"\"You inputted args into the program; I'm going to give it back to you.\"","startTime":855130,"duration":3190,"startOfParagraph":false},{"content":"So let's say we want to say args then foo bar.","startTime":858320,"duration":4850,"startOfParagraph":false},{"content":"So then it prints it out back to us. All right?","startTime":863170,"duration":3400,"startOfParagraph":false},{"content":"So there is an example of how you can use argc and argv","startTime":866570,"duration":4220,"startOfParagraph":false},{"content":"knowing that argc represents the length of argv.","startTime":870790,"duration":2670,"startOfParagraph":false},{"content":"Make sure that you do not ever with arrays access one beyond the length of the array","startTime":873460,"duration":9290,"startOfParagraph":false},{"content":"because C will definitely shout at you.","startTime":882750,"duration":2390,"startOfParagraph":false},{"content":"You'll get something called a segmentation fault,","startTime":885140,"duration":2420,"startOfParagraph":false},{"content":"which is never fun, basically saying you're trying to access something","startTime":887560,"duration":4910,"startOfParagraph":false},{"content":"that doesn't exist, doesn't belong to you.","startTime":892470,"duration":2530,"startOfParagraph":false},{"content":"So make sure, and especially with the zero-indexing, we don't want to--","startTime":895000,"duration":4430,"startOfParagraph":false},{"content":"Like for instance, if we have an array of length 4, ","startTime":899430,"duration":2960,"startOfParagraph":false},{"content":"that array index 4 doesn't exist because we start at 0, at zero index.","startTime":902390,"duration":4850,"startOfParagraph":false},{"content":"It will become second nature just like for loops when we start at 0.","startTime":907240,"duration":4490,"startOfParagraph":false},{"content":"So just keep that in mind. ","startTime":911730,"duration":1880,"startOfParagraph":false},{"content":"You don't want to ever access the index of an array that's beyond your reach.","startTime":913610,"duration":8980,"startOfParagraph":false},{"content":"So we can see now how we can kind of access ","startTime":926710,"duration":5850,"startOfParagraph":false},{"content":"the command line arguments that are passed in.","startTime":932560,"duration":3370,"startOfParagraph":false},{"content":"But as you saw the string, the argv is actually a string array.","startTime":935930,"duration":5400,"startOfParagraph":false},{"content":"So it's actually not an integer yet, but in Caesar we want to deal with integers.","startTime":941330,"duration":4410,"startOfParagraph":false},{"content":"Luckily, there is a function created for us that can actually convert a string to an integer.","startTime":945740,"duration":8690,"startOfParagraph":false},{"content":"Also in here we aren't dealing with user input where we're prompting them ","startTime":954430,"duration":4280,"startOfParagraph":false},{"content":"for input here for the key, so we can't actually reprompt and say,","startTime":958710,"duration":5030,"startOfParagraph":false},{"content":"\"Oh, give me another integer, say, if it's not valid.\"","startTime":963740,"duration":4100,"startOfParagraph":false},{"content":"But we do still need to check for correct usage.","startTime":967840,"duration":2700,"startOfParagraph":false},{"content":"In Caesar they are only allowed to pass in 1 number,","startTime":970540,"duration":2980,"startOfParagraph":false},{"content":"and so they have to run ./caesar and then they have to give you a number.","startTime":973520,"duration":4510,"startOfParagraph":false},{"content":"So argc has to be a certain number. ","startTime":978030,"duration":5630,"startOfParagraph":false},{"content":"What number would that be if they have to pass you the ./caesar and then a key?","startTime":983660,"duration":5400,"startOfParagraph":false},{"content":"What is argc? >>[student] 2. >>Two. Exactly.","startTime":989060,"duration":3860,"startOfParagraph":false},{"content":"So you want to make sure that argc is 2.","startTime":992920,"duration":2570,"startOfParagraph":false},{"content":"Otherwise you basically refuse to run the program.","startTime":995490,"duration":4130,"startOfParagraph":false},{"content":"In main it's a function that says int main,","startTime":999620,"duration":3420,"startOfParagraph":false},{"content":"so then we always in good practice return 0 at the end of a successful program.","startTime":1003040,"duration":4320,"startOfParagraph":false},{"content":"So if, say, they give you 3 command line arguments instead of 2","startTime":1007360,"duration":3480,"startOfParagraph":false},{"content":"or give you 1, for instance, then what you'll do is you'll want to check for that","startTime":1010840,"duration":3510,"startOfParagraph":false},{"content":"and then return 1 saying, no, I can't proceed with this program.","startTime":1014350,"duration":5550,"startOfParagraph":false},{"content":"[student] There can't be a space in your text. >>Pardon me?","startTime":1019900,"duration":3290,"startOfParagraph":false},{"content":"[student] There can't be a space in the text you're trying to encrypt.","startTime":1023190,"duration":3590,"startOfParagraph":false},{"content":"Ah! ","startTime":1026780,"duration":1700,"startOfParagraph":false},{"content":"In terms of the text that we're trying to encrypt, that actually comes later","startTime":1028480,"duration":2800,"startOfParagraph":false},{"content":"when we give that text.","startTime":1031280,"duration":2690,"startOfParagraph":false},{"content":"So right now we're just accepting as command arguments the actual number,","startTime":1033970,"duration":4290,"startOfParagraph":false},{"content":"the actual shift for the Caesar encryption.","startTime":1038260,"duration":3319,"startOfParagraph":false},{"content":"[student] Why do you need 2 as opposed to just 1 argc? There's definitely 1 number.","startTime":1041579,"duration":5990,"startOfParagraph":false},{"content":"Right. The reason why we need 2 for argc instead of 1","startTime":1047569,"duration":4631,"startOfParagraph":false},{"content":"is because when you run a program and say ./caesar or ./hello,","startTime":1052200,"duration":4060,"startOfParagraph":false},{"content":"that actually counts as a command line argument.","startTime":1056260,"duration":2020,"startOfParagraph":false},{"content":"So then that already takes up 1 and so then we're inputting 1 extra.","startTime":1058280,"duration":4740,"startOfParagraph":false},{"content":"So you're inputting actually a string in the command line argument.","startTime":1065030,"duration":4410,"startOfParagraph":false},{"content":"What you want to do, for Caesar we want to deal with an integer,","startTime":1069440,"duration":3290,"startOfParagraph":false},{"content":"so you can use this atoi function.","startTime":1072730,"duration":4450,"startOfParagraph":false},{"content":"And basically, you pass it in a string and then it will return you back an integer","startTime":1077180,"duration":5670,"startOfParagraph":false},{"content":"if it's possible to make that string into an integer.","startTime":1082850,"duration":3220,"startOfParagraph":false},{"content":"Now remember when we're dealing with printf or GetString, things like that,","startTime":1086070,"duration":4890,"startOfParagraph":false},{"content":"we include the libraries that are specific to us.","startTime":1090960,"duration":2430,"startOfParagraph":false},{"content":"So at the beginning we start with a hash tag standard I/O, .h, something like that.","startTime":1093390,"duration":6060,"startOfParagraph":false},{"content":"Well, atoi isn't within one of those libraries,","startTime":1099450,"duration":2980,"startOfParagraph":false},{"content":"so what we have to do is we have to include the right library for that.","startTime":1102430,"duration":4170,"startOfParagraph":false},{"content":"So recall back to Walkthrough 1 where I discussed the manual function.","startTime":1106600,"duration":6120,"startOfParagraph":false},{"content":"You type man in your terminal and then followed by the name of a function.","startTime":1112720,"duration":4390,"startOfParagraph":false},{"content":"And so that will bring up a whole list of its usage,","startTime":1117110,"duration":2610,"startOfParagraph":false},{"content":"but as well it will bring up which library that belongs to.","startTime":1119720,"duration":3170,"startOfParagraph":false},{"content":"So I'll leave that to you to use the manual function with atoi","startTime":1122890,"duration":4110,"startOfParagraph":false},{"content":"and figure out which library you need to include to be able to use the atoi function.","startTime":1127000,"duration":6360,"startOfParagraph":false},{"content":"So we've got the key and now it comes to getting the plain text,","startTime":1134450,"duration":3220,"startOfParagraph":false},{"content":"and so that actually is going to be user input where you prompt.","startTime":1137670,"duration":4150,"startOfParagraph":false},{"content":"We dealt with GetInt and GetFloat, and so in the same vein ","startTime":1141820,"duration":3720,"startOfParagraph":false},{"content":"we're going to be dealing with GetString.","startTime":1145540,"duration":2130,"startOfParagraph":false},{"content":"But in this case we don't need to do any do while or while loops to check.","startTime":1147670,"duration":4770,"startOfParagraph":false},{"content":"GetString will definitely give us a string, ","startTime":1152440,"duration":2040,"startOfParagraph":false},{"content":"and we're going to encrypt whatever the user gives us.","startTime":1154480,"duration":3150,"startOfParagraph":false},{"content":"So you can assume that all of these user inputted strings are correct.","startTime":1157630,"duration":6140,"startOfParagraph":false},{"content":"Great.","startTime":1163770,"duration":900,"startOfParagraph":false},{"content":"So then once you've got the key and once you've got the text,","startTime":1164670,"duration":2600,"startOfParagraph":false},{"content":"now what's left is you have to encipher the plaintext.","startTime":1167270,"duration":4390,"startOfParagraph":false},{"content":"Just to quickly cover over lingo, the plaintext is what the user gives you,","startTime":1171660,"duration":4870,"startOfParagraph":false},{"content":"and the ciphertext is what you return to them.","startTime":1176530,"duration":4500,"startOfParagraph":false},{"content":"So strings, to be able to go through actually letter by letter","startTime":1182450,"duration":3400,"startOfParagraph":false},{"content":"because we have to shift every letter,","startTime":1185850,"duration":2700,"startOfParagraph":false},{"content":"we understand that strings, if we kind of peel back the layer,","startTime":1188550,"duration":2840,"startOfParagraph":false},{"content":"we see that they're just really a list of characters.","startTime":1191390,"duration":2740,"startOfParagraph":false},{"content":"One comes after the other.","startTime":1194130,"duration":1800,"startOfParagraph":false},{"content":"And so we can treat strings as arrays because they are arrays of characters.","startTime":1195930,"duration":5760,"startOfParagraph":false},{"content":"So say you have a string named text, ","startTime":1201690,"duration":3950,"startOfParagraph":false},{"content":"and within that variable text is stored This is CS50.","startTime":1205640,"duration":3760,"startOfParagraph":false},{"content":"Then text at index 0 would be a capital T, index 1 would be h, etc.","startTime":1209400,"duration":6280,"startOfParagraph":false},{"content":"And then with arrays, in the argc example in args.c,","startTime":1217530,"duration":6440,"startOfParagraph":false},{"content":"we saw that we had to iterate over an array","startTime":1223970,"duration":3120,"startOfParagraph":false},{"content":"and so we had to iterate from i = 0 up until i is less than the length.","startTime":1227090,"duration":5350,"startOfParagraph":false},{"content":"So we need some way of figuring out what the length of our string is","startTime":1232440,"duration":3120,"startOfParagraph":false},{"content":"if we're going to iterate over it.","startTime":1235560,"duration":1530,"startOfParagraph":false},{"content":"Luckily again, there is a function there for us, although later on in CS50","startTime":1237090,"duration":5210,"startOfParagraph":false},{"content":"you'll definitely be able to implement and make your own function","startTime":1242300,"duration":3560,"startOfParagraph":false},{"content":"that can calculate the length of a string.","startTime":1245860,"duration":2400,"startOfParagraph":false},{"content":"But for now we're going to use string length, so strlen.","startTime":1248260,"duration":3860,"startOfParagraph":false},{"content":"You pass in a string, and then it will return you an int that represents the length of your string.","startTime":1252120,"duration":8320,"startOfParagraph":false},{"content":"Let's look at an example of how we might be able to iterate over each character in a string","startTime":1260440,"duration":5400,"startOfParagraph":false},{"content":"and do something with that.","startTime":1265840,"duration":2630,"startOfParagraph":false},{"content":"What we want to do is iterate over each character of the string,","startTime":1268470,"duration":4780,"startOfParagraph":false},{"content":"and what we want to do is we print back each character 1 by 1","startTime":1273250,"duration":5900,"startOfParagraph":false},{"content":"except we add something next to it.","startTime":1279150,"duration":2910,"startOfParagraph":false},{"content":"So let's start with the for loop. Int i = 0.","startTime":1282060,"duration":4960,"startOfParagraph":false},{"content":"We're going to leave space for the condition.","startTime":1287020,"duration":3050,"startOfParagraph":false},{"content":"We want to iterate until we reach the end of the string, right?","startTime":1292700,"duration":4140,"startOfParagraph":false},{"content":"So then what function gives us the length of the string?","startTime":1296840,"duration":4500,"startOfParagraph":false},{"content":"[inaudible student response]","startTime":1301340,"duration":1820,"startOfParagraph":false},{"content":"That's the length of the command line arguments.","startTime":1303160,"duration":3260,"startOfParagraph":false},{"content":"But for a string we want to use a function that gives us the length of the string.","startTime":1306420,"duration":4230,"startOfParagraph":false},{"content":"So that's string length.","startTime":1310650,"duration":2440,"startOfParagraph":false},{"content":"And so then you have to pass in a string to it.","startTime":1313090,"duration":4040,"startOfParagraph":false},{"content":"It needs to know what string it needs to calculate the length of.","startTime":1317130,"duration":2630,"startOfParagraph":false},{"content":"So then in this case we're dealing with string s.","startTime":1319760,"duration":3400,"startOfParagraph":false},{"content":"Great. ","startTime":1324790,"duration":1070,"startOfParagraph":false},{"content":"So then what we want to do, let's printf.","startTime":1325860,"duration":4910,"startOfParagraph":false},{"content":"Now, we want to deal with characters. We want to print out each individual character.","startTime":1330770,"duration":4080,"startOfParagraph":false},{"content":"When you want it to print out a float, you would use the placeholder like %f.","startTime":1334850,"duration":7300,"startOfParagraph":false},{"content":"With an int you would use %d.","startTime":1342150,"duration":2430,"startOfParagraph":false},{"content":"And so similarly, with a character you use %c to say I'm going to be printing a character","startTime":1344580,"duration":6310,"startOfParagraph":false},{"content":"that's stored inside a variable.","startTime":1350890,"duration":3680,"startOfParagraph":false},{"content":"So we have this, and let's add a period and a space to it.","startTime":1354570,"duration":6270,"startOfParagraph":false},{"content":"Which character are we using? ","startTime":1360840,"duration":4590,"startOfParagraph":false},{"content":"We're going to be using whatever character we're at of the string.","startTime":1365430,"duration":4350,"startOfParagraph":false},{"content":"So then we're going to be using something with string,","startTime":1369780,"duration":3110,"startOfParagraph":false},{"content":"but we want to be accessing the certain character there.","startTime":1372890,"duration":3530,"startOfParagraph":false},{"content":"So if a string is just an array, then how do we access elements of arrays?","startTime":1376420,"duration":6320,"startOfParagraph":false},{"content":"We have those square brackets, and then we put the index in there.","startTime":1382740,"duration":3740,"startOfParagraph":false},{"content":"So we have square brackets. Our index in this case we can just use i. Exactly.","startTime":1386480,"duration":5340,"startOfParagraph":false},{"content":"So here we're saying we're going to be printing a character followed by a dot and a space,","startTime":1395290,"duration":7080,"startOfParagraph":false},{"content":"and that character is going to be the ith letter in our string s.","startTime":1402370,"duration":8500,"startOfParagraph":false},{"content":"I'm just going to save that. Okay.","startTime":1412920,"duration":6410,"startOfParagraph":false},{"content":"Now I'm going to run string length.","startTime":1422510,"duration":4330,"startOfParagraph":false},{"content":"So we had a string called OMG, and now it's emphasized even more.","startTime":1426840,"duration":6600,"startOfParagraph":false},{"content":"Similarly, let's say we actually want to get a string from the user.","startTime":1433440,"duration":4430,"startOfParagraph":false},{"content":"How might we do this?","startTime":1437870,"duration":1710,"startOfParagraph":false},{"content":"Before, how did we get an int?","startTime":1439580,"duration":2030,"startOfParagraph":false},{"content":"We said GetInt, right? But this isn't int, so let's GetString.","startTime":1441610,"duration":6430,"startOfParagraph":false},{"content":"Let's make string length. Here we didn't enter a specific prompt.","startTime":1451780,"duration":5990,"startOfParagraph":false},{"content":"So I don't know. ","startTime":1457770,"duration":2170,"startOfParagraph":false},{"content":"I'm going to put my name in here and so then I can do one of those things","startTime":1459940,"duration":3880,"startOfParagraph":false},{"content":"where I assign a word for every letter or something like that. Cool.","startTime":1463820,"duration":5780,"startOfParagraph":false},{"content":"So that's string length.","startTime":1469600,"duration":2300,"startOfParagraph":false},{"content":"So we're back to Caesar. ","startTime":1473000,"duration":1640,"startOfParagraph":false},{"content":"We have a few tools on how we iterate over a string, ","startTime":1474640,"duration":3980,"startOfParagraph":false},{"content":"how we access each individual element.","startTime":1478620,"duration":2630,"startOfParagraph":false},{"content":"So now we can get back to the program.","startTime":1481250,"duration":3470,"startOfParagraph":false},{"content":"As I mentioned before, in the ASCII table, your best friend,","startTime":1484720,"duration":3930,"startOfParagraph":false},{"content":"you're going to see the numbers that are associated with every letter.","startTime":1488650,"duration":3650,"startOfParagraph":false},{"content":"So here say our plaintext is I'm dizzy!","startTime":1492300,"duration":3600,"startOfParagraph":false},{"content":"Then each of these characters is going to have a number and ASCII value associated with it,","startTime":1495900,"duration":5190,"startOfParagraph":false},{"content":"even the apostrophe, even the space, even the exclamation mark,","startTime":1501090,"duration":3620,"startOfParagraph":false},{"content":"so you'll want to keep that in mind.","startTime":1504710,"duration":1890,"startOfParagraph":false},{"content":"So say our key that the user included in their command line argument is 6.","startTime":1506600,"duration":5760,"startOfParagraph":false},{"content":"That means for the first letter, which is I, which is represented by 73,","startTime":1512360,"duration":5410,"startOfParagraph":false},{"content":"you want to return to them whatever letter is represented by the ASCII value of 73 + 6.","startTime":1517770,"duration":7840,"startOfParagraph":false},{"content":"In this case that would be 79.","startTime":1525610,"duration":3410,"startOfParagraph":false},{"content":"Now we want to go to the next character.","startTime":1530840,"duration":4200,"startOfParagraph":false},{"content":"So the next in index 1 of the plaintext would be the apostrophe.","startTime":1535040,"duration":5920,"startOfParagraph":false},{"content":"But remember we only want to encipher the letters.","startTime":1540960,"duration":5820,"startOfParagraph":false},{"content":"So we want to make sure that the apostrophe actually stays the same,","startTime":1546780,"duration":3260,"startOfParagraph":false},{"content":"that we don't change from 39 to whatever 45 is.","startTime":1550040,"duration":4270,"startOfParagraph":false},{"content":"We want to keep it as an apostrophe.","startTime":1554310,"duration":2840,"startOfParagraph":false},{"content":"So we want to remember to only encipher the letters","startTime":1557150,"duration":3630,"startOfParagraph":false},{"content":"because we want all of the other symbols to remain unchanged in our program.","startTime":1560780,"duration":3780,"startOfParagraph":false},{"content":"Another thing that we want is to preserve capitalization.","startTime":1564560,"duration":2570,"startOfParagraph":false},{"content":"So when you have an uppercase letter, it should stay as an uppercase.","startTime":1567130,"duration":3120,"startOfParagraph":false},{"content":"Lowercases should stay as lowercase.","startTime":1570250,"duration":2580,"startOfParagraph":false},{"content":"So some useful functions to be able to deal with only enciphering letters","startTime":1573620,"duration":5860,"startOfParagraph":false},{"content":"and keep preserving the capitalization of things ","startTime":1579480,"duration":2900,"startOfParagraph":false},{"content":"is the isalpha, isupper, islower functions.","startTime":1582380,"duration":2750,"startOfParagraph":false},{"content":"And so these are functions that return you a Boolean value.","startTime":1585130,"duration":4140,"startOfParagraph":false},{"content":"Basically, true or false. Is this an uppercase? Is this alphanumeric? ","startTime":1589270,"duration":4910,"startOfParagraph":false},{"content":"Is this a letter, essentially.","startTime":1594180,"duration":3000,"startOfParagraph":false},{"content":"So here are 3 examples of how you would use that function.","startTime":1597180,"duration":3890,"startOfParagraph":false},{"content":"Basically, you could test whether the value returned to you by that function is true or false","startTime":1601070,"duration":5990,"startOfParagraph":false},{"content":"based on that input.","startTime":1607060,"duration":2340,"startOfParagraph":false},{"content":"Either don't encipher something or cipher it or make sure that it's uppercase, etc.","startTime":1609400,"duration":5480,"startOfParagraph":false},{"content":"[student] Can you just explain those a little more and how you use them? >>Yeah, for sure.","startTime":1614880,"duration":6200,"startOfParagraph":false},{"content":"So if we look back, here we have a capital I, right?","startTime":1621080,"duration":7390,"startOfParagraph":false},{"content":"So we know that I goes to O because I + 6 is O.","startTime":1628470,"duration":6080,"startOfParagraph":false},{"content":"But we want to make sure that that O is going to be a capital O.","startTime":1634550,"duration":4190,"startOfParagraph":false},{"content":"So basically, that is kind of going to change our input.","startTime":1638740,"duration":4200,"startOfParagraph":false},{"content":"So whether it's uppercase or not will kind of change the way that we deal with it.","startTime":1642940,"duration":3930,"startOfParagraph":false},{"content":"So then if we use the isupper function on that particular index,","startTime":1646870,"duration":5490,"startOfParagraph":false},{"content":"so isupper(\"I\"), that returns for us true, so we know that it's upper.","startTime":1652360,"duration":4120,"startOfParagraph":false},{"content":"So then based on that, later we'll go into a formula ","startTime":1656480,"duration":3880,"startOfParagraph":false},{"content":"that you'll be using to shift things in Caesar,","startTime":1660360,"duration":2390,"startOfParagraph":false},{"content":"so then basically, there's going to be a slightly different formula if it's uppercase","startTime":1662750,"duration":3810,"startOfParagraph":false},{"content":"as opposed to lowercase. Make sense?","startTime":1666560,"duration":4110,"startOfParagraph":false},{"content":"Yeah. No worries.","startTime":1671020,"duration":1740,"startOfParagraph":false},{"content":"I talked a bit about adding 6 to a letter, which doesn't quite make sense","startTime":1674900,"duration":4090,"startOfParagraph":false},{"content":"except when we kind of understand that these characters ","startTime":1678990,"duration":6510,"startOfParagraph":false},{"content":"are kind of interchangeable with integers.","startTime":1685500,"duration":3420,"startOfParagraph":false},{"content":"What we do is we kind of use implicit casting.","startTime":1688920,"duration":2330,"startOfParagraph":false},{"content":"We'll go into casting a bit later on where you take a value and you turn it into a different type","startTime":1691250,"duration":6850,"startOfParagraph":false},{"content":"than it originally was.","startTime":1698100,"duration":2340,"startOfParagraph":false},{"content":"But with this pset we'll be able to kind of interchangeably use characters","startTime":1700440,"duration":5470,"startOfParagraph":false},{"content":"and their corresponding integer values.","startTime":1705910,"duration":4970,"startOfParagraph":false},{"content":"So if you simply encase a character with just the single quotes,","startTime":1710880,"duration":4260,"startOfParagraph":false},{"content":"then you'll be able to work with it with integers, dealing with it as an integer.","startTime":1715140,"duration":5250,"startOfParagraph":false},{"content":"So the capital C relates to 67. Lowercase f relates to 102.","startTime":1720390,"duration":7650,"startOfParagraph":false},{"content":"Again, if you want to know these values, look at your ASCII table.","startTime":1728040,"duration":3440,"startOfParagraph":false},{"content":"So let's go into some examples of how you might be able to subtract and add,","startTime":1731480,"duration":4680,"startOfParagraph":false},{"content":"how you can actually really work with these characters, use them interchangeably.","startTime":1736160,"duration":6970,"startOfParagraph":false},{"content":"I say that ASCIIMath is going to calculate the addition of a character to an integer","startTime":1743870,"duration":7480,"startOfParagraph":false},{"content":"and then displays the resultant character as well as the resultant ASCII value.","startTime":1751350,"duration":6240,"startOfParagraph":false},{"content":"And so here I'm saying--we'll deal with this part later--","startTime":1757590,"duration":4700,"startOfParagraph":false},{"content":"but basically, I'm saying that the user should say run ASCIIMath along with a key,","startTime":1762290,"duration":6810,"startOfParagraph":false},{"content":"and I'm saying that that key is going to be the number","startTime":1769100,"duration":1780,"startOfParagraph":false},{"content":"with which we're going to add this character.","startTime":1770880,"duration":3720,"startOfParagraph":false},{"content":"So here notice that since I'm demanding a key,","startTime":1774600,"duration":3960,"startOfParagraph":false},{"content":"since I'm demanding that they're giving me 1 thing, ","startTime":1778560,"duration":2030,"startOfParagraph":false},{"content":"I only want to accept ./asciimath and a key.","startTime":1780590,"duration":5010,"startOfParagraph":false},{"content":"So I'm going to demand that argc is equal to 2.","startTime":1785600,"duration":3730,"startOfParagraph":false},{"content":"If it's not, then I'm going to return 1 and the program will exit.","startTime":1789330,"duration":5030,"startOfParagraph":false},{"content":"So I'm saying the key isn't going to be the first command line argument,","startTime":1795070,"duration":3470,"startOfParagraph":false},{"content":"it's going to be the second one, and as you see here,","startTime":1798540,"duration":6540,"startOfParagraph":false},{"content":"I'm going to turn that into an integer.","startTime":1805080,"duration":6710,"startOfParagraph":false},{"content":"Then I'm going to set a character to be r.","startTime":1815740,"duration":3490,"startOfParagraph":false},{"content":"Notice that the type of the variable chr is actually an integer.","startTime":1819230,"duration":4740,"startOfParagraph":false},{"content":"The way that I'm able to use r as an integer is by encasing it with these single quotes.","startTime":1823970,"duration":6510,"startOfParagraph":false},{"content":"So back to our printf statement where we have a placeholder for a character","startTime":1833850,"duration":6710,"startOfParagraph":false},{"content":"and then a placeholder for an integer,","startTime":1840560,"duration":3030,"startOfParagraph":false},{"content":"the character is represented by the chr, and the integer is the key.","startTime":1843590,"duration":5860,"startOfParagraph":false},{"content":"And so then we're going to in result add the 2 together.","startTime":1849450,"duration":4870,"startOfParagraph":false},{"content":"So we're going to add r + whatever the key is,","startTime":1854320,"duration":4100,"startOfParagraph":false},{"content":"and then we're going to print the result of that.","startTime":1858420,"duration":5100,"startOfParagraph":false},{"content":"So let's make asciimath. It's up to date, so let's just run asciimath.","startTime":1866210,"duration":8010,"startOfParagraph":false},{"content":"Oh, but see, it doesn't do anything because we didn't actually give it a key.","startTime":1874220,"duration":4070,"startOfParagraph":false},{"content":"So when it just returned 1, our main function, it just returned back to us.","startTime":1878290,"duration":5560,"startOfParagraph":false},{"content":"So then let's pass in a key. Someone give me a number. >>[student] 4.","startTime":1883850,"duration":5400,"startOfParagraph":false},{"content":"4. Okay. ","startTime":1889250,"duration":1670,"startOfParagraph":false},{"content":"So r increased by 4 is going to give us v, which corresponds to the ASCII value of 118.","startTime":1890920,"duration":8360,"startOfParagraph":false},{"content":"So then it kind of makes sense that--","startTime":1899280,"duration":4600,"startOfParagraph":false},{"content":"Actually, can I ask you, what do you think the ASCII value of r is if r + 4 is 118?","startTime":1903880,"duration":7370,"startOfParagraph":false},{"content":"Then yeah, r is 114.","startTime":1913070,"duration":2400,"startOfParagraph":false},{"content":"So if you look on the ASCII table then, sure enough, you'll see that r is represented by 114.","startTime":1915470,"duration":7540,"startOfParagraph":false},{"content":"So now that we know that we can add integers to characters, this seems pretty simple.","startTime":1923010,"duration":5600,"startOfParagraph":false},{"content":"We're just going to iterate over a string like we saw in an example before.","startTime":1928610,"duration":4130,"startOfParagraph":false},{"content":"We'll check if it's a letter.","startTime":1932740,"duration":4430,"startOfParagraph":false},{"content":"If it is, then we'll shift it by whatever the key is.","startTime":1937170,"duration":3250,"startOfParagraph":false},{"content":"Pretty simple, except when you get to like this,","startTime":1940420,"duration":3230,"startOfParagraph":false},{"content":"you see that z, represented by 122, then would give you a different character.","startTime":1943650,"duration":8490,"startOfParagraph":false},{"content":"We actually want to stay within our alphabet, right?","startTime":1952140,"duration":5630,"startOfParagraph":false},{"content":"So we need to figure out some way of kind of wrapping around.","startTime":1957770,"duration":5410,"startOfParagraph":false},{"content":"When you reach zed and you want to increase by a certain number,","startTime":1963180,"duration":4010,"startOfParagraph":false},{"content":"you don't want to go into beyond the ASCII alphabet section;","startTime":1967190,"duration":4040,"startOfParagraph":false},{"content":"you want to wrap back all the way to A.","startTime":1971230,"duration":2910,"startOfParagraph":false},{"content":"But keep in mind you're still preserving the case.","startTime":1974140,"duration":4410,"startOfParagraph":false},{"content":"So knowing that letters can't become symbols","startTime":1978550,"duration":2430,"startOfParagraph":false},{"content":"just like symbols aren't going to be changing as well.","startTime":1980980,"duration":4310,"startOfParagraph":false},{"content":"In the last pset you definitely didn't need to,","startTime":1985290,"duration":2880,"startOfParagraph":false},{"content":"but an option was to implement your greedy pset by using the modulus function.","startTime":1988170,"duration":6140,"startOfParagraph":false},{"content":"But now we're actually going to need to use modulus,","startTime":1994310,"duration":2920,"startOfParagraph":false},{"content":"so let's just go over this a little bit.","startTime":1997230,"duration":2670,"startOfParagraph":false},{"content":"Essentially, when you have x modulo y, that gives you the remainder of x divided by y.","startTime":1999900,"duration":7020,"startOfParagraph":false},{"content":"Here are some examples here. We have 27 % 15.","startTime":2006920,"duration":4010,"startOfParagraph":false},{"content":"Basically, when you subtract 15 from 27 as many times as possible without getting negative","startTime":2010930,"duration":5270,"startOfParagraph":false},{"content":"then you get 12 left over.","startTime":2016200,"duration":2860,"startOfParagraph":false},{"content":"So that's kind of like in the math context, but how can we actually use this?","startTime":2019060,"duration":5590,"startOfParagraph":false},{"content":"It's going to be useful for our wrapover.","startTime":2024650,"duration":2450,"startOfParagraph":false},{"content":"For this, let's just say I asked you all to divide into 3 groups.","startTime":2027100,"duration":8320,"startOfParagraph":false},{"content":"Sometimes you do this in groups and something like that.","startTime":2035420,"duration":2590,"startOfParagraph":false},{"content":"Say I said, \"Okay, I want you all to be divided into 3.\"","startTime":2038010,"duration":3310,"startOfParagraph":false},{"content":"How might you do that?","startTime":2041320,"duration":2920,"startOfParagraph":false},{"content":"[inaudible student response] Yeah, exactly. Count off. Okay.","startTime":2044240,"duration":2570,"startOfParagraph":false},{"content":"Let's actually do that. Do you want to start?","startTime":2046810,"duration":3450,"startOfParagraph":false},{"content":"[students counting off] 1, 2, 3, 4.","startTime":2050260,"duration":3550,"startOfParagraph":false},{"content":"But remember... >>[student] Oh, sorry.","startTime":2053810,"duration":2810,"startOfParagraph":false},{"content":"That's a really good point.","startTime":2056620,"duration":2110,"startOfParagraph":false},{"content":"You said 4, but we actually want you to say 1 because we only want 3 groups.","startTime":2058730,"duration":5400,"startOfParagraph":false},{"content":"So then, how-- No, that's a really good example because then how might you say 1?","startTime":2064130,"duration":6029,"startOfParagraph":false},{"content":"What's the relationship between 4 and 1?","startTime":2070159,"duration":3211,"startOfParagraph":false},{"content":"Well, 4 mod 3 is 1.","startTime":2073370,"duration":3390,"startOfParagraph":false},{"content":"So if you continue, you would be 2.","startTime":2076760,"duration":4700,"startOfParagraph":false},{"content":"So we have 1, 2, 3, 1, 2.","startTime":2081460,"duration":3080,"startOfParagraph":false},{"content":"Again, you're actually the 5th person. How do you know to say 2 instead of 5?","startTime":2084540,"duration":4880,"startOfParagraph":false},{"content":"You say 5 mod 3 is 2.","startTime":2089420,"duration":4340,"startOfParagraph":false},{"content":"I want to see how many groups of 3 are left over, then which order am I.","startTime":2093760,"duration":5340,"startOfParagraph":false},{"content":"And so then if we continued along the whole room, ","startTime":2099100,"duration":3760,"startOfParagraph":false},{"content":"then we would see that we're always actually applying the mod function to ourselves","startTime":2102860,"duration":4900,"startOfParagraph":false},{"content":"to kind of count off.","startTime":2107760,"duration":2230,"startOfParagraph":false},{"content":"That's a more kind of tangible example of how you might use modulo","startTime":2109990,"duration":4500,"startOfParagraph":false},{"content":"because I'm sure most of us have probably gone through that process","startTime":2114490,"duration":3470,"startOfParagraph":false},{"content":"where we've had to count off.","startTime":2117960,"duration":1670,"startOfParagraph":false},{"content":"Any questions on modulo? ","startTime":2119630,"duration":2210,"startOfParagraph":false},{"content":"It will be pretty important to understand the concepts of this, ","startTime":2121840,"duration":3520,"startOfParagraph":false},{"content":"so I want to make sure you guys understand.","startTime":2125360,"duration":3280,"startOfParagraph":false},{"content":"[student] If there is no remainder, does it give you the actual number?","startTime":2128640,"duration":6020,"startOfParagraph":false},{"content":"If one of the first 3 of them had done it, would it have given them what they actually were,","startTime":2134660,"duration":5770,"startOfParagraph":false},{"content":"or would it have given them [inaudible] >>That's a good question.","startTime":2140430,"duration":2880,"startOfParagraph":false},{"content":"When there is no remainder for the modulo--so say you have 6 mod 3--","startTime":2143310,"duration":5440,"startOfParagraph":false},{"content":"that actually gives you back 0.","startTime":2148750,"duration":3590,"startOfParagraph":false},{"content":"We'll talk about that a bit later.","startTime":2153670,"duration":3620,"startOfParagraph":false},{"content":"Oh yeah, for instance, the 3rd person--3 mod 3 is actually 0 but she said 3.","startTime":2158810,"duration":8910,"startOfParagraph":false},{"content":"So that's kind of like an inner catch, for instance,","startTime":2167720,"duration":7180,"startOfParagraph":false},{"content":"like okay, if the mod is 0 then I'm going to be the 3rd person.","startTime":2174900,"duration":2720,"startOfParagraph":false},{"content":"But we'll get into kind of how we might want to deal with what 0 is later.","startTime":2177620,"duration":5120,"startOfParagraph":false},{"content":"So now we somehow have a way of mapping the zed to the right letter.","startTime":2182740,"duration":10010,"startOfParagraph":false},{"content":"So now we've gone through these examples,","startTime":2192750,"duration":2170,"startOfParagraph":false},{"content":"we kind of see how Caesar might work.","startTime":2194920,"duration":2960,"startOfParagraph":false},{"content":"You see the 2 alphabets and then you see them shifting.","startTime":2197880,"duration":4760,"startOfParagraph":false},{"content":"So let's try and express that in terms of formula.","startTime":2202640,"duration":1790,"startOfParagraph":false},{"content":"This formula is actually given to you in the spec,","startTime":2204430,"duration":2510,"startOfParagraph":false},{"content":"but let's kind of look through what each variable means.","startTime":2206940,"duration":5130,"startOfParagraph":false},{"content":"Our end result is going to be the ciphertext.","startTime":2212070,"duration":2930,"startOfParagraph":false},{"content":"So this says that the ith character of the ciphertext","startTime":2215000,"duration":3300,"startOfParagraph":false},{"content":"is going to correspond to the ith character of the plaintext. ","startTime":2218300,"duration":4200,"startOfParagraph":false},{"content":"That makes sense because we want to always be lining these things up.","startTime":2222500,"duration":5630,"startOfParagraph":false},{"content":"So it's going to be the ith character of the ciphertext plus k, which is our key--","startTime":2228130,"duration":5350,"startOfParagraph":false},{"content":"that makes sense--and then we have this mod 26.","startTime":2233480,"duration":3750,"startOfParagraph":false},{"content":"Remember back when we had the zed","startTime":2237230,"duration":2630,"startOfParagraph":false},{"content":"we didn't want to get into the character, so we wanted to mod it","startTime":2239860,"duration":4330,"startOfParagraph":false},{"content":"and kind of wrap around the alphabet.","startTime":2244190,"duration":2350,"startOfParagraph":false},{"content":"After zed you would go to a, b, c, d, until you got to the right number.","startTime":2246540,"duration":6890,"startOfParagraph":false},{"content":"So we know that zed, if + 6, would give us f because after zed comes a, b, c, d, e, f.","startTime":2253430,"duration":11260,"startOfParagraph":false},{"content":"So let's remember we know for sure that zed + 6 is going to give us f.","startTime":2264690,"duration":7840,"startOfParagraph":false},{"content":"In ASCII values, z is 122 and f is 102.","startTime":2272530,"duration":11000,"startOfParagraph":false},{"content":"So we have to find some way of making our Caesar formula give us 102","startTime":2283530,"duration":7040,"startOfParagraph":false},{"content":"after taking in 122.","startTime":2290570,"duration":3020,"startOfParagraph":false},{"content":"So if we just apply this formula, the ('z' + 6) % 26, that actually gives you 24","startTime":2293590,"duration":5960,"startOfParagraph":false},{"content":"because 122 + 6 is 128; 128 % 26 gives you 24 remainder.","startTime":2299550,"duration":6430,"startOfParagraph":false},{"content":"But that doesn't really mean f. That's definitely not 102.","startTime":2305980,"duration":3160,"startOfParagraph":false},{"content":"That's also not the 6th letter in the alphabet.","startTime":2309140,"duration":4450,"startOfParagraph":false},{"content":"So obviously, we need to have some way of tweaking this a little bit.","startTime":2313590,"duration":7960,"startOfParagraph":false},{"content":"In terms of the regular alphabet, we know that z is the 26th letter and f is the 6th.","startTime":2322970,"duration":8370,"startOfParagraph":false},{"content":"But we're in computer science, so we're going to index at 0.","startTime":2331340,"duration":4120,"startOfParagraph":false},{"content":"So then instead of z being the number 26, we're going to say it's number 25","startTime":2335460,"duration":5230,"startOfParagraph":false},{"content":"because a is 0.","startTime":2340690,"duration":1940,"startOfParagraph":false},{"content":"So now let's apply this formula.","startTime":2342630,"duration":2140,"startOfParagraph":false},{"content":"We have z represented by 25 + 6, which gives you 31.","startTime":2344770,"duration":6940,"startOfParagraph":false},{"content":"And 31 mod 26 gives you 5 as a remainder.","startTime":2351710,"duration":4080,"startOfParagraph":false},{"content":"That's perfect because we know that f is the 5th letter in the alphabet.","startTime":2355790,"duration":4710,"startOfParagraph":false},{"content":"But it still isn't f, right? It still isn't 102.","startTime":2360500,"duration":5900,"startOfParagraph":false},{"content":"So then for this pset, a challenge will be trying to find out the relationship","startTime":2366400,"duration":6330,"startOfParagraph":false},{"content":"between converting between these ASCII values and the alphabetical index.","startTime":2372730,"duration":4180,"startOfParagraph":false},{"content":"Essentially, what you'll want to do, you want to start out with the ASCII values,","startTime":2376910,"duration":3370,"startOfParagraph":false},{"content":"but then you want to somehow translate that into an alphabetical index","startTime":2380280,"duration":5110,"startOfParagraph":false},{"content":"then calculate what letter it should be--basically, what its alphabetical index is","startTime":2385390,"duration":7220,"startOfParagraph":false},{"content":"of the cipher character--then translate that back to the ASCII values.","startTime":2392610,"duration":5050,"startOfParagraph":false},{"content":"So if you whip out your ASCII table, then try and find relationships between, say, 102 and 5","startTime":2397660,"duration":7210,"startOfParagraph":false},{"content":"or the 122 and 25.","startTime":2404870,"duration":5570,"startOfParagraph":false},{"content":"We've gotten our key from the command line arguments, we've gotten the plaintext,","startTime":2412140,"duration":3550,"startOfParagraph":false},{"content":"we've enciphered it.","startTime":2415690,"duration":1830,"startOfParagraph":false},{"content":"Now all we have left to do is print it.","startTime":2417520,"duration":2300,"startOfParagraph":false},{"content":"We could do this a couple of different ways.","startTime":2419820,"duration":2220,"startOfParagraph":false},{"content":"What we could do is actually print as we go along.","startTime":2422040,"duration":2530,"startOfParagraph":false},{"content":"As we iterate over the characters in the string,","startTime":2424570,"duration":3680,"startOfParagraph":false},{"content":"we could simply just print right then when we calculate it.","startTime":2428250,"duration":3410,"startOfParagraph":false},{"content":"Alternatively, you could also store it in an array and have an array of characters","startTime":2431660,"duration":4370,"startOfParagraph":false},{"content":"and at the end iterate over that whole array and print it out.","startTime":2436030,"duration":3250,"startOfParagraph":false},{"content":"So you have a couple of options for that.","startTime":2439280,"duration":1700,"startOfParagraph":false},{"content":"And remember that %c is going to be the placeholder for printing a character.","startTime":2440980,"duration":6300,"startOfParagraph":false},{"content":"So there we have Caesar, and now we move on to Vigenere,","startTime":2447280,"duration":3140,"startOfParagraph":false},{"content":"which is very similar to Caesar but just slightly more complex.","startTime":2450420,"duration":7160,"startOfParagraph":false},{"content":"So essentially with Vigenere is you're going to be passing in a keyword.","startTime":2457580,"duration":5730,"startOfParagraph":false},{"content":"So instead of a number, you're going to have a string,","startTime":2463310,"duration":3200,"startOfParagraph":false},{"content":"and so that's going to act as your keyword.","startTime":2466510,"duration":2690,"startOfParagraph":false},{"content":"Then, as usual, you're going to get a prompt for a string from the user","startTime":2469200,"duration":5240,"startOfParagraph":false},{"content":"and then encipher it and then give them the ciphertext back.","startTime":2474440,"duration":4610,"startOfParagraph":false},{"content":"So as I said, it's very similar to Caesar, except instead of shifting by a certain number,","startTime":2479050,"duration":5600,"startOfParagraph":false},{"content":"the number is actually going to change every time from character to character.","startTime":2484650,"duration":5970,"startOfParagraph":false},{"content":"To represent that actual number to shift, it's represented by the keyboard letters.","startTime":2490620,"duration":4270,"startOfParagraph":false},{"content":"So if you enter in a shift of a, for instance, then that would correspond to a shift of 0.","startTime":2494890,"duration":8260,"startOfParagraph":false},{"content":"So it's again back to the alphabetical index.","startTime":2503150,"duration":2750,"startOfParagraph":false},{"content":"What might be useful if you're seeing that we're actually dealing with ASCII values","startTime":2505900,"duration":3200,"startOfParagraph":false},{"content":"as well as the letters, as well as the alphabetical index,","startTime":2509100,"duration":2690,"startOfParagraph":false},{"content":"maybe find or make your own ASCII table that shows the alphabetical index of 0 through 25,","startTime":2511790,"duration":6230,"startOfParagraph":false},{"content":"a through z, and the ASCII values so that you can kind of see the relationship","startTime":2518020,"duration":5730,"startOfParagraph":false},{"content":"and sketch out and try and find some patterns.","startTime":2523750,"duration":3270,"startOfParagraph":false},{"content":"Similarly, if you were shifting at the certain instance by f--","startTime":2527020,"duration":3990,"startOfParagraph":false},{"content":"and this is either lowercase or uppercase f--then that would correspond to 5.","startTime":2531010,"duration":10100,"startOfParagraph":false},{"content":"Are we good so far?","startTime":2541110,"duration":3070,"startOfParagraph":false},{"content":"The formula for Vigenere is a bit different.","startTime":2545770,"duration":4280,"startOfParagraph":false},{"content":"Basically, you see that it's just like Caesar,","startTime":2550050,"duration":2910,"startOfParagraph":false},{"content":"except instead of just k we have k index j.","startTime":2552960,"duration":4430,"startOfParagraph":false},{"content":"Notice that we're not using i because essentially, the length of the keyword","startTime":2557390,"duration":7420,"startOfParagraph":false},{"content":"isn't necessarily the length of our ciphertext.","startTime":2564810,"duration":5040,"startOfParagraph":false},{"content":"This will be a bit clearer when we see an example that I have a bit later on.","startTime":2569850,"duration":6280,"startOfParagraph":false},{"content":"Basically, if you run your program with a keyword of ohai,","startTime":2576130,"duration":7030,"startOfParagraph":false},{"content":"then that means that every time, ohai is going to be your shift.","startTime":2583160,"duration":5400,"startOfParagraph":false},{"content":"So depending on what position you are in your keyword,","startTime":2588560,"duration":2500,"startOfParagraph":false},{"content":"you're going to shift your certain ciphertext character by that amount.","startTime":2591060,"duration":4740,"startOfParagraph":false},{"content":"Again, just like Caesar, we want to make sure that we preserve the capitalization of things","startTime":2595800,"duration":3830,"startOfParagraph":false},{"content":"and we only encipher letters, not characters or spaces.","startTime":2599630,"duration":3270,"startOfParagraph":false},{"content":"So look back to Caesar on the functions that you may have used,","startTime":2602900,"duration":3430,"startOfParagraph":false},{"content":"the way that you decided how to shift things, and apply that to your program here.","startTime":2606330,"duration":6240,"startOfParagraph":false},{"content":"So let's map this out.","startTime":2612570,"duration":2690,"startOfParagraph":false},{"content":"We have a plaintext that we've gotten from the user from GetString","startTime":2615260,"duration":4420,"startOfParagraph":false},{"content":"saying This... is CS50!","startTime":2619680,"duration":4410,"startOfParagraph":false},{"content":"Then we have a keyword of ohai.","startTime":2624090,"duration":3000,"startOfParagraph":false},{"content":"The first 4 characters are pretty simple.","startTime":2627090,"duration":3840,"startOfParagraph":false},{"content":"We know that T is going to be shifted by o,","startTime":2630930,"duration":4650,"startOfParagraph":false},{"content":"then h is going to be shifted by h, i is going to be shifted by a.","startTime":2635580,"duration":6410,"startOfParagraph":false},{"content":"Here you see that a represents 0, ","startTime":2641990,"duration":2620,"startOfParagraph":false},{"content":"so then the end value is actually just the same letter as before.","startTime":2644610,"duration":7330,"startOfParagraph":false},{"content":"Then s is shifted by i.","startTime":2651940,"duration":3310,"startOfParagraph":false},{"content":"But then you have these periods here.","startTime":2655250,"duration":4120,"startOfParagraph":false},{"content":"We don't want to encipher that, so then we don't change it by anything","startTime":2659370,"duration":6590,"startOfParagraph":false},{"content":"and just print out the period unchanged.","startTime":2665960,"duration":5320,"startOfParagraph":false},{"content":"[student] I don't understand how you know that this is shifted by-- Where did you-- >>Oh, sorry.","startTime":2671280,"duration":6740,"startOfParagraph":false},{"content":"At the top here you see that the command line argument ohai here,","startTime":2678020,"duration":3600,"startOfParagraph":false},{"content":"that's going to be the keyword.","startTime":2681620,"duration":2120,"startOfParagraph":false},{"content":"And so basically, you're cycling over the characters in the keyword.","startTime":2683740,"duration":5810,"startOfParagraph":false},{"content":"[student] So o is going to be shifting the same--","startTime":2689550,"duration":2470,"startOfParagraph":false},{"content":"So o corresponds to a certain number in the alphabet.","startTime":2692020,"duration":4240,"startOfParagraph":false},{"content":"[student] Right. But where did you get the CS50 part from?","startTime":2696260,"duration":2140,"startOfParagraph":false},{"content":"Oh. That's in GetString where you're like, \"Give me a string to encode.\"","startTime":2698400,"duration":4140,"startOfParagraph":false},{"content":"[student] They're going to give you that argument to shift by","startTime":2702540,"duration":4970,"startOfParagraph":false},{"content":"and then you'll ask for your first string. >>Yeah.","startTime":2707510,"duration":1870,"startOfParagraph":false},{"content":"So when they run the program, they're going to include the keyword","startTime":2709380,"duration":3060,"startOfParagraph":false},{"content":"in their command line arguments when they run it.","startTime":2712440,"duration":2300,"startOfParagraph":false},{"content":"Then once you've checked that they've actually given you 1 and not more, not less,","startTime":2714740,"duration":5000,"startOfParagraph":false},{"content":"then you're going to prompt them for a string, say, \"Give me a string.\"","startTime":2719740,"duration":4010,"startOfParagraph":false},{"content":"So that's where in this case they've given you This... is CS50!","startTime":2723750,"duration":3880,"startOfParagraph":false},{"content":"So then you're going to use that and use ohai and iterate over.","startTime":2727630,"duration":4460,"startOfParagraph":false},{"content":"Notice that here we skipped over encrypting the periods,","startTime":2732090,"duration":6110,"startOfParagraph":false},{"content":"but in terms of our position for ohai, the next one we used o.","startTime":2738200,"duration":13460,"startOfParagraph":false},{"content":"In this case it's a bit harder to see because that's 4,","startTime":2751660,"duration":3330,"startOfParagraph":false},{"content":"so let's continue a bit. Just stick with me here.","startTime":2754990,"duration":2720,"startOfParagraph":false},{"content":"Then we have i and s, which are then translated by o and h respectively.","startTime":2757710,"duration":5250,"startOfParagraph":false},{"content":"Then we have a space, and so then we know that we aren't going to encipher the spaces.","startTime":2762960,"duration":6410,"startOfParagraph":false},{"content":"But notice that instead of going to a in this spot right here,","startTime":2769370,"duration":9560,"startOfParagraph":false},{"content":"we're encrypting by a--I don't know if you can see that--right here.","startTime":2778930,"duration":9400,"startOfParagraph":false},{"content":"So it's not like you actually predetermined, say, o goes here, h goes here,","startTime":2788330,"duration":5380,"startOfParagraph":false},{"content":"a goes here, i goes here, o, h, a, i, o, h, a, i. You don't do that.","startTime":2793710,"duration":5490,"startOfParagraph":false},{"content":"You only shift your position in the keyword ","startTime":2799200,"duration":4560,"startOfParagraph":false},{"content":"when you know that you're actually going to be encrypting an actual letter.","startTime":2803760,"duration":7260,"startOfParagraph":false},{"content":"Does that kind of make sense?","startTime":2811020,"duration":2900,"startOfParagraph":false},{"content":"Okay.","startTime":2813920,"duration":1880,"startOfParagraph":false},{"content":"So just some reminders.","startTime":2816490,"duration":2010,"startOfParagraph":false},{"content":"You want to make sure that you only advance to the next letter in your keyword","startTime":2818500,"duration":5260,"startOfParagraph":false},{"content":"if the character in your plaintext is a letter.","startTime":2823760,"duration":2630,"startOfParagraph":false},{"content":"So say we're at the o.","startTime":2826390,"duration":2730,"startOfParagraph":false},{"content":"We notice that the next character, the i index of the plaintext, is a number, for instance.","startTime":2829120,"duration":10190,"startOfParagraph":false},{"content":"Then we don't advance j, the index for our keyword, until we reach another letter.","startTime":2839310,"duration":12320,"startOfParagraph":false},{"content":"Again, you also want to make sure that you wraparound to the beginning of the keyword","startTime":2851630,"duration":4600,"startOfParagraph":false},{"content":"when you're at the end of it.","startTime":2856230,"duration":1540,"startOfParagraph":false},{"content":"If you see here we're at i, the next one has to be o.","startTime":2857770,"duration":4260,"startOfParagraph":false},{"content":"So you want to find some way of being able to wraparound to the beginning of your keyword","startTime":2862030,"duration":5660,"startOfParagraph":false},{"content":"every time that you reach the end.","startTime":2867690,"duration":1780,"startOfParagraph":false},{"content":"And so again, what kind of operator is useful in that case for wrapping around?","startTime":2869470,"duration":5570,"startOfParagraph":false},{"content":"Like in the counting off example.","startTime":2876630,"duration":3210,"startOfParagraph":false},{"content":"[student] The percent sign. >>Yeah, the percent sign, which is modulo.","startTime":2879840,"duration":3870,"startOfParagraph":false},{"content":"So modulo will come in handy here when you want to wrap over the index in your ohai.","startTime":2883710,"duration":7540,"startOfParagraph":false},{"content":"And just a quick hint: Try to think of wrapping over the keyword a bit like the counting off,","startTime":2891250,"duration":6450,"startOfParagraph":false},{"content":"where if there's 3 groups, the 4th person, ","startTime":2897700,"duration":5890,"startOfParagraph":false},{"content":"their number that they said was 4 mod 3, which was 1.","startTime":2903590,"duration":7020,"startOfParagraph":false},{"content":"So try and think of it that way.","startTime":2910610,"duration":2270,"startOfParagraph":false},{"content":"As you saw in the formula, wherever you have ci and then pi but then kj,","startTime":2914770,"duration":7970,"startOfParagraph":false},{"content":"you want to make sure that you keep track of those.","startTime":2922740,"duration":1960,"startOfParagraph":false},{"content":"You don't need to call it i, you don't need to call it j,","startTime":2924700,"duration":2880,"startOfParagraph":false},{"content":"but you want to make sure that you keep track of the position that you're at in your plaintext","startTime":2927580,"duration":5690,"startOfParagraph":false},{"content":"as well as the position that you're at in your keyword","startTime":2933270,"duration":2520,"startOfParagraph":false},{"content":"because those aren't necessarily going to be the same.","startTime":2935790,"duration":4050,"startOfParagraph":false},{"content":"Not only does the keyword--it could be a completely different length than your plaintext.","startTime":2939840,"duration":6560,"startOfParagraph":false},{"content":"Also, your plaintext, there are numbers and characters,","startTime":2946400,"duration":2740,"startOfParagraph":false},{"content":"so it's not going to perfectly match up together. Yes.","startTime":2949140,"duration":5310,"startOfParagraph":false},{"content":"[student] Is there a function to change case?","startTime":2954450,"duration":4830,"startOfParagraph":false},{"content":"Can you change a to capital A? >>Yeah, there definitely is.","startTime":2959280,"duration":5250,"startOfParagraph":false},{"content":"You can check out--I believe it's toupper, all 1 word.","startTime":2964530,"duration":3360,"startOfParagraph":false},{"content":"But when you're trying to cipher things and preserve the text,","startTime":2970650,"duration":5660,"startOfParagraph":false},{"content":"it's best basically to have separate cases.","startTime":2976310,"duration":3040,"startOfParagraph":false},{"content":"If it's an uppercase, then you want to shift by this","startTime":2979350,"duration":2690,"startOfParagraph":false},{"content":"because in your formula, when you look back how we have to kind of go ","startTime":2982040,"duration":4420,"startOfParagraph":false},{"content":"interchangeably between the ASCII way of representing the numbers","startTime":2986460,"duration":4440,"startOfParagraph":false},{"content":"and the actual alphabetical index, we want to make sure ","startTime":2990900,"duration":4120,"startOfParagraph":false},{"content":"there's going to be some kind of pattern that you're going to use.","startTime":2995020,"duration":6830,"startOfParagraph":false},{"content":"Another note on the pattern, actually.","startTime":3001850,"duration":2730,"startOfParagraph":false},{"content":"You're going to definitely be dealing with numbers.","startTime":3004580,"duration":2670,"startOfParagraph":false},{"content":"Try not to use magic numbers, which is an example of style.","startTime":3007250,"duration":4030,"startOfParagraph":false},{"content":"So say you want to every time shift something by like--","startTime":3011280,"duration":7190,"startOfParagraph":false},{"content":"Okay, so hint, another spoiler is when you're going to be shifting something ","startTime":3018470,"duration":3930,"startOfParagraph":false},{"content":"by a certain amount, try not to represent that by an actual number","startTime":3022400,"duration":3910,"startOfParagraph":false},{"content":"but rather try and see if you can use the ASCII value, which will kind of make more sense.","startTime":3026310,"duration":6500,"startOfParagraph":false},{"content":"Another note: Because we're dealing with formulas, ","startTime":3032810,"duration":2660,"startOfParagraph":false},{"content":"even though your TF will kind of know what pattern you might be using,","startTime":3035470,"duration":5730,"startOfParagraph":false},{"content":"best to in your comments kind of explain the logic, like,","startTime":3041200,"duration":3230,"startOfParagraph":false},{"content":"\"I'm using this pattern because...\" and kind of explain the pattern succinctly in your comments.","startTime":3044430,"duration":7450,"startOfParagraph":false},{"content":"[this was walkthrough 2] If there aren't any other questions, then I'll just stay here for a little bit.","startTime":3054090,"duration":4900,"startOfParagraph":false},{"content":"Good luck with your pset 2: Crypto and thanks for coming.","startTime":3058990,"duration":5380,"startOfParagraph":false},{"content":"[student] Thank you. >>Thanks.","startTime":3066070,"duration":2550,"startOfParagraph":false},{"content":"[Media Offline intro]","startTime":3069220,"duration":1580,"startOfParagraph":false}]}