Oceans play a crucial role in human welfare. From providing food, livelihood and recreational activities to the regulation of the climate. Sustainable management of the oceans therefore requires an integrated and quantitative method for measuring and monitoring the quality. The ocean health index (OHI) is calculated on the basis of ten different goals for a healthy connection between man and the ocean. With this an overall score for each coastal state can be determined. According to this index the oceans worldwide score 59 out of 100 (range 41 to 87). In addition, the industrialized countries generally perform better than the developing countries, but with some notable exceptions. Only 5% of countries scored higher than 70, while 32% of countries have a score lower than 50, the OHI is a powerful tool to improve public awareness, ocean management and policy decisions, and to establish priorities within the research.
The file ohi.txt1 contains the individual scores of all coastal states for the objectives used in the calculation of the OHI. Each line starts with an integer that is used as code for the coastal state, followed by the name of the coastal state and then thirteen integers between 0 and 100 indicating the score for thirteen different (sub)goals. The various fields on each line are separated by a single tab. Following table shows the scores for some coastal states from the file.
Asked:
Write a function mean to which a list should be passed as an argument. The elements of the given list may only be integers or have the value None. The function must return the average (a floating point number) of all integers from the given list as a result. That means that if, for example, a list of twenty elements is passed, five of which have a value None, the average should be calculated over only fifteen integers. If all the elements of the given list have the value None, then the function should return a value of None.
Write a function database that takes the location of a text file as its arguments. The function must read the information from the given text file, and converts it into a dictionary that must be returned by the function. The text file must be formatted according to the format of the sample file ohi.txt2, so with fifteen fields that are separated by a tab. The dictionary that is returned by the function should use the names of the coastal states from the file (second field) as keys, and as corresponding value a list of thirteen integers with individual scores on the goals for that coastal state. Goals for which no value was given in the file (empty cells) should be displayed in the list with the value None.
Write an oceanHealthIndex function with which the OHI for a given coastal state can be calculated. Two arguments must be passed to this function: a string with the name of a coastal state and a dictionary as returned by the database function. The OHI is to be calculated as the mean of the quality scores on ten goals for the coastal state. The score for the goals LE, SP and BD must always be calculated as the mean of the two scores of their sub-goals (which are included in the list of individual scores). On the basis of the thirteen scores in the file ohi.txt3, the mean score for Belgium can be calculated as \[ \frac{11 + 95 + 75 + 100 + 100 + \frac{61 + 56}{2} + 4 + \frac{58 + 5}{2} + 71 + \frac{99 + 82}{2}}{10} = 63,65 \] This mean must be returned by the function as an integer, by rounding off to the nearest integer. Whenever we speak of the mean of a list of scores (integer / None values) above, the calculation is in accordance with the mean function, so by ignoring the None values. If the calculation of the mean for the OHI itself yields a value of None, the ocean health index function should return the value None as a result.
>>> mean([3, 7, 6, 11])
6.75
>>> mean([3, None, None, 7, None, 6, 11])
6.75
>>> mean([None, None, None, None, None])
>>> data = database('ohi.txt')
>>> data['Belgium']
[11, 95, 75, 100, 100, 61, 56, 4, 58, 5, 71, 99, 82]
>>> data['Netherlands']
[35, 96, 71, 100, 100, 79, 53, 4, 65, 85, 67, 89, 81]
>>> data['France']
[72, 96, 75, 79, 49, 94, 67, 27, 63, 29, 60, 72, 77]
>>> data['Monaco']
[1, 95, None, None, None, 95, 71, 99, 43, 0, 69, None, 85]
>>> data['Jarvis Island']
[None, None, None, None, 98, None, None, None, 65, 100, 78, 95, 76]
>>> data['Antarctica']
[None, None, None, None, None, None, None, None, None, None, None, None, None]
>>> oceanHealthIndex('Belgium', data)
64
>>> oceanHealthIndex('Netherlands', data)
70
>>> oceanHealthIndex('France', data)
66
>>> oceanHealthIndex('Monaco', data)
65
>>> oceanHealthIndex('Jarvis Island', data)
86
>>> oceanHealthIndex('Antarctica', data)