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

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.

Algorithm Steps

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  # Total 5-star reviews
    b = 0  # Total reviews
    
    # Calculate current totals
    for c, d in reviews:
        a += c
        b += d
    
    # If already meets threshold, no additional reviews needed
    if a * 100 >= t * b:
        return 0
    
    # Calculate additional 5-star reviews needed
    delta = t * b - 100 * a
    return (delta + (99 - t)) // (100 - t)

# Test with given example
reviews = [
    [3, 4],
    [1, 2], 
    [4, 6]
]
t = 78
result = solve(reviews, t)
print(f"Additional 5-star reviews needed: {result}")

# Verify the calculation
total_5_star = 3 + 1 + 4
total_reviews = 4 + 2 + 6
current_percentage = (total_5_star / total_reviews) * 100

print(f"Current: {total_5_star}/{total_reviews} = {current_percentage:.1f}%")
print(f"Target: {t}%")
Additional 5-star reviews needed: 7
Current: 8/12 = 66.7%
Target: 78%

How the Formula Works

The formula derives from solving the equation: (a + x) / (b + x) >= t/100, where x is the number of additional 5-star reviews needed. After algebraic manipulation, we get ?

# Let's break down the mathematical reasoning
def explain_formula(reviews, t):
    a = sum(c for c, d in reviews)  # Current 5-star reviews
    b = sum(d for c, d in reviews)  # Current total reviews
    
    print(f"Current 5-star: {a}, Total reviews: {b}")
    print(f"Current percentage: {(a/b)*100:.1f}%")
    print(f"Target percentage: {t}%")
    
    # We need: (a + x) / (b + x) >= t/100
    # Solving: a + x >= (t/100) * (b + x)
    # a + x >= (t*b + t*x)/100
    # 100*a + 100*x >= t*b + t*x
    # 100*x - t*x >= t*b - 100*a
    # x*(100 - t) >= t*b - 100*a
    # x >= (t*b - 100*a) / (100 - t)
    
    delta = t * b - 100 * a
    min_needed = delta / (100 - t)
    
    print(f"Minimum needed (exact): {min_needed:.2f}")
    print(f"Rounded up: {int(min_needed) + (1 if min_needed % 1 > 0 else 0)}")

explain_formula([[3, 4], [1, 2], [4, 6]], 78)
Current 5-star: 8, Total reviews: 12
Current percentage: 66.7%
Target percentage: 78%
Minimum needed (exact): 7.27
Rounded up: 8

Conclusion

This algorithm efficiently calculates the minimum additional 5-star reviews needed to reach a target percentage. The mathematical formula handles edge cases and provides the exact minimum count required.

Updated on: 2026-03-26T15:07:44+05:30

893 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements