PROG0240 - Olympics

no tags 

The Olympics traditionally raise a feeling of national pride in many countries. All possible lists are kept with established records and everybody brags about the amount of medals their country has won.

Assignment

  1. Write a function medalSummary to which two arguments must be given. The first, obligatory argument is a list of tuples as explained below. The second, optional, argument has the values gold, silver and bronze. If this second argument is given, only the medals of the given kind are counted. If there is no second argument, all medals are counted. The function must print a dictionary that links every country code in the list to the medals that country has won.

    As a first argument, you receive a list of tuples, where each tuple contains a number of fields with information about the medal won. For example, you can see a (shortened) list below with information about all medals that were won during the 2008 Olympics in Beijing in fencing. Every tuple in the list contains 6 fields: a country code, the country, the sport, the discipline, the medal and the athletes.
    [('CN', 'China', 'Fencing', "Men's Individual Sabre", 'gold', 'ZHONG Man'),
     ('CN', 'China', 'Fencing', "Women's Team Sabre", 'silver', 'NI Hong, ...'),
     ('ES', 'Spain', 'Fencing', "Men's Individual Epee", 'bronze', 'ABAJO Jose Luis'),
     ('FR', 'France', 'Fencing', "Men's Individual Epee", 'silver', 'JEANNET Fabrice'),
     ('FR', 'France', 'Fencing', "Men's Individual Sabre", 'silver', 'LOPEZ Nicolas'),
     ('FR', 'France', 'Fencing', "Men's Team Epee", 'gold', 'JEANNET Fabrice,...'),
     ('FR', 'France', 'Fencing', "Men's Team Sabre", 'gold', 'PILLET Julien, ...'),
     ('DE', 'Germany', 'Fencing', "Men's Individual Foil", 'gold', 'KLEIBRINK Benjamin Philip'),
     ('DE', 'Germany', 'Fencing', "Women's Individual Epee", 'gold', 'HEIDEMANN Britta'),
     ('HU', 'Hungary', 'Fencing', "Women's Individual Epee", 'bronze', 'MINCZA-NEBALD Ildiko'),
     ('IT', 'Italy', 'Fencing', "Men's Individual Foil", 'bronze', 'SANZO Salvatore'),
     ('IT', 'Italy', 'Fencing', "Women's Individual Foil", 'gold', 'VEZZALI Maria Valentina'),
     ('IT', 'Italy', 'Fencing', "Women's Individual Foil", 'bronze', 'GRANBASSI Margherita'),
     ('IT', 'Italy', 'Fencing', "Men's Individual Epee", 'gold', 'TAGLIARIOL Matteo'),
     ('IT', 'Italy', 'Fencing', "Men's Team Epee", 'bronze', 'ROTA Alfredo, ...'),
     ('IT', 'Italy', 'Fencing', "Men's Team Sabre", 'bronze', 'MONTANO Aldo, ...'),
     ('IT', 'Italy', 'Fencing', "Women's Team Foil", 'bronze', 'GRANBASSI Margherita, ...'),
     ('JP', 'Japan', 'Fencing', "Men's Individual Foil", 'silver', 'OTA Yuki'),
     ('KR', 'Korea', 'Fencing', "Women's Individual Foil", 'silver', 'NAM Hyunhee'),
     ('PL', 'Poland', 'Fencing', "Men's Team Epee", 'silver', 'ANDRZEJUK Robert, ...'),
     ('RO', 'Romania', 'Fencing', "Women's Individual Epee", 'silver', 'BRANZA Ana Maria'),
     ('RO', 'Romania', 'Fencing', "Men's Individual Sabre", 'bronze', 'COVALIU Mihai'),
     ('RU', 'Russian Fed.', 'Fencing', "Women's Team Foil", 'gold', 'LAMONOVA Evgenia, ...'),
     ('UA', 'Ukraine', 'Fencing', "Women's Team Sabre", 'gold', 'KHOMROVA Olena, ...'),
     ('US', 'United States', 'Fencing', "Women's Individual Sabre", 'gold', 'ZAGUNIS Mariel'),
     ('US', 'United States', 'Fencing', "Women's Individual Sabre", 'silver', 'JACOBSON Sada'),
     ('US', 'United States', 'Fencing', "Women's Individual Sabre", 'bronze', 'WARD Becca'),
     ('US', 'United States', 'Fencing', "Men's Team Sabre", 'silver', 'MOREHOUSE Tim, ...'),
     ('US', 'United States', 'Fencing', "Women's Team Foil", 'silver', 'CROSS Emily, ...'),
     ('US', 'United States', 'Fencing', "Women's Team Sabre", 'bronze', 'JACOBSON Sada, ...')]
    
  2. Write a function top that takes a dictionary as an obligatory argument as it is printed in the function medalSummary, and an optional argument called n and a number as a value, that equals 10 as a standard. As a result, this function prints a list of tuples that contains a top n of countries according to their medals. This list must be ordered so that the first element on the list has the most medals. The tuples contain the country code as a first element and the amount of medals as a second.

