ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
def ed(a, b):
prev = list(range(len(b) + 1))
for i in range(1, len(a) + 1):
cur = [i] + [0] * len(b)
for j in range(1, len(b) + 1):
cost = 0 if a[i - 1] == b[j - 1] else 1
cur[j] = min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + cost)
prev = cur
return prev[-1]
print(ed('kitten', 'sitting'), ed('', 'abc'))- A. 3 3
- B. 3 2
- C. 2 3
- D. 4 3
Correct answer: A. 3 3
Levenshtein distance from kitten to sitting is 3: two substitutions and one insertion. Turning an empty string into abc takes 3 insertions.
More Python Programming questions
- What is printed? python def minlen(a, t): best = float('inf') s = 0 l = 0 for r, x in enumerate(a): s += x while s >= t: best = min(best…
- What is printed? python import heapq def kthlargest(a, k): h = [] for x in a: heapq.heappush(h, x) if len(h) > k: heapq.heappop(h) return…
- What is printed? python from collections import deque def winmax(a, k): dq = deque() out = [] for i, x in enumerate(a): while dq and…
- What is printed? python from collections import deque g = {1: [2, 3], 2: [4], 3: [4, 5], 4: [6], 5: [], 6: []} dist = {1: 0} q =…
- What is printed? python from functools import cache @cache def total(xs): return sum(xs) try: print(total((1, 2, 3))) print(total([1, 2…
- You must choose the maximum number of non-overlapping activities, each with a start and a finish time. Which greedy rule is correct?