Ds Practical ..
Ds Practical ..
b) List of students who play either cricket or badminton but not both
c) Number of students who play neither cricket nor badminton
d) Number of students who play cricket and football but not badminton.
(Note- While realizing the group, duplicate entries should be avoided, Do not use
SET built- in functions)
CODE
def remove_duplicates(lst):
unique_list = []
for item in lst:
if item not in
unique_list:
unique_list.append(it
em)
return unique_list
return common
students_playing_cricket_or_badminton =
remove_duplicates(students_playing_cricket_or_badminton)
neither = []
for student in total_students:
if student not in
students_playing_cricket_or_badminton:
neither.append(student)
return neither
cricket_players = remove_duplicates(cricket_players)
badminton_players =
remove_duplicates(badminton_players)
football_players = remove_duplicates(football_players)
either_cricket_or_badminton = symmetric_difference(cricket_players,
badminton_players)
cricket_and_football_not_badminton_list =
cricket_and_football_not_badminton(cricket_players, football_players,
badminton_players)
print("Students who play Cricket and Football but not Badminton:",
cricket_and_football_not_badminton_list)
OUTPUT
Name: Prajwal Charthad
Roll no: 133
string CODE
def longest_word(sentence):
words = sentence.split()
longest = max(words,
key=len) return longest
count = 0
for c in sentence:
if c == char:
count += 1
return count
def is_palindrome(sentence):
cleaned_sentence = ''.join(e for e in sentence if
cleaned_sentence[::-1]
def substring_index(sentence,
substring): return
sentence.find(substring)
def
word_count(sentence)
: words =
sentence.split()
count_dict = {}
for word in words:
if word in count_dict:
count_dict[word] += 1
else:
count_dict[word] = 1
return count_dict
# Input string
row = []
for j in range(len(matrix1[0])):
row.append(matrix1[i][j] + matrix2[i][j])
result.append(ro
w) return result
for i in range(len(matrix1)):
row = []
for j in range(len(matrix1[0])):
row.append(matrix1[i][j] - matrix2[i][j])
result.append(ro
w) return result
for k in range(len(matrix2)):
result[i][j] += matrix1[i][k] * matrix2[k][j]
return result
result = []
for i in range(len(matrix[0])):
row = []
for j in
range(len(matrix)):
row.append(matrix[j][i])
result.append(ro
w) return result
# Input
matrices matrix1
= [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
[3, 2, 1]]
# d) Transpose of a matrix
print("\nTranspose of the first
matrix:") for row in
matrix_transpose(matrix1):
print(row)
OUTPUT
Name: Prajwal Charthad
Roll no: 133
if roll_numbers[index] == target:
return
index return -
1
index = 0
while roll_numbers[index] != target:
index += 1
return
index return
-1
# Linear Search
linear_result = linear_search(roll_numbers,
target_roll_number) if linear_result != -1:
print(f"Linear Search: Student with roll number {target_roll_number} attended the
training program (Index: {linear_result}).")
else:
print(f"Linear Search: Student with roll number {target_roll_number} did not attend
the training program.")
# Sentinel Search
sentinel_result = sentinel_search(roll_numbers,
target_roll_number) if sentinel_result != -1:
print(f"Sentinel Search: Student with roll number {target_roll_number} attended the
training
program (Index:
{sentinel_result}).") else:
print(f"Sentinel Search: Student with roll number {target_roll_number} did not
attend the training program.")
OUTPUT
Name: Prajwal Charthad
Roll no: 133
5Write a Python program to store first year percentage of students in an-ay. Write
function for sorting array of floating point numbers in ascending order using
a) Selection Sort
b) Bubble sort and display top five
scores. CODE
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_index = i
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
def display_top_five_scores(arr):
OUTPUT
Name: Prajwal Charthad
Roll no: 133
6 write a Python program to store first year percentage of students in array. Write
function for sorting array of floating point numbers in ascending order using quick
sort and display top five scores.
CODE
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
def display_top_five_scores(arr):
print("Top five scores:", arr[-5:])
percentages = [76.5, 88.2, 45.0, 92.1, 67.4, 80.5, 55.3, 70.8, 91.0, 65.7]
sorted_percentages =
quick_sort(percentages) print("Sorted
percentages using Ǫuick Sort:")
print(sorted_percentages)
one may cancel the membership of club. First node is reserved for president
of club and last
node is reserved for secretary of club. Write C+ program to maintain club
member's information using singly linked list. Store student PRN and Name.
Write functions to:
a) Add and delete the members as well as president or even secretary.
c)Display members
d Two linked lists exists for two divisions. Concatenate two
lists. CODE
#include
<iostream>
#include <string>
struct Node
{ string
name;
string prn;
Node*
next;
};
private:
Node* president;
Node* secretary;
public:
PinnacleClub() {
} else {
Node* temp = president;
while (temp->next !=
nullptr) {
temp = temp->next;
}
temp->next = newNode; // Add to the end of the list
}
if (temp == nullptr) {
cout << "Member with PRN " << prn << " not found." <<
endl; return;
}
}
delete temp; // Free memory
cout << "Member with PRN " << prn << " deleted successfully." << endl;
}
return count;
this->president->next = other.president->next;
} else {
}
temp->next = other.president->next; // Concatenate the second list
~PinnacleClub() {
Node* current =
president; Node*
nextNode;
while (current != nullptr)
{ nextNode = current-
>next; delete current;
current = nextNode;
}
}
};
int main() {
PinnacleClub
divisionA;
PinnacleClub
divisionB;
divisionA.addMember("Divesh", "PRN-001");
divisionA.addMember("John", "PRN-002");
divisionA.addMember("Nobita", "PRN-003");
divisionB.addMember("Been", "PRN-004");
divisionB.addMember("Shinchan", "PRN-005");
// Example of deleting a
member
divisionA.deleteMember("PRN-
002");
divisionA.displayMembers();
cout << "Total Members after Deletion: " << divisionA.totalMembers() << endl;
return 0;
}
OUTPUT
Name: Prajwal Charthad
Roll no: 133
8Write C++ program for storing binary number using doubly linked lists. Write
functions- a) To compute 1 's and 2's complement b Add two bin numbers
CODE
#include
<iostream>
#include <string>
struct Node {
};
class BinaryNumber {
private:
public:
} else {
tail->next = newNode; // Insert at the
}
}
to int i--;
}
}
}
};
int main() {
BinaryNumber binaryNumber;
// Input binary
number string
inputBinary;
cout << "Enter a binary
number: "; cin >> inputBinary;
binaryNumber.addBinary(binaryTo
Add); cout << "Result after
addition: ";
binaryNumber.display();
return 0;
OUTPUT
Name: Prajwal Charthad
Roll no: 133
string
processed; for
(char ch : str) { if
(isalnum(ch)) {
processed += tolower(ch); // Keep only alphanumeric characters
}
}
return processed;
}
}
}
int main() {
string
input;
// Get user input
} else {
cout << "The given string is not a palindrome." << endl;
}
return 0;
}
OUTPUT
Name: Prajwal Charthad
Roll no: 133
}
// Function to convert infix expression to postfix
expression string infixToPostfix(const string& infix) {
stack<char>
opStack; string
postfix;
{ if
(isalnum(ch)) {
}
}
while (!
opStack.empty()) {
postfix +=
opStack.top();
opStack.pop();
}
return postfix;
}
} else if (isOperator(ch)) {
int right = valueStack.top();
valueStack.pop(); int left =
valueStack.top(); valueStack.pop();
switch (ch) {
int main() {
string infixExpression;
string postfixExpression =
infixToPostfix(infixExpression); cout << "Postfix
expression: " << postfixExpression << endl;
return 0;
}
OUTPUT
Name: Prajwal Charthad
Roll no: 133
CODE
#include
<iostream> using
namespace std;
};
Node*
front;
Node* rear;
public:
rear->next =
newNode; rear =
newNode;
}
cout << "Enqueued: " << value << endl;
if (front == nullptr) {
cout << "Ǫueue is empty. Cannot dequeue." <<
endl; return;
}
if (front == nullptr) {
cout << "Ǫueue is empty." <<
endl; return;
}
};
int choice,
value; do {
cout << "1. Enqueue\n2. Dequeue\n3. Display Ǫueue\n4.
Exit\n"; cout << "Enter your choice: ";
cin >> choice;
switch
(choice) {
case 1:
cout << "Enter value to
enqueue: "; cin >> value;
q.enqueue(valu
e); break;
case 2:
q.dequeue();
break;
case 3:
q.display(
); break;
case 4:
cout << "Exiting program." << endl;
break;
default:
return 0;
}
OUTPUT
Name: Prajwal Charthad
Roll no: 133
CODE
#include
<iostream> using
namespace std;
class Deque {
private:
int* arr;
int front;
int rear;
int capacity;
public:
Deque(int size) {
capacity = size;
arr = new
int[capacity]; front = -
1;
rear = -1;
~Deque() {
delete[] arr;
}
bool isFull() {
return (front == 0 && rear == capacity - 1) || (front == rear + 1);
}
bool isEmpty() {
return front == -
1;
}
void addRear(int
value) { if (isFull()) {
cout << "Deque is full. Cannot add " << value << " to rear." <<
endl; return;
}
if (front == -1) { // First element being
added front = 0;
rear = 0;
} else if (rear == capacity - 1) { // Wrap
around rear = 0;
} else {
rear++;
}
arr[rear] = value;
cout << "Added " << value << " to rear." << endl;
void
deleteFront() {
if (isEmpty()) {
cout << "Deleted " << arr[front] << " from front."
<< endl; if (front == rear) { // Only one element
front = -1;
rear = -1;
} else if (front == capacity - 1) { // Wrap
around front = 0;
} else {
front++;
}
void deleteRear()
{ if (isEmpty())
{
cout << "Deque is empty. Cannot delete from rear." <<
endl; return;
}
cout << "Deleted " << arr[rear] << " from rear."
<< endl; if (front == rear) { // Only one element
front = -1;
rear = -1;
} else if (rear == 0) { // Wrap
around rear = capacity - 1;
} else {
rear--
;
}
void display()
{ if
(isEmpty()) {
cout << "Deque is empty." <<
endl; return;
}
} else {
for (int i = front; i < capacity; i++)
{ cout << arr[i] << " ";
}
for (int i = 0; i <= rear; i++) {
cout << arr[i] << " ";
}
}
cout << endl;
}
};
int choice,
value; do {
cout << "1. Add to Front\n2. Add to Rear\n3. Delete from Front\n4. Delete from Rear\
n5.
Display Deque\n6. Exit\n";
switch
(choice) {
case 1:
cout << "Enter value to add to
front: "; cin >> value;
dq.addFront(valu
e); break;
case 2:
dq.addRear(value);
break;
case 3:
dq.deleteFront
();
break;
case 4:
dq.deleteRear();
break;
case 5:
dq.display(
); break;
case 6:
break;
default:
return 0;
}
OUTPUT
Name: Prajwal
Charthad Roll no:
133
#include
<iostream>
#include <string>
using namespace
std;
class
CircularǪueue {
private:
string* orders;
int front;
int rear;
int capacity;
public:
CircularǪueue(int
size) { capacity =
size;
orders = new
string[capacity]; front = -1;
rear = -1;
}
~CircularǪueue(
) { delete[]
orders;
}
bool isFull() {
return (front == 0 && rear == capacity - 1) || (front == rear + 1);
}
bool isEmpty()
{ return front
== -1;
}
void placeOrder(string
order) { if (isFull()) {
cout << "Order queue is full. Cannot place order: " << order <<
endl; return;
}
orders[rear] = order;
cout << "Order placed: " << order << endl;
void serveOrder()
{ if (isEmpty()) {
cout << "No orders to serve." <<
endl; return;
}
cout << "Serving order: " << orders[front] <<
endl; if (front == rear) { // Only one order
front = -1;
rear = -1;
void displayOrders()
{ if (isEmpty()) {
cout << "No orders in the queue." <<
endl; return;
}
CircularǪueue
cq(maxOrders); int choice;
string order;
do {
switch
(choice) {
case 1:
cout << "Enter order:
"; cin >> order;
cq.placeOrder(ord
er); break;
case 2:
cq.serveOrder
(); break;
case 3:
cq.displayOrders
(); break;
case 4:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice! Please enter again." << endl;
}
} while (choice != 4);
return 0;
}
OUTPUT