🟨 나의 풀이
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'matrixRotation' function below.
#
# The function accepts following parameters:
# 1. 2D_INTEGER_ARRAY matrix
# 2. INTEGER r
#
def matrixRotation(matrix, r):
# Write your code here
row = len(matrix)
col = len(matrix[0])
result = [[0] * col for _ in range(row)]
depth = 0
while row - depth > row / 2 and col - depth > col / 2 :
top = [(depth, i) for i in range(depth, col - depth)]
left = [(i, depth) for i in range(row - depth - 2, depth, -1)]
bottom = [(row - depth - 1, i) for i in range(col - depth - 2, depth - 1, -1)]
right = [(i, col - depth - 1) for i in range(depth + 1, row - depth)]
tmp = top + right + bottom + left
len_tmp = len(tmp)
for idx, val in enumerate(tmp) :
x, y = val
nx, ny = tmp[(idx + r) % len_tmp]
result[x][y] = matrix[nx][ny]
depth +=1
for x in range(row) :
print(*result[x])
if __name__ == '__main__':
first_multiple_input = input().rstrip().split()
m = int(first_multiple_input[0])
n = int(first_multiple_input[1])
r = int(first_multiple_input[2])
matrix = []
for _ in range(m):
matrix.append(list(map(int, input().rstrip().split())))
matrixRotation(matrix, r)
m*n 테이블에서 r칸씩 회전시킨다.
아래 코드에서 다음과 같은 점을 수정했다.
- 열의 길이 // 2만큼 반복 ➡️ 회전시킬 행, 열 길이가 있을 때까지로 변경함.
# 실패
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'matrixRotation' function below.
#
# The function accepts following parameters:
# 1. 2D_INTEGER_ARRAY matrix
# 2. INTEGER r
#
def matrixRotation(matrix, r):
# Write your code here
row = len(matrix)
col = len(matrix[0])
result = [[0] * col for _ in range(row)]
for depth in range(col//2) :
top = [(depth, i) for i in range(depth, col - depth)]
left = [(i, depth) for i in range(row - depth - 2, depth, -1)]
bottom = [(row - depth - 1, i) for i in range(col - depth - 2, depth - 1, -1)]
right = [(i, col - depth - 1) for i in range(depth + 1, row - depth)]
tmp = top + right + bottom + left
len_tmp = len(tmp)
for idx, val in enumerate(tmp) :
x, y = val
nx, ny = tmp[(idx + r) % len_tmp]
result[x][y] = matrix[nx][ny]
for x in range(row) :
print(*result[x])
if __name__ == '__main__':
first_multiple_input = input().rstrip().split()
m = int(first_multiple_input[0])
n = int(first_multiple_input[1])
r = int(first_multiple_input[2])
matrix = []
for _ in range(m):
matrix.append(list(map(int, input().rstrip().split())))
matrixRotation(matrix, r)
4개가 틀린다. 😂
🔔 다른 사람 풀이
# https://velog.io/@leejh3224/Hackerrank-Matrix-Layer-Rotation#%ED%86%B5%EA%B3%BC
def printMatrix(mat) :
for row in mat :
for elem in row :
print(elem, end = ' ')
print()
def rotateMatrix(matrix, r) :
m = len(matrix)
n = len(matrix[0])
offset = 0
while n - offset > n / 2 and m - offset > m / 2 :
top = [(offset, i) for i in range(offset, n - offset)]
right = [(i, n - 1 - offset) for i in range(offset + 1, m - 1 - offset)]
bottom = [(m - 1 - offset, i) for i in range(n - 1 - offset, offset - 1, -1)]
left = [(i, offset) for i in range(m - offset - 2, offset, -1)]
before_rotation = top + right + bottom + left
circle = [matrix[x][y] for x, y in before_rotation]
rMod = r % len(circle)
after_rotation = circle[rMod : ] + circle[0 : rMod]
for i in range(len(before_rotation)) :
x, y = before_rotation[i]
matrix[x][y] = after_rotation[i]
offset += 1
printMatrix(matrix)
나랑 비슷하지만 다른 점은 슬라이싱을 이용한 것이다.
문제 출처 👇👇
https://www.hackerrank.com/challenges/matrix-rotation-algo/problem?isFullScreen=true
반응형
'coding test > HackerRank' 카테고리의 다른 글
[HackerRank] Array Manipulation (0) | 2023.05.19 |
---|---|
[HackerRank] Insertion Sort Advanced Analysis (0) | 2023.05.18 |
[HackerRank] Queen's Attack 2 (0) | 2023.05.17 |
[HackerRank] Non-Divisible Subset (0) | 2023.05.16 |
[HackerRank] Climbing the Leaderboard (0) | 2023.05.15 |