PROG0596 - Decathlon

no tags 

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.

decathlon
A decathlon combines four runs, three jumps and three throws.

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 Athlete" 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.

Assignment

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.

Example

The following interactive session assumes that the text files parameters.txt and performances.txt 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

De decatlon of tienkamp is een gecombineerd atletieknummer dat bestaat uit tien verschillende disciplines. De term vindt zijn oorsprong in de samenstelling van de Griekse woorden δέκα (déka, wat "tien" betekent) en ἄθλος (áthlos, of ἄθλον, áthlon, wat "daad, prestatie" betekent). Een tienkamp wordt doorgaans enkel door mannelijk atleten bestreden, terwijl vrouwelijke atleten elkaar bekampen in een heptatlon of zevenkamp.

decathlon
De tienkamp combineert vier loopnummers, drie sprongnummers en drie werpnummers.

De atletieknummers van een tienkamp worden afgewerkt over twee opeenvolgende dagen en de winnaars worden bepaald op basis van de prestaties over alle disciplines heen. De prestaties worden beoordeeld volgens een puntensysteem voor elke individuele discipline, en niet volgens de rangschikking voor de individuele nummers. Hierbij worden voor elke discipline drie parameters $a$, $b$ en $c \in \mathbb{R}^{+}$ vastgelegd, en wordt de score voor een prestatie $p$ voor die discipline berekend op basis van de volgende formules:

  • score = $\left \lfloor{a(b - p)^c}\right \rfloor $ voor loopnummers (snellere tijd resulteert in een hogere score)
  • score = $\left \lfloor{a(p - b)^c}\right \rfloor $ voor sprong- en werpnummers (grotere afstand of hoogte resulteert in een hogere score)

Hierbij staat $\left \lfloor{x}\right \rfloor \in \mathbb{N}$ voor het geheel deel van $x \in \mathbb{R}^{+}$ en wordt de prestatie $p$ voor een bepaalde discipline uitgedrukt in seconden (loopnummers), meters (werpnummers) of centimeters (sprongnummers). Voor de olympische tienkamp wordt bijvoorbeeld gebruik gemaakt van de volgende parameters:

discipline $a$ $b$ $c$
100 m 25.4347 18 1.81
verspringen 0.14354 220 1.4
kogelstoten 51.39 1.5 1.05
hoogspringen 0.8465 75 1.42
400 m 1.53775 82 1.81
110 m horden 5.74352 28.5 1.92
discuswerpen 12.91 4 1.1
polsstokspringen 0.2797 100 1.35
speerwerpen 10.14 7 1.08
1500 m 0.03768 480 1.85

De traditie wil dat de atleet die de olympische tienkamp wint voor vier jaar de titel van "Beste Atleet ter Wereld" mag dragen. Aan de basis hiervoor lag de uitspraak van koning Gustav V van Zweden bij de overwinning van Jim Thorpe in de tienkamp tijdens de olympische spelen van 1912 in Stockholm: "You, sir, are the world's greatest athlete". De huidige wereldrecordhouder in de tienkamp is de Amerikaan Ashton Eaton, die 9045 punten behaalde bij het IAAF Wereldkampioenschap in 2015.

Opgave

In deze opgave werken we met twee soorten tekstbestanden, die regels bevatten met velden die van elkaar gescheiden worden door komma's (,). Tekstbestanden met parameters voor disciplines beschrijven de parameters die gebruikt worden voor de berekening van de score voor een bepaalde discipline. Elke regel bevat de naam van een discipline, de parameters $a$, $b$ en $c \in \mathbb{R}^{+}$, en een veld met daarin de tekst ja indien het een loopnummer betreft en de tekst neen indien het een sprong- of werpnummer betreft. Voor de disciplines van de tienkamp ziet het tekstbestand er bijvoorbeeld als volgt uit.

100 m,25.4347,18,1.81,ja
verspringen,0.14354,220,1.4,neen
kogelstoten,51.39,1.5,1.05,neen
hoogspringen,0.8465,75,1.42,neen
400 m,1.53775,82,1.81,ja
110 m horden,5.74352,28.5,1.92,ja
discuswerpen,12.91,4,1.1,neen
polsstokspringen,0.2797,100,1.35,neen
speerwerpen,10.14,7,1.08,neen
1500 m,0.03768,480,1.85,ja

Tekstbestanden met prestaties voor disciplines beschrijven de prestaties die een atleet geleverd heeft voor een aantal disciplines van een meerkamp (bijvoorbeeld een decatlon of een heptatlon). Elke regel bevat de naam van een discipline en de prestatie van de atleet voor die discipline. De prestaties worden uitgedrukt in seconden (loopnummers), meters (werpnummers) of centimeters (sprongnummers). Voor een reeks extreem goede prestaties voor de disciplines van de tienkamp (alle huidige wereldrecords van de individuele nummers) ziet het tekstbestand er bijvoorbeeld als volgt uit.

100 m,9.58
verspringen,895
kogelstoten,23.12
hoogspringen,245
400 m,43.18
110 m horden,12.87
discuswerpen,74.08
polsstokspringen,614
speerwerpen,98.48
1500 m,206.00

Gevraagd wordt:

  • Schrijf een functie parameters waaraan de locatie van een tekstbestand met parameters voor disciplines moet doorgegeven worden. De functie moet een dictionary teruggeven die de naam van elke discipline uit het bestand afbeeldt op een tuple met de parameters voor die discipline: de waarden $a$, $b$ en $c$ als drie floating point getallen en een Booleaanse waarde die aangeeft of de discipline een loopnummer is.
  • Schrijf een functie score waaraan drie argumenten moeten doorgegeven worden: de naam van een discipline (een string), een prestatie voor die discipline (een int of een float) en een dictionary die de namen van een aantal disciplines afbeeldt op tuples met de parameters voor die disciplines (conform de dictionaries die teruggegeven worden door de functie parameters). De functie moet de score (een int) teruggeven die bekomen wordt op basis van de gegeven prestatie voor de gegeven discipline, berekend volgens de scoreberekening die voor de tienkamp gebruikt wordt. Hiervoor moet de functie gebruik maken van de parameters voor de disciplines uit de gegeven dictionary. Indien de gegeven dictionary de parameters van de gegeven discipline niet bevat, dan moet de functie een AssertionError opwerpen met de boodschap ongeldige discipline.
  • Schrijf een functie eindscore waaraan de locaties van twee tekstbestanden moeten doorgegeven worden: een bestand met prestaties voor disciplines en een bestand met parameters voor disciplines. De functie moet de totale score (een int) teruggeven op basis van alle prestaties voor de disciplines uit het eerste tekstbestand. Indien dit tekstbestand een discipline bevat waarvan de parameters niet worden beschreven in het tweede tekstbestand, dan moet de functie een AssertionError opwerpen met de boodschap ongeldige discipline.

Voorbeeld

Bij onderstaande voorbeeldsessie gaan we ervan uit dat de tekstbestanden parameters.txt en prestaties.txt zich in de huidige directory bevinden.

>>> params = parameters('parameters.txt')
>>> params['100 m']
(25.4347, 18.0, 1.81, True)
>>> params['polsstokspringen']
(0.2797, 100.0, 1.35, False)

>>> score('100 m', 9.58, params)
1202
>>> score('polsstokspringen', 614, params)
1277
>>> score('diepzeeduiken', 614, params)
Traceback (most recent call last):
AssertionError: ongeldige discipline

>>> eindscore('prestaties.txt', 'parameters.txt')
12544


Added by:Peter Dawyndt
Date:2015-12-01
Time limit:10s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:PY_NBC