pH Measure

A corporation uses files to store several measurements of the pH of its products. In these files, each line records the data of one product and contains the name of the product and a varying number of pH measurements for that product, all separated by commas. As an example, you can download file ph_data.txt which has the following content:

prodF,7.1,7.25,7.0,7.0
prodB,6.97,6.99,7.07,6.96
prodD,6.98,7.2,7.1,7.0,7.02
prodX,7.0,7.0,7.0,7.0,7.0
prodZ,6.9,7.18,7.05,7.0,7.01,6.98
prodS,6.97,7.01,6.97,7.02,6.98,7.03
prodR,6.98,7.2,7.1,7.0,6.99
prodG,6.88,7.11,7.08,7.01
prodA,7.01,6.99,7.01,7.0
prodK,6.96,6.96,6.99,6.98,6.99

Write function ph_average() that takes the name of a file as that described above and a tolerance epsilon, and returns a list with the names of the products that have an average of measurements that indicate a neutral pH, that is, a pH of 7. To check the equality between two values you have to do the comparison with the given tolerance epsilon, i. e., check if the absolute value of the difference of the values is lower than epsilon. The list must be sorted lexicographically.

Save this function in file ph_measure.py. Examples:

>>> ph_average('ph_data.txt', 0.01)
['prodA', 'prodB', 'prodS', 'prodX']
>>> ph_average('ph_data.txt', 0.09)
['prodA', 'prodB', 'prodD', 'prodF', 'prodG', 'prodK', 'prodR', 'prodS', 'prodX', 'prodZ']

Solution

A solution is provided in file ph_measure.py.