Write a program of spiral pattern printing in python language
Write a program of spiral pattern printing in python language. There are you will learn how to print the spiral pattern of a matrix.
Take an example to print the spiral pattern through a python program:
# Write a program of a spiral pattern
# printing in python language
def spiralPrint(m, n, arru):
# m is the rows
# n is the columns
# arru is the list of elements
k, l = 0, 0
while k < m and l < n:
pass
# This will print the first row from the remaining rows
for i in range (l, n):
print (arru[k][i], end=", ")
k += 1
# This will print the last column from the remaining columns
for i in range (k, m):
print (arru[i][n - 1], end=", ")
n -= 1
# This will print the last row from the remaining rows
if k < m:
for i in range (n - 1, l - 1, -1):
print (arru[m - 1][i], end=", ")
m -= 1
# This will print the first column from the remaining columns
if l < n:
for i in range (m - 1, k - 1, -1):
print (arru[i][l], end=", ")
l += 1
print (end="\n")
arru = [
[31, 32, 33, 34, 35, 36],
[47, 48, 49, 40, 41, 42],
[53, 54, 55, 56, 57, 58]
]
print ("-----The spiral pattern is-----")
# This will print the spiral pattern
spiralPrint(3, 6, arru)
Output:
-----The spiral pattern is-----
31, 32, 33, 34, 35, 36, 42, 58, 57, 56, 55, 54, 53, 47, 48, 49, 40, 41,
Tags:
# python program of a spiral matrix from center
# python program to generate a spiral matrix
# python program of reverse spiral matrix
# python program of a print matrix in a waveform