Skip to content

Commit f756f07

Browse files
Hackerrank solutions
1 parent c4502b0 commit f756f07

35 files changed

+1425
-0
lines changed

hackerrank/10_Attribute_Parser.cpp

Whitespace-only changes.

hackerrank/11_StringStream.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Link: https://www.hackerrank.com/challenges/c-tutorial-stringstream/problem
2+
3+
#include <sstream>
4+
#include <vector>
5+
#include <iostream>
6+
using namespace std;
7+
8+
vector<int> parseInts(string str) {
9+
// Complete this function
10+
vector<int> result;
11+
stringstream ss(str);
12+
char ch;
13+
int tmp;
14+
while (ss >> tmp) {
15+
result.push_back(tmp);
16+
ss >> ch;
17+
}
18+
return result;
19+
}
20+
21+
int main() {
22+
string str;
23+
cin >> str;
24+
vector<int> integers = parseInts(str);
25+
for(int i = 0; i < integers.size(); i++) {
26+
cout << integers[i] << endl;
27+
}
28+
29+
return 0;
30+
}

hackerrank/12_Strings.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Link: https://www.hackerrank.com/challenges/c-tutorial-strings/problem
2+
3+
#include <iostream>
4+
#include <string>
5+
using namespace std;
6+
7+
int main() {
8+
string a, b;
9+
cin >> a >> b;
10+
cout << a.size() << " " << b.size() << endl;
11+
cout << a + b << endl;
12+
char tmp = a[0];
13+
a[0] = b[0];
14+
b[0] = tmp;
15+
cout << a << " " << b << endl;
16+
return 0;
17+
}

hackerrank/13_Structs.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Link: https://www.hackerrank.com/challenges/c-tutorial-struct/problem
2+
3+
#include <iostream>
4+
#include <sstream>
5+
using namespace std;
6+
7+
struct Student {
8+
int age;
9+
string first_name;
10+
string last_name;
11+
int standard;
12+
};
13+
14+
int main() {
15+
Student st;
16+
cin >> st.age >> st.first_name >> st.last_name >> st.standard;
17+
cout << st.age << " " << st.first_name << " " << st.last_name << " " << st.standard;
18+
return 0;
19+
}
20+

hackerrank/14_Class.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Link: https://www.hackerrank.com/challenges/c-tutorial-class/problem
2+
3+
#include <iostream>
4+
#include <sstream>
5+
using namespace std;
6+
7+
class Student {
8+
private:
9+
int age, standard;
10+
string first_name, last_name;
11+
public:
12+
void set_age(int a) {
13+
age = a;
14+
}
15+
void set_standard(int s) {
16+
standard = s;
17+
}
18+
void set_first_name(string fn) {
19+
first_name = fn;
20+
}
21+
void set_last_name(string ln) {
22+
last_name = ln;
23+
}
24+
int get_age() {
25+
return age;
26+
}
27+
int get_standard() {
28+
return standard;
29+
}
30+
string get_first_name() {
31+
return first_name;
32+
}
33+
string get_last_name() {
34+
return last_name;
35+
}
36+
string to_string() {
37+
stringstream ss;
38+
ss << age << "," << first_name << "," << last_name << "," << standard;
39+
return ss.str();
40+
}
41+
};
42+
43+
int main() {
44+
int age, standard;
45+
string first_name, last_name;
46+
47+
cin >> age >> first_name >> last_name >> standard;
48+
49+
Student st;
50+
st.set_age(age);
51+
st.set_standard(standard);
52+
st.set_first_name(first_name);
53+
st.set_last_name(last_name);
54+
55+
cout << st.get_age() << endl;
56+
cout << st.get_last_name() << ", " << st.get_first_name() << endl;
57+
cout << st.get_standard() << endl;
58+
cout << endl;
59+
cout << st.to_string();
60+
61+
return 0;
62+
}

hackerrank/15_Classes_and_Objects.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Link: https://www.hackerrank.com/challenges/classes-objects/problem
2+
3+
4+
#include <cmath>
5+
#include <cstdio>
6+
#include <vector>
7+
#include <iostream>
8+
#include <algorithm>
9+
using namespace std;
10+
11+
12+
class Student {
13+
private:
14+
int scores[5];
15+
public:
16+
void input() {
17+
for (int i = 0; i < 5; i++) {
18+
cin >> scores[i];
19+
}
20+
}
21+
int calculateTotalScore() {
22+
int total = 0;
23+
for (int i = 0; i < 5; i++) {
24+
total += scores[i];
25+
}
26+
return total;
27+
}
28+
};
29+
30+
int main() {
31+
int n; // number of students
32+
cin >> n;
33+
Student *s = new Student[n]; // an array of n students
34+
35+
for(int i = 0; i < n; i++){
36+
s[i].input();
37+
}
38+
39+
// calculate kristen's score
40+
int kristen_score = s[0].calculateTotalScore();
41+
42+
// determine how many students scored higher than kristen
43+
int count = 0;
44+
for(int i = 1; i < n; i++){
45+
int total = s[i].calculateTotalScore();
46+
if(total > kristen_score){
47+
count++;
48+
}
49+
}
50+
51+
// print result
52+
cout << count;
53+
54+
return 0;
55+
}

hackerrank/16_Box_It.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Link: https://www.hackerrank.com/challenges/box-it/problem
2+
3+
#include <bits/stdc++.h>
4+
5+
using namespace std;
6+
7+
8+
class Box {
9+
private:
10+
int l, b, h;
11+
public:
12+
Box() {
13+
l = 0;
14+
b = 0;
15+
h = 0;
16+
}
17+
Box(int length, int breadth, int height) {
18+
l = length;
19+
b = breadth;
20+
h = height;
21+
}
22+
Box(Box &B) {
23+
l = B.l;
24+
b = B.b;
25+
h = B.h;
26+
}
27+
int getLength() {
28+
return l;
29+
}
30+
int getBreadth() {
31+
return b;
32+
}
33+
int getHeight() {
34+
return h;
35+
}
36+
long long CalculateVolume() {
37+
return (long long)l * b * h;
38+
}
39+
bool operator<(Box &B) {
40+
if (l < B.l) {
41+
return true;
42+
} else if (b < B.b && l == B.l) {
43+
return true;
44+
} else if (h < B.h && b == B.b && l == B.l) {
45+
return true;
46+
} else {
47+
return false;
48+
}
49+
}
50+
friend ostream& operator<<(ostream& out, Box& B);
51+
};
52+
53+
ostream& operator<<(ostream& out, Box& B) {
54+
out << B.l << " " << B.b << " " << B.h;
55+
return out;
56+
}
57+
58+
void check2() {
59+
int n;
60+
cin >> n;
61+
Box temp;
62+
for (int i = 0; i < n; i++) {
63+
int type;
64+
cin >> type;
65+
if (type == 1) {
66+
cout << temp << endl;
67+
}
68+
if (type == 2) {
69+
int l, b, h;
70+
cin >> l >> b >> h;
71+
Box NewBox(l, b, h);
72+
temp = NewBox;
73+
cout << temp << endl;
74+
}
75+
if (type == 3) {
76+
int l, b, h;
77+
cin >> l >> b >> h;
78+
Box NewBox(l, b, h);
79+
if (NewBox < temp) {
80+
cout << "Lesser" << endl;
81+
} else {
82+
cout << "Greater" << endl;
83+
}
84+
}
85+
if (type == 4) {
86+
cout << temp.CalculateVolume() << endl;
87+
}
88+
if (type == 5) {
89+
Box NewBox(temp);
90+
cout << NewBox << endl;
91+
}
92+
}
93+
}
94+
95+
int main() {
96+
check2();
97+
}

hackerrank/17_Inherited_code.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Link: https://www.hackerrank.com/challenges/inherited-code/problem
2+
3+
#include <iostream>
4+
#include <string>
5+
#include <sstream>
6+
#include <exception>
7+
using namespace std;
8+
9+
/* Define the exception here */
10+
class BadLengthException {
11+
private:
12+
int n;
13+
public:
14+
BadLengthException(int errornumber) {
15+
n = errornumber;
16+
}
17+
18+
int what() {
19+
return n;
20+
}
21+
};
22+
23+
24+
bool checkUsername(string username) {
25+
bool isValid = true;
26+
int n = username.length();
27+
if(n < 5) {
28+
throw BadLengthException(n);
29+
}
30+
for(int i = 0; i < n-1; i++) {
31+
if(username[i] == 'w' && username[i+1] == 'w') {
32+
isValid = false;
33+
}
34+
}
35+
return isValid;
36+
}
37+
38+
int main() {
39+
int T; cin >> T;
40+
while(T--) {
41+
string username;
42+
cin >> username;
43+
try {
44+
bool isValid = checkUsername(username);
45+
if(isValid) {
46+
cout << "Valid" << '\n';
47+
} else {
48+
cout << "Invalid" << '\n';
49+
}
50+
} catch (BadLengthException e) {
51+
cout << "Too short: " << e.what() << '\n';
52+
}
53+
}
54+
return 0;
55+
}

0 commit comments

Comments
 (0)