12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

144 Chapter 14. FilesExercise 14.7 The Internet Movie Database (IMDb) is an online collection of information aboutmovies. Theirdatabaseisavailableinplaintextformat,soitisreasonablyeasytoreadfrom<strong>Python</strong>.Forthisexercise,thefilesyouneedareactors.list.gzandactresses.list.gz;youcandownloadthem fromwww.imdb.com/interfaces#plain.I have written a program that parses these files and splits them into actor names, movie titles, etc.You can download itfromthinkpython.com/code/imdb.py.If you run imdb.py as a script, it reads actors.list.gz and prints one actor-movie pair per line.Or, if you import imdb you can use the function process_file to, well, process the file. Thearguments are a filename, a function object and an optional number of lines to process. Here is anexample:import imdbdef print_info(actor, date, title, role):print actor, date, title, roleimdb.process_file('actors.list.gz', print_info)When you call process_file, it opens filename, reads the contents, and calls print_info oncefor each line in the file. print_info takes an actor, date, movie title and role as arguments andprintsthem.1. Write a program that reads actors.list.gz and actresses.list.gz and uses shelve tobuild adatabase that maps fromeach actor toalistof his orher films.2. Twoactorsare“costars”iftheyhavebeeninatleastonemovietogether. Processthedatabaseyou built in the previous step and build a second database that maps from each actor to a listof his or her costars.3. Write a program that can play the “Six Degrees of Kevin Bacon,” which you can read aboutat wikipedia.org/wiki/Six_Degrees_of_Kevin_Bacon. This problem is challenging becauseit requires you to find the shortest path in a graph. You can read about shortest pathalgorithms atwikipedia.org/wiki/Shortest_path_problem.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!