CodingSpace

[HackerRank/Algorithms] Warmup - Grading Students 본문

HackerRank/Algorithm

[HackerRank/Algorithms] Warmup - Grading Students

개발자_조이킴 2022. 8. 24. 21:00

Problem. Warmup - Grading Students


Link.

https://www.hackerrank.com/challenges/grading/problem?isFullScreen=true 

 

Grading Students | HackerRank

Round student grades according to Sam's rules.

www.hackerrank.com


Description.

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

  • If the difference between the  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.

Given the initial value of grade for each of Sam's n students, write code to automate the rounding process.

 


Key Point. 


My Answer. 

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
    inputString += inputStdin;
});

process.stdin.on('end', function() {
    inputString = inputString.split('\n');

    main();
});

function readLine() {
    return inputString[currentLine++];
}

/*
 * Complete the 'gradingStudents' function below.
 *
 * The function is expected to return an INTEGER_ARRAY.
 * The function accepts INTEGER_ARRAY grades as parameter.
 */

function gradingStudents(grades) {
    // Write your code here
    let answer = [];
    
    for(let i = 0; i < grades.length; i++) {
        if(grades[i] < 38 ||  (Math.ceil(grades[i] / 5) * 5) - grades[i] >= 3)
            answer.push(grades[i]);
        else
            answer.push(Math.ceil(grades[i] / 5) * 5);
    }
    return answer;
}

function main() {
    const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const gradesCount = parseInt(readLine().trim(), 10);

    let grades = [];

    for (let i = 0; i < gradesCount; i++) {
        const gradesItem = parseInt(readLine().trim(), 10);
        grades.push(gradesItem);
    }

    const result = gradingStudents(grades);

    ws.write(result.join('\n') + '\n');

    ws.end();
}

References. 

 

Comments