WEBVTT

00:00.350 --> 00:06.260
All right, it's finally time to tackle our final project, the Caesar cipher.

00:06.320 --> 00:15.590
As I've mentioned before, Caesar cipher is this way of encoding text that was seen as early as during

00:15.590 --> 00:16.880
the times of Julius Caesar.

00:16.880 --> 00:24.560
So when he had these top secret military messages, what he would do is he would shift each letter of

00:24.560 --> 00:28.460
the alphabet by a certain predetermined amount.

00:28.490 --> 00:30.980
I want to show you quickly how this works.

00:30.980 --> 00:35.990
So we've got the alphabet here, and let's say we wanted to encode the letter E.

00:36.350 --> 00:40.910
So now we can line up the alphabet with a new set of alphabet.

00:40.910 --> 00:48.110
And at the moment the shift is zero because the top alphabet and the bottom alphabet are lined up with

00:48.110 --> 00:49.400
a zero difference.

00:49.430 --> 00:52.400
Now let's say that we had a shift of one.

00:52.400 --> 00:59.300
Well, then the alphabet moves to the left and A becomes B, B becomes C, etc. and we can keep going

00:59.350 --> 01:02.770
until we get to the amount of shift that we wanted.

01:02.770 --> 01:07.180
So let's say we're going to encode all of our text with the shift of three.

01:07.210 --> 01:13.750
We'll then E becomes h, f becomes I, g becomes J, etc. and so on and so forth.

01:13.750 --> 01:20.860
And they've actually discovered these artifacts from ages ago where people have created these sort of

01:20.890 --> 01:21.730
dials.

01:21.730 --> 01:29.020
And by simply rotating the dial to a certain amount of shift, then you can line up the letters with

01:29.020 --> 01:29.740
each other.

01:29.740 --> 01:34.570
So A becomes N and Z becomes O, and so on and so forth.

01:34.570 --> 01:38.320
And this is a way that people actually encoded top secret messages.

01:38.500 --> 01:45.280
By the end of this day, you have also built a digital form of the Caesar cipher.

01:45.370 --> 01:50.560
And all you have to do is to type encode to start encoding a message.

01:50.560 --> 01:53.590
Let's say something like hello World.

01:53.680 --> 01:59.820
And once we hit enter, we get to type the shift number, which I'm just going to choose a random one

01:59.820 --> 02:03.150
and it gives us the encoded result.

02:03.180 --> 02:11.160
So now if I take this encoded result and I go ahead and type yes to restart my program, and I'm going

02:11.190 --> 02:15.600
to use the decode function to decode my message.

02:16.080 --> 02:22.710
Now when I use the same shift number, then I should be able to get back the decoded result.

02:22.740 --> 02:27.540
So if there's people monitoring your phone or you're trying to throw a message to your friend in a paper

02:27.540 --> 02:34.710
ball, then this is an easy way to ensure that if your message was intercepted, then it won't be understood

02:34.740 --> 02:36.420
by the other person.

02:36.600 --> 02:41.070
All right, so once you're ready, let's get started building out the Caesar cipher.

02:41.100 --> 02:44.550
Now you'll notice that I've split it into three parts.

02:44.550 --> 02:47.880
So three, four, five are all the Caesar cipher.

02:47.880 --> 02:50.160
And that's because there's quite a few things to do.

02:50.160 --> 02:55.230
And I thought it might be a good idea to break it down into separate lessons so that we can go through

02:55.230 --> 02:59.660
them individually and hopefully it'll make this very large problem.

02:59.690 --> 03:00.860
A little bit easier.

03:00.890 --> 03:07.100
And we practice that key engineering skill, which is breaking down a large problem into smaller problems,

03:07.100 --> 03:11.180
and then taking a small problem, breaking it down into even smaller problems.

03:11.180 --> 03:15.680
That is the only way that we can get through really, really hard problems.

03:15.710 --> 03:16.280
All right.

03:16.280 --> 03:22.160
So you'll notice that in Caesar cipher one I've split the tasks into four to dos.

03:22.160 --> 03:25.460
You'll notice that order is a little bit weird because it goes four then three.

03:25.460 --> 03:30.500
But they're roughly at the places where I expect you to write the relevant code.

03:30.500 --> 03:32.330
So it's not a mistake.

03:32.330 --> 03:34.760
It's actually very much on purpose.

03:34.790 --> 03:40.580
Now, what I want you to do is to go through each of the to dos and read through their descriptions

03:40.580 --> 03:44.030
on the right hand pane and try to solve them.

03:44.030 --> 03:48.140
If you get stuck, there are some hints in there that will hopefully help you.

03:48.140 --> 03:52.670
But essentially the really important thing is you actually read through the to dos, because some of

03:52.670 --> 03:55.210
the things we might not have seen before.

03:55.390 --> 04:02.950
For example, in order to complete the task, you will need to use a built in function called index.

04:02.980 --> 04:11.080
And index is really handy because it allows us to find out the position of any item in a list.

04:11.080 --> 04:15.970
So if we had a list of fruits and we know that they start counting from zero.

04:15.970 --> 04:20.170
So apple is at position zero, pear is at one, orange is at two.

04:20.200 --> 04:24.160
Then we can say fruits, the name of the list dot index.

04:24.160 --> 04:29.740
And then in between the parentheses of this index function we pass in whatever it is we want to check.

04:29.740 --> 04:34.180
So in this case pear would give us a position of one.

04:34.180 --> 04:37.450
And we can verify this by printing it out.

04:37.450 --> 04:44.080
So I can show this to you in the Python console, which is basically something that's completely separate

04:44.080 --> 04:45.610
from the rest of our code.

04:45.610 --> 04:54.680
And if I go ahead and now print this value, you can see it gives me one, which is what we would expect.

04:54.680 --> 05:02.210
So this is going to be key for you to be able to solve this challenge, especially to do number two.

05:02.240 --> 05:04.280
So look through the description.

05:04.280 --> 05:05.840
It's there to help you.

05:05.840 --> 05:09.650
And hopefully you'll be able to tackle each of these to do's.

05:09.650 --> 05:13.310
And in order to figure out what it is that you're trying to aim for.

05:13.340 --> 05:19.070
As always, you can right click on solution and run it without viewing the actual solution.

05:19.070 --> 05:25.220
So we already have some pre-built in code which I've added in for you.

05:25.250 --> 05:32.270
I wanted to save you the tedious task of typing out all the 26 letters of the alphabet, as well as

05:32.390 --> 05:34.460
all the various inputs that we need.

05:34.460 --> 05:41.150
So we need the user to tell us whether, if they want to encode or decode a message, and then we want

05:41.150 --> 05:46.370
to actually ask them for the message, and then we want to ask them for the shift number.

05:46.370 --> 05:51.310
So what you want to do is by the time you have entered these three pieces of information.

05:51.340 --> 05:55.300
The next time I hit enter, it should give me a printed output.

05:55.330 --> 06:00.940
Here is the encoded result and it gives me the encoded output.

06:00.940 --> 06:02.620
So we're encoding.

06:02.620 --> 06:03.130
Hello.

06:03.130 --> 06:06.790
So h plus three letters I j k.

06:06.820 --> 06:12.640
So h becomes k e plus three letters is f g h.

06:12.640 --> 06:15.700
So this becomes h and so on and so forth.

06:15.700 --> 06:21.940
Until we complete the entire text that we are supposed to encode by the shift number.

06:21.970 --> 06:23.800
So that's all there is to it.

06:23.800 --> 06:29.800
Look through the to dos, look through the description and try to complete part one of Caesar cipher.

06:29.830 --> 06:35.350
I'll go quiet now and you can pause the video and tackle this challenge.

06:36.100 --> 06:37.480
Cths.

06:40.660 --> 06:41.260
All right.

06:41.290 --> 06:45.880
Hopefully you have completed this challenge in its entirety.

06:45.910 --> 06:51.240
If you haven't, I'm going to go through the solution together with you and hopefully by viewing the

06:51.240 --> 06:57.480
solution, some things will become more obvious and you will be able to go back and tackle the code

06:57.480 --> 06:58.590
by yourself.

06:58.590 --> 07:02.430
So the first step is to create a function called encrypt.

07:02.430 --> 07:08.580
I'm going to hide this right hand side pane just so that I can show you the to do in the entire line.

07:08.580 --> 07:14.580
So we create a function with def, and then the function is going to be called encrypt.

07:14.580 --> 07:17.250
And then we add a set of parentheses.

07:17.250 --> 07:21.030
And in this case we want to have two inputs.

07:21.030 --> 07:26.190
One is called original text and the other one is called shift amount.

07:26.190 --> 07:30.210
And we add those if you remember, into the parentheses.

07:30.210 --> 07:35.490
Finally we finish this function definition with a colon.

07:35.490 --> 07:39.570
And then we can write something inside the function.

07:39.570 --> 07:43.800
So to do number two is going to go inside the function.

07:43.800 --> 07:47.490
So I'm just going to bring it up here to make it easier to see.

07:48.110 --> 07:55.340
So inside the encrypt function we're going to shift each letter of the original text forwards in the

07:55.340 --> 07:59.990
alphabet by the shift amount and print the encrypted text.

07:59.990 --> 08:02.240
So there's a couple of things that we need to do.

08:02.240 --> 08:05.060
So we're going to do them in several steps.

08:05.630 --> 08:12.470
And the first step is to loop through each of the letters in this text.

08:12.470 --> 08:14.900
So we can do that using a for loop.

08:14.900 --> 08:27.170
So for letter in the original text we're going to go ahead and figure out what is the shifted position

08:27.170 --> 08:28.460
for that letter.

08:28.460 --> 08:35.360
So if we take the current alphabet and we use dot index.

08:35.360 --> 08:40.130
And inside here we pass in the letter that we're currently looping through.

08:40.130 --> 08:43.400
So let's start with a concrete example.

08:43.400 --> 08:46.390
That way we can go through it line by line.

08:46.390 --> 08:51.640
So if the original text was hello, and if the shift amount.

08:51.670 --> 08:57.430
Was two, then the letter that we first loop through is h.

08:57.430 --> 09:04.720
So h is going to be checked against this list called alphabet for its index.

09:04.720 --> 09:11.860
So h is at position 01234567.

09:11.860 --> 09:15.340
So h is at position seven right.

09:15.340 --> 09:18.280
And this becomes seven.

09:18.550 --> 09:25.000
And once we've gotten this number what we want to do is we want to shift it upwards.

09:25.000 --> 09:25.540
Right.

09:25.570 --> 09:30.640
And we want to shift it by the shift amount, which in our case we said was going to be two.

09:30.670 --> 09:33.640
So h plus two is I j.

09:33.640 --> 09:35.170
So it becomes j.

09:35.170 --> 09:38.140
So how do we make it go to that position.

09:38.140 --> 09:44.290
Well we take this index seven and we add the shift amount to it.

09:44.310 --> 09:52.020
And then this means seven, then becomes nine, seven plus two becomes nine, and position nine is the

09:52.020 --> 09:54.270
letter J, which is what we want.

09:54.270 --> 10:00.150
So how can we turn this into the actual letter?

10:00.180 --> 10:07.770
Well, firstly we need to probably save this shifted position into a variable so that we can use it

10:07.770 --> 10:09.210
on the next line.

10:09.210 --> 10:19.110
And what we're going to do is we're going to create a letter by using the alphabet list, using the

10:19.110 --> 10:24.120
square brackets, and tapping to the item at this shifted position.

10:24.270 --> 10:26.640
Now what do we do with this letter.

10:26.640 --> 10:28.890
We actually need to accumulate it.

10:28.890 --> 10:36.270
So in order to accumulate it we need to have some sort of empty string, which I will call ciphertext.

10:36.300 --> 10:39.930
We started out as just an empty string with nothing in it.

10:39.930 --> 10:45.710
And as always, it's very important that it's outside the for loop because we don't want it to reset

10:45.740 --> 10:48.530
each time if it is inside the for loop.

10:48.530 --> 10:56.720
So now going inside the for loop so indented inside here I'm going to set the ciphertext to plus equal

10:56.720 --> 11:02.330
this new letter that we've found by this shifting process.

11:02.330 --> 11:09.350
So at each of these steps I recommend printing out the values that you've got just to understand what

11:09.350 --> 11:11.480
is going on behind the scenes.

11:11.480 --> 11:16.310
So at this point we've now got the letter J from this.

11:16.310 --> 11:23.030
And this J is being added to the ciphertext as the first value in there.

11:23.030 --> 11:26.570
So it should look something like this.

11:27.290 --> 11:36.050
Now the next step is to go ahead and for this loop to continue to the next cycle.

11:36.050 --> 11:38.150
So we've now done H.

11:38.180 --> 11:44.110
It's going to go to the next letter in the original text, which is E, and it's going to repeat this

11:44.110 --> 11:50.020
process until we end up with all of the letters of the ciphertext.

11:50.110 --> 11:54.340
And once it's done that, then we want to print it out.

11:54.340 --> 11:57.640
So making sure that we are outside of the for loop.

11:57.640 --> 12:04.330
So once the for loop has completed then we will go ahead and make a print statement.

12:04.360 --> 12:08.440
And what we want to print is something like this.

12:08.440 --> 12:11.680
Here is the encoded result.

12:11.710 --> 12:20.620
Now if I go ahead and turn this into an F string then I can replace this with my ciphertext.

12:20.620 --> 12:29.620
And now all I need to do is to actually call this function so that it gets triggered and it passes in

12:29.620 --> 12:33.010
all of the user inputs from up here.

12:33.040 --> 12:38.890
Now remember with functions, just defining it and running your code is not going to do anything.

12:38.890 --> 12:42.420
It's just going to sit there until it is called upon.

12:42.420 --> 12:47.160
So let's complete to do number three and complete this process.

12:47.160 --> 12:52.650
So let's call this function encrypt and pass in the user inputs.

12:52.650 --> 12:58.050
So I'm going to use a keyword arguments just to make it a little bit more clear.

12:58.050 --> 13:04.860
So I'm going to say original text is equal to the text that was entered by the user.

13:05.070 --> 13:12.780
And then the next value that I need to add in is the shift amount.

13:12.780 --> 13:16.320
So here let's go ahead and add the shift amount.

13:16.320 --> 13:21.630
And that's going to be set equal to whatever the user typed in here as an input.

13:22.020 --> 13:23.850
So it's called shift.

13:23.880 --> 13:31.530
And now I can run this file and go ahead and go through the process of encoding the word hello.

13:31.530 --> 13:34.020
And I'm going to shift it by two again.

13:34.020 --> 13:37.530
So now you can see the result is printed out.

13:37.550 --> 13:41.780
And the first letter indeed does start with J.

13:42.470 --> 13:47.000
And then it continues working out all of the other letters.

13:47.030 --> 13:53.990
And you can see that there's two ends, because there are two L's in the original text.

13:53.990 --> 13:57.290
So that all seems to be working just fine.

13:57.320 --> 14:02.540
Now there is one slight problem which is highlighted by this to do number four.

14:02.570 --> 14:08.570
What happens if you try to shift the letter Z forwards by nine?

14:08.600 --> 14:10.250
Well let's try it.

14:10.250 --> 14:14.210
Let's encode and we're going to encode just the letter z.

14:14.240 --> 14:16.040
We're going to shift it by nine.

14:16.070 --> 14:22.970
Well we get a classic error index error list index out of range.

14:22.970 --> 14:24.890
So what's going on here.

14:24.890 --> 14:32.510
Well if we take a look at our alphabet we know that Z has position 25.

14:32.540 --> 14:35.450
And I know that because there's 26 letters in the alphabet.

14:35.480 --> 14:39.430
If we start from zero, then Z is at 25.

14:39.460 --> 14:43.210
So now we've got that position.

14:43.210 --> 14:44.800
This is 25.

14:44.800 --> 14:46.930
And then we add a shift amount.

14:46.960 --> 14:48.790
We try to add nine to it.

14:48.790 --> 14:53.080
So nine plus 25 is 34.

14:53.110 --> 15:00.610
And then we're going to try and take 34 as the shifted position to go into the alphabet.

15:00.640 --> 15:07.480
Now of course that is going to give us a index out of range error because there is nothing at position

15:07.510 --> 15:08.110
34.

15:08.140 --> 15:10.450
It ends at 25.

15:10.450 --> 15:14.050
So how can we solve this problem?

15:14.050 --> 15:21.430
Well, if you think about this problem, there's actually many, many ways that you can solve it.

15:21.430 --> 15:25.540
Um, some are easier and some are more difficult.

15:25.540 --> 15:34.880
But if you think back to when we learned about the modulo, you might remember that we can use the modulo

15:34.880 --> 15:36.890
to get the remainder.

15:36.950 --> 15:40.850
So let's go into the Python console again and let me show you.

15:40.880 --> 15:52.010
So if we had the number four and I tried to modulo it or divide it by 25, it's going to give me four

15:52.010 --> 15:56.180
because four does not divide by 25.

15:56.180 --> 16:01.280
So the remainder is what we had at the beginning, which is four.

16:01.310 --> 16:04.850
Now let's see what happens if we go over 25.

16:04.850 --> 16:11.990
Let's say that we had 34 and we tried to modulo it by 25.

16:12.020 --> 16:16.220
Well what's going to happen here is 34 divided by 25.

16:16.250 --> 16:19.670
There is one set of 25 within 34.

16:19.670 --> 16:22.190
And then it's going to give us the remainder.

16:22.190 --> 16:25.580
So that means it's going to give us nine.

16:25.610 --> 16:32.920
And this is really really useful because in our case this is exactly the behavior we want.

16:32.920 --> 16:41.080
If we go outside the range just by modulating by the length of the alphabet, we can make sure that

16:41.080 --> 16:51.490
no matter how far outside of the range of the list, we're always going to adjust it to within the length

16:51.490 --> 16:54.190
from 0 to 25.

16:54.220 --> 17:05.380
So to do this, all we need to do is take this shifted position and we're going to set it to equal the

17:05.380 --> 17:10.450
previous version of shifted position modulo by the length.

17:10.450 --> 17:14.650
So the number of items in our list called alphabet.

17:14.680 --> 17:23.200
Now just as what you can do with plus equals where you take the ciphertext and add this alphabet to

17:23.230 --> 17:27.700
it and then set it as the new value, you can do the same with modulo.

17:27.700 --> 17:34.410
So we can actually change this to modulo equals to make our code even more short.

17:34.740 --> 17:44.880
So shifted position modulo equals the length of the alphabet is going to make sure that we are always

17:44.880 --> 17:48.480
within the range of 0 to 25.

17:48.660 --> 17:53.070
And this is going to prevent that problem that we had earlier.

17:53.070 --> 18:02.100
So if I go ahead and run the code and type Z and I shift it up by nine, you'll see that it's performing

18:02.100 --> 18:09.210
the correct implementation, which is taking it all the way around and starting over here.

18:09.210 --> 18:15.990
So Z plus nine is 123456789.

18:15.990 --> 18:19.500
And it should as expected give us I.

18:19.530 --> 18:21.840
Now this can be quite confusing.

18:21.840 --> 18:29.840
This idea of the modulo um because it does involve a little bit of maths, but it's nothing that you

18:29.870 --> 18:30.800
can't understand.

18:30.800 --> 18:37.190
If you just try out some principles, try out some different letters, try out some different numbers,

18:37.190 --> 18:42.740
experiment with it, and I'm sure that you'll eventually understand what's going on.

18:42.800 --> 18:47.240
Don't move on until you actually understand what the code does.

18:47.270 --> 18:55.010
Alternatively, you could have tried some of the other methods that I gave in the hint, and maybe one

18:55.010 --> 19:00.590
of the other ones would be a little bit easier to understand for you, but I think this one should be

19:00.590 --> 19:03.740
doable by most of the students.

19:03.740 --> 19:08.330
As long as you invest a bit of time understanding how it works.

19:08.420 --> 19:12.020
So that concludes part one of the Caesar cipher.

19:12.020 --> 19:16.970
And we've got a functioning cipher that can now encode messages.

19:16.970 --> 19:24.020
In the next part we're going to tackle how to decode messages and how to combine the encryption and

19:24.020 --> 19:26.270
decryption functionalities together.

19:26.270 --> 19:28.670
So for all of that and more I'll see you there.
