Pensions¶
The expenses of a group of pensioners are stored in a list of sublists where each sublist represents a pensioner’s data and it consists of the identifier (ID) of the pensioner (string), an integer that indicates the age and some recorded monthly expenses (integers). In the following example, the first sublist stands for a pensioner with ID ‘1111A’ who is 68 years old and three monthly expenses of 640, 589 and 573.
[['1111A', 68, 640, 589, 573], ['2222D', 69, 710, 550, 570, 698, 645, 512]]
Save all functions into the same file named pensions.py.
Write the function
expense_average()that given a list of pensioners returns another list with the average monthly expense of each pensioner. The list must be sorted increasingly.>>> l = [['1111A', 68, 640, 589, 573], ... ['2222D', 69, 710, 550, 570, 698, 645, 512], ... ['3333J', 72, 530, 534], ... ['4444N', 75, 770, 645, 630, 650, 590, 481, 602]] >>> l2 = expense_average(l) >>> for e in l2: ... print(round(e, 2)) 532.0 600.67 614.17 624.0
Note
More tests are provided in file
pensions1.txt.Write the function
exp_averag_sup()that takes a list of pensioners and an expense,d(float), and returns another list with two elements: ID and age of the first pensioner that has an average expense greater thand. If there is any such pensioner, the function must return an empty list.>>> l = [['1111A', 68, 640, 589, 573], ... ['2222D', 69, 710, 550, 570, 698, 645, 512], ... ['3333J', 72, 730, 734], ... ['4444N', 75, 770, 645, 630, 650, 590, 481, 602]] >>> exp_averag_sup(l, 455.5) ['1111A', 68] >>> exp_averag_sup(l, 700.5) ['3333J', 72] >>> exp_averag_sup(l, 1000) []
Note
More tests are provided in file
pensions2.txt.Write the function
exp_sup()that takes a list of pensioners and an expense,d(float), and returns another list with the ID of those pensioners with one or more of their monthly expenses superior tod. IDs in the new list must appear in the same order than in the original list. If there are any such pensioners, the function must return an empty list.>>> l = [['1111A', 68, 640, 589, 573], ... ['2222D', 69, 710, 550, 570, 698, 645, 512], ... ['3333J', 72, 530, 534], ... ['4444N', 75, 770, 645, 630, 650, 590, 481, 602]] >>> exp_sup(l, 700) ['2222D', '4444N'] >>> exp_sup(l, 800) []
Note
More tests are provided in file
pensions3.txt.Write the function
no_reach()that takes a list of pensioners and an expense,d(float), and returns the ID of the first pensioner that has an expense superior tod. If there is any such pensioner, the function must return an empty string.>>> l = [['1111A', 68, 640, 589, 573], ... ['2222D', 69, 710, 550, 570, 698, 645, 512], ... ['3333J', 72, 530, 534], ... ['4444N', 75, 770, 645, 630, 650, 590, 481, 602]] >>> no_reach(l, 650) '2222D' >>> no_reach(l, 850) ''
Note
More tests are provided in file
pensions4.txt.Write the modifier function
put_average()that given a list of pensioners, modifies this list adding at the end of each sublist the average expense.>>> lpens = [['1111A', 68, 640, 589, 574], ... ['2222D', 69, 710, 550, 570, 692]] >>> a = put_average (lpens) >>> lpens [['1111A', 68, 640, 589, 574, 601.0], ['2222D', 69, 710, 550, 570, 692, 630.5]]
Note
More tests are provided in file
pensions5.txt.
Solution
A solution of these functions is provided in file pensions.py