Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I can't for the life of me understand this test on Hackerrank. The nearest I got it was to 4/6 passes. Rules: In the Gregorian schedule three standards should be considered to recognize jump 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.

code:

def is_leap(year):

    leap = False

    

    # Write your logic here

    if year%400==0 :

        leap = True

    elif year%4 == 0 and year%100 != 0:

        leap = True

    return leap

year = int(input())

print(is_leap(year))

1 Answer

0 votes
by (26.4k points)

You failed to remember the ==0 or !=0 which will help comprehend the conditions better. You don't need to utilize them, yet then it can create turmoil keeping up the code.

def is_leap(year):

  leap = False

  if (year % 4 == 0) and (year % 100 != 0): 

      # Note that in your code the condition will be true if it is not..

      leap = True

  elif (year % 100 == 0) and (year % 400 != 0):

      leap = False

  elif (year % 400 == 0):

      # For some reason here you had False twice

      leap = True

  else:

      leap = False

  return leap

You can also try this code (smaller version):

def is_leap(year):

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

Want to be a python expert? Come and join this python course

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Mar 6, 2021 in Java by Jake (7k points)
0 votes
1 answer
asked Feb 17, 2021 in Java by Harsh (1.5k points)

Browse Categories

...