Problem 121152 · medium · Level 01 Prerequisites & Setup

Delay and Reverse on a Sample Pad

time shift · time reversal · zero padding · indexing · lists

A drum sample pad stores each sound as a clip of N samples and lets a performer apply effects one after another. The clip always keeps its length N: whatever is pushed past either end is lost, and any new gap is filled with silence (0). The pad has two effects:

  • ("delay", d): shift the sound d samples later. The new clip is y[k] = x[k - d] when 0 <= k - d < N, and y[k] = 0 otherwise. A negative d shifts the sound earlier (an advance).
  • ("reverse", 0): play the clip backwards, y[k] = x[N - 1 - k]. The number is ignored.

Write edit_clip(x, ops) that applies the operations in ops in order, each to the result of the one before, and returns the final clip. Do not change x.

Examples

Input:  x = [1, 2, 3, 4, 5], ops = [("delay", 2)]
Output: [0, 0, 1, 2, 3]
Explanation: the sound starts two samples later; the 4 and 5 fall off the end.

Input:  x = [1, 2, 3, 4, 5], ops = [("reverse", 0), ("delay", 1)]
Output: [0, 5, 4, 3, 2]

Input:  x = [1, 2, 3, 4, 5], ops = [("delay", 1), ("reverse", 0)]
Output: [4, 3, 2, 1, 0]
Explanation: the same two effects in the other order give a different clip.

Constraints

  • a shift of N samples or more in either direction leaves only silence
  • a list [kind, number] is accepted in place of a tuple for each operation

Goals

  • Delay or advance a signal by moving its samples and filling the gap with zeros
  • Reverse a signal in time with the index rule y[k] = x[N - 1 - k]
  • See that the order of operations changes the result
Starting Python…