0% found this document useful (0 votes)
33 views

Assignment 1

The document describes an assignment to create a Python program that manages a database of movies. It includes tasks to implement functions to print movies from the database, add a new movie, search for movies by title or director, update a movie record, delete a movie, and sort the movies.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Assignment 1

The document describes an assignment to create a Python program that manages a database of movies. It includes tasks to implement functions to print movies from the database, add a new movie, search for movies by title or director, update a movie record, delete a movie, and sort the movies.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

assignment-1

February 29, 2024

1 CSCI 115

2 Assignment 1
2.1 Topics: Python Exploration with Data Management
In this assignment, you will create a Python program that manages a database of movies. You will
implement functions
to add, search, update, and delete movie records. Each movie record will be stored as a dictionary
within a list,
simulating a basic database. You will practice using lists, dictionaries, functions, and basic search
algorithms.
Implementing a Print Function (20 Points)
Task: Develop a function named print_movies(movie_db, n) that prints the details of the first
n movies from the provided movie_db list. Your function should format the output in a reader-
friendly manner, displaying each movie’s title, director, year, and genre on separate lines, followed
by a blank line between each movie record. 1) Correctly implementing the function: 15 points
2) Output formatting: 5 points
Your function must take two parameters: the movie database (movie_db) and an integer (n)
indicating the number of movies to print.
If n is greater than the number of movies in the database, your function should gracefully handle
this by printing all available movies without raising an error.
The output should be well-formatted, making it easy to read and distinguish between different
movie records.
Each movie’s details should be printed according to the following format:
Title: [Movie Title]
Director: [Director’s Name]
Year: [Release Year]
Genre: [Genre]

1
2.1.1 Initializing the movie database

[ ]: movie_db = [
{'title': "Inception", 'director': "Christopher Nolan", 'year': 2010,␣
↪'genre': "Science Fiction"},

{'title': "The Matrix", 'director': "Lana Wachowski, Lilly Wachowski",␣


↪'year': 1999, 'genre': "Action"},

{'title': "Pulp Fiction", 'director': "Quentin Tarantino", 'year': 1994,␣


↪'genre': "Crime"},

{'title': "Blade Runner 2049", 'director': "Denis Villeneuve", 'year':␣


↪2017, 'genre': "Science Fiction"},

{'title': "The Grand Budapest Hotel", 'director': "Wes Anderson", 'year':␣


↪2014, 'genre': "Comedy"},

{'title': "Moonlight", 'director': "Barry Jenkins", 'year': 2016, 'genre':␣


↪"Drama"},

{'title': "Parasite", 'director': "Bong Joon Ho", 'year': 2019, 'genre':␣


↪"Thriller"},

{'title': "Interstellar", 'director': "Christopher Nolan", 'year': 2014,␣


↪'genre': "Science Fiction"},

{'title': "Mad Max: Fury Road", 'director': "George Miller", 'year': 2015,␣
↪'genre': "Action"},

{'title': "Spirited Away", 'director': "Hayao Miyazaki", 'year': 2001,␣


↪'genre': "Animation"}

2.1.2 Example usage


print_movies(movie_db, 3)

2.1.3 Expected output:


(1) Title: Inception
Director: Christopher Nolan
Year: 2010
Genre: Science Fiction
(2) Title: The Matrix
Director: Lana Wachowski, Lilly Wachowski
Year: 1999
Genre: Action
(3) Title: Pulp Fiction
Director: Quentin Tarantino
Year: 1994

2
Genre: Crime
[1]: # Part 1, write code here
def print_movies(movie_db, n):
pass # replace and write function implementation

Adding a New Movie (20 Points)


Task: Write a function add_movie(movie_db, movie) that takes the movie database and a movie
record as arguments, and adds the movie to the database. Ensure no duplicate movie titles are
added. 1) Function implementation: 10 points 2) Duplicate check: 10 points

2.1.4 Example usage:


add_movie(movie_db, {‘title’: ‘Inception’, ‘director’: ‘Christopher Nolan’, ‘year’: 2010, ‘genre’:
‘Sci-Fi’})

2.1.5 Expected output:


You cannot add this movie, it already exists

2.1.6 Example usage:


add_new_movie(movie_db, “Avatar: The Way of Water”, “James Cameron”, 2022, “Science Fic-
tion”)

2.1.7 Expected output:


Sucessfully added
[2]: # Part 2, write code here
def add_movie(movie_db, movie):
pass # replace and write function implementation

Searching for Movies (20 Points)


Task: Implement two search functions:
1) search_by_title(movie_db, title) which returns all movies matching the given title.
Correct implementation of search_by_title: 10 points
2) search_by_director(movie_db, director) which returns all movies directed by the given di-
rector.
Correct implementation of search_by_director: 10 points

2.1.8 Example usage:


print(search_by_title(movie_db, ‘Inception’))

2.1.9 Expected Output:


[{‘title’: ‘Inception’, ‘director’: ‘Christopher Nolan’, ‘year’: 2010, ‘genre’: ‘Science Fiction’}]

3
2.1.10 Example usage:
print(search_by_director(movie_db, ‘Christopher Nolan’))

2.1.11 Expected Output:


[{‘title’: ‘Inception’, ‘director’: ‘Christopher Nolan’, ‘year’: 2010, ‘genre’: ‘Science Fiction’}, {‘ti-
tle’: ‘Interstellar’, ‘director’: ‘Christopher Nolan’, ‘year’: 2014, ‘genre’: ‘Science Fiction’}]

[ ]: # Part 3, write code here


def search_by_title(movie_db, title):
pass # replace and write function implementation

def search_by_director(movie_db, director):


pass # replace and write function implementation

Updating a Movie Record (10 Points)


Write a function update_movie(movie_db, title, updated_info) where title is the movie to update,
and updated_info is a dictionary with new values for some attributes of the movie. If the movie
exists, update it; otherwise, print a message indicating the movie is not found.

2.1.12 Example usage:


update_movie_info = {‘year’: 2021, ‘genre’: “Action”}
update_movie(movie_db, “Inception”, update_movie_info)

2.1.13 Expected Output:


Movie titled ‘Inception’ has been updated.
[2]: #Part 4 write code here
def update_movie(movie_db, title, updated_info):
pass # replace and write function implementation

Deleting a Movie (10 Points)


Create a function delete_movie(movie_db, title) that removes the movie with the given title from
the database. If the movie does not exist, print an appropriate message.

2.1.14 Example usage:


delete_movie(movie_db, “The Matrix”)

2.1.15 Expected Output:


Movie titled ‘The Matrix’ has been deleted from the database.

2.1.16 Example usage:


delete_movie(movie_db, “Ghost Movie”)

4
2.1.17 Expected Output:
Movie titled ‘Ghost Movie’ not found in the database.
[4]: #Part 5 write code here
def delete_movie(movie_db, title):
pass # replace and write function implementation

Sorting Movies (20 Points)


Task: Write a function sort_movies(movie_db, key, descending=False) that sorts the movies in
the database based on a given key (e.g., year, title). The descending parameter determines whether
the sort should be in ascending (default) or descending order.

2.1.18 Example usage:


sort_movies(movie_db, ‘year’)
print_movies(movie_db, 3)

2.1.19 Expected Output:


The database are sorted based on year of the movie
(1) Title: Pulp Fiction
Director: Quentin Tarantino
Year: 1994
Genre: Crime
(2) Title: The Matrix
Director: Lana Wachowski, Lilly Wachowski
Year: 1999
Genre: Action
(3) Title: Spirited Away
Director: Hayao Miyazaki
Year: 2001
Genre: Animation
[5]: #Part 6 write code here
def sort_movies(movie_db, key, descending=False):
pass # replace and write function implementation

You might also like