-
Notifications
You must be signed in to change notification settings - Fork 3
/
spiral.py
58 lines (41 loc) · 1.13 KB
/
spiral.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
# Python3 program to print
# given matrix in spiral form
def spiralPrint(m, n, a):
k = 0
l = 0
''' k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator '''
while (k < m and l < n):
# Print the first row from
# the remaining rows
for i in range(l, n):
print(a[k][i], end=" ")
k += 1
# Print the last column from
# the remaining columns
for i in range(k, m):
print(a[i][n - 1], end=" ")
n -= 1
# Print the last row from
# the remaining rows
if (k < m):
for i in range(n - 1, (l - 1), -1):
print(a[m - 1][i], end=" ")
m -= 1
# Print the first column from
# the remaining columns
if (l < n):
for i in range(m - 1, k - 1, -1):
print(a[i][l], end=" ")
l += 1
# Driver Code
a = [[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18]]
R = 3
C = 6
# Function Call
spiralPrint(R, C, a)