Embercode
āœ•
ArraysQuick quiz

Sliding windows

9 questions. Covers Variable-size sliding window in Python.

Questions 1 to 5 of 9

  1. 1. What does this program print? ``python a = [4, 2, 7, 1, 3] k = 2 window = sum(a[:k]) best = window for i in range(k, len(a)): window += a[i] window -= a[i - k] best = max(best, window) print(best) ``
  2. 2. What does this program print? ``python a = [2, 1, 3, 1, 1, 4] limit = 6 left = 0 total = 0 best_length = 0 best_start = 0 for right in range(len(a)): total += a[right] while total > limit: total -= a[left] left += 1 length = right - left + 1 if length > best_length: best_length = length best_start = left print(best_length, "at", best_start) ``
  3. 3. What does this program print? ``python a = [2, 2, 1, 3, 0, 3] k = 2 window = sum(a[:k]) best_sum = window best_start = 0 for i in range(k, len(a)): window += a[i] window -= a[i - k] start = i - k + 1 if window > best_sum: best_sum = window best_start = start print(best_start, best_sum) ``
  4. 4. The program below should subtract the value that leaves the window, but it has no check for the first steps. What is the first line it prints? ``python a = [2, 1, 5, 1, 3, 2] k = 3 window = 0 for i in range(len(a)): window += a[i] window -= a[i - k] print(i, window) ``
  5. 5. Which window does this code never check? ``python a = [2, 1, 5, 1, 3, 2] k = 3 for start in range(len(a) - k): print(start, sum(a[start:start + k])) ``

5 question(s) left on this page.