I cannot for the life of me solve this challenge on Hackerrank. The closest I got it was to 4/6 passes. Rules: 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.

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))

2

You forgot the ==0 or !=0 which will help understand the conditions better. You don’t have to use them, but then it can cause confusion maintaining 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

a shorter version would be:

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

5

You can try this

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

This code might be easy for some of the people to wrap their head around

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))

If we split the question point by point.

  • Actual question: 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.

  • Explaining it : If a year is divided by 4, then its a leap year.

  • If this is divided by 100 but not 400 after being divisible by 4, then its not a leap year.

  • If this is visible by 4, 100 ,400 , then its a leap year.

Basically, nested if

    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

1

def is_leap(year): leap = False

if year % 4 == 0 and year % 100 != 0:
    leap = True
elif year % 400 == 0 and year % 100 == 0:
    leap = True

return leap

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

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

1

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

def is_leap(year): leap = False

# Write your logic here
if year % 4 == 0 and year % 100 != 0:
    leap =  True
elif year % 100 == 0 and year % 400 != 0:
    leap =  False
elif year % 400 == 0:
    leap = True
elif year % 4 != 0:
    leap = False
return leap

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

def is_leap(year):
    leap = False
    if(year%4==0):
        #century year check
        if(year%100==0):
            #century leap year check
            if(year%400==0):
                leap=True
        else:
            leap=True
    return leap

if year%4==0 and year%100==0 and year%400==0:
    return True
elif year%4==0 and year%100!=0 and year%400!=0:
    return True
else:
    return False

2

n = [int(input("Enter a year in four digit: "))]

a = list(map(lambda x: "is a Leap year" if x%4 == 0 or ( x % 100 != 0 or x % 400 == 0) else "is not a Leap year" , n))

print(f"{n[0]} {a[0]}")

1

I just try with this:

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

    return leap

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

if year can be evenly divided by 4 and 400 is True but if it can be evenly divided by 100 is False

#simplest solution for beginner

n = int(input('Enter year to check if it is leap year.'))
if n % 4 == 0 and n%100 != 0:
    leap = 'True'
elif n % 100 == 0 and n%400==0:
    leap = 'True'
else:
    leap = 'False'

print('the entered year is,'+leap)

This would be my solution for the problem. We want the number to be divisible with 4 no matter what. So year %4 will need to be true for the output to be bool True as well. Then we have to consider if the number which is divisible with 4 can be divided to 400. If the number is divisible to 100 but not 400 it should give us bool False. That is why we should check divisibility to 400 and non-divisibility to 100 together and use or statement.

def is_leap(year):
leap = False

if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
    return not leap
else: 
    return leap   

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

def is_leap(year):
    leap = False
    
    # Write your logic here
    if (year%4 ==0):
        if (year%100 == 0) and (year%400 ==0):
            leap = True
        elif(year%100 == 0) and (year%400 !=0):
            leap = False
        else:
            leap = True 
    else:
        leap = False
    return leap

year = int(input())

1

def is_leap(year):
    leap = False 
    if year % 4 == 0 and year % 100 == 0 and year % 400 == 0:
        return True
    elif year %4 == 0 and year % 100! = 0 and year % 400!= 0:
        return True
    else:
        return False
  
year = int(raw_input())
print is_leap(year)

2

def is_leap(year): leap = False

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

return leap

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

1

def is_leap(year):
    leap = False
    d4 = year%4
    d100 = year%100
    d400 = year%400
    
    if  d4  == 0 :
        if d100 != 0 or d400 == 0 :
            leap = True
    else :
        leap = False
    return leap

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

Not the answer you’re looking for? Browse other questions tagged python or ask your own question.