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.
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! !! !! ** **
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! ** **
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 ...?
** It is 90 degrees counterclockwise! !! !! !! !! !! ** ** ** Wow! ** **
** 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