← 2023 questions

2023 / NETFLIX / QUESTION 30

Viewing recommendations

Try the question in your own editor, then check your output below.

THE QUESTION

Charlotte has a Netflix account with thousands of movies and TV shows. She wants to create a program that can recommend a movie or TV show for her to watch based on her previous viewing history.

To do this, Charlotte has collected a dataset of her previous viewing history in a text file called "viewing_history.txt". Each line of the file contains the title of a movie or TV show she has watched, followed by a comma, and the genre of the movie or TV show.

Charlotte also has a list of favorite genres, which she has stored in a separate text file called "favorite_genres.txt". Each line of the file contains a single genre.

Your task is to write a program that uses this data to recommend a movie or TV show for Charlotte to watch. Your program should satisfy the following requirements:

1. The program should read in the "viewing_history.txt" file and store the data in a list of tuples, where each tuple contains the title of a movie or TV show and its genre.

2. The program should read in the "favorite_genres.txt" file and store the data in a list of strings.

3. The program should define a subroutine that takes the list of tuples and the list of favorite genres as input, and returns a list of movies or TV shows that match one of the favorite genres.

4. The program should use the "random" module to randomly select a movie or TV show from the list returned by the subroutine.

5. The program should print the title of the selected movie or TV show to the console.

This question requires you to create your own text file

Starter code:

import random

def get_viewing_history(filename):
    # TODO: Implement this function
    pass

def get_favorite_genres(filename):
    # TODO: Implement this function
    pass

def get_recommendations(viewing_history, favorite_genres):
    # TODO: Implement this function
    pass

if __name__ == '__main__':
    viewing_history = get_viewing_history('viewing_history.txt')
    favorite_genres = get_favorite_genres('favorite_genres.txt')
    recommendations = get_recommendations(viewing_history, favorite_genres)
    if recommendations:
        recommendation = random.choice(recommendations)
        print(recommendation[0])
    else:
        print('Sorry, no recommendations available.')

Question by Eleonora. Source: Girls In Code SEA — Behind the Screens, 2023, slides 48–49.

PRACTICE CHECK

Try your answer.

New tiny datasets; the original task asks participants to create their own files.

Run your program in your own editor, then paste its output below. This checks the stated output or checkpoint; it does not assess your full program. Practice attempts do not affect competition scores.

New viewing_history.txt: Orbit,Sci-Fi; Meadow,Drama; Comet,Sci-Fi. favorite_genres.txt contains Sci-Fi. Enter a possible recommended title.

Reveal worked answer

Recommended title · one valid answer

Orbit

Filter history to matching genres, then choose randomly from Orbit and Comet.