Question

Python 3 We’re going to create a class which stores information about product ratings on a...

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.

Homework Answers

Answer #1
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())

===================================================================

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
A python question... In your class, many students are friends. Let’s assume that two students sharing...
A python question... In your class, many students are friends. Let’s assume that two students sharing a friend must be friends themselves; in other words, if students 0 and 1 are friends and students 1 and 2 are friends, then students 0 and 2 must be friends. Using this rule, we can partition the students into circles of friends. To do this, implement a function networks() that takes two input arguments. The first is the number n of students in...
Python Problem 1 Create a function, biggest_ip_sum, outside of the ServerClass class, that: 1. Takes two...
Python Problem 1 Create a function, biggest_ip_sum, outside of the ServerClass class, that: 1. Takes two ServerClass objects 2. Sums the octets of the IP together (example 127.0.0.1 = 127+0+0+1 = 128) 3. Returns the larger number Example: server_one = ServerClass("127.0.0.1") server_two = ServerClass("192.168.0.1") result = biggest_ip_sum(server_one, server_two) print(result) In the example above, result will be 361. _________________________________ Problem 1 Here is the code to start with class ServerClass: """ Server class for representing and manipulating servers. """ def __init__(self,...
Write a Python function count_bigger that takes two parameters, a nested list of objects and a...
Write a Python function count_bigger that takes two parameters, a nested list of objects and a threshold number. It returns an integer specifying how many of the objects anywhere in the nested list are numbers that are larger than the threshold. (For our purposes, "numbers" are either integers or floats.) There may be objects in the list other than numbers, in which case you would simply ignore them. Here are a couple of examples of how the function should behave...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). Details and Requirements Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 – “three and one-half”). The Mixed class must allow for both positive and negative mixed number values. A...
python If a number num1 divides another number num2 evenly then num1 is a divisor of...
python If a number num1 divides another number num2 evenly then num1 is a divisor of num2. For example, 2 is a divisor of 2, 4, 6, 8, but 2 is not a divisor of 1, 3, 5, 7, 9, 11, 13. Write a function named count_divisors(m,n) that works as follows. Input: the function takes two integer arguments m and n Process: the function asks the user to enter numbers (positive or negative), and counts the total number of inputs...
We are using a raspberry pi in my computer systems class. We need to Write a...
We are using a raspberry pi in my computer systems class. We need to Write a statically allocated linked list in ARM assembly. Steps You Should Follow Step 1 You will statically allocate some space for your link list elements. You need to make 5 elements, each elements should have two components, the next pointer first then the value stored. First inialize next pointer to NULL. The value stored at the address will be a string pointer. It doesn't matter...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
1-Create a week_tuple and assign the days of the week as strings to the week_tuple. b....
1-Create a week_tuple and assign the days of the week as strings to the week_tuple. b. Print out the elements in the week_tuple. 2-a. The following list has been declared as follows: credit_list = [24,3,15] b. Convert the credit_list to a tuple named credit_tuple. c. Print out the elements in the credit_tuple. 3-a. Write an initialize_list_values function that takes in a number for the number of elements in a list and an initial value for each element. The function creates...
Problem 2: Python 3 Implement a function called gee_whiz that does the following: given argument n,...
Problem 2: Python 3 Implement a function called gee_whiz that does the following: given argument n, a positive integer, it returns a list of n tuples corresponding to the numbers 1 through n (both inclusive): the tuple for the number k consists of k as the first component, and exactly one of the following strings as the second: • the string 'two!' if k is divisible by 2 • the string 'three!' if k is divisible by 3 • the...
using dr.racket programing language If we write a function that tests whether a list contains only...
using dr.racket programing language If we write a function that tests whether a list contains only strings, odd numbers, or even numbers, you will notice that the code that iterates through the list stays the same, with the only change being the predicate function that checks for the desired list element. If we were to write a new function for each of the tests listed above, it would be more error-prone and an example of bad abstraction. We could write...