├── Hash Tables ├── hash.cpp ├── hash.h └── hash_function.cpp ├── IBM ├── a.out ├── array_pair.cpp ├── deleteNode_x.cpp ├── powerset.cpp ├── ramdom_string.cpp ├── random.cpp └── reveseword.cpp ├── IKM C++11 ├── Inheritance.cpp ├── Opr_overldng.cpp ├── a.out ├── dynm_mem_alloc.cpp ├── exception.cpp ├── friend_fun.cpp ├── function_oveld.cpp ├── inheretance2.cpp ├── regex.cpp ├── te.cpp ├── templete_class.cpp ├── test.cpp └── test2.cpp ├── Linked list problems ├── Doublylinkedlist.cpp ├── a.out ├── add_linkedlist.cpp ├── create_linked list.cpp ├── palindrome_linklist.cpp └── sll.cpp ├── Median.cpp ├── Problems ├── Dec_Bin.cpp ├── Genomic_range_query.cpp ├── PermCheck.cpp ├── Tape_equilibrium.cpp ├── a.out ├── array_intersection.cpp ├── bouncing_ball.cpp ├── brackets.cpp ├── cyclic_array.cpp ├── distinct_num.cpp ├── divcount.cpp ├── fibonacchi.cpp ├── fish.cpp ├── frogjmp.cpp ├── frogriverone.cpp ├── max_counters.cpp ├── minAvgtwo.cpp ├── missing_num_bit.cpp ├── monkey.cpp ├── nesting.cpp ├── num_disc_intersection.cpp ├── pait_multi.cpp ├── passing_cars.cpp ├── permmissingelem.cpp ├── product_triplet.cpp ├── triangle.cpp ├── union_inter.cpp ├── word_exor.cpp └── zigzag.cpp ├── README.md ├── RWcsv ├── a.out ├── data.csv └── rwcsv.cpp ├── Recursion ├── factorial.cpp ├── fib_itr.cpp └── fibonacci.cpp ├── Red Black Tree ├── rbapp.cpp ├── rbtree.cpp └── rbtree.h ├── Sorting- ALL ├── CensusData.h ├── CensusDataSorts.cpp └── Patil-Tejas.pdf ├── Stacks and Queues ├── Stack_arrays.cpp ├── a.out ├── queue_array2.cpp ├── queue_array_based.cpp ├── queue_linkedlist.cpp └── stack_linkedlist.cpp ├── Strings ├── Check_Unique_str.cpp ├── Longestsubstring.cpp ├── Tape_equilibrium.cpp ├── a.out ├── frogjmp.cpp ├── increasing_sub.cpp ├── is_substringrit.cpp ├── missing_num_bit.cpp ├── missingrange.cpp ├── myfile.txt ├── output.txt ├── permmissingelem.cpp ├── remove_duplicate.cpp ├── remove_spaces.cpp ├── rev_Cstring.cpp ├── string_compression.cpp ├── string_permutations1.cpp ├── string_permutations2.cpp ├── string_replacement.cpp └── temp ├── Trees ├── BST.cpp ├── Succ_Pred.cpp ├── a.out ├── arry2BST.cpp ├── balance_BST.cpp ├── common_ancestor.cpp ├── sum_path.cpp ├── sum_tree_4'9.cpp └── test.cpp ├── a.out ├── arrays ├── 2-Darray.cpp ├── a.out ├── arry_geratest.cpp ├── matrix.out ├── matrix_opr.cpp ├── rotate.out └── rotate_2Darray.cpp ├── building_blocks ├── Data_types.cpp ├── a.out ├── class2.cpp ├── classes.cpp ├── dec2Bin.cpp ├── except_hand.cpp ├── factorials.cpp ├── fileio.cpp ├── findgreat_vector.cpp ├── findgreatest.cpp ├── myfile.txt ├── pointers.cpp ├── pointers2.cpp ├── pointers2pointers.cpp ├── polymorphism.cpp ├── string_test.cpp ├── strings.cpp ├── ternary.cpp ├── test1.cpp ├── test2.cpp ├── vectors.cpp └── virtual_methods.cpp ├── crickinfo ├── a.out ├── main.cpp ├── player.cpp ├── player.h ├── player.h.gch ├── playerinfo.txt ├── playerlist.cpp ├── playerlist.h └── playerlist.h.gch ├── dijkstra's Algorithm ├── graph.cpp ├── graph.h ├── minpriority.cpp ├── minpriority.h ├── sspapp.cpp └── sspapp.h ├── doublylinkedlist ├── a.out ├── doublylinkedlist.cpp ├── doublylinkedlist.h ├── playlist.cpp └── playlist.h ├── fileIO ├── a.out ├── atr_write.cpp ├── cricket.cpp ├── myfile.txt ├── newfile.txt ├── out.txt ├── read.cpp ├── scoreboard.txt ├── str_read.cpp └── write.cpp ├── mickey.cpp ├── ok.cpp └── test.cpp /Hash Tables/hash.cpp: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------- 2 | Name : TEJAS PATIL 006949762 3 | Assignment No: 03 4 | Date: .03/28/2015 5 | Description: This file contains the function defination for the functions 6 | decleared in hash.h . which are used to perform operation on 7 | Hash Table like insert,remove,delete and to calculate number 8 | of collision,avegerage length and loadfactor. 9 | ------------------------------------------------------------------------*/ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include"hash.h" 18 | 19 | using namespace std; 20 | list < string > ::iterator it; 21 | 22 | 23 | /*---------------------------------------------------------------------- 24 | Description: This is the constructor which initialize thr values for 25 | collision, longestList and avgList; 26 | ------------------------------------------------------------------------*/ 27 | Hash::Hash() 28 | { 29 | collisions = 0; 30 | longestList = 0; 31 | avgLength = 0; 32 | 33 | } 34 | /*---------------------------------------------------------------------- 35 | Description: This function removes the string from Hash_Table with 36 | respect to hash key from the specific location 37 | Input: ip_str ths is the srting input which has to be removed 38 | Output: void 39 | ------------------------------------------------------------------------*/ 40 | void Hash::remove(string ip_str) // remove hashvalue from hash table 41 | { 42 | int key; 43 | key = hf(ip_str); 44 | 45 | for (it = hashTable[key].begin(); it != hashTable[key].end(); ++it) 46 | { 47 | if (ip_str == *it) 48 | { //the line below erases the string 49 | hashTable[key].erase(it); 50 | //Here we calculate updated average length 51 | avgLength = (CurrentavgLength() + avgLength) / 2.0; 52 | break; 53 | } 54 | } 55 | } 56 | 57 | /*----------------------------------------------------------------------- 58 | Description: This function displays Hash table 59 | Input: void 60 | Output: void 61 | -------------------------------------------------------------------------*/ 62 | void Hash::print()// print the entire hash table 63 | { 64 | int i; 65 | 66 | for (i = 0; i < HASH_TABLE_SIZE; i++) 67 | { 68 | cout << i << ":" << "\t"; 69 | for (it = hashTable[i].begin(); it != hashTable[i].end(); it++) 70 | { 71 | cout << *it << ", "; 72 | } 73 | cout << endl; 74 | } 75 | } 76 | 77 | /*------------------------------------------------------------------------- 78 | Description: This function accept the string from input file and 79 | fix it in hash table 80 | Input: ip_str =this is the srting input which has to be added in hash table 81 | Output:void 82 | --------------------------------------------------------------------------*/ 83 | void Hash::processFile(string ip_str) 84 | { 85 | int key; 86 | ifstream fptr; 87 | fptr.open(ip_str); 88 | string str; 89 | collisions = 0; 90 | if (fptr.is_open()) 91 | { 92 | while (getline(fptr, str)) 93 | { 94 | key = hf(str); 95 | if(!hashTable[key].empty()) 96 | collisions++;//counts collisions; 97 | hashTable[key].push_back(str); 98 | //returns longest list 99 | Longestlist(); 100 | //Here we calculate updated average length for every insert 101 | avgLength = (CurrentavgLength() + avgLength) / 2.0; 102 | } 103 | } 104 | fptr.close(); 105 | } 106 | 107 | /*--------------------------------------------------------------------- 108 | Description: This function search the input strings 109 | Input: ip_str the String to be serached. 110 | Output: it returns true or false. (of type bool) 111 | -----------------------------------------------------------------------*/ 112 | bool Hash::search(string ip_str) 113 | // search for a hashvalue in the hash table 114 | { 115 | int key; 116 | key = hf(ip_str); 117 | 118 | for (it = hashTable[key].begin(); it != hashTable[key].end(); it++) 119 | { 120 | // checks whether it is present or not 121 | if (ip_str == *it) 122 | { 123 | return true; 124 | } 125 | } 126 | return false; 127 | } 128 | 129 | /*----------------------------------------------------------------------- 130 | Description: This function passes the output of the Hash_Table 131 | which is updated after all operation on hash table 132 | (like remove, search) to "hash.out" 133 | Input: void 134 | Output: void 135 | ------------------------------------------------------------------------*/ 136 | 137 | void Hash::output(string) 138 | { 139 | int i; 140 | { 141 | ofstream op_str; 142 | op_str.open("hash.out"); 143 | // print entire hash table to a file hash.out 144 | { 145 | list < string > ::iterator it; 146 | for (i = 0; i < HASH_TABLE_SIZE; i++) 147 | { 148 | op_str << i << ":" << "\t"; 149 | for(it=hashTable[i].begin();it!=hashTable[i].end();it++) 150 | { 151 | op_str << *it << ", "; 152 | } 153 | op_str << endl; 154 | } 155 | } 156 | op_str.close(); 157 | } 158 | } 159 | 160 | /*---------------------------------------------------------------------- 161 | Description: This function calculates the average value. 162 | Input: void 163 | Output:it returns avg of type double 164 | -------------------------------------------------------------------------*/ 165 | double Hash::CurrentavgLength() 166 | { 167 | double cur_avg = 0, len = 0 ,curr=0; 168 | int i = 0; 169 | double j = 0; 170 | 171 | for(i=0; i < HASH_TABLE_SIZE;i++) 172 | { 173 | curr = (hashTable[i].size()); 174 | len = len + curr; 175 | //calculates total numbers of elements in hash Table 176 | if(!hashTable[i].empty()) 177 | { 178 | j++; 179 | }//calculates non empty Lists 180 | } 181 | cur_avg = (len/(j)); 182 | return cur_avg; 183 | } 184 | 185 | /*---------------------------------------------------------------------- 186 | Description: This function calculates the longestList length 187 | Input: void 188 | Output:it returns longestList length 189 | ------------------------------------------------------------------------*/ 190 | unsigned int Hash::Longestlist() 191 | { 192 | int l1 = 0, l2 = 0; 193 | list < string > ::iterator it; 194 | for (int i = 0; i < HASH_TABLE_SIZE; i++) 195 | { 196 | for (it = hashTable[i].begin(); it != hashTable[i].end(); it++) 197 | { 198 | //increments the l2 for every element present in hashtable 199 | l2++; 200 | } 201 | if (l1 <= l2) 202 | { 203 | //checks current lenght with previous length 204 | l1 = l2; 205 | longestList = l1; 206 | //exchane if current lenght is more than previous length 207 | } 208 | l2 = 0; 209 | } 210 | return longestList; 211 | } 212 | 213 | 214 | /*----------------------------------------------------------------------- 215 | Description: This function calculates the load factor 216 | Input: void 217 | Output: void 218 | ------------------------------------------------------------------------*/ 219 | void Hash::Loadfactor() 220 | { 221 | double m=0; 222 | list < string > ::iterator it; 223 | for (int i = 0; i < HASH_TABLE_SIZE; i++) 224 | { 225 | for (it = hashTable[i].begin(); it != hashTable[i].end(); it++) 226 | { 227 | m++; 228 | } 229 | } 230 | //loadfactor=total number of elemrnts/Hash_table_size 231 | loadfactor = (m/HASH_TABLE_SIZE); 232 | } 233 | 234 | /*----------------------------------------------------------------------- 235 | Description: this functions prints values of number of collisions, 236 | longest chain and average length 237 | Input: void 238 | Output:void 239 | -----------------------------------------------------------------------*/ 240 | void Hash::printStats() 241 | { 242 | Loadfactor(); 243 | cout << "Total Collisions = " << collisions << endl; 244 | cout << "Longest List Ever = " << longestList << endl; 245 | cout << "Average List Length Over Time = " << avgLength << endl; 246 | cout << "Load Factor = "<< loadfactor << endl; 247 | }// print statistics 248 | 249 | /*---------------------------------End---------------------------------*/ 250 | -------------------------------------------------------------------------------- /Hash Tables/hash.h: -------------------------------------------------------------------------------- 1 | /* This assignment originated at UC Riverside. The hash table size 2 | should be defined at compile time. Use -D HASH_TABLE_SIZE=X */ 3 | 4 | /*---------------------------------------------------------------------- 5 | Name : TEJAS PATIL 006949762 6 | Assignment No: 03 7 | Date: .03/28/2015 8 | Description: This file contains the function decleration for the functions 9 | decleared in hash.h . which are used to perform operation on 10 | Hash Table like insert,remove,delete and to calculate number 11 | of collision,avegerage length and loadfactor. 12 | -----------------------------------------------------------------------*/ 13 | 14 | 15 | #ifndef __HASH_H 16 | #define __HASH_H 17 | 18 | #include 19 | #include 20 | 21 | using std::string; 22 | using std::list; 23 | 24 | class Hash { 25 | 26 | public: 27 | Hash(); // constructor 28 | void remove(string); // remove key from hash table 29 | void print(); // print the entire hash table 30 | void processFile(string); // open file and add keys to hash table 31 | bool search(string); // search for a key in the hash table 32 | void output(string); // print entire hash table to a file 33 | void printStats(); // print statistics 34 | 35 | private: 36 | // HASH_TABLE_SIZE should be defined using the -D option for g++ 37 | list hashTable [HASH_TABLE_SIZE]; 38 | int collisions; // total number of collisions 39 | unsigned int longestList; // longest list ever generated 40 | double avgLength; // running average of average list length 41 | 42 | int hf(string); // the hash function 43 | 44 | // put additional functions below as needed 45 | // do not change anything above! 46 | double CurrentavgLength(); //calculates current average length 47 | unsigned int Longestlist(); //calculates longestList's length 48 | void Loadfactor(); //calculates loadfactor 49 | double loadfactor; //loadfactor 50 | 51 | }; 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /Hash Tables/hash_function.cpp: -------------------------------------------------------------------------------- 1 | /* This assignment originated at UC Riverside.*/ 2 | /* 3 | Name: Tejas Patil 006949762 4 | Assignment No: 03 5 | Description: this file contains hash function 6 | */ 7 | 8 | /*----------------------------------------------------------------- 9 | 10 | This hash function is easy to compute and it distribute all the 11 | elements evenly, I have used prime numbers to distribute elements 12 | evenly in to the hash table,it gives lower load factor and reduces 13 | collision.As our input is string so, hash function operates on its 14 | ASCII values to generate hash key.the range of ASCII value is 0-127 15 | This Hash function includes all the characters in to the string to 16 | calculate hash key and produce hash key with respect to inout 17 | string. I have selected every character in string using for loop 18 | and multiples it with 27 and all sum together and divieded by hash 19 | table size. 20 | 21 | Refered from - LINK: https://www.youtube.com/watch?v=4ZJQ6ehmAsg 22 | -------------------------------------------------------------------*/ 23 | 24 | #include 25 | #include "hash.h" 26 | 27 | using std::string; 28 | 29 | 30 | 31 | int Hash::hf ( string ins ) { 32 | 33 | //return ( (int) ins[0] ) % HASH_TABLE_SIZE; 34 | 35 | int i; 36 | int total=0; 37 | int l; 38 | l=ins.length(); 39 | //loop iterates and coever all char in string 40 | for (i = 0;i < l; i++) 41 | { 42 | total = total + ins[l-i]*(27^i); 43 | } 44 | return (total % HASH_TABLE_SIZE); 45 | } 46 | 47 | -------------------------------------------------------------------------------- /IBM/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tpatil2/C-Programs/e7e8274470421f0bcb166318362df112e7df8db8/IBM/a.out -------------------------------------------------------------------------------- /IBM/array_pair.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void sum_up(int* arr, int sum){ 6 | 7 | int size =14 ;//sizeof(arr)/sizeof(arr[0]); 8 | 9 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | using namespace std; 8 | 9 | int main(){ 10 | 11 | char set[] = {'a','b','c'}; 12 | int set_size=3; 13 | 14 | /*set_size of power set of a set with set_size 15 | n is (2**n -1)*/ 16 | unsigned int pow_set_size = pow(2, set_size); 17 | 18 | int counter, j; 19 | /*Run from counter 000..0 to 111..1*/ 20 | for(counter = 0; counter < pow_set_size; counter++) 21 | { 22 | for(j = 0; j < set_size; j++) 23 | { 24 | /* Check if jth bit in the counter is set 25 | If set then pront jth element from set */ 26 | if(counter & (1< 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main(){ 9 | 10 | int len; 11 | int num; 12 | string result = ""; 13 | 14 | cout<<"please enter length of string"; 15 | cin>>len; 16 | srand(time(0)); 17 | 18 | for(int i=0 ;i 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int getRandom(int a) 9 | { 10 | int c=535,d=43443, b=4444; 11 | int ans; 12 | 13 | ans=a*(b/c)+d; 14 | ans=ans%27; 15 | ans=ans+65; 16 | return ans; 17 | } 18 | 19 | int main(){ 20 | 21 | int len; 22 | int num; 23 | string result = ""; 24 | 25 | cout<<"please enter length of string"; 26 | cin>>len; 27 | srand(time(0)); 28 | 29 | for(int i = 0; i < len; i++) 30 | { 31 | cout << getRandom(rand()%30 +1985) << endl; 32 | } 33 | 34 | std::cout << result << std::endl; 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /IBM/reveseword.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | 9 | int main(){ 10 | 11 | string name = "Patil vamanrao Tejas"; 12 | // 01234567890123456789 13 | stack result; 14 | 15 | int i=0 ,s=0; 16 | while(i < name.length()){ 17 | string pass = ""; 18 | while(name[i] != ' ' && i < name.length()){ 19 | pass+=name[i]; 20 | i++; 21 | } 22 | 23 | result.push(pass); 24 | i++; 25 | } 26 | 27 | while(!result.empty()){ 28 | std::cout << result.top()<<" "; 29 | result.pop(); 30 | } 31 | std::cout << std::endl; 32 | 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /IKM C++11/Inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Mother{ 6 | public: 7 | Mother(){}; 8 | void Sayname(); 9 | }; 10 | 11 | void Mother::Sayname(){ 12 | std::cout << "I am Mom" << '\n'; 13 | } 14 | 15 | class Daughter:public Mother{ 16 | 17 | public: 18 | 19 | }; 20 | 21 | int main(){ 22 | 23 | Daughter d; 24 | d.Sayname(); 25 | 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /IKM C++11/Opr_overldng.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | class Mycl{ 7 | 8 | public: 9 | int num; 10 | Mycl(); 11 | Mycl(int); 12 | Mycl operator+(Mycl n); 13 | }; 14 | 15 | Mycl::Mycl(){ 16 | 17 | } 18 | 19 | Mycl::Mycl(int c){ 20 | num = c; 21 | } 22 | 23 | Mycl Mycl::operator+(Mycl obj2){ 24 | Mycl n; 25 | n.num=num+obj2.num; 26 | return n; 27 | } 28 | 29 | int main(){ 30 | 31 | Mycl a(21); 32 | Mycl b(29); 33 | Mycl c; 34 | 35 | c=a+b; 36 | 37 | std::cout << c.num << '\n'; 38 | 39 | } 40 | -------------------------------------------------------------------------------- /IKM C++11/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tpatil2/C-Programs/e7e8274470421f0bcb166318362df112e7df8db8/IKM C++11/a.out -------------------------------------------------------------------------------- /IKM C++11/dynm_mem_alloc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | 7 | cout << "Enter value of n" << '\n'; 8 | cin >> n; 9 | 10 | int* p = (int*)malloc(n*sizeof(int)); 11 | 12 | for(int k=0;k 2 | 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | try{ 9 | int father_age = 50; 10 | int daughter_age = 32; 11 | 12 | if((father_age - daughter_age) < 18){ 13 | throw 99; 14 | } 15 | }catch(int x){ 16 | if(x==99){ 17 | std::cout << "ERROR::99 Age Conflict" << '\n'; 18 | } 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /IKM C++11/friend_fun.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Test{ 6 | private: 7 | int cnt; 8 | public: 9 | Test(){ cnt=0;} 10 | friend void changeVal(Test &ob); 11 | }; 12 | 13 | void changeVal(Test &obj){ 14 | std::cout << " obj->cnt"<< obj.cnt << '\n'; 15 | obj.cnt = 99; 16 | std::cout << " obj->cnt"<< obj.cnt << '\n'; 17 | } 18 | 19 | int main(){ 20 | Test o; 21 | changeVal(o); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /IKM C++11/function_oveld.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void printNum(int x){ 6 | std::cout << "Num is"< 2 | 3 | using namespace std; 4 | 5 | //base class 6 | class Mother{ 7 | private: 8 | int privateVar; 9 | protected: 10 | int protectedVar; 11 | public: 12 | int publicVar; 13 | }; 14 | 15 | class Daughter:public Mother{ 16 | private: 17 | protected: 18 | public: 19 | void printNum(); 20 | }; 21 | 22 | 23 | void Daughter::printNum(){ 24 | protectedVar=1; 25 | publicVar = 2; 26 | privateVar = 6; 27 | } 28 | 29 | int main(){ 30 | 31 | Daughter d; 32 | d.printNum(); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /IKM C++11/regex.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | int main(){ 6 | 7 | string str; 8 | std::cout << "Enter String" << '\n'; 9 | 10 | while(true){ 11 | std::cin >> str; 12 | 13 | //1. regex e("abc"); // match abc 14 | //regex e("abc", regex_constants::icase); //any case 15 | //regex e("abc.", regex_constants::icase); //abc ends with any char 16 | //regex e("abc?"); // zero or one preceding char matched 17 | //regex e("abc*"); // zero or more preceding char matched 18 | //regex e("abc+"); // one or more preceding char matched 19 | //regex e("abc[cd]*"); //[...]any chars inside the sq brackets 20 | //regex e("abc[^xy]*"); //[...]any chars except inside the sq brackets 21 | //regex e("abc[^xy]{3}*"); //[...]any chars except inside the sq brackets 22 | //exact 3 appreance 23 | //regex e("abc[^xy]{3,}*"); //[...]any chars except inside the sq brackets 24 | // 3 or more appreance 25 | //regex e("abc[^xy]{3,5}*"); //[...]any chars except inside the sq brackets 26 | // 3 to 5 more appreance 27 | //regex e("abc|de[fg]"); // | or 28 | //regex e("(abc)de+\\1"); 29 | //regex e("(abc)dc(xyz)\\2\\1"); 30 | //regex e("[[:w:]]+@[[:w:]]+\.com"); 31 | 32 | //bool match = regex_match(str,e); 33 | 34 | //search 35 | //regex e("abc."); //abc as substirng anywhere 36 | //regex e("^abc."); //abc as substirng at the begning 37 | regex e("abc.$"); //abc as substirng at the end 38 | 39 | 40 | bool match = regex_search(str,e); 41 | 42 | std::cout << (match? "matched":"Not matched") << '\n'; 43 | 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /IKM C++11/te.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | 7 | int main() 8 | { 9 | 10 | int rows =10; 11 | int columns =10; 12 | double **darray = new double*[rows]; 13 | 14 | for(int r=0;r 2 | 3 | using namespace std; 4 | 5 | template 6 | 7 | class Cal{ 8 | 9 | T first, second; 10 | 11 | public: 12 | Cal(int a, int b){ 13 | first = a; 14 | second =b; 15 | } 16 | T max(); 17 | }; 18 | 19 | template 20 | 21 | T Cal::max(){ 22 | return (first>second?first:second); 23 | } 24 | 25 | int main() 26 | { 27 | Cal c(22,10); 28 | std::cout << "max is:"< 2 | #define GREETING = "Hello!" 3 | 4 | using namespace std; 5 | 6 | class A{ 7 | public: 8 | A(){ 9 | std::cout << "A" << '\n'; 10 | } 11 | ~A(){ 12 | std::cout << "~A" << '\n'; 13 | 14 | } 15 | }; 16 | 17 | class B:public A{ 18 | public: 19 | B(){ 20 | std::cout << "B" << '\n'; 21 | } 22 | ~B(){ 23 | std::cout << "~B" << '\n'; 24 | 25 | } 26 | 27 | }; 28 | 29 | class C:public B{ 30 | public: 31 | C(){ 32 | std::cout << "C" << '\n'; 33 | } 34 | ~C(){ 35 | std::cout << "~C" << '\n'; 36 | 37 | } 38 | }; 39 | 40 | 41 | 42 | int main(){ 43 | 44 | C obj; 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /IKM C++11/test2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Object { 4 | public: 5 | Object() {} 6 | void Print() const 7 | { 8 | cout << "const" << endl; 9 | } 10 | void Print() 11 | { 12 | cout << "mutable" << endl; 13 | } 14 | }; 15 | void print_obj(const Object& obj) { 16 | obj.Print(); 17 | } 18 | 19 | int main() { 20 | Object obj1; 21 | const Object obj2; 22 | Object*const pobj1 = &obj1; 23 | print_obj(obj1); 24 | print_obj(obj2); 25 | obj1.Print(); 26 | obj2.Print(); 27 | pobj1->Print(); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Linked list problems/Doublylinkedlist.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | create Doubly linked list 3 | */ 4 | 5 | 6 | #include 7 | #include 8 | 9 | 10 | struct node{ 11 | 12 | int data; 13 | node* next; 14 | node* prev; 15 | 16 | }; 17 | 18 | 19 | int main(){ 20 | 21 | node* head; 22 | node* temp; 23 | node* curr; 24 | node* tail; 25 | 26 | int size,ipt; 27 | 28 | std::cout << "Enter size of linked list" << std::endl; 29 | std::cin >> size; 30 | 31 | curr=new node; 32 | std::cout << "Enter the First element" << std::endl; 33 | std::cin >> ipt; 34 | curr->data=ipt; 35 | head=curr; 36 | head->prev=NULL; 37 | temp=curr; 38 | 39 | 40 | for(int i=1;i> ipt; 44 | curr->data=ipt; 45 | temp->next=curr; 46 | curr->prev=temp; 47 | temp=temp->next; 48 | } 49 | 50 | curr->next=NULL; 51 | tail=curr; 52 | std::cout << "Your linked list is : " << std::endl; 53 | 54 | node* tracker; 55 | tracker=head; 56 | 57 | 58 | while(tracker != NULL){ 59 | std::cout << " "<< tracker->data <<"->"; 60 | tracker=tracker->next; 61 | } 62 | std::cout << " " << std::endl; 63 | 64 | std::cout << "Your reverse linked list is : " << std::endl; 65 | 66 | tracker=tail; 67 | while(tracker != NULL){ 68 | std::cout << " "<< tracker->data <<"->"; 69 | tracker=tracker->prev; 70 | } 71 | std::cout << " " << std::endl; 72 | 73 | 74 | 75 | 76 | } 77 | -------------------------------------------------------------------------------- /Linked list problems/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tpatil2/C-Programs/e7e8274470421f0bcb166318362df112e7df8db8/Linked list problems/a.out -------------------------------------------------------------------------------- /Linked list problems/add_linkedlist.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | Problem: You have two numbers represented by a linked list, where each node 4 | contains a single digit. The digits are stored in reverse order, such 5 | that the Ts digit is at the head of the list. Write a function that 6 | adds the two numbers and returns the sum as a linked list. 7 | EXAMPLE 8 | Input: (7-> 1 -> 6) + (5 -> 9 -> 2).That is, 617 + 295. 9 | Output: 2 -> 1 -> 9.That is, 912 10 | */ 11 | 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | using namespace std; 18 | 19 | class node{ 20 | public: 21 | int data; 22 | node* next; 23 | }; 24 | 25 | 26 | node* head1=NULL; 27 | node* tail1=NULL; 28 | node* head2=NULL; 29 | node* tail2=NULL; 30 | std::queue myqueue1; 31 | std::queue myqueue2; 32 | std::queue myqueue3; 33 | 34 | 35 | 36 | 37 | void add_list(){ 38 | int carry=0; 39 | 40 | while (!myqueue1.empty() && !myqueue2.empty() ){ 41 | int sum=0,unit=0; 42 | sum=myqueue1.front()+myqueue2.front()+carry; 43 | carry=sum/10; 44 | unit=sum%10; 45 | myqueue1.pop(); 46 | myqueue2.pop(); 47 | myqueue3.push(unit); 48 | } 49 | 50 | std::cout << "Addition is :" << std::endl; 51 | while (!myqueue3.empty()) { 52 | std::cout << myqueue3.front()<<" "; 53 | myqueue3.pop(); 54 | } 55 | std::cout << std::endl; 56 | return; 57 | 58 | 59 | 60 | 61 | } 62 | 63 | //--------------------------------------------- 64 | 65 | void pass_list(){ 66 | node * temp=new node; 67 | temp=head1; 68 | while(temp->next!=NULL){ 69 | myqueue1.push(temp->data); 70 | temp=temp->next; 71 | } 72 | myqueue1.push(temp->data); 73 | temp=head2; 74 | while(temp->next!=NULL){ 75 | myqueue2.push(temp->data); 76 | temp=temp->next; 77 | } 78 | myqueue2.push(temp->data); 79 | return; 80 | } 81 | 82 | //--------------------------------------------- 83 | void print(){ 84 | node * temp=new node; 85 | 86 | std::cout << "List first " << std::endl; 87 | temp=head1; 88 | while(temp->next != NULL){ 89 | std::cout << temp->data<<" "; 90 | temp=temp->next; 91 | } 92 | std::cout << temp->data<next != NULL){ 97 | std::cout << temp->data<<" "; 98 | temp=temp->next; 99 | } 100 | std::cout << temp->data<data=str1[i]; 115 | curr1->next=NULL; 116 | head1=curr1; 117 | } 118 | else{ 119 | node * temp=new node; 120 | temp->data=str1[i]; 121 | curr1->next=temp; 122 | temp->next=NULL; 123 | tail1=curr1=temp; 124 | } 125 | } 126 | 127 | for(int i=0;i<3;i++){ 128 | if(head2==NULL){ 129 | curr2->data=str2[i]; 130 | curr2->next=NULL; 131 | head2=curr2; 132 | } 133 | else{ 134 | node * temp=new node; 135 | temp->data=str2[i]; 136 | curr2->next=temp; 137 | temp->next=NULL; 138 | tail2=curr2=temp; 139 | } 140 | } 141 | 142 | print(); 143 | pass_list(); 144 | add_list(); 145 | 146 | } 147 | -------------------------------------------------------------------------------- /Linked list problems/create_linked list.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | creat singly linked list 3 | */ 4 | 5 | 6 | #include 7 | #include 8 | 9 | 10 | class node{ 11 | 12 | public: 13 | 14 | int data; 15 | node* next; 16 | 17 | }; 18 | 19 | 20 | node* deletnode(node* list, int x){ 21 | 22 | // When node to be deleted is head node 23 | 24 | if(list == n) 25 | { 26 | if(head->next == NULL) 27 | { 28 | printf("There is only one node. The list can't be made empty "); 29 | return; 30 | } 31 | 32 | /* Copy the data of next node to head */ 33 | head->data = head->next->data; 34 | 35 | // store address of next node 36 | n = head->next; 37 | 38 | // Remove the link of next node 39 | head->next = head->next->next; 40 | 41 | // free memory 42 | free(n); 43 | 44 | return; 45 | 46 | } 47 | 48 | 49 | 50 | 51 | 52 | return list 53 | } 54 | 55 | 56 | int main(){ 57 | 58 | node* head; 59 | node* temp; 60 | node* curr; 61 | int size,ipt; 62 | 63 | std::cout << "Enter size of linked list" << std::endl; 64 | std::cin >> size; 65 | 66 | curr=new node; 67 | std::cout << "Enter the First element" << std::endl; 68 | std::cin >> ipt; 69 | curr->data=ipt; 70 | head=curr; 71 | temp=curr; 72 | 73 | 74 | for(int i=1;i> ipt; 78 | curr->data=ipt; 79 | temp->next=curr; 80 | temp=temp->next; 81 | } 82 | 83 | curr->next=NULL; 84 | 85 | std::cout << "Your linked list is : " << std::endl; 86 | 87 | node* tracker; 88 | tracker=head; 89 | 90 | 91 | while(tracker != NULL){ 92 | std::cout << " "<< tracker->data <<"->"; 93 | tracker=tracker->next; 94 | } 95 | std::cout << " " << std::endl; 96 | 97 | 98 | 99 | std::cout << "curr-Data 1 :"<data << std::endl; 100 | 101 | curr=head; 102 | 103 | std::cout << "curr-Data 2 :"<data << std::endl; 104 | 105 | 106 | 107 | for(int i=0 ; i<3;i++){ 108 | 109 | curr=curr->next; 110 | 111 | } 112 | 113 | std::cout << "curr-Data 4 :"<data << std::endl; 114 | 115 | 116 | std::cout << "Deleting Node " << std::endl; 117 | 118 | node* extra; 119 | 120 | extra=curr; 121 | 122 | extra->data=curr->next->data; 123 | extra->next=curr->next->next; 124 | 125 | //delete curr; 126 | 127 | 128 | tracker=head; 129 | 130 | 131 | while(tracker != NULL){ 132 | std::cout << " "<< tracker->data <<"->"; 133 | tracker=tracker->next; 134 | } 135 | std::cout << " " << std::endl; 136 | 137 | } 138 | -------------------------------------------------------------------------------- /Linked list problems/palindrome_linklist.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | Problem: check for palindrom singly linked list 4 | */ 5 | 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | class node{ 14 | public: 15 | char data; 16 | node* next; 17 | }; 18 | 19 | 20 | node* head=NULL; 21 | node* tail=NULL; 22 | 23 | bool check_palindrome(){ 24 | node* slowptr; 25 | node* fastptr; 26 | slowptr=fastptr=head; 27 | 28 | std::stack mystack; 29 | 30 | while(fastptr!=NULL && fastptr->next != NULL){ 31 | mystack.push(slowptr->data); 32 | fastptr=fastptr->next->next; 33 | slowptr=slowptr->next; 34 | } 35 | 36 | //for odd length need to move next to midpoint 37 | if(fastptr != NULL) slowptr=slowptr->next; 38 | //pop elements and check with remaining list 39 | while(slowptr != NULL){ 40 | if(slowptr->data != mystack.top()) return false; 41 | mystack.pop(); 42 | slowptr=slowptr->next; 43 | } 44 | 45 | return true; 46 | } 47 | 48 | void Ispalindrome() { 49 | 50 | if(check_palindrome()){ 51 | std::cout << "Linked list is palindrome" << std::endl; 52 | } 53 | else{ 54 | std::cout << "Linked list is not palindrome" << std::endl; 55 | } 56 | std::cout << "---------------------" << std::endl; 57 | 58 | } 59 | 60 | void print(){ 61 | 62 | node * temp=new node; 63 | temp=head; 64 | while(temp->next != NULL){ 65 | std::cout << temp->data<<" "; 66 | temp=temp->next; 67 | } 68 | 69 | std::cout << temp->data<data=str[i]; 79 | curr->next=NULL; 80 | head=curr; 81 | print(); 82 | Ispalindrome(); 83 | 84 | } 85 | else{ 86 | node * temp=new node; 87 | temp->data=str[i]; 88 | curr->next=temp; 89 | temp->next=NULL; 90 | tail=curr=temp; 91 | print(); 92 | Ispalindrome(); 93 | 94 | } 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /Linked list problems/sll.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Singly linked list 3 | */ 4 | 5 | 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | class Node 12 | { 13 | 14 | private: 15 | int data; 16 | node* next; 17 | 18 | public: 19 | void insert_node(int data); 20 | void display(); 21 | 22 | }; 23 | 24 | node* curr; 25 | node* head; 26 | node* tail; 27 | 28 | 29 | int main(){ 30 | 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Median.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | 8 | int main(){ 9 | 10 | int A=9,B=33,C=11; 11 | 12 | int median; 13 | 14 | if ((A - B) * (C - B) <= 0) { 15 | median=B; 16 | } else if ((B - C) * (A - C) <= 0) { 17 | median=C; 18 | } else { 19 | median=A; 20 | } 21 | 22 | std::cout << "median is "<< median << std::endl; 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Problems/Dec_Bin.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N. 3 | For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. 4 | Write a function: 5 | int solution(int N); 6 | that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap. 7 | For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. 8 | Assume that: 9 | N is an integer within the range [1..2,147,483,647]. 10 | Complexity: 11 | expected worst-case time complexity is O(log(N)); 12 | expected worst-case space complexity is O(1). 13 | Copyright 2009–2016 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited. 14 | 15 | */ 16 | 17 | #include 18 | #include 19 | 20 | using namespace std; 21 | 22 | int solution(int n){ 23 | 24 | stack bin; 25 | int currlen=0; 26 | int maxlen=0; 27 | bool found_one=false; 28 | bool found_zero=false; 29 | 30 | while(n>0){ 31 | int i = n%2; 32 | if(i==1){ 33 | if(found_zero == true){ 34 | if(maxlen < currlen) maxlen = currlen; 35 | currlen = 0; 36 | } 37 | found_one=true; 38 | found_zero=false; 39 | }else { 40 | if(found_one==true) { 41 | found_zero=true; 42 | currlen++; 43 | } 44 | } 45 | bin.push(i); 46 | n=n/2; 47 | } 48 | //print binary number 49 | while(!bin.empty()){ 50 | std::cout < solution(string &S, vector &P, vector &Q); 19 | 20 | */ 21 | #include 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | vector solution(string &S, vector &P, vector &Q) { 28 | // write your code in C++11 (g++ 4.8.2) 29 | vector R; 30 | unsigned int size = P.size(); 31 | // string S = "CAGCCTA"; 32 | // 0123456 33 | for(unsigned int i=0;i P ={2,5,0}; 50 | vector Q ={4,5,6}; 51 | vector result; 52 | 53 | result=solution(S,P,Q); 54 | 55 | for(int i:result){ 56 | std::cout << i <<" "; 57 | } 58 | 59 | std::cout << std::endl; 60 | 61 | 62 | 63 | 64 | 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Problems/PermCheck.cpp: -------------------------------------------------------------------------------- 1 | // Check whether array A is a permutation. 2 | //Codility TASK free trial 3 | /*NOTE: Task Description 4 | A non-empty zero-indexed array A consisting of N integers is given. 5 | A permutation is a sequence containing each element from 1 to N once, and only once. 6 | For example, array A such that: 7 | A[0] = 4 8 | A[1] = 1 9 | A[2] = 3 10 | A[3] = 2 11 | is a permutation, but array A such that: 12 | A[0] = 4 13 | A[1] = 1 14 | A[2] = 3 15 | is not a permutation, because value 2 is missing. 16 | The goal is to check whether array A is a permutation. 17 | Write a function: 18 | int solution(vector &A); 19 | that, given a zero-indexed array A, returns 1 if array A is a permutation and 0 if it is not. 20 | */ 21 | 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | int solution(vector &A) { 28 | // write your code in C++11 (g++ 4.8.2) 29 | 30 | unsigned int temp = 0; 31 | unsigned int n = A.size(); 32 | 33 | for(unsigned int i=0; i A = {1,2,3,4,5,6,8}; 46 | 47 | if(solution(A)) std::cout << "It is permutation" << std::endl; 48 | else std::cout << "It is not permutation" << std::endl; 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Problems/Tape_equilibrium.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A non-empty zero-indexed array A consisting of N integers is given. Array A represents numbers on a tape. 3 | Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1]. 4 | The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])| 5 | In other words, it is the absolute difference between the sum of the first part and the sum of the second part. 6 | For example, consider array A such that: 7 | A[0] = 3 8 | A[1] = 1 9 | A[2] = 2 10 | A[3] = 4 11 | A[4] = 3 12 | We can split this tape in four places: 13 | P = 1, difference = |3 − 10| = 7 14 | P = 2, difference = |4 − 9| = 5 15 | P = 3, difference = |6 − 7| = 1 16 | P = 4, difference = |10 − 3| = 7 17 | Write a function: 18 | int solution(vector &A); 19 | that, given a non-empty zero-indexed array A of N integers, returns the minimal difference that can be achieved. 20 | For example, given: 21 | A[0] = 3 22 | A[1] = 1 23 | A[2] = 2 24 | A[3] = 4 25 | A[4] = 3 26 | the function should return 1, as explained above. 27 | Assume that: 28 | N is an integer within the range [2..100,000]; 29 | each element of array A is an integer within the range [−1,000..1,000]. 30 | 31 | Complexity: 32 | expected worst-case time complexity is O(N); 33 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 34 | 35 | Elements of input arrays can be modified. 36 | Copyright 2009–2016 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited. 37 | */// you can use includes, for example: 38 | 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | 47 | // you can write to stdout for debugging purposes, e.g. 48 | // cout << "this is a debug message" << endl; 49 | 50 | using namespace std; 51 | 52 | int solution(vector &A) { 53 | // write your code in C++11 (g++ 4.8.2) 54 | vector::iterator it; 55 | vector reverse_sum; 56 | unsigned int size; 57 | size=A.size(); 58 | reverse_sum.reserve(size-1); 59 | reverse_sum.push_back(*(A.end()-1)); 60 | long long sumA = *A.begin(); 61 | long long result = LLONG_MAX; 62 | int n=0; 63 | int i=0; 64 | 65 | for(it=A.end()-2;it!=A.begin();it--){ 66 | reverse_sum.push_back(reverse_sum[n] + *it); 67 | n++; 68 | }// ( 6,11,15,18,20,) 69 | 70 | while(n>-1){ 71 | if(i!=0){ 72 | sumA += *(A.begin()+i); 73 | } 74 | i++; 75 | result=min(result,abs(sumA - reverse_sum[n])); 76 | n--; 77 | } 78 | 79 | return result; 80 | } 81 | 82 | 83 | int main(){ 84 | 85 | vector A = {1,2,3,4,5,6}; 86 | 87 | std::cout << "val is " << solution(A)< 2 | #include 3 | using namespace std; 4 | 5 | void print(vector vec){ 6 | 7 | for(auto it=vec.begin();it cal_intersection(vector A,vector B){ 15 | 16 | vector result; 17 | for(auto it=A.begin();it!=A.end();it++){ 18 | for(auto it2=B.begin();it2!=B.end();it2++){ 19 | if(*it==*it2){ 20 | result.push_back(*it); 21 | break; 22 | } 23 | } 24 | } 25 | 26 | return result; 27 | } 28 | 29 | vector cal_union(vector A,vector B){ 30 | 31 | vector result; 32 | bool flag=false; 33 | for(auto it=A.begin();it!=A.end();it++){ 34 | 35 | for(auto it2=B.begin();it2!=B.end();it2++){ 36 | if(*it==*it2){ 37 | flag=true; 38 | } 39 | } 40 | if(flag==0) B.push_back(*it); 41 | flag=false; 42 | } 43 | 44 | return B; 45 | } 46 | 47 | 48 | int main(){ 49 | 50 | vector A = {2,4,3,5,6}; 51 | vector B = {1,2,6,5,0}; 52 | vector Result; 53 | 54 | std::cout << "intersection" << std::endl; 55 | Result=cal_intersection(A,B); 56 | print(Result); 57 | 58 | std::cout << "union" << std::endl; 59 | Result=cal_union(A,B); 60 | print(Result); 61 | 62 | 63 | 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Problems/bouncing_ball.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int solution(vector &A, int skip){ 6 | 7 | int count; 8 | for(int i:A) std::cout << i <<" "; 9 | 10 | 11 | 12 | return count; 13 | } 14 | 15 | int main(){ 16 | vector A; 17 | 18 | for(int i=0;i<100;i++){ 19 | A.push_back(i); 20 | } 21 | 22 | std::cout << "Skip count is : "<< solution(A,3) << std::endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Problems/brackets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | 7 | // you can use includes, for example: 8 | // #include 9 | #include 10 | using namespace std; 11 | 12 | // you can write to stdout for debugging purposes, e.g. 13 | // cout << "this is a debug message" << endl; 14 | 15 | int solution(string &S) { 16 | // write your code in C++11 (g++ 4.8.2) 17 | int size = S.size(); 18 | int counter1=0; 19 | int counter2=0; 20 | int counter3=0; 21 | 22 | for(int i=0;i solution(vector &A, int K); 9 | that, given a zero-indexed array A consisting of N integers and an integer K, returns the array A rotated K times. 10 | For example, given array A = [3, 8, 9, 7, 6] and K = 3, the function should return [9, 7, 6, 3, 8]. 11 | Assume that: 12 | N and K are integers within the range [0..100]; 13 | each element of array A is an integer within the range [−1,000..1,000]. 14 | In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment. 15 | Copyright 2009–2016 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited. 16 | */ 17 | 18 | #include 19 | #include 20 | 21 | using namespace std; 22 | 23 | vector solution(vector &A, int K) { 24 | // write your code in C++11 (g++ 4.8.2) 25 | 26 | size_t j= A.size()-2; 27 | 28 | if(A.size()>1){ 29 | for(int i=0;i0){ 33 | A[j+1]=A[j]; 34 | j--; 35 | } 36 | A[j+1]=temp; 37 | j= A.size()-2; 38 | } 39 | } 40 | // write your code in C++1git1 (g++ 4.8.2) 41 | return A; 42 | } 43 | 44 | int main(){ 45 | 46 | vector A = { 3, 8, 9, 7, 6}; 47 | 48 | for(auto it=A.begin();it != A.end();it++){ 49 | std::cout << *it<<" "; 50 | } 51 | 52 | std::cout << "\nAfter shifting" << std::endl; 53 | A = solution(A,3); 54 | 55 | for(auto it=A.begin();it != A.end();it++){ 56 | std::cout << *it <<" " ; 57 | } 58 | std::cout << std::endl; 59 | 60 | } 61 | -------------------------------------------------------------------------------- /Problems/distinct_num.cpp: -------------------------------------------------------------------------------- 1 | /*Write a function 2 | int solution(vector &A); 3 | that, given a zero-indexed array A consisting of N integers, returns the number of distinct values in array A. 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | 13 | int solution(vector &A) { 14 | // write your code in C++11 (g++ 4.8.2) 15 | if(A.size()==1) return 1; 16 | 17 | sort(A.begin(),A.end()); 18 | int count=0; 19 | 20 | for(auto it=A.begin();it!=A.end();it++){ 21 | 22 | while(*it == *(it+1)){ 23 | it++; 24 | } 25 | count++; 26 | } 27 | 28 | return count; 29 | } 30 | 31 | int main(){ 32 | 33 | vector A = {2,1,3,2,1,1,2,6,8,6,4,2,1}; 34 | 35 | // std::cout << "Number of Distince elements are : "<< 36 | // solution(A) << std::endl; 37 | 38 | std::cout << "last elements : "<< *(A.end()-2) << std::endl; 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Problems/divcount.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a function: 3 | int solution(int A, int B, int K); 4 | that, given three integers A, B and K, returns the number of integers within the range [A..B] that are divisible by K, i.e.: 5 | { i : A ≤ i ≤ B, i mod K = 0 } 6 | For example, for A = 6, B = 11 and K = 2, your function should return 3, because there are three numbers divisible by 2 within the range [6..11], namely 6, 8 and 10. 7 | Assume that: 8 | A and B are integers within the range [0..2,000,000,000]; 9 | K is an integer within the range [1..2,000,000,000]; 10 | A ≤ B. 11 | Complexity: 12 | expected worst-case time complexity is O(1); 13 | expected worst-case space complexity is O(1). 14 | */ 15 | 16 | #include 17 | #include 18 | 19 | using namespace std; 20 | 21 | int solution(int A,int B, int K){ 22 | int diff= B/K-A/K; // 0-0 23 | 24 | if(A%K == 0) diff+=1; // 22 25 | 26 | return diff; 27 | } 28 | 29 | int main(){ 30 | 31 | std::cout << "Divsion count is : "<< solution(10,15,13) << std::endl; 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Problems/fibonacchi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | 7 | void fib_seris(int n){ 8 | 9 | 10 | int first = 0; 11 | int second = 1; 12 | int next; 13 | 14 | for(int i=0;i<10;i++){ 15 | 16 | if(i<=1){ 17 | next=first+second; 18 | std::cout << next << '\n'; 19 | first = second; 20 | second=next; 21 | }else{ 22 | next=first+second; 23 | std::cout << next << '\n'; 24 | first = second; 25 | second=next; 26 | } 27 | 28 | } 29 | } 30 | 31 | 32 | int main(){ 33 | 34 | int n = 100; 35 | fib_seris(n); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Problems/fish.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tpatil2/C-Programs/e7e8274470421f0bcb166318362df112e7df8db8/Problems/fish.cpp -------------------------------------------------------------------------------- /Problems/frogjmp.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D. 4 | Count the minimal number of jumps that the small frog must perform to reach its target. 5 | Write a function: 6 | int solution(int X, int Y, int D); 7 | that, given three integers X, Y and D, returns the minimal number of jumps from position X to a position equal to or greater than Y. 8 | For example, given: 9 | X = 10 10 | Y = 85 11 | D = 30 12 | the function should return 3, because the frog will be positioned as follows: 13 | after the first jump, at position 10 + 30 = 40 14 | after the second jump, at position 10 + 30 + 30 = 70 15 | after the third jump, at position 10 + 30 + 30 + 30 = 100 16 | Assume that: 17 | X, Y and D are integers within the range [1..1,000,000,000]; 18 | X ≤ Y. 19 | Complexity: 20 | expected worst-case time complexity is O(1); 21 | expected worst-case space complexity is O(1). 22 | Copyright 2009–2016 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited. 23 | */ 24 | 25 | // you can use includes, for example: 26 | // #include 27 | #include 28 | #include 29 | 30 | using namespace std; 31 | 32 | int solution(int X, int Y, int D) { 33 | // write your code in C++11 (g++ 4.8.2) 34 | long long jump; 35 | jump = ceil(abs(double(X-Y))/D); 36 | 37 | return jump; 38 | } 39 | 40 | 41 | int main(){ 42 | 43 | std::cout << "Min number of jumps are : "<< solution(50,2222,30) << std::endl; 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Problems/frogriverone.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A small frog wants to get to the other side of a river. The frog is initially located on one bank of the river (position 0) and wants to get to the opposite bank (position X+1). Leaves fall from a tree onto the surface of the river. 3 | You are given a zero-indexed array A consisting of N integers representing the falling leaves. A[K] represents the position where one leaf falls at time K, measured in seconds. 4 | The goal is to find the earliest time when the frog can jump to the other side of the river. The frog can cross only when leaves appear at every position across the river from 1 to X (that is, we want to find the earliest moment when all the positions from 1 to X are covered by leaves). You may assume that the speed of the current in the river is negligibly small, i.e. the leaves do not change their positions once they fall in the river. 5 | For example, you are given integer X = 5 and array A such that: 6 | A[0] = 1 7 | A[1] = 3 8 | A[2] = 1 9 | A[3] = 4 10 | A[4] = 2 11 | A[5] = 3 12 | A[6] = 5 13 | A[7] = 4 14 | In second 6, a leaf falls into position 5. This is the earliest time when leaves appear in every position across the river. 15 | Write a function: 16 | int solution(int X, vector &A); 17 | */ 18 | 19 | #include 20 | #include 21 | 22 | using namespace std; 23 | 24 | int solution(int X,vector &A){ 25 | 26 | int target = X; 27 | int size = A.size(); 28 | vector position; 29 | position.resize(X+1,false); 30 | 31 | for(int i=0; i< size ;i++){ 32 | if( position[A[i]] == false){ 33 | position[A[i]]=true; 34 | target--; 35 | } 36 | if(target == 0){ 37 | return i; 38 | } 39 | } 40 | return -1; 41 | } 42 | 43 | 44 | 45 | int main(){ 46 | 47 | vector A = {1,3,1,4,2,3,5,4}; 48 | std::cout << "Time is " << solution(10,A) << std::endl; 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Problems/max_counters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | vector solutions(int N, vector &A){ 8 | 9 | // write your code in C++11 (g++ 4.8.2) 10 | vector counter; 11 | counter.resize(N,0); 12 | int Max=0; 13 | 14 | for( unsigned int i=0;iN){ 16 | fill(counter.begin(),counter.end(),Max); 17 | } 18 | else { 19 | counter[A[i]-1]++; 20 | } 21 | Max = max(Max, counter[A[i]-1]); 22 | } 23 | 24 | return counter; 25 | } 26 | 27 | int main(){ 28 | 29 | vector A = {3, 4, 4, 6, 1, 4, 4}; 30 | vector opt; 31 | opt=solutions(1,A); 32 | 33 | for(auto it=opt.begin();it!=opt.end();it++){ 34 | std::cout << *it <<" "; 35 | } 36 | std::cout << std::endl; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Problems/minAvgtwo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int solution(vector &A) { 7 | // write your code in C++11 (g++ 4.8.2) 8 | int N = A.size(); 9 | double min_avg=A[0]+A[1]/2.0; 10 | int min_index=0; 11 | 12 | for(unsigned int i=2;i A = {4, 2, 2, 5, 1, 5, 8}; 35 | 36 | std::cout << "Starting index of pair is "< &A); 6 | that, given a zero-indexed array A, returns the value of the missing element. 7 | For example, given array A such that: 8 | A[0] = 2 9 | A[1] = 3 10 | A[2] = 1 11 | A[3] = 5 12 | the function should return 4, as it is the missing element. 13 | Assume that: 14 | N is an integer within the range [0..100,000]; 15 | the elements of A are all distinct; 16 | each element of array A is an integer within the range [1..(N + 1)]. 17 | Complexity: 18 | expected worst-case time complexity is O(N); 19 | expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments). 20 | Elements of input arrays can be modified. 21 | Copyright 2009–2016 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited. 22 | */ 23 | #include 24 | #include 25 | 26 | using namespace std; 27 | 28 | int solution(vector &A) { 29 | // write your code in C++11 (g++ 4.8.2) 30 | unsigned int n = A.size()+1; 31 | int x = 0; 32 | 33 | if(n!=1){ 34 | for(unsigned int i=1;i<=n;i++){ 35 | x = x ^ i ^ A[i-1]; 36 | } 37 | } else x=1; 38 | 39 | return x; 40 | } 41 | 42 | int main(){ 43 | 44 | vector A = {1,2,3,5,6,7,8}; 45 | 46 | std::cout << "Missing number is "<< solution(A) << std::endl; 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Problems/monkey.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tpatil2/C-Programs/e7e8274470421f0bcb166318362df112e7df8db8/Problems/monkey.cpp -------------------------------------------------------------------------------- /Problems/nesting.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A string S consisting of N characters is called properly nested if: 3 | 4 | S is empty; 5 | S has the form "(U)" where U is a properly nested string; 6 | S has the form "VW" where V and W are properly nested strings. 7 | 8 | For example, string "(()(())())" is properly nested but string "())" isn't. 9 | 10 | Write a function: 11 | 12 | int solution(string &S); 13 | 14 | */ 15 | 16 | #include 17 | #include 18 | using namespace std; 19 | 20 | int solution(string &S) { 21 | // write your code in C++11 (g++ 4.8.2) 22 | int size=S.size(); 23 | int count=0; 24 | 25 | for(int i=0;i &A); 22 | 23 | that, given an array A describing N discs as explained above, returns the number of (unordered) pairs of intersecting discs. The function should return −1 if the number of intersecting pairs exceeds 10,000,000. 24 | 25 | Given array A shown above, the function should return 11, as explained above. 26 | 27 | */ 28 | //REF: https://codesays.com/2014/solution-to-beta2010-number-of-disc-intersections-by-codility/ 29 | 30 | #include 31 | #include 32 | #include // std::pair 33 | #include 34 | 35 | using namespace std; 36 | 37 | 38 | int solution(vector &A) { 39 | // write your code in C++11 (g++ 4.8.2) 40 | int len = A.size(); 41 | if (0 == len)return 0; 42 | 43 | vector > intervals; 44 | 45 | for (int i = 0; i < len; ++i) 46 | intervals.push_back(make_pair(i - (long long)A[i], i + (long long)A[i])); 47 | sort(intervals.begin(), intervals.end()); 48 | 49 | for(int i=0;i= intervals[m].first) 63 | l = m + 1; 64 | else 65 | h = m - 1; 66 | } 67 | count += l - 1 - i; 68 | if (count > 10000000) 69 | return -1; 70 | } 71 | return count; 72 | 73 | } 74 | 75 | int main(){ 76 | 77 | vector A = {1,5,2,1,4,0}; 78 | 79 | std::cout << "number of disc intersection is : "<< 80 | solution(A) << std::endl; 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /Problems/pait_multi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void print(vector vec){ 8 | 9 | for(auto it=vec.begin();it cal_multi(vector A,vector B){ 17 | vector result; 18 | 19 | int size_a=A.size(); 20 | int size_b=B.size(); 21 | int i=0,j=0; 22 | bool flag=true; 23 | 24 | 25 | lable:while(i < size_a && j A = {1,2,3,4,5}; 51 | vector B = {1,2,3}; 52 | 53 | vector Result; 54 | std::cout << "result is" << std::endl; 55 | Result=cal_multi(A,B); 56 | print(Result); 57 | 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Problems/passing_cars.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int solution(vector &A) { 7 | //NOTE: REF : https://codesays.com/2014/solution-to-passing-cars-by-codility/ 8 | int count = 0; 9 | int passing = 0; 10 | 11 | // for each value in the vector A 12 | for (int i : A) 13 | { 14 | if (i == 1) { 15 | passing += count; 16 | if(passing > 1000000000) 17 | return -1; 18 | }else 19 | count++; 20 | } 21 | return passing; 22 | } 23 | 24 | int main(){ 25 | 26 | vector A = {1,0,1}; 27 | std::cout << "Total Pairs are : "< &A); 6 | that, given a zero-indexed array A, returns the value of the missing element. 7 | For example, given array A such that: 8 | A[0] = 2 9 | A[1] = 3 10 | A[2] = 1 11 | A[3] = 5 12 | the function should return 4, as it is the missing element. 13 | Assume that: 14 | N is an integer within the range [0..100,000]; 15 | the elements of A are all distinct; 16 | each element of array A is an integer within the range [1..(N + 1)]. 17 | Complexity: 18 | expected worst-case time complexity is O(N); 19 | expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments). 20 | Elements of input arrays can be modified. 21 | Copyright 2009–2016 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited. 22 | */ 23 | #include 24 | #include 25 | 26 | using namespace std; 27 | 28 | int solution(vector &A) { 29 | // write your code in C++11 (g++ 4.8.2) 30 | unsigned int n = A.size()+1; 31 | long long sum = n*(n+1)/2; 32 | 33 | for(auto it=A.begin();it!=A.end();it++){ 34 | sum -= *it; 35 | } 36 | 37 | return sum; 38 | } 39 | 40 | int main(){ 41 | 42 | vector A = {1,2,3,4,5,6,7,8}; 43 | 44 | std::cout << "Missing number is "<< solution(A) << std::endl; 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Problems/product_triplet.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Task description 3 | 4 | A non-empty zero-indexed array A consisting of N integers is given. The product of triplet (P, Q, R) equates to A[P] * A[Q] * A[R] (0 ≤ P < Q < R < N). 5 | 6 | For example, array A such that: 7 | A[0] = -3 8 | A[1] = 1 9 | A[2] = 2 10 | A[3] = -2 11 | A[4] = 5 12 | A[5] = 6 13 | 14 | contains the following example triplets: 15 | 16 | (0, 1, 2), product is −3 * 1 * 2 = −6 17 | (1, 2, 4), product is 1 * 2 * 5 = 10 18 | (2, 4, 5), product is 2 * 5 * 6 = 60 19 | 20 | Your goal is to find the maximal product of any triplet. 21 | */ 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | using namespace std; 28 | 29 | 30 | 31 | int solution(vector &A) { 32 | // write your code in C++11 (g++ 4.8.2) 33 | if(A.size() < 3) return 0; 34 | sort(A.begin(),A.end()); 35 | int N = A.size(); 36 | int Max = max(A[0]*A[1]*A[N-1] , A[N-1]*A[N-2]*A[N-3]); 37 | 38 | return Max; 39 | } 40 | 41 | 42 | int main(){ 43 | 44 | vector A = {2,1,3,2,1,1,2,6,8,6,4,2,1}; 45 | 46 | std::cout << "Max product of triplet is : "<< 47 | solution(A) << std::endl; 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Problems/triangle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A zero-indexed array A consisting of N integers is given. A triplet (P, Q, R) is triangular if 0 ≤ P < Q < R < N and: 3 | 4 | A[P] + A[Q] > A[R], 5 | A[Q] + A[R] > A[P], 6 | A[R] + A[P] > A[Q]. 7 | 8 | For example, consider array A such that: 9 | A[0] = 10 A[1] = 2 A[2] = 5 10 | A[3] = 1 A[4] = 8 A[5] = 20 11 | 12 | Triplet (0, 2, 4) is triangular. 13 | 14 | Write a function: 15 | 16 | int solution(vector &A); 17 | 18 | that, given a zero-indexed array A consisting of N integers, returns 1 if there exists a triangular triplet for this array and returns 0 otherwise. 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | 28 | int solution(vector &A) { 29 | // write your code in C++11 (g++ 4.8.2) 30 | sort(A.begin(),A.end()); 31 | int N=A.size(); 32 | if(N<3) return 0; 33 | 34 | for(auto it=A.begin();it *(it+2)) return 1; 37 | 38 | } 39 | 40 | return 0; 41 | 42 | } 43 | 44 | int main(){ 45 | 46 | vector A = {3,3,5,7,3,8,0,55,3,2,24}; 47 | 48 | if(solution(A)) std::cout << "YES" << std::endl; 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Problems/union_inter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void print(vector vec){ 8 | 9 | for(auto it=vec.begin();it cal_intersection(vector A,vector B){ 17 | 18 | sort (A.begin(), A.end()); 19 | sort (B.begin(), B.end()); 20 | 21 | int size_a = A.size(); 22 | int size_b = B.size(); 23 | int i = 0, j = 0; 24 | 25 | vector result; 26 | 27 | while (i < size_a && j < size_b) 28 | { 29 | if (A[i] < B[j]) 30 | i++; 31 | else if (A[j] < B[i]) 32 | j++; 33 | else 34 | { 35 | result.push_back(B[j++]); 36 | i++; 37 | } 38 | } 39 | 40 | return result; 41 | } 42 | 43 | vector cal_union(vector A,vector B){ 44 | 45 | sort (A.begin(), A.end()); 46 | sort (B.begin(), B.end()); 47 | 48 | int i = 0, j = 0; 49 | int size_a = A.size(); 50 | int size_b = B.size(); 51 | 52 | vector result; 53 | 54 | while (i < size_a && j < size_b) 55 | { 56 | if (A[i] < B[j]) 57 | result.push_back(A[i++]); 58 | else if (A[j] < B[i]) 59 | result.push_back(B[j++]); 60 | else 61 | { 62 | result.push_back(B[j++]); 63 | i++; 64 | } 65 | } 66 | 67 | /* Print remaining elements of the larger array */ 68 | while(i < size_a) 69 | result.push_back(A[i++]); 70 | while(j < size_b) 71 | result.push_back(B[j++]); 72 | 73 | return result; 74 | 75 | } 76 | 77 | 78 | int main(){ 79 | 80 | vector A = {2,4,3,5,6,2,5}; 81 | vector B = {1,2,6,5,0,0,0,2,5}; 82 | vector Result; 83 | 84 | std::cout << "intersection" << std::endl; 85 | Result=cal_intersection(A,B); 86 | print(Result); 87 | 88 | std::cout << "union" << std::endl; 89 | Result=cal_union(A,B); 90 | print(Result); 91 | 92 | 93 | 94 | 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /Problems/word_exor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | using namespace std; 7 | 8 | 9 | 10 | 11 | int main(){ 12 | 13 | char text[] = "I am here for all my life"; 14 | char text2[] = "They are here to win us all"; 15 | 16 | char *word = strtok(text, " "); 17 | 18 | while (word != NULL) { 19 | if (strstr(text2, word)) { 20 | /* Match found */ 21 | printf("Match: %s\n", word); 22 | } 23 | word = strtok(NULL, " "); 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Problems/zigzag.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | void print(int array[], int n){ 8 | 9 | std::cout << "array is"<< std::endl; 10 | for(int i=0;iarr[i+1]) swap(arr[i],arr[i+1]); 27 | }else{ 28 | if(arr[i] 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | 8 | ifstream ip("data.csv"); 9 | 10 | if(!ip.is_open()) std::cout << "ERROR: File Open" << '\n'; 11 | 12 | string firstname; 13 | string lastname; 14 | string age; 15 | string weight; 16 | 17 | while(ip.good()){ 18 | 19 | getline(ip,firstname,','); 20 | getline(ip,lastname,','); 21 | getline(ip,age,','); 22 | getline(ip,weight,'\n'); 23 | 24 | std::cout << "Name: "< 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | long int get_factorial(int number){ 9 | 10 | long int factorial; 11 | if(number == 1) factorial=1; 12 | else factorial = get_factorial(number-1)*number; 13 | 14 | return factorial; 15 | 16 | } 17 | 18 | int main(int argc, char const *argv[]) { 19 | 20 | int num; 21 | std::cout << "enter your number" << std::endl; 22 | cin >>num; 23 | 24 | long int answer = get_factorial(num); 25 | 26 | std::cout << "Factorial of "< 2 | 3 | using namespace std; 4 | //1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 5 | int fibonacci(int n){ 6 | int sum=0; 7 | if(n<=1){ 8 | return n; 9 | } 10 | int f1=0; 11 | int f2=1; 12 | 13 | for(int i=2;i<=n;i++){ 14 | std::cout << sum << " " ; 15 | sum = f1 + f2; 16 | f1=f2; 17 | f2=sum; 18 | } 19 | 20 | return sum; 21 | } 22 | 23 | int main(){ 24 | 25 | int n; 26 | std::cout << "Entet your number" << std::endl; 27 | cin>>n; 28 | 29 | int result = fibonacci(n); 30 | std::cout << "result is : "<< result << std::endl; 31 | 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Recursion/fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int fibonacci(int n){ 6 | 7 | if(n<=1){ 8 | return n; 9 | } 10 | return fibonacci(n-1)+fibonacci(n-2); 11 | } 12 | 13 | int main(){ 14 | 15 | int n; 16 | std::cout << "Entet your number" << std::endl; 17 | cin>>n; 18 | 19 | int result = fibonacci(n); 20 | std::cout << "result is : "<< result << std::endl; 21 | 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Red Black Tree/rbapp.cpp: -------------------------------------------------------------------------------- 1 | /**----------------------------------------------------------------- 2 | *@File : rbapp.cpp 3 | *@Name:Tejas Patil @ChicoStateID: 006949762 4 | *@Assignment No: 04 5 | *@Description:This file takes input and pass it to functions in 6 | rbapp.cpp to make operation on Red Black Tree 7 | *@Date: 18 April 2015 8 | *-------------------------------------------------------------------*/ 9 | 10 | 11 | #include 12 | #include 13 | #include "rbapp.h" 14 | 15 | using namespace std; 16 | 17 | /*---------------------------------------------------------------------- 18 | *Description: RBapp constructor 19 | *INPUT: void 20 | *OUTPUT: void 21 | *----------------------------------------------------------------------*/ 22 | 23 | RBapp::RBapp() 24 | { 25 | done=false; 26 | } 27 | 28 | /*----------------------------------------------------------------------- 29 | *Description: Main function start of the program which calls mainloop() 30 | *INPUT: void 31 | *OUTPUT: void 32 | *----------------------------------------------------------------------*/ 33 | 34 | int main() 35 | { 36 | RBapp myRBTapp; 37 | myRBTapp.mainLoop(); 38 | return 0; 39 | } 40 | 41 | /*----------------------------------------------------------------------- 42 | *Description: This function checks for n=end of file or quit command 43 | *INPUT: void 44 | *OUTPUT:void 45 | *----------------------------------------------------------------------*/ 46 | 47 | void RBapp::mainLoop() 48 | { 49 | while(!cin.eof() && !done) 50 | processCommand(); 51 | } 52 | 53 | 54 | /*----------------------------------------------------------------------- 55 | *Description: This function accepts inpt and make strings operations 56 | and performs respective task 57 | *INPUT: void 58 | *OUTPUT: void 59 | *---------------------------------------------------------------------*/ 60 | void RBapp::processCommand() 61 | { 62 | string key_data,key,command,input; 63 | getline(cin, input); 64 | command = input.substr(0,input.find(" ")); 65 | if(command == "insert") 66 | { 67 | input.erase(0, input.find(" ") + 1); 68 | key_data=input.substr(0, input.find("\n")); 69 | processInsert(key_data); 70 | } 71 | else if(command == "print") 72 | { 73 | processPrint(); 74 | } 75 | else if(command == "find") 76 | { 77 | input.erase(0,input.find(" ") + 1); 78 | key=input.substr(0, input.find("\n")); 79 | processFind(key); 80 | } 81 | else if(command == "delete") 82 | { 83 | input.erase(0,input.find(" ") + 1); 84 | key_data=input.substr(0, input.find("\n")); 85 | processDelete(key_data); 86 | } 87 | else if(command == "quit") 88 | { 89 | processQuit(); 90 | } 91 | } 92 | 93 | /*----------------------------------------------------------------------- 94 | *Description: this function accepts key and data and pass it to 95 | rbInsert() function 96 | *INPUT: void 97 | *OUTPUT: void 98 | *---------------------------------------------------------------------*/ 99 | 100 | void RBapp::processInsert(string& key_data) 101 | { 102 | string data,key; 103 | key=key_data.substr(0, key_data.find(" ")); 104 | key_data.erase(0, key_data.find(" ") + 1); 105 | data=key_data.substr(0, key_data.find("\n")); 106 | myRBT.rbInsert(key, data); 107 | } 108 | 109 | /*----------------------------------------------------------------------- 110 | *Description:This function calls to rbprinttree() 111 | 112 | *INPUT: void 113 | *OUTPUT: void 114 | *----------------------------------------------------------------------*/ 115 | 116 | void RBapp::processPrint() 117 | { 118 | myRBT.rbPrintTree(); 119 | } 120 | 121 | /*----------------------------------------------------------------------- 122 | *Description: this function pass key to rbFind() function 123 | *INPUT:void 124 | *OUTPUT:void 125 | *----------------------------------------------------------------------*/ 126 | 127 | void RBapp::processFind(string& key) 128 | { 129 | vector printvector = myRBT.rbFind(key); 130 | for (unsigned int i=0; i < printvector.size(); i++) 131 | { 132 | cout << key <<" "<< *printvector[i] << endl; 133 | } 134 | } 135 | 136 | /*----------------------------------------------------------------------- 137 | *Description: this function passes key and data to rbdelete() 138 | *INPUT:void 139 | *OUTPUT:void 140 | *----------------------------------------------------------------------*/ 141 | 142 | void RBapp::processDelete(string& key_data) 143 | { 144 | string key,data; 145 | key=key_data.substr(0, key_data.find(" ")); 146 | key_data.erase(0, key_data.find(" ") + 1); 147 | data=key_data.substr(0, key_data.find("\n")); 148 | myRBT.rbDelete(key, data); 149 | } 150 | 151 | /*----------------------------------------------------------------------- 152 | *Description: this function quits execution 153 | *INPUT:void 154 | *OUTPUT:void 155 | *----------------------------------------------------------------------*/ 156 | 157 | void RBapp::processQuit() 158 | { 159 | done=true; 160 | } 161 | -------------------------------------------------------------------------------- /Red Black Tree/rbtree.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Nmae : Tejas patil chico st Id 006949762 3 | * @file rbtree.h Declaration of a red-black tree class. 4 | * 5 | * Note: The functions in this class should not do any I/O (except the 6 | * provided function rbPrintTree). 7 | * 8 | * @author Judy Challinger 9 | * @date 10/23/14 10 | */ 11 | 12 | #ifndef CSCI_311_RBTREE_H 13 | #define CSCI_311_RBTREE_H 14 | 15 | #include 16 | #include 17 | 18 | using std::string; 19 | using std::vector; 20 | 21 | class RBTree { 22 | 23 | public: 24 | 25 | RBTree(); 26 | ~RBTree(); 27 | 28 | void rbInsert(const string& key, const string& value); 29 | void rbDelete(const string& key, const string& value); 30 | vector rbFind(const string& key); 31 | void rbPrintTree(); 32 | 33 | private: 34 | 35 | // Node is a private nested class - used only by RBTree. 36 | class Node { 37 | public: 38 | Node(); 39 | Node(const string&, const string&, Node*); // Node* s/b a pointer to nil 40 | ~Node(); 41 | Node *parent; 42 | Node *left; 43 | Node *right; 44 | char color; 45 | string *key; 46 | string *value; 47 | }; 48 | 49 | Node *root; // pointer to the root of the red-black tree 50 | Node *nil; // pointer to the single nil node 51 | 52 | // private accessors - must all use nil 53 | Node* rbTreeMinimum(Node*); 54 | Node* rbTreeMaximum(Node*); 55 | Node* rbTreeSuccessor(Node*); 56 | Node* rbTreePredecessor(Node*); 57 | Node* rbTreeSearch(Node*, const string&); 58 | void reverseInOrderPrint(Node*, int); 59 | 60 | // private mutators 61 | void leftRotate(Node*); 62 | void rightRotate(Node*); 63 | void rbInsertFixup(Node*); 64 | void rbDeleteFixup(Node*); 65 | void rbTransplant(Node*, Node*); 66 | 67 | // private mutators overloaded from public interface 68 | void rbInsert(Node*); 69 | void rbDelete(Node*); 70 | 71 | // do not modify anything above this line! 72 | // you may add private functions below (although you shouldn't need to) 73 | void rbPostorderTreeWalk(Node*);//used for free memory 74 | }; 75 | 76 | #endif // CSCI_311_RBTREE_H 77 | -------------------------------------------------------------------------------- /Sorting- ALL/CensusData.h: -------------------------------------------------------------------------------- 1 | /**--------------------------------------------------------- 2 | * @file CensusData.h Declaration of the CensusData class. 3 | * 4 | * @author Judy Challinger 5 | * @date 2/22/13 6 | * @edited by Tejas Patil Chico State ID-006949762 7 | * @date 3/5/2015 8 | ----------------------------------------------------------*/ 9 | 10 | #ifndef CSCI_311_CENSUSDATA_H 11 | #define CSCI_311_CENSUSDATA_H 12 | 13 | #include 14 | using std::ifstream; 15 | using std::string; 16 | using std::vector; 17 | 18 | class CensusData { 19 | public: 20 | static const int POPULATION = 0; // type of sort 21 | static const int NAME = 1; 22 | ~CensusData(); 23 | void initialize(ifstream&); // reads in data 24 | int getSize(){return data.size();} 25 | void print(); // prints out data 26 | void insertionSort(int); // sorts data using insertionSort 27 | void mergeSort(int); // sorts data using mergeSort 28 | void quickSort(int); // sorts data using quickSort 29 | private: 30 | class Record { // declaration of a Record 31 | public: 32 | string* city; 33 | string* state; 34 | int population; 35 | Record(string&, string&, int); 36 | ~Record(); 37 | }; 38 | vector data; // data storage 39 | 40 | // private helper functions here 41 | 42 | void merge_Sort(int type,int p,int r); 43 | void Merge(int type,int p,int q,int r); 44 | int Partition(int type,int p,int r); 45 | void Randomized_quicksort(int type,int p,int r); 46 | int Randomized_partition(int type,int p,int r); 47 | int random(int p,int r); 48 | }; 49 | #endif // CSCI_311_CENSUSDATA_H 50 | -------------------------------------------------------------------------------- /Sorting- ALL/Patil-Tejas.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tpatil2/C-Programs/e7e8274470421f0bcb166318362df112e7df8db8/Sorting- ALL/Patil-Tejas.pdf -------------------------------------------------------------------------------- /Stacks and Queues/Stack_arrays.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | LIFO - Stack using Arrrays 3 | */ 4 | 5 | #include 6 | #define MAX_SIZE 101 7 | 8 | 9 | using namespace std; 10 | 11 | 12 | class stack{ 13 | 14 | private: 15 | int arr[MAX_SIZE]; 16 | int top; 17 | 18 | public: 19 | stack(){ 20 | top=-1; 21 | }//constructor 22 | 23 | void push(int x){ 24 | if(top == MAX_SIZE -1){ 25 | std::cout << "Stack overflow" << std::endl; 26 | } 27 | else{ 28 | top=top+1; 29 | arr[top]=x; 30 | } 31 | } 32 | 33 | int pop() 34 | { 35 | if(top == -1) { 36 | std::cout << "Error: Stack Underflow" << std::endl; 37 | return -1; 38 | } 39 | top--; 40 | return arr[top+1]; 41 | } 42 | 43 | bool isempty(){ 44 | if(top==-1) return true; 45 | return false; 46 | } 47 | 48 | 49 | void display(){ 50 | std::cout << "Stack is: "; 51 | if(top==-1){ 52 | std::cout << "EMPTY" << std::endl; 53 | } 54 | else { 55 | for (int i = 0; i <= top; i++) { 56 | std::cout << "\nelement at "< 7 | #include 8 | #define MAX 10 9 | 10 | using namespace std; 11 | 12 | 13 | 14 | class Queue{ 15 | 16 | private: 17 | int arr[MAX]; 18 | int front=-1, rear=-1; 19 | 20 | public: 21 | 22 | void Enqueue(int x){ 23 | 24 | if((rear+1)%MAX==front){ 25 | std::cout << "ERROR: Queue is FULL" << std::endl; 26 | } 27 | else{ 28 | if(front ==-1 && rear == -1){ 29 | front=0; 30 | rear=0; 31 | arr[rear]=x; 32 | }else{ 33 | rear=(rear+1)%MAX; 34 | arr[rear]=x; 35 | } 36 | } 37 | return; 38 | } 39 | 40 | 41 | void Dequeue(){ 42 | 43 | if(IsEmpty()){ std::cout << "Queue is Empty." << std::endl;} 44 | else if(front==rear){ 45 | front=-1; 46 | rear=-1; 47 | }else{ 48 | front=(front+1)%MAX; 49 | } 50 | } 51 | 52 | bool IsEmpty(){ 53 | if(front==-1 && rear==-1){ 54 | return true; 55 | } 56 | return false; 57 | } 58 | 59 | int Front(){ 60 | int start; 61 | if(front == -1) 62 | { 63 | cout<<"Error: cannot return front from empty queue\n"; 64 | return -1; 65 | }else{ 66 | 67 | start = arr[front]; 68 | return start; 69 | } 70 | } 71 | 72 | void print() { 73 | 74 | // Finding number of elements in queue 75 | int count = (rear+MAX-front)%MAX + 1; 76 | cout<<"\nQueue : "; 77 | for(int i = 0; i 5 | #include 6 | #define MAX 100 7 | 8 | using namespace std; 9 | 10 | 11 | 12 | class Queue{ 13 | 14 | private: 15 | int arr[MAX]; 16 | int front=-1, rear=-1; 17 | 18 | public: 19 | 20 | void Enqueue(int x){ 21 | 22 | if(rear > MAX){ 23 | std::cout << "ERROR: Queue is FULL" << std::endl; 24 | } 25 | else{ 26 | if(front ==-1 && rear == -1){ 27 | front++; 28 | rear++; 29 | arr[rear]=x; 30 | }else{ 31 | rear++; 32 | arr[rear]=x; 33 | } 34 | } 35 | return; 36 | } 37 | 38 | 39 | void Dequeue(){ 40 | 41 | if(IsEmpty()){ std::cout << "Queue is Empty." << std::endl;} 42 | else if(front==rear){ 43 | front=-1; 44 | rear=-1; 45 | }else{ 46 | front++; 47 | } 48 | } 49 | 50 | bool IsEmpty(){ 51 | if(front==-1 && rear==-1){ 52 | return true; 53 | } 54 | return false; 55 | } 56 | 57 | int Front(){ 58 | int start; 59 | start = arr[front]; 60 | return start; 61 | } 62 | 63 | void print() { 64 | 65 | std::cout << "Queue is " << std::endl; 66 | for(int i=front;i<=rear;i++){ 67 | std::cout << arr[i] << std::endl; 68 | } 69 | } 70 | 71 | }; 72 | 73 | 74 | int main(){ 75 | 76 | Queue q; 77 | 78 | for(int i=0;i<9;i++){ 79 | q.Enqueue(i); 80 | } 81 | 82 | q.print(); 83 | std::cout << "front is : "<< q.Front() << std::endl; 84 | 85 | for(int i=0;i<=10;i++){ 86 | q.Dequeue(); 87 | } 88 | 89 | q.print(); 90 | std::cout << "front is : "<< q.Front() << std::endl; 91 | 92 | 93 | return 0; 94 | } 95 | -------------------------------------------------------------------------------- /Stacks and Queues/queue_linkedlist.cpp: -------------------------------------------------------------------------------- 1 | //Implementation of queue using linked list 2 | 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main(){ 10 | 11 | 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Stacks and Queues/stack_linkedlist.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Stack using singly linked list 3 | */ 4 | 5 | 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | class node{ 12 | 13 | private: 14 | int data; 15 | node* next; 16 | node* top; 17 | 18 | public: 19 | 20 | node(){ 21 | top=NULL; 22 | next=NULL; 23 | } 24 | 25 | void push(int x){ 26 | node* temp = new node; 27 | temp->data=x; 28 | temp->next=top; 29 | top=temp; 30 | std::cout << "Pushed" << std::endl; 31 | } 32 | 33 | void pop(){ 34 | node* temp = new node; 35 | temp=top; 36 | top=top->next; 37 | temp->next=NULL; 38 | delete temp; 39 | std::cout << "popped" << std::endl; 40 | } 41 | 42 | void print(){ 43 | node* temp = new node; 44 | std::cout << "Stack is :" << std::endl; 45 | temp=top; 46 | while(temp!=NULL){ 47 | std::cout << temp->data << std::endl; 48 | temp=temp->next; 49 | } 50 | delete temp; 51 | } 52 | 53 | }; 54 | 55 | int main(){ 56 | 57 | node stack; 58 | 59 | for(int i=9;i>1;i--){ 60 | stack.push(i); 61 | } 62 | stack.pop(); 63 | stack.pop(); 64 | stack.pop(); 65 | stack.pop(); 66 | 67 | stack.print(); 68 | 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /Strings/Check_Unique_str.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /*Implement an algorithm to determine if a string has all unique 5 | characters.What if you can not use additional data structures? 6 | */ 7 | 8 | using namespace std; 9 | 10 | bool is_Unique(string str){ 11 | 12 | bool arr[256] = {false}; 13 | 14 | for (int i=0;i>str; 33 | 34 | if(is_Unique(str)){ 35 | std::cout << "String is unique" << std::endl; 36 | }else { 37 | std::cout << "String is not unique" << std::endl; 38 | } 39 | 40 | return 0; 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Strings/Longestsubstring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | bool Is_palindrome(string str){ 7 | 8 | int i=0; 9 | size_t j=str.length()-1; 10 | 11 | while(i0;i--){ 27 | for(int j=0;j+i<=str.length();j++){ 28 | //std::cout << str.substr(j,i) << std::endl; 29 | if(Is_palindrome(str.substr(j,i))){ 30 | std::cout << str.substr(j,i)<<" Is palindrome" << std::endl; 31 | } 32 | } 33 | } 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Strings/Tape_equilibrium.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A non-empty zero-indexed array A consisting of N integers is given. Array A represents numbers on a tape. 3 | Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1]. 4 | The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])| 5 | In other words, it is the absolute difference between the sum of the first part and the sum of the second part. 6 | For example, consider array A such that: 7 | A[0] = 3 8 | A[1] = 1 9 | A[2] = 2 10 | A[3] = 4 11 | A[4] = 3 12 | We can split this tape in four places: 13 | P = 1, difference = |3 − 10| = 7 14 | P = 2, difference = |4 − 9| = 5 15 | P = 3, difference = |6 − 7| = 1 16 | P = 4, difference = |10 − 3| = 7 17 | Write a function: 18 | int solution(vector &A); 19 | that, given a non-empty zero-indexed array A of N integers, returns the minimal difference that can be achieved. 20 | For example, given: 21 | A[0] = 3 22 | A[1] = 1 23 | A[2] = 2 24 | A[3] = 4 25 | A[4] = 3 26 | the function should return 1, as explained above. 27 | Assume that: 28 | N is an integer within the range [2..100,000]; 29 | each element of array A is an integer within the range [−1,000..1,000]. 30 | 31 | Complexity: 32 | expected worst-case time complexity is O(N); 33 | expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). 34 | 35 | Elements of input arrays can be modified. 36 | Copyright 2009–2016 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited. 37 | */// you can use includes, for example: 38 | 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | 47 | // you can write to stdout for debugging purposes, e.g. 48 | // cout << "this is a debug message" << endl; 49 | 50 | using namespace std; 51 | 52 | int solution(vector &A) { 53 | // write your code in C++11 (g++ 4.8.2) 54 | vector::iterator it; 55 | vector reverse_sum; 56 | unsigned int size; 57 | size=A.size(); 58 | reverse_sum.reserve(size-1); 59 | reverse_sum.push_back(*(A.end()-1)); 60 | long long sumA = *A.begin(); 61 | long long result = LLONG_MAX; 62 | int n=0; 63 | int i=0; 64 | 65 | for(it=A.end()-2;it!=A.begin();it--){ 66 | reverse_sum.push_back(reverse_sum[n] + *it); 67 | n++; 68 | }// ( 6,11,15,18,20,) 69 | 70 | while(n>-1){ 71 | if(i!=0){ 72 | sumA += *(A.begin()+i); 73 | } 74 | i++; 75 | result=min(result,abs(sumA - reverse_sum[n])); 76 | n--; 77 | } 78 | 79 | return result; 80 | } 81 | 82 | 83 | int main(){ 84 | 85 | vector A = {1,2,3,4,5,6}; 86 | 87 | std::cout << "val is " << solution(A)< 27 | #include 28 | #include 29 | 30 | using namespace std; 31 | 32 | int solution(int X, int Y, int D) { 33 | // write your code in C++11 (g++ 4.8.2) 34 | long long jump; 35 | jump = ceil(abs(double(X-Y))/D); 36 | 37 | return jump; 38 | } 39 | 40 | 41 | int main(){ 42 | 43 | std::cout << "Min number of jumps are : "<< solution(50,2222,30) << std::endl; 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Strings/increasing_sub.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | 8 | string str="abcdefghijklmnopqrstuvwxyz"; 9 | 10 | for(int i=1;i 2 | #include 3 | 4 | using namespace std; 5 | 6 | bool is_substring(string s1, string s2){ 7 | 8 | string str = s1+s2; 9 | 10 | std::cout << str << std::endl; 11 | 12 | 13 | 14 | 15 | return ; 16 | } 17 | 18 | int main(){ 19 | 20 | string s1="waterbottle"; 21 | string s2="erbottlewat"; 22 | 23 | if(is_substring(s1,s2)) std::cout << "yes" << std::endl; 24 | else std::cout << "No" << std::endl; 25 | 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Strings/missing_num_bit.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A zero-indexed array A consisting of N different integers is given. The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing. 3 | Your goal is to find that missing element. 4 | Write a function: 5 | int solution(vector &A); 6 | that, given a zero-indexed array A, returns the value of the missing element. 7 | For example, given array A such that: 8 | A[0] = 2 9 | A[1] = 3 10 | A[2] = 1 11 | A[3] = 5 12 | the function should return 4, as it is the missing element. 13 | Assume that: 14 | N is an integer within the range [0..100,000]; 15 | the elements of A are all distinct; 16 | each element of array A is an integer within the range [1..(N + 1)]. 17 | Complexity: 18 | expected worst-case time complexity is O(N); 19 | expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments). 20 | Elements of input arrays can be modified. 21 | Copyright 2009–2016 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited. 22 | */ 23 | #include 24 | #include 25 | 26 | using namespace std; 27 | 28 | int solution(vector &A) { 29 | // write your code in C++11 (g++ 4.8.2) 30 | unsigned int n = A.size()+1; 31 | int x = 0; 32 | 33 | if(n!=1){ 34 | for(unsigned int i=1;i<=n;i++){ 35 | x = x ^ i ^ A[i-1]; 36 | } 37 | } else x=1; 38 | 39 | return x; 40 | } 41 | 42 | int main(){ 43 | 44 | vector A = {1,2,3,5,6,7,8}; 45 | 46 | std::cout << "Missing number is "<< solution(A) << std::endl; 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Strings/missingrange.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define LIMIT 100 4 | 5 | using namespace std; 6 | 7 | string missing_num(int arr[], int size){ 8 | bool present[LIMIT]={false}; 9 | 10 | for(int i=0;i &A); 6 | that, given a zero-indexed array A, returns the value of the missing element. 7 | For example, given array A such that: 8 | A[0] = 2 9 | A[1] = 3 10 | A[2] = 1 11 | A[3] = 5 12 | the function should return 4, as it is the missing element. 13 | Assume that: 14 | N is an integer within the range [0..100,000]; 15 | the elements of A are all distinct; 16 | each element of array A is an integer within the range [1..(N + 1)]. 17 | Complexity: 18 | expected worst-case time complexity is O(N); 19 | expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments). 20 | Elements of input arrays can be modified. 21 | Copyright 2009–2016 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited. 22 | */ 23 | #include 24 | #include 25 | 26 | using namespace std; 27 | 28 | int solution(vector &A) { 29 | // write your code in C++11 (g++ 4.8.2) 30 | unsigned int n = A.size()+1; 31 | long long sum = n*(n+1)/2; 32 | 33 | for(auto it=A.begin();it!=A.end();it++){ 34 | sum -= *it; 35 | } 36 | 37 | return sum; 38 | } 39 | 40 | int main(){ 41 | 42 | vector A = {1,2,3,4,5,6,7,8}; 43 | 44 | std::cout << "Missing number is "<< solution(A) << std::endl; 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Strings/remove_duplicate.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Design an algorithm and write code to remove the duplicate characters 3 | in a string without using any additional buffer. NOTE: One or two 4 | additional variables are fine. An extra copy of the array is no 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | 13 | char *remove_duplicates(char *str) 14 | { 15 | int i=1; int j=1; 16 | 17 | while(*(str+j)){ 18 | if(*(str+j) != *(str+j-1)){ 19 | *(str+i)=*(str+j); 20 | i++; 21 | } 22 | j++; 23 | } 24 | 25 | *(str+i)='\0'; 26 | 27 | return str; 28 | 29 | } 30 | 31 | 32 | int main(){ 33 | 34 | char name[]="heello"; 35 | int n = strlen(name); 36 | 37 | sort(name,name+n); 38 | 39 | std::cout << "Sorted string is :"<< name << std::endl; 40 | char *opt=remove_duplicates(name); 41 | std::cout << "String after removing deplicate: "<< opt << std::endl; 42 | 43 | 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Strings/remove_spaces.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | remove wite spaces in sting 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | 9 | using namespace std; 10 | 11 | 12 | int main(){ 13 | 14 | string str,line; 15 | 16 | ifstream file("myfile.txt"); 17 | 18 | while (!file.eof()) { 19 | getline(file,line); 20 | str+=line; 21 | } 22 | 23 | std::cout << "Stirng is : "<< str << std::endl; 24 | 25 | for(int i=0;i 8 | #include 9 | 10 | 11 | using namespace std; 12 | 13 | void rev_string(char* str){ 14 | 15 | char* end = str; 16 | 17 | if(str){ 18 | while(*end){ 19 | ++end; 20 | } 21 | --end;//set to last char which is exactly befor NULL char 22 | } 23 | 24 | if(str < end){ 25 | swap(*str,*end); 26 | str++; 27 | end--; 28 | } 29 | 30 | return; 31 | } 32 | 33 | 34 | void swap(char* start, char* end){ 35 | 36 | char temp; 37 | temp=*start; 38 | *start=*end; 39 | *end=temp; 40 | 41 | return; 42 | 43 | } 44 | 45 | 46 | int main(){ 47 | 48 | char name[]="Tejas"; 49 | 50 | rev_string(name); 51 | 52 | std::cout << "Reverse of C-string Tejas is : "<< name << std::endl; 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Strings/string_compression.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Implement a method to perform basic string compression using the counts 3 | of repeated characters. For example, the string aabcccccaaa would become 4 | a2blc5a3. If the "compressed" string would not become smaller than the original 5 | string, your method should return the original string. 6 | */ 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | 14 | string compress_string(string const &str){ 15 | 16 | string buffer{}; 17 | char set=str[0]; 18 | int count = 1; 19 | 20 | for(int i=1;i<=str.size();i++){ 21 | 22 | if(i != str.size() && str[i]==set){ 23 | count++; 24 | } 25 | else{ 26 | buffer+=set; 27 | buffer.append(to_string(count)); 28 | set=str[i]; 29 | count=1; 30 | 31 | } 32 | } 33 | 34 | if(str.size() < buffer.size()){ 35 | return str; 36 | } 37 | else { 38 | return buffer; 39 | } 40 | 41 | } 42 | 43 | 44 | int main(){ 45 | string str = "aabcccccaaa"; 46 | 47 | cout<<"output is: "< 8 | #include 9 | 10 | using namespace std; 11 | 12 | 13 | bool check_permutations(char* str1, char* str2){ 14 | 15 | bool arr[256]={false}; 16 | 17 | while(*str1){ 18 | 19 | int val=*str1; 20 | arr[val]=true; 21 | str1++; 22 | } 23 | 24 | while(*str2){ 25 | int val=*str2; 26 | if(arr[val]==true){str2++;} 27 | else{return false;} 28 | } 29 | 30 | return true; 31 | } 32 | 33 | 34 | 35 | int main(){ 36 | 37 | char str1[]="tejas patil"; 38 | char str2[]="patil"; 39 | 40 | bool flag=check_permutations(str1,str2); 41 | 42 | if(flag){ 43 | std::cout << "Stirngs are permutations of each other" << std::endl; 44 | } 45 | else{ 46 | std::cout << "Stirngs are not permutations of each other" << std::endl; 47 | } 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Strings/string_permutations2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given two strings, 3 | write a method to decide if one is a permutation of the other 4 | NOTE: This code dose works for repeated char but not case sensative 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | 13 | bool check_permutations(char* str1, char* str2){ 14 | 15 | if(strlen(str1)!=strlen(str2)){ 16 | return false; 17 | } 18 | 19 | if(0!=strcmp(str1,str2)){ 20 | return false; 21 | } 22 | 23 | return true; 24 | } 25 | 26 | 27 | 28 | int main(){ 29 | 30 | char str1[]="tejas vamanrao patil"; 31 | char str2[]="patil tejas vamanrao"; 32 | 33 | sort(str1,str1+strlen(str1)); 34 | sort(str2,str2+strlen(str2)); 35 | 36 | bool flag=check_permutations(str1,str2); 37 | 38 | if(flag){ 39 | std::cout << "Stirngs are permutations of each other" << std::endl; 40 | } 41 | else{ 42 | std::cout << "Stirngs are not permutations of each other" << std::endl; 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Strings/string_replacement.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a method to replace all spaces in a string with'%20'. You may assume 3 | that the string has sufficient space at the end of the string to hold the 4 | additional characters, and that you are given the "true" length of the 5 | string. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | 13 | 14 | using namespace std; 15 | 16 | void replace_spaces(char* str1, int length){ 17 | 18 | int spaces=0; 19 | int newlength; 20 | 21 | for(int i=0 ; i=0;i--){ 30 | if(str1[i]==' '){ 31 | str1[--newlength]='%'; 32 | str1[--newlength]='0'; 33 | str1[--newlength]='2'; 34 | }else{ 35 | str1[--newlength]=str1[i]; 36 | } 37 | } 38 | 39 | } 40 | 41 | int main(){ 42 | 43 | char str1[]="Mr John Smith"; 44 | 45 | int length = strlen(str1); 46 | 47 | replace_spaces(str1,length); 48 | 49 | std::cout << "Output is : "<=0;i--){ 14 | if(str1[i]==' '){ 15 | 16 | str1[newlength-1]='0'; 17 | str1[newlength-2]='2'; 18 | str1[newlength-3]='%'; 19 | newlength=newlength-3; 20 | 21 | } 22 | else{ 23 | str1[newlength-1]=str1[i]; 24 | newlength=newlength-1; 25 | } 26 | 27 | } 28 | 29 | return; 30 | -------------------------------------------------------------------------------- /Trees/BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | using namespace std; 8 | 9 | 10 | class node{ 11 | public: 12 | node* left; 13 | node* right; 14 | int data; 15 | }; 16 | 17 | node* root =NULL;// initilize empty tree 18 | 19 | node* getNewnode(int newdata){ 20 | 21 | node* newNode = new node; 22 | newNode->data=newdata; 23 | newNode->left=NULL; 24 | newNode->right=NULL; 25 | 26 | return newNode; 27 | 28 | } 29 | 30 | void print(node* curr){ 31 | 32 | if(curr!=NULL){ 33 | print(curr->left); 34 | std::cout << curr->data<<" "; 35 | print(curr->right); 36 | 37 | } 38 | 39 | return; 40 | } 41 | 42 | bool search(node* curr,int data){ 43 | 44 | if(curr==NULL) return false; 45 | else if(data <= curr->data) return search(curr->left,data); 46 | else if(data > curr->data) return search(curr->right,data); 47 | else if(curr->data == data) return true; 48 | 49 | } 50 | 51 | node* insertNode(node* curr, int newdata){ 52 | 53 | if(curr==NULL){ 54 | curr=getNewnode(newdata); 55 | }else if(newdata <= curr->data){ 56 | curr->left=(insertNode(curr->left,newdata)); 57 | }else if(newdata > curr->data){ 58 | curr->right=(insertNode(curr->right,newdata)); 59 | } 60 | return curr; 61 | 62 | } 63 | 64 | //returns min of right sub-tree 65 | node* findmin(node* curr){ 66 | 67 | while(curr->left != NULL) curr=curr->left; 68 | 69 | return curr; 70 | } 71 | 72 | 73 | //returns max of right sub-tree 74 | node* findmax(node* curr){ 75 | 76 | while(curr->right != NULL) curr=curr->right; 77 | 78 | return curr; 79 | } 80 | 81 | node* deleteNode(node* curr, int data){ 82 | 83 | if(curr==NULL) return curr; 84 | else if( data < curr->data) curr->left=deleteNode(curr->left,data); 85 | else if( data > curr->data) curr->right=deleteNode(curr->right,data); 86 | else { 87 | //case 1 both childs are NULL 88 | if(curr->left ==NULL && curr->right == NULL){ 89 | delete curr; 90 | curr=NULL; 91 | }//case 2 only one child 92 | else if(curr->left == NULL){ 93 | node* temp = curr; 94 | curr=curr->right; 95 | delete temp; 96 | } 97 | else if (curr->right == NULL) { 98 | node* temp = curr; 99 | curr=curr->left; 100 | delete temp; 101 | }//case 3 node has both childs 102 | else if(curr->left != NULL && curr->right != NULL ){ 103 | 104 | node* temp = findmin(curr->right); 105 | curr->data=temp->data; 106 | curr->right = deleteNode(curr->right, temp->data); 107 | 108 | } 109 | } 110 | 111 | return curr; 112 | } 113 | 114 | /* 115 | node* successor(node* curr){ 116 | node* succ = new node; 117 | if(curr->right != NULL) return findmin(curr->right); 118 | succ = curr-> parent; 119 | while( suuc != NULL && curr = succ->right){ 120 | curr=succ; 121 | succ=succ->parent; 122 | } 123 | return succ; 124 | } 125 | 126 | node* predecessor(node* curr){ 127 | node* pred = new node; 128 | if(curr->left != NULL) return findmax(curr->left); 129 | pred = curr-> parent; 130 | while( pred != NULL && curr = pred->left){ 131 | curr=pred; 132 | pred=pred->parent; 133 | } 134 | return pred; 135 | } 136 | */ 137 | 138 | 139 | void print_BFS(node* curr){ 140 | 141 | std::cout << "Inside BFS" << std::endl; 142 | // print(curr); 143 | std::cout <<"\n"<< std::endl; 144 | 145 | if(curr == NULL) return; 146 | queue discovered; 147 | discovered.push(curr); 148 | 149 | while(!discovered.empty()){ 150 | 151 | node * temp = discovered.front(); 152 | discovered.pop(); 153 | if(temp != NULL) std::cout << temp->data <<" " << std::endl; 154 | if(temp->left!=NULL) discovered.push(temp->left); 155 | if(temp->right!=NULL) discovered.push(temp->right); 156 | 157 | } 158 | 159 | } 160 | 161 | node* common_ancestor(node* curr, int a, int b){ 162 | 163 | if(curr->data < a && curr->data right, a,b); 165 | } 166 | else if(curr->data > a && curr->data > b){ 167 | curr = common_ancestor(curr->left, a,b); 168 | }else 169 | return curr; 170 | } 171 | 172 | bool IsBST(node* curr, int max, int min){ 173 | 174 | if(curr == NULL) return true; 175 | 176 | if( curr->data < max && curr->data > min 177 | && IsBST(curr->left,curr->data, min) 178 | && IsBST(curr->right,max,curr->data)) 179 | return true; 180 | else return false; 181 | } 182 | 183 | bool BST_check(node* root){ 184 | return IsBST(root, INT_MAX, INT_MIN); 185 | } 186 | 187 | 188 | bool Is_balanced(node* curr){ 189 | if(curr== NULL) retun true; 190 | } 191 | 192 | int main(){ 193 | 194 | root = insertNode(root,15); 195 | root = insertNode(root,11); 196 | root = insertNode(root,18); 197 | root = insertNode(root,10); 198 | root = insertNode(root,16); 199 | root = insertNode(root,12); 200 | root = insertNode(root,17); 201 | //root = insertNode(root,17); 202 | 203 | 204 | std::cout << "Inorder Tree" << std::endl; 205 | print(root); 206 | 207 | if(search(root,10)) std::cout << "Element found" << std::endl; 208 | else std::cout << "\nNot found" << std::endl; 209 | 210 | 211 | std::cout << "\nBFS" << std::endl; 212 | print_BFS(root); 213 | //root = deleteNode(root,18); 214 | 215 | std::cout << "After deleting" << std::endl; 216 | print(root); 217 | 218 | std::cout << "\nBFS" << std::endl; 219 | print_BFS(root); 220 | std::cout << "in commin " << std::endl; 221 | node* temp = common_ancestor(root,17,17); 222 | std::cout << "common_ancestor is : "<data << std::endl; 223 | 224 | if(BST_check(root)) std::cout << "YES IT Is BST" << std::endl; 225 | else std::cout << "IT IS NOT BST" << std::endl; 226 | return 0; 227 | 228 | } 229 | -------------------------------------------------------------------------------- /Trees/Succ_Pred.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | class node{ 8 | 9 | public: 10 | 11 | int data; 12 | node* left; 13 | node* right; 14 | node* parent; 15 | }; 16 | 17 | node* root = NULL; 18 | 19 | node* getNewnode(int data){ 20 | 21 | node* newNode = new node; 22 | newNode->data = data; 23 | newNode->left = NULL; 24 | newNode->right= NULL; 25 | 26 | return newNode; 27 | } 28 | 29 | void print(node* curr){ 30 | 31 | if(curr!=NULL){ 32 | std::cout << curr->data <<" " << std::endl; 33 | print(curr->left); 34 | print(curr->right); 35 | } 36 | } 37 | 38 | void print_BFS(node* curr){ 39 | 40 | std::cout << "Inside BFS" << std::endl; 41 | // print(curr); 42 | std::cout <<"\n"<< std::endl; 43 | 44 | if(curr == NULL) return; 45 | queue discovered; 46 | discovered.push(curr); 47 | 48 | while(!discovered.empty()){ 49 | 50 | node * temp = discovered.front(); 51 | discovered.pop(); 52 | if(temp != NULL) std::cout << temp->data <<" " << std::endl; 53 | if(temp->left!=NULL) discovered.push(temp->left); 54 | if(temp->right!=NULL) discovered.push(temp->right); 55 | 56 | } 57 | 58 | } 59 | 60 | 61 | node* insertNode(node* curr, int data){ 62 | 63 | if(curr==NULL){ 64 | curr=getNewnode(data); 65 | } 66 | else if( data <= curr->data ){ 67 | curr->left=insertNode(curr->left , data); 68 | curr->left->parent=curr; 69 | 70 | }else if( data > curr->data ){ 71 | curr->right=insertNode(curr->right , data); 72 | curr->right->parent=curr; 73 | } 74 | 75 | return curr; 76 | } 77 | 78 | node* findmin(node* curr){ 79 | 80 | while(curr->left != NULL) curr=curr->left; 81 | 82 | return curr; 83 | } 84 | 85 | 86 | //returns max of right sub-tree 87 | node* findmax(node* curr){ 88 | 89 | while(curr->right != NULL) curr=curr->right; 90 | 91 | return curr; 92 | } 93 | 94 | 95 | node* successor(node* curr){ 96 | node* succ = new node; 97 | 98 | //case1: If node has right subtree 99 | if(curr->right != NULL) return findmin(curr->right); 100 | //case 2: if node is left child then its parent is cucessor 101 | succ = curr-> parent; 102 | // case 3:if node has right child 103 | while( succ != NULL && curr == succ->right){ 104 | curr=succ; 105 | succ=succ->parent; 106 | } 107 | return succ; 108 | } 109 | 110 | node* predecessor(node* curr){ 111 | node* pred = new node; 112 | // case 1 : if node has left subtree 113 | if(curr->left != NULL) return findmax(curr->left); 114 | //case 2 : if node is right child then its parent is predecessor 115 | pred = curr-> parent; 116 | // case 3 : if node is left child theb go upwards untill you find correct parent 117 | while( pred != NULL && curr == pred->left){ 118 | curr=pred; 119 | pred=pred->parent; 120 | } 121 | return pred; 122 | } 123 | 124 | void level_oreder_print(node* curr){ 125 | 126 | std::cout << "Level order Print" << std::endl; 127 | if(curr==NULL) return; 128 | 129 | queue q1; 130 | queue q2; 131 | q1.push(curr); 132 | 133 | while(!q1.empty() || !q2.empty()){ 134 | 135 | while(!q1.empty()){ 136 | node *temp1 = q1.front(); 137 | q1.pop(); 138 | std::cout << temp1->data<< " "; 139 | if(temp1->left != NULL) q2.push(temp1->left); 140 | if(temp1->right != NULL) q2.push(temp1->right); 141 | } 142 | std::cout<< std::endl; 143 | while(!q2.empty()){ 144 | node *temp2 = q2.front(); 145 | q2.pop(); 146 | std::cout << temp2->data<< " "; 147 | if(temp2->left != NULL) q1.push(temp2->left); 148 | if(temp2->right != NULL) q1.push(temp2->right); 149 | } 150 | std::cout<< std::endl; 151 | 152 | } 153 | 154 | return; 155 | } 156 | 157 | int main(){ 158 | 159 | root = insertNode(root, 45); 160 | root = insertNode(root, 35); 161 | root = insertNode(root, 33); 162 | root = insertNode(root, 56); 163 | root = insertNode(root, 60); 164 | root = insertNode(root, 49); 165 | 166 | print(root); 167 | 168 | print_BFS(root); 169 | 170 | level_oreder_print(root); 171 | 172 | node* temp=predecessor(root->left->left); 173 | if(temp!=NULL) std::cout << "succer of "<left->left->data<< " is "<< temp->data << std::endl; 174 | else std::cout << "No successor" << std::endl; 175 | 176 | return 0; 177 | } 178 | -------------------------------------------------------------------------------- /Trees/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tpatil2/C-Programs/e7e8274470421f0bcb166318362df112e7df8db8/Trees/a.out -------------------------------------------------------------------------------- /Trees/arry2BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class node{ 7 | 8 | public: 9 | 10 | int data; 11 | node* left; 12 | node* right; 13 | 14 | }; 15 | 16 | node* root=NULL; 17 | 18 | node* getNewNode(int data){ 19 | 20 | node* newnode = new node; 21 | newnode->data = data; 22 | newnode->right = NULL; 23 | newnode->left = NULL; 24 | 25 | return newnode; 26 | } 27 | 28 | void print_inorder(node* curr){ 29 | 30 | if(curr != NULL){ 31 | std::cout << curr->data<<" " << std::endl; 32 | print_inorder(curr->left); 33 | print_inorder(curr->right); 34 | } 35 | 36 | return; 37 | 38 | } 39 | 40 | void print_BFS(node* curr){ 41 | 42 | std::cout << "Inside BFS" << std::endl; 43 | // print(curr); 44 | std::cout <<"\n"<< std::endl; 45 | 46 | if(curr == NULL) return; 47 | queue discovered; 48 | discovered.push(curr); 49 | 50 | while(!discovered.empty()){ 51 | 52 | node * temp = discovered.front(); 53 | discovered.pop(); 54 | if(temp != NULL) std::cout << temp->data <<" " << std::endl; 55 | if(temp->left!=NULL) discovered.push(temp->left); 56 | if(temp->right!=NULL) discovered.push(temp->right); 57 | 58 | } 59 | 60 | } 61 | 62 | 63 | void level_oreder_print(node* curr){ 64 | 65 | std::cout << "Level order Print" << std::endl; 66 | if(curr==NULL) return; 67 | 68 | queue q1; 69 | queue q2; 70 | q1.push(curr); 71 | 72 | while(!q1.empty() || !q2.empty()){ 73 | 74 | while(!q1.empty()){ 75 | node *temp1 = q1.front(); 76 | q1.pop(); 77 | std::cout << temp1->data<< " "; 78 | if(temp1->left != NULL) q2.push(temp1->left); 79 | if(temp1->right != NULL) q2.push(temp1->right); 80 | } 81 | std::cout<< std::endl; 82 | while(!q2.empty()){ 83 | node *temp2 = q2.front(); 84 | q2.pop(); 85 | std::cout << temp2->data<< " "; 86 | if(temp2->left != NULL) q1.push(temp2->left); 87 | if(temp2->right != NULL) q1.push(temp2->right); 88 | } 89 | std::cout<< std::endl; 90 | 91 | } 92 | 93 | return; 94 | } 95 | 96 | node* insert_node(int arr[], int start, int end){ 97 | 98 | std::cout << "start "< end) 101 | return NULL; 102 | 103 | int mid = (start+end)/2; 104 | node* curr = new node; 105 | curr = getNewNode(arr[mid]); 106 | curr->left=insert_node(arr, start,mid-1); 107 | curr->right=insert_node(arr,mid+1,end); 108 | 109 | return curr; 110 | } 111 | 112 | 113 | int main(){ 114 | 115 | int arr[]={1,2,3,4,5,6,7,8,9,10}; 116 | 117 | int size = sizeof(arr)/sizeof(int); 118 | root = insert_node(arr,0,size-1); 119 | print_inorder(root); 120 | print_BFS(root); 121 | level_oreder_print(root); 122 | 123 | return 0; 124 | 125 | } 126 | -------------------------------------------------------------------------------- /Trees/balance_BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | 7 | class node{ 8 | public: 9 | 10 | int data; 11 | node* left; 12 | node* right; 13 | 14 | }; 15 | 16 | node* root = NULL; 17 | 18 | node* getNewnode(int data){ 19 | node* temp= new node; 20 | temp->data = data; 21 | temp->left=NULL; 22 | temp->right=NULL; 23 | 24 | return temp; 25 | 26 | } 27 | 28 | 29 | 30 | node* insertNode(node* curr, int data){ 31 | 32 | if(curr==NULL){ 33 | curr= getNewnode(data); 34 | return curr; 35 | } 36 | if(data < curr->data) curr->left=insertNode(curr->left,data); 37 | else curr->right=insertNode(curr->right,data); 38 | 39 | return curr; 40 | } 41 | 42 | 43 | int Is_balanced(node* curr){ 44 | 45 | if(curr==NULL) return 0; 46 | 47 | int lh = Is_balanced(curr->left); 48 | int rh = Is_balanced(curr->right); 49 | 50 | if(lh==-1 || rh==-1) return -1; 51 | if(abs(lh-rh) > 1) return -1; 52 | 53 | if(lh>rh) return lh+1; 54 | else return rh+1; 55 | 56 | } 57 | 58 | 59 | 60 | 61 | bool balanced_utility(node* curr){ 62 | 63 | if(Is_balanced(curr) > -1) return true; 64 | 65 | else return false; 66 | 67 | } 68 | 69 | 70 | 71 | void level_oreder_print(node* curr){ 72 | 73 | std::cout << "Level order Print" << std::endl; 74 | if(curr==NULL) return; 75 | 76 | queue q1; 77 | queue q2; 78 | q1.push(curr); 79 | 80 | while(!q1.empty() || !q2.empty()){ 81 | 82 | while(!q1.empty()){ 83 | node *temp1 = q1.front(); 84 | q1.pop(); 85 | std::cout << temp1->data<< " "; 86 | if(temp1->left != NULL) q2.push(temp1->left); 87 | if(temp1->right != NULL) q2.push(temp1->right); 88 | } 89 | std::cout<< std::endl; 90 | while(!q2.empty()){ 91 | node *temp2 = q2.front(); 92 | q2.pop(); 93 | std::cout << temp2->data<< " "; 94 | if(temp2->left != NULL) q1.push(temp2->left); 95 | if(temp2->right != NULL) q1.push(temp2->right); 96 | } 97 | std::cout<< std::endl; 98 | 99 | } 100 | 101 | return; 102 | } 103 | 104 | int main(){ 105 | 106 | root = insertNode(root,10); 107 | root = insertNode(root,15); 108 | root = insertNode(root,5); 109 | root = insertNode(root,4); 110 | root = insertNode(root,13); 111 | root = insertNode(root,34); 112 | root = insertNode(root,33); 113 | root = insertNode(root,6); 114 | root = insertNode(root,6); 115 | 116 | 117 | level_oreder_print(root); 118 | if(balanced_utility(root)) std::cout << "Balanced" << std::endl; 119 | else std::cout << "Not balanced" << std::endl; 120 | return 0; 121 | } 122 | -------------------------------------------------------------------------------- /Trees/common_ancestor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | 6 | class node{ 7 | public: 8 | 9 | int data; 10 | node* left; 11 | node* right; 12 | 13 | }; 14 | 15 | node* root = NULL; 16 | 17 | node* getNewnode(int data){ 18 | node* temp= new node; 19 | temp->data = data; 20 | temp->left=NULL; 21 | temp->right=NULL; 22 | 23 | return temp; 24 | 25 | } 26 | 27 | 28 | void print(node* curr){ 29 | 30 | if(curr!=NULL){ 31 | print(curr->left); 32 | std::cout << curr->data << std::endl; 33 | print(curr->right); 34 | } 35 | 36 | return; 37 | } 38 | 39 | 40 | node* common_ancestor(node* root, int n1, int n2){ 41 | 42 | if(root == NULL){ 43 | return NULL; 44 | } 45 | if(root == n1 || root == n2){ 46 | return root; 47 | } 48 | 49 | Node* left = lca(root.left, n1, n2); 50 | Node* right = lca(root.right, n1, n2); 51 | 52 | if(left != NULL && right != NULL){ 53 | return root; 54 | } 55 | return left != NULL ? left : right; 56 | } 57 | 58 | 59 | 60 | node* insertNode(node* curr, int data){ 61 | 62 | if(curr==NULL){ 63 | curr= getNewnode(data); 64 | return curr; 65 | } 66 | if(data < curr->data) curr->left=insertNode(curr->left,data); 67 | else curr->right=insertNode(curr->right,data); 68 | 69 | return curr; 70 | } 71 | 72 | 73 | int main(){ 74 | 75 | root = insertNode(root,10); 76 | root = insertNode(root,15); 77 | root = insertNode(root,5); 78 | root = insertNode(root,4); 79 | root = insertNode(root,13); 80 | root = insertNode(root,34); 81 | root = insertNode(root,33); 82 | root = insertNode(root,6); 83 | 84 | 85 | print(root); 86 | 87 | node* temp = common_ancestor(root,34,32); 88 | 89 | std::cout << "common_ancestor is "<< temp->data << std::endl; 90 | 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /Trees/sum_path.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | 8 | class node{ 9 | public: 10 | node* left; 11 | node* right; 12 | int data; 13 | }; 14 | 15 | node* root =NULL;// initilize empty tree 16 | 17 | node* getNewnode(int newdata){ 18 | 19 | node* newNode = new node; 20 | newNode->data=newdata; 21 | newNode->left=NULL; 22 | newNode->right=NULL; 23 | 24 | return newNode; 25 | 26 | } 27 | 28 | void print(node* curr){ 29 | 30 | if(curr!=NULL){ 31 | print(curr->left); 32 | std::cout << curr->data<<" "; 33 | print(curr->right); 34 | 35 | } 36 | 37 | return; 38 | } 39 | 40 | 41 | 42 | void level_oreder_print(node* curr){ 43 | 44 | std::cout << "\nLevel order Print" << std::endl; 45 | if(curr==NULL) return; 46 | 47 | queue q1; 48 | queue q2; 49 | q1.push(curr); 50 | 51 | while(!q1.empty() || !q2.empty()){ 52 | 53 | while(!q1.empty()){ 54 | node *temp1 = q1.front(); 55 | q1.pop(); 56 | std::cout << temp1->data<< " "; 57 | if(temp1->left != NULL) q2.push(temp1->left); 58 | if(temp1->right != NULL) q2.push(temp1->right); 59 | } 60 | std::cout<< std::endl; 61 | while(!q2.empty()){ 62 | node *temp2 = q2.front(); 63 | q2.pop(); 64 | std::cout << temp2->data<< " "; 65 | if(temp2->left != NULL) q1.push(temp2->left); 66 | if(temp2->right != NULL) q1.push(temp2->right); 67 | } 68 | std::cout<< std::endl; 69 | 70 | } 71 | 72 | return; 73 | } 74 | 75 | node* insertNode(node* curr, int newdata){ 76 | 77 | if(curr==NULL){ 78 | curr=getNewnode(newdata); 79 | }else if(newdata <= curr->data){ 80 | curr->left=(insertNode(curr->left,newdata)); 81 | }else if(newdata > curr->data){ 82 | curr->right=(insertNode(curr->right,newdata)); 83 | } 84 | return curr; 85 | 86 | } 87 | 88 | 89 | 90 | //root to leaf only 91 | bool path_sum(node* curr, int sum){ 92 | 93 | if(curr==NULL){ 94 | return (sum==0); 95 | } 96 | if(path_sum(curr->left,sum-curr->data)){ 97 | return true; 98 | } 99 | if(path_sum(curr->right,sum-curr->data)){ 100 | return true; 101 | } 102 | 103 | return false; 104 | } 105 | 106 | 107 | int main(){ 108 | 109 | 110 | root = insertNode(root, 45); 111 | root = insertNode(root, 35); 112 | root = insertNode(root, 33); 113 | root = insertNode(root, 56); 114 | root = insertNode(root, 60); 115 | root = insertNode(root, 49); 116 | 117 | print(root); 118 | level_oreder_print(root); 119 | if(path_sum(root,161)) std::cout << "path exists" << std::endl; 120 | 121 | return 0; 122 | } 123 | -------------------------------------------------------------------------------- /Trees/sum_tree_4'9.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | # include 3 | using namespace std; 4 | 5 | class node{ 6 | public: 7 | int data; 8 | node* left; 9 | node* right; 10 | }; 11 | 12 | node* root = NULL; 13 | 14 | node* getNewnode(int data){ 15 | 16 | node* newNode= new node; 17 | newNode->data=data; 18 | newNode->left=NULL; 19 | newNode->right=NULL; 20 | 21 | return newNode; 22 | } 23 | 24 | void print(node* curr){ 25 | 26 | if(curr == NULL) return; 27 | print(curr->left); 28 | std::cout <data << " "; 29 | print(curr->right); 30 | 31 | } 32 | 33 | node* insertNode(node* curr,int newdata){ 34 | 35 | if(curr == NULL) return getNewnode(newdata); 36 | else if(curr->data > newdata){ 37 | curr->left=insertNode(curr->left,newdata); 38 | } 39 | else if(curr->data <= newdata){ 40 | curr->right=insertNode(curr->right,newdata); 41 | } 42 | 43 | return curr; 44 | } 45 | void level_oreder_print(node* curr){ 46 | 47 | std::cout << "Level order Print" << std::endl; 48 | if(curr==NULL) return; 49 | 50 | queue q1; 51 | queue q2; 52 | q1.push(curr); 53 | 54 | while(!q1.empty() || !q2.empty()){ 55 | 56 | while(!q1.empty()){ 57 | node *temp1 = q1.front(); 58 | q1.pop(); 59 | std::cout << temp1->data<< " "; 60 | if(temp1->left != NULL) q2.push(temp1->left); 61 | if(temp1->right != NULL) q2.push(temp1->right); 62 | } 63 | std::cout<< std::endl; 64 | while(!q2.empty()){ 65 | node *temp2 = q2.front(); 66 | q2.pop(); 67 | std::cout << temp2->data<< " "; 68 | if(temp2->left != NULL) q1.push(temp2->left); 69 | if(temp2->right != NULL) q1.push(temp2->right); 70 | } 71 | std::cout<< std::endl; 72 | 73 | } 74 | 75 | return; 76 | } 77 | 78 | bool path_sum(node* curr, int sum){ 79 | 80 | if(curr==NULL){ 81 | return (sum==0);//true or false 82 | } 83 | if(path_sum(curr->left, sum-curr->data)){ 84 | return true; 85 | } 86 | if(path_sum(curr->right,sum-curr->data)){ 87 | return true; 88 | } 89 | return false; 90 | } 91 | 92 | 93 | 94 | 95 | int main(){ 96 | 97 | 98 | root = insertNode(root,15); 99 | root = insertNode(root,11); 100 | root = insertNode(root,18); 101 | root = insertNode(root,10); 102 | root = insertNode(root,16); 103 | root = insertNode(root,12); 104 | root = insertNode(root,17); 105 | //root = insertNode(root,17); 106 | 107 | 108 | std::cout << "Inorder Tree" << std::endl; 109 | print(root); 110 | level_oreder_print(root); 111 | if(path_sum(root,4)) std::cout << "yes exists" << std::endl; 112 | return 0; 113 | } 114 | -------------------------------------------------------------------------------- /Trees/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int a[]= {9,8,7,6,5,4,3,2,1}; 8 | 9 | 10 | std::cout <<"0[a] "<<0[a] << std::endl;//9 11 | std::cout <<"a[0] "< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | 27 | int main(){ 28 | vector< vector > arr(6,vector(6)); 29 | 30 | for(int arr_i = 0;arr_i < 6;arr_i++){ 31 | for(int arr_j = 0;arr_j < 6;arr_j++){ 32 | cin >> arr[arr_i][arr_j]; 33 | } 34 | } 35 | 36 | for(int arr_i = 0;arr_i < 6;arr_i++){ 37 | for(int arr_j = 0;arr_j < 6;arr_j++){ 38 | cout << arr[arr_i][arr_j]<<" "; 39 | } 40 | std::cout << std::endl; 41 | } 42 | 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /arrays/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tpatil2/C-Programs/e7e8274470421f0bcb166318362df112e7df8db8/arrays/a.out -------------------------------------------------------------------------------- /arrays/arry_geratest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | 8 | //int a[]={1,1,2,4,3,3,4,4,4,3,3,3,4,0,5,3}; 9 | //int a[]={1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,2,4,3,3,4,4,4,3,3,3,4,0,5,3}; 10 | //int a[]={1,2,3,4,5,6,7,8}; 11 | //int a[] = {}; 12 | int a[]={5,2,2,4,2,2,3,1}; 13 | 14 | stack mystack; 15 | int size=sizeof(a)/sizeof(int); 16 | std::cout << "size is "< 6 | #include 7 | 8 | 9 | using namespace std; 10 | 11 | 12 | void display(int buffer[4][4]){ 13 | 14 | for(int i=0;i<4;i++){ 15 | 16 | for(int j=0;j<4;j++){ 17 | cout<<" "<<"["< 9 | #include 10 | 11 | 12 | using namespace std; 13 | 14 | 15 | void display(int buffer[4][4]){ 16 | 17 | for(int i=0;i<4;i++){ 18 | 19 | for(int j=0;j<4;j++){ 20 | cout<<" "<<"["< 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main(int argc, char const *argv[]) { 9 | 10 | cout << "Some data types in C++" << endl; 11 | 12 | //variables and datatypes 13 | 14 | const double PI = 3.1424342345434; 15 | char myGrade= 'A';// one byte 16 | bool flag = true; //one byte 17 | int age = 234567890; // four byte 18 | float speed = 9.12345; //6 decimal place 19 | double accleration = 86.1234567891112131415; // 15 digits a lenght 20 | 21 | cout << "char :"< 2 | #include 3 | 4 | using namespace std; 5 | 6 | class game 7 | { 8 | 9 | private: 10 | string name; 11 | int players; 12 | string type; 13 | 14 | static int numbersOfGames; 15 | 16 | public: 17 | 18 | string get_name(){ return name;} 19 | int get_players(){ return players;} 20 | string get_type(){ return type;} 21 | 22 | void set_name(string play){ name = play; } 23 | void set_players(int num){ players = num;} 24 | void set_type(string typ){ type = typ; } 25 | 26 | //constructor Declared 27 | game(string, int ,string); 28 | //destructor 29 | ~game(); 30 | //default constructor 31 | game(); 32 | 33 | void toString(); 34 | 35 | static int getnumOfGames(){ return numbersOfGames;} 36 | 37 | }; 38 | 39 | 40 | class ipl : public game{ 41 | 42 | private: 43 | int payment; 44 | 45 | public: 46 | 47 | void set_payment(int paym){ payment=paym;} 48 | int get_payment(){return payment;} 49 | 50 | //constructor 51 | ipl(string ,int , string , int ); 52 | ipl():game(){}; 53 | 54 | //overwritten method 55 | void toString(); 56 | 57 | 58 | }; 59 | 60 | 61 | ipl::ipl(string name, int plyr, string typ,int payment):game( name,plyr,typ){ 62 | 63 | this->payment=payment; 64 | 65 | 66 | } 67 | 68 | void ipl::toString() { 69 | 70 | cout<<"The game played is "<< this->get_name() <<" and it has total" 71 | << this->get_players() <<" players and this game is of type " << 72 | this->get_type() <<"got payment of Rs. "<< this->payment<name=name; 81 | this->players=plyr; 82 | this->type=typ; 83 | 84 | this->numbersOfGames++; 85 | 86 | } 87 | 88 | game::~game(){ 89 | std::cout << "End of program "<< this->name<< " is destroyed" << std::endl; 90 | } 91 | 92 | game::game(){ 93 | 94 | this->numbersOfGames++; 95 | 96 | 97 | } 98 | void game::toString() { 99 | 100 | cout<<"The game played is "<< this->name <<" and it has total" 101 | << this->players <<" players and this game is of type " << 102 | this->type < 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Player 8 | { 9 | 10 | // private variables are only available to methods in the class 11 | private: 12 | int odi; 13 | int test; 14 | string name; 15 | 16 | // A static variable shares the same value with every object in the class 17 | static int numOfPlayers; 18 | 19 | // Public variables can be accessed by anything with access to the object 20 | public: 21 | int getOdi(){return odi;} 22 | int getTest(){return test;} 23 | string getName(){return name;} 24 | void setOdi(int od){ odi = od; } 25 | void setTest(int te){ test = te; } 26 | void setName(string plrname){ name = plrname; } 27 | 28 | // Declared as a prototype 29 | void setAll(int, int, string); 30 | 31 | // Declare the constructor 32 | Player(int, int, string); 33 | 34 | // Declare the deconstructor 35 | ~Player(); 36 | 37 | // An overloaded constructor called when no data is passed 38 | Player(); 39 | 40 | // protected members are available to members of the same class and 41 | // sub classes 42 | 43 | // Static methods aren't attached to an object and can only access 44 | // static member variables 45 | static int getNumOfPlayers() { return numOfPlayers; } 46 | 47 | // This method will be overwritten in Dog 48 | void toString(); 49 | 50 | }; 51 | 52 | int Player::numOfPlayers = 0; 53 | 54 | // Define the protoype method setAll 55 | void Player::setAll(int odi, int test, string name){ 56 | 57 | // This is used to refer to an object created of this class type 58 | this -> odi = odi; 59 | this -> test = test; 60 | this -> name = name; 61 | Player::numOfPlayers++; 62 | 63 | } 64 | 65 | // A constructor is called when an object is created 66 | Player::Player(int odi, int test, string name) { 67 | 68 | this -> odi = odi; 69 | this -> test = test; 70 | this -> name = name; 71 | 72 | } 73 | 74 | // The destructor is called when an object is destroyed 75 | Player::~Player() { 76 | 77 | cout << "Player " << this -> name << " destroyed" << endl; 78 | 79 | } 80 | 81 | // A constructor called when no attributes are passed 82 | Player::Player() { 83 | numOfPlayers++; 84 | } 85 | 86 | // This method prints object info to screen and will be overwritten 87 | void Player::toString(){ 88 | 89 | cout << this -> name << " Played " << this -> odi << " odi matches and " 90 | << this -> test << " matches" << endl; 91 | 92 | } 93 | 94 | // We can inherit the variables and methods of other classes 95 | class Bowler : public Player{ 96 | 97 | private: 98 | string type = "spinner"; 99 | public: 100 | void getType() { cout << type << endl; } 101 | 102 | // Declare the constructor 103 | Bowler(int, int, string, string); 104 | 105 | // Declare the default constructor and call the default superclass 106 | // constructor 107 | Bowler() : Player(){}; 108 | 109 | // Overwrite toString 110 | void toString(); 111 | 112 | }; 113 | 114 | // Dog constructor passes the right attributes to the superclass 115 | // constructor and then handles the attribute bark that remains 116 | Bowler::Bowler(int odi, int test, string name, string types) : 117 | Player(odi, test, name){ 118 | 119 | this -> type = types; 120 | 121 | } 122 | 123 | // toString method overwritten 124 | void Bowler::toString(){ 125 | 126 | // Because the attributes were private in Animal they must be retrieved 127 | // by called the get methods 128 | cout << this -> getName() << " played " << this -> getTest() << 129 | " test series and he payed " << this -> getOdi() << " ODI matches and he balls" << 130 | this -> type << endl; 131 | 132 | } 133 | 134 | // ---------- END OF CLASSES ---------- 135 | // ---------- CLASSES & OBJECTS ---------- 136 | // Classes are the blueprints for modeling real world objects 137 | // Real world objects have attributes, classes have members / variables 138 | // Real world objects have abilities, classes have methods / functions 139 | // Classes believe in hiding data (encapsulation) from outside code 140 | 141 | int main(){ 142 | // Declare a Animal type object 143 | Player virat; 144 | 145 | // Set the values for the Animal 146 | virat.setOdi(33); 147 | virat.setTest(10); 148 | virat.setName("Virat Kohali"); 149 | 150 | // Get the values for the Animal 151 | cout << virat.getName() << " Played " << virat.getOdi() << " Matches and " 152 | << virat.getTest() << " Test series" << endl; 153 | 154 | virat.setAll(34, 12, "virat"); 155 | 156 | cout << virat.getName() << " Played " << virat.getOdi() << " Matches and " 157 | << virat.getTest() << " Test series" << endl; 158 | 159 | // Creating an object using the constructor 160 | Player yuvi(36, 15, "Yuvraj Singh"); 161 | 162 | cout << yuvi.getName() << " Played " << yuvi.getOdi() << " Matches and " 163 | << yuvi.getTest() << " Test series" << endl; 164 | 165 | // Demonstrate the inheriting class Dog 166 | Bowler bumra(38, 16, "Bumra", "spinner 1"); 167 | 168 | // static methods are called by using the class name and the scope operator 169 | cout << "Number of Players " << Player::getNumOfPlayers() << endl; 170 | 171 | bumra.getType(); 172 | 173 | // Test the toString method that will be overwritten 174 | virat.toString(); 175 | bumra.toString(); 176 | 177 | // We can call the superclass version of a method with the class name 178 | // and the scope operator 179 | bumra.Player::toString(); 180 | 181 | // When a function finishes it must return an integer value 182 | // Zero means that the function ended with success 183 | return 0; 184 | } 185 | -------------------------------------------------------------------------------- /building_blocks/dec2Bin.cpp: -------------------------------------------------------------------------------- 1 | // you can use includes, for example: 2 | // #include 3 | 4 | // you can write to stdout for debugging purposes, e.g. 5 | // cout << "this is a debug message" << endl; 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | int solution(int N) { 14 | 15 | if(N < 1) 16 | return -1; 17 | 18 | int maxlen = 0; 19 | int currlen = 0; 20 | 21 | bool binGapStart = false; 22 | bool gapZeroes = false; 23 | 24 | while(N >= 1) { 25 | if(N%2 == 1) { 26 | binGapStart = binGapStart && gapZeroes; 27 | if(binGapStart) { 28 | maxlen = (maxlen > currlen)? maxlen:currlen; 29 | gapZeroes = false; 30 | currlen = 0; 31 | } 32 | else 33 | binGapStart = true; 34 | } 35 | else{ 36 | gapZeroes = binGapStart; 37 | if(gapZeroes) 38 | currlen++; 39 | } 40 | N = N/2; 41 | } 42 | return maxlen; 43 | } 44 | 45 | 46 | int main() 47 | { 48 | 49 | std::cout << "lenght is"<< solution(1041) << std::endl; 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /building_blocks/except_hand.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main(int argc, char const *argv[]) { 11 | 12 | int num = 0; 13 | 14 | try{ 15 | if(num!=0){ 16 | int div = 2/num; 17 | }else throw(num); 18 | 19 | }catch(int number){ 20 | 21 | std::cout << "Numbe is not valid" << std::endl; 22 | 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /building_blocks/factorials.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | long factorial(int n){ 6 | 7 | long fact=0; 8 | 9 | if(n<=1){ 10 | return 1; 11 | } 12 | 13 | fact = n * factorial(n-1); 14 | 15 | return fact; 16 | } 17 | 18 | 19 | 20 | int main(){ 21 | long n,m; 22 | std::cout << "Enter the Number" << std::endl; 23 | cin>> n; 24 | 25 | m = factorial(n); 26 | std::cout << "rec in main "< 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main(int argc, char const *argv[]) { 10 | 11 | string my_str = "I am the theme of my perfect life"; 12 | 13 | //open output file if it dosent exists then create it 14 | ofstream output("myfile.txt"); 15 | 16 | if(!output){ 17 | cout<<"Error in opening file"< 2 | #include 3 | using namespace std; 4 | 5 | 6 | 7 | int findgreatest(vector v){ 8 | 9 | string str; 10 | string temp; 11 | int i=0; 12 | int largest=0; 13 | 14 | if(!v.empty()){ 15 | 16 | for(auto it = v.begin();it != v.end(); ++it){ 17 | str += to_string(*it); 18 | } 19 | 20 | while(i < str.length()-1){ 21 | 22 | if(str[i]==str[i+1]){ 23 | temp = str; 24 | temp.erase(i,1); 25 | int x = stoi(temp); 26 | // std::cout < n = {2,2,3,3,3,6,2,2,6}; 57 | //vector n = {1,2,3,4,5,6,7,8}; 58 | //vector n = {}; 59 | 60 | std::cout << "Largest is "<< findgreatest(n) << std::endl; 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /building_blocks/findgreatest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Name Tejas Patil 3 | https://github.com/tpatil2/C-Programs/blob/master/findgreatest.cpp 4 | */ 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main(){ 11 | 12 | int n=223336226; 13 | //int n=14567890; 14 | 15 | string str = to_string(n); 16 | string copy = str; 17 | string temp; 18 | 19 | int i=0; 20 | int largest=0; 21 | 22 | while(i < str.length()-1){ 23 | 24 | if(str[i]==str[i+1]){ 25 | temp = str; 26 | temp.erase(i,1); 27 | int x = stoi(temp); 28 | std::cout < 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | void pass_ref(int* age){ 11 | 12 | std::cout << "my age iside function is : "<< *age << std::endl; 13 | 14 | *age=20; 15 | } 16 | 17 | void num2_ref(int& number){ 18 | 19 | std::cout << "my age iside num2 function is : "<< number << std::endl; 20 | number=100; 21 | } 22 | // * value at : 23 | // & address of : 24 | 25 | int main(int argc, char const *argv[]) { 26 | 27 | int num = 26; 28 | int* pnum; 29 | pnum = # 30 | 31 | std::cout << "num is "<< num << std::endl; 32 | std::cout << "&num is "<< &num << std::endl; 33 | std::cout << "pnum is "<< pnum << std::endl; 34 | std::cout << "*pnum is "<< *pnum << std::endl; 35 | std::cout << "&pnum is "<< &pnum << std::endl; 36 | 37 | pass_ref(pnum); 38 | 39 | std::cout << "my age iside main function is : "<< num << std::endl; 40 | 41 | 42 | int num2=11; 43 | int& anum=num2; 44 | 45 | std::cout << "num2 is :" < 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | char* a=(char*)"Tejas"; 8 | 9 | 10 | std::cout << "a :"< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int a=6; 8 | int *p1=&a; 9 | 10 | std::cout << "a :"< 2 | 3 | using namespace std; 4 | 5 | class Game{ 6 | 7 | public: 8 | void get_game(){ std::cout << "I am in Game base" << std::endl;} 9 | virtual void get_class(){ std::cout << "I am in animal class" << std::endl;} 10 | 11 | }; 12 | 13 | class Art : public Game{ 14 | 15 | public: 16 | void get_class(){ std::cout << "I am in Art class" << std::endl;} 17 | }; 18 | 19 | void what_is_myClass(Game *gm){ 20 | 21 | gm->get_class(); 22 | } 23 | 24 | int main(){ 25 | 26 | Game *game = new Game; 27 | Art *art = new Art; 28 | 29 | game->get_class(); 30 | art->get_class(); 31 | 32 | what_is_myClass(game); 33 | what_is_myClass(art); 34 | 35 | 36 | return 0; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /building_blocks/string_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | using namespace std; 6 | int main(){ 7 | 8 | char str[] = "tejastejas"; 9 | string str1="TEJAS"; 10 | 11 | int n = strlen(str); 12 | 13 | std::cout << "str is : "<< *str << std::endl; 14 | std::cout << "str is : "<< str << std::endl; 15 | std::cout << "str length is : "<< n << std::endl; 16 | 17 | sort(str,str+n); 18 | std::cout << "str sorted is : "<< str << std::endl; 19 | 20 | 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /building_blocks/strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main(int argc, char const *argv[]) { 9 | 10 | string name, surname = "Patil" ; 11 | 12 | std::cout << "Enter your name" << std::endl; 13 | getline(cin,name); 14 | 15 | std::cout << "Hello " << name < 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main(int argc, char const *argv[]) { 10 | 11 | int age; 12 | string input; 13 | std::cout << "Pease enter age" << std::endl; 14 | getline(cin,input); 15 | age=stoi(input); 16 | int large_number = (age > 5) ? age : 5 ; 17 | std::cout << "large_number " << large_number<< std::endl; 18 | 19 | // arrays stoes multiple values of same datatypes 20 | // multidimentional arrat 21 | /* 22 | index 0 1 2 3 23 | 0 [0][0] [0][1] [0][2] [0][3] 24 | 1 [][] [][] [][] [][] 25 | 2 [][] [][] [][] [][] 26 | 3 [][] [][] [][] [][] 27 | */ 28 | 29 | //random number 30 | 31 | int rand_num; 32 | 33 | do { 34 | rand_num = (rand() % 100) + 2; 35 | } while(rand_num !=101); 36 | 37 | std::cout << "random number is : "< 2 | #include 3 | 4 | using namespace std; 5 | 6 | int solution(int X) { 7 | 8 | string str = to_string(X); 9 | 10 | unsigned int i=0; 11 | bool flag = true; 12 | int smallest=0; 13 | string temp; 14 | unsigned int len = str.length()-1; 15 | 16 | 17 | while(i < len){ 18 | 19 | if(str[i] == str[i+1]){ 20 | temp=str; 21 | temp.erase(i,1); 22 | int n = stoi(temp); 23 | if(flag == true) smallest = n; 24 | else if(smallest > n) smallest = n; 25 | } 26 | else if(str[i] != str[i+1]){ 27 | temp=str; 28 | if(str[i]>str[i+1]) temp.erase(i+1,1); 29 | else temp.erase(i,1); 30 | int n2 = stoi(temp); 31 | if(flag == true) smallest = n2; 32 | else if(smallest > n2) smallest = n2; 33 | } 34 | i++; 35 | } 36 | 37 | 38 | 39 | return smallest; 40 | 41 | } 42 | 43 | 44 | int main(){ 45 | 46 | std::cout << "ans is "<< solution(1) << std::endl; 47 | 48 | 49 | } 50 | -------------------------------------------------------------------------------- /building_blocks/test2.cpp: -------------------------------------------------------------------------------- 1 | // you can use includes, for example: 2 | // #include 3 | 4 | // you can write to stdout for debugging purposes, e.g. 5 | // cout << "this is a debug message" << endl; 6 | 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | int find_sum(int spaces){ 13 | 14 | int length=0; 15 | if(spaces==0){ 16 | return length; 17 | }else{ 18 | int sum=12;//modified wrt to values of spaces as in given example 19 | for(int i=0;i0;i--){ 61 | string ext=S.substr(i,4); 62 | if(ext==".png"|| ext==".gif"){ 63 | total+=find_length(S,ext); 64 | } 65 | } 66 | 67 | for(size_t i=S.length()-5;i>0;i--){ 68 | string ext=S.substr(i,5); 69 | if(ext==".jpeg"){ 70 | total+=find_length(S,ext); 71 | } 72 | } 73 | 74 | 75 | return total; 76 | } 77 | -------------------------------------------------------------------------------- /building_blocks/vectors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | void print_vector(vector vec){ 9 | 10 | std::cout << "\nVector is :" << std::endl; 11 | 12 | std::vector::iterator it; 13 | 14 | for(it = vec.begin();it != vec.end(); ++it){ 15 | std::cout <<"[" <<*it <<"]"<< " "; 16 | } 17 | 18 | /* 19 | for(int i=0;i vec) { 28 | 29 | std::cout << "\nMy vector contains" << std::endl; 30 | 31 | for(auto it=vec.cbegin(); it!=vec.cend();it++){ 32 | std::cout <<"["<< *it <<"]"<< " " ; 33 | } 34 | 35 | std::cout << "\nEnd of vector" << std::endl; 36 | 37 | while (!vec.empty()) 38 | { 39 | vec.pop_back(); 40 | } 41 | 42 | if (vec.empty()) {std::cout << "vec is EMPTY NOW" << std::endl;} 43 | 44 | 45 | std::cout << "Print using auto" << std::endl; 46 | for(auto& x:vec) 47 | std::cout << x<<" " << std::endl; 48 | std::cout << "Done printing" << std::endl; 49 | 50 | } 51 | 52 | 53 | void print_revit(vector vec) { 54 | 55 | std::cout << "\nMy vector contains" << std::endl; 56 | 57 | for(auto rit=vec.crbegin(); rit!=vec.crend();rit++){ 58 | std::cout <<"["<< *rit <<"]"<< " " ; 59 | } 60 | 61 | std::cout << "\nEnd of vector" << std::endl; 62 | 63 | } 64 | int main(int argc, char const *argv[]) { 65 | 66 | //vectors are like arrays but size is changed 67 | //decleration vector vectorname(size); 68 | 69 | vector rainfall(7); 70 | std:std::vector::iterator it; 71 | 72 | rainfall[0]=22.1; 73 | rainfall[1]=44.4; 74 | rainfall[2]=44.3; 75 | rainfall[3]=22.5; 76 | rainfall[4]=11.4; 77 | rainfall[5]=77.5; 78 | rainfall[6]=33.3; 79 | 80 | rainfall.push_back(25.3); 81 | rainfall.emplace(rainfall.begin()+1, 123); 82 | rainfall.emplace_back(999); 83 | rainfall.erase(rainfall.begin()+2,rainfall.end()-3); 84 | 85 | it=rainfall.begin(); 86 | 87 | rainfall.insert(it,5,992); 88 | 89 | std::cout << "Value at front is :"< myvector (5); 109 | 110 | int* p = myvector.data(); 111 | 112 | *p = 10; 113 | ++p; 114 | *p = 20; 115 | p[1] = 100; 116 | 117 | std::cout << "myvector contains:"; 118 | for (unsigned i=0; i vec1(5); 124 | std::vector vec2(3); 125 | 126 | vec1.assign(5,555); 127 | vec2.assign(3,333); 128 | 129 | print_it(vec1); 130 | print_it(vec2); 131 | 132 | print_vector(vec1); 133 | print_vector(vec2); 134 | 135 | vec1.swap(vec2); 136 | 137 | print_vector(vec1); 138 | print_vector(vec2); 139 | 140 | 141 | 142 | return 0; 143 | } 144 | -------------------------------------------------------------------------------- /building_blocks/virtual_methods.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Game{ 6 | 7 | public: 8 | void get_game(){ std::cout << "I am in Game base" << std::endl;} 9 | virtual void get_class(){ std::cout << "I am in animal class" << std::endl;} 10 | 11 | }; 12 | 13 | class Art : public Game{ 14 | 15 | public: 16 | void get_class(){ std::cout << "I am in Art class" << std::endl;} 17 | }; 18 | 19 | void what_is_myClass(Game *gm){ 20 | 21 | gm->get_class(); 22 | } 23 | 24 | int main(){ 25 | 26 | Game *game = new Game; 27 | Art *art = new Art; 28 | 29 | game->get_class(); 30 | art->get_class(); 31 | 32 | what_is_myClass(game); 33 | what_is_myClass(art); 34 | 35 | 36 | return 0; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /crickinfo/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tpatil2/C-Programs/e7e8274470421f0bcb166318362df112e7df8db8/crickinfo/a.out -------------------------------------------------------------------------------- /crickinfo/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "player.h" 3 | #include "playerlist.h" 4 | 5 | using namespace std; 6 | 7 | void Display(Player p) { 8 | std::cout << "Rank: " <> rank; 10 | playerinfo >> name; 11 | playerinfo >> contry; 12 | playerinfo >> rating; 13 | } 14 | 15 | int Player::get_rank(){ 16 | return rank; 17 | } 18 | 19 | string Player::get_name(){ 20 | return name; 21 | } 22 | 23 | string Player::get_contry(){ 24 | return contry; 25 | } 26 | 27 | double Player::get_rating(){ 28 | return rating; 29 | } 30 | -------------------------------------------------------------------------------- /crickinfo/player.h: -------------------------------------------------------------------------------- 1 | #ifndef PLAYER_H 2 | #define PLAYER_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class Player{ 11 | 12 | public: 13 | 14 | Player(string player_information); 15 | 16 | string get_name(); 17 | string get_contry(); 18 | double get_rating(); 19 | int get_rank(); 20 | 21 | private: 22 | int rank; 23 | string name; 24 | string contry; 25 | double rating; 26 | 27 | }; 28 | #endif 29 | -------------------------------------------------------------------------------- /crickinfo/player.h.gch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tpatil2/C-Programs/e7e8274470421f0bcb166318362df112e7df8db8/crickinfo/player.h.gch -------------------------------------------------------------------------------- /crickinfo/playerinfo.txt: -------------------------------------------------------------------------------- 1 | 1 S.P.D.Smith AUS 933 2 | 2 V.Kohli IND 875 3 | 3 J.E.Root ENG 848 4 | 4 K.S.Williamson NZ 846 5 | 5 D.A.Warner A846US 812 6 | 6 H.M.Amla SA 787 7 | 7 Azhar-Ali PAK 779 8 | 8 Younus-Khan PAK 772 9 | 9 Q.de-Kock SA 760 10 | 10 A.B.de-Villiers SA 755 11 | 11 Tejas IND 500 12 | -------------------------------------------------------------------------------- /crickinfo/playerlist.cpp: -------------------------------------------------------------------------------- 1 | #include "playerlist.h" 2 | 3 | using namespace std; 4 | 5 | Playerlist::Playerlist(string filepath){ 6 | 7 | ifstream playerfile(filepath); 8 | 9 | string linecontent; 10 | 11 | while(playerfile.good()){ 12 | getline(playerfile, linecontent); 13 | if(linecontent != "\0"){ 14 | Player p(linecontent); 15 | players.push_back(p); 16 | } 17 | } 18 | playerfile.close(); 19 | 20 | sort(players.begin(), players.end(),[](Player &p1, Player &p2){ 21 | return p1.get_rating() > p2.get_rating(); 22 | }); 23 | } 24 | -------------------------------------------------------------------------------- /crickinfo/playerlist.h: -------------------------------------------------------------------------------- 1 | #ifndef PLAYERLIST_H 2 | #define PLAYERLIST_H 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "player.h" 8 | 9 | using namespace std; 10 | 11 | class Playerlist{ 12 | 13 | public: 14 | Playerlist(string filepath); 15 | 16 | //private: 17 | vector players; 18 | }; 19 | 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /crickinfo/playerlist.h.gch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tpatil2/C-Programs/e7e8274470421f0bcb166318362df112e7df8db8/crickinfo/playerlist.h.gch -------------------------------------------------------------------------------- /dijkstra's Algorithm/graph.cpp: -------------------------------------------------------------------------------- 1 | /**----------------------------------------------------------------- 2 | *@File : Graph.cpp 3 | *@Name:Tejas Patil @ChicoStateID: 006949762 4 | *@Assignment No: 05 5 | *@Description:This file contains all function definations used to 6 | claculate shortest path using dikstra's Algorithm 7 | used minipriority queue in this algo 8 | 9 | *@Date: 15 May 2015 10 | *-------------------------------------------------------------------*/ 11 | 12 | #include "graph.h" 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | using namespace std; 20 | 21 | /**------------------------------------------------------------------- 22 | *Description: This function accepts all vertices od graph 23 | *INPUT: void 24 | *OUTPUT: void 25 | *-------------------------------------------------------------------*/ 26 | void Graph::addVertex(string name){ 27 | 28 | Vertex *v = new Vertex(); 29 | v->pi = "NIL"; 30 | v->key= 100; 31 | tempvertices[name]=v; 32 | } 33 | 34 | /**------------------------------------------------------------------- 35 | *Description: This is a function used to store all input vertices and 36 | their parent and key. to reuse it 37 | *INPUT: void 38 | *OUTPUT: void 39 | *-------------------------------------------------------------------*/ 40 | void Graph::retain() 41 | { 42 | 43 | for(map::const_iterator it = tempvertices.begin() 44 | ;it !=tempvertices.end();++it ) 45 | { 46 | Vertex *v = new Vertex(); 47 | v->pi = "NIL"; 48 | v->key= 100; 49 | vertices[it->first]=v; 50 | 51 | } 52 | 53 | } 54 | /**------------------------------------------------------------------- 55 | *Description: This is a function used to store adjcency list of the 56 | graph in map. 57 | *INPUT: void 58 | *OUTPUT: void 59 | *-------------------------------------------------------------------*/ 60 | void Graph::addEdge(string from, string to, int weight) 61 | { 62 | Neighbor *n = new Neighbor(); 63 | n->name = to; 64 | n->weight = weight; 65 | adjList[from].push_back(n); 66 | } 67 | 68 | /**------------------------------------------------------------------- 69 | *Description: This is a function which sorts the neighbors of vertex 70 | in decending order. used insertion sort logic from 71 | Assignment NO 2 72 | *INPUT: void 73 | *OUTPUT: void 74 | *-------------------------------------------------------------------*/ 75 | void Graph::SortNeighbor() 76 | { 77 | string tempname; 78 | int keytemp,i; 79 | 80 | for(map>::const_iterator 81 | it = adjList.begin();it !=adjList.end();++it ) 82 | { 83 | { 84 | for(unsigned int j = 1; jsecond.size(); j++) 85 | { 86 | tempname = it->second[j]->name; 87 | keytemp = it->second[j]->weight; 88 | i=j-1; 89 | 90 | while(i>=0 &&(it->second[i]->name >= tempname)) 91 | { 92 | it->second[i+1]->name = it->second[i]->name; 93 | it->second[i+1]->weight = it->second[i]->weight; 94 | i=i-1; 95 | 96 | } 97 | 98 | it->second[i+1]->name = tempname; 99 | it->second[i+1]->weight = keytemp; 100 | 101 | } 102 | } 103 | } 104 | 105 | } 106 | 107 | /**------------------------------------------------------------------- 108 | *Description: This is a function which accepts start and end vertices 109 | and find the shortest path between them 110 | *INPUT: void 111 | *OUTPUT: return value of type string 112 | *-------------------------------------------------------------------*/ 113 | string Graph::getShortestPath(string from, string to) 114 | { 115 | string u,path; 116 | string g=to; 117 | 118 | vertices[from]->key=0; 119 | 120 | for(map::const_iterator it = vertices.begin() 121 | ;it !=vertices.end();++it ) 122 | { 123 | mq.insert(it->first,it->second->key); 124 | } 125 | 126 | buidSSPTree(from); 127 | 128 | 129 | do 130 | { 131 | ssp.push_back(to); 132 | to=vertices.find(to)->second->pi; 133 | 134 | }while(to!="NIL"); 135 | 136 | 137 | path=""; 138 | 139 | 140 | while(!ssp.empty()) 141 | { 142 | string mac=ssp.back(); 143 | ssp.pop_back(); 144 | path = path + mac + "->"; 145 | } 146 | 147 | int h = vertices[g]->key; 148 | string len = to_string(h); 149 | 150 | path.pop_back(); 151 | path.pop_back(); 152 | 153 | 154 | 155 | 156 | path=path+" with length " + len; 157 | 158 | return path; 159 | } 160 | 161 | 162 | /**------------------------------------------------------------------- 163 | *Description: This function is called in grt shortes path() it accepts 164 | source and construct tree from input source. 165 | *INPUT: void 166 | *OUTPUT: void 167 | *-------------------------------------------------------------------*/ 168 | void Graph::buidSSPTree(string source) 169 | { 170 | string u=source; 171 | 172 | while(!mq.Empty()) 173 | { 174 | u=mq.extractMin(); 175 | SortNeighbor(); 176 | for(map>::const_iterator it = adjList. 177 | begin();it !=adjList.end();++it ) 178 | { 179 | if(it->first==u) 180 | { 181 | for(unsigned int i = 0; isecond.size(); i++) 182 | { 183 | relax(u,it->second[i]->name,it->second[i]->weight); 184 | } 185 | } 186 | } 187 | } 188 | } 189 | 190 | 191 | /**------------------------------------------------------------------- 192 | *Description: This is a function which replace parent ad=nd key of the 193 | tree if it founds new key less than current key 194 | *INPUT: void 195 | *OUTPUT: void 196 | *-------------------------------------------------------------------*/ 197 | void Graph::relax(string u, string v , int w) 198 | { 199 | 200 | if(vertices[v]->key > (vertices[u]->key + w)) 201 | { 202 | 203 | mq.decreaseKey(v ,(vertices[u]->key + w)); 204 | vertices[v]->key = (vertices[u]->key + w); 205 | vertices[v]->pi = u; 206 | 207 | } 208 | 209 | } 210 | -------------------------------------------------------------------------------- /dijkstra's Algorithm/graph.h: -------------------------------------------------------------------------------- 1 | /**----------------------------------------------------------------- 2 | *@File : graph.h 3 | *@Name:Tejas Patil @ChicoStateID: 006949762 4 | *@Assignment No: 05 5 | *@Description:This file contains all function decleration for 6 | the function used in graph.cpp 7 | *@Date: 15 May 2015 8 | *-------------------------------------------------------------------*/ 9 | 10 | 11 | #ifndef GRAPH_H_INCLUDED 12 | #define GRAPH_H_INCLUDED 13 | 14 | #include 15 | #include 16 | #include "minpriority.h" 17 | 18 | using namespace std; 19 | 20 | /**------------------------------------------------------------------ 21 | This is class graph 22 | *-------------------------------------------------------------------*/ 23 | class Graph 24 | { 25 | public: 26 | void addVertex(string name);//to add vertes 27 | void addEdge(string from, string to, int weight);//add edge 28 | string getShortestPath(string from, string to); 29 | //to find shortes path 30 | void retain();//to store vertices for further use 31 | void SortNeighbor();//to sort adjLIST 32 | 33 | private: 34 | 35 | 36 | class Vertex 37 | { 38 | public: 39 | string pi; 40 | int key; 41 | }; 42 | 43 | 44 | 45 | class Neighbor 46 | { 47 | public: 48 | string name; 49 | int weight; 50 | 51 | }; 52 | 53 | void buidSSPTree(string source);//build ssp tree 54 | void relax(string u, string v, int weight);//relax fun 55 | 56 | map vertices;//map for vertices 57 | map tempvertices;//temp map for vertices 58 | map> adjList;//map for adjLIst 59 | MinPriorityQ mq;// minpriority queue object 60 | vector ssp;// vector to store path 61 | int length=0;//variable to store lenth 62 | 63 | }; 64 | 65 | #endif // GRAPH_H_INCLUDED 66 | -------------------------------------------------------------------------------- /dijkstra's Algorithm/minpriority.cpp: -------------------------------------------------------------------------------- 1 | /**----------------------------------------------------------------- 2 | *@File : minpriority.cpp 3 | *@Name:Tejas Patil @ChicoStateID: 006949762 4 | *@Assignment No: 05 Part 1 5 | *@Description:This file contains all function defi9nations for 6 | operations on Heap like Insert, Decrease and extract minimum. 7 | *@Date: 30 April 2015 8 | *-------------------------------------------------------------------*/ 9 | 10 | 11 | #include 12 | #include 13 | #include "minpriority.h" 14 | 15 | /**------------------------------------------------------------------- 16 | *Description: This is destructor of MinpriorityQ, 17 | *INPUT: void 18 | *OUTPUT: void 19 | *-------------------------------------------------------------------*/ 20 | MinPriorityQ::MinPriorityQ(){ 21 | } 22 | 23 | /**--------------------------------------------------------------------- 24 | *Description: his is constructor of MinpriorityQ,frees memory 25 | *INPUT: void 26 | *OUTPUT: void 27 | *---------------------------------------------------------------------*/ 28 | MinPriorityQ::~MinPriorityQ(){ 29 | /*vector::iterator it; 30 | 31 | for(it=minHeap.begin();it!=minHeap.end();it++){ 32 | delete *it; 33 | minHeap.erase(minHeap.begin(),minHeap.end()); 34 | }*/ 35 | } 36 | 37 | /**--------------------------------------------------------------------- 38 | *Description: This is insert function which inserts key and id to the 39 | heap 40 | *INPUT: void 41 | *OUTPUT: void 42 | *---------------------------------------------------------------------*/ 43 | 44 | 45 | void MinPriorityQ::insert(string id, int key){ 46 | 47 | Element *e1 = new Element(); 48 | e1->id = id; 49 | e1->Key= key; 50 | 51 | if(key == 0) 52 | { 53 | minHeap.insert(minHeap.begin(),e1); 54 | //minHeap.push_front(e1); 55 | } 56 | else 57 | { 58 | minHeap.push_back(e1); 59 | //minHeap.insert(minHeap.end(),e1); 60 | decreaseKey(id, key); 61 | } 62 | buildMinHeap(); 63 | 64 | } 65 | /**--------------------------------------------------------------------- 66 | *Description: This is decreasekey function which decrease the key 67 | corresponding to the id of newkey 68 | *INPUT: void 69 | *OUTPUT: void 70 | *---------------------------------------------------------------------*/ 71 | 72 | void MinPriorityQ::decreaseKey(string id , int newKey){ 73 | int i,p; 74 | p= minHeap.size(); 75 | 76 | for( i=0; i < p;i++) 77 | { 78 | if(minHeap[i]->id == id) 79 | { 80 | if(newKey >= minHeap[i]->Key) 81 | { 82 | // cout<< "new key is greater or equal than current key"<< endl; 83 | } 84 | else 85 | { 86 | minHeap[i]->Key =newKey; 87 | while(i > 0 && minHeap[parent(i)]->Key > minHeap[i]->Key) 88 | { 89 | swap(minHeap[i],minHeap[parent(i)]); 90 | i=parent(i); 91 | } 92 | } 93 | break; 94 | } 95 | } 96 | } 97 | /**--------------------------------------------------------------------- 98 | *Description: This is function returns minimum value from heap 99 | *INPUT: void 100 | *OUTPUT: return string 101 | *---------------------------------------------------------------------*/ 102 | 103 | string MinPriorityQ::extractMin(){ 104 | int p; 105 | p= minHeap.size(); 106 | 107 | 108 | if(p!=0) 109 | { 110 | string mini = minHeap[0]->id; 111 | minHeap.erase(minHeap.begin()); 112 | //minHeap.resize(minHeap.size()-1); 113 | minHeapify(0); 114 | return mini; 115 | 116 | } 117 | else 118 | { 119 | cout<<"empty"; 120 | } 121 | return ""; 122 | } 123 | 124 | /**--------------------------------------------------------------------- 125 | *Description: This function checks for id present or not 126 | *OUTPUT: bool return value 127 | *input: void 128 | *---------------------------------------------------------------------*/ 129 | bool MinPriorityQ::isMember(string ){ 130 | return false; 131 | } 132 | 133 | 134 | /**--------------------------------------------------------------------- 135 | *Description: This function construct min heap 136 | *INPUT: void 137 | *OUTPUT: void 138 | *---------------------------------------------------------------------*/ 139 | void MinPriorityQ::buildMinHeap(){ 140 | int p,i; 141 | p= minHeap.size()-1; 142 | 143 | for(i=p/2 ; i >= 0 ; i-- ) 144 | { 145 | minHeapify(i); 146 | } 147 | 148 | } 149 | 150 | 151 | /**--------------------------------------------------------------------- 152 | *Description: This is print function min heapify to heap by accepting 153 | the index 154 | *INPUT: void 155 | *OUTPUT: void 156 | *---------------------------------------------------------------------*/ 157 | void MinPriorityQ::minHeapify(int i){ 158 | 159 | unsigned int l,r,p; 160 | int smallest; 161 | p= minHeap.size(); 162 | 163 | l = left(i); 164 | r = right(i); 165 | 166 | if(l

