Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 7d512bbf40
Fetching contributors…

Cannot retrieve contributors at this time

executable file 79 lines (51 sloc) 1.732 kb
#!/usr/bin/env python
import sys
MIN_USERS_IN_CLUSTER = 3
def x_unique_combinations(items, n):
if n == 0:
yield []
else:
for i in xrange(len(items)):
for cc in x_unique_combinations(items[i+1:],n-1):
yield [items[i]]+cc
def is_a_cluster(cluster, user_actions):
for user in cluster:
for other_user in cluster:
if user is not other_user:
if not (other_user in user_actions[user] and user in user_actions[other_user]):
return False
return True
def main():
file = open(sys.argv[1])
users = []
user_actions = {}
logs = []
for line in file:
log = line.strip().split('\t')[1:]
logs.append(log)
file.close()
for log in logs:
user1 = log[0]
user2 = log[1]
if(user1 not in users):
users.append(user1)
if(user2 not in users):
users.append(user2)
if user1 not in user_actions:
user_actions[user1] = []
if user2 not in user_actions[user1]:
user_actions[user1].append(user2)
users.sort()
clusters = {}
for i in xrange(MIN_USERS_IN_CLUSTER, len(users) + 1):
for s in x_unique_combinations(users, i):
clusters[tuple(s)] = True
print_clusters = []
for cluster in clusters:
if(is_a_cluster(cluster, user_actions)):
print_clusters.append(', '.join(cluster))
print_clusters.sort()
for cluster in print_clusters:
print(cluster)
if __name__ == "__main__":
main()
Jump to Line
Something went wrong with that request. Please try again.