In: Computer Science
Please code the following prompt in python.
Today you will be building a movie/actor lookup. The data structure that will power your lookups will be a dictionary which maps movies (as strings) to a list of actors (you may also use a set if you prefer). You will also add features such as finding the most common actor in a data set, or the movies that two actors have been in.
The first function you should write is add_or_update_a_movie. This function takes the movie dictionary, a movie, and a list of actors. You will need this for all other functions, so complete this feature first. If a movie is added that doesn't exist in the dictionary yet, make a new entry. If it does exist, the list of actors should be added to the current list of actors, with no actors repeated.
All of the other functions in movies.py are explained in the documentation. The last function is for extra credit and will require some knowledge of recursive functions.
Incorporate a function that should have the signiture
def degrees_between(movie_dict, actor1, actor2):
and uses the following file to open and create a dictionary:
https://s3.amazonaws.com/mimirplatform.production/files/a5dac972-8c2d-4565-95be-4b036cb08e7b/movies_large.txt
I have completed the read function but no details given for recursive function degrees_between how to do that so i have skipped this
def add_or_update_a_movie(movies, movie_name, actors):
# checking if movie name is in list
if movie_name not in movies:
# if not adding entry
movies[movie_name] = list(set(actors))
else:
# if movie name already found adding actor list to existing , by using set removing duplicate
movies[movie_name] = list(set(movies[movie_name].extend(actors)))
if __name__ == '__main__':
# name of file to be read
# TODO: update the file as per your directory
file_name = 'movies_large.txt'
movies = {}
with open(file_name, encoding='utf-8') as f:
while f:
line = f.readline().strip()
if line:
split_entry = list(map(str.strip, line.split(',')))
# calling function with attributes
add_or_update_a_movie(movies, split_entry[0], split_entry[1:])
else:break
print(movies)
# OUT
{'15 Minutes (2001)': ['Edward Burns', 'Robert De Niro'], '16
Blocks (2006)': ['Bruce Willis'], '21 Grams (2003)': ['Rodrigo
Prieto', 'Naomi Watts'], '25th Hour (2002)': ['Edward Norton',
'Rosario Dawson', 'Brian Cox', 'Rodrigo Prieto'], '28 Days (2000)':
['Steve Buscemi'], '40 Days and 40 Nights (2002)': ['Elliot Davis',
'Josh Hartnett'], '50 First Dates (2004)': ['Adam Sandler', 'Drew
Barrymore', 'Rob Schneider'], '8 Mile (2002)': ['Brittany Murphy',
'Rodrigo Prieto'], 'Abandon (2002)': ['Matthew Libatique',
'Benjamin Bratt', 'Zooey Deschanel'], 'About a Boy (2002)':
['Rachel Weisz', 'Toni Collette'], 'Adventures of Pluto Nash':
['Oliver Wood', 'Alec Baldwin', 'Rosario Dawson', 'Luis
Guzm√°n', 'Eddie Murphy'], 'Adventures of Rocky &
Bullwinkle': ['Robert De Niro', 'Rick Kain', 'Janeane Garofalo',
'Thomas Ackerman'], 'Affair of the Necklace': ['Hayden Panettiere',
'Brian Cox', 'Christopher Walken'], 'After the Sunset (2004)':
['Edward Norton', 'Don Cheadle'], 'Agent Cody Banks (2003)':
['Hilary Duff', 'Keith David'], 'Agent Cody Banks 2: Destination
London (2004)': ['Anthony Anderson'], 'Alex & Emma (2003)':
['John Tobin', 'Luke Wilson'], 'Alexander (2004)': ['Angelina
Jolie', 'Rodrigo Prieto', 'Anthony Hopkins']}
..... more out put