Failures (4 points)

Failures in the tram network of a city are stored in a nested list. Each item of this list is another list with the following 6 items: the tram identifier (str), the line identifier (str), the failure code (str), the type of failure (str) that can be 'elec' or 'mec', the time spent in arriving at the place with the tram failure (int), and the time spent in repairing the failure (int). Both times are given in minutes.

Solve the two following exercises and save each function in the corresponding file:

  1. (2 points) Write function failures() that takes a nested list as that described, and returns 2 dictionaries (dict): the first one for electrical failures (type 'elec') and the second one for mechanical failures (type 'mec'). In both dictionaries the key is the line identifier (str) and the value is a list of tuples (tuple); each tuple corresponds to a failure and has three items: the total time (in minutes) needed to repair the failure (int), the tram identifier (str), and the failure code (str). The total time is the sum of the arriving time plus the repairing time. Tuples in all the lists of both dictionaries must be sorted by total time.

    Save this function in file failures.py.

    Example:

    >>> lfail = [['A05', 'T1', 'CX1', 'elec', 110, 50],
    ...          ['B04', 'T3', 'ER4', 'elec', 50, 125],
    ...          ['A11', 'T1', 'GY6', 'mec', 40, 190],
    ...          ['B04', 'T3', 'CX2', 'mec', 30, 155],
    ...          ['A11', 'T1', 'HH7', 'mec', 70, 80],
    ...          ['B03', 'T2', 'XC7', 'mec', 100, 110],
    ...          ['A05', 'T1', 'XZ3', 'mec', 40, 50],
    ...          ['A02', 'T4', 'BT5', 'elec', 30, 60],
    ...          ['B04', 'T3', 'AX4', 'elec', 90, 95],
    ...          ['A05', 'T1', 'HT6', 'elec', 45, 105]]
    
    >>> delec, dmec = failures(lfail)
    >>> if delec != {'T1': [(150, 'A05', 'HT6'), (160, 'A05', 'CX1')], 'T3': [(175, 'B04', 'ER4'), (185, 'B04', 'AX4')], 'T4': [(90, 'A02', 'BT5')]}:
    ...   print(delec)
    >>> if dmec != {'T1': [(90, 'A05', 'XZ3'), (150, 'A11', 'HH7'), (230, 'A11', 'GY6')], 'T3': [(185, 'B04', 'CX2')], 'T2': [(210, 'B03', 'XC7')]}:
    ...   print(dmec)
    

    Note

    Tests are provided in file test-failures.txt.

  2. (2 points) More data on failures is stored in a dictionary (dict) in which the key is the failure code (str) and the value is a tuple with 2 items: the time when the failure was notified, and the name of the worker (str). The notification time is represented with a tuple of the form (hour, minute), where hour and minute are two positive integers and hour < 24 and minute < 60.

    Write the modifier function time_failure() that takes a nested list as that described in the introduction, and a dictionary as that described in the previous paragraph. This function modifies the given list in such a way that each sublist will have a new format with 3 items: the tram identifier (str), the worker name (str), and the ending time of the repair. This ending time is computed by adding both the arriving and the repairing time to the notification time and it is represented in the same format as the notification time. The given data is such that all these ending times will fall in the same day, i.e., hour < 24 (you don’t need to check it).

    Save this function in file time_failure.py.

    Example:

    >>> lfail = [['A05', 'T1', 'CX1', 'elec', 110, 50],
    ...          ['B04', 'T3', 'ER4', 'elec', 50, 125],
    ...          ['A11', 'T1', 'GY6', 'mec', 40, 190],
    ...          ['B04', 'T3', 'CX2', 'mec', 30, 155],
    ...          ['A11', 'T1', 'HH7', 'mec', 70, 80],
    ...          ['B03', 'T2', 'XC7', 'mec', 100, 110],
    ...          ['A05', 'T1', 'XZ3', 'mec', 40, 50],
    ...          ['A02', 'T4', 'BT5', 'elec', 30, 60],
    ...          ['B04', 'T3', 'AX4', 'elec', 90, 95],
    ...          ['A05', 'T1', 'HT6', 'elec', 45, 105]]
    
    >>> dfail = {'CX1': ((12, 30), 'Jim'), 'ER4': ((9, 5),'Ann'),
    ...  'HT6': ((10, 5), 'Jim'), 'AX4': ((9, 10), 'John'),
    ...  'GY6': ((11, 15), 'Ann'), 'CX2': ((18, 5), 'John'),
    ...  'BT5': ((14, 10), 'Peter'), 'XZ3': ((13, 35),'Peter'),
    ...  'HH7': ((10, 10), 'Ann'), 'XC7': ((10, 20), 'Jim')}
    
    >>> time_failure(lfail, dfail)
    >>> if lfail != [['A05', 'Jim', (15, 10)], ['B04', 'Ann', (12, 0)],
    ...              ['A11', 'Ann', (15, 5)], ['B04', 'John', (21, 10)],
    ...              ['A11', 'Ann', (12, 40)], ['B03', 'Jim', (13, 50)],
    ...              ['A05', 'Peter', (15, 5)], ['A02', 'Peter', (15, 40)],
    ...              ['B04', 'John', (12, 15)], ['A05', 'Jim', (12, 35)]]:
    ...     print(lfail)
    

    Note

    Tests are provided in file test-time_failure.txt.