
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to print matrix elements in spiral order in python
Suppose we have a 2D matrix mat. We have to print the matrix elements in a spiral way. At first starting from the first row (mat[0, 0]), print the whole content and then follow the last column to print, then the last row, and so on, thus it prints the elements in a spiral fashion.
So, if the input is like
7 | 10 | 9 |
2 | 9 | 1 |
6 | 2 | 3 |
9 | 1 | 4 |
2 | 7 | 5 |
9 | 9 | 11 |
then the output will be [7, 10, 9, 1, 3, 4, 5, 11, 9, 9, 2, 9, 6, 2, 9, 2, 1, 7]
To solve this, we will follow these steps:
- d := 0
- top := 0, down := row count of matrix – 1, left := 0, right := column count of matrix - 1
- c := 0
- res := a new list
- direction := 0
- while top <= down and left <= right, do
- if direction is same as 0, then
- for i in range left to right + 1, do
- insert matrix[top, i] into res
- top := top + 1
- for i in range left to right + 1, do
- if direction is same as 1, then
- for i in range top to down + 1, do
- insert matrix[i, right] into res
- right := right - 1
- for i in range top to down + 1, do
- if direction is same as 2, then
- for i in range right to left - 1, decrease by 1, do
- insert matrix[down, i] into res
- down := down - 1
- for i in range right to left - 1, decrease by 1, do
- if direction is same as 3, then
- for i in range down to top - 1, decrease by 1, do
- insert matrix[i, left] into res
- left := left + 1
- for i in range down to top - 1, decrease by 1, do
- if direction is same as 0, then
- direction :=(direction + 1) mod 4
- return res
Let us see the following implementation to get better understanding:
Example
class Solution: def solve(self, matrix): d = 0 top = 0 down = len(matrix) - 1 left = 0 right = len(matrix[0]) - 1 c = 0 res = [] direction = 0 while top <= down and left <= right: if direction == 0: for i in range(left, right + 1): res.append(matrix[top][i]) top += 1 if direction == 1: for i in range(top, down + 1): res.append(matrix[i][right]) right -= 1 if direction == 2: for i in range(right, left - 1, -1): res.append(matrix[down][i]) down -= 1 if direction == 3: for i in range(down, top - 1, -1): res.append(matrix[i][left]) left += 1 direction = (direction + 1) % 4 return res ob = Solution() matrix = [ [7, 10, 9], [2, 9, 1], [6, 2, 3], [9, 1, 4], [2, 7, 5], [9, 9, 11] ] print(ob.solve(matrix))
Input
[ [7, 10, 9],[2, 9, 1],
[6, 2, 3],[9, 1, 4],
[2, 7, 5],[9, 9, 11]
]
Output
[7, 10, 9, 1, 3, 4, 5, 11, 9, 9, 2, 9, 6, 2, 9, 2, 1, 7]
- Related Articles
- Java program to print a given matrix in Spiral Form.
- Print Matrix in spiral way\n
- How to print a matrix of size n*n in spiral order using C#?
- Spiral Matrix II in Python
- Python program to print the elements of an array in reverse order
- To print all elements in sorted order from row and column wise sorted matrix in Python
- Print a given matrix in reverse spiral form in C++
- Swift program to print spiral pattern
- Print n x n spiral matrix using O(1) extra space in C Program.
- Print a given matrix in counter-clockwise spiral form in C++
- Python Program to Print Matrix in Z form
- Spiral Matrix in C++
- Java Program to Print Spiral Pattern of Numbers
- Golang Program to Print Spiral Pattern of Numbers
- Python program to print matrix in a snake pattern

Advertisements