PROG0294 - Eurovision song contest

The Eurovision Song Contest is a yearly competition that is held among the countries that are a member of the European Broadcasting Union (EBU).

Eurovisiesongfestival logo

Every participating country sends in a song that is sung live during a television program that is broadcast simultaneously by the EBU in all countries in the Eurovision network. After all songs are performed, a (tele)voting follows after which the winner is announced. The winner of the festival is determined by giving points for every participating country to ten of their favourite songs. Since 1975 this is 1 till 8, then 10 and eventually 12 points for the most popular song. The participating countries are not allowed to vote for themselves.

The final of the Eurovision Song Contest in 2012 took place in Baku, the capital of Azerbaijan, on 26 May 2012. The festival was won by the Swedish singer Loreen with the song Euphoria. This song got a total of 372 points. Russia and Serbia respectively got second and third place. 

Assignment

  1. Write a function addScores, that a country can use to appoint scores to its favourite songs. Two dictionaries should be given to this function as arguments. The first dictionary links the countries to the number of points they have received until now. The second dictionary links the countries to the amount of points they were appointed by another country. The function must update the given dictionary that links each country to the amount of points the country has received with new scores from the second dictionary that was given to the function. In other words, the function must not print a value (or the value None), but update the dictionary that was given as a first argument (dictionaries are after all mutable in Python). The dictionary that is given as a second argument may not be modified by the function.
  2. Write a function showScores to which a dictionary must be given as an argument. This dictionary links countries to the amount of point they have received so far. The function must print a list of tuples as a result, where every tuple consists of two elements: the name of a country, and the number of points that country has so far. These tuples must be ordered in decreasing number of points. Countries that have an equal score must be ordered alphabetically. To this function a second, optional, integer-argument $n$ can be given. If this is the case, only the first $n$ countries must be printed. The dictionary that is given as an argument of the function may not be modified by the function. 

Example

>>> scoreboard = {}

>>> scores_UK = {'Lithuania': 7, 'Russia': 3, 'Estonia': 4, 'Azerbaijan': 2, 'Sweden': 12, 'Turkey': 1, 'Spain': 8, 'Germany': 6, 'Malta': 5, 'Ireland': 10}
>>> addScores(scoreboard, scores_UK)
>>> showScores(scoreboard)
[('Sweden', 12), ('Ireland', 10), ('Spain', 8), ('Lithuania', 7), ('Germany', 6), ('Malta', 5), ('Estonia', 4), ('Russia', 3), ('Azerbaijan', 2), ('Turkey', 1)]

>>> scores_HU = {'Albania': 8, 'Russia': 7, 'Iceland': 6, 'Italy': 5, 'Sweden': 12, 'Turkey': 3, 'Spain': 1, 'Germany': 10, 'Serbia': 4, 'Moldova': 2}
>>> addScores(scoreboard, scores_HU)
>>> showScores(scoreboard, 3)
[('Sweden', 24), ('Germany', 16), ('Ireland', 10)]

Het Eurovisiesongfestival is een jaarlijkse competitie die gehouden wordt onder de landen die lid zijn van de Europese Radio-unie (EBU, European Broadcasting Union).

Eurovisiesongfestival logo

Elk deelnemend land zendt een lied in dat live wordt gezongen tijdens een televisieprogramma dat door de EBU gelijktijdig wordt uitgezonden in alle landen in het Eurovisienetwerk. Nadat alle liedjes uitgevoerd werden, volgt een (tele)voting waarna de winnaar wordt uitgeroepen. De winnaar van het festival wordt bepaald door het geven van punten door ieder deelnemend land aan hun tien favoriete liedjes. Sinds 1975 is dit 1 t/m 8, dan 10 en uiteindelijk 12 punten voor het populairste lied. Deelnemende landen mogen niet op zichzelf stemmen.

De finale van het Eurovisiesongfestival 2012 ging door op 26 mei 2012 in Baku, de hoofdstad van Azerbeidzjan. Het festival werd gewonnen door de Zweedse zangeres Loreen met het liedje Euphoria. Dit lied verzamelde in totaal 372 punten. Tweede en derde werden Rusland en Servië.

Opgave

  1. Schrijf een functie scoresToevoegen, waarmee een land punten kan toekennen aan zijn favoriete liedjes. Aan deze functie moeten twee dictionaries als argument doorgegeven worden. De eerste dictionary koppelt landen aan het aantal punten dat ze tot dusver behaald hebben. De tweede dictionary koppelt landen aan het aantal punten dat hen door een ander land werd toegekend. De functie moet de gegeven dictionary die elk land koppelt aan het aantal punten dat het land heeft behaald bijwerken met de nieuwe scores uit de tweede dictionary die aan functie moet doorgegeven. De functie moet met andere woorden geen waarde teruggeven (of dus impliciet de waarde None teruggeven), maar de dictionary die als eerste argument wordt doorgegeven wijzigen (dictionaries zijn immers mutable in Python). De dictionary die als tweede argument wordt doorgegeven mag door de functie niet gewijzigd worden.
  2. Schrijf een functie scoresTonen waaraan een dictionary als argument moet doorgegeven worden. Deze dictionary koppelt landen aan het aantal punten ze tot dusver behaald hebben. De functie moet als resultaat een lijst van tuples teruggeven, waarbij elk tuple bestaat uit twee elementen: de naam van een land, en het aantal punten dat het land tot dusver behaald heeft. Deze tuples moeten gerangschikt zijn in dalende puntenaantal. Landen die evenveel punten behaald hebben moeten alfabetisch gerangschikt staan. Aan de functie kan optioneel nog een tweede integer-argument $n$ doorgegeven worden. Indien dat het geval is, dan moeten enkel de eerste $n$ landen teruggegeven worden. De dictionary die als argument aan de functie wordt doorgegeven, mag door de functie niet gewijzigd worden.

Voorbeeld

>>> scorebord = {}

>>> scores_UK = {'Lithuania': 7, 'Russia': 3, 'Estonia': 4, 'Azerbaijan': 2, 'Sweden': 12, 'Turkey': 1, 'Spain': 8, 'Germany': 6, 'Malta': 5, 'Ireland': 10}
>>> scoresToevoegen(scorebord, scores_UK)
>>> scoresTonen(scorebord)
[('Sweden', 12), ('Ireland', 10), ('Spain', 8), ('Lithuania', 7), ('Germany', 6), ('Malta', 5), ('Estonia', 4), ('Russia', 3), ('Azerbaijan', 2), ('Turkey', 1)]

>>> scores_HU = {'Albania': 8, 'Russia': 7, 'Iceland': 6, 'Italy': 5, 'Sweden': 12, 'Turkey': 3, 'Spain': 1, 'Germany': 10, 'Serbia': 4, 'Moldova': 2}
>>> scoresToevoegen(scorebord, scores_HU)
>>> scoresTonen(scorebord, 3)
[('Sweden', 24), ('Germany', 16), ('Ireland', 10)]

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

© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.