Question

You will need to define 2 Python functions. These functions could be used by a website...

You will need to define 2 Python functions. These functions could be used by a website tracking movie data. Each movie is represented as a dictionary containing three keys:/p>

the key "film" whose value is a string representing the name of the film;

the key "tomat" whose value is am int of its rating from a well-known website;

the key "gross" whose value is a float equalling its worldwide gross income (in million of dollars).

First, define a key selector function named gross. This function returns a result allowing the built-in sorting function to order dictionaries from the one with the lowest gross income to the dictionary with the greatest gross income.

Next, define another function named sortByGross. This function has a single parameter. This parameter will be a list of movie dictionaries. Your function will result in the list's dictionaries being ordered from the dictionary with the smallest gross to the dictionary with the greatest gross.

Examples:

gross({ "film":"WALL-E", "tomat" : 96, "gross" : 521.28}) would evaluate to 521.28

gross({ "film":"1 Day", "tomat" : 37, "gross" : 55.24}) would evaluate to 55.24

movies = [{"film":"WALL-E", "tomat" : 96, "gross" : 521.28},

{ "film":"Letters to Juliet", "tomat" : 40, "gross" : 79.18},

{ "film":"Twilight", "tomat" : 49, "gross" : 376.66},

{ "film":"The Time Traveler's Wife", "tomat" : 38, "gross" : 101.33}]

sortByGross(movies)

print(movies)

would result in the following output:

[ { 'film':'Letters to Juliet', 'tomat' : 40, 'gross' : 79.18},

{ 'film':'The Time Traveler's Wife', 'tomat' : 38, 'gross' : 101.33},

{ 'film':'Twilight', 'tomat' : 49, 'gross' : 376.66},

{ 'film':'WALL-E', 'tomat' : 96, 'gross' : 521.28}]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

# function that returns the gross of the dictionary about film data
def gross(data):
    return data['gross']


# function to sort data by gross
def sortByGross(movies):
    # get all the gross data
    grossList = []
    for g in movies:
        grossList.append(gross(g))

    # now sort the gross as follows
    grossList.sort()
    # now add the movies data
    sortedMovies = []

    # add each movie
    for g in grossList:
        for data in movies:
            if gross(data) == g:
                sortedMovies.append(data)
                break
    return sortedMovies


# TEST
if __name__ == '__main__':
    # TEST GROSS
    print(gross({"film": "WALL-E", "tomat": 96, "gross": 521.28}))
    print(gross({"film": "1 Day", "tomat": 37, "gross": 55.24}))

    # TEST SORT
    movies = [{"film": "WALL-E", "tomat": 96, "gross": 521.28},

              {"film": "Letters to Juliet", "tomat": 40, "gross": 79.18},

              {"film": "Twilight", "tomat": 49, "gross": 376.66},

              {"film": "The Time Traveler's Wife", "tomat": 38, "gross": 101.33}]

    movies = sortByGross(movies)

    print(movies)

=============

# function that returns the gross of the dictionary about film data def gross(data): e return data[gross] 6 8 g # function26 27 28 # TEST bif name == main : # TEST GROSS print(gross({film: WALL-E, tomat: 96, gross: 521.28})) print(gross({

If there are any doubts, comment down here. We are right here to help you. Happy Learning..!! PLEASE give an UPVOTE I Have Pu

Add a comment
Know the answer?
Add Answer to:
You will need to define 2 Python functions. These functions could be used by a website...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT