LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Please tell the error in code below, as seems as perfectly fine as it's alternative. (https://www.linuxquestions.org/questions/programming-9/please-tell-the-error-in-code-below-as-seems-as-perfectly-fine-as-its-alternative-4175735438/)

ajiten 03-29-2024 02:44 AM

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.

grail 03-29-2024 08:17 AM

Please provide why the first code is incorrect?

teckk 03-29-2024 08:35 AM

First all, this is python.

Both of those work.
Code:

python
Python 3.11.8 (main, Feb 12 2024, 14:50:05) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 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())
2010
>>> print(is_leap(year))
False

Second example has mixed indents.(Which wont work in python)
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
...
>>> year = int(input())
2010
>>> print(is_leap(year))
False


boughtonp 03-29-2024 09:48 AM


 
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?


ajiten 03-30-2024 05:34 AM

Quote:

Originally Posted by grail (Post 6492689)
Please provide why the first code is incorrect?

I don't know, the source says. If you allow, can quote the source.

ajiten 03-30-2024 05:35 AM

Quote:

Originally Posted by boughtonp (Post 6492703)
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)


grail 03-30-2024 07:09 AM

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

sundialsvcs 03-30-2024 04:59 PM

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."


All times are GMT -5. The time now is 04:26 AM.