/
hyper_rank.py
85 lines (61 loc) · 3.48 KB
/
hyper_rank.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from typing import List, Dict
class HyperRank():
# Define the HyperRank class, which will contain the functions necessary for ranking the items
def assign_item_integers(self, item_data: List[Dict[str, any]]) -> List[Dict[str, any]]:
# This function takes in a list of dictionaries, each containing item data and a list of votes.
# It assigns each item a numerical integer based on the number of votes it has.
# The updated item data with the integer value is returned as a list of dictionaries.
updated_item_data = []
for item in item_data:
votes = item["votes"]
item_int = len(votes)
new_item = dict(item)
new_item["item_int"] = item_int
updated_item_data.append(new_item)
return updated_item_data
def rank_items(self, item_data: List[Dict[str, any]]) -> List[Dict[str, any]]:
# This function takes in a list of dictionaries containing item data and their assigned integers.
# It ranks the items into three tiers: A, B, and C based on their assigned integer values.
# It then modifies the integer values of the items in tier B and tier C.
# Finally, it returns the sorted list of items based on their modified integer values.
sorted_items = sorted(item_data, key=lambda x: x["item_int"], reverse=True)
item_data_quarter = len(item_data) // 4
tier_A = sorted_items[:item_data_quarter]
tier_B = sorted_items[item_data_quarter:(3 * item_data_quarter)]
tier_C = sorted_items[(3 * item_data_quarter):]
for item in tier_B:
item["item_int"] = item["item_int"] ** 2
for item in tier_C:
item["item_int"] = item["item_int"] ** 3
sorted_items = sorted(item_data, key=lambda x: x["item_int"], reverse=True)
return sorted_items
def hyper_rank(self, current_user: Dict[str, any], user_data: List[Dict[str, any]], item_data: List[Dict[str, any]]) -> List[Dict[str, any]]:
# This function takes in a dictionary of votes for the current user, a list of dictionaries containing user data,
# and a list of dictionaries containing item data and their assigned integers.
# It assigns each item an integer value, ranks them, and returns the sorted list of items.
item_data = self.assign_item_integers(item_data)
ranked_items = self.rank_items(item_data)
return ranked_items
# example usage
hr = HyperRank()
# Create an example current user dictionary and example lists of user and item dictionaries
current_user = {"votes": ["item1", "item2"]}
user_data = [
{"uid": "user1", "votes": ["item1", "item2", "item4"]},
{"uid": "user2", "votes": ["item2", "item3", "item5"]},
{"uid": "user3", "votes": ["item3", "item4", "item6"]},
{"uid": "user4", "votes": ["item1", "item3", "item5", "item6"]},
{"uid": "user5", "votes": ["item2", "item4", "item6"]}
]
item_data = [
{"item_id": "item1", "votes": ["user1", "user4"]},
{"item_id": "item2", "votes": ["user1", "user2", "user5"]},
{"item_id": "item3", "votes": ["user2", "user3", "user4"]},
{"item_id": "item4", "votes": ["user1", "user3", "user5"]},
{"item_id": "item5", "votes": ["user2", "user4"]},
{"item_id": "item6", "votes": ["user3", "user4", "user5"]}
]
# call the hyper_rank() function and store the result in the variable ranked_items
ranked_items = hr.hyper_rank(current_user, user_data, item_data)
# print the ranked_items to the console
print(ranked_items)