Skip to content

Commit

Permalink
Hackerrank_c++
Browse files Browse the repository at this point in the history
  • Loading branch information
AdithyakrishnaV committed Sep 23, 2021
0 parents commit 3c42027
Show file tree
Hide file tree
Showing 23 changed files with 815 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"algorithm": "cpp"
}
}
32 changes: 32 additions & 0 deletions Hackerrank_C++/Arrays_Introduction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++){
cin>>arr[i];
}

int l=0, h=n-1;

while(l<h){
int temp=arr[l];
arr[l]=arr[h];
arr[h]=temp;
l++;
h--;
}

for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}

return 0;
}
83 changes: 83 additions & 0 deletions Hackerrank_C++/Attribute_Parser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;

int main()
{
int n, q,i;
cin>>n>>q;
string temp;
vector<string> hrml;
vector<string> quer;
cin.ignore();

for(i=0;i<n;i++)
{
getline(cin,temp);
hrml.push_back(temp);
}
for(i=0;i<q;i++)
{
getline(cin,temp);
quer.push_back(temp);
}

map<string, string> m;
vector<string> tag;

for(i=0;i<n;i++)
{
temp=hrml[i];
temp.erase(remove(temp.begin(), temp.end(), '\"' ), temp.end());
temp.erase(remove(temp.begin(), temp.end(), '>' ), temp.end());

if(temp.substr(0,2)=="</")
{
tag.pop_back();
}
else
{
stringstream ss;
ss.str("");
ss<<temp;
string t1,p1,v1;
char ch;
ss>>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<q;i++)
{
if (m.find(quer[i]) == m.end())
{
cout << "Not Found!\n";
}
else
{
cout<<m[quer[i]]<<endl;
}
}
return 0;
}
24 changes: 24 additions & 0 deletions Hackerrank_C++/Basic_Data_Types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>
#include <cstdio>
#include <iomanip>
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;
}

111 changes: 111 additions & 0 deletions Hackerrank_C++/Box_It!.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include<bits/stdc++.h>

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<n;i++)
{
int type;
cin>>type;
if(type ==1)
{
cout<<temp<<endl;
}
if(type == 2)
{
int l,b,h;
cin>>l>>b>>h;
Box NewBox(l,b,h);
temp=NewBox;
cout<<temp<<endl;
}
if(type==3)
{
int l,b,h;
cin>>l>>b>>h;
Box NewBox(l,b,h);
if(NewBox<temp)
{
cout<<"Lesser\n";
}
else
{
cout<<"Greater\n";
}
}
if(type==4)
{
cout<<temp.CalculateVolume()<<endl;
}
if(type==5)
{
Box NewBox(temp);
cout<<NewBox<<endl;
}

}
}

int main()
{
check2();
}
68 changes: 68 additions & 0 deletions Hackerrank_C++/Class.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <bits/stdc++.h>
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<<st.get_age()<<"\n";
cout<<st.get_last_name()<<c<<" "<<st.get_first_name()<<"\n";
cout<<st.get_standard()<<"\n";
cout<<"\n";
cout<<st.get_age()<<c<<st.get_first_name()<<c<<st.get_last_name()<<c<<st.get_standard();

return 0;
}
28 changes: 28 additions & 0 deletions Hackerrank_C++/Classes and Objects.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cassert>
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() {
Loading

0 comments on commit 3c42027

Please sign in to comment.