Skip to content

Instantly share code, notes, and snippets.

@phonemkyaw
Last active February 22, 2022 04:27
Show Gist options
  • Save phonemkyaw/ba6bbfcc300831861db9feea760e4293 to your computer and use it in GitHub Desktop.
Save phonemkyaw/ba6bbfcc300831861db9feea760e4293 to your computer and use it in GitHub Desktop.
Python function to calculate the star rating from a given list of ratings
def calculate_star_rating(ratings):
"""This function calculates the star rating of the given list with array positions as
0 - 1 Star
1 - 2 Stars
2 - 3 Stars
3 - 4 Stars
4 - 5 Stars
"""
stars = [1,2,3,4,5]
return round(sum(list(map(lambda a,b: a*b, stars, ratings))) / sum(ratings), 2)
ratings = [100,200,300,400,500]
calculate_star_rating(ratings)
#3.67
@khorammfar
Copy link

Thats not clear code.

@khorammfar
Copy link

Your function got ratings but not used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment