ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def lcs(a, b):
dp = [[0] * (len(b) + 1) for _ in range(len(a) + 1)]
for i in range(1, len(a) + 1):
for j in range(1, len(b) + 1):
dp[i][j] = dp[i - 1][j - 1] + 1 if a[i - 1] == b[j - 1] else max(dp[i - 1][j], dp[i][j - 1])
return dp[-1][-1]
print(lcs('abcde', 'ace'))- A. 5
- B. 3
- C. 2
- D. 4
Correct answer: B. 3
The longest common subsequence of abcde and ace is ace, which has length 3.
More Python Programming questions
- What is printed? python from itertools import accumulate a = [2, -1, 3] P = list(accumulate(a, initial=0)) print(P, P[3] - P[1])
- What is printed? python from itertools import combinations print(sum(1 for r in range(5) for c in combinations([1, 2, 3, 4], r) if sum(c)…
- What is printed? python print([i ^ (i >> 1) for i in range(4)])
- What is printed? python print(pow(6, -1, 11), pow(4, -1, 9))
- What is printed? python print(int.frombytes(b'x01x00', 'big'), int.frombytes(b'x01x00', 'little'))
- What is printed? python print((255).tobytes(2, 'big'), (1).tobytes(3, 'little'))