Preparation

In this exercise you can make use of the data types date and timedelta that are defined in the datetime1 module. This module is part of the Python Standard Library2. Before working on the actual assignment, you may first have a look at 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 why a particular result is generated.

Description

According to believers in biorhythms, a person's life is influenced by rhythmic biological cycles that affect one's ability in various domains, such as mental, physical and emotional activity. These cycles begin at birth and oscillate in a steady (sine wave) fashion throughout life. By modeling the cycles mathematically, a person's level of ability in each of these domains can be predicted from day to day. All three cycles have a different period, as biorhythmics assumes that a person does not have 100% all of his abilities at every moment. Instead, his availabilities fluctuate in a cyclic manner similar to most natural processes. The availability of a particular ability increases, reaches a maximum, and then again declines until a minimum is reached. When this cyclic process is projected onto a continuous line, a chain of sinusoids is obtained. The three biorhythmic cycles are: 

cycle period (days) function
physical 23 $$\sin(\frac{2 \pi t}{23})$$
emotional 28 $$\sin(\frac{2 \pi t}{28})$$
intellectual 33 $$\sin(\frac{2 \pi t}{33})$$

The expression $$\sin(\frac{2 \pi t}{a})$$ represents a sine wave with period $$a$$, where the sine function $$\sin$$ only takes values in the interval $$[-1,1]$$. In applying this expression for the calculation of the biorhythm, $$t$$ indicates the age of a person expressed as a given number of days. At birth ($$t = 0$$) all three cycles of a person start at value 0. As such, the score for someone's physical, emotional and intellectual cycle is initially 0%. After one year ($$t = 365$$) the same person has a physical score of -73%, an emotional score of 22%, and an intellectual score of 37%. The biorhythm hypotheses originates from the beginning of the 20th century and was very popular during the seventies. As this theory never has been proven statistically, these days it is most popular on websites that also test your paranormal abilities.

Bioritme
The physical, emotional and intellectual cycles that determine the biorhythm of a person born at the 13th of May, 2012. All curves start at the day of birth ($$t = 0$$) with an initial value of 0.

Assignment

Write a function biorhythm that returns the physical, emotional and intellectual score of a person with a given birthday (mandatory first parameter) at a particular date (optional second parameter). In case no value is explicitly passed to the second parameter, the function must compute the biorhythm scores for the current day. Dates are passed to the function in string representation of the format dd-mm-yyyy. As such, the date September 26, 2013 is passed as the string 26-09-2013. The scores must be returned as a tuple containing three integers, where each score is expressed as a percentage that is rounded to the nearest integer.

Example

>>> biorhythm('08-02-2007', '08-02-2008')  # 365 days
(-73, 22, 37)

>>> biorhythm('26-03-1985', '14-08-2009')
(100, 62, -54)

>>> biorhythm('06-08-1945', '14-11-2013')
(98, -62, -87)

>>> biorhythm('09-08-1945')                # calculated at 12-11-2013
(0, 43, -10)                               # do not include in doctest !!

Pay attention! The value returned by evaluating biorhythm('09-08-1945') is dependent on the current day. Today's result will thus be different from tomorrow's result. As such, we cannot include this test case directly into a doctest. The following rewritten version of the test case however can be used in a doctest:

>>> from datetime import date
>>> today = '{t.day}-{t.month}-{t.year}'.format(t=date.today())
>>> biorhythm('09-08-1945') == biorhythm('09-08-1945', today)
True
>>> biorhythm('18-03-1994') == biorhythm('18-03-1994', today)
True

Epilogue

stop sine
A sine function that stops after one period is called a stop sine.