--> Grading Students - Java HackerRank Solution

Grading Students – Java HackerRank Solution

grading students pic

Summary

The “Grading Students” problem requires you to adjust grades according to specific rounding rules to help students pass. By following the steps outlined, you can easily determine the final grades for all students.

Problem Statement

Given a list of grading students, apply the following rounding rules:

  1. If the grade is less than 38, it remains unchanged (since it’s a failing grade).
  2. If the grade is 38 or higher, round it up to the next multiple of 5 if the difference between the grade and the next multiple of 5 is less than 3.

Steps to Solve the Grading Students

  1. Input Reading
    • You will receive an integer n (number of students).
    • Then, you will receive n grades (one for each student).
  2. Process Each Grade
    • For each grade, determine if it is less than 38.
      • If it is, leave it unchanged.
    • If it is 38 or more, find the next multiple of 5.
      • If the difference between the grade and this multiple is less than 3, round up the grade to this multiple.
      • Otherwise, leave the grade unchanged.
  3. Output the Adjusted Grades
    • Print each adjusted grade.

Examples:

  1. Example 1
    • Input: 73
    • Next multiple of 5: 75
    • Difference: 75 – 73 = 2
    • Adjusted Grade: 75 (since the difference is less than 3)
  2. Example 2
    • Input: 67
    • Next multiple of 5: 70
    • Difference: 70 – 67 = 3
    • Adjusted Grade: 67 (since the difference is not less than 3)
  3. Example 3
    • Input: 38
    • Next multiple of 5: 40
    • Difference: 40 – 38 = 2
    • Adjusted Grade: 40 (since the difference is less than 3)
  4. Example 4
    • Input: 33
    • Adjusted Grade: 33 (since it is less than 38)
grading students page 1
grading students page 2
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 should return an INTEGER_ARRAY.
     * The function accepts INTEGER_ARRAY grades as parameter.
     */

    public static List<Integer> gradingStudents(List<Integer> grades) {
    // Write your code here
        for(int i=0; i<grades.size(); i++){
            int value = grades.get(i);
            if(value > 37){
                for(int j=value+1; j<=value+2; j++){
                    if(j%5==0){
                        grades.set(i, j);
                        break;
                    }
                }
            }
        }
        return grades;
    }

}

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();
    }
}
The Problem Solution

Leave a Reply

Your email address will not be published. Required fields are marked *