Hackerrank Solution: How to check leap year in Python


Written by - Bashir Alam
Reviewed by - Deepak Prasad

Question: Write a function- Hacker Rank (Python)

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, and is a leap year, unless:
  • 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

Given a year, determine whether it is a leap year. If it is a leap year, return the Boolean True, otherwise, return False.

Note that the code stub provided reads from STDIN and passes arguments to the is_leap function. It is only necessary to complete the is_leap function.

leap year

Possible solutions

There can be many solutions to this question, depending on the requirements. As we are solving this question based on the requirements of hacker rank, so we will find possible solutions based on those requirements as mentioned in the description.

Here we will solve the question using 4 different methods:

  1. Using multiple if-else statements
  2. Using a single if statement
  3. Using nested if-else statements
  4. Using Python module

 

Solution-1: Using multiple if-else statements to find leap year

As there are some conditions for leap year which are described in the description of the question. So, we can use those conditions in our Python solution using multiple if-else statements. Let us create a function that will return True if the given year is a leap year, else it will return false.

def is_leap(year):
    
    # variable to check the leap year
    leap = False
    
    # divided by 100 means century year
    # century year divided by 400 is leap year
    if (year % 400 == 0) and (year % 100 == 0):
        
       # change leap to True
        leap = True

    # not divided by 100 means not a century year
    # year divided by 4 is a leap year
    elif (year % 4 ==0) and (year % 100 != 0):
        
        #Change leap to true
        leap = True

    #else not a leap year
    else:      
        pass
    
    return leap

Now, you can call the function by providing different years. For example, if we call the function using 2016, and 2017 as input years, We get the following results:

Input to the function:

is_leap(2016)
is_leap(2017)

Output:

True
False

Because 2016 was a leap year so we get, true and 2017 was not a leap year, so we get false.

 

Solution-2: Using a single if statement

Although we have multiple conditions for a leap year we can combine all those conditions in only one if statement using AND and OR operators. Let us create a function with only one if statement.

def is_leap(year):
    
    # variable to check the leap year
    leap = False
    
    # One if statement that contains multiple conditions
    if ((year % 400 == 0) and (year % 100 == 0)) | ((year % 4 ==0) and (year % 100 != 0)):
        
       # change leap to True
        leap = True

    
    return leap

Now, let us call the function by providing 2016 and 2017 as input years.

Input:

is_leap(2016)
is_leap(2017)

Output:

True
False

As you can see, we get True for 2016 as it was a leap year and we get False for 2017 which was not a leap year.

 

Solution-3: Using Nested if statements

Another way is to use the nested if statements where each if statement contains one condition. Let us use the nested if statements to create a function that checks whether the given year is a leap year or not.

# creating the function
def is_leap(year):
    
    # variable to check the leap year
    leap = False
    
    # using nested if statements
    if (year % 4) == 0:
        if (year % 100) == 0:
            if (year % 400) == 0:
                leap = True
            else:
                leap = False
        else:
            leap = True
    else:
        leap = False
    
    return leap

Now we will call the function using the input years.

is_leap(2016)
is_leap(2017)

Output:

True 
False

As excepted we get the desired result.

 

Solution-4: Using the Python calendar module

Python is known for its various built-in modules. Instead of writing the code for leap year manually using the conditions, we can simply use the Python module to check if the given year is a leap year or not as shown below:

# importing the module
import calendar

# function
def is_leap(year):
    
    leap = False
    
    # checking for leap year
    if calendar.isleap(year):
        leap = True
    
    return leap

Let us now call the function:

is_leap(2016) 
is_leap(2017)

Output:

True 
False

As you can see, we get the desired outputs. In a similar way, you can call the function using any year and it will return True for leap year and False for other years.

 

Summary

A year, occurring once every four years, which has 366 days including 29 February as an intercalary day is a leap year. In this short article, we learned how we can use Python language to find if the given year is a leap year or not by solving the question from hacker rank.

 

References

Python module
Question on Hacker rank - Write a Function

 

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in OCR, text extraction, data preprocessing, and predictive models. You can reach out to him on his Linkedin or check his projects on GitHub page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment

X