diff --git a/Lab-01/README.md b/Lab-01/README.md new file mode 100644 index 0000000..6af07d8 --- /dev/null +++ b/Lab-01/README.md @@ -0,0 +1,107 @@ +# Lab-01 +In school days, we used to play a game called FLAMES (F: Friend, L: Love, A: Attraction, M: Marriage, E: Enemy, S: Sibling); which finds the relation between two names. Write a program that finds the relation between two names using the algorithm of FLAMES. + +[Disclaimer: Examples or names in this problem statement are unintentional. Any resemblance in the real world is a mere coincidence.] + +An example: Let’s check the relation between "abhishek" and "aishwarya". Remove the common characters from both the names. So, in the above problem the characters a, i, s and h are common. (Remember there are three a in second name. But only one a in first name and hence only once occurrence is common.) Now, the remaining number of characters in both the names is 4+5 = 9; which is the number of iterations required for eliminating a letter from FLAMES. + +Now, you have to check the number 9 with FLAMES as explained below. + +State 0: FLAMES + +Run/Count through FLAMES with number 9 like, + +F(1) L(2) A(3) M(4) E(5) S(6) F(7) L(8) A(9) M() E() S() + +So, 9 ends at A. Now delete A from FLAMES. Hence, remaining letters are FLMES + +State 1: FLMES + +Now, you have to start from M, i.e, the last position you were at. + +F() L() M(1) E(2) S(3) F(4) L(5) M(6) E(7) S(8) F(9) L() M() E() S() + +Delete F from FLMES. Hence, remaining letters are LMES. + +State 2: LMES - Starting point: L + +L(1) M(2) E(3) S(4) L(5) M(6) E(7) S(8) L(9) M() E() S() + +Delete L. Remaining MES. + +State 3: MES - Starting point: M + +M(1) E(2) S(3) M(4) E(5) S(6) M(7) E(8) S(9) M() E() S() + +Delete S. Remaining ME. + +State 4: ME - Starting point: M + +M(1) E(2) M(3) E(4) M(5) E(6) M(7) E(8) M(9) E() + +Delete M. Remaining E. + +State 5: E + +The remaining character E gives the relation between the two persons: (abhishek, aishwarya) : Enemies. + +Function Prototypes: + +// Function to shift characters of a string to left indices. +void shiftLeft(string& str, int pos); +Ex. If str contains "abcdefgh" and pos is 4. Then shiftLeft function deletes 'd' and modifies str to "abcefgh". + +// Function to find the common characters between two strings. +int findSame(string str1, string str2); +Ex. If str1 is ”abcdab” and str2 is ”abxymnda”, findSame function finds the common characters 'a', 'b', 'd' and 'a' So the function returns integer 4. + +// Function to remove characters from the string 'str' after repeated 'iter' iterations on str. +void solveFlames(string& str, int iter); +Ex. If str is "FLAMES" and iter is 5, the string remaining after removing characters is F. (FLAMES -> FLAMS -> FLAS -> FLA -> FA -> F) + +Input Format + +Format: Two strings (names of the two persons) separated by a space + +Constraints + +Note: 1. The input would be in lower case. 2. No two names will have all common characters. + +Test Cases Set A: + +None of the names will have repeating characters (e.g. RIA and RIAN) +Number of iterations required to remove a character in FLAMES is 1. +Test Cases Set B: + +None of the names will have repeating characters +There are no constraints on number of iterations required to remove a character in FLAMES. +Test Cases Set C: + +Names can have repeating characters (e.g. AISHWARYA and ABHISHEK) +There are no constraints on number of iterations required to remove a character in FLAMES. +Output Format + +Format: [Length of Name1, Length of Name 2, Number of common characters, Relationship]. + +Sample Input 0 + +riya +riyan +Sample Output 0 + +[4,5,4,S] +Explanation 0 + +r,i,y,a are common in both the strings, so the strings remaining after removing common characters from "riya" and "riyan" are " " and "n" respectively. Therefore the number of iterations required to remove a character from "FLAMES" will be 0 + 1 = 1. + +After 1st iteration f is removed, and lames is left. + +After 2nd iteration l is removed, and ames is left. + +After 3rd iteration a is removed, and mes is left. + +After 4th iteration m is removed, and es is left. + +After 5th iteration e is removed, and s is left. Remaining string: S + +Since the length of "riya" is 4 and of "riyan" is 5, no. of common character is 4, hence the output is [4,5,4,S] diff --git a/Lab-01/lab1.cpp b/Lab-01/lab1.cpp new file mode 100644 index 0000000..4f173ff --- /dev/null +++ b/Lab-01/lab1.cpp @@ -0,0 +1,93 @@ +#include +using namespace std; + +#define mod 1000000007 +#define pb push_back +#define mp make_pair +#define PI 3.14159265358979323 +#define debug(x) cout<<"Case "<=b; i--) +#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); +typedef long long int ll; +typedef long double ld; +typedef unsigned long long int ull; +typedef vector vi; +typedef vector vll; +typedef pair pii; +typedef pair pll; +typedef vector < pii > vpii; +typedef vector < pll > vpll; +typedef vector vs; + +//Handle:cyber_rajat + + +// Complete the findCommon function below. +int findCommon(string str1, string str2) { + int len1=str1.size(); + int len2=str2.size(); + int cnt=0; + for(int i=0;i>a>>b; + // Create variable flames of type string and initialize it to "FLAMES". + string k="FLAMES"; + // Call function findCommon with both the inputs as parameters and store the result in a variable: commonCount. + int common=findCommon(a,b); + // Using commonCount, calculate the number of iterations required to remove a character from "FLAMES" and store the result in the variable 'iter' of type integer. + int len1=a.size(); + int len2=b.size(); + int iter=len1+len2-2*common; + solveFlames(k,iter); + // Call the function solveFlames with string flames and integer iter as parameters. + + // Print the appropriate result. + cout<<"["< +using namespace std; + +#define mod 1000000007 +#define pb push_back +#define mp make_pair +#define PI 3.14159265358979323 +#define debug(x) cout<<"Case "<=b; i--) +#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); +typedef long long int ll; +typedef long double ld; +typedef unsigned long long int ull; +typedef vector vi; +typedef vector vll; +typedef pair pii; +typedef pair pll; +typedef vector < pii > vpii; +typedef vector < pll > vpll; +typedef vector vs; + +//Handle:cyber_rajat + +class trainlist +{ +public: + string name; + int num; + string source; + string destination; + int seats; +}; + +int scan_trains(int n,vector& v) +{ + int a,d,i; + string e,b,c; + trainlist temp; + for(i=0;i>temp.name>>temp.num>>temp.source>>temp.destination>>temp.seats; + v.push_back(temp); + } + return i; +} + +int print_trains(vector& v) +{ + vector::iterator it; + int x=0; + for(it=v.begin();it!=v.end();it++) + { + x++; + cout<<(*it).name<<" "<<(*it).num<<" "<<(*it).source<<" "<<(*it).destination<<" "<<"500 "<<(*it).seats<<" "<& v) +{ + int a,b,x=-1; + cin>>a>>b; + vector::iterator it; + for(it=v.begin();it!=v.end();it++) + { + if((*it).num==a) + { + if(((*it).seats+b)<=500) + { + x=(*it).seats+b; + (*it).seats=x; + break; + } + else + { + x=0; + break; + } + } + } + if(x>0) + { + //vector::iterator iti; + cout<<"Train number to make cancellation: "<> n; + + // declare a vector named trainList to contain train records + vector v; + + if( n > 0 ){ + cout << "Testing reading train records." << endl; + + // call scan_trains with n and trainList as parameters. + int value=scan_trains(n,v); + cout << "Total number of train records: " << value << endl << endl; + } + + cin >> m; + if( m > 0 ){ + cout << "Testing reading train records again." << endl; + + // call scan_trains again with m and trainList as parameters. + int value=scan_trains(m,v); + cout << "Total number of train records: " << value+n << endl << endl; + } + + + cout << "Testing printing train records."<< endl; + + // call print_trains with trainList as parameter. + int value=print_trains(v); + cout << "Total number of train records printed: " << value<< endl << endl; + + cin >> k; + if( k > 0 ){ + cout << "Testing cancelling tickets." << endl; + //int sum=0; + for(int i=0; idatadata){ + head=temp1; + temp=head; + temp1=temp1->next; + } + else{ + head=temp2; + temp=head; + temp2=temp2->next; + } + } + else + { + if(temp1->datadata){ + temp->next=temp1; + temp=temp1; + temp1=temp1->next; + } + else + { + temp->next=temp2; + temp=temp2; + temp2=temp2->next; + } + + } + + } + while(temp1!=NULL) + { + temp->next=temp1; + temp1=temp1->next; + temp=temp->next; + } + while(temp2!=NULL) + { + temp->next=temp2; + temp2=temp2->next; + temp=temp->next; + } + return head; +} diff --git a/Lab-03/Lab-03q2.cpp b/Lab-03/Lab-03q2.cpp new file mode 100644 index 0000000..8994cc9 --- /dev/null +++ b/Lab-03/Lab-03q2.cpp @@ -0,0 +1,298 @@ +#include +using namespace std; + +#define mod 1000000007 +#define pb push_back +#define mp make_pair +#define PI 3.14159265358979323 +#define debug(x) cout<<"Case "<=b; i--) +#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); +typedef long long int ll; +typedef long double ld; +typedef unsigned long long int ull; +typedef vector vi; +typedef vector vll; +typedef pair pii; +typedef pair pll; +typedef vector < pii > vpii; +typedef vector < pll > vpll; +typedef vector vs; + +//Handle:cyber_rajat +class SinglyLinkedListNode { + public: + int data; + SinglyLinkedListNode *next; + + SinglyLinkedListNode(int node_data) { + this->data = node_data; + this->next = nullptr; + } +}; + +class SinglyLinkedList { + public: + SinglyLinkedListNode *head; + SinglyLinkedListNode *tail; + + SinglyLinkedList() { + this->head = nullptr; + this->tail = nullptr; + } + + void insert_node(int node_data) { + SinglyLinkedListNode* node = new SinglyLinkedListNode(node_data); + + if (!this->head) { + this->head = node; + } else { + this->tail->next = node; + } + + this->tail = node; + } +}; + +void print_singly_linked_list(SinglyLinkedListNode* node, string sep, ofstream& fout) { + while (node) { + fout << node->data; + + node = node->next; + + if (node) { + fout << sep; + } + } +} + +void free_singly_linked_list(SinglyLinkedListNode* node) { + while (node) { + SinglyLinkedListNode* temp = node; + node = node->next; + + free(temp); + } +} + +// Complete the function below. + +/* + * For your reference: + * + * SinglyLinkedListNode { + * int data; + * SinglyLinkedListNode* next; + * }; + * + */ +bool Helper(SinglyLinkedListNode* ans, SinglyLinkedListNode* ans2,int l1,int l2) +{ + SinglyLinkedListNode* ans1=ans; + int l=0; + bool a=true; + while(ans1!=NULL && ans2!=NULL) + { + if(ans1->data!=ans2->data) + { + a =false; + break; + } + ans1=ans1->next; + ans2=ans2->next; + } + + if((ans1!=NULL || ans2!=NULL) && a!=false) + { + /* if(l1next; + } + if(ans==NULL) + { + if(l1>l2){ + return false; + //cout<data<data<next; + } + while(temp2!=NULL) + { + l2++; + temp2=temp2->next; + } + temp1=head1,temp2=head2; + bool val; + SinglyLinkedListNode* ans=NULL; + SinglyLinkedListNode* ans2=NULL; + if(l1data==temp2->data) + { + ans=temp1; + ans2=temp2; + val= Helper(ans,ans2,l1,l2);//break; + if(val) + break; + } + temp2=temp2->next; + } + if(val) + break; + temp1=temp1->next; + } + + } + else + { + while(temp2!=NULL) + { + temp1=head1; + while(temp1!=NULL) + { + if(temp1->data==temp2->data) + { + ans=temp2; + ans2=temp1; + val=Helper(ans,ans2,l1,l2); + if(val) + break; + } + temp1=temp1->next; + } + if(val) + break; + temp2=temp2->next; + } + } + + if(!val) + { + if(l1next; + } + + if(l1data<data<> llist1_count; + + int llist2_count; + cin >> llist2_count; + + for (int i = 0; i < llist1_count; i++) { + int llist1_item; + cin >> llist1_item; + + llist1->insert_node(llist1_item); + } + + SinglyLinkedList* llist2 = new SinglyLinkedList(); + + for (int i = 0; i < llist2_count; i++) { + int llist2_item; + cin >> llist2_item; + + llist2->insert_node(llist2_item); + } + + solve(llist1->head, llist2->head); + + return 0; +} diff --git a/Lab-03/README.md b/Lab-03/README.md new file mode 100644 index 0000000..5562fa4 --- /dev/null +++ b/Lab-03/README.md @@ -0,0 +1,87 @@ +# Lab-02 + +## Question-1 +You’re given the pointer to the head nodes of two sorted linked lists. The data in both lists will be sorted in ascending order. Change the next pointers to obtain a single, merged linked list which also has data in ascending order. Either head pointer given may be null meaning that the corresponding list is empty. + +Input Format + +You have to complete the SinglyLinkedListNode MergeLists(SinglyLinkedListNode headA, SinglyLinkedListNode headB) method which takes two arguments - the heads of the two sorted linked lists to merge. You should NOT read any input from stdin/console. + +The input is handled by the code in the editor and the format is as follows: + +The first line contains an integer , denoting the number of test cases. +The format for each test case is as follows: + +The first line contains an integer , denoting the length of the first linked list. +The next lines contain an integer each, denoting the elements of the linked list. +The next line contains an integer , denoting the length of the second linked list. +The next lines contain an integer each, denoting the elements of the second linked list. + +Constraints + +, where is the element of the list. +Output Format + +Change the next pointer of individual nodes so that nodes from both lists are merged into a single list. Then return the head of this merged list. Do NOT print anything to stdout/console. + +The output is handled by the editor and the format is as follows: + +For each test case, print in a new line, the linked list after merging them separated by spaces. + +Sample Input + +1 +3 +1 +2 +3 +2 +3 +4 +Sample Output + +1 2 3 3 4 +Explanation + +The first linked list is: 1 -> 2 -> 3 -> NULL + +The second linked list is: 3 -> 4 -> NULL + +Hence, the merged linked list is: 1 -> 2 -> 3 -> 3 -> 4 -> NULL + +## Question-2 +There are two singly linked lists in a system. By some programming error, the end node of one of the linked list might have gotten linked to the second list, forming an inverted Y shaped list. Write a program to get the point where two linked list merge. + +Note: In case of no point of intersection print "NULL" in your answer in the place of the point of intersection. + +Input Format + +The input is handled by the code in the editor and the format is as follows: + +The first line contains two integers denoting the number of elements in the following lines. The next lines contain an integer each, denoting the elements of the first linked list. The next lines contain an integer each, denoting the elements of the second linked list. + +Constraints + +No constraints + +Output Format + +Print number of nodes in bigger list, number of nodes in smaller list, point of intersection + +Sample Input 0 + +5 3 +3 6 9 15 30 +10 15 30 +Sample Output 0 + +5,1,15 +Explanation 0 + +Explanation + +The first linked list is: 3 -> 6 -> 9 -> 15 -> 30 -> NULL + +The second linked list is: 10 -> 15 -> 30 -> NULL + +Hence, the second linked list is actually 10 -> NULL and the two linked list have 15 as the intersection point.