Skip to content

Commit e945783

Browse files
committed
Added solution for HackerRank Grading Students problem
1 parent c5f7f0d commit e945783

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Author:- Rahul Malhotra
3+
* Source:- Programming Vidya
4+
* Description:- Solution for HackerRank Grading Students problem
5+
* Problem Link:- https://www.hackerrank.com/challenges/grading/problem
6+
* Website:- www.programmingvidya.com
7+
*/
8+
9+
#include <bits/stdc++.h>
10+
11+
using namespace std;
12+
13+
string ltrim(const string &);
14+
string rtrim(const string &);
15+
16+
/*
17+
* Complete the 'gradingStudents' function below.
18+
*
19+
* The function is expected to return an INTEGER_ARRAY.
20+
* The function accepts INTEGER_ARRAY grades as parameter.
21+
*/
22+
23+
vector<int> gradingStudents(vector<int> grades) {
24+
25+
// * Initializing variables
26+
int length = grades.size();
27+
28+
// * Check each grade one by one
29+
for(int i=0; i<length; i++) {
30+
31+
// * Calculating the remainder when grade is divided by 5
32+
int rem = grades[i]%5;
33+
34+
/*
35+
* If grade is more than 37 and
36+
* remainder is greater than or equal to 3,
37+
* add 5 - remainder to the grade
38+
*/
39+
if(grades[i]>37 && rem>=3) {
40+
grades[i] += 5-rem;
41+
}
42+
}
43+
44+
// * Return the grades vector
45+
return grades;
46+
}
47+
48+
int main()
49+
{
50+
ofstream fout(getenv("OUTPUT_PATH"));
51+
52+
string grades_count_temp;
53+
getline(cin, grades_count_temp);
54+
55+
int grades_count = stoi(ltrim(rtrim(grades_count_temp)));
56+
57+
vector<int> grades(grades_count);
58+
59+
for (int i = 0; i < grades_count; i++) {
60+
string grades_item_temp;
61+
getline(cin, grades_item_temp);
62+
63+
int grades_item = stoi(ltrim(rtrim(grades_item_temp)));
64+
65+
grades[i] = grades_item;
66+
}
67+
68+
vector<int> result = gradingStudents(grades);
69+
70+
for (int i = 0; i < result.size(); i++) {
71+
fout << result[i];
72+
73+
if (i != result.size() - 1) {
74+
fout << "\n";
75+
}
76+
}
77+
78+
fout << "\n";
79+
80+
fout.close();
81+
82+
return 0;
83+
}
84+
85+
string ltrim(const string &str) {
86+
string s(str);
87+
88+
s.erase(
89+
s.begin(),
90+
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
91+
);
92+
93+
return s;
94+
}
95+
96+
string rtrim(const string &str) {
97+
string s(str);
98+
99+
s.erase(
100+
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
101+
s.end()
102+
);
103+
104+
return s;
105+
}

0 commit comments

Comments
 (0)