Hackerrank – Problem Statement
A description of the problem can be found on Hackerrank.
Solution
We will use professor’s rules and transform all grades:
– We find how many times (n
) is 5
in given grade
– if it is divisible without a remainder, we do nothing
– it not we round it to neareast greater number disible by 5
– (n + 1) * 5
– do nothing with grades less than 38
– return the result
I created solution in:
All solutions are also available on my GitHub profile.
Scala
1 2 3 4 5 6 7 8 9 10 11 12 |
object GradingStudents extends App { val grades = Source.stdin.getLines().toList.tail.map(_.toInt) val finalGrades = grades.map(professorGradeRule) println(finalGrades.mkString("\n")) def professorGradeRule(grade: Int): Int = { val multiply5 = grade / 5 val roundBy5 = if(grade % 5 == 0) multiply5 * 5 else (multiply5 + 1) * 5 if(roundBy5 - grade < 3 && grade >= 38) roundBy5 else grade } } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.*; public class GradingStudents { private static int professorGradeRule(int grade) { int multiply5 = grade / 5; int roundBy5 = (grade % 5 == 0) ? (multiply5 * 5) : ((multiply5 + 1) * 5); if(roundBy5 - grade < 3 && grade >= 38) { return roundBy5; } return grade; } public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int tests = Integer.parseInt(stdin.nextLine()); for(int i = 0; i < tests; i++) { int grade = Integer.parseInt(stdin.nextLine()); int finalGrade = professorGradeRule(grade); System.out.println(finalGrade); } stdin.close(); } } |
Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
'use strict'; const professorGradeRule = (grade) => { const multiply5 = parseInt(grade / 5); const roundBy5 = (grade % 5 == 0) ? (multiply5 * 5) : ((multiply5 + 1) * 5); if(roundBy5 - grade < 3 && grade >= 38) { return roundBy5; } return grade; } const processData = input => { const grades = input.split('\n').map(i => parseInt(i)); for(let i = 1; i < grades.length; i++) { const grade = grades[i]; const finalGrade = professorGradeRule(grade); console.log(finalGrade); } }; process.stdin.resume(); process.stdin.setEncoding("ascii"); let _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 |
def professor_grade_rule(grade) multiply5 = grade / 5 roundBy5 = (grade % 5 == 0) ? (multiply5 * 5) : ((multiply5 + 1) * 5) return roundBy5 if(roundBy5 - grade < 3 && grade >= 38) return grade end n = gets.to_i n.times do grade = gets.to_i final_grade = professor_grade_rule(grade) puts final_grade end |