Lab 7
Using sort
Submit after the end of the lab session

Overview

For this lab, you will apply the sort method to a list of dictionary objects, each representing a movie. The application allows you to present a list of recommendations based on an example movie, sorted in terms of the movies that are most similar.

As an example consider this example run:

>>> recommend(load_movies(), 'pulp') Top recommendations based on Pulp Fiction Pulp Fiction Beau Travail In the Mood for Love Lost in Translation Come and See Brokeback Mountain There Will Be Blood Taxi Driver All the President’s Men Under the Skin

Here, the load_movies function produces a list of dictionary objects, each representing a movie. The string 'pulp' matches the title of Pulp Fiction, which is then used as the basis to recommend 10 similar movies (of course, Pulp Fiction is most similar to itself).

Instructions, questions, and code writing

  1. Download the zip folder for the lab.
  2. Study the sort_by_year function. Notice how it refers to the year_key method so that the sort knows which key to use for the sorting. You can try it out with this code:
    mlist = load_movies() sort_by_year(mlist) print_top_ten(mlist)
  3. Write a new sort function called sort_by_title, which should sort the movies by their title. Like sort_by_year, you will need to create a helper function (e.g. title_key) so that your sort method knows which key to use. Test it to be sure it works.
  4. Study the similarity function, which takes two dictionary entries, each representing a movie. Try calling it with the movie dictionary objects. You'll see that it returns a number from 0 to 1. Do you similarity provides a good indicator of how similar two movies are?
  5. Write a function called find_movie. It should take two parameters: a list of the movie dictionary objects and a string. It should return the first dictionary object whose title contains the string with case being insensitive. Here's an example run:
    find_movie(load_movies(), 'pulp') {'title': 'Pulp Fiction', 'rank': '35', 'year': 1994, 'genre': 'Drama'}
  6. Write the recommend function as described at the beginning of this lab. Here is an outline for completing it:
    1. Use your find_movie function to obtain the movie dictionary object identified by the string (second parameter).
    2. Using the similarity method, run through the movie_list and add a new dictionary attribute called 'similarity' and assign it the similarity value of the retrieved object and each object in the list. Now, each dictionary object has a similarity value.
    3. Sort the list in reverse order by the similarity key.
    4. Use the print_top_ten function to display the top recommendation.
  7. Modify your recommend function so that if the movie is not found (find_movie will return None), write a message saying there are no recommendations.
  8. Try the recommend function with several movies. Does it work well? Do you have ideas for improving it?

Deliverable

Create a text or pdf file that contains the following:

  1. A statement that summarizes your completion of the lab. As appropriate, the statement should include the following:
    • Who you worked with on the lab
    • Any difficulties you encountered
    • Answers to questions
    • Your summary of your experience
    • Demonstration that your code works
    • A summary of the files included in your submitted folder

Put your file and Python code in a folder, zip it and submit it under Lab 7 on D2L. Check that your submitted zip file is complete.

Grading

Your lab submission will be graded using the following rubric:

  • + .5 --- Your submission is clearly formatted.
  • + .5 --- Your submission includes a summary statement and includes how you collaborated.
  • + .5 / 1.0 --- You submitted most of the lab (0.5) or you submitted all of the lab (1.0).
  • + .5 --- Your lab submission is generally correct.