DSL Practical
DSL Practical
def get_marks():
n = int(input("Enter the number of students in the class: "))
marks = []
print("Enter marks scored by each student (use -1 for absent
students):")
for _ in range(n):
mark = int(input(f"Enter marks for student {_ + 1}: "))
marks.append(mark)
return marks
def average_score(marks):
valid_marks = [mark for mark in marks if mark != -1]
if valid_marks:
return sum(valid_marks) / len(valid_marks)
else:
return 0
def highest_and_lowest_score(marks):
valid_marks = [mark for mark in marks if mark != -1]
if valid_marks:
return max(valid_marks), min(valid_marks)
else:
return None, None
def count_absent_students(marks):
return marks.count(-1)
def highest_frequency_mark(marks):
from collections import Counter
valid_marks = [mark for mark in marks if mark != -1]
if valid_marks:
frequency = Counter(valid_marks)
most_common = frequency.most_common(1)
return most_common[0][0]
else:
return None
def main():
marks = get_marks()
avg = average_score(marks)
print(f"\nThe average score of the class is: {avg:.2f}")
absent_count = count_absent_students(marks)
print(f"The number of students who were absent: {absent_count}")
freq_mark = highest_frequency_mark(marks)
if freq_mark is not None:
print(f"The mark with the highest frequency is: {freq_mark}")
else:
print("No valid marks to determine the highest frequency mark.")
if __name__ == "__main__":
main()
def string_operations():
user_string = input("Enter a string: ")
words = user_string.split()
longest_word = max(words, key=len)
print(f"Word with the longest length: {longest_word}")
word_count = {}
for word in words:
word_count[word] = word_count.get(word, 0) + 1
print("Occurrences of each word:")
for word, count in word_count.items():
print(f"{word}: {count}")
if __name__ == "__main__":
string_operations()
def print_matrix(matrix):
for row in matrix:
print(row)
if cols_matrix1 != rows_matrix2:
print("Matrix multiplication not possible due to incompatible
dimensions.")
return None
if __name__ == "__main__":
rows, cols = map(int, input("Enter the number of rows and columns for
the matrices: ").split())
print("Matrix 1:")
matrix1 = input_matrix(rows, cols)
print("Matrix 2:")
matrix2 = input_matrix(rows, cols)
print("\nMatrix 1:")
print_matrix(matrix1)
print("\nMatrix 2:")
print_matrix(matrix2)
print("\nAddition of matrices:")
addition_result = add_matrices(matrix1, matrix2)
print_matrix(addition_result)
print("\nSubtraction of matrices:")
subtraction_result = subtract_matrices(matrix1, matrix2)
print_matrix(subtraction_result)
print("\nMultiplication of matrices:")
multiplication_result = multiply_matrices(matrix1, matrix2)
if multiplication_result:
print_matrix(multiplication_result)
arr[-1] = last
def main():
roll_numbers = [105, 123, 132, 107, 120, 115]
if __name__ == "__main__":
main()
offset = -1
if arr[i] < x:
fib = fib1
fib1 = fib2
fib2 = fib - fib1
offset = i
elif arr[i] > x:
fib = fib2
fib1 = fib1 - fib2
fib2 = fib - fib1
else:
return i
return -1
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_index = i
for j in range(i + 1, n):
if arr[j] < arr[min_index]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[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]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
def display_top_scores(arr):
top_scores = arr[-5:][::-1]
print("Top 5 scores:", top_scores)
if __name__ == "__main__":
scores = [78.5, 82.3, 69.8, 90.4, 88.7, 75.2, 95.5, 60.4, 84.6]
selection_sorted_scores = scores.copy()
selection_sort(selection_sorted_scores)
print("Scores sorted using Selection Sort:", selection_sorted_scores)
display_top_scores(selection_sorted_scores)
bubble_sorted_scores = scores.copy()
bubble_sort(bubble_sorted_scores)
print("\nScores sorted using Bubble Sort:", bubble_sorted_scores)
display_top_scores(bubble_sorted_scores)
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j=i-1
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
def shell_sort(arr):
n = len(arr)
gap = n // 2
if __name__ == "__main__":
percentages = [78.5, 88.2, 90.0, 67.5, 85.3, 92.1, 74.6, 89.9, 81.4]
sorted_insertion = percentages[:]
insertion_sort(sorted_insertion)
print("Sorted using Insertion Sort:", sorted_insertion)
display_top_scores(sorted_insertion)
sorted_shell = percentages[:]
shell_sort(sorted_shell)
print("Sorted using Shell Sort:", sorted_shell)
display_top_scores(sorted_shell)
#include <iostream>
#include <string>
using namespace std;
struct Node {
string name;
int prn;
Node* next;
};
class PinnacleClub {
private:
Node* head;
Node* tail;
public:
PinnacleClub() : head(nullptr), tail(nullptr) {}
if (position == "president") {
newNode->next = head;
head = newNode;
if (tail == nullptr) {
tail = head;
}
} else if (position == "secretary") {
if (tail == nullptr) {
head = tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
} else {
if (head == nullptr) {
head = tail = newNode;
} else {
newNode->next = head->next;
head->next = newNode;
}
}
}
if (head->prn == prn) {
head = head->next;
if (head == nullptr) {
tail = nullptr;
}
delete temp;
return;
}
if (temp == nullptr) {
cout << "Member with PRN " << prn << " not found.\n";
return;
}
prev->next = temp->next;
if (temp == tail) {
tail = prev;
}
delete temp;
}
int totalMembers() {
int count = 0;
Node* temp = head;
while (temp != nullptr) {
count++;
temp = temp->next;
}
return count;
}
void displayMembers() {
if (head == nullptr) {
cout << "The list is empty.\n";
return;
}
other.head = nullptr;
other.tail = nullptr;
}
~PinnacleClub() {
while (head != nullptr) {
Node* temp = head;
head = head->next;
delete temp;
}
}
};
int main() {
PinnacleClub divisionA, divisionB;
divisionA.concatenate(divisionB);
return 0;
}
struct Node {
int studentId;
Node* next;
};
return result;
}
insert(setA, 5);
insert(setA, 4);
insert(setA, 3);
insert(setA, 2);
insert(setA, 1);
insert(setB, 8);
insert(setB, 7);
insert(setB, 6);
insert(setB, 5);
insert(setB, 4);
cout << "Students who like Vanilla Ice-cream (Set A): ";
printList(setA);
cout << "Students who like Butterscotch Ice-cream (Set B): ";
printList(setB);
cout << "Students who like both Vanilla and Butterscotch: ";
Node* both = intersection(setA, setB);
printList(both);
cout << "Students who like either Vanilla or Butterscotch, but not both:
";
Node* eitherButNotBoth = difference(setA, setB);
printList(eitherButNotBoth);
cout << "Number of students who like neither Vanilla nor Butterscotch:
";
cout << studentsWhoLikeNeither << endl;
return 0;
}
stack
b) To check whether given string is palindrome or not
#include <iostream>
#include <stack>
#include <cctype>
#include <string>
int start = 0;
int end = filteredStr.length() - 1;
return true;
}
int main() {
std::string inputString;
printOriginalAndReversed(inputString);
if (isPalindrome(inputString)) {
std::cout << "The given string is a palindrome." << std::endl;
} else {
std::cout << "The given string is not a palindrome." << std::endl;
}
return 0;
}
#include <iostream>
#include <stack>
#include <string>
using namespace std;
return s.empty();
}
int main() {
string expression;
if (isWellParenthesized(expression)) {
cout << "The expression is well-parenthesized." << endl;
} else {
cout << "The expression is not well-parenthesized." << endl;
}
return 0;
}
#include <iostream>
#include <list>
std::list<QueueItem> queue;
public:
auto it = queue.begin();
while (it != queue.end() && *it <= newItem) {
++it;
}
queue.insert(it, newItem);
}
T pop() {
if (queue.empty()) {
std::cerr << "Queue is empty!" << std::endl;
exit(1);
}
T data = queue.front().data;
queue.pop_front();
return data;
}
int main() {
PriorityQueue<std::string> pq;
return 0;
}
#include <iostream>
using namespace std;
class PizzaParlor {
private:
int front, rear, size;
string* queue;
public:
PizzaParlor(int m) {
size = m;
queue = new string[size];
front = rear = -1;
}
~PizzaParlor() {
delete[] queue;
}
bool isFull() {
return (rear + 1) % size == front;
}
bool isEmpty() {
return front == -1;
}
void serveOrder() {
if (isEmpty()) {
cout << "No orders to serve." << endl;
} else {
cout << "Serving order: " << queue[front] << endl;
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % size;
}
}
}
void displayOrders() {
if (isEmpty()) {
cout << "No orders in the queue." << endl;
} else {
cout << "Current orders in the queue: ";
int i = front;
while (i != rear) {
cout << queue[i] << " ";
i = (i + 1) % size;
}
cout << queue[rear] << endl;
}
}
};
int main() {
int M;
cout << "Enter the maximum number of orders the pizza parlor can
accept: ";
cin >> M;
PizzaParlor parlor(M);
int choice;
string order;
do {
cout << "\nMenu: \n";
cout << "1. Place an order\n";
cout << "2. Serve an order\n";
cout << "3. Display orders\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter your order: ";
cin.ignore();
getline(cin, order);
parlor.addOrder(order);
break;
case 2:
parlor.serveOrder();
break;
case 3:
parlor.displayOrders();
break;
case 4:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice, please try again.\n";
}
} while (choice != 4);
return 0;
}
#include <iostream>
using namespace std;
class Deque {
private:
int *arr;
int front, rear, size;
int capacity;
public:
Deque(int cap) {
capacity = cap;
arr = new int[capacity];
front = -1;
rear = -1;
size = 0;
}
bool isEmpty() {
return size == 0;
}
bool isFull() {
return size == capacity;
}
void deleteRear() {
if (isEmpty()) {
cout << "Deque is empty. Cannot delete from the rear.\n";
return;
}
if (front == rear) {
front = -1;
rear = -1;
} else if (rear == 0) {
rear = capacity - 1;
} else {
rear--;
}
size--;
}
int getFront() {
if (isEmpty()) {
cout << "Deque is empty.\n";
return -1;
}
return arr[front];
}
int getRear() {
if (isEmpty()) {
cout << "Deque is empty.\n";
return -1;
}
return arr[rear];
}
void display() {
if (isEmpty()) {
cout << "Deque is empty.\n";
return;
}
int i = front;
while (true) {
cout << arr[i] << " ";
if (i == rear) break;
i = (i + 1) % capacity;
}
cout << endl;
}
~Deque() {
delete[] arr;
}
};
int main() {
Deque dq(5);
dq.insertRear(10);
dq.insertRear(20);
dq.insertFront(5);
dq.insertFront(1);
dq.insertRear(30);
cout << "Deque elements: ";
dq.display();
dq.deleteFront();
dq.deleteRear();
return 0;
}