Skip to main content
Added more complete example.
Source Link
SteveJ
  • 381
  • 1
  • 5

You can do the rotate in one line.

d = 3
m1 = [[y*d+x+1 for x in range(d)] for y in range(d)]
m2 = [list(r) for r in zip(*m1[::-1])]

[print(r) for r in m1]
[print(r) for r in m2]

and...I don't think you want to pass the matrix, and d. You can get d from the matrix - passing in both you run the risk of a mismatch.

[Edit: Since pasting into the interpreter is not as assumed as I thought]

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

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

[Edit: ...for completeness]

def rotate_matrix(matrix):
    return [list(r) for r in zip(*matrix[::-1])]

def print_matrix(matrix, name):
    print(f"{name} = ")
    [print(r) for r in matrix]

if __name__ == "__main__":
    h, w = 3, 4
    m1 = [[y * w + x + 1 for x in range(w)] for y in range(h)]
    print_matrix(m1, "original")
    print_matrix(rotate_matrix(m1), "rotated once")
    print_matrix(rotate_matrix(rotate_matrix(m1)), "rotated twice")
    print_matrix(rotate_matrix(rotate_matrix(rotate_matrix(rotate_matrix(m1)))), "back to the beginning")

Yields:

original = 
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]

rotated once = 
[9, 5, 1]
[10, 6, 2]
[11, 7, 3]
[12, 8, 4]

rotated twice = 
[12, 11, 10, 9]
[8, 7, 6, 5]
[4, 3, 2, 1]

back to the beginning = 
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]

You can do the rotate in one line.

d = 3
m1 = [[y*d+x+1 for x in range(d)] for y in range(d)]
m2 = [list(r) for r in zip(*m1[::-1])]

[print(r) for r in m1]
[print(r) for r in m2]

and...I don't think you want to pass the matrix, and d. You can get d from the matrix - passing in both you run the risk of a mismatch.

[Edit: Since pasting into the interpreter is not as assumed as I thought]

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

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

You can do the rotate in one line.

d = 3
m1 = [[y*d+x+1 for x in range(d)] for y in range(d)]
m2 = [list(r) for r in zip(*m1[::-1])]

[print(r) for r in m1]
[print(r) for r in m2]

and...I don't think you want to pass the matrix, and d. You can get d from the matrix - passing in both you run the risk of a mismatch.

[Edit: Since pasting into the interpreter is not as assumed as I thought]

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

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

[Edit: ...for completeness]

def rotate_matrix(matrix):
    return [list(r) for r in zip(*matrix[::-1])]

def print_matrix(matrix, name):
    print(f"{name} = ")
    [print(r) for r in matrix]

if __name__ == "__main__":
    h, w = 3, 4
    m1 = [[y * w + x + 1 for x in range(w)] for y in range(h)]
    print_matrix(m1, "original")
    print_matrix(rotate_matrix(m1), "rotated once")
    print_matrix(rotate_matrix(rotate_matrix(m1)), "rotated twice")
    print_matrix(rotate_matrix(rotate_matrix(rotate_matrix(rotate_matrix(m1)))), "back to the beginning")

Yields:

original = 
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]

rotated once = 
[9, 5, 1]
[10, 6, 2]
[11, 7, 3]
[12, 8, 4]

rotated twice = 
[12, 11, 10, 9]
[8, 7, 6, 5]
[4, 3, 2, 1]

back to the beginning = 
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]
Added results.
Source Link
SteveJ
  • 381
  • 1
  • 5

You can do the rotate in one line.

d = 3
m1 = [[y*d+x+1 for x in range(d)] for y in range(d)]
m2 = [list(r) for r in zip(*m1[::-1])]

[print(r) for r in m1]
[print(r) for r in m2]

and...I don't think you want to pass the matrix, and d. You can get d from the matrix - passing in both you run the risk of a mismatch.

[Edit: Since pasting into the interpreter is not as assumed as I thought]

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

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

You can do the rotate in one line.

d = 3
m1 = [[y*d+x+1 for x in range(d)] for y in range(d)]
m2 = [list(r) for r in zip(*m1[::-1])]

[print(r) for r in m1]
[print(r) for r in m2]

and...I don't think you want to pass the matrix, and d. You can get d from the matrix - passing in both you run the risk of a mismatch.

You can do the rotate in one line.

d = 3
m1 = [[y*d+x+1 for x in range(d)] for y in range(d)]
m2 = [list(r) for r in zip(*m1[::-1])]

[print(r) for r in m1]
[print(r) for r in m2]

and...I don't think you want to pass the matrix, and d. You can get d from the matrix - passing in both you run the risk of a mismatch.

[Edit: Since pasting into the interpreter is not as assumed as I thought]

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

m2 =
    [7, 4, 1]
    [8, 5, 2]
    [9, 6, 3]
Source Link
SteveJ
  • 381
  • 1
  • 5

You can do the rotate in one line.

d = 3
m1 = [[y*d+x+1 for x in range(d)] for y in range(d)]
m2 = [list(r) for r in zip(*m1[::-1])]

[print(r) for r in m1]
[print(r) for r in m2]

and...I don't think you want to pass the matrix, and d. You can get d from the matrix - passing in both you run the risk of a mismatch.