-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathMatrix_Layer_Rotation.py
49 lines (44 loc) · 1.39 KB
/
Matrix_Layer_Rotation.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
rows, cols, rotations = [int(x) for x in input().strip().split(" ")]
num_layers = int(min(rows, cols)/2)
layers = [[] for x in range(num_layers)]
data = []
for row in range(rows):
data.append([int(x) for x in input().strip().split(" ")])
rows -= 1
cols -= 1
for current_layer in range(num_layers):
i = j = current_layer
while i < rows-current_layer:
layers[current_layer].append(data[i][j])
i += 1
while j < cols-current_layer:
layers[current_layer].append(data[i][j])
j += 1
while i > current_layer:
layers[current_layer].append(data[i][j])
i -= 1
while j > current_layer:
layers[current_layer].append(data[i][j])
j -= 1
new_layers = []
for layer in layers:
rots = rotations%len(layer)
new_layers.append(list(layer[-rots:] + layer[:-rots]))
for current_layer in range(num_layers):
i = j = current_layer
while i < rows-current_layer:
data[i][j] = new_layers[current_layer].pop(0)
i += 1
while j < cols-current_layer:
data[i][j] = new_layers[current_layer].pop(0)
j += 1
while i > current_layer:
data[i][j] = new_layers[current_layer].pop(0)
i -= 1
while j > current_layer:
data[i][j] = new_layers[current_layer].pop(0)
j -= 1
for row in range(rows+1):
for col in range(cols+1):
print(data[row][col], end=" ")
print()