#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2

    What's wrong with this code of mine?


    OK,
    Here's a function which takes 6 arguments: First three will be the birth year, birth month and birth day respectively; the last three will be the current year, current month and current day respectively.
    We have to calculate how many days have been passed since the birth. Maybe this code is too inefficient since I am just a beginner and it probably could be done by writing fewer lines of code, but I just wanna know, why is this code returning the wrong output?
    Code:
    def daysBetweenDates(year1, month1, day1, year2, month2, day2):
    
    # Birth calender in days
    
        def birthyear(year1):
            if year1 % 4 == 0:
                no_of_leap_years = year1 // 4
                year1 = (year1 * 365) + no_of_leap_years
                return year1
            else:
                no_of_leap_gaps = year1 % 4
                year1 = year1 - no_of_leap_gaps
                no_of_leap_years = year1 // 4
                year1 = (year1 * 365) + no_of_leap_years
                year1 = year1 + (366 * no_of_leap_gaps)
                return year1
        def birthmonth(month1):
            if year1 % 4 == 0:
                months_in_leap_year = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
                days_in_leap_year = sum(months_in_leap_year[:month1])
                return days_in_leap_year
            else:
                months_in_year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
                days_in_year = sum(months_in_year[:month1])
                return days_in_year
    
            
    # Current calender in days
    
        def currentyear(year2):
            if year2 % 4 == 0:
                no_of_leap_years = year2 // 4
                year2 = (year2 * 365) + no_of_leap_years
                return year2
            else:
                no_of_leap_gaps = year2 % 4
                year2 = year2 - no_of_leap_gaps
                no_of_leap_years = year2 // 4
                year2 = (year2 * 365) + no_of_leap_years
                year2 = year2 + (366 * no_of_leap_gaps)
                return year2
        def currentmonth(month2):
            if year2 % 4 == 0:
                months_in_leap_year = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
                days_in_leap_year = sum(months_in_leap_year[:month2])
                return days_in_leap_year
            else:
                months_in_year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
                days_in_year = sum(months_in_year[:month2])
                return days_in_year
        return (currentyear(year2) + currentmonth(month2) + day2) - (birthyear(year1) + birthmonth(month1) + day1)
    Thanks, in advance
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    2
    so, what doesn't work? can you post an example?
    maybe it has something to do with this: "One year has the length of 365 days, 5 hours, 48 minutes and 47 seconds. Because this is rather unfunctional, a normal year has been given 365 days and a leap year 366 days. At leap years February 29th is added, which doesn't exist in a normal year. A leap year is every 4 years, but not every 100 years, then again every 400 years.
    " ?
    Last edited by leonnaley2; May 26th, 2013 at 05:18 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Here's the bottom test:
    Code:
    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()
    And it gives me:
    Code:
    Test with data: (2012, 1, 1, 2012, 2, 28) failed
    Test case passed!
    Test with data: (2011, 6, 30, 2012, 6, 30) failed
    Test with data: (2011, 1, 1, 2012, 8, 8) failed
    Test with data: (1900, 1, 1, 1999, 12, 31) failed
    Any idea(s)?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481

    Enhance the error message.


    Change the test to provide more information. There are still a lot of numbers that are "incorrect". Check out the red line:
    Code:
    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"
                print('expected {} .  Got {} .'.format(answer, result)) ########HERE
            else:
                print "Test case passed!"
    with output
    Code:
    $ python p.py
    Test with data: (2012, 1, 1, 2012, 2, 28) failed
    expected 58 .  Got 56 .
    Test case passed!
    Test with data: (2011, 6, 30, 2012, 6, 30) failed
    expected 366 .  Got 364 .
    Test with data: (2011, 1, 1, 2012, 8, 8) failed
    expected 585 .  Got 583 .
    Test with data: (1900, 1, 1, 1999, 12, 31) failed
    expected 36523 .  Got 36526 .
    $
    Your algorithm is off by 2 or 3. Looks like the code miscounts leap years on and days at the end-point years. Probably correct everywhere in between.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by b49P23TIvg
    Change the test to provide ...
    in between.
    Here's a better test for leap year but it still prints the wrong output!
    Code:
    def daysBetweenDates(year1, month1, day1, year2, month2, day2):
    
    # Defining a function to check whether it's a leap year or not
        def is_leap_baby(year):
            if year % 400 == 0:
                return True
            elif year % 100 == 0:
                return False
            else:
                return year % 4 == 0
    
    # Birth calender in days
    
        def birthyear(year1):
            if is_leap_baby(year1):
                no_of_leap_years = year1 // 4
                year1 = (year1 * 365) + no_of_leap_years
                return year1
            else:
                no_of_leap_gaps = year1 % 4
                year1 = year1 - no_of_leap_gaps
                no_of_leap_years = year1 // 4
                year1 = (year1 * 365) + no_of_leap_years
                year1 = year1 + (366 * no_of_leap_gaps)
                return year1
        def birthmonth(month1):
            if is_leap_baby(year1):
                months_in_leap_year = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
                days_in_leap_year = sum(months_in_leap_year[:month1])
                return days_in_leap_year
            else:
                months_in_year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
                days_in_year = sum(months_in_year[:month1])
                return days_in_year
    
            
    # Current calender in days
    
        def currentyear(year2):
            if is_leap_baby(year2):
                no_of_leap_years = year2 // 4
                year2 = (year2 * 365) + no_of_leap_years
                return year2
            else:
                no_of_leap_gaps = year2 % 4
                year2 = year2 - no_of_leap_gaps
                no_of_leap_years = year2 // 4
                year2 = (year2 * 365) + no_of_leap_years
                year2 = year2 + (366 * no_of_leap_gaps)
                return year2
        def currentmonth(month2):
            if is_leap_baby(year2):
                months_in_leap_year = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
                days_in_leap_year = sum(months_in_leap_year[:month2])
                return days_in_leap_year
            else:
                months_in_year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
                days_in_year = sum(months_in_year[:month2])
                return days_in_year
        return (currentyear(year2) + currentmonth(month2) + day2) - (birthyear(year1) + birthmonth(month1) + day1)

IMN logo majestic logo threadwatch logo seochat tools logo