27Spiral Matrix

    Hard2D listsloops

    Write spiral(matrix) that returns all elements of a rectangular grid (a list of rows) in clockwise spiral order, starting from the top-left corner.

    For [[1, 2, 3], [4, 5, 6], [7, 8, 9]] the answer is [1, 2, 3, 6, 9, 8, 7, 4, 5].

    Examples

    Example 1
    >>> spiral([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    [1, 2, 3, 6, 9, 8, 7, 4, 5]
    Example 2
    >>> spiral([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
    [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]

    Run examples checks the 2 examples above. Submit runs all 7 tests, including hidden edge cases, and marks the challenge solved.

    solution.py
    Loading Python…

    Test results show up here

    Write spiral in the editor, then run the examples. Use print() freely: its output is shown for each test.