Professional Documents
Culture Documents
PYTHON LAB Exp-2 22
PYTHON LAB Exp-2 22
0905CS191008
Abhishek kumar
CS-506
CSE-A(B1)
Experiment-2
Source Code:-
for j in range( i + 1, n ):
if itemsList[j] < itemsList[minValueIndex] :
minValueIndex = j
if minValueIndex != i :
temp = itemsList[i]
itemsList[i] = itemsList[minValueIndex]
itemsList[minValueIndex] = temp
return itemsList
el = [21,6,9,33,3]
print(selectionSort(el))
Output:-
2) WAP in python to perform insertion sort Algorithm.
Source Code:-
def insertionSort(arr):
key = arr[i]
j = i-1
while j >=0 and key < arr[j] :
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
Output:-
3) WAP in python to perform merge sort algorithm.
Source Code:-
i = 0
j = 0
k = l
m = l+(r-l)//2
mergeSort(arr, l, m)
mergeSort(arr, m+1, r)
merge(arr, l, m, r)
mergeSort(arr, 0, n-1)
print("\n\nSorted array is")
for i in range(n):
print("%d" % arr[i]),
Output:-
4) WAP to find First N Prime Numbers.
Source Code:-
numr=int(input("Enter range:"))
for n in range(1,numr):
for i in range(2,n):
if(n%i==0):
break
else:
print(n,end=' ')
Output:-
5) WAP to find Multiplication of Two Matrices.
Source Code:-
A = [[12, 7, 3],
[4, 5, 6],
[7, 8, 9]]
B = [[5, 8, 1, 2],
[6, 7, 3, 0],
[4, 5, 9, 1]]
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
result[i][j] += A[i][k] * B[k][j]
for r in result:
print(r)
Output:-
6) WAP in python to take input as Command Line Arguments.
Source Code:-
import sys
n = len(sys.argv)
print("Total arguments passed:", n)
Sum = 0
for i in range(1, n):
Sum += int(sys.argv[i])
print("\n\nResult:", Sum)
Output:-
7) WAP in Python to read a file and find Most Frequent word from file.
Source Code:-
file = open("gfg.txt","r")
frequent_word = ""
frequency = 0
words = []
for w in line_word:
words.append(w);
count = 1;