Goals Practice reading files Practice using dictionaries Programming style is im
ID: 3644756 • Letter: G
Question
GoalsPractice reading files
Practice using dictionaries
Programming style is important! Remember to
Include a docstring in every function
Use whitespace between operators and operands
Use descriptive variable names
Add appropriate comments.
Rubric
9 points possible: for each of the following -
1 pt. for code heading in the right direction
1 pt. for working code
1 pt. for programming style.
find the most recommended movies in the movie dictionary
report the most recommended movies
report all of the movies in alphabetical order
Project 8 code to get started with
For this project, open and edit project8.py. Save it as project8.py as soon as you have entered code that you would not like to lose.
Movie Madness
As part of project 1, you each supplied the name of a favorite movie. We collected this information and consolidated it into a single text file full of titles, cis122movies.txt. For this assignment, you will analyze this movie data and determine the most popular movies in the class.
Write a function analyzeMovieData(filename) that takes a single argument, the name of a file of movie titles. This function should open up the specified file, and read through each of the titles within it, keeping track of the number of times each movie title occurs. You may find a dictionary helpful for this purpose. Having stored this data, your function should determine the most popular movie, and print it out. If multiple movies are tied for first, print all of them. Afterwards, print out all of the recommended movies in alphabetical order.
For testing purposes, here is a much smaller movie file, shortmovielist.txt, consisting of the following five movies:
Sneakers
Tron
The Matrix
Tron
Short Circuit
If you run your analyzeMovieData function on this file, it should run as follows:
>>> analyzeMovieData("shortmovielist.txt")
The most popular movie(s) is(are):
Tron
All of the recommended movies are:
Short Circuit
Sneakers
The Matrix
Tron
NOTE: When running your code, make sure to place the movie file in the same location as your code file. Otherwise, Python will not be able to find it.
CHALLENGE: When printing out all of the recommended movies, do not include movie(s) already printed in the most popular movies report.
Explanation / Answer
def analyzeMovieData(filename): movie_names = [] popular_movies = [] recomended_movies = [] for each_line in open(filename) : movie_names.append(each_line) movie_dict = {} for each_movie in movie_names : if each_movie in movie_dict : movie_dict[each_movie] += 1 else : movie_dict[each_movie] = 1 most_popular_movie = max(movie_dict.values()) for each_movie in movie_dict : if movie_dict[each_movie] == most_popular_movie : popular_movies.append(each_movie) else : recomended_movies.append(each_movie) if len(popular_movies) > 1 : print 'Most popular movies are:' else : print 'Most popular movie is ' for each_movie in sorted(popular_movies): print each_movie print 'Recomended movies :' for each_movie in sorted(recomended_movies) : print each_movie
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.