1
00:00:00,520 --> 00:00:04,480
All right. It's finally time to tackle our final project,

2
00:00:04,540 --> 00:00:07,930
the Caesar Cipher. As I mentioned before,

3
00:00:08,109 --> 00:00:13,060
Caesar Cipher is this way of encoding text that,

4
00:00:13,090 --> 00:00:16,870
um, was seen as early as during the times of Julius Caesar.

5
00:00:17,110 --> 00:00:20,170
So when he had these top secret military messages,

6
00:00:20,560 --> 00:00:25,560
what he would do is he would shift each letter of the alphabet by a certain

7
00:00:26,830 --> 00:00:30,790
predetermined amount. I want to show you quickly how this works.

8
00:00:31,270 --> 00:00:36,270
So we've got the alphabet here and let's say we wanted to encode the letter E. So

9
00:00:36,970 --> 00:00:40,750
now we can line up the alphabet with a new set of alphabet.

10
00:00:41,260 --> 00:00:42,430
And at the moment,

11
00:00:42,490 --> 00:00:47,490
the shift is zero because the top alphabet and the bottom alphabet are lined up

12
00:00:47,860 --> 00:00:52,840
with a zero difference. Now, let's say that we had a shift of one. Well,

13
00:00:52,840 --> 00:00:57,700
then the alphabet moves to the left and A becomes B, B become C et cetera.

14
00:00:58,090 --> 00:01:02,710
And we can keep going until we get to the amount of shift that we wanted.

15
00:01:03,010 --> 00:01:07,060
So let's say we're going to encode all of our text with the shift of three.

16
00:01:07,480 --> 00:01:11,980
Well, then E becomes H, F becomes I, G becomes J et cetera,

17
00:01:12,250 --> 00:01:13,660
and so on and so forth.

18
00:01:14,020 --> 00:01:17,920
And they've actually discovered these artifacts from ages ago

19
00:01:17,920 --> 00:01:21,580
where people have created these sort of dials.

20
00:01:22,060 --> 00:01:27,060
And by simply rotating the dial to a certain amount of shift,

21
00:01:27,400 --> 00:01:29,620
then you can line up the letters with each other.

22
00:01:29,950 --> 00:01:34,510
So A becomes N and Z becomes O and so on and so forth.

23
00:01:34,750 --> 00:01:38,050
And this is a way that people actually encoded top secret messages.

24
00:01:38,830 --> 00:01:40,750
By the end of this day,

25
00:01:40,960 --> 00:01:45,070
you will have also built a digital form of the Caesar cipher.

26
00:01:45,730 --> 00:01:50,440
And all you have to do is to type encode to start encoding a message.

27
00:01:50,830 --> 00:01:53,380
Let's say something like Hello World.

28
00:01:54,130 --> 00:01:57,580
And once we hit enter, we get to type the shift number,

29
00:01:57,910 --> 00:01:59,710
which I'm just gonna choose a random one,

30
00:02:00,100 --> 00:02:02,980
and it gives us the encoded result.

31
00:02:03,460 --> 00:02:08,460
So now if I take this encoded results and I go ahead and type yes,

32
00:02:08,800 --> 00:02:10,570
to restart my program,

33
00:02:10,870 --> 00:02:15,340
and I'm going to use the decode function to decode my message.

34
00:02:16,180 --> 00:02:18,820
Now, when I use the same shift number,

35
00:02:19,450 --> 00:02:22,600
then I should be able to get back the decoded result.

36
00:02:23,050 --> 00:02:26,470
So if there's people monitoring your phone, or you're trying to throw a message to

37
00:02:26,470 --> 00:02:28,000
your friend in a paper ball,

38
00:02:28,330 --> 00:02:33,330
then this is an easy way to ensure that if your message was intercepted then it

39
00:02:33,430 --> 00:02:37,480
won't be understood by the other person. Once you're ready,

40
00:02:37,510 --> 00:02:42,510
let's get started by heading over to repl.it/@appbrewery/caesar

41
00:02:42,940 --> 00:02:47,320
-cipher-1-start. And this contains the starting code for this project

42
00:02:47,710 --> 00:02:49,480
and this is part one of four.

43
00:02:49,960 --> 00:02:54,520
So here I've already saved you the laborious task of typing out all the letters

44
00:02:54,520 --> 00:02:57,790
in the alphabet and they're stores in a single list.

45
00:02:58,390 --> 00:03:01,600
Now I've also got three inputs.

46
00:03:01,750 --> 00:03:03,820
So this is what the user is going to type in.

47
00:03:03,850 --> 00:03:06,610
They're gonna type in encode or decode,

48
00:03:06,760 --> 00:03:10,630
and that's gonna be saved to the direction. Tey're going to type them message

49
00:03:10,660 --> 00:03:12,730
which is going to be changed to lower case

50
00:03:13,090 --> 00:03:15,850
and this is going to be saved in text, and finally,

51
00:03:15,850 --> 00:03:17,800
they're going to input a shift number

52
00:03:18,130 --> 00:03:22,180
which is going to be converted to an integer and saved inside this variable

53
00:03:22,180 --> 00:03:27,040
called shift. So if you want to, have a run of the program as it is right now,

54
00:03:27,280 --> 00:03:30,970
so that you fully understand what's actually going on before you get started.

55
00:03:31,390 --> 00:03:33,880
And remember, don't change any of the code above,

56
00:03:34,210 --> 00:03:37,150
but instead tackle the to-dos one by one.

57
00:03:37,750 --> 00:03:40,330
The first step is to create a function called encrypt

58
00:03:40,570 --> 00:03:45,430
that's going to take this text that the user typed in and the shift that they

59
00:03:45,430 --> 00:03:47,260
typed in as inputs.

60
00:03:48,100 --> 00:03:51,310
And this is going to use what you learned about functions with inputs,

61
00:03:51,340 --> 00:03:52,780
from today's lessons.

62
00:03:53,530 --> 00:03:58,000
And then once you're done with that, step 2 is to go inside the encrypt

63
00:03:58,000 --> 00:04:03,000
function that you just created and shift each letter of the text forwards by the

64
00:04:05,650 --> 00:04:09,870
shift amount. So, again, as we saw before a shift of one,

65
00:04:09,880 --> 00:04:11,860
a shifter of two, a shift of three,

66
00:04:12,100 --> 00:04:15,310
basically just moves the characters forward

67
00:04:15,340 --> 00:04:19,060
so that each letter of the alphabet becomes a letter of the alphabet much

68
00:04:19,089 --> 00:04:23,830
further down the line. Let's say that the user entered the text

69
00:04:23,860 --> 00:04:27,370
Hello and if we tried a shift of five,

70
00:04:27,640 --> 00:04:31,000
then the end result should be mjqqt

71
00:04:31,450 --> 00:04:35,530
and the printed output from this function should say something like the encoded

72
00:04:35,530 --> 00:04:38,770
text is this. And finally,

73
00:04:38,770 --> 00:04:42,880
you're going to call the encrypt function and then pass in the user inputs from

74
00:04:42,880 --> 00:04:43,713
over here

75
00:04:43,900 --> 00:04:48,190
and you should be able to test the code and encrypt a message and see something

76
00:04:48,190 --> 00:04:52,480
like this happen. Pause the video now, have a look through the starting code,

77
00:04:52,720 --> 00:04:53,830
and once you're ready,

78
00:04:53,980 --> 00:04:57,250
go ahead and tackle it and then come back and we'll go through the solution

79
00:04:57,250 --> 00:05:02,200
together. Good luck. All right.

80
00:05:02,200 --> 00:05:07,200
So I'm gonna fork a copy of the starting file and tackle to-do number one,

81
00:05:07,930 --> 00:05:12,930
which is to create a function which is done using Def and it's called encrypt.

82
00:05:14,020 --> 00:05:18,250
And this function is going to take two inputs. So inside the parentheses,

83
00:05:18,580 --> 00:05:22,300
I'm going to put two parameters. Now you can,

84
00:05:22,300 --> 00:05:26,110
of course use the same word as the inputs that you're going to put into it.

85
00:05:26,380 --> 00:05:28,000
But in an ideal world,

86
00:05:28,000 --> 00:05:31,600
you try to not confuse yourself between the arguments and the parameters.

87
00:05:31,900 --> 00:05:34,690
So I'm going to call this plain_text,

88
00:05:34,840 --> 00:05:37,570
this first parameter and the second parameter

89
00:05:37,600 --> 00:05:41,230
I'm going to call it shift_amount. Now, of course, at a later date,

90
00:05:41,230 --> 00:05:43,330
when we're sending the input to this function,

91
00:05:43,570 --> 00:05:47,860
we're going to be sending the text to plain_text and shift to shift_amount.

92
00:05:48,460 --> 00:05:53,460
Keeping the names of the argument and the parameter different will help us

93
00:05:54,430 --> 00:05:59,430
later on to see which one is which. The next step is to go inside the encrypt

94
00:06:00,200 --> 00:06:03,410
function, which of course means indenting by one,

95
00:06:03,710 --> 00:06:08,710
we're going to shift each letter of the text forwards in the alphabet by the

96
00:06:08,930 --> 00:06:11,450
shift amount, and then print the encrypted text.

97
00:06:12,020 --> 00:06:15,230
This is going to be the challenging part of this whole exercise.

98
00:06:15,740 --> 00:06:18,020
But essentially if we think about the problem,

99
00:06:18,050 --> 00:06:22,940
what we want to do is we want to take each letter in the plain text.

100
00:06:23,000 --> 00:06:26,060
So let's say all plain text is equal to hello.

101
00:06:26,540 --> 00:06:31,540
Then we're going to take each of these letters in turn and then shift it up in

102
00:06:31,790 --> 00:06:36,290
the alphabet by the shift amount. So let's say for example,

103
00:06:36,290 --> 00:06:39,800
our shift amount was five and our plain text was hello,

104
00:06:40,340 --> 00:06:43,670
the first thing we're going to do is we're going to take the H which is over

105
00:06:43,670 --> 00:06:48,440
here, and then we're going to shift it up by five. So one, two, three, four,

106
00:06:48,680 --> 00:06:49,220
five,

107
00:06:49,220 --> 00:06:54,220
and we're going to turn it into an M. We can start off by writing a for loop that

108
00:06:54,380 --> 00:06:58,520
loops through each of the letters in the plain text input.

109
00:06:59,270 --> 00:07:04,270
And then we can take each of these letters and work out its position in the

110
00:07:05,390 --> 00:07:06,620
alphabet list here.

111
00:07:07,340 --> 00:07:10,820
And we can do that by using the index method.

112
00:07:11,030 --> 00:07:15,140
So we can tap into a list, write .index,

113
00:07:15,470 --> 00:07:18,470
and then put in the value that we want to get the index for

114
00:07:18,800 --> 00:07:23,720
and we should be able to get that as the result. So back over here,

115
00:07:23,750 --> 00:07:24,890
we're going to say,

116
00:07:25,010 --> 00:07:29,030
alphabet, is the list, .index

117
00:07:29,360 --> 00:07:33,560
and then inside the parentheses, we can put in our letter.

118
00:07:34,070 --> 00:07:39,070
So we want to find out the index of the letter that we're looping through in

119
00:07:39,410 --> 00:07:43,430
this alphabet up here. And once we've gotten a hold of that,

120
00:07:43,460 --> 00:07:47,180
we're gonna save it into a variable called position.

121
00:07:48,410 --> 00:07:52,790
Now, once we've got that, then we're going to calculate the new position.

122
00:07:54,050 --> 00:07:57,320
Now the new position is just going to be the previous position,

123
00:07:57,350 --> 00:08:02,270
which is going to be a number, and then plus the shift amount.

124
00:08:03,230 --> 00:08:06,170
So while we're going through this code, let's look at this word

125
00:08:06,260 --> 00:08:11,260
H. So the first time the loop runs letter's going to be equal to H, position is

126
00:08:11,330 --> 00:08:15,920
going to be equal to zero one, two, three, four, five, six, seven.

127
00:08:16,610 --> 00:08:18,620
And then the shift amount is five,

128
00:08:18,650 --> 00:08:21,620
so seven plus five is going to be 12.

129
00:08:21,950 --> 00:08:24,680
So now the new position is going to be 12,

130
00:08:25,070 --> 00:08:29,150
and we can tap into the alphabet list at position 12,

131
00:08:30,410 --> 00:08:33,320
so seven, eight, nine, ten, eleven, twelve,

132
00:08:33,409 --> 00:08:36,289
and get hold of the value at that position.

133
00:08:37,520 --> 00:08:42,520
So we can say that new_letter is equal to the alphabet at the new_position.

134
00:08:47,240 --> 00:08:50,120
And now once we've got this new_letter,

135
00:08:50,210 --> 00:08:55,040
we can create a empty string here which we'll call the ciphertext

136
00:08:56,160 --> 00:09:00,330
which is going to be the encoded text. And it starts out being an empty string

137
00:09:00,810 --> 00:09:03,600
and then once we've gotten each of the new letters,

138
00:09:03,870 --> 00:09:08,160
we're going to add it to the cipher text. Now,

139
00:09:08,160 --> 00:09:12,750
the final thing we have to do inside this encrypt function is once the

140
00:09:12,750 --> 00:09:14,340
for loop has run its course,

141
00:09:14,730 --> 00:09:19,260
and we've added all of the new letters into the cipher text,

142
00:09:19,530 --> 00:09:24,180
then at this point, it's a good idea to print out the output.

143
00:09:24,600 --> 00:09:27,570
And it tells us that we should say something like this,

144
00:09:27,870 --> 00:09:32,340
where we replace this final thing with the actual encoded text,

145
00:09:32,400 --> 00:09:35,250
which has of course, called a cipher text in our case.

146
00:09:35,910 --> 00:09:38,250
Now let's just make that into an fstring

147
00:09:38,250 --> 00:09:43,080
so that it'll insert it when it runs and we're ready to tackle the last to-do. So

148
00:09:43,080 --> 00:09:43,620
here,

149
00:09:43,620 --> 00:09:47,850
we're going to call the encrypt function and we're going to pass in all of the

150
00:09:47,850 --> 00:09:48,750
user inputs.

151
00:09:49,140 --> 00:09:53,850
So it remember that our parameter names are plain text and shift amount.

152
00:09:54,270 --> 00:09:57,840
So I'm actually going to use the keyword arguments, so I'm going to say

153
00:09:57,870 --> 00:10:02,870
plain text is equal to the text that the user has typed in.

154
00:10:04,650 --> 00:10:07,380
And then on the next argument,

155
00:10:07,530 --> 00:10:12,530
I'm going to say the shift amount is equal to the shift that the user has typed

156
00:10:13,440 --> 00:10:14,273
in.

157
00:10:17,100 --> 00:10:21,870
So now we're ready to test our code and let's take a look at what happens.

158
00:10:22,290 --> 00:10:27,290
So I'm going to use this example that we've done here by trying to encode hello

159
00:10:27,870 --> 00:10:31,980
with the shift of five. Now the first thing that we type, the direction,

160
00:10:32,010 --> 00:10:35,820
doesn't actually matter because it's not being used anywhere in our code,

161
00:10:36,000 --> 00:10:39,060
at least not yet. So you can type anything you want,

162
00:10:39,060 --> 00:10:43,380
I'm just going to type encode. And then the message is going to be hello.

163
00:10:44,310 --> 00:10:47,790
And the shift number is going to be five.

164
00:10:48,390 --> 00:10:52,500
And now it tells us that the encoded text is mjqqt,

165
00:10:52,830 --> 00:10:55,410
which matches with the expected output.

166
00:10:55,990 --> 00:11:00,430
You might think that we're done, right? This is perfect. This is great

167
00:11:00,430 --> 00:11:03,850
and we've completed this entire exercise.

168
00:11:04,390 --> 00:11:07,600
But lets consider a different scenario.

169
00:11:08,200 --> 00:11:13,200
What happens if we tried to encode a word that had letters that were really

170
00:11:15,190 --> 00:11:17,200
close to the end of the alphabet?

171
00:11:17,770 --> 00:11:22,360
Let's say that we wanted to encode the message zulu.

172
00:11:23,320 --> 00:11:26,590
Now I'm only doing that because if there's a z in that word, of course.

173
00:11:27,100 --> 00:11:29,380
And we wanted to shift it by five.

174
00:11:29,560 --> 00:11:32,620
Now let's think of what's going to happen right? In our code,

175
00:11:32,620 --> 00:11:36,130
we're going to take each of the letters, we're going to take z first

176
00:11:36,490 --> 00:11:41,490
and then we're going to get its position in the alphabet and add the shift

177
00:11:41,530 --> 00:11:45,430
amount to the position to get the new position.

178
00:11:45,910 --> 00:11:50,620
Now, this is already at the end of the alphabet. So if this number

179
00:11:50,620 --> 00:11:52,150
which is going to be 25,

180
00:11:52,180 --> 00:11:57,180
because there's 26 letters in the alphabet and we start the position at zero.

181
00:11:57,820 --> 00:12:01,210
So this plus five is going to be 30.

182
00:12:01,750 --> 00:12:06,580
And what happens when we try to get the 30th item from the alphabet? Well,

183
00:12:06,610 --> 00:12:07,900
you'll find out when I hit enter.

184
00:12:08,350 --> 00:12:13,350
We get an index error and it tells us that list index out of range, and it

185
00:12:13,690 --> 00:12:16,240
occurred on line 15.

186
00:12:16,660 --> 00:12:20,710
So just as we predicted, this does not exist.

187
00:12:21,100 --> 00:12:25,510
We have to try and tackle this right? So if you haven't thought about this,

188
00:12:25,630 --> 00:12:29,440
this is the time to think about how can you solve this problem.

189
00:12:30,160 --> 00:12:35,160
I want you to pause for a moment and try to see if you can fix it.

190
00:12:39,480 --> 00:12:39,870
All right.

191
00:12:39,870 --> 00:12:44,870
So we know that this has a fixed length and what we want to happen when we have

192
00:12:46,470 --> 00:12:51,470
a letter towards the end that we need to encode is to loop back to the beginning

193
00:12:51,720 --> 00:12:54,360
of the alphabet. So to solve this,

194
00:12:54,600 --> 00:12:59,600
all we have to do is just to copy the existing letters in the alphabet and then

195
00:13:00,450 --> 00:13:02,610
just duplicate it once more.

196
00:13:03,120 --> 00:13:08,120
So this means that if we find a letter that's z and it needs to be shifted up

197
00:13:08,190 --> 00:13:13,020
by five letters, it's going to continue until it gets to e.

198
00:13:13,650 --> 00:13:14,010
Now,

199
00:13:14,010 --> 00:13:19,010
the really wonderful thing about this index function is it's going to give you the

200
00:13:19,080 --> 00:13:21,510
first index that it finds.

201
00:13:21,960 --> 00:13:26,370
So if we're looking for the letter a, it's going to give you the index zero

202
00:13:26,670 --> 00:13:28,080
and then it's going to stop.

203
00:13:28,170 --> 00:13:33,170
It's not going to look for all the indices and find this one at position 26.

204
00:13:33,690 --> 00:13:36,690
So it means the our code will now work

205
00:13:37,020 --> 00:13:41,670
even if we have a word that have a lot of letters towards the end of the

206
00:13:41,670 --> 00:13:44,970
alphabet. Let's go ahead and give it a spin,

207
00:13:46,100 --> 00:13:46,890
Right?

208
00:13:46,890 --> 00:13:51,240
Let's type encode. And our message is again,

209
00:13:51,510 --> 00:13:55,140
zulu. And we hit enter.

210
00:13:55,800 --> 00:14:00,300
And then we have a shift number of five and we get our encoded text.

211
00:14:00,540 --> 00:14:03,810
z becomes one, two, three, four,

212
00:14:03,810 --> 00:14:08,250
five, it becomes e, and u, one, two, three, four,

213
00:14:08,250 --> 00:14:12,870
five, becomes z. So now we've solved that small bug.

214
00:14:14,520 --> 00:14:17,280
Now there's a lot of other ways that you could have solved this bug,

215
00:14:17,310 --> 00:14:21,960
but I think this is the easiest way without disturbing our logic here and not

216
00:14:21,960 --> 00:14:24,330
making our function even longer.

217
00:14:25,110 --> 00:14:29,310
If you had any problems with your code, then this is the time to go back and fix

218
00:14:29,310 --> 00:14:33,630
it and make sure that you really understand everything that's going on here

219
00:14:33,930 --> 00:14:37,530
before you move on to the next lesson. But once you're ready,

220
00:14:37,770 --> 00:14:41,160
I'll see you on the next lesson where we're going to decrypt our code.

