Why does my code produce incorrect output?

In summary: The change from the Julian calendar to the Gregorian calendar didn't happen uniformly in all countries. In Great Britain and the colonies (including what is now the U.S.), this change didn't occur until almost two centuries later, in September of 1752. Here's an image of a calendar from that year. Notice the jump from Sep 2 to Sep 14, with no dates shown for the intervening 11 days.Have to dust off this iconic spreadsheet and see if I can find the date for 1900,1,1,199,12,31.
  • #1
doktorwho
181
6
Python:
# Use Dave's suggestions to finish your daysBetweenDates
# procedure. It will need to take into account leap years
# in addition to the correct number of days in each month.

def nextDay(year, month, day):
    n=month
    if (n==1 or n==3 or n==5 or n==7 or n==8 or n==10 or n==12) and day < 31:
        return year, month, day + 1
    if (n==4 or n==6 or n==9 or n==11) and day <30 :
        return year, month, day + 1
    if month==2 and day < 29 and (year%4)==0:
        return year, month, day + 1
    if month==2 and day < 28 and (year%4)!=0:
        return year, month, day + 1
    else:
        if month == 12:
            return year + 1, 1, 1
        else:
            return year, month + 1, 1
 

def dateIsBefore(year1, month1, day1, year2, month2, day2):
    """Returns True if year1-month1-day1 is before year2-month2-day2. Otherwise, returns False."""
    if year1 < year2:
        return True
    if year1 == year2:
        if month1 < month2:
            return True
        if month1 == month2:
            return day1 < day2
    return False   

def daysBetweenDates(year1, month1, day1, year2, month2, day2):
    """Returns the number of days between year1/month1/day1
       and year2/month2/day2. Assumes inputs are valid dates
       in Gregorian calendar."""
    # program defensively! Add an assertion if the input is not valid!
    assert not dateIsBefore(year2, month2, day2, year1, month1, day1)
    days = 0
    while dateIsBefore(year1, month1, day1, year2, month2, day2):
        year1, month1, day1 = nextDay(year1, month1, day1)
        days += 1
    return days

def test():
    test_cases = [((2012,1,1,2012,2,28), 58),
                  ((2012,1,1,2012,3,1), 60),
                  ((2011,6,30,2012,6,30), 366),
                  ((2011,1,1,2012,8,8), 585 ),
                  ((1900,1,1,1999,12,31), 36523)]
 
    for (args, answer) in test_cases:
        result = daysBetweenDates(*args)
        if result != answer:
            print ("Test with data:"), args, "failed"
        else:
            print ("Test case passed!")
test()

I am suppose to write a code that calculates the number of days between two dates and i get everything from the test correctly except the last one, 1900,1,1,199,12,31. I get +1 day, why is that?
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
Do you know that there are no leap years on the 100's that are not divisible by 400?
 
  • Like
Likes jedishrfu
  • #3
Borg said:
Do you know that there are no leap years on the 100's that are not divisible by 400?
I did not know that, thanks :-)
 
  • #5
jedishrfu said:
Also historically there was a calendar change in 1582 by Pope Gregory to fix the time of the vernal equinox which was 10 days too late:

http://www.findingdulcinea.com/news...this-Day--In-1582--Oct--5-Did-Not-Exist-.html
The change from the Julian calendar to the Gregorian calendar didn't happen uniformly in all countries. In Great Britain and the colonies (including what is now the U.S.), this change didn't occur until almost two centuries later, in September of 1752. Here's an image of a calendar from that year. Notice the jump from Sep 2 to Sep 14, with no dates shown for the intervening 11 days.
Sept_1752_calendar.jpg
 
  • Like
Likes jedishrfu

Related to Why does my code produce incorrect output?

1. Why is my code producing incorrect output even though it compiled without errors?

This is a common issue and can be caused by a variety of reasons. One possibility is that there is a logical error in your code, meaning that the program is functioning as it was coded, but not producing the desired result. Another possibility is that there is a syntax error, which can be more difficult to spot but can also lead to incorrect output.

2. How can I debug my code to find the source of the incorrect output?

There are several ways to debug your code. One approach is to use print statements to track the values of variables and see where the program is going wrong. Another option is to use a debugger tool, which allows you to step through your code line by line and see the values of variables at each step. Additionally, reading through your code carefully can help identify any logical or syntax errors.

3. Could the incorrect output be caused by a problem with my input data?

Yes, incorrect input data can definitely lead to incorrect output. Make sure to check that your input data is formatted correctly and that all necessary data is included. It can also be helpful to test your code with different input data to see if the incorrect output persists.

4. Is it possible that my code is producing incorrect output because of a bug in the programming language or library?

While this is possible, it is unlikely. Programming languages and libraries are rigorously tested and updated regularly to fix any bugs. It is more likely that the issue lies with your code or input data. However, if you suspect a bug, it is always a good idea to check for any updates or known issues with the language or library you are using.

5. How can I prevent incorrect output in the future?

To prevent incorrect output, it is important to thoroughly test and debug your code before using it in a production environment. Additionally, following best practices such as writing clear and concise code, using comments, and testing with various input data can help catch any potential errors. It can also be helpful to seek feedback from colleagues or mentors to identify any potential issues in your code.

Similar threads

  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
10
Views
737
  • Programming and Computer Science
Replies
22
Views
893
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
Back
Top