0% found this document useful (0 votes)
7 views

PYTHON PROJECT agya full and final

The document contains a series of Python experiments demonstrating various algorithms and functions, including GCD computation, square root calculation using Newton's method, exponentiation, finding maximum values in a list, and different search and sorting algorithms. It also includes matrix multiplication, prime number generation, and finding the most frequent words in a text. Each experiment is presented with code snippets and user interaction for input.

Uploaded by

agyaprajapati3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

PYTHON PROJECT agya full and final

The document contains a series of Python experiments demonstrating various algorithms and functions, including GCD computation, square root calculation using Newton's method, exponentiation, finding maximum values in a list, and different search and sorting algorithms. It also includes matrix multiplication, prime number generation, and finding the most frequent words in a text. Each experiment is presented with code snippets and user interaction for input.

Uploaded by

agyaprajapati3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

‭EXPERIMENT (1)‬

‭To compute the GCD of two numbers‬


‭def gcd(a,b):‬
‭if(b==0):‬
‭return a else:return gcd(b,a%b)‬
‭a=int(input("Enter first number:"))‬
‭b=int(input("Enter second number:"))‬
‭GCD=gcd(a,b)‬
‭print("GCD is: ")‬
‭print(GCD)‬
‭EXPERIMENT (2)‬
‭Find the square root of a number (Newton’s method)‬
‭def‬ ‭newtons_method_sqrt(number,‬ ‭tolerance=1e-10,‬‭max_iterations=1000):‬
‭if number < 0:‬
‭raise‬ ‭ValueError("Cannot‬ ‭compute‬ ‭the‬ ‭square‬ ‭root‬ ‭of‬‭a‬‭negative‬‭number.")‬
‭guess = number‬
‭for _ in range(max_iterations):‬
‭next_guess = 0.5 * (guess + number / guess)‬
‭if abs(next_guess - guess)< tolerance:‬
‭return‬ ‭next_guess‬ ‭guess‬ ‭=‬ ‭next_guess‬
‭return guess def main():‬
‭try:‬
‭number = float(input("Enter a number to find its square root:”)‬

‭if number < 0:‬

‭print("Cannot compute the square root of a negative number.")‬


‭return result = newtons_method_sqrt(number)‬
‭print(f"The‬‭square‬‭root‬‭of‬‭{number}‬‭is‬‭approximately‬‭{result}")‬
‭except ValueError:‬
‭print("Please enter a valid number.")‬
‭if‬ ‭name‬ ‭== "‬ ‭main‬ ‭":‬
‭main()‬
‭EXPERIMENT (3)‬
‭Exponentiation (power of a number)‬
‭a= int(input("enter a number :"))‬
‭b= int(input("enter a power :")) if b==0:‬
‭print(1)‬
‭else:‬
‭for i in range(1,b+1): result=a**i print(result)‬
‭EXPERIMENT (4)‬
‭Find the maximum of a list of numbers‬
‭n=int(input("Enter number of element in list : "))‬
‭mylist=[]‬
‭print("Enter elements of the list : ")‬
‭for _ in range(n):‬
‭a=int(input("Enter a Value : "))‬
‭mylist.append(a)‬
‭maximum=mylist[0]‬
‭for i in mylist:‬
‭if maximum<i:‬
‭maximum=i‬
‭print("Maximum of the list is :",maximum)‬
‭EXPERIMENT (5)‬
‭To write a python program to perform Linear Search‬
‭def LS(): list1 = [] flag = 0‬
‭N = int(input("Enter number of elements: "))‬
‭print("Enter values one by one:")‬
‭for i in range(0, N):‬
‭a= int(input("Enter a value: "))‬
‭list1.append(a)‬
‭print("List:", list1)‬
‭key = int(input("Enter the Key value: "))‬
‭for i in range(0, N):‬
‭if key == list1[i]:‬
‭print(key, "is found in the list at position", i + 1)‬
‭else:‬
‭print(key, "is not found in the list")‬

‭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 : -‬

You might also like