logo Use CA10RAM to get 10%* Discount.
Order Nowlogo
(5/5)

COMPUTER SCIENCE 10A (FALL TERM, 2021) INTRODUCTION TO PROBLEM SOLVING IN PYTHON PROGRAMMING ASSIGNMENT 8

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

I need help with my Python Hw assignment. I have to make a program that makes book recommendations and gives average ratings for each book. It’s really long and I don’t really know how to do it.

 

Implementation Details:

Your program will read from a file that contains sets of three lines. The first will

contain a username, the second a book title and the third the rating that user gave the

book. An example is displayed on the right.

Use the following command to open and read the lines in a file:

lines = open("ratings.txt").readlines()

The open() function to open a file. This function returns the file as object.

The readlines() function reads a file until the end. This function returns a list

containing the lines.

When your program starts it should read through the file and create a list with one

occurrence of each book in the file. For example, the file at right might produce the

following list:

['1984', 'Cats', 'Harry Potter', 'Animal Farm', 'Watership

Down', 'The Hobbit']

Note that the order of the books does not matter. We suggest that you create this list by

putting all books in the file into a set and then casting that set to a list. If you have a

variable called data that stores a set, you can turn it into a list by writing data

= list(data).

Once you have this list, create a dictionary to store the rating data and loop through the

file again. This time add each person in the file as a key to the dictionary. The value

that is associated with them should be a list the same length as the list of books you

created in your first pass through the file. You should store the rating at the same index

that book’s name appears at in in the list of books. For example, when the first three

lines of the file on the right are read, we would add a mapping from the key Bob to a

value of [1, 0, 0, 0, 0, 0]. Books that the user has not rated (or whose

ratings we have not read yet, as in this case) should be represented by 0s.

The dictionary created with the file at right and the list of books written above would

be as follows:

{'Kalid' : [0, 0, 1, -3, 3, 0], 'Carlos': [-5, 0, 3, 1, 0, 0],

'Suelyn': [1, 0, 1, -3, 0, 0], 'Bob' : [0, 1, -3, 0, 0, 1]}

Now your program is ready to make recommendations. It should output the following message:

Welcome to the Book Recommender. Type the word in the

left column to do the action on the right.

recommend : recommend books for a particular user

averages : output the average ratings of all books in the system

quit : exit the program

next task?

The prompt should be repeated after every task is finished.

Bob

Cats

1

Suelyn

Harry Potter

1

Carlos

Animal Farm

1

Kalid

Watership Down

3

Carlos

1984

-5

Bob

Harry Potter

-3

Suelyn

1984

1

Carlos

Harry Potter

3

Suelyn

Animal Farm

-3

Bob

The Hobbit

1

Kalid

Animal Farm

-3

Kalid

Harry Potter

1

Averages

If the user selects the averages option, the program should output

all of the books in the file sorted by average rating from highest to

lowest. For example, for the file on the previous page you should

output the listing on the right.

We suggest that you figure this out by building up a list of tuples

containing the average rating for a book first and the title of that

book second. You can build up this list by going through the list of

books one at a time and for each person in the dictionary adding up their rating of that book and

counting how many people in the dictionary rated it something other than 0. The average score

for that book is the sum scores divided by the count of non-zero ratings. Once you have created

this list you can sort it by using the list’s sort function.

Since the averages will stay the same throughout the run of the program you may want to

calculate them once at the start of the program.

Recommendations

When the user selects the recommend option the program should first prompt the user for the

name of the user that the program wants recommendations for as follows:

user?

If the name that the user types in isn’t in the dictionary of ratings, the program should output the

same list of books as when the user selects the averages option.

If the name the user inputs is in the dictionary of ratings you should use the data in the dictionary

to find the other users that are most similar to the user you are looking for recommendations for.

The first step to do this is to calculate the similarities between your user and the other users. We

will use the dot product between the users’ lists of ratings to calculate their similarity. This means

that we will multiply each element in your user’s list with the element at the same index in the

other user’s list and sum the result. For example, if we were looking for a recommendation for

Kalid we would do the following to calculate his similarity to Carlos:

(0 * -5) + (0 * 0) + (1 * 3) + (-3 * 1) + (3 * 0) + (0 * 0) = 0 +

0 + 3 + -3 + 0 + 0 = 0

Compute this similarity for each user in the dictionary. Store tuples containing first the similarity

number and second the name of the other user in a list. You can use the list sort function to sort

this list.

Note that the user that you are looking for will always be in the dictionary. We are not interested

in how similar the user is to themselves. You may find it helpful not to add the user to the list or

to remove them after sorting. Note that the user will always be most similar to themselves.

Now that we have a list of the most similar users, we can use this to figure out which books to

recommend. To generate recommendations, take an average of the ratings of the three users with

the highest similarity to the user you are looking for.

To average the ratings, create a new list the same length as the list of books and filled with 0s.

Loop through this list. For every index of this list loop through the first three users, add up their

ratings and then divide by the number of non-zero ratings. If there are no non-zero ratings for a

book you should not divide as you will get a divide by 0 error.

Watership Down 3.0

The Hobbit 1.0

Cats 1.0

Harry Potter 0.5

Animal Farm -

1.6666666666666667

1984 -2.0

Once you have calculated these averages, create a list of tuples that contain first the average

rating and then the book title for all books that have non-zero ratings in the averages list. Then,

sort this list. Now you have a list of books to recommend.

Note that it must be possible for the user to choose options multiple times in any order and get the

correct results each time.

Your program should match the expected output files provided on the course web page exactly.

Development Strategy and Hints:

We suggest that you complete the assignment in the order described above.

You will find the list sort function very helpful for this assignment. The sort function sorts

elements in a list from smallest to largest. If the list contains tuples it sorts by the first element in

the tuple. Therefore, it is very important to order your tuple contents as described in the

instructions above. If you have a variable called my_list, the following code will sort it:

my_list.sort(). Note that sort alters the list, it doesn’t return a new list.

Use print statements to view the state of your structures. Create small input files for yourself to

help you debug. 

 

(5/5)
Attachments:

Related Questions

. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Um e HaniScience

530 Answers

Hire Me
expert
Muhammad Ali HaiderFinance

682 Answers

Hire Me
expert
Husnain SaeedComputer science

861 Answers

Hire Me
expert
Atharva PatilComputer science

616 Answers

Hire Me