In: Computer Science
1. Dictionaries and Lists.
a) Implement a list of student dictionaries containing the following data:
name: Andy, classes: ET580 MA471 ET574
name: Tim, classes: ET574 MA441 ET575
name: Diane, classes: MA441 MA471 ET574
name: Lucy, classes: ET574 ET575 MA471
name: Steven, classes: ET574 MA441 ET580
b) Implement a dictionary of courses and set each courses enrollment to 0: ET580: 0 ET574: 0 ET575: 0 MA441: 0 MA471: 0
c) Use a loop and if statements to read class data from the list of students and update the enrollment for each course.
d) Print the updated list of courses. If the above steps are done correctly your output should match the output example.
Output Example
ET580 2 ET575 2 ET574 5 MA441 3 MA471 3
Have a look at the below code. I have put comments wherever required for better understanding. Since any specific language was not mentioned, I have used Python, it will be easy to understand.
# a) Implement a list of student dictionaries
list = {"Andy":["ET580","MA471","ET574"],"Tim":["ET574","MA441","ET575"],"Diane":["MA441","MA471","ET574"],"Lucy":["ET574","ET575","MA471"],"Steven":["ET574","MA441","ET580"]}
# b) Implement a dictionary of courses and set each courses enrollment to 0
courses = {"ET580":0,"ET574":0,"ET575":0,"MA441":0,"MA471":0}
# c) Use a loop and if statements to read class data from the list of students and update the enrollment for each course.
for student in list:
for course in list[student]:
courses[course]+=1
# d) Print the updated list of courses
print(courses) # Output: {'ET580': 2, 'ET574': 5, 'ET575': 2, 'MA441': 3, 'MA471': 3}
Happy Learning!