Skip to content

S3Q1 · Matrix Walk

⚡ Quick Reference

Function: walk_matrix(M, shape) -> list

def walk_matrix(M, shape):
    n = len(M)
    if shape == "L":
        return [M[r][0] for r in range(n)] + [M[n-1][c] for c in range(1, n)]
    elif shape == "Z":
        top  = [M[0][c] for c in range(n)]
        diag = [M[r][n-1-r] for r in range(1, n-1)]
        bot  = [M[n-1][c] for c in range(n)]
        return top + diag + bot
    elif shape == "O":
        top    = [M[0][c]   for c in range(n)]
        right  = [M[r][n-1] for r in range(1, n)]
        bottom = [M[n-1][c] for c in range(n-2, -1, -1)]
        left   = [M[r][0]   for r in range(n-2, 0, -1)]
        return top + right + bottom + left

Key paths: - L: first column ↓, then bottom row → - Z: top row →, diagonal ↘ (top-right to bottom-left), bottom row → - O: clockwise border - top→, right↓, bottom←, left↑


Problem Statement

Problem

Write a function walk_matrix(M, shape) that returns a list of elements traversed along the given shape path in a square matrix.

Sample matrix:

M = [[1,2,3],[4,5,6],[7,8,9]]

shape
"L"
Output
[1, 4, 7, 8, 9]
shape
"Z"
Output
[1, 2, 3, 5, 7, 8, 9]
shape
"O"
Output
[1, 2, 3, 6, 9, 8, 7, 4]

L-shape walkthrough

[1] 2  3
[4] 5  6
[7][8][9]
  • Go down column 0: 1, 4, 7
  • Go right along bottom row (skip col 0 already visited): 8, 9
[M[r][0] for r in range(n)]          # col 0: 1,4,7
+ [M[n-1][c] for c in range(1, n)]   # bottom row from col 1: 8,9

Z-shape walkthrough

[1][2][3]
 4 [5] 6
[7][8][9]
  • Top row left→right: 1, 2, 3
  • Diagonal: for rows 1 to n-2, column = n-1-r
  • r=1: col n-2=1M[1][1]=5
  • Bottom row left→right: 7, 8, 9
top  = [M[0][c]       for c in range(n)]
diag = [M[r][n-1-r]   for r in range(1, n-1)]
bot  = [M[n-1][c]     for c in range(n)]

Z diagonal formula

The Z diagonal goes from top-right to bottom-left. At row r, the column is n-1-r (starts at n-2 for row 1, ends at 1 for row n-2).


O-shape walkthrough

[1][2][3]
[4] 5 [6]
[7][8][9]

Clockwise from top-left: 1. Top row left→right: 1, 2, 3 2. Right column top→bottom (skip top-right already visited): 6, 9 3. Bottom row right→left (skip bottom-right already visited): 8, 7 4. Left column bottom→top (skip both corners already visited): 4

top    = [M[0][c]   for c in range(n)]           # 1,2,3
right  = [M[r][n-1] for r in range(1, n)]         # 6,9
bottom = [M[n-1][c] for c in range(n-2, -1, -1)]  # 8,7
left   = [M[r][0]   for r in range(n-2, 0, -1)]   # 4
Segment Start End Step Elements
Top col 0 col n-1 +1 1,2,3
Right row 1 row n-1 +1 6,9
Bottom col n-2 col 0 -1 8,7
Left row n-2 row 1 -1 4

Complete solution approaches

def walk_matrix(M, shape):
    n = len(M)
    if shape == "L":
        return [M[r][0] for r in range(n)] + \
               [M[n-1][c] for c in range(1, n)]
    elif shape == "Z":
        return ([M[0][c] for c in range(n)] +
                [M[r][n-1-r] for r in range(1, n-1)] +
                [M[n-1][c] for c in range(n)])
    elif shape == "O":
        return ([M[0][c]   for c in range(n)] +
                [M[r][n-1] for r in range(1, n)] +
                [M[n-1][c] for c in range(n-2, -1, -1)] +
                [M[r][0]   for r in range(n-2, 0, -1)])
def walk_matrix(M, shape):
    n = len(M)

    if shape == "L":
        col_0  = [M[r][0]   for r in range(n)]
        bot_row = [M[n-1][c] for c in range(1, n)]
        return col_0 + bot_row

    elif shape == "Z":
        top  = [M[0][c]     for c in range(n)]
        diag = [M[r][n-1-r] for r in range(1, n-1)]
        bot  = [M[n-1][c]   for c in range(n)]
        return top + diag + bot

    elif shape == "O":
        top    = [M[0][c]   for c in range(n)]
        right  = [M[r][n-1] for r in range(1, n)]
        bottom = [M[n-1][c] for c in range(n-2, -1, -1)]
        left   = [M[r][0]   for r in range(n-2, 0, -1)]
        return top + right + bottom + left
def walk_matrix(M, shape):
    n = len(M)
    paths = {
        "L": lambda: (
            [M[r][0] for r in range(n)] +
            [M[n-1][c] for c in range(1, n)]
        ),
        "Z": lambda: (
            [M[0][c] for c in range(n)] +
            [M[r][n-1-r] for r in range(1, n-1)] +
            [M[n-1][c] for c in range(n)]
        ),
        "O": lambda: (
            [M[0][c]   for c in range(n)] +
            [M[r][n-1] for r in range(1, n)] +
            [M[n-1][c] for c in range(n-2, -1, -1)] +
            [M[r][0]   for r in range(n-2, 0, -1)]
        ),
    }
    return paths[shape]()

Lambdas stored in a dict - calling paths[shape]() dispatches to the right traversal.


Key takeaways

01

Clockwise O - careful with range endpoints

Each segment of the O must start where the previous left off, without repeating corners. Use range(1, n), range(n-2, -1, -1), and range(n-2, 0, -1) to skip already-visited positions.

02

Z diagonal: col = n-1-r

The diagonal goes from top-right to bottom-left. At row r, the column index is n-1-r. Draw it out for a 3×3 to verify: row 1 → col 1 (centre), which is correct.

03

Lambda dispatch for multi-shape functions

Storing each shape as a lambda in a dict avoids a long if-elif chain. Call paths[shape]() - clean, extensible, and easy to add new shapes later.