BYU logo Computer Science

Lab 17 — References

In this lab you will practice using references. Start by downloading lab17.zip. Write all your code in references.py.

Find Actors

Write a function called find_actors(actor_list, letter). This function takes two parameters:

  • actor_list: a list of actors, given as a tuple (last_name, first_name)
  • letter: a capital letter

The function returns a new list that has only those actors whose last names start with the given letter. For example:

>>> find_actors([('Freeman', 'Morgan'), ('Streep', 'Meryl'), ('Hanks', 'Tom'), ('Bullock', 'Sandra')], 'B')
[('Bullock', 'Sandra')]

The best way to write this function is to use the accumulator pattern.

Find Actors 2, The Sequel

Write a function called find_actors2(actor_list, name, letter). This function takes three parameters:

  • actor_list: a list of actors, given as a tuple (last_name, first_name)
  • name: either ‘first’, or ‘last’
  • letter: a capital letter

The function returns a new list that has only those actors whose first or last names start with the given letter, depending on the value of name. For example:

>>> find_actors2([('Freeman', 'Morgan'), ('Streep', 'Meryl'), ('Hanks', 'Tom'), ('Bullock', 'Sandra')], 'last', 'S')
[('Streep', 'Meryl')]
>>> find_actors2([('Freeman', 'Morgan'), ('Streep', 'Meryl'), ('Hanks', 'Tom'), ('Bullock', 'Sandra')], 'first', 'S')
[('Bullock', 'Sandra')]

The best way to write this function is to use the accumulator function.

Movies

Write a function called find_movies(movie_data, actor). This function takes two parameters:

  • movie_data: a dictionary that maps movie titles to a list of the actors that starred in the movie; actors are listed as tuples of (last_name, first_name)

  • actor: the name of an actor, as a tuple of (last_name, first_name)

The function returns a dictionary that has the same structure as movie_data, but contains only those movies with the given actor listed. For example:

>>> movie_data = {
    'Suffragette':[('Mulligan', 'Carey'), ('Duff', 'Anne-Marie'),
                   ('Bonham   Carter', 'Helena'), ('Streep', 'Meryl')],
    'Into the Woods': [('Kendrick', 'Anna'), ('Streep', 'Meryl'),
                       ('Pine', 'Chris'), ('Blunt', 'Emily'), ('Cordon', 'James'), ('Depp', 'Johnny')],
    'Wonder Woman': [('Godot', 'Gal'), ('Pine', 'Chris'), ('Wright', 'Robin'),
                     ('Davis', 'Lucy')]
}
>>> find_movies(movie_data, ('Streep', 'Meryl'))
{
    'Suffragette':[('Mulligan', 'Carey'), ('Duff', 'Anne-Marie'),
                   ('Bonham Carter', 'Helena'), ('Streep', 'Meryl')],
    'Into the Woods': [('Kendrick', 'Anna'), ('Streep', 'Meryl'),
                       ('Pine', 'Chris'), ('Blunt', 'Emily'), ('Cordon', 'James'), ('Depp', 'Johnny')]
}

The best way to write this function is to use the accumulator pattern.

Leading Actor

Write a function called leading_actor(movie_data). This function takes a movie_data dictionary, like above, and returns a new dictionary that contains only the leading actor for each movie. The leading actor is the first one listed in the original movie_data.

The best way to write this function is to create a deep copy of the movie data and then modify the copy.

Currently Streaming

Write a function called currently_streaming(movie_list). This function takes one parameter:

  • movie_list: a list of movies

The movie are a dictionary, with the following structure:

{ title: string, genre: string, streaming: boolean }

The function returns a new list of movies, containing only those movies that are currently streaming.

The best way to write this function is to use the accumulator pattern.

All Streaming

Let’s imagine a pandemic hits and so all movies are temporarily set to streaming. Write a function called all_streaming(movie_list). This function takes a movie_list, as above, and returns a new list where all the movies have the streaming key set to true.

The best way to write this function is to create a deep copy of the list and then modify the copy. We would like to save the original so we can easily go back to it when needed.

Convert Prices

We have given you a function called convert_prices(price_data, exchange_rate) along with doctests for this function. Notice that it doesn’t pass the second doctest!

Can you figure out why?

Rewrite the code for this function, without changing the doctests. Get it to pass both tests.

Lessons

What we want you to get from this lab:

  • You understand how references work in Python

  • You can figure out what went wrong when something unexpected happens

  • Hopefully you had fun!

Points

Turn in a zip file that has your code.

TaskDescriptionPoints
Find ActorsYour solution works1
Find Actors 2Your solution works1
MoviesYour solution works1
Leading ActorYour solution works1
Currently StreamingYour solution works2
All streamingYour solution works2
Convert PricesYour solution works2