HackerRank Grading Students Problem Solution
In this post, We are going to solve HackerRank Grading Students Problem. HackerLand University has the following grading policy:
- Every student receives a grade in the inclusive range from 0 to 100.
- Any Grade less than 40 is a failing grade.
Sam is a professor at the university and likes to round each student’s grade according to these rules:
- If the difference between the grade and the next multiple of 5 is less than 3, round the grade up to the next multiple of 5.
- If the value of the grade is less than 38, no rounding occurs and the result will still be a failing grade.
Examples
- grade = 84 round to 85 (85 – 84 is less than 3)
- grade = 29 do not round (result is less than 40)
- grade = 57 do not round (60 – 57 is 3 or higher)
Given the initial value of grade for each of Sam’s n students, write code to automate the rounding process.
Function Description
Complete the function grading students in the editor below.
grading students have the following parameter(s):
- int grades[n]: the grades before rounding
Returns
- int[n]: the grades after rounding as appropriate
Input Format
The first line contains a single integer, n, the number of students.
Each line I of the n subsequent lines contains a single integer, grades[I].
Constraints
- 1 < n < 60
- 0 < Grades[I] < 100
Sample Input 0
4 73 67 38 33
Sample Output 0
75 67 40 33
Grading Students C Solution
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int n,i,j;
scanf("%d",&n);
int a[n];
for(i = 0; i < n; i++){
scanf("%d",&a[i]);
// your code goes here
}
for(i=0;i<n;i++)
{
if(a[i]<38)
printf("%d\n",a[i]);
else{
int x=a[i];
for(j=1;j<=5;j++)
{
x++;
if(x%5==0)
break;
}
if((x-a[i])<3)
a[i]=x;
printf("%d\n",a[i]);}
}
return 0;
}
Grading Students C++ Solution
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
typedef long long llong;
typedef pair<int, int> pii;
void solve() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int val;
cin >> val;
int next_mult_5 = val / 5 * 5 + 5;
int diff = abs(next_mult_5 - val);
if (diff < 3 && next_mult_5 >= 40) val = next_mult_5;
cout << val << endl;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifdef LOCAL
ifstream in("in");
cin.rdbuf(in.rdbuf());
#endif
solve();
return 0;
}
Grading Students C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void Main(String[] args) {
int n = Convert.ToInt32(Console.ReadLine());
for(int a0 = 0; a0 < n; a0++){
int grade = Convert.ToInt32(Console.ReadLine());
// your code goes here
int rounded = Round(grade);
Console.WriteLine(rounded);
}
}
static int Round(int grade)
{
if (grade < 38)
return grade;
int diff = grade % 5;
if (diff == 3 || diff == 4)
{
grade += 5 - diff;
}
return grade;
}
}
Grading Students Java Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'gradingStudents' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts INTEGER_ARRAY grades as parameter.
*/
public static List<Integer> gradingStudents(List<Integer> grades) {
// Write your code here
List<Integer> result = new ArrayList<>();
for(Integer valor: grades) {
if(valor >= 38) {
for(int i = 1; i < 3; i++) {
String mult = Integer.toString(valor + i);
if (mult.charAt(mult.length()-1) == '0' | mult.charAt(mult.length()-1) == '5') {
result.add(Integer.parseInt(mult));
break;
}
if (i + 1 == 3) {
result.add(valor);
break;
}
}
}
else {
result.add(valor);
}
}
return(result);
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int gradesCount = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> grades = IntStream.range(0, gradesCount).mapToObj(i -> {
try {
return bufferedReader.readLine().replaceAll("\\s+$", "");
} catch (IOException ex) {
throw new RuntimeException(ex);
}
})
.map(String::trim)
.map(Integer::parseInt)
.collect(toList());
List<Integer> result = Result.gradingStudents(grades);
bufferedWriter.write(
result.stream()
.map(Object::toString)
.collect(joining("\n"))
+ "\n"
);
bufferedReader.close();
bufferedWriter.close();
}
}
Grading Students JavaScript Solution
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var n = parseInt(readLine());
for(var a0 = 0; a0 < n; a0++){
var grade = parseInt(readLine());
// your code goes here
var modu = grade % 5;
if(modu >= 3 && grade >= 38){
console.log(grade + 5 - modu);
}
else{
console.log(grade);
}
}
}
Grading Students Python solution
#!/bin/python3
import sys
n = int(input().strip())
for a0 in range(n):
grade = int(input().strip())
if ( grade % 5 >= 3):
x = (int(grade/5)+1)*5
if ( x >= 40):
grade = x
print(grade)
Other Solutions