Count the Connections
Beginner · 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 lessonRedundancy costs resources, and one way to measure the cost is the number of connections (cables or links) a network needs. Write count_links(network), which returns how many connections the network has. In the dictionary, every connection appears twice, once for each end, but it should be counted once.
count_links({'A': ['B', 'C'], 'B': ['A'], 'C': ['A']}) -> 2def count_links(network):
# Each connection is listed by both of its devices
pass
What Check runs
Your code runs first, then each of these lines, and 1 more that stay hidden. The output must match exactly.
| Runs | Expected output |
|---|---|
| print(count_links({"A": ["B", "C"], "B": ["A"], "C": ["A"]})) | 2 |
| print(count_links({"A": ["B", "C"], "B": ["A", "C"], "C": ["A", "B"]})) | 3 |
| print(count_links({"A": ["B", "C", "D"], "B": ["A", "C", "D"], "C": ["A", "B", "D"], "D": ["A", "B", "C"]})) | 6 |
More Fault Tolerance exercises
- Still Connected?Intermediate
- Find the Single Points of FailureAdvanced