-
Notifications
You must be signed in to change notification settings - Fork 0
/
que1.py
69 lines (61 loc) · 1.75 KB
/
que1.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
# rotate matrix anti-clockwise r times
import array
def rotateMatrix(m: int, n: int, r: int, array: array):
temp: int = array[0][0]
dept: int = 0
pos: int = 0
while r != 0:
while dept < m - 1:
val = temp
temp = array[dept + 1][pos]
array[dept + 1][pos] = val
dept += 1
while pos < n - 1:
val = temp
temp = array[dept][pos + 1]
array[dept][pos + 1] = val
pos += 1
while dept > 0:
val = temp
temp = array[dept - 1][pos]
array[dept - 1][pos] = val
dept -= 1
while pos > 0:
val = temp
temp = array[dept][pos - 1]
array[dept][pos - 1] = val
pos -= 1
rotateInnerMatrix(m=m - 1, n=n - 1, array=array)
r -= 1
def rotateInnerMatrix(m: int, n: int, array: array):
temp: int = array[1][1]
dept: int = 1
pos: int = 1
while(True):
while dept < m - 1:
val = temp
temp = array[dept + 1][pos]
array[dept + 1][pos] = val
dept += 1
while pos < n - 1:
val = temp
temp = array[dept][pos + 1]
array[dept][pos + 1] = val
pos += 1
while dept > 1:
val = temp
temp = array[dept - 1][pos]
array[dept - 1][pos] = val
dept -= 1
while pos > 1:
val = temp
temp = array[dept][pos - 1]
array[dept][pos - 1] = val
pos -= 1
break
_2Darray: array = [[1, 2, 3, 9],
[4, 5, 6, 4],
[7, 8, 9, 3],
[10, 11, 12, 2]]
rotateMatrix(4, 4, 1, _2Darray)
print(_2Darray)