Grading Students Hackerrank problem solution -coderInme

At HackerLand University, a passing grade is any grade 40 points or higher on a 100 point scale. Sam is a professor at the university and likes to round each student’s grade according to the following rules:

  • If the difference between the grade and the next higher multiple of 5 is less than 3, round to the next higher multiple of 5
  • If the grade is less than 38, don’t bother as it’s still a failing grade

Automate the rounding process then round a list of grades and print the results.

Input Format

First Line

  • integer
    • n: number of students
    • 1<=n<=60

Next Line

  • integer
    • grades i: individual grades
    • 0<=grades i<=100

Output Format

Print n lines, each with the rounded value of a student’s grade in input order.

Sample Input 0

4
73
67
38
33

Sample Output 0

75
67
40
33

Explanation 0

The first grade, 73 is two below the next higher multiple of 5, so it rounds to 75.
67 is 3 points less than the next higher multiple of 5 so it doesn’t round.
38, like 73, rounds up to next higher multiple of 5, or 40 in this case.
33 is less than 38, so it does not round.

solutions of Grading Students Hackerrank problem

The difference between the student’s grade and the next multiple of 5 will be less than 3 if grade mode 5 >=3. For example, let’s consider values for the grade in the inclusive interval from 70 through 75:

  •  70 mod 5 =0  because 5 evenly divides 70, so this would remain unchanged.
  • 71 mod 5 =1 , so this would remain unchanged.
  • 72 mod 5 =2 , so this would remain unchanged.
  • 71 mod 5 =3 , so this would be rounded up to the next multiple of 5.
  • 71 mod 5 =4 , so this would be rounded up to the next multiple of 5.
  • 75 mod 5 =0 , so this would remain unchanged.

If you perform the same calculations for the grade interval from 75 through 80, you’ll see the same results.

this means we have to check wether grade >38 and it’s remainder is >3 or not, or we can say we can check that difference between next highest multiple of 5 and grade is <3  then we will round it else print the same.

C++

#include <iostream>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    int n,a[60];
    cin>>n;
    for(int i=0;i<n; i++){
        cin>>a[i];
        if(a[i]>=38){ // grade > 38
            int div=(a[i]/5)+1; // 73/5= 14 +1= 15
            int next=div*5; // // next higher multiple of 5
            if(next-a[i]<3) // diff less than 3
                cout<<next<<endl;
            else
                cout<<a[i]<<endl;
        
        }
        else
            cout<<a[i]<<endl;
    }
    return 0;
}
c++ alternate using mod
#include <bits/stdc++.h>
using namespace std;

void solution() {
     int n, x;
     cin>>n;
     for(int i=0; i<n; i++){
        cin>>x;
        if(x>=38 and x%5>=3){
            while(x%5!=0){
               x++;
            }
        }
        cout<<x<<endl;
     }
}

int main () {
    solution();
    return 0;
}

Python 2

n = int(raw_input().strip())

for a0 in xrange(n):
    grade = int(raw_input().strip())

    if grade>=38 and grade%5>=3:
        while grade%5!=0:
            grade = grade + 1
    print grade

Java

import java.util.*;

public class Solution {

    public static int getRoundedGrade(int grade) {
        if (grade >= 38) {
            int mod5 = grade % 5;
            if (mod5 > 2) {
                grade += 5 - mod5;
            }
        }

        return grade;
    }

    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();
            System.out.println(getRoundedGrade(grade));
        }
        in.close();
    }
}

 

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.