LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2024, 02:44 AM   #1
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Rep: Reputation: 4
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.
 
Old 03-29-2024, 08:17 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Please provide why the first code is incorrect?
 
Old 03-29-2024, 08:35 AM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,138
Blog Entries: 6

Rep: Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827
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
 
Old 03-29-2024, 09:48 AM   #4
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,602

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

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?

 
Old 03-30-2024, 05:34 AM   #5
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by grail View Post
Please provide why the first code is incorrect?
I don't know, the source says. If you allow, can quote the source.
 
Old 03-30-2024, 05:35 AM   #6
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by boughtonp View Post
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)
 
Old 03-30-2024, 07:09 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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
 
1 members found this post helpful.
Old 03-30-2024, 04:59 PM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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."
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Another Episode of "Seems Perfectly Feasible and Then Dies"--Script to Simplify the Process of Changing System Call Tables LXer Syndicated Linux News 0 08-08-2019 01:25 AM
Please tell me what does each field stand for in the below log rahul99rocks AIX 4 08-28-2012 11:39 PM
LXer: The Toshiba Satellite L755D-S5204 Laptop: A Perfectly Priced (and Perfectly Awesome) Christmas LXer Syndicated Linux News 0 12-16-2011 03:10 PM
files are created in the future while date cmd output is perfectly fine zvika Linux - Newbie 1 10-16-2008 11:03 AM
LXer: Taxed Net Sales Are Perfectly Fine LXer Syndicated Linux News 0 04-19-2007 11:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration