Check your understanding of the previous reading: primitive and composite types, lists and dataclasses nested inside each other, and working through heavily nested data.
Given the following code:
from dataclasses import dataclass
@dataclass
class Movie:
name: str
length: int
cast: list[str]
movies = [
Movie("Jurassic Park", 127, ["Goldblum", "Neill", "Dern"]),
Movie("Castle in the Sky", 140, ["Paquin", "Beek", "Leachman"]),
Movie("It", 135, ["Curry", "Green", "Brandis"])
]
What is the value of the following expressions?
The next few questions will all use the same code:
from dataclasses import dataclass
@dataclass
class Profit:
# In millions
costs: int
revenue: int
@dataclass
class Movie:
name: str
length: int
profit: Profit
cast: list[str]
@dataclass
class Theater:
name: str
is_open: bool
showing: list[Movie]
theaters = [
Theater("Main Street Theater", True, [
Movie("Jurassic Park", 127, Profit(63, 1046),
["Goldblum", "Neill", "Dern"]),
Movie("Castle in the Sky", 140, Profit(3, 16),
["Paquin", "Beek", "Leachman"]),
Movie("The Notebook", 121, Profit(29, 118),
["McAdams", "Gosling", "Rowlands"])
]),
Theater("BlockBuster Cinema", False, [
Movie("Cats", 110, Profit(100, 75),
["Dench", "Corden", "Derulo"]),
Movie("Morbius", 104, Profit(86, 45),
["Leto", "Smith", "Arjona"])
]),
Theater("All Star Movies", True, [
Movie("Gone with the Wind", 221, Profit(4, 390),
["Leigh", "Gable", "Havilland"])
])
]
A diagram is also provided to help you understand the data and its relationships.

Given the code above and the code below:
alpha = 0
for theater in theaters:
for movie in theater.showing:
alpha += movie.profit.costs
Inside of the loop body, what are the most accurate types of the following expressions:
Given the following function:
def alpha(theaters: list[Theater]) -> str:
for theater in theaters:
for movie in theater.showing:
potential = beta(movie)
if potential < 0:
return movie.name
return None
def beta(movie: Movie) -> int:
profit = movie.profit
return profit.revenue - profit.costs
What will the expression print(alpha(theaters)) output?
beta returns a movie's revenue minus its costs. alpha goes through the movies of every theater, open or not, and returns the name of the first movie where that is negative. The Main Street Theater movies all make a profit (983, 13 and 89); the first movie of BlockBuster Cinema, Cats, gives 75 - 100 = -25. So alpha returns "Cats" and print shows Cats.
Given the following function:
def unknown(theaters: list[Theater]) -> str:
theaters = step1(theaters)
if not theaters:
return "No results"
result = step2(theaters)
return result.name
def step1(theaters: list[Theater]) -> list[Theater]:
result = []
for theater in theaters:
if theater.is_open:
result.append(theater)
return result
def step2(theaters: list[Theater]) -> Theater:
result = theaters[0]
result_length = theaters[0].showing[0].length
for theater in theaters:
for movie in theater.showing:
if result_length > movie.length:
result = theater
result_length = movie.length
return result
What will the expression print(unknown(theaters)) output?
step1 keeps the open theaters: Main Street Theater and All Star Movies. That list is not empty, so step2 looks for the theater showing the shortest movie. It starts with Main Street Theater and the length of its first movie, 127. The Notebook (121) is shorter, so the result stays Main Street Theater with 121; Castle in the Sky (140) and Gone with the Wind (221) are longer. unknown returns that theater's name, so print shows Main Street Theater.