Write a function in Python - Hacker Rank Solution


Write a function in Python - Hacker Rank Solution


Problem


We add a Leap Day on February 29, almost every four years. The leap day is an extra, or intercalary day and we add it to the shortest month of the year, February.
In the Gregorian calendar three criteria must be taken into account to identify leap years:
  • The year can be evenly divided by 4, 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.Source

Task :
You are given the year, and you have to write a function to check if the year is leap or not.
Note that you have to complete the function and remaining code is given as template.



Input Format :

Read y, the year that needs to be checked.

Constraints :

  • 1900 <= y <=10^5

Output Format :

Output is taken care of by the template. Your function must return a boolean value (True/False)



Input Format :

1990

Output Format :

False

Explanation :

1990 is not a multiple of 4 hence it's not a leap year.



Solution :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Write a function in Python - Hacker Rank Solution
def is_leap(year):
    leap = False
    
    # Write your logic here
    # Write a function in Python - Hacker Rank Solution START
    if year%400==0 :
        leap = True;
    elif year%4 == 0 and year%100 != 0:
        leap = True;
    return leap
    # Write a function in Python - Hacker Rank Solution END
year = int(raw_input())
print is_leap(year)





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
3 Comments
  • Unknown
    Unknown Saturday, December 04, 2021

    def is_leap(year):
    leap = False

    # Write your logic here
    if (year%400==0) or (year%4==0 and year%100!=0):
    return True
    return leap

    year = int(input())
    print(is_leap(year))

    • Anonymous
      Anonymous Sunday, July 10, 2022

      Hi this is Jeeva. Actually I'm trying to pratice code at hackerrank.. while trying to write a function code
      if(year%4 = = 0 or year%400 == 0 and year%100 !=0)


      If I change the condition as like this getting error. Even the hackerrank question this order is different.. Could you please help me with this?

      Error:

      Error: wrong answer and test case 1

    • Anonymous
      Anonymous Wednesday, August 31, 2022

      In ur logic compiler considers the priority of the operators as ur logic mentions like an expression. So 1st considers bitwise and operator(as year%400==0 and year%100!=0 satisfies) then the resultant condition checks checks with bitwise operator(with year%4==0)according to your logic...

Add Comment
comment url