Questions for F18 Final Review Session

1. L33T speak – lists

L33T speak is a different form of writing, usually online. It consists of replacing certain letters with symbols or numbers. Consider the table below of letters, and their corresponding L33T translations.

Write a Python function called leet which consumes a list of strings, los, and mutates the list where each string becomes its leet version. The function returns None.

Example:

L = ["password", "secrets", "CS116 "]

leet(L)

L = ['p@$$w0r)', '$3(r3+$', '($116']

2. common – dictionary

Complete the Python function common that consumes two dictionaries (d1 and d2, with common key and value types). Return a list of all the keys (in increasing order) that occur in both d1 and d2 and have the same associated values in each.

Examples:

common({1:'1', 2:'2', 4:'4'}, {3:'3', 2:'2', 1:'1', 4:’4’}) => [1,2] common({1:'1', 2:'2', 4:'4'}, {3:'3', 5:'2', 6:'1’}) => []

3. add_star – class

Complete the class function add_star that consumes a Movie object and a string, star, and add the star's name to the end of the movie’s stars list. In addition, the function returns the number of actors listed under stars.

Examples:

princess_bride = Movie("Princess Bride", 1987,["Cary Elwes", "Robin Wright", "Mandy Patinkin"]) princess_bride.add_star("Andre the Giant") => 4

princess_bride = Movie("Princess Bride", 1987,["Cary Elwes", "Robin Wright", "Mandy Patinkin", "Andre the Giant"])

4. movies_by_year – class/dictionary

Write a function movies_by_year that consumes a list of Movie objects and returns a dictionary where the keys are the years the movies came out in and the associated values is a list of the movie titles ( if two movies came out in the same year then both their titles would be in the associated list for that key)

Example:

m_list = [Movie("Princess Bride", 1987,["Cary Elwes", "Robin Wright","Mandy Patinkin"]), Movie("Alien Nation", 1988, ["James Caan", "Mandy Patinkin"]), Movie("RoboCop", 1987, ["Nancy Allen", ""])]

movies_by_year(m_list) => {1987: ['Princess Bride', 'RoboCop'], 1988: ['Alien Nation']}

5. num_words – files

Write a Python function num_words that consumes the name of a filename and returns the the number of words in the file

Note: words are considered characters separated by a space and there are only letters in the file

Examples:

If "test1.txt" contains "I love Computer Science\n”, then num_words("test1.txt") => 4

If "test2.txt" is an empty file, then num_words("test2.txt") => 0

6. write_reverse – file

Write a function write_reverse that reads in the contents from the input file, infile, and writes all of the lines in reverse to an outfile where outfile is a filename entered by the user and returns None

Examples:

If "test1.txt" contains "I love Computer Science\n", then write_reverse("test1.txt") would write the line "ecneics retupmoc evol I\n" to the file "output1.txt" where "output1" is the filename entered by the user.

If "test2.txt" is an empty file then write_reverse("test2.txt") would write nothing to "output2.txt" where "output2" is the filename entered by the user