Eurovision

Save all functions into a file named eurovision.py.

The results obtained by Spain at the Eurovision festival are stored in a string with the points given by each country and following the next format:

  • The country name: 3 alphabetical characters followed by a colon.

  • The points given by this country: 2 digits.

  • The text ' points. ' (there is a whitespace at the beginning and at the end)

Example:

'FRA:10 points. GBR:03 points. POR:00 points. '

  1. Write the function best_hate() that given a string with the festival results, returns:

    • An empty string if the given string is empty.

    • The string ‘cheat’ if there isn’t any 10 points vote.

    • If the previous conditions aren’t met, it returns a string equal

      to the given string but changing each occurrence of ‘10 points’ for ‘we are the best’ and each occurrence of ‘00 points’ for ‘they hate us’. Examples:

    >>> best_hate("FRA:10 points. GBR:03 points. POR:00 points. ")
    'FRA:we are the best. GBR:03 points. POR:they hate us. '
    >>> best_hate("FRA:03 points. GBR:03 points. POR: 00 points. ")
    'cheat'
    >>> best_hate("")
    ''
    >>> best_hate("FRA:10 points. GBR:10 points. POR:00 points. ")
    'FRA:we are the best. GBR:we are the best. POR:they hate us. '
    >>> best_hate("FRA:00 points. GBR:00 points. POR: 00 points. ")
    'cheat'
    >>> best_hate("")
    ''
    >>> best_hate("FRA:00 points. GBR:10 points. POR:00 points. ")
    'FRA:they hate us. GBR:we are the best. POR:they hate us. '
    >>> best_hate("FRA:01 points. GBR:03 points. POR: 00 points. ")
    'cheat'
    >>> best_hate("")
    ''
    

    Note

    More tests are provided in the eurovision1.txt file.

  2. Write the function they_hate_us() that given a string with the festival results, returns another string with the first country that gave 00 points to Spain. If there isn’t any country that gave 00 points to Spain or the string is empty, this function returns a empty string. Examples:

    >>> they_hate_us("FRA:10 points. GBR:03 points. POR:00 points. ")
    'POR'
    >>> they_hate_us("FRA:03 points. GBR:03 points. POR:03 points. ")
    ''
    >>> they_hate_us("FRA:00 points. GBR:03 points. POR:00 points. ")
    'FRA'
    >>> they_hate_us('')
    ''
    >>> they_hate_us("FRA:10 points. GBR:00 points. POR:00 points. ")
    'GBR'
    >>> they_hate_us("FRA:10 points. GBR:03 points. POR:03 points. ")
    ''
    >>> they_hate_us("FRA:10 points. GBR:10 points. POR:10 points. ")
    ''
    >>> they_hate_us('')
    ''
    

    Note

    More tests are provided in the eurovision2.txt file.

  3. Write the function transform() that given a string with the festival results in the described format, returns another string with a new format: the country name (3 characters), a whitespace, the points without the left hand side zeros, the text ‘ points’ (there is a whitespace at the beginning) and ended with a colon. The last result won’t end in a colon. Examples:

    >>> transform("FRA:10 points. GBR:03 points. POR:00 points. ")
    'FRA 10 points:GBR 3 points:POR 0 points'
    >>> transform("FRA:10 points. GBR:00 points. POR:00 points. ")
    'FRA 10 points:GBR 0 points:POR 0 points'
    >>> transform("FRA:10 points. GBR:03 points. ")
    'FRA 10 points:GBR 3 points'
    

    Note

    More tests are provided in the eurovision3.txt file.

  4. Write the function score() that given a string with the festival results and another string with the name of a country (3 alphabetical characters), returns an integer with the score given by the country or -1 if there isn’t any result.

    >>> score("FRA:10 points. GBR:03 points. POR:00 points. ",'POR')
    0
    >>> score("FRA:10 points. GBR:03 points. POR:00 points. ",'FRA')
    10
    >>> score("FRA:10 points. GBR:03 points. POR:00 points. ",'GBR')
    3
    >>> score("FRA:10 points. GBR:03 points. POR:00 points. ",'ITA')
    -1
    >>> score("FRA:10 points. GBR:03 points. POR:01 points. ",'POR')
    1
    >>> score("FRA:00 points. GBR:03 points. POR:00 points. ",'FRA')
    0
    

    Note

    More tests are provided in the eurovision4.txt file.

Solutions

A solution of these functions is provided in the eurovision.py file.