140. Python Program to Transverse the given matrix helically.
How does this program work?
- In this program we are going to learn about how to transverse the given matrix helically using Python.
- Elements are stored in array variable.
Here is the code
a=[]
n=int(input("Enter the number of rows in the matrix: "))
m=int(input("Enter the number of columnn in the matrix: "))
print("Enter the elements: ")
for i in range(n):
row=[]
for j in range(m):
row.append(int(input()))
a.append(row)
print(a)
print("Display an array In Matrix Form: ")
for i in range(n):
for j in range(m):
print(a[i][j], end=" ")
print()
k = 0
l = 0
print("Traversal of a matrix ")
while (k < n and l < m) :
for i in range(l, m) :
print(a[k][i], end = " ")
k += 1
for i in range(k, n) :
print(a[i][m - 1], end = " ")
m -= 1
if ( k < n) :
for i in range(m - 1, (l - 1), -1) :
print(a[n - 1][i], end = " ")
n -= 1
if (l < m) :
for i in range(n - 1, k - 1, -1) :
print(a[i][l], end = " ")
l += 1