Still Connected?
Intermediate · Write the code · from Fault Tolerance and Redundancy: How the Internet Survives Failures
From the lesson: How can a program tell whether two devices are still connected?
Here is a small school district network. Each device maps to the list of devices it is directly connected to. A connection goes both ways, so it appears twice: "router A" lists "school", and "school" lists "router A".
Read this part of the lessonWrite still_connected(network, start, goal, broken). broken is a list of failed connections, each a tuple of two devices, such as ('B', 'D'). A connection is broken in both directions, so ('B', 'D') also blocks D to B. Return True if start can still reach goal using only working connections.
Tip: start from the can_reach search in the lesson, and skip any neighbour that is joined to the current device by a broken connection.
net = {'A': ['B', 'C'], 'B': ['A', 'D'], 'C': ['A', 'D'], 'D': ['B', 'C']}
still_connected(net, 'A', 'D', [('B', 'D')]) -> True (A, C, D)
still_connected(net, 'A', 'D', [('B', 'D'), ('A', 'C')]) -> FalseWhat Check runs
Your code runs first, then each of these lines, and 2 more that stay hidden. The output must match exactly.
| Runs | Expected output |
|---|---|
| print(still_connected({"A": ["B", "C"], "B": ["A", "D"], "C": ["A", "D"], "D": ["B", "C"]}, "A", "D", [("B", "D")])) | True |
| print(still_connected({"A": ["B", "C"], "B": ["A", "D"], "C": ["A", "D"], "D": ["B", "C"]}, "A", "D", [("B", "D"), ("A", "C")])) | False |
| print(still_connected({"A": ["B", "C"], "B": ["A", "D"], "C": ["A", "D"], "D": ["B", "C"]}, "A", "D", [])) | True |
More Fault Tolerance exercises
- Count the ConnectionsBeginner
- Find the Single Points of FailureAdvanced