Please tell the error in code below, as seems as perfectly fine as it's alternative.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Please tell the error in code below, as seems as perfectly fine as it's alternative.
Please tell why the below code is considered incorrect?
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))
If asked for, can provide link of where it has been marked as incorrect.
The alternative provided is :
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
But, I see nothing gained, except redundant code.
Also, ran the stated (earlier) code, and gives correct results.
The Python code for determining a leap year is "calendar.isleap(year)"
Anything else is redundant code.
If this is homework, then a correct answer should start by pointing out the built-in function, then solving it anyway, along with with a full set of test cases showing how the function handles all edge cases correctly.
The "alternative provided" is stupidly over-verbose but also looks like it is a correction of different code - so maybe the teacher sent you an updated version of someone else's attempt. Have you tried asking the teacher?
The Python code for determining a leap year is "calendar.isleap(year)"
Anything else is redundant code.
If this is homework, then a correct answer should start by pointing out the built-in function, then solving it anyway, along with with a full set of test cases showing how the function handles all edge cases correctly.
The "alternative provided" is stupidly over-verbose but also looks like it is a correction of different code - so maybe the teacher sent you an updated version of someone else's attempt. Have you tried asking the teacher?
The python code is:
Code:
def isleap(year):
"""Return True for leap years, False for non-leap years."""
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
I would suggest that if you are doing a course then it is expecting specific answers.
Whilst your version may work and still be correct, it would appear the source material required the answer provided
I actually prefer your version, as long as it can be proved to be correct using an appropriate set of test cases. I have doubts about the second with regard to the "% 400" case, since "% 100" is tested first, and it would also "pass" the "400 case." Very importantly, yours does not do that.
However, I would have written the final bit as: "else return false." I would not bother to have a "leap" variable – just "return true/false" – because you never subsequently test that variable.
The resulting code is clear: the most-restrictive case first, each one directly "returns" true or false, and the final "else" exists to very-clearly show the catch-all case.
But, such a function then needs to be rigorously tested against a set of appropriately-chosen dates which test both the expected results and the "edge cases."
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.