-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiral traversing.py
More file actions
69 lines (49 loc) · 1.24 KB
/
spiral traversing.py
File metadata and controls
69 lines (49 loc) · 1.24 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
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
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 9 14:43:40 2021
@author: DELL
"""
def spiralPrint(m, n, a):
'''
Parameters
----------
m : number of rows
n : number of columns
a : list
01 02 03 04
05 06 07 08
09 10 11 12
13 14 15 16
Returns
-------
None.
'''
k=0 #k=starting index of rows
l=0 #l=starting index of columns
while (k<m and l<n):
# printing the first row fromteh remaining rows
for i in range(l, n):
print(a[k][i], end=" ")
k+=1
# printing the last full column from the remaining columns
for i in range(k,m):
print(a[i][n-1],end=" ")
n-=1
# Printing the last row from remaining rows
if k<m:
for i in range(n-1, l-1, -1):
print(a[m-1][i], end=" ")
m-=1
#printing the first column from the remaining columns
for i in range(m-1, k-1, -1):
print(a[i][l], end=" ")
l+=1
a=[]
count=1
for i in range(3):
l=[]
for j in range(6):
l.append(count)
count+=1
a.append(l)
spiralPrint(3, 6,a)