In: Computer Science
I have this mystery code. I dont know what the code does. Can somone please explain with examples. (python 3.xx)
from typing import Dict, TextIO, Tuple, List
def exam(d1: Dict[str, List[int]], d2: Dict[int, int]) ->
None:
""" *Mystery code*
"""
for key in d1:
value = d1[key]
for i in range(len(value)):
value[i] = d2[value[i]]
Explanation:
The exam method takes input as two dictionaries d1 and d2.
The dictionary d1 has a string value as key and list of integers as value.
The dictionary d2 has interger value as key and integer value as value
This code can be understood using a example.
Consider the exam function has two dictionaries d1 and d2 such that d1 has exam_name as key and list of integers as value.
These list of integers represent studentid's which are integers.
The dictionary d2 has student id as key and marks as value.
See that for every value from the list of integers in d1,there must be an entry in d2.
This represent the marks of that student.
The outer loop iterates based on number of exams.
The inner loop iterates based on number of students in each
exam.
For each exam,the student ids are considered.
For each student id,the marks are found using d2 and stored in
value list.
See that the value list is printed and it has the marks of
students for those respective exams.