58.4k views
3 votes
#Write a function called find_max_sales. find_max_sales will #have one parameter: a list of tuples. Each tuple in the #list will have two items: a string and an integer. The #string will represent the name of a movie, and the integer #will represent that movie's total ticket sales (in millions #of dollars). # #The function should return the movie from the list that #had the most sales. Return only the movie name, not the #full tuple. #Write your function here!

1 Answer

3 votes

Answer:

I am writing a Python program:

This is the find_max_sales

def find_max_sales(tuple_list): #method that takes a list of tuples as parameter and returns the movie name which had the most sales

maximum=0 #stores the maximum of the ticket sales

name="" #stores the name of the movie with maximum sales

for tuple in tuple_list: # iterates through each tuple of the tuple list

[movie, sale]= tuple #tuple has two items i.e. movie contains name of the movies and sale holds the total ticket sale of the movie

if sale>maximum: #if the sale value of a tuple is greater than that of maximum sale value

maximum=sale #assigns the maximum of sales to maximum variable

name=movie # assigns the movie name which had most sales to name variable

return name #returns the movie name that had the most sales and not the full tuple

#Below is the list of list of movies. This list is passed to the find_max_sales method which returns the movies name from the list that had the most sales.

movie_list = [("Finding Dory", 486), ("Captain America: Civil War", 408), ("Deadpool", 363), ("Zootopia", 341), ("Rogue One", 529), ("The Secret Life of Pets", 368), ("Batman v Superman", 330), ("Sing", 268), ("Squad", 325), ("The Jungle Book", 364)]

print(find_max_sales(movie_list))

Step-by-step explanation:

The program has a method find_max_sales that has one parameter tuple_list which is a list of tuples. Each tuple i.e. tuple has two items a movie which is a string and sale which is an integer. The movie represents the name of a movie, and the sales represents that movie's total ticket sales The function has a loop that iterates through each tuple tuple of the list tuple_list. Variable maximum holds the value of the maximum sales. The if condition checks if the value of tuple sale is greater than that stored in maximum, at each iteration. If the condition evaluates to true then that sale tuple value is assigned to maximum variable so that the maximum holds the maximum of the ticket sales. The name variable holds the name of the movie corresponding to the maximum sale tuple value. The method returns the movie name from the tuple_list that has the most sales.

For example in the above given movie_list , the maximum of sale is of the movie Rogue One. When the method is called it returns the movie name and not the full tuple so the output of the above program is:

Rogue One

#Write a function called find_max_sales. find_max_sales will #have one parameter: a-example-1
User Victor Hugo
by
5.7k points