HackerRank Grading Students Solution

Hello Programmers, In this post, you will know how to solve the HackerRank Grading Students Solution. This problem is a part of the HackerRank Algorithms Series.

HackerRank Grading Students Solution
HackerRank Grading Students Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Grading Students Solution

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 studentgrade according to these rules:

  • If the difference between the grade and the next multiple of 5 is less than 3, round grade up to the next multiple of 5.
  • If the value of grade is less than 38, no rounding occurs as 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 gradingStudents in the editor below.

gradingStudents has 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

Explanation 0

image
  1. Student 1 received a 73, and the next multiple of 5 from 73 is 75. Since 75 – 73 < 3, the student’s grade is rounded to 75.
  2. Student 2 received a 67, and the next multiple of 5 from 67 is 70. Since 70 – 67 = 3, the grade will not be modified and the student’s final grade is 67.
  3. Student 3 received a 38, and the next multiple of 5 from 38 is 40. Since 40 – 38 < 3, the student’s grade will be rounded to 40.
  4. Student 4 received a grade below 33, so the grade will not be modified and the student’s final grade is 33.

HackerRank Grading Students Solution

Grading Students Solution in C

#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; 
    scanf("%d",&n);
    for(int a0 = 0; a0 < n; a0++){
        int grade; 
        scanf("%d",&grade);
        // your code goes here
        int x = (grade+4)/5;
        x *= 5;
        if(x>=40 && x-grade<3)grade=x;
        printf("%d\n",grade);
    }
    return 0;
}

Grading Students Solution in Cpp

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
int main(){
    int n;
    cin >> n;
    for(int a0 = 0; a0 < n; a0++){
        int grade;
        cin >> grade;
        if (grade >= 38) {
            int rem = grade % 5;
            if (rem >= 3) grade += 5 - rem;
        }
        cout << grade << endl;
    }
    return 0;
}

Grading Students Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for(int a0 = 0; a0 < n; a0++){
            int grade = in.nextInt();
            if (grade >= 38) {
                if (grade % 5 >= 3) {
                    grade += 5 - (grade % 5);
                }
            }
            System.out.println(grade);
        }
    }
}
Ezoicreport this ad

Grading Students Solution in Python

#!/bin/python3
import sys
n = int(input().strip())
for a0 in range(n):
    x = int(input().strip())
    
    if x >= 38:
        if x % 5 > 2:
            while x % 5 != 0: x += 1
    print(x)

Grading Students Solution using JavaScript

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());
        if (grade > 37 && grade % 5 > 2)
            grade += 5 - grade % 5;
        process.stdout.write(grade + "\n");
    }
}

Grading Students Solution in Scala

object Solution {
    def main(args: Array[String]) {
        val sc = new java.util.Scanner (System.in);
        var n = sc.nextInt();
        var a0 = 0;
        while(a0 < n){
            var grade = sc.nextInt();
            // your code goes here
            if(grade >= 38 && grade % 5 != 0) {
                   var d = 5 - (grade % 5)
                   if(d < 3) grade = grade + d 
                }
            println(grade)
            a0+=1;
        }
    }
}

Ezoicreport this adGrading Students Solution in Pascal

(* Enter your code here. Read input from STDIN. Print output to STDOUT *)
var i,n,a:longint;
begin
    readln(n);
    for i:=1 to n do
     begin
        readln(a);
        if a<38 then writeln(a) else
        if (a div 5)*5+5-a<3 then writeln((a div 5)*5+5) else writeln(a);
     end;
end.

Disclaimer: This problem (Grading Students) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Mini Max Sum Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad