WEBVTT

00:00.410 --> 00:07.280
All right, so now that we've managed to get our program to encode some text, it's time to do the opposite

00:07.280 --> 00:13.520
direction, which is how do we get our program to be able to decode a piece of text based on a shift

00:13.520 --> 00:14.150
number?

00:14.180 --> 00:17.360
So let's say that we try to decode the letter G.

00:17.600 --> 00:20.390
And we had a shift number of three.

00:20.420 --> 00:27.740
Well in this case we actually have to work backwards and shift backwards in the alphabet in order to

00:27.770 --> 00:29.150
get the letter.

00:29.150 --> 00:32.180
That's three spaces previously.

00:32.210 --> 00:38.990
So go ahead and open up the step two of our Caesar cipher project.

00:39.020 --> 00:46.430
Now we've got all the code from the previous lesson, and we now have to complete three more to dos

00:46.430 --> 00:51.020
to complete the decryption part of our program.

00:51.200 --> 00:58.520
So the three to dos are you're going to create a function called decrypt that takes also two inputs.

00:58.550 --> 01:06.270
Then inside the decrypt function, you're going to write some code to shift each letter of the original

01:06.270 --> 01:13.560
text backwards in the alphabet, so that we decode it to the original text, and then we print out the

01:13.560 --> 01:15.030
decrypted text.

01:15.210 --> 01:23.430
Finally, we're going to figure out how to combine the encrypt and decrypt functions into a single function.

01:23.430 --> 01:28.890
And then within the function itself, write some conditional statements to figure out whether if the

01:28.890 --> 01:37.680
user wants to encrypt or decrypt using this direction variable, and then we end up with a single function

01:37.680 --> 01:41.190
rather than two functions encrypt and decrypt.

01:41.190 --> 01:48.150
So I recommend to do these to DOS in order and not to go straight towards to do number three, because

01:48.150 --> 01:54.120
it's quite important that you understand how the decryption works and how it differs from encryption

01:54.120 --> 01:56.730
before we move on to combining them.

01:56.730 --> 02:03.150
So there are some hints in the description box as well, and there's some more details in the description

02:03.150 --> 02:05.490
for you to read once you're ready.

02:05.520 --> 02:11.760
Pause the video and I'll go silent and wait for you to complete this exercise.

02:17.790 --> 02:22.140
All right, hopefully that went really well for you and you managed to figure it all out.

02:22.170 --> 02:24.360
It's really, really hard.

02:24.360 --> 02:31.050
So all of these projects are really hard because they don't just rely on what you've learned in class.

02:31.050 --> 02:37.200
They also rely a lot of thinking, a lot of thinking outside the box, a lot of googling, a lot of

02:37.200 --> 02:45.660
figuring things out, and also that key skill of engineering, which is breaking down problems to small

02:45.660 --> 02:47.550
enough, a problem that you can solve.

02:47.580 --> 02:54.090
So there is no way that you would be able to just breeze through any of these projects.

02:54.090 --> 02:57.110
So don't feel bad if that's not happening.

02:57.170 --> 03:02.990
But now I want to go through the solution with you in case you wanted to check your solution.

03:02.990 --> 03:07.400
Or you need a couple of more hints to figure out what's going on.

03:07.430 --> 03:11.900
So the first thing we're going to do is to create a function called decrypt.

03:11.930 --> 03:20.660
And this function is again going to take two inputs the original text and also the shift amount.

03:21.470 --> 03:30.170
So now that we're passing in these two variables inside the decrypt function we're going to shift each

03:30.170 --> 03:39.650
of these letters by the amount that is required based on whatever it is the user wanted.

03:39.680 --> 03:43.250
And then we're going to shift it backwards in the alphabet.

03:43.250 --> 03:49.610
So if the original letter was F and their shift amount is two becomes H.

03:49.640 --> 03:53.960
So now they're giving us h as the letter to decode.

03:53.960 --> 03:59.270
Then we take the shift amount and we subtract two and to get back to the original letter.

03:59.270 --> 04:05.990
So most of it is pretty much exactly the same as what happened in our encrypt.

04:05.990 --> 04:12.080
But the only difference between decrypt and encrypt is the shift direction.

04:12.080 --> 04:19.040
Because instead of taking the position of the current letter in the alphabet and shifting it up by adding

04:19.040 --> 04:25.460
the shift amount, what we want to do is we want to subtract it by the shift amount so that instead

04:25.460 --> 04:31.010
of going in this direction, we're now shifting it down this direction.

04:31.010 --> 04:35.570
And the rest of the code is pretty much exactly the same.

04:35.570 --> 04:39.650
Let's walk through them line by line, just so that we revise what's happening.

04:39.680 --> 04:44.390
Firstly, we're looping through each letter in the text to decode.

04:44.390 --> 04:47.060
We're working out the shifted position.

04:47.060 --> 04:52.970
So the position of the new letter, by taking the current letter's position and subtracting it by the

04:52.970 --> 04:53.900
shift amount.

04:53.930 --> 05:01.070
Once we've got that shift to position, we're using the modulo to divide that position by the length

05:01.070 --> 05:03.830
of the alphabet, which is 26.

05:03.860 --> 05:05.060
26 letters.

05:05.060 --> 05:11.480
And what that means is we're always going to be within that range of 0 to 25.

05:11.480 --> 05:16.220
And we're never going to go out of range of our list, which is called alphabet.

05:16.280 --> 05:23.180
Finally, we take our empty ciphertext and we add each of these letters as we loop through each of the

05:23.180 --> 05:28.100
letters in the original text, and we end up with the final ciphertext.

05:29.000 --> 05:35.630
Now, even though ciphertext is what worked for us previously in encrypt, it's probably not the best

05:35.630 --> 05:44.510
wording in this case, because the ciphertext is the result that we get after we encrypt some plaintext.

05:44.510 --> 05:48.230
So let's just change this to output text.

05:50.360 --> 05:56.490
And we need to change it all the places that we use it, and now we're ready to go ahead and test our

05:56.490 --> 05:57.870
decrypt function.

05:57.870 --> 06:06.240
So let's go ahead and call decrypt and pass in the original text, which is going to be whatever the

06:06.240 --> 06:09.420
user added to this input called text.

06:09.450 --> 06:16.710
And then we also add the shift amount based on what the user typed for the shift.

06:17.310 --> 06:20.940
So now let's go ahead and run our code.

06:20.970 --> 06:24.300
And this part is still not considered.

06:24.300 --> 06:26.310
So it doesn't actually matter what you type.

06:26.310 --> 06:29.280
And then we're going to check just one letter.

06:29.280 --> 06:37.680
So let's say that I have the letter A and I want to shift it down by two.

06:37.710 --> 06:43.620
So remember even though we can type, encode or decode, the only function that's being called right

06:43.620 --> 06:45.180
now in our code is decrypt.

06:45.180 --> 06:50.280
So it should be taking my letter and shifting it down by two.

06:50.310 --> 06:56.980
So if A is right here, then shifting it backwards will take it to Z and then to Y.

06:57.010 --> 06:59.590
So now let's go ahead and hit enter.

06:59.590 --> 07:04.810
And you can see that my decryption is working as expected.

07:05.410 --> 07:13.180
So now all that we have left to do is to combine the encrypt and decrypt functions together into a single

07:13.180 --> 07:13.690
one.

07:13.690 --> 07:18.010
Because as you can see, they are extremely repetitive.

07:18.040 --> 07:24.100
They're so repetitive that I was able to copy and paste the encrypt function into the decrypt function

07:24.100 --> 07:26.500
to get the same functionality.

07:26.530 --> 07:31.870
So let's go ahead and create this new function called Caesar.

07:32.530 --> 07:40.450
And this function is going to take the original text as an input and also the shift amount as the input.

07:40.450 --> 07:41.920
So same as before.

07:41.920 --> 07:48.790
But in addition I'm also going to add the directionality into this function.

07:48.820 --> 07:54.760
Now instead of calling it direction, which will be a little bit confusing because am I referring to

07:54.790 --> 08:00.130
the direction variable here, or am I referring to the direction variable here?

08:00.130 --> 08:05.680
And you can see as soon as I write that PyCharm is already giving me a warning, telling me that it

08:05.680 --> 08:12.340
shadows the name direction from the outer scope, which means you're trying to create a new variable

08:12.340 --> 08:18.670
name that's only going to be used inside your function, but you've already got this variable name declared

08:18.670 --> 08:19.600
outside.

08:19.630 --> 08:21.430
Now this will still work.

08:21.430 --> 08:25.180
By the way, this is valid code, but it's just confusing code.

08:25.180 --> 08:26.380
So let's change it.

08:26.380 --> 08:30.190
Let's call this variable encode or decode.

08:30.220 --> 08:32.380
It's pretty easy to understand right?

08:32.410 --> 08:36.190
We're trying to figure out do we encode or do we decode in this function.

08:36.190 --> 08:43.540
So I'm going to copy everything I had from the decrypt function and paste it inside my Caesar function.

08:43.540 --> 08:53.390
And now what I want to do is to change it so that whether if the user wanted encode or decode, I will

08:53.390 --> 09:00.920
make this function do the exact thing that they want and encode and decode the text.

09:00.950 --> 09:03.410
So what is the difference?

09:03.410 --> 09:06.770
Well, the only difference really is this line right here.

09:06.800 --> 09:14.060
Either we subtract the shift amount when we're decoding or we add the shift amount when we're encoding.

09:14.060 --> 09:22.430
So in maths, as you might have seen in the little hint I had here in maths, when you multiply a number

09:22.430 --> 09:26.030
by minus one you will reverse its sign.

09:26.030 --> 09:32.720
So positive five can become negative five by simply multiplying it by minus one.

09:32.750 --> 09:38.930
And then if we add a negative number, that's the same as subtracting it.

09:38.930 --> 09:45.470
So this might be a little bit of a mathematical knowledge that you may or may not know or may or may

09:45.470 --> 09:52.950
not remember, as in my case, but hopefully with that hint, you could work out that in order to have

09:52.950 --> 10:01.410
this line of code work, both during encoding and decoding, all we need to do is to change that shift

10:01.410 --> 10:06.690
amount based on the encode or decode variable.

10:06.720 --> 10:17.130
So if encode or decode is equal to decode well in this case we're going to take the shift amount and

10:17.130 --> 10:22.110
we're going to multiply equals it by minus one.

10:22.110 --> 10:28.290
So this is going to change this value to the opposite sign.

10:28.290 --> 10:32.310
So if it was positive it becomes negative.

10:32.310 --> 10:35.340
And that's what we want when we're decoding.

10:35.580 --> 10:40.290
Now in this case instead of subtracting we can simply add.

10:40.320 --> 10:48.300
So whatever it is when we add a negative number such as in the case of decoding, it's the same UVs

10:48.330 --> 10:57.810
as subtraction, and when it is in code, then this if statement is not going to trigger and we have

10:57.810 --> 11:03.300
the original code that we have for the encrypt function.

11:03.300 --> 11:07.500
So this is a really neat way that you can get around this problem.

11:10.440 --> 11:18.930
So now all that's left to do is to actually call this Caesar function instead of encrypt or decrypt.

11:18.930 --> 11:29.040
So let's clear the code a little bit to make this file a little less confusing, so that we only have

11:29.040 --> 11:31.560
our Caesar function.

11:31.560 --> 11:39.990
And now I'm going to call this function Caesar and pass in the three variables that it expects original

11:40.020 --> 11:42.600
text shift amount, encode or decode.

11:42.600 --> 11:53.320
So text is the name that we had for the input of the message, and then the shift is the variable we

11:53.320 --> 11:57.130
had for the shift amount input.

11:57.220 --> 12:01.300
And finally we also have the direction.

12:01.300 --> 12:07.330
So this is of course a positional argument way of calling this function.

12:07.330 --> 12:17.260
But if you want it to be more clear then you can of course add in the keyword arguments rthy.

12:19.060 --> 12:22.570
So now let's go ahead and run our code to see if it works.

12:22.570 --> 12:26.470
So I'm first going to type encode to encode my message.

12:26.470 --> 12:31.720
And I'm going to encode just a single letter to make our lives a little bit more easy.

12:31.780 --> 12:35.770
So now I want to encode which means shifting it up.

12:35.770 --> 12:37.360
And I want to shift it up by two.

12:37.390 --> 12:39.340
So a then b, then C.

12:39.340 --> 12:42.910
So hopefully I get the result c.

12:42.920 --> 12:48.080
Now let's go ahead and rerun this, um, code.

12:48.080 --> 12:50.660
So go ahead and run it again.

12:50.660 --> 12:53.360
And this time I'm going to say decode.

12:53.360 --> 12:55.880
So again I'm going to use the same letter A.

12:55.880 --> 12:57.740
And I want to shift it by two.

12:57.770 --> 13:06.380
And this time instead of getting C, I now have y because our code is working as expected, and we only

13:06.380 --> 13:12.110
have a single function that achieves all of that functionality.

13:12.110 --> 13:19.460
Now there's just one last thing that you might be bothered by if you have a little bit of OCD like me,

13:19.490 --> 13:25.940
I don't like the fact that this prints out here is the encoded result, whether if it's decode or encode.

13:25.940 --> 13:32.030
So how can we get it to say here is the encoded result when it is encoded.

13:32.030 --> 13:35.840
And here is the decoded result when it is decode.

13:35.840 --> 13:43.550
Well there's a really neat trick here because notice that the word encoded and decode it in English.

13:43.580 --> 13:51.830
Actually, both have the word encode and decode, and the only thing that they have in common is the

13:51.860 --> 13:54.980
D to represent the fact that it happened in the past.

13:54.980 --> 14:05.810
So for us, it's as simple as simply taking this part, adding in the encode or decode variable, adding

14:05.840 --> 14:08.420
D at the end no matter which one it is.

14:08.450 --> 14:19.070
And this f string will now print out encoded result if it is encoded, and it will print out decoded

14:19.070 --> 14:23.000
result when it is decoded.

14:24.470 --> 14:30.980
So hopefully you manage to understand what's going on in the explanation or you already got it by yourself

14:30.980 --> 14:33.260
anyways and you're just watching to check.

14:33.260 --> 14:39.470
But once you're ready, we're going to move on to the next step and complete our Caesar cipher program.

14:39.470 --> 14:41.750
So for all of that and more, I'll see you there.
