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

Please anyone? I\'m really very stuck and could use instruction . . . Python ass

ID: 3833178 • Letter: P

Question

Please anyone? I'm really very stuck and could use instruction . . . Python assignement

Books.txt:

"

Ratings.txt

"

Data Files used in your Recommendation System There are two files that are used in this assignment: a list of books (books.txt) and a list of ratings from readers (ratings.txt. There are ratings associated with each book in the books file for each reader. The order of the ratings by an individual reader is the same as the order of the books in the books txt data file. The ratings given by a reader are shown in the table below. Be careful to note that if a reader has read the book, then the rating is a non-zero. books txt The data file containing the books contains a single line per book. Each entry has the author's name and title of the book separated by commas. Douglas Adams, The Hitchhiker's Guide To The Galaxy Richard Adams, Watership Down Mitch Albom, The Five People You Meet in Heaven Laurie Halse Anderson, Speak Maya Angelou, I Know Why the Caged Bird Sings Jay Asher, Thirteen Reasons Why Isaac Asimov, Foundation Series Ann Brashares, The Sisterhood of the Travelling Pants ratings txt The data file containing the reader's ratings contains one line per entry. Each entry has the reader's user id followed by a list of ratings separated by spaces. The user id cannot contain spaces. The ratings are listed in the order of the books supplied in the books data file. Ben 5 0 0 0 0 0 0 1 0 1 -3 5 0 0 0 5 5 0 0 0 0 5 0 0 0 0 0 0 0 0 1 -3 0 1 0 -5 Moose 5 5 0 5 0 0 3 0 0 1 0 5 3 0 5 0 3 3 5 0 0 3 0 0 5 0 0 4 0 0 3 5 0 0 5 0 Ted 5 5 0 0 0 0 -3 -5 0 5 5 0 1 0 1 3 1 5 0 0 0 0 0 0 3 0 0 0 0 -5 1 0 1 0 5 Franny 3 3 5 0 0 0 3 0 0 30 3 0 0 0 0 0 3 0 5 0 0 0 1 3 1 0 0 0 0 0 3 0 -3 0 0 Cust 2 3 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 -3 0 0 0 0 0 0 0 5 0 0 0 0 Francois 3 3 5 0 0 0 3 0 0 3 0 3 0 0 0 0 0 3 0 5 0 0 0 1 3 1 0 0 0 0 0 3 0 3 0 0 The n-th rating on a line in the ratings file corresponds to the n-th line in the books file. For example, Reuven gave a 5 to The Hitchhiker's Guide, a -5 for Watership Down, and a 0 (have not read the book) for The Five People You Meet in Heaven.

Explanation / Answer

def read_books(file_name):
   """Reads the file containing books data and returns a dictionary of that
   data."""
   my_books = {}

   with open(file_name, 'r') as books:
       i = 0
       for line in books.readlines():
           author, book_name = line.rstrip().split(',')
           my_books[i] = [book_name, author]
           i += 1

   if not bool(my_books):
       return None
   else:
       return my_books

def read_users(user_file):
   """Reads the file cotaining user data and returns a dictionary with
   usernames as keys and ratings array as values."""
   my_users = {}

   with open(user_file, 'r') as users:
       for line in users.readlines():
           line = line.rstrip().split(' ')
           for i in range(len(line)):
               if i == 0:
                   pass
               else:
                   line[i] = int(line[i])
           user, ratings = line[0], line[1:]
           my_users[user] = ratings

   if not bool(my_users):
       return None
   else:
       return my_users


def calculate_average_rating(ratings_dict):
   """Calculates the average rating of all books and returns a dictionary."""
   all_ratings = {}

   for user in ratings_dict:
       ratings = ratings_dict[user]
       for i in range(len(ratings)):
           try:
               all_ratings[i] += ratings[i]
               if ratings[i] != 0:
                   all_ratings[i] /= 2
           except KeyError:
               all_ratings[i] = ratings[i]

   if not bool(all_ratings):
       return None
   else:
       return all_ratings

def lookup_average_rating(index, book_dict, average_ratings_dict):
   """Pass in the index to get the name of the book and rating at given index. Index must be a numeral
   less than or equal to length of books."""
   print "(%.3f)" % (average_ratings_dict[index]),
   i = 0
   for key in book_dict:
       if i == index:
           print key
       else:
           pass
       i += 1