The decathlon is a combined event in athletics consisting of ten track and field events. The word decathlon is of Greek origin, from δέκα (déka, meaning "ten") and ἄθλος (áthlos, or ἄθλον, áthlon, meaning "feat"). The decathlon is mainly contested by male athletes, while female athletes typically compete in the heptathlon.
All events of a decathlon are held over two consecutive days and the winners are determined by the combined performance in all. Performance is judged on a points system in each event, not by the position achieved. The point system uses three parameters $$a$$, $$b$$ and $$c \in \mathbb{R}^{+}$$ that vary by discipline. A performance $$p$$ for a given discipline is scored using the following formulae:
score = $$\left \lfloor{a(b - p)^c}\right \rfloor $$ for track events (faster time produces a better score)
score = $$\left \lfloor{a(p - b)^c}\right \rfloor $$ for field events (greater distance or height produces a better score)
The notation $$\left \lfloor{x}\right \rfloor \in \mathbb{N}$$ denotes the integer part of $$x \in \mathbb{R}^{+}$$ and the performance $$p$$ by the athlete for a given discipline is measured in seconds (running), meters (throwing) or centimeters (jumping). The Olympic decathlon, for example, uses the parameters in the following table:
discipline | $$a$$ | $$b$$ | $$c$$ |
---|---|---|---|
100 m | 25.4347 | 18 | 1.81 |
long jump | 0.14354 | 220 | 1.4 |
shot put | 51.39 | 1.5 | 1.05 |
high jump | 0.8465 | 75 | 1.42 |
400 m | 1.53775 | 82 | 1.81 |
110 m hurdles | 5.74352 | 28.5 | 1.92 |
discus throw | 12.91 | 4 | 1.1 |
pole vault | 0.2797 | 100 | 1.35 |
javelin throw | 10.14 | 7 | 1.08 |
1500 m | 0.03768 | 480 | 1.85 |
Traditionally, the title of "World's Greatest Athlete1" has been given to the man who wins the Olympic decathlon. This began when King Gustav V of Sweden told Jim Thorpe, "You, sir, are the world's greatest athlete" after Thorpe won the decathlon at the Stockholm Olympics in 1912. The current decathlon world record holder is American Ashton Eaton, who scored 9045 points at the 2015 IAAF World Championships.
In this assignment we will process two types of text files that both have lines containing comma-separated fields. Text files containing parameters for disciplines define the parameters that are used to compute the score for a series of disciplines. Each line contains the name of a discipline, the parameters $$a$$, $$b$$ en $$c \in \mathbb{R}^{+}$$, and a field containing the text yes for track events and no for field events. For the disciplines of the decathlon, such a text file might look like this.
100 m,25.4347,18,1.81,yes long jump,0.14354,220,1.4,no shot put,51.39,1.5,1.05,no high jump,0.8465,75,1.42,no 400 m,1.53775,82,1.81,yes 110 m hurdles,5.74352,28.5,1.92,yes discus throw,12.91,4,1.1,no pole vault,0.2797,100,1.35,no javelin throw,10.14,7,1.08,no 1500 m,0.03768,480,1.85,yes
Text files containing performances for disciplines define the performances of an athlete in a combined event in athletics (a decathlon or a heptathlon, for example). Each line contains the name of a discipline and the performance of the athlete for that discipline. Performances are expressed in seconds (running), meters (throwing) or centimeters (jumping). For a sequence of extremely good performances for the disciplines of the decathlon (all current world records for each of the individual disciplines) the text file might look like this.
100 m,9.58 long jump,895 shot put,23.12 high jump,245 400 m,43.18 110 m hurdles,12.87 discus throw,74.08 pole vault,614 javelin throw,98.48 1500 m,206.00
Your task:
Write a function parameters that takes the location of a text file containing parameters for disciplines. The function must return a dictionary that maps the name of each discipline in the text file onto a tuple containing the parameters for that discipline: the values $$a$$, $$b$$ and $$c$$ as three floating point numbers and a Boolean value indicating whether or not the discipline is a track event.
Write a function score that takes three arguments: the name of a discipline (a string), a performance for that discipline (an int or a float) and a dictionary that maps names of disciplines onto tuples containing the parameters for these disciplines (according to the format of the dictionaries returned by the function parameters). The function must return the score (an int) obtained for the given performance for the given discipline, computed according to the point system used for the decalthlon. To this end, the function must make use of the parameters for the disciplines as defined by the given dictionary. In case the given dictionary does not contain the parameters of the given discipline, the function must raise an AssertionError with the message invalid discipline.
Write a function finalScore that takes the location of two text files: a file containing performances for disciplines and a file containing parameters for disciplines. The function must return the total score (an int) obtained for all performances for the disciplines in the first text file. In case this text file contains a discipline whose parameters cannot be retrieved from the second text file, the function must raise an AssertionError with the message invalid discipline.
The following interactive session assumes that the text files parameters.txt2 and performances.txt3 are located in the current directory.
>>> params = parameters('parameters.txt')
>>> params['100 m']
(25.4347, 18.0, 1.81, True)
>>> params['pole vault']
(0.2797, 100.0, 1.35, False)
>>> score('100 m', 9.58, params)
1202
>>> score('pole vault', 614, params)
1277
>>> score('scuba diving', 614, params)
Traceback (most recent call last):
AssertionError: invalid discipline
>>> finalScore('performances.txt', 'parameters.txt')
12544