Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Exercice 1: Les Graphes solution in Clear category for Network Attack by ghannam.mohammad.tsi
def capture(a) -> int:
n, r, infected = len(a), 0, {0}
while [a[i][i] for i in range(n)] != [0] * n:
daz = []
for pc in infected:
pc_neigh = [i for i in range(n) if a[pc][i] == 1 and pc != i]
for pc1 in pc_neigh:
if a[pc1][pc1] != 0 and (not pc1 in daz):
a[pc1][pc1] -= 1
daz.append(pc1)
for i in range(n):
if a[i][i] == 0:
infected.add(i)
r += 1
return r
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert capture([[0, 1, 0, 1, 0, 1],
[1, 8, 1, 0, 0, 0],
[0, 1, 2, 0, 0, 1],
[1, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 3, 1],
[1, 0, 1, 0, 1, 2]]) == 8, "Base example"
assert capture([[0, 1, 0, 1, 0, 1],
[1, 1, 1, 0, 0, 0],
[0, 1, 2, 0, 0, 1],
[1, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 3, 1],
[1, 0, 1, 0, 1, 2]]) == 4, "Low security"
assert capture([[0, 1, 1],
[1, 9, 1],
[1, 1, 9]]) == 9, "Small"
Aug. 6, 2020