Problem 218638 · easy · Phase 02 Linear Data Structures

Slide One Row

2d-lists · modulo · slicing

Given a grid grid, a row index r and an integer k, return a new grid in which row r has been slid k positions to the right, with values that fall off the end wrapping around to the front. A negative k slides to the left. k may be larger than the row length. All other rows are unchanged.

Examples

Input:  grid = [[1, 2, 3, 4],
                [5, 6, 7, 8]], r = 1, k = 1
Output: [[1, 2, 3, 4],
         [8, 5, 6, 7]]

Input:  grid = [[1, 2, 3, 4],
                [5, 6, 7, 8]], r = 0, k = -1
Output: [[2, 3, 4, 1],
         [5, 6, 7, 8]]

Constraints

  • 1 <= rows, cols <= 100, 0 <= r < rows, -10**6 <= k <= 10**6
  • The input grid must not be modified.

Goals

  • Rotate a single row cyclically with a possibly negative or oversized shift
  • Copy a grid while replacing exactly one row
Starting Python…