PROG0367 - World Cup soccer

no tags 

Belgium has participated eleven times in the finals of the World Cup soccer, and will return on the WC2014 for the first time since its last qualification for the finals of the WC2002 in Japan and South Korea. The finals of a World Cup always begins with a group stage, where the participating countries are distributed over a number of groups. The number of groups and the number of countries per group differs between championships, but each group is always assigned a unique label in the form of a capital letter of the alphabet. Within each group, each country plays once against each of the other countries.

Your task is to generate an overview of the standings in a given group based on the results of a number of games played in the group stage of a World Cup. The list of matches played is stored in a text file containing the following information for each match on a separate line: i) name of the home team, ii) name of the visiting team, iii) final score of the match, and iv) the capital letter assigned to the group of both teams (countries). The individual information fields about a match are separated by commas and the final score is given as the number of goals scored by each team, separated by a hyphen (-). In addition, the file with the matches played may also contain empty lines and comment lines starting with a pound sign (#). Such lines must be ignored.

See the examples below to get an idea of the format that needs to be used to generate an overview of the standings for a particular group. The overview should display the following statistics for each country in the group: i) number of matches played (P), ii) number of matches won (W), iii) number of matches lost (L), iv) number of matches that ended in a draw (D), v) number of goals scored (F), vi) number of goals conceded (A), vii) goal difference (S) and viii) number of points (Pts). The goal difference is the number of goals scored minus the number of goals conceded. A team gets three points for each match won and a single point for each match that ended in a draw. The overview must list the countries in the group in sorted order, first according to decreasing number of points, then according to decreasing goal difference, and finally in increasing alphabetic order according to the name of the country. In addition, the format should look like in the examples shown below, where a table is drawn and the label of the group is centred on top of the table.

Assignment

  • Write a function processMatches that takes the location of a text file as its argument. This file must contain the statistics of the matches in the group stage of a World Cup, according to the format outlined in the introduction. The function should return a dictionary, that maps each group label to another dictionary. The latter dictionary must map each country in the group onto a list of five integers that respectively indicate the following statistics for the country: i) number of matches won, ii) number of matches lost, iii) number of matches that ended in a draw, iv) number of goals scored, and v) number of goals conceded.
  • Write a function showGroup that returns an overview of the standings in a given group. The first argument passed to the function must be a dictionary that takes the form of the dictionaries returned by the function processMatches. As a second argument, the function takes a capital letter that is used as a group label. By default, the function must print the overview of the standings in the group. However, if a third argument is passed to the function, the function must write the overview to a text file having the name given as the third argument.

Example

In the following interactive session we assume that the text file worldcup2010.txt is located in the current directory.

>>> stats = processMatches('worldcup2010.txt')
>>> stats['A']
{'Uruguay': [1, 1, 1, 3, 2], 'Mexico': [0, 2, 1, 1, 5], 'France': [2, 0, 1, 6, 2], 'South Africa': [1, 1, 1, 3, 4]}
>>> stats['B']
{'Argentina': [2, 1, 0, 3, 2], 'Greece': [3, 0, 0, 3, 0], 'Nigeria': [1, 2, 0, 2, 3], 'South Korea': [0, 3, 0, 0, 3]}
>>> stats['F']
{'Paraguay': [2, 0, 1, 5, 2], 'Slovakia': [0, 0, 3, 3, 3], 'New Zealand': [0, 2, 1, 2, 6], 'Italy': [1, 1, 1, 4, 3]}
>>> showGroup(stats, 'A')
                            GROUP A                            
+-----------------------+-----+-------------------------+-----+
|                       |   P |   W   L   D   F   A   S | Pts |
+-----------------------+-----+-------------------------+-----+
|                France |   3 |   2   0   1   6   2   4 |   7 |
|               Uruguay |   3 |   1   1   1   3   2   1 |   4 |
|          South Africa |   3 |   1   1   1   3   4  -1 |   4 |
|                Mexico |   3 |   0   2   1   1   5  -4 |   1 |
+-----------------------+-----+-------------------------+-----+
>>> showGroup(stats, 'D')
                            GROUP D                            
+-----------------------+-----+-------------------------+-----+
|                       |   P |   W   L   D   F   A   S | Pts |
+-----------------------+-----+-------------------------+-----+
|               Germany |   3 |   2   1   0   5   1   4 |   6 |
|                 Ghana |   3 |   1   1   1   3   3   0 |   4 |
|                Serbia |   3 |   1   1   1   3   4  -1 |   4 |
|             Australia |   3 |   1   2   0   3   6  -3 |   3 |
+-----------------------+-----+-------------------------+-----+
>>> showGroup(stats, 'F', 'groupF.txt')

België heeft elf keer deelgenomen aan de eindronde van het wereldkampioenschap voetbal, en zal er op het WK2014 in Brazilië voor het eerst terug bij zijn nadat het zich voor het laatst had kunnen kwalificeren voor de eindronde van het WK2002 in Japan en Zuid-Korea. De eindronde van een wereldkampioenschap begint steeds met een poulefase, waarbij de deelnemende landen over een aantal poules verdeeld worden. Het aantal poules en het aantal landen per poule varieert van kampioenschap tot kampioenschap, maar elke poule krijgt steeds een uniek label toegekend onder de vorm van een hoofdletter uit het alfabet. Binnen elke poule speelt elk land eenmaal tegen elk ander land.

We vragen je om op basis van de uitslagen van een aantal gespeelde wedstrijden een overzicht op te maken van de stand binnen een bepaalde poule. De lijst van gespeelde wedstrijden is opgeslagen in een tekstbestand waarin voor elke wedstrijd op een afzonderlijke regel de volgende informatie gegeven wordt: i) naam van de thuisploeg, ii) naam van de bezoekende ploeg, iii) eindstand van de wedstrijd en iv) de hoofdletter waarmee de poule van de ploegen (landen) gelabeld werd. De verschillende informatievelden van een wedstrijd worden gescheiden door komma's en bij de eindstand wordt het aantal gescoorde doelpunten van beide ploegen gescheiden door een koppelteken (-). Het bestand met de gespeelde wedstrijden bevat verder mogelijks ook lege regels of commentaarregels die starten met hekje (#). Dergelijke regels moeten genegeerd worden.

Bekijk onderstaande voorbeelden om een idee te krijgen van het formaat waarmee een overzicht van de stand voor een bepaalde poule moet opgemaakt worden. Het overzicht moet voor elk land uit de poule de volgende statistieken weergeven: i) aantal gespeelde wedstrijden (P), ii) aantal gewonnen wedstrijden (W), iii) aantal verloren wedstrijden (L), iv) aantal wedstrijden die op een gelijkspel geëindigd zijn (D), v) aantal gescoorde doelpunten (F), vi) aantal tegendoelpunten (A), vii) doelsaldo (S) en viii) aantal punten (Pts). Het doelsaldo is het aantal gescoorde doelpunten min het aantal tegendoelpunten. Voor elke gewonnen wedstrijd krijgt een ploeg drie punten, en voor elk gelijkspel één punt. Bij de weergave moeten de landen binnen een poule gesorteerd worden, eerst volgens dalend aantal punten, daarna volgens dalend doelsaldo en daarna alfabetisch volgens de naam van het land. Verder moet de opmaak er uitzien zoals in de voorbeelden wordt weergegeven, waarbij een tabel getekend wordt en het label van de poule gecentreerd boven de tabel geplaatst wordt.

Opgave

  • Schrijf een functie verwerkWedstrijden waaraan de locatie van een bestand moet doorgegeven worden. Dit bestand bevat de uitslagen van de wedstrijden uit de poulefase van een wereldkampioenschap voetbal, volgens het formaat dat in de inleiding beschreven werd. De functie moet een dictionary teruggeven, die elk label van een poule afbeeldt op een dictionary. Deze laatste dictionary moet de landen uit de poule afbeelden op een lijst waarin vijf natuurlijke getallen staan die respectievelijk de volgende statistieken van het land aangeven: i) aantal gewonnen wedstrijden, ii) aantal verloren wedstrijden, iii) aantal wedstrijden die op een gelijkspel geëindigd zijn, iv) aantal gescoorde doelpunten en v) aantal tegendoelpunten.
  • Schrijf een functie toonPoule die een overzicht van een gegeven poule teruggeeft. Aan deze functie moet als eerste argument een dictionary doorgeven worden, die opgebouwd is zoals de dictionaries die door de functie verwerkWedstrijden teruggegeven worden. Als tweede argument moet aan de functie de hoofdletter doorgegeven worden waarmee de poule gelabeld werd. Standaard moet de functie het overzicht van de poule uitschrijven. Als aan de functie echter een derde argument doorgegeven wordt, dan moet de functie het overzicht wegschrijven naar een bestand met de opgegeven bestandsnaam.

Voorbeeld

Bij onderstaande voorbeeldsessie gaan we ervan uit dat het bestand wereldbeker2010.txt zich in de huidige directory bevindt.

>>> statistieken = verwerkWedstrijden('wereldbeker2010.txt')
>>> statistieken['A']
{'Uruguay': [1, 1, 1, 3, 2], 'Mexico': [0, 2, 1, 1, 5], 'France': [2, 0, 1, 6, 2], 'South Africa': [1, 1, 1, 3, 4]}
>>> statistieken['B']
{'Argentina': [2, 1, 0, 3, 2], 'Greece': [3, 0, 0, 3, 0], 'Nigeria': [1, 2, 0, 2, 3], 'South Korea': [0, 3, 0, 0, 3]}
>>> statistieken['F']
{'Paraguay': [2, 0, 1, 5, 2], 'Slovakia': [0, 0, 3, 3, 3], 'New Zealand': [0, 2, 1, 2, 6], 'Italy': [1, 1, 1, 4, 3]}
>>> toonPoule(statistieken, 'A')
                            POULE A                            
+-----------------------+-----+-------------------------+-----+
|                       |   P |   W   L   D   F   A   S | Pts |
+-----------------------+-----+-------------------------+-----+
|                France |   3 |   2   0   1   6   2   4 |   7 |
|               Uruguay |   3 |   1   1   1   3   2   1 |   4 |
|          South Africa |   3 |   1   1   1   3   4  -1 |   4 |
|                Mexico |   3 |   0   2   1   1   5  -4 |   1 |
+-----------------------+-----+-------------------------+-----+
>>> toonPoule(statistieken, 'D')
                            POULE D                            
+-----------------------+-----+-------------------------+-----+
|                       |   P |   W   L   D   F   A   S | Pts |
+-----------------------+-----+-------------------------+-----+
|               Germany |   3 |   2   1   0   5   1   4 |   6 |
|                 Ghana |   3 |   1   1   1   3   3   0 |   4 |
|                Serbia |   3 |   1   1   1   3   4  -1 |   4 |
|             Australia |   3 |   1   2   0   3   6  -3 |   3 |
+-----------------------+-----+-------------------------+-----+
>>> toonPoule(statistieken, 'F', 'pouleF.txt')


Added by:Peter Dawyndt
Date:2013-04-04
Time limit:10s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:PY_NBC
Resource:None