Program 10. User Defined Module 2 Aim
Program 10. User Defined Module 2 Aim
PROGRAM CODING
MATRIX.PY
#module matrix
def swap(r,c,matrix):
'''wap() to take as parameter a nested list and
swaps the left and right diagonal elements'''
for i in range(r):
matrix[i][i],matrix[i][-(i+1)]=matrix[i][-(i+1)],matrix[i][i]
def sortrowcol(r,c,matrix):
'''sortrowcol() – takes as parameter a nested list
of integers and sorts the rows using bubble sort
in ascending order and sorts columns using
Insertion sort in ascending order.
This function to be tested with a nested list of integers.'''
print("1.Sort rowwise\n2.sort cloumnwise")
ch1=int(input("enter your choice"))
if ch1==1: bubble_r(r,c,matrix)
else: insertion_c(r,c,matrix)
def bubble_r(r,c,matrix):
'''
bubble_r() – takes as parameter a nested list
of integers and sorts the rows using bubble sort'''
for i in range(r):
m=matrix[i]
for p in range(len(m)):
for j in range(len(m)-1-p):
if m[j]>m[j+1]:
m[j],m[j+1]=m[j+1],m[j]
def insertion_c(r,c,matrix):
'''insertion_c() – takes as parameter a nested list
of integers sorts columns using
Insertion sort in ascending order.'''
mat=[]
for i in range(c):
col=[]
#to store column values
for p in range(r):
col+=[matrix[p][i]]
#insertion sort for the column
for p in range(1,len(col)):
temp=col[p]
j=p-1
while temp<=col[j] and j>=0:
col[j+1]=col[j]
j-=1
col[j+1]=temp
mat+=[col]
#to change col to row
for i in range(len(mat)):
for j in range(len(mat[i])):
matrix[j][i]=mat[i][j]
#PROGRAM10.PY
import MATRIX
def inputmatrix(r,c,matrix):
print("Original matrix")
for i in range(r):
for j in range(c):
print(matrix[i][j],end=' ')
print()
print("******************************")
def outputmatrix(r,c,matrix):
print("Updated Matrix")
for i in range(r):
for j in range(c):
print(matrix[i][j],end=' ')
print()
#main prg
matrix=[]
r=int(input("enter the length of nested list:"))
c=r
for i in range(r):
a=[]
for j in range(c):
x=int(input("enter the number"))
a+=[x]
matrix+=[a]
print("MENU\n 1.swap the left and right diagonal")
print("2.sort the elements rowwise/columnwise")
print("3.Exit")
while True:
ch=int(input("enter choice:"))
if ch==1:
inputmatrix(r,c,matrix)
MATRIX.swap(r,c,matrix)
outputmatrix(r,c,matrix)
elif ch==2:
inputmatrix(r,c,matrix)
MATRIX.sortrowcol(r,c,matrix)
outputmatrix(r,c,matrix)
elif ch==3:
break
else:
print("wrong choice")
OUTPUT
enter the length of nested list:3
enter the number1
enter the number2
enter the number3
enter the number4
enter the number5
enter the number6
enter the number7
enter the number8
enter the number9
MENU
1.swap the left and right diagonal
2.sort the elements rowwise/columnwise
3.Exit
enter choice:1
Original matrix
123
456
789
******************************
Updated Matrix
3 2 1
4 5 6
9 8 7
Original matrix
321
456
987
******************************
1.Sort rowwise
2.sort cloumnwise
enter your choice1
Updated Matrix
1 2 3
4 5 6
7 8 9
enter choice:2
Original matrix
123
456
789
******************************
1.Sort rowwise
2.sort cloumnwise
enter your choice2
Updated Matrix
1 2 3
4 5 6
7 8 9
enter choice:3
>>>