Last active
October 4, 2016 15:17
-
-
Save jdiez17/175c3ec2e2ce4d386d5c1e212783efd1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
events = [ | |
('a', 0), | |
('b', 1), | |
('a', 2), | |
('b', 2), | |
('b', 3), | |
('a', 3) | |
] | |
groups = defaultdict(list) | |
for (ip, time) in events: | |
groups[ip].append(time) | |
tol = 1 | |
threshold = 2 | |
matches = [] | |
for group, times in groups.items(): | |
i = 0 | |
while i < len(times): | |
cnt = 0 | |
prev = times[i] | |
for cur in times[i:]: | |
if abs(cur - prev) > tol: | |
break | |
cnt += 1 | |
prev = cur | |
if cnt >= threshold: | |
matches.append((group, times[i:i+cnt])) | |
i += cnt | |
print(matches) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment