Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
Hey guys. So I've got another code challenge for you. And this one,
2
as the title suggests, this is a difficult challenge,
3
and this is something that I expect some of you might need to spend 15,
4
20 minutes on. Others might need to spend 30, 40 minutes on,
5
and it might just be something that you need to have a think about, get stuck,
6
come back to it after a bit of a break or sleep on it,
7
come back to it the next day. It's not easy,
8
but what I'm trying to say is really,
9
really give it all you have before you check the solution video,
10
because it's a really, really interesting challenge.
11
So the idea is that we want you to write a program that works out
12
whether if any given year is a leap year.
13
We know that a normal year has 365 days,
14
but leap years have an extra day in February.
15
And the reason is actually really, really interesting. If you're interested,
16
there's a really good explanation of how leap years work and why we have them at
17
this YouTube link. So just go ahead and copy this URL and check it out.
18
Now, the way that you work out if a given year is a leap year,
19
so let's say we pick one out of thin air, 2020,
20
is that on every year that is evenly divisible by four,
21
so that means divisible by four with no remainder.
22
Then that is a leap year, but except the years
23
that is also evenly divisible by a hundred,
24
unless the year is also evenly divisible by 400.
25
Now 2020 divided by four is equal to 505.
26
So it's evenly divisible by four. There's no remainder.
27
But the rule says that even if it's divisible by four,
28
if it's evenly divisible by a hundred,
29
then that is still not going to be a leap year.
30
So let's try that 2020 divided by a hundred equals 20.2.
31
So this is in fact not evenly divisible by a hundred.
32
So that means it is still a leap year. But then it says
33
unless the years also evenly divisible by 400.
34
So 2020
35
divided by 400 is not evenly divisible.
36
So it is in fact, still a leap year.
37
The trickiest part of this entire challenge
38
I would say is getting your head around these three sentences: a year that is
39
divisible by four with no remainder is a leap year,
40
except if it's also divisible by a hundred. In that case,
41
it's no longer a leap year,
42
unless that year also happens to be evenly divisible by 400 then it is still a
43
leap year. What I recommend doing is a)
44
if English is not your native language
45
then have a look online for the rules for determining a leap
46
year in your own language, because these words, except
47
and unless, are so,
48
so integral to how this algorithm works. And you really,
49
really need to understand exactly what it means.
50
Then what I recommend you doing is to go ahead and create a flow chart,
51
just like what I've done in previous examples.
52
Head over to a website like draw.io,
53
or simply just write it on a piece of paper.
54
Draw it out and visualize what this actually means in terms of your code.
55
Now, if you really get stuck on that,
56
I've also created a flowchart for you.
57
So if you actually get stuck creating the flowchart,
58
there is one there that you can refer to as well.
59
But it's a really good exercise
60
creating your own flowchart based on what it is that you want your program to
61
do, and your algorithm or your code should correctly tell us whether
62
if it's a leap year or not a leap year. So it just has to print out leap year
63
if it is like 2400, and if it's not a leap year,
64
like 1989, then just print, not leap year.
65
This is the time to pause the video and really give this challenge
66
everything you've got. I'll see you back here once you're done.
67
[inaudible]
68
All right. So how did you get on? As I mentioned,
69
this is a really difficult challenge. If you haven't managed to solve it,
70
then try to come back to it a little bit later on and spend more time on it.
71
This is a decisive moment. Don't give up on yourself. Keep going.
72
I'm always going to be here for you with the solution.
73
You're not going to get stuck. It's really,
74
really important to try and solve it yourself. Now,
75
the first thing I mentioned is turning this rule for determining whether if a
76
year is a leap year or not into a flow chart.
77
So I've created one which I linked to in the challenge
78
and it's very simple.
79
We start off by asking ourself is a particular year that we have in mind,
80
um, let's say the year 2000, is it cleanly divisible by four?
81
Which means can you divide it by four with zero remainder?
82
Well, if the case is no, then it already is definitely not a leap year.
83
But if the case is yes, then we have to ask ourselves some more questions.
84
Namely, can we divide it by a hundred with no remainder?
85
Well, if we can't, then it's definitely a leap year, but if we can,
86
then we have to check yet again. Can we divide it by 400 with no remainder?
87
If the answer is yes, then it's a leap year. If the answer is no,
88
then it's not a leap year. Let's try and code that logic out,
89
keep your flow chart up somewhere so that you can write your code while
90
referring to the diagram.
91
We start off by checking whether if the year that came in through the input is
92
divisible by four with no remainder.
93
And you might remember from our previous code exercise
94
that the way we do this is using the modulo. So year
95
modulo four will give us the remainder.
96
So if the remainder is equal to zero, well in that case it's cleanly
97
divisible by four. And you can check this out yourself in the console.
98
Let's say,
99
if you have 8 % 4, 4 goes into 8 cleanly,
100
there's two four's in an eight. The remainder is zero.
101
Whereas 9 % 4, 4 + 4 is 8 remainder 1.
102
So if the remainder from the year divided by four is equal to zero,
103
then it is cleanly divisible by four. So in this case,
104
we'll have to go into yet another if statement.
105
But if it's actually false, then it's definitely not a leap year.
106
So the else statement that matches this
107
if statement is going to tell us for sure that this is not a leap year.
108
But if it is true, then we have to check the next thing. Is it cleanly
109
divisible by a hundred?
110
So we check if the year % 100 is equal to zero.
111
And if the answer is yes,
112
then we have to check that year against another question. But if it's no,
113
then it's definitely a leap year. In this case that will lead to an if statement
114
if it's true, but if it's false,
115
then we can simply print leap year because that's what it's going to be.
116
The final question that we have to ask ourselves about the year is,
117
is it cleanly divisible by 400?
118
So now if year % 400 is equal to zero,
119
well, in this case, if it is true,
120
then it's definitely a leap year.
121
If it's false, then it's definitely not a leap year. So, you can print
122
lear year if it's true, otherwise or if it's false,
123
then we can print not leap year.
124
And that's it.
125
That's all the code that you would need in order to represent this logic.
126
Now there's plenty of other ways you could have written your code,
127
but this is probably the most readable. Now,
128
if you've written some different code
129
but it still uses these rules to work out
130
whether if a given year is a leap year and it's correct,
131
then that's fine as well.
132
There's plenty of ways that you can solve this challenge.
133
The most important thing I want you to take away from this code exercise is that
134
you can turn any sort of logic into a flowchart to be able to visualize it.
135
And once you've got your logic straight,
136
then converting it into code is much, much simpler.
137
If you haven't figured out the logic and then you try to start writing code,
138
then it's going to be a mess.
139
So first, get to the stage of being able to create your own flowcharts and then
140
take those flow charts and turn them into code.
141
If you got stuck on this challenge
142
or if you didn't manage to complete it for some reason,
143
head back to it now and give it some extra attention, because this is really,
144
really important. It's in these moments, these code challenges,
145
where you are actually going to a level up as a programmer.
146
It's not during the tutorials,
147
it's not during the videos where I explain things to you.
148
It's where you actually write the code yourself.
149
I hope you had fun on this code challenge and you managed to complete it,
150
be it with hints or without hints. On the next lesson, we're going to take the
151
if statement one step further and I want to show you how you can have multiple
152
if statements and tests for multiple conditions.
153
So for all of that and more, I'll see you on the next lesson.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.