Python Online Compiler
Example: Print Spiral Pattern in Python language
C
C++
C#
Java
Python
PHP
main.py
STDIN
Run
# Print Spiral Pattern 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
Clear
ADVERTISEMENTS