CW Inheritance and Composition
10 Points Possible
2026/10/27
Attempt 1
In Progress
NEXT UP: Submit Assignment
Add Comment
2026/10/19 to 2026/10/30
Details
Problem statement
Using the provided template (recommender_TEMPLATE.py (https://umd.instructure.com/courses/1412783/files/91477169?
wrap=1) ), complete a course recommendation system that supports different ways of ranking courses. Each course
contains information such as a title, video length, number of views, and descriptive tags. Save and submit the file as
recommender.py .
The program will read courses from a JSON file (courses.json
(https://umd.instructure.com/courses/1412783/files/91477170?wrap=1) , put in the same folder as your recommender.py ) and
recommend up to three courses that fit within a user's available time. The order of the recommendations will depend on
the ranking policy used by the recommender.
In this assignment, you will use inheritance to implement different ranking policies and composition to connect a
recommender with its courses and ranking policy.
Update 2026-10-23
The Gradescope autograder has been updated.
The updated autograder will show you a breakdown of all of the scoring tests run against your code (8/10 points with 1
manual point for style and documentation, and 1 manual point for the answers to the two questions). All tests give details.
Failing tests will have details about the test that was run, the expected value, and the actual (failing) value.
You can keep submitting your code in Gradescope and iterating while eliminating bugs.
Instructions
Provided code
The template includes a completed Course class and the read_courses() and rank_courses() functions. Do not modify
these components. Read their code and docstrings before implementing your classes.
Course class (supplied)
Each Course object stores the following attributes:
title : the title of the course (string).
length : the length of the course in minutes (integer).
views : the number of times the course has been viewed (integer).
tags : descriptive tags associated with the course (list of strings).
The class also provides the following methods:
has_tag(tag) : returns True
if the given tag appears in the course's tags, and False otherwise.
within_length(max_length) : returns True
False
if the course's length is less than or equal to the given maximum length, and
otherwise.
match_score(interests) : returns the number of course tags that appear in the user's list of interests.
read_courses() function (supplied)
The read_courses(file_path) function reads course information from a JSON file, creates a Course object for each course,
and returns a list of those objects.
rank_courses() function (supplied)
The rank_courses(courses, policy, interests) function takes a list of Course objects, a ranking-policy object, and a list of
the user's interest tags. It calls the policy's ranking_key() method for each course and returns a new list ordered from
highest to lowest ranking key.
If two courses have identical ranking keys, they remain in their original relative order. This function does not modify the
original list.
Ranking policy class requirements
Implement three classes: RankingPolicy , PopularityPolicy , and InterestPolicy . Both PopularityPolicy and
InterestPolicy must inherit from RankingPolicy .
Every ranking policy must support the following method:
ranking_key(self, course, interests)
The method takes a Course object and a list of strings representing the user's interests. It returns a tuple containing two
integers. Courses with larger ranking keys appear earlier in the recommendations.
Ranking keys are compared by their first value. If those values are equal, their second values are compared. For example:
(3, 500)
ranks ahead of (2, 2000) because 3 is greater than 2 .
(3, 800)
ranks ahead of (3, 500) because the first values are equal and 800 is greater than 500 .
RankingPolicy class
Create a class named RankingPolicy that represents a default ranking policy.
Implement its ranking_key() method so that it returns (0, 0) for every course. This policy gives all courses equal priority.
PopularityPolicy class
Create a class named PopularityPolicy that inherits from RankingPolicy .
Override ranking_key() so that it returns a tuple containing the course's view count followed by 0 . For example, a course
with 540 views should receive the ranking key (540, 0) . Courses with more views will therefore rank earlier.
This method must accept the interests argument to support the same interface as the other policies, but it does not
need to use it.
InterestPolicy class
Create a class named InterestPolicy that inherits from RankingPolicy .
Override ranking_key() so that it returns a tuple containing the course's interest-match score followed by its view count.
Use the course's provided match_score() method to calculate the first value.
For example, a course with a match score of 2 and 540 views should receive the ranking key (2, 540) . Courses with
higher match scores will rank earlier. If two courses have equal match scores, the course with more views will rank earlier.
Recommender class requirements
Create a class named Recommender that stores a collection of courses and uses a ranking-policy object to generate
recommendations.
Required attributes
courses : a list of Course
objects.
policy : a RankingPolicy
object or an object belonging to one of its subclasses.
__init__() constructor
__init__(self, courses, policy)
Write a constructor that takes a list of Course objects and a ranking-policy object. Initialize the recommender's courses
and policy attributes using the provided values.
You may store the provided course list directly. You do not need to make a copy in the constructor.
set_policy() method
set_policy(self, policy)
Write a method that replaces the recommender's current policy with the provided ranking-policy object. It should not
change the stored courses. This method does not return a value.
recommend() method
recommend(self, interests, max_length)
Write a method that takes a list of strings representing the user's interests and a maximum course length in minutes. It
should return a new list containing up to three recommended Course objects.
Your method should perform the following steps:
1. Create a new list containing only courses whose lengths are less than or equal to max_length . Use each course's
within_length() method to check this condition.
2. Use the provided rank_courses() function to rank the eligible courses using the recommender's current policy and the
user's interests.
3. Return the first three courses from the ranked list.
Your implementation must meet these additional requirements:
If no courses satisfy the length limit, return an empty list.
If only one or two courses satisfy the length limit, return a list containing those courses in ranked order.
Courses with no matching interest tags are still eligible. Interest matching affects their ranking under InterestPolicy ,
not whether they may be recommended.
Do not modify the contents or order of the recommender's stored course list.
Do not use conditional statements to check which policy class is being used. Pass the current policy to
rank_courses() , which calls the policy's shared ranking_key() method.
Do not duplicate the ranking rules inside Recommender . Those rules belong in the policy classes.
Testing your implementation
Run the demonstration provided in the template. It uses the same recommender to generate recommendations with
PopularityPolicy , then replaces the policy with InterestPolicy
and generates recommendations again.
Test your implementation with the following cases:
A course whose length is exactly equal to the maximum length.
No courses that satisfy the length limit.
Only one or two courses that satisfy the length limit.
An empty course list.
An empty list of user interests.
Courses with equal match scores but different view counts.
Courses with identical ranking keys.
A policy change that produces different recommendations without changing the stored courses.
Design questions
Answer the following questions in the designated comments in the template. Write two or three sentences for each
answer.
1. Why does Recommender store a ranking-policy object rather than inherit from InterestPolicy ?
2. Suppose you add a new ranking policy that ranks shorter courses first. Which existing classes, if any, would need to
change? Explain how your recommender could use the new policy.
Other instructions
Include your name and any partner's name in the STUDENT_NAMES constant in the code template.
Write docstrings for each method you implement. Start with a brief statement of the method's purpose. Include an
section documenting arguments other than self , and a Returns section describing the return value, if any.
We encourage you to leave in passing assert statements you used to check your work.
Args
Do not add imports. All necessary imports and file-reading code are supplied in the template.
Do not modify the supplied Course class, read_courses() function, or rank_courses() function.
Academic integrity
Please do not look at anyone else's solution to this problem. Do not use generative AI to solve this problem. As with many
aspects of education, the journey is more important than the destination; jumping to the solution robs you of the
experience of working through the problem. If you need help, please come to office hours.
This assignment is to be done with a partner, or individually if you were unable to attend class or unable to finish during
class time.
Disseminating these instructions in whole or in part without written permission of the instructor is considered an infraction
of academic integrity. Posting the instructions, or any part thereof, on the internet is considered dissemination and is
strictly prohibited.
INST326-020x: Fall 2026
Fall 2026
Course ID: 1367510
Name
Status
Released
Due (EDT)
1 week, 1 day left
CW Regular Expressions
No Submission
Oct 26 at 12:01AM
Nov 03 at 11:59PM
Late Due Date: Nov 06 at 11:59PM
4 days, 10 hours left
Project 2
No Submission
Oct 05 at 12:01AM
Oct 30 at 11:59PM
Late Due Date: Nov 06 at 11:59PM
1 day, 10 hours left
CW Inheritance and Composition
No Submission
Oct 19 at 12:01AM
Oct 27 at 11:59PM
Late Due Date: Oct 30 at 11:59PM
(https://umd.instructure.com/courses/1412783/modules/items/14854726)
(https://umd.instructure.com/courses/1412783/mod