Problem 285874 · easy · Level 02 Linear Data Structures

Mirror a Grid

2d-lists · slicing · reversal

Given a grid grid and a string axis, return a new grid that is the mirror image of the input:

  • axis == "h": mirror left-to-right (each row is reversed).
  • axis == "v": mirror top-to-bottom (the order of the rows is reversed).

Examples

Input:  grid = [[1, 2, 3],
                [4, 5, 6]], axis = "h"
Output: [[3, 2, 1],
         [6, 5, 4]]

Input:  grid = [[1, 2, 3],
                [4, 5, 6]], axis = "v"
Output: [[4, 5, 6],
         [1, 2, 3]]

Constraints

  • 1 <= rows, cols <= 100
  • axis is always "h" or "v"
  • The input grid must not be modified, and the result must not share any row with it. One test calls grid_after(grid, axis), which runs your flip, writes 0 into every cell of the result, and then returns grid itself, so it must come back unchanged.

Goals

  • Reverse rows or the row order based on a flag
  • Return a copy rather than mutating the input
Starting Python…