Saturday, December 28, 2024
Google search engine
HomeLanguagesPython Program to Interchange elements of first and last rows in matrix

Python Program to Interchange elements of first and last rows in matrix

Given a 4 x 4 matrix, we have to interchange the elements of first and last row and show the resulting matrix.

Examples : 

Input : 3 4 5 0
        2 6 1 2
        2 7 1 2
        2 1 1 2
Output : 2 1 1 2
         2 6 1 2
         2 7 1 2
         3 4 5 0

Input : 9 7 5 1
        2 3 4 1
        5 6 6 5
        1 2 3 1
Output : 1 2 3 1
         2 3 4 1
         5 6 6 5
         9 7 5 1

Method 1:
The approach is very simple, we can simply swap the elements of first and last row of the matrix inorder to get the desired matrix as output.

Below is the implementation of the approach : 

Python3




# Python code to swap the element
# of first and last row and display
# the result
 
 
def interchangeFirstLast(mat, n, m):
    rows = n
 
    # swapping of element between
    # first and last rows
    for i in range(n):
        t = mat[0][i]
        mat[0][i] = mat[rows-1][i]
        mat[rows-1][i] = t
 
 
# Driver Program
mat = [[8, 9, 7, 6],
       [4, 7, 6, 5],
       [3, 2, 1, 8],
       [9, 9, 7, 7]]
 
n = 4
m = 4
interchangeFirstLast(mat, n, m)
 
# printing the interchanged matrix
for i in range(n):
    for j in range(m):
        print(mat[i][j], end=" ")
    print("")


Output

9 9 7 7 
4 7 6 5 
3 2 1 8 
8 9 7 6 

Time Complexity: O(N)
Auxiliary Space: O(1), as we are not using any extra space.

Method 2: Without any Loop

We can achieve this by swapping the first and last list of the list of lists(matrix) in python

Python3




# Python code to swap the element
# of first and last row and display
# the result
 
# Driver Program
mat = [[8, 9, 7, 6],
       [4, 7, 6, 5],
       [3, 2, 1, 8],
       [9, 9, 7, 7]]
 
# Swapping first and last row
mat[0], mat[-1] = mat[-1], mat[0]
 
n = 4
m = 4
 
# printing the interchanged matrix
for i in range(n):
    for j in range(m):
        print(mat[i][j], end=" ")
    print()


Output

9 9 7 7 
4 7 6 5 
3 2 1 8 
8 9 7 6 

Time Complexity: O(n*m), where n is the number of rows and m is the number of columns in the matrix. This is because the time complexity of the code is dependent on the size of the matrix, and the code needs to traverse each element in the matrix.
Auxiliary Space: O(1), as the code only swaps the values of two rows and does not use any additional space.

Method 3 : Using pop(),insert() and append() methods

Python3




# Python code to swap the element
# of first and last row and display
# the result
 
mat = [[8, 9, 7, 6],
       [4, 7, 6, 5],
       [3, 2, 1, 8],
       [9, 9, 7, 7]]
x = mat[-1]
y = mat[0]
mat.pop()
mat.pop(0)
mat.insert(0, x)
mat.append(y)
n = 4
m = 4
# printing the interchanged matrix
for i in range(n):
    for j in range(m):
        print(mat[i][j], end=" ")
    print("")


Output

9 9 7 7 
4 7 6 5 
3 2 1 8 
8 9 7 6 

Method 4: Using XOR operation

Step-by-step approach:

  • Initialize the matrix.
  • XOR the first and last rows of the matrix using a loop and XOR operation.
  • Print the resulting matrix.

Below is the implementation of the above approach:

Python3




# initialize the matrix
mat = [[8, 9, 7, 6],
       [4, 7, 6, 5],
       [3, 2, 1, 8],
       [9, 9, 7, 7]]
 
# XOR the first and last rows
# loop through each element in the row
for i in range(len(mat[0])):
    # XOR the corresponding elements in the first and last rows
    mat[0][i] = mat[0][i] ^ mat[-1][i]
    mat[-1][i] = mat[0][i] ^ mat[-1][i]
    mat[0][i] = mat[0][i] ^ mat[-1][i]
 
# print the matrix
for row in mat:
    # join each element in the row with a space
    # and print the resulting string
    print(' '.join(str(x) for x in row))


Output

9 9 7 7
4 7 6 5
3 2 1 8
8 9 7 6

Time complexity: O(n), where n is the number of elements in the matrix
Auxiliary space: O(1)

Please refer complete article on Interchange elements of first and last rows in matrix for more details!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments