Embercode
✕
Dynamic ProgrammingQuick quiz

Substrings and reconstruction

8 questions. Covers Reconstructing the answer by storing choices in Python.

Questions 1 to 5 of 8

  1. 1. What does this program print? ``python def longest(a, b): n = len(a) m = len(b) dp = [0] * (m + 1) best = 0 best_end = 0 for i in range(1, n + 1): for j in range(m, 0, -1): if a[i - 1] == b[j - 1]: dp[j] = dp[j - 1] + 1 if dp[j] > best: best = dp[j] best_end = i else: dp[j] = 0 return f"length = {best} text = {a[best_end - best:best_end]}" print(longest('xabcyz', 'zzabcq')) ``
  2. 2. What does this program print? ``python def longest_match(a, b): n = len(a) m = len(b) dp = [[0] * (m + 1) for _ in range(n + 1)] for i in range(1, n + 1): for j in range(1, m + 1): if a[i - 1] == b[j - 1]: dp[i][j] = dp[i - 1][j - 1] + 1 else: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) return dp[n][m] print(longest_match('abxcdy', 'abcdz')) ``
  3. 3. On a mismatch, the substring method writes 0 into the cell instead of taking the larger neighbour. Why?
  4. 4. The edit distance walk back loops while i > 0 or j > 0. Why not and?
  5. 5. What name does the lesson give to the method that keeps only two rows of the value table?

    Type your answer.

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.