ProgrammingHardGATE CS & IT · Infosys
What is printed?
What is printed?
from collections import deque
g = {1: [2, 3], 2: [4], 3: [4, 5], 4: [6], 5: [], 6: []}
dist = {1: 0}
q = deque([1])
while q:
u = q.popleft()
for v in g[u]:
if v not in dist:
dist[v] = dist[u] + 1
q.append(v)
print(sorted(v for v in dist if dist[v] == 2))- A. [4, 5]
- B. [4, 5, 6]
- C. [2, 3]
- D. [4]
Correct answer: A. [4, 5]
Vertices at distance 2 are reached through distance-1 vertices 2 or 3, which gives 4 and 5. Vertex 6 is at distance 3 because it is reached through 4.
More Python Programming questions
- What is printed? python from functools import cache @cache def total(xs): return sum(xs) try: print(total((1, 2, 3))) print(total([1, 2…
- You must choose the maximum number of non-overlapping activities, each with a start and a finish time. Which greedy rule is correct?
- What is printed? python a = [4, 1, 7, 3, 2] pre = [0] for x in a: pre.append(pre[-1] + x) def rng(l, r): return pre[r] - pre[l - 1]…
- What is printed? python def mostwater(h): i, j, best = 0, len(h) - 1, 0 while i < j: best = max(best, min(h[i], h[j]) (j - i)) if h[i] <…
- What is printed? python def longest(s): last = {} l = best = 0 for r, ch in enumerate(s): if ch in last and last[ch] >= l: l = last[ch] +…
- What is printed? python import bisect a = sorted([5, 1, 9, 5, 3, 7, 5]) lo = bisect.bisectleft(a, 3) hi = bisect.bisectright(a, 7)…