Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

1.6 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
68adce01c0e1144d0a90295a Challenge 27: Matrix Rotate 29 challenge-27

--description--

Given a matrix (an array of arrays), rotate the matrix 90 degrees clockwise and return it. For instance, given [[1, 2], [3, 4]], which looks like this:

1 2
3 4

You should return [[3, 1], [4, 2]], which looks like this:

3 1
4 2

--hints--

rotate([[1]]) should return [[1]].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(rotate([[1]]), [[1]])`)
}})

rotate([[1, 2], [3, 4]]) should return [[3, 1], [4, 2]].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(rotate([[1, 2], [3, 4]]), [[3, 1], [4, 2]])`)
}})

rotate([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) should return [[7, 4, 1], [8, 5, 2], [9, 6, 3]].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(rotate([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), [[7, 4, 1], [8, 5, 2], [9, 6, 3]])`)
}})

rotate([[0, 1, 0], [1, 0, 1], [0, 0, 0]]) should return [[0, 1, 0], [0, 0, 1], [0, 1, 0]].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(rotate([[0, 1, 0], [1, 0, 1], [0, 0, 0]]), [[0, 1, 0], [0, 0, 1], [0, 1, 0]])`)
}})

--seed--

--seed-contents--

def rotate(matrix):

    return matrix

--solutions--

def rotate(matrix):
    n = len(matrix)
    result = [[0] * n for _ in range(n)]

    for i in range(n):
        for j in range(n):
            result[j][n - 1 - i] = matrix[i][j]

    return result