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.
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())))
python 2
ReplyDeletedef is_leap(year):
leap = False
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
leap = True
return leap
can you pls explain how?
Deletedef is_leap(year):
Deleteleap=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))
ek baar yh briefly smjha dijiye
Deletedef is_leap(year):
Deleteleap = False
# Write your logic here
return year % 4 == 0 and (year % 400 == 0 or year % 100 != 0)
year = int(raw_input())
can someone tell me what the Test Case#5 is?
ReplyDeletethe value?
1992
DeleteHere is one line solution for this.
ReplyDeletedef is_leap(year):
return((year%400==0 or year%100!=0) and year%4==0)
Will you explain me pls
DeleteThe order is slightly wrong in this.
DeleteIt 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.
This comment has been removed by the author.
ReplyDeleteyour code really wrong
ReplyDeletedefinately right
Deleteyour code is failing to return true when 1992 is passed what to do
Deletewell 1992 is a leap year
Deletedef is_leap(year):
ReplyDeleteleap = 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))
def is_leap(year):
ReplyDeleteleap = 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))
Is_leap=lambda year : (year%4==0 or year%400==0 or year%100==0)
ReplyDeleteyear=int(input())
Print(is_leap(year))
what about 2100?
ReplyDeletei have same question
DeleteYa same problem
DeleteCan 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.
ReplyDeleteYour code contains year % 4
Deleteworst code you can write
ReplyDelete# i hope this will help you
ReplyDeletedef 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)
its so simple just write it down step by step :python 3
ReplyDeletedef 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())
def is_leap(year):
ReplyDeleteleap = 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**
2100 TEST CASE FAILED
Delete2100 is divisible by 4 and 100 what to do
ReplyDeletedef is_leap(year):
ReplyDeleteleap = 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