-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_highest_affinity.py
executable file
·32 lines (25 loc) · 1.04 KB
/
compute_highest_affinity.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
# No need to process files and manipulate strings - we will
# pass in lists (of equal length) that correspond to
# sites views. The first list is the site visited, the second is
# the user who visited the site.
# See the test cases for more details.
def highest_affinity(site_list, user_list, time_list):
# Returned string pair should be ordered by dictionary order
# I.e., if the highest affinity pair is "foo" and "bar"
# return ("bar", "foo").
user_history = {}
for site, user in zip(site_list, user_list):
if user not in user_history:
user_history[user] = set()
user_history[user].add(site)
affinities = {}
for user, history in user_history.items():
history = list(history)
history.sort()
for i, site1 in enumerate(history):
for site2 in history[i+1:]:
pair = (site1, site2)
if pair not in affinities:
affinities[pair] = 0
affinities[pair] += 1
return max(affinities, key=affinities.get)