Predict the output
What does this print?
boss = list(range(5))
def find(x: int) -> int:
while boss[x] != x:
boss[x] = boss[boss[x]]
x = boss[x]
return x
for a, b in [(0, 1), (1, 2), (3, 4)]:
boss[find(a)] = find(b)
print(len({find(x) for x in range(5)}))Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 2
Right: {0, 1, 2} and {3, 4}.
Every successful join removes one group, and the three links here make three joins.
Why not the others?
- B 3. Three links join five computers into two groups.
- C 5. Each link merges two groups, so the count drops.