Question

PYTHON 3 Netflix stores a database that we can represent as a dict, whose keys are...

PYTHON 3


Netflix stores a database that we can represent as a dict, whose keys are movie titles like 'Pscyho' (str); associated with each title is a set of 2-tuples. Each 2-tuple specifies the name of a reviewer (str) followed by a review score (int: a number 0-5); for example ('Alice', 5). A simple/small database can look like

  {'Psycho':  {('Bob', 5),  ('Carrie', 5), ('Alan', 1), ('Diane', 1)},
   'Amadeus': {('Carrie', 3), ('Diane', 3), ('Bob', 3)},
   'Up':      {('Alan', 2), ('Diane', 5)},
   'Jaws':    {('Carrie', 2), ('Alan', 5)} }

Define the score_dict function to return a dict whose keys are the scores, where each reviewer is associated with a set of 2-tuples (movie names and their reviewer). If db is bound to the database above, calling score_dict (db) returns the dict

{1: {('Psycho', 'Diane'), ('Psycho', 'Alan')},
2: {('Jaws', 'Carrie'), ('Up', 'Alan')},
3: {('Amadeus', 'Diane'), ('Amadeus', 'Bob'), ('Amadeus', 'Carrie')},
5: {('Jaws', 'Alan'), ('Up', 'Diane'), ('Psycho', 'Carrie'), ('Psycho', 'Bob')} }

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

Solution:

from collections import defaultdict
db_data = {'Psycho':  {('Bob', 5),  ('Carrie', 5), ('Alan', 1), ('Diane', 1)},
           'Amadeus': {('Carrie', 3), ('Diane', 3), ('Bob', 3)},
           'Up':      {('Alan', 2), ('Diane', 5)},
           'Jaws':    {('Carrie', 2), ('Alan', 5)}}

def score_dict():
    # Creating score of type defaultdict to store set
    score = defaultdict(set)
    # Iterating over the db data and extracting key and values
    for key, values in db_data.items():
        movie_title = key # storing key into movie_title
        # Iterating over the values
        for value in values:
            # Storing 1st and 2nd tuple value into meaningful names
            reviewer_name = value[0]
            reviewer_score = value[1]
            # Adding tuple of movie title and reviewer name based on the scores
            score[reviewer_score].add((movie_title, reviewer_name))
    # Returning the score
    return score

# Calling and printing the method output
print(score_dict())

Sample Run:

defaultdict(<class 'set'>, {1: {('Psycho', 'Alan'), ('Psycho', 'Diane')}, 2: {('Up', 'Alan'), ('Jaws', 'Carrie')}, 3: {('Amadeus', 'Bob'), ('Amadeus', 'Carrie'), ('Amadeus', 'Diane')}, 5: {('Psycho', 'Bob'), ('Psycho', 'Carrie'), ('Jaws', 'Alan'), ('Up', 'Diane')}})

Sample Image of code and run:

Note: Just copy paste the code it should work fine, I have attached the image for the reference. If you have any doubt then please do let me know. Thanks

Add a comment
Know the answer?
Add Answer to:
PYTHON 3 Netflix stores a database that we can represent as a dict, whose keys are...
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
  • python 3 inheritance Define a class named bidict (bidirectional dict) derived from the dict class; in...

    python 3 inheritance Define a class named bidict (bidirectional dict) derived from the dict class; in addition to being a regular dictionary (using inheritance), it also defines an auxiliary/attribute dictionary that uses the bidict’s values as keys, associated to a set of the bidict’s keys (the keys associated with that value). Remember that multiple keys can associate to the same value, which is why we use a set: since keys are hashable (hashable = immutable) we can store them in...

  • (MMU) has decided to consolidate the functionality of three small overlapping database systems, which support applications...

    (MMU) has decided to consolidate the functionality of three small overlapping database systems, which support applications for 1) teaching (e.g. instructor assignment and evaluation), for 2) registration (e.g. online course status, waiting lists), and for 3) student records (e.g. transcript generation) The resulting new system will support the following enterprise description: Professors and GTAs are assigned to teach the sections of each class being offered in a semester. At the end of the semester, they get a "team rating" (professors...

  • python 3 question Project Description Electronic gradebooks are used by instructors to store grades on individual assignments and to calculate students’ overall grades. Perhaps your instructor uses gr...

    python 3 question Project Description Electronic gradebooks are used by instructors to store grades on individual assignments and to calculate students’ overall grades. Perhaps your instructor uses gradebook software to keep track of your grades. Some instructors like to calculate grades based on what is sometimes called a “total points” system. This is the simplest way to calculate grades. A student’s grade is the sum of the points earned on all assignments divided by the sum of the points available...

  • 1 L, as a dynamical system (Notes from Assignment #2) We take our definition of dynamical system ...

    1 L, as a dynamical system (Notes from Assignment #2) We take our definition of dynamical system to be an "object" along with a specific set of modifications that can be performed (dynamically) upon this object. In this case, the object is a bi-infinite straight road with a lamp post at every street corner and a marked lamp (the position of the lamplighter). There are two possible types of modifications: the lamplighter can walk any distance in either direction from...

  • Overview This lab provides you the opportunity to insert and update data with the use of SQL comm...

    Overview This lab provides you the opportunity to insert and update data with the use of SQL commands. The lab will utilize the FLIX2YOU problem, the current schema. In order to start this lab, you must have successfully completed Lab # 6. In Lab # 6, you executed a script that was provided to you. This script created 7 of the FLIX2YOU tables as documented in the Entity Relationship Diagram in the FLIX2YOU problem document. The second part of lab...

  • Write MySQL query statements for the questions below including the output that proves the accuracy of...

    Write MySQL query statements for the questions below including the output that proves the accuracy of your solution. Your answers should be stored in a text file that captures your interaction with MySQL. 1. Find the movieID, title, year and DVDPrice of all movies where the DVD-Price is equal to the discountPrice. 2. Find the actorID, lastName, firstName, middleName, and suffix of all actors whose middleName is not NULL. 3. Suppose you remember a movie quote as “Play it again,...

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