Students¶
Save all functions into the same file named students.py.
To manage the class lists of a school year we use a dictionary
dict where the key is a 3-tuple with the name and the two
surnames of the student (str), and the associated value is
the list of subjects in which they are enrolled. For example,
the following dictionary:
dic = {('Joan', 'Rubio', 'Boix'): ['Programming', 'Physics', 'Calculus'],
('Anna', 'Pons', 'Adell'): ['Calculus', 'Programming']}
contains the student Joan Rubio Boix enrolled in Programming,
Physics and Calculus and the student Anna Pons Adell
enrolled in Programming and Calculus.
Write the modifier function
add_student()that given a dictionary as that described, the name and two surnames of a new student (str), and alistwith the subjects in which they are enrolled, modifies the dictionary by adding this new student. Examples:>>> dic = {('Joan', 'Rubio', 'Boix'): ['Programming', 'Physics', 'Calculus']} >>> add_student(dic, 'Anna', 'Pons', 'Adell', ['Programming', 'Calculus']) >>> if dic != {('Joan', 'Rubio', 'Boix'): ['Programming', 'Physics', 'Calculus'], ... ('Anna', 'Pons', 'Adell'): ['Programming', 'Calculus']}: ... print(dic)
Note
More tests are provided in
test-students1.txt.Write the function
subject_student()that given a dictionary as that described, and the name and two surnames of a new student (str), returns thelistof subjects in which that student is enrolled. If the student isn’t in the dictionary it returns an empty list. Examples:>>> dic = {('Pol','Amber','Coll'):['PHY','PE','CHM'], ... ('Aina','Soler','Gil'):['PHY','PE','CHM','MTH']} >>> subject_student(dic, 'Pol', 'Amber', 'Coll') ['PHY', 'PE', 'CHM']
Note
More tests are provided in
test-students2.txt.Write the function
listed()that given a dictionary as that described and a subject (str), returns thelistof all the students enrolled in this subject. In this list, a student must be represented with a string following this pattern: the first surname, a space, the second surname, a comma and a space, and the name. The list has to be sorted by the first surname. If no student is enrolled in the given subject, the function will return an empty list. Examples:>>> dic = {('Pol','Amber','Coll'):['PHY','PE','CHM'], ... ('Aina','Soler','Gil'):['PHY','PE','CHM','MTH'], ... ('Joan','Rubio','Boix'):['PE','CHM','MTH', 'ENG'], ... ('Mar','Pou','Coll'):['PE','GE']} >>> listed(dic, 'PHY') ['Amber Coll, Pol', 'Soler Gil, Aina'] >>> listed(dic, 'PR') []
Note
More tests are provided in
test-students3.txt.Write the function
students_per_subject()that given a dictionary as that described, returns another dictionary where the key is the name of a subject, and its associate value is the number of students enrolled in this subject. Examples:>>> dic = {('Pol','Amber','Coll'):['PHY','PE','CHM'], ... ('Aina','Soler','Gil'):['PHY','PE','CHM','MTH'], ... ('Joan','Rubio','Boix'):['PE','CHM','MTH', 'ENG'], ... ('Mar','Pou','Coll'):['PE','GE']} >>> ds = students_per_subject(dic) >>> if ds != {'PHY':2,'PE':4,'CHM':3,'MTH':2,'ENG':1,'GE':1}: ... print(ds)
Note
More tests are provided in
test-students4.txt.
Solution
A solution is provided in the students.py