MOHAMED SATHAK COLLEGE OF ARTS & SCIENCE
(Affiliated to University of Madras Approved by UGC & AICTE and Reaccredited by NAAC)
SHOLINGANALLUR, CHENNAI – 600 119
PG DEPARTMENT OF COMPUTER SCIENCE
MASTER OF COMPUTER APPLICATION
I SEMESTER (2023 - 2024)
PROGRAMMING IN PYTHON LAB
Name :
Register No :
Roll No :
MOHAMED SATHAK COLLEGE OF ARTS & SCIENCE
(Affiliated to University of Madras Approved by UGC & AICTE and Reaccredited by NAAC)
SHOLINGANALLUR, CHENNAI-600 119.
PG DEPARTMENT OF COMPUTER SCIENCE
BONAFIDE CERTIFICATE
Register No:__________________
This is to be certify that the record work done by ___________________________ in the year 2023 –
2024 in the [Link] of Computer Science, for the degree of MASTER OF COMPUTER
APPLICATIONS submitted for the practical examination held on _______________________ at Mohamed
Sathak College of Arts and Science.
Head of the Department Lecturer – in – charge
Internal Examiner External Examiner
INDEX
Pg.
[Link] DATE TITLE No SIGNATURE
1 a) Linear recursion
2 Binary recursion
3 Stack ADT
4 Queue ADT
5 Doubly Linked List ADT
6 Heaps using Priority Queues
7 Merge sort
8 Quick sort
9 Binary Search Tree
10 Minimum Spanning Tree
11 Depth First Search Tree Traversal
12 Age limit
Two Dimensional Array To Perform
13 Matrix
14 Array using priority queue
1. LINEAR RECURSION DATE:
Aim:
Algorithm:
CODING:
#linear recursion
deffibonacci(n):
if (n<+1):
returnn else:
return(fibonacci(n-1)+fibonacci(n-2))
n=int(input("enter number of terms:"))
print("fibonacci sequence:")
fori in range(n):
print(fibonacci(i))
OUTPUT :
Enter number of terms: 10
fibonacci sequence:
13
21
34
RESULT:
2. BINARY RECURSION DATE:
Aim:
Algorithm:
CODING:
# Binary Recursion
class Node:
def __init__(self, key):
[Link] = key
[Link] = None
[Link] = None
def insert(node, key):
if node is None:
return Node(key)
if key <[Link]:
[Link] = insert([Link], key)
elif key >[Link]:
[Link] = insert([Link], key)
return node
def search(root, key):
if root is None or [Link] == key:
return root
[Link]< key:
return search([Link], key)
return search([Link], key)
if __name__ == '__main__':
root = None
root = insert(root, 50)
insert(root, 30)
insert(root, 20)
insert(root, 40)
insert(root, 70)
insert(root, 60)
insert(root, 80)
key = 6
if search(root, key) is None:
print(key, "not found")
else:
print(key, "found")
key = 60
if search(root, key) is None:
print(key, "not found")
else:
print(key, "found")
OUTPUT:
6 not found
60 found
RESULT:
3. STACK ADT DATE:
Aim:
Algorithm:
CODING:
# Stack ADT
stack=[]
[Link]('a')
[Link]('b')
[Link]('c')
print('Initial stack')
print(stack)
print('\n Elements popped from stack')
print([Link]())
print([Link]())
print([Link]())
print('\n Stack after elements are popped:')
print(stack)
OUTPUT:
Initial stack
['a', 'b', 'c']
Elements popped from stack
c
b
a
Stack after elements are popped:
[]
RESULT:
4. QUEUE ADT DATE:
Aim:
Algorithm:
CODING:
#Queue ADT
queue=[]
[Link]('a')
[Link]('b')
[Link]('c')
print('Initial queue')
print(queue)
print('\n Elements dequeued from queue')
print([Link](0))
print([Link](0))
print([Link](0))
print('\n queue after removing elements:')
print(queue)
OUTPUT:
Initial queue
['a', 'b', 'c']
Elements dequeued from queue
a
b
c
queue after removing elements:
[]
RESULT:
5. DOUBLY LINKED LIST ADT DATE:
Aim:
Algorithm:
CODING:
#Doubly Linked List ADT
class Node:
def __init__(self, data):
[Link] = data
[Link] = None
[Link] = None
classdoubly_linked_list:
def __init__(self):
[Link] = None
def push(self, NewVal):
NewNode = Node(NewVal)
[Link] = [Link]
[Link] is not None:
[Link] = NewNode
[Link] = NewNode
def insert(self, prev_node, NewVal):
ifprev_node is None:
return
NewNode = Node(NewVal)
[Link] = prev_node.next
prev_node.next = NewNode
[Link] = prev_node
[Link] is not None:
[Link] = NewNode
deflistprint(self, node):
while (node is not None):
print([Link]),
last = node
node = [Link]
dllist = doubly_linked_list()
[Link](12)
[Link](8)
[Link](62)
[Link]([Link], 13)
[Link]([Link])
OUTPUT:
62
8
13
12
RESULT:
6. HEAPS USING PRIORITY QUEUES DATE:
Aim:
Algorithm:
CODING:
#Heaps Using Priority Queue
importheapq as hq
list_stu = [(5,'Rina'),(1,'Anish'),(3,'Moana'),(2,'cathy'),(4,'Lucy')]
[Link](list_stu)
print("The order of presentation is :")
fori in list_stu:
print(i[0],':',i[1])
OUTPUT:
The order of presentation is :
1 : Anish
2 :cathy
3 :Moana
5 :Rina
4 : Lucy
RESULT
7. MERGE SORT DATE:
Aim:
Algorithm:
CODING:
#Merge Sort
def merge(arr, l, m, r):
n1 = m - l + 1
n2 = r - m
L = [0] * (n1)
R = [0] * (n2)
fori 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
whilei< n1 and j < n2:
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
whilei< n1:
arr[k] = L[i]
i += 1
k += 1
while j < n2:
arr[k] = R[j]
j += 1
k += 1
defmergeSort(arr, l, r):
if l < r:
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")
fori in range(n):
print("%d" % arr[i],end=" ")
mergeSort(arr, 0, n-1)
print("\n\nSorted array is")
fori in range(n):
print("%d" % arr[i],end=" ")
OUTPUT:
Given array is
12 11 13 5 6 7
Sorted array is
5 6 7 11 12 13
RESULT:
7. QUICK SORT DATE:
Aim:
Algorithm:
CODING:
#Quick Sort
def partition(array, low, high):
pivot = array[high]
i = low - 1
for j in range(low, high):
if array[j] <= pivot:
i=i+1
(array[i], array[j]) = (array[j], array[i])
(array[i + 1], array[high]) = (array[high], array[i + 1])
returni + 1
defquickSort(array, low, high):
if low < high:
pi = partition(array, low, high)
quickSort(array, low, pi - 1)
quickSort(array, pi + 1, high)
data = [1, 7, 4, 1, 10, 9, -2]
print("Unsorted Array")
print(data)
size = len(data)
quickSort(data, 0, size - 1)
print('Sorted Array in Ascending Order:')
print(data)
OUTPUT:
Unsorted Array
[1, 7, 4, 1, 10, 9, -2]
Sorted Array in Ascending Order:
[-2, 1, 1, 4, 7, 9, 10]
RESULT:
9. BINARY SEARCH TREE DATE:
Aim:
Algorithm:
CODING:
#Binary Search Tree
class Node:
def __init__(self, key):
[Link] = key
[Link] = None
[Link] = None
def insert(node, key):
if node is None:
return Node(key)
if key <[Link]:
[Link] = insert([Link], key)
elif key >[Link]:
[Link] = insert([Link], key)
return node
def search(root, key):
if root is None or [Link] == key:
return root
[Link]< key:
return search([Link], key)
return search([Link], key)
if __name__ == '__main__':
root = None
root = insert(root, 50)
insert(root, 30)
insert(root, 20)
insert(root, 40)
insert(root, 70)
insert(root, 60)
insert(root, 80)
key = 6
if search(root, key) is None:
print(key, "not found")
else:
print(key, "found")
key = 60
if search(root, key) is None:
print(key, "not found")
else:
print(key, "found")
OUTPUT:
6 not found
60 found
RESULT:
10. PRIM’S MINIMUM SPANNING TREE DATE:
Aim:
Algorithm:
CODING:
#Prims Minimum Spanning Tree
INF = 9999999
N=5
G = [[0, 19, 5, 0, 0],
[19, 0, 5, 9, 2],
[5, 5, 0, 1, 6],
[0, 9, 1, 0, 1],
[0, 2, 6, 1, 0]]
selected_node = [0, 0, 0, 0, 0]
no_edge = 0
selected_node[0] = True
print("Edge : Weight\n")
while (no_edge< N - 1):
minimum = INF
a=0
b=0
for m in range(N):
ifselected_node[m]:
for n in range(N):
if ((not selected_node[n]) and G[m][n]):
# not in selected and there is an edge
if minimum > G[m][n]:
minimum = G[m][n]
a=m
b=n
print(str(a) + "-" + str(b) + ":" + str(G[a][b]))
selected_node[b] = True
no_edge += 1
OUTPUT:
Edge : Weight
0-2:5
2-3:1
3-4:1
4-1:2
RESULT:
11. DEPTH FIRST SEARCH TREE TRAVERSAL DATE:
Aim:
Algorithm:
CODING:
#Depth First Search Tree Traversal
from collections import defaultdict
class Graph:
def __init__(self):
[Link] = defaultdict(list)
defaddEdge(self,u,v):
[Link][u].append(v)
defDFSUtil(self, v, visited):
visited[v]= True
print (v)
fori in [Link][v]:
if visited[i] == False:
[Link](i, visited)
def DFS(self):
V = len([Link])
visited =[False]*(V)
fori in range(V):
if visited[i] == False:
[Link](i, visited)
g = Graph()
[Link](0, 1)
[Link](0, 2)
[Link](1, 2)
[Link](2, 0)
[Link](2, 3)
[Link](3, 3)
print ("Following is Depth First Traversal")
[Link]()
OUTPUT:
Following is Depth First Traversal
0
1
2
3
RESULT:
12. AGE LIMIT DATE:
Aim:
Algorithm:
CODING:
#Age Limit
fromdatetime import datetime
defcalculate_age_limit(date_of_birth, age_limit):
dob = [Link](date_of_birth, "%Y-%m-%d")
age = [Link]().year - [Link]
if age >= age_limit:
returnf"You meet the age limit of {age_limit} years."
else:
returnf"Sorry, you must be at least {age_limit} years old."
date_of_birth = input("Enter your date of birth (YYYY-MM-DD): ")
age_limit = int(input("Enter the age limit: "))
result = calculate_age_limit(date_of_birth, age_limit)
print(result)
OUTPUT:
Enter your date of birth (YYYY-MM-DD): 1997-04-15
Enter the age limit: 25
You meet the age limit of 25 years.
Enter your date of birth (YYYY-MM-DD): 2003-05-20
Enter the age limit: 25
Sorry, you must be at least 25 years old.
RESULT:
13. TWO DIMENSIONAL ARRAY TO PERFORM MATRIX DATE:
Aim:
Algorithm:
CODING:
#Two dimensional array to perform matrix
importnumpy as np
matrix1=[Link]([[1,2],[3,4]])
matrix2=[Link]([[5,6],[7,8]])
result=[Link](matrix1,matrix2)
print("Matrix multiplication result:")
print(result)
result=[Link](matrix1,matrix2)
print("Matrix addition result:")
print(result)
result=[Link](matrix1,matrix2)
print("Matrix subtraction result:")
print(result)
print("Scanning the matrix:")
for row in matrix1:
for element in row:
print(element,end=" ")
print()
OUTPUT:
Matrix multiplication result:
[[19 22]
[43 50]]
Matrix addition result:
[[ 6 8]
[10 12]]
Matrix subtraction result:
[[-4 -4]
[-4 -4]]
Scanning the matrix:
12
34
RESULT:
14. ARRAY USING BINARY SEARCH DATE:
Aim:
Algorithm:
CODING:
#Binary search
defbinary_search(arr,target):
low,high=0,len(arr)-1
while low<=high:
mid=(low+high)//2
mid_element=arr[mid]
ifmid_element==target:
return mid
elifmid_element<target:
low=mid+1
else:
high=mid-1
return-1
array=[3,6,8,12,14,17,25,29,31,36,42,47,53,59,66]
target_element=53
result=binary_search(array,target_element)
if result!=1:
print(f"Element {target_element} found at index {result}")
else:
print(f"Element{target_element} not found in the array.")
OUTPUT:
Element 53 found at index 12
RESULT: