Skip to content
DeepForgeby svx
All problems
MediumTime Seriests-323

Backshift Operator Apply

Problem

Apply the backshift operator k times to a series, so that the value at index t becomes the value from index t - k. Positions near the start with no earlier value are filled with series[0].

If k is not positive, return the series unchanged. Return an empty list for an empty series.

Hint

In backshift notation B x[t] = x[t-1], and B^k x[t] = x[t-k].

Test cases

case 1
in: [[1,2,3,4],1]
expected: [1,1,2,3]
case 2
in: [[1,2,3,4],2]
expected: [1,1,1,2]
case 3
in: [[5,6],0]
expected: [5,6]
case 4
in: [[],3]
expected: []
Starter codePython
def apply_backshift(series, k):
    # Your code here
    pass

4 test cases · run your code against them in the editor