@@ -9,20 +9,21 @@ class Solution:
9
9
def printMatrix (self , matrix ):
10
10
# write code here
11
11
size = len (matrix )
12
- size = int (np .sqrt (size ))
13
- matrix = np .reshape (np .array (matrix ), newshape = (size , size ))
14
12
15
13
newArray = []
14
+ if size == 1 :
15
+ newArray .append (matrix [0 ][0 ])
16
+ return newArray
16
17
17
- for index1 in range (0 , int ( size / 2 ) ):
18
+ for index1 in range (0 , size ):
18
19
for index2 in range (index1 , size - index1 - 1 ):
19
- newArray .append (matrix [index1 , index2 ])
20
+ newArray .append (matrix [index1 ][ index2 ])
20
21
for index3 in range (index1 , size - index1 - 1 ):
21
- newArray .append (matrix [index3 , size - index1 - 1 ])
22
+ newArray .append (matrix [index3 ][ size - index1 - 1 ])
22
23
for index4 in range (size - index1 - 1 , index1 , - 1 ):
23
- newArray .append (matrix [size - index1 - 1 , index4 ])
24
+ newArray .append (matrix [size - index1 - 1 ][ index4 ])
24
25
for index5 in range (size - index1 - 1 , index1 , - 1 ):
25
- newArray .append (matrix [index5 , index1 ])
26
+ newArray .append (matrix [index5 ][ index1 ])
26
27
27
28
return newArray
28
29
@@ -32,15 +33,14 @@ def reversematrix(self,matrix):
32
33
for i in range (s [1 ]- 1 ,- 1 ,- 1 ):
33
34
a = []
34
35
for j in range (s [0 ]):
35
- a .append (matrix [j , i ])
36
+ a .append (matrix [j ][ i ])
36
37
newmatrix .append (a )
37
38
newmatrix = np .array (newmatrix )
38
39
return newmatrix
39
40
40
41
def printMatrix2 (self , matrix ):
41
42
newArray = []
42
43
matrix = np .array (matrix )
43
- print matrix
44
44
while len (matrix )!= 0 :
45
45
newArray .extend (matrix [0 ])
46
46
matrix = matrix [1 :]
@@ -49,4 +49,5 @@ def printMatrix2(self, matrix):
49
49
50
50
51
51
s = Solution ()
52
- s .printMatrix2 ([[1 , 2 , 3 , 4 ], [5 , 6 , 7 , 8 ], [9 , 10 , 11 , 12 ], [13 , 14 , 15 , 16 ]])
52
+ #print s.printMatrix([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])
53
+ print s .printMatrix ([[1 ]])
0 commit comments