Imp(Practical File)
Imp(Practical File)
def compute_average(marks):
if len(valid_marks) == 0:
return 0
def find_highest_and_lowest(marks):
if len(valid_marks) == 0:
def count_absent_students(marks):
return marks.count(-1)
def find_highest_frequency(marks):
if len(valid_marks) == 0:
return None, 0
marks = []
for i in range(n):
marks.append(mark)
# Perform computations
average = compute_average(marks)
highest, lowest = find_highest_and_lowest(marks)
absent_count = count_absent_students(marks)
# Display results
print("\nResults:")
else:
else:
Output:
Practical 2
def longest_word(string):
words = string.split()
return longest
return string.count(char)
def is_palindrome(string):
return string.find(substring)
def word_occurrences(string):
words = string.split()
occurrences = {}
occurrences[word] = occurrences.get(word, 0) + 1
return occurrences
# Input string
# Menu of operations
print("\nSelect an operation:")
if choice == 'a':
longest = longest_word(input_string)
if is_palindrome(input_string):
else:
if index != -1:
else:
occurrences = word_occurrences(input_string)
print(f"{word}: {count}")
else:
matrix = []
for i in range(rows):
matrix.append(row)
return np.array(matrix)
def matrix_operations():
print("Matrix Operations:")
rows1 = int(input("Enter the number of rows for the first matrix: "))
cols1 = int(input("Enter the number of columns for the first matrix: "))
rows2 = int(input("Enter the number of rows for the second matrix: "))
cols2 = int(input("Enter the number of columns for the second matrix: "))
if matrix1.shape == matrix2.shape:
else:
if matrix1.shape == matrix2.shape:
print(result)
else:
if cols1 == rows2:
print(result)
else:
print("Error: Number of columns of the first matrix must equal the number of rows of the second
matrix for multiplication.")
result = matrix.T
print(result)
else:
matrix_operations()
Output:
a) Addition of two matrices.
d) Transpose of a matrix.
Practical 4
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
min_idx = j
def bubble_sort(arr):
n = len(arr)
for i in range(n):
percentages = []
for i in range(n):
selection_sort(percentages)
bubble_sort(percentages)
else:
exit()
top_scores = display_top_scores(percentages)
Output:
a) Selection sort :
b) Bubble sort :
Practical 5
def insertion_sort(arr):
key = arr[i]
j=i-1
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
def shell_sort(arr):
n = len(arr)
gap = n // 2
temp = arr[i]
j=i
j -= gap
arr[j] = temp
gap //= 2
percentages = []
for i in range(n):
if choice == 'a':
insertion_sort(percentages)
shell_sort(percentages)
else:
exit()
top_scores = display_top_scores(percentages)
Output:
a)Insertion sort :
b) Shell sort :
Practical 6
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
percentages = []
for i in range(n):
sorted_percentages = quick_sort(percentages)
top_scores = display_top_scores(sorted_percentages)
#include <string>
// Node structure
struct Node {
int PRN;
string name;
Node* next;
};
class PinnacleClub {
private:
Node* head;
public:
PinnacleClub() : head(nullptr) {}
// Add a member
void addMember(int PRN, string name, bool isPresident = false, bool isSecretary = false) {
newNode->next = head;
head = newNode;
if (head == nullptr) {
head = newNode;
} else {
temp = temp->next;
}
temp->next = newNode;
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;
// Delete a member
if (head == nullptr) {
return;
head = head->next;
delete temp;
return;
temp = temp->next;
if (temp->next == nullptr) {
cout << "Member with PRN " << PRN << " not found.\n";
} else {
delete toDelete;
cout << "Member with PRN " << PRN << " deleted.\n";
int countMembers() {
int count = 0;
count++;
temp = temp->next;
return count;
// Display members
void displayMembers() {
if (head == nullptr) {
return;
cout << "PRN: " << temp->PRN << ", Name: " << temp->name << "\n";
temp = temp->next;
if (head == nullptr) {
head = other.head;
} else {
temp = temp->next;
temp->next = other.head;
};
int main() {
// Adding members
// Display members
divA.displayMembers();
divB.displayMembers();
cout << "\nTotal members in Division A: " << divA.countMembers() << "\n";
divA.concatenate(divB);
divA.displayMembers();
// Deleting a member
divA.deleteMember(2);
divA.displayMembers();
return 0;
Output
Practical 8
#include <iostream>
#include <string>
#include <algorithm>
struct Node {
int bit;
Node* prev;
Node* next;
};
class BinaryNumber {
private:
Node* head;
Node* tail;
public:
if (!head) {
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
void display() {
while (temp) {
temp = temp->next;
void onesComplement() {
while (temp) {
temp->bit = (temp->bit == 0) ? 1 : 0;
temp = temp->next;
void twosComplement() {
int carry = 1;
temp->bit = sum % 2;
carry = sum / 2;
temp = temp->prev;
}
// Function to add two binary numbers (current binary + another binary)
int carry = 0;
if (temp1) {
sum += temp1->bit;
temp1 = temp1->prev;
if (temp2) {
sum += temp2->bit;
temp2 = temp2->prev;
carry = sum / 2;
// Insert the result bit at the front of the current binary number
insertAtFront(resultBit);
if (!head) {
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
~BinaryNumber() {
while (temp) {
delete temp;
temp = next;
};
int main() {
cout << "Enter binary number 1 (Enter each bit followed by Enter, followed by -1 to finish):\n";
int bit;
while (true) {
bin1.insertBit(bit);
cout << "Enter binary number 2 (Enter each bit followed by Enter, followed by -1 to finish):\n";
while (true) {
bin1.display();
bin2.display();
// Compute and display the 1's complement of the first binary number
bin1Copy.onesComplement();
bin1Copy.display();
// Compute and display the 2's complement of the first binary number
bin1Copy.twosComplement();
bin1Copy.display();
binSum.add(bin2);
binSum.display();
return 0;
}
Output:
Practical 9
#include <iostream>
#include <stack>
#include <unordered_map>
{')', '('},
{'}', '{'},
{']', '['}
};
s.push(ch);
// If the character is a closing bracket, check if it matches the top of the stack
return s.empty();
}
int main() {
string expression;
getline(cin, expression);
if (isWellParenthesized(expression)) {
} else {
return 0;
Output :
Practical 10
#include <iostream>
#include <stack>
#include <cctype>
#include <cmath>
#include <string>
return 1;
return 2;
return 0;
switch (op) {
default: return 0;
stack<char> operators;
if (isalnum(c)) {
postfix += c;
else if (c == '(') {
operators.push(c);
// If the character is ')', pop and append to postfix until '(' is encountered
else if (c == ')') {
postfix += operators.top();
operators.pop();
postfix += operators.top();
operators.pop();
while (!operators.empty()) {
postfix += operators.top();
operators.pop();
}
return postfix;
stack<int> values;
if (isalnum(c)) {
// The final result will be the only value left in the stack
return values.top();
int main() {
cout << "Enter infix expression (with single character operands and operators): ";
postfix = infixToPostfix(infix);
cout << "Postfix Expression: " << postfix << endl;
return 0;
Output:
Practical 11
#include <iostream>
#include <queue>
#include <string>
if (!jobQueue.empty()) {
} else {
cout << "No jobs to delete. The queue is empty." << endl;
if (jobQueue.empty()) {
return;
queue<string> tempQueue = jobQueue; // Create a temporary queue to preserve the original queue
while (!tempQueue.empty()) {
tempQueue.pop();
}
cout << endl;
int main() {
int choice;
string job;
while (true) {
// Display menu
switch (choice) {
case 1:
// Add job
addJob(jobQueue, job);
break;
case 2:
// Delete job
deleteJob(jobQueue);
break;
case 3:
break;
case 4:
// Exit
return 0;
default:
return 0;
}
Output:
Practical 12
#include <iostream>
class Deque {
private:
public:
Deque(int cap) {
capacity = cap;
front = -1;
rear = -1;
size = 0;
~Deque() {
delete[] arr;
bool isFull() {
return size == 0;
void addFront(int x) {
if (isFull()) {
cout << "Deque is full, cannot add element to the front." << endl;
return;
if (isEmpty()) {
front = 0;
rear = 0;
} else {
arr[front] = x;
size++;
cout << "Added " << x << " to the front." << endl;
void addRear(int x) {
if (isFull()) {
cout << "Deque is full, cannot add element to the rear." << endl;
return;
if (isEmpty()) {
front = 0;
rear = 0;
} else {
arr[rear] = x;
size++;
cout << "Added " << x << " to the rear." << endl;
void deleteFront() {
if (isEmpty()) {
cout << "Deque is empty, cannot delete element from the front." << endl;
return;
cout << "Deleted " << arr[front] << " from the front." << endl;
if (front == rear) {
front = -1;
rear = -1;
} else {
size--;
void deleteRear() {
if (isEmpty()) {
cout << "Deque is empty, cannot delete element from the rear." << endl;
return;
cout << "Deleted " << arr[rear] << " from the rear." << endl;
if (front == rear) {
front = -1;
rear = -1;
} else {
size--;
void display() {
if (isEmpty()) {
return;
int i = front;
while (i != rear) {
i = (i + 1) % capacity;
};
int main() {
Deque dq(5); // Create a deque with capacity of 5
while (true) {
cout << "3. Delete element from the front" << endl;
cout << "4. Delete element from the rear" << endl;
switch (choice) {
case 1:
dq.addFront(value);
break;
case 2:
dq.addRear(value);
break;
case 3:
dq.deleteFront();
break;
case 4:
dq.deleteRear();
break;
case 5:
dq.display();
break;
case 6:
return 0;
default:
return 0;
Output:
Deque Operations Menu:
1. Add element to the front
2. Add element to the rear
3. Delete element from the front
4. Delete element from the rear
5. Display deque
6. Exit
Enter your choice: 1
Enter value to add to front: 12
Added 12 to the front.
#include <string>
class PizzaParlor {
private:
int front, rear; // Front and rear pointers for the circular queue
public:
PizzaParlor(int maxOrders) {
capacity = maxOrders;
front = -1;
rear = -1;
size = 0;
~PizzaParlor() {
delete[] orders;
bool isFull() {
return size == 0;
if (isFull()) {
cout << "Sorry, the pizza parlor is full. Cannot accept more orders at the moment." << endl;
return;
if (isEmpty()) {
front = 0; // Set front and rear when the first order is placed
rear = 0;
} else {
orders[rear] = order; // Add the new order to the rear of the queue
size++;
// Function to serve the order (remove from the front of the queue)
void serveOrder() {
if (isEmpty()) {
cout << "No orders to serve. The queue is empty." << endl;
return;
if (front == rear) {
// If there was only one order, reset front and rear
front = -1;
rear = -1;
} else {
size--;
void displayOrders() {
if (isEmpty()) {
return;
int i = front;
while (i != rear) {
i = (i + 1) % capacity;
};
int main() {
int maxOrders;
cout << "Enter the maximum number of orders the pizza parlor can accept: ";
int choice;
string order;
while (true) {
// Display menu
switch (choice) {
case 1:
parlor.placeOrder(order);
break;
case 2:
// Serve an order
parlor.serveOrder();
break;
case 3:
parlor.displayOrders();
break;
case 4:
return 0;
default:
return 0;
Output:
Enter the maximum number of orders the pizza parlor can accept: 10