-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatrixLayerRotation.py
71 lines (52 loc) · 1.82 KB
/
MatrixLayerRotation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the matrixRotation function below.
def matrixRotation(matrix, r):
#get the dimensions of the matrix
m = len(matrix)
n = len(matrix[0])
#get the number of layers of the matrix we will rotate
layers = min(m,n)//2
for i in range(layers):
#calculate the number of elements in each layer
num_els = 2*(m-2*i)+2*(n-2*i)-4
#calculate the number of rotations needed for each layer
rots = r%num_els
#flatten the layer and place it in a list
layer = [matrix[x][i] for x in range(i,i+(m-2*i))]
layer += [matrix[i+(m-2*i)-1][x] for x in range(i+1,i+(n-2*i)-1)]
layer += [matrix[x][i+(n-2*i)-1] for x in range(i+(m-2*i)-1,i-1,-1)]
layer += [matrix[i][x] for x in range(i+(n-2*i)-2,i,-1)]
#easily r-left rotate the layer
layer = layer[num_els-rots:]+layer[:num_els-rots]
#place the rotated elements into the matrix
for x in range(i,i+(m-2*i)):
matrix[x][i] = layer[0]
layer = layer[1:]
for x in range(i+1,i+(n-2*i)-1):
matrix[i+(m-2*i)-1][x]=layer[0]
layer = layer[1:]
for x in range(i+(m-2*i)-1,i-1,-1):
matrix[x][i+(n-2*i)-1]=layer[0]
layer = layer[1:]
for x in range(i+(n-2*i)-2,i,-1):
matrix[i][x]=layer[0]
layer = layer[1:]
#print the matrix
for i in range(m):
for j in range(n):
print(matrix[i][j],end=' ')
print()
if __name__ == '__main__':
mnr = input().rstrip().split()
m = int(mnr[0])
n = int(mnr[1])
r = int(mnr[2])
matrix = []
for _ in range(m):
matrix.append(list(map(int, input().rstrip().split())))
matrixRotation(matrix, r)