Skip to content

Instantly share code, notes, and snippets.

@rodricios
Last active September 19, 2015 04:29
Show Gist options
  • Save rodricios/8532d178bfd3ac18ea8b to your computer and use it in GitHub Desktop.
Save rodricios/8532d178bfd3ac18ea8b to your computer and use it in GitHub Desktop.
Super simple ranking algo implementation inspired by Reddit and https://www.reddit.com/r/algorithms/comments/3lfxla/what_is_the_best_ranking_algorithm_to_rank_a/
list_of_items = [
['Wow, did you hear about this thing that happened over here?!?', 201, 10],
['TIL Something interesting happened at some place.', 14, 24],
['My cat.', 101, 14],
['Somebody said this to me the other day. I dont agree with that person.', 449, 55],
['For my cakeday, I want a lot of upvotes and presents.', 348, 33],
['My cat. Part 2.', 777, 13]
]
ranked_list_of_items = sorted(list_of_items, key=lambda x:x[1], reverse=True)
upvote_count_normalizing_contant = sum([x[1] for x in list_of_items])
item_age_normalizing_contant = sum([x[2] for x in list_of_items])
normalized_list_of_items = [[x[0], x[1]/upvote_count_normalizing_contant,
(1-x[2]/item_age_normalizing_contant)] # subtract from 1, old posts score less
for x in list_of_items]
def multiply_weights_to_scores(item, a, b):
"""assumes item is a list of 3 elements, where
the first element is a text, and the second and
third are numerical values multiplied by a and b
"""
return [item[0], a*item[1], b*item[2]]
a, b = 0.2, 0.8
weighted_normalized_list_of_items = [multiply_weights_to_scores(item, a, b)
for item in normalized_list_of_items]
sorted(weighted_normalized_list_of_items, key=lambda x: x[1]+x[2], reverse=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment