└── DSL ├── GROUP B (Selection Sort And Bubble Sort)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 a) Selection Sort b) Bubble sort and display top five scores.py ├── palindrome(stack).cpp ├── QUEUE JOB.cpp ├── GROUP A- 8. (SADDLE POINT)Write a python program that determines the location of a saddle point of matrix if one exists.py ├── pizzaparlour.cpp ├── GROUP B - 16. (QUICK SORT) 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.py ├── Group A - 2 Write a Python program to store marks scored in subject “Fundamental of Data Structure” by N students in the class. Write functions to compute following: a) The average score of class b) Highest score and lowest score of class c) Count of students who were absent for the test d) Display mark with highest frequency.py ├── ticketbooking.cpp ├── GROUP B - 13.(TERNARY SEARCH) Write a python program to maintain club members, sort on roll numbers in ascending order. Write function “Ternary_Search” to search whether particular student is member of club or not. Ternary search is modified binary search that divides array into 3 halves instead of two.py ├── GROUP A - 7 (MAGIC SQUARE) Write a python Program for magic square. A magic square is an arrangement of the numbers from 1 to N^2 (N-squared) in an NxN matrix, with each number occurring exactly once, and such that the sum of the entries of any row, any column, or any main diagonal is the same. Perform follwing operations a) Generate Magic Square b) Check whether matrix is magic square or not.py ├── (MATRIX OPERATION)GROUP A - 9. Write a python program to compute following computation on matrix: a) Addition of two matrices b) Subtraction of two matrices c) Multiplication of two matrices d) Transpose of a matrix.py ├── (STRING OPERATION)GROUP A - 5 Write a Python program to compute following operations on String: a) To display word with the longest length b) To determines the frequency of occurrence of particular character in the string c) To check whether given string is palindrome or not d) To display index of first appearance of the substring e) To count the occurrences of each word in a given string.py ├── (LINEAR SENTINENT BINARY)GROUP B - 11. a) Write a python program to store roll numbers of student in array who attended training program in random order. Write function for searching whether particular student attended training program or not, using Linear search and Sentinel search. b) Write a python program to store roll numbers of student array who attended training program in sorted order. Write function for searching whether particular student attended training program or not, using Binary search and Fibonacci search.py └── Pinnacle club.cpp /DSL/GROUP B (Selection Sort And Bubble Sort)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 a) Selection Sort b) Bubble sort and display top five scores.py: -------------------------------------------------------------------------------- 1 | 2 | def selection_sort(percentage): 3 | print("Original array is ", percentage) 4 | for i in range(len(percentage)-1): 5 | minimum = i 6 | for j in range(i+1, len(percentage), 1): 7 | if (percentage[j] < percentage[minimum]): 8 | minimum = j 9 | percentage[i],percentage[minimum] = percentage[minimum],percentage[i] 10 | print("Pass " + str(i+1) + ":" , percentage) 11 | return percentage 12 | 13 | def bubble_sort(percentage): 14 | print("Original array is ", percentage) 15 | for i in range(len(percentage)-1): 16 | for j in range(len(percentage)-i-1): 17 | if percentage[j] > percentage[j+1]: 18 | percentage[j],percentage[j+1] = percentage[j+1],percentage[j] 19 | print("Pass " + str(i+1) + ":", percentage) 20 | return percentage 21 | 22 | n = int(input("Enter the number of students here-\n")) 23 | percentage = [] 24 | for i in range(n): 25 | element = float(input("Enter the percentage of the student-\n")) 26 | if element > 0: 27 | percentage.append(element) 28 | choice = int(input("Enter 0 if you want to use selection sort and enter 1 if you want to use bubble sort-\n")) 29 | if choice == 0: 30 | selection_sort(percentage) 31 | else: 32 | bubble_sort(percentage) 33 | print("The percentages of the students after sorting are-", percentage) 34 | print("The percentages of top 5 students are-", percentage[-1:-6:-1]) 35 | -------------------------------------------------------------------------------- /DSL/palindrome(stack).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define MAX 50 6 | 7 | class Stack 8 | { 9 | private: 10 | char data[MAX],str[MAX]; 11 | int top,length,count; 12 | 13 | void pushData(char); 14 | char popData(); 15 | 16 | public: 17 | Stack() 18 | { 19 | top=-1; 20 | length=0; 21 | count=0; 22 | } 23 | void getString(); 24 | void checkPalindrome(); 25 | void extractString(); 26 | void displayReverse(); 27 | }; 28 | 29 | int main() 30 | { 31 | Stack obj; 32 | obj.getString(); 33 | cout<<"\n Extracted string: "; 34 | obj.extractString(); 35 | cout<<"\n Reverse of entered string: "; 36 | obj.displayReverse(); 37 | obj.checkPalindrome(); 38 | return 0; 39 | } 40 | 41 | void Stack::getString() 42 | { 43 | cout<<"\n Enter a String: "; 44 | cin.getline(str,MAX); 45 | 46 | length=strlen(str); 47 | } 48 | 49 | void Stack::extractString() 50 | { 51 | char temp[MAX]; 52 | int i,j; 53 | for(i=0; i=0; i--) 92 | cout< 2 | #define MAX 10 3 | using namespace std; 4 | 5 | 6 | struct queue 7 | { 8 | 9 | int data[MAX]; 10 | int front,rear; 11 | }; 12 | 13 | 14 | class Queue 15 | { 16 | 17 | struct queue q; 18 | public: 19 | Queue() 20 | 21 | { 22 | 23 | q.front=q.rear=-1; 24 | 25 | } 26 | int isempty(); 27 | int isfull(); 28 | void enqueue(int); 29 | int delqueue(); 30 | void display(); 31 | 32 | }; 33 | 34 | 35 | int Queue::isempty() 36 | { 37 | if (q.front==q.rear) 38 | 39 | return 1; 40 | 41 | else 42 | 43 | return 0; 44 | 45 | } 46 | 47 | 48 | int Queue::isfull() 49 | { 50 | 51 | if(q.rear==MAX-1) 52 | 53 | return 1; 54 | 55 | else 56 | 57 | return 0; 58 | 59 | } 60 | 61 | 62 | void Queue::enqueue(int x) 63 | { 64 | 65 | q.data[++q.rear]=x; 66 | 67 | } 68 | 69 | 70 | int Queue::delqueue() 71 | { 72 | 73 | return q.data[++q.front]; 74 | 75 | } 76 | 77 | 78 | void Queue::display() 79 | { 80 | 81 | int i; 82 | cout<<"\n"; 83 | for(i=q.front+1;i<=q.rear;i++) 84 | cout<>ch; 97 | switch(ch) 98 | { 99 | 100 | case 1: if (!obj.isfull()) 101 | { 102 | 103 | cout<<"\n Enter data:"; 104 | cin>>x; 105 | obj.enqueue(x); 106 | } 107 | 108 | else 109 | cout<< "Queue is overflow"; 110 | break; 111 | 112 | 113 | 114 | case 2: if(!obj.isempty()) 115 | cout<<"\n Deleted Element="< m[i][j]: 31 | min_row = m[i][j] 32 | col_ind = j 33 | 34 | # Check if the minimum element of row is also 35 | # the maximum element of column or not 36 | flag = 0 37 | for k in range(0, len(m)): 38 | if min_row < m[k][col_ind]: 39 | flag = 1 40 | break 41 | 42 | if flag == 0: 43 | print("Saddle point is : ", min_row) 44 | print("Saddle point is present at location m[{0}][{1}]".format(i, col_ind)) 45 | return 46 | print("No Saddle point present") 47 | return 48 | 49 | # Read the matrix 50 | def read_mat(): 51 | row = int(input(" Enter the rows in the matrix : ")) 52 | col = int(input(" Enter the columns in the matrix : ")) 53 | # Accept the matrix of size row X col 54 | # Initialize matrix 55 | mat = [] 56 | print("Enter the elements rowwise:") 57 | # For user input 58 | for i in range(0, row): # A for loop for row entries 59 | a = [] 60 | for j in range(0, col): # A for loop for column entries 61 | a.append(int(input("Enter element : "))) 62 | mat.append(a) 63 | return mat, row, col 64 | 65 | matrix = [] 66 | # example of no saddle point 67 | #matrix = [[1, 2, 3], [4, 5, 6], [10, 18, 4]] 68 | # example of saddle point 69 | # matrix = [[1,2,3],[4,5,6],[7,8,9]] 70 | matrix, row, col = read_mat() 71 | 72 | # Print Matrix 73 | print("The matrix is : ") 74 | for i in range(0, row): 75 | for j in range(0, col): 76 | print(matrix[i][j], end = " ") 77 | print() 78 | 79 | saddle(matrix) 80 | 81 | ''' 82 | /*****************OUTPUT***************/ 83 | 84 | Enter the rows in the matrix : 3 85 | Enter the columns in the matrix : 3 86 | Enter the elements rowwise: 87 | Enter element : 1 88 | Enter element : 2 89 | Enter element : 3 90 | Enter element : 4 91 | Enter element : 5 92 | Enter element : 6 93 | Enter element : 7 94 | Enter element : 8 95 | Enter element : 9 96 | The matrix is : 97 | 1 2 3 98 | 4 5 6 99 | 7 8 9 100 | Saddle point is : 7 101 | Saddle point is present at location m[2][0] 102 | 103 | ''' 104 | -------------------------------------------------------------------------------- /DSL/pizzaparlour.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | using namespace std; 6 | 7 | const int MAX=5; 8 | 9 | class PizzaParlour 10 | 11 | { 12 | 13 | int front,rear; 14 | 15 | int orders[MAX]; 16 | 17 | public: 18 | 19 | PizzaParlour() 20 | 21 | { 22 | 23 | front=rear=-1; 24 | 25 | } 26 | 27 | bool addOrder(int data); 28 | 29 | void serveOrder(); 30 | 31 | void display(); 32 | 33 | }; 34 | 35 | bool PizzaParlour::addOrder(int id){ 36 | 37 | if(rear==-1) 38 | 39 | { 40 | 41 | front=rear=0; 42 | 43 | orders[rear]=id; 44 | 45 | return true; 46 | 47 | } 48 | 49 | else 50 | 51 | { 52 | 53 | int pos=(rear+1)%MAX; 54 | 55 | if(pos==front) 56 | 57 | { 58 | 59 | cout<<"\nCafe is Full.Please wait.\n"; 60 | 61 | return false; 62 | 63 | } 64 | 65 | else 66 | 67 | { 68 | 69 | rear=pos; 70 | 71 | orders[rear]=id; 72 | 73 | return true; 74 | 75 | } 76 | 77 | } 78 | 79 | } 80 | 81 | void PizzaParlour::serveOrder() 82 | 83 | { 84 | 85 | if(front==-1) 86 | 87 | { 88 | 89 | cout<<"\n No Orders in Cafe.[Cafe is Empty)\n"; 90 | 91 | return; 92 | 93 | } 94 | 95 | else 96 | 97 | { 98 | 99 | cout<<"\n Order No. "<>ch; 190 | 191 | switch(ch) 192 | 193 | { 194 | 195 | case 1: 196 | 197 | id++; 198 | 199 | if(q.addOrder(id)) 200 | 201 | { 202 | 203 | cout<<"Thank you for order.Order id is : "<= pivot: 36 | high = high - 1 37 | 38 | # Opposite process of the one above 39 | while low <= high and a[low] <= pivot: 40 | low = low + 1 41 | 42 | # We either found a value for both high and low that is out of order 43 | # or low is higher than high, in which case we exit the loop 44 | if low <= high: 45 | a[low], a[high] = a[high], a[low] 46 | # The loop continues 47 | else: 48 | # We exit out of the loop 49 | break 50 | 51 | a[start], a[high] = a[high], a[start] 52 | 53 | return high 54 | 55 | 56 | # Quick Sort function 57 | 58 | def quick_sort(a, start, end): 59 | if start >= end: 60 | return 61 | 62 | p = partition(a, start, end) 63 | quick_sort(a, start, p - 1) 64 | quick_sort(a, p + 1, end) 65 | return a 66 | 67 | 68 | # Top 5 Score 69 | 70 | def top_five(a): 71 | print("Top five score are : ") 72 | cnt = len(a) 73 | 74 | if cnt < 5: 75 | start, stop = cnt - 1, -1 # stop set to -1 as we want to print the 0th element 76 | else: 77 | start, stop = cnt - 1, cnt - 6 78 | 79 | for i in range(start, stop, -1): 80 | print("\t {0:.2f}".format(a[i]), end=" ") 81 | 82 | 83 | # Driver program 84 | if __name__ == "__main__": 85 | 86 | unsort_A = arr.array('f', []) 87 | quick_sort_A = arr.array('f', []) 88 | flag = 1 89 | 90 | while flag == 1: 91 | print("\n 1. Accept array elements \n 2. Display the Elements \n 3. Quick Sort \n 4. exit") 92 | choice = int(input("Enter your choice : ")) 93 | 94 | if choice == 1: 95 | unsort_A = accept_perc() 96 | 97 | elif choice == 2: 98 | print_perc(unsort_A) 99 | 100 | elif choice == 3: 101 | print("Elements after sorting using Insertion Sort :") 102 | quick_sort_A = quick_sort(unsort_A, 0, len(unsort_A) - 1) 103 | print_perc(quick_sort_A) 104 | top_five(quick_sort_A) 105 | 106 | else: 107 | print("Wrong choice") 108 | flag = 0 109 | 110 | ''' 111 | /********************OUTPUT********************/ 112 | 1. Accept array elements 113 | 2. Display the Elements 114 | 3. Quick Sort 115 | 4. exit 116 | Enter your choice : 1 117 | Enter the number of Students : 7 118 | Enter the First Year % of Student[0] : 79.5 119 | Enter the First Year % of Student[1] : 84.92 120 | Enter the First Year % of Student[2] : 65.79 121 | Enter the First Year % of Student[3] : 91.45 122 | Enter the First Year % of Student[4] : 45.34 123 | Enter the First Year % of Student[5] : 58.6 124 | Enter the First Year % of Student[6] : 66.8 125 | 126 | 1. Accept array elements 127 | 2. Display the Elements 128 | 3. Quick Sort 129 | 4. exit 130 | Enter your choice : 2 131 | 79.50 84.92 65.79 91.45 45.34 58.60 66.80 132 | 133 | 1. Accept array elements 134 | 2. Display the Elements 135 | 3. Quick Sort 136 | 4. exit 137 | Enter your choice : 3 138 | Elements after sorting using Insertion Sort : 139 | 45.34 58.60 65.79 66.80 79.50 84.92 91.45 140 | Top five score are : 141 | 91.45 84.92 79.50 66.80 65.79 142 | 1. Accept array elements 143 | 2. Display the Elements 144 | 3. Quick Sort 145 | 4. exit 146 | Enter your choice : 4 147 | Wrong choice 148 | 149 | ''' 150 | -------------------------------------------------------------------------------- /DSL/Group A - 2 Write a Python program to store marks scored in subject “Fundamental of Data Structure” by N students in the class. Write functions to compute following: a) The average score of class b) Highest score and lowest score of class c) Count of students who were absent for the test d) Display mark with highest frequency.py: -------------------------------------------------------------------------------- 1 | # The average score of class 2 | 3 | def average(l): 4 | sum = 0 5 | cnt = 0 6 | for i in range(len(l)): 7 | if l[i] != -999: 8 | sum += l[i] 9 | cnt += 1 10 | 11 | avg = sum / cnt 12 | print("Total Marks are : ", sum) 13 | print("Average Marks are : {:.2f}".format(avg)) 14 | 15 | 16 | # Highest score in the class 17 | 18 | def Maximum(l): 19 | Max = l[0] 20 | for i in range(len(l)): 21 | if l[i] > Max: 22 | Max = l[i] 23 | return (Max) 24 | 25 | 26 | # Lowest score in the class 27 | 28 | 29 | def Minimum(l): 30 | # Assign first element in the array which corresponds to marks of first present student 31 | # This for loop ensures the above condition 32 | 33 | for i in range(len(l)): 34 | if l[i] != -999: 35 | Min = l[i] 36 | break 37 | 38 | for j in range(i + 1, len(l)): 39 | if l[j] != -999 and l[j] < Min: 40 | Min = l[j] 41 | return (Min) 42 | 43 | 44 | # Count of students who were absent for the test 45 | 46 | def absentCnt(l): 47 | cnt = 0 48 | for i in range(len(l)): 49 | if l[i] == -999: 50 | cnt += 1 51 | return (cnt) 52 | 53 | 54 | # Display mark with highest frequency 55 | # refeence link : https://www.youtube.com/watch?v=QrIXGqvvpk4&t=422s 56 | def maxFrequency(l): 57 | i = 0 58 | Max = 0 59 | print(" Marks ----> frequency count ") 60 | for ele in l: 61 | if l.index(ele) == i: 62 | print(ele, "---->", l.count(ele)) 63 | if l.count(ele) > Max: 64 | Max = l.count(ele) 65 | mark = ele 66 | i += 1 67 | return (mark, Max) 68 | 69 | 70 | # Input the number of students and their corresponding marks in FDS 71 | 72 | marksInFDS = [] 73 | noStudents = int(input("Enter total number of students : ")) 74 | for i in range(noStudents): 75 | marks = int(input("Enter marks of Student " + str(i + 1) + " : ")) 76 | marksInFDS.append(marks) 77 | 78 | flag = 1 79 | while flag == 1: 80 | print("/*************MENU**************/") 81 | print("1. The average score of class ") 82 | print("2. Highest score and lowest score of class ") 83 | print("3. Count of students who were absent for the test ") 84 | print("4. Display mark with highest frequency ") 85 | print("5. Exit ") 86 | choice = int(input("Enter your choice : ")) 87 | 88 | if choice == 1: 89 | average(marksInFDS) 90 | 91 | elif choice == 2: 92 | print("Highest score in the class is : ", Maximum(marksInFDS)) 93 | print("Lowest score in the class is : ", Minimum(marksInFDS)) 94 | 95 | elif choice == 3: 96 | print("Count of students who were absent for the test is : ", absentCnt(marksInFDS)) 97 | 98 | elif choice == 4: 99 | mark, count = maxFrequency(marksInFDS) 100 | print("Highest frequency of marks {0} is {1} ".format(mark, count)) 101 | 102 | else: 103 | print("Wrong choice") 104 | flag = 0 105 | 106 | ''' 107 | /*********************OUTPUT********************/ 108 | Enter marks of Student 1 : 87 109 | Enter marks of Student 2 : 68 110 | Enter marks of Student 3 : -999 111 | Enter marks of Student 4 : 90 112 | Enter marks of Student 5 : 68 113 | /*************MENU**************/ 114 | 1. The average score of class 115 | 2. Highest score and lowest score of class 116 | 3. Count of students who were absent for the test 117 | 4. Display mark with highest frequency 118 | 5. Exit 119 | Enter your choice : 1 120 | Total Marks are : 313 121 | Average Marks are : 78.25 122 | /*************MENU**************/ 123 | 1. The average score of class 124 | 2. Highest score and lowest score of class 125 | 3. Count of students who were absent for the test 126 | 4. Display mark with highest frequency 127 | 5. Exit 128 | Enter your choice : 2 129 | Highest score in the class is : 90 130 | Lowest score in the class is : 68 131 | /*************MENU**************/ 132 | 1. The average score of class 133 | 2. Highest score and lowest score of class 134 | 3. Count of students who were absent for the test 135 | 4. Display mark with highest frequency 136 | 5. Exit 137 | Enter your choice : 3 138 | Count of students who were absent for the test is : 1 139 | /*************MENU**************/ 140 | 1. The average score of class 141 | 2. Highest score and lowest score of class 142 | 3. Count of students who were absent for the test 143 | 4. Display mark with highest frequency 144 | 5. Exit 145 | Enter your choice : 4 146 | Marks ----> frequency count 147 | 87 ----> 1 148 | 68 ----> 2 149 | -999 ----> 1 150 | 90 ----> 1 151 | Highest frequency of marks 68 is 2 152 | /*************MENU**************/ 153 | 1. The average score of class 154 | 2. Highest score and lowest score of class 155 | 3. Count of students who were absent for the test 156 | 4. Display mark with highest frequency 157 | 5. Exit 158 | Enter your choice : 5 159 | Wrong choice 160 | -------------------------------------------------------------------------------- /DSL/ticketbooking.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct node 6 | 7 | { 8 | 9 | int seatc,seatr; 10 | 11 | string status; 12 | 13 | struct node *next ,*prev; 14 | 15 | }*head[10],*last[10]; 16 | 17 | 18 | 19 | class ticket 20 | 21 | { 22 | 23 | public: 24 | 25 | ticket() 26 | 27 | { 28 | 29 | for(int i=1 ; i<=10 ; i++) 30 | 31 | { 32 | 33 | head[i]=last[i]=NULL; 34 | 35 | struct node* temp; 36 | 37 | for(int j=1 ; j<=7 ; j++) 38 | 39 | { 40 | 41 | temp=create_node(i,j); 42 | 43 | if(head[i]==last[i] && head[i]==NULL) 44 | 45 | { 46 | 47 | head[i]=last[i]=temp; 48 | 49 | head[i]->next=last[i]->next=NULL; 50 | 51 | head[i]->prev=last[i]->prev=NULL; 52 | 53 | 54 | 55 | } 56 | 57 | Else //Insertion at beginning 58 | 59 | { 60 | 61 | temp->next=head[i]; 62 | 63 | head[i]->prev=temp; 64 | 65 | head[i]=temp; 66 | 67 | head[i]->prev=last[i]; 68 | 69 | last[i]->next=head[i]; 70 | 71 | } 72 | 73 | } 74 | 75 | } 76 | 77 | } 78 | 79 | 80 | 81 | node* create_node(int x,int y) 82 | 83 | { 84 | 85 | struct node*temp; 86 | 87 | temp=new(struct node); 88 | 89 | if(temp==NULL) 90 | 91 | { 92 | 93 | cout<<"\nMemory not allocated"; 94 | 95 | return 0; 96 | 97 | } 98 | 99 | else 100 | 101 | { 102 | 103 | temp->seatr=x; 104 | 105 | temp->seatc=y; 106 | 107 | temp->status="A"; 108 | 109 | temp->next=NULL; 110 | 111 | temp->prev=NULL; 112 | 113 | return temp; 114 | 115 | } 116 | 117 | 118 | 119 | } 120 | 121 | void book() 122 | 123 | { 124 | 125 | int x,y; 126 | 127 | cout<<"\nEnter row and column"; 128 | 129 | cin>>x>>y; 130 | 131 | struct node* temp; 132 | 133 | temp=head[x]; 134 | 135 | for(int i=1 ; i<=7 ; i++) 136 | 137 | { 138 | 139 | if(temp->seatc==y) 140 | 141 | { 142 | 143 | if(temp->status=="A") 144 | 145 | { 146 | 147 | temp->status="B"; 148 | 149 | } 150 | 151 | else 152 | 153 | { 154 | 155 | cout<<"\nSORRY !! Already booked!!"; 156 | 157 | } 158 | 159 | } 160 | 161 | temp=temp->next; 162 | 163 | } 164 | 165 | display(); 166 | 167 | } 168 | 169 | 170 | 171 | void cancel(){ 172 | 173 | int x,y; 174 | 175 | cout<<"\nEnter row and column to cancel booking : "; 176 | 177 | cin>>x>>y; 178 | 179 | struct node* temp; 180 | 181 | temp=head[x]; 182 | 183 | for(int i=1 ; i<=7 ; i++) 184 | 185 | { 186 | 187 | if(temp->seatc==y) 188 | 189 | { 190 | 191 | if(temp->status=="B") 192 | 193 | { 194 | 195 | temp->status="A"; 196 | 197 | } 198 | 199 | else 200 | 201 | { 202 | 203 | cout<<"\nSORRY !! Already unbooked!!"; 204 | 205 | } 206 | 207 | } 208 | 209 | temp=temp->next; 210 | 211 | } 212 | 213 | display(); 214 | 215 | } 216 | 217 | 218 | 219 | void display() 220 | 221 | { 222 | 223 | struct node* temp; 224 | 225 | for(int j=1 ; j<=10 ; j++) 226 | 227 | { 228 | 229 | temp=head[j]; 230 | 231 | for(int i=1 ; i<=7 ; i++) 232 | 233 | { 234 | 235 | cout<seatr<<","<seatc<status<<"\t"; 236 | 237 | temp=temp->next; 238 | 239 | } 240 | 241 | cout<<"\n"; 242 | 243 | } 244 | 245 | } 246 | 247 | 248 | 249 | }; 250 | 251 | 252 | 253 | int main() 254 | 255 | { 256 | 257 | ticket t; 258 | 259 | int ch; 260 | 261 | t.display(); 262 | 263 | do{ 264 | 265 | cout<<"\n1.Book Ticket \n2.Cancel Booking \n3.EXIT"; 266 | 267 | cin>>ch; 268 | 269 | switch(ch) 270 | 271 | { 272 | 273 | case 1:t.book();break; 274 | 275 | case 2:t.cancel();break; 276 | 277 | } 278 | 279 | }while(ch!=3); 280 | 281 | 282 | 283 | return 0; 284 | 285 | } 286 | -------------------------------------------------------------------------------- /DSL/GROUP B - 13.(TERNARY SEARCH) Write a python program to maintain club members, sort on roll numbers in ascending order. Write function “Ternary_Search” to search whether particular student is member of club or not. Ternary search is modified binary search that divides array into 3 halves instead of two.py: -------------------------------------------------------------------------------- 1 | import array as arr 2 | 3 | # Accept the Roll Numbers of the students 4 | 5 | def accept_perc(): 6 | a = arr.array('I', []) 7 | no_stud = int(input("Enter the number of Students : ")) 8 | for i in range(0, no_stud): 9 | a.append(int(input("Enter the Roll Number : "))) 10 | return a 11 | 12 | 13 | # Print the Roll Numbers of the Students 14 | 15 | def print_roll(a): 16 | for i in range(0, len(a)): 17 | print("\t", a[i], end=" ") 18 | print() 19 | 20 | 21 | # Insertion sort 22 | 23 | def ins_sort(a): 24 | # Traverse through 1 to len(a) 25 | for i in range(1, len(a)): 26 | 27 | key = a[i] 28 | 29 | # Move elements of a[0..i-1], that are 30 | # greater than key, to one position ahead 31 | # of their current position 32 | j = i - 1 33 | while j >= 0 and key < a[j]: 34 | a[j + 1] = a[j] 35 | j -= 1 36 | a[j + 1] = key 37 | return a 38 | 39 | 40 | # Non-Recursive Ternary Search function 41 | 42 | def ternarySearch_NR(arr, to_find): 43 | left = 0 44 | right = len(arr) - 1 45 | while left <= right: 46 | mid1 = left + (right - left) // 3 47 | mid2 = left + 2 * (right - left) // 3 48 | if to_find == arr[left]: 49 | return left 50 | elif to_find == arr[right]: 51 | return right 52 | elif to_find < arr[left] or to_find > arr[right]: 53 | return -1 54 | elif to_find <= arr[mid1]: 55 | right = mid1 56 | elif to_find > arr[mid1] and to_find <= arr[mid2]: 57 | left = mid1 + 1 58 | right = mid2 59 | else: 60 | left = mid2 + 1 61 | return -1 62 | 63 | 64 | # Recursive Ternary Search 65 | 66 | def ternarySearch_R(arr, left, right, to_find): 67 | if (right >= left): 68 | mid1 = left + (right - left) // 3; # // operator returns the floor not float 69 | mid2 = right - (right - left) // 3; 70 | if (arr[mid1] == to_find): 71 | return mid1; 72 | if (arr[mid2] == to_find): 73 | return mid2; 74 | 75 | if (to_find < arr[mid1]): 76 | return ternarySearch_R(arr, left, mid1 - 1, to_find); 77 | elif (to_find > arr[mid2]): 78 | return ternarySearch_R(arr, mid2 + 1, right, to_find); 79 | else: 80 | return ternarySearch_R(arr, mid1 + 1, mid2 - 1, to_find); 81 | return -1; 82 | 83 | 84 | # Driver program 85 | if __name__ == "__main__": 86 | 87 | unsort_A = arr.array('I', []) # array of unsigned integer 88 | ins_sort_A = arr.array('I', []) 89 | flag = 1 90 | 91 | while flag == 1: 92 | menu = "1. Accept Student Roll Numbers \n" \ 93 | "2. Display the Roll Numbers \n" \ 94 | "3. Sort Roll Numbers \n" \ 95 | "4. Ternary Search (Non-Recursive) \n" \ 96 | "5. Ternary Search (Recursive) \n" \ 97 | "6. Exit" 98 | print(menu) 99 | # print("\n 1. Accept Student Roll Numbers \n 2. Display the Roll Numbers \n 3. Sort Roll Numbers \n 4. Ternary Search (Non-Recursive) \n 5. Ternary Search (Recursive) \n 6. exit") 100 | choice = int(input("Enter your choice : ")) 101 | 102 | if choice == 1: 103 | unsort_A = accept_perc() 104 | 105 | elif choice == 2: 106 | print_roll(unsort_A) 107 | 108 | elif choice == 3: 109 | print("Elements after sorting using Insertion Sort :") 110 | ins_sort_A = ins_sort(unsort_A) 111 | print_roll(ins_sort_A) 112 | 113 | elif choice == 4: 114 | roll = int(input("Enter the Roll Number to be search : ")) 115 | 116 | index = ternarySearch_NR(ins_sort_A, roll) 117 | if index != -1: 118 | print("The element", roll, "is at the index", index) 119 | else: 120 | print("Element", roll, "not found!") 121 | 122 | elif choice == 5: 123 | roll = int(input("Enter the Roll Number to be search : ")) 124 | left = 0 125 | right = len(ins_sort_A) - 1 126 | index = ternarySearch_R(ins_sort_A, left, right, roll) 127 | if index != -1: 128 | print("The element", roll, "is at the index", index) 129 | else: 130 | print("Element", roll, "not found!") 131 | 132 | else: 133 | print("Wrong choice") 134 | flag = 0 135 | 136 | ''' 137 | /****************OUTPUT*******************/ 138 | 1. Accept Student Roll Numbers 139 | 2. Display the Roll Numbers 140 | 3. Sort Roll Numbers 141 | 4. Ternary Search (Non-Recursive) 142 | 5. Ternary Search (Recursive) 143 | 6. Exit 144 | 145 | Enter your choice : 1 146 | Enter the number of Students : 4 147 | Enter the Roll Number : 78 148 | Enter the Roll Number : 56 149 | Enter the Roll Number : 49 150 | Enter the Roll Number : 35 151 | 152 | Enter your choice : 3 153 | Elements after sorting using Insertion Sort : 154 | 35 49 56 78 155 | 156 | Enter your choice : 4 157 | Enter the Roll Number to be search : 78 158 | The element 78 is at the index 3 159 | 160 | Enter your choice : 5 161 | Enter the Roll Number to be search : 49 162 | The element 49 is at the index 1 163 | 164 | Enter your choice : 6 165 | Wrong choice 166 | 167 | ''' 168 | -------------------------------------------------------------------------------- /DSL/GROUP A - 7 (MAGIC SQUARE) Write a python Program for magic square. A magic square is an arrangement of the numbers from 1 to N^2 (N-squared) in an NxN matrix, with each number occurring exactly once, and such that the sum of the entries of any row, any column, or any main diagonal is the same. Perform follwing operations a) Generate Magic Square b) Check whether matrix is magic square or not.py: -------------------------------------------------------------------------------- 1 | def generateSquare(n): 2 | # 2-D array with all 3 | # slots set to 0 4 | magicSquare = [[0 for x in range(n)] 5 | for y in range(n)] 6 | 7 | # initialize position of 1 8 | i = n / 2 9 | j = n - 1 10 | 11 | # Fill the magic square 12 | # by placing values 13 | num = 1 14 | while num <= (n * n): 15 | if i == -1 and j == n: # 3rd condition 16 | j = n - 2 17 | i = 0 18 | else: 19 | 20 | # next number goes out of 21 | # right side of square 22 | if j == n: 23 | j = 0 24 | 25 | # next number goes 26 | # out of upper side 27 | if i < 0: 28 | i = n - 1 29 | 30 | if magicSquare[int(i)][int(j)]: # 2nd condition 31 | j = j - 2 32 | i = i + 1 33 | continue 34 | else: 35 | magicSquare[int(i)][int(j)] = num 36 | num = num + 1 37 | 38 | j = j + 1 39 | i = i - 1 # 1st condition 40 | print("Magic Squre for n =", n) 41 | print("Sum of each row or column or diagonal i.e Magic Number is : {0:.0f}".format(n * (n * n + 1) / 2), "\n") 42 | print_mat(magicSquare, n) 43 | 44 | 45 | # Printing magic square 46 | def print_mat(t, n): 47 | for i in range(0, n): 48 | for j in range(0, n): 49 | print(t[i][j], end=" ") 50 | print() 51 | 52 | 53 | # Determine whether a given matrix is magic matrix or not 54 | 55 | def isMagicSquare(mat, n): 56 | # calculate the sum of the prime diagonal 57 | s = 0 58 | 59 | for i in range(0, n): 60 | s = s + mat[i][i] 61 | 62 | # Calculate the sum of the secondary diagonal 63 | s2 = 0 64 | for i in range(0, n): 65 | s2 = s2 + mat[i][n - i - 1] 66 | 67 | if (s != s2): 68 | return False 69 | 70 | # For sums of Rows 71 | for i in range(0, n): 72 | rowSum = 0; 73 | for j in range(0, n): 74 | rowSum += mat[i][j] 75 | 76 | # check if every row sum is 77 | # equal to prime diagonal sum 78 | if (rowSum != s): 79 | return False 80 | 81 | # For sums of Columns 82 | for i in range(0, n): 83 | colSum = 0 84 | for j in range(0, n): 85 | colSum += mat[j][i] 86 | 87 | # check if every column sum is 88 | # equal to prime diagonal sum 89 | if (s != colSum): 90 | return False 91 | 92 | return True 93 | 94 | 95 | flag = 1 96 | 97 | while flag == 1: 98 | menu = " /************MENU***********/ \n" \ 99 | "1. Generate Magic Square \n" \ 100 | "2. Determine whether matrix is magic square or not \n" \ 101 | "3. Exit" 102 | print(menu) 103 | choice = int(input("Enter your choice : ")) 104 | if choice == 1: 105 | # Works only when n is odd 106 | n = int(input(" Enter the size of Magic square : ")) 107 | generateSquare(n) 108 | 109 | elif choice == 2: 110 | # Works only when n is odd 111 | n = int(input(" Enter the size of Magic square : ")) 112 | # Accept the matrix of size n X n 113 | # Initialize matrix 114 | mat = [] 115 | print("Enter the elements rowwise:") 116 | 117 | # For user input 118 | for i in range(0, n): # A for loop for row entries 119 | a = [] 120 | for j in range(0, n): # A for loop for column entries 121 | a.append(int(input("Enter element : "))) 122 | mat.append(a) 123 | 124 | if (isMagicSquare(mat, n)): 125 | print("Magic Square") 126 | else: 127 | print("Not a magic Square") 128 | 129 | else: 130 | print("Wrong choice") 131 | flag = 0 132 | 133 | ''' 134 | /****************OUTPUT**************/ 135 | C:\Users\admin\PycharmProjects\DSL_GPA\venv\Scripts\python.exe C:/Users/admin/PycharmProjects/DSL_GPA/GPA_7.py 136 | /************MENU***********/ 137 | 1. Generate Magic Square 138 | 2. Determine whether matrix is magic square or not 139 | 3. Exit 140 | Enter your choice : 1 141 | Enter the size of Magic square : 3 142 | Magic Squre for n = 3 143 | Sum of each row or column or diagonal i.e Magic Number is : 15 144 | 145 | 2 7 6 146 | 9 5 1 147 | 4 3 8 148 | /************MENU***********/ 149 | 1. Generate Magic Square 150 | 2. Determine whether matrix is magic square or not 151 | 3. Exit 152 | Enter your choice : 2 153 | Enter the size of Magic square : 3 154 | Enter the elements rowwise: 155 | Enter element : 1 156 | Enter element : 2 157 | Enter element : 3 158 | Enter element : 4 159 | Enter element : 5 160 | Enter element : 6 161 | Enter element : 7 162 | Enter element : 8 163 | Enter element : 9 164 | Not a magic Square 165 | /************MENU***********/ 166 | 1. Generate Magic Square 167 | 2. Determine whether matrix is magic square or not 168 | 3. Exit 169 | Enter your choice : 2 170 | Enter the size of Magic square : 3 171 | Enter the elements rowwise: 172 | Enter element : 2 173 | Enter element : 7 174 | Enter element : 6 175 | Enter element : 9 176 | Enter element : 5 177 | Enter element : 1 178 | Enter element : 4 179 | Enter element : 3 180 | Enter element : 8 181 | Magic Square 182 | /************MENU***********/ 183 | 1. Generate Magic Square 184 | 2. Determine whether matrix is magic square or not 185 | 3. Exit 186 | Enter your choice : 3 187 | Wrong choice 188 | 189 | ''' 190 | -------------------------------------------------------------------------------- /DSL/(MATRIX OPERATION)GROUP A - 9. Write a python program to compute following computation on matrix: a) Addition of two matrices b) Subtraction of two matrices c) Multiplication of two matrices d) Transpose of a matrix.py: -------------------------------------------------------------------------------- 1 | # Print the matrix 2 | 3 | def print_matrix(matrix): 4 | for i in range(len(matrix)): 5 | for j in range(len(matrix[0])): 6 | print("\t", matrix[i][j], end=" ") 7 | print("\n") 8 | 9 | # Initialize the matrix to zero 10 | 11 | def init_matrix(matrix, m, n): 12 | matrix = [[0 for j in range(0, n)] for i in range(0, m)] 13 | return matrix 14 | 15 | # Input the Matrix elements 16 | def read_matrix(): 17 | mat = [] 18 | r = int(input("Enter number of rows in First Matrix: ")) 19 | c = int(input("Enter number of columns in First Matrix : ")) 20 | # in python initilization is needed before indexing. 21 | mat = init_matrix(mat, r, c) # matrix 1 initialization with 0s 22 | for i in range(0, r): 23 | for j in range(0, c): 24 | mat[i][j] = int(input("Enter an element : ")) 25 | return mat, r, c 26 | 27 | # Addition of two matrices 28 | 29 | def mat_add(m1, m2, res, m, n): 30 | for i in range(0, m): 31 | for j in range(0, n): 32 | res[i][j] = m1[i][j] + m2[i][j] 33 | return res 34 | 35 | # Subtraction of two matrices 36 | 37 | def mat_sub(m1, m2, res, m, n): 38 | for i in range(0, m): 39 | for j in range(0, n): 40 | res[i][j] = m1[i][j] - m2[i][j] 41 | return res 42 | 43 | # Multiplication of two matrices 44 | 45 | def mat_mul(m1, m2, res, r1, c1, c2): 46 | # for multiplication 47 | # i will run throgh each row of matrix1 48 | for i in range(0, r1): 49 | # k will run through each column of matrix 1 50 | for k in range(0, c2): 51 | # j will run throguh each column of matrix 2 52 | for j in range(0, c1): 53 | res[i][k] += m1[i][j] * m2[j][k] 54 | return res 55 | 56 | # Transpose of matrix1 57 | 58 | def trans_mat(m1, res, r1, c1): 59 | for i in range(0, r1): 60 | for j in range(0, c1): 61 | res[j][i] = m1[i][j] 62 | return res 63 | 64 | matrix1 = [] 65 | matrix2 = [] 66 | res_matrix = [] 67 | print(" First Matrix : ") 68 | matrix1, r1, c1 = read_matrix() 69 | print(" Second Matrix : ") 70 | matrix2, r2, c2 = read_matrix() 71 | 72 | res_matrix = init_matrix(res_matrix, r1, c2) # matrix for storing result 73 | # print input matrices 74 | print(" matrix 1") 75 | print_matrix(matrix1) 76 | print(" matrix 2") 77 | print_matrix(matrix2) 78 | 79 | flag = 1 80 | while flag: 81 | print("/*********MENU********/ \n") 82 | print(" 1. Addition of two matrices ") 83 | print(" 2. Subtraction of two matrices ") 84 | print(" 3. Multiplication of two matrices ") 85 | print(" 4. Transpose of a matrix ") 86 | choice = int(input("Enter Your Choice : ")) 87 | 88 | if choice == 1: 89 | # printing Addition matrix 90 | if r1 == r2 and c1 == c2: 91 | print("resultant matrix after addition") 92 | res_matrix = mat_add(matrix1, matrix2, res_matrix, r1, c1) 93 | print_matrix(res_matrix) 94 | else: 95 | print("Addition can't be performed ") 96 | 97 | elif choice == 2: 98 | if r1 == r2 and c1 == c2: 99 | print("resultant matrix after subtraction") 100 | res_matrix = mat_sub(matrix1, matrix2, res_matrix, r1, c1) 101 | print_matrix(res_matrix) 102 | else: 103 | print("Subtraction can't be performed ") 104 | 105 | elif choice == 3: 106 | if c1 == r2: 107 | print("resultant matrix after Multiplication ") 108 | res_matrix = mat_mul(matrix1, matrix2, res_matrix, r1, c1, c2) 109 | print_matrix(res_matrix) 110 | else: 111 | print("Multiplication can't be performed ") 112 | 113 | elif choice == 4: 114 | print("Transpose of First Matrix is ") 115 | res_matrix1 = [] 116 | res_matrix1 = init_matrix(res_matrix1, c1, r1) 117 | res_matrix = trans_mat(matrix1, res_matrix1, r1, c1) 118 | print_matrix(res_matrix) 119 | 120 | else: 121 | print(" Wrong choice ") 122 | flag = 0 123 | 124 | ''' 125 | /***************OUTPUT****************/ 126 | 127 | First Matrix : 128 | Enter number of rows in First Matrix: 2 129 | Enter number of columns in First Matrix : 2 130 | Enter an element : 1 131 | Enter an element : 2 132 | Enter an element : 3 133 | Enter an element : 4 134 | Second Matrix : 135 | Enter number of rows in First Matrix: 2 136 | Enter number of columns in First Matrix : 2 137 | Enter an element : 4 138 | Enter an element : 3 139 | Enter an element : 2 140 | Enter an element : 1 141 | matrix 1 142 | 1 2 143 | 144 | 3 4 145 | 146 | matrix 2 147 | 4 3 148 | 149 | 2 1 150 | 151 | /*********MENU********/ 152 | 153 | 1. Addition of two matrices 154 | 2. Subtraction of two matrices 155 | 3. Multiplication of two matrices 156 | 4. Transpose of a matrix 157 | Enter Your Choice : 1 158 | resultant matrix after addition 159 | 5 5 160 | 161 | 5 5 162 | 163 | /*********MENU********/ 164 | 165 | 1. Addition of two matrices 166 | 2. Subtraction of two matrices 167 | 3. Multiplication of two matrices 168 | 4. Transpose of a matrix 169 | Enter Your Choice : 2 170 | resultant matrix after subtraction 171 | -3 -1 172 | 173 | 1 3 174 | 175 | /*********MENU********/ 176 | 177 | 1. Addition of two matrices 178 | 2. Subtraction of two matrices 179 | 3. Multiplication of two matrices 180 | 4. Transpose of a matrix 181 | Enter Your Choice : 3 182 | resultant matrix after Multiplication 183 | 5 4 184 | 185 | 21 16 186 | 187 | /*********MENU********/ 188 | 189 | 1. Addition of two matrices 190 | 2. Subtraction of two matrices 191 | 3. Multiplication of two matrices 192 | 4. Transpose of a matrix 193 | Enter Your Choice : 4 194 | Transpose of First Matrix is 195 | 1 3 196 | 197 | 2 4 198 | 199 | /*********MENU********/ 200 | 201 | 1. Addition of two matrices 202 | 2. Subtraction of two matrices 203 | 3. Multiplication of two matrices 204 | 4. Transpose of a matrix 205 | Enter Your Choice : 5 206 | Wrong choice 207 | 208 | ''' 209 | -------------------------------------------------------------------------------- /DSL/(STRING OPERATION)GROUP A - 5 Write a Python program to compute following operations on String: a) To display word with the longest length b) To determines the frequency of occurrence of particular character in the string c) To check whether given string is palindrome or not d) To display index of first appearance of the substring e) To count the occurrences of each word in a given string.py: -------------------------------------------------------------------------------- 1 | # Count the number of characters in the longest word in the sentence 2 | 3 | def LongestWordLength(str): 4 | n = len(str) 5 | res = 0; 6 | curr_len = 0; 7 | longest = "" 8 | 9 | for i in range(0, n): 10 | 11 | # If current character is not end of current word. 12 | if (str[i] != ' '): 13 | longest += str[i] 14 | curr_len += 1 15 | 16 | # If end of word is found 17 | else: 18 | res = max(res, curr_len) 19 | curr_len = 0 20 | longest = "" 21 | 22 | # We do max one more time to consider last word as there won't be any space 23 | # after last word. 24 | return max(res, curr_len), longest 25 | 26 | # Determines the frequency of occurrence of particular character in the string 27 | # Iterate the entire string for that particular character and then increase the 28 | # counter when we encounter the particular character. 29 | 30 | def countOccurance(str, char): 31 | count = 0 32 | for i in str: 33 | if i == char: 34 | count = count + 1 35 | return char, count 36 | 37 | 38 | # Checks string is palindrome or not 39 | 40 | def isPalindrome(str): 41 | # Run loop from 0 to len/2 42 | for i in range(0, int(len(str) / 2)): 43 | if str[i] != str[len(str) - i - 1]: 44 | return False 45 | return True 46 | 47 | 48 | # Find all occurrences of substring in a string 49 | 50 | def subStr(mainstr, sub): 51 | res = [] 52 | flag = 0 53 | k = 0 54 | for i in range(0, len(mainstr)): 55 | k = i 56 | flag = 0 57 | for j in range(len(sub)): 58 | if mainstr[k] != sub[j]: 59 | flag = 1 60 | if flag: 61 | break 62 | k = k + 1 63 | if flag == 0: 64 | res.append(i) 65 | return res 66 | 67 | 68 | # Find frequency of each word in the string 69 | def freq(str): 70 | # break the string into list of words 71 | str = str.split() 72 | str2 = [] 73 | 74 | # loop till string values present in list str 75 | for i in str: 76 | 77 | # checking for the duplicacy 78 | if i not in str2: 79 | # insert value in str2 80 | str2.append(i) 81 | 82 | print("Frequency of each word in the string is") 83 | for i in range(0, len(str2)): 84 | # count the frequency of each word(present in str2) in str and print 85 | print('Frequency of', str2[i], 'is :', str.count(str2[i])) 86 | 87 | 88 | flag = 1 89 | while flag == 1: 90 | print("/*************MENU**************/") 91 | print("1. To display word with the longest length :") 92 | print("2. To determines the frequency of occurrence of particular character in the string ") 93 | print("3. To check whether given string is palindrome or not :") 94 | print("4. To display index of first appearance of the substring :") 95 | print("5. To count the occurrences of each word in a given string :") 96 | print("6. Exit ") 97 | choice = int(input("Enter your choice : ")) 98 | 99 | if choice == 1: 100 | # s = input("Enter the String to find the longest word : ") 101 | s = "I am working in the department of computer engineering" 102 | l, str = LongestWordLength(s) 103 | print("Longest word is {1} of length {0}".format(l, str)) 104 | 105 | elif choice == 2: 106 | # s = input("Enter the String to find the occurance of a particular character : ") 107 | # c = input("Enter the character whose occurance is to be find : ") 108 | c = "r" 109 | c, cnt = countOccurance(s, c) 110 | print("Character {0} occured {1} times in string : {2} ".format(c, cnt, s)) 111 | 112 | elif choice == 3: 113 | s = input("Enter the String to check palindrome: ") 114 | if isPalindrome(s): 115 | print("Yes string {0} is palindrom ".format(s)) 116 | else: 117 | print("No string {0} is not palindrom ".format(s)) 118 | 119 | elif choice == 4: 120 | # s = input("Enter the main String : ") 121 | # s1 = input("Enter the substring : ") 122 | s = "I am working in the department of computer engineering" 123 | s1 = "ing" 124 | print("starting indices of substring {1} in string {0} is {2} ".format(s, s1, subStr(s, s1))) 125 | 126 | elif choice == 5: 127 | s = "apple mango apple orange orange apple guava mango mango" 128 | freq(s) 129 | 130 | else: 131 | print("Wrong choice") 132 | flag = 0 133 | 134 | ''' 135 | /********************OUTPUT***********************/ 136 | /*************MENU**************/ 137 | 1. To display word with the longest length : 138 | 2. To determines the frequency of occurrence of particular character in the string 139 | 3. To check whether given string is palindrome or not : 140 | 4. To display index of first appearance of the substring : 141 | 5. To count the occurrences of each word in a given string : 142 | 6. Exit 143 | Enter your choice : 1 144 | Longest word is engineering of length 11 145 | /*************MENU**************/ 146 | 1. To display word with the longest length : 147 | 2. To determines the frequency of occurrence of particular character in the string 148 | 3. To check whether given string is palindrome or not : 149 | 4. To display index of first appearance of the substring : 150 | 5. To count the occurrences of each word in a given string : 151 | 6. Exit 152 | Enter your choice : 2 153 | Character r occured 4 times in string : I am working in the department of computer engineering 154 | /*************MENU**************/ 155 | 1. To display word with the longest length : 156 | 2. To determines the frequency of occurrence of particular character in the string 157 | 3. To check whether given string is palindrome or not : 158 | 4. To display index of first appearance of the substring : 159 | 5. To count the occurrences of each word in a given string : 160 | 6. Exit 161 | Enter your choice : 3 162 | Enter the String to check palindrome: radar 163 | Yes string radar is palindrom 164 | /*************MENU**************/ 165 | 1. To display word with the longest length : 166 | 2. To determines the frequency of occurrence of particular character in the string 167 | 3. To check whether given string is palindrome or not : 168 | 4. To display index of first appearance of the substring : 169 | 5. To count the occurrences of each word in a given string : 170 | 6. Exit 171 | Enter your choice : 3 172 | Enter the String to check palindrome: computer 173 | No string computer is not palindrom 174 | /*************MENU**************/ 175 | 1. To display word with the longest length : 176 | 2. To determines the frequency of occurrence of particular character in the string 177 | 3. To check whether given string is palindrome or not : 178 | 4. To display index of first appearance of the substring : 179 | 5. To count the occurrences of each word in a given string : 180 | 6. Exit 181 | Enter your choice : 4 182 | starting indices of substring ing in string I am working in the department of computer engineering is [9, 51] 183 | /*************MENU**************/ 184 | 1. To display word with the longest length : 185 | 2. To determines the frequency of occurrence of particular character in the string 186 | 3. To check whether given string is palindrome or not : 187 | 4. To display index of first appearance of the substring : 188 | 5. To count the occurrences of each word in a given string : 189 | 6. Exit 190 | Enter your choice : 5 191 | Frequency of each word in the string is 192 | Frequency of apple is : 3 193 | Frequency of mango is : 3 194 | Frequency of orange is : 2 195 | Frequency of guava is : 1 196 | /*************MENU**************/ 197 | 1. To display word with the longest length : 198 | 2. To determines the frequency of occurrence of particular character in the string 199 | 3. To check whether given string is palindrome or not : 200 | 4. To display index of first appearance of the substring : 201 | 5. To count the occurrences of each word in a given string : 202 | 6. Exit 203 | Enter your choice : 6 204 | Wrong choice 205 | 206 | ''' 207 | -------------------------------------------------------------------------------- /DSL/(LINEAR SENTINENT BINARY)GROUP B - 11. a) Write a python program to store roll numbers of student in array who attended training program in random order. Write function for searching whether particular student attended training program or not, using Linear search and Sentinel search. b) Write a python program to store roll numbers of student array who attended training program in sorted order. Write function for searching whether particular student attended training program or not, using Binary search and Fibonacci search.py: -------------------------------------------------------------------------------- 1 | import array as arr 2 | 3 | 4 | # Accept the Roll Numbers of the students 5 | 6 | def accept_roll(): 7 | a = arr.array('I', []) 8 | no_stud = int(input("Enter the number of Students : ")) 9 | for i in range(0, no_stud): 10 | a.append(int(input("Enter the Roll Number : "))) 11 | return a 12 | 13 | 14 | # Print the Roll Numbers of the Students 15 | 16 | def print_roll(a): 17 | for i in range(0, len(a)): 18 | print("\t", a[i], end=" ") 19 | print() 20 | 21 | 22 | # Linear Search 23 | 24 | def linear_search(a, x): 25 | for i in range(len(a)): 26 | 27 | if a[i] == x: 28 | return i 29 | 30 | return -1 31 | 32 | 33 | # Sentinel Search 34 | 35 | def sentinel_search(a, x): 36 | count = len(a) 37 | a.append(x) 38 | i = 0 39 | 40 | while a[i] != x: 41 | i = i + 1 42 | 43 | if i != count: 44 | return i 45 | else: 46 | return -1 47 | 48 | 49 | # Binary Search recursive 50 | # Returns index of x in arr if present, else -1 51 | 52 | def binary_search_R(a, l, r, x): 53 | # Check base case 54 | if r >= l: 55 | 56 | mid = l + (r - l) // 2 57 | 58 | # If element is present at the middle itself 59 | if a[mid] == x: 60 | return mid 61 | 62 | # If element is smaller than mid, then it 63 | # can only be present in left subarray 64 | elif a[mid] > x: 65 | return binary_search_R(a, l, mid - 1, x) 66 | 67 | # Else the element can only be present 68 | # in right subarray 69 | else: 70 | return binary_search_R(a, mid + 1, r, x) 71 | 72 | else: 73 | # Element is not present in the array 74 | return -1 75 | 76 | 77 | # Python3 code to implement iterative Binary 78 | # Search. 79 | 80 | # It returns location of x in given array arr 81 | # if present, else returns -1 82 | def binary_search_NR(a, l, r, x): 83 | while l <= r: 84 | 85 | mid = l + (r - l) // 2; 86 | 87 | # Check if x is present at mid 88 | if a[mid] == x: 89 | return mid 90 | 91 | # If x is greater, ignore left half 92 | elif a[mid] < x: 93 | l = mid + 1 94 | 95 | # If x is smaller, ignore right half 96 | else: 97 | r = mid - 1 98 | 99 | # If we reach here, then the element 100 | # was not present 101 | return -1 102 | 103 | 104 | # Fibonacci Search 105 | 106 | def fibonacci_search(a, x): 107 | # Initialize the Fibonacci numbers 108 | fib1, fib2 = 1, 0 109 | fibn = fib2 + fib1 110 | a_len = len(a) 111 | while fibn < a_len: 112 | fib2 = fib1 113 | fib1 = fibn 114 | fibn = fib2 + fib1 115 | ind = -1 116 | while fibn > 1: 117 | n = min(ind + fib2, a_len - 1) 118 | if a[n] > x: 119 | fibn = fib2 120 | fib1 = fib1 - fib2 121 | fib2 = fibn - fib1 122 | elif a[n] < x: 123 | fibn = fib1 124 | fib1 = fib2 125 | fib2 = fibn - fib1 126 | ind = n 127 | else: 128 | return n 129 | if a[ind + 1] == n and fib1 == 1: 130 | return ind + 1 131 | return -1 132 | 133 | 134 | # Insertion sort 135 | 136 | def ins_sort(a): 137 | # Traverse through 1 to len(a) 138 | for i in range(1, len(a)): 139 | 140 | key = a[i] 141 | 142 | # Move elements of a[0..i-1], that are 143 | # greater than key, to one position ahead 144 | # of their current position 145 | j = i - 1 146 | while j >= 0 and key < a[j]: 147 | a[j + 1] = a[j] 148 | j -= 1 149 | a[j + 1] = key 150 | return a 151 | 152 | 153 | # Driver program 154 | if __name__ == "__main__": 155 | 156 | unsort_A = arr.array('I', []) # array of unsigned integer 157 | ins_sort_A = arr.array('I', []) 158 | flag = 1 159 | 160 | while flag == 1: 161 | menu = "1. Accept Student Roll Numbers of students who attended training program\n" \ 162 | "2. Display the Roll Numbers of students who attended training program\n" \ 163 | "3. Linear Search \n" \ 164 | "4. Sentinel Search \n" \ 165 | "5. Insertion Sort \n" \ 166 | "6. Binary Search (Recursive) \n" \ 167 | "7. Binary Search (Non-Recursive) \n" \ 168 | "8. Fibonacci Search \n" \ 169 | "9. Exit \n " 170 | print(menu) 171 | 172 | choice = int(input("Enter your choice : ")) 173 | 174 | if choice == 1: 175 | unsort_A = accept_roll() 176 | 177 | elif choice == 2: 178 | print_roll(unsort_A) 179 | 180 | elif choice == 3: 181 | roll = int(input("Enter the Roll Number to be search : ")) 182 | 183 | index = linear_search(unsort_A, roll) 184 | if index != -1: 185 | print("Roll number", roll, " at the index", index, "has Attended the training program") 186 | else: 187 | print("Roll number", roll, "has not Attended the training program") 188 | 189 | elif choice == 4: 190 | roll = int(input("Enter the Roll Number to be search : ")) 191 | 192 | index = sentinel_search(unsort_A, roll) 193 | if index == -1: 194 | print("Roll number", roll, "has not Attended the training program") 195 | else: 196 | print("Roll number", roll, " at the index", index, "has Attended the training program") 197 | 198 | elif choice == 5: 199 | print("Elements after sorting using Insertion Sort :") 200 | ins_sort_A = ins_sort(unsort_A) 201 | print_roll(ins_sort_A) 202 | 203 | elif choice == 6: 204 | roll = int(input("Enter the Roll Number to be search : ")) 205 | 206 | index = binary_search_R(unsort_A, 0, len(unsort_A) - 1, roll) 207 | if index == -1: 208 | print("Roll number", roll, "has not Attended the training program") 209 | else: 210 | print("Roll number", roll, " at the index", index, "has Attended the training program") 211 | 212 | elif choice == 7: 213 | roll = int(input("Enter the Roll Number to be search : ")) 214 | 215 | index = binary_search_NR(unsort_A, 0, len(unsort_A) - 1, roll) 216 | if index == -1: 217 | print("Roll number", roll, "has not Attended the training program") 218 | else: 219 | print("Roll number", roll, " at the index", index, "has Attended the training program") 220 | 221 | elif choice == 8: 222 | roll = int(input("Enter the Roll Number to be search : ")) 223 | 224 | index = fibonacci_search(unsort_A, roll) 225 | if index == -1: 226 | print("Roll number", roll, "has not Attended the training program") 227 | else: 228 | print("Roll number", roll, " at the index", index, "has Attended the training program") 229 | 230 | else: 231 | print("Wrong choice") 232 | flag = 0 233 | ''' 234 | /*******************OUTPUT*****************/ 235 | 236 | 1. Accept Student Roll Numbers of students who attended training program 237 | 2. Display the Roll Numbers of students who attended training program 238 | 3. Linear Search 239 | 4. Sentinel Search 240 | 5. Insertion Sort 241 | 6. Binary Search (Recursive) 242 | 7. Binary Search (Non-Recursive) 243 | 8. Fibonacci Search 244 | 9. Exit 245 | 246 | Enter your choice : 1 247 | Enter the number of Students : 5 248 | Enter the Roll Number : 34 249 | Enter the Roll Number : 56 250 | Enter the Roll Number : 52 251 | Enter the Roll Number : 18 252 | Enter the Roll Number : 9 253 | 254 | Enter your choice : 2 255 | 34 56 52 18 9 256 | 257 | Enter your choice : 3 258 | Enter the Roll Number to be search : 18 259 | Roll number 18 at the index 3 has Attended the training program 260 | 261 | Enter your choice : 4 262 | Enter the Roll Number to be search : 34 263 | Roll number 34 at the index 0 has Attended the training program 264 | 265 | Enter your choice : 5 266 | Elements after sorting using Insertion Sort : 267 | 9 18 34 34 52 56 268 | 269 | Enter your choice : 6 270 | Enter the Roll Number to be search : 34 271 | Roll number 34 at the index 2 has Attended the training program 272 | 273 | Enter your choice : 7 274 | Enter the Roll Number to be search : 18 275 | Roll number 18 at the index 1 has Attended the training program 276 | 277 | Enter your choice : 8 278 | Enter the Roll Number to be search : 56 279 | Roll number 56 at the index 5 has Attended the training program 280 | 281 | Enter your choice : 9 282 | ''' 283 | -------------------------------------------------------------------------------- /DSL/Pinnacle club.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | 10 | 11 | class node 12 | 13 | { 14 | 15 | int prn; 16 | 17 | string name; 18 | 19 | struct node *link; 20 | 21 | friend class linkedlist; 22 | 23 | }; 24 | 25 | class linkedlist 26 | 27 | { 28 | 29 | node *start; 30 | 31 | 32 | 33 | public: 34 | 35 | linkedlist() 36 | 37 | { 38 | 39 | start=NULL; 40 | 41 | } 42 | 43 | void insert_President() 44 | 45 | { 46 | 47 | node *tmp; 48 | 49 | tmp=new node(); 50 | 51 | int pr; 52 | 53 | string nm; 54 | 55 | cout<<"Enter PRN and Name:\n"; 56 | 57 | cin>>pr>>nm; 58 | 59 | tmp->prn=pr; 60 | 61 | tmp->name=nm; 62 | 63 | tmp->link=NULL; 64 | 65 | if(start==NULL) 66 | 67 | start=tmp; 68 | 69 | else 70 | 71 | { 72 | 73 | tmp->link=start; 74 | 75 | start=tmp; 76 | 77 | } 78 | 79 | display(); 80 | 81 | } 82 | 83 | void insert_Secretary() 84 | 85 | { 86 | 87 | 88 | 89 | node *tmp,*q; 90 | 91 | int pr; 92 | 93 | string nm; 94 | 95 | tmp=new node(); 96 | 97 | cout<<"Enter PRN and Name:\n"; 98 | 99 | cin>>pr>>nm; 100 | 101 | 102 | 103 | tmp->prn=pr; 104 | 105 | tmp->name=nm; 106 | 107 | tmp->link=NULL; 108 | 109 | 110 | 111 | if(start==NULL) 112 | 113 | start=tmp; 114 | 115 | else 116 | 117 | { 118 | 119 | q=start; 120 | 121 | while(q->link!=NULL) 122 | 123 | q=q->link; 124 | 125 | q->link=tmp; 126 | 127 | } 128 | 129 | display(); 130 | 131 | } 132 | 133 | void insert_Member() 134 | 135 | { 136 | 137 | display(); 138 | 139 | int pr; 140 | 141 | string nm; 142 | 143 | node *q,*tmp; 144 | 145 | tmp=new node(); 146 | 147 | int index; 148 | 149 | 150 | 151 | cout<<"Enter PRN and Name:\n"; 152 | 153 | cin>>pr>>nm; 154 | 155 | 156 | 157 | tmp->prn=pr; 158 | 159 | tmp->name=nm; 160 | 161 | tmp->link=NULL; 162 | 163 | 164 | 165 | if(start==NULL) 166 | 167 | { 168 | 169 | start=tmp; 170 | 171 | } 172 | 173 | else 174 | 175 | { 176 | 177 | cout<<"\nEnter index after which element to be inserted :\n"; 178 | 179 | cin>>index; 180 | 181 | q=start; 182 | 183 | for(int i=0;ilink; 188 | 189 | if(q==NULL) 190 | 191 | { 192 | 193 | cout<<"There are less elements\n"; 194 | 195 | return; 196 | 197 | } 198 | 199 | } 200 | 201 | tmp->link = q->link; 202 | 203 | q->link = tmp; 204 | 205 | } 206 | 207 | display(); 208 | 209 | } 210 | 211 | void del_President() 212 | 213 | { 214 | 215 | node *tmp; 216 | 217 | tmp=start; 218 | 219 | start=start->link; 220 | 221 | free(tmp); 222 | 223 | display(); 224 | 225 | } 226 | 227 | void del_Secretary() 228 | 229 | { 230 | 231 | struct node *q,*tmp; 232 | 233 | q=start; 234 | 235 | tmp=start; 236 | 237 | while(tmp->link!=NULL) 238 | 239 | { 240 | 241 | q=tmp; 242 | 243 | tmp=tmp->link; 244 | 245 | } 246 | 247 | q->link=NULL; 248 | 249 | free(tmp); 250 | 251 | display(); 252 | 253 | } 254 | 255 | void del_Member() 256 | 257 | { 258 | 259 | int pr; 260 | 261 | 262 | 263 | cout<<"enter PRN to be deleted"; 264 | 265 | cin>>pr; 266 | 267 | node *q,*tmp; 268 | 269 | q=start; 270 | 271 | tmp=start; 272 | 273 | 274 | 275 | while(tmp->link!=NULL) 276 | 277 | { 278 | 279 | if(tmp->prn==pr) 280 | 281 | { 282 | 283 | q->link=tmp->link; 284 | 285 | free(tmp); 286 | 287 | break; 288 | 289 | } 290 | 291 | q=tmp; 292 | 293 | tmp=tmp->link; 294 | 295 | } 296 | 297 | 298 | 299 | display(); 300 | 301 | } 302 | 303 | void display() 304 | 305 | { 306 | 307 | node *q; 308 | 309 | if(start==NULL) 310 | 311 | cout<<"Club is empty!!\n"; 312 | 313 | else 314 | 315 | { 316 | 317 | cout<<"**** Members in Club ****\n"; 318 | 319 | q=start; 320 | 321 | while(q!=NULL) 322 | 323 | { 324 | 325 | cout<prn<<" "<name<link; 328 | 329 | } 330 | 331 | } 332 | 333 | } 334 | 335 | void count() 336 | 337 | { 338 | 339 | node *q; 340 | 341 | int i=0; 342 | 343 | 344 | 345 | q=start; 346 | 347 | while(q!=NULL) 348 | 349 | { 350 | 351 | i++; 352 | 353 | q=q->link; 354 | 355 | } 356 | 357 | cout<<"Total no. of members in club"<link); 386 | 387 | } 388 | 389 | cout<< "**Members in reverse order**"; 390 | 391 | cout<<"\n"<prn; 392 | 393 | cout<<"\t"<name; 394 | 395 | } 396 | 397 | 398 | 399 | void concat(linkedlist l1,linkedlist l2) 400 | 401 | { 402 | 403 | node *ptr; 404 | 405 | ptr=l1.start; 406 | 407 | while(ptr->link!=NULL) 408 | 409 | ptr=ptr->link; 410 | 411 | ptr->link=l2.start; 412 | 413 | l1.display(); 414 | 415 | 416 | 417 | } 418 | 419 | }; 420 | 421 | 422 | 423 | int main() 424 | 425 | { 426 | 427 | linkedlist l1,l2; 428 | 429 | int ch; 430 | 431 | 432 | 433 | cout<<"\n****Linked List*****\n"; 434 | 435 | cout<<"\n1.Insert President \n2.Insert Secretary \n3.Insert Member \n4.Delete President \n5.Delete Secretary \n6.Delete Member \n7.Display \n8.Count \n9.Reverse \n10.Concat \n11.Exit\n"; 436 | 437 | while(1) 438 | 439 | { 440 | 441 | cout<<"\nEnter Your Choice :(1.Insert President 2.Insert Secretary 3.Insert Member 4.Delete President 5.Delete Secretary 6. Delete Member 7.Display 8.Count 9. Reverse 10.Concat 11.Exit)\n"; 442 | 443 | cin>>ch; 444 | 445 | switch(ch) 446 | 447 | { 448 | 449 | case 1: 450 | 451 | l1.insert_President(); 452 | 453 | break; 454 | 455 | case 2: 456 | 457 | l1.insert_Secretary(); 458 | 459 | break; 460 | 461 | case 3: 462 | 463 | l1.insert_Member(); 464 | 465 | break; 466 | 467 | case 4: 468 | 469 | l1.del_President(); 470 | 471 | break; 472 | 473 | case 5: 474 | 475 | l1.del_Secretary(); 476 | 477 | break; 478 | 479 | case 6: 480 | 481 | l1.del_Member(); 482 | 483 | break; 484 | 485 | case 7: 486 | 487 | l1.display(); 488 | 489 | break; 490 | 491 | case 8: 492 | 493 | l1.count(); 494 | 495 | break; 496 | 497 | case 9: 498 | 499 | l1.reverse1(); 500 | 501 | break; 502 | 503 | case 10: 504 | 505 | l2.insert_President(); 506 | 507 | l2.insert_Member(); 508 | 509 | l2.insert_Secretary(); 510 | 511 | l1.concat(l1,l2); 512 | 513 | break; 514 | 515 | case 11: 516 | 517 | exit(0); 518 | 519 | default: 520 | 521 | printf("\nWrong Choice !!\n"); 522 | 523 | } 524 | 525 | } 526 | 527 | return 0; 528 | 529 | } 530 | --------------------------------------------------------------------------------