Measures¶
A device measures the atmospheric pressure of an exterior zone. When
meteorological conditions aren’t good, measures aren’t reliable, and
the device has a sensor that checks whether the meteorological
conditions are good or bad. We have been given a non empty
list of measures, corresponding to a determined time
period. This list contains real values (float) corresponding
to atmospheric pressures, taken in chronological order. This list also
contains the characters asterisk ('*') and equals ('='). When
traversing the list if an asterisk is found, it means that the
following measures aren’t reliable and if an equal sign is found, it
means that the following measures are reliable. The initial values of
the given list are reliable.
Save the following functions into the same file measures.py.
Write the function
separate(), that takes a list of measures as described, and returns atuplewith two lists. The first list should only have the reliable measures of the given list and the second list should only have the measures of the given list that aren’t reliable. For example:>>> data = [101.4, 105.2, '*', 103.1, 103.4, '=', 101.2, 102.5] >>> separate(data) ([101.4, 105.2, 101.2, 102.5], [103.1, 103.4])
More tests are provided in file
test-measures-1.txt.Write the function
average(), that takes a list of real values, and returns the average (float) of the list elements. Suppose that the list is not empty. Example:>>> data_reliable = [101.4, 105.2, 101.2, 102.5] >>> data_no_reliable = [103.1, 103.4] >>> mreliable = average(data_reliable) >>> round(mreliable, 2) 102.58 >>> mtot = average(data_reliable + data_no_reliable) >>> round(mtot, 2) 102.8
More tests are provided in file
test-measures-2.txt.Write the function
averages(), that takes a list of measures as described, and returns atuplewith two averages (float) corresponding respectively to the average of the reliable measures and the average of all measures. For example:>>> data = [101.4, 105.2, '*', 103.1, 103.4, '=', 101.2, 102.5] >>> mreliable, mtot = averages(data) >>> round(mreliable, 2) 102.58 >>> round(mtot, 2) 102.8
This function
averages()must call the previous two functionsseparate()andaverage().More tests are provided in file
test-measures-3.txt.Write the function
proportion(), that takes a list of measures as described, and returns the percentage (float) of reliable measures with respect to the total number of measures in the given list. For example:>>> data = [101.4, 105.2, '*', 103.1, 103.4, '=', 101.2, 102.5] >>> propreliable = proportion(data) >>> round(propreliable, 3) 0.667
This function
proportion()must call the previous functionseparate().More tests are provided in file
test-measures-4.txt.
Solution
A solution of these functions is provided in file measures.py