- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPrintMatrixSpiral.py
More file actions
Latest commit
40 lines (37 loc) · 1.12 KB
/
Copy pathPrintMatrixSpiral.py
File metadata and controls
40 lines (37 loc) · 1.12 KB
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
defPrintMatrixSpiral(matrix):
'''
given a matrix, print out in a spiral form
Args: matrix: two dimensional array
'''
rowNum=len(matrix) -1
columnNum=len(matrix[0]) -1
startRow=0
startColumn=0
whilestartRow<rowNumandstartColumn<columnNum:
# print the first row for the remaining column
ifstartColumn<columnNum:
forcolinrange(startColumn, columnNum+1):
printmatrix[startRow][col]
startRow+=1
# print the last column for the remaining rows
ifstartRow<rowNum:
forrowinrange(startRow, rowNum+1):
printmatrix[row][columnNum]
columnNum-=1
# print the last row in reverse order
ifstartColumn<columnNum:
forcolinrange(columnNum, startColumn-1, -1):
printmatrix[rowNum][col]
rowNum-=1
# print the first column in reverse order
ifstartRow<rowNum:
forrowinrange(rowNum, startRow-1, -1):
printmatrix[row][startColumn]
startColumn+=1
matrix= [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]
]
PrintMatrixSpiral(matrix)