Spiral order
You get a grid of integers grid: a list of m rows, each a list of n values. Read every cell once, in a clockwise spiral that starts at the top-left cell: right along the top row, down the right column, left along the bottom row, then up the left column. That reads the outer ring, the cells on the edge of the grid. Then read what is left inside it the same way, and keep going until no cell is left. Return the values in the order you read them, as one list.
Leave grid unchanged: the caller still needs it.
grid = [[1, 2, 3], [8, 9, 4], [7, 6, 5]]Output[1, 2, 3, 4, 5, 6, 7, 8, 9]This grid is numbered along its spiral. The outer ring reads 1 to 8, and the 9 is all that is left inside.
grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]Output[1, 2, 3, 6, 9, 12, 11, 10, 7, 4, 5, 8]The outer ring reads 1, 2, 3, then 6, 9, 12, then 11, 10, then 7, 4. Inside it is one column, 5 above 8, read downward.
grid = [[4, 7, 1]]Output[4, 7, 1]One row: reading it left to right reads every cell, so nothing comes back the other way.
m = len(grid), and every row has the same lengthn.1 ≤ m, n ≤ 10
-100 ≤ grid[r][c] ≤ 100
Plan it first
Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.