[Python] 90 degree clockwise rotation, 90 degree counterclockwise rotation, 180 degree rotation of matrix [AtCoder]

I was impressed by how to rotate the matrix, so I will write it as an article. It is a mystery whether this knowledge can be applied in the future.

Rotate 90 degrees clockwise in the procession! !! ABC036B

I think there are many ways to solve it, but here is the code when I first got the AC. I wonder how to index with notebook and paper. I did it while thinking.

test.py


def I(): return int(input())
N = I()
S = [input() for _ in range(N)]
ans =[['']*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        ans[j][N-1-i] = S[i][j]
for x in ans:
    print(*x,sep='')

Look at the code of a strong person, ** surprised! Impressed! ** ** ** 90 degree clockwise rotation ① After turning it upside down ② Transpose What should I do! !! !! ** **

IMG_3975.JPG

Here is the code that refers to this idea! Since S is a two-dimensional array, you can turn it upside down withS [:: -1]! If you don't know about transpose, Googling with "transpose zip python" (not difficult)!

test.py


def I(): return int(input())
N = I()
S = [input() for _ in range(N)]
for x in zip(*S[::-1]):
    print(*x,sep='')

It became AC! ** Wow! ** **

90 degree left rotation of the procession! !!

The opposite of 90 degree clockwise rotation, ** 90 degree left rotation is ① Transpose, ② Upside down It looks good! !! !! ** **

test.py


def I(): return int(input())
N = I()
S = [input() for _ in range(N)]
T_S = list(zip(*S))
for x in T_S[::-1]:
    print(*x,sep='')

Is it really 90 degrees counterclockwise ...? スクリーンショット 2020-04-18 21.44.36.png

** It is 90 degrees counterclockwise! !! !! !! !! !! ** ** ** Wow! ** **

Rotate the procession 180 degrees! !! ABC004B

** 180 degree rotation, ① Reverse left and right ② Upside down It looks good! !! !! ** **

test.py


def LS(): return list(input().split())
A = [LS()[::-1] for _ in range(4)]
for x in A[::-1]:
    print(*x,sep=' ')

At the time of input, use LS () [:: -1] to reverse the left and right! It became AC! I was impressed~

end!

Recommended Posts

[Python] 90 degree clockwise rotation, 90 degree counterclockwise rotation, 180 degree rotation of matrix [AtCoder]
atCoder 173 Python
[Python] Solve 10 past elite problems of Atcoder
Comparison of matrix transpose speeds with Python
Matrix Convolution Filtering-Reinventor of Python Image Processing-
Affine transformation by matrix (scaling, rotation, shearing, movement) -Reinventor of Python image processing-
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
[Answer example (python3)] ABS (AtCoder Beginners Selection) of atcoder
Introduction of Python
AtCoder ABC 174 Python
AtCoder ABC187 Python
Basics of Python ①
AtCoder ABC188 Python
Basics of python ①
Copy of python
[Python] Matrix operation
AtCoder ABC 175 Python
Introduction of Python
Image processing by matrix Basics & Table of Contents-Reinventor of Python image processing-
Review of atcoder ABC158, up to question E (Python)