Problem 223710 · easy · Phase 02 Linear Data Structures

Snake Order Flatten

2d-lists · flattening · alternating-direction

Given a grid grid, return all its values in a single list in snake order: the first row from left to right, the second row from right to left, the third from left to right, and so on.

Examples

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

Input:  grid = [[1, 2],
                [3, 4]]
Output: [1, 2, 4, 3]

Constraints

  • 0 <= rows, cols <= 100; an empty grid gives an empty list

Goals

  • Alternate the direction of traversal on every row
  • Flatten a grid into a single list
Starting Python…