Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Node Disconnected Users solution in Clear category for Node Disconnected Users by alco
def disconnected_users(net, users, source, crushes):
# use sets to add tuples without worrying about duplicates
set1 = set([(x,y) for (x,y) in net if x not in crushes and y not in crushes])
# set2 is the result set
set2 = set()
# dummy procedure to add existing connections from each node to every
# other node but proceeding one node each time
# that's why i use the first for loop (after len(set1) times max every
# connection will be discovered)
for count in range(len(set1)):
for (x1, y1) in set1:
for (x2, y2) in set1:
if (y1 == x2):
set2.add((x1,y2))
# union with the initial set
set2 = set1.union(set2);
# return sum of users
# check if source in crushed nodes in the end
return sum([users[k] for k in users.keys() if k not in [y for (x,y) in set2
if x == source] and (k != source if source not in crushes else 1)])
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert disconnected_users([
['A', 'B'],
['B', 'C'],
['C', 'D']
], {
'A': 10,
'B': 20,
'C': 30,
'D': 40
},
'A', ['C']) == 70, "First"
assert disconnected_users([
['A', 'B'],
['B', 'D'],
['A', 'C'],
['C', 'D']
], {
'A': 10,
'B': 0,
'C': 0,
'D': 40
},
'A', ['B']) == 0, "Second"
assert disconnected_users([
['A', 'B'],
['A', 'C'],
['A', 'D'],
['A', 'E'],
['A', 'F']
], {
'A': 10,
'B': 10,
'C': 10,
'D': 10,
'E': 10,
'F': 10
},
'C', ['A']) == 50, "Third"
print('Done. Try to check now. There are a lot of other tests')
Sept. 22, 2019