Problem 285874 · easy · Phase 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.

Goals

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