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.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Python
  3. Introduction
  4. Write a function
  5. Discussions

Write a function

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • kpagcha
    5 years ago+ 148 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.

    1286|
    Permalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature