PYTHON PROJECT agya full and final
PYTHON PROJECT agya full and final
OUTPUT :-
EXPERIMENT (6)
To write a python program to perform binary search
def binarySearch(array, x, low, high):
while low <= high:
mid = low + (high - low)//2
if x == array[mid]:
return mid
elif x > array[mid]:
low = mid + 1
else:
high = mid - 1
return -1
array = [3, 4, 5, 6, 7, 8, 9]
x = 4
result = binarySearch(array, x, 0,len(array)-1)
if result != -1:
print("Element is present at index " + str(result))
else:
print("Not found")
EXPERIMENT (7)
To write a python program to perform selection sort
def selectionSort(array, size) for step in range(size):
min_idx = step
for i in range(step + 1, size):
if array[i] < array[min_idx]:
min_idx = i
(array[step], array[min_idx]) = (array[min_idx], array[step])
data = [-2, 45, 0, 11, -9]
size = len(data) selectionSort(data, size)
print('Sorted Array in Ascending Order:')
print(data)
EXPERIMENT (8)
To write a python program to perform insertion sort
def insertionSort(array):
for step in range(1, len(array)): key = array[step]
j = step - 1
while j >= 0 and key < array[j]: array[j+ 1] = array[j]
j = j - 1 array[j + 1] = key data = [9, 5, 1, 4, 3]
insertionSort(data)
print('Sorted Array in Ascending Order:')
print(data)
OUTPUT :-
EXPERIMENT (9)
To write a python program to perform insertion sort
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2 left_half = arr[:mid] right_half = arr[mid:]
left_half = merge_sort(left_half)
right_half = merge_sort(right_half)
return merge(left_half, right_half)
def merge(left, right):
result = [] i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i]) i += 1
else:
result.append(right[j])
j += 1
while i < len(left): result.append(left[i]) i += 1
while j < len(right): result.append(right[j]) j += 1
return result
array = [99,2,1,36,13,9]
sorted_array = merge_sort(array)
print("Original Array =", array)
print("Sorted Array =", sorted_array)
OUTPUT :-
EXPERIMENT (10)
To write a python program to find first n prime numbers
def isPrime(n):
if(n==1 or n==0):
return False
for i in range(2,n):
if(n%i==0):
return False
return True
N = 100;
for i in range(1,N+1):
if(isPrime(i)):
print(i,end=" ")
EXPERIMENT (11)
To write a python program to multiply two matrices
# 3x3 matrix
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
# result is 3x4
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
# iterate through rows of X for i in range(len(X)):
# iterate through columns of Y for j in
range(len(Y[0])):
# iterate through rows of Y
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for r in result: print(r)
OUTPUT : -
XPERIMENT (12)
E
o write a python program to find the most frequent words in a text read from a file
T
from collections import Counter
def find_most_frequent_words(input_text, top_n): words =
input_text.lower().split()
word_counts = Counter(words)
most_common = word_counts.most_common(top_n) print(f"The
top_n} most frequent words are:")
for word, count in most_common:
print(f"{word}: {count} times")
input_text = input("Enter your text: ")
top_n = int(input("How many top frequent words would you like to see? "))
find_most_frequent_words(input_text, top_n)
OUTPUT : -