Question

In: Computer Science

In python! please be thorough and read question carefully :) if possible, give explanation for code...

In python! please be thorough and read question carefully :) if possible, give explanation for code

We’re going to create a class which stores information about product ratings on a website such as Amazon. The ratings are all numbers between 1 and 5 and represent a number of “stars” that the customer gives to the product. We will store this information as a list of integers which is maintained inside the class StarRatings. You will be asked to write the following:

1) A constructor which takes and stores the product name.

2) A function “giveRating” which takes an integer between 1 and 5 and stores it. If the number is outside that range, it should not store the number.

3) A function “calcAverage” which returns the average of the ratings. This can be done in linear time

4) A function getStarCount which returns a five element list containing the count of the number of ratings for each star. For example if the ratings were 5,5,2,3,1, and 1, this function would return [2,1,1,0,2] (2x 1 stars, 1x 2 stars, 1x 3 stars, 0x 4 stars, 2x 5 stars)

5) Sometimes a mistake is made and the site posts the same product twice. If that’s the case we will need to “add” the two star ratings together to produce a larger list with the result. write a function so that two StarRatings class objects can be “added” together using the + operator

6) If the product manufacturer pays us, we can adjust the star ratings by an amount. Allow for “multiplication” of every star rating by a constant amount. For example if the StarRatings Object is sr, allow for “sr1.5”. However, the star ratings must ALWAYS remain integers. In that example all 1 star reviews would become 2 stars (11.5 = 1.5, rounded to integer would be 2), 2 star reviews would be 3(2*1.5 = 3) etc.

Solutions

Expert Solution

Program Code Screenshot:

Sample output:

The screenshots are attached below for reference.

Please follow them for proper indentation and output.

Program to copy:

import math
class StarRatings:
def __init__(self,name):#constructor of class
self.pname=name
self.ratings=[]
def giveRating(self,r):
if r in [1,2,3,4,5]:#check rating value and append only if it is valid
self.ratings.append(r)
def calcAverage(self):
return sum(self.ratings)//len(self.ratings)#calculate and return average
  
def getStarCount(self):
res=[0,0,0,0,0]
for i in self.ratings:#for each rating in list increment it in res list for getting count
res[i-1]+=1
return res
  
def add(self,other):
self.ratings=self.ratings+other.ratings#add two ratings list when there are two same products
  
def adjust_ratings(self,n):
for i in range(len(self.ratings)):
if self.ratings[i]*n > 5:
self.ratings[i]=(5)
else:
self.ratings[i]=math.ceil(self.ratings[i]*n)#adjust the rating after multplication
  
s1=StarRatings("P1")
s1.giveRating(2)
s1.giveRating(2)#create object and giveRating
s1.giveRating(3)
s1.giveRating(4)
s1.giveRating(5)
s1.giveRating(5)
print(s1.getStarCount())#get the count
s1.adjust_ratings(1.5)
print(s1.getStarCount())


Related Solutions

Please, read the question carefully. I need correct answers and clear explanation. Thank you! On Sept...
Please, read the question carefully. I need correct answers and clear explanation. Thank you! On Sept 15, 2017, Julia Inc entered into a contract to provide 12 new deep fryers to a Montreal Poutinerie. Julia normally charges $1,500 for each deep fryer. As part of the agreement, Julia will provide on-site training at the Poutinerie’s 3 locations. Julia will host 3 separate training sessions at each location. For this training, Julia normally charges $700 / day. The Poutinerie needed Julia’s...
I paid to be able to post question here. Please read carefully and give quality answer....
I paid to be able to post question here. Please read carefully and give quality answer. there is nothing to clarify. just do what is required. Do not mess up my question as there are limited. Thanks. Choose at least two concepts from the following: equity method, fair value method, partnership, corporation, bankruptcy chapter 7, bankruptcy chapter 11, subsidiary, parent company, consolidation of financial statements, trust, estates, liquidation, partnerships termination, estates tax, acquisition, variable interest entity, EPS, income tax, reorganization....
Please give a brief yet thorough explanation of the physiology of the lungs broken down short...
Please give a brief yet thorough explanation of the physiology of the lungs broken down short enough and in bullets for a powerpoint.
Please Read Carefully Before start answering this question. Please follow the instructions. This Question is from...
Please Read Carefully Before start answering this question. Please follow the instructions. This Question is from 'BSBFIM501 Manage budgets and financial plans' course. There are no parts missing for this Question; guaranteed!. This is the original Screenshot direct from the question. Therefore, there are nothing any further information can be provided. Thanks for your understanding and Cooperation. Please answer the following questions from the topics discussed for Prepare, implement, monitor and modify contingency plans: 1.a. Explain the process of preparing...
----Modern Physics question: Please be thorough with your explanation. Using the Internet, find the location of...
----Modern Physics question: Please be thorough with your explanation. Using the Internet, find the location of a particle accelerator. In several paragraphs describe its size, what particles are being accelerated, how they are being accelerated, what their target is, how the particles are detected and what sub-atomic particles have been discovered at the site. Be sure to cite your sources.
Please read the question carefully and relate the situation to the type of antagonist with reasoning...
Please read the question carefully and relate the situation to the type of antagonist with reasoning During anaphylaxis, massive quantities of histamine are released, causing vasodilation that leads to bronchoconstriction. The emergency treatment for anaphylaxis is adrenaline, which causes vasoconstriction that leads to bronchodilation. What type of antagonist/antagonism does this represent? (Physiological, pharmacokinetic, chemical, irreversible, competitive). The thing that confuses me is how to come to the conclusion of what the drug is doing? I'm wondering does adrenaline react with...
PLEASE READ VERY CAREFULLY write a client.py and server.py file for tic-tac-toe IN PYTHON with the...
PLEASE READ VERY CAREFULLY write a client.py and server.py file for tic-tac-toe IN PYTHON with the following restrictions (SO WRITE TWO FILES THAT PLAY PYTHON THROUGH A SOCKET) Use a 5 x 5 grid (dimensions are subject to change, so use constants for NUM_ROWS and NUM_COLS) Use 'X' for player 1 and 'O' for player 2 (symbols and the number of players is subject to change, so use constants) Each player can make 1 move per turn before having to...
C++ Please read the question carefully and match the output example given at the end! Question...
C++ Please read the question carefully and match the output example given at the end! Question In this project you will implement operator overloading for a two dimensional Vector class. Begin with the declaration of the Vector class on the final page. There are three operators that must be overloaded insertion (​<<​), addition (​+​), and subtraction (​-​). insertion (​<<​): The insertion operator is used to send the values of a vector to an ostream object, if a vector has two...
Question1 if possible, could anyone do with explanation For each of the following code snippets, give...
Question1 if possible, could anyone do with explanation For each of the following code snippets, give both of the following: a. Give the overall T(n) run me analysis expression for the code. b. Describe the worst case running me of the code snippet in Big‑Oh notation. 1.for (int i=0;i<n;i++) { for (int j=0;j<n/2;j++) { sum++;} } 2.void silly(int n,int x,int y) { for (int i=0;i<n/2;i++) { if (x<y) { for (int j=0;j<100*n*n;j++) { cout<< "j="<<j<<endl;}} else {cout <<"i="<<i<<endl;}}} 3.void silly(int...
PYTHON PLEASE!! ALSO: if you can include an explanation of what the code means that would...
PYTHON PLEASE!! ALSO: if you can include an explanation of what the code means that would be appreciated 8.19 LAB*: Program: Soccer team roster (Dictionaries) This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team. (1) Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey numbers and the ratings in a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT