Skip to content

Instantly share code, notes, and snippets.

@inspirit941
Created July 12, 2025 14:36
Show Gist options
  • Save inspirit941/c84104fe67f5abccf97a88a131e85e21 to your computer and use it in GitHub Desktop.
Save inspirit941/c84104fe67f5abccf97a88a131e85e21 to your computer and use it in GitHub Desktop.
#!/bin/python3
import math
import os
import random
import re
import sys
from collections import deque
#
# Complete the 'matrixRotation' function below.
#
# The function accepts following parameters:
# 1. 2D_INTEGER_ARRAY matrix
# 2. INTEGER r
#
def serialize(matrix, start_y, end_y, start_x,end_x):
# first
restore_idx = {}
result = []
first = [matrix[start_y][x] for x in range(start_x, end_x)]
result.extend(first)
restore_idx[(start_y, start_x)] = (0, len(result)-1)
# second
second = [matrix[y][end_x-1] for y in range(start_y, end_y)][1:]
result.extend(second)
restore_idx[(start_y, end_x)] = restore_idx[(start_y, start_x)][1], len(result)-1
# third
third = list(reversed([matrix[end_y-1][x] for x in range(start_x, end_x)]))[1:]
result.extend(third)
restore_idx[(end_y, end_x)] = restore_idx[(start_y, end_x)][1], len(result)-1
# forth
forth = list(reversed([matrix[y][start_x] for y in range(start_y, end_y)]))[1:-1]
result.extend(forth)
restore_idx[(end_y, start_x)] = restore_idx[(end_y, end_x)][1], len(result)-1
return result, restore_idx
def deserialize(matrix, array, restore_idx, start_y, end_y, start_x, end_x):
# first
start, end = restore_idx[(start_y, start_x)]
data = array[start:end+1]
# print("first", data)
for idx, x in enumerate(range(start_x, end_x)):
matrix[start_y][x] = data[idx]
# second
start, end = restore_idx[(start_y, end_x)]
data = array[start:end+1]
# print("second", data)
for idx, y in enumerate(range(start_y, end_y)):
matrix[y][end_x-1] = data[idx]
# third
start, end = restore_idx[(end_y, end_x)]
data = list(reversed(array[start:end+1]))
# print("third", data)
for idx, x in enumerate(range(start_x, end_x)):
matrix[end_y-1][x] = data[idx]
# forth
start, end = restore_idx[(end_y, start_x)]
data = array[start:end+1]
data.append(array[0])
data = list(reversed(data))
# print("forth", data)
for idx, y in enumerate(range(start_y, end_y)):
matrix[y][start_x] = data[idx]
return matrix
def matrixRotation(matrix, r):
# Write your code here
# 7->5->3...
answer = [[0 for _ in range(len(matrix[0]))] for _ in range(len(matrix))]
start_y, start_x = 0, 0
end_y, end_x = len(matrix), len(matrix[0])
while start_y < end_y and start_x < end_x:
array, restore_idx = serialize(matrix, start_y, end_y, start_x, end_x)
toDeque = deque(array)
# print(toDeque)
toDeque.rotate(-r)
# print(toDeque)
# print(restore_idx)
toArray = list(toDeque)
answer = deserialize(answer, toArray, restore_idx, start_y, end_y, start_x, end_x)
# print(answer)
start_y += 1
start_x += 1
end_y -=1
end_x -=1
for row in answer:
print(" ".join([str(a) for a in row]))
# arr = []
# # first
# matrix[start_y][:]
# # second
# [matrix[end_y][x] for x in range(start_x, end_x)]
# # third
# list(reversed(matrix[end_y][:]))
# # forth
# list(reversed([matrix[start_y][x] for x in range(start_x, end_x)]))
# # for i, y in enumerate(range(len(matrix))):
# # arr.extend(matrix[y])
# # matrix[y][len(matrix)-i]
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment