Dates

Save all functions into the same file named dates.py.

  1. Write the function age() that takes the birth date of a person and the present date, and returns the age of the person. Each date is represented with three integers (int): day, month and year. Example:

    >>> e = age(10, 4, 1990, 23, 5, 2013)
    >>> e
    23
    >>> e = age(10, 4, 1990, 5, 3, 2013)
    >>> e
    22
    >>> e = age(11, 1, 1998, 23, 5, 2018)
    >>> e
    20
    >>> e = age(7, 6, 1945, 5, 3, 2010)
    >>> e
    64
    

    Note

    More tests are provided in the dates1.txt file.

  2. Write the function complete_year() that takes a year and a century. The value of the year is between 0 and 99, and returns the complete year computed by adding the century to the year. Example:

    >>> a = complete_year(57, 20)
    >>> a
    1957
    >>> type(a)
    <class 'int'>
    >>> a = complete_year(16, 21)
    >>> a
    2016
    >>> type(a)
    <class 'int'>
    >>> a = complete_year(66, 19)
    >>> a
    1866
    >>> type(a)
    <class 'int'>
    >>> a = complete_year(86, 17)
    >>> a
    1686
    >>> type(a)
    <class 'int'>
    

    Note

    More tests are provided in the dates2.txt file.

  3. Write the function age_21() that takes the same parameters as function age(), and returns the age of the person. This function age_21() has to deal with completely represented years (as 1976 or 2003) or with years represented without the century. We will suppose that the years without century (between 0 and 99) are from the XXI century. This function must call the previous functions age() and complete_year(). Example:

    >>> e = age_21(10, 4, 1990, 23, 5, 13)
    >>> e
    23
    >>> e = age_21(10, 4, 3, 5, 3, 2013)
    >>> e
    9
    >>> e = age_21(10, 4, 1998, 23, 5, 13)
    >>> e
    15
    >>> e = age_21(10, 4, 3, 5, 3, 2016)
    >>> e
    12
    

    Note

    More tests are provided in the dates3.txt file.

  4. Write the function compare_dates() that takes 6 integers day1, mont1, year1, day2, month2 and year2, that represent two dates, and returns -1 if the first date is previous to the second date, 0 if they are the same date and 1 if the first date is later to the second one. Example:

    >>> c = compare_dates(10, 4, 1917, 8, 2, 1923)
    >>> c
    -1
    >>> c = compare_dates(10, 4, 1917, 8, 4, 1917)
    >>> c
    1
    >>> c = compare_dates(10, 4, 1917, 10, 4, 1917)
    >>> c
    0
    

    Note

    More tests are provided in the dates4.txt file.

  5. Write the functions previous_date(), same_date() and later_date() that take two dates each, and return True if the first date is previous, equal or later to the second one respectively. Otherwise they return False. You must call function compare_dates(). Example:

    >>> c = previous_date(10, 4, 1917, 8, 2, 1923)
    >>> c
    True
    >>> c = same_date(10, 4, 1917, 8, 2, 1923)
    >>> c
    False
    >>> c = later_date(10, 4, 1917, 8, 2, 1923)
    >>> c
    False
    

    Note

    More tests are provided in the dates5.txt file.

Solutions

A solution of these functions is provided in the dates.py file.