-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongest_contiguous_sublists.py
87 lines (67 loc) · 2.22 KB
/
longest_contiguous_sublists.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
86
87
"""
We have some clickstream data that we gathered on our client's website.
Using cookies, we collected snippets of users' anonymized URL histories
while they browsed the site. The histories are in chronological order
and no URL was visited more than once per person.
Write a function that takes two users' browsing histories as input and
returns the longest contiguous sequence of URLs that appears in both.
Sample output:
findContiguousHistory(user0, user1)
/pink.php
/register.asp
/orange.html
findContiguousHistory(user1, user2)
/green.html
/blue.html
/pink.php
/register.asp
findContiguousHistory(user0, user3)
(empty)
findContiguousHistory(user2, user3)
/blue.html
findContiguousHistory(user3, user3)
/blue.html
/logout.php
"""
from itertools import combinations
user0 = [ "/start.html", "/pink.php", "/register.asp", "/orange.html", "/red.html" ];
user1 = [ "/start.html", "/green.html", "/blue.html", "/pink.php", "/register.asp", "/orange.html" ]
user2 = [ "/red.html", "/green.html", "/blue.html", "/pink.php", "/register.asp" ]
user3 = [ "/blue.html", "/logout.php" ]
def all_slices(s):
for start, end in combinations(range(len(s)+1), 2):
yield s[start:end]
def findContiguousHistory(userA, userB):
if len(userA) < len(userB):
shortest, longest = userA, userB
else:
shortest, longest = userB, userA
a_slices = sorted(all_slices(shortest), key=len, reverse=True)
b_slices = sorted(all_slices(longest), key=len, reverse=True)
for item in a_slices:
if item in b_slices:
print("\n".join(item))
return item
print('empty')
return []
print('findContiguousHistory(user0, user1)')
findContiguousHistory(user0, user1)
# /pink.php
# /register.asp
# /orange.html
print('findContiguousHistory(user1, user2)')
findContiguousHistory(user1, user2)
# /green.html
# /blue.html
# /pink.php
# /register.asp
print('findContiguousHistory(user0, user3)')
findContiguousHistory(user0, user3)
# (empty)
print('findContiguousHistory(user2, user3)')
findContiguousHistory(user2, user3)
# /blue.html
print('findContiguousHistory(user3, user3)')
findContiguousHistory(user3, user3)
# /blue.html
# /logout.php