Preparation

The programming problem requires you to make use of the data types date and timedelta that are defined in the datetime module of the Python Standard Library. Before you start solving the actual problem, you should check yourself how Python responds when you successively enter the following instructions in an interactive Python session:

  1. >>> from datetime import date
    >>> birthday = date(1983, 1, 14)
    >>> d = date.today() - birthday
    >>> type(d)
    >>> d.days

  2. >>> from datetime import timedelta
    >>> birthday + timedelta(1)
    >>> day1 = birthday + timedelta(1)
    >>> day1
    >>> day2 = day1 + timedelta(1)
    >>> day2

  3. >>> today = date.today()
    >>> today
    >>> today.weekday()
    >>> tomorrow = today + timedelta(1)
    >>> tomorrow.weekday()
    >>> tomorrow.day

Make sure you understand the meaning of all results generated with these code fragments.

Description

Friday the 13th is considered an unlucky day in Western superstition. According to folklorists, there is no written evidence for a "Friday the 13th" superstition before the 19th century. Several theories have been proposed about the origin of the Friday the 13th superstition. One theory states that it is a modern amalgamation of two older superstitions: that thirteen is an unlucky number and that Friday is an unlucky day.

But the combination of Friday and the number thirteen might not be as universal as you may think. In Spanish-speaking countries, instead of Friday, Tuesday the 13th (martes trece) is considered a day of bad luck. The Greeks also consider Tuesday (and especially the 13th) to be an unlucky day. Tuesday is considered to be dominated by the influence of Ares, the god of war. In Italian popular culture, Friday the 17th (and not the 13th) is considered a day of bad luck. In fact, in Italy, 13 is generally considered a lucky number.

Assignment

Write a function luckyperiode that takes one mandatory argument and three optional arguments. The mandatory argument specifies a start date, and the optional arguments respectively specify an end date, and day number and a weekday number. The function must return the maximal number of successive days without "unlucky days" in between the start date and the end date (both days are included in the period under investigation). Unlucky days are determined by the given day number and weekday number. If the day number is 13 and the weekday number is 1, then Tuesday the 13th is considered to be the unlucky day. If only the mandatory argument is given, the number of days in the longest period without a Friday the 13th in between the start date and today is returned. If the start date is more recent than the end date, there are no days in between the start date and the end date, en the function logically should return the value 0.

Example

>>> from datetime import date
>>> luckyperiod(date(2012, 1, 1), date(2012, 12, 31))
171
>>> luckyperiod(date(2012, 1, 1), date(2012, 12, 31), 14, 5)
170
>>> luckyperiod(date(2012, 1, 1), date(2012, 12, 31), 17, 4)
182
>>> luckyperiod(date(2012, 1, 1), date(2012, 12, 31), 13, 1)
245
>>> luckyperiod(startdate=date(2012, 1, 1), daynumber = 1, weekdaynumber = 0, enddate = date(2012, 12, 31))
275