Skip to content

Instantly share code, notes, and snippets.

@erikbgithub
Created April 23, 2012 15:03
Show Gist options
  • Save erikbgithub/2471490 to your computer and use it in GitHub Desktop.
Save erikbgithub/2471490 to your computer and use it in GitHub Desktop.
#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