SO2 levels

A nested list of stations contains information on SO2 levels measured in different stations of air quality. Each sublist corresponds to a station and contains a string with the name of the station and a variable number of float values corresponding to the measured SO2 levels. Some of these values may be negative and denote wrong SO2 levels that should not be taken into account. For example, the data corresponding to one of such stations could be ['bcn-34b', 10.3, -1.0, 11.5, 10, 11, 12.1, -2], which contains a total of 7 measurements: 5 of them are correct and 2 of them are wrong.

It can be assumed that there will always be more than one station in the nested list and that any station sublist will have at least one correct measurement.

Save the following functions into the same file so2_levels.py.

  1. Write function average() that takes a list of SO2 levels corresponding to one station (a sublist of the described nested list without the station name), and returns a float with the average of the correct SO2 levels measured. There will always be at least one correct value. Example:

    >>> m = average([10.5, 9.7, 11.2])
    >>> round(m, 4)
    10.4667
    >>> m = average([12, 11, 13, 12])
    >>> round(m, 3)
    12.0
    >>> m = average([10.5, -1, 9.7, 11.2, -2.5])
    >>> round(m, 4)
    10.4667
    

    Note

    More tests are provided in file so2_levels_1.txt

  2. Write function same_as_first_average() that takes a nested list of stations as described and a tolerance, epsilon, and returns the number of stations that have the same SO2 level average as the first station, considering the given tolerance epsilon. As you can see in the examples, we also count the first station. This function must call the previous function average().

    Warning

    Remember that to check the equality between two float values r1 and r2 we must not use the operator ==. Instead, we must use the given tolerance epsilon: abs(r1-r2) < epsilon

    Example:

    >>> l1 = [ ['bcn-34b', 10.3, -1.0, 11.5, 10, 11, 12.1, -2] ]
    >>> same_as_first_average(l1, 0.1)
    1
    >>> l2 = [ ['bcn-12', 12.4, 11.8, 13.7, -1, 12.09],          # average = 12.4975
    ...        ['ber-07', 15.9, 14.5],                           # average = 15.2
    ...        ['ber-10', 12.5, 12.4, 12.6],                     # average = 12.5
    ...        ['car-66', 12.81, -1, 12.6, -2.3, 15.8, 8.92] ]   # average = 12.5325
    >>> same_as_first_average(l2, 0.1)
    3
    >>> same_as_first_average(l2, 0.005)
    2
    >>> same_as_first_average(l2, 0.0001)
    1
    

    Note

    More tests are provided in file so2_levels_2.txt

  3. Write function under_threshold() that takes a list with the SO2 levels corresponding to one station (a sublist of the described nested list without the station name) and a float value, so2, and returns True if at least one of the correct values in the given list is strictly below so2 or False otherwise. Example:

    >>> under_threshold([10.3, 11.5, 10, 11, 12.1], 9.5)
    False
    >>> under_threshold([10.3, 11.5, 10, 11, 12.1], 12)
    True
    >>> under_threshold([10.3, -1, 11.5, 10, -2, 11, 12.1], 9.5)
    False
    

    Note

    More tests are provided in file so2_levels_3.txt

  4. Write function under_threshold_station() that takes a nested list of stations as described and a float value, so2, and returns a string with the name of the first station with an SO2 level lower than so2. If there is no station that meets this condition, the function will return an empty string. This function must use the previous function under_threshold(). Example:

    >>> l1 = [ ['bcn-34b', 10.3, 11.5, 10, 11, 12.1, -1] ]
    >>> under_threshold_station(l1, 9)
    ''
    >>> l2 = [ ['bcn-12', 12.4, 11.8, 13.7, -1, 12.09],
    ...        ['ber-07', 10.9, 14.5],
    ...        ['ber-10', 12.5, 7.5, 12.6],
    ...        ['car-66', 12.81, -1, 12.6, -2.3, 15.8, 8.92] ]
    >>> under_threshold_station(l2, 8)
    'ber-10'
    >>> under_threshold_station(l2, 12.5)
    'bcn-12'
    >>> under_threshold_station(l2, 7)
    ''
    

    Note

    More tests are provided in file so2_levels_4.txt

Solution

Solutions are provided in file so2_levels.py