Matrix

Practicetrackerever
3 min readJun 8, 2022

--

Matrix is a 2-dimensional data structure formed by a collection of lists.

A matrix with m rows and n columns

When m is equal to n, it is called a square matrix. A square matrix can be flipped along the diagonal easily. Uisng the upper triangular matrix swapping elements with the lower triangular matrix, flipping along the diagonal is done.

Flip the given matrix along the diagonal.

  1. Check if the matrix is a square matrix or not.
  2. If the matrix is a square matrix, loop along with the matrix element-wise to swap the elements on the lower triangular matrix with the upper triangular matrix.
  3. This is an in-place method. Return the matrix itself.

CODE:

class Solution:
def solve(self, A):
#Steps 1.
if len(A) != len(A[0]):
return

#Steps 2.
for i in range(len(A)):
for j in range(i, len(A[0])):
A[i][j], A[j][i] = A[j][i], A[i][j]

#Steps 3.
return A

PRACTICE:

A few important traversals in matrix are possible like the spiral order of a matrix.

Find the spiral order array of the given matrix.

To find the spiral order array of the given matrix, the algorithm is:

  1. Initialize an answer array to store the matrix.
  2. Initialize the extreme points of the matrix. Left is 0. Right is the last column. Top is 0. Bottom is last row.
  3. Run a loop till left is less than or equal to right and top is less than or equal to bottom.
  4. First, check if left is less than or equal to right. If it is, run a loop from left to right(both included). Since the row is fixed(equal to top when traversing from left to right), append A(top, i) where i is every element and in order in [left, right]. Since the traversal of this row is done, increment top. Top needs to be incremented.
  5. Second, check if top is less than or equal to bottom. If it is, run a loop from top to bottom(both included). Since the column is fixed(equal to right when traversing from top to bottom), append A(i, right) where i is every element and in order in [top, bottom]. Since the traversal of this column is done, decrement right. Right needs to be decremented.
  6. Third, check if left is less than or equal to right. If it is, run a loop from right to left(both included). Since the row is fixed(equal to bottom when traversing from right to left), append A(bottom, i) where i is every element and in order in [right, left]. Since the traversal of this row is done, decrement bottom. Bottom needs to be decremented.
  7. Fourth, check if top is less than or equal to bottom. If it is, run a loop from bottom to top(both included). Since the column is fixed(equal to left when traversing from bottom to top), append A(i, left) where i is every element and in order in [bottom, top]. Since the traversal of this column is done, increment left. Left needs to be incremented.
  8. Return the answer once the loop is done.

CODE:

class Solution:
def spiralOrder(self, A):

#Steps 1.
ans = []
#Steps 2.
left = 0
right = len(A[0]) - 1
top = 0
bottom = len(A) - 1
#Steps 3.
while left <= right and top <= bottom:
#Steps 4.
if left <= right:
for i in range(left, right + 1):
ans.append(A[top][i])
top = top + 1

#Steps 5.
if top <= bottom:
for i in range(top, bottom + 1):
ans.append(A[i][right])
right = right - 1
#Steps 6.
if left <= right:
for i in range(right, left - 1, -1):
ans.append(A[bottom][i])
bottom = bottom - 1
#Steps 7.
if top <= bottom:
for i in range(bottom, top - 1, -1):
ans.append(A[i][left])
left = left + 1

#Steps 8.
return ans

PRACTICE:

--

--