Discussing an exercise proposal

Hello there.

I have created an exercise and I would like to hear your opinions about it.

Summary: An exercise about working with collections of key:value pairs, in the context of an information retrieval problem.

Proposal:
After some initial success with your new website, Netflow(insert random website name), you have decided to implement a recommendation system to suggest movies to users based on their preferences.

Your task consists of implementing two functions to provide the desired interface.

Filter(movie_collection,user_preferences,max_suggestions,threshold) => another_movie_collection

Rank(movie,user_preferences) => real_number

You will be given a set of movies and a user profile. You will have to return the most relevant movies according to the user profile. The rank of a movie given some preferences must be above certain relevance threshold to be recommend in order to maintain a quality standard. If a tie occurs between two or more movies you should return the one with lexicographically smaller name, in the sake of simplicity.

To calculate the score of a movie for a given user you should use a provided formula.
A movie can be represented by a collection of key value pairs.
Name:…
Producer:…
Drama:0.32
User preferences are also represented by a collection of key value pairs.
Name:…
Romance:0.10
Drama:0.25
…
The rank of a movie can calculated as the weighted average of the user preferences by the movie features.
For example:
movie[drama] * user_prefrences[drama] +…

Some concepts required:
Arithmetic
Loops
Conditionals
Dictionaries
Sorting
Custom sorting

Do you mind presenting an example solution (in a not too exotic programming language) that people will be able to understand?

That helps getting a feeling for what this exercise is about, how hard it is and whether it’s a good fit for exercism.

Sure. I can provide a python snippet showing an example solution.

Test:

movies = [
    {"name": "Inception", "id": 1, "features": {"action": 5, "sci-fi": 4, "drama": 2}},
    {"name": "The Matrix", "id": 2, "features": {"action": 5, "sci-fi": 5}},
    {"name": "La La Land", "id": 3, "features": {"drama": 5, "romance": 4}},
    {"name": "Die Hard", "id": 4, "features": {"action": 5, "thriller": 3}},
    {"name": "Interstellar", "id": 5, "features": {"sci-fi": 5, "drama": 3}}
]

user_preferences = {"action": 3, "sci-fi": 2, "drama": 1}
threshold = 15  # Minimum rank to include
limit = 3       # Max movies to return

expected = [
    (25, "Inception", 1),
    (25, "The Matrix", 2),
    (15, "Die Hard", 4)      
]

Code:

def rank(movie_features,user_preferences):
    rank = 0
    for key in movie_features:
        rank += movie_features[key] * user_preferences.get(key,0)
    return rank

def filter(movies,user_preferences,threshold,limit):
    filtered_movies = []
    for movie in movies:
        movie_rank = rank(movie["features"],user_preferences)
        if movie_rank < threshold:
        continue
        movie_name = movie["name"]
        movie_id = movie["id"]
        filtered_movies.add((movie_rank,movie_name,movie_id))

    filtered_movies.sort(lambda movie: (-movie[0],movie[1])
    return filtered_movies[:limit]

Thank you. Now we will have to wait for others to comment.

Would this exercise cover any “new ground” not covered by existing exercises? Dictionaries and multi-criteria sorting is already covered by, say, grade school and tournament.

Hello Isaac. Indeed grade school, and tournament already cover dictionary usage and multi-criteria sorting. I just checked. In this sense the proposed exercise doesn’t cover any new ground.

While reading again the proposal I consider it’s main feature to be to present the user with the task of building some components of a recommendation system.

However, I understand if this is out of the scope of Exercism, and will consider focusing on language features on future proposals.

Thanks for feedback.

Exercism exercises are all stand alone. As such, this exercise wouldn’t act like a component in a larger system, but as an arbitrary sorting exercise. Which would be good to add, if we didn’t already have two.

Exercism has concept exercises and practice exercises. Concept exercises are tightly coupled to syllabus lessons and language features. Practice exercises are not tied to any specific language and tend not to be tied to any specific language feature. They are more general and/or can be linked to a general programming problem (eg sorting), algorithm (eg sieve) or data structure (eg linked lists).

1 Like

Thanks for the explanation Isaac :+1:.