Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Current functions for the question: def reviewer_rank(db : {str:{(str,int)}}) ->

ID: 3591422 • Letter: C

Question

Current functions for the question:

def reviewer_rank(db : {str:{(str,int)}}) -> [(str,int)]:
pass


def reviewer_nested_dict(db : {str:{(str,int)}}) -> {str:{str:int}}:
pass

5. (6 pts) 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 his/her score (int: a number 0-5); for example ('Bob', 5). A simple/small database can look like Psycho': ('Bob', 5), ('Carry', 5), ('Diane', 1)), 'Amadeus': f('Bob', 3), ('Carry', 3), ('Diane', 3), [ ('Alan',2), 'Bob', 5), ('Diane', 5)) ) a. (3pts) Define the reviewer rank function to return a list of 2-tuples (reviewer name, number of reviews) sorted from highest to lowest number of reviews, with equal numbers listed alphabetically by reviewer. For example, if db is bound to the database above, calling reviewer_rank (db) returns the list [ ( 'Bob' , 3) , ( 'Diane' , 3) , ( 'Carry' , 2) , ( 'Alan' , 1) ] . Hint: I used a defaultdict b. (3 pts) Define the reviewer_nested dict function to return a dict whose keys are the reviewers, where each reviewer is associated with an inner dict whose keys are movies reviewed by that reviewer, with each movie associated with the reviewer's score for it. For example, if db is bound to the database above, calling reviewer nested dict (db) returns the dict ['Alan': ('Up': 2), 'Bob': ['Amadeus': 3, 'Psycho': 5, 'Up': 5], 'Carry': ('Amadeus': 3, 'Psycho': 5), 'Diane': ('Amadeus': 3, 'Psycho': 1, 'Up': 5] ) Of course, the inner and outer dicts can print their key/value associations in any order. Hint: I used a defaultdict to accumulate this information and then created/returned an equivalent dict

Explanation / Answer

#This Program is written for Python 2.7

import operator

def reviewer_rank(db):

reviewer_count_map=dict()

for movie,reviews in db.iteritems():#Iterate over db items. This is different for Python 3

for reviewer,review in reviews:

if reviewer in reviewer_count_map:

reviewer_count_map[reviewer] += 1 #For each reviewer increase the count by one if the reviewer already exists in our reviewer map

else:

reviewer_count_map[reviewer] = 1 #For each reviewer add a reviewer if he does not already exists in our reviewer map

sorted_reviewer_list = sorted(reviewer_count_map.items(), key=operator.itemgetter(0)) #Sort the reviewers count map in ascending order according to their names

sorted_reviewer_list.sort(key=operator.itemgetter(1),reverse=True) #Sort the reviewer map list (of tuples) in descending order of their number of reviews

return sorted_reviewer_list #Return the resulting list

def reviewer_nested_dict(db):

reviewer_movie_map=dict()

for movies in db:

reviewers = db[movies]

for reviewer, rating in reviewers:

if reviewer in reviewer_movie_map:

reviewer_movie_map[reviewer][movies] = rating

else:

reviewer_movie_map[reviewer] = {movies:rating}

return reviewer_movie_map

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote