Hi all , today i just saw a problem of printing the elements of a matrix in a spiral so spent few time in solving it ..
I did it somehow , here is the solution coded in python.
I did it somehow , here is the solution coded in python.
a=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20]]
size = 4
n1=5
m=0
n=size
i1=0
i=0
while (i<size):
for j in range(m,n):
print a[i][j] ,
i=i+1
m=m+1
if(i<size):
for i1 in range(m,n1):
print a[i1][j],
n=n-1
n1=n1-1
#print "range is "+str(range(j-1,m-2,-1))
for j in range(n-1,m-2,-1):
print a[i1][j],
for i1 in range(n1-1,m-1,-1):
print a[i1][j],
No comments:
Post a Comment