Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,390 --> 00:00:05,340
Hey guys. So I've got another code challenge for you. And this one,
2
00:00:05,790 --> 00:00:08,970
as the title suggests, this is a difficult challenge,
3
00:00:09,120 --> 00:00:13,380
and this is something that I expect some of you might need to spend 15,
4
00:00:13,380 --> 00:00:17,460
20 minutes on. Others might need to spend 30, 40 minutes on,
5
00:00:17,820 --> 00:00:22,290
and it might just be something that you need to have a think about, get stuck,
6
00:00:22,620 --> 00:00:25,920
come back to it after a bit of a break or sleep on it,
7
00:00:25,980 --> 00:00:28,650
come back to it the next day. It's not easy,
8
00:00:28,890 --> 00:00:31,530
but what I'm trying to say is really,
9
00:00:31,530 --> 00:00:35,310
really give it all you have before you check the solution video,
10
00:00:35,790 --> 00:00:38,940
because it's a really, really interesting challenge.
11
00:00:40,020 --> 00:00:43,710
So the idea is that we want you to write a program that works out
12
00:00:44,070 --> 00:00:46,980
whether if any given year is a leap year.
13
00:00:47,670 --> 00:00:50,400
We know that a normal year has 365 days,
14
00:00:50,490 --> 00:00:53,340
but leap years have an extra day in February.
15
00:00:54,060 --> 00:00:57,750
And the reason is actually really, really interesting. If you're interested,
16
00:00:58,110 --> 00:01:02,550
there's a really good explanation of how leap years work and why we have them at
17
00:01:02,550 --> 00:01:06,780
this YouTube link. So just go ahead and copy this URL and check it out.
18
00:01:07,530 --> 00:01:11,160
Now, the way that you work out if a given year is a leap year,
19
00:01:11,250 --> 00:01:15,030
so let's say we pick one out of thin air, 2020,
20
00:01:15,570 --> 00:01:19,980
is that on every year that is evenly divisible by four,
21
00:01:20,130 --> 00:01:23,760
so that means divisible by four with no remainder.
22
00:01:24,480 --> 00:01:28,170
Then that is a leap year, but except the years
23
00:01:28,230 --> 00:01:31,710
that is also evenly divisible by a hundred,
24
00:01:32,640 --> 00:01:37,560
unless the year is also evenly divisible by 400.
25
00:01:38,100 --> 00:01:43,050
Now 2020 divided by four is equal to 505.
26
00:01:43,380 --> 00:01:47,520
So it's evenly divisible by four. There's no remainder.
27
00:01:48,480 --> 00:01:52,560
But the rule says that even if it's divisible by four,
28
00:01:53,130 --> 00:01:55,920
if it's evenly divisible by a hundred,
29
00:01:56,250 --> 00:01:59,040
then that is still not going to be a leap year.
30
00:01:59,340 --> 00:02:04,340
So let's try that 2020 divided by a hundred equals 20.2.
31
00:02:05,550 --> 00:02:09,540
So this is in fact not evenly divisible by a hundred.
32
00:02:09,990 --> 00:02:14,970
So that means it is still a leap year. But then it says
33
00:02:15,180 --> 00:02:19,560
unless the years also evenly divisible by 400.
34
00:02:20,310 --> 00:02:21,210
So 2020
35
00:02:21,210 --> 00:02:26,210
divided by 400 is not evenly divisible.
36
00:02:26,790 --> 00:02:29,970
So it is in fact, still a leap year.
37
00:02:30,870 --> 00:02:33,990
The trickiest part of this entire challenge
38
00:02:34,020 --> 00:02:39,020
I would say is getting your head around these three sentences: a year that is
39
00:02:41,310 --> 00:02:45,570
divisible by four with no remainder is a leap year,
40
00:02:46,080 --> 00:02:50,160
except if it's also divisible by a hundred. In that case,
41
00:02:50,160 --> 00:02:51,360
it's no longer a leap year,
42
00:02:51,960 --> 00:02:56,960
unless that year also happens to be evenly divisible by 400 then it is still a
43
00:02:59,440 --> 00:03:03,220
leap year. What I recommend doing is a)
44
00:03:03,610 --> 00:03:05,860
if English is not your native language
45
00:03:06,220 --> 00:03:10,060
then have a look online for the rules for determining a leap
46
00:03:10,060 --> 00:03:13,990
year in your own language, because these words, except
47
00:03:14,020 --> 00:03:16,660
and unless, are so,
48
00:03:16,720 --> 00:03:20,950
so integral to how this algorithm works. And you really,
49
00:03:20,950 --> 00:03:23,530
really need to understand exactly what it means.
50
00:03:24,970 --> 00:03:29,970
Then what I recommend you doing is to go ahead and create a flow chart,
51
00:03:31,150 --> 00:03:33,820
just like what I've done in previous examples.
52
00:03:34,210 --> 00:03:36,730
Head over to a website like draw.io,
53
00:03:37,000 --> 00:03:39,580
or simply just write it on a piece of paper.
54
00:03:39,610 --> 00:03:44,610
Draw it out and visualize what this actually means in terms of your code.
55
00:03:45,820 --> 00:03:48,010
Now, if you really get stuck on that,
56
00:03:48,430 --> 00:03:51,640
I've also created a flowchart for you.
57
00:03:52,090 --> 00:03:55,660
So if you actually get stuck creating the flowchart,
58
00:03:55,840 --> 00:03:58,720
there is one there that you can refer to as well.
59
00:03:59,020 --> 00:04:00,370
But it's a really good exercise
60
00:04:00,370 --> 00:04:04,720
creating your own flowchart based on what it is that you want your program to
61
00:04:04,720 --> 00:04:09,720
do, and your algorithm or your code should correctly tell us whether
62
00:04:10,750 --> 00:04:15,730
if it's a leap year or not a leap year. So it just has to print out leap year
63
00:04:15,760 --> 00:04:20,290
if it is like 2400, and if it's not a leap year,
64
00:04:20,290 --> 00:04:23,230
like 1989, then just print, not leap year.
65
00:04:24,580 --> 00:04:28,510
This is the time to pause the video and really give this challenge
66
00:04:28,570 --> 00:04:32,380
everything you've got. I'll see you back here once you're done.
67
00:04:32,910 --> 00:04:37,910
[inaudible]
68
00:04:40,200 --> 00:04:43,290
All right. So how did you get on? As I mentioned,
69
00:04:43,350 --> 00:04:46,950
this is a really difficult challenge. If you haven't managed to solve it,
70
00:04:47,250 --> 00:04:51,120
then try to come back to it a little bit later on and spend more time on it.
71
00:04:51,750 --> 00:04:56,430
This is a decisive moment. Don't give up on yourself. Keep going.
72
00:04:56,880 --> 00:04:59,760
I'm always going to be here for you with the solution.
73
00:05:00,180 --> 00:05:02,340
You're not going to get stuck. It's really,
74
00:05:02,340 --> 00:05:06,510
really important to try and solve it yourself. Now,
75
00:05:06,540 --> 00:05:11,190
the first thing I mentioned is turning this rule for determining whether if a
76
00:05:11,190 --> 00:05:14,550
year is a leap year or not into a flow chart.
77
00:05:15,120 --> 00:05:19,410
So I've created one which I linked to in the challenge
78
00:05:19,950 --> 00:05:21,270
and it's very simple.
79
00:05:21,540 --> 00:05:26,160
We start off by asking ourself is a particular year that we have in mind,
80
00:05:26,610 --> 00:05:30,870
um, let's say the year 2000, is it cleanly divisible by four?
81
00:05:31,080 --> 00:05:36,080
Which means can you divide it by four with zero remainder?
82
00:05:37,410 --> 00:05:41,940
Well, if the case is no, then it already is definitely not a leap year.
83
00:05:42,690 --> 00:05:46,650
But if the case is yes, then we have to ask ourselves some more questions.
84
00:05:47,250 --> 00:05:51,420
Namely, can we divide it by a hundred with no remainder?
85
00:05:52,170 --> 00:05:56,910
Well, if we can't, then it's definitely a leap year, but if we can,
86
00:05:57,290 --> 00:06:01,730
then we have to check yet again. Can we divide it by 400 with no remainder?
87
00:06:02,240 --> 00:06:05,750
If the answer is yes, then it's a leap year. If the answer is no,
88
00:06:05,870 --> 00:06:10,130
then it's not a leap year. Let's try and code that logic out,
89
00:06:10,280 --> 00:06:14,270
keep your flow chart up somewhere so that you can write your code while
90
00:06:14,270 --> 00:06:15,560
referring to the diagram.
91
00:06:16,430 --> 00:06:21,430
We start off by checking whether if the year that came in through the input is
92
00:06:22,250 --> 00:06:25,280
divisible by four with no remainder.
93
00:06:25,910 --> 00:06:28,790
And you might remember from our previous code exercise
94
00:06:29,060 --> 00:06:33,310
that the way we do this is using the modulo. So year
95
00:06:33,350 --> 00:06:36,500
modulo four will give us the remainder.
96
00:06:37,760 --> 00:06:42,650
So if the remainder is equal to zero, well in that case it's cleanly
97
00:06:42,650 --> 00:06:46,640
divisible by four. And you can check this out yourself in the console.
98
00:06:46,700 --> 00:06:47,300
Let's say,
99
00:06:47,300 --> 00:06:52,300
if you have 8 % 4, 4 goes into 8 cleanly,
100
00:06:52,730 --> 00:06:56,360
there's two four's in an eight. The remainder is zero.
101
00:06:57,050 --> 00:07:02,050
Whereas 9 % 4, 4 + 4 is 8 remainder 1.
102
00:07:04,820 --> 00:07:09,140
So if the remainder from the year divided by four is equal to zero,
103
00:07:09,560 --> 00:07:14,510
then it is cleanly divisible by four. So in this case,
104
00:07:14,630 --> 00:07:17,630
we'll have to go into yet another if statement.
105
00:07:18,230 --> 00:07:21,740
But if it's actually false, then it's definitely not a leap year.
106
00:07:22,370 --> 00:07:25,730
So the else statement that matches this
107
00:07:25,760 --> 00:07:30,760
if statement is going to tell us for sure that this is not a leap year.
108
00:07:34,990 --> 00:07:39,700
But if it is true, then we have to check the next thing. Is it cleanly
109
00:07:39,700 --> 00:07:41,350
divisible by a hundred?
110
00:07:42,100 --> 00:07:47,100
So we check if the year % 100 is equal to zero.
111
00:07:48,490 --> 00:07:51,130
And if the answer is yes,
112
00:07:51,250 --> 00:07:56,230
then we have to check that year against another question. But if it's no,
113
00:07:56,380 --> 00:08:01,380
then it's definitely a leap year. In this case that will lead to an if statement
114
00:08:02,050 --> 00:08:04,660
if it's true, but if it's false,
115
00:08:04,810 --> 00:08:09,810
then we can simply print leap year because that's what it's going to be.
116
00:08:11,170 --> 00:08:16,060
The final question that we have to ask ourselves about the year is,
117
00:08:16,320 --> 00:08:18,970
is it cleanly divisible by 400?
118
00:08:19,690 --> 00:08:24,340
So now if year % 400 is equal to zero,
119
00:08:24,970 --> 00:08:28,810
well, in this case, if it is true,
120
00:08:29,050 --> 00:08:30,520
then it's definitely a leap year.
121
00:08:30,730 --> 00:08:34,630
If it's false, then it's definitely not a leap year. So, you can print
122
00:08:36,340 --> 00:08:40,840
lear year if it's true, otherwise or if it's false,
123
00:08:40,870 --> 00:08:43,120
then we can print not leap year.
124
00:08:45,880 --> 00:08:46,840
And that's it.
125
00:08:46,900 --> 00:08:51,900
That's all the code that you would need in order to represent this logic.
126
00:08:53,230 --> 00:08:56,610
Now there's plenty of other ways you could have written your code,
127
00:08:57,060 --> 00:09:00,390
but this is probably the most readable. Now,
128
00:09:00,420 --> 00:09:02,250
if you've written some different code
129
00:09:02,730 --> 00:09:05,520
but it still uses these rules to work out
130
00:09:05,820 --> 00:09:08,760
whether if a given year is a leap year and it's correct,
131
00:09:09,060 --> 00:09:10,620
then that's fine as well.
132
00:09:10,650 --> 00:09:13,620
There's plenty of ways that you can solve this challenge.
133
00:09:15,060 --> 00:09:20,060
The most important thing I want you to take away from this code exercise is that
134
00:09:20,700 --> 00:09:25,700
you can turn any sort of logic into a flowchart to be able to visualize it.
135
00:09:27,480 --> 00:09:30,030
And once you've got your logic straight,
136
00:09:30,330 --> 00:09:34,050
then converting it into code is much, much simpler.
137
00:09:34,770 --> 00:09:39,000
If you haven't figured out the logic and then you try to start writing code,
138
00:09:39,360 --> 00:09:40,530
then it's going to be a mess.
139
00:09:40,860 --> 00:09:45,860
So first, get to the stage of being able to create your own flowcharts and then
140
00:09:46,950 --> 00:09:49,260
take those flow charts and turn them into code.
141
00:09:50,520 --> 00:09:52,170
If you got stuck on this challenge
142
00:09:52,200 --> 00:09:54,630
or if you didn't manage to complete it for some reason,
143
00:09:54,870 --> 00:09:59,220
head back to it now and give it some extra attention, because this is really,
144
00:09:59,220 --> 00:10:03,270
really important. It's in these moments, these code challenges,
145
00:10:03,330 --> 00:10:05,580
where you are actually going to a level up as a programmer.
146
00:10:05,880 --> 00:10:07,380
It's not during the tutorials,
147
00:10:07,410 --> 00:10:10,260
it's not during the videos where I explain things to you.
148
00:10:10,770 --> 00:10:13,290
It's where you actually write the code yourself.
149
00:10:13,950 --> 00:10:17,700
I hope you had fun on this code challenge and you managed to complete it,
150
00:10:18,060 --> 00:10:22,050
be it with hints or without hints. On the next lesson, we're going to take the
151
00:10:22,050 --> 00:10:27,050
if statement one step further and I want to show you how you can have multiple
152
00:10:27,480 --> 00:10:30,390
if statements and tests for multiple conditions.
153
00:10:31,110 --> 00:10:34,500
So for all of that and more, I'll see you on the next lesson.
14768
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.