-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiralTraversal.py
More file actions
81 lines (62 loc) · 1.81 KB
/
spiralTraversal.py
File metadata and controls
81 lines (62 loc) · 1.81 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
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 1 15:59:48 2020
Code to traverse a matrix in spiral fashion
@author: Nikhil Bhargava
"""
import turtle
turtle.bgcolor("white")
seurat=turtle.Turtle()
#For plotting of dots on screen
width=15
height=17
dot_distance=40
turtle.done()
seurat.setpos(250, 250)
def spiral(m,n):
#set color of turtle as white
seurat.color("red")
#starting index of row and col resp.
k=0
l=0
seurat.forward(dot_distance)
flag=0
while(k<n and l<n):
if(flag==1):
seurat.right(90)
# printing the 1st row from remaining row
for i in range(l,n):
#print(A[k][i],end=" ")
seurat.forward(dot_distance)
# Move to next row and set flag to done
k=k+1
flag=1
#Turn turtel to right
seurat.right(90)
#Print the lst col from relamining col
for i in range(k,m):
#print(A[i][n-1],end=" ")
seurat.forward(dot_distance)
# Move to the second last col
n=n-1
#turn turtle to right
seurat.right(90)
if(k<m):
#Printing the last row from remaining rows
for i in range ((n-1),(l-1),-1):
#print(A[n-1][i],end=" ")
seurat.forward(dot_distance)
m=m-1
#turn turtle to right
seurat.right(90)
if(l<n):
#printing the first col from remaining col
for i in range(m-1,k-1,-1):
#print(A[i][l],end=" ")
seurat.forward(dot_distance)
l=l+1
#Test Code
m=20
n=20
spiral(m,n)
turtle.done()