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
{- Alexa loves movies and maintains a list of negative and/or positive integer ratings for the...
{- Alexa loves movies and maintains a list of negative and/or positive integer ratings for the n movies in her collection. She's getting ready for a film festival and wants to choose some subsequence of movies from her collection to bring such that the following conditions are satisfied: The collective sum of their ratings is maximal. She must go through her list in order and cannot skip more than one movie in a row. In other words, she cannot skip...
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,...
In c++ create a class to maintain a GradeBook. The class should allow information on up...
In c++ create a class to maintain a GradeBook. The class should allow information on up to 3 students to be stored. The information for each student should be encapsulated in a Student class and should include the student's last name and up to 5 grades for the student. Note that less than 5 grades may sometimes be stored. Your GradeBook class should at least support operations to add a student record to the end of the book (i.e., the...
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...
Challenge: Animal Class Description: Create a class in Python 3 named Animal that has specified attributes...
Challenge: Animal Class Description: Create a class in Python 3 named Animal that has specified attributes and methods. Purpose: The purpose of this challenge is to provide experience creating a class and working with OO concepts in Python 3. Requirements: Write a class in Python 3 named Animal that has the following attributes and methods and is saved in the file Animal.py. Attributes __animal_type is a hidden attribute used to indicate the animal’s type. For example: gecko, walrus, tiger, etc....
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...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class Zillion implements a decimal counter that allows numbers with an effectively infinite number of digits. Of course the number of digits isn’t really infinite, since it is bounded by the amount of memory in your computer, but it can be very large. 1. Examples. Here are some examples of how your class Zillion must work. I’ll first create an instance of Zillion. The string...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT