Created
April 23, 2012 15:03
-
-
Save erikbgithub/2471490 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
#Input data: | |
calls = [ | |
( 'n1', 'n2', 'success' ), | |
( 'n1', 'n3', 'failed' ), | |
( 'n1', 'n2', 'failed' ), | |
( 'n2', 'n1', 'failed' ) | |
] | |
def connectivity(calls): | |
"""given a list of calls in the form of [(caller, callee, 'success' or 'failed'),...] | |
return a string telling how many percent of calls succeeded | |
""" | |
#1. init: | |
connections = list(set([(a,b) for a,b,c in calls])) #who called whom | |
successes = [0 for i in connections] #how many times 'success' | |
call_ctr = [0 for i in connections] #number of calls | |
#2. count successes | |
for a,b,c in calls: | |
i = connections.index((a,b)) | |
if c == 'success': successes[i] += 1 | |
call_ctr[i] += 1 | |
#3. create result and return | |
return '\n'.join(["{0} => {1}: {2}%".format( | |
connections[i][0], #caller | |
connections[i][1], #callee | |
successes[i]*100.0/call_ctr[i] #percentage | |
) | |
for i in range(len(connections)) | |
]) | |
print connectivity(calls) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment