coding test/HackerRank

[HackerRank] Matrix Layer Rotation

잔망루피 2023. 5. 18. 18:24
반응형

🟨 나의 풀이

#!/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칸씩 회전시킨다.

아래 코드에서 다음과 같은 점을 수정했다.

  1. 열의 길이 // 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 

 

Matrix Layer Rotation | HackerRank

Rotate the matrix R times and print the resultant matrix.

www.hackerrank.com

 

반응형