Embercode
✕
ArraysQuick quiz

Rotation and differences

7 questions. Covers Difference arrays in Python lists.

Questions 1 to 5 of 7

  1. 1. What does this program print? ``python def reverse(a, left, right): while left < right: a[left], a[right] = a[right], a[left] left += 1 right -= 1 def rotate_right(a, k): n = len(a) k = k % n reverse(a, 0, n - 1) reverse(a, 0, k - 1) reverse(a, k, n - 1) a = [1, 2, 3, 4, 5] rotate_right(a, 2) print(a) ``
  2. 2. In the rotation by reversal method, the last k items move to the front. What word does the lesson use for those last k items? Type one word.

    Type your answer.

  3. 3. What does this program print? ``python def add_range(diff, left, right, value): diff[left] += value diff[right + 1] -= value def recover(diff, n): result = [0] * n running = 0 for i in range(n): running += diff[i] result[i] = running return result diff = [0] * 5 add_range(diff, 0, 2, 4) add_range(diff, 2, 3, -1) print(recover(diff, 4)) ``
  4. 4. To add value to every position from left to right in a difference array, the code is diff[left] += value and diff[____] -= value. Type the index expression that goes in the blank.

    Type your answer.

  5. 5. Why does the rotation method run k = k % n before it does any reversal?

5 question(s) left on this page.

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