Marathon

The results of marathon races are stored in files with the following structure: the first line contains a header detailing the meaning of each data field in the file and the following lines correspond to a participant each and have the following data fields: surname (str), racing time (hours, minutes and seconds, three integers (int)) and country (str), separated by a space. File marathon.txt is an example of this kind of files, with the following content:

surname hours minutes seconds country
Gebrmariam 02 08 14 ETH
Mutai 02 09 18 KEN
Kipkosgei 02 10 39 KEN
Goumri 02 10 51 MAR
Kwambai 02 11 31 KEN
Keflezighi 02 11 38 USA
DosSantos 02 11 51 BRA

Save the following functions into file marathon.py.

  1. Write the function convert() that given the name of a file with the results of a marathon (str), returns a list of tuples (tuple) where each tuple corresponds to a participant and contains the surname (str), the racing time converted to seconds (int) and the country (str). Participants in this list must be in the same order as the file. Example:

    >>> convert('marathon.txt')
    [('Gebrmariam', 7694, 'ETH'), ('Mutai', 7758, 'KEN'), ('Kipkosgei', 7839, 'KEN'), ('Goumri', 7851, 'MAR'), ('Kwambai', 7891, 'KEN'), ('Keflezighi', 7898, 'USA'), ('DosSantos', 7911, 'BRA')]
    
    

    Note

    More tests are provided in file test-convert.txt

  2. Write the function time_under() that given a list of tuples (tuple) such as that returned by function convert() and a time, t, in seconds (int), returns True if there is at least one runner that has made a time below t, and False otherwise. Example:

    >>> lt = [('Gebrmariam', 7694, 'ETH'), ('Mutai', 7758, 'KEN'),
    ...       ('Kipkosgei', 7839, 'KEN'), ('Goumri', 7851, 'MAR'),
    ...       ('Kwambai', 7891, 'KEN'), ('Keflezighi', 7898, 'USA'),
    ...       ('DosSantos', 7911, 'BRA')]
    
    >>> time_under(lt,7700)
    True
    >>> time_under(lt,7650)
    False
    >>> lt = [('Gebrmariam', 7694, 'ETH'), ('Mutai', 7758, 'KEN'),
    ...       ('Kipkosgei', 7839, 'KEN'), ('Goumri', 7851, 'MAR')]
    >>> time_under(lt,7600)
    False
    >>> lt = [('Gebrmariam', 7694, 'ETH'), ('Mutai', 7758, 'KEN'),
    ...       ('Kipkosgei', 7439, 'KEN'), ('Goumri', 7851, 'MAR')]
    >>> time_under(lt,7600)
    True
    

    Note

    More tests are provided in file test-time_under.txt

  3. Write the function first_difference() that given list of tuples (tuple) as that returned by function convert() and the name of a file (str), creates a file with the given name where each line represents one runner and includes the surname (str) and the time difference with the first classified, in seconds (int), separated by a space. We can suppose that the given list is sorted increasingly by time. The first runner must also appear in the file with a time difference of 0. Examples:

    >>> lt = [('Gebrmariam', 7694, 'ETH'), ('Mutai', 7758, 'KEN'),
    ...       ('Kipkosgei', 7839, 'KEN'), ('Goumri', 7851, 'MAR'),
    ...       ('Kwambai', 7891, 'KEN'), ('Keflezighi', 7898, 'USA'),
    ...       ('DosSantos', 7911, 'BRA')]
    
    >>> first_difference(lt, 'dif1.txt')
    >>> f1 = open ('dif1.txt', 'r')
    >>> s1 = f1.read()
    >>> s1
    'Gebrmariam 0\nMutai 64\nKipkosgei 145\nGoumri 157\nKwambai 197\nKeflezighi 204\nDosSantos 217\n'
    
    >>> lt = [('Kwambai', 7871, 'KEN'), ('Gebrmariam', 7894, 'ETH'),
    ...       ('Keflezighi', 7899, 'USA'), ('Mutai', 7931, 'KEN'),
    ...       ('Kipkosgei', 7956, 'KEN'), ('Goumri', 7979, 'MAR')]
    >>> first_difference(lt, 'dif2.txt')
    >>> f2 = open ('dif2.txt', 'r')
    >>> s2 = f2.read()
    >>> s2
    'Kwambai 0\nGebrmariam 23\nKeflezighi 28\nMutai 60\nKipkosgei 85\nGoumri 108\n'
    

    Note

    More tests are provided in file test-first_difference.txt

Solutions

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