CS106 Instructor: Michael Eckmann Summer 2026 Date given: 07/20/2026 Date due: 07/24/2026 This is an individual assignment. You are not allowed to work with others, nor ask for solutions or partial solutions online or with AI. Feel free to ask me (your instructor) questions in person or via email. If you ask me questions via email it is helpful for me to see your code --- please send it as an attached file with extension txt (my email client does not allow me to look at .py files). Don't send .py files as an attachment nor images of your code. Comment your code to summarize what chunks of your code are doing as well as comments at the top in this format: ############################ # # Name: moviedb.py # # Description: A program to sort and query a database of movies. # # Written by: Your name # # Date: (range of dates you worked on it or just the date it was completed) # ############################ Start from (that is, copy/paste) the selection_sort code, binary_search code, and linear_search code below. # # SELECTION SORT # def selectionsort(alist): for phase in range(len(alist) - 1): maxidx = 0 for i in range(1,len(alist) - phase): if alist[maxidx] < alist[i]: maxidx = i temp = alist[maxidx] alist[maxidx] = alist[i] alist[i] = temp # # BINARY SEARCH # # assumes alist is sorted low to high # returns index of key if it is in alist # returns -1 otherwise def binary_search(alist, key): count = 0 startidx = 0 endidx = len(alist) - 1 while startidx <= endidx: count += 1 mididx = (startidx + endidx ) // 2 if key == alist[mididx]: return mididx elif key < alist[mididx]: endidx = mididx - 1 else: startidx = mididx + 1 return -1 # # LINEAR SEARCH # # returns index of key if it is in alist # returns -1 otherwise def linear_search(alist, key): for i in range(len(alist)): if key == alist[i]: return i # found at i return -1 # not found ------------------- Edit the selection_sort function to take in 3 lists and an optional sortby parameter that defaults to 0. The sortby parameter will be either 0 to sort the lists by movie title (low to high) 1 to sort the lists by year (low to high) 2 to sort the lists by imdb rating (high to low) Also edit the selection_sort code then to use those new parameters appropriately. e.g. if sorting the lists by title, make sure the years and ratings also move around to "stay with" their titles. Similar if sorting by year or rating. Hints: You'll need to check the sortby parameter to decide which list is the one that needs its elements compared to each other. When swapping items during sorting, make sure to swap the corresponding elements in all three lists. ---------------------- The binary_search code should not need to be edited. ---------------------- Edit the linear_search code to return a list of indices where the key is found --- Note: because you'll use this linear_search to search the list of years and since year is not unique it is possible to have multiple instances of a year and you want to find all of the ones that match. Even though linear search does not require a sorted list, assume the list is sorted and take advantage of that fact in your edited code. Start searching from the beginning as normal, but stop the search when the element is > key or you have reached the end of the list. Also, if the key is not found, return an empty list. e.g. for a list like: 1999, 2000, 2000, 2002, 2003 if searching for 2001 stop when you get to 2002 and return the empty list for a list like: 1999, 2000, 2000, 2002, 2003 if searching for 2000 also stop when you get to 2002 but return the list [1, 2] because 2000 was found in the list at indices 1 and 2. ------------------------------- Main Program You are given a list of movies (copy/paste these three lists into your program), where each movie's information is stored in parallel lists: titles = ["Inception", "The Matrix", "Interstellar", "The Godfather", "The Dark Knight"] years = [2010, 1999, 2014, 1972, 2008] ratings = [8.8, 8.7, 8.6, 9.2, 9.0] You will use sorting and searching to organize and query this data. Ask the user: Would you like to sort, search, or quit? If the user wants to sort then Ask the user what they want to sort by: "title", "year", or "rating" Use the edited selection_sort algorithm to reorder all three lists together (so titles, years, and ratings stay aligned). Then after sorting, loop through the lists and print them out. Example: Would you like to sort, search, or quit? sort Sort by (title/year/rating): rating Movies sorted by rating: The Godfather (1972) - 9.2 The Dark Knight (2008) - 9.0 Inception (2010) - 8.8 The Matrix (1999) - 8.7 Interstellar (2014) - 8.6 If the user wants to search, Ask the user whether they want to search by "title" or "year". Ask the user the title or year depending on previous answer. Keep track of which way the movies are currently sorted (by title, year, or rating). If the movies are not already sorted by their choice (title or year) then sort them by title or year and use binary search (if searching by title) or linear_search (if searching by year) to perform the search and print the result (title, year, rating), or say if it wasn't found. Example: Would you like to sort, search, or quit? search Search by (title/year): title Enter title: Inception Found: Inception (2010) - 8.8 Note: you may assume titles are unique. However years are not unique as multiple movies come out in the same year. Repeat the original question (Would you like to sort, search, or quit?) until user enters quit. Extra Credit: EC1: Add a feature to show only movies above a certain rating that they enter. Change question to ask user: Would you like to sort, search, show high rated or quit (enter sort, search, show, or quit)? If they enter "show", ask for the rating that is minimum to show. EC2: Allow the user to add a new movie (title, year, rating) to the lists. Change question to ask user: Would you like to add a movie, sort, search, show high rated or quit (enter add, sort, search, show, or quit)? EC3: Rather than have the lists of movie information hardcoded like: titles = ["Inception", "The Matrix", "Interstellar", "The Godfather", "The Dark Knight"] years = [2010, 1999, 2014, 1972, 2008] ratings = [8.8, 8.7, 8.6, 9.2, 9.0] instead when the program starts up read that information from a file named movies.txt whose format will be one movie per line with comma separated values like: Inception,2010,8.8 The Matrix,1999,8.7 Interstellar,2014,8.6 The Godfather,1972,9.2 The Dark Knight,2008,9.0 And when the user quits the program, overwrite the movies.txt file with the data in the lists in the same format as above (title,year,rating) so that if the user added any movies to the database, they will go in the file at the end so that each time you run the program it can retain any added movies. e.g. if say the user added this movie: title: Project Hail Mary year: 2026 rating: 8.2 the output file would contain: Inception,2010,8.8 The Matrix,1999,8.7 Interstellar,2014,8.6 The Godfather,1972,9.2 The Dark Knight,2008,9.0 Project Hail Mary,2026,8.2