Key < minHeap[i]->Key) 167 | { 168 | smallest = l; 169 | } 170 | else 171 | { 172 | smallest = i; 173 | } 174 | 175 | if(r

Key < minHeap[smallest]->Key ) 176 | { 177 | smallest=r; 178 | } 179 | if(smallest != i) 180 | { 181 | swap(minHeap[i],minHeap[smallest]); 182 | minHeapify(smallest); 183 | } 184 | } 185 | 186 | 187 | /**--------------------------------------------------------------------- 188 | *Description: This is function returns parent by accepting index 189 | *INPUT: void 190 | *OUTPUT: int 191 | *---------------------------------------------------------------------*/ 192 | int MinPriorityQ::parent(int i){ 193 | 194 | return ((i-1)/2); 195 | 196 | } 197 | 198 | /**--------------------------------------------------------------------- 199 | *Description: This is function returns left child by accepting index 200 | *INPUT: void 201 | *OUTPUT: int 202 | *---------------------------------------------------------------------*/ 203 | 204 | int MinPriorityQ::left(int i){ 205 | 206 | return (2*i)+1; 207 | 208 | } 209 | 210 | 211 | /**--------------------------------------------------------------------- 212 | *Description: This is function returns right child by accepting index 213 | *INPUT: void 214 | *OUTPUT: void 215 | *---------------------------------------------------------------------*/ 216 | 217 | int MinPriorityQ::right(int i){ 218 | 219 | return (2*i)+2; 220 | 221 | } 222 | 223 | /**------------------------------------------------------------------- 224 | *Description: print minpriority queue 225 | *INPUT: void 226 | *OUTPUT: void 227 | *-------------------------------------------------------------------*/ 228 | void MinPriorityQ::print(){ 229 | 230 | int q=minHeap.size(); 231 | 232 | for(int i= 0;iid<<"-----"<< minHeap[i]->Key< 15 | #include 16 | #include 17 | 18 | using namespace std; 19 | 20 | class MinPriorityQ{ 21 | 22 | public: 23 | MinPriorityQ();//constructor 24 | ~MinPriorityQ();//destructor 25 | void insert(string id, int key);//insert key and id 26 | void decreaseKey(string id , int newKey);//decrease key 27 | string extractMin();//extract minimum 28 | bool isMember(string id);//check for id 29 | void print(); 30 | bool Empty(); 31 | 32 | private: 33 | void buildMinHeap();//build min heap 34 | void minHeapify(int i);//minheapify 35 | int parent(int i);//parent of i 36 | int left(int i);//Left child of i 37 | int right(int i);//right child of i 38 | 39 | //class element 40 | class Element{ 41 | 42 | public: 43 | string id; 44 | int Key; 45 | }; 46 | vector minHeap; 47 | }; 48 | #endif // MINPRIORITY_H_INCLUDED 49 | -------------------------------------------------------------------------------- /dijkstra's Algorithm/sspapp.cpp: -------------------------------------------------------------------------------- 1 | /**----------------------------------------------------------------- 2 | *@File : sspapp.cpp 3 | *@Name:Tejas Patil @ChicoStateID: 006949762 4 | *@Assignment No: 05 5 | *@Description:This file contains all function definations for functions 6 | consol input output. 7 | *@Date: 15 May 2015 8 | *-------------------------------------------------------------------*/ 9 | 10 | #include 11 | #include "sspapp.h" 12 | #include 13 | 14 | using namespace std; 15 | /**------------------------------------------------------------------- 16 | *Description: executition starts here and calls readgraph and process 17 | query 18 | *INPUT: void 19 | *OUTPUT: int return 20 | *-------------------------------------------------------------------*/ 21 | int main() 22 | { 23 | SSPapp s1; 24 | while(!cin.eof() && !s1.don()) 25 | s1.readGraph(); 26 | while(!cin.eof()) 27 | s1.processQueries(); 28 | 29 | return 0; 30 | } 31 | 32 | /**------------------------------------------------------------------- 33 | *Description: This function accepts vertices and pass this to 34 | add vertex. and also accepts edges and weights 35 | and build adjList. 36 | *INPUT: void 37 | *OUTPUT: void 38 | *-------------------------------------------------------------------*/ 39 | void SSPapp::readGraph() 40 | { 41 | int no_v, no_e ,weight; 42 | string temp,first,second; 43 | 44 | cin >> no_v; 45 | 46 | for(int i=0;i> temp; 49 | myGraph.addVertex(temp); 50 | } 51 | cin>> no_e; 52 | 53 | for(int i=0; i>first; 56 | cin>>second; 57 | cin>>weight; 58 | myGraph.addEdge(first ,second , weight); 59 | } 60 | 61 | done=true; 62 | 63 | } 64 | 65 | 66 | /**------------------------------------------------------------------- 67 | *Description: this accepts start and end and prints path. 68 | *INPUT: void 69 | *OUTPUT: void 70 | *-------------------------------------------------------------------*/ 71 | void SSPapp::processQueries() 72 | { 73 | 74 | myGraph.retain(); 75 | string str; 76 | string start,End; 77 | cin>>start; 78 | if(!start.empty()) 79 | { 80 | cin>>End; 81 | str=myGraph.getShortestPath(start, End); 82 | cout< 13 | #include 14 | 15 | 16 | using namespace std; 17 | 18 | 19 | /*---------------------------------------------------------------------- 20 | Description: Decleration of class DoublyLinkedList and Node 21 | which act as a decleration for doublylinked list 22 | -----------------------------------------------------------------------*/ 23 | class DoublyLinkedList 24 | 25 | { 26 | private: 27 | 28 | 29 | //class Node decleration and it constructor and Destructor 30 | class Node 31 | { 32 | public: 33 | 34 | Node(); 35 | Node(string& str); 36 | ~Node(); //Node dectructor 37 | 38 | 39 | //Node pointers 40 | string* data; 41 | Node* next; 42 | Node* prev; 43 | }; 44 | 45 | Node* head; 46 | Node* tail; 47 | Node* current; 48 | 49 | 50 | 51 | public: 52 | //Doublylinkedlist constructor and destructor decleration 53 | DoublyLinkedList(); 54 | ~DoublyLinkedList(); 55 | bool empty(); 56 | void append(string& str); 57 | void insertBefore(string& str); 58 | void insertAfter(string& str); 59 | void remove(string& str); 60 | void begin(); 61 | void end(); 62 | bool next(); 63 | bool prev(); 64 | bool find(string& str); 65 | const string& getData(); 66 | 67 | }; 68 | 69 | 70 | 71 | /*-------------------------------EOF-----------------------------------*/ 72 | -------------------------------------------------------------------------------- /doublylinkedlist/playlist.cpp: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------- 2 | Programming Assignment No: 1 (CSCI311 spring 15) 3 | Name: Tejas Vamanrao Patil 4 | Date : 6 Feb 2015 5 | 6 | File Name: playlist.cpp Implementation of a Playlist for music. 7 | 8 | Description: 9 | Implementation of a Playlist for music. A doubly linked list is 10 | used to store the song names. 11 | 12 | -----------------------------------------------------------------------*/ 13 | 14 | #include 15 | #include 16 | #include 17 | #include "playlist.h" 18 | //#include "doublylinkedlist.h" 19 | 20 | using std::string; 21 | using std::cin; 22 | using std::cout; 23 | using std::endl; 24 | 25 | 26 | string U_str,temp,input; 27 | /*---------------------------------------------------------------------- 28 | Description: Process a playlist command.Gets an entire line from 29 | std::cin, extracts the first word as the command,and calls the 30 | appropriate processing function. Invalid commands are ignored. 31 | -----------------------------------------------------------------------*/ 32 | 33 | void Playlist::processCommand() 34 | { 35 | 36 | //string str,input; 37 | getline(cin, U_str); 38 | int p,q; 39 | 40 | 41 | input = U_str.substr(0,U_str.find(' ')); 42 | p = input.length(); 43 | q = U_str.length(); 44 | 45 | if (p 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | int main(){ 8 | 9 | ofstream op; 10 | op.open("out.txt"); 11 | 12 | std::cout << "Please enter ID, name , Salary" << '\n'; 13 | std::cout << "Please enter comand+z after entering" << '\n'; 14 | 15 | int id; 16 | string name; 17 | double Salary; 18 | 19 | while(cin >> id >>name >> Salary){ 20 | op << id << " " << name <<" "< 2 | #include 3 | using namespace std; 4 | 5 | int get_choice(){ 6 | 7 | int choice; 8 | std::cout << "Menu" << '\n'; 9 | std::cout << "Enter 1 for ---Centuries" << '\n'; 10 | std::cout << "Enter 2 for -----Fifties" << '\n'; 11 | std::cout << "Enter 3 for Less than 49" << '\n'; 12 | std::cout << "Enter 4 to ---------Exit" << '\n'; 13 | 14 | cin >> choice; 15 | return choice; 16 | } 17 | 18 | void display_score(int x) { 19 | 20 | ifstream ip; 21 | ip.open("scoreboard.txt"); 22 | if(!ip.is_open()) std::cout << "File Does not exists" << '\n'; 23 | int score; 24 | string player; 25 | 26 | if(x==1){ 27 | while (ip >> player >>score ) { 28 | if(score >= 100){ 29 | std::cout << player<< " "<> player >>score ) { 35 | if(score >= 50 && score < 100 ){ 36 | std::cout << player<< " "<> player >>score ) { 42 | if(score < 50){ 43 | std::cout << player<< " "< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | 8 | int main(){ 9 | 10 | char filename[50]; 11 | ifstream ip; 12 | cin.getline(filename, 50); 13 | 14 | ip.open(filename); 15 | 16 | if(!ip.is_open()){ 17 | std::cout << "File Name does not Exists" << '\n'; 18 | exit(EXIT_FAILURE); 19 | } 20 | 21 | char word[50]; 22 | ip >> word; 23 | while(ip.good()){ 24 | std::cout << word<< ' '; 25 | ip>>word; 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /fileIO/scoreboard.txt: -------------------------------------------------------------------------------- 1 | Sachin 101 2 | Rahul 45 3 | Dhoni 89 4 | Virat 111 5 | Yuvraj 66 6 | kedar 99 7 | Sehwag 43 8 | -------------------------------------------------------------------------------- /fileIO/str_read.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | 8 | ifstream ip; 9 | ip.open("out.txt"); 10 | 11 | int id; 12 | string name; 13 | double Salary; 14 | 15 | if(!ip.is_open()){ 16 | std::cout << "file does not exists:" << '\n'; 17 | } 18 | ip>>id; 19 | ip>>name; 20 | ip>>Salary; 21 | while (ip.good()) { 22 | /* code */ 23 | std::cout << id<<" "<>id; 25 | ip>>name; 26 | ip>>Salary; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /fileIO/write.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | 8 | ofstream myfile; 9 | myfile.open("newfile.txt"); 10 | myfile << "hello World 2. \n"; 11 | myfile.close(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /mickey.cpp: -------------------------------------------------------------------------------- 1 | hello 2 | -------------------------------------------------------------------------------- /ok.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(){ 8 | 9 | string str = "Tejas"; 10 | 11 | std::cout << str.substr(0,1) << std::endl; 12 | 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | int x = 25; 7 | int y = 10; 8 | 9 | 10 | std::cout << "x" <