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.
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, ...')]
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.txt1.
>>> 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)]