Thursday, July 21, 2016

Write a function - Hacker Rank Solution

Write a function - Hacker Rank Solution

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;
  • If 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
Output Format
Output is taken care of by the template. Your function must return a boolean value (True/False)
Sample Input
1990  
Sample Output
False  
Explanation
1990 is not a multiple of 4 hence it's not a leap year.

Write a function - Hacker Rank Solution

Problem Setter's code :
Python 2
def is_leap(n):
    if n % 400 == 0:
        return True
    if n % 100 == 0:
        return False
    if n % 4 == 0:
        return True
    return False

print is_leap(input())
Python 3
def is_leap(n):
    if n % 400 == 0:
        return True
    if n % 100 == 0:
        return False
    if n % 4 == 0:
        return True
    return False

print(is_leap(int(input())))

30 comments:

  1. python 2

    def is_leap(year):
    leap = False
    if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
    leap = True
    return leap

    ReplyDelete
    Replies
    1. def is_leap(year):
      leap=False
      if year % 400 == 0:
      return True
      if year %100 == 0:
      return leap
      if year % 4 ==0:
      return True
      else:
      return False
      return leap


      #print(is_leap(int(input())))


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

      Delete
    2. ek baar yh briefly smjha dijiye

      Delete
    3. def is_leap(year):
      leap = False

      # Write your logic here

      return year % 4 == 0 and (year % 400 == 0 or year % 100 != 0)

      year = int(raw_input())

      Delete
  2. can someone tell me what the Test Case#5 is?
    the value?

    ReplyDelete
  3. Here is one line solution for this.

    def is_leap(year):

    return((year%400==0 or year%100!=0) and year%4==0)

    ReplyDelete
    Replies
    1. The order is slightly wrong in this.
      It should be
      year%4==0 and (year%400==0 or year%100!=0)

      This is in accordance to the conditions laid down in the task.
      If the number is evenly dividable by 4, then it is a leap year, unless it is evenly dividable by 100 then it is not, unless it is also evenly dividable by 400.

      So in this code snippet we check, if the number is evenly dividable by 4 and whether it is NOT evenly dividable by 100.

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. def is_leap(year):
    leap = False

    if year%400 == 0:
    return("True")
    if year%100 == 0:
    return(leap)
    if year%4 == 0:
    return("True")

    return leap

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

    ReplyDelete
  6. def is_leap(year):
    leap = False
    # Write your logic here
    if year%4==0:
    leap = True
    if year%100==0:
    leap = False
    if year%400==0:
    leap = True
    return leap

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

    ReplyDelete
  7. Is_leap=lambda year : (year%4==0 or year%400==0 or year%100==0)
    year=int(input())
    Print(is_leap(year))

    ReplyDelete
  8. Can someone please explain the case for 1992, when we divide it by 400 the remainder is not zero and so it should return a False but miraculously it is returning a True. Can someone please comment on it.

    ReplyDelete
  9. # i hope this will help you
    def is_leap(year):
    if year % 4 == 0:
    return(True)
    elif year % 100 == 0:
    return(False)
    elif year % 400 == 0:
    return(True)
    elif year % 2100 != 0:
    return(False)
    else :
    return(False)


    year = int(raw_input())
    print is_leap(year)

    ReplyDelete
  10. its so simple just write it down step by step :python 3
    def is_leap(year):
    leap = False
    if year%4==0:
    leap=True
    if year%100==0:
    leap=False
    if year%400==0:
    leap=True


    # Write your logic here

    return leap

    year = int(input())

    ReplyDelete
  11. def is_leap(year):
    leap = False
    if year%4==0:
    leap=True
    if year%100==0:
    leap=False
    if year%400==0:
    leap=True
    return leap

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

    **TRIED AND TESTED ALL CASES SATISFIED**

    ReplyDelete
  12. 2100 is divisible by 4 and 100 what to do

    ReplyDelete
  13. def is_leap(year):
    leap = False

    # Write your logic here
    if year%4==0:
    if year%100==0 and year%400==0:
    leap = True
    else:
    leap = False
    else:
    leap = False
    return leap
    year = int(input())
    print(is_leap(year))





    ONE PERFECT SOLUTION

    ReplyDelete

Powered by Blogger.