ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def pi(s):
p = [0] * len(s)
k = 0
for i in range(1, len(s)):
while k and s[i] != s[k]:
k = p[k - 1]
if s[i] == s[k]:
k += 1
p[i] = k
return p
print(pi('aabaaab'), pi('abcab')[-1])- A. [0, 1, 0, 1, 2, 2, 3] 2
- B. [0, 1, 0, 1, 2, 3, 3] 2
- C. [0, 1, 0, 1, 2, 2, 3] 1
- D. [0, 0, 0, 1, 2, 2, 3] 2
Correct answer: A. [0, 1, 0, 1, 2, 2, 3] 2
The prefix function gives, for each position, the length of the longest proper prefix that also ends there. For aabaaab it rises 1, 0, 1, 2, 2, 3, and for abcab the last value is 2 because ab repeats.
More Python Programming questions
- What is printed? python from collections import deque def bip(g): col = {} for s in g: if s in col: continue col[s] = 0 q = deque([s])…
- What is printed? python def mst(n, edges): p = list(range(n)) def f(x): while p[x] != x: x = p[x] return x total = 0 for w, u, v in…
- What is printed? python def candy(r): n = len(r) c = [1] n for i in range(1, n): if r[i] > r[i - 1]: c[i] = c[i - 1] + 1 for i in range(n…
- What is printed? python def mat(a, b, m): return [[sum(a[i][k] b[k][j] for k in range(2)) % m for j in range(2)] for i in range(2)] def…
- What is printed? python class A: x = 1 def get(self): return self.x a = A() a.x = 5 A.x = 9 b = A() print(a.get(), b.get(), A.x)
- What is printed? python class A: def f(self): return ['A'] class B(A): def f(self): return ['B'] + super().f() class C(A): def f(self)…