Course Enrollment With a Waitlist
Implement a class Course that enrolls students up to a capacity and keeps everyone else on a waitlist. Whenever fewer than capacity students are enrolled and someone is waiting, the first student on the waitlist is enrolled. Write these methods:
Course(capacity)starts with nobody enrolled or waiting.join(student)returns"already"ifstudentis enrolled or waiting (nothing changes). Otherwise the student is enrolled if there is room and it returns"enrolled", or goes to the end of the waitlist and it returns"waitlisted".drop(student)takesstudentout of the course or off the waitlist. Returns the name of the student who got enrolled because of this, or""if nobody did (also whenstudentwas neither enrolled nor waiting).change_capacity(capacity)sets a new capacity and returns how many waiting students got enrolled because of it. Lowering the capacity never removes anyone who is enrolled.waitlist_spot(student)returns0ifstudentis enrolled, their place on the waitlist (1is first) if they are waiting, or-1otherwise.get_roster()returns the enrolled students in the order they were enrolled.
The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
Course(1)
join("ana") returns "enrolled"
join("ben") returns "waitlisted"
join("cy") returns "waitlisted"
join("ben") returns "already"
drop("ana") returns "ben"
change_capacity(3) returns 1
get_roster() returns ["ben","cy"]
waitlist_spot("ana") returns -1
When ana drops, ben moves up from the waitlist. Raising the capacity to 3 makes room for cy as well, and ana is no longer in the course at all.
Constraints
0 <= capacity <= 4studenthas1to5lowercase letters- Each test makes
1to40method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.