ProgrammingHardCodeforces-style · placement coding round
What is printed?
What is printed?
def steps_to_cycle(f, s):
seen = {}
i = 0
while s not in seen:
seen[s] = i
s = f[s]
i += 1
return i, i - seen[s]
print(steps_to_cycle({0: 1, 1: 2, 2: 3, 3: 1}, 0))- A. (3, 3)
- B. (4, 4)
- C. (4, 3)
- D. (3, 2)
Correct answer: C. (4, 3)
The walk 0, 1, 2, 3 reaches the 1-2-3 cycle. The first repeat is at node 1 on step 4, and the cycle length is 4 - 1 = 3.
More Python Programming questions
- What is printed? python from collections import Counter def divpairs(a, k): c = Counter(x % k for x in a) total = 0 for r in c: s = (k…
- What is printed? python def isrot(a, b): return len(a) == len(b) and b in a + a print(isrot('abcde', 'cdeab'), isrot('abc', 'acb')…
- What is printed? python def egcd(a, b): if b == 0: return a, 1, 0 g, x, y = egcd(b, a % b) return g, y, x - (a // b) y g, x, y = egcd(240…
- What is printed? python def canjump(a): far = 0 for i, x in enumerate(a): if i > far: return False far = max(far, i + x) return True…
- What is printed? python import copy a = [[1], [2]] b = a[:] c = copy.deepcopy(a) a[0].append(9) print(b, c)
- What is printed? python def f(x, acc=[]): acc.append(x) return acc f(1) f(2) print(f(3, []), f(4))