PROG0290 - Lucky period

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

Voorbereiding

Voor deze opgave moet je gebruik maken van de gegevenstypes date en timedelta die gedefinieerd worden in de module datetime van de Python Standard Library. Voor je aan de eigenlijke opgave begint, kan je best eerst nagaan hoe Python reageert als je achtereenvolgens de volgende instructies uitvoert binnen een interactieve Python sessie:

  1. >>> from datetime import date
    >>> geboortedatum = date(1983, 1, 14)
    >>> d = date.today() - geboortedatum
    >>> type(d)
    >>> d.days
    
  2. >>> from datetime import timedelta
    >>> geboortedatum + timedelta(1)
    >>> dag1 = geboortedatum + timedelta(1)
    >>> dag1
    >>> dag2 = dag1 + timedelta(1)
    >>> dag2
    
  3. >>> vandaag = date.today()
    >>> vandaag
    >>> vandaag.weekday()
    >>> morgen = vandaag + timedelta(1)
    >>> morgen.weekday()
    >>> morgen.day
    

Zorg er zeker voor dat je begrijpt waarom de verschillende resultaten gegenereerd worden.

Omschrijving

Vrijdag de dertiende wordt in onze cultuur algemeen gezien als een ongeluksdag. Vanwaar dit bijgeloof komt, is niet direct geweten en hoewel er allerlei verklaringen zijn in verschillende godsdiensten en gebruiken, lijkt het er op dat dit bijgeloof nog maar hooguit een dikke 100 jaar oud is.

Ook is de combinatie van vrijdag en het getal dertien niet zo universieel als je wel zou denken. In ondere andere België, Nederland en Engeland wordt inderdaad vrijdag de dertiende als ongeluksdag gezien. In Griekenland, Spanje en Latijns-Amerika is het echter dinsdag de dertiende dat een ongeluksdag zou moeten zijn. In Italië is iedereen dan weer extra voorzichtig op vrijdag de zeventiende.

Opgave

Schrijf een functie geluksperiode die één verplicht argument heeft en drie optionele argumenten. Het verplichte argument geeft een startdatum en de optionele argumenten geven respectievelijk een einddatum, een dagnummer en een weekdagnummer. De functie moet als resultaat het maximaal aantal opeenvolgende dagen zonder "ongeluksdag" tussen de begindatum en de einddatum (inclusief de eindpunten) teruggeven. De ongeluksdag wordt bepaald door het opgegeven dagnummer en de opgegeven weekdag. Als het dagnummer 13 is en het weekdagnummer is 1 dan wordt dinsdag de dertiende als ongeluksdag beschouwd. Als enkel het verplichte argument wordt meegegeven, wordt het aantal dagen in de langste periode zonder vrijdag de dertiende tussen de begindatum en vandaag berekend. Indien de begindatum recenter is dan de einddatum, dan liggen er geen dagen tussen de begindatum en de einddatum, en moet de functie logischerwijs de waarde 0 als resultaat teruggeven.

Voorbeeld

>>> from datetime import date
>>> geluksperiode(date(2012, 1, 1), date(2012, 12, 31))
171
>>> geluksperiode(date(2012, 1, 1), date(2012, 12, 31), 14, 5)
170
>>> geluksperiode(date(2012, 1, 1), date(2012, 12, 31), 17, 4)
181
>>> geluksperiode(date(2012, 1, 1), date(2012, 12, 31), 13, 1)
244
>>> geluksperiode(
...     date(2012, 1, 1),
...     dagnummer = 1, 
...     weekdagnummer = 0, 
...     einddatum = date(2012, 12, 31)
... )
274

Added by:Peter Dawyndt
Date:2012-11-07
Time limit:60s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:PY_NBC
Resource:None

© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.