From 3c42027e5817b52aa171681ba03e36e737fd819e Mon Sep 17 00:00:00 2001 From: AdithyakrishnaV Date: Thu, 23 Sep 2021 13:36:13 +0530 Subject: [PATCH] Hackerrank_c++ --- .vscode/settings.json | 5 + Hackerrank_C++/Arrays_Introduction.cpp | 32 +++++ Hackerrank_C++/Attribute_Parser.cpp | 83 +++++++++++++ Hackerrank_C++/Basic_Data_Types.cpp | 24 ++++ Hackerrank_C++/Box_It!.cpp | 111 ++++++++++++++++++ Hackerrank_C++/Class.cpp | 68 +++++++++++ Hackerrank_C++/Classes and Objects.cpp | 28 +++++ Hackerrank_C++/Conditional_Statements.cpp | 45 +++++++ Hackerrank_C++/For_Loop.cpp | 23 ++++ Hackerrank_C++/Functions.cpp | 36 ++++++ Hackerrank_C++/Inherited_Code.cpp | 51 ++++++++ Hackerrank_C++/Input_and_Output.cpp | 18 +++ Hackerrank_C++/Lower_Bound_STL.cpp | 33 ++++++ Hackerrank_C++/Maps_STL.cpp | 38 ++++++ Hackerrank_C++/Pointer.cpp | 21 ++++ Hackerrank_C++/Say_Hello, World!_With_C++.cpp | 8 ++ Hackerrank_C++/Sets_STL.cpp | 45 +++++++ Hackerrank_C++/StringStream.cpp | 18 +++ Hackerrank_C++/Strings.cpp | 24 ++++ Hackerrank_C++/Structs.cpp | 20 ++++ Hackerrank_C++/Variable_Sized_Arrays.cpp | 32 +++++ Hackerrank_C++/Vector_Erase.cpp | 28 +++++ Hackerrank_C++/Vector_Sort.cpp | 24 ++++ 23 files changed, 815 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 Hackerrank_C++/Arrays_Introduction.cpp create mode 100644 Hackerrank_C++/Attribute_Parser.cpp create mode 100644 Hackerrank_C++/Basic_Data_Types.cpp create mode 100644 Hackerrank_C++/Box_It!.cpp create mode 100644 Hackerrank_C++/Class.cpp create mode 100644 Hackerrank_C++/Classes and Objects.cpp create mode 100644 Hackerrank_C++/Conditional_Statements.cpp create mode 100644 Hackerrank_C++/For_Loop.cpp create mode 100644 Hackerrank_C++/Functions.cpp create mode 100644 Hackerrank_C++/Inherited_Code.cpp create mode 100644 Hackerrank_C++/Input_and_Output.cpp create mode 100644 Hackerrank_C++/Lower_Bound_STL.cpp create mode 100644 Hackerrank_C++/Maps_STL.cpp create mode 100644 Hackerrank_C++/Pointer.cpp create mode 100644 Hackerrank_C++/Say_Hello, World!_With_C++.cpp create mode 100644 Hackerrank_C++/Sets_STL.cpp create mode 100644 Hackerrank_C++/StringStream.cpp create mode 100644 Hackerrank_C++/Strings.cpp create mode 100644 Hackerrank_C++/Structs.cpp create mode 100644 Hackerrank_C++/Variable_Sized_Arrays.cpp create mode 100644 Hackerrank_C++/Vector_Erase.cpp create mode 100644 Hackerrank_C++/Vector_Sort.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..57c2030 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "algorithm": "cpp" + } +} \ No newline at end of file diff --git a/Hackerrank_C++/Arrays_Introduction.cpp b/Hackerrank_C++/Arrays_Introduction.cpp new file mode 100644 index 0000000..4b70998 --- /dev/null +++ b/Hackerrank_C++/Arrays_Introduction.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +using namespace std; + + +int main() { + int n; + cin>>n; + int arr[n]; + for(int i=0; i>arr[i]; + } + + int l=0, h=n-1; + + while(l +#include +#include +#include +#include +#include +using namespace std; + +int main() +{ + int n, q,i; + cin>>n>>q; + string temp; + vector hrml; + vector quer; + cin.ignore(); + + for(i=0;i m; + vector tag; + + for(i=0;i' ), temp.end()); + + if(temp.substr(0,2)==">ch>>t1>>p1>>ch>>v1; + string temp1=""; + if(tag.size()>0) + { + temp1=*tag.rbegin(); + temp1=temp1+"."+t1; + } + else + { + temp1=t1; + } + tag.push_back(temp1); + m[*tag.rbegin()+"~"+p1]=v1; + while(ss) + { + ss>>p1>>ch>>v1; + m[*tag.rbegin()+"~"+p1]=v1; + } + } + + } + + for(i=0;i +#include +#include +using namespace std; +int main() +{ + int a; + long b; + char c; + float d; + double e; + cin>>a; + cin>>b; + cin>>c; + cin>>d; + cin>>e; + cout << setprecision(20)<< a<< endl; + cout << setprecision(20)<< b<< endl; + cout << setprecision(20)<< c<< endl; + cout << setprecision(20)<< d<< endl; + cout << setprecision(20)<< e<< endl; + return 0; +} + diff --git a/Hackerrank_C++/Box_It!.cpp b/Hackerrank_C++/Box_It!.cpp new file mode 100644 index 0000000..88384de --- /dev/null +++ b/Hackerrank_C++/Box_It!.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; +//Implement the class Box +class Box{ + private: + int l, b, h; //l,b,h are integers representing the dimensions of the box + public: + //Default + Box():l(0), b(0), h(0) {} + + Box(int x, int y, int z){ + l = x; + b = y; + h = z; + } + + Box(const Box& B){ + this->l = B.l; + this->b = B.b; + this->h = B.h; + } + + //Getter + int getLength() const { //Return box's length + return l; + } + + int getBreadth() const { // Return box's breadth + return b; + } + + int getHeight() const { //Return box's height + return h; + } + + long long CalculateVolume(){ + long long volume =(long long) l*b*h; + return volume; // Return the volume of the box + } + + bool operator<(Box& other){ + bool condition1 = l < other.l; + bool condition2 = b < other.b && l == other.l; + bool condition3 = h < other.h && b == other.b && h == other.h; + + if(condition1 || condition2 || condition1) + return true; + + return false; + } + //Friend function + friend ostream& operator<<(ostream& out, Box& B){ + out << B.l <<" "<< B.b <<" "<< B.h; + return out; + } +}; + + +void check2() +{ + int n; + cin>>n; + Box temp; + for(int i=0;i>type; + if(type ==1) + { + cout<>l>>b>>h; + Box NewBox(l,b,h); + temp=NewBox; + cout<>l>>b>>h; + Box NewBox(l,b,h); + if(NewBox +using namespace std; + +class Student { + private: + int m_age; + int m_standard; + string m_first_name; + string m_last_name; + + public: + + void set_age( int age){ + m_age = age; + } + + void set_standard(int standard){ + m_standard = standard; + } + + void set_first_name(string firstName){ + m_first_name = firstName; + } + + void set_last_name(string lastName){ + m_last_name = lastName; + } + + int get_age(){ + return m_age; + } + + int get_standard(){ + return m_standard; + } + + string get_first_name(){ + return m_first_name; + } + + string get_last_name(){ + return m_last_name; + } + +}; + + + +int main(){ + Student st; + int m_age, m_standard; + string m_first_name, m_last_name; + char c=','; + cin>>m_age>>m_first_name>>m_last_name>>m_standard; + + st.set_age(m_age); + st.set_first_name(m_first_name); + st.set_last_name(m_last_name); + st.set_standard(m_standard); + + cout< +#include +#include +#include +#include +#include +using namespace std; + +class Student{ + private: + int sum; + int m[5]; + public: + Student(){ + sum=0; + } + void input(){ + for(int i=0; i<5; i++){ + cin>>m[i]; + sum+=m[i]; + } + } + int calculateTotalScore(){ + return sum; + } + +}; +int main() { \ No newline at end of file diff --git a/Hackerrank_C++/Conditional_Statements.cpp b/Hackerrank_C++/Conditional_Statements.cpp new file mode 100644 index 0000000..4164ada --- /dev/null +++ b/Hackerrank_C++/Conditional_Statements.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +int main() +{ + int n; + cin>>n; + + if(n==0){ + cout << "zero "; + + }else if(n==1){ + cout << "one "; + + }else if (n==2){ + cout << "two "; + + }else if(n==3){ + cout << "three "; + + }else if(n==4){ + cout << "four "; + + }else if(n==5){ + cout << "five "; + + }else if(n==6){ + cout << "six "; + + }else if(n==7){ + cout << "seven "; + + }else if(n==8){ + cout << "eight "; + + }else if(n==9){ + cout << "nine "; + } + else{ + cout<<"Greater than 9"; + } + + return 0; +} diff --git a/Hackerrank_C++/For_Loop.cpp b/Hackerrank_C++/For_Loop.cpp new file mode 100644 index 0000000..4baf83e --- /dev/null +++ b/Hackerrank_C++/For_Loop.cpp @@ -0,0 +1,23 @@ +#include +#include +using namespace std; + +int main() { + + int a,b,i; + char *str[] = {"one","two","three","four","five","six","seven","eight","nine","even","odd"}; + cin>>a; + cin>>b; + for(i=a;i<=b;i++) + { + if(i<=9) + { + cout< +#include +using namespace std; + + +int max_of_four(int a, int b, int c, int d){ + if(a>b && a>c){ + if(a>d){ + cout<c){ + if(b>d){ + cout<d){ + cout<>a; + cin>>b; + cin>>c; + cin>>d; + max_of_four(a, b, c, d); + + return 0; +} diff --git a/Hackerrank_C++/Inherited_Code.cpp b/Hackerrank_C++/Inherited_Code.cpp new file mode 100644 index 0000000..372733e --- /dev/null +++ b/Hackerrank_C++/Inherited_Code.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +using namespace std; + +class BadLengthException{ + private: + int num; + public: + BadLengthException(int n) : num(n) {} + + //Getter + int what() { + return num; + } +}; + + +bool checkUsername(string username) { + bool isValid = true; + int n = username.length(); + if(n < 5) { + throw BadLengthException(n); + } + for(int i = 0; i < n-1; i++) { + if(username[i] == 'w' && username[i+1] == 'w') { + isValid = false; + } + } + return isValid; +} + +int main() { + int T; cin >> T; + while(T--) { + string username; + cin >> username; + try { + bool isValid = checkUsername(username); + if(isValid) { + cout << "Valid" << '\n'; + } else { + cout << "Invalid" << '\n'; + } + } catch (BadLengthException e) { + cout << "Too short: " << e.what() << '\n'; + } + } + return 0; +} \ No newline at end of file diff --git a/Hackerrank_C++/Input_and_Output.cpp b/Hackerrank_C++/Input_and_Output.cpp new file mode 100644 index 0000000..89d4f0f --- /dev/null +++ b/Hackerrank_C++/Input_and_Output.cpp @@ -0,0 +1,18 @@ +#include +#include +#include +#include +#include +using namespace std; + + +int main() { + + int a; + int b; + int c; + cin>>a>>b>>c; + int sum = a+b+c; + cout< +#include +#include +#include +#include +using namespace std; + + +int main() { + long n, a, q, query; + cin>>n; + vector v; + + for(int i=0; i>a; + v.push_back(a); + } + + cin>>q; + + for(int i=0; i>query; + + vector::iterator itr = lower_bound(v.begin(), v.end(), query); + if(query != *itr){ + cout<< "No " <<(itr - v.begin()+1) << endl; + }else{ + cout<< "Yes " <<(itr - v.begin()+1) << endl; + } + } + + return 0; +} diff --git a/Hackerrank_C++/Maps_STL.cpp b/Hackerrank_C++/Maps_STL.cpp new file mode 100644 index 0000000..587e26d --- /dev/null +++ b/Hackerrank_C++/Maps_STL.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +int main() { + int q; + cin>>q; + mapmp; + while(q--){ + int type; + cin>>type; + if(type == 1){ + string name; + int mark; + cin>>name>>mark; + if(mp.find(name) != mp.end()){ + mp[name] = mp[name] + mark; + }else{ + mp.insert(make_pair(name,mark)); + } + }else if(type == 2){ + string name; + cin>>name; + mp[name] = 0; + }else if(type == 3){ + string name; + cin>>name; + cout< + +void update(int *a,int *b) { + int temp=*a; + *a=*a+*b; + *b=temp-*b; + if(*b<0){ + *b=-*b; + } +} + +int main() { + int a, b; + int *pa = &a, *pb = &b; + + scanf("%d %d", &a, &b); + update(pa, pb); + printf("%d\n%d", a, b); + + return 0; +} diff --git a/Hackerrank_C++/Say_Hello, World!_With_C++.cpp b/Hackerrank_C++/Say_Hello, World!_With_C++.cpp new file mode 100644 index 0000000..b4c6dab --- /dev/null +++ b/Hackerrank_C++/Say_Hello, World!_With_C++.cpp @@ -0,0 +1,8 @@ +#include +#include +using namespace std; + +int main() { + cout<<"Hello, World!"; + return 0; +} \ No newline at end of file diff --git a/Hackerrank_C++/Sets_STL.cpp b/Hackerrank_C++/Sets_STL.cpp new file mode 100644 index 0000000..7c506aa --- /dev/null +++ b/Hackerrank_C++/Sets_STL.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +int main() { + set s; + int n; + cin>>n; + + for(int i=0; i<=n; i++){ + string query_input; + int query_type, query_number; + getline(cin, query_input); + + if(query_input.size() > 1) { + istringstream ss(query_input); + ss >> query_type >> query_number; + + switch(query_type){ + case 1: + s.insert(query_number); + break; + case 2: + s.erase(query_number); + break; + case 3: + set::iterator it = s.find(query_number); + + if(it == s.end()){ + cout<< "No" << endl; + }else{ + cout<< "Yes" << endl; + } + } + } + } + return 0; +} diff --git a/Hackerrank_C++/StringStream.cpp b/Hackerrank_C++/StringStream.cpp new file mode 100644 index 0000000..f0f42e3 --- /dev/null +++ b/Hackerrank_C++/StringStream.cpp @@ -0,0 +1,18 @@ +#include +#include +#include +using namespace std; + +int main() { + string str; + cin>>str; + for (int i=0; i +#include +using namespace std; + +int main() { + string a, b, c; + cin>>a; + cin>>b; + int n, m; + n=a.size();//find length + m=b.size();//find length + c=a+b; + + char temp=a.at(0); + a.at(0)=b.at(0); + b.at(0)=temp; + + + cout< +#include +#include +#include +#include +using namespace std; + + +int main() { + int a, d; + string b, c; + cin>>a; + cin>>b; + cin>>c; + cin>>d; + + cout< +#include +#include +#include +#include +using namespace std; + + +int main() { + int n, q; + cin>> n >> q; + vector arr[n]; + for(int i=0; i>s; + + int o; + for(int j=0; j>o; + arr[i].push_back(o); + } + } + + int r, s; + for(int k=0; k>r>>s; + cout< +#include +#include +#include +#include +using namespace std; + + +int main() { + vector v; + int x, y, z; + int n; + cin>>n; + for(int i=0; i>x; + v.push_back(x); + } + int a1, a2, a3; + cin>>a1; + cin>>a2>>a3; + v.erase(v.begin()+a1-1); + v.erase(v.begin()+a2-1, v.begin()+a3-1); + cout< +#include +#include +#include +#include +using namespace std; + + +int main() { + int n; + cin>>n; + + int arr[n]; + for(int i=0; i>arr[i]; + } + + sort(arr, arr+n); + + for(int i=0; i