Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to exchange columns of a matrix in python

I am trying to exchange the columns of a matrix in Python the column with the smallest prime with the column with the bigger prime of the given matrix

I was trying

def swapcol(matriz,col1,col2):
    for i in range(len(matriz)):
        aux = matriz[i][col1]
        matriz[i][col1] = matriz[i][col2]
        matriz[i][col2] = aux
    


def esprimo(n):
    m = 0
    for i in range(1,n+1):
        if n % i == 0:
            m += 1
    if m == 2:
        return True
    else:
        return False

def leermatrix(matrix,r,c):
    for i in range(r):
        a = []
        for j in range(c):
            a.append(int(input("enter element: ")))
        matrix.append(a)

r = int(input("enter the number of rows: "))
c = int(input("enter the number of columns: "))
matrix = []


leermatrix(matrix,r,c)

mayp = matrix[0][0]
menp = matrix[0][0]
colmen = 0
colmay = 0

for j in range(c):
    for i in range(r):
        if esprimo(matrix[i][j]) and matrix[i][j] > mayp:
            mayp = matrix[i][j]
            colmay = j
        if esprimo(matrix[i][j]) and esprimo(matrix[i][j]) < menp:
            menp = matrix[i][j]
            colmen = j



swapcol(matrix,colmay,colmen)
print(matrix)


the output I get is the same matrix that I put, what did I get wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I made a few modifications to your code.

  • The condition esprimo(matrix[i][j]) and matrix[i][j] > mayp: should be replaced with matrix[i][j] > mayp
  • The condition esprimo(matrix[i][j]) and esprimo(matrix[i][j]) < menp should be replaced with matrix[i][j] < menp and matrix[i][j] > 1
  • Initialized mayp with -1 and menp with positive infinity to ensure they are updated correctly.
  • Also there’s no need to swap columns if the colmay and colmen values are the same

Here’s your solution:

def swapcol(matriz, col1, col2):
    for i in range(len(matriz)):
        aux = matriz[i][col1]
        matriz[i][col1] = matriz[i][col2]
        matriz[i][col2] = aux
    return matriz

def esprimo(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

def leermatrix(matrix, r, c):
    for i in range(r):
        a = []
        for j in range(c):
            a.append(int(input("Enter element: ")))
        matrix.append(a)

r = int(input("Enter the number of rows: "))
c = int(input("Enter the number of columns: "))
matrix = []

leermatrix(matrix, r, c)

print("Original matrix:")
for row in matrix:
    print(row)


mayp = -1
menp = float('inf')
colmen = 0
colmay = 0

for j in range(c):
    for i in range(r):
        if esprimo(matrix[i][j]):
            if matrix[i][j] > mayp:
                mayp = matrix[i][j]
                colmay = j
            if matrix[i][j] < menp and matrix[i][j] > 1:
                menp = matrix[i][j]
                colmen = j

if colmay != colmen:
    matriz = swapcol(matrix, colmay, colmen)

print("\nMatrix after swapping columns:")
for row in matriz:
    print(row)

Output:

Original matrix:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

Matrix after swapping columns:
[2, 1, 3]
[5, 4, 6]
[8, 7, 9]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading