Mixed structures 2 - Challenges
Download exercises zip
We now propose some exercises without solution, do you accept the challenge?
Challenge - Guilty!
Recently there has been a brutal execution at a pizzeria in Little Italy, and the FBI has a list of suspects as a dictionary list. Each dictionary holds the suspect’s name and values for weapon
, place
and motive
, which tell the degree of suspicion for each category. The FBI also has a weights
list assigned to each suspicion category, which are used by judges to determine the degree of guiltiness of each suspect. weights
is expressed as a list of tuples: each tuple contains
a suspicion category and the related weight as float. To calculate the guiltiness of each suspect, each weight is multiplied for the corresponding suspect’s suspicion value and an overall sum is performed.
The FBI asks you to produce a NEW table as a list of lists containing the gangster data, plus a column 'guiltiness'
calculated as explained above.
REMEMBER the table headers
USE the same items order as found in the
weights
list
[1]:
db = [
{'name' : 'Cadillac Frank',
'weapon' : 5,
'place' : 3,
'motive' : 7},
{'name' : 'Lucky Vincent',
'weapon' : 7,
'place' : 4,
'motive' : 8},
{'name' : 'Three Fingers',
'weapon' : 1,
'place' : 7,
'motive' : 4},
{'name' : 'Vito The Butcher',
'weapon' : 3,
'place' : 6,
'motive' : 5},
]
def judge(gangsters, weights):
raise Exception('TODO IMPLEMENT ME !')
res = judge(db, [('weapon',0.1),
('motive',0.7),
('place', 0.2)])
from pprint import pprint
pprint(res, width=80)
# note: since the table contains floats it would be better to check for closeness of values ..
assert res == [['name', 'weapon', 'motive', 'place', 'guiltiness'],
['Cadillac Frank', 5, 7, 3, 6.0 ],
['Lucky Vincent', 7, 8, 4, 7.1 ],
['Three Fingers', 1, 4, 7, 4.3 ],
['Vito The Butcher', 3, 5, 6, 5.0 ]]
[ ]: