Skip to content

Instantly share code, notes, and snippets.

@jdiez17
Last active October 4, 2016 15:17
Show Gist options
  • Save jdiez17/175c3ec2e2ce4d386d5c1e212783efd1 to your computer and use it in GitHub Desktop.
Save jdiez17/175c3ec2e2ce4d386d5c1e212783efd1 to your computer and use it in GitHub Desktop.
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