HackerRank: Solving Leap Years Puzzle Using Python

Aflah Agus Rizkika
5 min readMar 10, 2022

Sounds pretty easy, but is it?

Source: Pinterest

Introduction

Sometimes when we learn about programming, we need to measure our skills, right? The best ways to do that is by solving a challenging problem. And guess what? Leap Years Puzzle by HackerRank (one of the most favorite websites to learn programming) is one of it.

But first, you need to understand what the Leap Years is. A leap year is a calendar year that contains an additional day added to keep the calendar year with the astronomical year or seasonal year (Wikipedia said). Long story short, it is the year when February have 29 days rather than the common 28 days. So, you might guess you will build an easy algorithm that solve whatever years and determine which is a leap year or not. Congratulation, you’re right!

Here’s the puzzle by HackerRank itself.

An extra day is added to the calendar almost every four years as February 29, and the day is called a leap day. It corrects the calendar for the fact that our planet takes approximately 365.25 days to orbit the sun. A leap year contains a leap day.

In the Gregorian calendar, three conditions are used to identify leap years:

· The year can be evenly divided by 4, is a leap year, unless:

o The year can be evenly divided by 100, it is NOT a leap year, unless:

§ The year is also evenly divisible by 400. Then it is a leap year.

This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.

Your task is to determine given year whether it is a leap year or not. If it is leap year, you must return the Boolean True, if not then False. So, for example, given input 1999 your return must be False, because 1999 is not multiple of 4 so it must not a leap year.

Pretty simple task, right? Let’s quickly jump over it!

Programming Logic

(Actually, in the HackerRank you will given a function by default, your jobs are making the logic behind it.)

First, as you might guess you would think that you make if else logic, yeah your right, the we going from there, so you need to make year can evenly divided by 4, which means it is going to use mod (%) function.

So, the mod function is a useful arithmetic operation to produce output from the rest of the division. For example the remaining division of 3 divided by 2 is 1. In this case we are asked to make a condition if the year is divided by 4 then the year is considered leap.

First Condition

Unless, if the year produces the remaining division of 0 by 100, then the year cannot be considered leap.

Second Condition

However, the third condition says that if the year is divisible by 400, then the year is considered leap.

Third Condition

So, on the first experiment we will generate the following code,

If we try to take a test case for example 1990, then the return generated False. True! 1990 was not a leap year.

The question now is whether in every possible case (in HackerRank) the code will produce the correct output? The answer is No.

You might ask why not? What’s wrong with my program? Actually if there is a trial with 2100 input then the return to be returned is True. Unfortunately, 2100 is not a leap year. (If you don’t believe me, then try it yourself through HackerRank or whatever IDE you choose) If you try it, you’ll believe me.

Now, the question is where am I wrong? I’ve done what the algorithm commands step by step but I’m still wrong. Where’s the wrong?

Thought Process

First of all, let’s “surgically” why the resulting return is True. What we need to pay attention to lies in the iteration / logic that we make.

We know that the first thing when we enter a number is to enter the mod 4 function, where if the year we enter is a leap year then the year we enter must be the remaining 0 if divided by 4. Well, that’s True.

However, what makes the final return yield true? If you guess because the year meets the conditions then you are right, but what conditions are meant? The answer is that the resulting output will meet the first condition first, then if it is not fulfilled it will move to the next condition.

This means that if the year meets the first condition, where the first condition is if the year is divided by 4 then the year is a leap year. The year 2100 is the year that is divided by 4, so from that logic 2100 is considered a leap year (because it is fulfill the first condition, so the return must stop there and not continue to other condition), when in fact 2100 is not a leap year.

So, since we already know that the input entered is always assumed to meet the first condition first, then we need to change the condition of the first.

The second step is to determine which conditions to move or exchange more precisely? We know that if the year is divided by 4 the output results of our algorithm do not match expectations. So, what if the year is divided by 100 first?

Maybe that’s good advice, the question now is what is the second condition of the algorithm that we create, suppose the condition is the year can be divided by 4. So the logic that we will make is as follows,

If we take the 1992 input, then the output issued is True, as we want. However, suppose we take 2000 then the resulting output is False (because it meets the first condition, therefore it does not continue to the previous condition).

So, using a depleted condition divided by 100 is a bad choice. The only option is to use the terms out divided by 400. So the code that we will create, will likely appear as follows,

You may ask why the terms are divisible by the last 4, which is actually closely related to the complexity of the sequence of numbers of the year, so the smaller the division the more likely it is to be used as the final condition to determine whether a year is said to be leap or not.

In the end, if you try to enter the code into the solution in HackerRank then the puzzle will be solved. And I congratulate you for solving the puzzle together, may this writing be useful. If you want to use my code you can see it through the references I listed (+link to puzzles from HackerRank)

Reference

raaflahar/HackerRank_Leap_Year_Problem: This is a project about leap years using the Python programming language, where the cases provided are derived from HackerRank. (github.com)

Write a function | HackerRank

--

--