Using python, complete function def link(d1, d2)
d1: {'A': {'Comment': 'Good', 'Math': '100', 'English': '80', 'Science': '70', 'History': '60'}, 'B': {'Comment': 'Satisfactory', 'Math': '70', 'English': '80', 'Science': '80', 'History': '70'}, 'C': {'Comment': 'Good', 'Math': '80', 'English': '80', 'Science': '80', 'History': '70'}, 'D': {'Comment': 'TBD', 'Math': '50', 'English': '60', 'Science': '80', 'History': '60'}}
d2: {'Math': '90', 'English': '80', 'Science': '80', 'History': '80', 'Comment': 'Good'}
Add totals from each respective subject and divide by 2 for student A and student from d2, for student B and student from d2 and so on.
Example: For the above dictionaries function, comparison returns {'A': 320, 'B': 315, 'C': 320, 'D': 290}
Calculations - Student A and d2:
Math: (100 + 90) / 2 = 95
English: (80 +80) / 2 = 80
Science: (70 + 80) / 2 = 75
History: (60 + 80) / 2 = 70
Total = 320
Same is done for Student B and d2, then Student C and d2, and so on.
Function rank returns {'A', 'C', 'B', 'D'} which is comparison in descending order.
Function link should return the comments that were found in function rank in descending order according to their assocaited value.
From above example, def link should return ['Good', 'Satisfactory']
Since comments for student A and C are the same it is only recorded once. Also, comment 'TBD' is ignored as a comment for that student is yet to be decided.
SourceCode:
# Defing the dictinaries d1 = {'A': {'Comment': 'Good', 'Math': '100', 'English': '80', 'Science': '70', 'History': '60'}, 'B': {'Comment': 'Satisfactory', 'Math': '70', 'English': '80', 'Science': '80', 'History': '70'}, 'C': {'Comment': 'Good', 'Math': '80', 'English': '80', 'Science': '80', 'History': '70'}, 'D': {'Comment': 'TBD', 'Math': '50', 'English': '60', 'Science': '80', 'History': '60'}} d2 = {'Math': '90', 'English': '80', 'Science': '80', 'History': '80', 'Comment': 'Good'} def comparison(d1, d2): # looping thorugh the dictionaries comp = {} total = 0 for keyd1, valued1 in d1.items(): for keyd11, valued11 in valued1.items(): for keyd2, valued2 in d2.items(): if keyd11 != 'Comment': if keyd11 == keyd2: # calculating the marks total = total + ((int(valued11) + int(valued2)) // 2) # adding the calculated marks to the dictionary comp[keyd1] = total total = 0 return comp def rank(comp_dict): # sorting the dictionary dict_keys = [] sorted_comp_dict = sorted(comp_dict.items(), key=lambda kv: (-kv[1], kv[0])) # exctratcing the keys for key, value in sorted_comp_dict: dict_keys.append(key) return dict_keys def link(sorted_comp_dict): comments = [] temp_comments = [] # getting the comments for key, value in d1.items(): if value['Comment'] != 'TBD': temp_comments.append(value['Comment']) temp_comments = set(temp_comments) for i in temp_comments: comments.append(i) return comments # Displaying the result comp_dict = comparison(d1, d2) print("comparison : ", comp_dict) sorted_comp_dict = rank(comp_dict) print("comparison in descending order : ", sorted_comp_dict) comments = link(sorted_comp_dict) print("comparison in descending order : ", comments)
OUTPUT:
Using python, complete function def link(d1, d2) d1: {'A': {'Comment': 'Good', 'Math': '100', 'English': '80', 'Science':...
Designing functions Reminder: When designing functions, follow the given steps to reinforce good software development practices and problem solving strategies: a) Start by writing the documentation for the function. Use docstrings shown in lecture and lab. b) Write test calls to the function in your main function for the different cases of input the function will encounter. This will help you understand the behavior of the function and later behave as tests for your function. c) Define the function d)...