ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def edit(a, b):
dp = [[0] * (len(b) + 1) for _ in range(len(a) + 1)]
for i in range(len(a) + 1):
for j in range(len(b) + 1):
if i == 0 or j == 0:
dp[i][j] = i + j
elif a[i - 1] == b[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1])
return dp[-1][-1]
print(edit('kitten', 'sitting'))- A. 3
- B. 4
- C. 5
- D. 2
Correct answer: A. 3
kitten becomes sitting with two substitutions and one insertion, a total of 3 edits, which is the Levenshtein distance.
More Python Programming questions
- What is printed? python 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…
- 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'))