Published using Google Docs
CSE160 21wi Final problems
Updated automatically every 5 minutes

CSE160 Final Problems

Problem 1

Make a function get_student_info(courses) that, given a list of dictionaries where each dictionary represents a course and every dictionary maps the names of students in that course to their current grade, returns a dictionary mapping each student taking at least 1 course to a list of two numbers, where the first number is the number of courses that student is taking and the second is their average grade over all their courses.

For example, given the list of dictionaries

courses = [{"Zoe" : 3.0, "Jack": 3.8, "Amanda": 3.0},

           {"Zoe" : 3.5, "David": 2.8},

           {"Zoe" : 4.0, "David": 4.0, "Amanda": 3.4, "Joely": 3.1}]

get_student_info(courses) would return

{'Zoe': [3, 3.5],'Jack': [1, 3.8], 'Amanda': [2, 3.2], 'David': [2, 3.4], 'Joely': [1, 3.1]}

Notes: Don’t worry about rounding average grades that have long decimal values. Student names are case sensitive. Your code should not modify the provided input. You cannot assume that courses is non-empty, nor that the dictionaries inside are non-empty.

Problem 2

Write a function sorted_zoos(given_zoo, zoo_dict) that, given the name of a particular zoo and a dictionary mapping names of all zoos to sets of the animals they contain, returns a list of zoo names sorted in descending order by the number of animals they have in common with the zoo whose name was given. Break ties when sorting alphabetically by zoo name.

For example, given the zoo_dict

zoo_dict = {"Woodland Park": {"bear", "squirrel", "cheetah", "frog"},

                     "zooB": {"bear", "squirrel", "cheetah", "frog"},

                     "zooA": {"bear", "squirrel"}}

sorted_zoos("Woodland Park", zoo_dict) would return ["zooB", "zooA"] because zooB has 4 animals in common while zooA only has 2.

Notes: Do not include the name of the given zoo in the returned list. You are guaranteed the given zoo will be one of the keys in the zoo_dict. Your code should not modify the provided input. You may use itemgetter for this problem.

Problem 3

Write a function called office_hour_queue(student_dict, time_began) that takes in two arguments: (1) a dictionary which maps student names to the time they arrived at office hours and (2) the time office hours began. Times are represented as strings, in a 24-hour format with a colon in between the hour and minute (example: 9am would be "9:00", 4pm would be "16:00").

The function should return a list of the students' names, ordered by the times the students arrived, with students who arrived earlier coming first. If two students arrive at the same time, ties are broken based on the students’ names (alphabetical). Office hours last for one hour, so any student arriving after they are done should not be included in the output list (if office hours began at 11:00 and a student arrived at 12:00, they should not be included). However, if a student arrives before office hours begins, then they can still be included in the list.

For example, with the given arrival times

student_dict = {'Elizabeth': '9:45', 'Jane': '9:30', 'Caroline': '10:30', 'Lydia': '10:20', 'Kitty': '10:20', 'Mary': '9:35'}

office_hour_queue(student_dict, ‘9:30’) should return ['Jane', 'Mary', 'Elizabeth', 'Kitty', 'Lydia']. Jane arrived earliest, followed by Mary and Elizabeth. Kitty and Lydia arrived at the same time, so Kitty is placed first in alphabetical order. Caroline arrived after office hours ended so they are not included in the output.

Notes: You can assume all students arrive within the same 24 hour period (no student will arrive before midnight from the previous day, and no student will arrive at or after midnight the next day). Office hours can begin any time from 0:00 to 23:00. Your code should not modify the provided input. You may use itemgetter for this problem.

Hint: It might be helpful to think about times as being a certain number of minutes after midnight in order to compare them.

------------------------------------------------------------------------------------------------------------------------

You are done with part 1! You are only half done though. BE SURE TO COMPLETE PART 2 OF THE EXAM!

------------------------------------------------------------------------------------------------------------------------