Python 3
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.
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. Let me know for any help with any other questions. Thank You! =========================================================================== import random class StarRatings(): def __init__(self, name): self.product_name = name self.ratings = [] def giveRating(self, rating): if 1 <= rating <= 5: self.ratings.append(rating) def calcAverage(self): return sum(self.ratings) / len(self.ratings) if len(self.ratings) != 0 else 0 def getStarCount(self): return [self.ratings.count(star) for star in range(1, 6)] def __add__(self, other): starRating = StarRatings(self.product_name) for rating in self.ratings: starRating.giveRating(rating) for rating in other.ratings: starRating.giveRating(rating) return starRating def __mul__(self, value): starRating = StarRatings(self.product_name) for i in range(len(self.ratings)): starRating.giveRating(int(self.ratings[i] * value)) return starRating r1 = StarRatings('Amazon') for _ in range(10):r1.giveRating(random.randint(1,5)) r2 = StarRatings('Walmart') for _ in range(10):r2.giveRating(random.randint(1,5)) combine = r1+r2 print(combine.ratings) print(combine.getStarCount())
===================================================================
Get Answers For Free
Most questions answered within 1 hours.