sdfinall
sdfinall
STUDENT NAME:
TITLE :- 1 Write a program to perform using NumPy to perform Various functions in Array.
Program :
import numpy as np
# Creating an array
# Various operations
Output :
1
SAVITRIBAI PHULE PUNE UNIVERSITY
STUDENT NAME:
Program :
import numpy as np
def sparse_matrix(matrix):
sparse = []
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if matrix[i][j] != 0:
sparse.append((i, j, matrix[i][j]))
return sparse
# Input Matrix
matrix = [
[5, 0, 0],
[0, 8, 0],
[0, 0, 3]
print("Original Matrix:")
print(row)
2
print(sparse_matrix(matrix))
Output :
3
SAVITRIBAI PHULE PUNE UNIVERSITY
STUDENT NAME:
Program :
def string_manipulations(s):
arr.reverse()
# Adding a character
arr.append('!')
# Removing a character
arr.pop()
string_manipulations("hello")
4
Output :
5
SAVITRIBAI PHULE PUNE UNIVERSITY
MASTER OF COMPUTER APPLICATION
DR.D.Y. PATIL SCHOOL OF MCA
Charoli (bk) pune-412105
STUDENT NAME:
Program :
class Node:
self.left = None
self.right = None
self.key = key
# Insert function
if root is None:
return Node(key)
else:
return root
# Delete function
return root
else:
if root.left is None:
return root.right
return root.left
temp = find_min(root.right)
root.key = temp.key
return root
def find_min(node):
current = node
current = current.left
return current
# Search function
return root
31
if key < root.key:
def inorder(root):
if root:
inorder(root.left)
inorder(root.right)
# Menu-Driven Program
root = None
while True:
if choice == 1:
elif choice == 2:
elif choice == 3:
if result:
else:
inorder(root)
print()
elif choice == 5:
break
Output :
33
SAVITRIBAI PHULE PUNE UNIVERSITY
STUDENT NAME:
Program :
for i in range(len(arr)):
if arr[i] == x:
result = linear_search(arr, x)
if result != -1:
else:
Output :
34
SAVITRIBAI PHULE PUNE UNIVERSITY
STUDENT NAME:
Program :
if arr[mid] == x:
return mid
low = mid + 1
else:
high = mid - 1
return -1
result = binary_search(arr, x)
if result != -1:
else:
35
print("Element not found")
Output :
36
SAVITRIBAI PHULE PUNE UNIVERSITY
MASTER OF COMPUTER APPLICATION
DR.D.Y. PATIL SCHOOL OF MCA
Charoli (bk) pune-412105
STUDENT NAME:
Program :
class Node:
self.left = None
self.right = None
self.key = key
# Traversal Functions
def preorder(root):
if root:
preorder(root.left)
preorder(root.right)
def inorder(root):
if root:
inorder(root.left)
inorder(root.right)
def postorder(root):
if root:
postorder(root.left)
37
postorder(root.right)
if root is None:
return Node(key)
else:
return root
# Menu-Driven Program
root = None
while True:
if choice == 1:
elif choice == 2:
preorder(root)
print()
elif choice == 3:
inorder(root)
print()
38
elif choice == 4:
postorder(root)
print()
elif choice == 5:
break
Output :
39
SAVITRIBAI PHULE PUNE UNIVERSITY
STUDENT NAME:
Program :
class Node:
self.key = key
self.left = None
self.right = None
self.height = 1
def get_height(node):
if not node:
return 0
return node.height
def get_balance(node):
if not node:
return 0
def rotate_right(y):
40
x = y.left
T2 = x.right
x.right = y
y.left = T2
return x
def rotate_left(x):
y = x.right
T2 = y.left
y.left = x
x.right = T2
return y
# Insert operation
if not node:
return Node(key)
else:
balance = get_balance(node)
# Left heavy
41
if balance > 1 and key < node.left.key:
return rotate_right(node)
# Right heavy
return rotate_left(node)
# Left-Right heavy
node.left = rotate_left(node.left)
return rotate_right(node)
# Right-Left heavy
node.right = rotate_right(node.right)
return rotate_left(node)
return node
# Inorder Traversal
def inorder(node):
if node:
inorder(node.left)
inorder(node.right)
# Search operation
return node
# Menu-driven Program
root = None
while True:
if choice == 1:
elif choice == 2:
elif choice == 3:
inorder(root)
print()
elif choice == 4:
break
43
Output :
44
SAVITRIBAI PHULE PUNE UNIVERSITY
STUDENT NAME:
Adjacency Matrix
Adjacency List
Program :
u, v = edge
matrix[u][v] = 1
return matrix
u, v = edge
adj_list[u].append(v)
return adj_list
45
# Input
edges = []
for _ in range(edges_count):
edges.append((u, v))
# Output
print("\nAdjacency Matrix:")
print(row)
print("\nAdjacency List:")
print(f"{key}: {value}")
Output :
46
SAVITRIBAI PHULE PUNE UNIVERSITY
MASTER OF COMPUTER APPLICATION
DR.D.Y. PATIL SCHOOL OF MCA
Charoli (bk) pune-412105
STUDENT NAME:
BFS
DFS
Program :
visited = set()
queue = deque([start])
while queue:
node = queue.popleft()
visited.add(node)
queue.append(neighbor)
if visited is None:
visited = set()
visited.add(start)
47
print(start, end=" ")
# Input
for _ in range(edges_count):
graph[u].append(v)
graph[v].append(u)
# Traversal
bfs(graph, start)
dfs(graph, start)
48
Output :
49
SAVITRIBAI PHULE PUNE UNIVERSITY
STUDENT NAME:
Program :
def bubble_sort(arr):
n = len(arr)
for i in range(n):
bubble_sort(arr)
Output :
50
SAVITRIBAI PHULE PUNE UNIVERSITY
STUDENT NAME:
Program :
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]
merge_sort(left_half)
merge_sort(right_half)
i=j=k=0
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1
51
arr[k] = left_half[i]
i += 1
k += 1
arr[k] = right_half[j]
j += 1
k += 1
merge_sort(arr)
Output :
52
SAVITRIBAI PHULE PUNE UNIVERSITY
MASTER OF COMPUTER APPLICATION
DR.D.Y. PATIL SCHOOL OF MCA
Charoli (bk) pune-412105
STUDENT NAME:
Program :
i += 1
return i + 1
quick_sort(arr, 0, len(arr) - 1)
53
Output :
54
SAVITRIBAI PHULE PUNE UNIVERSITY
STUDENT NAME:
Heap sort.
Program :
left = 2 * i + 1
right = 2 * i + 2
largest = left
largest = right
if largest != i:
heapify(arr, n, largest)
def heap_sort(arr):
n = len(arr)
heapify(arr, n, i)
55
for i in range(n - 1, 0, -1): # Extract elements
heapify(arr, i, 0)
heap_sort(arr)
Output :
56
SAVITRIBAI PHULE PUNE UNIVERSITY
STUDENT NAME:
Program :
class HashTable:
self.size = size
index = self.hash_function(key)
if self.table[index] is None:
self.table[index] = key
else:
index = self.hash_function(key)
if self.table[index] == key:
else:
57
def display(self):
print("Hash Table:")
# Menu-driven program
hash_table = HashTable(size)
while True:
if choice == 1:
hash_table.insert(key)
elif choice == 2:
hash_table.search(key)
elif choice == 3:
hash_table.display()
elif choice == 4:
break
58
Output :
59