We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Practice
  • Certification
  • Compete
  • Career Fair
  • Hiring developers?
  1. Practice
  2. Python
  3. Introduction
  4. Write a function
  5. Discussions

Write a function

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 1191 Discussions, By:

votes

Please Login in order to post a comment

  • kpagcha
    4 years ago+ 0 comments

    For the learners: you should know that doing something like the setup for this challenge inclines you to do is a bad practice. Never do the following:

    def f():
    	if condition:
        	return True
        else:
        	return False
    

    This is just dumb. You are returning a boolean, so why even use if blocks in the first place? The correct what of doing this would be:

    def f():
    	return condition
    

    Because this already evaluates as a boolean.

    So in this challenge, forget about ifs and elses, and that leap variable, and just do the following:

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

    Don't be redundant, be DRY.

    936|
    Permalink
  • am110
    4 years ago+ 0 comments

    The calendar builtin module has a function isleap() that returns True if the year is leap, otherwise it returns False

    import calendar
    
    def is_leap(year):
        return calendar.isleap(year)
    
    105|
    Permalink
  • flmmartins
    4 years ago+ 0 comments

    The problem's explanation is not clear. I am not an english speaker and the bullet points were not clear enough for me.

    Can that be improved?

    Suggested explanation is bellow:

    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, then it is not a leap year
    
    The year is also evenly divisible by 400 then it is a leap year.
    
    43|
    Permalink
  • JonathanV
    4 years ago+ 0 comments

    Working

    if year%4 == 0 and (year%100 != 0 or year%400 == 0): leap = True
    
    12|
    Permalink
  • nayanbhiwapurkar
    2 years ago+ 0 comments

    Method 1

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

    Method 2

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

    Method 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
        return leap
    
    10|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature