0% found this document useful (0 votes)
2 views13 pages

Python Lab

The document contains multiple Python programs demonstrating various algorithms and functionalities, including GCD calculation, square root finding, exponentiation, searching algorithms, sorting methods, prime number checking, matrix multiplication, and command line argument handling. Each program is accompanied by its coding implementation and expected output. The programs serve as examples for learning and understanding basic programming concepts and algorithms.

Uploaded by

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

Python Lab

The document contains multiple Python programs demonstrating various algorithms and functionalities, including GCD calculation, square root finding, exponentiation, searching algorithms, sorting methods, prime number checking, matrix multiplication, and command line argument handling. Each program is accompanied by its coding implementation and expected output. The programs serve as examples for learning and understanding basic programming concepts and algorithms.

Uploaded by

bsri7608
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROGRAM 1: GCD OF TWO NUMBERS

CODING

def hcf(a, b):

if(b == 0):

return a

else:

return hcf(b, a % b)

a = 50

b=3

# prints 12

print("The gcd of 50 and 35 is : ", end="")

print(hcf(50, 35))

OUTPUT:

PROGRAM NAME : 2. SQUARE ROOT

CODING

def find_sqrt(N, guess, tolerance):

next_guess = (guess + N / guess) / 2

if abs(guess - next_guess) <= tolerance:

return next_guess

else:
return find_sqrt(N, next_guess, tolerance)

if __name__ == "__main__":

N = 327

tolerance = 0.00001

guess = N / 2 # Initialize the guess to N/2

sqrt = find_sqrt(N, guess, tolerance)

sqrt = round(sqrt, 6)

print(sqrt)

OUTPUT :

PROGRAM NAME : 3 EXPONENTIATION

CODING

def power(x, y):

temp = 0

if(y == 0):

return 1

temp = power(x, int(y / 2))


if y % 2 == 0:

return temp * temp

else:

return x * temp * temp

result1 = power(2, 5)

print(result1)

result2 = power(3, 6)

print(result2)

729

OUTPUT :

PROGRAM NAME : 4. MAXIMUM OF A LIST OF NUMBER

CODINGS

list1 = ['!', '$', '/', '3', '61']

maxValue = max(list1)

print(maxValue)

OUTPUT :
PROGRAM NAME : 5, LINEAR SEARCH AND BINARY SEARCH

CODINGS

def binarySearch(array, x, low, high):

while low <= high:

mid = low + (high - low)//2

if array[mid] == x:

return mid

elif array[mid] < x:

low = mid + 1

else:

high = mid - 1

return -1

array = [2, 4, 5, 7, 14, 17, 19, 22]

x = 22

result = binarySearch(array, x, 0, len(array)-1)

if result != -1:

print(str(result))

else:

print("Not found")
OUTPUT :

PROGRAM 6: SELECTION SORT AND INSERTION SORT

CODINGS

A) SELECTION SORT

def insertionSort(arr, n):

i=0

key = 0

j=0

for i in range(1,n,1):

key = arr[i]

j=i-1

while (j >= 0 and arr[j] > key):

arr[j + 1] = arr[j]

j=j-1

arr[j + 1] = key
def printArray(arr, n):

i=0

for i in range(n):

print(arr[i],end = "")

print("\n",end = "")

if __name__ == '__main__':

arr = [12, 11, 13, 5, 6]

N = len(arr)

insertionSort(arr, N)

printArray(arr, N)

OUTPUT:

PROGRAM 6 (B) INSERTION SORT

def selectionSort(arr, n):

for i in range(n - 1):

min_idx = i

for j in range(i + 1, n):

if (arr[j] < arr[min_idx]):

min_idx = j

arr[min_idx], arr[i] = arr[i], arr[min_idx]

def printArray(arr, size):


for i in range(size):

print(arr[i], end = "")

print()

if __name__ == "__main__":

arr = [64, 25, 12, 22, 11]

n = len(arr)

selectionSort(arr, n)

print("Sorted array: ")

printArray(arr, n)

OUTPUT :

PROGRAM 7 : MERGE SORT.

CODINGS

def merge(arr, l, m, r):

n1 = m - l + 1

n2 = r - m

L = [0] * (n1)
R = [0] * (n2)

for i in range(0, n1):

L[i] = arr[l + i]

for j in range(0, n2):

R[j] = arr[m + 1 + j]

i=0

j=0

k=l

while i < n1 and j < n2:

if L[i] <= R[j]:

arr[k] = L[i]

i += 1

else:

arr[k] = R[j]

j += 1

k += 1

while i < n1:

arr[k] = L[i]

i += 1

k += 1

while j < n2:

arr[k] = R[j]

j += 1

k += 1

def mergeSort(arr, l, r):

if l < r:

# Same as (l+r)//2, but avoids overflow for

# large l and h
m = l+(r-l)//2

mergeSort(arr, l, m)

mergeSort(arr, m+1, r)

merge(arr, l, m, r)

arr = [12, 11, 13, 5, 6, 7]

n = len(arr)

print("Given array is")

for i in range(n):

print("%d" % arr[i],end="")

mergeSort(arr, 0, n-1)

print("\n\nSorted array is")

for i in range(n):

print("%d" % arr[i],end="")

OUTPUT :

PROGRAM 8 : FIND N PRIME NUMBER


CODINGS

num = 11

if num > 1:

for i in range(2, int(num/2)+1):

if (num % i) == 0:

print(num, "is not a prime number")

break

else:

print(num, "is a prime number")

else:

print(num, "is not a prime number")

OUTPUT :
.

PROGRAM 9 : MULTIPLE MATRIX

CODINGS

def multiply_matrices(matrix1, matrix2):

result = [[0 for _ in range(len(matrix2[0]))] for _ in range(len(matrix1))]

for i in range(len(matrix1)):

for j in range(len(matrix2[0])):

for k in range(len(matrix2)):

result[i][j] += matrix1[i][k] * matrix2[k][j]

return result

matrix1 = [

[1, 2, 3],

[4, 5, 6],

[7, 8, 9] ]

matrix2 = [
[9, 8, 7],

[6, 5, 4],

[3, 2, 1]

result_matrix = multiply_matrices(matrix1, matrix2)

print("Matrix 1:")

for row in matrix1:

print(row)

print("\nMatrix 2:")

for row in matrix2:

print(row)

print("\nResultant Matrix:")

for row in result_matrix:

print(row)

OUTPUT :

PROGRAM : 10 COMMAND LINE ARGUMENTS (WORD COUNT )


CODINGS

import sys

n = len([Link])

print("Total arguments passed:", n)

print("\nName of Python script:", [Link][0])

print("\nArguments passed:", end = "")

for i in range(1, n):

print([Link][i], end = "")

Sum = 0

for i in range(1, n):

Sum += int([Link][i])

print("\n\nResult:", Sum)

OUTPUT :

You might also like