Take a look at the example below to avoid indistinctness about the working of the function. You can also download a list with all medals from the 2008 Olympics in Beijing as the file beijing_medals.txt.

Example

>>> fencing = [
...     ('CN', 'China', 'Fencing', "Men's Individual Sabre", 'gold', 'ZHONG Man'),
...     ('CN', 'China', 'Fencing', "Women's Team Sabre", 'silver', 'NI Hong, TAN Xue, HUANG Haiyang, BAO Yingying'),
...     ('ES', 'Spain', 'Fencing', "Men's Individual Epee", 'bronze', 'ABAJO Jose Luis'),
...     ('FR', 'France', 'Fencing', "Men's Individual Epee", 'silver', 'JEANNET Fabrice'),
...     ('FR', 'France', 'Fencing', "Men's Individual Sabre", 'silver', 'LOPEZ Nicolas'),
...     ('FR', 'France', 'Fencing', "Men's Team Epee", 'gold', 'JEANNET Fabrice, JEANNET Jerome, ROBEIRI Ulrich'),
...     ('FR', 'France', 'Fencing', "Men's Team Sabre", 'gold', 'PILLET Julien, SANSON Boris, LOPEZ Nicolas'),
...     ('DE', 'Germany', 'Fencing', "Men's Individual Foil", 'gold', 'KLEIBRINK Benjamin Philip'),
...     ('DE', 'Germany', 'Fencing', "Women's Individual Epee", 'gold', 'HEIDEMANN Britta'),
...     ('HU', 'Hungary', 'Fencing', "Women's Individual Epee", 'bronze', 'MINCZA-NEBALD Ildiko'),
...     ('IT', 'Italy', 'Fencing', "Men's Individual Foil", 'bronze', 'SANZO Salvatore'),
...     ('IT', 'Italy', 'Fencing', "Women's Individual Foil", 'gold', 'VEZZALI Maria Valentina'),
...     ('IT', 'Italy', 'Fencing', "Women's Individual Foil", 'bronze', 'GRANBASSI Margherita'),
...     ('IT', 'Italy', 'Fencing', "Men's Individual Epee", 'gold', 'TAGLIARIOL Matteo'),
...     ('IT', 'Italy', 'Fencing', "Men's Team Epee", 'bronze', 'ROTA Alfredo, TAGLIARIOL Matteo, CAROZZO Stefano, CONFALONIERI Diego'),
...     ('IT', 'Italy', 'Fencing', "Men's Team Sabre", 'bronze', 'MONTANO Aldo, PASTORE Giampiero, TARANTINO Luigi, OCCHIUZZI Diego'),
...     ('IT', 'Italy', 'Fencing', "Women's Team Foil", 'bronze', 'GRANBASSI Margherita, VEZZALI Maria Valentina, TRILLINI Giovanna, SALVATORI Ilaria'),
...     ('JP', 'Japan', 'Fencing', "Men's Individual Foil", 'silver', 'OTA Yuki'),
...     ('KR', 'Korea', 'Fencing', "Women's Individual Foil", 'silver', 'NAM Hyunhee'),
...     ('PL', 'Poland', 'Fencing', "Men's Team Epee", 'silver', 'ANDRZEJUK Robert, MOTYKA Tomasz, ZAWROTNIAK Radoslaw, WIERCIOCH Adam'),
...     ('RO', 'Romania', 'Fencing', "Women's Individual Epee", 'silver', 'BRANZA Ana Maria'),
...     ('RO', 'Romania', 'Fencing', "Men's Individual Sabre", 'bronze', 'COVALIU Mihai'),
...     ('RU', 'Russian Fed.', 'Fencing', "Women's Team Foil", 'gold', 'LAMONOVA Evgenia, NIKICHINA Victoria, SHANAEVA Aida, BOYKO Svetlana'),
...     ('UA', 'Ukraine', 'Fencing', "Women's Team Sabre", 'gold', 'KHOMROVA Olena, PUNDYK Halyna, KHARLAN Olga, ZHOVNIR Olha'),
...     ('US', 'United States', 'Fencing', "Women's Individual Sabre", 'gold', 'ZAGUNIS Mariel'),
...     ('US', 'United States', 'Fencing', "Women's Individual Sabre", 'silver', 'JACOBSON Sada'),
...     ('US', 'United States', 'Fencing', "Women's Individual Sabre", 'bronze', 'WARD Becca'),
...     ('US', 'United States', 'Fencing', "Men's Team Sabre", 'silver', 'MOREHOUSE Tim, ROGERS Jason, SMART Keeth, WILLIAMS James'),
...     ('US', 'United States', 'Fencing', "Women's Team Foil", 'silver', 'CROSS Emily, SMART Erinn, THOMPSON Hanna'),
...     ('US', 'United States', 'Fencing', "Women's Team Sabre", 'bronze', 'JACOBSON Sada, ZAGUNIS Mariel, WARD Becca')
... ]

>>> medalSummary(fencing)
{'CN': 2, 'DE': 2, 'ES': 1, 'FR': 4, 'HU': 1, 'IT': 7, 'JP': 1, 'KR': 1, 'PL': 1, 'RO': 2, 'RU': 1, 'UA': 1, 'US': 6}
>>> medalSummary(fencing, 'gold')
{'CN': 1, 'DE': 2, 'FR': 2, 'IT': 2, 'RU': 1, 'UA': 1, 'US': 1}
>>> medalSummary(fencing, 'silver')
{'CN': 1, 'FR': 2, 'JP': 1, 'KR': 1, 'PL': 1, 'RO': 1, 'US': 3}
>>> medalSummary(fencing, 'bronze')
{'ES': 1, 'HU': 1, 'IT': 5, 'RO': 1, 'US': 2}

>>> top(medalSummary(fencing), n=3)
[('IT', 7), ('US', 6), ('FR', 4)]

De Olympische Spelen doen traditioneel in heel veel landen een gevoel van nationale trots opflakkeren. Alle mogelijke lijstjes van gevestigde records worden bijgehouden en iedereen schept op met het aantal medailles dat zijn land heeft binnengehaald.

Opgave

  1. Schrijf een functie medailleOverzicht waaraan twee argumenten moeten doorgegeven worden. Het eerste argument is verplicht en is een lijst met tuples zoals hieronder uitgelegd. Het tweede argument is optioneel en heeft als mogelijke waarden de strings gold, silver en bronze. Als dit tweede argument wordt opgegeven, dan worden enkel de medailles van die soort geteld. Als het tweede argument niet opgegeven wordt, dan worden alle medailles meegeteld. De functie moet een dictionary teruggeven die elke landcode in de lijst koppelt aan het aantal medailles dat dat land heeft behaald.

    Als eerste argument krijg je een lijst van tuples, waarbij elk tuple een reeks velden bevat met informatie over een gewonnen medaille. Als voorbeeld zie je hieronder de (ingekorte) lijst met informatie over alle medailles die tijdens de Olympische Spelen van 2008 in Peking werden behaald in het schermen. Elk tuple in de lijst bevat 6 velden: een landcode, het land, de sport, de discipline, de medaille en de atleten.
    [('CN', 'China', 'Fencing', "Men's Individual Sabre", 'gold', 'ZHONG Man'),
     ('CN', 'China', 'Fencing', "Women's Team Sabre", 'silver', 'NI Hong, ...'),
     ('ES', 'Spain', 'Fencing', "Men's Individual Epee", 'bronze', 'ABAJO Jose Luis'),
     ('FR', 'France', 'Fencing', "Men's Individual Epee", 'silver', 'JEANNET Fabrice'),
     ('FR', 'France', 'Fencing', "Men's Individual Sabre", 'silver', 'LOPEZ Nicolas'),
     ('FR', 'France', 'Fencing', "Men's Team Epee", 'gold', 'JEANNET Fabrice,...'),
     ('FR', 'France', 'Fencing', "Men's Team Sabre", 'gold', 'PILLET Julien, ...'),
     ('DE', 'Germany', 'Fencing', "Men's Individual Foil", 'gold', 'KLEIBRINK Benjamin Philip'),
     ('DE', 'Germany', 'Fencing', "Women's Individual Epee", 'gold', 'HEIDEMANN Britta'),
     ('HU', 'Hungary', 'Fencing', "Women's Individual Epee", 'bronze', 'MINCZA-NEBALD Ildiko'),
     ('IT', 'Italy', 'Fencing', "Men's Individual Foil", 'bronze', 'SANZO Salvatore'),
     ('IT', 'Italy', 'Fencing', "Women's Individual Foil", 'gold', 'VEZZALI Maria Valentina'),
     ('IT', 'Italy', 'Fencing', "Women's Individual Foil", 'bronze', 'GRANBASSI Margherita'),
     ('IT', 'Italy', 'Fencing', "Men's Individual Epee", 'gold', 'TAGLIARIOL Matteo'),
     ('IT', 'Italy', 'Fencing', "Men's Team Epee", 'bronze', 'ROTA Alfredo, ...'),
     ('IT', 'Italy', 'Fencing', "Men's Team Sabre", 'bronze', 'MONTANO Aldo, ...'),
     ('IT', 'Italy', 'Fencing', "Women's Team Foil", 'bronze', 'GRANBASSI Margherita, ...'),
     ('JP', 'Japan', 'Fencing', "Men's Individual Foil", 'silver', 'OTA Yuki'),
     ('KR', 'Korea', 'Fencing', "Women's Individual Foil", 'silver', 'NAM Hyunhee'),
     ('PL', 'Poland', 'Fencing', "Men's Team Epee", 'silver', 'ANDRZEJUK Robert, ...'),
     ('RO', 'Romania', 'Fencing', "Women's Individual Epee", 'silver', 'BRANZA Ana Maria'),
     ('RO', 'Romania', 'Fencing', "Men's Individual Sabre", 'bronze', 'COVALIU Mihai'),
     ('RU', 'Russian Fed.', 'Fencing', "Women's Team Foil", 'gold', 'LAMONOVA Evgenia, ...'),
     ('UA', 'Ukraine', 'Fencing', "Women's Team Sabre", 'gold', 'KHOMROVA Olena, ...'),
     ('US', 'United States', 'Fencing', "Women's Individual Sabre", 'gold', 'ZAGUNIS Mariel'),
     ('US', 'United States', 'Fencing', "Women's Individual Sabre", 'silver', 'JACOBSON Sada'),
     ('US', 'United States', 'Fencing', "Women's Individual Sabre", 'bronze', 'WARD Becca'),
     ('US', 'United States', 'Fencing', "Men's Team Sabre", 'silver', 'MOREHOUSE Tim, ...'),
     ('US', 'United States', 'Fencing', "Women's Team Foil", 'silver', 'CROSS Emily, ...'),
     ('US', 'United States', 'Fencing', "Women's Team Sabre", 'bronze', 'JACOBSON Sada, ...')]
    
  2. Schrijf een functie top die als verplicht argument een dictionary neemt zoals die door de functie medailleOverzicht wordt teruggegeven, en ook een optioneel argument heeft met naam n en als waarde een getal, dat standaard gelijk is aan 10. Als resultaat geeft deze functie een lijst van tuples terug die een top n bevat van landen volgens hun aantal medailles. Deze lijst moet gerangschikt zijn zodat het eerste element in de lijst het meest aantal medailles heeft. De tuples bevatten als eerste element de landcode en als tweede element het aantal medailles.

Bekijk ook het onderstaande voorbeeld om eventuele onduidelijkheden over hoe de functies moeten werken te vermijden. Je kan ook de lijst met alle medailles van de Olympische Spelen van 2008 in Peking downloaden als het bestand peking_medals.txt.

Voorbeeld

>>> fencing = [
...     ('CN', 'China', 'Fencing', "Men's Individual Sabre", 'gold', 'ZHONG Man'),
...     ('CN', 'China', 'Fencing', "Women's Team Sabre", 'silver', 'NI Hong, TAN Xue, HUANG Haiyang, BAO Yingying'),
...     ('ES', 'Spain', 'Fencing', "Men's Individual Epee", 'bronze', 'ABAJO Jose Luis'),
...     ('FR', 'France', 'Fencing', "Men's Individual Epee", 'silver', 'JEANNET Fabrice'),
...     ('FR', 'France', 'Fencing', "Men's Individual Sabre", 'silver', 'LOPEZ Nicolas'),
...     ('FR', 'France', 'Fencing', "Men's Team Epee", 'gold', 'JEANNET Fabrice, JEANNET Jerome, ROBEIRI Ulrich'),
...     ('FR', 'France', 'Fencing', "Men's Team Sabre", 'gold', 'PILLET Julien, SANSON Boris, LOPEZ Nicolas'),
...     ('DE', 'Germany', 'Fencing', "Men's Individual Foil", 'gold', 'KLEIBRINK Benjamin Philip'),
...     ('DE', 'Germany', 'Fencing', "Women's Individual Epee", 'gold', 'HEIDEMANN Britta'),
...     ('HU', 'Hungary', 'Fencing', "Women's Individual Epee", 'bronze', 'MINCZA-NEBALD Ildiko'),
...     ('IT', 'Italy', 'Fencing', "Men's Individual Foil", 'bronze', 'SANZO Salvatore'),
...     ('IT', 'Italy', 'Fencing', "Women's Individual Foil", 'gold', 'VEZZALI Maria Valentina'),
...     ('IT', 'Italy', 'Fencing', "Women's Individual Foil", 'bronze', 'GRANBASSI Margherita'),
...     ('IT', 'Italy', 'Fencing', "Men's Individual Epee", 'gold', 'TAGLIARIOL Matteo'),
...     ('IT', 'Italy', 'Fencing', "Men's Team Epee", 'bronze', 'ROTA Alfredo, TAGLIARIOL Matteo, CAROZZO Stefano, CONFALONIERI Diego'),
...     ('IT', 'Italy', 'Fencing', "Men's Team Sabre", 'bronze', 'MONTANO Aldo, PASTORE Giampiero, TARANTINO Luigi, OCCHIUZZI Diego'),
...     ('IT', 'Italy', 'Fencing', "Women's Team Foil", 'bronze', 'GRANBASSI Margherita, VEZZALI Maria Valentina, TRILLINI Giovanna, SALVATORI Ilaria'),
...     ('JP', 'Japan', 'Fencing', "Men's Individual Foil", 'silver', 'OTA Yuki'),
...     ('KR', 'Korea', 'Fencing', "Women's Individual Foil", 'silver', 'NAM Hyunhee'),
...     ('PL', 'Poland', 'Fencing', "Men's Team Epee", 'silver', 'ANDRZEJUK Robert, MOTYKA Tomasz, ZAWROTNIAK Radoslaw, WIERCIOCH Adam'),
...     ('RO', 'Romania', 'Fencing', "Women's Individual Epee", 'silver', 'BRANZA Ana Maria'),
...     ('RO', 'Romania', 'Fencing', "Men's Individual Sabre", 'bronze', 'COVALIU Mihai'),
...     ('RU', 'Russian Fed.', 'Fencing', "Women's Team Foil", 'gold', 'LAMONOVA Evgenia, NIKICHINA Victoria, SHANAEVA Aida, BOYKO Svetlana'),
...     ('UA', 'Ukraine', 'Fencing', "Women's Team Sabre", 'gold', 'KHOMROVA Olena, PUNDYK Halyna, KHARLAN Olga, ZHOVNIR Olha'),
...     ('US', 'United States', 'Fencing', "Women's Individual Sabre", 'gold', 'ZAGUNIS Mariel'),
...     ('US', 'United States', 'Fencing', "Women's Individual Sabre", 'silver', 'JACOBSON Sada'),
...     ('US', 'United States', 'Fencing', "Women's Individual Sabre", 'bronze', 'WARD Becca'),
...     ('US', 'United States', 'Fencing', "Men's Team Sabre", 'silver', 'MOREHOUSE Tim, ROGERS Jason, SMART Keeth, WILLIAMS James'),
...     ('US', 'United States', 'Fencing', "Women's Team Foil", 'silver', 'CROSS Emily, SMART Erinn, THOMPSON Hanna'),
...     ('US', 'United States', 'Fencing', "Women's Team Sabre", 'bronze', 'JACOBSON Sada, ZAGUNIS Mariel, WARD Becca')
... ]

>>> medailleOverzicht(fencing)
{'CN': 2, 'DE': 2, 'ES': 1, 'FR': 4, 'HU': 1, 'IT': 7, 'JP': 1, 'KR': 1, 'PL': 1, 'RO': 2, 'RU': 1, 'UA': 1, 'US': 6}
>>> medailleOverzicht(fencing, 'gold')
{'CN': 1, 'DE': 2, 'FR': 2, 'IT': 2, 'RU': 1, 'UA': 1, 'US': 1}
>>> medailleOverzicht(fencing, 'silver')
{'CN': 1, 'FR': 2, 'JP': 1, 'KR': 1, 'PL': 1, 'RO': 1, 'US': 3}
>>> medailleOverzicht(fencing, 'bronze')
{'ES': 1, 'HU': 1, 'IT': 5, 'RO': 1, 'US': 2}

>>> top(medailleOverzicht(fencing), n=3)
[('IT', 7), ('US', 6), ('FR', 4)]


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