← Quiz: DP on Grids and Strings: all 10 questions
Quiz: DP on Grids and Strings · Question 8 of 10
What does this print? (code)
What does this print?
def ed(a, b):
dp = [[i + j if i * j == 0 else 0 for j in range(len(b) + 1)] for i in range(len(a) + 1)]
for i in range(1, len(a) + 1):
for j in range(1, len(b) + 1):
if 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(ed("cat", "cut"), ed("", "abc"))
- A.1 3
- B.2 3
- C.1 0
- D.0 3
Answer & explanation
Correct answer: 1 3
"cat" to "cut" is one replace; an empty string needs 3 inserts to become "abc".
Can you get the rest right without peeking?
Take the timed Quiz: DP on Grids and Strings quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →