# Program to count number of 5-star reviews required to reach threshold percentage in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have a list called reviews and a threshold value t. Each item in reviews[i] has [x, y] means product i had x number of 5-star rating and y number of reviews. We have to find the minimum number of additional 5-star reviews we need so that the percentage of 5-star reviews for those items list is at least t percent.

So, if the input is like reviews = [[3, 4],[1, 2],[4, 6]] threshold = 78, then the output will be 7, as in total there were 8 5-star reviews and 12 reviews. To reach 78% 5-star reviews, we need 7 more 5-star reviews.

To solve this, we will follow these steps −

• a := 0, b := 0

• for each 5-star count c and review count d in reviews, do

• a := a + c

• b := b + d

• if a * 100 >= t * b, then

• return 0

• delta := t * b - 100 * a

• return floor of (delta +(99 - t))/(100 - t)

## Example

Let us see the following implementation to get better understanding

def solve(reviews, t):
a = 0
b = 0
for c, d in reviews:
a += c
b += d
if a * 100 >= t * b:
return 0
delta = t * b - 100 * a
return (delta + (99 - t)) // (100 - t)

reviews = [
[3, 4],
[1, 2],
[4, 6]
]
t = 78
print(solve(reviews, t))

## Input

[[3, 4], [1, 2],[4, 6] ],78


## Output

7