## Write a function

kpagcha + 28 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.

- JD
dujincong_dufe + 2 comments How does the "return year%4==0..." sentence know what you are returning is a boolean? Is that defined by "def is_leap"? Thank you

kpagcha + 3 comments because all expressions used in that return statement evaluate to booleans

- JR
IndianRockPython + 0 comments thanx

_Kalpesh_ + 0 comments thanks brother

- KS
Krishnasaivamsi + 0 comments sir,can you give a clear description about return statements

srious + 0 comments Check out python truthiness

- OE
ajayi + 0 comments some of us do that for clarity sake for those who may have a hard time processing what is going on.

- DJ
darshanjadav97 + 3 comments This is what i did.

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

Thanks for explaining

- AP
arpita0116 + 0 comments the first if statement if year%4==0: return True this will give true for 1900 year also but that it is not true

a_kamal_89 + 0 comments You can just reverse the order and it will be fine.

- RU
ryoutausami_pyd1 + 2 comments Took a few tries before I understood the conditions but I got it!

Here's my solution:

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

- HH
hhelzer + 0 comments That wound up failing two test cases.

- KS
Krishnasaivamsi + 0 comments how can you return heap without any function

- BJ
bjohnston370 + 5 comments You can write it without parentheses like this:

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

amitrajitbose9 + 1 comment Adding parenthesis increases readability. I might be wrong though!

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

- KB
karthik_bharadh1 + 0 comments what is the logic of this.....man!!!!

- I
ingoglia + 2 comments But then how does python know which one you mean: 1.y%4 AND y%100, or y%400

2.y%4, and y%100 OR y%400?

That's like saying these sentences state the same thing: 1.I will eat pie and roaches, or I will eat pasta.

2.I will eat pie, and either I will eat roaches or pasta.

Am I missing something?

- JM
jrmioduszewski + 0 comments Just like there is order of operations for mathematical operators, there is precedence for Python operators. AND has higher precedence than OR, so it is evaluated first.

rfstahl + 0 comments Order of precedence: NOT > AND > OR

- HZ
ujezidonye + 0 comments Thanks for this. I was having issues with If/ else statements.

- LP
valanukonda1999 + 0 comments plss explain this a bit clear how return works their

- KS
Krishnasaivamsi + 0 comments can you please explain the code.

- SS
sri_sam229 + 0 comments good one man.you seem to have pretty good understanding of python PEP.

- CN
zusammen + 1 comment why parenthesis are required and we get error without them??

- BH
codeharrier + 0 comments Associativity.

'a and b or c' reads like '(a and b) or c' to the interpreter.

'a and b' is only true every 400 years.

c is true every year except those divisible by 100, so you're marking only 3 years out of every 400 as non-leap years.

- HB
hamzabouabid7 + 0 comments if** year % 4 == 0 and (year % 400 == 0 or year % 100 != 0)** is true the how the true boolean is affected to leap variable since we didn't you didn't make leap=something? I hope you get the idea

chadwalt + 0 comments this is nice. it avoids repetition. Thanks alot.

intenseCtraining + 0 comments learners need to code like this imo. I use some of these programs with my classes and they simply dont understand any inline return equation, hell some of them dont even return values properly

scotmatson + 0 comments I agree, the multiple return statements are a tad heavy and go against DRY coding practices. However, your recommended solution also sacrifices readability, which IMO is more important than trying to massage your code into a single statement.

Be succinct but be readable. Let your bundlers handle the optimizations.

- DK
dhinastallion + 0 comments [deleted] sam_fold + 1 comment Your answer isn't really very clean code though. So while it works, it isn't very readable, which IMO is incredibly important.

- AR
abhishekreddy041 + 1 comment IMO means..?

- HH
hhelzer + 1 comment IMO = "in my opinion"

also there's

IMHO = "in my humble opinion" (which is often not very humble at all)

- AR
abhishekreddy041 + 0 comments thank u

- CM
Muttnstuff + 0 comments Thanks that explained it so clearly

- ZW
Zawaldo + 0 comments Makes perfect sense, thank you very much for your explanation!

- AR
Adivareyhanptr + 0 comments can you explain why you use the 'or' operator instead of 'and' for the 'year%100 != 0'? i still don't get it. thank you

- LW
wanglei_okay + 0 comments I like your func very much

himanshu_chauhan + 0 comments That's way shorter than my if and else loop's. Thanks.

ftakelait + 0 comments Hi @kpagcha,

Thanks for your detail explanation for this problem. Could you please explaine to me the relationship of your answer and the problem context, please? Because I didn't understand well the answer.

I want to unerstand how did you formulated these steps in this form you had programmed

In the Gregorian calendar three criteria must be taken into account to identify leap years:

**1. The year can be evenly divided by 4; 2. If the year can be evenly divided by 100, it is NOT a leap year, unless; 3. The year is also evenly divisible by 400. Then it is a leap year**Thanks for advance. Fouzi

- GS
gaurav_sonkusle + 0 comments very effective buddy.... thnk u

- DB
beckwithdylan + 0 comments if you want to reduce redundancy, why not also return the bool of the comparisons to avoid explicitly checking for zero equivalency..

def is_leap(year): return bool(not year % 4 and (year % 100 or not year % 400))

irregardless.. you deserve an upvote

Anaximandrake + 0 comments Your solution is certainly the most elegant, but when the user is first presented with the problem in the coding box, the return command has already been given the object 'year' and we are given a #Write your logic here note

**above**it. So it's at least forgivable for some of us to construct multiple 'if' blocks to solve it, wouldn't you agree?- SS
sachinsavita95 + 0 comments it is not passing all the test cases?

- KM
Veerubhotla + 0 comments thanks :D

zpf7879 + 0 comments Might be clearer:

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

- EG
guardadopatrici1 + 0 comments Hi just a question about parenthesis placement why does placing a parenthesis like below year % 4 == 0 and (year % 400 == 0 or year % 100 != 0) is correct code versus not placing parenthesis at all or writting it like ((y%4==0)and(y%100!=0)or(y%400==0)) results in error

- JP
JayPatel2810 + 0 comments thanks for advice, keep commenting :)

- MS
mayank_sept92 + 0 comments Hi, thanks for the shorter approach but, what about the condition-- 1900<= year <=10**5

- AB
aayushbis + 0 comments Thank you sir for the advice.

- A
moukhtari + 4 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)

michaelhuynhquoc + 1 comment What I just learnt from you: don't re-invent the wheel :D

- SD
super_geek + 0 comments true af

- OE
ajayi + 0 comments haha, you are the boss.

- RV
- C
cssd1983 + 0 comments Amazing

flmmartins + 4 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.`

- SD
veganXedge + 0 comments i agree. maybe this has something to do with my math skil level but, i couldn't understand how to properly figure out that(code below) was an acceptable way to write it.

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

i would like to understand or have some sort of guide on how to better interpret these questions.

iamphanisai + 0 comments yes. i too faced the same problem and had to google what a gregorian calendar was and found a page long explanation about how julias caesar invented it... etc etc before actually understanding what it means!

scotmatson + 1 comment Agree.

I am an English speaker and struggled with this definition. It was poorly written.

- CU
xbuildx_matrix + 0 comments I agree

- KR
kasunr + 0 comments My answers are ...

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

or

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

JonathanV + 2 comments Working

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

Vangelic_surgeon + 1 comment Thanks this stupid problem took me more time to solve and even the official solution is s**t

KavinRanawella + 0 comments if u are refering to the solution under the editorial tab, I would say, it is more elaborating. I think either you have misread the question, or you haven't noticed that there are no 'elseif' logic in the given solution. Only 'if' statements are there. It suits the requirements perfectly. Read the solution again carefully. U'll get it. :)

tricerabottoms + 2 comments This one is a little longer but I think you'll find it easier to follow:

class is_leap: pass is_leap.__init__ = lambda s,y: setattr(s,'y',y) is_leap.__repr__ = lambda s: str((not s.y%4)^(not s.y%100)^(not s.y%400))

- HH
hamichok11 + 1 comment lol :D much easier!

- C
cssd1983 + 1 comment LOL

- SC
saumitra13325 + 1 comment LOL

- SC
saumitra13325 + 1 comment LOL

- SC
saumitra13325 + 1 comment LOL

- SC
saumitra13325 + 1 comment LOL

- SC
saumitra13325 + 1 comment LOL

- SC
saumitra13325 + 1 comment LOL

- SC
saumitra13325 + 1 comment LOL

- SC
saumitra13325 + 1 comment LOL

- SC
saumitra13325 + 1 comment LOL

- SC
saumitra13325 + 1 comment LOL

- SC
saumitra13325 + 1 comment LOL

- SC
saumitra13325 + 1 comment LOL

- SC
saumitra13325 + 1 comment LOL

- SC
saumitra13325 + 1 comment LOL

- SC
saumitra13325 + 1 comment LOL

- SC
saumitra13325 + 1 comment LOL

- SC
saumitra13325 + 2 comments LOL

- SC
saumitra13325 + 0 comments [deleted] - SC
saumitra13325 + 0 comments Ahhh...got the end

- LB
bartyl + 0 comments :DDDD good joke

marinskiy + 0 comments Here is Python solution from my HackerrankPractice repository:

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

Feel free to ask if you have any questions :)

- KS
kiransajjanshet1 + 1 comment If the year is evenly divisible by 400, isnt it obviously divisible by 4 and 100 as well ? Can someone provide me an example where a year is divisible by 4, 400 but not 100 ? I am not sure if I am thinking right. Help please !

nitinchary + 0 comments The year that is evenly divisible by 4

**and not**divisible by 100 is a leap year. It has to satisfy both the conditions.Ex: 2016-->This is evenly divisible by 4, but not divisible by 100. So 2016 is a leap year. 1800-->This is evenly divisible by 4 as well as 100. So 1800 is not a leap year.

Any year that is evenly divisible by 400 is a leap year. Ex: 2400--> This is divisible by 4 as well as 100, however, it is evenly divisible by

**400**. So that makes it a leap year.

- DC
divide_et_imper1 + 1 comment This code is working for Py2

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

rishabh10 + 0 comments in the question it says evenly divisible doesn't that mean

(year/4)%2==0 ??

- SR
shifin_epz + 0 comments def is_leap(year): leap = False if year%4 == 0 and (year%100 != 0 or year%400 ==0): leap = True

`return leap`

- C
cnhnyu + 3 comments If a year is a leap year: 1. it can be evenly divided by 4 and NOT evenly divided by 100; OR 2. it can be evenly divided by 400

leap = False if any([year % 4 == 0 and year % 4 != 0, year % 400 == 0]): leap = True

- AN
alexandar_naray1 + 1 comment I like the use of any here, I didn't think of that. You could set

leap = any([year % 4 == 0 and year % 4 != 0, year % 400 == 0])

since any returns a boolean

- C
cnhnyu + 1 comment Thanks, you're right. It's much simpler.

leap = any([year % 4 == 0 and year % 100 != 0, year % 400 == 0])

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

- MM
marcon_zm + 2 comments What is the comma doing here exactly?

year % 100 != 0, year % 400 == 0

Is it testing the output of "%100 != 0", and if it returns false it tests for the "% 400 == 0" conditional?

- AN
alexandar_naray1 + 0 comments the any function takes an iterable so it is basically saying: if any of the items in this iterable is true return true. so the comma makes them elements of a list between the brackets.

- C
cnhnyu + 0 comments You can think it as an OR operation in this case.

alexandar_naray1 expains it very well.

- MF
mohamedfardinkh1 + 0 comments How to use "any" function? What are it's cases ?

- DM
bad_coder_lol + 0 comments for some reason, the Hacker Rank team wasn't able to clarify that clearly

- VA
agrawal_ved + 1 comment Using not: if not year % 4 and year % 100 or not year % 400 :

- PP
patrick_x99 + 0 comments that could evaluate to a number instead of True for years divisible by 4 but not 100. so you need to wrap it in a bool(...)

return bool(not year%4 and year%100 or not year%400)

Sort 259 Discussions, By:

Please Login in order to post a comment