Contest

In a contest we have a list with the ranking of the participants: a list with the names of the participants in descending punctuation order. The participants of this contest face each other in pairs and, depending on the result, they interchange their positions.

Save both functions into file contest.py.

  1. Write the modifier function change() that takes the current ranking, a list of participant’s names (str), and two more names (str) together with their position in the list of names, in this order: name1, position1, name2, position2 and modifies the ranking list so that the two given names exchange their position. We can consider that both names are in the given list. Examples:

    >>> names = ['Pau', 'Anna', 'Pol', 'Joan', 'Maria']
    >>> change (names, 'Pol', 2, 'Maria', 4)
    >>> names
    ['Pau', 'Anna', 'Maria', 'Joan', 'Pol']
    
    >>> names = ['Pau', 'Anna', 'Pol', 'Joan', 'Maria']
    >>> change (names, 'Pol', 2, 'Pau', 0)
    >>> names
    ['Pol', 'Anna', 'Pau', 'Joan', 'Maria']
    

    Note

    More tests are provided in file test-contest-1.txt.

  2. Write the modifier function update_ranking() that takes a list with the ranking of the participants of the contest and another list with the results of a confrontation of two contestants and modifies the ranking list according to this result. The second list will always have four elements, the name of a contestant (str), an integer with their score (int), the name of the other contestant (str) and their score (int).

    The winner is the one with the highest score. If the winner appears in the ranking list after the loser, then their positions must be changed. Otherwise the ranking list remains unchanged.

    In the confrontation list there can be one or two new participants (participants which are not yet in the ranking list). New participants must be placed at the end of the ranking list, in the order in which they come in the confrontation list, previously to compare their scores.

    This function must call function change(). Examples:

    >>> names = ['Pau', 'Anna', 'Pol', 'Joan', 'Maria']
    >>> update_ranking (names, ['Pol',10,'Maria',5])
    >>> names
    ['Pau', 'Anna', 'Pol', 'Joan', 'Maria']
    
    >>> names = ['Pau', 'Anna', 'Pol', 'Joan', 'Maria']
    >>> update_ranking (names, ['Pol',1,'Maria',10])
    >>> names
    ['Pau', 'Anna', 'Maria', 'Joan', 'Pol']
    
    >>> names = ['Pau', 'Anna', 'Pol', 'Joan', 'Maria']
    >>> update_ranking (names, ['Roser',10,'Anna',5])
    >>> names
    ['Pau', 'Roser', 'Pol', 'Joan', 'Maria', 'Anna']
    
    >>> names = ['Pau', 'Anna', 'Pol', 'Joan', 'Maria']
    >>> update_ranking (names, ['Carles',1,'Carla',10])
    >>> names
    ['Pau', 'Anna', 'Pol', 'Joan', 'Maria', 'Carla', 'Carles']
    

    Note

    More tests are provided in file test-contest-2.txt.

Solution

A solution of these functions is provided in file contest.py .